类class
agentscope.app.middlewareToolOffloadMiddleware
class ToolOffloadMiddleware(MiddlewareBase)
源码Source
agentscope/app/middleware/_tool_offload_middleware.py:46–394工具卸载中间件,在智能体执行流程中提供横切扩展。位于 agentscope.app.middleware 包,继承自 MiddlewareBase。核心成员包括 on_acting。
Middleware that offloads timed-out tool calls to background tasks. Declared in agentscope.app.middleware. Extends MiddlewareBase.
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
def __init__(bg_manager: BackgroundTaskManager, message_bus: MessageBus, user_id: str, agent_id: str, timeout_secs: flo… | 初始化 `ToolOffloadMiddleware` 实例。 Bind dependencies. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
async def on_acting(agent: Agent, input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGeneratorasync | 拦截并扩展工具执行阶段。 Execute a tool with timeout; offload to background on expiry. |
构造器详情Constructor Detail
ToolOffloadMiddleware
def __init__(bg_manager: BackgroundTaskManager, message_bus: MessageBus, user_id: str, agent_id: str, timeout_secs: float = 10.0) -> None
初始化 ToolOffloadMiddleware 实例。
Bind dependencies.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
bg_manager | BackgroundTaskManager | bg 管理器(类型 `BackgroundTaskManager`)。Application background task manager. |
message_bus | MessageBus | 消息总线(类型 `MessageBus`)。Application message bus. |
user_id | str | 当前请求的用户标识。User id of the current request. |
agent_id | str | 智能体标识(类型 `str`)。Agent record id (not the display name). |
timeout_secs默认default 10.0 | float | 超时 secs(类型 `float`)。Tool execution timeout before offloading to background. |
源码Source
agentscope/app/middleware/_tool_offload_middleware.py:73–99方法详情Method Detail
on_acting
async def on_acting(agent: Agent, input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGenerator
async
拦截并扩展工具执行阶段。
The inner next_handler generator is wrapped in an :mod:asyncio task whose output is fed through a :class:asyncio.Queue.
说明Notes
- 注意:该行为受运行时配置或底层实现影响,建议结合对应源码与版本说明确认。Note: Tools with `is_state_injected=True` or `is_external_tool=True` bypass this logic and are always executed synchronously.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | Agent | 智能体(类型 `Agent`)。The executing agent. |
input_kwargs | dict | 输入关键字参数(类型 `dict`)。Acting input kwargs (contains `tool_call`). |
next_handler | Callable[..., AsyncGenerator] | 下游中间件或`_acting_impl`。The downstream middleware or `_acting_impl`. |
AsyncGenerator
返回AsyncGenerator。`ToolChunk | ToolResponse`:
源码Source
agentscope/app/middleware/_tool_offload_middleware.py:105–394