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

ToolOffloadMiddleware

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_managerBackgroundTaskManagerbg 管理器(类型 `BackgroundTaskManager`)。Application background task manager.
message_busMessageBus消息总线(类型 `MessageBus`)。Application message bus.
user_idstr当前请求的用户标识。User id of the current request.
agent_idstr智能体标识(类型 `str`)。Agent record id (not the display name).
timeout_secs默认default 10.0float超时 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
agentAgent智能体(类型 `Agent`)。The executing agent.
input_kwargsdict输入关键字参数(类型 `dict`)。Acting input kwargs (contains `tool_call`).
next_handlerCallable[..., AsyncGenerator]下游中间件或`_acting_impl`。The downstream middleware or `_acting_impl`.
返回Returns AsyncGenerator
返回AsyncGenerator。`ToolChunk | ToolResponse`:
源码Source agentscope/app/middleware/_tool_offload_middleware.py:105–394

继承成员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.