agentscope.middlewareMiddlewareBase
class MiddlewareBase
agentscope/middleware/_base.py:13–303MiddlewareBase 提供回复、推理、行动、模型调用、权限检查、上下文压缩和系统提示词等钩子。中间件按“环绕”方式包装下一段逻辑,可在不修改智能体源码的情况下加入日志、审计、缓存或策略。位于 agentscope.middleware 包。核心成员包括 is_implemented、on_reply、on_reasoning、on_acting、on_check_permission、on_model_call。
Middleware provides interception mechanisms at 7 key execution points in the Agent lifecycle:
Onion Pattern Hooks (with before/after logic):
- on_reply: Intercepts the entire reply process
- on_reasoning: Intercepts the reasoning/model call phase
- on_check_permission: Intercepts permission checking for a tool call
- on_acting: Intercepts individual tool call execution
- on_model_call: Intercepts the raw model API call
- on_compress_context: Intercepts context compression
Transformer Pattern Hook (sequential pipeline):
- on_system_prompt: Transforms the system prompt string
Each hook is optional - only implement the ones you need. The middleware system will automatically detect which hooks are implemented at runtime.
示例Example
class LoggingMiddleware(MiddlewareBase):
async def on_reasoning(
self,
agent: Agent,
input_kwargs: dict,
next_handler: Callable[[], AsyncGenerator],
) -> AsyncGenerator:
print(f"Before reasoning for agent {agent.name}")
async for event in next_handler():
yield event
print(f"After reasoning for agent {agent.name}")
agent = Agent(
...
middlewares=[LoggingMiddleware()],
...
)
核心精讲Deep dive
MiddlewareBase:无侵入钩子MiddlewareBase: non-invasive hooks
MiddlewareBase 提供回复、推理、行动、模型调用、权限检查、上下文压缩和系统提示词等钩子。中间件按“环绕”方式包装下一段逻辑,可在不修改智能体源码的情况下加入日志、审计、缓存或策略。
MiddlewareBase exposes hooks for reply, reasoning, acting, model calls, permission checks, context compression and system prompts. Middleware wraps the next handler, adding logging, auditing, caching or policy without modifying agent source.
- 每个钩子都接收 agent、input_kwargs 与 next_handler。Every hook receives the agent, input_kwargs and the next_handler.
- 调用 next_handler 才会继续执行;不调用即可短路。Calling next_handler continues execution; skipping it short-circuits the pipeline.
- 多个中间件按注册顺序嵌套,适合观测与策略组合。Multiple middlewares nest in registration order, composing observation and policy.
记录推理阶段耗时Trace the reasoning phase
from collections.abc import AsyncGenerator, Callable
from agentscope.agent import Agent
from agentscope.middleware import MiddlewareBase
class LoggingMiddleware(MiddlewareBase):
async def on_reasoning(
self,
agent: Agent,
input_kwargs: dict,
next_handler: Callable[[], AsyncGenerator],
) -> AsyncGenerator:
print(f"Before reasoning for {agent.name}")
async for event in next_handler():
yield event
print(f"After reasoning for {agent.name}")方法摘要Method Summary
方法详情Method Detail
is_implemented
def is_implemented(hook_name: str) -> bool
判断是否 is_implemented。
Check if a hook method is implemented in the subclass.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
hook_name | str | 钩子方法到检查的名称。Name of the hook method to check |
agentscope/middleware/_base.py:55–66on_reply
async def on_reply(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGenerator
拦截并扩展回复流程。
Swallowing a completed reply's ReplyEndEvent (receiving it without yielding) forces another reasoning-acting round; the final Msg is only produced once the event escapes the whole chain.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | 'Agent' | 智能体(类型 `'Agent'`)。The Agent instance executing this middleware |
input_kwargs | dict | 输入关键字参数(类型 `dict`)。Dictionary containing: - inputs: Msg | list[Msg] | UserConfirmResultEvent | ExternalExecutionResultEvent | None — the unified inputs that trigger this reply (new message(s), a resumption event from a previous outside interaction, or None). |
next_handler | Callable[..., AsyncGenerator] | 下一个处理器(类型 `Callable[..., AsyncGenerator]`)。Callable that executes the next middleware or original method |
agentscope/middleware/_base.py:68–99on_reasoning
async def on_reasoning(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGenerator
拦截并扩展推理阶段。
Hook for intercepting the reasoning process.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | 'Agent' | 智能体(类型 `'Agent'`)。The Agent instance executing this middleware |
input_kwargs | dict | 输入关键字参数(类型 `dict`)。Dictionary containing: - tool_choice: ToolChoice (default None) |
next_handler | Callable[..., AsyncGenerator] | 下一个处理器(类型 `Callable[..., AsyncGenerator]`)。Callable that executes the next middleware or original method |
agentscope/middleware/_base.py:101–122on_acting
async def on_acting(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGenerator
拦截并扩展工具执行阶段。
This hook wraps only the toolkit.call_tool call — i.e.
说明Notes
- 注意:该行为受运行时配置或底层实现影响,建议结合对应源码与版本说明确认。Note: Tools with `is_state_injected=True` receive the live `agent.state` object. Offloading such tools to a background task may cause concurrent state mutations — guard against this in your middleware implementation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | 'Agent' | 智能体(类型 `'Agent'`)。The Agent instance executing this middleware. |
input_kwargs | dict | 输入关键字参数(类型 `dict`)。Dictionary containing: - `tool_call` (`ToolCallBlock`): the tool call to execute. By the time this hook is invoked the tool call has already been validated and permitted. |
next_handler | Callable[..., AsyncGenerator] | 下一个处理器(类型 `Callable[..., AsyncGenerator]`)。Callable that executes the next middleware or `_acting_impl`. |
agentscope/middleware/_base.py:124–168on_check_permission
async def on_check_permission(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., Awaitable['PermissionDecision']]) -> 'PermissionDecision'
处理检查权限事件。
This hook runs after the tool has been resolved and its input has been parsed and validated, but before the resulting decision is consumed by the agent.
说明Notes
- 注意:该行为受运行时配置或底层实现影响,建议结合对应源码与版本说明确认。Note: Returning without calling `next_handler` bypasses the built-in permission resolution for that call.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | 'Agent' | 智能体(类型 `'Agent'`)。The Agent instance performing the permission check. |
input_kwargs | dict | 输入关键字参数(类型 `dict`)。Dictionary containing: - `tool_call` (`ToolCallBlock`): validated call metadata used to correlate the permission decision. - `tool` (`ToolBase`): the resolved tool instance. - `tool_input` (`dict`): the parsed and validated input. |
next_handler | Callable[..., Awaitable['PermissionDecision']] | 下一个处理器(类型 `Callable[..., Awaitable['PermissionDecision']]`)。Callable that executes the next middleware or the built-in permission resolution. |
agentscope/middleware/_base.py:170–211on_model_call
async def on_model_call(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., Awaitable['ChatResponse' | AsyncGenerator['ChatResponse', None]]]) -> 'ChatResponse' | AsyncGenerator['ChatResponse', None]
拦截并扩展模型调用。
Hook for intercepting the model API call.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | 'Agent' | 智能体(类型 `'Agent'`)。The Agent instance executing this middleware |
input_kwargs | dict | 输入关键字参数(类型 `dict`)。Dictionary containing: - messages: list[Msg] - tools: list[dict] - tool_choice: ToolChoice - current_model: The model instance used for this call |
next_handler | Callable[..., Awaitable['ChatResponse' | AsyncGenerator['ChatResponse', None]]] | 下一个处理器(类型 `Callable[..., Awaitable['ChatResponse' | AsyncGenerator['ChatResponse', None]]]`)。Callable that executes the next middleware or original method |
agentscope/middleware/_base.py:213–239on_compress_context
async def on_compress_context(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., Awaitable[None]]) -> None
处理压缩上下文事件。
Onion hook for compress_context function in Agent class
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | 'Agent' | 智能体(类型 `'Agent'`)。The Agent instance executing this middleware |
input_kwargs | dict | 输入关键字参数(类型 `dict`)。Dictionary containing: - context_config: ContextConfig | None - instructions: HintBlock | None |
next_handler | Callable[..., Awaitable[None]] | 下一个处理器(类型 `Callable[..., Awaitable[None]]`)。Callable that executes the next middleware or original method |
agentscope/middleware/_base.py:241–262on_system_prompt
async def on_system_prompt(agent: 'Agent', current_prompt: str) -> str
拦截并修改系统提示词。
This uses a transformer/pipeline pattern rather than onion pattern.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | 'Agent' | 智能体(类型 `'Agent'`)。The Agent instance executing this middleware |
current_prompt | str | 当前提示词(类型 `str`)。The current system prompt string |
agentscope/middleware/_base.py:264–284list_tools
async def list_tools() -> list[ToolBase]
列出当前连接可用的工具。
List available tools provided by this middleware.
agentscope/middleware/_base.py:286–294get_middleware_key
async def get_middleware_key() -> str
获取中间件键。
Optionally, middleware classes can override this method to provide their own unique key.
agentscope/middleware/_base.py:296–303