Spaces:
Sleeping
Sleeping
| """Conversations router for managing chat history.""" | |
| from fastapi import APIRouter, Depends, HTTPException, status | |
| from sqlalchemy.orm import Session | |
| from typing import List | |
| from uuid import UUID | |
| from ..schemas.conversation import ( | |
| ConversationCreate, | |
| ConversationResponse, | |
| ConversationWithMessages, | |
| ConversationList, | |
| MessageResponse, | |
| ) | |
| from ..models.user import User | |
| from ..services.conversation_service import ConversationService | |
| from ..utils.deps import get_db, get_current_user | |
| router = APIRouter(tags=["Conversations"]) | |
| def list_conversations( | |
| user_id: UUID, | |
| limit: int = 20, | |
| offset: int = 0, | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user), | |
| ): | |
| """List all conversations for the authenticated user.""" | |
| # Verify user_id matches authenticated user (return 404 for security) | |
| if user_id != current_user.id: | |
| raise HTTPException(status_code=404, detail="Not found") | |
| conversation_service = ConversationService(db) | |
| conversations = conversation_service.list_conversations( | |
| user_id=current_user.id, | |
| limit=limit, | |
| offset=offset | |
| ) | |
| return ConversationList( | |
| conversations=[ | |
| ConversationResponse.model_validate(conv) | |
| for conv in conversations | |
| ], | |
| total=len(conversations) | |
| ) | |
| def create_conversation( | |
| user_id: UUID, | |
| conversation_data: ConversationCreate, | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user), | |
| ): | |
| """Create a new conversation.""" | |
| # Verify user_id matches authenticated user (return 404 for security) | |
| if user_id != current_user.id: | |
| raise HTTPException(status_code=404, detail="Not found") | |
| conversation_service = ConversationService(db) | |
| conversation = conversation_service.create_conversation( | |
| user_id=current_user.id, | |
| title=conversation_data.title | |
| ) | |
| return ConversationResponse.model_validate(conversation) | |
| def get_conversation( | |
| user_id: UUID, | |
| conversation_id: int, | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user), | |
| ): | |
| """Get a conversation with its messages.""" | |
| # Verify user_id matches authenticated user (return 404 for security) | |
| if user_id != current_user.id: | |
| raise HTTPException(status_code=404, detail="Not found") | |
| conversation_service = ConversationService(db) | |
| conversation = conversation_service.get_conversation( | |
| conversation_id=conversation_id, | |
| user_id=current_user.id | |
| ) | |
| if not conversation: | |
| raise HTTPException(status_code=404, detail="Conversation not found") | |
| messages = conversation_service.get_messages( | |
| conversation_id=conversation_id, | |
| user_id=current_user.id, | |
| limit=100 # Get more messages for viewing | |
| ) | |
| return ConversationWithMessages( | |
| id=conversation.id, | |
| user_id=conversation.user_id, | |
| title=conversation.title, | |
| created_at=conversation.created_at, | |
| updated_at=conversation.updated_at, | |
| messages=[ | |
| MessageResponse.model_validate(msg) | |
| for msg in messages | |
| ] | |
| ) | |
| def delete_conversation( | |
| user_id: UUID, | |
| conversation_id: int, | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user), | |
| ): | |
| """Delete a conversation and all its messages.""" | |
| # Verify user_id matches authenticated user (return 404 for security) | |
| if user_id != current_user.id: | |
| raise HTTPException(status_code=404, detail="Not found") | |
| conversation_service = ConversationService(db) | |
| deleted = conversation_service.delete_conversation( | |
| conversation_id=conversation_id, | |
| user_id=current_user.id | |
| ) | |
| if not deleted: | |
| raise HTTPException(status_code=404, detail="Conversation not found") | |