Commit 9fbe840b by sunqi02 Committed by 胡悦

fix:修复sub agent 重复写会内存消息的问题,在多伦任务的时候。

1 parent 13413f47
...@@ -180,8 +180,7 @@ async def _bg_finalize_sub_agent( ...@@ -180,8 +180,7 @@ async def _bg_finalize_sub_agent(
room_id=agent_code, room_id=agent_code,
channel="sub_agent", channel="sub_agent",
) )
sub_memory_msgs = await sub_agent.memory.get_memory() sub_memory_msgs = await sub_agent.memory.get_memory(exclude_mark="history")
from app.agent_core.memory import SessionMemory
sub_session_memory = SessionMemory( sub_session_memory = SessionMemory(
session_id=sub_session_id, session_id=sub_session_id,
store=hub.session_service.store, store=hub.session_service.store,
...@@ -648,8 +647,7 @@ class AgentHub: ...@@ -648,8 +647,7 @@ class AgentHub:
agent_memory = InMemoryMemory() agent_memory = InMemoryMemory()
history_msgs = [Msg.from_dict(m) for m in messages if "name" in m] history_msgs = [Msg.from_dict(m) for m in messages if "name" in m]
if history_msgs: if history_msgs:
await agent_memory.add(history_msgs) await agent_memory.add(history_msgs, marks=["history"])
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(
...@@ -689,10 +687,9 @@ class AgentHub: ...@@ -689,10 +687,9 @@ 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. 保存工具交互(只保存本次 ReAct 新增的,避免历史工具消息重复追加) # 1. 保存工具交互(只保存本次 ReAct 新增的,排除 history 标记)
try: try:
msg_list = await agent.memory.get_memory() new_msgs = await agent.memory.get_memory(exclude_mark="history")
new_msgs = msg_list[history_msg_count:]
self._save_tool_interactions(memory, new_msgs) self._save_tool_interactions(memory, new_msgs)
except Exception: except Exception:
logger.warning( logger.warning(
...@@ -823,7 +820,7 @@ class AgentHub: ...@@ -823,7 +820,7 @@ class AgentHub:
sub_history = sub_session_memory.get_messages(max_rounds=10) sub_history = sub_session_memory.get_messages(max_rounds=10)
history_msgs = [Msg.from_dict(m) for m in sub_history if "name" in m] history_msgs = [Msg.from_dict(m) for m in sub_history if "name" in m]
if history_msgs: if history_msgs:
await sub_memory.add(history_msgs) await sub_memory.add(history_msgs, marks=["history"])
logger.info( logger.info(
"[子Agent 开始] agent_code=%s history=%d instruction=%s", "[子Agent 开始] agent_code=%s history=%d instruction=%s",
agent_code, len(history_msgs), instruction[:80], agent_code, len(history_msgs), instruction[:80],
......
This diff could not be displayed because it is too large.
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
"sender_id": "", "sender_id": "",
"sender_name": "孙奇", "sender_name": "孙奇",
"msg_type": "text", "msg_type": "text",
"msg_content": "问界M8的价格和吸引客户的亮点", "msg_content": "问界M8的延保价格和吸引客户的亮点",
"payload": {} "payload": {}
} }
] ]
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!