AS AgentScopeAPI 手册 / API Reference

MemoryFlushMiddleware

public class MemoryFlushMiddleware implements HarnessRuntimeMiddleware
源码Source 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 per MemoryConfig.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 per userId.

  • IsolationScope#SESSION — one window per sessionId.

  • IsolationScope#AGENT / IsolationScope#GLOBAL — one shared window for the whole agent instance (prevents concurrent flush races on shared memory files).

构造器摘要Constructor Summary

构造器Constructor说明Description
public MemoryFlushMiddleware(WorkspaceManager workspaceManager, Model model)
初始化 `MemoryFlushMiddleware` 实例。
Creates a new instance.
public MemoryFlushMiddleware(WorkspaceManager workspaceManager, Model model, String flushPrompt, MemoryConfig.FlushTrigger flushTrigger)
初始化 `MemoryFlushMiddleware` 实例。
Creates a new instance.
public MemoryFlushMiddleware(WorkspaceManager workspaceManager, Model model, String flushPrompt, MemoryConfig.FlushTrigger flushTrigger, IsolationScope isolationScope)
初始化 `MemoryFlushMiddleware` 实例。
Creates a new instance.
public MemoryFlushMiddleware(WorkspaceManager workspaceManager, Model model, String flushPrompt, MemoryConfig.FlushTrigger flushTrigger, IsolationScope isolationScope, PeriodicGate periodicGate)
初始化 `MemoryFlushMiddleware` 实例。
Creates a new instance.

方法摘要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.

参数Parameters
名称Name类型Type说明Description
workspaceManagerWorkspaceManager工作区管理器(类型 `WorkspaceManager`)。The workspace manager.
modelModel模型实例(类型 `Model`)。The model.
源码Source io/agentscope/harness/agent/middleware/MemoryFlushMiddleware.java:96–104

MemoryFlushMiddleware

public MemoryFlushMiddleware(WorkspaceManager workspaceManager, Model model, String flushPrompt, MemoryConfig.FlushTrigger flushTrigger)

初始化 MemoryFlushMiddleware 实例。

Creates a new instance.

参数Parameters
名称Name类型Type说明Description
workspaceManagerWorkspaceManager工作区管理器(类型 `WorkspaceManager`)。The workspace manager.
modelModel模型实例(类型 `Model`)。The model.
flushPromptString刷新提示词(类型 `String`)。The flush prompt.
flushTriggerMemoryConfig.FlushTrigger刷新 Trigger(类型 `MemoryConfig.FlushTrigger`)。The flush trigger.
源码Source io/agentscope/harness/agent/middleware/MemoryFlushMiddleware.java:106–118

MemoryFlushMiddleware

public MemoryFlushMiddleware(WorkspaceManager workspaceManager, Model model, String flushPrompt, MemoryConfig.FlushTrigger flushTrigger, IsolationScope isolationScope)

初始化 MemoryFlushMiddleware 实例。

Creates a new instance.

参数Parameters
名称Name类型Type说明Description
workspaceManagerWorkspaceManager工作区管理器(类型 `WorkspaceManager`)。The workspace manager.
modelModel模型实例(类型 `Model`)。The model.
flushPromptString刷新提示词(类型 `String`)。The flush prompt.
flushTriggerMemoryConfig.FlushTrigger刷新 Trigger(类型 `MemoryConfig.FlushTrigger`)。The flush trigger.
isolationScopeIsolationScope隔离范围(类型 `IsolationScope`)。The isolation scope.
源码Source io/agentscope/harness/agent/middleware/MemoryFlushMiddleware.java:120–133

MemoryFlushMiddleware

public MemoryFlushMiddleware(WorkspaceManager workspaceManager, Model model, String flushPrompt, MemoryConfig.FlushTrigger flushTrigger, IsolationScope isolationScope, PeriodicGate periodicGate)

初始化 MemoryFlushMiddleware 实例。

Creates a new instance.

参数Parameters
名称Name类型Type说明Description
workspaceManagerWorkspaceManager工作区管理器(类型 `WorkspaceManager`)。The workspace manager.
modelModel模型实例(类型 `Model`)。The model.
flushPromptString刷新提示词(类型 `String`)。The flush prompt.
flushTriggerMemoryConfig.FlushTrigger刷新 Trigger(类型 `MemoryConfig.FlushTrigger`)。The flush trigger.
isolationScopeIsolationScope隔离范围(类型 `IsolationScope`)。The isolation scope.
periodicGatePeriodicGateperiodic Gate(类型 `PeriodicGate`)。The periodic gate.
源码Source 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.

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

继承成员Inherited Members

基于官方固定版本源码静态生成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.