MemoryFlushMiddleware
public class MemoryFlushMiddleware implements HarnessRuntimeMiddleware
io/agentscope/harness/agent/middleware/MemoryFlushMiddleware.java:85–343记忆刷新中间件,在智能体执行流程中提供横切扩展。位于 io.agentscope.harness.agent.middleware 包,实现 HarnessRuntimeMiddleware。核心成员包括 onAgent、MemoryFlushMiddleware。
Middleware that triggers memory flush and message offload at the end of each agent call.
Runs in #onAgent's doOnComplete so long-term memories are extracted and
persisted after every call, even when conversation compaction was not triggered during that
call. The flush is fire-and-forget: the agent stream completes immediately while the
extraction runs on a background scheduler. When CompactionMiddleware is active, it
handles flush/offload for the messages it summarizes; this middleware covers the remaining
tail of messages that were kept verbatim.
Flush is gated by a MemoryConfig.FlushTrigger:
-
MemoryConfig.FlushMode#ALWAYS(default) — flush after every call. -
MemoryConfig.FlushMode#NEVER— never flush via this middleware. The CompactionMiddleware and overflow-recovery paths still run their own flush when they fire. -
MemoryConfig.FlushMode#THROTTLED— flush at most once perMemoryConfig.FlushTrigger#minGap().
Session transcript append is not handled here — see TranscriptMiddleware,
which runs independently of memory flush so history stays complete even when flush is
disabled.
Concurrent flushes for the same isolation key are serialised: at most one flush runs per key at a time, and pending flushes of the same conversation coalesce into a single queued task, because a queued task reads the conversation state only when it executes and the newest task therefore subsumes the ones it replaces. Flushes of distinct conversations under the same key are queued separately.
The class is thread-safe: instances hold only configuration, and the per-key queues live in a static map and are synchronised internally.
The throttle window is tracked per isolation key, which matches the memory data isolation in use:
-
IsolationScope#USER(default) — one window peruserId. -
IsolationScope#SESSION— one window persessionId. -
IsolationScope#AGENT/IsolationScope#GLOBAL— one shared window for the whole agent instance (prevents concurrent flush races on shared memory files).
构造器摘要Constructor Summary
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public Flux<AgentEvent> onAgent(Agent agent, RuntimeContext ctx, AgentInput input, Function<AgentInput, Flux<AgentEvent>> next) | 处理智能体事件。 Performs the on agent operation. |
构造器详情Constructor Detail
MemoryFlushMiddleware
public MemoryFlushMiddleware(WorkspaceManager workspaceManager, Model model)
初始化 MemoryFlushMiddleware 实例。
Creates a new instance.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
workspaceManager | WorkspaceManager | 工作区管理器(类型 `WorkspaceManager`)。The workspace manager. |
model | Model | 模型实例(类型 `Model`)。The model. |
io/agentscope/harness/agent/middleware/MemoryFlushMiddleware.java:96–104MemoryFlushMiddleware
public MemoryFlushMiddleware(WorkspaceManager workspaceManager, Model model, String flushPrompt, MemoryConfig.FlushTrigger flushTrigger)
初始化 MemoryFlushMiddleware 实例。
Creates a new instance.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
workspaceManager | WorkspaceManager | 工作区管理器(类型 `WorkspaceManager`)。The workspace manager. |
model | Model | 模型实例(类型 `Model`)。The model. |
flushPrompt | String | 刷新提示词(类型 `String`)。The flush prompt. |
flushTrigger | MemoryConfig.FlushTrigger | 刷新 Trigger(类型 `MemoryConfig.FlushTrigger`)。The flush trigger. |
io/agentscope/harness/agent/middleware/MemoryFlushMiddleware.java:106–118MemoryFlushMiddleware
public MemoryFlushMiddleware(WorkspaceManager workspaceManager, Model model, String flushPrompt, MemoryConfig.FlushTrigger flushTrigger, IsolationScope isolationScope)
初始化 MemoryFlushMiddleware 实例。
Creates a new instance.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
workspaceManager | WorkspaceManager | 工作区管理器(类型 `WorkspaceManager`)。The workspace manager. |
model | Model | 模型实例(类型 `Model`)。The model. |
flushPrompt | String | 刷新提示词(类型 `String`)。The flush prompt. |
flushTrigger | MemoryConfig.FlushTrigger | 刷新 Trigger(类型 `MemoryConfig.FlushTrigger`)。The flush trigger. |
isolationScope | IsolationScope | 隔离范围(类型 `IsolationScope`)。The isolation scope. |
io/agentscope/harness/agent/middleware/MemoryFlushMiddleware.java:120–133MemoryFlushMiddleware
public MemoryFlushMiddleware(WorkspaceManager workspaceManager, Model model, String flushPrompt, MemoryConfig.FlushTrigger flushTrigger, IsolationScope isolationScope, PeriodicGate periodicGate)
初始化 MemoryFlushMiddleware 实例。
Creates a new instance.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
workspaceManager | WorkspaceManager | 工作区管理器(类型 `WorkspaceManager`)。The workspace manager. |
model | Model | 模型实例(类型 `Model`)。The model. |
flushPrompt | String | 刷新提示词(类型 `String`)。The flush prompt. |
flushTrigger | MemoryConfig.FlushTrigger | 刷新 Trigger(类型 `MemoryConfig.FlushTrigger`)。The flush trigger. |
isolationScope | IsolationScope | 隔离范围(类型 `IsolationScope`)。The isolation scope. |
periodicGate | PeriodicGate | periodic Gate(类型 `PeriodicGate`)。The periodic gate. |
io/agentscope/harness/agent/middleware/MemoryFlushMiddleware.java:135–150方法详情Method Detail
onAgent
public Flux<AgentEvent> onAgent(Agent agent, RuntimeContext ctx, AgentInput input, Function<AgentInput, Flux<AgentEvent>> next)
处理智能体事件。
Performs the on agent operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | Agent | 智能体(类型 `Agent`)。The agent. |
ctx | RuntimeContext | 运行时上下文(类型 `RuntimeContext`)。The ctx. |
input | AgentInput | 输入消息(类型 `AgentInput`)。The input. |
next | Function<AgentInput, Flux<AgentEvent>> | 下一个(类型 `Function >`)。The next. |
Flux<AgentEvent>io/agentscope/harness/agent/middleware/MemoryFlushMiddleware.java:152–160