File size: 2,194 Bytes
ba907cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Session management API routes.

This module handles session-related endpoints like history and cleanup.
"""

from fastapi import HTTPException

from configs.config import ErrorMessages, SuccessMessages
from models.models import SessionRequest, ChatHistoryResponse, StatusResponse
from services import session_manager


async def get_chat_history_handler(request: SessionRequest) -> ChatHistoryResponse:
    """
    Get chat history for a session.
    
    Args:
        request: Session request with session ID
        
    Returns:
        Chat history response
        
    Raises:
        HTTPException: If session not found
    """
    session_data, found = session_manager.get_session(request.session_id)
    if not found:
        raise HTTPException(status_code=404, detail=ErrorMessages.SESSION_NOT_FOUND)
    
    return ChatHistoryResponse(
        status="success",
        history=session_data.get("chat_history", [])
    )


async def clear_history_handler(request: SessionRequest) -> StatusResponse:
    """
    Clear chat history for a session.
    
    Args:
        request: Session request with session ID
        
    Returns:
        Status response
        
    Raises:
        HTTPException: If session not found
    """
    success = session_manager.clear_chat_history(request.session_id)
    if not success:
        raise HTTPException(status_code=404, detail=ErrorMessages.SESSION_NOT_FOUND)
    
    return StatusResponse(
        status="success",
        message=SuccessMessages.CHAT_HISTORY_CLEARED
    )


async def remove_pdf_handler(request: SessionRequest) -> StatusResponse:
    """
    Remove PDF and session data.
    
    Args:
        request: Session request with session ID
        
    Returns:
        Status response
        
    Raises:
        HTTPException: If session not found or removal failed
    """
    success = session_manager.remove_session(request.session_id)
    
    if success:
        return StatusResponse(
            status="success",
            message=SuccessMessages.PDF_REMOVED
        )
    else:
        raise HTTPException(
            status_code=404, 
            detail=ErrorMessages.SESSION_REMOVAL_FAILED
        )