|
1 | | -import importlib.resources |
2 | | -import json |
3 | 1 | import logging |
4 | 2 |
|
5 | 3 | from collections.abc import Awaitable, Callable |
|
36 | 34 | logger = logging.getLogger(__name__) |
37 | 35 |
|
38 | 36 |
|
39 | | -class A2AFastAPI(FastAPI): |
40 | | - """A FastAPI application that adds A2A-specific OpenAPI components.""" |
41 | | - |
42 | | - _a2a_components_added: bool = False |
43 | | - |
44 | | - def openapi(self) -> dict[str, Any]: |
45 | | - """Generates the OpenAPI schema for the application.""" |
46 | | - if self.openapi_schema: |
47 | | - return self.openapi_schema |
48 | | - |
49 | | - # Try to use the a2a.json schema generated from the proto file |
50 | | - # if available, instead of generating one from the python types. |
51 | | - try: |
52 | | - from a2a import types # noqa: PLC0415 |
53 | | - |
54 | | - schema_file = importlib.resources.files(types).joinpath('a2a.json') |
55 | | - if schema_file.is_file(): |
56 | | - self.openapi_schema = json.loads( |
57 | | - schema_file.read_text(encoding='utf-8') |
58 | | - ) |
59 | | - if self.openapi_schema: |
60 | | - return self.openapi_schema |
61 | | - except Exception: # noqa: BLE001 |
62 | | - logger.warning( |
63 | | - "Could not load 'a2a.json' from 'a2a.types'. Falling back to auto-generation." |
64 | | - ) |
65 | | - |
66 | | - openapi_schema = super().openapi() |
67 | | - if not self._a2a_components_added: |
68 | | - # A2ARequest is now a Union type of proto messages, so we can't use |
69 | | - # model_json_schema. Instead, we just mark it as added without |
70 | | - # adding the schema since proto types don't have Pydantic schemas. |
71 | | - # The OpenAPI schema will still be functional for the endpoints. |
72 | | - self._a2a_components_added = True |
73 | | - return openapi_schema |
74 | | - |
75 | | - |
76 | 37 | class A2AFastAPIApplication(JSONRPCApplication): |
77 | 38 | """A FastAPI application implementing the A2A protocol server endpoints. |
78 | 39 |
|
@@ -180,7 +141,7 @@ def build( |
180 | 141 | Returns: |
181 | 142 | A configured FastAPI application instance. |
182 | 143 | """ |
183 | | - app = A2AFastAPI(**kwargs) |
| 144 | + app = FastAPI(**kwargs) |
184 | 145 |
|
185 | 146 | self.add_routes_to_app(app, agent_card_url, rpc_url) |
186 | 147 |
|
|
0 commit comments