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 πŸ“–

feat(voice): transcribe speech batches via OpenAI or Groq whisper

Luis Ortiz84a185507b6e
diff --git a/src/core/capture/voice/__tests__/transcribe.test.ts b/src/core/capture/voice/__tests__/transcribe.test.ts
new file mode 100644
index 0000000..3cd5036
--- /dev/null
+++ b/src/core/capture/voice/__tests__/transcribe.test.ts
@@ -0,0 +1,137 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { createTranscriber, type TranscribeConfig } from '../transcribe';
+
+const wav = new Blob([new Uint8Array(44)], { type: 'audio/wav' });
+
+const scored = { no_speech_prob: 0.01, avg_logprob: -0.2, compression_ratio: 1.4 };
+const verbose = {
+ text: 'Open the settings menu.',
+ segments: [{ start: 0, end: 2, text: 'Open the settings menu.', ...scored }],
+ words: [{ word: 'Open', start: 0, end: 0.4 }],
+};
+
+let fetchMock: ReturnType<typeof vi.fn>;
+
+const respondWith = (body: unknown, status = 200) => {
+ const payload = typeof body === 'string' ? body : JSON.stringify(body);
+ fetchMock.mockImplementation(async () => new Response(payload, { status }));
+};
+
+const sentTo = (call = 0) => fetchMock.mock.calls[call][0] as string;
+const sentInit = (call = 0) => fetchMock.mock.calls[call][1] as RequestInit;
+const sentForm = (call = 0) => sentInit(call).body as FormData;
+
+const run = (config: TranscribeConfig) => createTranscriber(config)(wav);
+
+beforeEach(() => {
+ fetchMock = vi.fn();
+ vi.stubGlobal('fetch', fetchMock);
+ respondWith(verbose);
+});
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+});
+
+describe('createTranscriber', () => {
+ it('appends timestamp_granularities[] twice, as word and segment', async () => {
+ await run({ provider: 'openai', apiKey: 'sk-test' });
+ expect(sentForm().getAll('timestamp_granularities[]')).toEqual(['word', 'segment']);
+ });
+
+ it('requests verbose_json at temperature 0', async () => {
+ await run({ provider: 'openai', apiKey: 'sk-test' });
+ expect(sentForm().get('response_format')).toBe('verbose_json');
+ expect(sentForm().get('temperature')).toBe('0');
+ });
+
+ it('sends the audio file part', async () => {
+ await run({ provider: 'openai', apiKey: 'sk-test' });
+ expect(sentForm().get('file')).toBeInstanceOf(Blob);
+ });
+
+ it('sends whisper-1 to the OpenAI endpoint', async () => {
+ await run({ provider: 'openai', apiKey: 'sk-test' });
+ expect(sentTo()).toBe('https://api.openai.com/v1/audio/transcriptions');
+ expect(sentForm().get('model')).toBe('whisper-1');
+ });
+
+ it('sends whisper-large-v3 to the Groq endpoint', async () => {
+ await run({ provider: 'groq', apiKey: 'gsk-test' });
+ expect(sentTo()).toBe('https://api.groq.com/openai/v1/audio/transcriptions');
+ expect(sentForm().get('model')).toBe('whisper-large-v3');
+ });
+
+ it('authorises with a bearer token', async () => {
+ await run({ provider: 'openai', apiKey: 'sk-test' });
+ expect(sentInit().method).toBe('POST');
+ expect(sentInit().headers).toEqual({ Authorization: 'Bearer sk-test' });
+ });
+
+ it('never sends a prompt parameter under any config', async () => {
+ const configs: TranscribeConfig[] = [
+ { provider: 'openai', apiKey: 'sk-test' },
+ { provider: 'openai', apiKey: 'sk-test', language: 'en' },
+ { provider: 'groq', apiKey: 'gsk-test' },
+ { provider: 'groq', apiKey: 'gsk-test', language: 'pt' },
+ ];
+ for (const config of configs) await run(config);
+ for (let call = 0; call < configs.length; call += 1) {
+ expect(sentForm(call).has('prompt')).toBe(false);
+ }
+ });
+
+ it('sends exactly the expected parameters and nothing else', async () => {
+ await run({ provider: 'openai', apiKey: 'sk-test' });
+ expect([...sentForm().keys()].sort()).toEqual([
+ 'file',
+ 'model',
+ 'response_format',
+ 'temperature',
+ 'timestamp_granularities[]',
+ 'timestamp_granularities[]',
+ ]);
+ });
+
+ it('sends language when configured', async () => {
+ await run({ provider: 'openai', apiKey: 'sk-test', language: 'pt' });
+ expect(sentForm().get('language')).toBe('pt');
+ });
+
+ it('omits language when not configured', async () => {
+ await run({ provider: 'openai', apiKey: 'sk-test' });
+ expect(sentForm().has('language')).toBe(false);
+ });
+
+ it('throws with the status on a 401', async () => {
+ respondWith('{"error":{"message":"Incorrect API key provided"}}', 401);
+ await expect(run({ provider: 'openai', apiKey: 'sk-secret-key' })).rejects.toThrow(/401/);
+ });
+
+ it('throws with the status on a 429', async () => {
+ respondWith('{"error":{"message":"Rate limit reached"}}', 429);
+ await expect(run({ provider: 'groq', apiKey: 'gsk-test' })).rejects.toThrow(/429/);
+ });
+
+ it('never leaks the api key in the thrown error', async () => {
+ respondWith('{"error":{"message":"Incorrect API key provided"}}', 401);
+ await expect(run({ provider: 'openai', apiKey: 'sk-secret-key' })).rejects.toThrow(
+ expect.not.stringContaining('sk-secret-key'),
+ );
+ });
+
+ it('includes a truncated response body in the error', async () => {
+ respondWith('x'.repeat(5000), 500);
+ const error = await run({ provider: 'openai', apiKey: 'sk-test' }).catch((e: Error) => e);
+ expect((error as Error).message.length).toBeLessThan(400);
+ });
+
+ it('throws when a successful response carries no segments', async () => {
+ respondWith({ text: 'Open the settings menu.' });
+ await expect(run({ provider: 'openai', apiKey: 'sk-test' })).rejects.toThrow(/segments/);
+ });
+
+ it('returns a well-formed verbose_json response as-is', async () => {
+ await expect(run({ provider: 'openai', apiKey: 'sk-test' })).resolves.toEqual(verbose);
+ });
+});
diff --git a/src/core/capture/voice/transcribe.ts b/src/core/capture/voice/transcribe.ts
new file mode 100644
index 0000000..92640b2
--- /dev/null
+++ b/src/core/capture/voice/transcribe.ts
@@ -0,0 +1,49 @@
+import type { TranscriptionResponse } from './types';
+
+export type VoiceProvider = 'openai' | 'groq';
+
+export interface TranscribeConfig {
+ provider: VoiceProvider;
+ apiKey: string;
+ language?: string;
+}
+
+const PROVIDERS: Record<VoiceProvider, { url: string; model: string }> = {
+ openai: { url: 'https://api.openai.com/v1/audio/transcriptions', model: 'whisper-1' },
+ groq: { url: 'https://api.groq.com/openai/v1/audio/transcriptions', model: 'whisper-large-v3' },
+};
+
+const ERROR_BODY_LIMIT = 200;
+
+export function createTranscriber(config: TranscribeConfig): (wav: Blob) => Promise<TranscriptionResponse> {
+ const { url, model } = PROVIDERS[config.provider] ?? PROVIDERS.openai;
+
+ return async (wav: Blob): Promise<TranscriptionResponse> => {
+ const form = new FormData();
+ form.append('file', wav, 'audio.wav');
+ form.append('model', model);
+ form.append('response_format', 'verbose_json');
+ form.append('timestamp_granularities[]', 'word');
+ form.append('timestamp_granularities[]', 'segment');
+ form.append('temperature', '0');
+ if (config.language) form.append('language', config.language);
+
+ const response = await fetch(url, {
+ method: 'POST',
+ headers: { Authorization: `Bearer ${config.apiKey}` },
+ body: form,
+ });
+
+ if (!response.ok) {
+ const body = await response.text().catch(() => '');
+ throw new Error(`Transcription failed with ${response.status}: ${body.slice(0, ERROR_BODY_LIMIT)}`);
+ }
+
+ const result = (await response.json()) as TranscriptionResponse;
+ if (!Array.isArray(result.segments)) {
+ throw new Error('Transcription response has no segments; verbose_json is required');
+ }
+
+ return result;
+ };
+}