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(voice): push the narration result instead of returning it from stop

Luis Ortiz7e0d45b6e09a
diff --git a/src/entrypoints/offscreen/index.ts b/src/entrypoints/offscreen/index.ts
index b23551a..93e07d7 100644
--- a/src/entrypoints/offscreen/index.ts
+++ b/src/entrypoints/offscreen/index.ts
@@ -19,6 +19,7 @@ import {
type VoicePermissionQueryResponse,
type VoicePermissionState,
type VoiceRequest,
+ type VoiceResultEvent,
type VoiceStartRequest,
type VoiceStartResponse,
type VoiceStatusResponse,
@@ -26,10 +27,16 @@ import {
type VoiceStopResponse,
voiceMessage,
} from '@/lib/voice-messages';
-import { MicRecorder } from './mic-recorder';
+import { MicRecorder, type MicRecording } from './mic-recorder';
const VOICE_PROVIDERS: VoiceProvider[] = ['openai', 'groq'];
+interface VoiceSettings {
+ provider: VoiceProvider;
+ apiKey: string;
+ language?: string;
+}
+
const EMPTY_RESULT: NarrationResult = {
descriptions: [],
stats: {
@@ -87,7 +94,7 @@ const recorder = new MicRecorder(getExtensionURL('/pcm-processor.js'), {
),
});
-async function readVoiceSettings(): Promise<{ provider: VoiceProvider; apiKey: string; language?: string }> {
+async function readVoiceSettings(): Promise<VoiceSettings> {
const stored = await localStorage.get(['voiceProvider', 'voiceApiKey', 'voiceLanguage', 'aiLanguage']);
const provider = stored.voiceProvider as VoiceProvider;
const locale = (stored.voiceLanguage ?? stored.aiLanguage) as string | undefined;
@@ -113,6 +120,53 @@ async function handleStart(request: VoiceStartRequest): Promise<VoiceStartRespon
}
}
+let pending = 0;
+
+function deliver(guideId: string, result: NarrationResult): void {
+ emit(
+ voiceMessage<VoiceResultEvent>({
+ type: VoiceMessage.VOICE_RESULT,
+ target: VOICE_BACKGROUND_TARGET,
+ guideId,
+ result,
+ }),
+ );
+}
+
+async function narrate(
+ request: VoiceStopRequest,
+ recording: MicRecording,
+ audioEpochMs: number,
+ settings: VoiceSettings,
+): Promise<NarrationResult> {
+ try {
+ const result = await runNarrationPipeline({
+ pcm: recording.pcm,
+ sampleRate: recording.sampleRate,
+ steps: buildStepWindows(request.steps, audioEpochMs, recording.durationSeconds),
+ detectSpeech: detectSpeechByEnergy,
+ transcribe: createTranscriber(settings),
+ });
+ logger.info('voice: narration pipeline finished', result.stats);
+ return result;
+ } catch (error) {
+ logger.error('voice: narration pipeline failed', error);
+ return EMPTY_RESULT;
+ }
+}
+
+async function transcribeInBackground(
+ request: VoiceStopRequest,
+ recording: MicRecording,
+ audioEpochMs: number,
+ settings: VoiceSettings,
+): Promise<void> {
+ pending += 1;
+ const result = await narrate(request, recording, audioEpochMs, settings);
+ pending -= 1;
+ deliver(request.guideId, result);
+}
+
async function handleStop(request: VoiceStopRequest): Promise<VoiceStopResponse> {
if (!recorder.recording) {
return { ok: false, reason: 'not-recording', error: 'Microphone capture is not running' };
@@ -124,8 +178,10 @@ async function handleStop(request: VoiceStopRequest): Promise<VoiceStopResponse>
}
const audioEpochMs = recording.audioEpochMs;
+ const durationSeconds = recording.durationSeconds;
if (request.steps.length === 0) {
- return { ok: true, result: EMPTY_RESULT, audioEpochMs, durationSeconds: recording.durationSeconds };
+ deliver(request.guideId, EMPTY_RESULT);
+ return { ok: true, audioEpochMs, durationSeconds };
}
const settings = await readVoiceSettings();
@@ -133,20 +189,8 @@ async function handleStop(request: VoiceStopRequest): Promise<VoiceStopResponse>
return { ok: false, reason: 'missing-api-key', error: 'No transcription API key is configured' };
}
- try {
- const result = await runNarrationPipeline({
- pcm: recording.pcm,
- sampleRate: recording.sampleRate,
- steps: buildStepWindows(request.steps, audioEpochMs),
- detectSpeech: detectSpeechByEnergy,
- transcribe: createTranscriber(settings),
- });
- logger.info('voice: narration pipeline finished', result.stats);
- return { ok: true, result, audioEpochMs, durationSeconds: recording.durationSeconds };
- } catch (error) {
- logger.error('voice: narration pipeline failed', error);
- return { ok: false, reason: 'unknown', error: describe(error) };
- }
+ void transcribeInBackground(request, recording, audioEpochMs, settings);
+ return { ok: true, audioEpochMs, durationSeconds };
}
async function permissionState(): Promise<VoicePermissionQueryResponse> {
@@ -161,6 +205,7 @@ async function permissionState(): Promise<VoicePermissionQueryResponse> {
function status(): VoiceStatusResponse {
return {
recording: recorder.recording,
+ transcribing: pending > 0,
audioEpochMs: recorder.audioEpochMs,
sampleRate: recorder.sampleRate,
samples: recorder.sampleCount,
diff --git a/src/lib/offscreen.ts b/src/lib/offscreen.ts
index eed3092..c12d75c 100644
--- a/src/lib/offscreen.ts
+++ b/src/lib/offscreen.ts
@@ -107,9 +107,9 @@ export function startVoiceCapture(deviceId?: string): Promise<VoiceStartResponse
);
}
-export function stopVoiceCapture(steps: VoiceStepMark[]): Promise<VoiceStopResponse> {
+export function stopVoiceCapture(guideId: string, steps: VoiceStepMark[]): Promise<VoiceStopResponse> {
return request(
- voiceMessage<VoiceStopRequest>({ type: VoiceMessage.VOICE_STOP, target: VOICE_OFFSCREEN_TARGET, steps }),
+ voiceMessage<VoiceStopRequest>({ type: VoiceMessage.VOICE_STOP, target: VOICE_OFFSCREEN_TARGET, guideId, steps }),
);
}
diff --git a/src/lib/voice-messages.ts b/src/lib/voice-messages.ts
index 3ad6b0f..df71ec6 100644
--- a/src/lib/voice-messages.ts
+++ b/src/lib/voice-messages.ts
@@ -18,6 +18,7 @@ export const VoiceMessage = {
VOICE_EPOCH: 'VOICE_EPOCH',
VOICE_LEVEL: 'VOICE_LEVEL',
VOICE_ERROR: 'VOICE_ERROR',
+ VOICE_RESULT: 'VOICE_RESULT',
VOICE_PERMISSION_RESULT: 'VOICE_PERMISSION_RESULT',
} as const;
@@ -58,11 +59,12 @@ export interface VoiceStepMark {
export interface VoiceStopRequest extends VoiceEnvelope {
type: typeof VoiceMessage.VOICE_STOP;
target: typeof VOICE_OFFSCREEN_TARGET;
+ guideId: string;
steps: VoiceStepMark[];
}
export type VoiceStopResponse =
- | { ok: true; result: NarrationResult; audioEpochMs: number; durationSeconds: number }
+ | { ok: true; audioEpochMs: number; durationSeconds: number }
| { ok: false; reason: VoiceErrorReason; error: string };
export interface VoiceAbortRequest extends VoiceEnvelope {
@@ -81,6 +83,7 @@ export interface VoiceStatusRequest extends VoiceEnvelope {
export interface VoiceStatusResponse {
recording: boolean;
+ transcribing: boolean;
audioEpochMs: number | null;
sampleRate: number;
samples: number;
@@ -118,6 +121,13 @@ export interface VoiceErrorEvent extends VoiceEnvelope {
error: string;
}
+export interface VoiceResultEvent extends VoiceEnvelope {
+ type: typeof VoiceMessage.VOICE_RESULT;
+ target: typeof VOICE_BACKGROUND_TARGET;
+ guideId: string;
+ result: NarrationResult;
+}
+
export interface VoicePermissionResultEvent extends VoiceEnvelope {
type: typeof VoiceMessage.VOICE_PERMISSION_RESULT;
target: typeof VOICE_BACKGROUND_TARGET;
@@ -131,7 +141,12 @@ export type VoiceRequest =
| VoiceStatusRequest
| VoicePermissionQueryRequest;
-export type VoiceEvent = VoiceEpochEvent | VoiceLevelEvent | VoiceErrorEvent | VoicePermissionResultEvent;
+export type VoiceEvent =
+ | VoiceEpochEvent
+ | VoiceLevelEvent
+ | VoiceErrorEvent
+ | VoiceResultEvent
+ | VoicePermissionResultEvent;
export function voiceMessage<T extends VoiceEnvelope>(message: Omit<T, 'timestamp'>): T {
return { ...message, timestamp: Date.now() } as T;