|
| 1 | +import logging |
| 2 | + |
| 3 | +from collections.abc import AsyncGenerator |
| 4 | + |
| 5 | +import grpc |
| 6 | + |
| 7 | +from a2a.grpc import a2a_pb2, a2a_pb2_grpc |
| 8 | +from a2a.types import ( |
| 9 | + AgentCard, |
| 10 | + Message, |
| 11 | + MessageSendParams, |
| 12 | + Task, |
| 13 | + TaskArtifactUpdateEvent, |
| 14 | + TaskIdParams, |
| 15 | + TaskPushNotificationConfig, |
| 16 | + TaskQueryParams, |
| 17 | + TaskStatusUpdateEvent, |
| 18 | +) |
| 19 | +from a2a.utils import proto_utils |
| 20 | +from a2a.utils.telemetry import SpanKind, trace_class |
| 21 | + |
| 22 | + |
| 23 | +logger = logging.getLogger(__name__) |
| 24 | + |
| 25 | + |
| 26 | +@trace_class(kind=SpanKind.CLIENT) |
| 27 | +class A2AGrpcClient: |
| 28 | + """A2A Client for interacting with an A2A agent via gRPC.""" |
| 29 | + |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + grpc_stub: a2a_pb2_grpc.A2AServiceStub, |
| 33 | + agent_card: AgentCard, |
| 34 | + ): |
| 35 | + """Initializes the A2AGrpcClient. |
| 36 | +
|
| 37 | + Requires an `AgentCard` |
| 38 | +
|
| 39 | + Args: |
| 40 | + grpc_stub: A grpc client stub. |
| 41 | + agent_card: The agent card object. |
| 42 | + """ |
| 43 | + self.agent_card = agent_card |
| 44 | + self.stub = grpc_stub |
| 45 | + |
| 46 | + async def send_message( |
| 47 | + self, |
| 48 | + request: MessageSendParams, |
| 49 | + ) -> Task | Message: |
| 50 | + """Sends a non-streaming message request to the agent. |
| 51 | +
|
| 52 | + Args: |
| 53 | + request: The `MessageSendParams` object containing the message and configuration. |
| 54 | +
|
| 55 | + Returns: |
| 56 | + A `Task` or `Message` object containing the agent's response. |
| 57 | + """ |
| 58 | + response = await self.stub.SendMessage( |
| 59 | + a2a_pb2.SendMessageRequest( |
| 60 | + request=proto_utils.ToProto.message(request.message), |
| 61 | + configuration=proto_utils.ToProto.message_send_configuration( |
| 62 | + request.configuration |
| 63 | + ), |
| 64 | + metadata=proto_utils.ToProto.metadata(request.metadata), |
| 65 | + ) |
| 66 | + ) |
| 67 | + if response.task: |
| 68 | + return proto_utils.FromProto.task(response.task) |
| 69 | + return proto_utils.FromProto.message(response.msg) |
| 70 | + |
| 71 | + async def send_message_streaming( |
| 72 | + self, |
| 73 | + request: MessageSendParams, |
| 74 | + ) -> AsyncGenerator[ |
| 75 | + Message | Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent |
| 76 | + ]: |
| 77 | + """Sends a streaming message request to the agent and yields responses as they arrive. |
| 78 | +
|
| 79 | + This method uses gRPC streams to receive a stream of updates from the |
| 80 | + agent. |
| 81 | +
|
| 82 | + Args: |
| 83 | + request: The `MessageSendParams` object containing the message and configuration. |
| 84 | +
|
| 85 | + Yields: |
| 86 | + `Message` or `Task` or `TaskStatusUpdateEvent` or |
| 87 | + `TaskArtifactUpdateEvent` objects as they are received in the |
| 88 | + stream. |
| 89 | + """ |
| 90 | + stream = self.stub.SendStreamingMessage( |
| 91 | + a2a_pb2.SendMessageRequest( |
| 92 | + request=proto_utils.ToProto.message(request.message), |
| 93 | + configuration=proto_utils.ToProto.message_send_configuration( |
| 94 | + request.configuration |
| 95 | + ), |
| 96 | + metadata=proto_utils.ToProto.metadata(request.metadata), |
| 97 | + ) |
| 98 | + ) |
| 99 | + while True: |
| 100 | + response = await stream.read() |
| 101 | + if response == grpc.aio.EOF: |
| 102 | + break |
| 103 | + if response.HasField('msg'): |
| 104 | + yield proto_utils.FromProto.message(response.msg) |
| 105 | + elif response.HasField('task'): |
| 106 | + yield proto_utils.FromProto.task(response.task) |
| 107 | + elif response.HasField('status_update'): |
| 108 | + yield proto_utils.FromProto.task_status_update_event( |
| 109 | + response.status_update |
| 110 | + ) |
| 111 | + elif response.HasField('artifact_update'): |
| 112 | + yield proto_utils.FromProto.task_artifact_update_event( |
| 113 | + response.artifact_update |
| 114 | + ) |
| 115 | + |
| 116 | + async def get_task( |
| 117 | + self, |
| 118 | + request: TaskQueryParams, |
| 119 | + ) -> Task: |
| 120 | + """Retrieves the current state and history of a specific task. |
| 121 | +
|
| 122 | + Args: |
| 123 | + request: The `TaskQueryParams` object specifying the task ID |
| 124 | +
|
| 125 | + Returns: |
| 126 | + A `Task` object containing the Task or None. |
| 127 | + """ |
| 128 | + task = await self.stub.GetTask( |
| 129 | + a2a_pb2.GetTaskRequest(name=f'tasks/{request.id}') |
| 130 | + ) |
| 131 | + return proto_utils.FromProto.task(task) |
| 132 | + |
| 133 | + async def cancel_task( |
| 134 | + self, |
| 135 | + request: TaskIdParams, |
| 136 | + ) -> Task: |
| 137 | + """Requests the agent to cancel a specific task. |
| 138 | +
|
| 139 | + Args: |
| 140 | + request: The `TaskIdParams` object specifying the task ID. |
| 141 | +
|
| 142 | + Returns: |
| 143 | + A `Task` object containing the updated Task |
| 144 | + """ |
| 145 | + task = await self.stub.CancelTask( |
| 146 | + a2a_pb2.CancelTaskRequest(name=f'tasks/{request.id}') |
| 147 | + ) |
| 148 | + return proto_utils.FromProto.task(task) |
| 149 | + |
| 150 | + async def set_task_callback( |
| 151 | + self, |
| 152 | + request: TaskPushNotificationConfig, |
| 153 | + ) -> TaskPushNotificationConfig: |
| 154 | + """Sets or updates the push notification configuration for a specific task. |
| 155 | +
|
| 156 | + Args: |
| 157 | + request: The `TaskPushNotificationConfig` object specifying the task ID and configuration. |
| 158 | +
|
| 159 | + Returns: |
| 160 | + A `TaskPushNotificationConfig` object containing the config. |
| 161 | + """ |
| 162 | + config = await self.stub.CreateTaskPushNotification( |
| 163 | + a2a_pb2.CreateTaskPushNotificationRequest( |
| 164 | + parent='', |
| 165 | + config_id='', |
| 166 | + config=proto_utils.ToProto.task_push_notification_config( |
| 167 | + request |
| 168 | + ), |
| 169 | + ) |
| 170 | + ) |
| 171 | + return proto_utils.FromProto.task_push_notification_config(config) |
| 172 | + |
| 173 | + async def get_task_callback( |
| 174 | + self, |
| 175 | + request: TaskIdParams, # TODO: Update to a push id params |
| 176 | + ) -> TaskPushNotificationConfig: |
| 177 | + """Retrieves the push notification configuration for a specific task. |
| 178 | +
|
| 179 | + Args: |
| 180 | + request: The `TaskIdParams` object specifying the task ID. |
| 181 | +
|
| 182 | + Returns: |
| 183 | + A `TaskPushNotificationConfig` object containing the configuration. |
| 184 | + """ |
| 185 | + config = await self.stub.GetTaskPushNotification( |
| 186 | + a2a_pb2.GetTaskPushNotificationRequest( |
| 187 | + name=f'tasks/{request.id}/pushNotification/undefined', |
| 188 | + ) |
| 189 | + ) |
| 190 | + return proto_utils.FromProto.task_push_notification_config(config) |
0 commit comments