开发resend 是 ReActAgent 的重新回答功能:在不重新输入问题的情况下,让 Agent 对上一个用户问题重新生成一次回答。#1097
Open
lsh13865950442-droid wants to merge 7 commits intoagentscope-ai:mainfrom
Open
开发resend 是 ReActAgent 的重新回答功能:在不重新输入问题的情况下,让 Agent 对上一个用户问题重新生成一次回答。#1097lsh13865950442-droid wants to merge 7 commits intoagentscope-ai:mainfrom
resend 是 ReActAgent 的重新回答功能:在不重新输入问题的情况下,让 Agent 对上一个用户问题重新生成一次回答。#1097lsh13865950442-droid wants to merge 7 commits intoagentscope-ai:mainfrom
Conversation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ReActAgent Resend 功能使用指南
1. 功能简介
resend是ReActAgent提供的重新回答功能:在不重新输入问题的情况下,让 Agent 对上一个用户问题重新生成一次回答。典型使用场景:
2. 核心设计
架构分层
Resend 能力按三层实现:
锚点生命周期
3. 使用指南
3.1 正常流程(含持久化)
3.2 Resend 流程
4. API 参考
loadIfExists(Session, SessionKey)从 Session 中加载 Agent 状态(若存在)。返回
true表示 Session 存在并完成加载,false表示 Session 不存在(跳过加载)。参数:
session:Session 实例(JsonSession、Redis Session 等)sessionKey:Session 键,标识此 Agent 的状态存储位置loadIfExists(Session, SessionKey, boolean resend)加载状态(若存在),并在
resend=true时额外执行锚点恢复,返回原始输入消息列表。参数:
session:Session 实例sessionKey:Session 键resend:true时恢复所有组件到锚点状态,并返回anchorInputMsgs返回值:
resend=true:返回锚点保存时的原始输入消息列表(非空)resend=false:返回空列表抛出异常:
IllegalStateException:resend=true但无锚点(从未调用过call()或未持久化)loadIfExists(Session, String, boolean resend)loadIfExists(Session, SessionKey, boolean)的便利重载,使用字符串 sessionId 替代SessionKey。saveTo(Session, SessionKey)将 Agent 当前状态持久化到 Session,包括:
agent_meta)memory_messages,若 memoryManaged)toolkit_activeGroups,若 toolkitManaged)_state/_state_anchor,若 planNotebookManaged)skillbox_state/skillbox_state_anchor,若 skillBoxManaged)resend_input_msgs,若存在锚点)toolkit_activeGroups_anchor,若存在锚点)StatePersistence控制哪些组件参与状态管理(
saveTo/loadFrom/ 锚点)。StatePersistence.all()StatePersistence.none()StatePersistence.memoryOnly()StatePersistence.builder()...build()Builder 示例:
5. 回滚范围
执行 resend 时,以下组件参与状态回滚:
Memory(对话历史)deleteMessagesFrom(cutIndex)按消息 ID 截断PlanNotebook(规划笔记本)restoreAnchor()恢复内存快照Toolkit.activeGroups(工具组激活状态)setActiveGroups(anchorActiveGroups)SkillBox(技能激活状态)restoreAnchor()恢复内存快照LongTermMemory(外部长期记忆)6. 实现细节
Memory 恢复策略
Memory 的锚点不单独存储一份消息副本,而是通过消息 ID 定位截断点实现恢复:
saveResendAnchor()记录anchorInputMsgs(原始输入消息列表,含消息 ID)anchorInputMsgs[0].getId()对应的位置cutIndexmemory.deleteMessagesFrom(cutIndex)截断该位置之后的所有消息不同 Memory 实现的截断行为:
deleteMessagesFrom行为InMemoryMemoryList<Msg>AutoContextMemoryworkingMemoryStorage和originalMemoryStorage(按消息 ID 匹配)持久化文件说明(JsonSession)
使用
JsonSession时,saveTo会在 Session 目录下生成以下 key 文件:agent_metamemory_messagestoolkit_activeGroups_state/_state_anchorskillbox_state/skillbox_state_anchorresend_input_msgstoolkit_activeGroups_anchor7. 文件变更清单
核心模块(agentscope-core)
StateModule.javasaveAnchor()/restoreAnchor()/hasAnchor()default 方法Memory.javadeleteMessagesFrom(int fromIndex)default 方法(按索引截断消息)InMemoryMemory.javadeleteMessagesFrom(直接截断内部列表);实现saveAnchor/restoreAnchorPlanNotebook.javasaveAnchor/restoreAnchor;saveTo/loadFrom包含锚点持久化(key:_state_anchor)SkillBox.javasaveAnchor/restoreAnchor;saveTo/loadFrom包含锚点持久化(key:skillbox_state_anchor)ReActAgent.javasaveResendAnchor();新增loadIfExistsresend 重载;saveTo额外持久化resend_input_msgs和toolkit_activeGroups_anchorStatePersistence.javaall()/none()/memoryOnly()/builder())Session.java扩展模块(agentscope-extensions)
AutoContextMemory.javadeleteMessagesFrom,同时截断workingMemoryStorage和originalMemoryStorage(按消息 ID 匹配),保证双存储一致性8. 注意事项
必须绑定 Session 才能 resend:resend 功能依赖 Session 持久化锚点数据。没有默认的内置 Session,必须通过
loadIfExists(session, key)或saveTo(session, key)显式绑定 Session,否则无法跨请求执行 resend。至少执行一次 call() 后才能 resend:
loadIfExists(..., true)要求 Session 中存在resend_input_msgs,即之前至少完成过一次call()+saveTo(),否则抛出IllegalStateException。resend 后必须再次 saveTo:resend 执行
call()后会生成新的锚点(内存中),必须再次调用saveTo(session, key)将新锚点持久化,否则下次 resend 仍会读到旧锚点。连续 resend 是安全的:每次
call()开始时都会更新锚点,因此可以连续多次 resend,每次都是从同一问题重新回答。LongTermMemory 不参与回滚:外部长期记忆(向量数据库等)的写入不会被撤销。
工具副作用不会回滚:工具执行期间产生的外部副作用(写文件、数据库写入、HTTP 请求等)无法被自动撤销。
StatePersistence 影响回滚范围:若通过
StatePersistence禁用某组件的管理,该组件将不参与锚点保存与恢复。