144 lines
4.3 KiB
Python
144 lines
4.3 KiB
Python
"""Conversation routes"""
|
|
from typing import Optional, List
|
|
from fastapi import APIRouter, Depends
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.orm import Session
|
|
|
|
from luxx.database import get_db
|
|
from luxx.models import Conversation, User
|
|
from luxx.routes.auth import get_current_user
|
|
from luxx.utils.helpers import generate_id, success_response, error_response, paginate
|
|
|
|
|
|
router = APIRouter(prefix="/conversations", tags=["Conversations"])
|
|
|
|
|
|
class ConversationCreate(BaseModel):
|
|
"""Create conversation model"""
|
|
project_id: Optional[str] = None
|
|
title: Optional[str] = None
|
|
model: str = "deepseek-chat"
|
|
system_prompt: str = "You are a helpful assistant."
|
|
temperature: float = 0.7
|
|
max_tokens: int = 2000
|
|
thinking_enabled: bool = False
|
|
|
|
|
|
class ConversationUpdate(BaseModel):
|
|
"""Update conversation model"""
|
|
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(
|
|
page: int = 1,
|
|
page_size: int = 20,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""Get conversation list"""
|
|
query = db.query(Conversation).filter(Conversation.user_id == current_user.id)
|
|
result = paginate(query.order_by(Conversation.updated_at.desc()), page, page_size)
|
|
return success_response(data={
|
|
"conversations": [c.to_dict() for c in result["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)
|
|
):
|
|
"""Create conversation"""
|
|
conversation = Conversation(
|
|
id=generate_id("conv"),
|
|
user_id=current_user.id,
|
|
project_id=data.project_id,
|
|
title=data.title or "New Conversation",
|
|
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="Conversation created successfully")
|
|
|
|
|
|
@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)
|
|
):
|
|
"""Get conversation details"""
|
|
conversation = db.query(Conversation).filter(
|
|
Conversation.id == conversation_id,
|
|
Conversation.user_id == current_user.id
|
|
).first()
|
|
|
|
if not conversation:
|
|
return error_response("Conversation not found", 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)
|
|
):
|
|
"""Update conversation"""
|
|
conversation = db.query(Conversation).filter(
|
|
Conversation.id == conversation_id,
|
|
Conversation.user_id == current_user.id
|
|
).first()
|
|
|
|
if not conversation:
|
|
return error_response("Conversation not found", 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="Conversation updated successfully")
|
|
|
|
|
|
@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)
|
|
):
|
|
"""Delete conversation"""
|
|
conversation = db.query(Conversation).filter(
|
|
Conversation.id == conversation_id,
|
|
Conversation.user_id == current_user.id
|
|
).first()
|
|
|
|
if not conversation:
|
|
return error_response("Conversation not found", 404)
|
|
|
|
db.delete(conversation)
|
|
db.commit()
|
|
|
|
return success_response(message="Conversation deleted successfully")
|