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
fix(whisper): pass an ASCII-safe VAD model path on Windows (#1208) (#2623)
void82b529122183
diff --git a/src/helpers/modelDirUtils.js b/src/helpers/modelDirUtils.jsindex 98724f94b..715ce5c8a 100644--- a/src/helpers/modelDirUtils.js+++ b/src/helpers/modelDirUtils.js@@ -52,7 +52,7 @@ function getPreferredCacheRoot(homeCache) {return homeCache;}-// Only these subdirs resolve through getCacheRoot(). qdrant-data,+// Only these subdirs move from the home cache to getCacheRoot(). qdrant-data,// embedding-models, and yt-dlp are read from the home cache directly by their// managers (and tolerate non-ASCII paths), so they must stay put.const RELOCATED_SUBDIRS = ["whisper-models", "parakeet-models", "diarization-models", "models"];diff --git a/src/helpers/whisper.js b/src/helpers/whisper.jsindex 021a5b711..48c058778 100644--- a/src/helpers/whisper.js+++ b/src/helpers/whisper.js@@ -13,7 +13,11 @@ const {} = require("./downloadUtils");const WhisperServerManager = require("./whisperServer");const { createAbortError } = require("./abortError");-const { getModelsDirForService } = require("./modelDirUtils");+const {+ getCacheRoot,+ getModelsDirForService,+ pathHasProblematicChars,+} = require("./modelDirUtils");const modelRegistryData = require("../models/modelRegistryData.json");@@ -33,6 +37,39 @@ function getValidModelNames() {return Object.keys(modelRegistryData.whisperModels);}+// whisper-server reads its arguments in the Windows ANSI code page and crashes+// on a non-ASCII --vad-model path, as on the model paths #1514 moved (#1208).+// An install under such a folder bundles the VAD model there, so it is copied+// into the ASCII-safe cache root. The copy follows pathHasProblematicChars, the+// rule #1514 uses for the cache root, so a path with spaces is copied too. That+// root can be the shared ProgramData folder, so an existing copy is reused only+// when its bytes match, and a new one is renamed into place: a write cut short+// there would leave a file other accounts can neither use nor replace.+function asciiSafeVadModelPath(bundledPath) {+ if (process.platform !== "win32" || !pathHasProblematicChars(bundledPath)) return bundledPath;++ const copyPath = path.join(getCacheRoot(), "whisper-vad", path.basename(bundledPath));+ const tempPath = `${copyPath}.${process.pid}.tmp`;+ try {+ const model = fs.readFileSync(bundledPath);+ if (!fs.existsSync(copyPath) || !fs.readFileSync(copyPath).equals(model)) {+ fs.mkdirSync(path.dirname(copyPath), { recursive: true });+ fs.writeFileSync(tempPath, model);+ fs.renameSync(tempPath, copyPath);+ }+ return copyPath;+ } catch (error) {+ try {+ fs.rmSync(tempPath, { force: true });+ } catch {}+ debugLogger.warn("Could not copy the VAD model to an ASCII-safe path", {+ error: error.message,+ });+ // Spaces alone don't crash whisper-server, so such a path keeps VAD working.+ return /^[\x20-\x7E]*$/.test(bundledPath) ? bundledPath : null;+ }+}+// WHISPER_GPU_FAILED holds a comma-separated list of backends that fell back// to CPU on this machine (e.g. "cuda" or "cuda,vulkan")function resolveFailedGpuBackends(value) {@@ -211,8 +248,10 @@ class WhisperManager {candidates.push(path.join(__dirname, "..", "..", "resources", "bin", "whisper-vad", fileName));const resolved = candidates.find((p) => fs.existsSync(p)) || null;- this.cachedVadModelPath = resolved;- return resolved;+ const vadModelPath = resolved && asciiSafeVadModelPath(resolved);+ // A failed copy is retried on the next request rather than kept for the session.+ if (vadModelPath || !resolved) this.cachedVadModelPath = vadModelPath;+ return vadModelPath;}async initializeAtStartup(settings = {}) {@@ -487,7 +526,7 @@ class WhisperManager {const vadEnabled = options.vadEnabled === true;const vadModelPath = vadEnabled ? this.getVadModelPath() : null;if (vadEnabled && !vadModelPath) {- debugLogger.warn("VAD requested but ggml-silero model not found; running without VAD");+ debugLogger.warn("VAD requested but ggml-silero model unavailable; running without VAD");}this._warnIfGpuFallback();diff --git a/test/helpers/whisperVadModelPath.test.js b/test/helpers/whisperVadModelPath.test.jsnew file mode 100644index 000000000..b3d7cbf8c--- /dev/null+++ b/test/helpers/whisperVadModelPath.test.js@@ -0,0 +1,152 @@+const assert = require("node:assert/strict");+const fs = require("fs");+const os = require("os");+const path = require("path");+const Module = require("node:module");+const { describe, it, beforeEach, afterEach, mock } = require("node:test");++// On Windows, whisper-server crashes on a non-ASCII --vad-model path (#1208), so+// a VAD model bundled under an install folder that fails pathHasProblematicChars+// is handed over as a copy in the ASCII-safe cache root.+describe("WhisperManager.getVadModelPath (#1208)", () => {+ const VAD_FILE = "ggml-silero-v5.1.2.bin";+ const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform");+ const originalResourcesPath = process.resourcesPath;+ const originalEnv = { ...process.env };+ const originalLoad = Module._load;+ const modelDirUtilsPath = require.resolve("../../src/helpers/modelDirUtils");+ const whisperPath = require.resolve("../../src/helpers/whisper");+ let tempRoot;+ let cacheRoot;++ // A fresh modelDirUtils with a temp home, so getCacheRoot's one-time migration+ // never moves the real ~/.cache/openwhispr.+ function managerOn(platform) {+ Module._load = function loadWithElectronStub(request, parent, isMain) {+ if (request === "electron" && parent?.filename === modelDirUtilsPath) {+ return { app: { getPath: () => path.join(tempRoot, "home") } };+ }+ return originalLoad.call(this, request, parent, isMain);+ };+ delete require.cache[modelDirUtilsPath];+ delete require.cache[whisperPath];+ const WhisperManager = require(whisperPath);+ const manager = new WhisperManager();+ Object.defineProperty(process, "platform", { value: platform });+ return manager;+ }++ function vadModelPathOn(platform) {+ return managerOn(platform).getVadModelPath();+ }++ function installBundledVad(installDir) {+ const dir = path.join(installDir, "resources", "bin", "whisper-vad");+ fs.mkdirSync(dir, { recursive: true });+ fs.writeFileSync(path.join(dir, VAD_FILE), "silero-weights");+ process.resourcesPath = path.join(installDir, "resources");+ return path.join(dir, VAD_FILE);+ }++ beforeEach(() => {+ tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "ow-vad-test-"));+ cacheRoot = path.join(tempRoot, "cache");+ process.env.OPENWHISPR_CACHE_ROOT = cacheRoot;+ });++ afterEach(() => {+ mock.restoreAll();+ Module._load = originalLoad;+ Object.defineProperty(process, "platform", originalPlatform);+ if (originalResourcesPath === undefined) delete process.resourcesPath;+ else process.resourcesPath = originalResourcesPath;+ process.env = { ...originalEnv };+ delete require.cache[modelDirUtilsPath];+ delete require.cache[whisperPath];+ fs.rmSync(tempRoot, { recursive: true, force: true });+ });++ it("copies the model into the cache root when the install path is not ASCII", () => {+ installBundledVad(path.join(tempRoot, "Users", "Rémi", "Programs", "OpenWhispr"));++ const vadPath = vadModelPathOn("win32");++ assert.equal(vadPath, path.join(cacheRoot, "whisper-vad", VAD_FILE));+ assert.equal(fs.readFileSync(vadPath, "utf8"), "silero-weights");+ });++ it("runs without VAD rather than pass the crashing path when the copy fails", () => {+ installBundledVad(path.join(tempRoot, "Users", "Rémi", "Programs", "OpenWhispr"));+ fs.mkdirSync(cacheRoot, { recursive: true });+ fs.writeFileSync(path.join(cacheRoot, "whisper-vad"), "not a directory");++ assert.equal(vadModelPathOn("win32"), null);+ });++ it("keeps the bundled model when a path whose only problem is a space can't be copied", () => {+ const bundled = installBundledVad(path.join(tempRoot, "Program Files", "OpenWhispr"));+ fs.mkdirSync(cacheRoot, { recursive: true });+ fs.writeFileSync(path.join(cacheRoot, "whisper-vad"), "not a directory");++ assert.equal(vadModelPathOn("win32"), bundled);+ });++ it("retries a failed copy on the next request", () => {+ installBundledVad(path.join(tempRoot, "Users", "Rémi", "Programs", "OpenWhispr"));+ const blocker = path.join(cacheRoot, "whisper-vad");+ fs.mkdirSync(cacheRoot, { recursive: true });+ fs.writeFileSync(blocker, "not a directory");+ const manager = managerOn("win32");++ assert.equal(manager.getVadModelPath(), null);+ fs.rmSync(blocker);+ assert.equal(manager.getVadModelPath(), path.join(blocker, VAD_FILE));+ });++ it("never leaves a partial copy under the final name", () => {+ installBundledVad(path.join(tempRoot, "Users", "Rémi", "Programs", "OpenWhispr"));+ mock.method(fs, "renameSync", () => {+ throw new Error("EBUSY");+ });++ assert.equal(vadModelPathOn("win32"), null);+ assert.deepEqual(fs.readdirSync(path.join(cacheRoot, "whisper-vad")), []);+ });++ it("replaces a copy whose bytes differ from the bundled model", () => {+ installBundledVad(path.join(tempRoot, "Program Files", "OpenWhispr"));+ const copyPath = path.join(cacheRoot, "whisper-vad", VAD_FILE);+ fs.mkdirSync(path.dirname(copyPath), { recursive: true });+ fs.writeFileSync(copyPath, "planted-weight");++ assert.equal(vadModelPathOn("win32"), copyPath);+ assert.equal(fs.readFileSync(copyPath, "utf8"), "silero-weights");+ });++ it("reuses an identical copy without writing it again", () => {+ installBundledVad(path.join(tempRoot, "Users", "Rémi", "Programs", "OpenWhispr"));+ const copyPath = path.join(cacheRoot, "whisper-vad", VAD_FILE);+ fs.mkdirSync(path.dirname(copyPath), { recursive: true });+ fs.writeFileSync(copyPath, "silero-weights");+ // Read-only, like a copy another account owns: any rewrite would fail.+ fs.chmodSync(copyPath, 0o444);+ try {+ assert.equal(vadModelPathOn("win32"), copyPath);+ } finally {+ fs.chmodSync(copyPath, 0o644);+ }+ });++ it("keeps the bundled model for an ASCII install path", () => {+ const bundled = installBundledVad(path.join(tempRoot, "Programs", "OpenWhispr"));++ assert.equal(vadModelPathOn("win32"), bundled);+ assert.equal(fs.existsSync(path.join(cacheRoot, "whisper-vad")), false);+ });++ it("keeps the bundled model outside Windows", () => {+ const bundled = installBundledVad(path.join(tempRoot, "Users", "Rémi", "OpenWhispr"));++ assert.equal(vadModelPathOn("darwin"), bundled);+ });+});