Technical Reference
Chat Threads: Technical Reference
Data model, API routes, and runtime behavior for thread-scoped chat in Mission Control.
Chat Threads: Technical Reference
Data model
chat_threadsid(serial PK)tenant_id(FK)titlecreated_at,updated_at
chat_messages.thread_id- nullable integer FK →
chat_threads(id)
- nullable integer FK →
A default thread ("Main") is created per tenant when needed.
API routes
Threads
GET /api/chat/threads- Returns all threads for the current tenant, ordered by
updated_at DESC.
- Returns all threads for the current tenant, ordered by
POST /api/chat/threads- Body:
{ "title"?: string } - Creates a thread.
- Body:
PATCH /api/chat/threads/[id]- Body:
{ "title": string } - Renames a thread (tenant ownership enforced).
- Body:
DELETE /api/chat/threads/[id]- Deletes thread + its messages (tenant ownership enforced).
Messages
GET /api/chat/history?threadId=<id>&limit=<n>&beforeId=<id>- Returns thread-scoped history.
GET /api/chat/stream?threadId=<id>- Server-sent events (thread-scoped).
POST /api/chat/gateway- Body includes
threadId; messages are stored and context/history is scoped to that thread.
- Body includes
WebSocket payload
WebSocket broadcasts include threadId when available. The client filters messages to the selected thread.
Race handling / de-duplication
The client can receive the same message via both:
- the HTTP response (after sending), and
- the WebSocket broadcast (immediately after DB insert)
To avoid duplicate renders, the UI:
- replaces the optimistic local user message with the server message when it arrives,
- de-dupes by final DB message id,
- uses a near-duplicate heuristic as a fallback.