Back to AI Docs
Client SDK
React hooks and a client library for integrating AI features into your application. The SDK handles streaming, state management, and error handling.
Installation
The AI client is included in the web app. Import from the library:
// Hooks
import { useAIChat } from '@/hooks/use-ai-chat'
import { useAITask } from '@/hooks/use-ai-task'
// Direct client
import { getAIClient, AIClient } from '@/lib/ai/client'useAIChat
Hook for streaming chat with AI. Handles message state, streaming, and session management.
Options
| Option | Type | Description |
|---|---|---|
| projectId | string | Enable knowledge-aware responses |
| provider | 'openai' | 'anthropic' | AI provider (default: openai) |
| model | string | Model ID (e.g., gpt-5) |
| systemPrompt | string | Custom system prompt |
| temperature | number | Response randomness (0-2) |
| onChunk | (chunk) => void | Called on each stream chunk |
| onComplete | (content, sessionId) => void | Called when stream completes |
| onError | (error) => void | Called on error |
Return Values
const {
messages, // Message[] - conversation history
isStreaming, // boolean - is currently streaming
sessionId, // string | null - current session ID
error, // Error | null - last error
sendMessage, // (content: string) => Promise<void>
stopStream, // () => void - abort current stream
clearMessages, // () => void - clear conversation
setInitialMessages, // (messages: Message[]) => void
addSystemMessage, // (content: string) => void
} = useAIChat(options)useAITask
Hook for dispatching and tracking AI tasks like project analysis and knowledge generation.
Options
| Option | Type | Description |
|---|---|---|
| pollInterval | number | Status poll interval (ms, default: 3000) |
| autoPoll | boolean | Auto-start polling (default: true) |
| onStatusChange | (status) => void | Called on status change |
| onComplete | (status) => void | Called on task completion |
| onError | (error, status?) => void | Called on error |
Return Values
const {
task, // TaskStatus | null
isPolling, // boolean
error, // Error | null
dispatch, // (request: TaskRequest) => Promise<TaskStatus>
analyzeProject, // (projectId, repoUrl?, branch?) => Promise<TaskStatus>
generateKnowledgeBase, // (projectId, repoUrl?, branch?) => Promise<TaskStatus>
startPolling, // (taskId: string) => void
stopPolling, // () => void
refreshStatus, // () => Promise<TaskStatus | null>
} = useAITask(options)AIClient Class
For non-React environments or custom implementations, use the client directly.
import { getAIClient, AIClient } from '@/lib/ai/client'
// Use singleton
const client = getAIClient()
// Or create custom instance
const customClient = new AIClient({
baseUrl: 'https://testing.codmir.com',
apiKey: 'your-api-key',
timeout: 900000, // 15 minutes
})
// Methods
await client.chat(request) // ChatResponse
client.streamChat(request) // AsyncGenerator<StreamChunk>
await client.complete(request) // { text, model }
await client.searchKnowledge(projectId, query, options)
await client.getProjectContext(projectId, query)
await client.dispatchTask(request) // TaskStatus
await client.getTaskStatus(taskId) // TaskStatus
await client.healthCheck() // { status, service }TypeScript Types
interface Message {
id: string
role: 'user' | 'assistant' | 'system'
content: string
createdAt: Date
isStreaming?: boolean
}
interface ChatRequest {
sessionId?: string
messages: ChatMessage[]
provider?: 'openai' | 'anthropic'
model?: string
temperature?: number
maxTokens?: number
systemPrompt?: string
projectId?: string
}
interface TaskRequest {
type: 'analyze-project' | 'generate-knowledge-base'
projectId: string
repoUrl?: string
branch?: string
config?: Record<string, unknown>
}
interface TaskStatus {
id: string
projectId: string
type: string
status: 'pending' | 'queued' | 'running' | 'completed' | 'failed'
result?: Record<string, unknown>
error?: string
createdAt: string
startedAt?: string
completedAt?: string
}