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: await AI description in capture queue and add guide title generation

Luis Ortiza8aeb76f19d8
diff --git a/src/core/capture/ai-description.ts b/src/core/capture/ai-description.ts
index 2da6efa..a7d1c67 100644
--- a/src/core/capture/ai-description.ts
+++ b/src/core/capture/ai-description.ts
@@ -11,6 +11,30 @@ function createModel(provider: string, model: string, apiKey: string) {
return createOpenAI({ apiKey })(model);
}
+export async function generateGuideTitle(
+ stepDescriptions: string[],
+ provider: string,
+ model: string,
+ apiKey: string,
+): Promise<string | null> {
+ if (stepDescriptions.length === 0) return null;
+
+ const steps = stepDescriptions.map((d, i) => `${i + 1}. ${d}`).join('\n');
+ const prompt = `These are the steps of a browser workflow:\n\n${steps}\n\nWrite a short title (max 8 words) that describes what this workflow does. Examples: "Create a new GitHub repository", "Update profile settings in Slack". Just the title, no quotes, no preamble.`;
+
+ try {
+ const { text } = await generateText({
+ model: createModel(provider, model, apiKey),
+ prompt,
+ maxOutputTokens: 30,
+ });
+ return text.trim() || null;
+ } catch (err) {
+ logger.error('Guide title generation failed', err);
+ return null;
+ }
+}
+
export async function getAIDescription(
blob: Blob,
action: string,
diff --git a/src/entrypoints/background/index.ts b/src/entrypoints/background/index.ts
index afde26d..9f0e85d 100644
--- a/src/entrypoints/background/index.ts
+++ b/src/entrypoints/background/index.ts
@@ -1,9 +1,10 @@
import { defineBackground } from "#imports";
import { logger } from "@/lib/logger";
-import { setSidePanelBehavior, getActiveTab } from "@/lib/browser-api";
+import { setSidePanelBehavior, getActiveTab, localStorage } from "@/lib/browser-api";
import { onMessage } from "@/lib/messaging";
import { setupPortListener, broadcastStateToPanel } from "@/lib/port";
-import { createGuide, saveRrwebChunk } from "@/core/guides/service";
+import { createGuide, saveRrwebChunk, getStepsForGuide, updateGuideTitle } from "@/core/guides/service";
+import { generateGuideTitle } from "@/core/capture/ai-description";
import {
initActor,
initActorFallback,
@@ -15,6 +16,27 @@ import { broadcastStartCapture, broadcastStopCapture, showNotificationOnTab } fr
import { handleUserAction } from "./step-pipeline";
import { registerNavigationListeners } from "./navigation";
+async function generateTitleInBackground(guideId: string) {
+ try {
+ const settings = await localStorage.get(['aiApiKey', 'aiProvider', 'aiModel']);
+ if (!settings.aiApiKey) return;
+
+ const steps = await getStepsForGuide(guideId);
+ const descriptions = steps.map(s => s.description).filter(Boolean);
+ if (descriptions.length === 0) return;
+
+ const provider = (settings.aiProvider as string) || 'openai';
+ const model = (settings.aiModel as string) || 'gpt-4o-mini';
+ const title = await generateGuideTitle(descriptions, provider, model, settings.aiApiKey as string);
+ if (title) {
+ await updateGuideTitle(guideId, title);
+ logger.info('Generated guide title:', title);
+ }
+ } catch (err) {
+ logger.error('Guide title generation failed', err);
+ }
+}
+
export default defineBackground(() => {
logger.info("Background service worker started");
@@ -68,6 +90,11 @@ export default defineBackground(() => {
logger.info("stopRecording β†’ guideId:", guideId);
await broadcastStopCapture();
actor.send({ type: "STOP_RECORDING" });
+
+ if (guideId) {
+ generateTitleInBackground(guideId);
+ }
+
return { success: true, guideId: guideId ?? undefined };
});
diff --git a/src/entrypoints/background/step-pipeline.ts b/src/entrypoints/background/step-pipeline.ts
index 614be12..3486da6 100644
--- a/src/entrypoints/background/step-pipeline.ts
+++ b/src/entrypoints/background/step-pipeline.ts
@@ -80,28 +80,26 @@ export function handleUserAction(
await addStepToGuide(guideId, stepId);
if (screenshotBlob) {
- (async () => {
- try {
- const settings = await localStorage.get(['aiApiKey', 'aiProvider', 'aiModel']);
- if (settings.aiApiKey) {
- 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) {
- await updateStepDescription(stepId, aiDescription);
- }
+ try {
+ const settings = await localStorage.get(['aiApiKey', 'aiProvider', 'aiModel']);
+ if (settings.aiApiKey) {
+ 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) {
+ await updateStepDescription(stepId, aiDescription);
}
- } catch (err) {
- logger.error('AI description update failed', err);
}
- })();
+ } catch (err) {
+ logger.error('AI description update failed', err);
+ }
}
});