π¬ 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
feat: rewrite AI description with Vercel AI SDK and model selection
Luis Ortiz1937eec9cc4a
diff --git a/src/core/capture/ai-description.ts b/src/core/capture/ai-description.tsindex 4e973d6..2da6efa 100644--- a/src/core/capture/ai-description.ts+++ b/src/core/capture/ai-description.ts@@ -1,57 +1,46 @@+import { generateText } from 'ai';+import { createOpenAI } from '@ai-sdk/openai';+import { createAnthropic } from '@ai-sdk/anthropic';import { logger } from '@/lib/logger';-import OpenAI from 'openai';import type { ElementMeta } from '@/core/guides/types';+function createModel(provider: string, model: string, apiKey: string) {+ if (provider === 'anthropic') {+ return createAnthropic({ apiKey })(model);+ }+ return createOpenAI({ apiKey })(model);+}+export async function getAIDescription(blob: Blob,action: string,meta: ElementMeta,- provider: 'openai' | 'anthropic',- apiKey: string+ provider: string,+ model: string,+ apiKey: string,): Promise<string | null> {const arrayBuffer = await blob.arrayBuffer();const base64 = btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));+ const dataUrl = `data:image/jpeg;base64,${base64}`;const targetText = meta.textContent || meta.ariaLabel || meta.placeholder || meta.tag;const prompt = `This is a browser screenshot. The highlighted element (blue border) is a ${meta.tag} with text "${targetText}". The user performed a "${action}" action on it. Write a single concise sentence describing this step (e.g., "Click the Submit button"). No preamble.`;try {- if (provider === 'openai') {- const client = new OpenAI({ apiKey, dangerouslyAllowBrowser: true });- const response = await client.chat.completions.create({- model: 'gpt-4o-mini',- max_tokens: 100,- messages: [{- role: 'user',- content: [- { type: 'image_url', image_url: { url: `data:image/jpeg;base64,${base64}` } },- { type: 'text', text: prompt },- ],- }],- });- return response.choices[0]?.message?.content?.trim() || null;- } else {- const { default: Anthropic } = await import('@anthropic-ai/sdk');- const client = new Anthropic({ apiKey });- const response = await client.messages.create({- model: 'claude-3-5-haiku-20241022',- max_tokens: 100,- messages: [{- role: 'user',- content: [- {- type: 'image',- source: { type: 'base64', media_type: 'image/jpeg', data: base64 },- },- { type: 'text', text: prompt },- ],- }],- });- const block = response.content[0];- return block.type === 'text' ? block.text.trim() : null;- }+ const { text } = await generateText({+ model: createModel(provider, model, apiKey),+ messages: [{+ role: 'user',+ content: [+ { type: 'image', image: dataUrl },+ { type: 'text', text: prompt },+ ],+ }],+ maxOutputTokens: 100,+ });+ return text.trim() || null;} catch (err) {- logger.error(' AI description failed, using fallback', err);+ logger.error('AI description failed, using fallback', err);return null;}}diff --git a/src/entrypoints/background/step-pipeline.ts b/src/entrypoints/background/step-pipeline.tsindex f622930..614be12 100644--- a/src/entrypoints/background/step-pipeline.ts+++ b/src/entrypoints/background/step-pipeline.ts@@ -82,14 +82,16 @@ export function handleUserAction(if (screenshotBlob) {(async () => {try {- const settings = await localStorage.get(['aiApiKey', 'aiProvider']);+ const settings = await localStorage.get(['aiApiKey', 'aiProvider', 'aiModel']);if (settings.aiApiKey) {- const provider = (settings.aiProvider as 'openai' | 'anthropic') || 'openai';+ const provider = (settings.aiProvider as string) || 'openai';+ const model = (settings.aiModel as string) || 'gpt-4o-mini';const aiDescription = await getAIDescription(screenshotBlob,data.action,data.elementMeta,provider,+ model,settings.aiApiKey as string,);if (aiDescription) {@@ -97,7 +99,7 @@ export function handleUserAction(}}} catch (err) {- logger.error(' AI description update failed', err);+ logger.error('AI description update failed', err);}})();}