Commit 0271f22a by sunqi02 Committed by 胡悦

feat: get_or_create_session cold recovery includes intent_session

DB cold recovery now parses and restores intent_session to KV.
New sessions include intent_session: '' in payload for consistency.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent d7cc3627
......@@ -54,11 +54,27 @@ class ActiveSessionService:
)
# 2. 查 DB(冷恢复)
if self._db:
db_sess = self._db.query(Session).filter(
if self._db_factory:
try:
with self._db_factory() as db:
db_sess = db.query(Session).filter(
Session.session_id == session_id
).first()
# with 块结束 → db 关闭、连接归还池
except Exception:
logger.exception("Failed to cold-recover session %s from DB, 降级新建", session_id)
db_sess = None
if db_sess:
intent_session_raw = (db_sess.intent_session or "").strip()
try:
intent_session = json.loads(intent_session_raw) if intent_session_raw else []
except (json.JSONDecodeError, TypeError, ValueError) as e:
logger.warning(
"session=%s intent_session 解析失败: %s, 降级为空",
session_id, e,
)
intent_session = []
payload = {
"session_id": db_sess.session_id,
"channel": db_sess.channel,
......@@ -66,6 +82,7 @@ class ActiveSessionService:
"conversation_history": json.loads(
db_sess.conversation_history or "[]"
),
"intent_session": intent_session,
"context_entities": {},
"created_at": (
db_sess.created_at.isoformat()
......@@ -87,6 +104,7 @@ class ActiveSessionService:
"channel": channel,
"room_id": room_id,
"conversation_history": [],
"intent_session": "",
"context_entities": {},
"created_at": _utcnow_iso(),
"updated_at": _utcnow_iso(),
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!