forked from a2aproject/a2a-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest_handler.py
More file actions
182 lines (165 loc) · 6.71 KB
/
request_handler.py
File metadata and controls
182 lines (165 loc) · 6.71 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
import logging
import typing
from collections.abc import AsyncIterable
from a2a.compat.v0_3 import conversions
from a2a.compat.v0_3 import types as types_v03
from a2a.server.context import ServerCallContext
from a2a.server.request_handlers.request_handler import RequestHandler
from a2a.types.a2a_pb2 import Task
from a2a.utils import proto_utils as core_proto_utils
from a2a.utils.errors import TaskNotFoundError
logger = logging.getLogger(__name__)
class RequestHandler03:
"""A protocol-agnostic v0.3 RequestHandler that delegates to the v1.0 RequestHandler."""
def __init__(self, request_handler: RequestHandler):
self.request_handler = request_handler
async def on_message_send(
self,
request: types_v03.SendMessageRequest,
context: ServerCallContext,
) -> types_v03.Task | types_v03.Message:
"""Sends a message using v0.3 protocol types."""
v10_req = conversions.to_core_send_message_request(request)
task_or_message = await self.request_handler.on_message_send(
v10_req, context
)
if isinstance(task_or_message, Task):
return conversions.to_compat_task(task_or_message)
return conversions.to_compat_message(task_or_message)
async def on_message_send_stream(
self,
request: types_v03.SendMessageRequest,
context: ServerCallContext,
) -> AsyncIterable[types_v03.SendStreamingMessageSuccessResponse]:
"""Sends a message stream using v0.3 protocol types."""
v10_req = conversions.to_core_send_message_request(request)
async for event in self.request_handler.on_message_send_stream(
v10_req, context
):
v10_stream_resp = core_proto_utils.to_stream_response(event)
yield conversions.to_compat_stream_response(
v10_stream_resp, request.id
)
async def on_cancel_task(
self,
request: types_v03.CancelTaskRequest,
context: ServerCallContext,
) -> types_v03.Task:
"""Cancels a task using v0.3 protocol types."""
v10_req = conversions.to_core_cancel_task_request(request)
v10_task = await self.request_handler.on_cancel_task(v10_req, context)
if v10_task:
return conversions.to_compat_task(v10_task)
raise TaskNotFoundError
async def on_subscribe_to_task(
self,
request: types_v03.TaskResubscriptionRequest,
context: ServerCallContext,
) -> AsyncIterable[types_v03.SendStreamingMessageSuccessResponse]:
"""Subscribes to a task using v0.3 protocol types."""
v10_req = conversions.to_core_subscribe_to_task_request(request)
async for event in self.request_handler.on_subscribe_to_task(
v10_req, context
):
v10_stream_resp = core_proto_utils.to_stream_response(event)
yield conversions.to_compat_stream_response(
v10_stream_resp, request.id
)
async def on_get_task_push_notification_config(
self,
request: types_v03.GetTaskPushNotificationConfigRequest,
context: ServerCallContext,
) -> types_v03.TaskPushNotificationConfig:
"""Gets a push notification config using v0.3 protocol types."""
v10_req = conversions.to_core_get_task_push_notification_config_request(
request
)
v10_config = (
await self.request_handler.on_get_task_push_notification_config(
v10_req, context
)
)
return conversions.to_compat_task_push_notification_config(v10_config)
async def on_create_task_push_notification_config(
self,
request: types_v03.SetTaskPushNotificationConfigRequest,
context: ServerCallContext,
) -> types_v03.TaskPushNotificationConfig:
"""Creates a push notification config using v0.3 protocol types."""
v10_req = (
conversions.to_core_create_task_push_notification_config_request(
request
)
)
v10_config = (
await self.request_handler.on_create_task_push_notification_config(
v10_req, context
)
)
return conversions.to_compat_task_push_notification_config(v10_config)
async def on_get_task(
self,
request: types_v03.GetTaskRequest,
context: ServerCallContext,
) -> types_v03.Task:
"""Gets a task using v0.3 protocol types."""
v10_req = conversions.to_core_get_task_request(request)
v10_task = await self.request_handler.on_get_task(v10_req, context)
if v10_task:
return conversions.to_compat_task(v10_task)
raise TaskNotFoundError
async def on_list_task_push_notification_configs(
self,
request: types_v03.ListTaskPushNotificationConfigRequest,
context: ServerCallContext,
) -> list[types_v03.TaskPushNotificationConfig]:
"""Lists push notification configs using v0.3 protocol types."""
v10_req = (
conversions.to_core_list_task_push_notification_config_request(
request
)
)
v10_resp = (
await self.request_handler.on_list_task_push_notification_configs(
v10_req, context
)
)
v03_resp = (
conversions.to_compat_list_task_push_notification_config_response(
v10_resp, request.id
)
)
if isinstance(
v03_resp.root,
types_v03.ListTaskPushNotificationConfigSuccessResponse,
):
return typing.cast(
'list[types_v03.TaskPushNotificationConfig]',
v03_resp.root.result,
)
return []
async def on_delete_task_push_notification_config(
self,
request: types_v03.DeleteTaskPushNotificationConfigRequest,
context: ServerCallContext,
) -> None:
"""Deletes a push notification config using v0.3 protocol types."""
v10_req = (
conversions.to_core_delete_task_push_notification_config_request(
request
)
)
await self.request_handler.on_delete_task_push_notification_config(
v10_req, context
)
async def on_get_extended_agent_card(
self,
request: types_v03.GetAuthenticatedExtendedCardRequest,
context: ServerCallContext,
) -> types_v03.AgentCard:
"""Gets the authenticated extended agent card using v0.3 protocol types."""
v10_req = conversions.to_core_get_extended_agent_card_request(request)
v10_card = await self.request_handler.on_get_extended_agent_card(
v10_req, context
)
return conversions.to_compat_agent_card(v10_card)