|
| 1 | +# API Endpoints Documentation |
| 2 | + |
| 3 | +## Overview |
| 4 | +CODEEX AI provides RESTful API endpoints for AI-powered operations with automatic multi-provider fallback and robust error handling. |
| 5 | + |
| 6 | +## Core Features |
| 7 | +- **Multi-Provider Support**: Automatic fallback between Google AI, Hugging Face, and OpenRouter |
| 8 | +- **Error Resilience**: If one provider fails, automatically tries alternative providers |
| 9 | +- **5MB File Support**: Enhanced support for larger images and PDFs |
| 10 | +- **DuckDuckGo Integration**: Privacy-focused web search with AI-powered summarization |
| 11 | + |
| 12 | +## Available Endpoints |
| 13 | + |
| 14 | +### 1. Summarization API |
| 15 | +**Endpoint**: `POST /api/ai/summarize` |
| 16 | + |
| 17 | +**Purpose**: Summarize text content with multiple style options |
| 18 | + |
| 19 | +**Request Body**: |
| 20 | +```json |
| 21 | +{ |
| 22 | + "text": "string (required)", |
| 23 | + "style": "brief | detailed | bullets | eli5 (optional)", |
| 24 | + "preferredModel": "string (optional)" |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +**Response**: |
| 29 | +```json |
| 30 | +{ |
| 31 | + "summary": "string", |
| 32 | + "modelUsed": "string" |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +**Example Usage**: |
| 37 | +```typescript |
| 38 | +const response = await fetch('/api/ai/summarize', { |
| 39 | + method: 'POST', |
| 40 | + headers: { 'Content-Type': 'application/json' }, |
| 41 | + body: JSON.stringify({ |
| 42 | + text: 'Long text to summarize...', |
| 43 | + style: 'bullets' |
| 44 | + }) |
| 45 | +}); |
| 46 | +const data = await response.json(); |
| 47 | +``` |
| 48 | + |
| 49 | +### 2. Problem Solver API |
| 50 | +**Endpoint**: `POST /api/ai/solve` |
| 51 | + |
| 52 | +**Purpose**: Solve math problems, quizzes, coding challenges, and general questions |
| 53 | + |
| 54 | +**Request Body**: |
| 55 | +```json |
| 56 | +{ |
| 57 | + "problem": "string (required)", |
| 58 | + "tone": "helpful | formal | casual (optional)", |
| 59 | + "technicalLevel": "beginner | intermediate | expert (optional)", |
| 60 | + "preferredModel": "string (optional)" |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +**Response**: |
| 65 | +```json |
| 66 | +{ |
| 67 | + "solution": "string", |
| 68 | + "modelUsed": "string" |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +**Example Usage**: |
| 73 | +```typescript |
| 74 | +const response = await fetch('/api/ai/solve', { |
| 75 | + method: 'POST', |
| 76 | + headers: { 'Content-Type': 'application/json' }, |
| 77 | + body: JSON.stringify({ |
| 78 | + problem: 'Solve: 2x + 5 = 15', |
| 79 | + technicalLevel: 'beginner' |
| 80 | + }) |
| 81 | +}); |
| 82 | +const data = await response.json(); |
| 83 | +``` |
| 84 | + |
| 85 | +### 3. Web Search API |
| 86 | +**Endpoint**: `POST /api/ai/search` |
| 87 | + |
| 88 | +**Purpose**: Search the web using DuckDuckGo and provide AI-powered answers with citations |
| 89 | + |
| 90 | +**Request Body**: |
| 91 | +```json |
| 92 | +{ |
| 93 | + "query": "string (required)", |
| 94 | + "preferredModel": "string (optional)" |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +**Response**: |
| 99 | +```json |
| 100 | +{ |
| 101 | + "answer": "string", |
| 102 | + "sources": [ |
| 103 | + { |
| 104 | + "title": "string", |
| 105 | + "url": "string", |
| 106 | + "snippet": "string" |
| 107 | + } |
| 108 | + ], |
| 109 | + "modelUsed": "string" |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +**Example Usage**: |
| 114 | +```typescript |
| 115 | +const response = await fetch('/api/ai/search', { |
| 116 | + method: 'POST', |
| 117 | + headers: { 'Content-Type': 'application/json' }, |
| 118 | + body: JSON.stringify({ |
| 119 | + query: 'What is the latest news about AI?' |
| 120 | + }) |
| 121 | +}); |
| 122 | +const data = await response.json(); |
| 123 | +``` |
| 124 | + |
| 125 | +### 4. Image Problem Solver API |
| 126 | +**Endpoint**: `POST /api/ai/image-solver` |
| 127 | + |
| 128 | +**Purpose**: Solve problems from images (math equations, quizzes, etc.) - Supports up to 5MB |
| 129 | + |
| 130 | +**Request Body**: |
| 131 | +```json |
| 132 | +{ |
| 133 | + "imageDataUri": "string (required, data URI format)", |
| 134 | + "problemType": "math | quiz | general (optional)", |
| 135 | + "preferredModel": "string (optional)" |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +**Response**: |
| 140 | +```json |
| 141 | +{ |
| 142 | + "recognizedContent": "string", |
| 143 | + "solution": "string", |
| 144 | + "isSolvable": "boolean", |
| 145 | + "modelUsed": "string" |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +**Example Usage**: |
| 150 | +```typescript |
| 151 | +// Convert image to data URI first |
| 152 | +const fileInput = document.querySelector('input[type="file"]'); |
| 153 | +const file = fileInput.files[0]; |
| 154 | +const reader = new FileReader(); |
| 155 | + |
| 156 | +reader.onload = async (e) => { |
| 157 | + const response = await fetch('/api/ai/image-solver', { |
| 158 | + method: 'POST', |
| 159 | + headers: { 'Content-Type': 'application/json' }, |
| 160 | + body: JSON.stringify({ |
| 161 | + imageDataUri: e.target.result, |
| 162 | + problemType: 'math' |
| 163 | + }) |
| 164 | + }); |
| 165 | + const data = await response.json(); |
| 166 | +}; |
| 167 | + |
| 168 | +reader.readAsDataURL(file); |
| 169 | +``` |
| 170 | + |
| 171 | +### 5. PDF Analyzer API |
| 172 | +**Endpoint**: `POST /api/ai/pdf-analyzer` |
| 173 | + |
| 174 | +**Purpose**: Analyze PDF documents and answer questions - Supports up to 5MB |
| 175 | + |
| 176 | +**Request Body**: |
| 177 | +```json |
| 178 | +{ |
| 179 | + "pdfDataUri": "string (required, data URI format)", |
| 180 | + "question": "string (required)", |
| 181 | + "preferredModel": "string (optional)" |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +**Response**: |
| 186 | +```json |
| 187 | +{ |
| 188 | + "answer": "string", |
| 189 | + "modelUsed": "string" |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +**Example Usage**: |
| 194 | +```typescript |
| 195 | +const fileInput = document.querySelector('input[type="file"]'); |
| 196 | +const file = fileInput.files[0]; |
| 197 | +const reader = new FileReader(); |
| 198 | + |
| 199 | +reader.onload = async (e) => { |
| 200 | + const response = await fetch('/api/ai/pdf-analyzer', { |
| 201 | + method: 'POST', |
| 202 | + headers: { 'Content-Type': 'application/json' }, |
| 203 | + body: JSON.stringify({ |
| 204 | + pdfDataUri: e.target.result, |
| 205 | + question: 'What is the main topic of this document?' |
| 206 | + }) |
| 207 | + }); |
| 208 | + const data = await response.json(); |
| 209 | +}; |
| 210 | + |
| 211 | +reader.readAsDataURL(file); |
| 212 | +``` |
| 213 | + |
| 214 | +## Error Handling |
| 215 | + |
| 216 | +### Standard Error Response |
| 217 | +```json |
| 218 | +{ |
| 219 | + "error": "string" |
| 220 | +} |
| 221 | +``` |
| 222 | + |
| 223 | +### HTTP Status Codes |
| 224 | +- `200`: Success |
| 225 | +- `400`: Bad Request (invalid input) |
| 226 | +- `500`: Internal Server Error (AI service failure) |
| 227 | + |
| 228 | +### Multi-Provider Fallback |
| 229 | +All endpoints automatically handle provider failures: |
| 230 | +1. Try preferred model (if specified) |
| 231 | +2. Try fallback models in the same category |
| 232 | +3. Try models from alternative providers |
| 233 | +4. Return error only if all providers fail |
| 234 | + |
| 235 | +### Example Error Handling |
| 236 | +```typescript |
| 237 | +try { |
| 238 | + const response = await fetch('/api/ai/solve', { |
| 239 | + method: 'POST', |
| 240 | + headers: { 'Content-Type': 'application/json' }, |
| 241 | + body: JSON.stringify({ problem: 'Solve x^2 = 16' }) |
| 242 | + }); |
| 243 | + |
| 244 | + if (!response.ok) { |
| 245 | + const error = await response.json(); |
| 246 | + console.error('API Error:', error.error); |
| 247 | + return; |
| 248 | + } |
| 249 | + |
| 250 | + const data = await response.json(); |
| 251 | + console.log('Solution:', data.solution); |
| 252 | + console.log('Model used:', data.modelUsed); |
| 253 | +} catch (error) { |
| 254 | + console.error('Network error:', error); |
| 255 | +} |
| 256 | +``` |
| 257 | + |
| 258 | +## Rate Limiting & Quotas |
| 259 | +- Rate limits depend on the AI provider being used |
| 260 | +- Automatic fallback to alternative providers when rate limits are hit |
| 261 | +- No application-level rate limiting (relies on provider limits) |
| 262 | + |
| 263 | +## Authentication |
| 264 | +- Currently no authentication required for API endpoints |
| 265 | +- Consider adding authentication for production deployments |
| 266 | +- Firebase Auth can be integrated for user-specific rate limiting |
| 267 | + |
| 268 | +## Best Practices |
| 269 | + |
| 270 | +### 1. File Size Management |
| 271 | +```typescript |
| 272 | +// Check file size before upload (5MB limit) |
| 273 | +const MAX_SIZE = 5 * 1024 * 1024; // 5MB |
| 274 | + |
| 275 | +if (file.size > MAX_SIZE) { |
| 276 | + alert('File size exceeds 5MB limit'); |
| 277 | + return; |
| 278 | +} |
| 279 | +``` |
| 280 | + |
| 281 | +### 2. Loading States |
| 282 | +```typescript |
| 283 | +const [loading, setLoading] = useState(false); |
| 284 | + |
| 285 | +const handleSubmit = async () => { |
| 286 | + setLoading(true); |
| 287 | + try { |
| 288 | + const response = await fetch('/api/ai/solve', { |
| 289 | + method: 'POST', |
| 290 | + headers: { 'Content-Type': 'application/json' }, |
| 291 | + body: JSON.stringify({ problem: userInput }) |
| 292 | + }); |
| 293 | + const data = await response.json(); |
| 294 | + // Handle response |
| 295 | + } finally { |
| 296 | + setLoading(false); |
| 297 | + } |
| 298 | +}; |
| 299 | +``` |
| 300 | + |
| 301 | +### 3. Retry Logic |
| 302 | +```typescript |
| 303 | +async function fetchWithRetry(url: string, options: RequestInit, retries = 3) { |
| 304 | + for (let i = 0; i < retries; i++) { |
| 305 | + try { |
| 306 | + const response = await fetch(url, options); |
| 307 | + if (response.ok) return response; |
| 308 | + if (response.status === 400) throw new Error('Bad request'); |
| 309 | + // Retry on 500 errors |
| 310 | + } catch (error) { |
| 311 | + if (i === retries - 1) throw error; |
| 312 | + await new Promise(resolve => setTimeout(resolve, 1000 * (i + 1))); |
| 313 | + } |
| 314 | + } |
| 315 | +} |
| 316 | +``` |
| 317 | + |
| 318 | +## Integration with Existing Commands |
| 319 | + |
| 320 | +### Slash Commands |
| 321 | +The API endpoints integrate with existing slash commands: |
| 322 | +- `/solve` → Uses `/api/ai/solve` |
| 323 | +- `/summarize` → Uses `/api/ai/summarize` |
| 324 | +- `/search` → Uses `/api/ai/search` |
| 325 | + |
| 326 | +### Command Router Integration |
| 327 | +```typescript |
| 328 | +// In command-router.ts |
| 329 | +if (command === '/solve') { |
| 330 | + const response = await fetch('/api/ai/solve', { |
| 331 | + method: 'POST', |
| 332 | + headers: { 'Content-Type': 'application/json' }, |
| 333 | + body: JSON.stringify({ problem: args }) |
| 334 | + }); |
| 335 | + return await response.json(); |
| 336 | +} |
| 337 | +``` |
| 338 | + |
| 339 | +## Provider Configuration |
| 340 | + |
| 341 | +### Required Environment Variables |
| 342 | +```bash |
| 343 | +# Hugging Face (Required - 100% FREE) |
| 344 | +HUGGINGFACE_API_KEY=your_key_here |
| 345 | +``` |
| 346 | + |
| 347 | +### Provider Priority |
| 348 | +1. **Google AI** (Primary) - Most reliable, best for multimodal |
| 349 | +2. **OpenRouter** (Secondary) - Access to multiple models |
| 350 | +3. **Hugging Face** (Tertiary) - Open-source models |
| 351 | + |
| 352 | +## Monitoring & Debugging |
| 353 | + |
| 354 | +### Server-Side Logging |
| 355 | +All endpoints log errors to console: |
| 356 | +```typescript |
| 357 | +console.error('Solve API error:', error); |
| 358 | +``` |
| 359 | + |
| 360 | +### Client-Side Debugging |
| 361 | +```typescript |
| 362 | +// Enable verbose logging |
| 363 | +const DEBUG = true; |
| 364 | + |
| 365 | +if (DEBUG) { |
| 366 | + console.log('Request:', requestBody); |
| 367 | + console.log('Response:', responseData); |
| 368 | + console.log('Model used:', responseData.modelUsed); |
| 369 | +} |
| 370 | +``` |
| 371 | + |
| 372 | +## Future Enhancements |
| 373 | +- [ ] Add authentication and user-specific rate limiting |
| 374 | +- [ ] Implement caching for repeated queries |
| 375 | +- [ ] Add streaming responses for long-running operations |
| 376 | +- [ ] Add webhook support for async processing |
| 377 | +- [ ] Implement request queuing for high load |
| 378 | +- [ ] Add analytics and usage tracking |
0 commit comments