SDKs & Libraries
Chat Client
Simple client for integrating with the codmir AI chat API.
Chat Client
The Chat Client is a lightweight wrapper for the codmir AI chat API. It's built into the main application and can be used for custom integrations.
Usage
import { codmir, CodmirClient } from '@/lib/codmir-sdk';
// Using the default instance
const response = await codmir.sendMessage('How do I fix this TypeScript error?');
console.log(response);
// Or create your own instance
const client = new CodmirClient();
const response = await client.sendMessage('Explain this code');API Reference
CodmirClient
class CodmirClient {
/**
* Send a message to the AI chat API.
* @param message - The user's message
* @returns The AI response as a string
*/
async sendMessage(message: string): Promise<string>;
}Integration Examples
React Component
'use client';
import { useState } from 'react';
import { codmir } from '@/lib/codmir-sdk';
export function ChatWidget() {
const [message, setMessage] = useState('');
const [response, setResponse] = useState('');
const [loading, setLoading] = useState(false);
const handleSubmit = async () => {
setLoading(true);
try {
const result = await codmir.sendMessage(message);
setResponse(result);
} finally {
setLoading(false);
}
};
return (
<div>
<input
value={message}
onChange={(e) => setMessage(e.target.value)}
placeholder="Ask codmir..."
/>
<button onClick={handleSubmit} disabled={loading}>
{loading ? 'Thinking...' : 'Send'}
</button>
{response && <div>{response}</div>}
</div>
);
}Server Action
'use server';
import { codmir } from '@/lib/codmir-sdk';
export async function askCodmir(message: string) {
return await codmir.sendMessage(message);
}Advanced: Streaming
For streaming responses, use the Vercel AI SDK directly:
import { useChat } from 'ai/react';
export function StreamingChat() {
const { messages, input, handleInputChange, handleSubmit } = useChat({
api: '/api/chat',
});
return (
<form onSubmit={handleSubmit}>
{messages.map((m) => (
<div key={m.id}>
<strong>{m.role}:</strong> {m.content}
</div>
))}
<input value={input} onChange={handleInputChange} />
</form>
);
}Chat API Endpoint
The client calls POST /api/chat with this format:
// Request
{
messages: [
{ role: 'user', content: 'Your message' }
]
}
// Response (streaming text)
"AI response content..."Authentication
The Chat API uses session-based authentication. Ensure the user is logged in before making requests.
// Check if authenticated
const session = await getSession();
if (!session) {
redirect('/login');
}
// Then use the client
const response = await codmir.sendMessage(message);Rate Limiting
The chat API is rate-limited to prevent abuse:
- 30 requests per minute per user
- Returns
429 Too Many Requestswhen exceeded
try {
await codmir.sendMessage(message);
} catch (error) {
if (error.message.includes('Rate limit')) {
console.log('Please wait before sending more messages');
}
}Related
- AI Fabrick - Advanced AI pipeline configuration
- Codec SDK - Intent capture and prompt generation