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: add whisper-server HTTP mode for faster repeated transcriptions with CLI fallback, reduce Windows clipboard delays, add dependency logging
Gabriel Stein96dd61000ed9
diff --git a/electron-builder.json b/electron-builder.jsonindex e9662aba1..c1c3ed2f4 100644--- a/electron-builder.json+++ b/electron-builder.json@@ -33,7 +33,7 @@{"from": "resources/bin/","to": "bin/",- "filter": ["whisper-cpp-*"]+ "filter": ["whisper-cpp-*", "whisper-server-*"]}],"mac": {diff --git a/main.js b/main.jsindex f9f10d8ee..6b9113208 100644--- a/main.js+++ b/main.js@@ -124,6 +124,12 @@ async function startApp() {// Whisper not being available at startup is not critical});+ // Log nircmd status on Windows (for debugging bundled dependencies)+ if (process.platform === "win32") {+ const nircmdStatus = clipboardManager.getNircmdStatus();+ debugLogger.debug("Windows paste tool status", nircmdStatus);+ }+// Create main windowtry {await windowManager.createMainWindow();@@ -244,4 +250,6 @@ app.on("will-quit", () => {globalShortcut.unregisterAll();globeKeyManager.stop();updateManager.cleanup();+ // Stop whisper server if running+ whisperManager.stopServer().catch(() => {});});diff --git a/preload.js b/preload.jsindex 7cd8aeb70..0590ea6d9 100644--- a/preload.js+++ b/preload.js@@ -88,6 +88,12 @@ contextBridge.exposeInMainWorld("electronAPI", {checkFFmpegAvailability: () =>ipcRenderer.invoke("check-ffmpeg-availability"),+ // Whisper server functions (faster repeated transcriptions)+ whisperServerStart: (modelName) =>+ ipcRenderer.invoke("whisper-server-start", modelName),+ whisperServerStop: () => ipcRenderer.invoke("whisper-server-stop"),+ whisperServerStatus: () => ipcRenderer.invoke("whisper-server-status"),+// Window control functionswindowMinimize: () => ipcRenderer.invoke("window-minimize"),windowMaximize: () => ipcRenderer.invoke("window-maximize"),diff --git a/scripts/download-whisper-cpp.js b/scripts/download-whisper-cpp.jsindex 336c9ea80..4a5ed0a7b 100644--- a/scripts/download-whisper-cpp.js+++ b/scripts/download-whisper-cpp.js@@ -4,6 +4,8 @@** Binaries are built via GitHub Actions and published to:* https://github.com/gabrielste1n/whisper.cpp/releases+ *+ * Downloads both whisper-cli (transcription tool) and whisper-server (HTTP API server).*/const https = require("https");@@ -13,10 +15,10 @@ const { execSync } = require("child_process");// Configuration - Update WHISPER_CPP_VERSION when releasing new buildsconst WHISPER_CPP_REPO = "gabrielste1n/whisper.cpp";-const WHISPER_CPP_VERSION = "0.0.3";+const WHISPER_CPP_VERSION = "0.0.4"; // Bump version for server binaries-// Platform-specific binary info-const BINARIES = {+// Platform-specific binary info for whisper-cli+const CLI_BINARIES = {"darwin-arm64": {zipName: "whisper-cpp-darwin-arm64.zip",binaryName: "whisper-cpp-darwin-arm64",@@ -39,6 +41,30 @@ const BINARIES = {},};+// Platform-specific binary info for whisper-server (HTTP API)+const SERVER_BINARIES = {+ "darwin-arm64": {+ zipName: "whisper-server-darwin-arm64.zip",+ binaryName: "whisper-server-darwin-arm64",+ outputName: "whisper-server-darwin-arm64",+ },+ "darwin-x64": {+ zipName: "whisper-server-darwin-x64.zip",+ binaryName: "whisper-server-darwin-x64",+ outputName: "whisper-server-darwin-x64",+ },+ "win32-x64": {+ zipName: "whisper-server-win32-x64.zip",+ binaryName: "whisper-server-win32-x64.exe",+ outputName: "whisper-server-win32-x64.exe",+ },+ "linux-x64": {+ zipName: "whisper-server-linux-x64.zip",+ binaryName: "whisper-server-linux-x64",+ outputName: "whisper-server-linux-x64",+ },+};+const BIN_DIR = path.join(__dirname, "..", "resources", "bin");function getDownloadUrl(zipName) {@@ -155,29 +181,28 @@ function extractZip(zipPath, destDir) {}}-async function downloadBinary(platformArch) {- const config = BINARIES[platformArch];+async function downloadBinary(platformArch, config, label) {if (!config) {- console.log(` ${platformArch}: Not supported`);+ console.log(` ${label} ${platformArch}: Not supported`);return false;}const outputPath = path.join(BIN_DIR, config.outputName);if (fs.existsSync(outputPath)) {- console.log(` ${platformArch}: Already exists, skipping`);+ console.log(` ${label} ${platformArch}: Already exists, skipping`);return true;}const url = getDownloadUrl(config.zipName);- console.log(` ${platformArch}: Downloading from ${url}`);+ console.log(` ${label} ${platformArch}: Downloading from ${url}`);const zipPath = path.join(BIN_DIR, config.zipName);try {await downloadFile(url, zipPath);- const extractDir = path.join(BIN_DIR, `temp-${platformArch}`);+ const extractDir = path.join(BIN_DIR, `temp-${label}-${platformArch}`);fs.mkdirSync(extractDir, { recursive: true });extractZip(zipPath, extractDir);@@ -188,9 +213,9 @@ async function downloadBinary(platformArch) {if (process.platform !== "win32") {fs.chmodSync(outputPath, 0o755);}- console.log(` ${platformArch}: Extracted to ${config.outputName}`);+ console.log(` ${label} ${platformArch}: Extracted to ${config.outputName}`);} else {- console.error(` ${platformArch}: Binary not found in archive`);+ console.error(` ${label} ${platformArch}: Binary not found in archive`);return false;}@@ -200,12 +225,18 @@ async function downloadBinary(platformArch) {return true;} catch (error) {- console.error(` ${platformArch}: Failed - ${error.message}`);+ console.error(` ${label} ${platformArch}: Failed - ${error.message}`);if (fs.existsSync(zipPath)) fs.unlinkSync(zipPath);return false;}}+async function downloadForPlatform(platformArch) {+ // Download both CLI and server binaries for the platform+ await downloadBinary(platformArch, CLI_BINARIES[platformArch], "[cli]");+ await downloadBinary(platformArch, SERVER_BINARIES[platformArch], "[server]");+}+async function main() {console.log(`\nDownloading whisper.cpp binaries (${WHISPER_CPP_VERSION})...\n`);@@ -219,31 +250,53 @@ async function main() {if (process.argv.includes("--current")) {// Only download for current platformconsole.log(`Downloading for current platform (${currentPlatformArch}):`);- await downloadBinary(currentPlatformArch);+ await downloadForPlatform(currentPlatformArch);+ } else if (process.argv.includes("--cli-only")) {+ // Only download CLI binaries (no server)+ console.log("Downloading CLI binaries only:");+ for (const platformArch of Object.keys(CLI_BINARIES)) {+ await downloadBinary(platformArch, CLI_BINARIES[platformArch], "[cli]");+ }} else if (process.argv.includes("--all")) {// Download all platforms- console.log("Downloading all platform binaries:");- for (const platformArch of Object.keys(BINARIES)) {- await downloadBinary(platformArch);+ console.log("Downloading all platform binaries (CLI + server):");+ for (const platformArch of Object.keys(CLI_BINARIES)) {+ await downloadForPlatform(platformArch);}} else {// Default: download for build targets (all platforms)- console.log("Downloading binaries for all platforms:");- for (const platformArch of Object.keys(BINARIES)) {- await downloadBinary(platformArch);+ console.log("Downloading binaries for all platforms (CLI + server):");+ for (const platformArch of Object.keys(CLI_BINARIES)) {+ await downloadForPlatform(platformArch);}}console.log("\n---");// List what we have- const files = fs.readdirSync(BIN_DIR).filter(f => f.startsWith("whisper-cpp"));+ const files = fs.readdirSync(BIN_DIR).filter(f => f.startsWith("whisper-"));if (files.length > 0) {console.log("Available binaries:");- files.forEach(f => {- const stats = fs.statSync(path.join(BIN_DIR, f));- console.log(` - ${f} (${Math.round(stats.size / 1024 / 1024)}MB)`);- });++ // Group by type+ const cliFiles = files.filter(f => f.startsWith("whisper-cpp"));+ const serverFiles = files.filter(f => f.startsWith("whisper-server"));++ if (cliFiles.length > 0) {+ console.log("\n CLI (whisper-cli):");+ cliFiles.forEach(f => {+ const stats = fs.statSync(path.join(BIN_DIR, f));+ console.log(` - ${f} (${Math.round(stats.size / 1024 / 1024)}MB)`);+ });+ }++ if (serverFiles.length > 0) {+ console.log("\n Server (whisper-server):");+ serverFiles.forEach(f => {+ const stats = fs.statSync(path.join(BIN_DIR, f));+ console.log(` - ${f} (${Math.round(stats.size / 1024 / 1024)}MB)`);+ });+ }} else {console.log("No binaries downloaded yet.");console.log("\nMake sure you've created a release in your whisper.cpp fork:");diff --git a/src/helpers/clipboard.js b/src/helpers/clipboard.jsindex cfdfbd548..897d21aed 100644--- a/src/helpers/clipboard.js+++ b/src/helpers/clipboard.js@@ -54,6 +54,17 @@ class ClipboardManager {return null;}+ getNircmdStatus() {+ if (process.platform !== "win32") {+ return { available: false, reason: "Not Windows" };+ }+ const nircmdPath = this.getNircmdPath();+ return {+ available: !!nircmdPath,+ path: nircmdPath,+ };+ }+// Safe logging method - only log in developmentsafeLog(...args) {if (process.env.NODE_ENV === "development") {@@ -199,11 +210,10 @@ class ClipboardManager {if (code === 0) {this.safeLog(`✅ nircmd paste completed successfully in ${elapsed}ms`);- // Restore original clipboard quicklysetTimeout(() => {clipboard.writeText(originalClipboard);this.safeLog("🔄 Original clipboard content restored");- }, 50);+ }, 10);resolve();} else {this.safeLog(@@ -280,12 +290,10 @@ class ClipboardManager {if (code === 0) {this.safeLog(`✅ PowerShell paste completed successfully in ${elapsed}ms`);- // Text pasted successfully - restore original clipboard- // Use shorter delay since paste is already completesetTimeout(() => {clipboard.writeText(originalClipboard);this.safeLog("🔄 Original clipboard content restored");- }, 50);+ }, 10);resolve();} else {this.safeLog(diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.jsindex c52e32492..2cad0fdc6 100644--- a/src/helpers/ipcHandlers.js+++ b/src/helpers/ipcHandlers.js@@ -227,6 +227,19 @@ class IPCHandlers {return this.whisperManager.cancelDownload();});+ // Whisper server handlers (for faster repeated transcriptions)+ ipcMain.handle("whisper-server-start", async (event, modelName) => {+ return this.whisperManager.startServer(modelName);+ });++ ipcMain.handle("whisper-server-stop", async () => {+ return this.whisperManager.stopServer();+ });++ ipcMain.handle("whisper-server-status", async () => {+ return this.whisperManager.getServerStatus();+ });+ipcMain.handle("check-ffmpeg-availability", async (event) => {return this.whisperManager.checkFFmpegAvailability();});diff --git a/src/helpers/whisper.js b/src/helpers/whisper.jsindex 9390bf3a0..981df2b0d 100644--- a/src/helpers/whisper.js+++ b/src/helpers/whisper.js@@ -8,6 +8,7 @@ const crypto = require("crypto");const https = require("https");const { runCommand, killProcess, TIMEOUTS } = require("../utils/process");const debugLogger = require("./debugLogger");+const WhisperServerManager = require("./whisperServer");// Cache TTL for availability checksconst CACHE_TTL_MS = 30000;@@ -47,6 +48,10 @@ class WhisperManager {this.currentDownloadProcess = null;this.ffmpegAvailabilityCache = { result: null, expiresAt: 0 };this.isInitialized = false;+ // Server manager for HTTP-based transcription (faster for repeated use)+ this.serverManager = new WhisperServerManager();+ this.useServerMode = true; // Prefer server mode when available+ this.currentServerModel = null;}getModelsDir() {@@ -215,6 +220,104 @@ class WhisperManager {debugLogger.log("whisper.cpp not available at startup:", error.message);this.isInitialized = true;}++ // Log dependency status for debugging+ await this.logDependencyStatus();+ }++ async logDependencyStatus() {+ const status = {+ whisperServer: {+ available: this.serverManager.isAvailable(),+ path: this.serverManager.getServerBinaryPath(),+ },+ whisperCli: {+ available: this.cachedBinaryPath !== null,+ path: this.cachedBinaryPath,+ },+ ffmpeg: {+ available: false,+ path: null,+ },+ models: [],+ };++ // Check FFmpeg+ try {+ const ffmpegPath = await this.getFFmpegPath();+ status.ffmpeg.available = !!ffmpegPath;+ status.ffmpeg.path = ffmpegPath;+ } catch {+ // FFmpeg not available+ }++ // Check downloaded models+ for (const modelName of Object.keys(WHISPER_MODELS)) {+ const modelPath = this.getModelPath(modelName);+ if (fs.existsSync(modelPath)) {+ try {+ const stats = fs.statSync(modelPath);+ status.models.push({+ name: modelName,+ size: `${Math.round(stats.size / (1024 * 1024))}MB`,+ });+ } catch {+ // Skip if can't stat+ }+ }+ }++ debugLogger.info("OpenWhispr dependency check", status);++ // Log a summary for easy scanning+ const serverStatus = status.whisperServer.available+ ? `✓ ${status.whisperServer.path}`+ : "✗ Not found";+ const cliStatus = status.whisperCli.available ? `✓ ${status.whisperCli.path}` : "✗ Not found";+ const ffmpegStatus = status.ffmpeg.available ? `✓ ${status.ffmpeg.path}` : "✗ Not found";+ const modelsStatus =+ status.models.length > 0+ ? status.models.map((m) => `${m.name} (${m.size})`).join(", ")+ : "None downloaded";++ debugLogger.info(`[Dependencies] whisper-server: ${serverStatus}`);+ debugLogger.info(`[Dependencies] whisper-cli: ${cliStatus}`);+ debugLogger.info(`[Dependencies] FFmpeg: ${ffmpegStatus}`);+ debugLogger.info(`[Dependencies] Models: ${modelsStatus}`);+ }++ async startServer(modelName) {+ if (!this.serverManager.isAvailable()) {+ debugLogger.debug("whisper-server not available, will use CLI fallback");+ return { success: false, reason: "whisper-server binary not found" };+ }++ const modelPath = this.getModelPath(modelName);+ if (!fs.existsSync(modelPath)) {+ return { success: false, reason: `Model "${modelName}" not downloaded` };+ }++ try {+ await this.serverManager.start(modelPath);+ this.currentServerModel = modelName;+ debugLogger.info("whisper-server started", {+ model: modelName,+ port: this.serverManager.port,+ });+ return { success: true, port: this.serverManager.port };+ } catch (error) {+ debugLogger.error("Failed to start whisper-server", { error: error.message });+ return { success: false, reason: error.message };+ }+ }++ async stopServer() {+ await this.serverManager.stop();+ this.currentServerModel = null;+ }++ getServerStatus() {+ return this.serverManager.getStatus();}async checkWhisperInstallation() {@@ -237,26 +340,103 @@ class WhisperManager {options,audioBlobType: audioBlob?.constructor?.name,audioBlobSize: audioBlob?.byteLength || audioBlob?.size || 0,+ serverMode: this.useServerMode,+ serverAvailable: this.serverManager.isAvailable(),+ serverReady: this.serverManager.ready,});- const binaryPath = await this.getWhisperBinaryPath();- if (!binaryPath) {- throw new Error(- "whisper.cpp not found. Please install it via Homebrew (brew install whisper-cpp) or ensure it is bundled with the app."- );- }-- const tempAudioPath = await this.createTempAudioFile(audioBlob);const model = options.model || "base";const language = options.language || null;const modelPath = this.getModelPath(model);// Check if model existsif (!fs.existsSync(modelPath)) {- await this.cleanupTempFile(tempAudioPath);throw new Error(`Whisper model "${model}" not downloaded. Please download it from Settings.`);}+ // Try server mode first (faster for repeated transcriptions)+ if (this.useServerMode && this.serverManager.isAvailable()) {+ try {+ return await this.transcribeViaServer(audioBlob, model, language);+ } catch (serverError) {+ debugLogger.warn("Server transcription failed, falling back to CLI", {+ error: serverError.message,+ });+ // Fall through to CLI mode+ }+ }++ // Fallback to CLI mode (spawns process per transcription)+ return this.transcribeViaCLI(audioBlob, model, language, modelPath);+ }++ async transcribeViaServer(audioBlob, model, language) {+ debugLogger.info("Transcription mode: SERVER", { model, language: language || "auto" });+ const modelPath = this.getModelPath(model);++ // Start server if not running or if model changed+ if (!this.serverManager.ready || this.currentServerModel !== model) {+ debugLogger.debug("Starting/restarting whisper-server for model", { model });+ await this.serverManager.start(modelPath);+ this.currentServerModel = model;+ }++ // Convert audioBlob to Buffer if needed+ let audioBuffer;+ if (audioBlob instanceof ArrayBuffer) {+ audioBuffer = Buffer.from(audioBlob);+ } else if (audioBlob instanceof Uint8Array) {+ audioBuffer = Buffer.from(audioBlob);+ } else if (typeof audioBlob === "string") {+ audioBuffer = Buffer.from(audioBlob, "base64");+ } else if (audioBlob && audioBlob.buffer) {+ audioBuffer = Buffer.from(audioBlob.buffer);+ } else {+ throw new Error(`Unsupported audio data type: ${typeof audioBlob}`);+ }++ if (!audioBuffer || audioBuffer.length === 0) {+ throw new Error("Audio buffer is empty - no audio data received");+ }++ debugLogger.logWhisperPipeline("transcribeViaServer - sending to server", {+ bufferSize: audioBuffer.length,+ model,+ language,+ port: this.serverManager.port,+ });++ const startTime = Date.now();+ const result = await this.serverManager.transcribe(audioBuffer, { language });+ const elapsed = Date.now() - startTime;++ debugLogger.logWhisperPipeline("transcribeViaServer - completed", {+ elapsed,+ resultKeys: Object.keys(result),+ });++ return this.parseWhisperResult(result);+ }++ async transcribeViaCLI(audioBlob, _model, language, modelPath) {+ debugLogger.info("Transcription mode: CLI (fallback)", {+ model: path.basename(modelPath, ".bin").replace("ggml-", ""),+ language: language || "auto",+ reason: !this.useServerMode+ ? "server mode disabled"+ : !this.serverManager.isAvailable()+ ? "server binary not found"+ : "server failed",+ });+ const binaryPath = await this.getWhisperBinaryPath();+ if (!binaryPath) {+ throw new Error(+ "whisper.cpp not found. Please install it via Homebrew (brew install whisper-cpp) or ensure it is bundled with the app."+ );+ }++ const tempAudioPath = await this.createTempAudioFile(audioBlob);+try {const result = await this.runWhisperProcess(binaryPath, tempAudioPath, modelPath, language);return this.parseWhisperResult(result);@@ -524,43 +704,50 @@ class WhisperManager {}}- parseWhisperResult(stdout) {- debugLogger.logWhisperPipeline("Parsing result", { stdoutLength: stdout.length });-- try {- // whisper.cpp outputs JSON with transcription array- const result = JSON.parse(stdout);-- // Handle whisper.cpp JSON format- if (result.transcription && Array.isArray(result.transcription)) {- const text = result.transcription- .map((seg) => seg.text)- .join("")- .trim();- if (!text) {- return { success: false, message: "No audio detected" };+ parseWhisperResult(output) {+ // Handle both string (from CLI) and object (from server) inputs+ let result;+ if (typeof output === "string") {+ debugLogger.logWhisperPipeline("Parsing result (string)", { length: output.length });+ try {+ result = JSON.parse(output);+ } catch (parseError) {+ // Try parsing as plain text (non-JSON output)+ const text = output.trim();+ if (text) {+ return { success: true, text };}- return { success: true, text };+ throw new Error(`Failed to parse Whisper output: ${parseError.message}`);}+ } else if (typeof output === "object" && output !== null) {+ debugLogger.logWhisperPipeline("Parsing result (object)", { keys: Object.keys(output) });+ result = output;+ } else {+ throw new Error(`Unexpected Whisper output type: ${typeof output}`);+ }- // Fallback for simple text output- if (result.text) {- const text = result.text.trim();- if (!text) {- return { success: false, message: "No audio detected" };- }- return { success: true, text };+ // Handle whisper.cpp JSON format (CLI mode)+ if (result.transcription && Array.isArray(result.transcription)) {+ const text = result.transcription+ .map((seg) => seg.text)+ .join("")+ .trim();+ if (!text) {+ return { success: false, message: "No audio detected" };}+ return { success: true, text };+ }- return { success: false, message: "No audio detected" };- } catch (parseError) {- // Try parsing as plain text (non-JSON output)- const text = stdout.trim();- if (text) {- return { success: true, text };+ // Handle whisper-server format (has "text" field directly)+ if (result.text !== undefined) {+ const text = typeof result.text === "string" ? result.text.trim() : "";+ if (!text) {+ return { success: false, message: "No audio detected" };}- throw new Error(`Failed to parse Whisper output: ${parseError.message}`);+ return { success: true, text };}++ return { success: false, message: "No audio detected" };}async cleanupTempFile(tempAudioPath) {diff --git a/src/helpers/whisperServer.js b/src/helpers/whisperServer.jsnew file mode 100644index 000000000..0d46c43d6--- /dev/null+++ b/src/helpers/whisperServer.js@@ -0,0 +1,436 @@+const { spawn } = require("child_process");+const fs = require("fs");+const net = require("net");+const path = require("path");+const http = require("http");+const debugLogger = require("./debugLogger");+const { killProcess } = require("../utils/process");++const PORT_RANGE_START = 8178;+const PORT_RANGE_END = 8199;+const STARTUP_TIMEOUT_MS = 30000;+const HEALTH_CHECK_INTERVAL_MS = 5000;+const HEALTH_CHECK_TIMEOUT_MS = 2000;++class WhisperServerManager {+ constructor() {+ this.process = null;+ this.port = null;+ this.ready = false;+ this.modelPath = null;+ this.startupPromise = null;+ this.healthCheckInterval = null;+ this.cachedServerBinaryPath = null;+ this.cachedFFmpegPath = null;+ }++ getFFmpegPath() {+ if (this.cachedFFmpegPath) return this.cachedFFmpegPath;++ try {+ let ffmpegPath = require("ffmpeg-static");+ ffmpegPath = path.normalize(ffmpegPath);++ if (process.platform === "win32" && !ffmpegPath.endsWith(".exe")) {+ ffmpegPath += ".exe";+ }++ // Try unpacked ASAR path first (production builds)+ const unpackedPath = ffmpegPath.replace(/app\.asar([/\\])/, "app.asar.unpacked$1");+ if (fs.existsSync(unpackedPath)) {+ this.cachedFFmpegPath = unpackedPath;+ return unpackedPath;+ }++ if (fs.existsSync(ffmpegPath)) {+ this.cachedFFmpegPath = ffmpegPath;+ return ffmpegPath;+ }+ } catch {+ // Bundled FFmpeg not available+ }++ // Try system FFmpeg locations+ const systemCandidates =+ process.platform === "darwin"+ ? ["/opt/homebrew/bin/ffmpeg", "/usr/local/bin/ffmpeg"]+ : process.platform === "win32"+ ? ["C:\\ffmpeg\\bin\\ffmpeg.exe"]+ : ["/usr/bin/ffmpeg", "/usr/local/bin/ffmpeg"];++ for (const candidate of systemCandidates) {+ if (fs.existsSync(candidate)) {+ this.cachedFFmpegPath = candidate;+ return candidate;+ }+ }++ return null;+ }++ getServerBinaryPath() {+ if (this.cachedServerBinaryPath) return this.cachedServerBinaryPath;++ const platform = process.platform;+ const arch = process.arch;+ const platformArch = `${platform}-${arch}`;+ const binaryName =+ platform === "win32"+ ? `whisper-server-${platformArch}.exe`+ : `whisper-server-${platformArch}`;+ const genericName = platform === "win32" ? "whisper-server.exe" : "whisper-server";++ const candidates = [];++ if (process.resourcesPath) {+ candidates.push(+ path.join(process.resourcesPath, "bin", binaryName),+ path.join(process.resourcesPath, "bin", genericName)+ );+ }++ candidates.push(+ path.join(__dirname, "..", "..", "resources", "bin", binaryName),+ path.join(__dirname, "..", "..", "resources", "bin", genericName)+ );++ for (const candidate of candidates) {+ if (fs.existsSync(candidate)) {+ try {+ fs.statSync(candidate);+ this.cachedServerBinaryPath = candidate;+ return candidate;+ } catch {+ // Can't access binary+ }+ }+ }++ return null;+ }++ isAvailable() {+ return this.getServerBinaryPath() !== null;+ }++ async findAvailablePort() {+ for (let port = PORT_RANGE_START; port <= PORT_RANGE_END; port++) {+ if (await this.isPortAvailable(port)) return port;+ }+ throw new Error(`No available ports in range ${PORT_RANGE_START}-${PORT_RANGE_END}`);+ }++ isPortAvailable(port) {+ return new Promise((resolve) => {+ const server = net.createServer();+ server.once("error", () => resolve(false));+ server.once("listening", () => {+ server.close();+ resolve(true);+ });+ server.listen(port, "127.0.0.1");+ });+ }++ async start(modelPath, options = {}) {+ if (this.startupPromise) return this.startupPromise;++ if (this.ready && this.modelPath === modelPath) return;++ if (this.process) {+ await this.stop();+ }++ this.startupPromise = this._doStart(modelPath, options);+ try {+ await this.startupPromise;+ } finally {+ this.startupPromise = null;+ }+ }++ async _doStart(modelPath, options = {}) {+ const serverBinary = this.getServerBinaryPath();+ if (!serverBinary) throw new Error("whisper-server binary not found");+ if (!fs.existsSync(modelPath)) throw new Error(`Model file not found: ${modelPath}`);++ this.port = await this.findAvailablePort();+ this.modelPath = modelPath;++ const args = [+ "--model",+ modelPath,+ "--host",+ "127.0.0.1",+ "--port",+ String(this.port),+ "--convert",+ ];++ if (options.threads) args.push("--threads", String(options.threads));+ if (options.language && options.language !== "auto") {+ args.push("--language", options.language);+ }++ // Add FFmpeg to PATH for --convert flag+ const ffmpegPath = this.getFFmpegPath();+ const spawnEnv = { ...process.env };+ if (ffmpegPath) {+ const ffmpegDir = path.dirname(ffmpegPath);+ const pathSep = process.platform === "win32" ? ";" : ":";+ spawnEnv.PATH = ffmpegDir + pathSep + (process.env.PATH || "");+ }++ debugLogger.debug("Starting whisper-server", { port: this.port, modelPath });++ this.process = spawn(serverBinary, args, {+ stdio: ["ignore", "pipe", "pipe"],+ windowsHide: true,+ env: spawnEnv,+ });++ let stderrBuffer = "";+ let exitCode = null;++ this.process.stdout.on("data", (data) => {+ debugLogger.debug("whisper-server stdout", { data: data.toString().trim() });+ });++ this.process.stderr.on("data", (data) => {+ stderrBuffer += data.toString();+ debugLogger.debug("whisper-server stderr", { data: data.toString().trim() });+ });++ this.process.on("error", (error) => {+ debugLogger.error("whisper-server process error", { error: error.message });+ this.ready = false;+ });++ this.process.on("close", (code) => {+ exitCode = code;+ debugLogger.debug("whisper-server process exited", { code });+ this.ready = false;+ this.process = null;+ this.stopHealthCheck();+ });++ await this.waitForReady(() => ({ stderr: stderrBuffer, exitCode }));+ this.startHealthCheck();++ debugLogger.info("whisper-server started successfully", {+ port: this.port,+ model: path.basename(modelPath),+ });+ }++ async waitForReady(getProcessInfo) {+ const startTime = Date.now();++ while (Date.now() - startTime < STARTUP_TIMEOUT_MS) {+ if (!this.process || this.process.killed) {+ const info = getProcessInfo ? getProcessInfo() : {};+ const stderr = info.stderr ? info.stderr.trim().slice(0, 200) : "";+ const details = stderr || (info.exitCode !== null ? `exit code: ${info.exitCode}` : "");+ throw new Error(+ `whisper-server process died during startup${details ? `: ${details}` : ""}`+ );+ }++ if (await this.checkHealth()) {+ this.ready = true;+ return;+ }++ await new Promise((resolve) => setTimeout(resolve, 500));+ }++ throw new Error(`whisper-server failed to start within ${STARTUP_TIMEOUT_MS}ms`);+ }++ checkHealth() {+ return new Promise((resolve) => {+ const req = http.request(+ {+ hostname: "127.0.0.1",+ port: this.port,+ path: "/",+ method: "GET",+ timeout: HEALTH_CHECK_TIMEOUT_MS,+ },+ (res) => {+ resolve(true);+ res.resume();+ }+ );++ req.on("error", () => resolve(false));+ req.on("timeout", () => {+ req.destroy();+ resolve(false);+ });+ req.end();+ });+ }++ startHealthCheck() {+ this.stopHealthCheck();+ this.healthCheckInterval = setInterval(async () => {+ if (!this.process) {+ this.stopHealthCheck();+ return;+ }+ if (!(await this.checkHealth())) {+ debugLogger.warn("whisper-server health check failed");+ this.ready = false;+ }+ }, HEALTH_CHECK_INTERVAL_MS);+ }++ stopHealthCheck() {+ if (this.healthCheckInterval) {+ clearInterval(this.healthCheckInterval);+ this.healthCheckInterval = null;+ }+ }++ async transcribe(audioBuffer, options = {}) {+ if (!this.ready || !this.process) {+ throw new Error("whisper-server is not running");+ }++ const { language } = options;+ const boundary = `----WhisperBoundary${Date.now()}`;+ const parts = [];++ parts.push(+ `--${boundary}\r\n` ++ `Content-Disposition: form-data; name="file"; filename="audio.webm"\r\n` ++ `Content-Type: audio/webm\r\n\r\n`+ );+ parts.push(audioBuffer);+ parts.push("\r\n");++ if (language && language !== "auto") {+ parts.push(+ `--${boundary}\r\n` ++ `Content-Disposition: form-data; name="language"\r\n\r\n` ++ `${language}\r\n`+ );+ }++ parts.push(+ `--${boundary}\r\n` ++ `Content-Disposition: form-data; name="response_format"\r\n\r\n` ++ `json\r\n`+ );+ parts.push(`--${boundary}--\r\n`);++ const bodyParts = parts.map((part) => (typeof part === "string" ? Buffer.from(part) : part));+ const body = Buffer.concat(bodyParts);++ return new Promise((resolve, reject) => {+ const startTime = Date.now();++ const req = http.request(+ {+ hostname: "127.0.0.1",+ port: this.port,+ path: "/inference",+ method: "POST",+ headers: {+ "Content-Type": `multipart/form-data; boundary=${boundary}`,+ "Content-Length": body.length,+ },+ timeout: 300000,+ },+ (res) => {+ let data = "";+ res.on("data", (chunk) => {+ data += chunk;+ });+ res.on("end", () => {+ debugLogger.debug("whisper-server transcription completed", {+ statusCode: res.statusCode,+ elapsed: Date.now() - startTime,+ });++ if (res.statusCode !== 200) {+ reject(new Error(`whisper-server returned status ${res.statusCode}: ${data}`));+ return;+ }++ try {+ resolve(JSON.parse(data));+ } catch (e) {+ reject(new Error(`Failed to parse whisper-server response: ${e.message}`));+ }+ });+ }+ );++ req.on("error", (error) => {+ reject(new Error(`whisper-server request failed: ${error.message}`));+ });+ req.on("timeout", () => {+ req.destroy();+ reject(new Error("whisper-server request timed out"));+ });++ req.write(body);+ req.end();+ });+ }++ async stop() {+ this.stopHealthCheck();++ if (!this.process) {+ this.ready = false;+ return;+ }++ debugLogger.debug("Stopping whisper-server");++ try {+ killProcess(this.process, "SIGTERM");++ await new Promise((resolve) => {+ const timeout = setTimeout(() => {+ if (this.process) {+ killProcess(this.process, "SIGKILL");+ }+ resolve();+ }, 5000);++ if (this.process) {+ this.process.once("close", () => {+ clearTimeout(timeout);+ resolve();+ });+ } else {+ clearTimeout(timeout);+ resolve();+ }+ });+ } catch (error) {+ debugLogger.error("Error stopping whisper-server", { error: error.message });+ }++ this.process = null;+ this.ready = false;+ this.port = null;+ this.modelPath = null;+ }++ getStatus() {+ return {+ available: this.isAvailable(),+ running: this.ready && this.process !== null,+ port: this.port,+ modelPath: this.modelPath,+ modelName: this.modelPath ? path.basename(this.modelPath, ".bin").replace("ggml-", "") : null,+ };+ }+}++module.exports = WhisperServerManager;