π¬ 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(ai): generate guide title and description in one structured call
Luis Ortiz632306c6a6d8
diff --git a/src/core/capture/ai/__tests__/meta.test.ts b/src/core/capture/ai/__tests__/meta.test.tsnew file mode 100644index 0000000..4b47c2e--- /dev/null+++ b/src/core/capture/ai/__tests__/meta.test.ts@@ -0,0 +1,102 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++const { generateObjectMock } = vi.hoisted(() => ({ generateObjectMock: vi.fn() }));++vi.mock('ai', () => ({+ generateObject: generateObjectMock,+ jsonSchema: (schema: unknown) => schema,+}));++vi.mock('../provider', () => ({ createModel: () => ({ id: 'test-model' }) }));++vi.mock('@/lib/browser-api', () => ({+ localStorage: { get: vi.fn().mockResolvedValue({ aiLanguage: 'en' }) },+}));++import { generateGuideMeta } from '../meta';++const steps = [{ description: 'Click Directory', url: 'https://admin.okta.com/users' }];++describe('generateGuideMeta', () => {+ beforeEach(() => {+ generateObjectMock.mockReset();+ });++ it('returns both title and description from a well-formed response', async () => {+ generateObjectMock.mockResolvedValue({+ object: {+ title: 'Reset a User Password in Okta',+ description: 'Reset a locked-out user password.',+ },+ });++ const result = await generateGuideMeta(steps, 'openai', 'gpt-4o-mini', 'key');++ expect(result).toEqual({+ title: 'Reset a User Password in Okta',+ description: 'Reset a locked-out user password.',+ });+ });++ it('still yields a usable title when the model omits the description', async () => {+ generateObjectMock.mockResolvedValue({ object: { title: 'Reset a User Password in Okta' } });++ const result = await generateGuideMeta(steps, 'openai', 'gpt-4o-mini', 'key');++ expect(result?.title).toBe('Reset a User Password in Okta');+ expect(result?.description).toBeUndefined();+ });++ it('truncates a title over 70 characters', async () => {+ generateObjectMock.mockResolvedValue({ object: { title: 'x'.repeat(90), description: 'ok' } });++ const result = await generateGuideMeta(steps, 'openai', 'gpt-4o-mini', 'key');++ expect(result?.title.length).toBe(70);+ expect(result?.title.endsWith('...')).toBe(true);+ });++ it('marks every declared property as required, as strict mode demands', async () => {+ generateObjectMock.mockResolvedValue({ object: { title: 'T', description: null } });++ await generateGuideMeta(steps, 'openai', 'gpt-4o-mini', 'key');++ const { schema } = generateObjectMock.mock.calls[0][0];+ expect(schema.required.sort()).toEqual(Object.keys(schema.properties).sort());+ });++ it('treats a null description from the model as absent', async () => {+ generateObjectMock.mockResolvedValue({ object: { title: 'Okta Password Reset', description: null } });++ const result = await generateGuideMeta(steps, 'openai', 'gpt-4o-mini', 'key');++ expect(result?.title).toBe('Okta Password Reset');+ expect(result?.description).toBeUndefined();+ });++ it('returns null when the model returns a blank title', async () => {+ generateObjectMock.mockResolvedValue({ object: { title: ' ', description: 'ok' } });++ expect(await generateGuideMeta(steps, 'openai', 'gpt-4o-mini', 'key')).toBeNull();+ });++ it('interpolates the numbered steps into the prompt', async () => {+ generateObjectMock.mockResolvedValue({ object: { title: 'Okta Password Reset' } });++ await generateGuideMeta(steps, 'openai', 'gpt-4o-mini', 'key');++ const { prompt } = generateObjectMock.mock.calls[0][0];+ expect(prompt).toContain('1. [https://admin.okta.com/users] Click Directory');+ expect(prompt).not.toContain('{{steps}}');+ });++ it('returns null for an empty step list without calling the model', async () => {+ expect(await generateGuideMeta([], 'openai', 'gpt-4o-mini', 'key')).toBeNull();+ expect(generateObjectMock).not.toHaveBeenCalled();+ });++ it('returns null when the request throws', async () => {+ generateObjectMock.mockRejectedValue(new Error('rate limited'));+ expect(await generateGuideMeta(steps, 'openai', 'gpt-4o-mini', 'key')).toBeNull();+ });+});diff --git a/src/core/capture/ai/__tests__/prompts.test.ts b/src/core/capture/ai/__tests__/prompts.test.tsindex 9a68834..42b77a2 100644--- a/src/core/capture/ai/__tests__/prompts.test.ts+++ b/src/core/capture/ai/__tests__/prompts.test.ts@@ -1,5 +1,20 @@import { describe, expect, it } from 'vitest';-import { AI_LANGUAGES, getLanguageSuffix } from '../prompts';+import { AI_LANGUAGES, GUIDE_META_PROMPT, getLanguageSuffix } from '../prompts';++describe('GUIDE_META_PROMPT', () => {+ it('has a steps placeholder', () => {+ expect(GUIDE_META_PROMPT).toContain('{{steps}}');+ });++ it('keeps the 60-character title constraint', () => {+ expect(GUIDE_META_PROMPT).toContain('60 characters');+ });++ it('asks for a description of one or two sentences', () => {+ expect(GUIDE_META_PROMPT.toLowerCase()).toContain('description');+ expect(GUIDE_META_PROMPT).toMatch(/one or two sentences/i);+ });+});describe('getLanguageSuffix', () => {it('returns empty string for English', () => {@@ -29,7 +44,7 @@ describe('getLanguageSuffix', () => {it('includes IMPORTANT instruction', () => {const suffix = getLanguageSuffix('es');expect(suffix).toContain('IMPORTANT');- expect(suffix).toContain('Write the description in');+ expect(suffix).toContain('Write the output in');});});diff --git a/src/core/capture/ai/meta.ts b/src/core/capture/ai/meta.tsnew file mode 100644index 0000000..fd95138--- /dev/null+++ b/src/core/capture/ai/meta.ts@@ -0,0 +1,52 @@+import { generateObject, jsonSchema } from 'ai';+import { localStorage } from '@/lib/browser-api';+import { logger } from '@/lib/logger';+import { GUIDE_META_PROMPT, getLanguageSuffix } from './prompts';+import { createModel } from './provider';++export interface GuideMeta {+ title: string;+ description?: string;+}++const guideMetaSchema = jsonSchema<{ title: string; description?: string | null }>({+ type: 'object',+ properties: {+ title: { type: 'string' },+ description: { type: ['string', 'null'] },+ },+ required: ['title', 'description'],+ additionalProperties: false,+});++export async function generateGuideMeta(+ steps: { description: string; url: string }[],+ provider: string,+ model: string,+ apiKey: string,+): Promise<GuideMeta | null> {+ if (steps.length === 0) return null;++ const formatted = steps.map((s, i) => `${i + 1}. [${s.url}] ${s.description}`).join('\n');++ try {+ const settings = await localStorage.get(['aiLanguage']);+ const locale = (settings.aiLanguage as string) || 'en';+ const { object } = await generateObject({+ model: createModel(provider, model, apiKey),+ schema: guideMetaSchema,+ prompt: GUIDE_META_PROMPT.replace('{{steps}}', formatted) + getLanguageSuffix(locale),+ maxOutputTokens: 200,+ });++ let title = object.title?.trim().replace(/^"|"$/g, '') ?? '';+ if (!title) return null;+ if (title.length > 70) title = `${title.slice(0, 67)}...`;++ const description = object.description?.trim().replace(/^"|"$/g, '') || undefined;+ return { title, description };+ } catch (err) {+ logger.error('Guide meta generation failed', err);+ return null;+ }+}diff --git a/src/core/capture/ai/prompts.ts b/src/core/capture/ai/prompts.tsindex 71dec74..763b09a 100644--- a/src/core/capture/ai/prompts.ts+++ b/src/core/capture/ai/prompts.ts@@ -10,21 +10,25 @@ Examples of good descriptions:Write only the description, no preamble.`;-export const GUIDE_TITLE_PROMPT = `These are the steps of a browser workflow, with the page URL and description for each step:+export const GUIDE_META_PROMPT = `These are the steps of a browser workflow, with the page URL and description for each step:{{steps}}-Write a specific, descriptive title for this workflow. The title should mention the application or website name and the specific task being performed. Be precise β reference specific pages, features, or items that were interacted with.+Write a title and a description for this workflow.-IMPORTANT: The title MUST be under 60 characters. Keep it concise.+TITLE: specific and descriptive. Mention the application or website name and the specific task performed. Reference specific pages, features, or items that were interacted with. MUST be under 60 characters.-Examples:+Examples of good titles:- "Review claude-code Pull Requests"- "Configure Slack Notification Preferences"- "Submit Expense Report in Workday"- "Create Repository in GitHub Organization"-Just the title, no quotes, no preamble. Under 60 characters.`;+DESCRIPTION: one or two sentences stating what the workflow accomplishes and who would follow it. Do not repeat the title. Do not list the individual steps. Do not mention any UI element that does not appear in the steps above.++Examples of good descriptions:+- "Reset a locked-out user's password from the Okta admin panel. For IT support staff."+- "Configure which Slack channels send desktop notifications, and set a do-not-disturb schedule."`;export const AI_LANGUAGES = [{ code: 'en', label: 'English' },@@ -49,5 +53,5 @@ const LANGUAGE_NAMES: Record<string, string> = {export function getLanguageSuffix(locale: string): string {if (locale.startsWith('en')) return '';const lang = LANGUAGE_NAMES[locale.split('-')[0]] || locale;- return `\nIMPORTANT: Write the description in ${lang}.`;+ return `\nIMPORTANT: Write the output in ${lang}.`;}diff --git a/src/core/capture/ai/title.ts b/src/core/capture/ai/title.tsdeleted file mode 100644index 3ac462a..0000000--- a/src/core/capture/ai/title.ts+++ /dev/null@@ -1,32 +0,0 @@-import { generateText } from 'ai';-import { localStorage } from '@/lib/browser-api';-import { logger } from '@/lib/logger';-import { GUIDE_TITLE_PROMPT, getLanguageSuffix } from './prompts';-import { createModel } from './provider';--export async function generateGuideTitle(- steps: { description: string; url: string }[],- provider: string,- model: string,- apiKey: string,-): Promise<string | null> {- if (steps.length === 0) return null;-- const formatted = steps.map((s, i) => `${i + 1}. [${s.url}] ${s.description}`).join('\n');-- try {- const settings = await localStorage.get(['aiLanguage']);- const locale = (settings.aiLanguage as string) || 'en';- const { text } = await generateText({- model: createModel(provider, model, apiKey),- prompt: GUIDE_TITLE_PROMPT.replace('{{steps}}', formatted) + getLanguageSuffix(locale),- maxOutputTokens: 30,- });- let title = text.trim().replace(/^"|"$/g, '');- if (title.length > 70) title = `${title.slice(0, 67)}...`;- return title || null;- } catch (err) {- logger.error('Guide title generation failed', err);- return null;- }-}