AS AgentScopeAPI 手册 / API Reference

CompactionMiddleware

public class CompactionMiddleware implements HarnessRuntimeMiddleware
源码Source io/agentscope/harness/agent/middleware/CompactionMiddleware.java:60–234

压缩中间件,在智能体执行流程中提供横切扩展。位于 io.agentscope.harness.agent.middleware 包,实现 HarnessRuntimeMiddleware。核心成员包括 onReasoning、CompactionMiddleware。

Middleware that performs conversation compaction before each LLM reasoning call.

Fires on #onReasoning. 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 context (AgentState#contextMutable()) is replaced with [summaryMsg] + preservedTail.

  • 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

构造器Constructor说明Description
public CompactionMiddleware(WorkspaceManager workspaceManager, Model model, CompactionConfig config)
初始化 `CompactionMiddleware` 实例。
Creates a new instance.

方法摘要Method Summary

方法Method说明Description
public Flux<AgentEvent> onReasoning(Agent agent, RuntimeContext ctx, ReasoningInput input, Function<ReasoningInput, Flux<AgentEvent>> next)
处理推理事件。
Performs the on reasoning operation.

构造器详情Constructor Detail

CompactionMiddleware

public CompactionMiddleware(WorkspaceManager workspaceManager, Model model, CompactionConfig config)

初始化 CompactionMiddleware 实例。

Creates a new instance.

参数Parameters
名称Name类型Type说明Description
workspaceManagerWorkspaceManager工作区管理器(类型 `WorkspaceManager`)。The workspace manager.
modelModel模型实例(类型 `Model`)。The model.
configCompactionConfig配置对象(类型 `CompactionConfig`)。The config.
源码Source io/agentscope/harness/agent/middleware/CompactionMiddleware.java:68–73

方法详情Method Detail

onReasoning

public Flux<AgentEvent> onReasoning(Agent agent, RuntimeContext ctx, ReasoningInput input, Function<ReasoningInput, Flux<AgentEvent>> next)

处理推理事件。

Performs the on reasoning operation.

参数Parameters
名称Name类型Type说明Description
agentAgent智能体(类型 `Agent`)。The agent.
ctxRuntimeContext运行时上下文(类型 `RuntimeContext`)。The ctx.
inputReasoningInput输入消息(类型 `ReasoningInput`)。The input.
nextFunction<ReasoningInput, Flux<AgentEvent>>下一个(类型 `Function >`)。The next.
返回Returns Flux<AgentEvent>
返回Flux。Returns flux.
源码Source io/agentscope/harness/agent/middleware/CompactionMiddleware.java:75–150

继承成员Inherited Members

继承自Methods inherited from MiddlewareBase
基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.