115 lines
3.3 KiB
Python
115 lines
3.3 KiB
Python
"""
|
|
backend/app/services/chat/session_service.py
|
|
|
|
用途:聊天会话 CRUD,供 turn_runtime 与 api/chat 共用。
|
|
依赖:models.ChatSession、SQLAlchemy。
|
|
逻辑:从 api/chat.py 抽取 ensure/update session 逻辑,避免 turn 与 completions 各写一套。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
from datetime import datetime
|
|
from typing import Any
|
|
|
|
from app import db
|
|
from app.models import ChatSession
|
|
|
|
|
|
def _session_title_from_text(text: str) -> str:
|
|
value = str(text or "").strip()
|
|
if not value:
|
|
return ""
|
|
return value[:30] + ("..." if len(value) > 30 else "")
|
|
|
|
|
|
def get_chat_session(user_id: int, agent_id: int, session_id: str | None) -> ChatSession | None:
|
|
"""按 user/agent/session_id 查询已有会话。"""
|
|
session_key = str(session_id or "").strip()
|
|
if not session_key:
|
|
return None
|
|
return db.session.query(ChatSession).filter_by(
|
|
user_id=user_id,
|
|
agent_id=agent_id,
|
|
session_id=session_key,
|
|
).first()
|
|
|
|
|
|
def ensure_chat_session(
|
|
user_id: int,
|
|
agent_id: int,
|
|
session_id: str | None,
|
|
*,
|
|
title: str = "",
|
|
status: str = "running",
|
|
external_conversation_id: str | None = None,
|
|
) -> ChatSession:
|
|
"""
|
|
获取或创建聊天会话,并更新 last_active_at。
|
|
|
|
Args:
|
|
user_id: 当前用户 ID。
|
|
agent_id: 当前 Agent ID。
|
|
session_id: 已有 session_id;为空则新建。
|
|
title: 会话标题候选。
|
|
status: 会话状态 running/done/error。
|
|
external_conversation_id: 外部 provider 会话 ID。
|
|
|
|
Returns:
|
|
ChatSession ORM 实例。
|
|
"""
|
|
session_key = str(session_id or "").strip()
|
|
external_id = str(external_conversation_id or "").strip() or None
|
|
session = get_chat_session(user_id, agent_id, session_key)
|
|
changed = False
|
|
if not session:
|
|
session = ChatSession(
|
|
session_id=session_key or uuid.uuid4().hex,
|
|
user_id=user_id,
|
|
agent_id=agent_id,
|
|
title=_session_title_from_text(title),
|
|
status=status,
|
|
external_conversation_id=external_id,
|
|
created_at=datetime.utcnow(),
|
|
updated_at=datetime.utcnow(),
|
|
last_active_at=datetime.utcnow(),
|
|
)
|
|
db.session.add(session)
|
|
db.session.commit()
|
|
return session
|
|
|
|
if not session.title and title:
|
|
session.title = _session_title_from_text(title)
|
|
changed = True
|
|
if status and session.status != status:
|
|
session.status = status
|
|
changed = True
|
|
if external_id and session.external_conversation_id != external_id:
|
|
session.external_conversation_id = external_id
|
|
changed = True
|
|
session.last_active_at = datetime.utcnow()
|
|
session.updated_at = datetime.utcnow()
|
|
changed = True
|
|
if changed:
|
|
db.session.commit()
|
|
return session
|
|
|
|
|
|
def touch_chat_session(
|
|
user_id: int,
|
|
agent_id: int,
|
|
session_id: str,
|
|
*,
|
|
status: str | None = None,
|
|
) -> ChatSession | None:
|
|
"""更新会话活跃时间与可选状态。"""
|
|
session = get_chat_session(user_id, agent_id, session_id)
|
|
if not session:
|
|
return None
|
|
if status and session.status != status:
|
|
session.status = status
|
|
session.last_active_at = datetime.utcnow()
|
|
session.updated_at = datetime.utcnow()
|
|
db.session.commit()
|
|
return session
|