Fixing local transcription flow #5
@ -1,14 +1,15 @@
|
|||||||
const { FusesPlugin } = require('@electron-forge/plugin-fuses');
|
const { FusesPlugin } = require("@electron-forge/plugin-fuses");
|
||||||
const { FuseV1Options, FuseVersion } = require('@electron/fuses');
|
const { FuseV1Options, FuseVersion } = require("@electron/fuses");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
packagerConfig: {
|
packagerConfig: {
|
||||||
asar: {
|
asar: {
|
||||||
unpack: '**/{onnxruntime-node,onnxruntime-common,@huggingface/transformers,sharp,@img}/**',
|
unpack:
|
||||||
|
"**/{onnxruntime-node,onnxruntime-common,@huggingface/transformers,sharp,@img}/**",
|
||||||
},
|
},
|
||||||
extraResource: ['./src/assets/SystemAudioDump'],
|
extraResource: ["./src/assets/SystemAudioDump"],
|
||||||
name: 'Mastermind',
|
name: "Mastermind",
|
||||||
icon: 'src/assets/logo',
|
icon: "src/assets/logo",
|
||||||
// use `security find-identity -v -p codesigning` to find your identity
|
// use `security find-identity -v -p codesigning` to find your identity
|
||||||
// for macos signing
|
// for macos signing
|
||||||
// also fuck apple
|
// also fuck apple
|
||||||
@ -27,40 +28,44 @@ module.exports = {
|
|||||||
// teamId: 'your team id',
|
// teamId: 'your team id',
|
||||||
// },
|
// },
|
||||||
},
|
},
|
||||||
rebuildConfig: {},
|
rebuildConfig: {
|
||||||
|
// Ensure onnxruntime-node is rebuilt against Electron's Node.js headers
|
||||||
|
// so the native binding matches the ABI used in packaged builds.
|
||||||
|
onlyModules: ["onnxruntime-node", "sharp"],
|
||||||
|
},
|
||||||
makers: [
|
makers: [
|
||||||
{
|
{
|
||||||
name: '@electron-forge/maker-squirrel',
|
name: "@electron-forge/maker-squirrel",
|
||||||
config: {
|
config: {
|
||||||
name: 'mastermind',
|
name: "mastermind",
|
||||||
productName: 'Mastermind',
|
productName: "Mastermind",
|
||||||
shortcutName: 'Mastermind',
|
shortcutName: "Mastermind",
|
||||||
createDesktopShortcut: true,
|
createDesktopShortcut: true,
|
||||||
createStartMenuShortcut: true,
|
createStartMenuShortcut: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: '@electron-forge/maker-dmg',
|
name: "@electron-forge/maker-dmg",
|
||||||
platforms: ['darwin'],
|
platforms: ["darwin"],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: '@reforged/maker-appimage',
|
name: "@reforged/maker-appimage",
|
||||||
platforms: ['linux'],
|
platforms: ["linux"],
|
||||||
config: {
|
config: {
|
||||||
options: {
|
options: {
|
||||||
name: 'Mastermind',
|
name: "Mastermind",
|
||||||
productName: 'Mastermind',
|
productName: "Mastermind",
|
||||||
genericName: 'AI Assistant',
|
genericName: "AI Assistant",
|
||||||
description: 'AI assistant for interviews and learning',
|
description: "AI assistant for interviews and learning",
|
||||||
categories: ['Development', 'Education'],
|
categories: ["Development", "Education"],
|
||||||
icon: 'src/assets/logo.png'
|
icon: "src/assets/logo.png",
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
plugins: [
|
plugins: [
|
||||||
{
|
{
|
||||||
name: '@electron-forge/plugin-auto-unpack-natives',
|
name: "@electron-forge/plugin-auto-unpack-natives",
|
||||||
config: {},
|
config: {},
|
||||||
},
|
},
|
||||||
// Fuses are used to enable/disable various Electron functionality
|
// Fuses are used to enable/disable various Electron functionality
|
||||||
|
|||||||
@ -9,7 +9,8 @@
|
|||||||
"package": "electron-forge package",
|
"package": "electron-forge package",
|
||||||
"make": "electron-forge make",
|
"make": "electron-forge make",
|
||||||
"publish": "electron-forge publish",
|
"publish": "electron-forge publish",
|
||||||
"lint": "echo \"No linting configured\""
|
"lint": "echo \"No linting configured\"",
|
||||||
|
"postinstall": "electron-rebuild -f -w onnxruntime-node"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"mastermind",
|
"mastermind",
|
||||||
@ -33,6 +34,7 @@
|
|||||||
"ws": "^8.19.0"
|
"ws": "^8.19.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@electron/rebuild": "^3.7.1",
|
||||||
"@electron-forge/cli": "^7.8.1",
|
"@electron-forge/cli": "^7.8.1",
|
||||||
"@electron-forge/maker-deb": "^7.8.1",
|
"@electron-forge/maker-deb": "^7.8.1",
|
||||||
"@electron-forge/maker-dmg": "^7.8.1",
|
"@electron-forge/maker-dmg": "^7.8.1",
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
206
src/storage.js
206
src/storage.js
@ -1,6 +1,6 @@
|
|||||||
const fs = require('fs');
|
const fs = require("fs");
|
||||||
const path = require('path');
|
const path = require("path");
|
||||||
const os = require('os');
|
const os = require("os");
|
||||||
|
|
||||||
const CONFIG_VERSION = 1;
|
const CONFIG_VERSION = 1;
|
||||||
|
|
||||||
@ -8,38 +8,39 @@ const CONFIG_VERSION = 1;
|
|||||||
const DEFAULT_CONFIG = {
|
const DEFAULT_CONFIG = {
|
||||||
configVersion: CONFIG_VERSION,
|
configVersion: CONFIG_VERSION,
|
||||||
onboarded: false,
|
onboarded: false,
|
||||||
layout: 'normal'
|
layout: "normal",
|
||||||
};
|
};
|
||||||
|
|
||||||
const DEFAULT_CREDENTIALS = {
|
const DEFAULT_CREDENTIALS = {
|
||||||
apiKey: '',
|
apiKey: "",
|
||||||
groqApiKey: '',
|
groqApiKey: "",
|
||||||
openaiCompatibleApiKey: '',
|
openaiCompatibleApiKey: "",
|
||||||
openaiCompatibleBaseUrl: '',
|
openaiCompatibleBaseUrl: "",
|
||||||
openaiCompatibleModel: ''
|
openaiCompatibleModel: "",
|
||||||
};
|
};
|
||||||
|
|
||||||
const DEFAULT_PREFERENCES = {
|
const DEFAULT_PREFERENCES = {
|
||||||
customPrompt: '',
|
customPrompt: "",
|
||||||
selectedProfile: 'interview',
|
selectedProfile: "interview",
|
||||||
selectedLanguage: 'en-US',
|
selectedLanguage: "en-US",
|
||||||
selectedScreenshotInterval: '5',
|
selectedScreenshotInterval: "5",
|
||||||
selectedImageQuality: 'medium',
|
selectedImageQuality: "medium",
|
||||||
advancedMode: false,
|
advancedMode: false,
|
||||||
audioMode: 'speaker_only',
|
audioMode: "speaker_only",
|
||||||
fontSize: 'medium',
|
fontSize: "medium",
|
||||||
backgroundTransparency: 0.8,
|
backgroundTransparency: 0.8,
|
||||||
googleSearchEnabled: false,
|
googleSearchEnabled: false,
|
||||||
responseProvider: 'gemini',
|
responseProvider: "gemini",
|
||||||
ollamaHost: 'http://127.0.0.1:11434',
|
ollamaHost: "http://127.0.0.1:11434",
|
||||||
ollamaModel: 'llama3.1',
|
ollamaModel: "llama3.1",
|
||||||
whisperModel: 'Xenova/whisper-small',
|
whisperModel: "Xenova/whisper-small",
|
||||||
|
whisperDevice: "", // '' = auto-detect, 'cpu' = native, 'wasm' = compatible
|
||||||
};
|
};
|
||||||
|
|
||||||
const DEFAULT_KEYBINDS = null; // null means use system defaults
|
const DEFAULT_KEYBINDS = null; // null means use system defaults
|
||||||
|
|
||||||
const DEFAULT_LIMITS = {
|
const DEFAULT_LIMITS = {
|
||||||
data: [] // Array of { date: 'YYYY-MM-DD', flash: { count }, flashLite: { count }, groq: { 'qwen3-32b': { chars, limit }, 'gpt-oss-120b': { chars, limit }, 'gpt-oss-20b': { chars, limit } }, gemini: { 'gemma-3-27b-it': { chars } } }
|
data: [], // Array of { date: 'YYYY-MM-DD', flash: { count }, flashLite: { count }, groq: { 'qwen3-32b': { chars, limit }, 'gpt-oss-120b': { chars, limit }, 'gpt-oss-20b': { chars, limit } }, gemini: { 'gemma-3-27b-it': { chars } } }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get the config directory path based on OS
|
// Get the config directory path based on OS
|
||||||
@ -47,12 +48,22 @@ function getConfigDir() {
|
|||||||
const platform = os.platform();
|
const platform = os.platform();
|
||||||
let configDir;
|
let configDir;
|
||||||
|
|
||||||
if (platform === 'win32') {
|
if (platform === "win32") {
|
||||||
configDir = path.join(os.homedir(), 'AppData', 'Roaming', 'cheating-daddy-config');
|
configDir = path.join(
|
||||||
} else if (platform === 'darwin') {
|
os.homedir(),
|
||||||
configDir = path.join(os.homedir(), 'Library', 'Application Support', 'cheating-daddy-config');
|
"AppData",
|
||||||
|
"Roaming",
|
||||||
|
"cheating-daddy-config",
|
||||||
|
);
|
||||||
|
} else if (platform === "darwin") {
|
||||||
|
configDir = path.join(
|
||||||
|
os.homedir(),
|
||||||
|
"Library",
|
||||||
|
"Application Support",
|
||||||
|
"cheating-daddy-config",
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
configDir = path.join(os.homedir(), '.config', 'cheating-daddy-config');
|
configDir = path.join(os.homedir(), ".config", "cheating-daddy-config");
|
||||||
}
|
}
|
||||||
|
|
||||||
return configDir;
|
return configDir;
|
||||||
@ -60,34 +71,34 @@ function getConfigDir() {
|
|||||||
|
|
||||||
// File paths
|
// File paths
|
||||||
function getConfigPath() {
|
function getConfigPath() {
|
||||||
return path.join(getConfigDir(), 'config.json');
|
return path.join(getConfigDir(), "config.json");
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCredentialsPath() {
|
function getCredentialsPath() {
|
||||||
return path.join(getConfigDir(), 'credentials.json');
|
return path.join(getConfigDir(), "credentials.json");
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPreferencesPath() {
|
function getPreferencesPath() {
|
||||||
return path.join(getConfigDir(), 'preferences.json');
|
return path.join(getConfigDir(), "preferences.json");
|
||||||
}
|
}
|
||||||
|
|
||||||
function getKeybindsPath() {
|
function getKeybindsPath() {
|
||||||
return path.join(getConfigDir(), 'keybinds.json');
|
return path.join(getConfigDir(), "keybinds.json");
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLimitsPath() {
|
function getLimitsPath() {
|
||||||
return path.join(getConfigDir(), 'limits.json');
|
return path.join(getConfigDir(), "limits.json");
|
||||||
}
|
}
|
||||||
|
|
||||||
function getHistoryDir() {
|
function getHistoryDir() {
|
||||||
return path.join(getConfigDir(), 'history');
|
return path.join(getConfigDir(), "history");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper to read JSON file safely
|
// Helper to read JSON file safely
|
||||||
function readJsonFile(filePath, defaultValue) {
|
function readJsonFile(filePath, defaultValue) {
|
||||||
try {
|
try {
|
||||||
if (fs.existsSync(filePath)) {
|
if (fs.existsSync(filePath)) {
|
||||||
const data = fs.readFileSync(filePath, 'utf8');
|
const data = fs.readFileSync(filePath, "utf8");
|
||||||
return JSON.parse(data);
|
return JSON.parse(data);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -103,7 +114,7 @@ function writeJsonFile(filePath, data) {
|
|||||||
if (!fs.existsSync(dir)) {
|
if (!fs.existsSync(dir)) {
|
||||||
fs.mkdirSync(dir, { recursive: true });
|
fs.mkdirSync(dir, { recursive: true });
|
||||||
}
|
}
|
||||||
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
|
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), "utf8");
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error writing ${filePath}:`, error.message);
|
console.error(`Error writing ${filePath}:`, error.message);
|
||||||
@ -119,7 +130,7 @@ function needsReset() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
const config = JSON.parse(fs.readFileSync(configPath, "utf8"));
|
||||||
return !config.configVersion || config.configVersion !== CONFIG_VERSION;
|
return !config.configVersion || config.configVersion !== CONFIG_VERSION;
|
||||||
} catch {
|
} catch {
|
||||||
return true;
|
return true;
|
||||||
@ -130,7 +141,7 @@ function needsReset() {
|
|||||||
function resetConfigDir() {
|
function resetConfigDir() {
|
||||||
const configDir = getConfigDir();
|
const configDir = getConfigDir();
|
||||||
|
|
||||||
console.log('Resetting config directory...');
|
console.log("Resetting config directory...");
|
||||||
|
|
||||||
// Remove existing directory if it exists
|
// Remove existing directory if it exists
|
||||||
if (fs.existsSync(configDir)) {
|
if (fs.existsSync(configDir)) {
|
||||||
@ -146,7 +157,7 @@ function resetConfigDir() {
|
|||||||
writeJsonFile(getCredentialsPath(), DEFAULT_CREDENTIALS);
|
writeJsonFile(getCredentialsPath(), DEFAULT_CREDENTIALS);
|
||||||
writeJsonFile(getPreferencesPath(), DEFAULT_PREFERENCES);
|
writeJsonFile(getPreferencesPath(), DEFAULT_PREFERENCES);
|
||||||
|
|
||||||
console.log('Config directory initialized with defaults');
|
console.log("Config directory initialized with defaults");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize storage - call this on app startup
|
// Initialize storage - call this on app startup
|
||||||
@ -193,7 +204,7 @@ function setCredentials(credentials) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getApiKey() {
|
function getApiKey() {
|
||||||
return getCredentials().apiKey || '';
|
return getCredentials().apiKey || "";
|
||||||
}
|
}
|
||||||
|
|
||||||
function setApiKey(apiKey) {
|
function setApiKey(apiKey) {
|
||||||
@ -201,7 +212,7 @@ function setApiKey(apiKey) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getGroqApiKey() {
|
function getGroqApiKey() {
|
||||||
return getCredentials().groqApiKey || '';
|
return getCredentials().groqApiKey || "";
|
||||||
}
|
}
|
||||||
|
|
||||||
function setGroqApiKey(groqApiKey) {
|
function setGroqApiKey(groqApiKey) {
|
||||||
@ -211,9 +222,9 @@ function setGroqApiKey(groqApiKey) {
|
|||||||
function getOpenAICompatibleConfig() {
|
function getOpenAICompatibleConfig() {
|
||||||
const creds = getCredentials();
|
const creds = getCredentials();
|
||||||
return {
|
return {
|
||||||
apiKey: creds.openaiCompatibleApiKey || '',
|
apiKey: creds.openaiCompatibleApiKey || "",
|
||||||
baseUrl: creds.openaiCompatibleBaseUrl || '',
|
baseUrl: creds.openaiCompatibleBaseUrl || "",
|
||||||
model: creds.openaiCompatibleModel || ''
|
model: creds.openaiCompatibleModel || "",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -221,7 +232,7 @@ function setOpenAICompatibleConfig(apiKey, baseUrl, model) {
|
|||||||
return setCredentials({
|
return setCredentials({
|
||||||
openaiCompatibleApiKey: apiKey,
|
openaiCompatibleApiKey: apiKey,
|
||||||
openaiCompatibleBaseUrl: baseUrl,
|
openaiCompatibleBaseUrl: baseUrl,
|
||||||
openaiCompatibleModel: model
|
openaiCompatibleModel: model,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -266,7 +277,7 @@ function setLimits(limits) {
|
|||||||
|
|
||||||
function getTodayDateString() {
|
function getTodayDateString() {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
return now.toISOString().split('T')[0]; // YYYY-MM-DD
|
return now.toISOString().split("T")[0]; // YYYY-MM-DD
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTodayLimits() {
|
function getTodayLimits() {
|
||||||
@ -274,21 +285,21 @@ function getTodayLimits() {
|
|||||||
const today = getTodayDateString();
|
const today = getTodayDateString();
|
||||||
|
|
||||||
// Find today's entry
|
// Find today's entry
|
||||||
const todayEntry = limits.data.find(entry => entry.date === today);
|
const todayEntry = limits.data.find((entry) => entry.date === today);
|
||||||
|
|
||||||
if (todayEntry) {
|
if (todayEntry) {
|
||||||
// ensure new fields exist
|
// ensure new fields exist
|
||||||
if(!todayEntry.groq) {
|
if (!todayEntry.groq) {
|
||||||
todayEntry.groq = {
|
todayEntry.groq = {
|
||||||
'qwen3-32b': { chars: 0, limit: 1500000 },
|
"qwen3-32b": { chars: 0, limit: 1500000 },
|
||||||
'gpt-oss-120b': { chars: 0, limit: 600000 },
|
"gpt-oss-120b": { chars: 0, limit: 600000 },
|
||||||
'gpt-oss-20b': { chars: 0, limit: 600000 },
|
"gpt-oss-20b": { chars: 0, limit: 600000 },
|
||||||
'kimi-k2-instruct': { chars: 0, limit: 600000 }
|
"kimi-k2-instruct": { chars: 0, limit: 600000 },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if(!todayEntry.gemini) {
|
if (!todayEntry.gemini) {
|
||||||
todayEntry.gemini = {
|
todayEntry.gemini = {
|
||||||
'gemma-3-27b-it': { chars: 0 }
|
"gemma-3-27b-it": { chars: 0 },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
setLimits(limits);
|
setLimits(limits);
|
||||||
@ -296,20 +307,20 @@ function getTodayLimits() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// No entry for today - clean old entries and create new one
|
// No entry for today - clean old entries and create new one
|
||||||
limits.data = limits.data.filter(entry => entry.date === today);
|
limits.data = limits.data.filter((entry) => entry.date === today);
|
||||||
const newEntry = {
|
const newEntry = {
|
||||||
date: today,
|
date: today,
|
||||||
flash: { count: 0 },
|
flash: { count: 0 },
|
||||||
flashLite: { count: 0 },
|
flashLite: { count: 0 },
|
||||||
groq: {
|
groq: {
|
||||||
'qwen3-32b': { chars: 0, limit: 1500000 },
|
"qwen3-32b": { chars: 0, limit: 1500000 },
|
||||||
'gpt-oss-120b': { chars: 0, limit: 600000 },
|
"gpt-oss-120b": { chars: 0, limit: 600000 },
|
||||||
'gpt-oss-20b': { chars: 0, limit: 600000 },
|
"gpt-oss-20b": { chars: 0, limit: 600000 },
|
||||||
'kimi-k2-instruct': { chars: 0, limit: 600000 }
|
"kimi-k2-instruct": { chars: 0, limit: 600000 },
|
||||||
},
|
},
|
||||||
gemini: {
|
gemini: {
|
||||||
'gemma-3-27b-it': { chars: 0 }
|
"gemma-3-27b-it": { chars: 0 },
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
limits.data.push(newEntry);
|
limits.data.push(newEntry);
|
||||||
setLimits(limits);
|
setLimits(limits);
|
||||||
@ -322,7 +333,7 @@ function incrementLimitCount(model) {
|
|||||||
const today = getTodayDateString();
|
const today = getTodayDateString();
|
||||||
|
|
||||||
// Find or create today's entry
|
// Find or create today's entry
|
||||||
let todayEntry = limits.data.find(entry => entry.date === today);
|
let todayEntry = limits.data.find((entry) => entry.date === today);
|
||||||
|
|
||||||
if (!todayEntry) {
|
if (!todayEntry) {
|
||||||
// Clean old entries and create new one
|
// Clean old entries and create new one
|
||||||
@ -330,18 +341,18 @@ function incrementLimitCount(model) {
|
|||||||
todayEntry = {
|
todayEntry = {
|
||||||
date: today,
|
date: today,
|
||||||
flash: { count: 0 },
|
flash: { count: 0 },
|
||||||
flashLite: { count: 0 }
|
flashLite: { count: 0 },
|
||||||
};
|
};
|
||||||
limits.data.push(todayEntry);
|
limits.data.push(todayEntry);
|
||||||
} else {
|
} else {
|
||||||
// Clean old entries, keep only today
|
// Clean old entries, keep only today
|
||||||
limits.data = limits.data.filter(entry => entry.date === today);
|
limits.data = limits.data.filter((entry) => entry.date === today);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Increment the appropriate model count
|
// Increment the appropriate model count
|
||||||
if (model === 'gemini-2.5-flash') {
|
if (model === "gemini-2.5-flash") {
|
||||||
todayEntry.flash.count++;
|
todayEntry.flash.count++;
|
||||||
} else if (model === 'gemini-2.5-flash-lite') {
|
} else if (model === "gemini-2.5-flash-lite") {
|
||||||
todayEntry.flashLite.count++;
|
todayEntry.flashLite.count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -354,9 +365,9 @@ function incrementCharUsage(provider, model, charCount) {
|
|||||||
|
|
||||||
const limits = getLimits();
|
const limits = getLimits();
|
||||||
const today = getTodayDateString();
|
const today = getTodayDateString();
|
||||||
const todayEntry = limits.data.find(entry => entry.date === today);
|
const todayEntry = limits.data.find((entry) => entry.date === today);
|
||||||
|
|
||||||
if(todayEntry[provider] && todayEntry[provider][model]) {
|
if (todayEntry[provider] && todayEntry[provider][model]) {
|
||||||
todayEntry[provider][model].chars += charCount;
|
todayEntry[provider][model].chars += charCount;
|
||||||
setLimits(limits);
|
setLimits(limits);
|
||||||
}
|
}
|
||||||
@ -370,29 +381,29 @@ function getAvailableModel() {
|
|||||||
// RPD limits: flash = 20, flash-lite = 20
|
// RPD limits: flash = 20, flash-lite = 20
|
||||||
// After both exhausted, fall back to flash (for paid API users)
|
// After both exhausted, fall back to flash (for paid API users)
|
||||||
if (todayLimits.flash.count < 20) {
|
if (todayLimits.flash.count < 20) {
|
||||||
return 'gemini-2.5-flash';
|
return "gemini-2.5-flash";
|
||||||
} else if (todayLimits.flashLite.count < 20) {
|
} else if (todayLimits.flashLite.count < 20) {
|
||||||
return 'gemini-2.5-flash-lite';
|
return "gemini-2.5-flash-lite";
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'gemini-2.5-flash'; // Default to flash for paid API users
|
return "gemini-2.5-flash"; // Default to flash for paid API users
|
||||||
}
|
}
|
||||||
|
|
||||||
function getModelForToday() {
|
function getModelForToday() {
|
||||||
const todayEntry = getTodayLimits();
|
const todayEntry = getTodayLimits();
|
||||||
const groq = todayEntry.groq;
|
const groq = todayEntry.groq;
|
||||||
|
|
||||||
if (groq['qwen3-32b'].chars < groq['qwen3-32b'].limit) {
|
if (groq["qwen3-32b"].chars < groq["qwen3-32b"].limit) {
|
||||||
return 'qwen/qwen3-32b';
|
return "qwen/qwen3-32b";
|
||||||
}
|
}
|
||||||
if (groq['gpt-oss-120b'].chars < groq['gpt-oss-120b'].limit) {
|
if (groq["gpt-oss-120b"].chars < groq["gpt-oss-120b"].limit) {
|
||||||
return 'openai/gpt-oss-120b';
|
return "openai/gpt-oss-120b";
|
||||||
}
|
}
|
||||||
if (groq['gpt-oss-20b'].chars < groq['gpt-oss-20b'].limit) {
|
if (groq["gpt-oss-20b"].chars < groq["gpt-oss-20b"].limit) {
|
||||||
return 'openai/gpt-oss-20b';
|
return "openai/gpt-oss-20b";
|
||||||
}
|
}
|
||||||
if (groq['kimi-k2-instruct'].chars < groq['kimi-k2-instruct'].limit) {
|
if (groq["kimi-k2-instruct"].chars < groq["kimi-k2-instruct"].limit) {
|
||||||
return 'moonshotai/kimi-k2-instruct';
|
return "moonshotai/kimi-k2-instruct";
|
||||||
}
|
}
|
||||||
|
|
||||||
// All limits exhausted
|
// All limits exhausted
|
||||||
@ -419,8 +430,12 @@ function saveSession(sessionId, data) {
|
|||||||
profile: data.profile || existingSession?.profile || null,
|
profile: data.profile || existingSession?.profile || null,
|
||||||
customPrompt: data.customPrompt || existingSession?.customPrompt || null,
|
customPrompt: data.customPrompt || existingSession?.customPrompt || null,
|
||||||
// Conversation data
|
// Conversation data
|
||||||
conversationHistory: data.conversationHistory || existingSession?.conversationHistory || [],
|
conversationHistory:
|
||||||
screenAnalysisHistory: data.screenAnalysisHistory || existingSession?.screenAnalysisHistory || []
|
data.conversationHistory || existingSession?.conversationHistory || [],
|
||||||
|
screenAnalysisHistory:
|
||||||
|
data.screenAnalysisHistory ||
|
||||||
|
existingSession?.screenAnalysisHistory ||
|
||||||
|
[],
|
||||||
};
|
};
|
||||||
return writeJsonFile(sessionPath, sessionData);
|
return writeJsonFile(sessionPath, sessionData);
|
||||||
}
|
}
|
||||||
@ -437,17 +452,19 @@ function getAllSessions() {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const files = fs.readdirSync(historyDir)
|
const files = fs
|
||||||
.filter(f => f.endsWith('.json'))
|
.readdirSync(historyDir)
|
||||||
|
.filter((f) => f.endsWith(".json"))
|
||||||
.sort((a, b) => {
|
.sort((a, b) => {
|
||||||
// Sort by timestamp descending (newest first)
|
// Sort by timestamp descending (newest first)
|
||||||
const tsA = parseInt(a.replace('.json', ''));
|
const tsA = parseInt(a.replace(".json", ""));
|
||||||
const tsB = parseInt(b.replace('.json', ''));
|
const tsB = parseInt(b.replace(".json", ""));
|
||||||
return tsB - tsA;
|
return tsB - tsA;
|
||||||
});
|
});
|
||||||
|
|
||||||
return files.map(file => {
|
return files
|
||||||
const sessionId = file.replace('.json', '');
|
.map((file) => {
|
||||||
|
const sessionId = file.replace(".json", "");
|
||||||
const data = readJsonFile(path.join(historyDir, file), null);
|
const data = readJsonFile(path.join(historyDir, file), null);
|
||||||
if (data) {
|
if (data) {
|
||||||
return {
|
return {
|
||||||
@ -457,13 +474,14 @@ function getAllSessions() {
|
|||||||
messageCount: data.conversationHistory?.length || 0,
|
messageCount: data.conversationHistory?.length || 0,
|
||||||
screenAnalysisCount: data.screenAnalysisHistory?.length || 0,
|
screenAnalysisCount: data.screenAnalysisHistory?.length || 0,
|
||||||
profile: data.profile || null,
|
profile: data.profile || null,
|
||||||
customPrompt: data.customPrompt || null
|
customPrompt: data.customPrompt || null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}).filter(Boolean);
|
})
|
||||||
|
.filter(Boolean);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error reading sessions:', error.message);
|
console.error("Error reading sessions:", error.message);
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -476,7 +494,7 @@ function deleteSession(sessionId) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error deleting session:', error.message);
|
console.error("Error deleting session:", error.message);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -485,14 +503,16 @@ function deleteAllSessions() {
|
|||||||
const historyDir = getHistoryDir();
|
const historyDir = getHistoryDir();
|
||||||
try {
|
try {
|
||||||
if (fs.existsSync(historyDir)) {
|
if (fs.existsSync(historyDir)) {
|
||||||
const files = fs.readdirSync(historyDir).filter(f => f.endsWith('.json'));
|
const files = fs
|
||||||
files.forEach(file => {
|
.readdirSync(historyDir)
|
||||||
|
.filter((f) => f.endsWith(".json"));
|
||||||
|
files.forEach((file) => {
|
||||||
fs.unlinkSync(path.join(historyDir, file));
|
fs.unlinkSync(path.join(historyDir, file));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error deleting all sessions:', error.message);
|
console.error("Error deleting all sessions:", error.message);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -550,5 +570,5 @@ module.exports = {
|
|||||||
deleteAllSessions,
|
deleteAllSessions,
|
||||||
|
|
||||||
// Clear all
|
// Clear all
|
||||||
clearAllData
|
clearAllData,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -7,6 +7,7 @@ const {
|
|||||||
} = require("./gemini");
|
} = require("./gemini");
|
||||||
const { fork } = require("child_process");
|
const { fork } = require("child_process");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
const { getSystemNode } = require("./nodeDetect");
|
||||||
|
|
||||||
// ── State ──
|
// ── State ──
|
||||||
|
|
||||||
@ -19,6 +20,9 @@ let localConversationHistory = [];
|
|||||||
let currentSystemPrompt = null;
|
let currentSystemPrompt = null;
|
||||||
let isLocalActive = false;
|
let isLocalActive = false;
|
||||||
|
|
||||||
|
// Set when we intentionally kill the worker to suppress crash handling
|
||||||
|
let whisperShuttingDown = false;
|
||||||
|
|
||||||
// Pending transcription callback (one at a time)
|
// Pending transcription callback (one at a time)
|
||||||
let pendingTranscribe = null;
|
let pendingTranscribe = null;
|
||||||
|
|
||||||
@ -189,12 +193,54 @@ function spawnWhisperWorker() {
|
|||||||
const workerPath = path.join(__dirname, "whisperWorker.js");
|
const workerPath = path.join(__dirname, "whisperWorker.js");
|
||||||
console.log("[LocalAI] Spawning Whisper worker:", workerPath);
|
console.log("[LocalAI] Spawning Whisper worker:", workerPath);
|
||||||
|
|
||||||
|
// Determine the best way to spawn the worker:
|
||||||
|
// 1. System Node.js (preferred) — native addons were compiled against this
|
||||||
|
// ABI, so onnxruntime-node works without SIGTRAP / ABI mismatches.
|
||||||
|
// 2. Electron utilityProcess (packaged builds) — proper Node.js child
|
||||||
|
// process API that doesn't require the RunAsNode fuse.
|
||||||
|
// 3. ELECTRON_RUN_AS_NODE (last resort, dev only) — the old approach that
|
||||||
|
// only works when the RunAsNode fuse isn't flipped.
|
||||||
|
|
||||||
|
const systemNode = getSystemNode();
|
||||||
|
|
||||||
|
if (systemNode) {
|
||||||
|
// Spawn with system Node.js — onnxruntime-node native binary matches ABI
|
||||||
|
console.log("[LocalAI] Using system Node.js:", systemNode.nodePath);
|
||||||
|
whisperWorker = fork(workerPath, [], {
|
||||||
|
stdio: ["pipe", "pipe", "pipe", "ipc"],
|
||||||
|
execPath: systemNode.nodePath,
|
||||||
|
env: {
|
||||||
|
...process.env,
|
||||||
|
// Unset ELECTRON_RUN_AS_NODE so the system node doesn't inherit it
|
||||||
|
ELECTRON_RUN_AS_NODE: undefined,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// No system Node.js found — try utilityProcess (Electron >= 22)
|
||||||
|
// utilityProcess.fork() creates a proper child Node.js process without
|
||||||
|
// needing the RunAsNode fuse. Falls back to ELECTRON_RUN_AS_NODE for
|
||||||
|
// dev mode where fuses aren't applied.
|
||||||
|
try {
|
||||||
|
const { utilityProcess: UP } = require("electron");
|
||||||
|
if (UP && typeof UP.fork === "function") {
|
||||||
|
console.log("[LocalAI] Using Electron utilityProcess");
|
||||||
|
const up = UP.fork(workerPath);
|
||||||
|
// Wrap utilityProcess to look like a ChildProcess for the rest of localai.js
|
||||||
|
whisperWorker = wrapUtilityProcess(up);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (_) {
|
||||||
|
// utilityProcess not available (older Electron or renderer context)
|
||||||
|
}
|
||||||
|
|
||||||
|
console.warn(
|
||||||
|
"[LocalAI] No system Node.js — falling back to ELECTRON_RUN_AS_NODE (WASM backend will be used)",
|
||||||
|
);
|
||||||
whisperWorker = fork(workerPath, [], {
|
whisperWorker = fork(workerPath, [], {
|
||||||
stdio: ["pipe", "pipe", "pipe", "ipc"],
|
stdio: ["pipe", "pipe", "pipe", "ipc"],
|
||||||
// ELECTRON_RUN_AS_NODE makes the Electron binary behave as plain Node.js,
|
|
||||||
// which is required for child_process.fork() in packaged Electron apps.
|
|
||||||
env: { ...process.env, ELECTRON_RUN_AS_NODE: "1" },
|
env: { ...process.env, ELECTRON_RUN_AS_NODE: "1" },
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
whisperWorker.stdout.on("data", (data) => {
|
whisperWorker.stdout.on("data", (data) => {
|
||||||
console.log("[WhisperWorker stdout]", data.toString().trim());
|
console.log("[WhisperWorker stdout]", data.toString().trim());
|
||||||
@ -230,6 +276,12 @@ function spawnWhisperWorker() {
|
|||||||
whisperWorker = null;
|
whisperWorker = null;
|
||||||
whisperReady = false;
|
whisperReady = false;
|
||||||
|
|
||||||
|
// If we intentionally shut down, don't treat as crash
|
||||||
|
if (whisperShuttingDown) {
|
||||||
|
whisperShuttingDown = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Reject any pending transcription
|
// Reject any pending transcription
|
||||||
if (pendingTranscribe) {
|
if (pendingTranscribe) {
|
||||||
pendingTranscribe.reject(
|
pendingTranscribe.reject(
|
||||||
@ -263,11 +315,52 @@ function spawnWhisperWorker() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrap Electron's utilityProcess to behave like a ChildProcess (duck-typing)
|
||||||
|
* so the rest of localai.js can use the same API.
|
||||||
|
*/
|
||||||
|
function wrapUtilityProcess(up) {
|
||||||
|
const EventEmitter = require("events");
|
||||||
|
const wrapper = new EventEmitter();
|
||||||
|
|
||||||
|
// Forward messages
|
||||||
|
up.on("message", (msg) => wrapper.emit("message", msg));
|
||||||
|
|
||||||
|
// Map utilityProcess exit to ChildProcess-like exit event
|
||||||
|
up.on("exit", (code) => wrapper.emit("exit", code, null));
|
||||||
|
|
||||||
|
// Provide stdout/stderr stubs (utilityProcess pipes to parent console)
|
||||||
|
const { Readable } = require("stream");
|
||||||
|
wrapper.stdout = new Readable({ read() {} });
|
||||||
|
wrapper.stderr = new Readable({ read() {} });
|
||||||
|
|
||||||
|
wrapper.send = (data) => up.postMessage(data);
|
||||||
|
wrapper.kill = (signal) => up.kill();
|
||||||
|
wrapper.removeAllListeners = () => {
|
||||||
|
up.removeAllListeners();
|
||||||
|
EventEmitter.prototype.removeAllListeners.call(wrapper);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Setup stdout/stderr forwarding
|
||||||
|
wrapper.stdout.on("data", (data) => {
|
||||||
|
console.log("[WhisperWorker stdout]", data.toString().trim());
|
||||||
|
});
|
||||||
|
wrapper.stderr.on("data", (data) => {
|
||||||
|
console.error("[WhisperWorker stderr]", data.toString().trim());
|
||||||
|
});
|
||||||
|
|
||||||
|
return wrapper;
|
||||||
|
}
|
||||||
|
|
||||||
let pendingLoad = null;
|
let pendingLoad = null;
|
||||||
|
|
||||||
function handleWorkerLoadResult(msg) {
|
function handleWorkerLoadResult(msg) {
|
||||||
if (msg.success) {
|
if (msg.success) {
|
||||||
console.log("[LocalAI] Whisper model loaded successfully (in worker)");
|
console.log(
|
||||||
|
"[LocalAI] Whisper model loaded successfully (in worker, device:",
|
||||||
|
msg.device || "unknown",
|
||||||
|
")",
|
||||||
|
);
|
||||||
whisperReady = true;
|
whisperReady = true;
|
||||||
sendToRenderer("whisper-downloading", false);
|
sendToRenderer("whisper-downloading", false);
|
||||||
isWhisperLoading = false;
|
isWhisperLoading = false;
|
||||||
@ -312,11 +405,49 @@ function respawnWhisperWorker() {
|
|||||||
"Xenova/whisper-small";
|
"Xenova/whisper-small";
|
||||||
sendToRenderer("whisper-downloading", true);
|
sendToRenderer("whisper-downloading", true);
|
||||||
isWhisperLoading = true;
|
isWhisperLoading = true;
|
||||||
whisperWorker.send({ type: "load", modelName, cacheDir });
|
const device = resolveWhisperDevice();
|
||||||
|
whisperWorker.send({ type: "load", modelName, cacheDir, device });
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine which ONNX backend to use for Whisper inference.
|
||||||
|
* - "cpu" → onnxruntime-node (fast, native — requires matching ABI)
|
||||||
|
* - "wasm" → onnxruntime-web (slower but universally compatible)
|
||||||
|
*
|
||||||
|
* When spawned with system Node.js, native CPU backend is safe.
|
||||||
|
* Otherwise default to WASM to prevent native crashes.
|
||||||
|
*/
|
||||||
|
function resolveWhisperDevice() {
|
||||||
|
const prefs = require("../storage").getPreferences();
|
||||||
|
if (prefs.whisperDevice) return prefs.whisperDevice;
|
||||||
|
// Auto-detect: if we're running with system Node.js, native is safe
|
||||||
|
const systemNode = getSystemNode();
|
||||||
|
return systemNode ? "cpu" : "wasm";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map the app's BCP-47 language tag (e.g. "en-US", "ru-RU") to the
|
||||||
|
* ISO 639-1 code that Whisper expects (e.g. "en", "ru").
|
||||||
|
* Returns "auto" when the user selected auto-detect, which tells the
|
||||||
|
* worker to let Whisper detect the language itself.
|
||||||
|
*/
|
||||||
|
function resolveWhisperLanguage() {
|
||||||
|
const prefs = require("../storage").getPreferences();
|
||||||
|
const lang = prefs.selectedLanguage || "en-US";
|
||||||
|
if (lang === "auto") return "auto";
|
||||||
|
// BCP-47: primary subtag is the ISO 639 code
|
||||||
|
// Handle special case: "cmn-CN" → "zh" (Mandarin Chinese → Whisper uses "zh")
|
||||||
|
const primary = lang.split("-")[0].toLowerCase();
|
||||||
|
const WHISPER_LANG_MAP = {
|
||||||
|
cmn: "zh",
|
||||||
|
yue: "zh",
|
||||||
|
};
|
||||||
|
return WHISPER_LANG_MAP[primary] || primary;
|
||||||
}
|
}
|
||||||
|
|
||||||
function killWhisperWorker() {
|
function killWhisperWorker() {
|
||||||
if (whisperWorker) {
|
if (whisperWorker) {
|
||||||
|
whisperShuttingDown = true;
|
||||||
try {
|
try {
|
||||||
whisperWorker.removeAllListeners();
|
whisperWorker.removeAllListeners();
|
||||||
whisperWorker.kill();
|
whisperWorker.kill();
|
||||||
@ -345,9 +476,12 @@ async function loadWhisperPipeline(modelName) {
|
|||||||
const { app } = require("electron");
|
const { app } = require("electron");
|
||||||
const cacheDir = path.join(app.getPath("userData"), "whisper-models");
|
const cacheDir = path.join(app.getPath("userData"), "whisper-models");
|
||||||
|
|
||||||
|
const device = resolveWhisperDevice();
|
||||||
|
console.log("[LocalAI] Whisper device:", device);
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
pendingLoad = { resolve };
|
pendingLoad = { resolve };
|
||||||
whisperWorker.send({ type: "load", modelName, cacheDir });
|
whisperWorker.send({ type: "load", modelName, cacheDir, device });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -393,7 +527,11 @@ async function transcribeAudio(pcm16kBuffer) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
whisperWorker.send({ type: "transcribe", audioBase64 });
|
whisperWorker.send({
|
||||||
|
type: "transcribe",
|
||||||
|
audioBase64,
|
||||||
|
language: resolveWhisperLanguage(),
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
pendingTranscribe = null;
|
pendingTranscribe = null;
|
||||||
|
|||||||
177
src/utils/nodeDetect.js
Normal file
177
src/utils/nodeDetect.js
Normal file
@ -0,0 +1,177 @@
|
|||||||
|
/**
|
||||||
|
* nodeDetect.js — Locate the system Node.js binary.
|
||||||
|
*
|
||||||
|
* When spawning child processes that rely on native addons compiled against the
|
||||||
|
* system Node.js ABI (e.g. onnxruntime-node), we must NOT run them inside
|
||||||
|
* Electron's embedded Node.js runtime — the ABI mismatch causes SIGTRAP /
|
||||||
|
* SIGSEGV crashes. This module finds the real system `node` binary so we can
|
||||||
|
* pass it as `execPath` to `child_process.fork()`.
|
||||||
|
*
|
||||||
|
* Falls back to `null` when no system Node.js is found, letting the caller
|
||||||
|
* decide on an alternative strategy (e.g. WASM backend).
|
||||||
|
*/
|
||||||
|
|
||||||
|
const { execSync } = require("child_process");
|
||||||
|
const fs = require("fs");
|
||||||
|
const path = require("path");
|
||||||
|
const os = require("os");
|
||||||
|
|
||||||
|
/** Well-known Node.js install locations per platform. */
|
||||||
|
const KNOWN_PATHS = {
|
||||||
|
darwin: [
|
||||||
|
"/usr/local/bin/node",
|
||||||
|
"/opt/homebrew/bin/node", // Apple Silicon Homebrew
|
||||||
|
path.join(os.homedir(), ".nvm/versions/node"), // nvm — needs glob
|
||||||
|
path.join(os.homedir(), ".volta/bin/node"), // Volta
|
||||||
|
path.join(os.homedir(), ".fnm/aliases/default/bin/node"), // fnm
|
||||||
|
path.join(os.homedir(), ".mise/shims/node"), // mise (rtx)
|
||||||
|
path.join(os.homedir(), ".asdf/shims/node"), // asdf
|
||||||
|
],
|
||||||
|
linux: [
|
||||||
|
"/usr/bin/node",
|
||||||
|
"/usr/local/bin/node",
|
||||||
|
path.join(os.homedir(), ".nvm/versions/node"),
|
||||||
|
path.join(os.homedir(), ".volta/bin/node"),
|
||||||
|
path.join(os.homedir(), ".fnm/aliases/default/bin/node"),
|
||||||
|
path.join(os.homedir(), ".mise/shims/node"),
|
||||||
|
path.join(os.homedir(), ".asdf/shims/node"),
|
||||||
|
],
|
||||||
|
win32: [
|
||||||
|
"C:\\Program Files\\nodejs\\node.exe",
|
||||||
|
"C:\\Program Files (x86)\\nodejs\\node.exe",
|
||||||
|
path.join(os.homedir(), "AppData", "Roaming", "nvm", "current", "node.exe"),
|
||||||
|
path.join(os.homedir(), ".volta", "bin", "node.exe"),
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find the latest nvm-installed Node.js binary on macOS / Linux.
|
||||||
|
* Returns the path to the `node` binary or null.
|
||||||
|
*/
|
||||||
|
function findNvmNode() {
|
||||||
|
const nvmDir = path.join(os.homedir(), ".nvm", "versions", "node");
|
||||||
|
try {
|
||||||
|
if (!fs.existsSync(nvmDir)) return null;
|
||||||
|
const versions = fs.readdirSync(nvmDir).filter((d) => d.startsWith("v"));
|
||||||
|
if (versions.length === 0) return null;
|
||||||
|
// Sort semver descending (rough but sufficient)
|
||||||
|
versions.sort((a, b) => b.localeCompare(a, undefined, { numeric: true }));
|
||||||
|
const nodeBin = path.join(nvmDir, versions[0], "bin", "node");
|
||||||
|
if (fs.existsSync(nodeBin)) return nodeBin;
|
||||||
|
} catch (_) {
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempt to resolve `node` via the system PATH using `which` (Unix) or
|
||||||
|
* `where` (Windows). Returns the path string or null.
|
||||||
|
*/
|
||||||
|
function whichNode() {
|
||||||
|
try {
|
||||||
|
const cmd = process.platform === "win32" ? "where node" : "which node";
|
||||||
|
const result = execSync(cmd, {
|
||||||
|
encoding: "utf8",
|
||||||
|
timeout: 5000,
|
||||||
|
env: {
|
||||||
|
...process.env,
|
||||||
|
// Ensure common manager shim dirs are on PATH
|
||||||
|
PATH: [
|
||||||
|
process.env.PATH || "",
|
||||||
|
"/usr/local/bin",
|
||||||
|
"/opt/homebrew/bin",
|
||||||
|
path.join(os.homedir(), ".volta", "bin"),
|
||||||
|
path.join(os.homedir(), ".fnm", "aliases", "default", "bin"),
|
||||||
|
path.join(os.homedir(), ".mise", "shims"),
|
||||||
|
path.join(os.homedir(), ".asdf", "shims"),
|
||||||
|
].join(process.platform === "win32" ? ";" : ":"),
|
||||||
|
},
|
||||||
|
stdio: ["ignore", "pipe", "ignore"],
|
||||||
|
}).trim();
|
||||||
|
// `where` on Windows may return multiple lines — take the first
|
||||||
|
const first = result.split(/\r?\n/)[0].trim();
|
||||||
|
if (first && fs.existsSync(first)) return first;
|
||||||
|
} catch (_) {
|
||||||
|
// Command failed
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether a given path is a real Node.js binary (not the Electron binary
|
||||||
|
* pretending to be Node via ELECTRON_RUN_AS_NODE).
|
||||||
|
*/
|
||||||
|
function isRealNode(nodePath) {
|
||||||
|
if (!nodePath) return false;
|
||||||
|
try {
|
||||||
|
const out = execSync(
|
||||||
|
`"${nodePath}" -e "process.stdout.write(String(!process.versions.electron))"`,
|
||||||
|
{
|
||||||
|
encoding: "utf8",
|
||||||
|
timeout: 5000,
|
||||||
|
env: { ...process.env, ELECTRON_RUN_AS_NODE: undefined },
|
||||||
|
stdio: ["ignore", "pipe", "ignore"],
|
||||||
|
},
|
||||||
|
).trim();
|
||||||
|
return out === "true";
|
||||||
|
} catch (_) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find the system Node.js binary.
|
||||||
|
*
|
||||||
|
* @returns {{ nodePath: string } | null} The absolute path to system `node`,
|
||||||
|
* or null if none found. The caller should fall back to WASM when null.
|
||||||
|
*/
|
||||||
|
function findSystemNode() {
|
||||||
|
// 1. Try `which node` / `where node` first (respects user's PATH / shims)
|
||||||
|
const fromPath = whichNode();
|
||||||
|
if (fromPath && isRealNode(fromPath)) {
|
||||||
|
return { nodePath: fromPath };
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Try nvm (has multiple version dirs)
|
||||||
|
const fromNvm = findNvmNode();
|
||||||
|
if (fromNvm && isRealNode(fromNvm)) {
|
||||||
|
return { nodePath: fromNvm };
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Walk the well-known paths for the current platform
|
||||||
|
const platform = process.platform;
|
||||||
|
const candidates = KNOWN_PATHS[platform] || KNOWN_PATHS.linux;
|
||||||
|
for (const candidate of candidates) {
|
||||||
|
// Skip the nvm root — already handled above
|
||||||
|
if (candidate.includes(".nvm/versions/node")) continue;
|
||||||
|
if (fs.existsSync(candidate) && isRealNode(candidate)) {
|
||||||
|
return { nodePath: candidate };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Cache so we only search once per process lifetime. */
|
||||||
|
let _cached = undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cached version of `findSystemNode()`.
|
||||||
|
* @returns {{ nodePath: string } | null}
|
||||||
|
*/
|
||||||
|
function getSystemNode() {
|
||||||
|
if (_cached === undefined) {
|
||||||
|
_cached = findSystemNode();
|
||||||
|
if (_cached) {
|
||||||
|
console.log("[nodeDetect] Found system Node.js:", _cached.nodePath);
|
||||||
|
} else {
|
||||||
|
console.warn(
|
||||||
|
"[nodeDetect] No system Node.js found — will fall back to WASM backend",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return _cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { findSystemNode, getSystemNode, isRealNode };
|
||||||
@ -8,18 +8,48 @@
|
|||||||
*
|
*
|
||||||
* Protocol (parent ↔ worker):
|
* Protocol (parent ↔ worker):
|
||||||
* parent → worker:
|
* parent → worker:
|
||||||
* { type: 'load', modelName, cacheDir }
|
* { type: 'load', modelName, cacheDir, device? }
|
||||||
* { type: 'transcribe', audioBase64 } // PCM 16-bit 16kHz as base64
|
* { type: 'transcribe', audioBase64, language? } // PCM 16-bit 16kHz as base64
|
||||||
* { type: 'shutdown' }
|
* { type: 'shutdown' }
|
||||||
*
|
*
|
||||||
* worker → parent:
|
* worker → parent:
|
||||||
* { type: 'load-result', success, error? }
|
* { type: 'load-result', success, error?, device? }
|
||||||
* { type: 'transcribe-result', success, text?, error? }
|
* { type: 'transcribe-result', success, text?, error? }
|
||||||
* { type: 'status', message }
|
* { type: 'status', message }
|
||||||
* { type: 'ready' }
|
* { type: 'ready' }
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// ── Crash handlers — report fatal errors before the process dies ──
|
||||||
|
|
||||||
|
process.on("uncaughtException", (err) => {
|
||||||
|
try {
|
||||||
|
send({
|
||||||
|
type: "status",
|
||||||
|
message: `[Worker] Uncaught exception: ${err.message || err}`,
|
||||||
|
});
|
||||||
|
console.error("[WhisperWorker] Uncaught exception:", err);
|
||||||
|
} catch (_) {
|
||||||
|
// Cannot communicate with parent anymore
|
||||||
|
}
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on("unhandledRejection", (reason) => {
|
||||||
|
try {
|
||||||
|
send({
|
||||||
|
type: "status",
|
||||||
|
message: `[Worker] Unhandled rejection: ${reason?.message || reason}`,
|
||||||
|
});
|
||||||
|
console.error("[WhisperWorker] Unhandled rejection:", reason);
|
||||||
|
} catch (_) {
|
||||||
|
// Cannot communicate with parent anymore
|
||||||
|
}
|
||||||
|
// Don't exit — let it be caught by the pipeline's own handlers
|
||||||
|
});
|
||||||
|
|
||||||
let whisperPipeline = null;
|
let whisperPipeline = null;
|
||||||
|
/** Which ONNX backend is actually active: "cpu" | "wasm" */
|
||||||
|
let activeDevice = null;
|
||||||
|
|
||||||
function pcm16ToFloat32(pcm16Buffer) {
|
function pcm16ToFloat32(pcm16Buffer) {
|
||||||
if (!pcm16Buffer || pcm16Buffer.length === 0) {
|
if (!pcm16Buffer || pcm16Buffer.length === 0) {
|
||||||
@ -35,9 +65,18 @@ function pcm16ToFloat32(pcm16Buffer) {
|
|||||||
return float32;
|
return float32;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadModel(modelName, cacheDir) {
|
/**
|
||||||
|
* Load the Whisper model.
|
||||||
|
*
|
||||||
|
* @param {string} modelName HuggingFace model id, e.g. "Xenova/whisper-small"
|
||||||
|
* @param {string} cacheDir Directory for cached model files
|
||||||
|
* @param {string} [device] "cpu" (onnxruntime-node) or "wasm" (onnxruntime-web).
|
||||||
|
* When "cpu" is requested we try native first and fall
|
||||||
|
* back to "wasm" on failure (ABI mismatch, etc.).
|
||||||
|
*/
|
||||||
|
async function loadModel(modelName, cacheDir, device = "cpu") {
|
||||||
if (whisperPipeline) {
|
if (whisperPipeline) {
|
||||||
send({ type: "load-result", success: true });
|
send({ type: "load-result", success: true, device: activeDevice });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,23 +85,112 @@ async function loadModel(modelName, cacheDir) {
|
|||||||
type: "status",
|
type: "status",
|
||||||
message: "Loading Whisper model (first time may take a while)...",
|
message: "Loading Whisper model (first time may take a while)...",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Validate / create cache directory
|
||||||
|
const fs = require("fs");
|
||||||
|
const path = require("path");
|
||||||
|
if (cacheDir) {
|
||||||
|
try {
|
||||||
|
if (!fs.existsSync(cacheDir)) {
|
||||||
|
fs.mkdirSync(cacheDir, { recursive: true });
|
||||||
|
console.log("[WhisperWorker] Created cache directory:", cacheDir);
|
||||||
|
}
|
||||||
|
} catch (mkdirErr) {
|
||||||
|
console.warn(
|
||||||
|
"[WhisperWorker] Cannot create cache dir:",
|
||||||
|
mkdirErr.message,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for corrupted partial downloads — if an onnx file exists but
|
||||||
|
// is suspiciously small (< 1 KB), delete it so the library re-downloads.
|
||||||
|
try {
|
||||||
|
const modelDir = path.join(cacheDir, modelName.replace("/", path.sep));
|
||||||
|
if (fs.existsSync(modelDir)) {
|
||||||
|
const walk = (dir) => {
|
||||||
|
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
||||||
|
const full = path.join(dir, entry.name);
|
||||||
|
if (entry.isDirectory()) {
|
||||||
|
walk(full);
|
||||||
|
} else if (
|
||||||
|
entry.name.endsWith(".onnx") &&
|
||||||
|
fs.statSync(full).size < 1024
|
||||||
|
) {
|
||||||
|
console.warn(
|
||||||
|
"[WhisperWorker] Removing likely-corrupt file:",
|
||||||
|
full,
|
||||||
|
);
|
||||||
|
fs.unlinkSync(full);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
walk(modelDir);
|
||||||
|
}
|
||||||
|
} catch (cleanErr) {
|
||||||
|
console.warn("[WhisperWorker] Cache cleanup error:", cleanErr.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const { pipeline, env } = await import("@huggingface/transformers");
|
const { pipeline, env } = await import("@huggingface/transformers");
|
||||||
env.cacheDir = cacheDir;
|
env.cacheDir = cacheDir;
|
||||||
|
|
||||||
|
// Attempt to load with the requested device
|
||||||
|
const devicesToTry = device === "wasm" ? ["wasm"] : ["cpu", "wasm"];
|
||||||
|
|
||||||
|
let lastError = null;
|
||||||
|
|
||||||
|
for (const dev of devicesToTry) {
|
||||||
|
try {
|
||||||
|
send({
|
||||||
|
type: "status",
|
||||||
|
message: `Loading Whisper (${dev} backend)...`,
|
||||||
|
});
|
||||||
|
console.log(
|
||||||
|
`[WhisperWorker] Trying device: ${dev}, model: ${modelName}`,
|
||||||
|
);
|
||||||
|
|
||||||
whisperPipeline = await pipeline(
|
whisperPipeline = await pipeline(
|
||||||
"automatic-speech-recognition",
|
"automatic-speech-recognition",
|
||||||
modelName,
|
modelName,
|
||||||
{
|
{
|
||||||
dtype: "q8",
|
dtype: "q8",
|
||||||
device: "cpu",
|
device: dev,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
send({ type: "load-result", success: true });
|
|
||||||
|
activeDevice = dev;
|
||||||
|
console.log(
|
||||||
|
`[WhisperWorker] Model loaded successfully (device: ${dev})`,
|
||||||
|
);
|
||||||
|
send({ type: "load-result", success: true, device: dev });
|
||||||
|
return;
|
||||||
|
} catch (err) {
|
||||||
|
lastError = err;
|
||||||
|
console.error(
|
||||||
|
`[WhisperWorker] Failed to load with device "${dev}":`,
|
||||||
|
err.message || err,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (dev === "cpu" && devicesToTry.includes("wasm")) {
|
||||||
|
send({
|
||||||
|
type: "status",
|
||||||
|
message: `Native CPU backend failed (${err.message}). Trying WASM fallback...`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset pipeline state before retry
|
||||||
|
whisperPipeline = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// All devices failed
|
||||||
|
throw lastError || new Error("All ONNX backends failed");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
send({ type: "load-result", success: false, error: error.message });
|
send({ type: "load-result", success: false, error: error.message });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function transcribe(audioBase64) {
|
async function transcribe(audioBase64, language) {
|
||||||
if (!whisperPipeline) {
|
if (!whisperPipeline) {
|
||||||
send({
|
send({
|
||||||
type: "transcribe-result",
|
type: "transcribe-result",
|
||||||
@ -101,11 +229,16 @@ async function transcribe(audioBase64) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await whisperPipeline(float32Audio, {
|
// Build pipeline options with the requested language
|
||||||
|
const pipelineOpts = {
|
||||||
sampling_rate: 16000,
|
sampling_rate: 16000,
|
||||||
language: "en",
|
|
||||||
task: "transcribe",
|
task: "transcribe",
|
||||||
});
|
};
|
||||||
|
if (language && language !== "auto") {
|
||||||
|
pipelineOpts.language = language;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await whisperPipeline(float32Audio, pipelineOpts);
|
||||||
|
|
||||||
const text = result.text?.trim() || "";
|
const text = result.text?.trim() || "";
|
||||||
send({ type: "transcribe-result", success: true, text });
|
send({ type: "transcribe-result", success: true, text });
|
||||||
@ -131,17 +264,32 @@ function send(msg) {
|
|||||||
process.on("message", (msg) => {
|
process.on("message", (msg) => {
|
||||||
switch (msg.type) {
|
switch (msg.type) {
|
||||||
case "load":
|
case "load":
|
||||||
loadModel(msg.modelName, msg.cacheDir).catch((err) => {
|
loadModel(msg.modelName, msg.cacheDir, msg.device).catch((err) => {
|
||||||
send({ type: "load-result", success: false, error: err.message });
|
send({ type: "load-result", success: false, error: err.message });
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "transcribe":
|
case "transcribe":
|
||||||
transcribe(msg.audioBase64).catch((err) => {
|
transcribe(msg.audioBase64, msg.language).catch((err) => {
|
||||||
send({ type: "transcribe-result", success: false, error: err.message });
|
send({ type: "transcribe-result", success: false, error: err.message });
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "shutdown":
|
case "shutdown":
|
||||||
process.exit(0);
|
// Dispose the ONNX session gracefully before exiting to avoid
|
||||||
|
// native cleanup race conditions (SIGABRT on mutex destroy).
|
||||||
|
(async () => {
|
||||||
|
if (whisperPipeline) {
|
||||||
|
try {
|
||||||
|
if (typeof whisperPipeline.dispose === "function") {
|
||||||
|
await whisperPipeline.dispose();
|
||||||
|
}
|
||||||
|
} catch (_) {
|
||||||
|
// Best-effort cleanup
|
||||||
|
}
|
||||||
|
whisperPipeline = null;
|
||||||
|
}
|
||||||
|
// Small delay to let native threads wind down
|
||||||
|
setTimeout(() => process.exit(0), 200);
|
||||||
|
})();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user