feat(context-enhance): add RuntimeContext onStateLoaded callback for anchor-based AG-UI message auto-merge - #2533
Conversation
…anchor-based AG-UI message auto-merge
…st to retrieve the last message
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
关于 onStateLoaded 的设计,对 msgList 的自动衔接处理,这只是一个场景。 |
AgentScopeJavaBot
left a comment
There was a problem hiding this comment.
🤖 AI Review
This PR introduces an onStateLoaded lifecycle callback on RuntimeContext, fired after AgentState is loaded in ReActAgent.beforeAgentExecution(). The AG-UI adapter leverages this hook to implement anchor-based message deduplication, replacing the crude hasMemory + extractLatestUserMessage approach. Well-designed PR with clear intent, clean separation of concerns, and solid test coverage. Two actionable issues should be addressed before merge.
AgentScopeJavaBot
left a comment
There was a problem hiding this comment.
🤖 AI Review
This PR introduces an onStateLoaded lifecycle callback on RuntimeContext, fired after AgentState is loaded in ReActAgent.beforeAgentExecution(). The AG-UI adapter leverages this hook to implement anchor-based message deduplication, replacing the crude hasMemory + extractLatestUserMessage approach. Well-designed PR with clear intent, clean separation of concerns, and solid test coverage. Two actionable issues should be addressed before merge.
新增 RuntimeContext onStateLoaded 回调设计 & AG-UI 消息自动融合
一、背景:AgentState 的时序缺口
AgentState 在 agent 生命周期的
beforeAgentExecution中才加载(从 stateStore 或 stateCache),而此时 agent 调用已发起,调用方无法再直接处理消息(不管是 context 还是 msgs,有些场景我们是需要基于 agentstate 做对应处理的):这个时序缺口导致:调用方知道需要基于持久化状态处理消息(如去重、注入、过滤),但拿不到 state,只能在 processor 层用
hasMemory粗暴判断 +extractLatestUserMessage简化处理。二、方案:onStateLoaded 回调
原理
调用方在 RuntimeContext 上设置
onStateLoaded回调。agent 生命周期在 state 加载完成后、doCall 执行前触发它。调用方在回调中基于 AgentState 做任何需要的二次处理。回调签名
无返回值,回调内三个对象全可访问:
RuntimeContextAgentStatectx.getAgentState()List<Msg>核心代码变更
RuntimeContext 新增属性
RuntimeContext.java
ReActAgent 在
beforeAgentExecution中触发ReActAgent.java,紧跟ctx.setAgentState(scope.state)之后:三、应用场景:消息自动合并
作为
onStateLoaded的首个应用场景,一并优化当前 AG-UI processor 的消息处理逻辑。问题
AguiRequestProcessor.java 在判定有内存时,仅抽取最后一条 UserMessage:
agent.call()的入口无法享受去重逻辑锚点去重实现
通过
onStateLoaded回调实现。用 messageId 锚点判断传入消息中哪些是新的:AguiAgentAdapter 设置回调
AguiAgentAdapter.javabuildRuntimeContext: