Overview
An AI-powered chat assistant embedded in PulseBox that allows Next Novas staff to interact with the platform in natural language — querying data, managing schemas, and building integrations via chat.
Phase 1 scope: Next Novas staff only. Architecture must be tenant-scoped from day 1 to support v2 expansion to tenant-facing usage.
Scope
A — Data Queries (NL2SQL)
- Natural language → SQL → results streamed back in chat
- Example: "Show me total active employees across all tenants"
- Read + write operations, scoped to tenant context
B — Schema Operations
- Create/update/delete collections and fields via chat
- Example: "Create an Employee collection with Name, Email, Department, Status fields"
C — Integration Builder
- AI researches external product docs (crawls developer portal)
- Breaks integration into spec → proposes approach A/B/C → user picks
- Generates migrations, API routes, and config
- Stores credentials encrypted in a new
integrations system collection
- Example: "Build a Larksuite SSO integration"
Architecture
New System Collections
integrations
id, name, provider, status (active|testing|inactive),
credentials (encrypted JSONB via pgcrypto/Supabase Vault),
config (JSONB), created_by, tenant_id, created_at, updated_at
ai_chat_history
id, session_id, tenant_id, user_id, role (user|assistant),
content (text), metadata (JSONB), created_at
Both collections must have RLS policies: tenant_id = current_tenant_id. Chat history must NEVER leak across tenants.
New API Endpoint
POST /api/integrations/chat
- Streams Claude API response (Sonnet 4.6)
- Accepts:
{ message, session_id, mode: explore|plan|build|test|deploy }
- Resolves tenant from
pb-tenant cookie — all queries scoped to that tenant
- Executes NL2SQL via Supabase admin client within tenant boundary
- Logs every turn to
ai_chat_history with tenant_id
New Frontend
- Page:
/dashboard/integrations/chat (super_admin only, v1)
- Component:
src/components/integration-chat.tsx
- Stack: Vercel
ai package (streaming) + react-markdown + shadcn/ui
- Custom UI: code preview pane, approval buttons (Yes/Build/Skip), secret input forms
AI Stack
- Model: Claude Sonnet 4.6 via Anthropic API
- Schema introspection: Fetch once per session, cache in-memory + invalidate on schema change
Token Cost-Cutting (MVP)
| Strategy |
Savings |
Notes |
| Schema caching (in-memory per session) |
~40-50% |
Invalidate on schema change |
| Lossy schema (pgvector to find relevant tables) |
~30% |
Send only relevant tables per query |
Anthropic prompt caching (cache_control) |
~20-30% |
System message cached after first use |
| Conversation pruning (last 5 turns + summary) |
~15-20% |
Avoid ballooning context |
| Combined |
~65% reduction |
Real-time, no quality loss |
Model routing (Haiku for simple NL2SQL, Sonnet for code gen) deferred to v2.
Tenant Isolation Requirements (Hard Rules)
Integration Builder Flow (Example: Larksuite SSO)
- Explore — AI crawls Larksuite developer portal, summarizes OAuth/SSO requirements
- Plan — proposes A/B/C approaches (email auto-provisioning, OAuth-only, org sync)
- User picks — approval via chat
- Build — AI generates migration + API routes (staged, not committed)
- Test — user provides test credentials, AI validates OAuth flow
- Deploy — user approves, migrations pushed, code committed, secrets encrypted in DB
Implementation Checklist
Out of Scope (v1)
- Tenant-facing chat (v2)
- n8n integration
- Batch API / async processing
- Model routing (Haiku for simple queries)
- Multi-tenant chat billing/metering
Notes
- Do NOT use n8n for chat — custom backend gives full control over tenant scoping and audit trail
- Claude Code (IDE) is for building this feature; Claude API (paid) is what runs in production
- All token cost strategies must be implemented at MVP, not deferred
Overview
An AI-powered chat assistant embedded in PulseBox that allows Next Novas staff to interact with the platform in natural language — querying data, managing schemas, and building integrations via chat.
Scope
A — Data Queries (NL2SQL)
B — Schema Operations
C — Integration Builder
integrationssystem collectionArchitecture
New System Collections
integrationsai_chat_historyNew API Endpoint
POST /api/integrations/chat{ message, session_id, mode: explore|plan|build|test|deploy }pb-tenantcookie — all queries scoped to that tenantai_chat_historywithtenant_idNew Frontend
/dashboard/integrations/chat(super_admin only, v1)src/components/integration-chat.tsxaipackage (streaming) +react-markdown+shadcn/uiAI Stack
Token Cost-Cutting (MVP)
cache_control)Model routing (Haiku for simple NL2SQL, Sonnet for code gen) deferred to v2.
Tenant Isolation Requirements (Hard Rules)
ai_chat_historyrows havetenant_id— RLS enforces tenant boundaryintegrationscredentials scoped totenant_id/api/integrations/chatresolves tenant from cookie, never from request bodytenant_idfilter explicitlyIntegration Builder Flow (Example: Larksuite SSO)
Implementation Checklist
integrationssystem collection + migration + RLSai_chat_historysystem collection + migration + RLSPOST /api/integrations/chatstreaming endpointaipackage + Claude API integrationcache_controlin system message)integration-chat.tsxcomponent (streaming, markdown, code preview, approval buttons)/dashboard/integrations/chatpage (super_admin only)Out of Scope (v1)
Notes