154 lines
4.3 KiB
Python
154 lines
4.3 KiB
Python
"""会话路由"""
|
|
from typing import Optional, List
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.orm import Session
|
|
|
|
from alcor.database import get_db
|
|
from alcor.models import Conversation, User
|
|
from alcor.routes.auth import get_current_user
|
|
from alcor.utils.helpers import generate_id, success_response, error_response, paginate
|
|
|
|
|
|
router = APIRouter(prefix="/conversations", tags=["会话"])
|
|
|
|
|
|
class ConversationCreate(BaseModel):
|
|
"""创建会话模型"""
|
|
project_id: Optional[str] = None
|
|
title: str = ""
|
|
model: str = "glm-5"
|
|
system_prompt: str = ""
|
|
temperature: float = 1.0
|
|
max_tokens: int = 65536
|
|
thinking_enabled: bool = False
|
|
|
|
|
|
class ConversationUpdate(BaseModel):
|
|
"""更新会话模型"""
|
|
title: Optional[str] = None
|
|
model: Optional[str] = None
|
|
system_prompt: Optional[str] = None
|
|
temperature: Optional[float] = None
|
|
max_tokens: Optional[int] = None
|
|
thinking_enabled: Optional[bool] = None
|
|
|
|
|
|
@router.get("/", response_model=dict)
|
|
def list_conversations(
|
|
project_id: Optional[str] = None,
|
|
page: int = 1,
|
|
page_size: int = 20,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""获取会话列表"""
|
|
query = db.query(Conversation).filter(Conversation.user_id == current_user.id)
|
|
|
|
if project_id:
|
|
query = query.filter(Conversation.project_id == project_id)
|
|
|
|
query = query.order_by(Conversation.updated_at.desc())
|
|
|
|
result = paginate(query, page, page_size)
|
|
items = [conv.to_dict() for conv in result["items"]]
|
|
|
|
return success_response(data={
|
|
"items": items,
|
|
"total": result["total"],
|
|
"page": result["page"],
|
|
"page_size": result["page_size"]
|
|
})
|
|
|
|
|
|
@router.post("/", response_model=dict)
|
|
def create_conversation(
|
|
data: ConversationCreate,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""创建会话"""
|
|
conversation = Conversation(
|
|
id=generate_id("conv"),
|
|
user_id=current_user.id,
|
|
project_id=data.project_id,
|
|
title=data.title or "新会话",
|
|
model=data.model,
|
|
system_prompt=data.system_prompt,
|
|
temperature=data.temperature,
|
|
max_tokens=data.max_tokens,
|
|
thinking_enabled=data.thinking_enabled
|
|
)
|
|
|
|
db.add(conversation)
|
|
db.commit()
|
|
db.refresh(conversation)
|
|
|
|
return success_response(data=conversation.to_dict(), message="会话创建成功")
|
|
|
|
|
|
@router.get("/{conversation_id}", response_model=dict)
|
|
def get_conversation(
|
|
conversation_id: str,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""获取会话详情"""
|
|
conversation = db.query(Conversation).filter(
|
|
Conversation.id == conversation_id,
|
|
Conversation.user_id == current_user.id
|
|
).first()
|
|
|
|
if not conversation:
|
|
return error_response("会话不存在", 404)
|
|
|
|
return success_response(data=conversation.to_dict())
|
|
|
|
|
|
@router.put("/{conversation_id}", response_model=dict)
|
|
def update_conversation(
|
|
conversation_id: str,
|
|
data: ConversationUpdate,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""更新会话"""
|
|
conversation = db.query(Conversation).filter(
|
|
Conversation.id == conversation_id,
|
|
Conversation.user_id == current_user.id
|
|
).first()
|
|
|
|
if not conversation:
|
|
return error_response("会话不存在", 404)
|
|
|
|
# 更新字段
|
|
update_data = data.dict(exclude_unset=True)
|
|
for key, value in update_data.items():
|
|
setattr(conversation, key, value)
|
|
|
|
db.commit()
|
|
db.refresh(conversation)
|
|
|
|
return success_response(data=conversation.to_dict(), message="会话更新成功")
|
|
|
|
|
|
@router.delete("/{conversation_id}", response_model=dict)
|
|
def delete_conversation(
|
|
conversation_id: str,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""删除会话"""
|
|
conversation = db.query(Conversation).filter(
|
|
Conversation.id == conversation_id,
|
|
Conversation.user_id == current_user.id
|
|
).first()
|
|
|
|
if not conversation:
|
|
return error_response("会话不存在", 404)
|
|
|
|
db.delete(conversation)
|
|
db.commit()
|
|
|
|
return success_response(message="会话删除成功")
|