Commit 1431eaf6 by sunqi02 Committed by 胡悦

feat: intent_session 持久化与冷恢复

- save_to_db() 顺带写入 intent_session 字段
- get_or_create_session() 冷恢复 intent_session

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 6966332e
...@@ -72,6 +72,9 @@ class ActiveSessionService: ...@@ -72,6 +72,9 @@ class ActiveSessionService:
"conversation_history": json.loads( "conversation_history": json.loads(
db_sess.conversation_history or "[]" db_sess.conversation_history or "[]"
), ),
"intent_session": json.loads(
db_sess.intent_session or "[]",
),
"context_entities": {}, "context_entities": {},
"created_at": ( "created_at": (
db_sess.created_at.isoformat() db_sess.created_at.isoformat()
...@@ -133,6 +136,10 @@ class ActiveSessionService: ...@@ -133,6 +136,10 @@ class ActiveSessionService:
existing.conversation_history = json.dumps( existing.conversation_history = json.dumps(
data.get("conversation_history", []), ensure_ascii=False data.get("conversation_history", []), ensure_ascii=False
) )
existing.intent_session = json.dumps(
data.get("intent_session", []),
ensure_ascii=False,
)
else: else:
db_sess = Session( db_sess = Session(
session_id=session_id, session_id=session_id,
...@@ -141,6 +148,10 @@ class ActiveSessionService: ...@@ -141,6 +148,10 @@ class ActiveSessionService:
conversation_history=json.dumps( conversation_history=json.dumps(
data.get("conversation_history", []), ensure_ascii=False data.get("conversation_history", []), ensure_ascii=False
), ),
intent_session=json.dumps(
data.get("intent_session", []),
ensure_ascii=False,
),
) )
db.add(db_sess) db.add(db_sess)
......
...@@ -273,6 +273,7 @@ class AgentHub: ...@@ -273,6 +273,7 @@ class AgentHub:
model_name=self.main_model_config.model_name, model_name=self.main_model_config.model_name,
api_key=self.main_model_config.api_key, api_key=self.main_model_config.api_key,
stream=False, stream=False,
generate_kwargs={"temperature": 0.3},
client_kwargs={"base_url": self.main_model_config.api_base} client_kwargs={"base_url": self.main_model_config.api_base}
) )
self.formatter = OpenAIChatFormatter() self.formatter = OpenAIChatFormatter()
......
...@@ -185,3 +185,74 @@ class TestDatabasePersistence: ...@@ -185,3 +185,74 @@ class TestDatabasePersistence:
svc = ActiveSessionService() # db=None svc = ActiveSessionService() # db=None
svc.get_or_create_session("no_db", room_id="room_001") svc.get_or_create_session("no_db", room_id="room_001")
svc.save_to_db("no_db") # 不应抛异常 svc.save_to_db("no_db") # 不应抛异常
class TestIntentSessionPersistence:
"""intent_session 持久化与冷恢复"""
def test_save_to_db_writes_intent_session(self, db_session):
"""save_to_db 时将 intent_session 写入 DB"""
from app.model.session import Session
svc = ActiveSessionService(db_factory=lambda: db_session)
svc.get_or_create_session(session_id="sess_intent", room_id="r1")
data = svc.store.get("sess_intent")
data["intent_session"] = [
{"role": "user", "content": "测试消息1"},
{"role": "user", "content": "测试消息2"},
]
svc.store.set("sess_intent", data)
svc.save_to_db("sess_intent")
db_row = db_session.query(Session).filter(
Session.session_id == "sess_intent"
).first()
assert db_row is not None
restored = json.loads(db_row.intent_session)
assert len(restored) == 2
assert restored[0]["content"] == "测试消息1"
def test_cold_recovery_restores_intent_session(self, db_session):
"""冷恢复时 intent_session 从 DB 回到 KV"""
from app.model.session import Session
# 先写入 DB
db_sess_row = Session(
session_id="sess_cold_intent",
room_id="r1",
channel="wechat_group",
conversation_history="[]",
intent_session=json.dumps([
{"role": "user", "content": "恢复消息"},
], ensure_ascii=False),
)
db_session.add(db_sess_row)
db_session.commit()
# 新的 ActiveSessionService(KV 为空)冷恢复
svc2 = ActiveSessionService(db_factory=lambda: db_session)
svc2.get_or_create_session(session_id="sess_cold_intent", room_id="r1")
data = svc2.store.get("sess_cold_intent")
assert data is not None
assert data["intent_session"] == [
{"role": "user", "content": "恢复消息"},
]
def test_empty_intent_session_in_db(self, db_session):
"""intent_session 为空时冷恢复也不报错"""
from app.model.session import Session
db_sess_row = Session(
session_id="sess_empty_intent",
room_id="r1",
channel="wechat_group",
conversation_history="[]",
intent_session="",
)
db_session.add(db_sess_row)
db_session.commit()
svc = ActiveSessionService(db_factory=lambda: db_session)
svc.get_or_create_session(session_id="sess_empty_intent", room_id="r1")
data = svc.store.get("sess_empty_intent")
assert data["intent_session"] == []
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!