Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots 📖

fix(ai): distinguish model errors during key checks

Mortify431571b79073fc4a
diff --git a/src/core/capture/ai/__tests__/validate.test.ts b/src/core/capture/ai/__tests__/validate.test.ts
index 917c7a0..1004cc3 100644
--- a/src/core/capture/ai/__tests__/validate.test.ts
+++ b/src/core/capture/ai/__tests__/validate.test.ts
@@ -81,8 +81,9 @@ describe('validateApiKey', () => {
valid: false,
reason: 'rejected',
});
- expect(fetchMock).toHaveBeenCalledTimes(1);
+ expect(fetchMock).toHaveBeenCalledTimes(2);
expect(fetchMock.mock.calls[0][0]).toBe('https://api.example.com/v1/chat/completions');
+ expect(fetchMock.mock.calls[1][0]).toBe('https://api.example.com/v1/models');
});
it('strips trailing slashes from a compatible base URL', async () => {
@@ -101,11 +102,65 @@ describe('validateApiKey', () => {
});
it('does not validate a compatible key when the model is empty', async () => {
+ fetchMock.mockResolvedValue({
+ ok: true,
+ status: 200,
+ json: () => Promise.resolve({ data: [{ id: 'discovered-model' }] }),
+ });
expect(await validateApiKey('openaiCompatible', 'sk-key', 'https://api.example.com/v1', ' ')).toEqual({
valid: false,
- reason: 'network',
+ reason: 'model-required',
+ models: ['discovered-model'],
+ });
+ expect(fetchMock).toHaveBeenCalledTimes(1);
+ expect(fetchMock.mock.calls[0][0]).toBe('https://api.example.com/v1/models');
+ });
+
+ it('keeps a compatible key rejected when the catalog includes the selected model', async () => {
+ fetchMock.mockImplementation((url: string) =>
+ Promise.resolve(
+ url.endsWith('/chat/completions')
+ ? { ok: false, status: 401 }
+ : { ok: true, status: 200, json: () => Promise.resolve({ data: [{ id: 'public-model' }] }) },
+ ),
+ );
+ expect(await validateApiKey('openaiCompatible', 'sk-key', 'https://api.example.com/v1', 'public-model')).toEqual({
+ valid: false,
+ reason: 'rejected',
+ models: ['public-model'],
+ });
+ });
+
+ it('classifies a rejected inference for a model absent from a successful catalog as model-invalid', async () => {
+ fetchMock.mockImplementation((url: string) =>
+ Promise.resolve(
+ url.endsWith('/chat/completions')
+ ? { ok: false, status: 401 }
+ : { ok: true, status: 200, json: () => Promise.resolve({ data: [{ id: 'available-model' }] }) },
+ ),
+ );
+ expect(
+ await validateApiKey('openaiCompatible', 'sk-key', 'https://api.example.com/v1', ' missing-model '),
+ ).toEqual({
+ valid: false,
+ reason: 'model-invalid',
+ models: ['available-model'],
+ });
+ });
+
+ it('classifies a network inference failure for a model absent from a successful catalog as model-invalid', async () => {
+ fetchMock.mockImplementation((url: string) =>
+ Promise.resolve(
+ url.endsWith('/chat/completions')
+ ? { ok: false, status: 500 }
+ : { ok: true, status: 200, json: () => Promise.resolve({ data: [{ id: 'available-model' }] }) },
+ ),
+ );
+ expect(await validateApiKey('openaiCompatible', 'sk-key', 'https://api.example.com/v1', 'missing-model')).toEqual({
+ valid: false,
+ reason: 'model-invalid',
+ models: ['available-model'],
});
- expect(fetchMock).not.toHaveBeenCalled();
});
it('probes a compatible key with the selected model, then returns catalog models', async () => {
diff --git a/src/core/capture/ai/validate.ts b/src/core/capture/ai/validate.ts
index 6ddc1b6..c421506 100644
--- a/src/core/capture/ai/validate.ts
+++ b/src/core/capture/ai/validate.ts
@@ -1,6 +1,8 @@
import { logger } from '@/lib/logger';
-export type KeyValidation = { valid: true; models?: string[] } | { valid: false; reason: 'rejected' | 'network' };
+export type KeyValidation =
+ | { valid: true; models?: string[] }
+ | { valid: false; reason: 'rejected' | 'network' | 'model-required' | 'model-invalid'; models?: string[] };
const REQUEST_TIMEOUT_MS = 10_000;
@@ -54,6 +56,23 @@ function parseModelIds(body: unknown): string[] | undefined {
return models.length > 0 ? models : undefined;
}
+async function fetchCompatibleModels(apiKey: string, baseUrl?: string): Promise<string[] | undefined> {
+ const url = endpointUrl('openaiCompatible', baseUrl);
+ if (!url) return undefined;
+
+ try {
+ const catalog = await fetch(url, {
+ headers: ENDPOINTS.openaiCompatible.headers(apiKey),
+ signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
+ });
+ if (!catalog.ok) return undefined;
+ return parseModelIds(await catalog.json().catch(() => null));
+ } catch (err) {
+ logger.error('Compatible model catalog request failed', err);
+ return undefined;
+ }
+}
+
export async function validateApiKey(
provider: string,
apiKey: string,
@@ -62,11 +81,19 @@ export async function validateApiKey(
): Promise<KeyValidation> {
if (provider === 'openaiCompatible') {
const chatUrl = compatibleChatUrl(baseUrl);
+ const catalogUrl = endpointUrl(provider, baseUrl);
const selectedModel = model?.trim();
- if (!chatUrl || !selectedModel) {
+ if (!chatUrl || !catalogUrl) {
logger.error('No API key validation endpoint or model for provider', provider);
return { valid: false, reason: 'network' };
}
+
+ if (!selectedModel) {
+ const models = await fetchCompatibleModels(apiKey, baseUrl);
+ return models ? { valid: false, reason: 'model-required', models } : { valid: false, reason: 'model-required' };
+ }
+
+ let result: KeyValidation;
try {
const probe = await fetch(chatUrl, {
method: 'POST',
@@ -83,27 +110,23 @@ export async function validateApiKey(
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
});
if (!probe.ok) {
- if (probe.status === 401 || probe.status === 403) return { valid: false, reason: 'rejected' };
- return { valid: false, reason: 'network' };
- }
-
- try {
- const catalog = await fetch(endpointUrl(provider, baseUrl) as string, {
- headers: ENDPOINTS[provider].headers(apiKey),
- signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
- });
- if (catalog.ok) {
- const models = parseModelIds(await catalog.json().catch(() => null));
- return models ? { valid: true, models } : { valid: true };
- }
- } catch (err) {
- logger.error('Compatible model catalog request failed', err);
+ result =
+ probe.status === 401 || probe.status === 403
+ ? { valid: false, reason: 'rejected' }
+ : { valid: false, reason: 'network' };
+ } else {
+ result = { valid: true };
}
- return { valid: true };
} catch (err) {
logger.error('API key validation request failed', err);
- return { valid: false, reason: 'network' };
+ result = { valid: false, reason: 'network' };
+ }
+
+ const models = await fetchCompatibleModels(apiKey, baseUrl);
+ if (!result.valid && models && !models.includes(selectedModel)) {
+ return { valid: false, reason: 'model-invalid', models };
}
+ return models ? { ...result, models } : result;
}
const url = endpointUrl(provider, baseUrl);
diff --git a/src/lib/messaging.ts b/src/lib/messaging.ts
index 37d224d..0f1006f 100644
--- a/src/lib/messaging.ts
+++ b/src/lib/messaging.ts
@@ -116,7 +116,7 @@ export interface ValidateApiKeyData {
export interface ValidateApiKeyResponse {
valid: boolean;
- reason?: 'rejected' | 'network';
+ reason?: 'rejected' | 'network' | 'model-required' | 'model-invalid';
models?: string[];
}
diff --git a/src/locales/de.yml b/src/locales/de.yml
index 1de840f..d02fe6d 100644
--- a/src/locales/de.yml
+++ b/src/locales/de.yml
@@ -132,6 +132,8 @@ settings:
validatingKey: "Schlüssel wird geprüft…"
keyValid: API-Schlüssel bestätigt.
keyInvalid: Dieser API-Schlüssel wurde abgelehnt. Prüfe ihn und versuche es erneut.
+ keyModelRequired: Wähle ein Modell, bevor du diesen Schlüssel prüfst.
+ keyModelInvalid: Das ausgewählte Modell ist bei diesem Anbieter nicht verfügbar.
checkKey: Schlüssel prüfen
keyUnreachable: Der Anbieter war nicht erreichbar. Prüfe deine Verbindung und versuche es erneut.
modelsFound: "$1 Modelle verfügbar"
diff --git a/src/locales/en.yml b/src/locales/en.yml
index 4b6b9c9..06b3934 100644
--- a/src/locales/en.yml
+++ b/src/locales/en.yml
@@ -126,6 +126,8 @@ settings:
validatingKey: "Checking key\u2026"
keyValid: API key verified.
keyInvalid: That API key was rejected. Check it and try again.
+ keyModelRequired: Choose a model before checking this key.
+ keyModelInvalid: The selected model is not available from this provider.
checkKey: Check key
keyUnreachable: Could not reach the provider. Check your connection and try again.
modelsFound: "$1 models available"
diff --git a/src/locales/es.yml b/src/locales/es.yml
index 55d358d..0f59de0 100644
--- a/src/locales/es.yml
+++ b/src/locales/es.yml
@@ -126,6 +126,8 @@ settings:
validatingKey: "Comprobando clave\u2026"
keyValid: Clave de API verificada.
keyInvalid: "Esa clave de API fue rechazada. Compru\xE9bala e int\xE9ntalo de nuevo."
+ keyModelRequired: Elige un modelo antes de comprobar esta clave.
+ keyModelInvalid: El modelo seleccionado no está disponible en este proveedor.
checkKey: Comprobar clave
keyUnreachable: No se pudo conectar con el proveedor. Revisa tu conexión e inténtalo de nuevo.
modelsFound: "$1 modelos disponibles"
diff --git a/src/locales/fr.yml b/src/locales/fr.yml
index 4213edb..920216a 100644
--- a/src/locales/fr.yml
+++ b/src/locales/fr.yml
@@ -126,6 +126,8 @@ settings:
validatingKey: "V\xE9rification de la cl\xE9\u2026"
keyValid: "Cl\xE9 API v\xE9rifi\xE9e."
keyInvalid: "Cette cl\xE9 API a \xE9t\xE9 refus\xE9e. V\xE9rifiez-la et r\xE9essayez."
+ keyModelRequired: Choisissez un modèle avant de vérifier cette clé.
+ keyModelInvalid: "Le modèle sélectionné n'est pas disponible chez ce fournisseur."
checkKey: Vérifier la clé
keyUnreachable: Le fournisseur est injoignable. Vérifiez votre connexion et réessayez.
modelsFound: "$1 modèles disponibles"
diff --git a/src/locales/pt-BR.yml b/src/locales/pt-BR.yml
index 4c2655d..12ce322 100644
--- a/src/locales/pt-BR.yml
+++ b/src/locales/pt-BR.yml
@@ -126,6 +126,8 @@ settings:
validatingKey: "Verificando chave\u2026"
keyValid: Chave de API verificada.
keyInvalid: Essa chave de API foi recusada. Verifique-a e tente novamente.
+ keyModelRequired: Escolha um modelo antes de verificar esta chave.
+ keyModelInvalid: O modelo selecionado não está disponível neste provedor.
checkKey: Verificar chave
keyUnreachable: Não deu pra falar com o provedor. Confere sua conexão e tenta de novo.
modelsFound: "$1 modelos disponíveis"
diff --git a/src/ui/onboarding/App.tsx b/src/ui/onboarding/App.tsx
index b592731..2124c77 100644
--- a/src/ui/onboarding/App.tsx
+++ b/src/ui/onboarding/App.tsx
@@ -149,24 +149,25 @@ function AISetupStep({ onNext, onSkip, onBack, index, total }: StepProps) {
const nextModel = AI_PROVIDERS[newProvider].defaultModel;
setProvider(newProvider);
setModel(nextModel);
- aiKeyCheck.setStatus(null);
+ aiKeyCheck.reset();
void localStorage.set({ aiProvider: newProvider, aiModel: nextModel });
};
const handleModelChange = (nextModel: string) => {
setModel(nextModel);
+ aiKeyCheck.reset();
void localStorage.set({ aiModel: nextModel });
};
const handleApiKeyChange = (nextKey: string) => {
setApiKey(nextKey);
- aiKeyCheck.setStatus(null);
+ aiKeyCheck.reset();
void localStorage.set({ aiApiKey: nextKey });
};
const handleBaseUrlChange = (nextUrl: string) => {
setBaseUrl(nextUrl);
- aiKeyCheck.setStatus(null);
+ aiKeyCheck.reset();
void localStorage.set({ aiBaseUrl: nextUrl });
};
diff --git a/src/ui/shared/SettingsView.tsx b/src/ui/shared/SettingsView.tsx
index 66710ec..5698a55 100644
--- a/src/ui/shared/SettingsView.tsx
+++ b/src/ui/shared/SettingsView.tsx
@@ -182,7 +182,7 @@ export default function SettingsView({ onBack }: SettingsViewProps) {
const handleProviderChange = (newProvider: AIProviderKey) => {
setProvider(newProvider);
- aiKeyCheck.setStatus(null);
+ aiKeyCheck.reset();
setCustomModel(false);
setModel(AI_PROVIDERS[newProvider].defaultModel);
};
@@ -191,10 +191,12 @@ export default function SettingsView({ onBack }: SettingsViewProps) {
if (value === CUSTOM_MODEL_VALUE) {
setCustomModel(true);
setModel('');
+ aiKeyCheck.reset();
return;
}
setCustomModel(false);
setModel(value);
+ aiKeyCheck.reset();
};
const providerConfig = AI_PROVIDERS[provider];
@@ -279,7 +281,10 @@ export default function SettingsView({ onBack }: SettingsViewProps) {
{usingCustomModel && (
<Input
value={model}
- onChange={(e) => setModel(e.target.value)}
+ onChange={(e) => {
+ setModel(e.target.value);
+ aiKeyCheck.reset();
+ }}
placeholder={providerConfig.defaultModel}
aria-label={i18n.t('settings.modelCustom')}
className="mt-1.5 h-8 text-[12px] rounded-lg border-border"
@@ -298,7 +303,7 @@ export default function SettingsView({ onBack }: SettingsViewProps) {
value={baseUrl}
onChange={(e) => {
setBaseUrl(e.target.value);
- aiKeyCheck.setStatus(null);
+ aiKeyCheck.reset();
}}
placeholder="https://api.example.com/v1"
aria-label={i18n.t('settings.baseUrl')}
@@ -315,7 +320,7 @@ export default function SettingsView({ onBack }: SettingsViewProps) {
value={apiKey}
onChange={(e) => {
setApiKey(e.target.value);
- aiKeyCheck.setStatus(null);
+ aiKeyCheck.reset();
}}
placeholder="sk-..."
className="h-8 text-[12px] rounded-lg border-border"
@@ -497,7 +502,7 @@ export default function SettingsView({ onBack }: SettingsViewProps) {
value={voiceProvider}
onChange={(e) => {
setVoiceProvider(e.target.value as VoiceProvider);
- voiceKeyCheck.setStatus(null);
+ voiceKeyCheck.reset();
}}
className="w-full border border-border rounded-lg px-3 py-2 text-[13px] text-foreground bg-card font-medium outline-none focus:border-ring focus:ring-2 focus:ring-ring/10"
>
@@ -514,7 +519,7 @@ export default function SettingsView({ onBack }: SettingsViewProps) {
value={voiceApiKey}
onChange={(e) => {
setVoiceApiKey(e.target.value);
- voiceKeyCheck.setStatus(null);
+ voiceKeyCheck.reset();
}}
placeholder={voiceProvider === 'groq' ? 'gsk_...' : 'sk-...'}
/>
diff --git a/src/ui/shared/__tests__/key-check.test.tsx b/src/ui/shared/__tests__/key-check.test.tsx
new file mode 100644
index 0000000..7890b21
--- /dev/null
+++ b/src/ui/shared/__tests__/key-check.test.tsx
@@ -0,0 +1,63 @@
+// @vitest-environment jsdom
+import '@testing-library/jest-dom/vitest';
+import { act, render, renderHook, screen } from '@testing-library/react';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+const { sendMessageMock } = vi.hoisted(() => ({ sendMessageMock: vi.fn() }));
+
+vi.mock('@/lib/messaging', () => ({ sendMessage: sendMessageMock }));
+
+import { KeyStatusNote, useKeyCheck } from '@/ui/shared/key-check';
+
+describe('useKeyCheck', () => {
+ beforeEach(() => {
+ sendMessageMock.mockReset();
+ });
+
+ it('retains models returned with an unsuccessful validation', async () => {
+ sendMessageMock.mockResolvedValue({ valid: false, reason: 'rejected', models: ['public-model'] });
+ const { result } = renderHook(() => useKeyCheck());
+
+ await act(async () => {
+ await result.current.check('openaiCompatible', 'sk-key', 'https://api.example.com/v1', 'selected-model');
+ });
+
+ expect(result.current.status).toBe('rejected');
+ expect(result.current.models).toEqual(['public-model']);
+ });
+
+ it('maps an invalid model response to a distinct status', async () => {
+ sendMessageMock.mockResolvedValue({ valid: false, reason: 'model-invalid', models: ['public-model'] });
+ const { result } = renderHook(() => useKeyCheck());
+
+ await act(async () => {
+ await result.current.check('openaiCompatible', 'sk-key', 'https://api.example.com/v1', 'missing-model');
+ });
+
+ expect(result.current.status).toBe('model-invalid');
+ expect(result.current.models).toEqual(['public-model']);
+ });
+
+ it('clears status and models when reset', async () => {
+ sendMessageMock.mockResolvedValue({ valid: false, reason: 'model-required', models: ['public-model'] });
+ const { result } = renderHook(() => useKeyCheck());
+
+ await act(async () => {
+ await result.current.check('openaiCompatible', 'sk-key', 'https://api.example.com/v1');
+ result.current.reset();
+ });
+
+ expect(result.current.status).toBeNull();
+ expect(result.current.models).toBeNull();
+ });
+});
+
+it('shows a distinct model-required note', () => {
+ render(<KeyStatusNote status="model-required" />);
+ expect(screen.getByText('settings.keyModelRequired')).toBeInTheDocument();
+});
+
+it('shows a distinct model-invalid note', () => {
+ render(<KeyStatusNote status="model-invalid" />);
+ expect(screen.getByText('settings.keyModelInvalid')).toBeInTheDocument();
+});
diff --git a/src/ui/shared/key-check.tsx b/src/ui/shared/key-check.tsx
index 443122f..25b95e2 100644
--- a/src/ui/shared/key-check.tsx
+++ b/src/ui/shared/key-check.tsx
@@ -3,12 +3,13 @@ import { useCallback, useRef, useState } from 'react';
import { i18n } from '#imports';
import { sendMessage } from '@/lib/messaging';
-export type KeyStatus = 'checking' | 'valid' | 'rejected' | 'unreachable' | null;
+export type KeyStatus = 'checking' | 'valid' | 'rejected' | 'unreachable' | 'model-required' | 'model-invalid' | null;
export function useKeyCheck() {
const [status, setStatus] = useState<KeyStatus>(null);
const [models, setModels] = useState<string[] | null>(null);
const validated = useRef('');
+ const requestId = useRef(0);
const check = useCallback(async (provider: string, apiKey: string, baseUrl?: string, model?: string) => {
const fingerprint = `${provider}:${apiKey}:${baseUrl ?? ''}:${model ?? ''}`;
@@ -16,15 +17,34 @@ export function useKeyCheck() {
setStatus('valid');
return;
}
+ const currentRequestId = ++requestId.current;
setStatus('checking');
setModels(null);
const result = await sendMessage('validateApiKey', { provider, apiKey, baseUrl, model }).catch(() => null);
+ if (requestId.current !== currentRequestId) return;
if (result?.valid) validated.current = fingerprint;
- if (result?.valid && result.models?.length) setModels(result.models);
- setStatus(result?.valid ? 'valid' : result?.reason === 'rejected' ? 'rejected' : 'unreachable');
+ setModels(result?.models?.length ? result.models : null);
+ setStatus(
+ result?.valid
+ ? 'valid'
+ : result?.reason === 'rejected'
+ ? 'rejected'
+ : result?.reason === 'model-required'
+ ? 'model-required'
+ : result?.reason === 'model-invalid'
+ ? 'model-invalid'
+ : 'unreachable',
+ );
+ }, []);
+
+ const reset = useCallback(() => {
+ requestId.current += 1;
+ validated.current = '';
+ setStatus(null);
+ setModels(null);
}, []);
- return { status, setStatus, models, check };
+ return { status, models, check, reset };
}
export function KeyStatusNote({ status }: { status: KeyStatus }) {
@@ -49,6 +69,20 @@ export function KeyStatusNote({ status }: { status: KeyStatus }) {
if (status === 'unreachable') {
return <p className="mt-1 text-[11px] text-muted-foreground">{i18n.t('settings.keyUnreachable')}</p>;
}
+ if (status === 'model-required') {
+ return (
+ <p className="mt-1 text-[11px] text-destructive" role="alert">
+ {i18n.t('settings.keyModelRequired')}
+ </p>
+ );
+ }
+ if (status === 'model-invalid') {
+ return (
+ <p className="mt-1 text-[11px] text-destructive" role="alert">
+ {i18n.t('settings.keyModelInvalid')}
+ </p>
+ );
+ }
return null;
}