Commit 2ca9309c by sunqi02 Committed by 胡悦

fix: optimize tool interaction saving to avoid duplicate historical messages

1 parent 49ada179
...@@ -515,6 +515,7 @@ class AgentHub: ...@@ -515,6 +515,7 @@ class AgentHub:
history_msgs = [Msg.from_dict(m) for m in recent] history_msgs = [Msg.from_dict(m) for m in recent]
if history_msgs: if history_msgs:
await agent_memory.add(history_msgs) await agent_memory.add(history_msgs)
history_msg_count = len(history_msgs) # 用于后续只保存本次 ReAct 新增的工具消息
# Create per-request ReActAgent with per-agent toolkit # Create per-request ReActAgent with per-agent toolkit
agent = ReActAgent( agent = ReActAgent(
...@@ -554,10 +555,11 @@ class AgentHub: ...@@ -554,10 +555,11 @@ class AgentHub:
response_text = response_msg.get_text_content() or "" # 纯文本,用于返回值和日志 response_text = response_msg.get_text_content() or "" # 纯文本,用于返回值和日志
target_agent_name = target_agent target_agent_name = target_agent
# 1. 保存工具交互(先于 assistant,保证 remove_last 只命中最终回复) # 1. 保存工具交互(只保存本次 ReAct 新增的,避免历史工具消息重复追加)
try: try:
msg_list = await agent.memory.get_memory() msg_list = await agent.memory.get_memory()
self._save_tool_interactions(memory, msg_list) new_msgs = msg_list[history_msg_count:]
self._save_tool_interactions(memory, new_msgs)
except Exception: except Exception:
logger.warning( logger.warning(
"[AgentHub] session=%s 保存工具消息异常(不影响主流程)", "[AgentHub] session=%s 保存工具消息异常(不影响主流程)",
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!