Class CompactionMiddleware

java.lang.Object
io.agentscope.harness.agent.middleware.CompactionMiddleware
All Implemented Interfaces:
io.agentscope.core.middleware.MiddlewareBase, HarnessRuntimeMiddleware

public class CompactionMiddleware extends Object implements HarnessRuntimeMiddleware
Middleware that performs conversation compaction before each LLM reasoning call.

Fires on onReasoning(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>>). When the compaction threshold is exceeded:

  1. Long-term memories are flushed from the prefix via MemoryFlushManager.
  2. The full conversation is offloaded to the session JSONL.
  3. The prefix is distilled into a structured summary via one LLM call.
  4. The agent's working context is replaced with [summaryMsg] + preservedTail.
  5. The downstream ReasoningInput is rebuilt with [systemMsg] + [summaryMsg] + preservedTail.

When CompactionConfig.getTriggerTokens() is 0 (dynamic mode, the default), the effective trigger threshold is computed as model.getContextWindowSize() - reserved. If the model does not report its context window, falls back to CompactionConfig.FALLBACK_TRIGGER_TOKENS.

  • Constructor Summary

    Constructors
    Constructor
    Description
    CompactionMiddleware(WorkspaceManager workspaceManager, io.agentscope.core.model.Model model, CompactionConfig config)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>
    onReasoning(io.agentscope.core.agent.Agent agent, io.agentscope.core.agent.RuntimeContext ctx, io.agentscope.core.middleware.ReasoningInput input, Function<io.agentscope.core.middleware.ReasoningInput,reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>> next)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface io.agentscope.core.middleware.MiddlewareBase

    onActing, onAgent, onModelCall, onSystemPrompt, order
  • Constructor Details

  • Method Details

    • onReasoning

      public reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent> onReasoning(io.agentscope.core.agent.Agent agent, io.agentscope.core.agent.RuntimeContext ctx, io.agentscope.core.middleware.ReasoningInput input, Function<io.agentscope.core.middleware.ReasoningInput,reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>> next)
      Specified by:
      onReasoning in interface io.agentscope.core.middleware.MiddlewareBase