File size: 7,216 Bytes
cf699d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import APIRouter, Depends, HTTPException, status, Request
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from sqlmodel import Session
from typing import Optional, List
from slowapi import Limiter
from slowapi.util import get_remote_address
from jose import JWTError, jwt
import os
from dotenv import load_dotenv

from ..database import get_db
from ..models.conversation import ConversationResponse
from ..models.message import MessageCreate, MessageResponse
from ..services.conversation_service import ConversationService
from ..services.message_service import MessageService
from ..services.agent_service import AgentService
from ..utils.logger import logger, log_error, log_request

load_dotenv()

# JWT Configuration
SECRET_KEY = os.getenv("JWT_SECRET", "your-super-secret-jwt-key-change-in-production")
ALGORITHM = "HS256"

router = APIRouter()
security = HTTPBearer()
limiter = Limiter(key_func=get_remote_address)

# Initialize agent service
agent_service = AgentService()


def verify_token(token: str) -> str:
    """
    Verify and decode a JWT token
    Returns user_id as string
    """
    try:
        logger.info(f"Verifying token: {token[:20]}...")
        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
        user_id: str = payload.get("sub")
        logger.info(f"Token decoded successfully, user_id: {user_id}")
        if user_id is None:
            logger.error("Token payload missing 'sub' field")
            raise HTTPException(status_code=401, detail="Could not validate credentials")
        return user_id
    except JWTError as e:
        logger.error(f"JWT verification failed: {str(e)}")
        raise HTTPException(status_code=401, detail="Could not validate credentials")


@router.post("/chat")
@limiter.limit("30/minute")
async def send_chat_message(
    request: Request,
    message_data: MessageCreate,
    credentials: HTTPAuthorizationCredentials = Depends(security),
    db: Session = Depends(get_db)
):
    """
    Send a message to the AI agent and receive a response.
    If no conversation_id is provided, a new conversation is created.
    """
    try:
        # Verify JWT and get user_id
        user_id = verify_token(credentials.credentials)
        log_request("POST", "/chat", user_id)

        # Validate message content
        if not message_data.content or not message_data.content.strip():
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail="Message content cannot be empty"
            )

        # Get or create conversation
        conversation = None
        if message_data.conversation_id:
            # Load existing conversation
            conversation = ConversationService.get_conversation_by_id(
                db, message_data.conversation_id, user_id
            )
            if not conversation:
                raise HTTPException(
                    status_code=status.HTTP_404_NOT_FOUND,
                    detail="Conversation not found"
                )
        else:
            # Create new conversation
            conversation = ConversationService.create_conversation(db, user_id)

        # Load conversation history
        conversation_history = MessageService.get_conversation_messages(
            db, conversation.id, user_id
        )

        # Format history for agent
        formatted_history = agent_service.format_conversation_history(conversation_history)

        # Save user message
        user_message = MessageService.create_message(
            db=db,
            conversation_id=conversation.id,
            user_id=user_id,
            role="user",
            content=message_data.content
        )

        # Process message with agent
        agent_response = agent_service.process_message(
            user_message=message_data.content,
            conversation_history=formatted_history,
            conversation_id=conversation.id,
            user_id=user_id
        )

        # Save agent response
        assistant_message = MessageService.create_message(
            db=db,
            conversation_id=conversation.id,
            user_id=user_id,
            role="assistant",
            content=agent_response["response"],
            tool_calls=agent_response.get("tool_calls")
        )

        # Update conversation timestamp
        ConversationService.update_conversation_timestamp(db, conversation.id)

        # Return response
        return {
            "conversation_id": conversation.id,
            "message_id": assistant_message.id,
            "response": agent_response["response"],
            "tool_calls": agent_response.get("tool_calls", [])
        }

    except HTTPException:
        raise
    except Exception as e:
        log_error(e, "send_chat_message")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Failed to process chat message"
        )


@router.get("/conversations", response_model=List[ConversationResponse])
@limiter.limit("60/minute")
async def list_conversations(
    request: Request,
    skip: int = 0,
    limit: int = 20,
    credentials: HTTPAuthorizationCredentials = Depends(security),
    db: Session = Depends(get_db)
):
    """
    List all conversations for the authenticated user
    """
    try:
        user_id = verify_token(credentials.credentials)
        log_request("GET", "/conversations", user_id)

        conversations = ConversationService.get_user_conversations(
            db, user_id, skip=skip, limit=limit
        )
        return conversations

    except HTTPException:
        raise
    except Exception as e:
        log_error(e, "list_conversations")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Failed to retrieve conversations"
        )


@router.get("/conversations/{conversation_id}/messages", response_model=List[MessageResponse])
@limiter.limit("60/minute")
async def get_conversation_messages(
    request: Request,
    conversation_id: str,
    limit: int = 100,
    credentials: HTTPAuthorizationCredentials = Depends(security),
    db: Session = Depends(get_db)
):
    """
    Get all messages for a specific conversation
    """
    try:
        user_id = verify_token(credentials.credentials)
        log_request("GET", f"/conversations/{conversation_id}/messages", user_id)

        # Verify conversation ownership
        conversation = ConversationService.get_conversation_by_id(
            db, conversation_id, user_id
        )
        if not conversation:
            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail="Conversation not found"
            )

        # Get messages
        messages = MessageService.get_conversation_messages(
            db, conversation_id, user_id, limit=limit
        )
        return messages

    except HTTPException:
        raise
    except Exception as e:
        log_error(e, "get_conversation_messages")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Failed to retrieve messages"
        )