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

StateChangeMiddleware

class StateChangeMiddleware(MiddlewareBase)
源码Source agentscope/app/middleware/_state_change_middleware.py:42–196

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

Detect state / team changes after each tool call and push notifications to the session event stream. Declared in agentscope.app.middleware. Extends MiddlewareBase.

构造器摘要Constructor Summary

构造器Constructor说明Description
def __init__(message_bus: MessageBus, session_id: str) -> None
初始化 `StateChangeMiddleware` 实例。
Initialise the middleware.

方法摘要Method Summary

方法Method说明Description
async def on_reply(agent: Any, input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGeneratorasync
拦截并扩展回复流程。
Wrap the whole reply turn to catch state changes that happen **outside** the ``on_acting`` tool-execution window.
async def on_acting(agent: Any, input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGeneratorasync
拦截并扩展工具执行阶段。
Wrap tool execution: snapshot state hash before, compare after, and push notifications if anything changed.

构造器详情Constructor Detail

StateChangeMiddleware

def __init__(message_bus: MessageBus, session_id: str) -> None

初始化 StateChangeMiddleware 实例。

Initialise the middleware.

参数Parameters
名称Name类型Type说明Description
message_busMessageBus消息总线(类型 `MessageBus`)。Application message bus.
session_idstr会话标识到发布事件用于。The session id to publish events for.
源码Source agentscope/app/middleware/_state_change_middleware.py:54–68

方法详情Method Detail

on_reply

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

拦截并扩展回复流程。

Permission rules added while handling a UserConfirmResultEvent (the user's "always allow" choice) mutate permission_context in _handle_incoming_event — which runs at the start of the reply turn, before the confirmed tool's on_acting snapshot is taken.

参数Parameters
名称Name类型Type说明Description
agentAny智能体(类型 `Any`)。The executing agent.
input_kwargsdict输入关键字参数(类型 `dict`)。The reply inputs (new message(s) or a resumption event).
next_handlerCallable[..., AsyncGenerator]下一个处理器(类型 `Callable[..., AsyncGenerator]`)。The downstream middleware or core reply logic.
返回Returns AsyncGenerator
返回AsyncGenerator。`AgentEvent | Msg` — unchanged from downstream.
源码Source agentscope/app/middleware/_state_change_middleware.py:117–152

on_acting

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

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

Wrap tool execution: snapshot state hash before, compare after, and push notifications if anything changed.

参数Parameters
名称Name类型Type说明Description
agentAny智能体(类型 `Any`)。The executing agent.
input_kwargsdict输入关键字参数(类型 `dict`)。Contains `tool_call` (`ToolCallBlock`).
next_handlerCallable[..., AsyncGenerator]下一个处理器(类型 `Callable[..., AsyncGenerator]`)。The downstream middleware or core acting logic.
返回Returns AsyncGenerator
返回AsyncGenerator。`ToolChunk | ToolResponse` — unchanged from downstream.
源码Source agentscope/app/middleware/_state_change_middleware.py:154–196

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