-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathservice_parameters.py
More file actions
64 lines (48 loc) · 2 KB
/
service_parameters.py
File metadata and controls
64 lines (48 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from collections.abc import Callable
from typing import TypeAlias
from a2a.extensions.common import (
HTTP_EXTENSION_HEADER,
get_requested_extensions,
)
ServiceParameters: TypeAlias = dict[str, str]
ServiceParametersUpdate: TypeAlias = Callable[[ServiceParameters], None]
class ServiceParametersFactory:
"""Factory for creating ServiceParameters."""
@staticmethod
def create(updates: list[ServiceParametersUpdate]) -> ServiceParameters:
"""Create ServiceParameters from a list of updates.
Args:
updates: List of update functions to apply.
Returns:
The created ServiceParameters dictionary.
"""
return ServiceParametersFactory.create_from(None, updates)
@staticmethod
def create_from(
service_parameters: ServiceParameters | None,
updates: list[ServiceParametersUpdate],
) -> ServiceParameters:
"""Create new ServiceParameters from existing ones and apply updates.
Args:
service_parameters: Optional existing ServiceParameters to start from.
updates: List of update functions to apply.
Returns:
New ServiceParameters dictionary.
"""
result = service_parameters.copy() if service_parameters else {}
for update in updates:
update(result)
return result
def with_a2a_extensions(extensions: list[str]) -> ServiceParametersUpdate:
"""Create a ServiceParametersUpdate that merges A2A extension URIs.
Unions the supplied URIs with any already present in the A2A-Extensions
parameter, deduplicating and emitting them in sorted order. Repeated
calls accumulate rather than overwrite.
"""
def update(parameters: ServiceParameters) -> None:
if not extensions:
return
existing = parameters.get(HTTP_EXTENSION_HEADER, '')
merged = sorted(get_requested_extensions([existing, *extensions]))
parameters[HTTP_EXTENSION_HEADER] = ','.join(merged)
return update