Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 045f8a4

Browse files
rajeshvelichetiGerrit Code Review
authored andcommitted
Merge "Validate agent capabilities before invoking api" into main
2 parents abf68d0 + 92a4445 commit 045f8a4

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/a2a/server/request_handlers/jsonrpc_handler.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
from collections.abc import AsyncIterable
44

55
from a2a.server.request_handlers.request_handler import RequestHandler
6-
from a2a.server.request_handlers.response_helpers import (
7-
prepare_response_object,
8-
)
6+
from a2a.server.request_handlers.response_helpers import prepare_response_object
97
from a2a.types import (
108
AgentCard,
119
CancelTaskRequest,
@@ -37,6 +35,7 @@
3735
TaskStatusUpdateEvent,
3836
)
3937
from a2a.utils.errors import ServerError
38+
from a2a.utils.helpers import validate
4039

4140

4241
logger = logging.getLogger(__name__)
@@ -83,6 +82,10 @@ async def on_message_send(
8382
)
8483

8584
# message/stream
85+
@validate(
86+
lambda self: self.agent_card.capabilities.streaming,
87+
'Streaming is not supported by the agent',
88+
)
8689
async def on_message_send_stream(
8790
self, request: SendStreamingMessageRequest
8891
) -> AsyncIterable[SendStreamingMessageResponse]:
@@ -183,6 +186,10 @@ async def get_push_notification(
183186
)
184187

185188
# tasks/pushNotification/set
189+
@validate(
190+
lambda self: self.agent_card.capabilities.pushNotifications,
191+
'Push notifications are not supported by the agent',
192+
)
186193
async def set_push_notification(
187194
self, request: SetTaskPushNotificationConfigRequest
188195
) -> SetTaskPushNotificationConfigResponse:

src/a2a/utils/helpers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
TaskStatus,
1313
TextPart,
1414
)
15+
from a2a.utils.errors import ServerError, UnsupportedOperationError
1516

1617

1718
logger = logging.getLogger(__name__)
@@ -82,3 +83,22 @@ def build_text_artifact(text: str, artifact_id: str) -> Artifact:
8283
text_part = TextPart(text=text)
8384
part = Part(root=text_part)
8485
return Artifact(parts=[part], artifactId=artifact_id)
86+
87+
88+
def validate(expression, error_message=None):
89+
"""Decorator that validates if the given expression evaluates to True."""
90+
91+
def decorator(function):
92+
def wrapper(self, *args, **kwargs):
93+
if not expression(self):
94+
if not error_message:
95+
message = str(expression)
96+
logger.error(f'Unsuppported Operation: {error_message}')
97+
raise ServerError(
98+
UnsupportedOperationError(message=error_message)
99+
)
100+
return function(self, *args, **kwargs)
101+
102+
return wrapper
103+
104+
return decorator

0 commit comments

Comments
 (0)