Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/a2a/compat/v0_3/grpc_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ async def send_message(
resp_proto = await self.stub.SendMessage(
req_proto,
metadata=self._get_grpc_metadata(context),
timeout=self._get_grpc_timeout(context),
)

which = resp_proto.WhichOneof('payload')
Expand Down Expand Up @@ -181,6 +182,7 @@ async def send_message_streaming(
stream = self.stub.SendStreamingMessage(
req_proto,
metadata=self._get_grpc_metadata(context),
timeout=self._get_grpc_timeout(context),
)
while True:
response = await stream.read()
Expand All @@ -207,6 +209,7 @@ async def subscribe(
stream = self.stub.TaskSubscription(
req_proto,
metadata=self._get_grpc_metadata(context),
timeout=self._get_grpc_timeout(context),
)
while True:
response = await stream.read()
Expand All @@ -233,6 +236,7 @@ async def get_task(
resp_proto = await self.stub.GetTask(
req_proto,
metadata=self._get_grpc_metadata(context),
timeout=self._get_grpc_timeout(context),
)
return conversions.to_core_task(proto_utils.FromProto.task(resp_proto))

Expand Down Expand Up @@ -261,6 +265,7 @@ async def cancel_task(
resp_proto = await self.stub.CancelTask(
req_proto,
metadata=self._get_grpc_metadata(context),
timeout=self._get_grpc_timeout(context),
)
return conversions.to_core_task(proto_utils.FromProto.task(resp_proto))

Expand All @@ -287,6 +292,7 @@ async def create_task_push_notification_config(
resp_proto = await self.stub.CreateTaskPushNotificationConfig(
req_proto,
metadata=self._get_grpc_metadata(context),
timeout=self._get_grpc_timeout(context),
)
return conversions.to_core_task_push_notification_config(
proto_utils.FromProto.task_push_notification_config(resp_proto)
Expand All @@ -306,6 +312,7 @@ async def get_task_push_notification_config(
resp_proto = await self.stub.GetTaskPushNotificationConfig(
req_proto,
metadata=self._get_grpc_metadata(context),
timeout=self._get_grpc_timeout(context),
)
return conversions.to_core_task_push_notification_config(
proto_utils.FromProto.task_push_notification_config(resp_proto)
Expand All @@ -325,6 +332,7 @@ async def list_task_push_notification_configs(
resp_proto = await self.stub.ListTaskPushNotificationConfig(
req_proto,
metadata=self._get_grpc_metadata(context),
timeout=self._get_grpc_timeout(context),
)
return conversions.to_core_list_task_push_notification_config_response(
proto_utils.FromProto.list_task_push_notification_config_response(
Expand All @@ -346,6 +354,7 @@ async def delete_task_push_notification_config(
await self.stub.DeleteTaskPushNotificationConfig(
req_proto,
metadata=self._get_grpc_metadata(context),
timeout=self._get_grpc_timeout(context),
)

@_handle_grpc_exception
Expand All @@ -360,6 +369,7 @@ async def get_extended_agent_card(
resp_proto = await self.stub.GetAgentCard(
req_proto,
metadata=self._get_grpc_metadata(context),
timeout=self._get_grpc_timeout(context),
)
card = conversions.to_core_agent_card(
proto_utils.FromProto.agent_card(resp_proto)
Expand Down Expand Up @@ -387,3 +397,9 @@ def _get_grpc_metadata(
metadata.append((key.lower(), value))

return metadata

def _get_grpc_timeout(
self, context: ClientCallContext | None
) -> float | None:
"""Returns the per-call deadline in seconds, if one is set."""
return context.timeout if context else None
127 changes: 126 additions & 1 deletion tests/compat/v0_3/test_grpc_transport.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections.abc import Callable
from unittest.mock import AsyncMock, MagicMock

import grpc
Expand All @@ -9,14 +10,21 @@
from a2a.compat.v0_3 import a2a_v0_3_pb2
from a2a.compat.v0_3.grpc_transport import CompatGrpcTransport
from a2a.types.a2a_pb2 import (
CancelTaskRequest,
DeleteTaskPushNotificationConfigRequest,
GetExtendedAgentCardRequest,
GetTaskPushNotificationConfigRequest,
GetTaskRequest,
ListTaskPushNotificationConfigsRequest,
Message,
Role,
SendMessageRequest,
SendMessageResponse,
SubscribeToTaskRequest,
TaskPushNotificationConfig,
)
from a2a.utils.errors import TaskNotFoundError, UnsupportedOperationError
from google.protobuf import any_pb2
from google.protobuf import any_pb2, empty_pb2
from google.rpc import error_details_pb2, status_pb2


Expand Down Expand Up @@ -153,3 +161,120 @@ def test_compat_grpc_transport_no_extension_metadata():

assert 'a2a-extensions' not in metadata
assert 'x-a2a-extensions' not in metadata


def _unary(response: object) -> AsyncMock:
return AsyncMock(return_value=response)


def _stream() -> MagicMock:
call = MagicMock()
call.read = AsyncMock(return_value=grpc.aio.EOF)
return MagicMock(return_value=call)


_PUSH_CONFIG_NAME = 'tasks/t1/pushNotificationConfigs/c1'


@pytest.mark.parametrize(
('method', 'stub_method', 'stub', 'request_obj'),
[
(
'send_message',
'SendMessage',
lambda: _unary(a2a_v0_3_pb2.SendMessageResponse()),
SendMessageRequest(message=Message(message_id='m1')),
),
(
'send_message_streaming',
'SendStreamingMessage',
_stream,
SendMessageRequest(message=Message(message_id='m1')),
),
(
'subscribe',
'TaskSubscription',
_stream,
SubscribeToTaskRequest(id='t1'),
),
(
'get_task',
'GetTask',
lambda: _unary(a2a_v0_3_pb2.Task(id='t1')),
GetTaskRequest(id='t1'),
),
(
'cancel_task',
'CancelTask',
lambda: _unary(a2a_v0_3_pb2.Task(id='t1')),
CancelTaskRequest(id='t1'),
),
(
'create_task_push_notification_config',
'CreateTaskPushNotificationConfig',
lambda: _unary(
a2a_v0_3_pb2.TaskPushNotificationConfig(name=_PUSH_CONFIG_NAME)
),
TaskPushNotificationConfig(
task_id='t1', id='c1', url='https://example.com/hook'
),
),
(
'get_task_push_notification_config',
'GetTaskPushNotificationConfig',
lambda: _unary(
a2a_v0_3_pb2.TaskPushNotificationConfig(name=_PUSH_CONFIG_NAME)
),
GetTaskPushNotificationConfigRequest(task_id='t1', id='c1'),
),
(
'list_task_push_notification_configs',
'ListTaskPushNotificationConfig',
lambda: _unary(
a2a_v0_3_pb2.ListTaskPushNotificationConfigResponse()
),
ListTaskPushNotificationConfigsRequest(task_id='t1'),
),
(
'delete_task_push_notification_config',
'DeleteTaskPushNotificationConfig',
lambda: _unary(empty_pb2.Empty()),
DeleteTaskPushNotificationConfigRequest(task_id='t1', id='c1'),
),
(
'get_extended_agent_card',
'GetAgentCard',
lambda: _unary(
a2a_v0_3_pb2.AgentCard(name='agent', url='http://agent')
),
GetExtendedAgentCardRequest(),
),
],
)
@pytest.mark.asyncio
async def test_compat_grpc_transport_passes_context_timeout(
method: str,
stub_method: str,
stub: Callable[[], MagicMock],
request_obj: object,
) -> None:
"""The per-call timeout in ClientCallContext must reach the gRPC stub,
as it does for the v1.0 GrpcTransport, or a call to a slow v0.3 server
never times out."""
transport = CompatGrpcTransport(
channel=AsyncMock(spec=Channel), agent_card=None
)
transport.stub = MagicMock()
stub_mock = stub()
setattr(transport.stub, stub_method, stub_mock)
context = ClientCallContext(timeout=12.5)

result = getattr(transport, method)(request_obj, context=context)
if hasattr(result, '__aiter__'):
async for _ in result:
pass
else:
await result

stub_mock.assert_called_once()
assert stub_mock.call_args.kwargs['timeout'] == 12.5
Loading