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(desktop): send AI requests from main so a valid key is not reported rejected

Luis Ortiz3d017470cd69
diff --git a/AGENTS.md b/AGENTS.md
index e40332f..e988d37 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -618,6 +618,20 @@ rather than the placeholder the extension fills in with AI. Desktop has no AI ti
comes from the recorded application, or a generic one where no application was identified. Without
it the guide screen waits forever on a title that is never written.
+**Every AI request goes through the main process.** A renderer is an ordinary web origin, so a
+`fetch` to `api.anthropic.com` is blocked by CORS and throws β€” which reads as a rejected key when it
+is really a request that never left. The extension never hits this because it validates in the
+background service worker, where `host_permissions` exempts it, and that is the thing the desktop had
+no equivalent of. `CoreEnv` therefore carries an optional `fetch` and core calls it through
+`coreFetch`, so `validateApiKey`, `getAIDescription` and `generateGuideMeta` all route through
+whatever the surface supplies. The desktop supplies a wrapper over `mimik:ai:fetch`, which runs
+Electron's `net.fetch` in main. It refuses any url that is not `http:` or `https:`, because the
+renderer names the url and main is the one holding the network.
+
+A failed key check says which failure it was. `reason: 'network'` is worded "could not reach the
+provider", not "rejected" β€” the two are indistinguishable to a user and only one of them is their
+key's fault.
+
Descriptions and the guide's name are written by the user's own provider key when there is one, and
by rule when there is not. `getAIDescription` takes a serialised context string rather than a
`DOMContext`, because the desktop has no DOM to hand it: `serializeScreenContext` writes the same
diff --git a/apps/desktop/src/main/__tests__/ai-fetch.test.ts b/apps/desktop/src/main/__tests__/ai-fetch.test.ts
new file mode 100644
index 0000000..81d2f30
--- /dev/null
+++ b/apps/desktop/src/main/__tests__/ai-fetch.test.ts
@@ -0,0 +1,15 @@
+import { describe, expect, it } from 'vitest';
+import { allowedEndpoint } from '../ai-fetch';
+
+describe('allowedEndpoint', () => {
+ it('allows the provider APIs and a local server', () => {
+ expect(allowedEndpoint('https://api.anthropic.com/v1/models')).toBe(true);
+ expect(allowedEndpoint('http://localhost:11434/v1/models')).toBe(true);
+ });
+
+ it('refuses anything that is not an http url', () => {
+ expect(allowedEndpoint('file:///etc/passwd')).toBe(false);
+ expect(allowedEndpoint('mimik-screenshot://abc')).toBe(false);
+ expect(allowedEndpoint('not a url')).toBe(false);
+ });
+});
diff --git a/apps/desktop/src/main/ai-fetch.ts b/apps/desktop/src/main/ai-fetch.ts
new file mode 100644
index 0000000..899db48
--- /dev/null
+++ b/apps/desktop/src/main/ai-fetch.ts
@@ -0,0 +1,46 @@
+import { ipcMain, net } from 'electron';
+
+export interface AiRequest {
+ url: string;
+ method?: string;
+ headers?: Record<string, string>;
+ body?: string;
+}
+
+export interface AiResponse {
+ status: number;
+ statusText: string;
+ headers: Record<string, string>;
+ body: string;
+}
+
+const ALLOWED = new Set(['http:', 'https:']);
+
+export function allowedEndpoint(url: string): boolean {
+ try {
+ return ALLOWED.has(new URL(url).protocol);
+ } catch {
+ return false;
+ }
+}
+
+export function registerAiFetch(): void {
+ ipcMain.handle('mimik:ai:fetch', async (_event, request: AiRequest): Promise<AiResponse> => {
+ if (!allowedEndpoint(request.url)) throw new Error('unsupported url');
+ const response = await net.fetch(request.url, {
+ method: request.method ?? 'GET',
+ headers: request.headers,
+ body: request.body,
+ });
+ const headers: Record<string, string> = {};
+ response.headers.forEach((value, key) => {
+ headers[key] = value;
+ });
+ return {
+ status: response.status,
+ statusText: response.statusText,
+ headers,
+ body: await response.text(),
+ };
+ });
+}
diff --git a/apps/desktop/src/main/index.ts b/apps/desktop/src/main/index.ts
index 42c1214..7094d85 100644
--- a/apps/desktop/src/main/index.ts
+++ b/apps/desktop/src/main/index.ts
@@ -1,5 +1,6 @@
import { join } from 'node:path';
import { app, BrowserWindow, dialog, ipcMain, Menu, nativeImage, protocol, screen, shell, Tray } from 'electron';
+import { registerAiFetch } from './ai-fetch';
import { ask } from './ask';
import { DesktopRecorder } from './capture/recorder';
import { registerScreenshotProtocol, SCREENSHOT_SCHEME, sweepScreenshots } from './capture/screenshot-store';
@@ -195,6 +196,7 @@ if (!app.requestSingleInstanceLock()) {
app.whenReady().then(() => {
registerScreenshotProtocol();
+ registerAiFetch();
ipcMain.handle('mimik:openAtLogin:get', () => opensAtLogin());
ipcMain.handle('mimik:openAtLogin:set', (_event, enabled: boolean) => {
setOpenAtLogin(Boolean(enabled));
diff --git a/apps/desktop/src/preload/index.ts b/apps/desktop/src/preload/index.ts
index 19a9e6d..d8de43e 100644
--- a/apps/desktop/src/preload/index.ts
+++ b/apps/desktop/src/preload/index.ts
@@ -27,6 +27,9 @@ const api = {
);
},
},
+ ai: {
+ fetch: (request: unknown): Promise<unknown> => ipcRenderer.invoke('mimik:ai:fetch', request),
+ },
onRequest: (channel: string, handler: (payload: unknown) => Promise<unknown>): void => {
ipcRenderer.on(channel, async (_event, replyChannel: string, payload: unknown) => {
try {
diff --git a/apps/desktop/src/renderer/core-env.ts b/apps/desktop/src/renderer/core-env.ts
index 5a75e19..33bf229 100644
--- a/apps/desktop/src/renderer/core-env.ts
+++ b/apps/desktop/src/renderer/core-env.ts
@@ -3,8 +3,24 @@ import type { Settings, SettingsKey } from '@mimik/core/guides/types';
import { type Messages, translate } from '@mimik/core/i18n/translate';
import en from '@mimik/locales/en.yml';
+async function mainFetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
+ const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
+ const headers: Record<string, string> = {};
+ new Headers(init?.headers).forEach((value, key) => {
+ headers[key] = value;
+ });
+ const reply = (await window.mimik.ai.fetch({
+ url,
+ method: init?.method ?? 'GET',
+ headers,
+ body: typeof init?.body === 'string' ? init.body : undefined,
+ })) as { status: number; statusText: string; headers: Record<string, string>; body: string };
+ return new Response(reply.body, { status: reply.status, statusText: reply.statusText, headers: reply.headers });
+}
+
configureCore({
client: 'desktop',
+ fetch: mainFetch,
t: (key, substitutions) => translate(en as Messages, key, substitutions),
assetUrl: (path) => new URL(path, document.baseURI).href,
storage: {
diff --git a/packages/core/src/capture/ai/provider.ts b/packages/core/src/capture/ai/provider.ts
index aef3d23..1459b2b 100644
--- a/packages/core/src/capture/ai/provider.ts
+++ b/packages/core/src/capture/ai/provider.ts
@@ -1,11 +1,12 @@
import { createAnthropic } from '@ai-sdk/anthropic';
import { createOpenAI } from '@ai-sdk/openai';
+import { coreFetch } from '@/core/env';
import { AI_PROVIDERS, DEFAULT_AI_PROVIDER, findProvider, openAITransport, resolveBaseUrl } from './models';
export function createModel(provider: string, model: string, apiKey: string, baseUrl?: string) {
const config = findProvider(provider) ?? AI_PROVIDERS[DEFAULT_AI_PROVIDER];
const baseURL = resolveBaseUrl(config, baseUrl);
- if (config.protocol === 'anthropic') return createAnthropic({ apiKey, baseURL })(model);
- const openai = createOpenAI({ apiKey, baseURL, name: provider });
+ if (config.protocol === 'anthropic') return createAnthropic({ apiKey, baseURL, fetch: coreFetch })(model);
+ const openai = createOpenAI({ apiKey, baseURL, name: provider, fetch: coreFetch });
return openAITransport(config, baseUrl) === 'responses' ? openai(model) : openai.chat(model);
}
diff --git a/packages/core/src/capture/ai/validate.ts b/packages/core/src/capture/ai/validate.ts
index a17c90f..e312a4c 100644
--- a/packages/core/src/capture/ai/validate.ts
+++ b/packages/core/src/capture/ai/validate.ts
@@ -1,3 +1,4 @@
+import { coreFetch } from '@/core/env';
import { logger } from '@/core/logger';
import {
type AIProtocol,
@@ -44,7 +45,7 @@ function parseModelIds(body: unknown): string[] | undefined {
async function fetchModelsFromUrl(url: string, headers: Record<string, string>): Promise<string[] | undefined> {
try {
- const res = await fetch(url, {
+ const res = await coreFetch(url, {
headers,
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
});
@@ -60,7 +61,7 @@ type AuthProbe = { valid: true; body: unknown } | { valid: false; reason: 'rejec
async function probeAuth(url: string, headers: Record<string, string>): Promise<AuthProbe> {
try {
- const res = await fetch(url, {
+ const res = await coreFetch(url, {
headers,
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
});
@@ -103,7 +104,7 @@ async function probeWithInference(
? { model, max_tokens: 8, messages: [{ role: 'user', content: 'Reply with OK.' }] }
: { model, messages: [{ role: 'user', content: 'Reply with OK.' }], max_tokens: 8, stream: false };
try {
- const res = await fetch(url, {
+ const res = await coreFetch(url, {
method: 'POST',
headers: { ...headers, 'Content-Type': 'application/json' },
body: JSON.stringify(body),
diff --git a/packages/core/src/env.ts b/packages/core/src/env.ts
index 4d6a24e..00203b3 100644
--- a/packages/core/src/env.ts
+++ b/packages/core/src/env.ts
@@ -6,6 +6,7 @@ export interface CoreEnv {
client: ClientType;
t: (key: string, substitutions?: string[]) => string;
assetUrl: (path: string) => string;
+ fetch?: typeof globalThis.fetch;
storage: {
get<K extends SettingsKey>(keys: readonly K[]): Promise<Partial<Pick<Settings, K>>>;
set(items: Partial<Settings>): Promise<void>;
@@ -33,6 +34,8 @@ export const assetUrl = (path: string) => env().assetUrl(path);
export const client = (): ClientType => env().client;
+export const coreFetch: typeof globalThis.fetch = (input, init) => (env().fetch ?? globalThis.fetch)(input, init);
+
export const localStorage = {
get: <K extends SettingsKey>(keys: readonly K[]) => env().storage.get(keys),
set: (items: Partial<Settings>) => env().storage.set(items),
diff --git a/packages/ui/src/shared/AiSettings.tsx b/packages/ui/src/shared/AiSettings.tsx
index 86b1a6d..2737864 100644
--- a/packages/ui/src/shared/AiSettings.tsx
+++ b/packages/ui/src/shared/AiSettings.tsx
@@ -1,4 +1,4 @@
-import { keyFor, withKeyFor } from '@mimik/core/capture/ai/keys';
+import { keyFor, migrateApiKeys, withKeyFor } from '@mimik/core/capture/ai/keys';
import { AI_PROVIDERS, type AIProviderKey, CUSTOM_MODEL_VALUE, DEFAULT_AI_PROVIDER } from '@mimik/core/capture/ai/models';
import { AI_LANGUAGES } from '@mimik/core/capture/ai/prompts';
import type { KeyValidation } from '@mimik/core/capture/ai/validate';
@@ -31,7 +31,7 @@ export default function AiSettings({ validate }: AiSettingsProps) {
localStorage.get(['aiProvider', 'aiApiKeys', 'aiModel', 'aiBaseUrl', 'aiLanguage']).then((stored) => {
const next = (stored.aiProvider as AIProviderKey) || DEFAULT_AI_PROVIDER;
setProvider(next);
- setKeys((stored.aiApiKeys as AIApiKeys) ?? {});
+ setKeys(migrateApiKeys(stored));
setModel((stored.aiModel as string) || AI_PROVIDERS[next].defaultModel);
setBaseUrl((stored.aiBaseUrl as string) || '');
setLanguage((stored.aiLanguage as string) || 'en');
@@ -177,10 +177,11 @@ function KeyResult({ result }: { result: KeyValidation }) {
</p>
);
}
+ const unreachable = result.reason === 'network';
return (
<p className="flex items-center gap-1.5 text-xs font-medium text-destructive">
<TriangleAlert size={14} />
- {i18n.t('settings_keyInvalid')}
+ {i18n.t(unreachable ? 'settings_keyUnreachable' : 'settings_keyInvalid')}
</p>
);
}