Skip to content

Commit 17aa3c0

Browse files
committed
Refactor chat to use LlamaStack Conversations API
- Replace custom streaming adapter with direct LlamaStack SSE integration - Add comprehensive TypeScript types for streaming events - Implement batched updates with requestAnimationFrame for better performance - Add empty response detection with user-friendly warnings - Delete deprecated chat CRUD and adapter files - Fix API endpoint trailing slash issue Signed-off-by: Yuval Turgeman <[email protected]> Assisted-by: Claude
1 parent d577735 commit 17aa3c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1738
-2127
lines changed

backend/.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ repos:
1818
rev: 25.1.0
1919
hooks:
2020
- id: black
21-
language_version: python3.12
21+
language_version: python3.13
2222
exclude: '^(backend/migrations/|migrations/)'
2323

2424
- repo: https://github.com/pycqa/isort

backend/app/api/llamastack.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1+
import json
12
import logging
23
import os
3-
from typing import Optional
4+
from typing import Any, Optional
45

56
import httpx
67
from dotenv import load_dotenv
78
from fastapi import Request
89
from llama_stack_client import AsyncLlamaStackClient
910

10-
# TODO: Need to update this import when virtual_agents is refactored
11-
# from ..virtual_agents.agent_resource import EnhancedAgentResource
12-
1311
load_dotenv()
1412

1513
LLAMASTACK_URL = os.getenv("LLAMASTACK_URL", "http://localhost:8321")
16-
LLAMASTACK_TIMEOUT = float(os.getenv("LLAMASTACK_TIMEOUT", "300.0"))
14+
LLAMASTACK_TIMEOUT = float(os.getenv("LLAMASTACK_TIMEOUT", "180.0"))
1715

1816
# Set up logging
1917
logger = logging.getLogger(__name__)
@@ -178,3 +176,28 @@ def get_sync_client() -> AsyncLlamaStackClient:
178176

179177
# Create sync client instance
180178
sync_client = get_sync_client()
179+
180+
181+
def create_tool_call_trace_entry(item: Any) -> dict:
182+
"""Create trace entry for MCP tool call."""
183+
args = item.arguments
184+
if args and isinstance(args, str):
185+
try:
186+
args = json.loads(args)
187+
except Exception:
188+
pass
189+
190+
return {
191+
"type": "tool_call",
192+
"name": item.name or "tool",
193+
"server_label": item.server_label,
194+
"arguments": args,
195+
"output": item.output,
196+
"error": item.error,
197+
"status": "failed" if item.error else "completed",
198+
}
199+
200+
201+
ERROR_NO_RESPONSE_MESSAGE = (
202+
"⚠️ Unable to generate a response. Please try rephrasing your question or try again."
203+
)

backend/app/api/v1/chat.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,26 @@ async def chat(
4848
# Create chat service with the database session
4949
chat_service = ChatService(request, db, user_id=current_user.id)
5050

51-
async def chat_stream():
52-
try:
53-
async for chunk in chat_service.stream(
54-
chat_request.virtualAgentId,
55-
chat_request.sessionId or "default",
56-
chat_request.message.content,
57-
):
58-
yield f"data: {chunk}\n\n"
59-
60-
yield "data: [DONE]\n\n"
61-
62-
except Exception as e:
63-
logger.error(f"Error in stream: {str(e)}")
64-
yield f'data: {{"type":"error","content":"Error: {str(e)}"}}\n\n'
51+
# Require a valid session ID
52+
if not chat_request.sessionId:
53+
raise HTTPException(
54+
status_code=400,
55+
detail="Session ID is required",
56+
)
6557

66-
return StreamingResponse(chat_stream(), media_type="text/event-stream")
58+
# Stream directly from service (SSE format)
59+
return StreamingResponse(
60+
chat_service.stream(
61+
agent, # Pass the agent object, not ID (already fetched above)
62+
chat_request.sessionId,
63+
chat_request.message.content,
64+
),
65+
media_type="text/event-stream",
66+
headers={
67+
"Cache-Control": "no-cache",
68+
"X-Accel-Buffering": "no", # Disable nginx buffering
69+
},
70+
)
6771

6872
except Exception as e:
6973
logger.error(f"Error in chat endpoint: {str(e)}")

0 commit comments

Comments
 (0)