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

TracingMiddleware

class TracingMiddleware(MiddlewareBase)
源码Source agentscope/middleware/_tracing/_trace.py:117–374

追踪中间件,在智能体执行流程中提供横切扩展。位于 agentscope.middleware 包,继承自 MiddlewareBase。核心成员包括 on_reply、on_model_call、on_acting。

When tracing has not been configured (setup_tracing was not called), every hook short-circuits to next_handler with near-zero overhead.

Example::

from agentscope.middleware import TracingMiddleware

agent = Agent(
    ...,
    middlewares=[TracingMiddleware()],
)

方法摘要Method Summary

方法Method说明Description
async def on_reply(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGeneratorasync
拦截并扩展回复流程。
Handles the reply event.
async def on_model_call(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., Awaitable['ChatResponse' | Asyn…async
拦截并扩展模型调用。
Handles the model call event.
async def on_acting(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGeneratorasync
拦截并扩展工具执行阶段。
Handles the acting event.

方法详情Method Detail

on_reply

async def on_reply(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGenerator
async

拦截并扩展回复流程。

Handles the reply event.

参数Parameters
名称Name类型Type说明Description
agent'Agent'智能体(类型 `'Agent'`)。The agent.
input_kwargsdict输入关键字参数(类型 `dict`)。The input kwargs.
next_handlerCallable[..., AsyncGenerator]下一个处理器(类型 `Callable[..., AsyncGenerator]`)。The next handler.
返回Returns AsyncGenerator
返回AsyncGenerator。Returns async generator.
源码Source agentscope/middleware/_tracing/_trace.py:137–254

on_model_call

async def on_model_call(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., Awaitable['ChatResponse' | AsyncGenerator['ChatResponse', None]]]) -> Union['ChatResponse', AsyncGenerator['ChatResponse', None]]
async

拦截并扩展模型调用。

Handles the model call event.

参数Parameters
名称Name类型Type说明Description
agent'Agent'智能体(类型 `'Agent'`)。The agent.
input_kwargsdict输入关键字参数(类型 `dict`)。The input kwargs.
next_handlerCallable[..., Awaitable['ChatResponse' | AsyncGenerator['ChatResponse', None]]]下一个处理器(类型 `Callable[..., Awaitable['ChatResponse' | AsyncGenerator['ChatResponse', None]]]`)。The next handler.
返回Returns Union['ChatResponse', AsyncGenerator['ChatResponse', None]]
返回Union['ChatResponse',AsyncGenerator['ChatResponse',None]]。Returns union chat response async generator chat response none.
源码Source agentscope/middleware/_tracing/_trace.py:259–307

on_acting

async def on_acting(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGenerator
async

拦截并扩展工具执行阶段。

Handles the acting event.

参数Parameters
名称Name类型Type说明Description
agent'Agent'智能体(类型 `'Agent'`)。The agent.
input_kwargsdict输入关键字参数(类型 `dict`)。The input kwargs.
next_handlerCallable[..., AsyncGenerator]下一个处理器(类型 `Callable[..., AsyncGenerator]`)。The next handler.
返回Returns AsyncGenerator
返回AsyncGenerator。Returns async generator.
源码Source agentscope/middleware/_tracing/_trace.py:312–374

继承成员Inherited Members

基于官方固定版本源码静态生成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.