File size: 12,315 Bytes
b7bf9e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Chat endpoints with SSE streaming for the AI agent."""

import json
import logging
import math
import uuid
from datetime import datetime

from agents import Runner, RunContextWrapper
from agents.stream_events import RawResponsesStreamEvent, RunItemStreamEvent
from agents.items import MessageOutputItem, ToolCallItem, ToolCallOutputItem
from fastapi import APIRouter, Depends, HTTPException, Query, status
from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession
from sse_starlette.sse import EventSourceResponse

from src.api.agent.context import ChatContext
from src.api.agent.jett import jett_agent
from src.api.auth import TokenPayload, get_current_user
from src.api.database import get_session
from src.api.models.chat import ChatMessage, ChatThread
from src.api.schemas.chat import (
    ChatMessageListResponse,
    ChatMessageResponse,
    ChatRequest,
    ChatThreadListResponse,
    ChatThreadResponse,
)

logger = logging.getLogger(__name__)
router = APIRouter(prefix="/chat", tags=["chat"])


def _extract_title(text: str) -> tuple[str | None, str]:
    """Extract TITLE: prefix from first agent response."""
    if text.startswith("TITLE:"):
        parts = text.split("\n", 1)
        title = parts[0].replace("TITLE:", "").strip()[:200]
        remaining = parts[1].strip() if len(parts) > 1 else ""
        return title, remaining
    return None, text


@router.post("")
async def chat(
    body: ChatRequest,
    current_user: TokenPayload = Depends(get_current_user),
    session: AsyncSession = Depends(get_session),
):
    """Send a message and receive a streaming SSE response."""
    user_id = current_user.sub

    # Load or create thread
    if body.thread_id:
        result = await session.execute(
            select(ChatThread).where(
                ChatThread.id == body.thread_id,
                ChatThread.user_id == uuid.UUID(user_id),
            )
        )
        thread = result.scalar_one_or_none()
        if thread is None:
            raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Thread not found")
    else:
        thread = ChatThread(user_id=uuid.UUID(user_id))
        session.add(thread)
        await session.commit()
        await session.refresh(thread)

    thread_id = str(thread.id)
    is_new_thread = body.thread_id is None

    # Save user message
    user_msg = ChatMessage(
        thread_id=thread.id,
        role="user",
        content=body.message,
    )
    session.add(user_msg)
    thread.updated_at = datetime.utcnow()
    await session.commit()
    await session.refresh(user_msg)

    # Load last 50 messages for context
    msg_result = await session.execute(
        select(ChatMessage)
        .where(ChatMessage.thread_id == thread.id)
        .order_by(ChatMessage.created_at.desc())
        .limit(50)
    )
    db_messages = list(reversed(msg_result.scalars().all()))

    # Convert to OpenAI message format
    openai_messages = [
        {"role": m.role, "content": m.content}
        for m in db_messages
    ]

    # Create agent context
    chat_ctx = ChatContext(user_id=user_id, thread_id=thread_id)

    async def event_generator():
        assistant_message_id = str(uuid.uuid4())
        full_text = ""
        token_index = 0
        finish_reason = "stop"

        # Emit stream_start immediately
        yield {
            "event": "stream_start",
            "data": json.dumps({
                "thread_id": thread_id,
                "message_id": assistant_message_id,
                "timestamp": datetime.utcnow().isoformat() + "Z",
            }),
        }

        # Send initial processing status
        yield {
            "event": "processing_status",
            "data": json.dumps({
                "status": "Juggling your to-dos... 🤹‍♂️",
                "type": "status_update"
            }),
        }

        try:
            result = Runner.run_streamed(
                jett_agent,
                input=openai_messages,
                context=chat_ctx,
            )

            async for event in result.stream_events():
                if isinstance(event, RawResponsesStreamEvent):
                    # Only stream text content deltas, NOT function call argument deltas.
                    # Both have .delta, but their .type differs:
                    #   "response.output_text.delta" = text for user
                    #   "response.function_call_arguments.delta" = tool call JSON (not for user)
                    event_type = getattr(event.data, "type", "")
                    if event_type == "response.output_text.delta" and event.data.delta:
                        token = event.data.delta
                        full_text += token
                        yield {
                            "event": "text_token",
                            "data": json.dumps({"token": token, "index": token_index}),
                        }
                        token_index += 1

                elif isinstance(event, RunItemStreamEvent):
                    item = event.item
                    if isinstance(item, ToolCallOutputItem):
                        # Send status update when tool is called
                        yield {
                            "event": "processing_status",
                            "data": json.dumps({
                                "status": "Processing your request... 🛠️",
                                "type": "status_update"
                            }),
                        }
                        
                        # Check if task was modified
                        if chat_ctx.tasks_modified:
                            for action_type, task_id, task_title in chat_ctx.modified_tasks:
                                yield {
                                    "event": "task_action",
                                    "data": json.dumps({
                                        "action_type": action_type,
                                        "task_id": task_id,
                                        "task_title": task_title,
                                        "task_count": 1,
                                    }),
                                }
                            # Reset for potential further tool calls
                            chat_ctx.modified_tasks.clear()
                            chat_ctx.tasks_modified = False

        except Exception as e:
            logger.error(f"Agent error: {type(e).__name__}: {e}")
            finish_reason = "error"
            error_message = "I'm having trouble right now. Please try again in a moment."

            # Check for rate limit
            try:
                import openai
                if isinstance(e, openai.RateLimitError):
                    error_message = "I'm a bit busy right now. Please try again in a moment."
            except ImportError:
                pass

            yield {
                "event": "error",
                "data": json.dumps({
                    "code": "AGENT_ERROR",
                    "message": error_message,
                    "retryable": True,
                }),
            }

        # Handle title extraction for new threads
        display_text = full_text
        if is_new_thread:
            title, display_text = _extract_title(full_text)
            if title:
                # Update thread title in a separate session scope
                try:
                    thread.title = title
                    session.add(thread)
                    await session.commit()
                except Exception:
                    logger.warning("Failed to update thread title")

        # Save assistant message
        if display_text.strip():
            try:
                tool_calls_data = None
                if chat_ctx.modified_tasks:
                    tool_calls_data = [
                        {"action": a, "task_id": tid, "title": t}
                        for a, tid, t in chat_ctx.modified_tasks
                    ]

                assistant_msg = ChatMessage(
                    id=uuid.UUID(assistant_message_id),
                    thread_id=uuid.UUID(thread_id),
                    role="assistant",
                    content=display_text.strip(),
                    tool_calls=tool_calls_data,
                )
                session.add(assistant_msg)
                thread.updated_at = datetime.utcnow()
                await session.commit()
            except Exception:
                logger.warning("Failed to persist assistant message")

        yield {
            "event": "stream_end",
            "data": json.dumps({
                "message_id": assistant_message_id,
                "finish_reason": finish_reason,
                "full_text": display_text.strip(),
            }),
        }

    return EventSourceResponse(event_generator())


@router.get("/threads", response_model=ChatThreadListResponse)
async def list_threads(
    page: int = Query(default=1, ge=1),
    per_page: int = Query(default=20, ge=1, le=50),
    current_user: TokenPayload = Depends(get_current_user),
    session: AsyncSession = Depends(get_session),
):
    """List the authenticated user's conversation threads."""
    user_id = uuid.UUID(current_user.sub)

    # Count total threads
    count_q = select(func.count()).select_from(ChatThread).where(ChatThread.user_id == user_id)
    total = (await session.execute(count_q)).scalar_one()

    # Fetch page with message counts
    msg_count = (
        select(func.count())
        .where(ChatMessage.thread_id == ChatThread.id)
        .correlate(ChatThread)
        .scalar_subquery()
    )
    q = (
        select(ChatThread, msg_count.label("message_count"))
        .where(ChatThread.user_id == user_id)
        .order_by(ChatThread.updated_at.desc())
        .offset((page - 1) * per_page)
        .limit(per_page)
    )
    rows = (await session.execute(q)).all()

    total_pages = math.ceil(total / per_page) if total > 0 else 0
    return ChatThreadListResponse(
        data=[
            ChatThreadResponse(
                id=row.ChatThread.id,
                title=row.ChatThread.title,
                created_at=row.ChatThread.created_at,
                updated_at=row.ChatThread.updated_at,
                message_count=row.message_count,
            )
            for row in rows
        ],
        total=total,
        page=page,
        per_page=per_page,
        total_pages=total_pages,
    )


