Commit 63e5ac49 by sunqi02 Committed by 胡悦

fix: post_observe hook 加 try/except 保护 ReAct 循环

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 1a7145d1
...@@ -177,36 +177,42 @@ async def _post_observe_intent_hook( ...@@ -177,36 +177,42 @@ async def _post_observe_intent_hook(
注入后不触发新工具调用:消息作为 system 上下文注入,LLM 自然看到,不开启新 ReAct 循环。 注入后不触发新工具调用:消息作为 system 上下文注入,LLM 自然看到,不开启新 ReAct 循环。
""" """
data = store.get(session_id) try:
if not data: data = store.get(session_id)
return None if not data:
return None
intent_msgs = data.get("intent_session", [])
if not intent_msgs: intent_msgs = data.get("intent_session", [])
return None if not intent_msgs:
return None
# 读取并清空
data["intent_session"] = [] # 读取并清空
store.set(session_id, data) data["intent_session"] = []
store.set(session_id, data)
# 构造注入文本
text_lines = [f"[系统检测到 {len(intent_msgs)} 条新消息]"] # 构造注入文本
for m in intent_msgs: text_lines = [f"[系统检测到 {len(intent_msgs)} 条新消息]"]
text_lines.append(f"用户: {m.get('content', '')}") for m in intent_msgs:
content = "\n".join(text_lines) text_lines.append(f"用户: {m.get('content', '')}")
content = "\n".join(text_lines)
# 写入 InMemoryMemory(让 LLM 下一轮推理感知)
await self.memory.add( # 写入 InMemoryMemory(让 LLM 下一轮推理感知)
Msg("system", content, "system"), await self.memory.add(
) Msg("system", content, "system"),
)
# 写入 SessionMemory → conversation_history(持久化存档) # 写入 SessionMemory → conversation_history(持久化存档)
session_memory.add("system", content, name="intent_hook") session_memory.add("system", content, name="intent_hook")
logger.info( logger.info(
"[intent_hook] session=%s 注入 %d 条新消息到 Agent memory + conversation_history", "[intent_hook] session=%s 注入 %d 条新消息到 Agent memory + conversation_history",
session_id, len(intent_msgs), session_id, len(intent_msgs),
) )
except Exception:
logger.warning(
"[intent_hook] session=%s intent_session 处理异常(不影响 ReAct 循环)",
session_id, exc_info=True,
)
return None return None
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!