Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
refactor(cloud): extract chunked transcribe helper; chunk dictation/retry above 4 MB
Gabriel Steind44b85a5066d
diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.jsindex 98e7e1838..80d138c1f 100644--- a/src/helpers/ipcHandlers.js+++ b/src/helpers/ipcHandlers.js@@ -1,5 +1,7 @@const { ipcMain, app, shell, BrowserWindow, systemPreferences } = require("electron");const path = require("path");+const fs = require("fs");+const os = require("os");const http = require("http");const https = require("https");const crypto = require("crypto");@@ -41,6 +43,10 @@ const AUDIO_MIME_TYPES = {aac: "audio/aac",};+const CLOUD_INLINE_LIMIT = 4 * 1024 * 1024;+const CLOUD_CHUNK_CONCURRENCY = 5;+const CLOUD_CHUNK_SEGMENT_SECONDS = 240;+function buildMultipartBody(fileBuffer, fileName, contentType, fields = {}) {const boundary = `----OpenWhispr${Date.now()}`;const parts = [];@@ -102,6 +108,146 @@ function postMultipart(url, body, boundary, headers = {}) {});}+function interpretTranscribeResponse(data) {+ if (data.statusCode === 401) {+ throw Object.assign(new Error("Session expired"), { code: "AUTH_EXPIRED" });+ }+ if (data.statusCode === 503) {+ throw Object.assign(new Error("Request timed out"), { code: "SERVER_ERROR" });+ }+ if (data.statusCode === 429) {+ throw Object.assign(new Error("Daily word limit reached"), {+ code: "LIMIT_REACHED",+ ...data.data,+ });+ }+ if (data.statusCode === 422 && data.data?.code === "NO_SPEECH_DETECTED") {+ throw Object.assign(new Error(data.data.error || "No speech detected in audio"), {+ code: "NO_SPEECH_DETECTED",+ });+ }+ if (data.statusCode !== 200) {+ throw new Error(data.data?.error || `API error: ${data.statusCode}`);+ }+ return data.data;+}++async function chunkedCloudTranscribe({+ buffer = null,+ filePath = null,+ apiUrl,+ cookieHeader,+ multipartFields = {},+ onProgress,+ concurrencyLimit = CLOUD_CHUNK_CONCURRENCY,+ segmentDuration = CLOUD_CHUNK_SEGMENT_SECONDS,+}) {+ const { splitAudioFile } = require("./ffmpegUtils");++ const jobId = `${Date.now()}-${crypto.randomBytes(4).toString("hex")}`;+ const chunkDir = path.join(os.tmpdir(), `ow-chunks-${jobId}`);+ let tmpInputPath = null;++ let inputPath = filePath;+ if (!inputPath && buffer) {+ tmpInputPath = path.join(os.tmpdir(), `ow-audio-${jobId}.webm`);+ fs.writeFileSync(tmpInputPath, buffer);+ inputPath = tmpInputPath;+ }++ fs.mkdirSync(chunkDir, { recursive: true });++ try {+ onProgress?.({ stage: "splitting", chunksTotal: 0, chunksCompleted: 0 });++ const chunkPaths = await splitAudioFile(inputPath, chunkDir, { segmentDuration });+ const totalChunks = chunkPaths.length;++ onProgress?.({ stage: "transcribing", chunksTotal: totalChunks, chunksCompleted: 0 });++ const results = new Array(totalChunks).fill(null);+ const failureCodes = new Set();+ let completedCount = 0;++ const transcribeChunk = async (index) => {+ const chunkBuffer = fs.readFileSync(chunkPaths[index]);+ const chunkName = path.basename(chunkPaths[index]);+ const { body, boundary } = buildMultipartBody(+ chunkBuffer,+ chunkName,+ "audio/mpeg",+ multipartFields+ );+ const url = new URL(`${apiUrl}/api/transcribe`);+ const data = await postMultipart(url, body, boundary, { Cookie: cookieHeader });++ results[index] = interpretTranscribeResponse(data);+ completedCount++;+ onProgress?.({+ stage: "transcribing",+ chunksTotal: totalChunks,+ chunksCompleted: completedCount,+ });+ };++ const executing = new Set();+ for (let index = 0; index < totalChunks; index++) {+ const p = transcribeChunk(index).then(+ () => executing.delete(p),+ (err) => {+ executing.delete(p);+ if (err.code === "AUTH_EXPIRED" || err.code === "LIMIT_REACHED") throw err;+ if (err.code) failureCodes.add(err.code);+ debugLogger.warn(`Chunk ${index} failed`, { error: err.message, code: err.code });+ }+ );+ executing.add(p);+ if (executing.size >= concurrencyLimit) {+ await Promise.race(executing);+ }+ }+ await Promise.all(executing);++ const succeeded = results.filter((r) => r !== null);+ if (succeeded.length === 0) {+ if (failureCodes.size === 1 && failureCodes.has("NO_SPEECH_DETECTED")) {+ throw Object.assign(new Error("No speech detected in audio"), {+ code: "NO_SPEECH_DETECTED",+ });+ }+ throw new Error("All chunks failed to transcribe");+ }++ const text = results+ .filter((r) => r !== null)+ .map((r) => r.text)+ .join(" ")+ .replace(/\s+/g, " ")+ .trim();++ const failed = totalChunks - succeeded.length;+ return {+ text,+ responses: succeeded,+ lastResponse: succeeded[succeeded.length - 1],+ ...(failed > 0 ? { warning: `${failed} of ${totalChunks} chunks failed` } : {}),+ };+ } finally {+ if (tmpInputPath) {+ try {+ fs.unlinkSync(tmpInputPath);+ } catch {+ // ignore+ }+ }+ try {+ fs.rmSync(chunkDir, { recursive: true, force: true });+ } catch (cleanupErr) {+ debugLogger.warn("Failed to cleanup chunk dir", { error: cleanupErr.message });+ }+ }+}+class IPCHandlers {constructor(managers) {this.environmentManager = managers.environmentManager;@@ -2942,7 +3088,7 @@ class IPCHandlers {if (!cookieHeader) throw new Error("No session cookies available");const audioData = Buffer.from(audioBuffer);- const { body, boundary } = buildMultipartBody(audioData, "audio.webm", "audio/webm", {+ const multipartFields = {language: opts.language,prompt: opts.prompt,sendLogs: opts.sendLogs,@@ -2950,14 +3096,40 @@ class IPCHandlers {appVersion: app.getVersion(),clientVersion: app.getVersion(),sessionId: this.sessionId,- });+ };- debugLogger.debug(- "Cloud transcribe request",- { audioSize: audioData.length, bodySize: body.length },- "cloud-api"- );+ debugLogger.debug("Cloud transcribe request", { audioSize: audioData.length }, "cloud-api");++ if (audioData.length > CLOUD_INLINE_LIMIT) {+ const { text, responses, lastResponse } = await chunkedCloudTranscribe({+ buffer: audioData,+ apiUrl,+ cookieHeader,+ multipartFields,+ });+ const sum = (field) => responses.reduce((s, r) => s + (r?.[field] || 0), 0);+ return {+ success: true,+ text,+ wordsUsed: lastResponse?.wordsUsed,+ wordsRemaining: lastResponse?.wordsRemaining,+ plan: lastResponse?.plan,+ limitReached: lastResponse?.limitReached || false,+ sttProvider: lastResponse?.sttProvider,+ sttModel: lastResponse?.sttModel,+ sttProcessingMs: sum("sttProcessingMs"),+ sttWordCount: sum("sttWordCount"),+ sttLanguage: lastResponse?.sttLanguage,+ audioDurationMs: sum("audioDurationMs"),+ };+ }+ const { body, boundary } = buildMultipartBody(+ audioData,+ "audio.webm",+ "audio/webm",+ multipartFields+ );const url = new URL(`${apiUrl}/api/transcribe`);const data = await postMultipart(url, body, boundary, { Cookie: cookieHeader });@@ -2967,41 +3139,26 @@ class IPCHandlers {"cloud-api");- if (data.statusCode === 401) {- return { success: false, error: "Session expired", code: "AUTH_EXPIRED" };- }- if (data.statusCode === 503) {- return { success: false, error: "Request timed out", code: "SERVER_ERROR" };- }- if (data.statusCode === 429) {- return {- success: false,- error: "Daily word limit reached",- code: "LIMIT_REACHED",- limitReached: true,- ...data.data,- };- }- if (data.statusCode !== 200) {- throw new Error(data.data?.error || `API error: ${data.statusCode}`);- }-+ const result = interpretTranscribeResponse(data);return {success: true,- text: data.data.text,- wordsUsed: data.data.wordsUsed,- wordsRemaining: data.data.wordsRemaining,- plan: data.data.plan,- limitReached: data.data.limitReached || false,- sttProvider: data.data.sttProvider,- sttModel: data.data.sttModel,- sttProcessingMs: data.data.sttProcessingMs,- sttWordCount: data.data.sttWordCount,- sttLanguage: data.data.sttLanguage,- audioDurationMs: data.data.audioDurationMs,+ text: result.text,+ wordsUsed: result.wordsUsed,+ wordsRemaining: result.wordsRemaining,+ plan: result.plan,+ limitReached: result.limitReached || false,+ sttProvider: result.sttProvider,+ sttModel: result.sttModel,+ sttProcessingMs: result.sttProcessingMs,+ sttWordCount: result.sttWordCount,+ sttLanguage: result.sttLanguage,+ audioDurationMs: result.audioDurationMs,};} catch (error) {debugLogger.error("Cloud transcription error", { error: error.message }, "cloud-api");+ if (error.code) {+ return { success: false, error: error.message, code: error.code, ...error };+ }return { success: false, error: error.message };}});@@ -3077,17 +3234,36 @@ class IPCHandlers {if (cookieHeader) {const apiUrl = getApiUrl();if (apiUrl) {- const { body, boundary } = buildMultipartBody(buffer, "audio.webm", "audio/webm", {+ const multipartFields = {clientType: "desktop",appVersion: app.getVersion(),sessionId: this.sessionId,- });- const url = new URL(`${apiUrl}/api/transcribe`);- const data = await postMultipart(url, body, boundary, {- Cookie: cookieHeader,- });- if (data.statusCode === 200 && data.data?.text) {- result = { text: data.data.text, source: "openwhispr", model: "cloud" };+ };+ if (buffer.length > CLOUD_INLINE_LIMIT) {+ const { text } = await chunkedCloudTranscribe({+ buffer,+ apiUrl,+ cookieHeader,+ multipartFields,+ });+ result = { text, source: "openwhispr", model: "cloud" };+ } else {+ const { body, boundary } = buildMultipartBody(+ buffer,+ "audio.webm",+ "audio/webm",+ multipartFields+ );+ const url = new URL(`${apiUrl}/api/transcribe`);+ const data = await postMultipart(url, body, boundary, {+ Cookie: cookieHeader,+ });+ const responseData = interpretTranscribeResponse(data);+ result = {+ text: responseData.text,+ source: "openwhispr",+ model: "cloud",+ };}}}@@ -3164,9 +3340,12 @@ class IPCHandlers {} catch (error) {debugLogger.error("Retry transcription failed",- { id, error: error.message },+ { id, error: error.message, code: error.code },"audio-storage");+ if (error.code) {+ return { success: false, error: error.message, code: error.code, ...error };+ }return { success: false, error: error.message };}});@@ -5264,11 +5443,6 @@ class IPCHandlers {});ipcMain.handle("transcribe-audio-file-cloud", async (event, filePath) => {- const fs = require("fs");- const os = require("os");- const { splitAudioFile } = require("./ffmpegUtils");- const FILE_SIZE_LIMIT = 4 * 1024 * 1024; // 4 MB — Vercel function body limit is ~4.5 MB- const CONCURRENCY_LIMIT = 5;try {const apiUrl = getApiUrl();if (!apiUrl) throw new Error("OpenWhispr API URL not configured");@@ -5276,146 +5450,29 @@ class IPCHandlers {const cookieHeader = await getSessionCookies(event);if (!cookieHeader) throw new Error("No session cookies available");+ const multipartFields = {+ source: "file_upload",+ clientType: "desktop",+ appVersion: app.getVersion(),+ clientVersion: app.getVersion(),+ sessionId: this.sessionId,+ };+const fileSize = fs.statSync(filePath).size;- if (fileSize > FILE_SIZE_LIMIT) {+ if (fileSize > CLOUD_INLINE_LIMIT) {debugLogger.debug("Large file detected, using client-side chunking", {fileSize,filePath: path.basename(filePath),});-- const chunkDir = path.join(os.tmpdir(), `ow-chunks-${Date.now()}`);- fs.mkdirSync(chunkDir, { recursive: true });-- try {- event.sender.send("upload-transcription-progress", {- stage: "splitting",- chunksTotal: 0,- chunksCompleted: 0,- });-- const chunkPaths = await splitAudioFile(filePath, chunkDir, {- segmentDuration: 240, // ~3.75 MB/chunk, under Vercel's 4.5 MB payload limit- });- const totalChunks = chunkPaths.length;-- debugLogger.debug("Audio split into chunks", { totalChunks });-- event.sender.send("upload-transcription-progress", {- stage: "transcribing",- chunksTotal: totalChunks,- chunksCompleted: 0,- });-- const results = new Array(totalChunks).fill(null);- let completedCount = 0;-- const transcribeChunk = async (index) => {- const chunkBuffer = fs.readFileSync(chunkPaths[index]);- const chunkName = path.basename(chunkPaths[index]);-- const { body, boundary } = buildMultipartBody(chunkBuffer, chunkName, "audio/mpeg", {- source: "file_upload",- clientType: "desktop",- appVersion: app.getVersion(),- clientVersion: app.getVersion(),- sessionId: this.sessionId,- });-- const url = new URL(`${apiUrl}/api/transcribe`);- const data = await postMultipart(url, body, boundary, { Cookie: cookieHeader });-- if (data.statusCode === 401) {- throw Object.assign(new Error("Session expired"), { code: "AUTH_EXPIRED" });- }- if (data.statusCode === 503) {- throw Object.assign(new Error("Request timed out"), { code: "SERVER_ERROR" });- }- if (data.statusCode === 429) {- throw Object.assign(new Error("Daily word limit reached"), {- code: "LIMIT_REACHED",- ...data.data,- });- }- if (data.statusCode === 422 && data.data?.code === "NO_SPEECH_DETECTED") {- throw Object.assign(new Error(data.data.error || "No speech detected"), {- code: "NO_SPEECH_DETECTED",- });- }- if (data.statusCode !== 200) {- throw new Error(data.data?.error || `API error: ${data.statusCode}`);- }-- results[index] = data.data;- completedCount++;-- event.sender.send("upload-transcription-progress", {- stage: "transcribing",- chunksTotal: totalChunks,- chunksCompleted: completedCount,- });- };-- const indices = Array.from({ length: totalChunks }, (_, i) => i);- const executing = new Set();- const failureCodes = new Set();-- for (const index of indices) {- const p = transcribeChunk(index).then(- () => executing.delete(p),- (err) => {- executing.delete(p);- if (err.code === "AUTH_EXPIRED" || err.code === "LIMIT_REACHED") throw err;- if (err.code) failureCodes.add(err.code);- debugLogger.warn(`Chunk ${index} failed`, {- error: err.message,- code: err.code,- });- }- );- executing.add(p);- if (executing.size >= CONCURRENCY_LIMIT) {- await Promise.race(executing);- }- }- await Promise.all(executing);-- const succeeded = results.filter((r) => r !== null);- if (succeeded.length === 0) {- if (failureCodes.size === 1 && failureCodes.has("NO_SPEECH_DETECTED")) {- return {- success: false,- error: "No speech detected in audio",- code: "NO_SPEECH_DETECTED",- };- }- throw new Error("All chunks failed to transcribe");- }-- const fullText = results- .filter((r) => r !== null)- .map((r) => r.text)- .join(" ")- .replace(/\s+/g, " ")- .trim();-- const failed = results.filter((r) => r === null).length;- if (failed > 0) {- debugLogger.warn("Some chunks failed", { failed, total: totalChunks });- }-- return {- success: true,- text: fullText,- ...(failed > 0 ? { warning: `${failed} of ${totalChunks} chunks failed` } : {}),- };- } finally {- try {- fs.rmSync(chunkDir, { recursive: true, force: true });- } catch (cleanupErr) {- debugLogger.warn("Failed to cleanup chunk dir", { error: cleanupErr.message });- }- }+ const { text, warning } = await chunkedCloudTranscribe({+ filePath,+ apiUrl,+ cookieHeader,+ multipartFields,+ onProgress: (payload) => event.sender.send("upload-transcription-progress", payload),+ });+ return { success: true, text, ...(warning ? { warning } : {}) };}const audioBuffer = fs.readFileSync(filePath);@@ -5423,50 +5480,20 @@ class IPCHandlers {const contentType = AUDIO_MIME_TYPES[ext] || "audio/mpeg";const fileName = path.basename(filePath);- const { body, boundary } = buildMultipartBody(audioBuffer, fileName, contentType, {- source: "file_upload",- clientType: "desktop",- appVersion: app.getVersion(),- clientVersion: app.getVersion(),- sessionId: this.sessionId,- });-+ const { body, boundary } = buildMultipartBody(+ audioBuffer,+ fileName,+ contentType,+ multipartFields+ );const url = new URL(`${apiUrl}/api/transcribe`);const data = await postMultipart(url, body, boundary, { Cookie: cookieHeader });+ const result = interpretTranscribeResponse(data);- if (data.statusCode === 401) {- return { success: false, error: "Session expired", code: "AUTH_EXPIRED" };- }- if (data.statusCode === 503) {- return { success: false, error: "Request timed out", code: "SERVER_ERROR" };- }- if (data.statusCode === 429) {- return {- success: false,- error: "Daily word limit reached",- code: "LIMIT_REACHED",- ...data.data,- };- }- if (data.statusCode === 422 && data.data?.code === "NO_SPEECH_DETECTED") {- return {- success: false,- error: data.data.error || "No speech detected in audio",- code: "NO_SPEECH_DETECTED",- };- }- if (data.statusCode !== 200) {- throw new Error(data.data?.error || `API error: ${data.statusCode}`);- }-- return { success: true, text: data.data.text };+ return { success: true, text: result.text };} catch (error) {debugLogger.error("Cloud audio file transcription error", { error: error.message });- if (- error.code === "AUTH_EXPIRED" ||- error.code === "LIMIT_REACHED" ||- error.code === "NO_SPEECH_DETECTED"- ) {+ if (error.code) {return { success: false, error: error.message, code: error.code, ...error };}return { success: false, error: error.message };