接口 MiddlewareBase
- 所有已知实现类:
DynamicSkillMiddleware,GracefulShutdownMiddleware,OtelTracingMiddleware,TaskReminderMiddleware
public interface MiddlewareBase
Middleware provides interception mechanisms at 5 key execution points
in the Agent lifecycle.
Onion Pattern (4 hooks — wrap execution with before/after logic):
onAgent(io.agentscope.core.agent.Agent, io.agentscope.core.agent.RuntimeContext, io.agentscope.core.middleware.AgentInput, java.util.function.Function<io.agentscope.core.middleware.AgentInput, reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>>)— intercepts the entire agent invocationonReasoning(io.agentscope.core.agent.Agent, io.agentscope.core.agent.RuntimeContext, io.agentscope.core.middleware.ReasoningInput, java.util.function.Function<io.agentscope.core.middleware.ReasoningInput, reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>>)— intercepts the reasoning/model-call phaseonActing(io.agentscope.core.agent.Agent, io.agentscope.core.agent.RuntimeContext, io.agentscope.core.middleware.ActingInput, java.util.function.Function<io.agentscope.core.middleware.ActingInput, reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>>)— intercepts individual tool-call executiononModelCall(io.agentscope.core.agent.Agent, io.agentscope.core.agent.RuntimeContext, io.agentscope.core.middleware.ModelCallInput, java.util.function.Function<io.agentscope.core.middleware.ModelCallInput, reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>>)— intercepts the raw model API call
Transformer/Pipeline Pattern (1 hook — sequential transform):
onSystemPrompt(io.agentscope.core.agent.Agent, io.agentscope.core.agent.RuntimeContext, java.lang.String)— transforms the system prompt string
Each hook has a default implementation that delegates directly to
next, so subclasses only need to override the hooks they care about.
Example:
MiddlewareBase logging = new MiddlewareBase() {
@Override
public Flux<AgentEvent> onReasoning(
Agent agent, RuntimeContext ctx, ReasoningInput input,
Function<ReasoningInput, Flux<AgentEvent>> next) {
System.out.println("Before reasoning, session=" + ctx.getSessionId());
return next.apply(input)
.doOnComplete(() -> System.out.println("After reasoning"));
}
};
-
方法概要
修饰符和类型方法说明default reactor.core.publisher.Flux<AgentEvent> onActing(Agent agent, RuntimeContext ctx, ActingInput input, Function<ActingInput, reactor.core.publisher.Flux<AgentEvent>> next) Intercept the tool-call execution phase.default reactor.core.publisher.Flux<AgentEvent> onAgent(Agent agent, RuntimeContext ctx, AgentInput input, Function<AgentInput, reactor.core.publisher.Flux<AgentEvent>> next) Intercept the entire agent invocation.default reactor.core.publisher.Flux<AgentEvent> onModelCall(Agent agent, RuntimeContext ctx, ModelCallInput input, Function<ModelCallInput, reactor.core.publisher.Flux<AgentEvent>> next) Intercept the raw model API call.default reactor.core.publisher.Flux<AgentEvent> onReasoning(Agent agent, RuntimeContext ctx, ReasoningInput input, Function<ReasoningInput, reactor.core.publisher.Flux<AgentEvent>> next) Intercept the reasoning phase (LLM call + streaming output parsing).default reactor.core.publisher.Mono<String> onSystemPrompt(Agent agent, RuntimeContext ctx, String currentPrompt) Transform the system prompt string (pipeline pattern).
-
方法详细资料
-
onAgent
default reactor.core.publisher.Flux<AgentEvent> onAgent(Agent agent, RuntimeContext ctx, AgentInput input, Function<AgentInput, reactor.core.publisher.Flux<AgentEvent>> next) Intercept the entire agent invocation.- 参数:
agent- the agent instancectx- per-call runtime context (session, user, attributes)input- agent input (messages)next- calls the next middleware or the core agent logic- 返回:
- event stream from the agent invocation
-
onReasoning
default reactor.core.publisher.Flux<AgentEvent> onReasoning(Agent agent, RuntimeContext ctx, ReasoningInput input, Function<ReasoningInput, reactor.core.publisher.Flux<AgentEvent>> next) Intercept the reasoning phase (LLM call + streaming output parsing).- 参数:
agent- the agent instancectx- per-call runtime context (session, user, attributes)input- reasoning input (messages, tools, options)next- calls the next middleware or the core reasoning logic- 返回:
- event stream from reasoning
-
onActing
default reactor.core.publisher.Flux<AgentEvent> onActing(Agent agent, RuntimeContext ctx, ActingInput input, Function<ActingInput, reactor.core.publisher.Flux<AgentEvent>> next) Intercept the tool-call execution phase.- 参数:
agent- the agent instancectx- per-call runtime context (session, user, attributes)input- acting input (the tool calls)next- calls the next middleware or the core acting logic- 返回:
- event stream from acting
-
onModelCall
default reactor.core.publisher.Flux<AgentEvent> onModelCall(Agent agent, RuntimeContext ctx, ModelCallInput input, Function<ModelCallInput, reactor.core.publisher.Flux<AgentEvent>> next) Intercept the raw model API call.- 参数:
agent- the agent instancectx- per-call runtime context (session, user, attributes)input- model-call input (messages, tools, options, model)next- calls the next middleware or the actual model invocation- 返回:
- event stream from the model call
-
onSystemPrompt
default reactor.core.publisher.Mono<String> onSystemPrompt(Agent agent, RuntimeContext ctx, String currentPrompt) Transform the system prompt string (pipeline pattern).Multiple middlewares are applied sequentially; each receives the output of the previous one.
- 参数:
agent- the agent instancectx- per-call runtime context (session, user, attributes)currentPrompt- the current system prompt- 返回:
- the (possibly transformed) system prompt
-