AS AgentScopeAPI 手册 / API Reference
类class agentscope.app.middleware

ProtocolMiddlewareBase

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
appASGIApp应用(类型 `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
requestRequest请求(类型 `Request`)。The incoming HTTP request.
call_nextCallable下一个中间件或端点处理器。The next middleware or endpoint handler.
返回Returns Response
返回响应。The response, potentially with converted stream content.
源码Source agentscope/app/middleware/_protocol/_base.py:47–83
基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.