Commit f9daae51 by sunqi02 Committed by 胡悦

fix: incomplete 追问写入 intent_session 而非 conversation_history

incomplete 路径是意图分类的中间状态,追问交换应追加到 intent_session
(作为分类上下文),不应污染 conversation_history(真正的对话历史)。
_classify_and_rewrite 同时修复:拼入 intent_session 中非 system 消息,
写回时保留 incomplete 追问对、丢弃旧的 user_current。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent c37f065a
......@@ -227,8 +227,13 @@ class MessageGate:
"content": self._build_classify_prompt(agent_codes=agent_codes or []),
}
# 拼 history 成多轮 messages 对
# 拼 intent_session 中的非 system 消息(incomplete 路径追加的追问上下文)
messages = [system_msg]
for m in base_messages:
content = (m.get("content") or "").strip()
if m.get("role") != "system" and content:
messages.append({"role": m["role"], "content": m.get("content", "")})
# 拼 history 成多轮 messages 对
for h in history:
if h.get("role") in ("user", "assistant"):
messages.append({"role": h["role"], "content": h.get("content", "")})
......@@ -247,9 +252,15 @@ class MessageGate:
type(response).__name__, text[:300],
)
# 写回 intent_session(不含 history,下次再拼)
# 注意:SessionStore.get() 返回反序列化后新 dict,必须显式 set() 写回 KV
data["intent_session"] = [system_msg, {"role": "user", "content": user_prompt}]
# 写回 intent_session:保留 system + incomplete 追问上下文 + 本次 user_current
# "## 当前用户消息" 前缀的消息是历次 classify 的 user_current,丢弃
intent_session = [system_msg]
for m in base_messages:
content = (m.get("content") or "").strip()
if m.get("role") != "system" and "## 当前用户消息" not in content and content:
intent_session.append({"role": m["role"], "content": m.get("content", "")})
intent_session.append({"role": "user", "content": user_prompt})
data["intent_session"] = intent_session
store.set(session_id, data)
self.session_service.save_to_db(session_id)
......@@ -377,16 +388,25 @@ class MessageGate:
"""incomplete 分支:追问用户意图,保留 pending_messages 等待补充。"""
if not decision.reply:
return
from app.service.memory import SessionMemory
from app.infrastructure.session_store import SessionStore
store = SessionStore()
merged_text = "\n".join(
m.get("msg_content", "") for m in pending
if m.get("msg_type") in ("text", "other", "file")
)
mem = SessionMemory(session_id, store)
mem.add("user", merged_text)
mem.add("assistant", decision.reply, agent_code=self.main_agent_key)
# 将追问交换追加到 intent_session(分类上下文),不污染 conversation_history
data = store.get(session_id) or {}
intent_msgs = data.get("intent_session")
if isinstance(intent_msgs, list):
intent_msgs.append({"role": "user", "content": merged_text})
intent_msgs.append({"role": "assistant", "content": decision.reply})
else:
intent_msgs = [
{"role": "user", "content": merged_text},
{"role": "assistant", "content": decision.reply},
]
data["intent_session"] = intent_msgs
store.set(session_id, data)
self.session_service.save_to_db(session_id)
await self.post_reply_hook.execute({
"agent_name": self.main_agent_key,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!