-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathjsonrpc_routes.py
More file actions
68 lines (53 loc) · 2.09 KB
/
Copy pathjsonrpc_routes.py
File metadata and controls
68 lines (53 loc) · 2.09 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
import logging
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from starlette.routing import Route
_package_starlette_installed = True
else:
try:
from starlette.routing import Route
_package_starlette_installed = True
except ImportError:
Route = Any
_package_starlette_installed = False
from a2a.server.request_handlers.request_handler import RequestHandler
from a2a.server.routes.common import ServerCallContextBuilder
from a2a.server.routes.jsonrpc_dispatcher import JsonRpcDispatcher
logger = logging.getLogger(__name__)
def create_jsonrpc_routes(
request_handler: RequestHandler,
rpc_url: str,
context_builder: ServerCallContextBuilder | None = None,
enable_v0_3_compat: bool = False,
) -> list['Route']:
"""Creates the Starlette Route for the A2A protocol JSON-RPC endpoint.
Handles incoming JSON-RPC requests, routes them to the appropriate
handler methods, and manages response generation including Server-Sent Events
(SSE).
Args:
request_handler: The handler instance responsible for processing A2A
requests via http.
rpc_url: The URL prefix for the RPC endpoints. Should start with a leading slash '/'.
context_builder: The ServerCallContextBuilder used to construct the
ServerCallContext passed to the request_handler. If None the
DefaultServerCallContextBuilder is used.
enable_v0_3_compat: Whether to enable v0.3 backward compatibility on the same endpoint.
"""
if not _package_starlette_installed:
raise ImportError(
'The `starlette` package is required to use `create_jsonrpc_routes`.'
' It can be added as a part of `a2a-sdk` optional dependencies,'
' `a2a-sdk[http-server]`.'
)
dispatcher = JsonRpcDispatcher(
request_handler=request_handler,
context_builder=context_builder,
enable_v0_3_compat=enable_v0_3_compat,
)
return [
Route(
path=rpc_url,
endpoint=dispatcher.handle_requests,
methods=['POST'],
)
]