CompactionMiddleware
public class CompactionMiddleware implements HarnessRuntimeMiddleware
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
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
| 构造器Constructor | 说明Description |
|---|---|
public CompactionMiddleware(WorkspaceManager workspaceManager, Model model, CompactionConfig config) | 初始化 `CompactionMiddleware` 实例。 Creates a new instance. |
方法摘要Method Summary
构造器详情Constructor Detail
CompactionMiddleware
public CompactionMiddleware(WorkspaceManager workspaceManager, Model model, CompactionConfig config)
初始化 CompactionMiddleware 实例。
Creates a new instance.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
workspaceManager | WorkspaceManager | 工作区管理器(类型 `WorkspaceManager`)。The workspace manager. |
model | Model | 模型实例(类型 `Model`)。The model. |
config | CompactionConfig | 配置对象(类型 `CompactionConfig`)。The config. |
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.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | Agent | 智能体(类型 `Agent`)。The agent. |
ctx | RuntimeContext | 运行时上下文(类型 `RuntimeContext`)。The ctx. |
input | ReasoningInput | 输入消息(类型 `ReasoningInput`)。The input. |
next | Function<ReasoningInput, Flux<AgentEvent>> | 下一个(类型 `Function >`)。The next. |
Flux<AgentEvent>io/agentscope/harness/agent/middleware/CompactionMiddleware.java:75–150