Codapult includes a working AI chat surface, not just provider configuration. The module combines a dashboard chat UI, persisted conversations, a streaming API route, provider adapters, model selection, organization quotas, and optional RAG context injection.
src/components/ai/ChatUI is the only connected chat component. It is the production dashboard surface and owns conversation history, model selection, streaming transport, and rendering of both text and tool-call parts.
Runtime flow
- The user sends a message from
src/components/ai/ChatUI. POST /api/ai/chatauthenticates the session and validates the request.- The route checks rate limits and organization AI quota.
- The selected model is resolved through
src/lib/ai/providers.ts. - RAG context is retrieved when enabled and appended to the system prompt.
- The response streams back through the Vercel AI SDK.
- Conversation and message rows are persisted for history.
Key files
| File | Purpose |
|---|---|
src/app/api/ai/chat/route.ts | Streaming chat endpoint |
src/components/ai/ChatUI | Connected dashboard chat experience |
src/components/ai/ToolCallStream.tsx | Tool-call progress and results |
src/lib/ai/models.ts | Client-safe model list |
src/lib/ai/providers.ts | Provider/model resolver |
src/lib/ai/conversations.ts | Conversation and message persistence |
src/lib/ai/rag.ts | Optional retrieval context |
Models
Models are declared in src/lib/ai/models.ts. Each entry has an ID, label, provider, SDK model ID, capabilities, and pricing. Unknown model IDs are rejected instead of being inferred from a name prefix.
To restrict the UI to a subset of models, set AI_ALLOWED_MODELS to a comma-separated list. The server page resolves the list and passes only serializable model metadata to the client component.
Tool calls
The chat route supports Vercel AI SDK tool calls. Add product-specific tools in /api/ai/chat/route.ts with a Zod parameter schema and an execute function. Keep tools small and domain-specific: lookup a record, update a workflow, fetch a report, or call an internal service.
ChatUI renders tool-call parts returned by the route through ToolCallStream. The component also accepts optional agent, debug, and message-cost data props for specialized server-backed integrations; those values must be supplied by the parent integration and are not generated by the basic chat route automatically.
Configuration
| Setting | Location |
|---|---|
| Default model | AI_DEFAULT_MODEL |
| System prompt | src/lib/ai/system-prompt.ts |
| Allowed models | AI_ALLOWED_MODELS |
| OpenAI key | OPENAI_API_KEY |
| Anthropic key | ANTHROPIC_API_KEY |
For an OpenAI-compatible custom endpoint, add the model to src/lib/ai/models.ts with provider: 'custom', then set CUSTOM_AI_BASE_URL and CUSTOM_AI_API_KEY. These variables configure the endpoint only; they do not create an arbitrary model entry.