Spaces:
Sleeping
Sleeping
File size: 22,564 Bytes
a236811 bcbf1db a236811 bcbf1db e699f51 bcbf1db a236811 bcbf1db |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 |
# ============================================================================
# backend/app/api/v1/conversation_routes.py
# ============================================================================
"""
Conversation & Chat API Endpoints (UNIFIED)
Combines:
- Chat functionality (send message, get response)
- Conversation management (list, search, rename, archive, delete)
All endpoints require JWT authentication.
"""
from fastapi import APIRouter, HTTPException, status, Depends
from pydantic import BaseModel, Field
from typing import List, Dict, Optional
from datetime import datetime
from app.services.chat_service import chat_service
from app.services.conversation_service import conversation_service
from app.db.repositories.conversation_repository import conversation_repository
from app.utils.dependencies import get_current_user
from app.models.user import TokenData
from app.models.conversation import (
CreateConversationRequest,
UpdateConversationRequest,
ConversationResponse,
ConversationListResult,
ReactToMessageRequest # π NEW
)
# ============================================================================
# CREATE ROUTER
# ============================================================================
router = APIRouter()
# ============================================================================
# REQUEST/RESPONSE MODELS
# ============================================================================
class ChatRequest(BaseModel):
"""Request for chat endpoint"""
query: str = Field(..., min_length=1, max_length=2000)
conversation_id: Optional[str] = Field(None, description="Optional conversation ID. If not provided, creates new conversation.")
class Config:
json_schema_extra = {
"example": {
"query": "What is my account balance?",
"conversation_id": "507f1f77bcf86cd799439011"
}
}
class ChatResponse(BaseModel):
"""Response from chat endpoint"""
response: str
conversation_id: str
policy_action: str
policy_confidence: float
documents_retrieved: int
top_doc_score: Optional[float]
total_time_ms: float
timestamp: str
# ============================================================================
# CHAT ENDPOINTS
# ============================================================================
@router.post("/", response_model=ChatResponse, status_code=status.HTTP_200_OK)
async def chat(
request: ChatRequest,
current_user: TokenData = Depends(get_current_user)
):
"""
π¬ Send a message and get AI response.
**Main chat endpoint** - processes user query through RAG pipeline.
- If conversation_id provided: Adds to existing conversation
- If no conversation_id: Creates new conversation with auto-generated title
Requires JWT authentication.
"""
try:
user_id = current_user.user_id
# ====================================================================
# STEP 1: Get or Create Conversation
# ====================================================================
conversation_id = request.conversation_id
if conversation_id:
# Verify conversation exists and user owns it
conversation = await conversation_repository.get_conversation(conversation_id)
if not conversation:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Conversation not found"
)
if conversation["user_id"] != user_id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Access denied - you don't own this conversation"
)
else:
# Create new conversation (auto-title will be generated after first response)
from app.models.conversation import CreateConversationRequest
create_req = CreateConversationRequest(
title=None, # Will be auto-generated
first_message=request.query
)
new_conversation = await conversation_service.create_conversation(
user_id=user_id,
request=create_req,
llm_manager=None # Can pass llm_manager for smart titles
)
conversation_id = str(new_conversation.id)
# ====================================================================
# STEP 2: Get Conversation History
# ====================================================================
history = await conversation_repository.get_conversation_history(
conversation_id=conversation_id,
max_messages=10
)
# ====================================================================
# STEP 3: Save User Message
# ====================================================================
await conversation_repository.add_message(
conversation_id=conversation_id,
message={
'role': 'user',
'content': request.query,
'timestamp': datetime.utcnow(),
'metadata': None
}
)
# ====================================================================
# STEP 4: Process Query (RAG Pipeline)
# ====================================================================
result = await chat_service.process_query(
query=request.query,
conversation_history=history,
user_id=user_id
)
# ====================================================================
# STEP 5: Save Assistant Response
# ====================================================================
await conversation_repository.add_message(
conversation_id=conversation_id,
message={
'role': 'assistant',
'content': result['response'],
'timestamp': datetime.utcnow(),
'metadata': {
'policy_action': result['policy_action'],
'policy_confidence': result['policy_confidence'],
'documents_retrieved': result['documents_retrieved'],
'top_doc_score': result['top_doc_score'],
'retrieval_time_ms': result['retrieval_time_ms'],
'generation_time_ms': result['generation_time_ms']
}
}
)
# ====================================================================
# STEP 6: Log Retrieval Data (for RL training)
# ====================================================================
await conversation_repository.log_retrieval({
'conversation_id': conversation_id,
'user_id': user_id,
'query': request.query,
'policy_action': result['policy_action'],
'policy_confidence': result['policy_confidence'],
'should_retrieve': result['should_retrieve'],
'documents_retrieved': result['documents_retrieved'],
'top_doc_score': result['top_doc_score'],
'response': result['response'],
'retrieval_time_ms': result['retrieval_time_ms'],
'generation_time_ms': result['generation_time_ms'],
'total_time_ms': result['total_time_ms'],
'retrieved_docs_metadata': result.get('retrieved_docs_metadata', []),
'timestamp': datetime.utcnow()
})
# ====================================================================
# STEP 7: Return Response
# ====================================================================
return ChatResponse(
response=result['response'],
conversation_id=conversation_id,
policy_action=result['policy_action'],
policy_confidence=result['policy_confidence'],
documents_retrieved=result['documents_retrieved'],
top_doc_score=result['top_doc_score'],
total_time_ms=result['total_time_ms'],
timestamp=result['timestamp']
)
except HTTPException:
raise
except Exception as e:
print(f"β Chat endpoint error: {e}")
import traceback
traceback.print_exc()
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to process chat request: {str(e)}"
)
# ============================================================================
# CONVERSATION MANAGEMENT ENDPOINTS
# ============================================================================
@router.post("/conversation", response_model=ConversationResponse, status_code=status.HTTP_201_CREATED)
async def create_conversation(
request: CreateConversationRequest = CreateConversationRequest(),
current_user: TokenData = Depends(get_current_user)
):
"""
π Create a new conversation.
Optional parameters:
- title: Custom title (if not provided, auto-generated)
- first_message: Optional first message to start conversation
Returns full conversation object.
"""
try:
conversation = await conversation_service.create_conversation(
user_id=current_user.user_id,
request=request,
llm_manager=None
)
return ConversationResponse(
id=str(conversation.id),
user_id=conversation.user_id,
title=conversation.title,
messages=conversation.messages,
is_archived=conversation.is_archived,
created_at=conversation.created_at,
updated_at=conversation.updated_at,
last_message_at=conversation.last_message_at,
message_count=conversation.message_count
)
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to create conversation: {str(e)}"
)
@router.get("/conversations", response_model=ConversationListResult)
async def list_conversations(
page: int = 1,
page_size: int = 20,
include_archived: bool = False,
current_user: TokenData = Depends(get_current_user)
):
"""
π List all conversations for authenticated user.
Supports:
- Pagination (page, page_size)
- Filter archived conversations
- Sorted by last message (newest first)
Returns lightweight list (without full message history).
"""
try:
result = await conversation_service.list_conversations(
user_id=current_user.user_id,
page=page,
page_size=page_size,
include_archived=include_archived
)
return result
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to list conversations: {str(e)}"
)
@router.get("/conversation/{conversation_id}", response_model=ConversationResponse)
async def get_conversation(
conversation_id: str,
current_user: TokenData = Depends(get_current_user)
):
"""
π Get full conversation by ID.
Returns complete conversation with all messages.
User must own the conversation.
"""
try:
conversation = await conversation_service.get_conversation(
conversation_id=conversation_id,
user_id=current_user.user_id
)
if not conversation:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Conversation not found or access denied"
)
return ConversationResponse(
id=str(conversation.id),
user_id=conversation.user_id,
title=conversation.title,
messages=conversation.messages,
is_archived=conversation.is_archived,
created_at=conversation.created_at,
updated_at=conversation.updated_at,
last_message_at=conversation.last_message_at,
message_count=conversation.message_count
)
except HTTPException:
raise
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to get conversation: {str(e)}"
)
@router.patch("/conversation/{conversation_id}", response_model=ConversationResponse)
async def update_conversation(
conversation_id: str,
request: UpdateConversationRequest,
current_user: TokenData = Depends(get_current_user)
):
"""
βοΈ Update conversation properties.
Can update:
- title: Rename conversation
- is_archived: Archive/unarchive
User must own the conversation.
"""
try:
conversation = await conversation_service.update_conversation(
conversation_id=conversation_id,
user_id=current_user.user_id,
request=request
)
if not conversation:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Conversation not found or access denied"
)
return ConversationResponse(
id=str(conversation.id),
user_id=conversation.user_id,
title=conversation.title,
messages=conversation.messages,
is_archived=conversation.is_archived,
created_at=conversation.created_at,
updated_at=conversation.updated_at,
last_message_at=conversation.last_message_at,
message_count=conversation.message_count
)
except HTTPException:
raise
except ValueError as e:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e)
)
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to update conversation: {str(e)}"
)
@router.delete("/conversation/{conversation_id}")
async def delete_conversation(
conversation_id: str,
permanent: bool = False,
current_user: TokenData = Depends(get_current_user)
):
"""
ποΈ Delete a conversation.
- Default (permanent=False): Soft delete (can be recovered)
- permanent=True: Hard delete (cannot be recovered)
User must own the conversation.
"""
try:
success = await conversation_service.delete_conversation(
conversation_id=conversation_id,
user_id=current_user.user_id,
permanent=permanent
)
if not success:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Conversation not found or access denied"
)
return {
"message": "Conversation deleted successfully",
"conversation_id": conversation_id,
"permanent": permanent
}
except HTTPException:
raise
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to delete conversation: {str(e)}"
)
@router.get("/conversations/search", response_model=ConversationListResult)
async def search_conversations(
query: str,
page: int = 1,
page_size: int = 20,
current_user: TokenData = Depends(get_current_user)
):
"""
π Search conversations by title or message content.
Searches in:
- Conversation titles
- Message content
Returns paginated results.
"""
try:
if not query or len(query.strip()) < 2:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Search query must be at least 2 characters"
)
result = await conversation_service.search_conversations(
user_id=current_user.user_id,
query=query,
page=page,
page_size=page_size
)
return result
except HTTPException:
raise
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to search conversations: {str(e)}"
)
@router.get("/conversations/stats")
async def get_conversation_stats(
current_user: TokenData = Depends(get_current_user)
):
"""
π Get conversation statistics for user.
Returns:
- total: Total conversations
- active: Non-archived conversations
- archived: Archived conversations
"""
try:
stats = await conversation_service.get_conversation_stats(
user_id=current_user.user_id
)
return {
"user_id": current_user.user_id,
"stats": stats
}
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to get stats: {str(e)}"
)
# ========================================================================
# π NEW ENDPOINTS - Add at bottom before health check
# ========================================================================
@router.post("/conversation/{conversation_id}/message/{message_index}/react")
async def react_to_message(
conversation_id: str,
message_index: int,
request: ReactToMessageRequest,
current_user: TokenData = Depends(get_current_user)
):
"""
ππ React to a message.
- message_index: Index of message in conversation (0-based)
- reaction: 'like' or 'dislike'
Replaces existing reaction if user reacts again.
User must own the conversation.
"""
try:
# Get conversation
conversation = await conversation_service.get_conversation(
conversation_id=conversation_id,
user_id=current_user.user_id
)
if not conversation:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Conversation not found or access denied"
)
# Validate message index
if message_index < 0 or message_index >= len(conversation.messages):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Invalid message index. Conversation has {len(conversation.messages)} messages."
)
# Can only react to assistant messages
message = conversation.messages[message_index]
if message.role != 'assistant':
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Can only react to assistant messages"
)
# Update reaction in MongoDB
await conversation_repository.update_message_reaction(
conversation_id=conversation_id,
message_index=message_index,
reaction=request.reaction
)
return {
"message": "Reaction updated successfully",
"conversation_id": conversation_id,
"message_index": message_index,
"reaction": request.reaction
}
except HTTPException:
raise
except Exception as e:
print(f"β React to message error: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to update reaction: {str(e)}"
)
@router.delete("/conversation/{conversation_id}/message/{message_index}/react")
async def remove_reaction(
conversation_id: str,
message_index: int,
current_user: TokenData = Depends(get_current_user)
):
"""
β Remove reaction from a message.
User must own the conversation.
"""
try:
# Get conversation
conversation = await conversation_service.get_conversation(
conversation_id=conversation_id,
user_id=current_user.user_id
)
if not conversation:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Conversation not found or access denied"
)
# Validate message index
if message_index < 0 or message_index >= len(conversation.messages):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Invalid message index"
)
# Remove reaction in MongoDB
await conversation_repository.update_message_reaction(
conversation_id=conversation_id,
message_index=message_index,
reaction=None # Remove reaction
)
return {
"message": "Reaction removed successfully",
"conversation_id": conversation_id,
"message_index": message_index
}
except HTTPException:
raise
except Exception as e:
print(f"β Remove reaction error: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to remove reaction: {str(e)}"
)
# ============================================================================
# HEALTH CHECK
# ============================================================================
@router.get("/health")
async def chat_health():
"""
π₯ Health check for chat & conversation service.
Public endpoint (no auth required).
"""
try:
health = await chat_service.health_check()
return {
"status": "healthy",
"service": "chat & conversations",
"components": health.get('components', {}),
"timestamp": datetime.utcnow().isoformat()
}
except Exception as e:
return {
"status": "unhealthy",
"service": "chat & conversations",
"error": str(e),
"timestamp": datetime.utcnow().isoformat()
} |