类class
agentscope.middlewareTracingMiddleware
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_kwargs | dict | 输入关键字参数(类型 `dict`)。The input kwargs. |
next_handler | Callable[..., AsyncGenerator] | 下一个处理器(类型 `Callable[..., AsyncGenerator]`)。The next handler. |
AsyncGenerator
返回AsyncGenerator。Returns async generator.
源码Source
agentscope/middleware/_tracing/_trace.py:137–254on_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_kwargs | dict | 输入关键字参数(类型 `dict`)。The input kwargs. |
next_handler | Callable[..., Awaitable['ChatResponse' | AsyncGenerator['ChatResponse', None]]] | 下一个处理器(类型 `Callable[..., Awaitable['ChatResponse' | AsyncGenerator['ChatResponse', None]]]`)。The next handler. |
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–307on_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_kwargs | dict | 输入关键字参数(类型 `dict`)。The input kwargs. |
next_handler | Callable[..., AsyncGenerator] | 下一个处理器(类型 `Callable[..., AsyncGenerator]`)。The next handler. |
AsyncGenerator
返回AsyncGenerator。Returns async generator.
源码Source
agentscope/middleware/_tracing/_trace.py:312–374