@router.get("/threads/{thread_id}/messages", response_model=ChatMessageListResponse)
async def get_thread_messages(
    thread_id: uuid.UUID,
    page: int = Query(default=1, ge=1),
    per_page: int = Query(default=50, ge=1, le=100),
    current_user: TokenPayload = Depends(get_current_user),
    session: AsyncSession = Depends(get_session),
):
    """Get messages for a specific conversation thread."""
    user_id = uuid.UUID(current_user.sub)

    # Verify thread ownership
    result = await session.execute(
        select(ChatThread).where(
            ChatThread.id == thread_id,
            ChatThread.user_id == user_id,
        )
    )
    thread = result.scalar_one_or_none()
    if thread is None:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Thread not found")

    # Count total messages
    count_q = (
        select(func.count())
        .select_from(ChatMessage)
        .where(ChatMessage.thread_id == thread_id)
    )
    total = (await session.execute(count_q)).scalar_one()

    # Fetch page
    q = (
        select(ChatMessage)
        .where(ChatMessage.thread_id == thread_id)
        .order_by(ChatMessage.created_at.asc())
        .offset((page - 1) * per_page)
        .limit(per_page)
    )
    messages = (await session.execute(q)).scalars().all()

    total_pages = math.ceil(total / per_page) if total > 0 else 0
    return ChatMessageListResponse(
        data=[
            ChatMessageResponse(
                id=m.id,
                role=m.role,
                content=m.content,
                action_card=m.tool_calls,
                created_at=m.created_at,
            )
            for m in messages
        ],
        total=total,
        page=page,
        per_page=per_page,
        total_pages=total_pages,
    )