|
4 | 4 | import logging |
5 | 5 | import traceback |
6 | 6 |
|
7 | | -from abc import ABC, abstractmethod |
8 | 7 | from collections.abc import AsyncGenerator, Awaitable, Callable |
9 | 8 | from typing import TYPE_CHECKING, Any |
10 | 9 |
|
11 | 10 | from google.protobuf.json_format import MessageToDict, ParseDict |
12 | 11 | from jsonrpc.jsonrpc2 import JSONRPC20Request, JSONRPC20Response |
13 | 12 |
|
14 | | -from a2a.auth.user import UnauthenticatedUser |
15 | | -from a2a.auth.user import User as A2AUser |
16 | 13 | from a2a.compat.v0_3.jsonrpc_adapter import JSONRPC03Adapter |
17 | 14 | from a2a.extensions.common import ( |
18 | 15 | HTTP_EXTENSION_HEADER, |
19 | | - get_requested_extensions, |
20 | 16 | ) |
21 | 17 | from a2a.server.context import ServerCallContext |
22 | 18 | from a2a.server.jsonrpc_models import ( |
|
31 | 27 | from a2a.server.request_handlers.response_helpers import ( |
32 | 28 | build_error_response, |
33 | 29 | ) |
| 30 | +from a2a.server.routes.common import ( |
| 31 | + CallContextBuilder, |
| 32 | + DefaultCallContextBuilder, |
| 33 | +) |
34 | 34 | from a2a.types import A2ARequest |
35 | 35 | from a2a.types.a2a_pb2 import ( |
36 | 36 | AgentCard, |
|
113 | 113 | HTTP_413_CONTENT_TOO_LARGE = Any |
114 | 114 |
|
115 | 115 |
|
116 | | -class StarletteUserProxy(A2AUser): |
117 | | - """Adapts the Starlette User class to the A2A user representation.""" |
118 | | - |
119 | | - def __init__(self, user: BaseUser): |
120 | | - self._user = user |
121 | | - |
122 | | - @property |
123 | | - def is_authenticated(self) -> bool: |
124 | | - """Returns whether the current user is authenticated.""" |
125 | | - return self._user.is_authenticated |
126 | | - |
127 | | - @property |
128 | | - def user_name(self) -> str: |
129 | | - """Returns the user name of the current user.""" |
130 | | - return self._user.display_name |
131 | | - |
132 | | - |
133 | | -class CallContextBuilder(ABC): |
134 | | - """A class for building ServerCallContexts using the Starlette Request.""" |
135 | | - |
136 | | - @abstractmethod |
137 | | - def build(self, request: Request) -> ServerCallContext: |
138 | | - """Builds a ServerCallContext from a Starlette Request.""" |
139 | | - |
140 | | - |
141 | | -class DefaultCallContextBuilder(CallContextBuilder): |
142 | | - """A default implementation of CallContextBuilder.""" |
143 | | - |
144 | | - def build(self, request: Request) -> ServerCallContext: |
145 | | - """Builds a ServerCallContext from a Starlette Request. |
146 | | -
|
147 | | - Args: |
148 | | - request: The incoming Starlette Request object. |
149 | | -
|
150 | | - Returns: |
151 | | - A ServerCallContext instance populated with user and state |
152 | | - information from the request. |
153 | | - """ |
154 | | - user: A2AUser = UnauthenticatedUser() |
155 | | - state = {} |
156 | | - if 'user' in request.scope: |
157 | | - user = StarletteUserProxy(request.user) |
158 | | - state['auth'] = request.auth |
159 | | - state['headers'] = dict(request.headers) |
160 | | - return ServerCallContext( |
161 | | - user=user, |
162 | | - state=state, |
163 | | - requested_extensions=get_requested_extensions( |
164 | | - request.headers.getlist(HTTP_EXTENSION_HEADER) |
165 | | - ), |
166 | | - ) |
167 | | - |
168 | | - |
169 | 116 | @trace_class(kind=SpanKind.SERVER) |
170 | 117 | class JsonRpcDispatcher: |
171 | 118 | """Base class for A2A JSONRPC applications. |
|
0 commit comments