mirror of
https://gitee.com/qqtt-ah/ai-crm.git
synced 2026-11-24 22:29:20 +08:00
init
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
"""会话路由。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from api.deps import current_actor, session_service
|
||||
from api.response import ok
|
||||
from application.session_service import SessionService
|
||||
from domain.models import Actor
|
||||
|
||||
router = APIRouter(prefix="/sessions", tags=["sessions"])
|
||||
|
||||
|
||||
class CreateSessionBody(BaseModel):
|
||||
title: str | None = None
|
||||
llmProviderId: int | None = None
|
||||
llmModel: str | None = None
|
||||
scene: str | None = None
|
||||
sceneRef: str | None = None
|
||||
# 同 scene+sceneRef 已有活跃会话则复用(发布草稿一会话)
|
||||
reuseBySceneRef: bool = False
|
||||
|
||||
|
||||
class PatchSessionBody(BaseModel):
|
||||
title: str | None = Field(default=None, min_length=1, max_length=200)
|
||||
llmProviderId: int | None = None
|
||||
llmModel: str | None = None
|
||||
# 未落库会话(local-*)改绑到稳定业务键,如系统草稿 ID
|
||||
sceneRef: str | None = Field(default=None, min_length=1, max_length=200)
|
||||
|
||||
|
||||
def _session_vo(s) -> dict:
|
||||
return {
|
||||
"threadId": s.thread_id,
|
||||
"title": s.title,
|
||||
"status": s.status.value,
|
||||
"scene": s.scene or "general",
|
||||
"sceneRef": s.scene_ref,
|
||||
"llmProviderId": s.llm_provider_id,
|
||||
"llmModel": s.llm_model,
|
||||
"createdAt": s.created_at.isoformat() + "Z",
|
||||
"updatedAt": s.updated_at.isoformat() + "Z",
|
||||
"lastMessageAt": s.last_message_at.isoformat() + "Z" if s.last_message_at else None,
|
||||
}
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def list_sessions(
|
||||
scene: str | None = Query(default="general"),
|
||||
actor: Actor = Depends(current_actor),
|
||||
svc: SessionService = Depends(session_service),
|
||||
):
|
||||
rows = await svc.list_mine(actor, scene=scene)
|
||||
return ok({"items": [_session_vo(s) for s in rows]})
|
||||
|
||||
|
||||
@router.post("")
|
||||
async def create_session(
|
||||
body: CreateSessionBody,
|
||||
actor: Actor = Depends(current_actor),
|
||||
svc: SessionService = Depends(session_service),
|
||||
):
|
||||
scene = (body.scene or "general").strip() or "general"
|
||||
ref = (body.sceneRef or "").strip() or None
|
||||
if body.reuseBySceneRef and ref:
|
||||
existing = await svc.find_by_scene_ref(actor, scene=scene, scene_ref=ref)
|
||||
if existing is not None:
|
||||
return ok(_session_vo(existing))
|
||||
s = await svc.create(
|
||||
actor,
|
||||
body.title,
|
||||
llm_provider_id=body.llmProviderId,
|
||||
llm_model=body.llmModel,
|
||||
scene=scene,
|
||||
scene_ref=ref,
|
||||
)
|
||||
return ok(_session_vo(s))
|
||||
|
||||
|
||||
@router.patch("/{thread_id}")
|
||||
async def patch_session(
|
||||
thread_id: str,
|
||||
body: PatchSessionBody,
|
||||
actor: Actor = Depends(current_actor),
|
||||
svc: SessionService = Depends(session_service),
|
||||
):
|
||||
if (
|
||||
body.title is None
|
||||
and body.llmModel is None
|
||||
and body.llmProviderId is None
|
||||
and body.sceneRef is None
|
||||
):
|
||||
raise HTTPException(status_code=400, detail="无有效更新字段")
|
||||
try:
|
||||
s = None
|
||||
if body.sceneRef is not None:
|
||||
s = await svc.rebind_scene_ref(
|
||||
actor,
|
||||
thread_id,
|
||||
scene_ref=body.sceneRef,
|
||||
title=body.title,
|
||||
)
|
||||
elif body.title is not None:
|
||||
s = await svc.rename(actor, thread_id, body.title)
|
||||
if body.llmModel is not None or body.llmProviderId is not None:
|
||||
if body.llmModel is None or body.llmProviderId is None:
|
||||
raise HTTPException(
|
||||
status_code=400, detail="切换模型须同时提供 llmProviderId 与 llmModel"
|
||||
)
|
||||
s = await svc.update_llm_selection(
|
||||
actor,
|
||||
thread_id,
|
||||
llm_provider_id=body.llmProviderId,
|
||||
llm_model=body.llmModel,
|
||||
)
|
||||
assert s is not None
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e)) from e
|
||||
except LookupError as e:
|
||||
raise HTTPException(status_code=404, detail=str(e)) from e
|
||||
return ok(_session_vo(s))
|
||||
|
||||
|
||||
@router.post("/{thread_id}/archive")
|
||||
async def archive_session(
|
||||
thread_id: str,
|
||||
actor: Actor = Depends(current_actor),
|
||||
svc: SessionService = Depends(session_service),
|
||||
):
|
||||
try:
|
||||
s = await svc.archive(actor, thread_id)
|
||||
except LookupError as e:
|
||||
raise HTTPException(status_code=404, detail=str(e)) from e
|
||||
return ok(_session_vo(s))
|
||||
|
||||
|
||||
@router.delete("/{thread_id}")
|
||||
async def delete_session(
|
||||
thread_id: str,
|
||||
actor: Actor = Depends(current_actor),
|
||||
svc: SessionService = Depends(session_service),
|
||||
):
|
||||
"""软删会话并级联软删消息;列表与聊天不再可见。"""
|
||||
try:
|
||||
s = await svc.delete(actor, thread_id)
|
||||
except LookupError as e:
|
||||
raise HTTPException(status_code=404, detail=str(e)) from e
|
||||
return ok(_session_vo(s))
|
||||
Reference in New Issue
Block a user