1) 客服 Agent 四份交付文档 + 构建脚手架:品牌由包装占位 XX科技 / 旧名 南方财富 统一为南方基金(热线 400-889-8899 / 官网 nffund.com),系统名改为「智能服务系统」; 同步追加 §0.4 修订记录行,工程记录行保留原占位字面以支撑硬编码扫描验收。 2) 开发文档:清理 28 份已作废/残留文档(14 份移出归档 + 14 份仓库副本), 新增《文档规整方案与开发前待决事项-2026-09-17》。 3) 客服agent 四份交付文档首次纳入本分支。
45 lines
1.8 KiB
Python
45 lines
1.8 KiB
Python
from datetime import UTC, datetime
|
|
|
|
from sqlalchemy import select, update
|
|
from sqlalchemy.engine import CursorResult
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.errors import InvalidStateError, SessionNotFoundError
|
|
from app.model.session import ConversationSession
|
|
|
|
|
|
class SessionRepository:
|
|
def __init__(self, session: AsyncSession) -> None:
|
|
self.session = session
|
|
|
|
async def owned(
|
|
self, session_id: str, user_id: int, *, lock: bool = False
|
|
) -> ConversationSession:
|
|
query = select(ConversationSession).where(ConversationSession.session_id == session_id,
|
|
ConversationSession.user_id == user_id)
|
|
if lock:
|
|
query = query.with_for_update()
|
|
row = await self.session.scalar(query)
|
|
if row is None:
|
|
raise SessionNotFoundError("会话不存在")
|
|
return row
|
|
|
|
async def require_active(self, session_id: str, user_id: int, agent_type: str) -> None:
|
|
row = await self.owned(session_id, user_id, lock=True)
|
|
if row.status != "active":
|
|
raise InvalidStateError("会话已结束")
|
|
if row.agent_type != agent_type:
|
|
raise InvalidStateError("Agent 与会话不一致")
|
|
row.message_count += 1
|
|
row.last_active_at = datetime.now(UTC).replace(tzinfo=None)
|
|
|
|
async def advance_clarification(self, session_id: str, expected: int) -> bool:
|
|
if expected >= 10:
|
|
return False
|
|
result = await self.session.execute(update(ConversationSession).where(
|
|
ConversationSession.session_id == session_id,
|
|
ConversationSession.clarification_round == expected,
|
|
ConversationSession.status == "active",
|
|
).values(clarification_round=expected + 1))
|
|
return isinstance(result, CursorResult) and result.rowcount == 1
|