π¬ A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots π
TypeScript browser-extensionchrome-extensiondocumentationfirefox-extensionguide-generatorlocal-firstmanifest-v3privacy-firstproductivityreactscreenshotstep-by-step-guidetutorialtypescriptworkflowwxt
fix(ai): authenticate compatible key checks with inference
Mortify4315be25f047c777
diff --git a/src/core/capture/ai/__tests__/validate.test.ts b/src/core/capture/ai/__tests__/validate.test.tsindex 02671b9..917c7a0 100644--- a/src/core/capture/ai/__tests__/validate.test.ts+++ b/src/core/capture/ai/__tests__/validate.test.ts@@ -71,26 +71,71 @@ describe('validateApiKey', () => {expect(fetchMock).not.toHaveBeenCalled();});- it('checks a compatible key against the stored base URL, not openai', async () => {- fetchMock.mockResolvedValue({ ok: true, status: 200, json: () => Promise.resolve(null) });- expect(await validateApiKey('openaiCompatible', 'sk-key', 'https://api.example.com/v1')).toEqual({ valid: true });- const [url, init] = fetchMock.mock.calls[0];- expect(url).toBe('https://api.example.com/v1/models');- expect(init.headers.Authorization).toBe('Bearer sk-key');+ it('rejects a compatible key when the chat probe returns 401', async () => {+ fetchMock.mockImplementation((url: string) =>+ Promise.resolve(url.endsWith('/models') ? { ok: true, status: 200 } : { ok: false, status: 401 }),+ );+ expect(+ await validateApiKey('openaiCompatible', 'sk-key', 'https://api.example.com/v1', 'compatible-model'),+ ).toEqual({+ valid: false,+ reason: 'rejected',+ });+ expect(fetchMock).toHaveBeenCalledTimes(1);+ expect(fetchMock.mock.calls[0][0]).toBe('https://api.example.com/v1/chat/completions');});it('strips trailing slashes from a compatible base URL', async () => {fetchMock.mockResolvedValue({ ok: true, status: 200, json: () => Promise.resolve(null) });- await validateApiKey('openaiCompatible', 'sk-key', 'https://api.example.com/v1///');- const [url] = fetchMock.mock.calls[0];+ await validateApiKey('openaiCompatible', 'sk-key', 'https://api.example.com/v1///', 'compatible-model');+ const [url] = fetchMock.mock.calls[1];expect(url).toBe('https://api.example.com/v1/models');});it('does not send a compatible key anywhere when no base URL is set', async () => {- expect(await validateApiKey('openaiCompatible', 'sk-key')).toEqual({ valid: false, reason: 'network' });+ expect(await validateApiKey('openaiCompatible', 'sk-key', undefined, 'compatible-model')).toEqual({+ valid: false,+ reason: 'network',+ });+ expect(fetchMock).not.toHaveBeenCalled();+ });++ it('does not validate a compatible key when the model is empty', async () => {+ expect(await validateApiKey('openaiCompatible', 'sk-key', 'https://api.example.com/v1', ' ')).toEqual({+ valid: false,+ reason: 'network',+ });expect(fetchMock).not.toHaveBeenCalled();});+ it('probes a compatible key with the selected model, then returns catalog models', async () => {+ fetchMock.mockImplementation((url: string) =>+ Promise.resolve(+ url.endsWith('/chat/completions')+ ? { ok: true, status: 200 }+ : { ok: true, status: 200, json: () => Promise.resolve({ data: [{ id: 'model-a' }, { id: 'model-b' }] }) },+ ),+ );+ expect(await validateApiKey('openaiCompatible', 'sk-key', 'https://api.example.com/v1', 'selected-model')).toEqual({+ valid: true,+ models: ['model-a', 'model-b'],+ });+ const [probeUrl, probeInit] = fetchMock.mock.calls[0];+ expect(probeUrl).toBe('https://api.example.com/v1/chat/completions');+ expect(probeInit.method).toBe('POST');+ expect(JSON.parse(probeInit.body)).toMatchObject({ model: 'selected-model', max_tokens: 8, stream: false });+ expect(fetchMock.mock.calls[1][0]).toBe('https://api.example.com/v1/models');+ });++ it('keeps a compatible key valid when the catalog request fails', async () => {+ fetchMock.mockImplementation((url: string) =>+ Promise.resolve(url.endsWith('/chat/completions') ? { ok: true, status: 200 } : { ok: false, status: 500 }),+ );+ expect(await validateApiKey('openaiCompatible', 'sk-key', 'https://api.example.com/v1', 'selected-model')).toEqual({+ valid: true,+ });+ });+it('returns the model ids a provider lists for the key', async () => {fetchMock.mockResolvedValue({ok: true,diff --git a/src/core/capture/ai/validate.ts b/src/core/capture/ai/validate.tsindex 464d400..6ddc1b6 100644--- a/src/core/capture/ai/validate.ts+++ b/src/core/capture/ai/validate.ts@@ -38,6 +38,11 @@ function endpointUrl(provider: string, baseUrl?: string): string | null {return endpoint.url ?? null;}+function compatibleChatUrl(baseUrl?: string): string | null {+ const trimmed = baseUrl?.trim().replace(/\/+$/, '');+ return trimmed ? `${trimmed}/chat/completions` : null;+}+/** OpenAI, Anthropic and Groq all list models as `{ data: [{ id }] }`. */function parseModelIds(body: unknown): string[] | undefined {if (typeof body !== 'object' || body === null) return undefined;@@ -49,7 +54,58 @@ function parseModelIds(body: unknown): string[] | undefined {return models.length > 0 ? models : undefined;}-export async function validateApiKey(provider: string, apiKey: string, baseUrl?: string): Promise<KeyValidation> {+export async function validateApiKey(+ provider: string,+ apiKey: string,+ baseUrl?: string,+ model?: string,+): Promise<KeyValidation> {+ if (provider === 'openaiCompatible') {+ const chatUrl = compatibleChatUrl(baseUrl);+ const selectedModel = model?.trim();+ if (!chatUrl || !selectedModel) {+ logger.error('No API key validation endpoint or model for provider', provider);+ return { valid: false, reason: 'network' };+ }+ try {+ const probe = await fetch(chatUrl, {+ method: 'POST',+ headers: {+ ...ENDPOINTS[provider].headers(apiKey),+ 'Content-Type': 'application/json',+ },+ body: JSON.stringify({+ model: selectedModel,+ messages: [{ role: 'user', content: 'Reply with OK.' }],+ max_tokens: 8,+ stream: false,+ }),+ 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);+ }+ return { valid: true };+ } catch (err) {+ logger.error('API key validation request failed', err);+ return { valid: false, reason: 'network' };+ }+ }+const url = endpointUrl(provider, baseUrl);if (!url) {logger.error('No API key validation endpoint for provider', provider);diff --git a/src/entrypoints/background/index.ts b/src/entrypoints/background/index.tsindex 319cd52..ef9c541 100644--- a/src/entrypoints/background/index.ts+++ b/src/entrypoints/background/index.ts@@ -184,7 +184,7 @@ export default defineBackground(() => {onMessage('generateGuideDescription', ({ data }) => generateDescriptionOnDemand(data.guideId));- onMessage('validateApiKey', ({ data }) => validateApiKey(data.provider, data.apiKey, data.baseUrl));+ onMessage('validateApiKey', ({ data }) => validateApiKey(data.provider, data.apiKey, data.baseUrl, data.model));onMessage('rewriteSelection', ({ data }) => rewriteSelection(data.text, data.instruction));diff --git a/src/lib/messaging.ts b/src/lib/messaging.tsindex 710a026..37d224d 100644--- a/src/lib/messaging.ts+++ b/src/lib/messaging.ts@@ -111,6 +111,7 @@ export interface ValidateApiKeyData {provider: string;apiKey: string;baseUrl?: string;+ model?: string;}export interface ValidateApiKeyResponse {diff --git a/src/ui/onboarding/App.tsx b/src/ui/onboarding/App.tsxindex df74f38..b592731 100644--- a/src/ui/onboarding/App.tsx+++ b/src/ui/onboarding/App.tsx@@ -259,7 +259,7 @@ function AISetupStep({ onNext, onSkip, onBack, index, total }: StepProps) {<buttontype="button"disabled={!apiKey || aiKeyCheck.status === 'checking'}- onClick={() => void aiKeyCheck.check(provider, apiKey, baseUrl)}+ onClick={() => void aiKeyCheck.check(provider, apiKey, baseUrl, model)}className="px-4 py-2 bg-card text-foreground border border-border rounded-lg font-semibold text-xs hover:border-accent hover:text-accent transition-colors disabled:opacity-50 disabled:pointer-events-none">{i18n.t('settings.checkKey')}diff --git a/src/ui/shared/SettingsView.tsx b/src/ui/shared/SettingsView.tsxindex 6652509..66710ec 100644--- a/src/ui/shared/SettingsView.tsx+++ b/src/ui/shared/SettingsView.tsx@@ -324,7 +324,7 @@ export default function SettingsView({ onBack }: SettingsViewProps) {variant="outline"size="sm"disabled={!apiKey || aiKeyCheck.status === 'checking'}- onClick={() => void aiKeyCheck.check(provider, apiKey, baseUrl)}+ onClick={() => void aiKeyCheck.check(provider, apiKey, baseUrl, model)}className="h-8 shrink-0 rounded-lg bg-card text-[11px] font-semibold">{i18n.t('settings.checkKey')}diff --git a/src/ui/shared/key-check.tsx b/src/ui/shared/key-check.tsxindex 061233c..443122f 100644--- a/src/ui/shared/key-check.tsx+++ b/src/ui/shared/key-check.tsx@@ -10,15 +10,15 @@ export function useKeyCheck() {const [models, setModels] = useState<string[] | null>(null);const validated = useRef('');- const check = useCallback(async (provider: string, apiKey: string, baseUrl?: string) => {- const fingerprint = `${provider}:${apiKey}:${baseUrl ?? ''}`;+ const check = useCallback(async (provider: string, apiKey: string, baseUrl?: string, model?: string) => {+ const fingerprint = `${provider}:${apiKey}:${baseUrl ?? ''}:${model ?? ''}`;if (validated.current === fingerprint) {setStatus('valid');return;}setStatus('checking');setModels(null);- const result = await sendMessage('validateApiKey', { provider, apiKey, baseUrl }).catch(() => null);+ const result = await sendMessage('validateApiKey', { provider, apiKey, baseUrl, model }).catch(() => null);if (result?.valid) validated.current = fingerprint;if (result?.valid && result.models?.length) setModels(result.models);setStatus(result?.valid ? 'valid' : result?.reason === 'rejected' ? 'rejected' : 'unreachable');