feat: Add OpenRouter as AI provider with easy switch

- Added openrouter.ts with provider configuration
- Default provider: OpenRouter with google/gemma-3-27b-it
- Switch to Gemini via AI_PROVIDER=gemini in .env.local
- Both providers use same credit system
- OpenRouter uses OpenAI-compatible API

To switch providers, set in .env.local:
AI_PROVIDER=openrouter  # default
AI_PROVIDER=gemini      # Google Gemini
This commit is contained in:
2025-12-25 23:56:24 +01:00
parent f0f36e9c03
commit fb2a8d0f7b
3 changed files with 184 additions and 54 deletions

40
src/lib/openrouter.ts Normal file
View File

@@ -0,0 +1,40 @@
import OpenAI from 'openai';
/**
* AI Provider configuration
*
* Set AI_PROVIDER in .env.local to switch:
* - "openrouter" (default) - Uses OpenRouter with Gemma 3 27B
* - "gemini" - Uses Google Gemini 2.5 Flash
*/
export type AIProvider = 'openrouter' | 'gemini';
export function getAIProvider(): AIProvider {
const provider = process.env.AI_PROVIDER?.toLowerCase();
if (provider === 'gemini') return 'gemini';
return 'openrouter'; // Default
}
/**
* OpenRouter client for vision tasks
* Uses OpenAI-compatible API
*/
export function getOpenRouterClient(): OpenAI {
const apiKey = process.env.OPENROUTER_API_KEY;
if (!apiKey) {
throw new Error('OPENROUTER_API_KEY is not configured.');
}
return new OpenAI({
baseURL: 'https://openrouter.ai/api/v1',
apiKey: apiKey,
defaultHeaders: {
'HTTP-Referer': 'https://whiskyvault.app',
'X-Title': 'WhiskyVault',
},
});
}
// Default OpenRouter model for vision tasks
export const OPENROUTER_VISION_MODEL = 'google/gemma-3-27b-it';