qualia-framework 2.2.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/framework/hooks/confirm-delete.sh +2 -2
  2. package/framework/hooks/migration-validate.sh +2 -2
  3. package/framework/hooks/pre-commit.sh +4 -4
  4. package/framework/hooks/pre-deploy-gate.sh +6 -21
  5. package/framework/hooks/session-context-loader.sh +1 -1
  6. package/framework/install.sh +9 -4
  7. package/framework/qualia-engine/VERSION +1 -1
  8. package/framework/qualia-engine/templates/projects/ai-agent.md +1 -1
  9. package/framework/qualia-engine/templates/projects/voice-agent.md +4 -4
  10. package/framework/qualia-engine/templates/roadmap.md +10 -0
  11. package/framework/qualia-engine/templates/state.md +3 -0
  12. package/framework/qualia-engine/workflows/new-project.md +22 -21
  13. package/framework/skills/client-handoff/SKILL.md +125 -0
  14. package/framework/skills/collab-onboard/SKILL.md +111 -0
  15. package/framework/skills/docs-lookup/SKILL.md +4 -3
  16. package/framework/skills/learn/SKILL.md +1 -1
  17. package/framework/skills/mobile-expo/SKILL.md +117 -4
  18. package/framework/skills/openrouter-agent/SKILL.md +922 -0
  19. package/framework/skills/qualia/SKILL.md +11 -5
  20. package/framework/skills/qualia-audit-milestone/SKILL.md +5 -2
  21. package/framework/skills/qualia-complete-milestone/SKILL.md +9 -5
  22. package/framework/skills/qualia-execute-phase/SKILL.md +5 -2
  23. package/framework/skills/qualia-help/SKILL.md +96 -62
  24. package/framework/skills/qualia-new-project/SKILL.md +184 -62
  25. package/framework/skills/qualia-plan-phase/SKILL.md +5 -2
  26. package/framework/skills/qualia-verify-work/SKILL.md +14 -4
  27. package/framework/skills/qualia-workflow/SKILL.md +5 -5
  28. package/framework/skills/ship/SKILL.md +32 -6
  29. package/framework/skills/voice-agent/SKILL.md +1174 -269
  30. package/package.json +1 -1
