Skip to content

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

Unify prompt system into single intelligent prompt (#136)

Gabriel Stein286fab53a801
diff --git a/.env.example b/.env.example
index 82c893a68..6424c04e7 100644
--- a/.env.example
+++ b/.env.example
@@ -15,7 +15,7 @@ LANGUAGE=
ANTHROPIC_API_KEY=your_anthropic_api_key_here
# Google Gemini API Configuration (optional - for Gemini AI processing)
-# Get your API key from: https://makersuite.google.com/app/apikey
+# Get your API key from: https://aistudio.google.com/app/api-keys
GEMINI_API_KEY=your_gemini_api_key_here
# Optional: Debug mode
diff --git a/electron-builder.json b/electron-builder.json
index 41941bc0c..6768ac752 100644
--- a/electron-builder.json
+++ b/electron-builder.json
@@ -87,7 +87,7 @@
{
"from": "resources/bin/",
"to": "bin/",
- "filter": ["whisper-cpp-*", "whisper-server-*"]
+ "filter": ["whisper-cpp-*", "whisper-server-*", "llama-server-*"]
}
],
"mac": {
diff --git a/main.js b/main.js
index b03009d00..8dc6e8d48 100644
--- a/main.js
+++ b/main.js
@@ -116,6 +116,7 @@ const _ipcHandlers = new IPCHandlers({
clipboardManager,
whisperManager,
windowManager,
+ updateManager,
});
// Main application startup
@@ -145,6 +146,17 @@ async function startApp() {
debugLogger.debug("Whisper startup init error (non-fatal)", { error: err.message });
});
+ // Pre-warm llama-server if local reasoning is configured
+ // Settings can be provided via environment variables:
+ // - REASONING_PROVIDER=local to enable local reasoning
+ // - LOCAL_REASONING_MODEL=qwen3-8b-q4_k_m (or another model ID)
+ if (process.env.REASONING_PROVIDER === "local" && process.env.LOCAL_REASONING_MODEL) {
+ const modelManager = require("./src/helpers/modelManagerBridge").default;
+ modelManager.prewarmServer(process.env.LOCAL_REASONING_MODEL).catch((err) => {
+ debugLogger.debug("llama-server pre-warm error (non-fatal)", { error: err.message });
+ });
+ }
+
// Log nircmd status on Windows (for debugging bundled dependencies)
if (process.platform === "win32") {
const nircmdStatus = clipboardManager.getNircmdStatus();
@@ -322,5 +334,8 @@ if (gotSingleInstanceLock) {
updateManager.cleanup();
// Stop whisper server if running
whisperManager.stopServer().catch(() => {});
+ // Stop llama-server if running
+ const modelManager = require("./src/helpers/modelManagerBridge").default;
+ modelManager.stopServer().catch(() => {});
});
}
diff --git a/package.json b/package.json
index 350faf9bd..bf5cb7a22 100644
--- a/package.json
+++ b/package.json
@@ -13,23 +13,25 @@
"predev:main": "npm run compile:globe",
"dev:main": "cross-env NODE_ENV=development electron . --dev",
"dev:renderer": "cd src && vite",
- "prebuild": "npm run compile:globe && npm run download:whisper-cpp",
+ "prebuild": "npm run compile:globe && npm run download:whisper-cpp && npm run download:llama-server",
"build": "cd src && vite build && cd .. && electron-builder",
"postinstall": "electron-builder install-app-deps",
"build:renderer": "cd src && vite build",
- "prebuild:mac": "npm run compile:globe && npm run download:whisper-cpp",
+ "prebuild:mac": "npm run compile:globe && npm run download:whisper-cpp && npm run download:llama-server",
"build:mac": "npm run build:renderer && electron-builder --mac",
- "prebuild:win": "npm run compile:globe && npm run download:whisper-cpp",
+ "prebuild:win": "npm run compile:globe && npm run download:whisper-cpp && npm run download:llama-server",
"build:win": "npm run build:renderer && electron-builder --win",
- "prebuild:linux": "npm run compile:globe && npm run download:whisper-cpp",
+ "prebuild:linux": "npm run compile:globe && npm run download:whisper-cpp && npm run download:llama-server",
"build:linux": "npm run build:renderer && electron-builder --linux",
"setup": "node setup.js",
- "prepack": "npm run compile:globe && npm run download:whisper-cpp",
+ "prepack": "npm run compile:globe && npm run download:whisper-cpp && npm run download:llama-server",
"pack": "npm run build:renderer && cross-env CSC_IDENTITY_AUTO_DISCOVERY=false electron-builder --dir",
- "predist": "npm run compile:globe && npm run download:whisper-cpp",
+ "predist": "npm run compile:globe && npm run download:whisper-cpp && npm run download:llama-server",
"dist": "npm run build:renderer && electron-builder",
"download:whisper-cpp": "node scripts/download-whisper-cpp.js --current",
"download:whisper-cpp:all": "node scripts/download-whisper-cpp.js --all",
+ "download:llama-server": "node scripts/download-llama-server.js --current",
+ "download:llama-server:all": "node scripts/download-llama-server.js --all",
"format:js": "eslint . --fix && cd src && eslint . --fix && prettier --write \"**/*.{js,jsx,ts,tsx,json,css,md}\"",
"format": "npm run format:js",
"format:check": "eslint . && cd src && eslint . && prettier --check \"**/*.{js,jsx,ts,tsx,json,css,md}\"",
diff --git a/preload.js b/preload.js
index dc5eb087f..bdf08fa0a 100644
--- a/preload.js
+++ b/preload.js
@@ -160,6 +160,8 @@ contextBridge.exposeInMainWorld("electronAPI", {
getGroqKey: () => ipcRenderer.invoke("get-groq-key"),
saveGroqKey: (key) => ipcRenderer.invoke("save-groq-key", key),
+ saveAllKeysToEnv: () => ipcRenderer.invoke("save-all-keys-to-env"),
+
// Local reasoning
processLocalReasoning: (text, modelId, agentName, config) =>
ipcRenderer.invoke("process-local-reasoning", text, modelId, agentName, config),
@@ -174,7 +176,12 @@ contextBridge.exposeInMainWorld("electronAPI", {
llamaCppCheck: () => ipcRenderer.invoke("llama-cpp-check"),
llamaCppInstall: () => ipcRenderer.invoke("llama-cpp-install"),
llamaCppUninstall: () => ipcRenderer.invoke("llama-cpp-uninstall"),
-
+
+ // llama-server
+ llamaServerStart: (modelId) => ipcRenderer.invoke("llama-server-start", modelId),
+ llamaServerStop: () => ipcRenderer.invoke("llama-server-stop"),
+ llamaServerStatus: () => ipcRenderer.invoke("llama-server-status"),
+
getLogLevel: () => ipcRenderer.invoke("get-log-level"),
log: (entry) => ipcRenderer.invoke("app-log", entry),
@@ -207,9 +214,4 @@ contextBridge.exposeInMainWorld("electronAPI", {
ipcRenderer.on("hotkey-registration-failed", listener);
return () => ipcRenderer.removeListener("hotkey-registration-failed", listener);
},
-
- // Remove all listeners for a channel
- removeAllListeners: (channel) => {
- ipcRenderer.removeAllListeners(channel);
- },
});
diff --git a/scripts/download-llama-server.js b/scripts/download-llama-server.js
new file mode 100644
index 000000000..e96fa5702
--- /dev/null
+++ b/scripts/download-llama-server.js
@@ -0,0 +1,143 @@
+#!/usr/bin/env node
+const fs = require("fs");
+const path = require("path");
+const {
+ downloadFile,
+ extractZip,
+ findBinaryInDir,
+ parseArgs,
+ setExecutable,
+ cleanupFiles,
+} = require("./lib/download-utils");
+
+const LLAMA_CPP_REPO = "ggerganov/llama.cpp";
+const LLAMA_CPP_VERSION = "b4621";
+
+const BINARIES = {
+ "darwin-arm64": {
+ zipName: `llama-${LLAMA_CPP_VERSION}-bin-macos-arm64.zip`,
+ binaryPath: "build/bin/llama-server",
+ outputName: "llama-server-darwin-arm64",
+ },
+ "darwin-x64": {
+ zipName: `llama-${LLAMA_CPP_VERSION}-bin-macos-x64.zip`,
+ binaryPath: "build/bin/llama-server",
+ outputName: "llama-server-darwin-x64",
+ },
+ "win32-x64": {
+ zipName: `llama-${LLAMA_CPP_VERSION}-bin-win-avx2-x64.zip`,
+ binaryPath: "build/bin/llama-server.exe",
+ outputName: "llama-server-win32-x64.exe",
+ },
+ "linux-x64": {
+ zipName: `llama-${LLAMA_CPP_VERSION}-bin-ubuntu-x64.zip`,
+ binaryPath: "build/bin/llama-server",
+ outputName: "llama-server-linux-x64",
+ },
+};
+
+const BIN_DIR = path.join(__dirname, "..", "resources", "bin");
+
+function getDownloadUrl(zipName) {
+ return `https://github.com/${LLAMA_CPP_REPO}/releases/download/${LLAMA_CPP_VERSION}/${zipName}`;
+}
+
+async function downloadBinary(platformArch, config) {
+ if (!config) {
+ console.log(` ${platformArch}: Not supported`);
+ return false;
+ }
+
+ const outputPath = path.join(BIN_DIR, config.outputName);
+
+ if (fs.existsSync(outputPath)) {
+ console.log(` ${platformArch}: Already exists, skipping`);
+ return true;
+ }
+
+ const url = getDownloadUrl(config.zipName);
+ console.log(` ${platformArch}: Downloading from ${url}`);
+
+ const zipPath = path.join(BIN_DIR, config.zipName);
+
+ try {
+ await downloadFile(url, zipPath);
+
+ const extractDir = path.join(BIN_DIR, `temp-llama-${platformArch}`);
+ fs.mkdirSync(extractDir, { recursive: true });
+ extractZip(zipPath, extractDir);
+
+ const binaryName = path.basename(config.binaryPath);
+ let binaryPath = path.join(extractDir, config.binaryPath);
+
+ if (!fs.existsSync(binaryPath)) {
+ binaryPath = findBinaryInDir(extractDir, binaryName);
+ }
+
+ if (binaryPath && fs.existsSync(binaryPath)) {
+ fs.copyFileSync(binaryPath, outputPath);
+ setExecutable(outputPath);
+ console.log(` ${platformArch}: Extracted to ${config.outputName}`);
+ } else {
+ console.error(` ${platformArch}: Binary '${binaryName}' not found in archive`);
+ return false;
+ }
+
+ fs.rmSync(extractDir, { recursive: true, force: true });
+ if (fs.existsSync(zipPath)) fs.unlinkSync(zipPath);
+ return true;
+ } catch (error) {
+ console.error(` ${platformArch}: Failed - ${error.message}`);
+ if (fs.existsSync(zipPath)) fs.unlinkSync(zipPath);
+ return false;
+ }
+}
+
+async function main() {
+ console.log(`\nDownloading llama-server binaries (${LLAMA_CPP_VERSION})...\n`);
+
+ fs.mkdirSync(BIN_DIR, { recursive: true });
+
+ const args = parseArgs();
+
+ if (args.isCurrent) {
+ if (!BINARIES[args.platformArch]) {
+ console.error(`Unsupported platform/arch: ${args.platformArch}`);
+ process.exitCode = 1;
+ return;
+ }
+
+ console.log(`Downloading for target platform (${args.platformArch}):`);
+ const ok = await downloadBinary(args.platformArch, BINARIES[args.platformArch]);
+ if (!ok) {
+ console.error(`Failed to download binaries for ${args.platformArch}`);
+ process.exitCode = 1;
+ return;
+ }
+
+ if (args.shouldCleanup) {
+ cleanupFiles(BIN_DIR, "llama-server", `llama-server-${args.platformArch}`);
+ }
+ } else {
+ console.log("Downloading binaries for all platforms:");
+ for (const platformArch of Object.keys(BINARIES)) {
+ await downloadBinary(platformArch, BINARIES[platformArch]);
+ }
+ }
+
+ console.log("\n---");
+
+ const files = fs.readdirSync(BIN_DIR).filter((f) => f.startsWith("llama-server"));
+ if (files.length > 0) {
+ console.log("Available llama-server binaries:\n");
+ files.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(`\nCheck: https://github.com/${LLAMA_CPP_REPO}/releases/tag/${LLAMA_CPP_VERSION}`);
+ }
+}
+
+main().catch(console.error);
diff --git a/scripts/download-whisper-cpp.js b/scripts/download-whisper-cpp.js
index 68aa51e4c..894ac842a 100644
--- a/scripts/download-whisper-cpp.js
+++ b/scripts/download-whisper-cpp.js
@@ -1,48 +1,18 @@
#!/usr/bin/env node
-/**
- * Downloads whisper.cpp binaries from OpenWhispr's fork releases.
- *
- * 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");
const fs = require("fs");
const path = require("path");
-const { execSync } = require("child_process");
+const {
+ downloadFile,
+ extractZip,
+ parseArgs,
+ setExecutable,
+ cleanupFiles,
+} = require("./lib/download-utils");
-// Configuration - Update WHISPER_CPP_VERSION when releasing new builds
const WHISPER_CPP_REPO = "gabrielste1n/whisper.cpp";
-const WHISPER_CPP_VERSION = "0.0.5"; // Bump version for server binaries
+const WHISPER_CPP_VERSION = "0.0.5";
-// Platform-specific binary info for whisper-cli
-const CLI_BINARIES = {
- "darwin-arm64": {
- zipName: "whisper-cpp-darwin-arm64.zip",
- binaryName: "whisper-cpp-darwin-arm64",
- outputName: "whisper-cpp-darwin-arm64",
- },
- "darwin-x64": {
- zipName: "whisper-cpp-darwin-x64.zip",
- binaryName: "whisper-cpp-darwin-x64",
- outputName: "whisper-cpp-darwin-x64",
- },
- "win32-x64": {
- zipName: "whisper-cpp-win32-x64.zip",
- binaryName: "whisper-cpp-win32-x64.exe",
- outputName: "whisper-cpp-win32-x64.exe",
- },
- "linux-x64": {
- zipName: "whisper-cpp-linux-x64.zip",
- binaryName: "whisper-cpp-linux-x64",
- outputName: "whisper-cpp-linux-x64",
- },
-};
-
-// Platform-specific binary info for whisper-server (HTTP API)
-const SERVER_BINARIES = {
+const BINARIES = {
"darwin-arm64": {
zipName: "whisper-server-darwin-arm64.zip",
binaryName: "whisper-server-darwin-arm64",
@@ -71,283 +41,95 @@ function getDownloadUrl(zipName) {
return `https://github.com/${WHISPER_CPP_REPO}/releases/download/${WHISPER_CPP_VERSION}/${zipName}`;
}
-const REQUEST_TIMEOUT = 30000; // 30 seconds for connection
-const MAX_RETRIES = 3;
-const RETRY_DELAY = 2000; // 2 seconds between retries
-
-function sleep(ms) {
- return new Promise((resolve) => setTimeout(resolve, ms));
-}
-
-function downloadFile(url, dest, retryCount = 0) {
- return new Promise((resolve, reject) => {
- const file = fs.createWriteStream(dest);
- let activeRequest = null;
-
- const cleanup = () => {
- if (activeRequest) {
- activeRequest.destroy();
- activeRequest = null;
- }
- file.close();
- };
-
- const request = (currentUrl, redirectCount = 0) => {
- // Prevent infinite redirects
- if (redirectCount > 5) {
- cleanup();
- reject(new Error("Too many redirects"));
- return;
- }
-
- activeRequest = https.get(currentUrl, (response) => {
- if (response.statusCode === 302 || response.statusCode === 301) {
- const redirectUrl = response.headers.location;
- if (!redirectUrl) {
- cleanup();
- reject(new Error("Redirect without location header"));
- return;
- }
- request(redirectUrl, redirectCount + 1);
- return;
- }
-
- if (response.statusCode !== 200) {
- cleanup();
- reject(new Error(`Failed to download: HTTP ${response.statusCode}`));
- return;
- }
-
- const total = parseInt(response.headers["content-length"], 10);
- let downloaded = 0;
-
- response.on("data", (chunk) => {
- downloaded += chunk.length;
- const pct = total ? Math.round((downloaded / total) * 100) : 0;
- process.stdout.write(`\r Downloading: ${pct}%`);
- });
-
- response.on("error", (err) => {
- cleanup();
- reject(err);
- });
-
- response.pipe(file);
- file.on("finish", () => {
- file.close();
- console.log(" Done");
- resolve();
- });
-
- file.on("error", (err) => {
- cleanup();
- reject(err);
- });
- });
-
- activeRequest.on("error", (err) => {
- cleanup();
- reject(err);
- });
-
- // Connection timeout
- activeRequest.setTimeout(REQUEST_TIMEOUT, () => {
- cleanup();
- reject(new Error("Connection timed out"));
- });
- };
-
- request(url);
- }).catch(async (error) => {
- // Retry logic for transient failures
- if (retryCount < MAX_RETRIES) {
- console.log(`\n Retry ${retryCount + 1}/${MAX_RETRIES}: ${error.message}`);
- await sleep(RETRY_DELAY);
- // Clean up partial file before retry
- if (fs.existsSync(dest)) {
- fs.unlinkSync(dest);
- }
- return downloadFile(url, dest, retryCount + 1);
- }
- throw error;
- });
-}
-
-function extractZip(zipPath, destDir) {
- if (process.platform === "win32") {
- execSync(`powershell -command "Expand-Archive -Path '${zipPath}' -DestinationPath '${destDir}' -Force"`, { stdio: "inherit" });
- } else {
- execSync(`unzip -o "${zipPath}" -d "${destDir}"`, { stdio: "inherit" });
- }
-}
-
-async function downloadBinary(platformArch, config, label) {
+async function downloadBinary(platformArch, config) {
if (!config) {
- console.log(` ${label} ${platformArch}: Not supported`);
+ console.log(` [server] ${platformArch}: Not supported`);
return false;
}
const outputPath = path.join(BIN_DIR, config.outputName);
if (fs.existsSync(outputPath)) {
- console.log(` ${label} ${platformArch}: Already exists, skipping`);
+ console.log(` [server] ${platformArch}: Already exists, skipping`);
return true;
}
const url = getDownloadUrl(config.zipName);
- console.log(` ${label} ${platformArch}: Downloading from ${url}`);
+ console.log(` [server] ${platformArch}: Downloading from ${url}`);
const zipPath = path.join(BIN_DIR, config.zipName);
try {
await downloadFile(url, zipPath);
- // Remove brackets from label for safe directory names on Windows
- const safeLabel = label.replace(/[[\]]/g, "");
- const extractDir = path.join(BIN_DIR, `temp-${safeLabel}-${platformArch}`);
+ const extractDir = path.join(BIN_DIR, `temp-whisper-${platformArch}`);
fs.mkdirSync(extractDir, { recursive: true });
extractZip(zipPath, extractDir);
- // Find and copy the binary
const binaryPath = path.join(extractDir, config.binaryName);
if (fs.existsSync(binaryPath)) {
fs.copyFileSync(binaryPath, outputPath);
- if (process.platform !== "win32") {
- fs.chmodSync(outputPath, 0o755);
- }
- console.log(` ${label} ${platformArch}: Extracted to ${config.outputName}`);
+ setExecutable(outputPath);
+ console.log(` [server] ${platformArch}: Extracted to ${config.outputName}`);
} else {
- console.error(` ${label} ${platformArch}: Binary not found in archive`);
+ console.error(` [server] ${platformArch}: Binary not found in archive`);
return false;
}
- // Cleanup
fs.rmSync(extractDir, { recursive: true, force: true });
- fs.unlinkSync(zipPath);
+ if (fs.existsSync(zipPath)) fs.unlinkSync(zipPath);
return true;
-
} catch (error) {
- console.error(` ${label} ${platformArch}: Failed - ${error.message}`);
+ console.error(` [server] ${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
- const cliOk = await downloadBinary(platformArch, CLI_BINARIES[platformArch], "[cli]");
- const serverOk = await downloadBinary(platformArch, SERVER_BINARIES[platformArch], "[server]");
- return cliOk && serverOk;
-}
-
async function main() {
- console.log(`\nDownloading whisper.cpp binaries (${WHISPER_CPP_VERSION})...\n`);
+ console.log(`\nDownloading whisper-server binaries (${WHISPER_CPP_VERSION})...\n`);
- // Create bin directory
fs.mkdirSync(BIN_DIR, { recursive: true });
- const currentPlatform = process.platform;
- const currentArch = process.arch;
-
- // Parse command-line arguments for explicit platform/arch
- let targetPlatform = currentPlatform;
- let targetArch = currentArch;
+ const args = parseArgs();
- const platformIndex = process.argv.indexOf('--platform');
- if (platformIndex !== -1 && process.argv[platformIndex + 1]) {
- targetPlatform = process.argv[platformIndex + 1];
- }
-
- const archIndex = process.argv.indexOf('--arch');
- if (archIndex !== -1 && process.argv[archIndex + 1]) {
- targetArch = process.argv[archIndex + 1];
- }
-
- const targetPlatformArch = `${targetPlatform}-${targetArch}`;
- const isCI = process.env.CI === "true" || process.env.GITHUB_ACTIONS === "true";
- const shouldCleanup = process.argv.includes("--clean") || isCI;
-
- if (process.argv.includes("--current")) {
- if (!CLI_BINARIES[targetPlatformArch] || !SERVER_BINARIES[targetPlatformArch]) {
- console.error(`Unsupported platform/arch: ${targetPlatformArch}`);
+ if (args.isCurrent) {
+ if (!BINARIES[args.platformArch]) {
+ console.error(`Unsupported platform/arch: ${args.platformArch}`);
process.exitCode = 1;
return;
}
- // Only download for specified platform/arch
- console.log(`Downloading for target platform (${targetPlatformArch}):`);
- const downloadOk = await downloadForPlatform(targetPlatformArch);
- if (!downloadOk) {
- console.error(`Failed to download binaries for ${targetPlatformArch}`);
+ console.log(`Downloading for target platform (${args.platformArch}):`);
+ const ok = await downloadBinary(args.platformArch, BINARIES[args.platformArch]);
+ if (!ok) {
+ console.error(`Failed to download binaries for ${args.platformArch}`);
process.exitCode = 1;
return;
}
- if (shouldCleanup) {
- // Clean old binaries that don't match target platform/arch
- const existingFiles = fs.readdirSync(BIN_DIR).filter(f => f.startsWith("whisper-"));
- const targetPrefix = `whisper-cpp-${targetPlatformArch}`;
- const targetServerPrefix = `whisper-server-${targetPlatformArch}`;
-
- existingFiles.forEach(file => {
- if (!file.startsWith(targetPrefix) && !file.startsWith(targetServerPrefix)) {
- const filePath = path.join(BIN_DIR, file);
- console.log(`Removing old binary: ${file}`);
- fs.unlinkSync(filePath);
- }
- });
- }
- } 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 (CLI + server):");
- for (const platformArch of Object.keys(CLI_BINARIES)) {
- await downloadForPlatform(platformArch);
+ if (args.shouldCleanup) {
+ cleanupFiles(BIN_DIR, "whisper-server", `whisper-server-${args.platformArch}`);
}
} else {
- // Default: download for build targets (all platforms)
- console.log("Downloading binaries for all platforms (CLI + server):");
- for (const platformArch of Object.keys(CLI_BINARIES)) {
- await downloadForPlatform(platformArch);
+ console.log("Downloading binaries for all platforms:");
+ for (const platformArch of Object.keys(BINARIES)) {
+ await downloadBinary(platformArch, BINARIES[platformArch]);
}
}
console.log("\n---");
- // List what we have
- const files = fs.readdirSync(BIN_DIR).filter(f => f.startsWith("whisper-"));
+ const files = fs.readdirSync(BIN_DIR).filter((f) => f.startsWith("whisper-server"));
if (files.length > 0) {
- console.log("Available binaries:");
-
- // 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)`);
- });
- }
+ console.log("Available whisper-server binaries:\n");
+ files.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:");
- console.log(` https://github.com/${WHISPER_CPP_REPO}/releases`);
- console.log("\nRun the GitHub Actions workflow 'Build Binaries for OpenWhispr' first.");
+ console.log(`\nMake sure release exists: https://github.com/${WHISPER_CPP_REPO}/releases`);
}
}
diff --git a/scripts/lib/download-utils.js b/scripts/lib/download-utils.js
new file mode 100644
index 000000000..9eb3ad092
--- /dev/null
+++ b/scripts/lib/download-utils.js
@@ -0,0 +1,194 @@
+const https = require("https");
+const fs = require("fs");
+const path = require("path");
+const { execSync } = require("child_process");
+
+const REQUEST_TIMEOUT = 30000;
+const MAX_RETRIES = 3;
+const RETRY_DELAY = 2000;
+const MAX_REDIRECTS = 5;
+
+function sleep(ms) {
+ return new Promise((resolve) => setTimeout(resolve, ms));
+}
+
+function downloadFile(url, dest, retryCount = 0) {
+ return new Promise((resolve, reject) => {
+ const file = fs.createWriteStream(dest);
+ let activeRequest = null;
+
+ const cleanup = () => {
+ if (activeRequest) {
+ activeRequest.destroy();
+ activeRequest = null;
+ }
+ file.close();
+ };
+
+ const request = (currentUrl, redirectCount = 0) => {
+ if (redirectCount > MAX_REDIRECTS) {
+ cleanup();
+ reject(new Error("Too many redirects"));
+ return;
+ }
+
+ activeRequest = https.get(currentUrl, (response) => {
+ if (response.statusCode === 302 || response.statusCode === 301) {
+ const redirectUrl = response.headers.location;
+ if (!redirectUrl) {
+ cleanup();
+ reject(new Error("Redirect without location header"));
+ return;
+ }
+ request(redirectUrl, redirectCount + 1);
+ return;
+ }
+
+ if (response.statusCode !== 200) {
+ cleanup();
+ reject(new Error(`HTTP ${response.statusCode}`));
+ return;
+ }
+
+ const total = parseInt(response.headers["content-length"], 10);
+ let downloaded = 0;
+
+ response.on("data", (chunk) => {
+ downloaded += chunk.length;
+ const pct = total ? Math.round((downloaded / total) * 100) : 0;
+ process.stdout.write(`\r Downloading: ${pct}%`);
+ });
+
+ response.on("error", (err) => {
+ cleanup();
+ reject(err);
+ });
+
+ response.pipe(file);
+ file.on("finish", () => {
+ file.close();
+ console.log(" Done");
+ resolve();
+ });
+
+ file.on("error", (err) => {
+ cleanup();
+ reject(err);
+ });
+ });
+
+ activeRequest.on("error", (err) => {
+ cleanup();
+ reject(err);
+ });
+
+ activeRequest.setTimeout(REQUEST_TIMEOUT, () => {
+ cleanup();
+ reject(new Error("Connection timed out"));
+ });
+ };
+
+ request(url);
+ }).catch(async (error) => {
+ const isTransient = error.message.includes("timed out") ||
+ error.code === "ECONNRESET" ||
+ error.code === "ETIMEDOUT";
+
+ if (isTransient && retryCount < MAX_RETRIES) {
+ console.log(`\n Retry ${retryCount + 1}/${MAX_RETRIES}: ${error.message}`);
+ await sleep(RETRY_DELAY);
+ if (fs.existsSync(dest)) {
+ fs.unlinkSync(dest);
+ }
+ return downloadFile(url, dest, retryCount + 1);
+ }
+ throw error;
+ });
+}
+
+function extractZip(zipPath, destDir) {
+ if (process.platform === "win32") {
+ execSync(
+ `powershell -command "Expand-Archive -Path '${zipPath}' -DestinationPath '${destDir}' -Force"`,
+ { stdio: "inherit" }
+ );
+ } else {
+ execSync(`unzip -o "${zipPath}" -d "${destDir}"`, { stdio: "inherit" });
+ }
+}
+
+function findBinaryInDir(dir, binaryName, maxDepth = 5, currentDepth = 0) {
+ if (currentDepth >= maxDepth) return null;
+
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
+
+ for (const entry of entries) {
+ const fullPath = path.join(dir, entry.name);
+
+ if (entry.isDirectory()) {
+ const found = findBinaryInDir(fullPath, binaryName, maxDepth, currentDepth + 1);
+ if (found) return found;
+ } else if (entry.name === binaryName) {
+ return fullPath;
+ }
+ }
+
+ return null;
+}
+
+function parseArgs() {
+ const args = process.argv;
+ let targetPlatform = process.platform;
+ let targetArch = process.arch;
+
+ const platformIndex = args.indexOf("--platform");
+ if (platformIndex !== -1 && args[platformIndex + 1]) {
+ targetPlatform = args[platformIndex + 1];
+ }
+
+ const archIndex = args.indexOf("--arch");
+ if (archIndex !== -1 && args[archIndex + 1]) {
+ targetArch = args[archIndex + 1];
+ }
+
+ return {
+ targetPlatform,
+ targetArch,
+ platformArch: `${targetPlatform}-${targetArch}`,
+ isCurrent: args.includes("--current"),
+ isAll: args.includes("--all"),
+ shouldCleanup: args.includes("--clean") ||
+ process.env.CI === "true" ||
+ process.env.GITHUB_ACTIONS === "true",
+ };
+}
+
+function setExecutable(filePath) {
+ if (process.platform !== "win32") {
+ fs.chmodSync(filePath, 0o755);
+ }
+}
+
+function cleanupFiles(binDir, prefix, keepPrefix) {
+ const files = fs.readdirSync(binDir).filter((f) => f.startsWith(prefix));
+ files.forEach((file) => {
+ if (!file.startsWith(keepPrefix)) {
+ const filePath = path.join(binDir, file);
+ console.log(`Removing old binary: ${file}`);
+ fs.unlinkSync(filePath);
+ }
+ });
+}
+
+module.exports = {
+ downloadFile,
+ extractZip,
+ findBinaryInDir,
+ parseArgs,
+ setExecutable,
+ cleanupFiles,
+ sleep,
+ REQUEST_TIMEOUT,
+ MAX_RETRIES,
+ RETRY_DELAY,
+};
diff --git a/src/components/OnboardingFlow.tsx b/src/components/OnboardingFlow.tsx
index 1eef460d4..eed1d9a6b 100644
--- a/src/components/OnboardingFlow.tsx
+++ b/src/components/OnboardingFlow.tsx
@@ -1,4 +1,4 @@
-import React, { useState, useEffect, useCallback, useRef, useMemo } from "react";
+import React, { useState, useEffect, useCallback, useRef } from "react";
import { Card, CardContent } from "./ui/card";
import { Button } from "./ui/button";
import { Input } from "./ui/input";
@@ -9,17 +9,14 @@ import {
Check,
Settings,
Mic,
- Key,
Shield,
Command,
Sparkles,
- Lock,
User,
} from "lucide-react";
import TitleBar from "./TitleBar";
-import LocalWhisperPicker from "./LocalWhisperPicker";
+import TranscriptionModelPicker from "./TranscriptionModelPicker";
import ProcessingModeSelector from "./ui/ProcessingModeSelector";
-import ApiKeyInput from "./ui/ApiKeyInput";
import PermissionCard from "./ui/PermissionCard";
import MicPermissionWarning from "./ui/MicPermissionWarning";
import PasteToolsInfo from "./ui/PasteToolsInfo";
@@ -30,14 +27,9 @@ import { useDialogs } from "../hooks/useDialogs";
import { usePermissions } from "../hooks/usePermissions";
import { useClipboard } from "../hooks/useClipboard";
import { useSettings } from "../hooks/useSettings";
-import { getLanguageLabel } from "../utils/languages";
-import { REASONING_PROVIDERS } from "../models/ModelRegistry";
import LanguageSelector from "./ui/LanguageSelector";
-import ModelCardList from "./ui/ModelCardList";
import { setAgentName as saveAgentName } from "../utils/agentName";
import { formatHotkeyLabel, getDefaultHotkey } from "../utils/hotkeys";
-import { API_ENDPOINTS, buildApiUrl, normalizeBaseUrl } from "../config/constants";
-import { isSecureEndpoint } from "../utils/urlUtils";
import { HotkeyInput } from "./ui/HotkeyInput";
import { useHotkeyRegistration } from "../hooks/useHotkeyRegistration";
import { ActivationModeSelector } from "./ui/ActivationModeSelector";
@@ -46,16 +38,9 @@ interface OnboardingFlowProps {
onComplete: () => void;
}
-type ReasoningModelOption = {
- value: string;
- label: string;
- description?: string;
- icon?: string;
-};
-
export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) {
- // Max valid step index for the current onboarding flow (6 steps, index 0-5)
- const MAX_STEP = 5;
+ // Max valid step index for the current onboarding flow (5 steps, index 0-4)
+ const MAX_STEP = 4;
const [currentStep, setCurrentStep, removeCurrentStep] = useLocalStorage(
"onboardingCurrentStep",
@@ -77,38 +62,27 @@ export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) {
useLocalWhisper,
whisperModel,
preferredLanguage,
+ cloudTranscriptionProvider,
+ cloudTranscriptionModel,
cloudTranscriptionBaseUrl,
- cloudReasoningBaseUrl,
- useReasoningModel,
- reasoningModel,
openaiApiKey,
+ groqApiKey,
dictationKey,
activationMode,
setActivationMode,
- setUseLocalWhisper,
setWhisperModel,
- setPreferredLanguage,
setDictationKey,
+ setOpenaiApiKey,
+ setGroqApiKey,
updateTranscriptionSettings,
- updateReasoningSettings,
- updateApiKeys,
} = useSettings();
- const [apiKey, setApiKey] = useState(openaiApiKey);
const [hotkey, setHotkey] = useState(dictationKey || "`");
- const [transcriptionBaseUrl, setTranscriptionBaseUrl] = useState(cloudTranscriptionBaseUrl);
- const [reasoningBaseUrl, setReasoningBaseUrl] = useState(cloudReasoningBaseUrl);
const [agentName, setAgentName] = useState("Agent");
const [isModelDownloaded, setIsModelDownloaded] = useState(false);
const readableHotkey = formatHotkeyLabel(hotkey);
- const {
- alertDialog,
- confirmDialog,
- showAlertDialog,
- showConfirmDialog,
- hideAlertDialog,
- hideConfirmDialog,
- } = useDialogs();
+ const { alertDialog, confirmDialog, showAlertDialog, hideAlertDialog, hideConfirmDialog } =
+ useDialogs();
const practiceTextareaRef = useRef<HTMLInputElement>(null);
// Ref to prevent React.StrictMode double-invocation of auto-registration
@@ -125,213 +99,17 @@ export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) {
showErrorToast: false,
});
- const trimmedReasoningBase = (reasoningBaseUrl || "").trim();
- const normalizedReasoningBaseUrl = useMemo(
- () => normalizeBaseUrl(trimmedReasoningBase),
- [trimmedReasoningBase]
- );
- const hasEnteredReasoningBase = trimmedReasoningBase.length > 0;
- const isValidReasoningBase = Boolean(
- normalizedReasoningBaseUrl && normalizedReasoningBaseUrl.includes("://")
- );
- const usingCustomReasoningBase = hasEnteredReasoningBase && isValidReasoningBase;
-
- const [customReasoningModels, setCustomReasoningModels] = useState<ReasoningModelOption[]>([]);
- const [customModelsLoading, setCustomModelsLoading] = useState(false);
- const [customModelsError, setCustomModelsError] = useState<string | null>(null);
-
- const defaultReasoningModels = useMemo<ReasoningModelOption[]>(() => {
- const provider = REASONING_PROVIDERS.openai;
- return (
- provider?.models?.map((model) => ({
- value: model.value,
- label: model.label,
- description: model.description,
- })) ?? []
- );
- }, []);
-
- const displayedReasoningModels = usingCustomReasoningBase
- ? customReasoningModels
- : defaultReasoningModels;
-
- const reasoningModelsEndpoint = useMemo(() => {
- const base =
- usingCustomReasoningBase && normalizedReasoningBaseUrl
- ? normalizedReasoningBaseUrl
- : API_ENDPOINTS.OPENAI_BASE;
- return buildApiUrl(base, "/models");
- }, [usingCustomReasoningBase, normalizedReasoningBaseUrl]);
-
- const persistOpenAIKey = useCallback(
- async (nextKey: string) => {
- const trimmedKey = nextKey.trim();
- if (useLocalWhisper || !trimmedKey) {
- return false;
- }
- if (trimmedKey === openaiApiKey.trim()) {
- return true;
- }
-
- try {
- // updateApiKeys handles both localStorage and IPC save
- updateApiKeys({ openaiApiKey: trimmedKey });
- return true;
- } catch (error) {
- console.error("Failed to save OpenAI key", error);
- return false;
- }
- },
- [useLocalWhisper, updateApiKeys, openaiApiKey]
- );
-
- const reasoningModelRef = useRef(reasoningModel);
- useEffect(() => {
- reasoningModelRef.current = reasoningModel;
- }, [reasoningModel]);
-
- useEffect(() => {
- if (!usingCustomReasoningBase) {
- setCustomModelsLoading(false);
- setCustomReasoningModels([]);
- setCustomModelsError(null);
- return;
- }
-
- if (!normalizedReasoningBaseUrl) {
- return;
- }
-
- let isCancelled = false;
- const controller = new AbortController();
-
- const loadModels = async () => {
- setCustomModelsLoading(true);
- setCustomModelsError(null);
- try {
- if (!isSecureEndpoint(normalizedReasoningBaseUrl)) {
- throw new Error("HTTPS required (HTTP allowed for local network only).");
- }
-
- const headers: Record<string, string> = {};
- const trimmedKey = apiKey.trim();
- if (trimmedKey) {
- headers.Authorization = `Bearer ${trimmedKey}`;
- }
-
- const response = await fetch(buildApiUrl(normalizedReasoningBaseUrl, "/models"), {
- method: "GET",
- headers,
- signal: controller.signal,
- });
-
- if (!response.ok) {
- const errorText = await response.text().catch(() => "");
- throw new Error(
- errorText
- ? `${response.status} ${errorText.slice(0, 200)}`
- : `${response.status} ${response.statusText}`
- );
- }
-
- const payload = await response.json().catch(() => ({}));
- const rawModels = Array.isArray(payload?.data)
- ? payload.data
- : Array.isArray(payload?.models)
- ? payload.models
- : [];
-
- const mappedModels = (rawModels as Array<any>)
- .map((item) => {
- const value = item?.id || item?.name;
- if (!value) {
- return null;
- }
-
- const description =
- typeof item?.description === "string" && item.description.trim()
- ? item.description.trim()
- : undefined;
- const ownedBy = typeof item?.owned_by === "string" ? item.owned_by : undefined;
-
- return {
- value,
- label: item?.id || item?.name || value,
- description: description || (ownedBy ? `Owner: ${ownedBy}` : undefined),
- } as ReasoningModelOption;
- })
- .filter(Boolean) as ReasoningModelOption[];
-
- if (isCancelled) {
- return;
- }
-
- setCustomReasoningModels(mappedModels);
-
- if (mappedModels.length === 0) {
- setCustomModelsError("No models returned by this endpoint.");
- } else if (
- reasoningModelRef.current &&
- !mappedModels.some((model) => model.value === reasoningModelRef.current)
- ) {
- updateReasoningSettings({ reasoningModel: "" });
- }
- } catch (error) {
- if (isCancelled) {
- return;
- }
- if ((error as Error).name === "AbortError") {
- return;
- }
- setCustomModelsError((error as Error).message || "Unable to load models from endpoint.");
- setCustomReasoningModels([]);
- } finally {
- if (!isCancelled) {
- setCustomModelsLoading(false);
- }
- }
- };
-
- loadModels();
-
- return () => {
- isCancelled = true;
- controller.abort();
- };
- }, [usingCustomReasoningBase, normalizedReasoningBaseUrl, apiKey, updateReasoningSettings]);
-
- useEffect(() => {
- if (!usingCustomReasoningBase && defaultReasoningModels.length > 0) {
- if (
- reasoningModel &&
- !defaultReasoningModels.some((model) => model.value === reasoningModel)
- ) {
- updateReasoningSettings({ reasoningModel: "" });
- }
- }
- }, [usingCustomReasoningBase, defaultReasoningModels, reasoningModel, updateReasoningSettings]);
-
- const activeReasoningModelLabel = useMemo(() => {
- const match = displayedReasoningModels.find((model) => model.value === reasoningModel);
- return match?.label || reasoningModel;
- }, [displayedReasoningModels, reasoningModel]);
-
const permissionsHook = usePermissions(showAlertDialog);
- const { pasteFromClipboard } = useClipboard(showAlertDialog);
+ useClipboard(showAlertDialog); // Initialize clipboard hook for permission checks
const steps = [
{ title: "Welcome", icon: Sparkles },
- { title: "Privacy", icon: Lock },
{ title: "Setup", icon: Settings },
{ title: "Permissions", icon: Shield },
{ title: "Hotkey & Test", icon: Command },
{ title: "Agent Name", icon: User },
];
- const updateProcessingMode = (useLocal: boolean) => {
- updateTranscriptionSettings({ useLocalWhisper: useLocal });
- };
-
// Check if selected whisper model is downloaded
useEffect(() => {
if (!useLocalWhisper || !whisperModel) {
@@ -360,10 +138,10 @@ export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) {
}
}, [currentStep]);
- // Auto-register default hotkey when entering the hotkey step (step 4)
+ // Auto-register default hotkey when entering the hotkey step (step 3)
useEffect(() => {
- if (currentStep !== 4) {
- // Reset initialization flag when leaving step 4
+ if (currentStep !== 3) {
+ // Reset initialization flag when leaving step 3
hotkeyStepInitializedRef.current = false;
return;
}
@@ -426,26 +204,6 @@ export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) {
}, [hotkey, showAlertDialog]);
const saveSettings = useCallback(async () => {
- const normalizedTranscriptionBase = (transcriptionBaseUrl || "").trim();
- const normalizedReasoningBaseValue = (reasoningBaseUrl || "").trim();
-
- // Detect if user entered a non-default custom URL
- const isCustomTranscriptionUrl =
- normalizedTranscriptionBase !== "" &&
- normalizedTranscriptionBase !== API_ENDPOINTS.TRANSCRIPTION_BASE &&
- normalizedTranscriptionBase !== "https://api.openai.com/v1";
-
- updateTranscriptionSettings({
- whisperModel,
- preferredLanguage,
- cloudTranscriptionBaseUrl: normalizedTranscriptionBase,
- cloudTranscriptionProvider: isCustomTranscriptionUrl ? "custom" : "openai",
- });
- updateReasoningSettings({
- useReasoningModel,
- reasoningModel,
- cloudReasoningBaseUrl: normalizedReasoningBaseValue,
- });
const hotkeyRegistered = await ensureHotkeyRegistered();
if (!hotkeyRegistered) {
return false;
@@ -459,28 +217,19 @@ export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) {
permissionsHook.accessibilityPermissionGranted.toString()
);
localStorage.setItem("onboardingCompleted", "true");
- const trimmedApiKey = apiKey.trim();
- const skipAuth = trimmedApiKey.length === 0;
- localStorage.setItem("skipAuth", skipAuth.toString());
- if (!useLocalWhisper && trimmedApiKey) {
- await persistOpenAIKey(trimmedApiKey);
+ try {
+ await window.electronAPI?.saveAllKeysToEnv?.();
+ } catch (error) {
+ console.error("Failed to persist API keys:", error);
}
+
return true;
}, [
- whisperModel,
hotkey,
- preferredLanguage,
agentName,
permissionsHook.micPermissionGranted,
permissionsHook.accessibilityPermissionGranted,
- useLocalWhisper,
- apiKey,
- transcriptionBaseUrl,
- reasoningBaseUrl,
- updateTranscriptionSettings,
- updateReasoningSettings,
- persistOpenAIKey,
setDictationKey,
ensureHotkeyRegistered,
]);
@@ -491,20 +240,15 @@ export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) {
}
const newStep = currentStep + 1;
-
- if (currentStep === 2 && !useLocalWhisper) {
- await persistOpenAIKey(apiKey);
- }
-
setCurrentStep(newStep);
- // Show dictation panel when moving from permissions step (3) to hotkey & test step (4)
- if (currentStep === 3 && newStep === 4) {
+ // Show dictation panel when moving from permissions step (2) to hotkey & test step (3)
+ if (currentStep === 2 && newStep === 3) {
if (window.electronAPI?.showDictationPanel) {
window.electronAPI.showDictationPanel();
}
}
- }, [currentStep, setCurrentStep, steps.length, useLocalWhisper, persistOpenAIKey, apiKey]);
+ }, [currentStep, setCurrentStep, steps.length]);
const prevStep = useCallback(() => {
if (currentStep > 0) {
@@ -549,182 +293,50 @@ export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) {
</div>
);
- case 1: // Choose Mode
+ case 1: // Setup - Choose Mode & Configure
return (
<div className="space-y-6">
<div className="text-center">
- <h2 className="text-2xl font-bold text-stone-900 mb-2">
- Choose Your Processing Mode
- </h2>
- <p className="text-stone-600">How would you like to convert your speech to text?</p>
- </div>
-
- <ProcessingModeSelector
- useLocalWhisper={useLocalWhisper}
- setUseLocalWhisper={updateProcessingMode}
- />
- </div>
- );
-
- case 2: // Setup Processing
- return (
- <div className="space-y-6">
- <div className="text-center">
- <h2 className="text-2xl font-bold text-gray-900 mb-2">
- {useLocalWhisper ? "Local Processing Setup" : "Cloud Processing Setup"}
- </h2>
+ <h2 className="text-2xl font-bold text-gray-900 mb-2">Setup Your Transcription</h2>
<p className="text-gray-600">
- {useLocalWhisper
- ? "Let's install and configure Whisper on your device"
- : "Enter your OpenAI API key to get started"}
+ Choose between local (private) or cloud (faster) processing
</p>
</div>
- {useLocalWhisper ? (
- <div className="space-y-4">
- <div className="text-center">
- <div className="w-16 h-16 mx-auto bg-blue-100 rounded-full flex items-center justify-center mb-4">
- <Mic className="w-8 h-8 text-blue-600" />
- </div>
- <h3 className="font-semibold text-gray-900 mb-2">Choose Your Model</h3>
- <p className="text-sm text-gray-600">
- Select a transcription model based on your needs.
- </p>
- </div>
-
- <div>
- <label className="block text-sm font-medium text-gray-700 mb-3">
- Model quality
- </label>
- <p className="text-xs text-gray-500">
- Larger models are more accurate but slower. Base is recommended for most users.
- </p>
- </div>
-
- <LocalWhisperPicker
- selectedModel={whisperModel}
- onModelSelect={setWhisperModel}
- onModelDownloaded={(modelId) => {
- setIsModelDownloaded(true);
- setWhisperModel(modelId);
- }}
- variant="onboarding"
- />
- </div>
- ) : (
- <div className="space-y-4">
- <div className="text-center">
- <div className="w-16 h-16 mx-auto bg-blue-100 rounded-full flex items-center justify-center mb-4">
- <Key className="w-8 h-8 text-blue-600" />
- </div>
- </div>
-
- <ApiKeyInput
- apiKey={apiKey}
- setApiKey={setApiKey}
- label="OpenAI API Key"
- helpText={
- <>
- Need an API key?{" "}
- <a
- href="https://platform.openai.com"
- target="_blank"
- rel="noopener noreferrer"
- className="text-blue-600 underline"
- >
- platform.openai.com
- </a>
- </>
- }
- />
-
- <div className="space-y-2">
- <label className="block text-sm font-medium text-blue-900">
- Custom transcription base URL (optional)
- </label>
- <Input
- value={transcriptionBaseUrl}
- onChange={(event) => setTranscriptionBaseUrl(event.target.value)}
- placeholder="https://api.openai.com/v1"
- className="text-sm"
- />
- <p className="text-xs text-blue-800">
- Cloud transcription requests default to{" "}
- <code>{API_ENDPOINTS.TRANSCRIPTION_BASE}</code>. Enter an OpenAI-compatible base
- URL to override.
- </p>
- </div>
-
- <div className="space-y-2">
- <label className="block text-sm font-medium text-blue-900">
- Custom reasoning base URL (optional)
- </label>
- <Input
- value={reasoningBaseUrl}
- onChange={(event) => setReasoningBaseUrl(event.target.value)}
- placeholder="https://api.openai.com/v1"
- className="text-sm"
- />
- <p className="text-xs text-blue-800">
- We'll load AI models from this endpoint's /v1/models route during setup. Leave
- empty to use the default OpenAI endpoint.
- </p>
- </div>
-
- <div className="space-y-3 pt-4 border-t border-blue-100">
- <h4 className="font-medium text-blue-900">Reasoning Model</h4>
- {hasEnteredReasoningBase ? (
- <>
- {isValidReasoningBase ? (
- <p className="text-xs text-blue-800 break-all">
- Models load from <code>{reasoningModelsEndpoint}</code>.
- </p>
- ) : (
- <p className="text-xs text-amber-600">
- Enter a full base URL including protocol (e.g. https://server/v1).
- </p>
- )}
- {isValidReasoningBase && customModelsLoading && (
- <p className="text-xs text-blue-600">Fetching models...</p>
- )}
- {isValidReasoningBase && customModelsError && (
- <p className="text-xs text-red-600">{customModelsError}</p>
- )}
- {isValidReasoningBase &&
- !customModelsLoading &&
- !customModelsError &&
- displayedReasoningModels.length === 0 && (
- <p className="text-xs text-amber-600">
- No models returned by this endpoint.
- </p>
- )}
- </>
- ) : (
- <p className="text-xs text-blue-800">
- Using OpenAI defaults from <code>{reasoningModelsEndpoint}</code>.
- </p>
- )}
- <ModelCardList
- models={displayedReasoningModels}
- selectedModel={reasoningModel}
- onModelSelect={(modelId) =>
- updateReasoningSettings({ reasoningModel: modelId })
- }
- />
- </div>
+ {/* Mode Selector */}
+ <div className="space-y-4">
+ <ProcessingModeSelector
+ useLocalWhisper={useLocalWhisper}
+ setUseLocalWhisper={(useLocal) =>
+ updateTranscriptionSettings({ useLocalWhisper: useLocal })
+ }
+ />
+ </div>
- <div className="bg-blue-50 p-4 rounded-lg">
- <h4 className="font-medium text-blue-900 mb-2">How to get your API key:</h4>
- <ol className="text-sm text-blue-800 space-y-1">
- <li>1. Go to platform.openai.com</li>
- <li>2. Sign in to your account</li>
- <li>3. Navigate to API Keys</li>
- <li>4. Create a new secret key</li>
- <li>5. Copy and paste it here</li>
- </ol>
- </div>
- </div>
- )}
+ {/* Configuration for selected mode */}
+ <TranscriptionModelPicker
+ selectedCloudProvider={cloudTranscriptionProvider}
+ onCloudProviderSelect={(provider) =>
+ updateTranscriptionSettings({ cloudTranscriptionProvider: provider })
+ }
+ selectedCloudModel={cloudTranscriptionModel}
+ onCloudModelSelect={(model) =>
+ updateTranscriptionSettings({ cloudTranscriptionModel: model })
+ }
+ selectedLocalModel={whisperModel}
+ onLocalModelSelect={setWhisperModel}
+ useLocalWhisper={useLocalWhisper}
+ onModeChange={() => {}}
+ openaiApiKey={openaiApiKey}
+ setOpenaiApiKey={setOpenaiApiKey}
+ groqApiKey={groqApiKey}
+ setGroqApiKey={setGroqApiKey}
+ cloudTranscriptionBaseUrl={cloudTranscriptionBaseUrl}
+ setCloudTranscriptionBaseUrl={(url) =>
+ updateTranscriptionSettings({ cloudTranscriptionBaseUrl: url })
+ }
+ variant="onboarding"
+ />
{/* Language Selection - shown for both modes */}
<div className="space-y-4 p-4 bg-gray-50 border border-gray-200 rounded-xl">
@@ -742,13 +354,13 @@ export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) {
<p className="text-xs text-gray-600 mt-1">
{useLocalWhisper
? "Helps Whisper better understand your speech"
- : "Improves OpenAI transcription speed and accuracy. AI text enhancement is enabled by default."}
+ : "Improves transcription speed and accuracy. AI text enhancement is enabled by default."}
</p>
</div>
</div>
);
- case 3: // Permissions
+ case 2: // Permissions
const platform = permissionsHook.pasteToolsInfo?.platform;
const isMacOS = platform === "darwin";
@@ -817,7 +429,7 @@ export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) {
</div>
);
- case 4: // Hotkey & Test (combined)
+ case 3: // Hotkey & Test (combined)
return (
<div className="space-y-6">
<div className="text-center">
@@ -875,7 +487,7 @@ export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) {
</div>
);
- case 5: // Agent Name (final step)
+ case 4: // Agent Name (final step)
return (
<div className="space-y-6">
<div className="text-center">
@@ -917,26 +529,25 @@ export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) {
const canProceed = () => {
switch (currentStep) {
case 0:
- return true;
+ return true; // Welcome
case 1:
- return true; // Mode selection
- case 2:
+ // Setup - check if configuration is complete
if (useLocalWhisper) {
return whisperModel !== "" && isModelDownloaded;
} else {
- const trimmedKey = apiKey.trim();
- if (!trimmedKey) {
- return false;
- }
- if (!hasEnteredReasoningBase) {
+ // For cloud mode, check if appropriate API key is set
+ if (cloudTranscriptionProvider === "openai") {
+ return openaiApiKey.trim().length > 0;
+ } else if (cloudTranscriptionProvider === "groq") {
+ return groqApiKey.trim().length > 0;
+ } else if (cloudTranscriptionProvider === "custom") {
+ // Custom can work without API key for local endpoints
return true;
}
- if (!isValidReasoningBase) {
- return false;
- }
- return customReasoningModels.length > 0 && !customModelsLoading && !customModelsError;
+ return openaiApiKey.trim().length > 0; // Default to OpenAI
}
- case 3: {
+ case 2: {
+ // Permissions
if (!permissionsHook.micPermissionGranted) {
return false;
}
@@ -946,9 +557,9 @@ export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) {
}
return true;
}
- case 4:
+ case 3:
return hotkey.trim() !== ""; // Hotkey & Test step
- case 5:
+ case 4:
return agentName.trim() !== ""; // Agent name step (final)
default:
return false;
@@ -971,12 +582,6 @@ export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) {
<div
className="h-screen flex flex-col bg-gradient-to-br from-stone-50 via-white to-blue-50/30"
style={{
- backgroundImage: `repeating-linear-gradient(
- transparent,
- transparent 24px,
- #e7e5e4 24px,
- #e7e5e4 25px
- )`,
paddingTop: "env(safe-area-inset-top, 0px)",
}}
>
@@ -997,8 +602,6 @@ export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) {
description={alertDialog.description}
onOk={() => {}}
/>
- {/* Left margin line for entire page */}
- <div className="fixed left-6 md:left-12 top-0 bottom-0 w-px bg-red-300/40 z-0"></div>
{/* Title Bar */}
<div className="flex-shrink-0 z-10">
diff --git a/src/components/ReasoningModelSelector.tsx b/src/components/ReasoningModelSelector.tsx
index 0561a0cfd..3211eeb7a 100644
--- a/src/components/ReasoningModelSelector.tsx
+++ b/src/components/ReasoningModelSelector.tsx
@@ -11,6 +11,7 @@ import { REASONING_PROVIDERS } from "../models/ModelRegistry";
import { modelRegistry } from "../models/ModelRegistry";
import { getProviderIcon } from "../utils/providerIcons";
import { isSecureEndpoint } from "../utils/urlUtils";
+import { createExternalLinkHandler } from "../utils/externalLinks";
type CloudModelOption = {
value: string;
@@ -304,12 +305,23 @@ export default function ReasoningModelSelector({
const handleApplyCustomBase = useCallback(() => {
const trimmedBase = customBaseInput.trim();
- setCustomBaseInput(trimmedBase);
- setCloudReasoningBaseUrl(trimmedBase);
+ const normalized = trimmedBase ? normalizeBaseUrl(trimmedBase) : trimmedBase;
+ setCustomBaseInput(normalized);
+ setCloudReasoningBaseUrl(normalized);
lastLoadedBaseRef.current = null;
- loadRemoteModels(trimmedBase, true);
+ loadRemoteModels(normalized, true);
}, [customBaseInput, setCloudReasoningBaseUrl, loadRemoteModels]);
+ const handleBaseUrlBlur = useCallback(() => {
+ const trimmedBase = customBaseInput.trim();
+ if (!trimmedBase) return;
+
+ // Auto-apply on blur if changed
+ if (trimmedBase !== (cloudReasoningBaseUrl || "").trim()) {
+ handleApplyCustomBase();
+ }
+ }, [customBaseInput, cloudReasoningBaseUrl, handleApplyCustomBase]);
+
const handleResetCustomBase = useCallback(() => {
const defaultBase = API_ENDPOINTS.OPENAI_BASE;
setCustomBaseInput(defaultBase);
@@ -550,73 +562,94 @@ export default function ReasoningModelSelector({
<div className="p-4">
{selectedCloudProvider === "custom" ? (
<>
+ {/* 1. Endpoint URL - TOP */}
<div className="space-y-3">
- <h4 className="font-medium text-gray-900">Endpoint Settings</h4>
+ <h4 className="font-medium text-gray-900">Endpoint URL</h4>
<Input
value={customBaseInput}
onChange={(event) => setCustomBaseInput(event.target.value)}
+ onBlur={handleBaseUrlBlur}
placeholder="https://api.openai.com/v1"
className="text-sm"
/>
- <div className="flex flex-wrap gap-2">
- <Button
- type="button"
- size="sm"
- variant="outline"
- onClick={handleResetCustomBase}
- >
- Reset to Default
- </Button>
- <Button
- type="button"
- size="sm"
- variant="outline"
- onClick={handleRefreshCustomModels}
- disabled={
- customModelsLoading || (!trimmedCustomBase && !hasSavedCustomBase)
- }
- >
- {customModelsLoading
- ? "Loading models..."
- : isCustomBaseDirty
- ? "Apply & Refresh"
- : "Refresh Models"}
- </Button>
- </div>
- {isCustomBaseDirty && (
- <p className="text-xs text-amber-600">
- Apply the new base URL to refresh models.
- </p>
- )}
- <p className="text-xs text-gray-600">
- We'll query{" "}
- <code>
- {hasCustomBase
- ? `${effectiveReasoningBase}/models`
- : `${defaultOpenAIBase}/models`}
- </code>{" "}
- for available models.
+ <p className="text-xs text-gray-500">
+ Examples:{" "}
+ <code className="text-purple-600">http://localhost:11434/v1</code>{" "}
+ (Ollama),{" "}
+ <code className="text-purple-600">http://localhost:8080/v1</code>{" "}
+ (LocalAI).
</p>
</div>
- <div className="space-y-3 pt-4 border-t border-gray-200">
- <h4 className="font-medium text-gray-900">Authentication</h4>
+ {/* 2. API Key - SECOND */}
+ <div className="space-y-3 pt-4">
+ <h4 className="font-medium text-gray-900">API Key (Optional)</h4>
<ApiKeyInput
apiKey={openaiApiKey}
setApiKey={setOpenaiApiKey}
- helpText="Optional. Added as a Bearer token for your custom endpoint."
+ label=""
+ helpText="Optional. Sent as a Bearer token for authentication."
/>
</div>
- <div className="space-y-3 pt-4 border-t border-gray-200">
- <h4 className="text-sm font-medium text-gray-700">Available Models</h4>
+ {/* 3. Model Selection - THIRD */}
+ <div className="space-y-3 pt-4">
+ <div className="flex items-center justify-between">
+ <h4 className="text-sm font-medium text-gray-700">Available Models</h4>
+ <div className="flex gap-2">
+ <Button
+ type="button"
+ size="sm"
+ variant="outline"
+ onClick={handleResetCustomBase}
+ className="text-xs"
+ >
+ Reset
+ </Button>
+ <Button
+ type="button"
+ size="sm"
+ variant="outline"
+ onClick={handleRefreshCustomModels}
+ disabled={
+ customModelsLoading || (!trimmedCustomBase && !hasSavedCustomBase)
+ }
+ className="text-xs"
+ >
+ {customModelsLoading
+ ? "Loading..."
+ : isCustomBaseDirty
+ ? "Apply & Refresh"
+ : "Refresh"}
+ </Button>
+ </div>
+ </div>
+ <p className="text-xs text-gray-500">
+ We'll query{" "}
+ <code>
+ {hasCustomBase
+ ? `${effectiveReasoningBase}/models`
+ : `${defaultOpenAIBase}/models`}
+ </code>{" "}
+ for available models.
+ </p>
+ {isCustomBaseDirty && (
+ <p className="text-xs text-blue-600">
+ Models will reload when you click away from the URL field or click
+ "Apply & Refresh".
+ </p>
+ )}
{!hasCustomBase && (
- <p className="text-xs text-amber-600">Enter a base URL to load models.</p>
+ <p className="text-xs text-amber-600">
+ Enter an endpoint URL above to load models.
+ </p>
)}
{hasCustomBase && (
<>
{customModelsLoading && (
- <p className="text-xs text-blue-600">Fetching model list...</p>
+ <p className="text-xs text-blue-600">
+ Fetching model list from endpoint...
+ </p>
)}
{customModelsError && (
<p className="text-xs text-red-600">{customModelsError}</p>
@@ -625,7 +658,7 @@ export default function ReasoningModelSelector({
!customModelsError &&
customModelOptions.length === 0 && (
<p className="text-xs text-amber-600">
- No models returned by this endpoint.
+ No models returned. Check your endpoint URL.
</p>
)}
</>
@@ -639,7 +672,110 @@ export default function ReasoningModelSelector({
</>
) : (
<>
- <div className="space-y-3">
+ {/* 1. API Key - TOP */}
+ {selectedCloudProvider === "openai" && (
+ <div className="space-y-3">
+ <div className="flex items-baseline justify-between">
+ <h4 className="font-medium text-gray-900">API Key</h4>
+ <a
+ href="https://platform.openai.com/api-keys"
+ target="_blank"
+ rel="noopener noreferrer"
+ onClick={createExternalLinkHandler(
+ "https://platform.openai.com/api-keys"
+ )}
+ className="text-xs text-blue-600 hover:text-blue-700 underline cursor-pointer"
+ >
+ Get your API key →
+ </a>
+ </div>
+ <ApiKeyInput
+ apiKey={openaiApiKey}
+ setApiKey={setOpenaiApiKey}
+ label=""
+ helpText=""
+ />
+ </div>
+ )}
+
+ {selectedCloudProvider === "anthropic" && (
+ <div className="space-y-3">
+ <div className="flex items-baseline justify-between">
+ <h4 className="font-medium text-gray-900">API Key</h4>
+ <a
+ href="https://console.anthropic.com/settings/keys"
+ target="_blank"
+ rel="noopener noreferrer"
+ onClick={createExternalLinkHandler(
+ "https://console.anthropic.com/settings/keys"
+ )}
+ className="text-xs text-blue-600 hover:text-blue-700 underline cursor-pointer"
+ >
+ Get your API key →
+ </a>
+ </div>
+ <ApiKeyInput
+ apiKey={anthropicApiKey}
+ setApiKey={setAnthropicApiKey}
+ placeholder="sk-ant-..."
+ label=""
+ helpText=""
+ />
+ </div>
+ )}
+
+ {selectedCloudProvider === "gemini" && (
+ <div className="space-y-3">
+ <div className="flex items-baseline justify-between">
+ <h4 className="font-medium text-gray-900">API Key</h4>
+ <a
+ href="https://aistudio.google.com/app/api-keys"
+ target="_blank"
+ rel="noopener noreferrer"
+ onClick={createExternalLinkHandler(
+ "https://aistudio.google.com/app/api-keys"
+ )}
+ className="text-xs text-blue-600 hover:text-blue-700 underline cursor-pointer"
+ >
+ Get your API key →
+ </a>
+ </div>
+ <ApiKeyInput
+ apiKey={geminiApiKey}
+ setApiKey={setGeminiApiKey}
+ placeholder="AIza..."
+ label=""
+ helpText=""
+ />
+ </div>
+ )}
+
+ {selectedCloudProvider === "groq" && (
+ <div className="space-y-3">
+ <div className="flex items-baseline justify-between">
+ <h4 className="font-medium text-gray-900">API Key</h4>
+ <a
+ href="https://console.groq.com/keys"
+ target="_blank"
+ rel="noopener noreferrer"
+ onClick={createExternalLinkHandler("https://console.groq.com/keys")}
+ className="text-xs text-blue-600 hover:text-blue-700 underline cursor-pointer"
+ >
+ Get your API key →
+ </a>
+ </div>
+ <ApiKeyInput
+ apiKey={groqApiKey}
+ setApiKey={setGroqApiKey}
+ placeholder="gsk_..."
+ label=""
+ helpText=""
+ />
+ </div>
+ )}
+
+ {/* 2. Model Selection - BOTTOM */}
+ <div className="pt-4 space-y-3">
<h4 className="text-sm font-medium text-gray-700">Select Model</h4>
<ModelCardList
models={selectedCloudModels}
@@ -647,103 +783,6 @@ export default function ReasoningModelSelector({
onModelSelect={setReasoningModel}
/>
</div>
-
- <div className="mt-4 pt-4 border-t border-gray-200">
- {selectedCloudProvider === "openai" && (
- <div className="space-y-3">
- <h4 className="font-medium text-gray-900">API Configuration</h4>
- <ApiKeyInput
- apiKey={openaiApiKey}
- setApiKey={setOpenaiApiKey}
- helpText={
- <>
- Need an API key?{" "}
- <a
- href="https://platform.openai.com"
- target="_blank"
- rel="noopener noreferrer"
- className="text-blue-600 underline"
- >
- platform.openai.com
- </a>
- </>
- }
- />
- </div>
- )}
-
- {selectedCloudProvider === "anthropic" && (
- <div className="space-y-3">
- <h4 className="font-medium text-gray-900">API Configuration</h4>
- <ApiKeyInput
- apiKey={anthropicApiKey}
- setApiKey={setAnthropicApiKey}
- placeholder="sk-ant-..."
- helpText={
- <>
- Need an API key?{" "}
- <a
- href="https://console.anthropic.com"
- target="_blank"
- rel="noopener noreferrer"
- className="text-blue-600 underline"
- >
- console.anthropic.com
- </a>
- </>
- }
- />
- </div>
- )}
-
- {selectedCloudProvider === "gemini" && (
- <div className="space-y-3">
- <h4 className="font-medium text-gray-900">API Configuration</h4>
- <ApiKeyInput
- apiKey={geminiApiKey}
- setApiKey={setGeminiApiKey}
- placeholder="AIza..."
- helpText={
- <>
- Need an API key?{" "}
- <a
- href="https://makersuite.google.com/app/apikey"
- target="_blank"
- rel="noopener noreferrer"
- className="text-blue-600 underline"
- >
- makersuite.google.com
- </a>
- </>
- }
- />
- </div>
- )}
-
- {selectedCloudProvider === "groq" && (
- <div className="space-y-3">
- <h4 className="font-medium text-gray-900">API Configuration</h4>
- <ApiKeyInput
- apiKey={groqApiKey}
- setApiKey={setGroqApiKey}
- placeholder="gsk_..."
- helpText={
- <>
- Need an API key?{" "}
- <a
- href="https://console.groq.com"
- target="_blank"
- rel="noopener noreferrer"
- className="text-blue-600 underline"
- >
- console.groq.com
- </a>
- </>
- }
- />
- </div>
- )}
- </div>
</>
)}
</div>
diff --git a/src/components/SettingsModal.tsx b/src/components/SettingsModal.tsx
index e153d3148..0c522caed 100644
--- a/src/components/SettingsModal.tsx
+++ b/src/components/SettingsModal.tsx
@@ -12,7 +12,7 @@ export default function SettingsModal({ open, onOpenChange }: SettingsModalProps
const sidebarItems: SidebarItem<SettingsSectionType>[] = [
{ id: "general", label: "General", icon: Settings },
{ id: "transcription", label: "Transcription Mode", icon: Mic },
- { id: "aiModels", label: "AI Models", icon: Brain },
+ { id: "aiModels", label: "AI Text Cleanup", icon: Brain },
{ id: "agentConfig", label: "Agent Configuration", icon: User },
{ id: "prompts", label: "AI Prompts", icon: Sparkles },
{ id: "developer", label: "Troubleshooting", icon: Wrench },
diff --git a/src/components/SettingsPage.tsx b/src/components/SettingsPage.tsx
index b19cbbfca..6b87b755a 100644
--- a/src/components/SettingsPage.tsx
+++ b/src/components/SettingsPage.tsx
@@ -758,10 +758,11 @@ export default function SettingsPage({ activeSection = "general" }: SettingsPage
return (
<div className="space-y-6">
<div>
- <h3 className="text-lg font-semibold text-gray-900 mb-2">AI Prompt Management</h3>
+ <h3 className="text-lg font-semibold text-gray-900 mb-2">Prompt Studio</h3>
<p className="text-sm text-gray-600 mb-6">
- View and customize the prompts that power OpenWhispr's AI text processing. Adjust
- these to change how your transcriptions are formatted and enhanced.
+ OpenWhispr uses a single unified system prompt that handles both text cleanup and
+ instruction detection. View, customize, and test the prompt that powers your AI
+ assistant.
</p>
</div>
diff --git a/src/components/TranscriptionModelPicker.tsx b/src/components/TranscriptionModelPicker.tsx
index 0bd528c4c..8c9c2063d 100644
--- a/src/components/TranscriptionModelPicker.tsx
+++ b/src/components/TranscriptionModelPicker.tsx
@@ -18,6 +18,7 @@ import {
import { MODEL_PICKER_COLORS, type ColorScheme } from "../utils/modelPickerStyles";
import { getProviderIcon } from "../utils/providerIcons";
import { API_ENDPOINTS } from "../config/constants";
+import { createExternalLinkHandler } from "../utils/externalLinks";
interface WhisperModel {
model: string;
@@ -222,8 +223,10 @@ export default function TranscriptionModelPicker({
const provider = cloudProviders.find((p) => p.id === providerId);
if (providerId === "custom") {
- // Don't change base URL or model when switching to custom
- // The user will enter their own URL
+ // Clear model to whisper-1 (standard fallback) to avoid sending
+ // provider-specific models to custom endpoints
+ onCloudModelSelect("whisper-1");
+ // Don't change base URL - user will enter their own
return;
}
@@ -248,6 +251,40 @@ export default function TranscriptionModelPicker({
[onLocalProviderSelect]
);
+ const handleBaseUrlBlur = useCallback(() => {
+ if (!setCloudTranscriptionBaseUrl || selectedCloudProvider !== "custom") return;
+
+ const trimmed = (cloudTranscriptionBaseUrl || "").trim();
+ if (!trimmed) return;
+
+ // Normalize the URL using the existing util from constants
+ const { normalizeBaseUrl } = require("../config/constants");
+ const normalized = normalizeBaseUrl(trimmed);
+
+ if (normalized && normalized !== cloudTranscriptionBaseUrl) {
+ setCloudTranscriptionBaseUrl(normalized);
+ }
+
+ // Auto-detect if this matches a known provider
+ if (normalized) {
+ for (const provider of cloudProviders) {
+ const providerNormalized = normalizeBaseUrl(provider.baseUrl);
+ if (normalized === providerNormalized) {
+ onCloudProviderSelect(provider.id);
+ onCloudModelSelect("whisper-1");
+ break;
+ }
+ }
+ }
+ }, [
+ cloudTranscriptionBaseUrl,
+ selectedCloudProvider,
+ setCloudTranscriptionBaseUrl,
+ onCloudProviderSelect,
+ onCloudModelSelect,
+ cloudProviders,
+ ]);
+
const handleDelete = useCallback(
(modelId: string) => {
showConfirmDialog({
@@ -434,49 +471,54 @@ export default function TranscriptionModelPicker({
return (
<div className={`space-y-4 ${className}`}>
- <div className="grid grid-cols-1 md:grid-cols-2 gap-3">
- <button
- onClick={() => handleModeChange(false)}
- className={`p-4 border-2 rounded-xl text-left transition-all cursor-pointer ${
- !useLocalWhisper
- ? "border-purple-500 bg-purple-50"
- : "border-neutral-200 bg-white hover:border-neutral-300"
- }`}
- >
- <div className="flex items-center justify-between mb-2">
- <div className="flex items-center gap-3">
- <Cloud className="w-6 h-6 text-blue-600" />
- <h4 className="font-medium text-neutral-900">Cloud</h4>
+ {/* Only show mode selector in settings, not in onboarding (which has its own) */}
+ {variant === "settings" && (
+ <div className="grid grid-cols-1 md:grid-cols-2 gap-3">
+ <button
+ onClick={() => handleModeChange(false)}
+ className={`p-4 border-2 rounded-xl text-left transition-all cursor-pointer ${
+ !useLocalWhisper
+ ? "border-purple-500 bg-purple-50"
+ : "border-neutral-200 bg-white hover:border-neutral-300"
+ }`}
+ >
+ <div className="flex items-center justify-between mb-2">
+ <div className="flex items-center gap-3">
+ <Cloud className="w-6 h-6 text-blue-600" />
+ <h4 className="font-medium text-neutral-900">Cloud</h4>
+ </div>
+ <span className="text-xs text-green-600 bg-green-100 px-2 py-1 rounded-full">
+ Fast
+ </span>
</div>
- <span className="text-xs text-green-600 bg-green-100 px-2 py-1 rounded-full">Fast</span>
- </div>
- <p className="text-sm text-neutral-600">
- Transcription via API. Fast and accurate, requires internet.
- </p>
- </button>
-
- <button
- onClick={() => handleModeChange(true)}
- className={`p-4 border-2 rounded-xl text-left transition-all cursor-pointer ${
- useLocalWhisper
- ? "border-purple-500 bg-purple-50"
- : "border-neutral-200 bg-white hover:border-neutral-300"
- }`}
- >
- <div className="flex items-center justify-between mb-2">
- <div className="flex items-center gap-3">
- <Lock className="w-6 h-6 text-purple-600" />
- <h4 className="font-medium text-neutral-900">Local</h4>
+ <p className="text-sm text-neutral-600">
+ Transcription via API. Fast and accurate, requires internet.
+ </p>
+ </button>
+
+ <button
+ onClick={() => handleModeChange(true)}
+ className={`p-4 border-2 rounded-xl text-left transition-all cursor-pointer ${
+ useLocalWhisper
+ ? "border-purple-500 bg-purple-50"
+ : "border-neutral-200 bg-white hover:border-neutral-300"
+ }`}
+ >
+ <div className="flex items-center justify-between mb-2">
+ <div className="flex items-center gap-3">
+ <Lock className="w-6 h-6 text-purple-600" />
+ <h4 className="font-medium text-neutral-900">Local</h4>
+ </div>
+ <span className="text-xs text-blue-600 bg-blue-100 px-2 py-1 rounded-full">
+ Private
+ </span>
</div>
- <span className="text-xs text-blue-600 bg-blue-100 px-2 py-1 rounded-full">
- Private
- </span>
- </div>
- <p className="text-sm text-neutral-600">
- Runs on your device. Complete privacy, works offline.
- </p>
- </button>
- </div>
+ <p className="text-sm text-neutral-600">
+ Runs on your device. Complete privacy, works offline.
+ </p>
+ </button>
+ </div>
+ )}
{!useLocalWhisper ? (
<div className="space-y-4">
@@ -497,30 +539,43 @@ export default function TranscriptionModelPicker({
Custom Endpoint Configuration
</h4>
<p className="text-xs text-gray-500">
- Enter an OpenAI-compatible transcription endpoint URL.
+ Connect to any OpenAI-compatible transcription API.
</p>
</div>
- <div className="space-y-2">
- <label className="block text-sm font-medium text-gray-700">
- Transcription Base URL
- </label>
+ {/* 1. Endpoint URL - TOP */}
+ <div className="space-y-3">
+ <h4 className="font-medium text-gray-900">Endpoint URL</h4>
<Input
value={cloudTranscriptionBaseUrl}
onChange={(e) => setCloudTranscriptionBaseUrl?.(e.target.value)}
+ onBlur={handleBaseUrlBlur}
placeholder="https://your-api.example.com/v1"
className="text-sm"
/>
<p className="text-xs text-gray-500">
- The endpoint should be compatible with OpenAI's{" "}
- <code>/audio/transcriptions</code> API format.
+ Examples: <code className="text-purple-600">http://localhost:11434/v1</code>{" "}
+ (Ollama), <code className="text-purple-600">http://localhost:8080/v1</code>{" "}
+ (LocalAI).
+ <br />
+ Known providers (Groq, OpenAI) will be auto-detected.
</p>
</div>
- <div className="space-y-2">
- <label className="block text-sm font-medium text-gray-700">
- Model Name (optional)
- </label>
+ {/* 2. API Key - SECOND */}
+ <div className="space-y-3 pt-4">
+ <h4 className="font-medium text-gray-900">API Key (Optional)</h4>
+ <ApiKeyInput
+ apiKey={openaiApiKey}
+ setApiKey={setOpenaiApiKey}
+ label=""
+ helpText="Optional. Sent as a Bearer token for authentication."
+ />
+ </div>
+
+ {/* 3. Model Name - THIRD */}
+ <div className="space-y-2 pt-4">
+ <label className="block text-sm font-medium text-gray-700">Model Name</label>
<Input
value={selectedCloudModel}
onChange={(e) => onCloudModelSelect(e.target.value)}
@@ -528,24 +583,44 @@ export default function TranscriptionModelPicker({
className="text-sm"
/>
<p className="text-xs text-gray-500">
- Enter the model name supported by your custom endpoint.
+ The model name supported by your endpoint (defaults to whisper-1).
</p>
</div>
-
- <div className="pt-4 border-t border-gray-200">
- <div className="space-y-3">
- <h4 className="font-medium text-gray-900">API Key</h4>
- <ApiKeyInput
- apiKey={openaiApiKey}
- setApiKey={setOpenaiApiKey}
- helpText="Enter the API key for your custom endpoint."
- />
- </div>
- </div>
</div>
) : (
<>
- <div className="space-y-3">
+ {/* API Configuration First */}
+ <div className="space-y-3 mb-4">
+ <div className="flex items-baseline justify-between">
+ <h4 className="font-medium text-gray-900">API Key</h4>
+ <a
+ href={
+ selectedCloudProvider === "groq"
+ ? "https://console.groq.com/keys"
+ : "https://platform.openai.com/api-keys"
+ }
+ target="_blank"
+ rel="noopener noreferrer"
+ onClick={createExternalLinkHandler(
+ selectedCloudProvider === "groq"
+ ? "https://console.groq.com/keys"
+ : "https://platform.openai.com/api-keys"
+ )}
+ className="text-xs text-blue-600 hover:text-blue-700 underline cursor-pointer"
+ >
+ Get your API key →
+ </a>
+ </div>
+ <ApiKeyInput
+ apiKey={selectedCloudProvider === "groq" ? groqApiKey : openaiApiKey}
+ setApiKey={selectedCloudProvider === "groq" ? setGroqApiKey : setOpenaiApiKey}
+ label=""
+ helpText=""
+ />
+ </div>
+
+ {/* Model Selection Below */}
+ <div className="pt-4 space-y-3">
<h4 className="text-sm font-medium text-gray-700">Select Model</h4>
<ModelCardList
models={cloudModelOptions}
@@ -554,45 +629,6 @@ export default function TranscriptionModelPicker({
colorScheme={colorScheme === "purple" ? "purple" : "indigo"}
/>
</div>
-
- <div className="mt-4 pt-4 border-t border-gray-200">
- <div className="space-y-3">
- <h4 className="font-medium text-gray-900">API Configuration</h4>
- <ApiKeyInput
- apiKey={selectedCloudProvider === "groq" ? groqApiKey : openaiApiKey}
- setApiKey={
- selectedCloudProvider === "groq" ? setGroqApiKey : setOpenaiApiKey
- }
- helpText={
- selectedCloudProvider === "groq" ? (
- <>
- Need an API key?{" "}
- <a
- href="https://console.groq.com"
- target="_blank"
- rel="noopener noreferrer"
- className="text-blue-600 underline"
- >
- console.groq.com
- </a>
- </>
- ) : (
- <>
- Need an API key?{" "}
- <a
- href="https://platform.openai.com"
- target="_blank"
- rel="noopener noreferrer"
- className="text-blue-600 underline"
- >
- platform.openai.com
- </a>
- </>
- )
- }
- />
- </div>
- </div>
</>
)}
</div>
diff --git a/src/components/ui/PromptStudio.tsx b/src/components/ui/PromptStudio.tsx
index d1ec1fdf2..b3b6f7939 100644
--- a/src/components/ui/PromptStudio.tsx
+++ b/src/components/ui/PromptStudio.tsx
@@ -10,15 +10,16 @@ import {
RotateCcw,
Copy,
Sparkles,
- Zap,
TestTube,
AlertTriangle,
+ Info,
} from "lucide-react";
import { AlertDialog } from "./dialog";
import { useDialogs } from "../../hooks/useDialogs";
import { useAgentName } from "../../utils/agentName";
-import ReasoningService, { DEFAULT_PROMPTS } from "../../services/ReasoningService";
+import ReasoningService from "../../services/ReasoningService";
import { getModelProvider } from "../../models/ModelRegistry";
+import { UNIFIED_SYSTEM_PROMPT, LEGACY_PROMPTS } from "../../config/prompts";
interface PromptStudioProps {
className?: string;
@@ -42,12 +43,26 @@ const PROVIDER_CONFIG: Record<string, ProviderConfig> = {
local: { label: "Local" },
};
+/**
+ * Get the current prompt being used - either custom or default unified prompt
+ */
+function getCurrentPrompt(): string {
+ const customPrompt = localStorage.getItem("customUnifiedPrompt");
+ if (customPrompt) {
+ try {
+ return JSON.parse(customPrompt);
+ } catch {
+ return UNIFIED_SYSTEM_PROMPT;
+ }
+ }
+ return UNIFIED_SYSTEM_PROMPT;
+}
+
export default function PromptStudio({ className = "" }: PromptStudioProps) {
const [activeTab, setActiveTab] = useState<"current" | "edit" | "test">("current");
- const [editedAgentPrompt, setEditedAgentPrompt] = useState(DEFAULT_PROMPTS.agent);
- const [editedRegularPrompt, setEditedRegularPrompt] = useState(DEFAULT_PROMPTS.regular);
+ const [editedPrompt, setEditedPrompt] = useState(UNIFIED_SYSTEM_PROMPT);
const [testText, setTestText] = useState(
- "Hey Assistant, make this more professional: This is a test message that needs some work."
+ "um so like I was thinking we should probably you know schedule a meeting for next week to discuss the the project timeline"
);
const [testResult, setTestResult] = useState("");
const [isLoading, setIsLoading] = useState(false);
@@ -55,41 +70,50 @@ export default function PromptStudio({ className = "" }: PromptStudioProps) {
const { alertDialog, showAlertDialog, hideAlertDialog } = useDialogs();
const { agentName } = useAgentName();
- // Load saved custom prompts from localStorage
+ // Load saved custom prompt from localStorage
useEffect(() => {
- const savedPrompts = localStorage.getItem("customPrompts");
- if (savedPrompts) {
+ // Migrate legacy two-prompt system (customPrompts → customUnifiedPrompt)
+ const legacyPrompts = localStorage.getItem("customPrompts");
+ if (legacyPrompts && !localStorage.getItem("customUnifiedPrompt")) {
try {
- const parsed = JSON.parse(savedPrompts);
- setEditedAgentPrompt(parsed.agent || DEFAULT_PROMPTS.agent);
- setEditedRegularPrompt(parsed.regular || DEFAULT_PROMPTS.regular);
+ const parsed = JSON.parse(legacyPrompts);
+ // Use agent prompt as base (it's more comprehensive than regular prompt)
+ if (parsed.agent) {
+ localStorage.setItem("customUnifiedPrompt", JSON.stringify(parsed.agent));
+ localStorage.removeItem("customPrompts");
+ console.log("Migrated legacy custom prompts to unified format");
+ }
+ } catch (e) {
+ console.error("Failed to migrate legacy custom prompts:", e);
+ }
+ }
+
+ // Load current custom prompt
+ const customPrompt = localStorage.getItem("customUnifiedPrompt");
+ if (customPrompt) {
+ try {
+ setEditedPrompt(JSON.parse(customPrompt));
} catch (error) {
- console.error("Failed to load custom prompts:", error);
+ console.error("Failed to load custom prompt:", error);
}
}
}, []);
- const savePrompts = () => {
- const customPrompts = {
- agent: editedAgentPrompt,
- regular: editedRegularPrompt,
- };
-
- localStorage.setItem("customPrompts", JSON.stringify(customPrompts));
+ const savePrompt = () => {
+ localStorage.setItem("customUnifiedPrompt", JSON.stringify(editedPrompt));
showAlertDialog({
- title: "Prompts Saved!",
+ title: "Prompt Saved!",
description:
- "Your custom prompts have been saved and will be used for all future AI processing.",
+ "Your custom prompt has been saved and will be used for all future AI processing.",
});
};
- const resetToDefaults = () => {
- setEditedAgentPrompt(DEFAULT_PROMPTS.agent);
- setEditedRegularPrompt(DEFAULT_PROMPTS.regular);
- localStorage.removeItem("customPrompts");
+ const resetToDefault = () => {
+ setEditedPrompt(UNIFIED_SYSTEM_PROMPT);
+ localStorage.removeItem("customUnifiedPrompt");
showAlertDialog({
title: "Reset Complete",
- description: "Prompts have been reset to default values.",
+ description: "Prompt has been reset to the default value.",
});
};
@@ -100,21 +124,19 @@ export default function PromptStudio({ className = "" }: PromptStudioProps) {
setTestResult("");
try {
- // Check if reasoning model is enabled and if we have the necessary settings
const useReasoningModel = localStorage.getItem("useReasoningModel") === "true";
const reasoningModel = localStorage.getItem("reasoningModel") || "";
- // Determine provider from the model name, falling back to openai if needed
const reasoningProvider = reasoningModel ? getModelProvider(reasoningModel) : "openai";
if (!useReasoningModel) {
setTestResult(
- "⚠️ AI text enhancement is disabled. Enable it in AI Models settings to test prompts."
+ "AI text enhancement is disabled. Enable it in AI Text Cleanup settings to test prompts."
);
return;
}
if (!reasoningModel) {
- setTestResult("⚠️ No reasoning model selected. Choose one in AI Models settings.");
+ setTestResult("No reasoning model selected. Choose one in AI Text Cleanup settings.");
return;
}
@@ -126,66 +148,49 @@ export default function PromptStudio({ className = "" }: PromptStudioProps) {
if (providerConfig.baseStorageKey) {
const baseUrl = (localStorage.getItem(providerConfig.baseStorageKey) || "").trim();
if (!baseUrl) {
- setTestResult(`⚠️ ${providerLabel} base URL missing. Add it in AI Models settings.`);
+ setTestResult(`${providerLabel} base URL missing. Add it in AI Text Cleanup settings.`);
return;
}
}
- // Note: API key validation is handled by ReasoningService which uses Electron IPC
- // to fetch keys from the environment. We skip localStorage validation here.
-
- // Save current prompts temporarily so the test uses them
- const currentCustomPrompts = localStorage.getItem("customPrompts");
- localStorage.setItem(
- "customPrompts",
- JSON.stringify({
- agent: editedAgentPrompt,
- regular: editedRegularPrompt,
- })
- );
+ // Save the current edited prompt temporarily for the test
+ const currentCustomPrompt = localStorage.getItem("customUnifiedPrompt");
+ localStorage.setItem("customUnifiedPrompt", JSON.stringify(editedPrompt));
try {
- // For local models, use a different approach
if (reasoningProvider === "local") {
- // Call local reasoning directly
const result = await window.electronAPI.processLocalReasoning(
testText,
reasoningModel,
agentName,
- {
- customPrompts: {
- agent: editedAgentPrompt,
- regular: editedRegularPrompt,
- },
- }
+ {}
);
if (result.success) {
setTestResult(result.text);
} else {
- setTestResult(`❌ Local model error: ${result.error}`);
+ setTestResult(`Local model error: ${result.error}`);
}
} else {
- // Call the AI - ReasoningService will automatically use the custom prompts
- const result = await ReasoningService.processText(testText, reasoningModel, agentName, {
- customPrompts: {
- agent: editedAgentPrompt,
- regular: editedRegularPrompt,
- },
- });
+ const result = await ReasoningService.processText(
+ testText,
+ reasoningModel,
+ agentName,
+ {}
+ );
setTestResult(result);
}
} finally {
- // Restore original prompts
- if (currentCustomPrompts) {
- localStorage.setItem("customPrompts", currentCustomPrompts);
+ // Restore original prompt
+ if (currentCustomPrompt) {
+ localStorage.setItem("customUnifiedPrompt", currentCustomPrompt);
} else {
- localStorage.removeItem("customPrompts");
+ localStorage.removeItem("customUnifiedPrompt");
}
}
} catch (error) {
console.error("Test failed:", error);
- setTestResult(`❌ Test failed: ${error.message}`);
+ setTestResult(`Test failed: ${error.message}`);
} finally {
setIsLoading(false);
}
@@ -199,16 +204,19 @@ export default function PromptStudio({ className = "" }: PromptStudioProps) {
});
};
- const renderCurrentPrompts = () => (
+ // Check if the test text contains the agent name
+ const isAgentAddressed = testText.toLowerCase().includes(agentName.toLowerCase());
+
+ const renderCurrentPrompt = () => (
<div className="space-y-6">
<div>
<h3 className="text-lg font-semibold mb-4 flex items-center gap-2">
<Eye className="w-5 h-5 text-blue-600" />
- Current AI Prompts
+ Current System Prompt
</h3>
<p className="text-sm text-gray-600 mb-6">
- These are the exact prompts currently being sent to your AI models. Understanding these
- helps you see how OpenWhispr thinks!
+ This is the exact prompt sent to your AI model. It handles both text cleanup and
+ instruction detection in a single, unified approach.
</p>
</div>
@@ -216,40 +224,37 @@ export default function PromptStudio({ className = "" }: PromptStudioProps) {
<CardHeader>
<CardTitle className="flex items-center gap-2 text-base">
<Sparkles className="w-4 h-4 text-purple-600" />
- Agent Mode Prompt (when you say "Hey {agentName}")
+ Unified System Prompt
</CardTitle>
</CardHeader>
<CardContent>
- <div className="bg-gray-50 border rounded-lg p-4 font-mono text-sm">
- <pre className="whitespace-pre-wrap">
- {editedAgentPrompt.replace(/\{\{agentName\}\}/g, agentName)}
- </pre>
+ <div className="bg-blue-50 border border-blue-200 rounded-lg p-4 mb-4">
+ <div className="flex items-start gap-3">
+ <Info className="w-5 h-5 text-blue-600 flex-shrink-0 mt-0.5" />
+ <div className="text-sm text-blue-800">
+ <p className="font-medium mb-1">How it works:</p>
+ <ul className="list-disc list-inside space-y-1 text-blue-700">
+ <li>
+ <strong>Cleanup mode (default)</strong>: Cleans transcribed speech - removes
+ filler words, fixes grammar, punctuation
+ </li>
+ <li>
+ <strong>Instruction mode</strong>: When you directly address "{agentName}" with
+ a command, it executes the instruction AND cleans up the text
+ </li>
+ <li>The AI intelligently detects which mode to use based on context</li>
+ </ul>
+ </div>
+ </div>
</div>
- <Button
- onClick={() => copyPrompt(editedAgentPrompt)}
- variant="outline"
- size="sm"
- className="mt-3"
- >
- <Copy className="w-4 h-4 mr-2" />
- Copy Prompt
- </Button>
- </CardContent>
- </Card>
- <Card>
- <CardHeader>
- <CardTitle className="flex items-center gap-2 text-base">
- <Zap className="w-4 h-4 text-green-600" />
- Regular Mode Prompt (for automatic cleanup)
- </CardTitle>
- </CardHeader>
- <CardContent>
- <div className="bg-gray-50 border rounded-lg p-4 font-mono text-sm">
- <pre className="whitespace-pre-wrap">{editedRegularPrompt}</pre>
+ <div className="bg-gray-50 border rounded-lg p-4 font-mono text-sm max-h-96 overflow-y-auto">
+ <pre className="whitespace-pre-wrap">
+ {getCurrentPrompt().replace(/\{\{agentName\}\}/g, agentName)}
+ </pre>
</div>
<Button
- onClick={() => copyPrompt(editedRegularPrompt)}
+ onClick={() => copyPrompt(getCurrentPrompt())}
variant="outline"
size="sm"
className="mt-3"
@@ -262,57 +267,50 @@ export default function PromptStudio({ className = "" }: PromptStudioProps) {
</div>
);
- const renderEditPrompts = () => (
+ const renderEditPrompt = () => (
<div className="space-y-6">
<div>
<h3 className="text-lg font-semibold mb-4 flex items-center gap-2">
<Edit3 className="w-5 h-5 text-indigo-600" />
- Customize Your AI Prompts
+ Customize System Prompt
</h3>
- <p className="text-sm text-gray-600 mb-6">
- Edit these prompts to change how your AI behaves. Use <code>{"{{agentName}}"}</code> and{" "}
- <code>{"{{text}}"}</code> as placeholders.
+ <p className="text-sm text-gray-600 mb-2">
+ Edit the system prompt to change how your AI processes speech. Use{" "}
+ <code className="bg-gray-100 px-1 rounded">{"{{agentName}}"}</code> as a placeholder for
+ your agent's name.
+ </p>
+ <p className="text-sm text-amber-600 mb-6">
+ <strong>Caution:</strong> Modifying this prompt may affect transcription quality. The
+ default prompt has been carefully crafted for optimal results.
</p>
</div>
<Card>
<CardHeader>
- <CardTitle className="text-base">Agent Mode Prompt</CardTitle>
- </CardHeader>
- <CardContent>
- <Textarea
- value={editedAgentPrompt}
- onChange={(e) => setEditedAgentPrompt(e.target.value)}
- rows={12}
- className="font-mono text-sm"
- placeholder="Enter your custom agent prompt..."
- />
- </CardContent>
- </Card>
-
- <Card>
- <CardHeader>
- <CardTitle className="text-base">Regular Mode Prompt</CardTitle>
+ <CardTitle className="text-base">System Prompt</CardTitle>
</CardHeader>
<CardContent>
<Textarea
- value={editedRegularPrompt}
- onChange={(e) => setEditedRegularPrompt(e.target.value)}
- rows={12}
+ value={editedPrompt}
+ onChange={(e) => setEditedPrompt(e.target.value)}
+ rows={20}
className="font-mono text-sm"
- placeholder="Enter your custom regular prompt..."
+ placeholder="Enter your custom system prompt..."
/>
+ <p className="text-xs text-gray-500 mt-2">
+ Your agent name is: <strong>{agentName}</strong>
+ </p>
</CardContent>
</Card>
<div className="flex gap-3">
- <Button onClick={savePrompts} className="flex-1">
+ <Button onClick={savePrompt} className="flex-1">
<Save className="w-4 h-4 mr-2" />
- Save Custom Prompts
+ Save Custom Prompt
</Button>
- <Button onClick={resetToDefaults} variant="outline">
+ <Button onClick={resetToDefault} variant="outline">
<RotateCcw className="w-4 h-4 mr-2" />
- Reset to Defaults
+ Reset to Default
</Button>
</div>
</div>
@@ -335,10 +333,11 @@ export default function PromptStudio({ className = "" }: PromptStudioProps) {
<div>
<h3 className="text-lg font-semibold mb-4 flex items-center gap-2">
<TestTube className="w-5 h-5 text-green-600" />
- Test Your Prompts
+ Test Your Prompt
</h3>
<p className="text-sm text-gray-600 mb-6">
- Test your custom prompts with the actual AI model to see real results.
+ Test how the AI processes different types of input. Try both regular dictation and
+ addressing your agent directly.
</p>
</div>
@@ -349,7 +348,7 @@ export default function PromptStudio({ className = "" }: PromptStudioProps) {
<div>
<p className="text-sm text-amber-800 font-medium">AI Text Enhancement Disabled</p>
<p className="text-sm text-amber-700 mt-1">
- Enable AI text enhancement in the AI Models settings to test prompts.
+ Enable AI text enhancement in the AI Text Cleanup settings to test prompts.
</p>
</div>
</div>
@@ -361,7 +360,7 @@ export default function PromptStudio({ className = "" }: PromptStudioProps) {
<div className="grid grid-cols-2 gap-4 text-sm">
<div>
<span className="text-gray-600">Current Model:</span>
- <span className="ml-2 font-medium">{reasoningModel}</span>
+ <span className="ml-2 font-medium">{reasoningModel || "None selected"}</span>
</div>
<div>
<span className="text-gray-600">Provider:</span>
@@ -379,24 +378,26 @@ export default function PromptStudio({ className = "" }: PromptStudioProps) {
<Textarea
value={testText}
onChange={(e) => setTestText(e.target.value)}
- rows={3}
- placeholder="Enter text to test with your custom prompts..."
+ rows={4}
+ placeholder="Enter text to test..."
/>
<div className="flex items-center justify-between mt-2">
- <p className="text-xs text-gray-500">
- Try including "{agentName}" in your text to test agent mode prompts
- </p>
+ <div className="text-xs text-gray-500 space-y-1">
+ <p>Try: "um so like I think we should uh schedule a meeting" (cleanup mode)</p>
+ <p>
+ Try: "Hey {agentName}, make this more formal: gonna send the report tomorrow"
+ (instruction mode)
+ </p>
+ </div>
{testText && (
<span
- className={`text-xs px-2 py-1 rounded-full ${
- testText.toLowerCase().includes(agentName.toLowerCase())
+ className={`text-xs px-2 py-1 rounded-full whitespace-nowrap ml-4 ${
+ isAgentAddressed
? "bg-purple-100 text-purple-700"
: "bg-green-100 text-green-700"
}`}
>
- {testText.toLowerCase().includes(agentName.toLowerCase())
- ? "🤖 Agent Mode"
- : "✨ Regular Mode"}
+ {isAgentAddressed ? "May trigger instruction mode" : "Cleanup mode"}
</span>
)}
</div>
@@ -408,24 +409,18 @@ export default function PromptStudio({ className = "" }: PromptStudioProps) {
className="w-full"
>
<Play className="w-4 h-4 mr-2" />
- {isLoading ? "Processing with AI..." : "Test Prompt with AI"}
+ {isLoading ? "Processing..." : "Test with AI"}
</Button>
{testResult && (
<div>
<div className="flex items-center justify-between mb-2">
- <label className="text-sm font-medium">AI Response</label>
+ <label className="text-sm font-medium">AI Output</label>
<Button onClick={() => copyPrompt(testResult)} variant="ghost" size="sm">
<Copy className="w-4 h-4" />
</Button>
</div>
- <div
- className={`border rounded-lg p-4 text-sm max-h-60 overflow-y-auto ${
- testResult.startsWith("⚠️") || testResult.startsWith("❌")
- ? "bg-amber-50 border-amber-200 text-amber-800"
- : "bg-gray-50 border-gray-200"
- }`}
- >
+ <div className="border rounded-lg p-4 text-sm max-h-60 overflow-y-auto bg-gray-50 border-gray-200">
<pre className="whitespace-pre-wrap">{testResult}</pre>
</div>
</div>
@@ -449,7 +444,7 @@ export default function PromptStudio({ className = "" }: PromptStudioProps) {
{/* Tab Navigation */}
<div className="flex border-b border-gray-200 mb-6">
{[
- { id: "current", label: "Current Prompts", icon: Eye },
+ { id: "current", label: "Current Prompt", icon: Eye },
{ id: "edit", label: "Customize", icon: Edit3 },
{ id: "test", label: "Test", icon: TestTube },
].map((tab) => {
@@ -472,8 +467,8 @@ export default function PromptStudio({ className = "" }: PromptStudioProps) {
</div>
{/* Tab Content */}
- {activeTab === "current" && renderCurrentPrompts()}
- {activeTab === "edit" && renderEditPrompts()}
+ {activeTab === "current" && renderCurrentPrompt()}
+ {activeTab === "edit" && renderEditPrompt()}
{activeTab === "test" && renderTestPlayground()}
</div>
);
diff --git a/src/config/promptData.json b/src/config/promptData.json
new file mode 100644
index 000000000..b48516e0c
--- /dev/null
+++ b/src/config/promptData.json
@@ -0,0 +1,7 @@
+{
+ "UNIFIED_SYSTEM_PROMPT": "You are an AI assistant named \"{{agentName}}\", integrated into a speech-to-text dictation application. Your primary function is to process transcribed speech and output clean, polished, well-formatted text.\n\nCORE RESPONSIBILITY:\nYour job is ALWAYS to clean up transcribed speech. This is your default behavior for every input. Cleanup means:\n- Removing filler words (um, uh, er, like, you know, I mean, so, basically) unless they add genuine meaning\n- Fixing grammar, spelling, and punctuation errors\n- Breaking up run-on sentences with appropriate punctuation\n- Removing false starts, stutters, and accidental word repetitions\n- Correcting obvious speech-to-text transcription errors\n- Maintaining the speaker's natural voice, tone, vocabulary, and intent\n- Preserving technical terms, proper nouns, names, and specialized jargon exactly as spoken\n- Keeping the same level of formality (casual speech stays casual, formal stays formal)\n\nSMART FORMATTING:\nApply intelligent formatting based on content context. Use your judgment to make the output readable and well-structured:\n\nBullet points - Use when the user is listing items:\n- Shopping or grocery lists (\"I need to get eggs, milk, bread...\")\n- To-do items (\"I need to remember to call John, send the report, book the flight...\")\n- Multiple points or ideas (\"There are a few things... first... also... and finally...\")\n- Features, benefits, or options being enumerated\n\nNumbered lists - Use when order or sequence matters:\n- Step-by-step instructions (\"First do this, then do that, finally...\")\n- Ranked items or priorities\n- Processes or procedures\n\nParagraph breaks - Add line breaks between:\n- Distinct topics or ideas\n- Natural transitions in thought\n- Different sections of longer content\n\nEmail formatting - When dictating an email:\n- Greeting on its own line\n- Body paragraphs separated by line breaks\n- Closing and signature on separate lines\n\nSocial media / posts - When dictating content for LinkedIn, Twitter, etc:\n- Break into digestible paragraphs\n- Separate the hook/opening from the main content\n- Use line breaks for emphasis and readability\n\nDo NOT over-format. If someone is dictating a simple sentence or two, just output clean text. Only apply formatting when it genuinely improves readability and matches the content type.\n\nWHEN YOU ARE DIRECTLY ADDRESSED:\nSince your name is \"{{agentName}}\", the user may speak to you directly to give instructions. When you detect that the user is addressing YOU with a command or request, you should:\n1. STILL perform cleanup on the relevant content\n2. ALSO execute the instruction they gave you\n3. Remove your name and the instruction itself from the final output\n4. Output only the resulting processed text\n\nExamples of being directly addressed:\n- \"Hey {{agentName}}, make this sound more professional\"\n- \"{{agentName}}, put this in bullet points\"\n- \"Can you rewrite that more formally, {{agentName}}\"\n- \"{{agentName}} summarize what I just said\"\n\nCRITICAL: NOT EVERY MENTION OF YOUR NAME IS AN INSTRUCTION\nIf your name appears but the user is NOT giving you a command, treat it as normal content to clean up:\n- \"I was telling {{agentName}} about the project yesterday\" → Clean this up, keep your name in output\n- \"{{agentName}} is really helpful for dictation\" → Clean this up normally\n- \"My assistant {{agentName}} suggested we try this\" → Clean this up normally\n\nHOW TO TELL THE DIFFERENCE:\n- Direct address typically starts with or includes your name + a verb/action: \"{{agentName}}, make...\", \"Hey {{agentName}}, change...\", \"{{agentName}} please rewrite...\"\n- Talking ABOUT you uses your name as a subject/object in a sentence: \"I told {{agentName}}...\", \"{{agentName}} said...\", \"using {{agentName}} to...\"\n- When genuinely uncertain, default to cleanup-only mode\n\nOUTPUT RULES - THESE ARE ABSOLUTE:\n1. Output ONLY the processed text\n2. NEVER include explanations, commentary, or meta-text\n3. NEVER say things like \"Here's the cleaned up version:\" or \"I've made it more formal:\"\n4. NEVER offer alternatives or ask clarifying questions\n5. NEVER add content that wasn't in the original speech\n6. NEVER use labels, headers, or formatting unless specifically instructed\n7. If the input is empty or just filler words, output nothing\n\nYou are processing transcribed speech, so expect imperfect input. Your goal is to output exactly what the user intended to say, cleaned up and polished, as if they had typed it perfectly themselves.",
+ "LEGACY_PROMPTS": {
+ "agent": "You are {{agentName}}, a helpful AI assistant. Clean up the following dictated text by fixing grammar, punctuation, and formatting. If I give you an instruction, execute it and remove the instruction from the output. Output ONLY the cleaned text:\n\n{{text}}",
+ "regular": "Clean up the following dictated text by fixing grammar, punctuation, and formatting. Output ONLY the cleaned text without any explanations, options, or commentary:\n\n{{text}}"
+ }
+}
diff --git a/src/config/prompts.ts b/src/config/prompts.ts
new file mode 100644
index 000000000..f36392c39
--- /dev/null
+++ b/src/config/prompts.ts
@@ -0,0 +1,39 @@
+import promptData from "./promptData.json";
+
+export const UNIFIED_SYSTEM_PROMPT = promptData.UNIFIED_SYSTEM_PROMPT;
+export const LEGACY_PROMPTS = promptData.LEGACY_PROMPTS;
+
+export function buildPrompt(text: string, agentName: string | null): string {
+ const name = agentName?.trim() || "Assistant";
+ return UNIFIED_SYSTEM_PROMPT.replace(/\{\{agentName\}\}/g, name).replace(/\{\{text\}\}/g, text);
+}
+
+export function getSystemPrompt(agentName: string | null): string {
+ const name = agentName?.trim() || "Assistant";
+
+ let promptTemplate = UNIFIED_SYSTEM_PROMPT;
+ if (typeof window !== "undefined" && window.localStorage) {
+ const customPrompt = window.localStorage.getItem("customUnifiedPrompt");
+ if (customPrompt) {
+ try {
+ promptTemplate = JSON.parse(customPrompt);
+ } catch {
+ // Use default if parsing fails
+ }
+ }
+ }
+
+ return promptTemplate.replace(/\{\{agentName\}\}/g, name);
+}
+
+export function getUserPrompt(text: string): string {
+ return text;
+}
+
+export default {
+ UNIFIED_SYSTEM_PROMPT,
+ buildPrompt,
+ getSystemPrompt,
+ getUserPrompt,
+ LEGACY_PROMPTS,
+};
diff --git a/src/helpers/environment.js b/src/helpers/environment.js
index 3bfbe2276..293547ff1 100644
--- a/src/helpers/environment.js
+++ b/src/helpers/environment.js
@@ -39,10 +39,7 @@ class EnvironmentManager {
}
_saveKey(envVarName, key) {
- // Update the environment variable in memory for immediate use
process.env[envVarName] = key;
- // Persist all keys to file
- this.saveAllKeysToEnvFile();
return { success: true };
}
diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.js
index 0e12bccda..3da782240 100644
--- a/src/helpers/ipcHandlers.js
+++ b/src/helpers/ipcHandlers.js
@@ -2,6 +2,7 @@ const { ipcMain, app, shell, BrowserWindow } = require("electron");
const path = require("path");
const AppUtils = require("../utils");
const debugLogger = require("./debugLogger");
+const { getSystemPrompt } = require("./prompts");
class IPCHandlers {
constructor(managers) {
@@ -10,7 +11,7 @@ class IPCHandlers {
this.clipboardManager = managers.clipboardManager;
this.whisperManager = managers.whisperManager;
this.windowManager = managers.windowManager;
- this.modelManager = managers.modelManager;
+ this.updateManager = managers.updateManager;
this.setupHandlers();
}
@@ -447,6 +448,10 @@ class IPCHandlers {
return this.environmentManager.saveAnthropicKey(key);
});
+ ipcMain.handle("save-all-keys-to-env", async () => {
+ return this.environmentManager.saveAllKeysToEnvFile();
+ });
+
// Local reasoning handler
ipcMain.handle("process-local-reasoning", async (event, text, modelId, agentName, config) => {
try {
@@ -469,12 +474,9 @@ class IPCHandlers {
throw new Error("Anthropic API key not configured");
}
- const systemPrompt =
- "You are a dictation assistant. Clean up text by fixing grammar and punctuation. Output ONLY the cleaned text without any explanations, options, or commentary.";
- const userPrompt =
- agentName && text.toLowerCase().includes(agentName.toLowerCase())
- ? `You are ${agentName}, a helpful AI assistant. Clean up the following dictated text by fixing grammar, punctuation, and formatting. Remove any reference to your name. Output ONLY the cleaned text without explanations or options:\n\n${text}`
- : `Clean up the following dictated text by fixing grammar, punctuation, and formatting. Output ONLY the cleaned text without any explanations, options, or commentary:\n\n${text}`;
+ // Use the unified system prompt - LLM handles agent detection
+ const systemPrompt = getSystemPrompt(agentName);
+ const userPrompt = text;
if (!modelId) {
throw new Error("No model specified for Anthropic API call");
@@ -564,6 +566,48 @@ class IPCHandlers {
}
});
+ // llama-server management handlers
+ ipcMain.handle("llama-server-start", async (event, modelId) => {
+ try {
+ const modelManager = require("./modelManagerBridge").default;
+ const modelInfo = modelManager.findModelById(modelId);
+ if (!modelInfo) {
+ return { success: false, error: `Model "${modelId}" not found` };
+ }
+
+ const modelPath = require("path").join(modelManager.modelsDir, modelInfo.model.fileName);
+
+ await modelManager.serverManager.start(modelPath, {
+ contextSize: modelInfo.model.contextLength || 4096,
+ threads: 4,
+ });
+ modelManager.currentServerModelId = modelId;
+
+ return { success: true, port: modelManager.serverManager.port };
+ } catch (error) {
+ return { success: false, error: error.message };
+ }
+ });
+
+ ipcMain.handle("llama-server-stop", async () => {
+ try {
+ const modelManager = require("./modelManagerBridge").default;
+ await modelManager.stopServer();
+ return { success: true };
+ } catch (error) {
+ return { success: false, error: error.message };
+ }
+ });
+
+ ipcMain.handle("llama-server-status", async () => {
+ try {
+ const modelManager = require("./modelManagerBridge").default;
+ return modelManager.getServerStatus();
+ } catch (error) {
+ return { available: false, running: false, error: error.message };
+ }
+ });
+
ipcMain.handle("get-log-level", async () => {
return debugLogger.getLevel();
});
@@ -710,6 +754,31 @@ class IPCHandlers {
return { success: false, error: error.message };
}
});
+
+ // Update handlers
+ ipcMain.handle("check-for-updates", async () => {
+ return this.updateManager.checkForUpdates();
+ });
+
+ ipcMain.handle("download-update", async () => {
+ return this.updateManager.downloadUpdate();
+ });
+
+ ipcMain.handle("install-update", async () => {
+ return this.updateManager.installUpdate();
+ });
+
+ ipcMain.handle("get-app-version", async () => {
+ return this.updateManager.getAppVersion();
+ });
+
+ ipcMain.handle("get-update-status", async () => {
+ return this.updateManager.getUpdateStatus();
+ });
+
+ ipcMain.handle("get-update-info", async () => {
+ return this.updateManager.getUpdateInfo();
+ });
}
broadcastToWindows(channel, payload) {
diff --git a/src/helpers/llamaCppInstaller.js b/src/helpers/llamaCppInstaller.js
index af3b3efa9..21c59ae50 100644
--- a/src/helpers/llamaCppInstaller.js
+++ b/src/helpers/llamaCppInstaller.js
@@ -3,17 +3,13 @@ const { app } = require("electron");
const path = require("path");
const fs = require("fs");
const { promises: fsPromises } = require("fs");
-const https = require("https");
-const { createWriteStream } = require("fs");
-const tar = require("tar");
-const os = require("os");
-
-// Only import unzipper in main process
-let unzipper;
-if (typeof window === "undefined") {
- unzipper = require("unzipper");
-}
+/**
+ * LlamaCppInstaller - Manages llama-server binary availability
+ *
+ * Note: This module is kept for backwards compatibility and system installation checks.
+ * The bundled llama-server binary is primarily managed by LlamaServerManager.
+ */
class LlamaCppInstaller {
constructor() {
this.installDir = path.join(app.getPath("userData"), "llama-cpp");
@@ -27,15 +23,8 @@ class LlamaCppInstaller {
}
getBinaryName() {
- // Cross-platform binary name resolution
- switch (this.platform) {
- case "win32":
- return "llama-cli.exe";
- case "darwin":
- case "linux":
- default:
- return "llama-cli";
- }
+ // llama-server binary name
+ return this.platform === "win32" ? "llama-server.exe" : "llama-server";
}
getInstalledBinaryPath() {
@@ -45,14 +34,10 @@ class LlamaCppInstaller {
async isInstalled() {
try {
// First check for system installation
- const systemInstalled = await this.checkSystemInstallation();
- if (systemInstalled) {
- // Get the system path
- const systemPath = await this.getSystemBinaryPath();
- if (systemPath) {
- this.binPath = systemPath;
- return true;
- }
+ const systemPath = await this.getSystemBinaryPath();
+ if (systemPath) {
+ this.binPath = systemPath;
+ return true;
}
// Then check for local installation
@@ -69,12 +54,8 @@ class LlamaCppInstaller {
return new Promise((resolve) => {
// Cross-platform command resolution
const checkCmd = this.platform === "win32" ? "where" : "which";
- const binaryNames =
- this.platform === "win32"
- ? ["llama-cli.exe", "llama.exe"]
- : ["llama-cli", "llama", "llama.cpp"];
+ const binaryNames = this.platform === "win32" ? ["llama-server.exe"] : ["llama-server"];
- // Try each possible binary name
let found = false;
let remaining = binaryNames.length;
@@ -115,121 +96,18 @@ class LlamaCppInstaller {
return systemPath !== null;
}
- async getVersion() {
- try {
- const binaryPath = this.binPath || this.getInstalledBinaryPath();
-
- return new Promise((resolve, reject) => {
- const proc = spawn(binaryPath, ["--version"], {
- shell: false,
- stdio: "pipe",
- });
-
- let output = "";
- proc.stdout.on("data", (data) => {
- output += data.toString();
- });
-
- proc.on("close", (code) => {
- if (code === 0) {
- resolve(output.trim());
- } else {
- reject(new Error(`Failed to get version: exit code ${code}`));
- }
- });
-
- proc.on("error", (err) => {
- reject(err);
- });
- });
- } catch (error) {
- throw new Error(`Failed to get llama.cpp version: ${error.message}`);
- }
- }
-
- getReleaseUrl() {
- // Map platform and arch to GitHub release asset names
- const platformMap = {
- darwin: {
- x64: "llama-cli-macos-x64",
- arm64: "llama-cli-macos-arm64",
- },
- linux: {
- x64: "llama-cli-linux-x64",
- arm64: "llama-cli-linux-arm64",
- },
- win32: {
- x64: "llama-cli-windows-x64.exe",
- arm64: "llama-cli-windows-arm64.exe",
- },
- };
-
- const assetName = platformMap[this.platform]?.[this.arch];
- if (!assetName) {
- throw new Error(`Unsupported platform: ${this.platform}-${this.arch}`);
- }
-
- // Using a specific release for stability
- const releaseTag = "v0.1.0"; // Update this as needed
- return `https://github.com/yourusername/llama-cpp-binaries/releases/download/${releaseTag}/${assetName}`;
- }
-
- async downloadBinary(url, destPath) {
- return new Promise((resolve, reject) => {
- const file = createWriteStream(destPath);
-
- https
- .get(url, (response) => {
- if (response.statusCode === 302 || response.statusCode === 301) {
- // Handle redirect
- https
- .get(response.headers.location, (redirectResponse) => {
- redirectResponse.pipe(file);
- file.on("finish", () => {
- file.close();
- resolve();
- });
- })
- .on("error", reject);
- } else if (response.statusCode === 200) {
- response.pipe(file);
- file.on("finish", () => {
- file.close();
- resolve();
- });
- } else {
- reject(new Error(`Failed to download: HTTP ${response.statusCode}`));
- }
- })
- .on("error", reject);
- });
- }
-
async install() {
try {
await this.ensureInstallDir();
- // For now, return a message about manual installation
- // since we don't have a real binary distribution yet
+ // Return a message about manual installation
+ // The bundled binary is managed by LlamaServerManager
return {
success: false,
message:
- "Please install llama.cpp manually using Homebrew (macOS) or from source. Run: brew install llama.cpp",
+ "Please install llama.cpp manually using Homebrew (macOS): brew install llama.cpp, " +
+ "or ensure the llama-server binary is bundled with the app.",
};
-
- // Future implementation:
- // const url = this.getReleaseUrl();
- // const binaryPath = this.getInstalledBinaryPath();
- //
- // await this.downloadBinary(url, binaryPath);
- //
- // // Make executable on Unix-like systems
- // if (this.platform !== "win32") {
- // await fsPromises.chmod(binaryPath, 0o755);
- // }
- //
- // this.binPath = binaryPath;
- // return { success: true, path: binaryPath };
} catch (error) {
return {
success: false,
@@ -253,12 +131,11 @@ class LlamaCppInstaller {
}
async getBinaryPath() {
- // If we already have a path, use it
if (this.binPath) {
return this.binPath;
}
- // Check for system installation first
+ // Check for system installation
const systemPath = await this.getSystemBinaryPath();
if (systemPath) {
this.binPath = systemPath;
diff --git a/src/helpers/llamaServer.js b/src/helpers/llamaServer.js
new file mode 100644
index 000000000..a262c713f
--- /dev/null
+++ b/src/helpers/llamaServer.js
@@ -0,0 +1,387 @@
+const { spawn } = require("child_process");
+const fs = require("fs");
+const net = require("net");
+const os = require("os");
+const path = require("path");
+const http = require("http");
+const debugLogger = require("./debugLogger");
+const { killProcess } = require("../utils/process");
+
+const PORT_RANGE_START = 8200;
+const PORT_RANGE_END = 8220;
+const STARTUP_TIMEOUT_MS = 60000;
+const HEALTH_CHECK_INTERVAL_MS = 5000;
+const HEALTH_CHECK_TIMEOUT_MS = 2000;
+const STARTUP_POLL_INTERVAL_MS = 500;
+const HEALTH_CHECK_FAILURE_THRESHOLD = 3;
+
+class LlamaServerManager {
+ constructor() {
+ this.process = null;
+ this.port = null;
+ this.ready = false;
+ this.modelPath = null;
+ this.startupPromise = null;
+ this.healthCheckInterval = null;
+ this.healthCheckFailures = 0;
+ this.cachedServerBinaryPath = null;
+ }
+
+ getServerBinaryPath() {
+ if (this.cachedServerBinaryPath) return this.cachedServerBinaryPath;
+
+ const platform = process.platform;
+ const arch = process.arch;
+ const platformArch = `${platform}-${arch}`;
+ const binaryName =
+ platform === "win32" ? `llama-server-${platformArch}.exe` : `llama-server-${platformArch}`;
+ const genericName = platform === "win32" ? "llama-server.exe" : "llama-server";
+
+ const candidates = [];
+
+ // Production: check resourcesPath
+ if (process.resourcesPath) {
+ candidates.push(
+ path.join(process.resourcesPath, "bin", binaryName),
+ path.join(process.resourcesPath, "bin", genericName)
+ );
+ }
+
+ // Development: check relative to this file
+ 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;
+
+ // Already running with same model
+ if (this.ready && this.modelPath === modelPath) return;
+
+ // Stop existing server if running
+ 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("llama-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),
+ "--ctx-size",
+ String(options.contextSize || 4096),
+ "--threads",
+ String(options.threads || 4),
+ ];
+
+ // Add GPU layers if specified
+ if (options.gpuLayers) {
+ args.push("--n-gpu-layers", String(options.gpuLayers));
+ }
+
+ debugLogger.debug("Starting llama-server", { port: this.port, modelPath, args });
+
+ this.process = spawn(serverBinary, args, {
+ stdio: ["ignore", "pipe", "pipe"],
+ windowsHide: true,
+ cwd: os.tmpdir(),
+ });
+
+ let stderrBuffer = "";
+ let exitCode = null;
+
+ this.process.stdout.on("data", (data) => {
+ debugLogger.debug("llama-server stdout", { data: data.toString().trim() });
+ });
+
+ this.process.stderr.on("data", (data) => {
+ stderrBuffer += data.toString();
+ debugLogger.debug("llama-server stderr", { data: data.toString().trim() });
+ });
+
+ this.process.on("error", (error) => {
+ debugLogger.error("llama-server process error", { error: error.message });
+ this.ready = false;
+ });
+
+ this.process.on("close", (code) => {
+ exitCode = code;
+ debugLogger.debug("llama-server process exited", { code });
+ this.ready = false;
+ this.process = null;
+ this.stopHealthCheck();
+ });
+
+ await this.waitForReady(() => ({ stderr: stderrBuffer, exitCode }));
+ this.startHealthCheck();
+
+ debugLogger.info("llama-server started successfully", {
+ port: this.port,
+ model: path.basename(modelPath),
+ });
+ }
+
+ async waitForReady(getProcessInfo) {
+ const startTime = Date.now();
+ let pollCount = 0;
+
+ 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, 500) : "";
+ const details = stderr || (info.exitCode !== null ? `exit code: ${info.exitCode}` : "");
+ throw new Error(`llama-server process died during startup${details ? `: ${details}` : ""}`);
+ }
+
+ pollCount++;
+ if (await this.checkHealth()) {
+ this.ready = true;
+ debugLogger.debug("llama-server ready", {
+ startupTimeMs: Date.now() - startTime,
+ pollCount,
+ });
+ return;
+ }
+
+ await new Promise((resolve) => setTimeout(resolve, STARTUP_POLL_INTERVAL_MS));
+ }
+
+ throw new Error(`llama-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: "/health",
+ method: "GET",
+ timeout: HEALTH_CHECK_TIMEOUT_MS,
+ },
+ (res) => {
+ resolve(res.statusCode === 200);
+ res.resume();
+ }
+ );
+
+ req.on("error", () => resolve(false));
+ req.on("timeout", () => {
+ req.destroy();
+ resolve(false);
+ });
+ req.end();
+ });
+ }
+
+ startHealthCheck() {
+ this.stopHealthCheck();
+ this.healthCheckFailures = 0;
+ this.healthCheckInterval = setInterval(async () => {
+ try {
+ if (!this.process) {
+ this.stopHealthCheck();
+ return;
+ }
+ if (await this.checkHealth()) {
+ this.healthCheckFailures = 0;
+ } else {
+ this.healthCheckFailures++;
+ if (this.healthCheckFailures >= HEALTH_CHECK_FAILURE_THRESHOLD) {
+ debugLogger.warn("llama-server health check failed", {
+ consecutiveFailures: this.healthCheckFailures,
+ });
+ this.ready = false;
+ }
+ }
+ } catch (err) {
+ debugLogger.error("Health check error", { error: err.message });
+ }
+ }, HEALTH_CHECK_INTERVAL_MS);
+ }
+
+ stopHealthCheck() {
+ if (this.healthCheckInterval) {
+ clearInterval(this.healthCheckInterval);
+ this.healthCheckInterval = null;
+ }
+ }
+
+ async inference(messages, options = {}) {
+ if (!this.ready || !this.process) {
+ throw new Error("llama-server is not running");
+ }
+
+ const body = JSON.stringify({
+ messages,
+ temperature: options.temperature ?? 0.7,
+ max_tokens: options.max_tokens ?? 512,
+ stream: false,
+ });
+
+ return new Promise((resolve, reject) => {
+ const startTime = Date.now();
+
+ const req = http.request(
+ {
+ hostname: "127.0.0.1",
+ port: this.port,
+ path: "/v1/chat/completions",
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ "Content-Length": Buffer.byteLength(body),
+ },
+ timeout: 300000, // 5 minute timeout for inference
+ },
+ (res) => {
+ let data = "";
+ res.on("data", (chunk) => {
+ data += chunk;
+ });
+ res.on("end", () => {
+ debugLogger.debug("llama-server inference completed", {
+ statusCode: res.statusCode,
+ elapsed: Date.now() - startTime,
+ });
+
+ if (res.statusCode !== 200) {
+ reject(new Error(`llama-server returned status ${res.statusCode}: ${data}`));
+ return;
+ }
+
+ try {
+ const response = JSON.parse(data);
+ // Extract text from OpenAI-compatible response
+ const text = response.choices?.[0]?.message?.content || "";
+ resolve(text.trim());
+ } catch (e) {
+ reject(new Error(`Failed to parse llama-server response: ${e.message}`));
+ }
+ });
+ }
+ );
+
+ req.on("error", (error) => {
+ reject(new Error(`llama-server request failed: ${error.message}`));
+ });
+ req.on("timeout", () => {
+ req.destroy();
+ reject(new Error("llama-server request timed out"));
+ });
+
+ req.write(body);
+ req.end();
+ });
+ }
+
+ async stop() {
+ this.stopHealthCheck();
+
+ if (!this.process) {
+ this.ready = false;
+ return;
+ }
+
+ debugLogger.debug("Stopping llama-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 llama-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, ".gguf") : null,
+ };
+ }
+}
+
+module.exports = LlamaServerManager;
diff --git a/src/helpers/modelManagerBridge.js b/src/helpers/modelManagerBridge.js
index 97528850f..3264f42e4 100644
--- a/src/helpers/modelManagerBridge.js
+++ b/src/helpers/modelManagerBridge.js
@@ -1,5 +1,4 @@
const path = require("path");
-const { spawn } = require("child_process");
const fs = require("fs");
const { promises: fsPromises } = require("fs");
const https = require("https");
@@ -7,7 +6,7 @@ const { pipeline } = require("stream");
const { app } = require("electron");
const modelRegistryData = require("../models/modelRegistryData.json");
-const { parseLlamaCppOutput } = require("../utils/llamaOutputParser");
+const LlamaServerManager = require("./llamaServer");
const debugLogger = require("./debugLogger");
const MIN_FILE_SIZE = 1_000_000; // 1MB minimum for valid model files
@@ -37,7 +36,8 @@ class ModelManager {
this.downloadProgress = new Map();
this.activeDownloads = new Map();
this.activeRequests = new Map(); // Track HTTP requests for cancellation
- this.llamaCppPath = null;
+ this.serverManager = new LlamaServerManager();
+ this.currentServerModelId = null;
this.ensureModelsDirExists();
}
@@ -54,6 +54,16 @@ class ModelManager {
}
}
+ async ensureLlamaCpp() {
+ if (!this.serverManager.isAvailable()) {
+ throw new ModelError(
+ "llama-server binary not found. Please ensure the app is installed correctly.",
+ "LLAMASERVER_NOT_FOUND"
+ );
+ }
+ return true;
+ }
+
async getAllModels() {
try {
const models = [];
@@ -408,27 +418,6 @@ class ModelManager {
}
}
- async ensureLlamaCpp() {
- debugLogger.logReasoning("ENSURE_LLAMACPP_START", {});
-
- const llamaCppInstaller = require("./llamaCppInstaller").default;
-
- const isInstalled = await llamaCppInstaller.isInstalled();
- debugLogger.logReasoning("ENSURE_LLAMACPP_CHECK", { isInstalled });
-
- if (!isInstalled) {
- debugLogger.logReasoning("ENSURE_LLAMACPP_NOT_INSTALLED", {});
- console.error("[LocalAI] llama.cpp is not installed");
- throw new ModelError("llama.cpp is not installed", "LLAMACPP_NOT_INSTALLED");
- }
-
- this.llamaCppPath = await llamaCppInstaller.getBinaryPath();
- debugLogger.logReasoning("ENSURE_LLAMACPP_SUCCESS", {
- llamaCppPath: this.llamaCppPath,
- });
- return true;
- }
-
async runInference(modelId, prompt, options = {}) {
const startTime = Date.now();
debugLogger.logReasoning("INFERENCE_START", {
@@ -437,15 +426,18 @@ class ModelManager {
options: { ...options, systemPrompt: options.systemPrompt ? "[set]" : "[not set]" },
});
- await this.ensureLlamaCpp();
- debugLogger.logReasoning("INFERENCE_LLAMACPP_READY", {
- llamaCppPath: this.llamaCppPath,
- });
+ // Ensure server is available
+ if (!this.serverManager.isAvailable()) {
+ debugLogger.logReasoning("INFERENCE_SERVER_NOT_AVAILABLE", {});
+ throw new ModelError(
+ "llama-server binary not found. Please ensure the app is installed correctly.",
+ "LLAMASERVER_NOT_FOUND"
+ );
+ }
const modelInfo = this.findModelById(modelId);
if (!modelInfo) {
debugLogger.logReasoning("INFERENCE_MODEL_NOT_FOUND", { modelId });
- console.error(`[LocalAI] Model not found: ${modelId}`);
throw new ModelNotFoundError(modelId);
}
@@ -458,141 +450,103 @@ class ModelManager {
if (!(await this.checkModelValid(modelPath))) {
debugLogger.logReasoning("INFERENCE_MODEL_INVALID", { modelId, modelPath });
- console.error(`[LocalAI] Model invalid or not downloaded: ${modelPath}`);
throw new ModelError(
`Model ${modelId} is not downloaded or is corrupted`,
"MODEL_NOT_DOWNLOADED",
- {
- modelId,
- }
+ { modelId }
);
}
- const formattedPrompt = this.formatPrompt(
- modelInfo.provider,
- prompt,
- options.systemPrompt || ""
- );
+ // Start/restart server if needed or if model changed
+ if (!this.serverManager.ready || this.currentServerModelId !== modelId) {
+ debugLogger.logReasoning("INFERENCE_STARTING_SERVER", {
+ currentModel: this.currentServerModelId,
+ requestedModel: modelId,
+ serverReady: this.serverManager.ready,
+ });
- debugLogger.logReasoning("INFERENCE_PROMPT_FORMATTED", {
- formattedPromptLength: formattedPrompt.length,
- promptPreview:
- formattedPrompt.substring(0, 200) + (formattedPrompt.length > 200 ? "..." : ""),
- });
+ await this.serverManager.start(modelPath, {
+ contextSize: options.contextSize || modelInfo.model.contextLength || 4096,
+ threads: options.threads || 4,
+ });
+ this.currentServerModelId = modelId;
- return new Promise((resolve, reject) => {
- const args = [
- "-m",
- modelPath,
- "-p",
- formattedPrompt,
- "-n",
- String(options.maxTokens || 512),
- "--temp",
- String(options.temperature || 0.7),
- "--top-k",
- String(options.topK || 40),
- "--top-p",
- String(options.topP || 0.9),
- "--repeat-penalty",
- String(options.repeatPenalty || 1.1),
- "-c",
- String(options.contextSize || modelInfo.model.contextLength),
- "-t",
- String(options.threads || 4),
- "--no-display-prompt",
- "-no-cnv", // Disable conversation mode (model waits for stdin otherwise)
- ];
-
- debugLogger.logReasoning("INFERENCE_SPAWN", {
- command: this.llamaCppPath,
- argsCount: args.length,
- maxTokens: options.maxTokens || 512,
- contextSize: options.contextSize || modelInfo.model.contextLength,
+ debugLogger.logReasoning("INFERENCE_SERVER_STARTED", {
+ port: this.serverManager.port,
+ model: modelId,
});
+ }
- const proc = spawn(this.llamaCppPath, args);
- let output = "";
- let error = "";
+ // Build messages for chat completion
+ const messages = [
+ { role: "system", content: options.systemPrompt || "" },
+ { role: "user", content: prompt },
+ ];
- debugLogger.logReasoning("INFERENCE_PROCESS_STARTED", {
- pid: proc.pid,
- });
+ debugLogger.logReasoning("INFERENCE_SENDING_REQUEST", {
+ messageCount: messages.length,
+ systemPromptLength: (options.systemPrompt || "").length,
+ userPromptLength: prompt.length,
+ });
- proc.stdout.on("data", (data) => {
- output += data.toString();
- debugLogger.logReasoning("INFERENCE_STDOUT_CHUNK", {
- chunkLength: data.length,
- totalOutputLength: output.length,
- elapsedMs: Date.now() - startTime,
- });
+ try {
+ const result = await this.serverManager.inference(messages, {
+ temperature: options.temperature ?? 0.7,
+ max_tokens: options.maxTokens ?? 512,
});
- proc.stderr.on("data", (data) => {
- error += data.toString();
- debugLogger.logReasoning("INFERENCE_STDERR_CHUNK", {
- chunkLength: data.length,
- preview: data.toString().substring(0, 200),
- elapsedMs: Date.now() - startTime,
- });
+ const totalTime = Date.now() - startTime;
+ debugLogger.logReasoning("INFERENCE_SUCCESS", {
+ totalTimeMs: totalTime,
+ resultLength: result.length,
+ resultPreview: result.substring(0, 200) + (result.length > 200 ? "..." : ""),
});
- proc.on("close", (code) => {
- const totalTime = Date.now() - startTime;
- debugLogger.logReasoning("INFERENCE_CLOSE", {
- exitCode: code,
- totalTimeMs: totalTime,
- outputLength: output.length,
- errorLength: error.length,
- });
-
- if (code !== 0) {
- debugLogger.logReasoning("INFERENCE_FAILED", {
- code,
- error: error.substring(0, 1000),
- outputPreview: output.substring(0, 500),
- });
- console.error(`[LocalAI] Inference failed (${totalTime}ms): ${error.substring(0, 200)}`);
- reject(
- new ModelError(`Inference failed with code ${code}: ${error}`, "INFERENCE_FAILED", {
- code,
- error,
- })
- );
- } else {
- const parsed = parseLlamaCppOutput(output);
- debugLogger.logReasoning("INFERENCE_SUCCESS", {
- totalTimeMs: totalTime,
- rawOutputLength: output.length,
- parsedOutputLength: parsed.length,
- parsedPreview: parsed.substring(0, 200) + (parsed.length > 200 ? "..." : ""),
- });
- resolve(parsed);
- }
+ return result;
+ } catch (error) {
+ const totalTime = Date.now() - startTime;
+ debugLogger.logReasoning("INFERENCE_FAILED", {
+ totalTimeMs: totalTime,
+ error: error.message,
});
-
- proc.on("error", (err) => {
- const totalTime = Date.now() - startTime;
- debugLogger.logReasoning("INFERENCE_PROCESS_ERROR", {
- error: err.message,
- stack: err.stack,
- totalTimeMs: totalTime,
- });
- console.error(`[LocalAI] Process error: ${err.message}`);
- reject(
- new ModelError(`Failed to start inference: ${err.message}`, "INFERENCE_START_FAILED", {
- error: err.message,
- })
- );
+ throw new ModelError(`Inference failed: ${error.message}`, "INFERENCE_FAILED", {
+ error: error.message,
});
- });
+ }
+ }
+
+ async stopServer() {
+ await this.serverManager.stop();
+ this.currentServerModelId = null;
+ }
+
+ getServerStatus() {
+ return this.serverManager.getStatus();
}
- formatPrompt(provider, text, systemPrompt) {
- if (provider.promptTemplate) {
- return provider.promptTemplate.replace("{system}", systemPrompt).replace("{user}", text);
+ async prewarmServer(modelId) {
+ if (!modelId) return false;
+
+ const modelInfo = this.findModelById(modelId);
+ if (!modelInfo) return false;
+
+ const modelPath = path.join(this.modelsDir, modelInfo.model.fileName);
+ if (!(await this.checkModelValid(modelPath))) return false;
+
+ if (!this.serverManager.isAvailable()) return false;
+
+ try {
+ await this.serverManager.start(modelPath, {
+ contextSize: modelInfo.model.contextLength || 4096,
+ threads: 4,
+ });
+ this.currentServerModelId = modelId;
+ debugLogger.info("llama-server pre-warmed", { modelId });
+ return true;
+ } catch (error) {
+ debugLogger.warn("Failed to pre-warm llama-server", { error: error.message });
+ return false;
}
- return `${systemPrompt}\n\n${text}`;
}
}
diff --git a/src/helpers/prompts.js b/src/helpers/prompts.js
new file mode 100644
index 000000000..c71632cde
--- /dev/null
+++ b/src/helpers/prompts.js
@@ -0,0 +1,19 @@
+const promptData = require("../config/promptData.json");
+
+const UNIFIED_SYSTEM_PROMPT = promptData.UNIFIED_SYSTEM_PROMPT;
+
+function getSystemPrompt(agentName) {
+ const name = (agentName && agentName.trim()) || "Assistant";
+ return UNIFIED_SYSTEM_PROMPT.replace(/\{\{agentName\}\}/g, name);
+}
+
+function buildPrompt(text, agentName) {
+ const systemPrompt = getSystemPrompt(agentName);
+ return `${systemPrompt}\n\n${text}`;
+}
+
+module.exports = {
+ UNIFIED_SYSTEM_PROMPT,
+ getSystemPrompt,
+ buildPrompt,
+};
diff --git a/src/helpers/whisper.js b/src/helpers/whisper.js
index cdc93e3b0..fabbb50a4 100644
--- a/src/helpers/whisper.js
+++ b/src/helpers/whisper.js
@@ -1,12 +1,9 @@
-const { spawn } = require("child_process");
const { app } = require("electron");
const fs = require("fs");
const fsPromises = require("fs").promises;
const os = require("os");
const path = require("path");
-const crypto = require("crypto");
const https = require("https");
-const { runCommand, killProcess, TIMEOUTS } = require("../utils/process");
const debugLogger = require("./debugLogger");
const WhisperServerManager = require("./whisperServer");
@@ -43,14 +40,12 @@ const WHISPER_MODELS = {
class WhisperManager {
constructor() {
- this.cachedBinaryPath = null;
this.cachedFFmpegPath = null;
this.currentDownloadProcess = null;
this.ffmpegAvailabilityCache = { result: null, expiresAt: 0 };
this.isInitialized = false;
- // Server manager for HTTP-based transcription (faster for repeated use)
+ // Server manager for HTTP-based transcription
this.serverManager = new WhisperServerManager();
- this.useServerMode = true; // Prefer server mode when available
this.currentServerModel = null;
}
@@ -73,150 +68,10 @@ class WhisperManager {
return path.join(this.getModelsDir(), `ggml-${modelName}.bin`);
}
- getBundledBinaryPath() {
- const platform = process.platform;
- const arch = process.arch;
- const platformArch = `${platform}-${arch}`;
-
- // Binary naming matches download-whisper-cpp.js output: whisper-cpp-darwin-arm64, whisper-cpp-win32-x64.exe
- const platformBinaryName =
- platform === "win32" ? `whisper-cpp-${platformArch}.exe` : `whisper-cpp-${platformArch}`;
- const genericBinaryName = platform === "win32" ? "whisper-cpp.exe" : "whisper-cpp";
-
- const candidates = [];
-
- // Production: extraResources copies to {resourcesPath}/bin/ via from/to mapping
- if (process.resourcesPath) {
- // Primary location: extraResources with "to": "bin/"
- candidates.push(
- path.join(process.resourcesPath, "bin", platformBinaryName),
- path.join(process.resourcesPath, "bin", genericBinaryName)
- );
- }
-
- // Development: check project resources
- candidates.push(
- path.join(__dirname, "..", "..", "resources", "bin", platformBinaryName),
- path.join(__dirname, "..", "..", "resources", "bin", genericBinaryName)
- );
-
- for (const candidate of candidates) {
- if (fs.existsSync(candidate)) {
- try {
- const stats = fs.statSync(candidate);
- debugLogger.debug("Found whisper.cpp binary", {
- path: candidate,
- size: stats.size,
- });
- return candidate;
- } catch (statError) {
- debugLogger.warn("Binary exists but cannot be accessed", {
- path: candidate,
- error: statError.message,
- });
- }
- }
- }
-
- debugLogger.warn("whisper.cpp binary not found", {
- platform,
- arch,
- searchedPaths: candidates,
- });
- return null;
- }
-
- async getSystemBinaryPath() {
- // On Linux, avoid "whisper" as it conflicts with Python's openai-whisper package
- const binaryNames =
- process.platform === "win32"
- ? ["whisper-cpp.exe", "whisper.exe"]
- : process.platform === "linux"
- ? ["whisper-cpp", "main"]
- : ["whisper-cpp", "whisper", "main"];
-
- for (const name of binaryNames) {
- try {
- const checkCmd = process.platform === "win32" ? "where" : "which";
- const { output } = await runCommand(checkCmd, [name], { timeout: TIMEOUTS.QUICK_CHECK });
- const binaryPath = output.trim().split("\n")[0];
- if (
- binaryPath &&
- fs.existsSync(binaryPath) &&
- (await this.isWhisperCppBinary(binaryPath))
- ) {
- return binaryPath;
- }
- } catch {
- continue;
- }
- }
-
- // Check common Homebrew paths on macOS
- if (process.platform === "darwin") {
- const commonPaths = [
- "/opt/homebrew/bin/whisper-cpp",
- "/opt/homebrew/bin/whisper",
- "/usr/local/bin/whisper-cpp",
- "/usr/local/bin/whisper",
- ];
- for (const p of commonPaths) {
- if (fs.existsSync(p) && (await this.isWhisperCppBinary(p))) {
- return p;
- }
- }
- }
-
- return null;
- }
-
- async isWhisperCppBinary(binaryPath) {
- try {
- // whisper.cpp uses "-m FNAME" and "-f FNAME" flags; Python whisper uses different args
- const { output } = await runCommand(binaryPath, ["--help"], {
- timeout: TIMEOUTS.QUICK_CHECK,
- });
- return (
- output.includes("-m FNAME") ||
- output.includes("-f FNAME") ||
- output.includes("--model FNAME")
- );
- } catch {
- return false;
- }
- }
-
- async getWhisperBinaryPath() {
- // Use cached path if file still exists (binary type already validated during discovery)
- if (this.cachedBinaryPath && fs.existsSync(this.cachedBinaryPath)) {
- return this.cachedBinaryPath;
- }
- this.cachedBinaryPath = null;
-
- // Priority: bundled > system
- let binaryPath = this.getBundledBinaryPath();
-
- if (!binaryPath) {
- binaryPath = await this.getSystemBinaryPath();
- }
-
- if (binaryPath) {
- this.cachedBinaryPath = binaryPath;
- debugLogger.log("Using whisper.cpp binary:", binaryPath);
- }
-
- return binaryPath;
- }
-
- clearBinaryCache() {
- this.cachedBinaryPath = null;
- }
-
async initializeAtStartup(settings = {}) {
const startTime = Date.now();
try {
- await this.getWhisperBinaryPath();
this.isInitialized = true;
// Pre-warm whisper-server if local mode enabled (eliminates 2-5s cold-start delay)
@@ -285,10 +140,6 @@ class WhisperManager {
available: this.serverManager.isAvailable(),
path: this.serverManager.getServerBinaryPath(),
},
- whisperCli: {
- available: this.cachedBinaryPath !== null,
- path: this.cachedBinaryPath,
- },
ffmpeg: {
available: false,
path: null,
@@ -327,7 +178,6 @@ class WhisperManager {
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
@@ -335,14 +185,12 @@ class WhisperManager {
: "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" };
}
@@ -375,18 +223,16 @@ class WhisperManager {
}
async checkWhisperInstallation() {
- const binaryPath = await this.getWhisperBinaryPath();
- if (!binaryPath) {
+ const serverPath = this.serverManager.getServerBinaryPath();
+ if (!serverPath) {
return { installed: false, working: false };
}
- // Verify binary works
- try {
- await runCommand(binaryPath, ["--help"], { timeout: TIMEOUTS.QUICK_CHECK });
- return { installed: true, working: true, path: binaryPath };
- } catch (error) {
- return { installed: true, working: false, error: error.message };
- }
+ return {
+ installed: true,
+ working: this.serverManager.isAvailable(),
+ path: serverPath,
+ };
}
async transcribeLocalWhisper(audioBlob, options = {}) {
@@ -394,11 +240,17 @@ class WhisperManager {
options,
audioBlobType: audioBlob?.constructor?.name,
audioBlobSize: audioBlob?.byteLength || audioBlob?.size || 0,
- serverMode: this.useServerMode,
serverAvailable: this.serverManager.isAvailable(),
serverReady: this.serverManager.ready,
});
+ // Server mode required
+ if (!this.serverManager.isAvailable()) {
+ throw new Error(
+ "whisper-server binary not found. Please ensure the app is installed correctly."
+ );
+ }
+
const model = options.model || "base";
const language = options.language || null;
const modelPath = this.getModelPath(model);
@@ -408,20 +260,7 @@ class WhisperManager {
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);
+ return await this.transcribeViaServer(audioBlob, model, language);
}
async transcribeViaServer(audioBlob, model, language) {
@@ -472,292 +311,6 @@ class WhisperManager {
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);
- } catch (error) {
- // Exit code 2 typically means argument error - possibly wrong binary (Python whisper vs whisper.cpp)
- if (error.message?.includes("code 2") && this.cachedBinaryPath) {
- debugLogger.debug("Transcription failed with code 2, clearing cache and retrying", {
- binaryPath,
- });
- this.clearBinaryCache();
- const retryBinaryPath = await this.getWhisperBinaryPath();
- if (retryBinaryPath && retryBinaryPath !== binaryPath) {
- debugLogger.log("Retrying with different binary:", retryBinaryPath);
- const result = await this.runWhisperProcess(
- retryBinaryPath,
- tempAudioPath,
- modelPath,
- language
- );
- return this.parseWhisperResult(result);
- }
- }
- throw error;
- } finally {
- await this.cleanupTempFile(tempAudioPath);
- }
- }
-
- async createTempAudioFile(audioBlob) {
- const tempDir = os.tmpdir();
- const uniqueId = crypto.randomUUID();
-
- debugLogger.logAudioData("createTempAudioFile", audioBlob);
-
- let buffer;
- if (audioBlob instanceof ArrayBuffer) {
- buffer = Buffer.from(audioBlob);
- } else if (audioBlob instanceof Uint8Array) {
- buffer = Buffer.from(audioBlob);
- } else if (typeof audioBlob === "string") {
- buffer = Buffer.from(audioBlob, "base64");
- } else if (audioBlob && audioBlob.buffer) {
- buffer = Buffer.from(audioBlob.buffer);
- } else {
- throw new Error(`Unsupported audio data type: ${typeof audioBlob}`);
- }
-
- if (!buffer || buffer.length === 0) {
- throw new Error("Audio buffer is empty - no audio data received");
- }
-
- const MIN_AUDIO_SIZE = 44;
- if (buffer.length < MIN_AUDIO_SIZE) {
- throw new Error(`Audio data too small (${buffer.length} bytes) - recording may have failed`);
- }
-
- // Check if the audio is already in WAV format (starts with "RIFF" header)
- const isWav =
- buffer.length >= 4 &&
- buffer[0] === 0x52 &&
- buffer[1] === 0x49 &&
- buffer[2] === 0x46 &&
- buffer[3] === 0x46;
-
- if (isWav) {
- // Already WAV format - write directly
- const tempAudioPath = path.join(tempDir, `whisper_audio_${uniqueId}.wav`);
- await fsPromises.writeFile(tempAudioPath, buffer);
- const stats = await fsPromises.stat(tempAudioPath);
- debugLogger.logWhisperPipeline("Temp audio file created (WAV passthrough)", {
- path: tempAudioPath,
- size: stats.size,
- });
- return tempAudioPath;
- }
-
- // Audio is in WebM/Opus or other format - convert to WAV using FFmpeg
- const inputPath = path.join(tempDir, `whisper_input_${uniqueId}.webm`);
- const outputPath = path.join(tempDir, `whisper_audio_${uniqueId}.wav`);
-
- await fsPromises.writeFile(inputPath, buffer);
-
- const ffmpegPath = await this.getFFmpegPath();
- if (!ffmpegPath) {
- // Clean up input file
- await fsPromises.unlink(inputPath).catch(() => {});
- throw new Error("FFmpeg not found - required for audio format conversion");
- }
-
- debugLogger.logWhisperPipeline("Converting audio to WAV format", {
- inputPath,
- outputPath,
- ffmpegPath,
- inputSize: buffer.length,
- });
-
- try {
- // Convert to 16kHz mono WAV (optimal for Whisper)
- await new Promise((resolve, reject) => {
- const ffmpegProcess = spawn(
- ffmpegPath,
- [
- "-i",
- inputPath,
- "-ar",
- "16000", // 16kHz sample rate
- "-ac",
- "1", // Mono
- "-c:a",
- "pcm_s16le", // 16-bit PCM
- "-y", // Overwrite output
- outputPath,
- ],
- {
- stdio: ["ignore", "pipe", "pipe"],
- windowsHide: true,
- }
- );
-
- let stderrData = "";
- let settled = false;
-
- const settle = (resolver, value) => {
- if (settled) return;
- settled = true;
- clearTimeout(timeout);
- resolver(value);
- };
-
- // Timeout after 30 seconds
- const timeout = setTimeout(() => {
- ffmpegProcess.kill("SIGTERM");
- settle(reject, new Error("FFmpeg conversion timed out"));
- }, 30000);
-
- ffmpegProcess.stderr.on("data", (data) => {
- stderrData += data.toString();
- });
-
- ffmpegProcess.on("close", (code) => {
- if (code === 0) {
- settle(resolve, undefined);
- } else {
- settle(reject, new Error(`FFmpeg conversion failed (code ${code}): ${stderrData}`));
- }
- });
-
- ffmpegProcess.on("error", (err) => {
- settle(reject, new Error(`FFmpeg process error: ${err.message}`));
- });
- });
-
- // Clean up input file
- await fsPromises.unlink(inputPath).catch(() => {});
-
- const stats = await fsPromises.stat(outputPath);
- debugLogger.logWhisperPipeline("Audio converted to WAV", {
- path: outputPath,
- size: stats.size,
- });
-
- return outputPath;
- } catch (error) {
- // Clean up both files on error
- await fsPromises.unlink(inputPath).catch(() => {});
- await fsPromises.unlink(outputPath).catch(() => {});
- throw error;
- }
- }
-
- async runWhisperProcess(binaryPath, audioPath, modelPath, language) {
- // whisper.cpp --output-json writes to a file, not stdout
- const outputBasePath = audioPath.replace(/\.[^.]+$/, "");
- const jsonOutputPath = `${outputBasePath}.json`;
-
- const args = [
- "-m",
- modelPath,
- "-f",
- audioPath,
- "--output-json",
- "-of",
- outputBasePath,
- "--no-prints",
- ];
- if (language && language !== "auto") {
- args.push("-l", language);
- }
-
- debugLogger.logProcessStart(binaryPath, args, {});
-
- const cleanupJsonFile = () => fsPromises.unlink(jsonOutputPath).catch(() => {});
-
- const { code, stderr } = await new Promise((resolve, reject) => {
- const whisperProcess = spawn(binaryPath, args, {
- stdio: ["ignore", "pipe", "pipe"],
- windowsHide: true,
- });
-
- let stderrData = "";
- let settled = false;
-
- const settle = (resolver, value) => {
- if (settled) return;
- settled = true;
- clearTimeout(timeout);
- resolver(value);
- };
-
- const timeout = setTimeout(() => {
- killProcess(whisperProcess, "SIGTERM");
- settle(reject, new Error("Whisper transcription timed out"));
- }, TIMEOUTS.TRANSCRIPTION);
-
- whisperProcess.stdout.on("data", (data) =>
- debugLogger.logProcessOutput("Whisper", "stdout", data)
- );
- whisperProcess.stderr.on("data", (data) => {
- stderrData += data.toString();
- debugLogger.logProcessOutput("Whisper", "stderr", data);
- });
-
- whisperProcess.on("close", (exitCode) =>
- settle(resolve, { code: exitCode, stderr: stderrData })
- );
- whisperProcess.on("error", (err) => {
- debugLogger.error("Whisper process spawn failed", {
- error: err.code || err.message,
- binaryPath,
- binaryExists: fs.existsSync(binaryPath),
- });
- settle(reject, new Error(`Failed to start whisper.cpp: ${err.message}`));
- });
- });
-
- debugLogger.logWhisperPipeline("Process closed", {
- code,
- stderrLength: stderr.length,
- jsonOutputPath,
- });
-
- if (code !== 0) {
- await cleanupJsonFile();
- throw new Error(`Whisper transcription failed (code ${code}): ${stderr}`);
- }
-
- // Check stderr for errors - whisper.cpp may exit 0 even on failure
- if (stderr.includes("error:")) {
- await cleanupJsonFile();
- throw new Error(`Whisper processing error: ${stderr}`);
- }
-
- // Verify JSON file was created
- if (!fs.existsSync(jsonOutputPath)) {
- throw new Error(`Whisper did not produce output. stderr: ${stderr || "(empty)"}`);
- }
-
- try {
- const jsonContent = await fsPromises.readFile(jsonOutputPath, "utf-8");
- await cleanupJsonFile();
- return jsonContent;
- } catch (readError) {
- await cleanupJsonFile();
- throw new Error(`Failed to read Whisper output: ${readError.message}`);
- }
- }
-
// Normalize whitespace: replace newlines with spaces and collapse multiple spaces
// whisper.cpp returns text with \n between audio segments which causes formatting issues
normalizeWhitespace(text) {
@@ -814,14 +367,6 @@ class WhisperManager {
return normalized === "[blank_audio]" || normalized === "[ blank_audio ]";
}
- async cleanupTempFile(tempAudioPath) {
- try {
- await fsPromises.unlink(tempAudioPath);
- } catch {
- // Temp file cleanup error is not critical
- }
- }
-
// Model management methods
async downloadWhisperModel(modelName, progressCallback = null) {
this.validateModelName(modelName);
@@ -1165,24 +710,21 @@ class WhisperManager {
debugLogger.warn("Bundled FFmpeg not available", { error: err.message });
}
- // Try system FFmpeg
+ // Try system FFmpeg paths
const systemCandidates =
process.platform === "darwin"
- ? ["ffmpeg", "/opt/homebrew/bin/ffmpeg", "/usr/local/bin/ffmpeg"]
+ ? ["/opt/homebrew/bin/ffmpeg", "/usr/local/bin/ffmpeg"]
: process.platform === "win32"
- ? ["ffmpeg", "C:\\ffmpeg\\bin\\ffmpeg.exe"]
- : ["ffmpeg", "/usr/bin/ffmpeg", "/usr/local/bin/ffmpeg"];
+ ? ["C:\\ffmpeg\\bin\\ffmpeg.exe"]
+ : ["/usr/bin/ffmpeg", "/usr/local/bin/ffmpeg"];
debugLogger.debug("Trying system FFmpeg candidates", { candidates: systemCandidates });
for (const candidate of systemCandidates) {
- try {
- await runCommand(candidate, ["-version"], { timeout: TIMEOUTS.QUICK_CHECK });
+ if (fs.existsSync(candidate)) {
debugLogger.debug("Found system FFmpeg", { path: candidate });
this.cachedFFmpegPath = candidate;
return candidate;
- } catch {
- continue;
}
}
@@ -1215,7 +757,6 @@ class WhisperManager {
resourcesPath: process.resourcesPath || null,
isPackaged: !!process.resourcesPath && !process.resourcesPath.includes("node_modules"),
ffmpeg: { available: false, path: null, error: null },
- whisperBinary: { available: false, path: null, error: null },
whisperServer: { available: false, path: null },
modelsDir: this.getModelsDir(),
models: [],
@@ -1234,24 +775,11 @@ class WhisperManager {
diagnostics.ffmpeg = { available: false, path: null, error: err.message };
}
- // Check whisper binary
- try {
- this.cachedBinaryPath = null; // Clear cache for fresh check
- const whisperPath = await this.getWhisperBinaryPath();
- if (whisperPath) {
- diagnostics.whisperBinary = { available: true, path: whisperPath, error: null };
- } else {
- diagnostics.whisperBinary = { available: false, path: null, error: "Not found" };
- }
- } catch (err) {
- diagnostics.whisperBinary = { available: false, path: null, error: err.message };
- }
-
// Check whisper server
if (this.serverManager) {
- const serverPath = this.serverManager.getBinaryPath?.();
+ const serverPath = this.serverManager.getServerBinaryPath?.();
diagnostics.whisperServer = {
- available: !!serverPath && fs.existsSync(serverPath),
+ available: this.serverManager.isAvailable(),
path: serverPath || null,
};
}
diff --git a/src/hooks/useDebouncedCallback.ts b/src/hooks/useDebouncedCallback.ts
new file mode 100644
index 000000000..ba572c449
--- /dev/null
+++ b/src/hooks/useDebouncedCallback.ts
@@ -0,0 +1,34 @@
+import { useCallback, useRef, useEffect } from "react";
+
+export function useDebouncedCallback<T extends (...args: any[]) => any>(
+ callback: T,
+ delay: number
+): (...args: Parameters<T>) => void {
+ const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
+ const callbackRef = useRef(callback);
+
+ useEffect(() => {
+ callbackRef.current = callback;
+ }, [callback]);
+
+ useEffect(() => {
+ return () => {
+ if (timeoutRef.current) {
+ clearTimeout(timeoutRef.current);
+ }
+ };
+ }, []);
+
+ return useCallback(
+ (...args: Parameters<T>) => {
+ if (timeoutRef.current) {
+ clearTimeout(timeoutRef.current);
+ }
+
+ timeoutRef.current = setTimeout(() => {
+ callbackRef.current(...args);
+ }, delay);
+ },
+ [delay]
+ );
+}
diff --git a/src/hooks/useSettings.ts b/src/hooks/useSettings.ts
index fff7c6542..84b0e0a8b 100644
--- a/src/hooks/useSettings.ts
+++ b/src/hooks/useSettings.ts
@@ -1,5 +1,6 @@
import { useCallback, useEffect, useRef } from "react";
import { useLocalStorage } from "./useLocalStorage";
+import { useDebouncedCallback } from "./useDebouncedCallback";
import { getModelProvider } from "../models/ModelRegistry";
import { API_ENDPOINTS } from "../config/constants";
import ReasoningService from "../services/ReasoningService";
@@ -179,7 +180,15 @@ export function useSettings() {
// Silently ignore sync errors
});
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, []); // Run only once on mount - ref ensures this never runs twice
+ }, []);
+
+ const debouncedPersistToEnv = useDebouncedCallback(() => {
+ if (typeof window !== "undefined" && window.electronAPI?.saveAllKeysToEnv) {
+ window.electronAPI.saveAllKeysToEnv().catch(() => {
+ // Silently ignore persistence errors
+ });
+ }
+ }, 1000);
// Wrapped setters that sync to Electron IPC and invalidate cache
const setOpenaiApiKey = useCallback(
@@ -187,8 +196,9 @@ export function useSettings() {
setOpenaiApiKeyLocal(key);
window.electronAPI?.saveOpenAIKey?.(key);
ReasoningService.clearApiKeyCache("openai");
+ debouncedPersistToEnv();
},
- [setOpenaiApiKeyLocal]
+ [setOpenaiApiKeyLocal, debouncedPersistToEnv]
);
const setAnthropicApiKey = useCallback(
@@ -196,8 +206,9 @@ export function useSettings() {
setAnthropicApiKeyLocal(key);
window.electronAPI?.saveAnthropicKey?.(key);
ReasoningService.clearApiKeyCache("anthropic");
+ debouncedPersistToEnv();
},
- [setAnthropicApiKeyLocal]
+ [setAnthropicApiKeyLocal, debouncedPersistToEnv]
);
const setGeminiApiKey = useCallback(
@@ -205,8 +216,9 @@ export function useSettings() {
setGeminiApiKeyLocal(key);
window.electronAPI?.saveGeminiKey?.(key);
ReasoningService.clearApiKeyCache("gemini");
+ debouncedPersistToEnv();
},
- [setGeminiApiKeyLocal]
+ [setGeminiApiKeyLocal, debouncedPersistToEnv]
);
const setGroqApiKey = useCallback(
@@ -214,8 +226,9 @@ export function useSettings() {
setGroqApiKeyLocal(key);
window.electronAPI?.saveGroqKey?.(key);
ReasoningService.clearApiKeyCache("groq");
+ debouncedPersistToEnv();
},
- [setGroqApiKeyLocal]
+ [setGroqApiKeyLocal, debouncedPersistToEnv]
);
// Hotkey
diff --git a/src/services/BaseReasoningService.ts b/src/services/BaseReasoningService.ts
index 678fb3ea8..a709710f3 100644
--- a/src/services/BaseReasoningService.ts
+++ b/src/services/BaseReasoningService.ts
@@ -1,3 +1,5 @@
+import { getSystemPrompt } from "../config/prompts";
+
export interface ReasoningConfig {
maxTokens?: number;
temperature?: number;
@@ -8,42 +10,26 @@ export abstract class BaseReasoningService {
protected isProcessing = false;
/**
- * Get reasoning prompt
+ * Get the system prompt for reasoning
+ * Now uses the unified prompt from config/prompts.ts
+ */
+ protected getSystemPrompt(agentName: string | null): string {
+ return getSystemPrompt(agentName);
+ }
+
+ /**
+ * Get reasoning prompt (user message portion)
+ * With unified prompts, we just return the text - all instructions are in the system prompt
+ * @deprecated The unified prompt approach puts all instructions in the system prompt
*/
protected getReasoningPrompt(
text: string,
agentName: string | null,
config: ReasoningConfig = {}
): string {
- // Default prompts
- const DEFAULT_AGENT_PROMPT = `You are {{agentName}}, a helpful AI assistant. Clean up the following dictated text by fixing grammar, punctuation, and formatting. Remove any reference to your name. Output ONLY the cleaned text without explanations or options:\n\n{{text}}`;
- const DEFAULT_REGULAR_PROMPT = `Clean up the following dictated text by fixing grammar, punctuation, and formatting. Output ONLY the cleaned text without any explanations, options, or commentary:\n\n{{text}}`;
-
- // Get custom prompts from localStorage if available
- let agentPrompt = DEFAULT_AGENT_PROMPT;
- let regularPrompt = DEFAULT_REGULAR_PROMPT;
-
- if (typeof window !== "undefined" && window.localStorage) {
- const customPrompts = window.localStorage.getItem("customPrompts");
- if (customPrompts) {
- try {
- const parsed = JSON.parse(customPrompts);
- agentPrompt = parsed.agent || DEFAULT_AGENT_PROMPT;
- regularPrompt = parsed.regular || DEFAULT_REGULAR_PROMPT;
- } catch (error) {
- console.error("Failed to parse custom prompts:", error);
- }
- }
- }
-
- // Simple prompt construction
- if (agentName && text.toLowerCase().includes(agentName.toLowerCase())) {
- // Agent-based prompt - replace placeholders
- return agentPrompt.replace(/\{\{agentName\}\}/g, agentName).replace(/\{\{text\}\}/g, text);
- }
-
- // Regular prompt - replace placeholders
- return regularPrompt.replace(/\{\{text\}\}/g, text);
+ // With the unified prompt approach, the user message is just the text
+ // All instructions about cleanup and agent detection are in the system prompt
+ return text;
}
/**
diff --git a/src/services/ReasoningService.ts b/src/services/ReasoningService.ts
index f28350682..954ff6fbd 100644
--- a/src/services/ReasoningService.ts
+++ b/src/services/ReasoningService.ts
@@ -3,13 +3,15 @@ import { BaseReasoningService, ReasoningConfig } from "./BaseReasoningService";
import { SecureCache } from "../utils/SecureCache";
import { withRetry, createApiRetryStrategy } from "../utils/retry";
import { API_ENDPOINTS, TOKEN_LIMITS, buildApiUrl, normalizeBaseUrl } from "../config/constants";
+import { UNIFIED_SYSTEM_PROMPT, LEGACY_PROMPTS, getSystemPrompt } from "../config/prompts";
import logger from "../utils/logger";
import { isSecureEndpoint } from "../utils/urlUtils";
-export const DEFAULT_PROMPTS = {
- agent: `You are {{agentName}}, a helpful AI assistant. Process and improve the following text, removing any reference to your name from the output:\n\n{{text}}\n\nImproved text:`,
- regular: `Process and improve the following text:\n\n{{text}}\n\nImproved text:`,
-};
+/**
+ * @deprecated Use UNIFIED_SYSTEM_PROMPT from ../config/prompts instead
+ * Kept for backwards compatibility with PromptStudio UI
+ */
+export const DEFAULT_PROMPTS = LEGACY_PROMPTS;
class ReasoningService extends BaseReasoningService {
private apiKeyCache: SecureCache<string>;
@@ -207,9 +209,8 @@ class ReasoningService extends BaseReasoningService {
config: ReasoningConfig,
providerName: string
): Promise<string> {
- const systemPrompt =
- "You are a dictation assistant. Clean up text by fixing grammar and punctuation. Output ONLY the cleaned text without any explanations, options, or commentary.";
- const userPrompt = this.getReasoningPrompt(text, agentName, config);
+ const systemPrompt = getSystemPrompt(agentName);
+ const userPrompt = text;
const messages = [
{ role: "system", content: systemPrompt },
@@ -429,9 +430,8 @@ class ReasoningService extends BaseReasoningService {
this.isProcessing = true;
try {
- const systemPrompt =
- "You are a dictation assistant. Clean up text by fixing grammar and punctuation. Output ONLY the cleaned text without any explanations, options, or commentary.";
- const userPrompt = this.getReasoningPrompt(text, agentName, config);
+ const systemPrompt = getSystemPrompt(agentName);
+ const userPrompt = text;
// Build messages array (used by both APIs)
const messages = [
@@ -743,9 +743,8 @@ class ReasoningService extends BaseReasoningService {
this.isProcessing = true;
try {
- const systemPrompt =
- "You are a dictation assistant. Clean up text by fixing grammar and punctuation. Output ONLY the cleaned text without any explanations, options, or commentary.";
- const userPrompt = this.getReasoningPrompt(text, agentName, config);
+ const systemPrompt = getSystemPrompt(agentName);
+ const userPrompt = text;
const requestBody = {
contents: [
diff --git a/src/services/localReasoningBridge.js b/src/services/localReasoningBridge.js
index 87e979b78..3a0e557ed 100644
--- a/src/services/localReasoningBridge.js
+++ b/src/services/localReasoningBridge.js
@@ -1,5 +1,6 @@
const modelManager = require("../helpers/modelManagerBridge").default;
const debugLogger = require("../helpers/debugLogger");
+const { getSystemPrompt } = require("../helpers/prompts");
class LocalReasoningService {
constructor() {
@@ -55,7 +56,7 @@ class LocalReasoningService {
repeatPenalty: config.repeatPenalty || 1.1,
contextSize: config.contextSize || 4096,
threads: config.threads || 4,
- systemPrompt: "You are a helpful AI assistant that processes and improves text.",
+ systemPrompt: getSystemPrompt(agentName),
};
debugLogger.logReasoning("LOCAL_BRIDGE_INFERENCE", {
@@ -99,24 +100,10 @@ class LocalReasoningService {
}
getReasoningPrompt(text, agentName, customPrompts) {
- // Default prompts
- const DEFAULT_AGENT_PROMPT = `You are {{agentName}}, a helpful AI assistant. Process and improve the following text, removing any reference to your name from the output:\n\n{{text}}\n\nImproved text:`;
- const DEFAULT_REGULAR_PROMPT = `Process and improve the following text:\n\n{{text}}\n\nImproved text:`;
-
- let agentPrompt = DEFAULT_AGENT_PROMPT;
- let regularPrompt = DEFAULT_REGULAR_PROMPT;
-
- if (customPrompts) {
- agentPrompt = customPrompts.agent || DEFAULT_AGENT_PROMPT;
- regularPrompt = customPrompts.regular || DEFAULT_REGULAR_PROMPT;
- }
-
- // Check if agent name is mentioned
- if (agentName && text.toLowerCase().includes(agentName.toLowerCase())) {
- return agentPrompt.replace(/\{\{agentName\}\}/g, agentName).replace(/\{\{text\}\}/g, text);
- }
-
- return regularPrompt.replace(/\{\{text\}\}/g, text);
+ // With the unified prompt approach, all instructions are in the system prompt
+ // The user message is just the transcribed text
+ // Agent detection is now handled by the LLM itself based on the system prompt
+ return text;
}
calculateMaxTokens(textLength, minTokens = 100, maxTokens = 2048, multiplier = 2) {
diff --git a/src/types/electron.ts b/src/types/electron.ts
index 21ec64c44..0258aa204 100644
--- a/src/types/electron.ts
+++ b/src/types/electron.ts
@@ -131,25 +131,14 @@ declare global {
createProductionEnvFile: (key: string) => Promise<void>;
getAnthropicKey: () => Promise<string | null>;
saveAnthropicKey: (key: string) => Promise<void>;
+ saveAllKeysToEnv: () => Promise<{ success: boolean; path: string }>;
// Clipboard operations
readClipboard: () => Promise<string>;
writeClipboard: (text: string) => Promise<{ success: boolean }>;
- pasteFromClipboard: () => Promise<{ success: boolean; error?: string }>;
- pasteFromClipboardWithFallback: () => Promise<{ success: boolean; error?: string }>;
checkPasteTools: () => Promise<PasteToolsResult>;
- // Settings
- getSettings: () => Promise<any>;
- updateSettings: (settings: any) => Promise<void>;
-
// Audio
- getAudioDevices: () => Promise<MediaDeviceInfo[]>;
- transcribeAudio: (audioData: ArrayBuffer) => Promise<{
- success: boolean;
- text?: string;
- error?: string;
- }>;
onNoAudioDetected: (callback: (event: any, data?: any) => void) => (() => void) | void;
// Whisper operations (whisper.cpp)
@@ -220,8 +209,6 @@ declare global {
// App management
appQuit: () => Promise<void>;
cleanupApp: () => Promise<{ success: boolean; message: string }>;
- getTranscriptionHistory: () => Promise<any[]>;
- clearTranscriptionHistory: () => Promise<void>;
// Update operations
checkForUpdates: () => Promise<UpdateCheckResult>;
@@ -243,9 +230,6 @@ declare global {
// External URL operations
openExternal: (url: string) => Promise<{ success: boolean; error?: string } | void>;
- // Event listener cleanup
- removeAllListeners: (channel: string) => void;
-
// Hotkey management
updateHotkey: (key: string) => Promise<{ success: boolean; message: string }>;
setHotkeyListeningMode?: (enabled: boolean) => Promise<{ success: boolean }>;
diff --git a/src/updater.js b/src/updater.js
index 3a9f10fb8..414afd399 100644
--- a/src/updater.js
+++ b/src/updater.js
@@ -1,5 +1,4 @@
const { autoUpdater } = require("electron-updater");
-const { ipcMain } = require("electron");
class UpdateManager {
constructor() {
@@ -10,11 +9,9 @@ class UpdateManager {
this.lastUpdateInfo = null;
this.isInstalling = false;
this.isDownloading = false;
- this.ipcHandlers = [];
this.eventListeners = [];
this.setupAutoUpdater();
- this.setupIPCHandlers();
}
setWindows(mainWindow, controlPanelWindow) {
@@ -123,205 +120,170 @@ class UpdateManager {
}
}
- setupIPCHandlers() {
- const handlers = [
- {
- channel: "check-for-updates",
- handler: async () => {
- try {
- if (process.env.NODE_ENV === "development") {
- return {
- updateAvailable: false,
- message: "Update checks are disabled in development mode",
- };
- }
-
- console.log("🔍 Checking for updates...");
- const result = await autoUpdater.checkForUpdates();
-
- if (result?.isUpdateAvailable && result?.updateInfo) {
- console.log("📋 Update available:", result.updateInfo.version);
- console.log(
- "📦 Download size:",
- result.updateInfo.files
- ?.map((f) => `${(f.size / 1024 / 1024).toFixed(2)}MB`)
- .join(", ")
- );
- return {
- updateAvailable: true,
- version: result.updateInfo.version,
- releaseDate: result.updateInfo.releaseDate,
- files: result.updateInfo.files,
- releaseNotes: result.updateInfo.releaseNotes,
- };
- } else {
- console.log("✅ Already on latest version");
- return {
- updateAvailable: false,
- message: "You are running the latest version",
- };
- }
- } catch (error) {
- console.error("❌ Update check error:", error);
- throw error;
- }
- },
- },
- {
- channel: "download-update",
- handler: async () => {
- try {
- if (process.env.NODE_ENV === "development") {
- return {
- success: false,
- message: "Update downloads are disabled in development mode",
- };
- }
-
- if (this.isDownloading) {
- return {
- success: true,
- message: "Download already in progress",
- };
- }
-
- if (this.updateDownloaded) {
- return {
- success: true,
- message: "Update already downloaded. Ready to install.",
- };
- }
-
- this.isDownloading = true;
- console.log("📥 Starting update download...");
- await autoUpdater.downloadUpdate();
- console.log("📥 Download initiated successfully");
-
- return { success: true, message: "Update download started" };
- } catch (error) {
- this.isDownloading = false;
- console.error("❌ Update download error:", error);
- throw error;
- }
- },
- },
- {
- channel: "install-update",
- handler: async () => {
- try {
- if (process.env.NODE_ENV === "development") {
- return {
- success: false,
- message: "Update installation is disabled in development mode",
- };
- }
-
- if (!this.updateDownloaded) {
- return {
- success: false,
- message: "No update available to install",
- };
- }
-
- if (this.isInstalling) {
- return {
- success: false,
- message: "Update installation already in progress",
- };
- }
-
- this.isInstalling = true;
- console.log("🔄 Installing update and restarting...");
-
- const { app } = require("electron");
- app.emit("before-quit");
-
- setTimeout(() => {
- const isSilent = process.platform === "win32";
- autoUpdater.quitAndInstall(isSilent, true);
- }, 100);
-
- return { success: true, message: "Update installation started" };
- } catch (error) {
- this.isInstalling = false;
- console.error("❌ Update installation error:", error);
- throw error;
- }
- },
- },
- {
- channel: "get-app-version",
- handler: async () => {
- try {
- const { app } = require("electron");
- return { version: app.getVersion() };
- } catch (error) {
- console.error("❌ Error getting app version:", error);
- throw error;
- }
- },
- },
- {
- channel: "get-update-status",
- handler: async () => {
- try {
- return {
- updateAvailable: this.updateAvailable,
- updateDownloaded: this.updateDownloaded,
- isDevelopment: process.env.NODE_ENV === "development",
- };
- } catch (error) {
- console.error("❌ Error getting update status:", error);
- throw error;
- }
- },
- },
- {
- channel: "get-update-info",
- handler: async () => {
- try {
- return this.lastUpdateInfo;
- } catch (error) {
- console.error("❌ Error getting update info:", error);
- throw error;
- }
- },
- },
- ];
+ async checkForUpdates() {
+ try {
+ if (process.env.NODE_ENV === "development") {
+ return {
+ updateAvailable: false,
+ message: "Update checks are disabled in development mode",
+ };
+ }
- // Register all handlers and track for cleanup
- handlers.forEach(({ channel, handler }) => {
- ipcMain.handle(channel, handler);
- this.ipcHandlers.push({ channel, handler });
- });
+ console.log("🔍 Checking for updates...");
+ const result = await autoUpdater.checkForUpdates();
+
+ if (result?.isUpdateAvailable && result?.updateInfo) {
+ console.log("📋 Update available:", result.updateInfo.version);
+ console.log(
+ "📦 Download size:",
+ result.updateInfo.files?.map((f) => `${(f.size / 1024 / 1024).toFixed(2)}MB`).join(", ")
+ );
+ return {
+ updateAvailable: true,
+ version: result.updateInfo.version,
+ releaseDate: result.updateInfo.releaseDate,
+ files: result.updateInfo.files,
+ releaseNotes: result.updateInfo.releaseNotes,
+ };
+ } else {
+ console.log("✅ Already on latest version");
+ return {
+ updateAvailable: false,
+ message: "You are running the latest version",
+ };
+ }
+ } catch (error) {
+ console.error("❌ Update check error:", error);
+ throw error;
+ }
+ }
+
+ async downloadUpdate() {
+ try {
+ if (process.env.NODE_ENV === "development") {
+ return {
+ success: false,
+ message: "Update downloads are disabled in development mode",
+ };
+ }
+
+ if (this.isDownloading) {
+ return {
+ success: true,
+ message: "Download already in progress",
+ };
+ }
+
+ if (this.updateDownloaded) {
+ return {
+ success: true,
+ message: "Update already downloaded. Ready to install.",
+ };
+ }
+
+ this.isDownloading = true;
+ console.log("📥 Starting update download...");
+ await autoUpdater.downloadUpdate();
+ console.log("📥 Download initiated successfully");
+
+ return { success: true, message: "Update download started" };
+ } catch (error) {
+ this.isDownloading = false;
+ console.error("❌ Update download error:", error);
+ throw error;
+ }
+ }
+
+ async installUpdate() {
+ try {
+ if (process.env.NODE_ENV === "development") {
+ return {
+ success: false,
+ message: "Update installation is disabled in development mode",
+ };
+ }
+
+ if (!this.updateDownloaded) {
+ return {
+ success: false,
+ message: "No update available to install",
+ };
+ }
+
+ if (this.isInstalling) {
+ return {
+ success: false,
+ message: "Update installation already in progress",
+ };
+ }
+
+ this.isInstalling = true;
+ console.log("🔄 Installing update and restarting...");
+
+ const { app } = require("electron");
+ app.emit("before-quit");
+
+ setTimeout(() => {
+ const isSilent = process.platform === "win32";
+ autoUpdater.quitAndInstall(isSilent, true);
+ }, 100);
+
+ return { success: true, message: "Update installation started" };
+ } catch (error) {
+ this.isInstalling = false;
+ console.error("❌ Update installation error:", error);
+ throw error;
+ }
+ }
+
+ async getAppVersion() {
+ try {
+ const { app } = require("electron");
+ return { version: app.getVersion() };
+ } catch (error) {
+ console.error("❌ Error getting app version:", error);
+ throw error;
+ }
+ }
+
+ async getUpdateStatus() {
+ try {
+ return {
+ updateAvailable: this.updateAvailable,
+ updateDownloaded: this.updateDownloaded,
+ isDevelopment: process.env.NODE_ENV === "development",
+ };
+ } catch (error) {
+ console.error("❌ Error getting update status:", error);
+ throw error;
+ }
+ }
+
+ async getUpdateInfo() {
+ try {
+ return this.lastUpdateInfo;
+ } catch (error) {
+ console.error("❌ Error getting update info:", error);
+ throw error;
+ }
}
- // Method to check for updates on startup
checkForUpdatesOnStartup() {
if (process.env.NODE_ENV !== "development") {
- // Wait a bit for the app to fully initialize
setTimeout(() => {
console.log("🔄 Checking for updates on startup...");
autoUpdater.checkForUpdates().catch((err) => {
console.error("Startup update check failed:", err);
});
- }, 3000); // Reduced from 5s to 3s for better UX
+ }, 3000);
}
}
- // Cleanup method to be called on app quit
cleanup() {
- // Remove event listeners
this.eventListeners.forEach(({ event, handler }) => {
autoUpdater.removeListener(event, handler);
});
this.eventListeners = [];
-
- // Remove IPC handlers
- this.ipcHandlers.forEach(({ channel }) => {
- ipcMain.removeHandler(channel);
- });
- this.ipcHandlers = [];
}
}
diff --git a/src/utils/externalLinks.ts b/src/utils/externalLinks.ts
new file mode 100644
index 000000000..bbc3d7409
--- /dev/null
+++ b/src/utils/externalLinks.ts
@@ -0,0 +1,22 @@
+/**
+ * Opens a URL in the user's default system browser (not inside Electron window)
+ */
+export function openExternalLink(url: string): void {
+ if (window.electronAPI?.openExternal) {
+ window.electronAPI.openExternal(url);
+ } else {
+ // Fallback for non-Electron environments (development, etc.)
+ window.open(url, "_blank", "noopener,noreferrer");
+ }
+}
+
+/**
+ * Returns an onClick handler that opens a URL in the default browser
+ * Prevents default link behavior to avoid opening in Electron window
+ */
+export function createExternalLinkHandler(url: string) {
+ return (e: React.MouseEvent<HTMLAnchorElement>) => {
+ e.preventDefault();
+ openExternalLink(url);
+ };
+}
diff --git a/src/utils/process.js b/src/utils/process.js
index b31dff321..d16d2eac1 100644
--- a/src/utils/process.js
+++ b/src/utils/process.js
@@ -11,15 +11,16 @@ function killProcess(proc, signal = "SIGTERM") {
try {
if (process.platform === "win32") {
- // On Windows, just call kill() without a signal - it terminates the process
- // For SIGKILL equivalent, we use taskkill /F for forceful termination
if (signal === "SIGKILL") {
- spawn("taskkill", ["/pid", proc.pid.toString(), "/f", "/t"], { detached: true });
+ const taskkill = spawn("taskkill", ["/pid", proc.pid.toString(), "/f", "/t"], {
+ stdio: "ignore",
+ windowsHide: true,
+ });
+ taskkill.on("error", () => {});
} else {
proc.kill();
}
} else {
- // On Unix-like systems, use the signal as intended
proc.kill(signal);
}
} catch (e) {
diff --git a/src/utils/urlUtils.ts b/src/utils/urlUtils.ts
index 7cc62b70a..fdf1c1d70 100644
--- a/src/utils/urlUtils.ts
+++ b/src/utils/urlUtils.ts
@@ -1,43 +1,22 @@
-/**
- * URL security utilities - allows HTTP for private networks, requires HTTPS otherwise.
- */
-
function isPrivateHost(hostname: string): boolean {
const h = hostname.toLowerCase().replace(/^\[|\]$/g, "");
- // Loopback
if (h === "localhost" || h === "0.0.0.0" || h.startsWith("127.")) return true;
-
- // IPv6 loopback
if (h === "::1") return true;
-
- // RFC 1918 private ranges
- if (h.startsWith("10.")) return true;
- if (h.startsWith("192.168.")) return true;
+ if (h.startsWith("10.") || h.startsWith("192.168.")) return true;
if (h.startsWith("172.")) {
const octet = parseInt(h.split(".")[1], 10);
if (octet >= 16 && octet <= 31) return true;
}
-
- // Link-local IPv4
if (h.startsWith("169.254.")) return true;
- // IPv6 addresses contain colons - check before matching prefixes
const isIPv6 = h.includes(":");
-
- // Link-local IPv6 (fe80::/10)
- if (isIPv6 && h.startsWith("fe80")) return true;
-
- // IPv6 unique local (fc00::/7)
- if (isIPv6 && (h.startsWith("fc") || h.startsWith("fd"))) return true;
-
- // mDNS/Bonjour (.local domains)
+ if (isIPv6 && (h.startsWith("fe80") || h.startsWith("fc") || h.startsWith("fd"))) return true;
if (h.endsWith(".local")) return true;
return false;
}
-/** Returns true if URL is HTTPS or targets a private/local network. */
export function isSecureEndpoint(url: string): boolean {
try {
const parsed = new URL(url);