Class CompactionMiddleware
java.lang.Object
io.agentscope.harness.agent.middleware.CompactionMiddleware
- All Implemented Interfaces:
io.agentscope.core.middleware.MiddlewareBase,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:
- Long-term memories are flushed from the prefix via
MemoryFlushManager. - The full conversation is offloaded to the session JSONL.
- The prefix is distilled into a structured summary via one LLM call.
- The agent's working
contextis replaced with[summaryMsg] + preservedTail. - The downstream
ReasoningInputis 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
ConstructorsConstructorDescriptionCompactionMiddleware(WorkspaceManager workspaceManager, io.agentscope.core.model.Model model, CompactionConfig config) -
Method Summary
Modifier and TypeMethodDescriptionreactor.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, waitMethods inherited from interface io.agentscope.core.middleware.MiddlewareBase
onActing, onAgent, onModelCall, onSystemPrompt, order
-
Constructor Details
-
CompactionMiddleware
public CompactionMiddleware(WorkspaceManager workspaceManager, io.agentscope.core.model.Model model, CompactionConfig config)
-
-
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:
onReasoningin interfaceio.agentscope.core.middleware.MiddlewareBase
-