Data Flow
This document describes how requests and responses flow through the system for key operations.
Chat message flow
Request path
- User types a message and submits in the chat input.
- Frontend (
useChat) builds the request body (chat id, messages, model, visibility) and POSTs to/api/chatwithcredentials: 'include'(cookies sent). - Next.js proxy catches
/api/chatand forwards the request to the backend atNEXT_PUBLIC_API_URLorSERVER_API_URL. - Backend receives the request:
- Rate limit — Middleware checks limits (per user or IP).
- Auth —
get_current_userresolves the user from JWT, MSAL cookie, or session token. - Chat — Load or create chat; verify ownership if existing.
- Save — Persist user message to PostgreSQL.
- Route — Classify intent (RESEARCH vs DIRECT).
- Tools — Prepare local + MCP tools.
- Stream — Call LLM, execute tools, emit SSE events.
Response path
- Backend streams SSE events (text-delta, tool-call, data-thinking, data-stage, finish).
- Proxy pipes the stream to the client without buffering.
- Frontend (
useChat) consumes the stream, updates messages and status, andonDatahandles special parts (thinking, stages). - UI re-renders with new message parts and artifacts (e.g. charts).
Auth flow (email/password)
- User submits login form (email, password).
- Frontend POSTs to
/api/auth/login(or proxy forwards to backend). - Backend verifies password, creates JWT, sets
auth_tokencookie (httpOnly), returns token payload. - Frontend redirects or stays on chat; subsequent requests include the cookie automatically.
Auth flow (Azure AD / MSAL)
- User clicks "Sign in with Microsoft".
- Frontend redirects to Azure AD; user authenticates.
- Azure AD redirects back with token; frontend sends token to
/api/auth/msal/set-token(or equivalent). - Backend validates token, sets MSAL cookie (httpOnly), returns success.
- Frontend redirects to chat; subsequent requests include the cookie.
MCP tool execution flow
- LLM requests an MCP tool (e.g.
search_indicators) with arguments. - Backend stream executor calls
call_mcp_tool(tool_name, arguments). - MCP client sends request to Data360 MCP server (HTTP or SSE).
- Data360 MCP executes the tool (e.g. calls Data360 API), returns result.
- Backend formats result as tool result, injects into stream.
- StreamEventProcessor turns it into message parts (e.g. chart artifact).
- Frontend renders the artifact (e.g. chart preview, search results).