Commit 70679be8 by sunqi02

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

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