接口 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):

Transformer/Pipeline Pattern (1 hook — sequential transform):

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"));
     }
 };
 
  • 方法详细资料

    • 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 instance
      ctx - 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 instance
      ctx - 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 instance
      ctx - 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 instance
      ctx - 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 instance
      ctx - per-call runtime context (session, user, attributes)
      currentPrompt - the current system prompt
      返回:
      the (possibly transformed) system prompt