Commit 082720d5 by sunqi02

refactor(session): remove user_id and current_agent_code, add db param and save_to_db

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 6b5919f9
...@@ -15,41 +15,67 @@ def _utcnow_iso() -> str: ...@@ -15,41 +15,67 @@ def _utcnow_iso() -> str:
class ActiveSessionInfo: class ActiveSessionInfo:
id: str id: str
session_id: str session_id: str
user_id: str
channel: str channel: str
room_id: str room_id: str
current_agent_code: Optional[str]
status: str = "active" status: str = "active"
class ActiveSessionService: class ActiveSessionService:
def __init__(self, store: Optional[SessionStore] = None) -> None: def __init__(self, store: Optional[SessionStore] = None, db=None) -> None:
self.store = store or SessionStore() self.store = store or SessionStore()
self._db = db
def get_or_create_session( def get_or_create_session(
self, self,
session_id: str, session_id: str,
room_id: str = "", room_id: str = "",
user_id: str = "",
channel: str = "wechat_group", channel: str = "wechat_group",
) -> ActiveSessionInfo: ) -> ActiveSessionInfo:
# 1. 先查内存(热路径)
existing = self.store.get(session_id) existing = self.store.get(session_id)
if existing: if existing:
return ActiveSessionInfo( return ActiveSessionInfo(
id=session_id, id=session_id,
session_id=session_id, session_id=session_id,
user_id=existing.get("user_id", user_id),
channel=existing.get("channel", channel), channel=existing.get("channel", channel),
room_id=existing.get("room_id", room_id), room_id=existing.get("room_id", room_id),
current_agent_code=existing.get("current_agent_code"),
) )
# 2. 查 DB(冷恢复)
if self._db:
from app.model.session import Session
db_sess = self._db.query(Session).filter(
Session.session_id == session_id
).first()
if db_sess:
import json
payload = {
"session_id": db_sess.session_id,
"channel": db_sess.channel,
"room_id": db_sess.room_id,
"conversation_history": json.loads(
db_sess.conversation_history or "[]"
),
"context_entities": {},
"created_at": (
db_sess.created_at.isoformat()
if db_sess.created_at else _utcnow_iso()
),
"updated_at": _utcnow_iso(),
}
self.store.set(session_id, payload)
return ActiveSessionInfo(
id=session_id,
session_id=session_id,
channel=db_sess.channel,
room_id=db_sess.room_id,
)
# 3. 都没有,创建新 session
payload = { payload = {
"session_id": session_id, "session_id": session_id,
"user_id": user_id,
"channel": channel, "channel": channel,
"room_id": room_id, "room_id": room_id,
"current_agent_code": None,
"conversation_history": [], "conversation_history": [],
"context_entities": {}, "context_entities": {},
"created_at": _utcnow_iso(), "created_at": _utcnow_iso(),
...@@ -59,16 +85,41 @@ class ActiveSessionService: ...@@ -59,16 +85,41 @@ class ActiveSessionService:
return ActiveSessionInfo( return ActiveSessionInfo(
id=session_id, id=session_id,
session_id=session_id, session_id=session_id,
user_id=user_id,
channel=channel, channel=channel,
room_id=room_id, room_id=room_id,
current_agent_code=None,
) )
def update_current_agent(self, session_id: str, agent_code: str) -> None: def save_to_db(self, session_id: str) -> None:
existing = self.store.get(session_id) """将内存中的 session 持久化到 DB。"""
if not existing: if not self._db:
return
data = self.store.get(session_id)
if not data:
return return
existing["current_agent_code"] = agent_code
existing["updated_at"] = _utcnow_iso() import json
self.store.set(session_id, existing) from app.model.session import Session
existing = self._db.query(Session).filter(
Session.session_id == session_id
).first()
if existing:
existing.room_id = data.get("room_id", "")
existing.channel = data.get("channel", "wechat_group")
existing.conversation_history = json.dumps(
data.get("conversation_history", []), ensure_ascii=False
)
else:
db_sess = Session(
session_id=session_id,
room_id=data.get("room_id", ""),
channel=data.get("channel", "wechat_group"),
conversation_history=json.dumps(
data.get("conversation_history", []), ensure_ascii=False
),
)
self._db.add(db_sess)
self._db.commit()
...@@ -38,3 +38,42 @@ class TestSessionStoreTTLRefresh: ...@@ -38,3 +38,42 @@ class TestSessionStoreTTLRefresh:
kv._store[key].expires_at = time.time() - 1 kv._store[key].expires_at = time.time() - 1
assert store.get("expired_sess") is None assert store.get("expired_sess") is None
class TestActiveSessionServiceSimplified:
def test_active_session_info_has_no_user_id(self):
"""ActiveSessionInfo 不再包含 user_id"""
info = ActiveSessionInfo(
id="s1", session_id="s1",
channel="wechat_group", room_id="r1",
)
assert not hasattr(info, "user_id")
assert not hasattr(info, "current_agent_code")
def test_get_or_create_session_no_user_id(self):
"""get_or_create_session 不再接受 user_id 参数"""
svc = ActiveSessionService()
session = svc.get_or_create_session(
session_id="sess_no_uid",
room_id="room_001",
)
assert session.session_id == "sess_no_uid"
assert session.room_id == "room_001"
assert session.channel == "wechat_group"
def test_no_update_current_agent_method(self):
"""update_current_agent 方法已移除"""
svc = ActiveSessionService()
assert not hasattr(svc, "update_current_agent")
def test_payload_no_user_id_or_agent_code(self):
"""KV payload 不包含 user_id 和 current_agent_code"""
svc = ActiveSessionService()
svc.get_or_create_session(session_id="sess_payload", room_id="r1")
data = svc.store.get("sess_payload")
assert "user_id" not in data
assert "current_agent_code" not in data
assert "session_id" in data
assert "conversation_history" in data
assert "channel" in data
assert "room_id" in data
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!