Skip to content

[Feature] AG-UI 应在 Harness + AgentStateStore 场景下自动裁剪全量 messages(CopilotKit) #2023

Done

Description

@likebean

背景

我们在做 服务端多租户 Agent 平台(Spring Boot + AG-UI + CopilotKit),技术栈:

  • HarnessAgent + DistributedStore(MysqlAgentStateStore + JdbcStore)
  • AG-UI Spring Boot Starter
  • CopilotKit 前端(每轮 prepareRunAgentInput 会 POST 完整 messages 数组)

生产 / 多副本典型配置:

agentscope:
  agui:
    server-side-memory: false   # 避免 JVM ThreadSessionManager 粘会话,无法跨 Pod

会话上下文由 Harness 持久化到 MySQL(每轮 activateSlotForContext 从 AgentStateStore 加载,再 addToContext(msgs) 追加 请求消息)。

关联 Issue:#2014(AG-UI + Harness 注入 userId)、#2015(session jsonl / BaseStore 镜像)。

问题现象

CopilotKit 每轮 POST 全量 transcript 时,Harness 会 重复追加 历史:

MySQL 已加载:     [user1, assistant1, user2, assistant2]
+ POST 全量 body:  [user1, assistant1, user2, assistant2, user3]
= 实际上下文:      [user1, assistant1, user2, assistant2, user1, assistant1, user2, assistant2, user3]  ❌

期望契约(与官方 curl / @ag-ui/client 示例一致):threadId + 本轮增量消息。

框架已有能力,但未覆盖本场景

AguiRequestProcessor 在 agentResolver.hasMemory(threadId) 为 true 时会调用 extractLatestUserMessage(),但存在以下缺口:

缺口 说明
记忆模型不一致 server-side-memory: true 走 ThreadSessionManager(JVM 内按 threadId 复用 Agent 实例),不是 外部 AgentStateStore。官方文档 AG-UI 集成 示例是 ReActAgent + InMemoryMemory,未覆盖 Harness + MySQL。
hasMemory 判断过窄 ThreadSessionManager.hasMemory() 仅 agent instanceof ReActAgent。包装类(如 UserContextAgent → HarnessAgent)恒返回 false,即使开 server-side-memory: true 也不会裁剪。
未处理 tool 回传 extractLatestUserMessage 只保留最后一条 user。前端工具回传(末尾 role=tool)未覆盖;Harness pending tool 场景应只 append 尾部 tool(assistant+toolCalls 已在 store)。
命名易误解 DefaultAgentResolver 注释写「AgentStateStore mode」,实现却是 ThreadSessionManager。

应用层临时方案(我们已实现)

在 AguiAgentAdapter.run() 之前增加 RequestBodyAdvice + AguiIncrementalMessages.toIncremental():

  • 末尾 user → 只保留最后一条 user(及其后连续 tool,如有)
  • 末尾 tool → 只保留尾部连续 tool 消息(assistant+toolCalls 已在 AgentStateStore)

语义与 Harness 一致,但更希望由框架统一提供。

建议的框架改进

  1. 可插拔 AgentResolver(或 RunAgentInput 预处理 Hook),不要与 ThreadSessionManager 硬绑定。
  2. hasMemory(threadId) 支持外部 Store:例如查询 AgentStateStore / Harness 是否已有该会话,而非仅查 JVM session。
  3. extractIncrementalMessages(input)(扩展 extractLatestUserMessage):
    • 普通轮次:最后一条 user
    • 前端 tool 回传:尾部连续 tool(并行多工具 = 多条 tool)
  4. 文档区分两种模式:
    • 模式 A:单 Pod 演示 — server-side-memory: true + ReActAgent
    • 模式 B:生产 — Harness / AgentStateStore + 增量 POST(或由 processor 自动裁剪)
  5. 修复 hasMemory:支持 Agent 包装/委托,或通过 AgentStateStore 判断。

环境

  • agentscope-java: 2.0.0-local / main
  • Spring Boot 3.4.x + AG-UI Starter + HarnessAgent + MySQL MysqlAgentStateStore
  • 前端:CopilotKit v2
  • 部署:K8s 多副本(server-side-memory: false)

谢谢!

Activity

  1. added
    enhancement
    area/ext/integration
    on Jul 11, 2026
  2. self-assigned this
    on Oct 8, 2026
  3. xzxiaoshan commented on Dec 1, 2026

    @xzxiaoshan
    Collaborator

    这个问题在 #4523 里按「框架不裁剪、由应用处理、文档给出指导」的方向处理了。

    框架侧的变化

    AguiRequestProcessor 不再调用 hasMemory(),并移除了 extractLatestUserMessage。AgentScope 把每次调用的输入当作增量处理:直接追加到 agent 的会话后面,不检测其中是否已有 agent 持有的内容。全量重发的历史因此不再由框架裁剪。

    两条处理方式

    1. 升级 CopilotKit 到 1.73.1 或更高版本,用 messageFilter 只发送当前这一轮的消息,请求体也会随之变小。

      <CopilotKit runtimeUrl="/api/agui" messageFilter={(messages) => messages.slice(-1)}>
    2. 前端改不了时,在 agent 上注册一个自己写的 MiddlewareBase,在 onAgentStateReady 里裁剪输入。这个钩子在本次调用的 AgentState 就绪之后触发,能同时拿到已持久化的会话和本次输入。配置了 AgentStateStore 时,状态在每次调用开始时从 store 重新加载,所以它不依赖 JVM 会话,本 issue 里 server-side-memory: false 的多副本配置可以直接用。HarnessAgent 通过 builder 的 middleware(...) 注册,做法与 ReActAgent 相同。思路是以 agent 已持有的最后一条消息为 anchor,只保留输入中它之后的部分。

    以上写进了 AG-UI 文档的「会话历史」一节(#4523)。文档给的是思路和伪代码骨架,裁剪规则需要按自己的前端实现。

    两点说明

  4. moved this from In progress to Done in AgentScope Javaon Dec 1, 2026
  5. moved this from Done to In progress in AgentScope Javaon Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/ext/integrationenhancement

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions