Skip to content

Commit 8909634

Browse files
grantiguessclaude
andcommitted
fix: auto-detect Google Gemini API keys to prevent OpenRouter routing mismatch
When a user configures a Gemini API key but their stored profile has provider='openrouter', requests were incorrectly routed to OpenRouter causing 401 "User not found" errors. Added auto-detection for Google API keys (AIza prefix) in both wizard and chat request paths, matching the existing pattern for Anthropic and OpenAI keys. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2629993 commit 8909634

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

src/components/panel/views/LeftAIView.jsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3402,6 +3402,9 @@ const LeftAIView = ({ compact = false,
34023402
effectiveProvider = 'anthropic';
34033403
} else if (apiKey?.startsWith('sk-proj-') && (!effectiveProvider || effectiveProvider === 'openrouter')) {
34043404
effectiveProvider = 'openai';
3405+
} else if (apiKey?.startsWith('AIza') && (!effectiveProvider || effectiveProvider === 'openrouter')) {
3406+
console.warn('[LeftAIView] Detected Google API key with OpenRouter config. Auto-switching to Google.');
3407+
effectiveProvider = 'google';
34053408
}
34063409

34073410
// Collect any tabular data from attachments for tool access
@@ -3665,6 +3668,12 @@ const LeftAIView = ({ compact = false,
36653668
if (!apiConfig) { addMessage('ai', 'Please set up your API key first by clicking the key icon in the header.', {}, targetConversationId); return; }
36663669
const apiKey = await apiKeyManager.getAPIKey();
36673670
if (!apiKey) { addMessage('ai', 'No API key found. Please set one via the key icon.', {}, targetConversationId); return; }
3671+
// Auto-correct provider if key mismatches
3672+
let chatProvider = apiConfig?.provider;
3673+
if (apiKey?.startsWith('sk-ant-') && (!chatProvider || chatProvider === 'openrouter')) chatProvider = 'anthropic';
3674+
else if (apiKey?.startsWith('sk-proj-') && (!chatProvider || chatProvider === 'openrouter')) chatProvider = 'openai';
3675+
else if (apiKey?.startsWith('AIza') && (!chatProvider || chatProvider === 'openrouter')) chatProvider = 'google';
3676+
36683677
const response = await bridgeFetch('/api/ai/chat', {
36693678
method: 'POST',
36703679
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${apiKey}` },
@@ -3675,7 +3684,7 @@ const LeftAIView = ({ compact = false,
36753684
activeGraphId: activeGraphId || null,
36763685
graphInfo,
36773686
graphCount,
3678-
apiConfig: apiConfig ? { provider: apiConfig.provider, endpoint: apiConfig.endpoint, model: apiConfig.model, settings: apiConfig.settings } : null
3687+
apiConfig: apiConfig ? { provider: chatProvider || apiConfig.provider, endpoint: apiConfig.endpoint, model: apiConfig.model, settings: apiConfig.settings } : null
36793688
},
36803689
model: apiConfig?.model || undefined
36813690
})

wizard-server.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,10 @@ app.post('/api/wizard', async (req, res) => {
165165
: `[multimodal: ${message.length} blocks]`;
166166
console.log('[Wizard] Request:', {
167167
messagePreview,
168+
provider: llmConfig.provider,
169+
model: llmConfig.model,
168170
historyLength: conversationHistory?.length || 0,
169-
activeGraph: graphState?.activeGraphId,
170-
model: llmConfig.model
171+
activeGraph: graphState?.activeGraphId
171172
});
172173

173174
const abortController = new AbortController();

0 commit comments

Comments
 (0)