forked from a2aproject/a2a-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
309 lines (255 loc) · 11.1 KB
/
helpers.py
File metadata and controls
309 lines (255 loc) · 11.1 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"""General utility functions for the A2A Python SDK."""
import functools
import inspect
import logging
from collections.abc import Callable
from typing import Any
from uuid import uuid4
from a2a.types import (
Artifact,
MessageSendParams,
Part,
Task,
TaskArtifactUpdateEvent,
TaskState,
TaskStatus,
TextPart,
)
from a2a.utils.errors import ServerError, UnsupportedOperationError
from a2a.utils.telemetry import trace_function
logger = logging.getLogger(__name__)
@trace_function()
def create_task_obj(message_send_params: MessageSendParams) -> Task:
"""Create a new task object from message send params.
Generates UUIDs for task and context IDs if they are not already present in the message.
Args:
message_send_params: The `MessageSendParams` object containing the initial message.
Returns:
A new `Task` object initialized with 'submitted' status and the input message in history.
"""
if not message_send_params.message.context_id:
message_send_params.message.context_id = str(uuid4())
return Task(
id=str(uuid4()),
context_id=message_send_params.message.context_id,
status=TaskStatus(state=TaskState.submitted),
history=[message_send_params.message],
)
@trace_function()
def append_artifact_to_task(task: Task, event: TaskArtifactUpdateEvent) -> None:
"""Helper method for updating a Task object with new artifact data from an event.
Handles creating the artifacts list if it doesn't exist, adding new artifacts,
and appending parts to existing artifacts based on the `append` flag in the event.
Args:
task: The `Task` object to modify.
event: The `TaskArtifactUpdateEvent` containing the artifact data.
"""
if not task.artifacts:
task.artifacts = []
new_artifact_data: Artifact = event.artifact
artifact_id: str = new_artifact_data.artifact_id
append_parts: bool = event.append or False
existing_artifact: Artifact | None = None
existing_artifact_list_index: int | None = None
# Find existing artifact by its id
for i, art in enumerate(task.artifacts):
if art.artifact_id == artifact_id:
existing_artifact = art
existing_artifact_list_index = i
break
if not append_parts:
# This represents the first chunk for this artifact index.
if existing_artifact_list_index is not None:
# Replace the existing artifact entirely with the new data
logger.debug(
'Replacing artifact at id %s for task %s', artifact_id, task.id
)
task.artifacts[existing_artifact_list_index] = new_artifact_data
else:
# Append the new artifact since no artifact with this index exists yet
logger.debug(
'Adding new artifact with id %s for task %s',
artifact_id,
task.id,
)
task.artifacts.append(new_artifact_data)
elif existing_artifact:
# Append new parts to the existing artifact's part list
logger.debug(
'Appending parts to artifact id %s for task %s',
artifact_id,
task.id,
)
existing_artifact.parts.extend(new_artifact_data.parts)
else:
# We received a chunk to append, but we don't have an existing artifact.
# we will ignore this chunk
logger.warning(
'Received append=True for nonexistent artifact index %s in task %s. Ignoring chunk.',
artifact_id,
task.id,
)
def build_text_artifact(text: str, artifact_id: str) -> Artifact:
"""Helper to create a text artifact.
Args:
text: The text content for the artifact.
artifact_id: The ID for the artifact.
Returns:
An `Artifact` object containing a single `TextPart`.
"""
text_part = TextPart(text=text)
part = Part(root=text_part)
return Artifact(parts=[part], artifact_id=artifact_id)
def validate(
expression: Callable[[Any], bool], error_message: str | None = None
) -> Callable:
"""Decorator that validates if a given expression evaluates to True.
Typically used on class methods to check capabilities or configuration
before executing the method's logic. If the expression is False,
a `ServerError` with an `UnsupportedOperationError` is raised.
Args:
expression: A callable that takes the instance (`self`) as its argument
and returns a boolean.
error_message: An optional custom error message for the `UnsupportedOperationError`.
If None, the string representation of the expression will be used.
Examples:
Basic usage with capability check:
>>> class MyAgent:
... def __init__(self, streaming_enabled: bool):
... self.streaming_enabled = streaming_enabled
...
... @validate(lambda self: self.streaming_enabled)
... async def stream_response(self, message: str):
... return f'Streaming: {message}'
With custom error message:
>>> class SecureAgent:
... def __init__(self):
... self.auth_enabled = False
...
... @validate(
... lambda self: self.auth_enabled,
... 'Authentication must be enabled for this operation',
... )
... def secure_operation(self, data: str):
... return f'Processing secure data: {data}'
Error case example:
>>> from a2a.utils.errors import ServerError
>>> agent = SecureAgent()
>>> try:
... agent.secure_operation('secret')
... except ServerError as e:
... print(e.error.message)
Authentication must be enabled for this operation
Note:
This decorator works with both sync and async methods automatically.
"""
def decorator(function: Callable) -> Callable:
if inspect.iscoroutinefunction(function):
@functools.wraps(function)
async def async_wrapper(self: Any, *args, **kwargs) -> Any:
if not expression(self):
final_message = error_message or str(expression)
logger.error('Unsupported Operation: %s', final_message)
raise ServerError(
UnsupportedOperationError(message=final_message)
)
return await function(self, *args, **kwargs)
return async_wrapper
@functools.wraps(function)
def sync_wrapper(self: Any, *args, **kwargs) -> Any:
if not expression(self):
final_message = error_message or str(expression)
logger.error('Unsupported Operation: %s', final_message)
raise ServerError(
UnsupportedOperationError(message=final_message)
)
return function(self, *args, **kwargs)
return sync_wrapper
return decorator
def validate_async_generator(
expression: Callable[[Any], bool], error_message: str | None = None
):
"""Decorator that validates if a given expression evaluates to True for async generators.
Typically used on class methods to check capabilities or configuration
before executing the method's logic. If the expression is False,
a `ServerError` with an `UnsupportedOperationError` is raised.
Args:
expression: A callable that takes the instance (`self`) as its argument
and returns a boolean.
error_message: An optional custom error message for the `UnsupportedOperationError`.
If None, the string representation of the expression will be used.
Examples:
Streaming capability validation:
>>> class StreamingAgent:
... def __init__(self, streaming_enabled: bool):
... self.streaming_enabled = streaming_enabled
...
... @validate_async_generator(
... lambda self: self.streaming_enabled,
... 'Streaming is not supported by this agent',
... )
... async def stream_messages(self, count: int):
... for i in range(count):
... yield f'Message {i}'
Error case - validation fails:
>>> from a2a.utils.errors import ServerError
>>> import asyncio
>>>
>>> class FeatureAgent:
... def __init__(self):
... self.features = {'real_time': False}
...
... @validate_async_generator(
... lambda self: self.features.get('real_time', False),
... 'Real-time feature must be enabled to stream updates',
... )
... async def real_time_updates(self):
... yield 'This should not be yielded'
>>> async def run_test():
... agent = FeatureAgent()
... try:
... async for _ in agent.real_time_updates():
... pass
... except ServerError as e:
... print(e.error.message)
>>>
>>> asyncio.run(run_test())
Real-time feature must be enabled to stream updates
Note:
This decorator is specifically for async generator methods (async def with yield).
The validation happens before the generator starts yielding values.
"""
def decorator(function):
@functools.wraps(function)
async def wrapper(self, *args, **kwargs):
if not expression(self):
final_message = error_message or str(expression)
logger.error('Unsupported Operation: %s', final_message)
raise ServerError(
UnsupportedOperationError(message=final_message)
)
async for i in function(self, *args, **kwargs):
yield i
return wrapper
return decorator
def are_modalities_compatible(
server_output_modes: list[str] | None, client_output_modes: list[str] | None
) -> bool:
"""Checks if server and client output modalities (MIME types) are compatible.
Modalities are compatible if:
1. The client specifies no preferred output modes (client_output_modes is None or empty).
2. The server specifies no supported output modes (server_output_modes is None or empty).
3. There is at least one common modality between the server's supported list and the client's preferred list.
Args:
server_output_modes: A list of MIME types supported by the server/agent for output.
Can be None or empty if the server doesn't specify.
client_output_modes: A list of MIME types preferred by the client for output.
Can be None or empty if the client accepts any.
Returns:
True if the modalities are compatible, False otherwise.
"""
if client_output_modes is None or len(client_output_modes) == 0:
return True
if server_output_modes is None or len(server_output_modes) == 0:
return True
return any(x in server_output_modes for x in client_output_modes)