Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

Refactor: extract shared extraction/file-finding utils, fix encapsulation and ordering

Gabriel Stein21e5aaf276d4
diff --git a/src/helpers/downloadUtils.js b/src/helpers/downloadUtils.js
index a5402647c..61957ff96 100644
--- a/src/helpers/downloadUtils.js
+++ b/src/helpers/downloadUtils.js
@@ -3,6 +3,7 @@ const { promises: fsPromises } = require("fs");
const path = require("path");
const https = require("https");
const http = require("http");
+const { execFile } = require("child_process");
const { pipeline } = require("stream");
const debugLogger = require("./debugLogger");
@@ -402,10 +403,87 @@ async function checkDiskSpace(directory, requiredBytes) {
}
}
+function extractZipWindows(zipPath, destDir) {
+ return new Promise((resolve, reject) => {
+ execFile("tar", ["-xf", zipPath, "-C", destDir], (error) => {
+ if (error) {
+ debugLogger.info("tar extraction failed, trying PowerShell", { error: error.message });
+ execFile(
+ "powershell",
+ [
+ "-NoProfile",
+ "-Command",
+ `Expand-Archive -Force -Path '${zipPath}' -DestinationPath '${destDir}'`,
+ ],
+ (psError) => {
+ if (psError) reject(new Error(`Zip extraction failed: ${psError.message}`));
+ else resolve();
+ }
+ );
+ } else {
+ resolve();
+ }
+ });
+ });
+}
+
+function extractArchive(archivePath, destDir) {
+ if (archivePath.endsWith(".tar.gz") || archivePath.endsWith(".tgz")) {
+ return new Promise((resolve, reject) => {
+ execFile("tar", ["-xzf", archivePath, "-C", destDir], (err) => {
+ err ? reject(new Error(`Extraction failed: ${err.message}`)) : resolve();
+ });
+ });
+ }
+
+ if (process.platform === "win32") {
+ return extractZipWindows(archivePath, destDir);
+ }
+
+ return new Promise((resolve, reject) => {
+ execFile("unzip", ["-o", archivePath, "-d", destDir], (err) => {
+ err ? reject(new Error(`Extraction failed: ${err.message}`)) : resolve();
+ });
+ });
+}
+
+async function findFile(dir, name, maxDepth = 5, depth = 0) {
+ if (depth >= maxDepth) return null;
+ const entries = await fsPromises.readdir(dir, { withFileTypes: true });
+ for (const entry of entries) {
+ const full = path.join(dir, entry.name);
+ if (entry.isDirectory()) {
+ const found = await findFile(full, name, maxDepth, depth + 1);
+ if (found) return found;
+ } else if (entry.name === name) {
+ return full;
+ }
+ }
+ return null;
+}
+
+async function findFiles(dir, pattern, maxDepth = 5, depth = 0) {
+ if (depth >= maxDepth) return [];
+ const results = [];
+ const entries = await fsPromises.readdir(dir, { withFileTypes: true });
+ for (const entry of entries) {
+ const full = path.join(dir, entry.name);
+ if (entry.isDirectory()) {
+ results.push(...(await findFiles(full, pattern, maxDepth, depth + 1)));
+ } else if (pattern.test(entry.name)) {
+ results.push(full);
+ }
+ }
+ return results;
+}
+
module.exports = {
downloadFile,
createDownloadSignal,
validateFileSize,
cleanupStaleDownloads,
checkDiskSpace,
+ extractArchive,
+ findFile,
+ findFiles,
};
diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.js
index 62b06fa0a..5f4a2b8fb 100644
--- a/src/helpers/ipcHandlers.js
+++ b/src/helpers/ipcHandlers.js
@@ -968,7 +968,7 @@ class IPCHandlers {
}
return {
downloaded: this.whisperCudaManager.isDownloaded(),
- downloading: this.whisperCudaManager._downloading,
+ downloading: this.whisperCudaManager.isDownloading(),
path: this.whisperCudaManager.getCudaBinaryPath(),
gpuInfo,
};
@@ -1784,9 +1784,9 @@ class IPCHandlers {
delete process.env.LLAMA_GPU_BACKEND;
const modelManager = require("./modelManagerBridge").default;
modelManager.serverManager.cachedServerBinaryPaths = null;
+ await this.environmentManager.saveAllKeysToEnvFile().catch(() => {});
// Restart llama server so it picks up the Vulkan binary
await modelManager.stopServer().catch(() => {});
- this.environmentManager.saveAllKeysToEnvFile().catch(() => {});
}
return result;
diff --git a/src/helpers/llamaVulkanManager.js b/src/helpers/llamaVulkanManager.js
index 20b81ef3f..5ab11b0dd 100644
--- a/src/helpers/llamaVulkanManager.js
+++ b/src/helpers/llamaVulkanManager.js
@@ -1,7 +1,6 @@
const fs = require("fs");
const { promises: fsPromises } = require("fs");
const path = require("path");
-const { execFile } = require("child_process");
const { app } = require("electron");
const debugLogger = require("./debugLogger");
const {
@@ -9,6 +8,9 @@ const {
createDownloadSignal,
checkDiskSpace,
cleanupStaleDownloads,
+ extractArchive,
+ findFile,
+ findFiles,
} = require("./downloadUtils");
const GITHUB_RELEASE_URL = "https://api.github.com/repos/ggerganov/llama.cpp/releases/latest";
@@ -110,16 +112,16 @@ class LlamaVulkanManager {
await fsPromises.mkdir(extractDir, { recursive: true });
try {
- await this._extract(archivePath, extractDir);
+ await extractArchive(archivePath, extractDir);
- const binaryPath = this._findFile(extractDir, config.binaryName);
+ const binaryPath = await findFile(extractDir, config.binaryName);
if (!binaryPath) throw new Error(`${config.binaryName} not found in archive`);
const outputPath = path.join(this.binDir, config.outputName);
await fsPromises.copyFile(binaryPath, outputPath);
if (process.platform !== "win32") await fsPromises.chmod(outputPath, 0o755);
- const libs = this._findFiles(extractDir, config.libPattern);
+ const libs = await findFiles(extractDir, config.libPattern);
for (const lib of libs) {
const dest = path.join(this.binDir, path.basename(lib));
await fsPromises.copyFile(lib, dest);
@@ -205,70 +207,6 @@ class LlamaVulkanManager {
.on("error", reject);
});
}
-
- _extract(archivePath, destDir) {
- return new Promise((resolve, reject) => {
- if (archivePath.endsWith(".tar.gz") || archivePath.endsWith(".tgz")) {
- execFile("tar", ["-xzf", archivePath, "-C", destDir], (err) => {
- err ? reject(new Error(`Extraction failed: ${err.message}`)) : resolve();
- });
- } else if (process.platform === "win32") {
- // Use Windows built-in tar.exe (available since Windows 10 1803)
- execFile("tar", ["-xf", archivePath, "-C", destDir], (err) => {
- if (err) {
- debugLogger.info("Vulkan: tar extraction failed, trying PowerShell", { error: err.message });
- execFile(
- "powershell",
- [
- "-NoProfile",
- "-Command",
- `Expand-Archive -Force -Path '${archivePath}' -DestinationPath '${destDir}'`,
- ],
- (psErr) => {
- psErr ? reject(new Error(`Extraction failed: ${psErr.message}`)) : resolve();
- }
- );
- } else {
- resolve();
- }
- });
- } else {
- execFile("unzip", ["-o", archivePath, "-d", destDir], (err) => {
- err ? reject(new Error(`Extraction failed: ${err.message}`)) : resolve();
- });
- }
- });
- }
-
- _findFile(dir, name, maxDepth = 5, depth = 0) {
- if (depth >= maxDepth) return null;
- const entries = fs.readdirSync(dir, { withFileTypes: true });
- for (const entry of entries) {
- const full = path.join(dir, entry.name);
- if (entry.isDirectory()) {
- const found = this._findFile(full, name, maxDepth, depth + 1);
- if (found) return found;
- } else if (entry.name === name) {
- return full;
- }
- }
- return null;
- }
-
- _findFiles(dir, pattern, maxDepth = 5, depth = 0) {
- if (depth >= maxDepth) return [];
- const results = [];
- const entries = fs.readdirSync(dir, { withFileTypes: true });
- for (const entry of entries) {
- const full = path.join(dir, entry.name);
- if (entry.isDirectory()) {
- results.push(...this._findFiles(full, pattern, maxDepth, depth + 1));
- } else if (pattern.test(entry.name)) {
- results.push(full);
- }
- }
- return results;
- }
}
module.exports = LlamaVulkanManager;
diff --git a/src/helpers/whisperCudaManager.js b/src/helpers/whisperCudaManager.js
index 1ad501b1c..6de9ab8f4 100644
--- a/src/helpers/whisperCudaManager.js
+++ b/src/helpers/whisperCudaManager.js
@@ -2,7 +2,6 @@ const fs = require("fs");
const { promises: fsPromises } = require("fs");
const path = require("path");
const https = require("https");
-const { execFile } = require("child_process");
const { app } = require("electron");
const debugLogger = require("./debugLogger");
const {
@@ -10,6 +9,9 @@ const {
createDownloadSignal,
checkDiskSpace,
cleanupStaleDownloads,
+ extractArchive,
+ findFile,
+ findFiles,
} = require("./downloadUtils");
const { getSafeTempDir } = require("./safeTempDir");
@@ -61,6 +63,10 @@ class WhisperCudaManager {
return !!this.getCudaBinaryPath();
}
+ isDownloading() {
+ return this._downloading;
+ }
+
async fetchReleaseInfo() {
if (!isSupportedPlatform()) {
throw new Error(`CUDA binaries not available for ${process.platform}`);
@@ -135,12 +141,12 @@ class WhisperCudaManager {
});
await fsPromises.mkdir(extractDir, { recursive: true });
- await this._extractZip(zipPath, extractDir);
+ await extractArchive(zipPath, extractDir);
const binaryName = PLATFORM_BINARY_NAMES[process.platform];
const companionPattern = COMPANION_PATTERNS[process.platform];
- const binaryPath = this._findFile(extractDir, binaryName);
+ const binaryPath = await findFile(extractDir, binaryName);
if (!binaryPath) {
throw new Error(`Extraction completed but binary "${binaryName}" not found in archive`);
}
@@ -151,7 +157,7 @@ class WhisperCudaManager {
await fsPromises.chmod(dest, 0o755);
}
- const libs = this._findFiles(extractDir, companionPattern);
+ const libs = await findFiles(extractDir, companionPattern);
for (const lib of libs) {
const libDest = path.join(binDir, path.basename(lib));
await fsPromises.copyFile(lib, libDest);
@@ -232,36 +238,6 @@ class WhisperCudaManager {
};
}
- _findFile(dir, name, maxDepth = 5, depth = 0) {
- if (depth >= maxDepth) return null;
- const entries = fs.readdirSync(dir, { withFileTypes: true });
- for (const entry of entries) {
- const full = path.join(dir, entry.name);
- if (entry.isDirectory()) {
- const found = this._findFile(full, name, maxDepth, depth + 1);
- if (found) return found;
- } else if (entry.name === name) {
- return full;
- }
- }
- return null;
- }
-
- _findFiles(dir, pattern, maxDepth = 5, depth = 0) {
- if (depth >= maxDepth) return [];
- const results = [];
- const entries = fs.readdirSync(dir, { withFileTypes: true });
- for (const entry of entries) {
- const full = path.join(dir, entry.name);
- if (entry.isDirectory()) {
- results.push(...this._findFiles(full, pattern, maxDepth, depth + 1));
- } else if (pattern.test(entry.name)) {
- results.push(full);
- }
- }
- return results;
- }
-
_fetchJson(url) {
return new Promise((resolve, reject) => {
https
@@ -311,41 +287,6 @@ class WhisperCudaManager {
});
});
}
-
- _extractZip(zipPath, destDir) {
- if (process.platform === "win32") {
- return new Promise((resolve, reject) => {
- // Use Windows built-in tar.exe (available since Windows 10 1803)
- execFile("tar", ["-xf", zipPath, "-C", destDir], (error) => {
- if (error) {
- debugLogger.info("CUDA: tar extraction failed, trying PowerShell", { error: error.message });
- // Fallback to PowerShell Expand-Archive
- execFile(
- "powershell",
- [
- "-NoProfile",
- "-Command",
- `Expand-Archive -Force -Path '${zipPath}' -DestinationPath '${destDir}'`,
- ],
- (psError) => {
- if (psError) reject(new Error(`Zip extraction failed: ${psError.message}`));
- else resolve();
- }
- );
- } else {
- resolve();
- }
- });
- });
- }
-
- return new Promise((resolve, reject) => {
- execFile("unzip", ["-o", zipPath, "-d", destDir], (error) => {
- if (error) reject(new Error(`Zip extraction failed: ${error.message}`));
- else resolve();
- });
- });
- }
}
module.exports = WhisperCudaManager;