|
7 | 7 | from collections.abc import AsyncGenerator |
8 | 8 | from typing import Any |
9 | 9 |
|
| 10 | +from fastapi import FastAPI |
10 | 11 | from pydantic import ValidationError |
11 | 12 | from sse_starlette.sse import EventSourceResponse |
12 | 13 | from starlette.applications import Starlette |
13 | 14 | from starlette.authentication import BaseUser |
14 | 15 | from starlette.requests import Request |
15 | 16 | from starlette.responses import JSONResponse, Response |
16 | | -from starlette.routing import Route |
17 | 17 |
|
18 | 18 | from a2a.auth.user import UnauthenticatedUser |
19 | 19 | from a2a.auth.user import User as A2AUser |
@@ -81,8 +81,8 @@ def build(self, request: Request) -> ServerCallContext: |
81 | 81 | return ServerCallContext(user=user, state=state) |
82 | 82 |
|
83 | 83 |
|
84 | | -class A2AStarletteApplication: |
85 | | - """A Starlette application implementing the A2A protocol server endpoints. |
| 84 | +class JSONRPCApplication(ABC): |
| 85 | + """Base class for A2A JSONRPC applications. |
86 | 86 |
|
87 | 87 | Handles incoming JSON-RPC requests, routes them to the appropriate |
88 | 88 | handler methods, and manages response generation including Server-Sent Events |
@@ -391,73 +391,23 @@ async def _handle_get_authenticated_extended_agent_card( |
391 | 391 | status_code=404, |
392 | 392 | ) |
393 | 393 |
|
394 | | - def routes( |
395 | | - self, |
396 | | - agent_card_url: str = '/.well-known/agent.json', |
397 | | - extended_agent_card_url: str = '/agent/authenticatedExtendedCard', |
398 | | - rpc_url: str = '/', |
399 | | - ) -> list[Route]: |
400 | | - """Returns the Starlette Routes for handling A2A requests. |
401 | | -
|
402 | | - Args: |
403 | | - agent_card_url: The URL path for the agent card endpoint. |
404 | | - rpc_url: The URL path for the A2A JSON-RPC endpoint (POST requests). |
405 | | - extended_agent_card_url: The URL for the authenticated extended agent card endpoint. |
406 | | -
|
407 | | - Returns: |
408 | | - A list of Starlette Route objects. |
409 | | - """ |
410 | | - app_routes = [ |
411 | | - Route( |
412 | | - rpc_url, |
413 | | - self._handle_requests, |
414 | | - methods=['POST'], |
415 | | - name='a2a_handler', |
416 | | - ), |
417 | | - Route( |
418 | | - agent_card_url, |
419 | | - self._handle_get_agent_card, |
420 | | - methods=['GET'], |
421 | | - name='agent_card', |
422 | | - ), |
423 | | - ] |
424 | | - |
425 | | - if self.agent_card.supportsAuthenticatedExtendedCard: |
426 | | - app_routes.append( |
427 | | - Route( |
428 | | - extended_agent_card_url, |
429 | | - self._handle_get_authenticated_extended_agent_card, |
430 | | - methods=['GET'], |
431 | | - name='authenticated_extended_agent_card', |
432 | | - ) |
433 | | - ) |
434 | | - return app_routes |
435 | | - |
| 394 | + @abstractmethod |
436 | 395 | def build( |
437 | 396 | self, |
438 | 397 | agent_card_url: str = '/.well-known/agent.json', |
439 | | - extended_agent_card_url: str = '/agent/authenticatedExtendedCard', |
440 | 398 | rpc_url: str = '/', |
441 | 399 | **kwargs: Any, |
442 | | - ) -> Starlette: |
443 | | - """Builds and returns the Starlette application instance. |
| 400 | + ) -> FastAPI | Starlette: |
| 401 | + """Builds and returns the JSONRPC application instance. |
444 | 402 |
|
445 | 403 | Args: |
446 | | - agent_card_url: The URL path for the agent card endpoint. |
447 | | - rpc_url: The URL path for the A2A JSON-RPC endpoint (POST requests). |
448 | | - extended_agent_card_url: The URL for the authenticated extended agent card endpoint. |
449 | | - **kwargs: Additional keyword arguments to pass to the Starlette |
450 | | - constructor. |
| 404 | + agent_card_url: The URL for the agent card endpoint. |
| 405 | + rpc_url: The URL for the A2A JSON-RPC endpoint |
| 406 | + **kwargs: Additional keyword arguments to pass to the FastAPI constructor. |
451 | 407 |
|
452 | 408 | Returns: |
453 | | - A configured Starlette application instance. |
| 409 | + A configured JSONRPC application instance. |
454 | 410 | """ |
455 | | - app_routes = self.routes( |
456 | | - agent_card_url, extended_agent_card_url, rpc_url |
| 411 | + raise NotImplementedError( |
| 412 | + 'Subclasses must implement the build method to create the application instance.' |
457 | 413 | ) |
458 | | - if 'routes' in kwargs: |
459 | | - kwargs['routes'].extend(app_routes) |
460 | | - else: |
461 | | - kwargs['routes'] = app_routes |
462 | | - |
463 | | - return Starlette(**kwargs) |
0 commit comments