|
| 1 | +import { NextRequest, NextResponse } from 'next/server' |
| 2 | +import { getUserApiKey } from '@/lib/api-keys/user-keys' |
| 3 | + |
| 4 | +type Provider = 'openai' | 'gemini' | 'cursor' | 'anthropic' | 'aigateway' |
| 5 | + |
| 6 | +// Map agents to their required providers |
| 7 | +const AGENT_PROVIDER_MAP: Record<string, Provider> = { |
| 8 | + claude: 'anthropic', |
| 9 | + codex: 'aigateway', // Codex uses Vercel AI Gateway |
| 10 | + cursor: 'cursor', |
| 11 | + gemini: 'gemini', |
| 12 | + opencode: 'openai', // OpenCode can use OpenAI or Anthropic, but primarily OpenAI |
| 13 | +} |
| 14 | + |
| 15 | +// Check if a model is an Anthropic model |
| 16 | +function isAnthropicModel(model: string): boolean { |
| 17 | + const anthropicPatterns = [ |
| 18 | + 'claude', |
| 19 | + 'sonnet', |
| 20 | + 'opus', |
| 21 | + ] |
| 22 | + const lowerModel = model.toLowerCase() |
| 23 | + return anthropicPatterns.some(pattern => lowerModel.includes(pattern)) |
| 24 | +} |
| 25 | + |
| 26 | +// Check if a model is an OpenAI model |
| 27 | +function isOpenAIModel(model: string): boolean { |
| 28 | + const openaiPatterns = [ |
| 29 | + 'gpt', |
| 30 | + 'openai', |
| 31 | + ] |
| 32 | + const lowerModel = model.toLowerCase() |
| 33 | + return openaiPatterns.some(pattern => lowerModel.includes(pattern)) |
| 34 | +} |
| 35 | + |
| 36 | +// Check if a model is a Gemini model |
| 37 | +function isGeminiModel(model: string): boolean { |
| 38 | + const geminiPatterns = [ |
| 39 | + 'gemini', |
| 40 | + ] |
| 41 | + const lowerModel = model.toLowerCase() |
| 42 | + return geminiPatterns.some(pattern => lowerModel.includes(pattern)) |
| 43 | +} |
| 44 | + |
| 45 | +export async function GET(req: NextRequest) { |
| 46 | + try { |
| 47 | + const { searchParams } = new URL(req.url) |
| 48 | + const agent = searchParams.get('agent') |
| 49 | + const model = searchParams.get('model') |
| 50 | + |
| 51 | + if (!agent) { |
| 52 | + return NextResponse.json({ error: 'Agent parameter is required' }, { status: 400 }) |
| 53 | + } |
| 54 | + |
| 55 | + let provider = AGENT_PROVIDER_MAP[agent] |
| 56 | + if (!provider) { |
| 57 | + return NextResponse.json({ error: 'Invalid agent' }, { status: 400 }) |
| 58 | + } |
| 59 | + |
| 60 | + // Override provider based on model for multi-provider agents |
| 61 | + if (model && (agent === 'cursor' || agent === 'opencode')) { |
| 62 | + if (isAnthropicModel(model)) { |
| 63 | + provider = 'anthropic' |
| 64 | + } else if (isGeminiModel(model)) { |
| 65 | + provider = 'gemini' |
| 66 | + } else if (isOpenAIModel(model)) { |
| 67 | + // For OpenAI models, prefer AI Gateway if available, otherwise use OpenAI |
| 68 | + provider = 'aigateway' |
| 69 | + } |
| 70 | + // For cursor with no recognizable pattern, keep the default 'cursor' provider |
| 71 | + } |
| 72 | + |
| 73 | + // Check if API key is available (either user's or system) |
| 74 | + const apiKey = await getUserApiKey(provider) |
| 75 | + const hasKey = !!apiKey |
| 76 | + |
| 77 | + return NextResponse.json({ |
| 78 | + success: true, |
| 79 | + hasKey, |
| 80 | + provider, |
| 81 | + agentName: agent.charAt(0).toUpperCase() + agent.slice(1), |
| 82 | + }) |
| 83 | + } catch (error) { |
| 84 | + console.error('Error checking API key:', error) |
| 85 | + return NextResponse.json({ error: 'Failed to check API key' }, { status: 500 }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
0 commit comments