mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-24 21:25:45 +00:00
fix(ai): validate provider keys via /v1/models instead of a generatio… (#8340)
This commit is contained in:
@@ -4,7 +4,6 @@ const { getPreferences } = require('../../store/preferences');
|
||||
const { aiKeyStore } = require('../../store/ai-keys');
|
||||
const {
|
||||
PROVIDERS,
|
||||
MODEL_DEFINITIONS,
|
||||
listProviders,
|
||||
listModels,
|
||||
getModel,
|
||||
@@ -107,18 +106,20 @@ const registerAiIpc = (mainWindow) => {
|
||||
return { ok: false, error: `${PROVIDERS[providerId].label} is disabled` };
|
||||
}
|
||||
|
||||
const probeModel = Object.entries(MODEL_DEFINITIONS)
|
||||
.find(([, def]) => def.provider === providerId);
|
||||
if (!probeModel) {
|
||||
return { ok: false, error: `No models registered for ${providerId}` };
|
||||
}
|
||||
|
||||
try {
|
||||
const model = resolveModel(probeModel[0]);
|
||||
await generateText({ model, prompt: 'ping', maxOutputTokens: 1 });
|
||||
return { ok: true };
|
||||
const res = await PROVIDERS[providerId].validateApiKey({ apiKey });
|
||||
if (res.ok) {
|
||||
return { ok: true };
|
||||
}
|
||||
if (res.status === 401 || res.status === 403) {
|
||||
return { ok: false, error: 'Invalid API key' };
|
||||
}
|
||||
if (res.status === 429) {
|
||||
return { ok: false, error: 'Rate limited — try again in a moment' };
|
||||
}
|
||||
return { ok: false, error: `Could not verify key (HTTP ${res.status})` };
|
||||
} catch (err) {
|
||||
return { ok: false, error: err.message || 'Connection failed' };
|
||||
return { ok: false, error: 'Could not reach provider. Check your network connection.' };
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -7,14 +7,22 @@ const PROVIDERS = {
|
||||
label: 'OpenAI',
|
||||
apiKeyPlaceholder: 'sk-...',
|
||||
apiKeyHelpUrl: 'https://platform.openai.com/api-keys',
|
||||
createSdk: ({ apiKey }) => createOpenAI({ apiKey })
|
||||
createSdk: ({ apiKey }) => createOpenAI({ apiKey }),
|
||||
validateApiKey: ({ apiKey }) => fetch('https://api.openai.com/v1/models', {
|
||||
headers: { Authorization: `Bearer ${apiKey}` },
|
||||
signal: AbortSignal.timeout(10000)
|
||||
})
|
||||
},
|
||||
anthropic: {
|
||||
id: 'anthropic',
|
||||
label: 'Anthropic',
|
||||
apiKeyPlaceholder: 'sk-ant-...',
|
||||
apiKeyHelpUrl: 'https://console.anthropic.com/settings/keys',
|
||||
createSdk: ({ apiKey }) => createAnthropic({ apiKey })
|
||||
createSdk: ({ apiKey }) => createAnthropic({ apiKey }),
|
||||
validateApiKey: ({ apiKey }) => fetch('https://api.anthropic.com/v1/models', {
|
||||
headers: { 'x-api-key': apiKey, 'anthropic-version': '2023-06-01' },
|
||||
signal: AbortSignal.timeout(10000)
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user