Codapult ships with a retrieval-augmented generation pipeline that can index product knowledge and inject relevant context into AI chat responses. It is implemented as plain TypeScript modules, so you can reuse it for search pages, copilots, helpdesk suggestions, or admin tooling.
Architecture
| File | Purpose |
|---|---|
src/lib/ai/chunker.ts | Splits long text into overlapping chunks |
src/lib/ai/embeddings.ts | Embedding provider adapter |
src/lib/ai/vector-store.ts | Vector store adapter |
src/lib/ai/rag.ts | Index, search, delete, and prompt helpers |
src/app/api/ai/index/route.ts | Admin indexing/search endpoint |
src/app/api/ai/search/route.ts | Semantic search endpoint |
Indexing content
Use indexDocument when your app creates or updates searchable content:
import { indexDocument } from '@/lib/ai/rag';
await indexDocument({
sourceType: 'help',
sourceId: 'getting-started',
title: 'Getting Started',
content: markdownContent,
});
For larger batches, enqueue the rag-index background job so indexing does not block the user request.
Source types
| Type | Typical source |
|---|---|
blog | Blog posts |
help | Help center articles |
feature_request | Feature request descriptions |
custom | Product-specific content |
Embedding providers
Embeddings use an adapter selected by EMBEDDING_PROVIDER.
| Provider | Env value | Notes |
|---|---|---|
| OpenAI | openai | Default; requires OPENAI_API_KEY |
| Ollama | ollama | Self-hosted embeddings via OLLAMA_BASE_URL |
Vector stores
Vector storage is selected by VECTOR_STORE_PROVIDER.
| Store | Env value | Notes |
|---|---|---|
| SQLite | sqlite | Persists vectors in the app database |
| Memory | memory | Useful for tests and local experiments |
Search endpoint
/api/ai/search can power semantic search UI. Use it when you want search results without generating an AI response. Use the RAG helpers directly when you need to inject matching chunks into a prompt.
Removal
The RAG pipeline can be removed separately from the basic chat UI. Remove src/lib/ai/rag.ts, src/lib/ai/embeddings.ts, src/lib/ai/vector-store.ts, the AI indexing routes, the embedding table, and the related environment variables.