π¬ 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(voice): orchestrate narration pipeline over injected seams
Luis Ortizfd76b794b7c9
diff --git a/src/core/capture/voice/__tests__/pipeline.test.ts b/src/core/capture/voice/__tests__/pipeline.test.tsnew file mode 100644index 0000000..e0de9a3--- /dev/null+++ b/src/core/capture/voice/__tests__/pipeline.test.ts@@ -0,0 +1,87 @@+import { describe, expect, it, vi } from 'vitest';+import { runNarrationPipeline } from '../pipeline';+import { absoluteSeconds, type StepWindow } from '../types';++const steps: StepWindow[] = [+ { stepId: 's1', from: absoluteSeconds(0), to: absoluteSeconds(10) },+ { stepId: 's2', from: absoluteSeconds(10), to: absoluteSeconds(20) },+];+const pcm = new Int16Array(16000 * 20);+const scores = { no_speech_prob: 0.01, avg_logprob: -0.2, compression_ratio: 1.4 };+const speech = (start: number, end: number) => [+ {+ start: absoluteSeconds(start),+ end: absoluteSeconds(end),+ },+];+const textFor = (result: { descriptions: { stepId: string; text: string }[] }, id: string) =>+ result.descriptions.find((d) => d.stepId === id)?.text;++describe('runNarrationPipeline', () => {+ it('never calls transcribe when no speech is detected', async () => {+ const transcribe = vi.fn();+ const result = await runNarrationPipeline({+ pcm,+ sampleRate: 16000,+ steps,+ detectSpeech: async () => [],+ transcribe,+ });+ expect(transcribe).not.toHaveBeenCalled();+ expect(result.descriptions).toEqual([]);+ });++ it('maps detected speech to the step that owns it', async () => {+ const result = await runNarrationPipeline({+ pcm,+ sampleRate: 16000,+ steps,+ detectSpeech: async () => speech(1, 8),+ transcribe: async () => ({+ segments: [{ start: 0, end: 7, text: 'Open the settings menu.', ...scores }],+ }),+ });+ expect(textFor(result, 's1')).toBe('Open the settings menu.');+ });++ it('joins multiple segments belonging to one step', async () => {+ const result = await runNarrationPipeline({+ pcm,+ sampleRate: 16000,+ steps,+ detectSpeech: async () => speech(1, 9),+ transcribe: async () => ({+ segments: [+ { start: 0, end: 3, text: 'First this.', ...scores },+ { start: 3, end: 7, text: 'Then that.', ...scores },+ ],+ }),+ });+ expect(textFor(result, 's1')).toBe('First this. Then that.');+ });++ it('counts a failed batch and still returns a usable result', async () => {+ const result = await runNarrationPipeline({+ pcm,+ sampleRate: 16000,+ steps,+ detectSpeech: async () => speech(1, 8),+ transcribe: async () => {+ throw new Error('429');+ },+ });+ expect(result.stats.failedBatches).toBe(1);+ expect(result.descriptions).toEqual([]);+ });++ it('reports batches dropped for having too little speech', async () => {+ const result = await runNarrationPipeline({+ pcm,+ sampleRate: 16000,+ steps,+ detectSpeech: async () => speech(1, 1.3),+ transcribe: vi.fn(),+ });+ expect(result.stats.droppedBatches).toBe(1);+ });+});diff --git a/src/core/capture/voice/pipeline.ts b/src/core/capture/voice/pipeline.tsnew file mode 100644index 0000000..caa2928--- /dev/null+++ b/src/core/capture/voice/pipeline.ts@@ -0,0 +1,67 @@+import { assignSegments } from './attribute';+import { buildBatches, mergeGaps } from './batching';+import type { NarrationResult, SpeechSegment, StepWindow, TranscriptionResponse } from './types';+import { encodeWav } from './wav';++export interface PipelineInput {+ pcm: Int16Array;+ sampleRate: number;+ steps: StepWindow[];+ detectSpeech: (pcm: Int16Array, sampleRate: number) => Promise<SpeechSegment[]>;+ transcribe: (wav: Blob) => Promise<TranscriptionResponse>;+}++function sliceSpeech(pcm: Int16Array, segments: SpeechSegment[], sampleRate: number): Int16Array {+ const parts = segments.map((s) =>+ pcm.subarray(Math.max(0, Math.floor(s.start * sampleRate)), Math.min(pcm.length, Math.ceil(s.end * sampleRate))),+ );+ const out = new Int16Array(parts.reduce((total, p) => total + p.length, 0));+ let offset = 0;+ for (const part of parts) {+ out.set(part, offset);+ offset += part.length;+ }+ return out;+}++export async function runNarrationPipeline(input: PipelineInput): Promise<NarrationResult> {+ const { pcm, sampleRate, steps, detectSpeech, transcribe } = input;+ const { batches, dropped } = buildBatches(mergeGaps(await detectSpeech(pcm, sampleRate)));++ const collected = new Map<string, string[]>();+ const stats = {+ batches: batches.length,+ failedBatches: 0,+ droppedBatches: dropped,+ verbatimSegments: 0,+ splitSegments: 0,+ rejectedSegments: 0,+ };++ for (const batch of batches) {+ let response: TranscriptionResponse;+ try {+ response = await transcribe(encodeWav(sliceSpeech(pcm, batch.segments, sampleRate), sampleRate));+ } catch {+ stats.failedBatches += 1;+ continue;+ }+ const assigned = assignSegments(response, batch, steps);+ stats.verbatimSegments += assigned.verbatim;+ stats.splitSegments += assigned.split;+ stats.rejectedSegments += assigned.rejected;+ for (const [stepId, texts] of assigned.byStep) {+ const existing = collected.get(stepId);+ if (existing) existing.push(...texts);+ else collected.set(stepId, [...texts]);+ }+ }++ const descriptions: NarrationResult['descriptions'] = [];+ for (const [stepId, texts] of collected) {+ const text = texts.join(' ').trim();+ if (text) descriptions.push({ stepId, text });+ }++ return { descriptions, stats };+}