类class
agentscope.app.middlewareProtocolMiddlewareBase
class ProtocolMiddlewareBase(BaseHTTPMiddleware, ABC)
源码Source
agentscope/app/middleware/_protocol/_base.py:17–244协议中间件基类,承载智能体的部署、调度与服务化运行。位于 agentscope.app.middleware 包,继承自 BaseHTTPMiddleware, ABC。核心成员包括 dispatch。
This middleware intercepts text/event-stream responses, deserializes
AgentEvent objects from SSE data: frames, and converts them to a
specific protocol format.
Subclasses should implement the _convert_to_protocol method to define
the conversion logic for their specific protocol (e.g., AGUI, A2A).
示例Example
class AGUIMiddleware(ProtocolMiddlewareBase):
def _convert_to_protocol(self, event: AgentEvent) -> dict:
# Implement AGUI-specific conversion logic
return {...}
app = FastAPI()
app.add_middleware(AGUIMiddleware)
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
def __init__(app: ASGIApp) -> None | 初始化 `ProtocolMiddlewareBase` 实例。 Initialize the protocol middleware. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
async def dispatch(request: Request, call_next: Callable) -> Responseasync | 执行 `dispatch` 方法。 Process the request and convert AgentEvent stream to protocol format. |
构造器详情Constructor Detail
ProtocolMiddlewareBase
def __init__(app: ASGIApp) -> None
初始化 ProtocolMiddlewareBase 实例。
Initialize the protocol middleware.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
app | ASGIApp | 应用(类型 `ASGIApp`)。The ASGI application to wrap. |
源码Source
agentscope/app/middleware/_protocol/_base.py:39–45方法详情Method Detail
dispatch
async def dispatch(request: Request, call_next: Callable) -> Response
async
执行 dispatch 方法。
Process the request and convert AgentEvent stream to protocol format.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
request | Request | 请求(类型 `Request`)。The incoming HTTP request. |
call_next | Callable | 下一个中间件或端点处理器。The next middleware or endpoint handler. |
Response
返回响应。The response, potentially with converted stream content.
源码Source
agentscope/app/middleware/_protocol/_base.py:47–83