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): narrate and finish a guide the way the extension does

Luis Ortizdbbad574b12e
diff --git a/apps/desktop/scripts/check-pipeline.ts b/apps/desktop/scripts/check-pipeline.ts
index 46aed03..1088208 100644
--- a/apps/desktop/scripts/check-pipeline.ts
+++ b/apps/desktop/scripts/check-pipeline.ts
@@ -725,8 +725,8 @@ app.whenReady().then(async () => {
});
results.push({
- name: 'an empty recording keeps its untitled guide',
- ok: titled.toLowerCase() === 'untitled guide',
+ name: 'an empty recording is named New guide, as in the extension',
+ ok: titled === 'New guide',
detail: titled,
});
diff --git a/apps/desktop/src/main/overlay/index.ts b/apps/desktop/src/main/overlay/index.ts
index 0ddb8f8..acce825 100644
--- a/apps/desktop/src/main/overlay/index.ts
+++ b/apps/desktop/src/main/overlay/index.ts
@@ -6,6 +6,7 @@ import {
type captureMachine,
type PauseReason,
} from '@mimik/core/capture/machine';
+import type { VoiceErrorReason } from '@mimik/core/capture/voice/voice-update';
import { app, BrowserWindow, ipcMain, screen } from 'electron';
import type { Actor } from 'xstate';
import { clampToDisplays, loadRegion, type Region, saveRegion } from '../capture/region';
@@ -57,6 +58,7 @@ export interface OverlayAiFailure {
export interface OverlayNarration {
level: number;
speaking: boolean;
+ reason?: VoiceErrorReason;
}
export interface OverlayAim {
diff --git a/apps/desktop/src/renderer/App.tsx b/apps/desktop/src/renderer/App.tsx
index eec8ef1..d446baa 100644
--- a/apps/desktop/src/renderer/App.tsx
+++ b/apps/desktop/src/renderer/App.tsx
@@ -8,18 +8,21 @@ import {
TooltipProvider,
useFullview,
useRoute,
+ VoiceNotice,
} from '@mimik/ui';
import { useCallback, useEffect, useState } from 'react';
import type { CaptureInsert } from '../main/capture/insert';
import { CaptureSheet } from './CaptureSheet';
import { DesktopOnboarding } from './DesktopOnboarding';
import { GuideZoom } from './GuideZoom';
+import { useNarrationUpdate } from './hooks/use-narration-update';
import { PermissionsDialog } from './PermissionsDialog';
import { REOPEN_SETTINGS } from './settings/lib/reopen-settings';
import { SettingsDialog } from './settings/SettingsDialog';
export function App() {
const route = useRoute();
+ const narration = useNarrationUpdate();
const [settingsOpen, setSettingsOpen] = useState(() => sessionStorage.getItem(REOPEN_SETTINGS) !== null);
const [sheet, setSheet] = useState<{ insert?: CaptureInsert } | null>(null);
const [guideKey, setGuideKey] = useState(0);
@@ -116,6 +119,11 @@ export function App() {
</AppFrame>
<SearchModal />
<SettingsDialog open={settingsOpen} onOpenChange={setSettingsOpen} />
+ <VoiceNotice
+ update={narration.update}
+ seenLive={narration.seenLive}
+ onOpenSettings={() => setSettingsOpen(true)}
+ />
{sheet && <CaptureSheet insert={sheet.insert} onClose={() => setSheet(null)} />}
<PermissionsDialog open={permissionsOpen} onClose={closePermissions} />
</TooltipProvider>
diff --git a/apps/desktop/src/renderer/ai/name-guide.ts b/apps/desktop/src/renderer/ai/name-guide.ts
deleted file mode 100644
index 1bfbbea..0000000
--- a/apps/desktop/src/renderer/ai/name-guide.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-import { resolveGuideMetaInputs } from '@mimik/core/capture/ai/guide-description';
-import { type GuideMeta, generateGuideMeta } from '@mimik/core/capture/ai/meta';
-
-export async function nameGuide(guideId: string): Promise<GuideMeta | null> {
- const inputs = await resolveGuideMetaInputs(guideId);
- if (!inputs.ok) return null;
- try {
- return await generateGuideMeta(inputs.steps, inputs.provider, inputs.model, inputs.apiKey, inputs.baseUrl);
- } catch {
- return null;
- }
-}
diff --git a/apps/desktop/src/renderer/capture-host.ts b/apps/desktop/src/renderer/capture-host.ts
index d6dd5d2..5a96aba 100644
--- a/apps/desktop/src/renderer/capture-host.ts
+++ b/apps/desktop/src/renderer/capture-host.ts
@@ -1,4 +1,4 @@
-import { resolveGuideMetaInputs } from '@mimik/core/capture/ai/guide-description';
+import { finishGuide } from '@mimik/core/capture/ai/finish-guide';
import { settleDescriptions } from '@mimik/core/capture/ai/settle-descriptions';
import { CaptureState } from '@mimik/core/capture/machine';
import type { CaptureStepData } from '@mimik/core/capture/sink';
@@ -11,14 +11,10 @@ import {
getStepsForGuide,
mergeGuideInto,
permanentlyDeleteGuide,
- updateGuideDescription,
- updateGuideTitle,
} from '@mimik/core/guides/service';
-import { nameGuide } from './ai/name-guide';
import { DesktopCaptureSink } from './capture-sink';
-import { DesktopNarration } from './narration';
+import { desktopNarration as narration } from './desktop-narration';
-const narration = new DesktopNarration();
const sink = new DesktopCaptureSink(narration);
allScreenshotIds().then((ids) => window.mimik.screenshots.sweep(ids));
@@ -35,8 +31,9 @@ window.mimik.capture.onStateUpdate(async ({ command, state, currentGuideId }) =>
if (!currentGuideId) return;
const recording = state === CaptureState.RECORDING;
if (command === 'start' || command === 'resume' || (command === 'narration:start' && recording)) {
- narration.listen(currentGuideId);
- } else if (command === 'pause' || command === 'narration:stop') narration.cut();
+ narration.start(currentGuideId);
+ } else if (command === 'pause') narration.stop();
+ else if (command === 'narration:stop') narration.abort();
});
window.mimik.onRequest('mimik:capture:deleteStep', async (payload) => {
@@ -45,28 +42,19 @@ window.mimik.onRequest('mimik:capture:deleteStep', async (payload) => {
return true;
});
-window.mimik.onRequest('mimik:capture:finishGuide', async (payload) => {
- const guideId = payload as string;
- await narration.finish();
- const steps = await getStepsForGuide(guideId);
- if (steps.length === 0) return true;
- const app = getMostCommonApp(steps)?.name;
- const fallback = app ? i18n.t('desktop.guideInApp', [app]) : i18n.t('background.newGuide');
- if (!(await resolveGuideMetaInputs(guideId)).ok) {
- await updateGuideTitle(guideId, fallback);
- return true;
- }
+async function appTitle(guideId: string): Promise<string> {
+ const app = getMostCommonApp(await getStepsForGuide(guideId))?.name;
+ return app ? i18n.t('desktop.guideInApp', [app]) : i18n.t('background.newGuide');
+}
- await settleDescriptions(guideId);
- const meta = await nameGuide(guideId);
- await updateGuideTitle(guideId, meta?.title || fallback);
- if (meta?.description) await updateGuideDescription(guideId, meta.description);
+window.mimik.onRequest('mimik:capture:finishGuide', async (payload) => {
+ await finishGuide(payload as string, { fallbackTitle: appTitle, settleNarration: () => narration.settle() });
return true;
});
window.mimik.onRequest('mimik:capture:mergeGuideInto', async (payload) => {
const { guideId, targetGuideId, atIndex } = payload as { guideId: string; targetGuideId: string; atIndex: number };
- await narration.finish();
+ await narration.settle();
await settleDescriptions(guideId);
await createSnapshot(targetGuideId);
await mergeGuideInto(guideId, targetGuideId, atIndex);
diff --git a/apps/desktop/src/renderer/capture-sink.ts b/apps/desktop/src/renderer/capture-sink.ts
index bd4fb45..cafba14 100644
--- a/apps/desktop/src/renderer/capture-sink.ts
+++ b/apps/desktop/src/renderer/capture-sink.ts
@@ -74,8 +74,8 @@ export class DesktopCaptureSink implements CaptureSink {
const description = buildFallbackDescription(data.action, meta, data.inputValue);
const hasKey = (await credentials()) !== null;
- const narrating = this.narration?.capturing === true;
- const pending = hasKey || narrating;
+ const narrationCapturing = this.narration?.update.phase === 'recording';
+ const pending = hasKey || narrationCapturing;
const timestamp = Date.now();
await createStep({
@@ -103,7 +103,7 @@ export class DesktopCaptureSink implements CaptureSink {
const written = (await getStep(stepId))?.descriptionSource === 'ai';
window.mimik.capture.described(stepId, written ? text : null, failure);
};
- if (narrating) this.narration?.step(data.guideId, { stepId, timestamp }, hasKey ? describe : null);
+ if (narrationCapturing) this.narration?.flushForStep(data.guideId, { stepId, timestamp }, hasKey ? describe : null);
else if (hasKey) queueDescription(data.guideId, describe);
return { stepId, title: description, pending };
diff --git a/apps/desktop/src/renderer/desktop-narration.ts b/apps/desktop/src/renderer/desktop-narration.ts
new file mode 100644
index 0000000..729c6b0
--- /dev/null
+++ b/apps/desktop/src/renderer/desktop-narration.ts
@@ -0,0 +1,3 @@
+import { DesktopNarration } from './narration';
+
+export const desktopNarration = new DesktopNarration();
diff --git a/apps/desktop/src/renderer/hooks/use-narration-update.ts b/apps/desktop/src/renderer/hooks/use-narration-update.ts
new file mode 100644
index 0000000..bd1e4ee
--- /dev/null
+++ b/apps/desktop/src/renderer/hooks/use-narration-update.ts
@@ -0,0 +1,19 @@
+import type { VoiceUpdate } from '@mimik/core/capture/voice/voice-update';
+import { useEffect, useState } from 'react';
+import { desktopNarration } from '../desktop-narration';
+
+export function useNarrationUpdate(): { update: VoiceUpdate; seenLive: boolean } {
+ const [update, setUpdate] = useState<VoiceUpdate>(desktopNarration.update);
+ const [seenLive, setSeenLive] = useState(desktopNarration.update.phase !== 'idle');
+
+ useEffect(
+ () =>
+ desktopNarration.onUpdate((next) => {
+ if (next.phase !== 'idle') setSeenLive(true);
+ setUpdate(next);
+ }),
+ [],
+ );
+
+ return { update, seenLive };
+}
diff --git a/apps/desktop/src/renderer/narration.ts b/apps/desktop/src/renderer/narration.ts
index b8a0a02..6347124 100644
--- a/apps/desktop/src/renderer/narration.ts
+++ b/apps/desktop/src/renderer/narration.ts
@@ -5,8 +5,10 @@ import { narrateRecording, type VoiceRecording } from '@mimik/core/capture/voice
import { narrationUpdates } from '@mimik/core/capture/voice/narration-updates';
import { partialRecording } from '@mimik/core/capture/voice/partial-recording';
import { readTranscriptionSettings } from '@mimik/core/capture/voice/read-transcription-settings';
+import { startFailureReason } from '@mimik/core/capture/voice/start-failure-reason';
import type { StepMark } from '@mimik/core/capture/voice/step-windows';
import { usableRecording } from '@mimik/core/capture/voice/usable-recording';
+import type { VoiceUpdate } from '@mimik/core/capture/voice/voice-update';
import { assetUrl, localStorage } from '@mimik/core/env';
import {
applyNarrationToSteps,
@@ -18,7 +20,6 @@ import {
import { logger } from '@mimik/core/logger';
const SETTLE_MS = 30_000;
-const REFUSED = ['NotAllowedError', 'SecurityError'];
type Describe = () => Promise<void>;
@@ -27,41 +28,46 @@ export class DesktopNarration {
private mic: MicRecorder | null = null;
private guideId: string | null = null;
private flushedUpTo = 0;
+ private narrated = 0;
+ private lost = false;
+ private current: VoiceUpdate = { phase: 'idle' };
+ private readonly listeners = new Set<(update: VoiceUpdate) => void>();
private readonly pending = new Set<Promise<void>>();
- private readonly deferred = new Map<string, { guideId: string; describe: Describe | null }>();
+ private readonly deferredDescriptions = new Map<string, { guideId: string; describe: Describe | null }>();
- async wanted(): Promise<boolean> {
- const stored = await localStorage.get([...VOICE_KEY_SETTINGS, 'voiceEnabled']);
- return stored.voiceEnabled === true && hasVoiceApiKey(stored);
+ get update(): VoiceUpdate {
+ return this.current;
}
- get capturing(): boolean {
- return this.mic !== null;
+ onUpdate(listener: (update: VoiceUpdate) => void): () => void {
+ this.listeners.add(listener);
+ return () => this.listeners.delete(listener);
}
- listen(guideId: string): void {
+ start(guideId: string): void {
+ if (this.guideId !== guideId) this.narrated = 0;
this.guideId = guideId;
this.live ??= this.open();
}
- step(guideId: string, mark: StepMark, describe: Describe | null): void {
- this.deferred.set(mark.stepId, { guideId, describe });
+ flushForStep(guideId: string, mark: StepMark, describe: Describe | null): void {
+ this.deferredDescriptions.set(mark.stepId, { guideId, describe });
const mic = this.mic;
this.track(async () => {
const full = mic && usableRecording(mic.snapshot());
const closesAt = full ? (mark.timestamp - full.audioEpochMs) / 1000 : 0;
const slice = full && closesAt > this.flushedUpTo ? partialRecording(full, this.flushedUpTo, closesAt) : null;
if (!slice) {
- this.describeNow(mark.stepId);
+ this.describeStepNow(mark.stepId);
return;
}
this.flushedUpTo = closesAt;
const narrated = await this.transcribe(guideId, slice, [mark]);
- if (!narrated.includes(mark.stepId)) this.describeNow(mark.stepId);
+ if (!narrated.includes(mark.stepId)) this.describeStepNow(mark.stepId);
});
}
- cut(): void {
+ stop(): void {
const opening = this.live;
const guideId = this.guideId;
const from = this.flushedUpTo;
@@ -70,6 +76,7 @@ export class DesktopNarration {
this.flushedUpTo = 0;
window.mimik.capture.narration(null);
if (!opening || !guideId) return;
+ if (this.current.phase === 'recording') this.report({ phase: 'transcribing' });
const inflight = [...this.pending];
this.track(async () => {
const mic = await opening;
@@ -85,30 +92,53 @@ export class DesktopNarration {
steps.map((step) => ({ stepId: step.id, timestamp: step.timestamp })),
);
}
- for (const [stepId, entry] of [...this.deferred]) {
- if (entry.guideId === guideId) this.describeNow(stepId);
- }
+ this.describeDeferred(guideId);
+ this.settled();
});
}
abort(): void {
const opening = this.live;
+ const guideId = this.guideId;
this.live = null;
this.mic = null;
this.flushedUpTo = 0;
- this.deferred.clear();
window.mimik.capture.narration(null);
void opening?.then((mic) => {
mic?.release();
window.mimik.capture.narration(null);
});
+ if (guideId) this.describeDeferred(guideId);
+ if (this.current.phase === 'recording') this.report({ phase: 'idle' });
}
- async finish(): Promise<void> {
- this.cut();
+ async settle(): Promise<void> {
+ this.stop();
await Promise.race([Promise.all(this.pending), new Promise((resolve) => setTimeout(resolve, SETTLE_MS))]);
}
+ private report(update: VoiceUpdate): void {
+ this.current = update;
+ if (update.phase === 'error') window.mimik.capture.narration({ level: 0, speaking: false, reason: update.reason });
+ for (const listener of this.listeners) listener(update);
+ }
+
+ private settled(): void {
+ if (this.live || this.pending.size > 1) return;
+ if (this.lost) {
+ this.lost = false;
+ this.report({ phase: 'error', reason: 'stream-ended' });
+ return;
+ }
+ if (this.current.phase === 'transcribing') this.report({ phase: 'idle', narrated: this.narrated });
+ }
+
+ private describeDeferred(guideId: string): void {
+ for (const [stepId, entry] of [...this.deferredDescriptions]) {
+ if (entry.guideId === guideId) this.describeStepNow(stepId);
+ }
+ }
+
private track(work: () => Promise<void>): void {
const running = work()
.catch((error) => logger.error('voice: narration could not be applied', error))
@@ -116,10 +146,10 @@ export class DesktopNarration {
this.pending.add(running);
}
- private describeNow(stepId: string): void {
- const entry = this.deferred.get(stepId);
+ private describeStepNow(stepId: string): void {
+ const entry = this.deferredDescriptions.get(stepId);
if (!entry) return;
- this.deferred.delete(stepId);
+ this.deferredDescriptions.delete(stepId);
if (entry.describe) {
queueDescription(entry.guideId, entry.describe);
return;
@@ -128,26 +158,36 @@ export class DesktopNarration {
}
private async open(): Promise<MicRecorder | null> {
- if (!(await this.wanted())) return null;
- const { voiceMicrophoneId } = await localStorage.get(['voiceMicrophoneId']);
+ const stored = await localStorage.get([...VOICE_KEY_SETTINGS, 'voiceEnabled', 'voiceMicrophoneId']);
+ if (stored.voiceEnabled !== true) return null;
+ if (!hasVoiceApiKey(stored)) {
+ this.report({ phase: 'error', reason: 'missing-api-key' });
+ return null;
+ }
const mic = new MicRecorder(assetUrl('pcm-processor.js'), {
onEpoch: () => undefined,
onLevel: (level, speaking) => window.mimik.capture.narration({ level, speaking }),
- onStreamEnded: () => this.cut(),
+ onStreamEnded: () => {
+ this.lost = true;
+ this.stop();
+ },
});
try {
- await mic.start(voiceMicrophoneId || undefined);
+ await mic.start(stored.voiceMicrophoneId || undefined);
if (!this.live) {
mic.release();
return null;
}
this.mic = mic;
+ this.report({ phase: 'recording' });
window.mimik.capture.narration({ level: 0, speaking: false });
return mic;
} catch (error) {
mic.release();
logger.warn('voice: narration could not start, recording without it', error);
- if (error instanceof Error && REFUSED.includes(error.name)) await localStorage.set({ voiceEnabled: false });
+ const reason = startFailureReason(error);
+ this.report({ phase: 'error', reason });
+ if (reason === 'permission-denied') await localStorage.set({ voiceEnabled: false });
return null;
}
}
@@ -160,8 +200,9 @@ export class DesktopNarration {
const surviving = await findExistingStepIds(result.descriptions.map((entry) => entry.stepId));
const updates = narrationUpdates(result, surviving);
await applyNarrationToSteps(updates, result.transcript.epochMs);
+ this.narrated += updates.length;
for (const { stepId, description } of updates) {
- this.deferred.delete(stepId);
+ this.deferredDescriptions.delete(stepId);
window.mimik.capture.described(stepId, description, null, 'narration');
}
return updates.map((update) => update.stepId);
diff --git a/apps/desktop/src/renderer/overlay/controls.ts b/apps/desktop/src/renderer/overlay/controls.ts
index b99e80e..aea6280 100644
--- a/apps/desktop/src/renderer/overlay/controls.ts
+++ b/apps/desktop/src/renderer/overlay/controls.ts
@@ -1,6 +1,7 @@
import { aiFailureNotice } from '@mimik/core/capture/ai/errors';
import { splitAtShortcut } from '@mimik/core/capture/split-at-shortcut';
import { hasVoiceApiKey, VOICE_KEY_SETTINGS } from '@mimik/core/capture/voice/api-key';
+import { voiceErrorKey } from '@mimik/core/capture/voice/voice-error-key';
import { i18n, localStorage } from '@mimik/core/env';
import type { OverlayView } from '../../main/overlay';
import { icon } from '../icons';
@@ -227,12 +228,14 @@ export function controls(): void {
}
badge.hidden = paused || collapsed;
- const micLocked = !micKeyed && !micOn;
- const micState = micOn ? 'on' : micLocked ? 'locked' : 'off';
+ const micLocked = (!micKeyed && !micOn) || paused;
+ const micState = `${micOn ? 'on' : micLocked ? 'locked' : 'off'}${paused ? ':paused' : ''}`;
if (micState !== shownMic) {
shownMic = micState;
- const micLabel = i18n.t(micLocked ? 'voice_needsApiKey' : micOn ? 'voice_turnOff' : 'voice_turnOn');
- mic.className = micState;
+ const micLabel = i18n.t(
+ paused ? 'voice_pausedWithCapture' : micLocked ? 'voice_needsApiKey' : micOn ? 'voice_turnOff' : 'voice_turnOn',
+ );
+ mic.className = micLocked ? 'locked' : micOn ? 'on' : 'off';
mic.title = micLabel;
mic.setAttribute('aria-label', micLabel);
mic.setAttribute('aria-pressed', String(micOn));
@@ -288,9 +291,14 @@ export function controls(): void {
const key = armed ? shortcuts.startStop : shortcuts.capture;
hint.hidden = intro.hidden || resting || !key || Boolean(narration);
voice.hidden = collapsed || !narration;
- voiceHint.hidden = intro.hidden;
+ const voiceFailed = Boolean(narration?.reason);
+ voiceHint.hidden = intro.hidden && !voiceFailed;
+ voiceHint.textContent = i18n.t(voiceFailed ? 'voice_guideSafe' : 'voice_orderHint');
+ voiceBars.hidden = voiceFailed;
voiceBars.classList.toggle('speaking', narration?.speaking === true);
- voiceLabel.textContent = i18n.t(narration?.speaking ? 'voice_micHearing' : 'voice_micQuiet');
+ voiceLabel.textContent = narration?.reason
+ ? i18n.t(voiceErrorKey(narration.reason))
+ : i18n.t(narration?.speaking ? 'voice_micHearing' : 'voice_micQuiet');
bars.forEach((bar, index) => {
const scale = Math.max(VOICE_BAR_FLOOR, Math.min(1, (narration?.level ?? 0) * VOICE_BARS[index]));
bar.style.transform = `scaleY(${scale})`;
@@ -365,7 +373,7 @@ export function controls(): void {
secondary.addEventListener('click', () => window.mimikOverlay.command(secondary.dataset.command ?? 'disarm'));
remove.addEventListener('click', () => window.mimikOverlay.command('deleteStep'));
mic.addEventListener('click', () => {
- if (shownMic !== 'locked') window.mimikOverlay.command(micOn ? 'narration:stop' : 'narration:start');
+ if (mic.className !== 'locked') window.mimikOverlay.command(micOn ? 'narration:stop' : 'narration:start');
});
const readMic = () =>
localStorage.get([...VOICE_KEY_SETTINGS, 'voiceEnabled']).then((stored) => {
diff --git a/packages/core/src/capture/ai/finish-guide.ts b/packages/core/src/capture/ai/finish-guide.ts
new file mode 100644
index 0000000..1199f0f
--- /dev/null
+++ b/packages/core/src/capture/ai/finish-guide.ts
@@ -0,0 +1,52 @@
+import { localStorage } from '@/core/env';
+import { updateGuideDescription, updateGuideTitle } from '@/core/guides/service';
+import { logger } from '@/core/logger';
+import { resolveGuideMetaInputs } from './guide-description';
+import { AI_CREDENTIAL_SETTINGS, resolveAiCredentials } from './keys';
+import { generateGuideMeta } from './meta';
+import { settleDescriptions } from './settle-descriptions';
+
+export interface FinishGuideOptions {
+ fallbackTitle: (guideId: string) => Promise<string>;
+ settleNarration: () => Promise<void>;
+}
+
+export async function finishGuide(guideId: string, { fallbackTitle, settleNarration }: FinishGuideOptions) {
+ const applyFallbackTitle = async () => updateGuideTitle(guideId, await fallbackTitle(guideId));
+ const settle = async () => {
+ await settleNarration();
+ await settleDescriptions(guideId);
+ };
+
+ const keys = await localStorage
+ .get([...AI_CREDENTIAL_SETTINGS])
+ .then(resolveAiCredentials)
+ .catch(() => null);
+ if (!keys) {
+ const titled = applyFallbackTitle().catch((err) => logger.error('Fallback title write failed', err));
+ await settle().catch((err) => logger.error('Settling step descriptions failed', err));
+ await titled;
+ return;
+ }
+
+ try {
+ await settle();
+ const inputs = await resolveGuideMetaInputs(guideId);
+ const meta = inputs.ok
+ ? await generateGuideMeta(inputs.steps, inputs.provider, inputs.model, inputs.apiKey, inputs.baseUrl)
+ : null;
+ if (!meta) {
+ await applyFallbackTitle();
+ return;
+ }
+ await updateGuideTitle(guideId, meta.title);
+ if (meta.description) {
+ await updateGuideDescription(guideId, meta.description).catch((err) =>
+ logger.error('Guide description write failed', err),
+ );
+ }
+ } catch (err) {
+ logger.error('Guide meta generation failed', err);
+ await applyFallbackTitle();
+ }
+}
diff --git a/src/entrypoints/background/__tests__/guide-meta.test.ts b/src/entrypoints/background/__tests__/guide-meta.test.ts
index fe6c1f1..7c559af 100644
--- a/src/entrypoints/background/__tests__/guide-meta.test.ts
+++ b/src/entrypoints/background/__tests__/guide-meta.test.ts
@@ -230,13 +230,13 @@ describe('background guide-meta', () => {
expect(updateGuideTitleMock).toHaveBeenCalledWith(GUIDE_ID, 'background.newGuide');
});
- it('writes no title at all when no step has a description', async () => {
+ it('names a guide with no described step by the fallback, without asking the model', async () => {
getStepsForGuideMock.mockResolvedValue([{ description: '', url: 'https://example.com' }]);
await generateGuideMetaOnStop(GUIDE_ID);
expect(generateGuideMetaMock).not.toHaveBeenCalled();
- expect(updateGuideTitleMock).not.toHaveBeenCalled();
+ expect(updateGuideTitleMock).toHaveBeenCalledTimes(1);
expect(updateGuideDescriptionMock).not.toHaveBeenCalled();
});
diff --git a/src/entrypoints/background/guide-meta.ts b/src/entrypoints/background/guide-meta.ts
index e7c02a3..97a8d9c 100644
--- a/src/entrypoints/background/guide-meta.ts
+++ b/src/entrypoints/background/guide-meta.ts
@@ -1,23 +1,12 @@
-import { logger } from '@mimik/core/logger';
import { i18n } from '#imports';
-import { resolveGuideMetaInputs } from '@/core/capture/ai/guide-description';
-import { AI_CREDENTIAL_SETTINGS, resolveAiCredentials } from '@/core/capture/ai/keys';
-import { generateGuideMeta } from '@/core/capture/ai/meta';
+import { finishGuide } from '@/core/capture/ai/finish-guide';
import { settleDescriptions } from '@/core/capture/ai/settle-descriptions';
-import { localStorage } from '@/core/env';
-import { getGuideDomain, updateGuideDescription, updateGuideTitle } from '@/core/guides/service';
+import { getGuideDomain } from '@/core/guides/service';
import { whenNarrationSettled } from './voice';
-async function hasAiKey(): Promise<boolean> {
- return resolveAiCredentials(await localStorage.get([...AI_CREDENTIAL_SETTINGS])) !== null;
-}
-
-async function applyFallbackTitle(guideId: string) {
+async function domainTitle(guideId: string): Promise<string> {
const domain = await getGuideDomain(guideId);
- await updateGuideTitle(
- guideId,
- domain ? i18n.t('background.guideOnDomain', [domain]) : i18n.t('background.newGuide'),
- );
+ return domain ? i18n.t('background.guideOnDomain', [domain]) : i18n.t('background.newGuide');
}
export async function settlePendingDescriptions(guideId: string) {
@@ -25,41 +14,6 @@ export async function settlePendingDescriptions(guideId: string) {
await settleDescriptions(guideId);
}
-async function applyFallbackTitleThenSettle(guideId: string) {
- const titled = applyFallbackTitle(guideId).catch((err) => logger.error('Fallback title write failed', err));
- await settlePendingDescriptions(guideId).catch((err) => logger.error('Settling step descriptions failed', err));
- await titled;
-}
-
-export async function generateGuideMetaOnStop(guideId: string) {
- if (!(await hasAiKey().catch(() => false))) {
- await applyFallbackTitleThenSettle(guideId);
- return;
- }
-
- try {
- await settlePendingDescriptions(guideId);
- const inputs = await resolveGuideMetaInputs(guideId);
- if (!inputs.ok) {
- if (inputs.reason === 'no-api-key') await applyFallbackTitle(guideId);
- return;
- }
-
- const meta = await generateGuideMeta(inputs.steps, inputs.provider, inputs.model, inputs.apiKey, inputs.baseUrl);
- if (!meta) {
- await applyFallbackTitle(guideId);
- return;
- }
-
- await updateGuideTitle(guideId, meta.title);
- logger.info('Generated guide meta:', meta.title);
- if (meta.description) {
- await updateGuideDescription(guideId, meta.description).catch((err) =>
- logger.error('Guide description write failed', err),
- );
- }
- } catch (err) {
- logger.error('Guide meta generation failed', err);
- await applyFallbackTitle(guideId);
- }
+export function generateGuideMetaOnStop(guideId: string) {
+ return finishGuide(guideId, { fallbackTitle: domainTitle, settleNarration: whenNarrationSettled });
}