System architecture
High-level flow
Core backend modules
| Module | Responsibility |
|---|---|
middleware/auth.ts | Tenant JWT/API key auth for widget/chat routes. |
middleware/admin-auth.ts | Supabase auth + tenant mapping for admin routes. |
routes/chat.ts | Message processing, conversation read, manual escalation. |
routes/admin.ts | Tenant admin operations: stats, tickets, intents, settings, audit. |
services/intent.service.ts | Detects intent and required entities from user messages. |
services/action.service.ts | Executes configured intent actions. |
services/escalation.service.ts | Creates escalation tickets and user-facing escalation text. |
ws/websocket.ts | Real-time chat protocol with optional streaming responses. |
Tenant isolation model
Every request is scoped totenantId, including:
- conversations
- tickets
- intents
- integrations
- audit logs
- settings
Request lifecycle (Chat HTTP)
- Auth middleware validates token and attaches
authcontext. - Message is written to
messagestable. - Recent conversation history is loaded for context.
- Intent is detected with confidence score + entities.
- If confidence low or missing intent -> escalation workflow.
- Otherwise action executes and response is formatted.
- Assistant message and metadata are persisted.
- API returns assistant response + metadata.
Request lifecycle (Admin HTTP)
- Supabase token is validated.
- User is mapped to tenant via
admin_users. - Route handler loads/updates tenant-scoped records.
- Mutations write audit log entries.
- Response returns filtered, tenant-scoped objects.
Real-time lifecycle (WebSocket)
- Client connects with token query string.
- Server authenticates token and replies
connected. - Client sends
message,typing,history, orpingevents. - Server streams or returns message responses.
- Heartbeat (
ping/pong) keeps sessions healthy.