@@ -0,0 +1,922 @@
1
+ ---
2
+ name: openrouter-agent
3
+ description: "Build AI agents and chatbots using OpenRouter API — model selection, streaming, tool/function calling, cost-aware model switching, error handling, and provider failover. Use this skill whenever the user says 'build AI agent', 'build chatbot', 'openrouter', 'AI chat', 'LLM integration', or wants to integrate any LLM into a project. Also trigger when code imports from openrouter, or user mentions model selection, AI streaming, or chat endpoints."
4
+ tags: [ai-agent, openrouter, llm, chatbot, streaming]
5
+ ---
6
+
7
+ # OpenRouter Agent Builder
8
+
9
+ Build AI agents and chatbots using OpenRouter as the unified LLM gateway. One API key, every model.
10
+
11
+ **Announce at start:** "Activating OpenRouter agent builder. Let me set up your AI chat integration."
12
+
13
+ ## Why OpenRouter
14
+
15
+ - Single API key for Claude, GPT-4o, Mistral, Llama, Gemini, and 200+ models
16
+ - OpenAI SDK compatible — just swap `baseURL` and `apiKey`
17
+ - Automatic failover between providers
18
+ - Usage tracking and cost management built-in
19
+ - No vendor lock-in — switch models with one string change
20
+
21
+ ## 1. API Setup
22
+
23
+ ### Base Configuration
24
+
25
+ ```
26
+ Base URL: https://openrouter.ai/api/v1
27
+ Auth: Bearer $OPENROUTER_API_KEY
28
+ Headers: HTTP-Referer: https://yoursite.com
29
+ X-Title: YourAppName
30
+ ```
31
+
32
+ ### Environment Variable
33
+
34
+ ```env
35
+ # Server-side ONLY — never prefix with NEXT_PUBLIC_
36
+ OPENROUTER_API_KEY=sk-or-v1-...
37
+ ```
38
+
39
+ ### OpenAI SDK Compatibility
40
+
41
+ ```typescript
42
+ // lib/openrouter.ts
43
+ import OpenAI from 'openai';
44
+
45
+ export const openrouter = new OpenAI({
46
+ baseURL: 'https://openrouter.ai/api/v1',
47
+ apiKey: process.env.OPENROUTER_API_KEY!,
48
+ defaultHeaders: {
49
+ 'HTTP-Referer': process.env.NEXT_PUBLIC_SITE_URL || 'https://yoursite.com',
50
+ 'X-Title': process.env.NEXT_PUBLIC_APP_NAME || 'YourApp',
51
+ },
52
+ });
53
+ ```
54
+
55
+ ## 2. Model Selection Guide
56
+
57
+ | Use Case | Model | Why | Cost (in/out per M tokens) |
58
+ |----------|-------|-----|----------------------------|
59
+ | General chat | `anthropic/claude-sonnet-4-20250514` | Best balance of quality and cost | $3 / $15 |
60
+ | Complex reasoning | `anthropic/claude-opus-4-20250514` | Most capable, deep analysis | $15 / $75 |
61
+ | Fast / cheap | `mistralai/mistral-small-latest` | Low latency, low cost | $0.1 / $0.3 |
62
+ | Code generation | `anthropic/claude-sonnet-4-20250514` | Best at code tasks | $3 / $15 |
63
+ | Long context | `google/gemini-2.0-flash-001` | 1M token context window | $0.1 / $0.4 |
64
+ | Vision / multimodal | `anthropic/claude-sonnet-4-20250514` | Image understanding + reasoning | $3 / $15 |
65
+ | Budget chat | `meta-llama/llama-3.3-70b-instruct` | Open source, very cheap | $0.13 / $0.20 |
66
+ | Summarization | `mistralai/mistral-small-latest` | Fast, good at extraction | $0.1 / $0.3 |
67
+
68
+ ### Model Selection Constants
69
+
70
+ ```typescript
71
+ // lib/ai/models.ts
72
+ export const MODELS = {
73
+ // Primary
74
+ SMART: 'anthropic/claude-sonnet-4-20250514',
75
+ POWERFUL: 'anthropic/claude-opus-4-20250514',
76
+ FAST: 'mistralai/mistral-small-latest',
77
+ LONG_CONTEXT: 'google/gemini-2.0-flash-001',
78
+ BUDGET: 'meta-llama/llama-3.3-70b-instruct',
79
+ } as const;
80
+
81
+ export type ModelId = (typeof MODELS)[keyof typeof MODELS];
82
+ ```
83
+
84
+ ## 3. Basic Integration (Next.js + Vercel AI SDK)
85
+
86
+ ### Install Dependencies
87
+
88
+ ```bash
89
+ npm install ai openai zod
90
+ ```
91
+
92
+ ### Streaming Chat API Route
93
+
94
+ ```typescript
95
+ // app/api/chat/route.ts
96
+ import { streamText } from 'ai';
97
+ import { createOpenAI } from '@ai-sdk/openai';
98
+ import { z } from 'zod';
99
+
100
+ const openrouter = createOpenAI({
101
+ baseURL: 'https://openrouter.ai/api/v1',
102
+ apiKey: process.env.OPENROUTER_API_KEY!,
103
+ headers: {
104
+ 'HTTP-Referer': process.env.NEXT_PUBLIC_SITE_URL || 'https://yoursite.com',
105
+ 'X-Title': process.env.NEXT_PUBLIC_APP_NAME || 'YourApp',
106
+ },
107
+ });
108
+
109
+ const RequestSchema = z.object({
110
+ messages: z.array(z.object({
111
+ role: z.enum(['user', 'assistant', 'system']),
112
+ content: z.string(),
113
+ })).min(1),
114
+ model: z.string().optional(),
115
+ });
116
+
117
+ export async function POST(req: Request) {
118
+ const body = await req.json();
119
+ const parsed = RequestSchema.safeParse(body);
120
+
121
+ if (!parsed.success) {
122
+ return Response.json({ error: parsed.error.flatten() }, { status: 400 });
123
+ }
124
+
125
+ const { messages, model } = parsed.data;
126
+
127
+ const result = streamText({
128
+ model: openrouter(model || 'anthropic/claude-sonnet-4-20250514'),
129
+ system: `You are a helpful assistant. Be concise and accurate.`,
130
+ messages,
131
+ maxTokens: 4096,
132
+ });
133
+
134
+ return result.toDataStreamResponse();
135
+ }
136
+ ```
137
+
138
+ ### Client-Side Chat UI (React)
139
+
140
+ ```typescript
141
+ // app/chat/page.tsx
142
+ 'use client';
143
+
144
+ import { useChat } from '@ai-sdk/react';
145
+
146
+ export default function ChatPage() {
147
+ const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({
148
+ api: '/api/chat',
149
+ });
150
+
151
+ return (
152
+ <div>
153
+ <div>
154
+ {messages.map((msg) => (
155
+ <div key={msg.id}>
156
+ <strong>{msg.role}:</strong> {msg.content}
157
+ </div>
158
+ ))}
159
+ </div>
160
+ <form onSubmit={handleSubmit}>
161
+ <input
162
+ value={input}
163
+ onChange={handleInputChange}
164
+ placeholder="Type a message..."
165
+ disabled={isLoading}
166
+ />
167
+ <button type="submit" disabled={isLoading}>Send</button>
168
+ </form>
169
+ </div>
170
+ );
171
+ }
172
+ ```
173
+
174
+ ### System Prompt Management
175
+
176
+ ```typescript
177
+ // lib/ai/prompts.ts
178
+
179
+ export function buildSystemPrompt(context: {
180
+ agentName: string;
181
+ agentRole: string;
182
+ instructions: string[];
183
+ constraints?: string[];
184
+ }): string {
185
+ const lines = [
186
+ `You are ${context.agentName}, ${context.agentRole}.`,
187
+ '',
188
+ '## Instructions',
189
+ ...context.instructions.map(i => `- ${i}`),
190
+ ];
191
+
192
+ if (context.constraints?.length) {
193
+ lines.push('', '## Constraints', ...context.constraints.map(c => `- ${c}`));
194
+ }
195
+
196
+ return lines.join('\n');
197
+ }
198
+ ```
199
+
200
+ ## 4. Tool / Function Calling
201
+
202
+ ### Define Tools
203
+
204
+ ```typescript
205
+ // lib/ai/tools.ts
206
+ import { tool } from 'ai';
207
+ import { z } from 'zod';
208
+
209
+ export const weatherTool = tool({
210
+ description: 'Get the current weather for a location',
211
+ parameters: z.object({
212
+ city: z.string().describe('The city name'),
213
+ country: z.string().optional().describe('ISO country code'),
214
+ }),
215
+ execute: async ({ city, country }) => {
216
+ // Replace with actual weather API call
217
+ const response = await fetch(
218
+ `https://api.weatherapi.com/v1/current.json?key=${process.env.WEATHER_API_KEY}&q=${city}${country ? `,${country}` : ''}`
219
+ );
220
+ const data = await response.json();
221
+ return {
222
+ temperature: data.current.temp_c,
223
+ condition: data.current.condition.text,
224
+ humidity: data.current.humidity,
225
+ };
226
+ },
227
+ });
228
+
229
+ export const searchDatabaseTool = tool({
230
+ description: 'Search the knowledge base for relevant information',
231
+ parameters: z.object({
232
+ query: z.string().describe('The search query'),
233
+ limit: z.number().optional().default(5).describe('Max results'),
234
+ }),
235
+ execute: async ({ query, limit }) => {
236
+ const { createClient } = await import('@/lib/supabase/server');
237
+ const supabase = await createClient();
238
+
239
+ const { data, error } = await supabase
240
+ .from('knowledge_base')
241
+ .select('title, content')
242
+ .textSearch('content', query)
243
+ .limit(limit);
244
+
245
+ if (error) throw error;
246
+ return data;
247
+ },
248
+ });
249
+ ```
250
+
251
+ ### API Route with Tools
252
+
253
+ ```typescript
254
+ // app/api/chat/route.ts (with tools)
255
+ import { streamText } from 'ai';
256
+ import { createOpenAI } from '@ai-sdk/openai';
257
+ import { weatherTool, searchDatabaseTool } from '@/lib/ai/tools';
258
+
259
+ const openrouter = createOpenAI({
260
+ baseURL: 'https://openrouter.ai/api/v1',
261
+ apiKey: process.env.OPENROUTER_API_KEY!,
262
+ headers: {
263
+ 'HTTP-Referer': process.env.NEXT_PUBLIC_SITE_URL || 'https://yoursite.com',
264
+ 'X-Title': process.env.NEXT_PUBLIC_APP_NAME || 'YourApp',
265
+ },
266
+ });
267
+
268
+ export async function POST(req: Request) {
269
+ const { messages } = await req.json();
270
+
271
+ const result = streamText({
272
+ model: openrouter('anthropic/claude-sonnet-4-20250514'),
273
+ system: 'You are a helpful assistant with access to tools. Use them when needed.',
274
+ messages,
275
+ tools: {
276
+ weather: weatherTool,
277
+ searchDatabase: searchDatabaseTool,
278
+ },
279
+ maxSteps: 5, // Allow up to 5 tool call rounds
280
+ maxTokens: 4096,
281
+ });
282
+
283
+ return result.toDataStreamResponse();
284
+ }
285
+ ```
286
+
287
+ ### Manual Tool Calling (without Vercel AI SDK)
288
+
289
+ ```typescript
290
+ // lib/ai/tool-handler.ts
291
+ import { openrouter } from '@/lib/openrouter';
292
+
293
+ interface ToolDefinition {
294
+ name: string;
295
+ description: string;
296
+ parameters: Record<string, unknown>;
297
+ execute: (args: Record<string, unknown>) => Promise<unknown>;
298
+ }
299
+
300
+ export async function chatWithTools(
301
+ messages: Array<{ role: string; content: string }>,
302
+ tools: ToolDefinition[],
303
+ model = 'anthropic/claude-sonnet-4-20250514',
304
+ maxRounds = 5,
305
+ ) {
306
+ const openaiTools = tools.map(t => ({
307
+ type: 'function' as const,
308
+ function: {
309
+ name: t.name,
310
+ description: t.description,
311
+ parameters: t.parameters,
312
+ },
313
+ }));
314
+
315
+ let currentMessages = [...messages];
316
+
317
+ for (let round = 0; round < maxRounds; round++) {
318
+ const response = await openrouter.chat.completions.create({
319
+ model,
320
+ messages: currentMessages,
321
+ tools: openaiTools,
322
+ max_tokens: 4096,
323
+ });
324
+
325
+ const choice = response.choices[0];
326
+
327
+ if (choice.finish_reason !== 'tool_calls' || !choice.message.tool_calls?.length) {
328
+ // No more tool calls — return the final response
329
+ return choice.message.content;
330
+ }
331
+
332
+ // Add assistant message with tool calls
333
+ currentMessages.push(choice.message as never);
334
+
335
+ // Execute each tool call
336
+ for (const toolCall of choice.message.tool_calls) {
337
+ const tool = tools.find(t => t.name === toolCall.function.name);
338
+ if (!tool) {
339
+ currentMessages.push({
340
+ role: 'tool',
341
+ content: JSON.stringify({ error: `Unknown tool: ${toolCall.function.name}` }),
342
+ tool_call_id: toolCall.id,
343
+ } as never);
344
+ continue;
345
+ }
346
+
347
+ try {
348
+ const args = JSON.parse(toolCall.function.arguments);
349
+ const result = await tool.execute(args);
350
+ currentMessages.push({
351
+ role: 'tool',
352
+ content: JSON.stringify(result),
353
+ tool_call_id: toolCall.id,
354
+ } as never);
355
+ } catch (error) {
356
+ currentMessages.push({
357
+ role: 'tool',
358
+ content: JSON.stringify({ error: String(error) }),
359
+ tool_call_id: toolCall.id,
360
+ } as never);
361
+ }
362
+ }
363
+ }
364
+
365
+ // Exceeded max rounds — get final response without tools
366
+ const final = await openrouter.chat.completions.create({
367
+ model,
368
+ messages: currentMessages,
369
+ max_tokens: 4096,
370
+ });
371
+
372
+ return final.choices[0].message.content;
373
+ }
374
+ ```
375
+
376
+ ## 5. Cost-Aware Model Switching
377
+
378
+ ### Smart Router
379
+
380
+ ```typescript
381
+ // lib/ai/router.ts
382
+ import { MODELS, type ModelId } from './models';
383
+
384
+ interface RoutingContext {
385
+ messageLength: number;
386
+ hasImages: boolean;
387
+ conversationTurns: number;
388
+ taskType: 'chat' | 'code' | 'analysis' | 'summarize';
389
+ budgetCentsRemaining?: number;
390
+ }
391
+
392
+ // Cost per 1K tokens (input + output estimate)
393
+ const MODEL_COST_PER_1K: Record<ModelId, number> = {
394
+ [MODELS.POWERFUL]: 0.090, // ~$90/M combined
395
+ [MODELS.SMART]: 0.018, // ~$18/M combined
396
+ [MODELS.FAST]: 0.0004, // ~$0.4/M combined
397
+ [MODELS.LONG_CONTEXT]: 0.0005,
398
+ [MODELS.BUDGET]: 0.00033,
399
+ };
400
+
401
+ export function selectModel(ctx: RoutingContext): ModelId {
402
+ // Budget-constrained: use cheapest model
403
+ if (ctx.budgetCentsRemaining !== undefined && ctx.budgetCentsRemaining < 5) {
404
+ return MODELS.FAST;
405
+ }
406
+
407
+ // Long input: use long context model
408
+ if (ctx.messageLength > 50_000) {
409
+ return MODELS.LONG_CONTEXT;
410
+ }
411
+
412
+ // Complex analysis: use the most capable model
413
+ if (ctx.taskType === 'analysis' && ctx.conversationTurns > 3) {
414
+ return MODELS.POWERFUL;
415
+ }
416
+
417
+ // Code generation: Sonnet is best
418
+ if (ctx.taskType === 'code') {
419
+ return MODELS.SMART;
420
+ }
421
+
422
+ // Short simple queries: use fast model
423
+ if (ctx.messageLength < 200 && ctx.conversationTurns < 2) {
424
+ return MODELS.FAST;
425
+ }
426
+
427
+ // Default: Sonnet (best balance)
428
+ return MODELS.SMART;
429
+ }
430
+ ```
431
+
432
+ ### Token Budget Tracker
433
+
434
+ ```typescript
435
+ // lib/ai/budget.ts
436
+
437
+ interface UsageRecord {
438
+ model: string;
439
+ promptTokens: number;
440
+ completionTokens: number;
441
+ costCents: number;
442
+ timestamp: Date;
443
+ }
444
+
445
+ export class BudgetTracker {
446
+ private usage: UsageRecord[] = [];
447
+ private budgetCents: number;
448
+
449
+ constructor(budgetCents: number) {
450
+ this.budgetCents = budgetCents;
451
+ }
452
+
453
+ record(model: string, promptTokens: number, completionTokens: number) {
454
+ const costCents = this.calculateCost(model, promptTokens, completionTokens);
455
+ this.usage.push({
456
+ model,
457
+ promptTokens,
458
+ completionTokens,
459
+ costCents,
460
+ timestamp: new Date(),
461
+ });
462
+ return costCents;
463
+ }
464
+
465
+ get remaining(): number {
466
+ const spent = this.usage.reduce((sum, u) => sum + u.costCents, 0);
467
+ return Math.max(0, this.budgetCents - spent);
468
+ }
469
+
470
+ get spent(): number {
471
+ return this.usage.reduce((sum, u) => sum + u.costCents, 0);
472
+ }
473
+
474
+ private calculateCost(model: string, promptTokens: number, completionTokens: number): number {
475
+ // Costs in dollars per million tokens -> convert to cents
476
+ const rates: Record<string, { input: number; output: number }> = {
477
+ 'anthropic/claude-opus-4-20250514': { input: 15, output: 75 },
478
+ 'anthropic/claude-sonnet-4-20250514': { input: 3, output: 15 },
479
+ 'mistralai/mistral-small-latest': { input: 0.1, output: 0.3 },
480
+ 'google/gemini-2.0-flash-001': { input: 0.1, output: 0.4 },
481
+ 'meta-llama/llama-3.3-70b-instruct': { input: 0.13, output: 0.20 },
482
+ };
483
+
484
+ const rate = rates[model] || { input: 3, output: 15 }; // Default to Sonnet pricing
485
+ const inputCost = (promptTokens / 1_000_000) * rate.input * 100; // dollars -> cents
486
+ const outputCost = (completionTokens / 1_000_000) * rate.output * 100;
487
+ return inputCost + outputCost;
488
+ }
489
+ }
490
+ ```
491
+
492
+ ### Failover Chain
493
+
494
+ ```typescript
495
+ // lib/ai/failover.ts
496
+ import { openrouter } from '@/lib/openrouter';
497
+ import { MODELS } from './models';
498
+
499
+ const FAILOVER_CHAIN = [
500
+ MODELS.SMART, // Try Sonnet first
501
+ MODELS.FAST, // Fall back to Mistral
502
+ MODELS.BUDGET, // Last resort: Llama
503
+ ];
504
+
505
+ export async function chatWithFailover(
506
+ messages: Array<{ role: string; content: string }>,
507
+ options: {
508
+ preferredModel?: string;
509
+ maxTokens?: number;
510
+ temperature?: number;
511
+ } = {}
512
+ ) {
513
+ const chain = options.preferredModel
514
+ ? [options.preferredModel, ...FAILOVER_CHAIN.filter(m => m !== options.preferredModel)]
515
+ : FAILOVER_CHAIN;
516
+
517
+ let lastError: Error | null = null;
518
+
519
+ for (const model of chain) {
520
+ try {
521
+ const response = await openrouter.chat.completions.create({
522
+ model,
523
+ messages,
524
+ max_tokens: options.maxTokens ?? 4096,
525
+ temperature: options.temperature ?? 0.7,
526
+ });
527
+
528
+ return {
529
+ content: response.choices[0].message.content,
530
+ model,
531
+ usage: response.usage,
532
+ failedOver: model !== chain[0],
533
+ };
534
+ } catch (error) {
535
+ lastError = error as Error;
536
+ const status = (error as { status?: number }).status;
537
+
538
+ // Only retry on provider errors, not client errors
539
+ if (status && status >= 400 && status < 500 && status !== 429) {
540
+ throw error; // Client error — don't retry
541
+ }
542
+
543
+ console.warn(`Model ${model} failed, trying next:`, (error as Error).message);
544
+ }
545
+ }
546
+
547
+ throw new Error(`All models failed. Last error: ${lastError?.message}`);
548
+ }
549
+ ```
550
+
551
+ ## 6. Error Handling
552
+
553
+ ### Retry with Exponential Backoff
554
+
555
+ ```typescript
556
+ // lib/ai/retry.ts
557
+
558
+ interface RetryOptions {
559
+ maxRetries?: number;
560
+ baseDelayMs?: number;
561
+ maxDelayMs?: number;
562
+ }
563
+
564
+ export async function withRetry<T>(
565
+ fn: () => Promise<T>,
566
+ options: RetryOptions = {}
567
+ ): Promise<T> {
568
+ const { maxRetries = 3, baseDelayMs = 1000, maxDelayMs = 30000 } = options;
569
+
570
+ let lastError: Error | null = null;
571
+
572
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
573
+ try {
574
+ return await fn();
575
+ } catch (error) {
576
+ lastError = error as Error;
577
+ const status = (error as { status?: number }).status;
578
+
579
+ // Don't retry on non-retryable errors
580
+ if (status && status >= 400 && status < 500 && status !== 429) {
581
+ throw error;
582
+ }
583
+
584
+ if (attempt < maxRetries) {
585
+ const delay = Math.min(baseDelayMs * Math.pow(2, attempt), maxDelayMs);
586
+ const jitter = delay * (0.5 + Math.random() * 0.5);
587
+ await new Promise(resolve => setTimeout(resolve, jitter));
588
+ }
589
+ }
590
+ }
591
+
592
+ throw lastError;
593
+ }
594
+
595
+ // Usage:
596
+ // const response = await withRetry(() => openrouter.chat.completions.create({...}));
597
+ ```
598
+
599
+ ### OpenRouter-Specific Error Handling
600
+
601
+ ```typescript
602
+ // lib/ai/errors.ts
603
+
604
+ export class AIError extends Error {
605
+ constructor(
606
+ message: string,
607
+ public code: string,
608
+ public status?: number,
609
+ public model?: string,
610
+ ) {
611
+ super(message);
612
+ this.name = 'AIError';
613
+ }
614
+ }
615
+
616
+ export function handleOpenRouterError(error: unknown): AIError {
617
+ const err = error as {
618
+ status?: number;
619
+ error?: { message?: string; code?: string; type?: string };
620
+ message?: string;
621
+ };
622
+
623
+ const status = err.status;
624
+ const message = err.error?.message || err.message || 'Unknown error';
625
+ const code = err.error?.code || err.error?.type || 'unknown';
626
+
627
+ switch (status) {
628
+ case 400:
629
+ return new AIError(`Bad request: ${message}`, 'bad_request', status);
630
+ case 401:
631
+ return new AIError('Invalid OpenRouter API key', 'auth_error', status);
632
+ case 402:
633
+ return new AIError('OpenRouter credit balance exhausted', 'insufficient_credits', status);
634
+ case 429:
635
+ return new AIError('Rate limited — slow down or upgrade plan', 'rate_limited', status);
636
+ case 502:
637
+ case 503:
638
+ return new AIError(`Model provider unavailable: ${message}`, 'provider_down', status);
639
+ default:
640
+ return new AIError(message, code, status);
641
+ }
642
+ }
643
+ ```
644
+
645
+ ### Graceful Degradation in API Route
646
+
647
+ ```typescript
648
+ // app/api/chat/route.ts (production-grade)
649
+ import { streamText } from 'ai';
650
+ import { createOpenAI } from '@ai-sdk/openai';
651
+ import { selectModel } from '@/lib/ai/router';
652
+ import { handleOpenRouterError } from '@/lib/ai/errors';
653
+ import { z } from 'zod';
654
+
655
+ const openrouter = createOpenAI({
656
+ baseURL: 'https://openrouter.ai/api/v1',
657
+ apiKey: process.env.OPENROUTER_API_KEY!,
658
+ headers: {
659
+ 'HTTP-Referer': process.env.NEXT_PUBLIC_SITE_URL || 'https://yoursite.com',
660
+ 'X-Title': process.env.NEXT_PUBLIC_APP_NAME || 'YourApp',
661
+ },
662
+ });
663
+
664
+ const RequestSchema = z.object({
665
+ messages: z.array(z.object({
666
+ role: z.enum(['user', 'assistant', 'system']),
667
+ content: z.string().max(100_000),
668
+ })).min(1).max(100),
669
+ model: z.string().optional(),
670
+ });
671
+
672
+ export async function POST(req: Request) {
673
+ try {
674
+ const body = await req.json();
675
+ const parsed = RequestSchema.safeParse(body);
676
+
677
+ if (!parsed.success) {
678
+ return Response.json({ error: parsed.error.flatten() }, { status: 400 });
679
+ }
680
+
681
+ const { messages } = parsed.data;
682
+
683
+ // Smart model selection
684
+ const lastMessage = messages[messages.length - 1];
685
+ const model = parsed.data.model || selectModel({
686
+ messageLength: lastMessage.content.length,
687
+ hasImages: false,
688
+ conversationTurns: messages.length,
689
+ taskType: 'chat',
690
+ });
691
+
692
+ const result = streamText({
693
+ model: openrouter(model),
694
+ system: 'You are a helpful assistant. Be concise and accurate.',
695
+ messages,
696
+ maxTokens: 4096,
697
+ });
698
+
699
+ return result.toDataStreamResponse();
700
+ } catch (error) {
701
+ const aiError = handleOpenRouterError(error);
702
+
703
+ // Log for monitoring
704
+ console.error(`[AI Error] ${aiError.code}:`, aiError.message);
705
+
706
+ return Response.json(
707
+ { error: aiError.message, code: aiError.code },
708
+ { status: aiError.status || 500 }
709
+ );
710
+ }
711
+ }
712
+ ```
713
+
714
+ ## 7. Security Checklist
715
+
716
+ - **API key server-side only** — `OPENROUTER_API_KEY` in `.env.local`, never `NEXT_PUBLIC_`
717
+ - **Rate limiting** — Apply rate limits on `/api/chat` (use `@upstash/ratelimit` or similar)
718
+ - **Input validation** — Zod schema on all request bodies
719
+ - **Input sanitization** — Strip or escape user input before sending to LLM
720
+ - **Output validation** — Never render LLM output with `dangerouslySetInnerHTML`
721
+ - **maxTokens always set** — Prevent runaway costs from unbounded responses
722
+ - **Message count cap** — Limit conversation length (e.g., max 100 messages)
723
+ - **Content length cap** — Reject messages over a sane limit (e.g., 100K chars)
724
+ - **No service_role in client** — All Supabase mutations through server-side client
725
+
726
+ ### Rate Limiting Example
727
+
728
+ ```typescript
729
+ // lib/rate-limit.ts
730
+ import { Ratelimit } from '@upstash/ratelimit';
731
+ import { Redis } from '@upstash/redis';
732
+
733
+ const ratelimit = new Ratelimit({
734
+ redis: Redis.fromEnv(),
735
+ limiter: Ratelimit.slidingWindow(20, '1 m'), // 20 requests per minute
736
+ analytics: true,
737
+ });
738
+
739
+ export async function checkRateLimit(identifier: string) {
740
+ const { success, limit, remaining, reset } = await ratelimit.limit(identifier);
741
+ return { success, limit, remaining, reset };
742
+ }
743
+
744
+ // In API route:
745
+ // const ip = req.headers.get('x-forwarded-for') || 'anonymous';
746
+ // const { success } = await checkRateLimit(ip);
747
+ // if (!success) return Response.json({ error: 'Rate limited' }, { status: 429 });
748
+ ```
749
+
750
+ ## 8. Conversation Storage (Supabase)
751
+
752
+ ### Schema
753
+
754
+ ```sql
755
+ -- Conversations table
756
+ CREATE TABLE conversations (
757
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
758
+ user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
759
+ title TEXT,
760
+ model TEXT NOT NULL DEFAULT 'anthropic/claude-sonnet-4-20250514',
761
+ metadata JSONB DEFAULT '{}',
762
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
763
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
764
+ );
765
+
766
+ -- Messages table
767
+ CREATE TABLE messages (
768
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
769
+ conversation_id UUID NOT NULL REFERENCES conversations(id) ON DELETE CASCADE,
770
+ role TEXT NOT NULL CHECK (role IN ('user', 'assistant', 'system', 'tool')),
771
+ content TEXT NOT NULL,
772
+ model TEXT,
773
+ prompt_tokens INT,
774
+ completion_tokens INT,
775
+ cost_cents NUMERIC(10, 6),
776
+ metadata JSONB DEFAULT '{}',
777
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
778
+ );
779
+
780
+ -- RLS
781
+ ALTER TABLE conversations ENABLE ROW LEVEL SECURITY;
782
+ ALTER TABLE messages ENABLE ROW LEVEL SECURITY;
783
+
784
+ CREATE POLICY "Users read own conversations" ON conversations
785
+ FOR ALL USING (user_id = auth.uid());
786
+
787
+ CREATE POLICY "Users read own messages" ON messages
788
+ FOR ALL USING (
789
+ EXISTS (
790
+ SELECT 1 FROM conversations c
791
+ WHERE c.id = messages.conversation_id
792
+ AND c.user_id = auth.uid()
793
+ )
794
+ );
795
+
796
+ -- Indexes
797
+ CREATE INDEX idx_conversations_user ON conversations(user_id);
798
+ CREATE INDEX idx_messages_conversation ON messages(conversation_id);
799
+ CREATE INDEX idx_messages_created ON messages(created_at);
800
+ ```
801
+
802
+ ### Save Conversation Helper
803
+
804
+ ```typescript
805
+ // lib/ai/storage.ts
806
+ import { createClient } from '@/lib/supabase/server';
807
+
808
+ export async function saveMessage(
809
+ conversationId: string,
810
+ role: 'user' | 'assistant' | 'system' | 'tool',
811
+ content: string,
812
+ usage?: { promptTokens?: number; completionTokens?: number; costCents?: number; model?: string }
813
+ ) {
814
+ const supabase = await createClient();
815
+
816
+ const { error } = await supabase.from('messages').insert({
817
+ conversation_id: conversationId,
818
+ role,
819
+ content,
820
+ model: usage?.model,
821
+ prompt_tokens: usage?.promptTokens,
822
+ completion_tokens: usage?.completionTokens,
823
+ cost_cents: usage?.costCents,
824
+ });
825
+
826
+ if (error) throw error;
827
+
828
+ // Update conversation timestamp
829
+ await supabase
830
+ .from('conversations')
831
+ .update({ updated_at: new Date().toISOString() })
832
+ .eq('id', conversationId);
833
+ }
834
+
835
+ export async function loadConversation(conversationId: string) {
836
+ const supabase = await createClient();
837
+
838
+ const { data, error } = await supabase
839
+ .from('messages')
840
+ .select('role, content')
841
+ .eq('conversation_id', conversationId)
842
+ .order('created_at', { ascending: true });
843
+
844
+ if (error) throw error;
845
+ return data;
846
+ }
847
+
848
+ export async function createConversation(userId: string, title?: string) {
849
+ const supabase = await createClient();
850
+
851
+ const { data, error } = await supabase
852
+ .from('conversations')
853
+ .insert({ user_id: userId, title: title || 'New Chat' })
854
+ .select('id')
855
+ .single();
856
+
857
+ if (error) throw error;
858
+ return data.id;
859
+ }
860
+ ```
861
+
862
+ ## Quick Start Checklist
863
+
864
+ When user asks to build an AI agent or chatbot, follow this order:
865
+
866
+ 1. **Dependencies**: `npm install ai @ai-sdk/openai openai zod`
867
+ 2. **Environment**: Add `OPENROUTER_API_KEY` to `.env.local`
868
+ 3. **OpenRouter client**: Create `lib/openrouter.ts`
869
+ 4. **Model constants**: Create `lib/ai/models.ts`
870
+ 5. **API route**: Create `app/api/chat/route.ts` with streaming
871
+ 6. **Client UI**: Create chat page with `useChat` hook
872
+ 7. **Tools** (if needed): Define in `lib/ai/tools.ts`, wire into route
873
+ 8. **Storage** (if needed): Run Supabase migration, create `lib/ai/storage.ts`
874
+ 9. **Cost routing** (if needed): Create `lib/ai/router.ts`
875
+ 10. **Error handling**: Add retry, failover, graceful degradation
876
+ 11. **Security**: Rate limiting, Zod validation, maxTokens cap
877
+
878
+ ## Key Decisions to Ask User
879
+
880
+ - **Model**: Which model for primary use? (Default: Sonnet for balance)
881
+ - **Streaming**: Stream responses or wait for full response? (Default: stream)
882
+ - **Tools**: Does the agent need to call external APIs or query databases?
883
+ - **Persistence**: Store conversations in Supabase? (Recommended for production)
884
+ - **Auth**: Require login to chat? (Recommended — use Supabase auth)
885
+ - **Cost controls**: Budget cap per user? Smart model routing?
886
+ - **Rate limiting**: How many requests per minute? (Default: 20/min)
887
+
888
+ ## Environment Variables Needed
889
+
890
+ ```env
891
+ # Required
892
+ OPENROUTER_API_KEY=sk-or-v1-...
893
+
894
+ # Optional — for conversation storage
895
+ SUPABASE_URL=https://xxx.supabase.co
896
+ SUPABASE_ANON_KEY=eyJ...
897
+ SUPABASE_SERVICE_ROLE_KEY=eyJ...
898
+
899
+ # Optional — for rate limiting
900
+ UPSTASH_REDIS_REST_URL=https://...
901
+ UPSTASH_REDIS_REST_TOKEN=...
902
+
903
+ # App metadata (sent to OpenRouter for tracking)
904
+ NEXT_PUBLIC_SITE_URL=https://yoursite.com
905
+ NEXT_PUBLIC_APP_NAME=YourApp
906
+ ```
907
+
908
+ ## Integration with Other Skills
909
+
910
+ - **rag** — Add RAG retrieval as a tool for grounded responses
911
+ - **voice-agent** — Use OpenRouter for voice agent LLM backend
912
+ - **supabase** — Conversation storage, user auth, RLS
913
+ - **frontend-master** — Build polished chat UI
914
+
915
+ ## Trigger Phrases
916
+
917
+ - "build AI agent" / "build chatbot" / "chat feature"
918
+ - "openrouter" / "LLM integration" / "AI streaming"
919
+ - "model selection" / "which AI model"
920
+ - "chat endpoint" / "chat API"
921
+ - "function calling" / "tool calling"
922
+ - "AI cost" / "model routing" / "failover"