|
36 | 36 | logger = logging.getLogger(__name__) |
37 | 37 |
|
38 | 38 |
|
39 | | -class JsonRpcRoutes: |
40 | | - """Provides the Starlette Route for the A2A protocol JSON-RPC endpoint. |
| 39 | +def create_jsonrpc_routes( # noqa: PLR0913 |
| 40 | + agent_card: AgentCard, |
| 41 | + request_handler: RequestHandler, |
| 42 | + extended_agent_card: AgentCard | None = None, |
| 43 | + context_builder: CallContextBuilder | None = None, |
| 44 | + card_modifier: Callable[[AgentCard], Awaitable[AgentCard] | AgentCard] |
| 45 | + | None = None, |
| 46 | + extended_card_modifier: Callable[ |
| 47 | + [AgentCard, ServerCallContext], Awaitable[AgentCard] | AgentCard |
| 48 | + ] |
| 49 | + | None = None, |
| 50 | + enable_v0_3_compat: bool = False, |
| 51 | + rpc_url: str = DEFAULT_RPC_URL, |
| 52 | +) -> list['Route']: |
| 53 | + """Creates the Starlette Route for the A2A protocol JSON-RPC endpoint. |
41 | 54 |
|
42 | 55 | Handles incoming JSON-RPC requests, routes them to the appropriate |
43 | 56 | handler methods, and manages response generation including Server-Sent Events |
44 | 57 | (SSE). |
45 | | - """ |
46 | 58 |
|
47 | | - def __init__( # noqa: PLR0913 |
48 | | - self, |
49 | | - agent_card: AgentCard, |
50 | | - request_handler: RequestHandler, |
51 | | - extended_agent_card: AgentCard | None = None, |
52 | | - context_builder: CallContextBuilder | None = None, |
53 | | - card_modifier: Callable[[AgentCard], Awaitable[AgentCard] | AgentCard] |
54 | | - | None = None, |
55 | | - extended_card_modifier: Callable[ |
56 | | - [AgentCard, ServerCallContext], Awaitable[AgentCard] | AgentCard |
57 | | - ] |
58 | | - | None = None, |
59 | | - enable_v0_3_compat: bool = False, |
60 | | - rpc_url: str = DEFAULT_RPC_URL, |
61 | | - middleware: Sequence[Middleware] | None = None, |
62 | | - ) -> None: |
63 | | - """Initializes the JsonRpcRoute. |
64 | | -
|
65 | | - Args: |
66 | | - agent_card: The AgentCard describing the agent's capabilities. |
67 | | - request_handler: The handler instance responsible for processing A2A |
68 | | - requests via http. |
69 | | - extended_agent_card: An optional, distinct AgentCard to be served |
70 | | - at the authenticated extended card endpoint. |
71 | | - context_builder: The CallContextBuilder used to construct the |
72 | | - ServerCallContext passed to the request_handler. If None, no |
73 | | - ServerCallContext is passed. |
74 | | - card_modifier: An optional callback to dynamically modify the public |
75 | | - agent card before it is served. |
76 | | - extended_card_modifier: An optional callback to dynamically modify |
77 | | - the extended agent card before it is served. It receives the |
78 | | - call context. |
79 | | - enable_v0_3_compat: Whether to enable v0.3 backward compatibility on the same endpoint. |
80 | | - rpc_url: The URL prefix for the RPC endpoints. |
81 | | - middleware: An optional list of Starlette middleware to apply to the routes. |
82 | | - """ |
83 | | - if not _package_starlette_installed: |
84 | | - raise ImportError( |
85 | | - 'The `starlette` package is required to use the `JsonRpcRoutes`.' |
86 | | - ' It can be added as a part of `a2a-sdk` optional dependencies,' |
87 | | - ' `a2a-sdk[http-server]`.' |
88 | | - ) |
89 | | - |
90 | | - self.dispatcher = JsonRpcDispatcher( |
91 | | - agent_card=agent_card, |
92 | | - http_handler=request_handler, |
93 | | - extended_agent_card=extended_agent_card, |
94 | | - context_builder=context_builder, |
95 | | - card_modifier=card_modifier, |
96 | | - extended_card_modifier=extended_card_modifier, |
97 | | - enable_v0_3_compat=enable_v0_3_compat, |
| 59 | + Args: |
| 60 | + agent_card: The AgentCard describing the agent's capabilities. |
| 61 | + request_handler: The handler instance responsible for processing A2A |
| 62 | + requests via http. |
| 63 | + extended_agent_card: An optional, distinct AgentCard to be served |
| 64 | + at the authenticated extended card endpoint. |
| 65 | + context_builder: The CallContextBuilder used to construct the |
| 66 | + ServerCallContext passed to the request_handler. If None, no |
| 67 | + ServerCallContext is passed. |
| 68 | + card_modifier: An optional callback to dynamically modify the public |
| 69 | + agent card before it is served. |
| 70 | + extended_card_modifier: An optional callback to dynamically modify |
| 71 | + the extended agent card before it is served. It receives the |
| 72 | + call context. |
| 73 | + enable_v0_3_compat: Whether to enable v0.3 backward compatibility on the same endpoint. |
| 74 | + rpc_url: The URL prefix for the RPC endpoints. |
| 75 | + """ |
| 76 | + if not _package_starlette_installed: |
| 77 | + raise ImportError( |
| 78 | + 'The `starlette` package is required to use `create_jsonrpc_routes`.' |
| 79 | + ' It can be added as a part of `a2a-sdk` optional dependencies,' |
| 80 | + ' `a2a-sdk[http-server]`.' |
98 | 81 | ) |
99 | 82 |
|
100 | | - self.routes = [ |
101 | | - Route( |
102 | | - path=rpc_url, |
103 | | - endpoint=self.dispatcher.handle_requests, |
104 | | - methods=['POST'], |
105 | | - middleware=middleware, |
106 | | - ) |
107 | | - ] |
| 83 | + dispatcher = JsonRpcDispatcher( |
| 84 | + agent_card=agent_card, |
| 85 | + http_handler=request_handler, |
| 86 | + extended_agent_card=extended_agent_card, |
| 87 | + context_builder=context_builder, |
| 88 | + card_modifier=card_modifier, |
| 89 | + extended_card_modifier=extended_card_modifier, |
| 90 | + enable_v0_3_compat=enable_v0_3_compat, |
| 91 | + ) |
| 92 | + |
| 93 | + return [ |
| 94 | + Route( |
| 95 | + path=rpc_url, |
| 96 | + endpoint=dispatcher.handle_requests, |
| 97 | + methods=['POST'], |
| 98 | + ) |
| 99 | + ] |
0 commit comments