Back to AI Docs
Knowledge Base
Generate embeddings from your codebase to enable context-aware AI responses. The knowledge base is automatically queried during chat for relevant context.
How It Works
1. Analyze - Agent service clones your repo and analyzes structure, tech stack, and patterns
2. Chunk - Code, docs, and architecture info are split into semantic chunks
3. Embed - Each chunk is converted to a vector embedding using OpenAI
4. Store - Vectors are stored in Upstash Vector for fast retrieval
5. Query - When you chat, relevant chunks are retrieved and injected as context
API Endpoints
POST
/knowledge/projects/:projectId/searchSearch the project knowledge base using semantic similarity.
// Request
{
"query": "How is authentication implemented?",
"topK": 10,
"types": ["code", "doc"] // Optional filter
}
// Response
[
{
"id": "doc_abc123",
"score": 0.92,
"document": {
"type": "code",
"content": "export async function authenticate...",
"metadata": {
"filePath": "src/lib/auth.ts",
"language": "typescript"
}
}
}
]POST
/knowledge/projects/:projectId/contextGet formatted context for an AI prompt based on a query.
// Request
{ "query": "Explain the database schema" }
// Response
{
"context": "## Project Knowledge\n\n[CODE] prisma/schema.prisma\n..."
}POST
/knowledge/documentsManually store a document in the knowledge base.
{
"projectId": "project-123",
"type": "doc", // code | doc | architecture | remark | summary
"content": "This module handles user sessions...",
"filePath": "docs/sessions.md",
"tags": ["auth", "sessions"]
}React Hook: useAITask
import { useAITask } from '@/hooks/use-ai-task'
function KnowledgeManager({ projectId, repoUrl }) {
const {
task,
isPolling,
error,
analyzeProject,
generateKnowledgeBase,
refreshStatus,
} = useAITask({
pollInterval: 3000,
onComplete: (status) => {
console.log('Done:', status.result)
},
})
return (
<div>
<button onClick={() => analyzeProject(projectId, repoUrl)}>
Analyze Project
</button>
<button onClick={() => generateKnowledgeBase(projectId, repoUrl)}>
Generate Knowledge Base
</button>
{task && (
<div>
Status: {task.status}
{task.result && <pre>{JSON.stringify(task.result)}</pre>}
</div>
)}
</div>
)
}Document Types
| Type | Description |
|---|---|
| code | Source code snippets and files |
| doc | Documentation, README, comments |
| architecture | System architecture and patterns |
| remark | AI-generated observations and suggestions |
| summary | Project and component summaries |