fix: 修复 AgentLoop 消息保存逻辑

- 同时保存 assistant 消息的 content 和 tool_calls
- 修复多轮工具调用场景下的消息丢失问题

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-15 19:52:10 +08:00
parent 20f2ea8c38
commit 1e8e0533fd

View File

@@ -509,13 +509,14 @@ class AgentLoop:
if role == "user" and content: if role == "user" and content:
self.memory.add_to_history("user", str(content)[:1000], session_key) self.memory.add_to_history("user", str(content)[:1000], session_key)
elif role == "assistant" and content: elif role == "assistant":
self.memory.add_to_history("assistant", str(content)[:1000], session_key) # Save assistant message content
# Save tool_calls for assistant messages (needed for multi-turn tool calls) if content:
elif role == "assistant" and m.get("tool_calls"): self.memory.add_to_history("assistant", str(content)[:1000], session_key)
# Save the assistant message with tool_calls # Save tool_calls if present (needed for multi-turn tool calls)
tool_calls_str = json.dumps(m.get("tool_calls", [])) if m.get("tool_calls"):
self.memory.add_to_history("assistant", f"[tool_calls]{tool_calls_str}", session_key) tool_calls_str = json.dumps(m.get("tool_calls", []))
self.memory.add_to_history("assistant", f"[tool_calls]{tool_calls_str}", session_key)
# Save tool results (needed for multi-turn conversations) # Save tool results (needed for multi-turn conversations)
elif role == "tool": elif role == "tool":
tool_call_id = m.get("tool_call_id", "") tool_call_id = m.get("tool_call_id", "")