类class
agentscope.app.middlewareStateChangeMiddleware
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_bus | MessageBus | 消息总线(类型 `MessageBus`)。Application message bus. |
session_id | str | 会话标识到发布事件用于。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 |
|---|---|---|
agent | Any | 智能体(类型 `Any`)。The executing agent. |
input_kwargs | dict | 输入关键字参数(类型 `dict`)。The reply inputs (new message(s) or a resumption event). |
next_handler | Callable[..., AsyncGenerator] | 下一个处理器(类型 `Callable[..., AsyncGenerator]`)。The downstream middleware or core reply logic. |
AsyncGenerator
返回AsyncGenerator。`AgentEvent | Msg` — unchanged from downstream.
源码Source
agentscope/app/middleware/_state_change_middleware.py:117–152on_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 |
|---|---|---|
agent | Any | 智能体(类型 `Any`)。The executing agent. |
input_kwargs | dict | 输入关键字参数(类型 `dict`)。Contains `tool_call` (`ToolCallBlock`). |
next_handler | Callable[..., AsyncGenerator] | 下一个处理器(类型 `Callable[..., AsyncGenerator]`)。The downstream middleware or core acting logic. |
AsyncGenerator
返回AsyncGenerator。`ToolChunk | ToolResponse` — unchanged from downstream.
源码Source
agentscope/app/middleware/_state_change_middleware.py:154–196