Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
refactor: refactor main into helpers
Gabriel Stein186e0966f564
diff --git a/main.js b/main.jsindex 2f61b30c7..9240eedbf 100644--- a/main.js+++ b/main.js@@ -1,1556 +1,103 @@-const { app, BrowserWindow, globalShortcut, ipcMain, clipboard, shell, screen, Menu, Tray } = require("electron")-const path = require("path")-const { spawn } = require("child_process")-const Database = require('better-sqlite3')-const fs = require('fs')-const os = require('os')-const UpdateManager = require('./src/updater')--// Load .env file with better error handling and production support-function loadEnvironmentVariables() {- // In production, try multiple locations for .env file- const possibleEnvPaths = [- // Development path- path.join(__dirname, '.env'),- // Production packaged app paths- path.join(process.resourcesPath, '.env'),- path.join(process.resourcesPath, 'app.asar.unpacked', '.env'),- path.join(app.getPath('userData'), '.env'), // User data directory- // Legacy paths- path.join(process.resourcesPath, 'app', '.env'),- ];-- let envLoaded = false;-- for (const envPath of possibleEnvPaths) {- try {- const fs = require('fs');- if (fs.existsSync(envPath)) {- const result = require('dotenv').config({ path: envPath });- if (!result.error) {- envLoaded = true;- break;- }- }- } catch (error) {- // Continue to next path- }- }-}--// Load environment variables-loadEnvironmentVariables();--// Initialize update manager-const updateManager = new UpdateManager();--let mainWindow-let tray = null-let controlPanelWindow = null--// Function to check if dev server is ready-async function waitForDevServer(url, maxAttempts = 30, delay = 1000) {- for (let i = 0; i < maxAttempts; i++) {- try {- const response = await fetch(url);- if (response.ok) {- console.log(`Dev server ready after ${i + 1} attempts`);- return true;- }- } catch (error) {- console.log(`Waiting for dev server... attempt ${i + 1}/${maxAttempts}`);- }- await new Promise(resolve => setTimeout(resolve, delay));- }- console.error('Dev server failed to start within timeout');- return false;-}--function getAppUrl(isControlPanel = false) {- if (process.env.NODE_ENV === 'development') {- // Vite dev server- return isControlPanel- ? 'http://localhost:5174/?panel=true'- : 'http://localhost:5174/';- } else {- // Production build - files are in asar package- // __dirname in packaged app points to the root of app.asar- const htmlPath = path.join(__dirname, 'src', 'dist', 'index.html');-- // Production build paths-- return isControlPanel- ? `file://${htmlPath}?panel=true`- : `file://${htmlPath}`;- }-}--async function createWindow() {- // Always show window by default- const display = screen.getPrimaryDisplay();- const width = 100; // Increased width to accommodate tooltips and scaling- const height = 100; // Increased height to accommodate tooltips and scaling- const x = display.bounds.x + display.workArea.width - width;- const y = display.bounds.y + display.workArea.height;-- // Window positioning calculated for bottom-right corner- mainWindow = new BrowserWindow({- width,- height,- x,- y,- webPreferences: {- preload: path.join(__dirname, 'preload.js'),- nodeIntegration: false,- contextIsolation: true,- enableRemoteModule: false,- sandbox: true,- },- frame: false,- alwaysOnTop: true,- resizable: false,- transparent: true,- show: true, // Always show- })--- // Load URL with retry logic for development- const loadMainWindow = async () => {- const appUrl = getAppUrl(false);- if (process.env.NODE_ENV === 'development') {- const isReady = await waitForDevServer('http://localhost:5174/');- if (!isReady) {- console.error('Dev server not ready, loading anyway...');- }- }-- mainWindow.loadURL(appUrl);- };-- loadMainWindow();-- // Add error handling for failed loads with retry- mainWindow.webContents.on('did-fail-load', async (event, errorCode, errorDescription, validatedURL) => {- console.error('Failed to load main window:', errorCode, errorDescription, validatedURL);-- if (process.env.NODE_ENV === 'development' && validatedURL.includes('localhost:5174')) {- console.log('Retrying connection to dev server in 2 seconds...');- setTimeout(async () => {- const isReady = await waitForDevServer('http://localhost:5174/');- if (isReady) {- console.log('Dev server ready, reloading...');- mainWindow.reload();- }- }, 2000);- }- });-- // Ensure window is visible after load- mainWindow.webContents.on('did-finish-load', () => {- setTimeout(() => {- if (!mainWindow.isVisible()) {- console.log('Window not visible, forcing show...');- mainWindow.show();- mainWindow.focus();- }- }, 1000);- });-- // Remove all previous shortcuts- globalShortcut.unregisterAll();-- // Register global shortcut for backtick (`) key to toggle dictation- globalShortcut.register('`', () => {- if (!mainWindow.isVisible()) {- mainWindow.show();- }- mainWindow.webContents.send('toggle-dictation');- });-- // Ensure window is always on top, even above fullscreen apps- mainWindow.setAlwaysOnTop(true, 'screen-saver');- mainWindow.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true });- mainWindow.on('show', () => {- mainWindow.setAlwaysOnTop(true, 'screen-saver');- mainWindow.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true });- });-- // Add a working Quit menu item for macOS- if (process.platform === 'darwin') {- const template = [- {- label: 'OpenWispr',- submenu: [- { role: 'about' },- { type: 'separator' },- { role: 'quit', label: 'Quit OpenWispr' }- ]- }- ];- const menu = Menu.buildFromTemplate(template);- Menu.setApplicationMenu(menu);- }-}--async function createControlPanelWindow() {- if (controlPanelWindow) {- controlPanelWindow.focus();+const { app, globalShortcut, BrowserWindow } = require("electron");++// Add global error handling for uncaught exceptions+process.on("uncaughtException", (error) => {+ console.error("Uncaught Exception:", error);+ // Don't exit the process for EPIPE errors as they're harmless+ if (error.code === "EPIPE") {+ console.log("EPIPE error caught and ignored");return;}- controlPanelWindow = new BrowserWindow({- width: 800,- height: 700,- webPreferences: {- preload: path.join(__dirname, 'preload.js'),- nodeIntegration: false,- contextIsolation: true,- enableRemoteModule: false,- sandbox: false, // Disable sandbox for control panel to allow clipboard access- webSecurity: false, // Disable web security for clipboard access- },- title: 'OpenWispr Control Panel',- resizable: true,- show: true,- // Native macOS traffic lights with custom title bar- titleBarStyle: 'hiddenInset',- frame: false,- transparent: false,- backgroundColor: '#ffffff',- // Window behavior- minimizable: true,- maximizable: true,- closable: true,- fullscreenable: true,- });-- // Load control panel with same retry logic- const loadControlPanel = async () => {- const appUrl = getAppUrl(true);- // Loading control panel-- if (process.env.NODE_ENV === 'development') {- const isReady = await waitForDevServer('http://localhost:5174/');- if (!isReady) {- console.error('Dev server not ready for control panel, loading anyway...');- }- }-- controlPanelWindow.loadURL(appUrl);- };-- loadControlPanel();-- controlPanelWindow.on('closed', () => {- controlPanelWindow = null;- });-}--// Database setup-let db = null;--function cleanup() {- console.log('Starting cleanup process...');- // Database file deletion- try {- const dbPath = path.join(app.getPath('userData'), process.env.NODE_ENV === 'development' ? 'transcriptions-dev.db' : 'transcriptions.db');- if (fs.existsSync(dbPath)) {- fs.unlinkSync(dbPath);- console.log('✅ Database file deleted:', dbPath);- }- } catch (error) {- console.error('❌ Error deleting database file:', error);- }-- // Local storage clearing- mainWindow.webContents.executeJavaScript('localStorage.clear()').then(() => {- console.log('✅ Local storage cleared');- }).catch(error => {- console.error('❌ Error clearing local storage:', error);- });-- // Local Whisper model deletion- try {- const modelCacheDir = path.join(os.homedir(), '.cache', 'whisper');- if (fs.existsSync(modelCacheDir)) {- fs.rmSync(modelCacheDir, { recursive: true, force: true });- console.log('✅ Local Whisper models deleted:', modelCacheDir);- }- } catch (error) {- console.error('❌ Error deleting Whisper models:', error);- }-- // Permissions instruction- console.log('ℹ️ Please manually remove accessibility and microphone permissions via System Preferences if needed.');-- // Env file deletion- try {- const envPath = path.join(app.getPath('userData'), '.env');- if (fs.existsSync(envPath)) {- fs.unlinkSync(envPath);- console.log('✅ .env file deleted:', envPath);- }- } catch (error) {- console.error('❌ Error deleting .env file:', error);- }-- console.log('Cleanup process completed.');-}--function initDatabase() {- try {- console.log('🔄 Starting database initialization...');-- // Use different database files for development vs production- const dbFileName = process.env.NODE_ENV === 'development'- ? 'transcriptions-dev.db'- : 'transcriptions.db';-- const dbPath = path.join(app.getPath('userData'), dbFileName);- console.log('📁 Database path:', dbPath);-- // Database initialization- console.log('🔧 Creating database connection...');- db = new Database(dbPath);- console.log('✅ Database connection created');-- // Create transcriptions table if it doesn't exist- console.log('📋 Creating transcriptions table...');- db.exec(`- CREATE TABLE IF NOT EXISTS transcriptions (- id INTEGER PRIMARY KEY AUTOINCREMENT,- text TEXT NOT NULL,- timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,- created_at DATETIME DEFAULT CURRENT_TIMESTAMP- )- `);- console.log('✅ Transcriptions table created/verified');-- // Get count of existing transcriptions- console.log('📊 Counting existing transcriptions...');- const countStmt = db.prepare('SELECT COUNT(*) as count FROM transcriptions');- const { count } = countStmt.get();-- console.log(`✅ Database initialized successfully (${count} existing transcriptions)`);- return true;- } catch (error) {- console.error('❌ Database initialization failed:', error);- console.error('Error details:', {- message: error.message,- stack: error.stack- });- throw error;- }-}--app.whenReady().then(async () => {-- // Initialize database- try {-await new Promise((resolve, reject) => {- try {- initDatabase();- resolve();- } catch (error) {- reject('Failed to initialize database: ' + error);- }- });- } catch (error) {- console.error('Failed to initialize database:', error);- }-- // In development, add a small delay to let Vite start properly- if (process.env.NODE_ENV === 'development') {- await new Promise(resolve => setTimeout(resolve, 2000));- }-- try {- await createWindow();- } catch (error) {- console.error('Error creating main window:', error);- }-- // Auto-open control panel when app starts- try {- await createControlPanelWindow();- } catch (error) {- console.error('Error creating control panel window:', error);- }-- // Set windows for update manager and check for updates- updateManager.setWindows(mainWindow, controlPanelWindow);- updateManager.checkForUpdatesOnStartup();-- // Create tray icon on macOS- if (process.platform === 'darwin') {- try {- // For Electron apps, use nativeImage to load from resources properly- const { nativeImage } = require('electron');- let trayIcon;-- if (process.env.NODE_ENV === 'development') {- // Development mode - load from assets folder- const iconPath = path.join(__dirname, 'assets', '[email protected]');- trayIcon = nativeImage.createFromPath(iconPath);- } else {- // Production mode - try different locations for the icon- const possiblePaths = [- // First try extraResources (most likely for packaged app)- path.join(process.resourcesPath, 'assets', '[email protected]'),- // Then try asar.unpacked- path.join(process.resourcesPath, 'app.asar.unpacked', 'assets', '[email protected]'),- // Then try within app bundle- path.join(__dirname, 'assets', '[email protected]'),- // Legacy path- path.join(process.resourcesPath, 'app', 'assets', '[email protected]')- ];-- let iconPath = null;- for (const testPath of possiblePaths) {- try {- if (require('fs').existsSync(testPath)) {- iconPath = testPath;- break;- }- } catch (e) {- // Ignore and try next path- }- }-- if (iconPath) {- trayIcon = nativeImage.createFromPath(iconPath);- } else {- console.error('Could not find tray icon in any expected location');- console.log('Tried paths:', possiblePaths);-- // Create a fallback tray icon if the file isn't found- try {- // Create a simple fallback icon programmatically- const { nativeImage } = require('electron');- const canvas = require('canvas');- const canvasInstance = canvas.createCanvas(16, 16);- const ctx = canvasInstance.getContext('2d');-- // Draw a simple circle as fallback- ctx.fillStyle = '#000000';- ctx.beginPath();- ctx.arc(8, 8, 6, 0, 2 * Math.PI);- ctx.fill();-- const buffer = canvasInstance.toBuffer('image/png');- trayIcon = nativeImage.createFromBuffer(buffer);- console.log('✅ Created fallback tray icon');- } catch (fallbackError) {- console.error('Failed to create fallback tray icon:', fallbackError);- return; // Exit early if fallback also fails- }- }- }-- // Check if the image loaded successfully- if (trayIcon.isEmpty()) {- console.error('Tray icon is empty - failed to load');- return;- }-- // Ensure it's marked as a template image for macOS- trayIcon.setTemplateImage(true);-- tray = new Tray(trayIcon);-- // Prevent tray from being garbage collected- tray.setIgnoreDoubleClickEvents(true);-- const contextMenu = Menu.buildFromTemplate([- { label: 'Show Dictation Panel', click: () => {- if (!mainWindow.isVisible()) {- mainWindow.show();- }- mainWindow.focus();- }},- { label: 'Open Control Panel', click: () => { createControlPanelWindow(); } },- { type: 'separator' },- { label: 'Quit OpenWispr', click: () => {- console.log('Quitting app via tray menu');- app.quit();- }}- ]);-- tray.setToolTip('OpenWispr - Voice Dictation');- tray.setContextMenu(contextMenu);-- tray.on('click', () => {- // Always ensure the dictation panel is visible and focused- if (!mainWindow.isVisible()) {- mainWindow.show();- }- mainWindow.focus();- });-- // Handle tray destruction to prevent crashes- tray.on('destroyed', () => {- console.log('Tray icon destroyed');- tray = null;- });-- console.log('✅ Tray icon created successfully');- } catch (error) {- console.error('Error creating tray icon:', error);- }- }-})--app.on("window-all-closed", () => {- if (process.platform !== "darwin") {- app.quit()- }-})--app.on("activate", () => {- if (BrowserWindow.getAllWindows().length === 0) {- createWindow()- }-})--app.on("will-quit", () => {- globalShortcut.unregisterAll()-})--// Function to check and request accessibility permissions on macOS-async function checkAccessibilityPermissions() {- if (process.platform !== 'darwin') return true;-- return new Promise((resolve) => {- const { spawn } = require("child_process");-- console.log('🔍 Checking accessibility permissions...');-- // First, let's check if we can perform a simple accessibility action- const testProcess = spawn("osascript", [- "-e",- 'tell application "System Events" to get name of first process'- ]);-- let testOutput = '';- let testError = '';-- testProcess.stdout.on('data', (data) => {- testOutput += data.toString();- });-- testProcess.stderr.on('data', (data) => {- testError += data.toString();- });-- testProcess.on('close', (code) => {- console.log('Initial accessibility test - Code:', code);- console.log('Test output:', testOutput);- console.log('Test error:', testError);-- if (code === 0) {- console.log('✅ Accessibility permissions: GRANTED');- resolve(true);- } else {- // Permission denied - but let's check if this might be a "stuck permission" issue- console.log('❌ Accessibility permissions: DENIED');-- // Check if the error suggests we need to reset permissions- const isStuckPermission = testError.includes('not allowed assistive access') ||- testError.includes('(-1719)') ||- testError.includes('(-25006)');-- let dialogMessage;- if (isStuckPermission) {- dialogMessage = `🔒 OpenWispr needs Accessibility permissions, but it looks like you may have OLD PERMISSIONS from a previous version.--❗ COMMON ISSUE: If you've rebuilt/reinstalled OpenWispr, the old permissions may be "stuck" and preventing new ones.--🔧 To fix this:-1. Open System Settings → Privacy Security → Accessibility-2. Look for ANY old "OpenWispr" entries and REMOVE them (click the - button)-3. Also remove any entries that say "Electron" or have unclear names-4. Click the + button and manually add the NEW OpenWispr app-5. Make sure the checkbox is enabled-6. Restart OpenWispr--⚠️ This is especially common during development when rebuilding the app.--📝 Without this permission, text will only copy to clipboard (no automatic pasting).--Would you like to open System Settings now?`;- } else {- dialogMessage = `🔒 OpenWispr needs Accessibility permissions to paste text into other applications.--📋 Current status: Clipboard copy works, but pasting (Cmd+V simulation) fails.--🔧 To fix this:-1. Open System Settings (or System Preferences on older macOS)-2. Go to Privacy Security → Accessibility-3. Click the lock icon and enter your password-4. Add OpenWispr to the list and check the box-5. Restart OpenWispr--⚠️ Without this permission, dictated text will only be copied to clipboard but won't paste automatically.--💡 In production builds, this permission is required for full functionality.--Would you like to open System Settings now?`;- }-- const permissionDialog = spawn("osascript", [- "-e",- `display dialog "${dialogMessage}" buttons {"Cancel", "Open System Settings"} default button "Open System Settings"`- ]);-- permissionDialog.on('close', (dialogCode) => {- if (dialogCode === 0) {- // User clicked "Open System Settings" - try modern path first, then legacy- const settingsCommands = [- ["open", ["x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"]],- ["open", ["-b", "com.apple.systempreferences"]],- ["open", ["/System/Library/PreferencePanes/Security.prefPane"]]- ];-- let commandIndex = 0;- const tryNextCommand = () => {- if (commandIndex < settingsCommands.length) {- const [cmd, args] = settingsCommands[commandIndex];- const settingsProcess = spawn(cmd, args);-- settingsProcess.on('error', (error) => {- console.log(`Settings command ${commandIndex + 1} failed:`, error);- commandIndex++;- tryNextCommand();- });-- settingsProcess.on('close', (settingsCode) => {- if (settingsCode !== 0) {- console.log(`Settings command ${commandIndex + 1} failed with code:`, settingsCode);- commandIndex++;- tryNextCommand();- } else {- console.log(`✅ Opened System Settings with command ${commandIndex + 1}`);- }- });- } else {- console.log('❌ All settings commands failed, user will need to open manually');- // Try one more fallback - just open System Settings/Preferences- spawn("open", ["-a", "System Preferences"]).on('error', () => {- spawn("open", ["-a", "System Settings"]).on('error', () => {- console.log('Could not open settings app');- });- });- }- };-- tryNextCommand();- }- resolve(false);- });-- permissionDialog.on('error', (error) => {- console.error('Error showing permission dialog:', error);- console.log('Fallback: User needs to manually grant accessibility permissions');- console.log('💡 TIP: If this is a rebuilt app, remove old OpenWispr entries from Accessibility settings first');- resolve(false);- });- }- });-- testProcess.on('error', (error) => {- console.error('Error checking accessibility permissions:', error);- console.log('Fallback: Assuming permissions are needed');- resolve(false);- });- });-}--// Window control IPC handlers-ipcMain.handle('window-minimize', () => {- if (controlPanelWindow) {- controlPanelWindow.minimize();- }+ // For other errors, log and continue+ console.error("Error stack:", error.stack);});-ipcMain.handle('window-maximize', () => {- if (controlPanelWindow) {- if (controlPanelWindow.isMaximized()) {- controlPanelWindow.unmaximize();- } else {- controlPanelWindow.maximize();- }- }-});--ipcMain.handle('window-close', () => {- if (controlPanelWindow) {- controlPanelWindow.close();- }-});--ipcMain.handle('window-is-maximized', () => {- if (controlPanelWindow) {- return controlPanelWindow.isMaximized();- }- return false;+process.on("unhandledRejection", (reason, promise) => {+ console.error("Unhandled Rejection at:", promise, "reason:", reason);});-// IPC handlers-ipcMain.handle("paste-text", async (event, text) => {- try {- // Save original clipboard content first- const originalClipboard = clipboard.readText();- console.log('💾 Saved original clipboard content:', originalClipboard.substring(0, 50) + '...');-- // Copy text to clipboard first - this always works- clipboard.writeText(text);- console.log('📋 Text copied to clipboard:', text.substring(0, 50) + '...');-- if (process.platform === "darwin") {- // Check accessibility permissions first- console.log('🔍 Checking accessibility permissions for paste operation...');- const hasPermissions = await checkAccessibilityPermissions();-- if (!hasPermissions) {- console.log('⚠️ No accessibility permissions - text copied to clipboard only');- const errorMsg = 'Accessibility permissions required for automatic pasting. Text has been copied to clipboard - please paste manually with Cmd+V.';- throw new Error(errorMsg);- }-- console.log('✅ Permissions granted, attempting to paste...');-- // Use AppleScript to paste with better error handling and timeout- const { spawn } = require("child_process");- return new Promise((resolve, reject) => {- // Add a small delay to ensure clipboard is ready- setTimeout(() => {- const pasteProcess = spawn("osascript", [- "-e",- 'tell application "System Events" to keystroke "v" using command down'- ]);-- let errorOutput = '';- let hasTimedOut = false;-- pasteProcess.stderr.on('data', (data) => {- errorOutput += data.toString();- });-- pasteProcess.on('close', (code) => {- if (hasTimedOut) return; // Ignore if already timed out-- if (code === 0) {- console.log('✅ Text pasted successfully via Cmd+V simulation');- // Restore original clipboard content- setTimeout(() => {- clipboard.writeText(originalClipboard);- console.log('🔄 Original clipboard content restored');- }, 100); // Small delay to ensure paste is complete- resolve();- } else {- console.error('❌ Failed to paste text, code:', code);- console.error('Error output:', errorOutput);- const errorMsg = `Paste failed (code ${code}). Text is copied to clipboard - please paste manually with Cmd+V.`;- reject(new Error(errorMsg));- }- });-- pasteProcess.on('error', (error) => {- if (hasTimedOut) return; // Ignore if already timed out-- console.error('❌ Error running paste command:', error);- const errorMsg = `Paste command failed: ${error.message}. Text is copied to clipboard - please paste manually with Cmd+V.`;- reject(new Error(errorMsg));- });-- // Add timeout with cleanup- const timeoutId = setTimeout(() => {- hasTimedOut = true;- pasteProcess.kill('SIGKILL');- console.error('⏰ Paste operation timed out');- const errorMsg = 'Paste operation timed out. Text is copied to clipboard - please paste manually with Cmd+V.';- reject(new Error(errorMsg));- }, 3000); // Reduced timeout for better UX-- // Clear timeout if process completes normally- pasteProcess.on('close', () => {- clearTimeout(timeoutId);- });-- }, 100); // Small delay to ensure clipboard is ready- });-- } else if (process.platform === "win32") {- console.log("Simulating Ctrl+V on Windows");- return new Promise((resolve, reject) => {- const pasteProcess = spawn("powershell", [- "-Command",- 'Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait("^v")',- ]);-- pasteProcess.on('close', (code) => {- if (code === 0) {- console.log('✅ Text pasted successfully on Windows');- // Restore original clipboard content- setTimeout(() => {- clipboard.writeText(originalClipboard);- console.log('🔄 Original clipboard content restored');- }, 100);- resolve();- } else {- console.error('❌ Failed to paste on Windows, code:', code);- reject(new Error(`Windows paste failed with code ${code}. Text is copied to clipboard.`));- }- });-- pasteProcess.on('error', (error) => {- console.error('❌ Windows paste error:', error);- reject(new Error(`Windows paste failed: ${error.message}. Text is copied to clipboard.`));- });- });- } else {- // Linux- console.log("Simulating Ctrl+V on Linux");- return new Promise((resolve, reject) => {- const pasteProcess = spawn("xdotool", ["key", "ctrl+v"]);-- pasteProcess.on('close', (code) => {- if (code === 0) {- console.log('✅ Text pasted successfully on Linux');- // Restore original clipboard content- setTimeout(() => {- clipboard.writeText(originalClipboard);- console.log('🔄 Original clipboard content restored');- }, 100);- resolve();- } else {- console.error('❌ Failed to paste on Linux, code:', code);- reject(new Error(`Linux paste failed with code ${code}. Text is copied to clipboard.`));- }- });-- pasteProcess.on('error', (error) => {- console.error('❌ Linux paste error:', error);- reject(new Error(`Linux paste failed: ${error.message}. Text is copied to clipboard.`));- });- });- }- } catch (error) {- console.error('❌ Paste error:', error);- throw error;- }-})--ipcMain.handle("hide-window", () => {- if (process.platform === "darwin") {- mainWindow.minimize();- if (app.dock) app.dock.show();- } else {- mainWindow.hide();- }-})--// Database IPC handlers-ipcMain.handle('db-save-transcription', async (event, text) => {- try {- if (!db) {- throw new Error('Database not initialized');- }- const stmt = db.prepare('INSERT INTO transcriptions (text) VALUES (?)');- const result = stmt.run(text);-- const truncatedText = text.length > 50 ? text.substring(0, 50) + '...' : text;- console.log(`📝 Transcription saved to ${process.env.NODE_ENV || 'production'} DB:`, {- id: result.lastInsertRowid,- text: truncatedText,- length: text.length- });-- return { id: result.lastInsertRowid, success: true };- } catch (error) {- console.error('❌ Error saving transcription:', error);- throw error;- }-});+// Import helper modules+const EnvironmentManager = require("./src/helpers/environment");+const WindowManager = require("./src/helpers/windowManager");+const DatabaseManager = require("./src/helpers/database");+const ClipboardManager = require("./src/helpers/clipboard");+const WhisperManager = require("./src/helpers/whisper");+const TrayManager = require("./src/helpers/tray");+const IPCHandlers = require("./src/helpers/ipcHandlers");+const UpdateManager = require("./src/updater");-ipcMain.handle('db-get-transcriptions', async (event, limit = 50) => {- try {- if (!db) {- throw new Error('Database not initialized');- }- const stmt = db.prepare('SELECT * FROM transcriptions ORDER BY timestamp DESC LIMIT ?');- const transcriptions = stmt.all(limit);- console.log(`📚 Retrieved ${transcriptions.length} transcriptions from ${process.env.NODE_ENV || 'production'} DB (limit: ${limit})`);- return transcriptions;- } catch (error) {- console.error('❌ Error getting transcriptions:', error);- throw error;- }-});+// Initialize managers+const environmentManager = new EnvironmentManager();+const windowManager = new WindowManager();+const databaseManager = new DatabaseManager();+const clipboardManager = new ClipboardManager();+const whisperManager = new WhisperManager();+const trayManager = new TrayManager();+const updateManager = new UpdateManager();-ipcMain.handle('db-clear-transcriptions', async (event) => {- try {- if (!db) {- throw new Error('Database not initialized');- }- const stmt = db.prepare('DELETE FROM transcriptions');- const result = stmt.run();- console.log(`🗑️ Cleared ${result.changes} transcriptions from ${process.env.NODE_ENV || 'production'} DB`);- return { cleared: result.changes, success: true };- } catch (error) {- console.error('❌ Error clearing transcriptions:', error);- throw error;- }+// Initialize IPC handlers with all managers+const ipcHandlers = new IPCHandlers({+ environmentManager,+ databaseManager,+ clipboardManager,+ whisperManager,+ windowManager,});-ipcMain.handle('db-delete-transcription', async (event, id) => {- try {- if (!db) {- throw new Error('Database not initialized');- }- const stmt = db.prepare('DELETE FROM transcriptions WHERE id = ?');- const result = stmt.run(id);- console.log(`🗑️ Deleted transcription ${id}, affected rows: ${result.changes}`);- return { success: result.changes > 0 };- } catch (error) {- console.error('❌ Error deleting transcription:', error);- throw error;+// Main application startup+async function startApp() {+ // In development, add a small delay to let Vite start properly+ if (process.env.NODE_ENV === "development") {+ await new Promise((resolve) => setTimeout(resolve, 2000));}-});-// Environment variable handlers-ipcMain.handle('get-openai-key', async (event) => {- const apiKey = process.env.OPENAI_API_KEY;- console.log('🔑 OpenAI API Key requested:', apiKey ? 'Present' : 'Missing');- return apiKey || '';-});--ipcMain.handle('save-openai-key', async (event, key) => {+ // Create main windowtry {- // Update the environment variable in memory for immediate use- process.env.OPENAI_API_KEY = key;- console.log('🔑 OpenAI API Key updated in memory');- return { success: true };+ await windowManager.createMainWindow();} catch (error) {- console.error('❌ Error saving OpenAI API key:', error);- throw error;+ console.error("Error creating main window:", error);}-});-// Clipboard handler for better paste support-ipcMain.handle('read-clipboard', async (event) => {+ // Create control panel windowtry {- const text = clipboard.readText();- console.log('📋 Clipboard read:', text ? 'Text found' : 'Empty');- return text;+ await windowManager.createControlPanelWindow();} catch (error) {- console.error('❌ Error reading clipboard:', error);- throw error;+ console.error("Error creating control panel window:", error);}-});-// Production .env file creation handler-ipcMain.handle('create-production-env-file', async (event, apiKey) => {- try {- const fs = require('fs');- const envPath = path.join(app.getPath('userData'), '.env');-- const envContent = `# OpenWispr Environment Variables-# This file was created automatically for production use-OPENAI_API_KEY=${apiKey}-`;-- fs.writeFileSync(envPath, envContent, 'utf8');- console.log('✅ Production .env file created at:', envPath);-- // Reload environment variables- require('dotenv').config({ path: envPath });- console.log('🔄 Environment variables reloaded from production .env file');-- return { success: true, path: envPath };- } catch (error) {- console.error('❌ Error creating production .env file:', error);- throw error;- }-});+ // Set up tray+ trayManager.setWindows(+ windowManager.mainWindow,+ windowManager.controlPanelWindow+ );+ trayManager.setCreateControlPanelCallback(() =>+ windowManager.createControlPanelWindow()+ );+ await trayManager.createTray();-// Local Whisper transcription handler-ipcMain.handle('transcribe-local-whisper', async (event, audioBlob, options = {}) => {- try {- console.log('🎤 Starting local Whisper transcription...');-- // Create temporary file for audio- const fs = require('fs');- const os = require('os');- const crypto = require('crypto');-- const tempDir = os.tmpdir();- const filename = `whisper_audio_${crypto.randomUUID()}.wav`;- const tempAudioPath = path.join(tempDir, filename);-- // Convert Blob to Buffer and write to temp file- console.log('💾 Writing audio to temp file:', tempAudioPath);-- // Handle different audio data formats- let buffer;- if (audioBlob instanceof ArrayBuffer) {- buffer = Buffer.from(audioBlob);- } else if (audioBlob instanceof Uint8Array) {- buffer = Buffer.from(audioBlob);- } else if (typeof audioBlob === 'string') {- // Base64 encoded audio data- buffer = Buffer.from(audioBlob, 'base64');- } else if (audioBlob && audioBlob.buffer) {- // TypedArray with buffer property- buffer = Buffer.from(audioBlob.buffer);- } else {- throw new Error(`Unsupported audio data type: ${typeof audioBlob}`);- }-- fs.writeFileSync(tempAudioPath, buffer);-- // Prepare Whisper command- const model = options.model || 'base';- const language = options.language || null;-- // Find Python executable- const pythonCmd = await findPythonExecutable();- const whisperScriptPath = path.join(__dirname, 'whisper_bridge.py');-- const args = [whisperScriptPath, tempAudioPath, '--model', model];- if (language) {- args.push('--language', language);- }- args.push('--output-format', 'json');-- console.log('🔧 Running Whisper command:', pythonCmd, args.join(' '));-- // Execute Whisper transcription- return new Promise((resolve, reject) => {- const whisperProcess = spawn(pythonCmd, args);-- let stdout = '';- let stderr = '';-- whisperProcess.stdout.on('data', (data) => {- stdout += data.toString();- });-- whisperProcess.stderr.on('data', (data) => {- stderr += data.toString();- console.log('Whisper log:', data.toString());- });-- whisperProcess.on('close', (code) => {- // Clean up temp file- try {- fs.unlinkSync(tempAudioPath);- console.log('🗑️ Cleaned up temp audio file');- } catch (cleanupError) {- console.warn('⚠️ Could not clean up temp file:', cleanupError.message);- }-- if (code === 0) {- try {- const result = JSON.parse(stdout);-- // Check if the transcription is empty or just whitespace- if (!result.text || result.text.trim().length === 0) {- console.log('🛑 No meaningful audio content detected (empty transcription)');- event.sender.send('no-audio-detected');- resolve({ success: false, message: 'No audio detected' });- return;- }-- console.log('✅ Whisper transcription successful:', result.text?.substring(0, 50) + '...');- resolve(result);- } catch (parseError) {- console.error('❌ Failed to parse Whisper output:', parseError);- reject(new Error(`Failed to parse Whisper output: ${parseError.message}`));- }- } else {- if (stderr.includes('no audio') || stderr.includes('empty')) {- console.log('🛑 No audio content detected');- event.sender.send('no-audio-detected'); // Could trigger a tooltip in the UI- resolve({ success: false, message: 'No audio detected' });- } else {- console.error('❌ Whisper process failed with code:', code);- console.error('Stderr:', stderr);- reject(new Error(`Whisper transcription failed (code ${code}): ${stderr}`));- }- }- });-- whisperProcess.on('error', (error) => {- // Clean up temp file- try {- fs.unlinkSync(tempAudioPath);- } catch (cleanupError) {- console.warn('⚠️ Could not clean up temp file:', cleanupError.message);- }-- console.error('❌ Whisper process error:', error);- reject(new Error(`Whisper process error: ${error.message}`));- });-- // Add timeout (30 seconds for transcription)- setTimeout(() => {- whisperProcess.kill('SIGTERM');- try {- fs.unlinkSync(tempAudioPath);- } catch (cleanupError) {- console.warn('⚠️ Could not clean up temp file:', cleanupError.message);- }- reject(new Error('Whisper transcription timed out (30 seconds)'));- }, 30000);- });-- } catch (error) {- console.error('❌ Local Whisper transcription error:', error);- throw error;- }-});--// Helper function to find Python executable-async function findPythonExecutable() {- const possiblePaths = [- 'python3',- 'python',- '/usr/bin/python3',- '/usr/local/bin/python3',- '/opt/homebrew/bin/python3',- '/usr/bin/python',- '/usr/local/bin/python'- ];-- for (const pythonPath of possiblePaths) {- try {- const result = await new Promise((resolve, reject) => {- const testProcess = spawn(pythonPath, ['--version']);- testProcess.on('close', (code) => {- resolve(code === 0);- });- testProcess.on('error', () => {- resolve(false);- });- });-- if (result) {- console.log('🐍 Found Python at:', pythonPath);- return pythonPath;- }- } catch (error) {- continue;- }- }-- throw new Error('Python executable not found. Please ensure Python 3 is installed.');+ // Set windows for update manager and check for updates+ updateManager.setWindows(+ windowManager.mainWindow,+ windowManager.controlPanelWindow+ );+ updateManager.checkForUpdatesOnStartup();}-// Check Whisper installation-ipcMain.handle('check-whisper-installation', async (event) => {- try {- const pythonCmd = await findPythonExecutable();-- return new Promise((resolve) => {- const checkProcess = spawn(pythonCmd, ['-c', 'import whisper; print("OK")']);-- let output = '';- checkProcess.stdout.on('data', (data) => {- output += data.toString();- });-- checkProcess.on('close', (code) => {- if (code === 0 && output.includes('OK')) {- console.log('✅ Whisper is installed and working');- resolve({ installed: true, working: true });- } else {- console.log('❌ Whisper is not properly installed');- resolve({ installed: false, working: false });- }- });-- checkProcess.on('error', (error) => {- console.log('❌ Error checking Whisper:', error.message);- resolve({ installed: false, working: false, error: error.message });- });- });-- } catch (error) {- console.log('❌ Error finding Python:', error.message);- return { installed: false, working: false, error: error.message };- }-});--// Install Whisper automatically-ipcMain.handle('install-whisper', async (event) => {- try {- console.log('🔧 Starting automatic Whisper installation...');-- const pythonCmd = await findPythonExecutable();- console.log('🐍 Using Python:', pythonCmd);-- // Install Whisper using pip- const args = ['-m', 'pip', 'install', '-U', 'openai-whisper'];-- console.log('📦 Running installation command:', pythonCmd, args.join(' '));-- return new Promise((resolve, reject) => {- const installProcess = spawn(pythonCmd, args);-- let stdout = '';- let stderr = '';-- installProcess.stdout.on('data', (data) => {- const output = data.toString();- stdout += output;- console.log('Install output:', output);-- // Send progress updates to the renderer- if (output.includes('Downloading') || output.includes('Installing')) {- // Extract package name if possible- const match = output.match(/(\w+[-\w]*)/);- const packageName = match ? match[1] : 'package';- event.sender.send('whisper-install-progress', {- type: 'progress',- message: `Installing ${packageName}...`,- output: output.trim()- });- }- });-- installProcess.stderr.on('data', (data) => {- const output = data.toString();- stderr += output;- console.log('Install stderr:', output);-- // Send progress updates for stderr too (pip sometimes uses stderr for progress)- if (output.includes('Downloading') || output.includes('Installing') || output.includes('Collecting')) {- event.sender.send('whisper-install-progress', {- type: 'progress',- message: output.trim()- });- }- });-- installProcess.on('close', (code) => {- if (code === 0) {- console.log('✅ Whisper installation completed successfully');- resolve({- success: true,- message: 'Whisper installed successfully!',- output: stdout- });- } else {- console.error('❌ Whisper installation failed with code:', code);- console.error('Installation stderr:', stderr);- reject(new Error(`Whisper installation failed (code ${code}): ${stderr}`));- }- });-- installProcess.on('error', (error) => {- console.error('❌ Whisper installation process error:', error);- reject(new Error(`Whisper installation process error: ${error.message}`));- });-- // 10 minute timeout for installation- setTimeout(() => {- installProcess.kill('SIGTERM');- reject(new Error('Whisper installation timed out (10 minutes)'));- }, 600000);- });-- } catch (error) {- console.error('❌ Whisper installation error:', error);- throw error;- }-});+// App event handlers+app.whenReady().then(startApp);-// Download Whisper model-ipcMain.handle('download-whisper-model', async (event, modelName) => {- try {- console.log(`📥 Starting download of Whisper model: ${modelName}`);-- const pythonCmd = await findPythonExecutable();- const whisperScriptPath = path.join(__dirname, 'whisper_bridge.py');-- const args = [whisperScriptPath, '--mode', 'download', '--model', modelName];-- console.log('🔧 Running model download command:', pythonCmd, args.join(' '));-- return new Promise((resolve, reject) => {- const downloadProcess = spawn(pythonCmd, args);-- let stdout = '';- let stderr = '';-- downloadProcess.stdout.on('data', (data) => {- stdout += data.toString();- });-- downloadProcess.stderr.on('data', (data) => {- const output = data.toString();- stderr += output;- console.log('Model download log:', output);-- // Parse progress messages- const lines = output.split('\n');- for (const line of lines) {- if (line.startsWith('PROGRESS:')) {- try {- const progressData = JSON.parse(line.substring(9)); // Remove 'PROGRESS:' prefix- console.log('📊 Download progress:', progressData);-- // Forward progress to renderer- event.sender.send('whisper-download-progress', {- type: 'progress',- model: modelName,- ...progressData- });- } catch (parseError) {- console.error('Failed to parse progress data:', parseError);- }- }- }- });-- downloadProcess.on('close', (code) => {- if (code === 0) {- try {- const result = JSON.parse(stdout);- console.log(`✅ Model ${modelName} download completed:`, result);-- // Send final completion event- event.sender.send('whisper-download-progress', {- type: 'complete',- model: modelName,- result: result- });-- resolve(result);- } catch (parseError) {- console.error('❌ Failed to parse download result:', parseError);- reject(new Error(`Failed to parse download result: ${parseError.message}`));- }- } else {- console.error('❌ Model download failed with code:', code);- console.error('Stderr:', stderr);-- // Send error event- event.sender.send('whisper-download-progress', {- type: 'error',- model: modelName,- error: `Model download failed (code ${code}): ${stderr}`- });-- reject(new Error(`Model download failed (code ${code}): ${stderr}`));- }- });-- downloadProcess.on('error', (error) => {- console.error('❌ Model download process error:', error);-- // Send error event- event.sender.send('whisper-download-progress', {- type: 'error',- model: modelName,- error: `Model download process error: ${error.message}`- });-- reject(new Error(`Model download process error: ${error.message}`));- });-- // Add longer timeout for model downloads (20 minutes for large models)- const timeout = setTimeout(() => {- console.warn('⚠️ Model download timed out, attempting graceful shutdown...');- downloadProcess.kill('SIGTERM');-- // Give it 5 seconds to cleanup gracefully, then force kill- setTimeout(() => {- if (!downloadProcess.killed) {- console.warn('⚠️ Force killing download process...');- downloadProcess.kill('SIGKILL');- }- }, 5000);-- reject(new Error('Model download timed out (20 minutes)'));- }, 1200000); // 20 minutes-- // Clear timeout on successful completion- downloadProcess.on('close', () => {- clearTimeout(timeout);- });- });-- } catch (error) {- console.error('❌ Model download error:', error);- throw error;- }-});--// Check model status-ipcMain.handle('check-model-status', async (event, modelName) => {- try {- console.log(`🔍 Checking status of Whisper model: ${modelName}`);-- const pythonCmd = await findPythonExecutable();- const whisperScriptPath = path.join(__dirname, 'whisper_bridge.py');-- const args = [whisperScriptPath, '--mode', 'check', '--model', modelName];-- return new Promise((resolve, reject) => {- const checkProcess = spawn(pythonCmd, args);-- let stdout = '';- let stderr = '';-- checkProcess.stdout.on('data', (data) => {- stdout += data.toString();- });-- checkProcess.stderr.on('data', (data) => {- stderr += data.toString();- });-- checkProcess.on('close', (code) => {- if (code === 0) {- try {- const result = JSON.parse(stdout);- console.log(`📊 Model ${modelName} status:`, result);- resolve(result);- } catch (parseError) {- console.error('❌ Failed to parse model status:', parseError);- reject(new Error(`Failed to parse model status: ${parseError.message}`));- }- } else {- console.error('❌ Model status check failed with code:', code);- reject(new Error(`Model status check failed (code ${code}): ${stderr}`));- }- });-- checkProcess.on('error', (error) => {- console.error('❌ Model status check error:', error);- reject(new Error(`Model status check error: ${error.message}`));- });- });-- } catch (error) {- console.error('❌ Model status check error:', error);- throw error;- }-});--// List all models and their status-ipcMain.handle('list-whisper-models', async (event) => {- try {- console.log('📋 Listing all Whisper models...');-- const pythonCmd = await findPythonExecutable();- const whisperScriptPath = path.join(__dirname, 'whisper_bridge.py');-- const args = [whisperScriptPath, '--mode', 'list'];-- return new Promise((resolve, reject) => {- const listProcess = spawn(pythonCmd, args);-- let stdout = '';- let stderr = '';-- listProcess.stdout.on('data', (data) => {- stdout += data.toString();- });-- listProcess.stderr.on('data', (data) => {- stderr += data.toString();- });-- listProcess.on('close', (code) => {- if (code === 0) {- try {- const result = JSON.parse(stdout);- console.log('📋 Model list retrieved:', result);- resolve(result);- } catch (parseError) {- console.error('❌ Failed to parse model list:', parseError);- reject(new Error(`Failed to parse model list: ${parseError.message}`));- }- } else {- console.error('❌ Model list failed with code:', code);- reject(new Error(`Model list failed (code ${code}): ${stderr}`));- }- });-- listProcess.on('error', (error) => {- console.error('❌ Model list error:', error);- reject(new Error(`Model list error: ${error.message}`));- });- });-- } catch (error) {- console.error('❌ Model list error:', error);- throw error;+app.on("window-all-closed", () => {+ if (process.platform !== "darwin") {+ app.quit();}});-// Delete Whisper model-ipcMain.handle('delete-whisper-model', async (event, modelName) => {- try {- console.log(`🗑️ Deleting Whisper model: ${modelName}`);-- const pythonCmd = await findPythonExecutable();- const whisperScriptPath = path.join(__dirname, 'whisper_bridge.py');-- const args = [whisperScriptPath, '--mode', 'delete', '--model', modelName];-- return new Promise((resolve, reject) => {- const deleteProcess = spawn(pythonCmd, args);-- let stdout = '';- let stderr = '';-- deleteProcess.stdout.on('data', (data) => {- stdout += data.toString();- });-- deleteProcess.stderr.on('data', (data) => {- stderr += data.toString();- });-- deleteProcess.on('close', (code) => {- if (code === 0) {- try {- const result = JSON.parse(stdout);- console.log(`🗑️ Model ${modelName} delete result:`, result);- resolve(result);- } catch (parseError) {- console.error('❌ Failed to parse delete result:', parseError);- reject(new Error(`Failed to parse delete result: ${parseError.message}`));- }- } else {- console.error('❌ Model delete failed with code:', code);- reject(new Error(`Model delete failed (code ${code}): ${stderr}`));- }- });-- deleteProcess.on('error', (error) => {- console.error('❌ Model delete error:', error);- reject(new Error(`Model delete error: ${error.message}`));- });- });-- } catch (error) {- console.error('❌ Model delete error:', error);- throw error;+app.on("activate", () => {+ if (BrowserWindow.getAllWindows().length === 0) {+ windowManager.createMainWindow();}});-// Cleanup handler-ipcMain.handle('cleanup-app', async (event) => {- try {- cleanup();- return { success: true, message: 'Cleanup completed successfully' };- } catch (error) {- console.error('❌ Cleanup error:', error);- throw error;- }+app.on("will-quit", () => {+ globalShortcut.unregisterAll();});-diff --git a/src/helpers/clipboard.js b/src/helpers/clipboard.jsnew file mode 100644index 000000000..a96ea4be2--- /dev/null+++ b/src/helpers/clipboard.js@@ -0,0 +1,376 @@+const { clipboard } = require("electron");+const { spawn } = require("child_process");++class ClipboardManager {+ constructor() {+ // Initialize clipboard manager+ }++ // Safe logging method to prevent EPIPE errors+ safeLog(...args) {+ try {+ console.log(...args);+ } catch (error) {+ // Silently ignore EPIPE errors in logging+ if (error.code !== "EPIPE") {+ // Only log non-EPIPE errors to stderr+ process.stderr.write(`Log error: ${error.message}\n`);+ }+ }+ }++ async pasteText(text) {+ try {+ // Save original clipboard content first+ const originalClipboard = clipboard.readText();+ this.safeLog(+ "💾 Saved original clipboard content:",+ originalClipboard.substring(0, 50) + "..."+ );++ // Copy text to clipboard first - this always works+ clipboard.writeText(text);+ this.safeLog(+ "📋 Text copied to clipboard:",+ text.substring(0, 50) + "..."+ );++ if (process.platform === "darwin") {+ // Check accessibility permissions first+ this.safeLog(+ "🔍 Checking accessibility permissions for paste operation..."+ );+ const hasPermissions = await this.checkAccessibilityPermissions();++ if (!hasPermissions) {+ this.safeLog(+ "⚠️ No accessibility permissions - text copied to clipboard only"+ );+ const errorMsg =+ "Accessibility permissions required for automatic pasting. Text has been copied to clipboard - please paste manually with Cmd+V.";+ throw new Error(errorMsg);+ }++ this.safeLog("✅ Permissions granted, attempting to paste...");+ return await this.pasteMacOS(originalClipboard);+ } else if (process.platform === "win32") {+ return await this.pasteWindows(originalClipboard);+ } else {+ return await this.pasteLinux(originalClipboard);+ }+ } catch (error) {+ console.error("❌ Paste error:", error);+ throw error;+ }+ }++ async pasteMacOS(originalClipboard) {+ console.log("🍎 Attempting macOS paste...");+ return new Promise((resolve, reject) => {+ setTimeout(() => {+ const pasteProcess = spawn("osascript", [+ "-e",+ 'tell application "System Events" to keystroke "v" using command down',+ ]);++ let errorOutput = "";+ let hasTimedOut = false;++ pasteProcess.stderr.on("data", (data) => {+ errorOutput += data.toString();+ });++ pasteProcess.on("close", (code) => {+ if (hasTimedOut) return;++ // Clear timeout first+ clearTimeout(timeoutId);++ // Clean up the process reference+ pasteProcess.removeAllListeners();++ if (code === 0) {+ this.safeLog("✅ Text pasted successfully via Cmd+V simulation");+ setTimeout(() => {+ clipboard.writeText(originalClipboard);+ this.safeLog("🔄 Original clipboard content restored");+ }, 100);+ resolve();+ } else {+ console.error("❌ Failed to paste text, code:", code);+ console.error("Error output:", errorOutput);+ const errorMsg = `Paste failed (code ${code}). Text is copied to clipboard - please paste manually with Cmd+V.`;+ reject(new Error(errorMsg));+ }+ });++ pasteProcess.on("error", (error) => {+ if (hasTimedOut) return;+ clearTimeout(timeoutId);+ pasteProcess.removeAllListeners();+ console.error("❌ Error running paste command:", error);+ const errorMsg = `Paste command failed: ${error.message}. Text is copied to clipboard - please paste manually with Cmd+V.`;+ reject(new Error(errorMsg));+ });++ const timeoutId = setTimeout(() => {+ hasTimedOut = true;+ pasteProcess.kill("SIGKILL");+ pasteProcess.removeAllListeners();+ console.error("⏰ Paste operation timed out");+ const errorMsg =+ "Paste operation timed out. Text is copied to clipboard - please paste manually with Cmd+V.";+ reject(new Error(errorMsg));+ }, 3000);+ }, 100);+ });+ }++ async pasteWindows(originalClipboard) {+ console.log("🪟 Simulating Ctrl+V on Windows");+ return new Promise((resolve, reject) => {+ const pasteProcess = spawn("powershell", [+ "-Command",+ 'Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait("^v")',+ ]);++ pasteProcess.on("close", (code) => {+ if (code === 0) {+ console.log("✅ Text pasted successfully on Windows");+ setTimeout(() => {+ clipboard.writeText(originalClipboard);+ console.log("🔄 Original clipboard content restored");+ }, 100);+ resolve();+ } else {+ console.error("❌ Failed to paste on Windows, code:", code);+ reject(+ new Error(+ `Windows paste failed with code ${code}. Text is copied to clipboard.`+ )+ );+ }+ });++ pasteProcess.on("error", (error) => {+ console.error("❌ Windows paste error:", error);+ reject(+ new Error(+ `Windows paste failed: ${error.message}. Text is copied to clipboard.`+ )+ );+ });+ });+ }++ async pasteLinux(originalClipboard) {+ console.log("🐧 Simulating Ctrl+V on Linux");+ return new Promise((resolve, reject) => {+ const pasteProcess = spawn("xdotool", ["key", "ctrl+v"]);++ pasteProcess.on("close", (code) => {+ if (code === 0) {+ console.log("✅ Text pasted successfully on Linux");+ setTimeout(() => {+ clipboard.writeText(originalClipboard);+ console.log("🔄 Original clipboard content restored");+ }, 100);+ resolve();+ } else {+ console.error("❌ Failed to paste on Linux, code:", code);+ reject(+ new Error(+ `Linux paste failed with code ${code}. Text is copied to clipboard.`+ )+ );+ }+ });++ pasteProcess.on("error", (error) => {+ console.error("❌ Linux paste error:", error);+ reject(+ new Error(+ `Linux paste failed: ${error.message}. Text is copied to clipboard.`+ )+ );+ });+ });+ }++ async checkAccessibilityPermissions() {+ if (process.platform !== "darwin") return true;++ return new Promise((resolve) => {+ console.log("🔍 Checking accessibility permissions...");++ const testProcess = spawn("osascript", [+ "-e",+ 'tell application "System Events" to get name of first process',+ ]);++ let testOutput = "";+ let testError = "";++ testProcess.stdout.on("data", (data) => {+ testOutput += data.toString();+ });++ testProcess.stderr.on("data", (data) => {+ testError += data.toString();+ });++ testProcess.on("close", (code) => {+ console.log("Initial accessibility test - Code:", code);+ console.log("Test output:", testOutput);+ console.log("Test error:", testError);++ if (code === 0) {+ console.log("✅ Accessibility permissions: GRANTED");+ resolve(true);+ } else {+ console.log("❌ Accessibility permissions: DENIED");+ this.showAccessibilityDialog(testError);+ resolve(false);+ }+ });++ testProcess.on("error", (error) => {+ console.error("Error checking accessibility permissions:", error);+ console.log("Fallback: Assuming permissions are needed");+ resolve(false);+ });+ });+ }++ showAccessibilityDialog(testError) {+ const isStuckPermission =+ testError.includes("not allowed assistive access") ||+ testError.includes("(-1719)") ||+ testError.includes("(-25006)");++ let dialogMessage;+ if (isStuckPermission) {+ dialogMessage = `🔒 OpenWispr needs Accessibility permissions, but it looks like you may have OLD PERMISSIONS from a previous version.++❗ COMMON ISSUE: If you've rebuilt/reinstalled OpenWispr, the old permissions may be "stuck" and preventing new ones.++🔧 To fix this:+1. Open System Settings → Privacy & Security → Accessibility+2. Look for ANY old "OpenWispr" entries and REMOVE them (click the - button)+3. Also remove any entries that say "Electron" or have unclear names+4. Click the + button and manually add the NEW OpenWispr app+5. Make sure the checkbox is enabled+6. Restart OpenWispr++⚠️ This is especially common during development when rebuilding the app.++📝 Without this permission, text will only copy to clipboard (no automatic pasting).++Would you like to open System Settings now?`;+ } else {+ dialogMessage = `🔒 OpenWispr needs Accessibility permissions to paste text into other applications.++📋 Current status: Clipboard copy works, but pasting (Cmd+V simulation) fails.++🔧 To fix this:+1. Open System Settings (or System Preferences on older macOS)+2. Go to Privacy & Security → Accessibility+3. Click the lock icon and enter your password+4. Add OpenWispr to the list and check the box+5. Restart OpenWispr++⚠️ Without this permission, dictated text will only be copied to clipboard but won't paste automatically.++💡 In production builds, this permission is required for full functionality.++Would you like to open System Settings now?`;+ }++ const permissionDialog = spawn("osascript", [+ "-e",+ `display dialog "${dialogMessage}" buttons {"Cancel", "Open System Settings"} default button "Open System Settings"`,+ ]);++ permissionDialog.on("close", (dialogCode) => {+ if (dialogCode === 0) {+ this.openSystemSettings();+ }+ });++ permissionDialog.on("error", (error) => {+ console.error("Error showing permission dialog:", error);+ console.log(+ "Fallback: User needs to manually grant accessibility permissions"+ );+ console.log(+ "💡 TIP: If this is a rebuilt app, remove old OpenWispr entries from Accessibility settings first"+ );+ });+ }++ openSystemSettings() {+ const settingsCommands = [+ [+ "open",+ [+ "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility",+ ],+ ],+ ["open", ["-b", "com.apple.systempreferences"]],+ ["open", ["/System/Library/PreferencePanes/Security.prefPane"]],+ ];++ let commandIndex = 0;+ const tryNextCommand = () => {+ if (commandIndex < settingsCommands.length) {+ const [cmd, args] = settingsCommands[commandIndex];+ const settingsProcess = spawn(cmd, args);++ settingsProcess.on("error", (error) => {+ console.log(`Settings command ${commandIndex + 1} failed:`, error);+ commandIndex++;+ tryNextCommand();+ });++ settingsProcess.on("close", (settingsCode) => {+ if (settingsCode !== 0) {+ console.log(+ `Settings command ${commandIndex + 1} failed with code:`,+ settingsCode+ );+ commandIndex++;+ tryNextCommand();+ } else {+ console.log(+ `✅ Opened System Settings with command ${commandIndex + 1}`+ );+ }+ });+ } else {+ console.log(+ "❌ All settings commands failed, user will need to open manually"+ );+ spawn("open", ["-a", "System Preferences"]).on("error", () => {+ spawn("open", ["-a", "System Settings"]).on("error", () => {+ console.log("Could not open settings app");+ });+ });+ }+ };++ tryNextCommand();+ }++ readClipboard() {+ try {+ const text = clipboard.readText();+ console.log("📋 Clipboard read:", text ? "Text found" : "Empty");+ return text;+ } catch (error) {+ console.error("❌ Error reading clipboard:", error);+ throw error;+ }+ }+}++module.exports = ClipboardManager;diff --git a/src/helpers/database.js b/src/helpers/database.jsnew file mode 100644index 000000000..5febfdaa6--- /dev/null+++ b/src/helpers/database.js@@ -0,0 +1,164 @@+const Database = require("better-sqlite3");+const path = require("path");+const fs = require("fs");+const os = require("os");+const { app } = require("electron");++class DatabaseManager {+ constructor() {+ this.db = null;+ this.initDatabase();+ }++ initDatabase() {+ try {+ console.log("🔄 Starting database initialization...");++ const dbFileName =+ process.env.NODE_ENV === "development"+ ? "transcriptions-dev.db"+ : "transcriptions.db";++ const dbPath = path.join(app.getPath("userData"), dbFileName);+ console.log("📁 Database path:", dbPath);++ console.log("🔧 Creating database connection...");+ this.db = new Database(dbPath);+ console.log("✅ Database connection created");++ console.log("📋 Creating transcriptions table...");+ this.db.exec(`+ CREATE TABLE IF NOT EXISTS transcriptions (+ id INTEGER PRIMARY KEY AUTOINCREMENT,+ text TEXT NOT NULL,+ timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP+ )+ `);+ console.log("✅ Transcriptions table created/verified");++ console.log("📊 Counting existing transcriptions...");+ const countStmt = this.db.prepare(+ "SELECT COUNT(*) as count FROM transcriptions"+ );+ const { count } = countStmt.get();++ console.log(+ `✅ Database initialized successfully (${count} existing transcriptions)`+ );+ return true;+ } catch (error) {+ console.error("❌ Database initialization failed:", error);+ console.error("Error details:", {+ message: error.message,+ stack: error.stack,+ });+ throw error;+ }+ }++ saveTranscription(text) {+ try {+ if (!this.db) {+ throw new Error("Database not initialized");+ }+ const stmt = this.db.prepare(+ "INSERT INTO transcriptions (text) VALUES (?)"+ );+ const result = stmt.run(text);++ const truncatedText =+ text.length > 50 ? text.substring(0, 50) + "..." : text;+ console.log(+ `📝 Transcription saved to ${process.env.NODE_ENV || "production"} DB:`,+ {+ id: result.lastInsertRowid,+ text: truncatedText,+ length: text.length,+ }+ );++ return { id: result.lastInsertRowid, success: true };+ } catch (error) {+ console.error("❌ Error saving transcription:", error);+ throw error;+ }+ }++ getTranscriptions(limit = 50) {+ try {+ if (!this.db) {+ throw new Error("Database not initialized");+ }+ const stmt = this.db.prepare(+ "SELECT * FROM transcriptions ORDER BY timestamp DESC LIMIT ?"+ );+ const transcriptions = stmt.all(limit);+ console.log(+ `📚 Retrieved ${transcriptions.length} transcriptions from ${+ process.env.NODE_ENV || "production"+ } DB (limit: ${limit})`+ );+ return transcriptions;+ } catch (error) {+ console.error("❌ Error getting transcriptions:", error);+ throw error;+ }+ }++ clearTranscriptions() {+ try {+ if (!this.db) {+ throw new Error("Database not initialized");+ }+ const stmt = this.db.prepare("DELETE FROM transcriptions");+ const result = stmt.run();+ console.log(+ `🗑️ Cleared ${result.changes} transcriptions from ${+ process.env.NODE_ENV || "production"+ } DB`+ );+ return { cleared: result.changes, success: true };+ } catch (error) {+ console.error("❌ Error clearing transcriptions:", error);+ throw error;+ }+ }++ deleteTranscription(id) {+ try {+ if (!this.db) {+ throw new Error("Database not initialized");+ }+ const stmt = this.db.prepare("DELETE FROM transcriptions WHERE id = ?");+ const result = stmt.run(id);+ console.log(+ `🗑️ Deleted transcription ${id}, affected rows: ${result.changes}`+ );+ return { success: result.changes > 0 };+ } catch (error) {+ console.error("❌ Error deleting transcription:", error);+ throw error;+ }+ }++ cleanup() {+ console.log("Starting database cleanup...");+ try {+ const dbPath = path.join(+ app.getPath("userData"),+ process.env.NODE_ENV === "development"+ ? "transcriptions-dev.db"+ : "transcriptions.db"+ );+ if (fs.existsSync(dbPath)) {+ fs.unlinkSync(dbPath);+ console.log("✅ Database file deleted:", dbPath);+ }+ } catch (error) {+ console.error("❌ Error deleting database file:", error);+ }+ }+}++module.exports = DatabaseManager;diff --git a/src/helpers/environment.js b/src/helpers/environment.jsnew file mode 100644index 000000000..d265fb770--- /dev/null+++ b/src/helpers/environment.js@@ -0,0 +1,89 @@+const path = require("path");+const fs = require("fs");+const { app } = require("electron");++class EnvironmentManager {+ constructor() {+ this.loadEnvironmentVariables();+ }++ loadEnvironmentVariables() {+ // In production, try multiple locations for .env file+ const possibleEnvPaths = [+ // Development path+ path.join(__dirname, "..", ".env"),+ // Production packaged app paths+ path.join(process.resourcesPath, ".env"),+ path.join(process.resourcesPath, "app.asar.unpacked", ".env"),+ path.join(app.getPath("userData"), ".env"), // User data directory+ // Legacy paths+ path.join(process.resourcesPath, "app", ".env"),+ ];++ let envLoaded = false;++ for (const envPath of possibleEnvPaths) {+ try {+ if (fs.existsSync(envPath)) {+ const result = require("dotenv").config({ path: envPath });+ if (!result.error) {+ console.log("✅ Environment loaded from:", envPath);+ envLoaded = true;+ break;+ }+ }+ } catch (error) {+ // Continue to next path+ }+ }++ if (!envLoaded) {+ console.log("⚠️ No .env file found in any expected location");+ }+ }++ getOpenAIKey() {+ const apiKey = process.env.OPENAI_API_KEY;+ console.log("🔑 OpenAI API Key requested:", apiKey ? "Present" : "Missing");+ return apiKey || "";+ }++ saveOpenAIKey(key) {+ try {+ // Update the environment variable in memory for immediate use+ process.env.OPENAI_API_KEY = key;+ console.log("🔑 OpenAI API Key updated in memory");+ return { success: true };+ } catch (error) {+ console.error("❌ Error saving OpenAI API key:", error);+ throw error;+ }+ }++ createProductionEnvFile(apiKey) {+ try {+ const envPath = path.join(app.getPath("userData"), ".env");++ const envContent = `# OpenWispr Environment Variables+# This file was created automatically for production use+OPENAI_API_KEY=${apiKey}+`;++ fs.writeFileSync(envPath, envContent, "utf8");+ console.log("✅ Production .env file created at:", envPath);++ // Reload environment variables+ require("dotenv").config({ path: envPath });+ console.log(+ "🔄 Environment variables reloaded from production .env file"+ );++ return { success: true, path: envPath };+ } catch (error) {+ console.error("❌ Error creating production .env file:", error);+ throw error;+ }+ }+}++module.exports = EnvironmentManager;diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.jsnew file mode 100644index 000000000..5595080df--- /dev/null+++ b/src/helpers/ipcHandlers.js@@ -0,0 +1,226 @@+const { ipcMain, app } = require("electron");+const AppUtils = require("../utils");++class IPCHandlers {+ constructor(managers) {+ this.environmentManager = managers.environmentManager;+ this.databaseManager = managers.databaseManager;+ this.clipboardManager = managers.clipboardManager;+ this.whisperManager = managers.whisperManager;+ this.windowManager = managers.windowManager;+ this.setupHandlers();+ }++ setupHandlers() {+ // Window control handlers+ ipcMain.handle("window-minimize", () => {+ if (this.windowManager.controlPanelWindow) {+ this.windowManager.controlPanelWindow.minimize();+ }+ });++ ipcMain.handle("window-maximize", () => {+ if (this.windowManager.controlPanelWindow) {+ if (this.windowManager.controlPanelWindow.isMaximized()) {+ this.windowManager.controlPanelWindow.unmaximize();+ } else {+ this.windowManager.controlPanelWindow.maximize();+ }+ }+ });++ ipcMain.handle("window-close", () => {+ if (this.windowManager.controlPanelWindow) {+ this.windowManager.controlPanelWindow.close();+ }+ });++ ipcMain.handle("window-is-maximized", () => {+ if (this.windowManager.controlPanelWindow) {+ return this.windowManager.controlPanelWindow.isMaximized();+ }+ return false;+ });++ ipcMain.handle("hide-window", () => {+ if (process.platform === "darwin") {+ this.windowManager.mainWindow.minimize();+ if (app.dock) app.dock.show();+ } else {+ this.windowManager.mainWindow.hide();+ }+ });++ // Environment handlers+ ipcMain.handle("get-openai-key", async (event) => {+ return this.environmentManager.getOpenAIKey();+ });++ ipcMain.handle("save-openai-key", async (event, key) => {+ return this.environmentManager.saveOpenAIKey(key);+ });++ ipcMain.handle("create-production-env-file", async (event, apiKey) => {+ return this.environmentManager.createProductionEnvFile(apiKey);+ });++ // Database handlers+ ipcMain.handle("db-save-transcription", async (event, text) => {+ return this.databaseManager.saveTranscription(text);+ });++ ipcMain.handle("db-get-transcriptions", async (event, limit = 50) => {+ return this.databaseManager.getTranscriptions(limit);+ });++ ipcMain.handle("db-clear-transcriptions", async (event) => {+ return this.databaseManager.clearTranscriptions();+ });++ ipcMain.handle("db-delete-transcription", async (event, id) => {+ return this.databaseManager.deleteTranscription(id);+ });++ // Clipboard handlers+ ipcMain.handle("paste-text", async (event, text) => {+ return this.clipboardManager.pasteText(text);+ });++ ipcMain.handle("read-clipboard", async (event) => {+ return this.clipboardManager.readClipboard();+ });++ // Whisper handlers+ ipcMain.handle(+ "transcribe-local-whisper",+ async (event, audioBlob, options = {}) => {+ try {+ const result = await this.whisperManager.transcribeLocalWhisper(+ audioBlob,+ options+ );++ // Check if no audio was detected and send appropriate event+ if (!result.success && result.message === "No audio detected") {+ event.sender.send("no-audio-detected");+ }++ return result;+ } catch (error) {+ console.error("❌ Local Whisper transcription error:", error);+ throw error;+ }+ }+ );++ ipcMain.handle("check-whisper-installation", async (event) => {+ return this.whisperManager.checkWhisperInstallation();+ });++ ipcMain.handle("install-whisper", async (event) => {+ try {+ // Set up progress forwarding for installation+ const originalConsoleLog = console.log;+ console.log = (...args) => {+ const message = args.join(" ");+ if (+ message.includes("Installing") ||+ message.includes("Downloading") ||+ message.includes("Collecting")+ ) {+ event.sender.send("whisper-install-progress", {+ type: "progress",+ message: message,+ });+ }+ originalConsoleLog(...args);+ };++ const result = await this.whisperManager.installWhisper();++ // Restore original console.log+ console.log = originalConsoleLog;++ return result;+ } catch (error) {+ console.error("❌ Whisper installation error:", error);+ throw error;+ }+ });++ ipcMain.handle("download-whisper-model", async (event, modelName) => {+ try {+ // Set up progress forwarding for model downloads+ const originalConsoleError = console.error;+ console.error = (...args) => {+ const message = args.join(" ");+ if (message.startsWith("PROGRESS:")) {+ try {+ const progressData = JSON.parse(message.substring(9));+ event.sender.send("whisper-download-progress", {+ type: "progress",+ model: modelName,+ ...progressData,+ });+ } catch (parseError) {+ // Ignore parsing errors for progress data+ }+ }+ originalConsoleError(...args);+ };++ const result = await this.whisperManager.downloadWhisperModel(+ modelName+ );++ // Restore original console.error+ console.error = originalConsoleError;++ // Send completion event+ event.sender.send("whisper-download-progress", {+ type: "complete",+ model: modelName,+ result: result,+ });++ return result;+ } catch (error) {+ console.error("❌ Model download error:", error);++ // Send error event+ event.sender.send("whisper-download-progress", {+ type: "error",+ model: modelName,+ error: error.message,+ });++ throw error;+ }+ });++ ipcMain.handle("check-model-status", async (event, modelName) => {+ return this.whisperManager.checkModelStatus(modelName);+ });++ ipcMain.handle("list-whisper-models", async (event) => {+ return this.whisperManager.listWhisperModels();+ });++ ipcMain.handle("delete-whisper-model", async (event, modelName) => {+ return this.whisperManager.deleteWhisperModel(modelName);+ });++ // Utility handlers+ ipcMain.handle("cleanup-app", async (event) => {+ try {+ AppUtils.cleanup(this.windowManager.mainWindow);+ return { success: true, message: "Cleanup completed successfully" };+ } catch (error) {+ console.error("❌ Cleanup error:", error);+ throw error;+ }+ });+ }+}++module.exports = IPCHandlers;diff --git a/src/helpers/tray.js b/src/helpers/tray.jsnew file mode 100644index 000000000..592a9b93c--- /dev/null+++ b/src/helpers/tray.js@@ -0,0 +1,173 @@+const { Tray, Menu, nativeImage, app } = require("electron");+const path = require("path");+const fs = require("fs");++class TrayManager {+ constructor() {+ this.tray = null;+ this.mainWindow = null;+ this.controlPanelWindow = null;+ }++ setWindows(mainWindow, controlPanelWindow) {+ this.mainWindow = mainWindow;+ this.controlPanelWindow = controlPanelWindow;+ }++ setCreateControlPanelCallback(callback) {+ this.createControlPanelCallback = callback;+ }++ async createTray() {+ if (process.platform !== "darwin") return;++ try {+ const trayIcon = await this.loadTrayIcon();+ if (!trayIcon || trayIcon.isEmpty()) {+ console.error("Failed to load tray icon");+ return;+ }++ trayIcon.setTemplateImage(true);+ this.tray = new Tray(trayIcon);++ this.tray.setIgnoreDoubleClickEvents(true);+ this.setupTrayMenu();+ this.setupTrayEventHandlers();++ console.log("✅ Tray icon created successfully");+ } catch (error) {+ console.error("Error creating tray icon:", error);+ }+ }++ async loadTrayIcon() {+ if (process.env.NODE_ENV === "development") {+ const iconPath = path.join(+ __dirname,+ "..",+ "..",+ "assets",+ );+ return nativeImage.createFromPath(iconPath);+ } else {+ const possiblePaths = [+ path.join(process.resourcesPath, "assets", "[email protected]"),+ path.join(+ process.resourcesPath,+ "app.asar.unpacked",+ "assets",+ ),+ path.join(__dirname, "..", "..", "assets", "[email protected]"),+ path.join(+ process.resourcesPath,+ "app",+ "assets",+ ),+ ];++ for (const testPath of possiblePaths) {+ try {+ if (fs.existsSync(testPath)) {+ return nativeImage.createFromPath(testPath);+ }+ } catch (e) {+ // Ignore and try next path+ }+ }++ console.error("Could not find tray icon in any expected location");+ console.log("Tried paths:", possiblePaths);++ return this.createFallbackIcon();+ }+ }++ createFallbackIcon() {+ try {+ // Create a simple 16x16 PNG icon programmatically+ const { createCanvas } = require("canvas");+ const canvas = createCanvas(16, 16);+ const ctx = canvas.getContext("2d");++ ctx.fillStyle = "#000000";+ ctx.beginPath();+ ctx.arc(8, 8, 6, 0, 2 * Math.PI);+ ctx.fill();++ const buffer = canvas.toBuffer("image/png");+ const fallbackIcon = nativeImage.createFromBuffer(buffer);+ console.log("✅ Created fallback tray icon");+ return fallbackIcon;+ } catch (fallbackError) {+ console.warn("Canvas not available, creating minimal fallback icon");+ // Create a minimal 16x16 black square PNG as fallback+ const pngData = Buffer.from([+ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,+ 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10,+ 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x91, 0x68, 0x36, 0x00, 0x00, 0x00,+ 0x0c, 0x49, 0x44, 0x41, 0x54, 0x28, 0x53, 0x63, 0x08, 0x05, 0x00, 0x00,+ 0x02, 0x00, 0x01, 0xe5, 0x27, 0xde, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x49,+ 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,+ ]);++ const fallbackIcon = nativeImage.createFromBuffer(pngData);+ console.log("✅ Created minimal fallback tray icon");+ return fallbackIcon;+ }+ }++ setupTrayMenu() {+ const contextMenu = Menu.buildFromTemplate([+ {+ label: "Show Dictation Panel",+ click: () => {+ if (!this.mainWindow.isVisible()) {+ this.mainWindow.show();+ }+ this.mainWindow.focus();+ },+ },+ {+ label: "Open Control Panel",+ click: () => {+ if (this.controlPanelWindow) {+ this.controlPanelWindow.focus();+ } else if (this.createControlPanelCallback) {+ this.createControlPanelCallback();+ }+ },+ },+ { type: "separator" },+ {+ label: "Quit OpenWispr",+ click: () => {+ console.log("Quitting app via tray menu");+ app.quit();+ },+ },+ ]);++ this.tray.setToolTip("OpenWispr - Voice Dictation");+ this.tray.setContextMenu(contextMenu);+ }++ setupTrayEventHandlers() {+ this.tray.on("click", () => {+ if (!this.mainWindow.isVisible()) {+ this.mainWindow.show();+ }+ this.mainWindow.focus();+ });++ this.tray.on("destroyed", () => {+ console.log("Tray icon destroyed");+ this.tray = null;+ });+ }+}++module.exports = TrayManager;diff --git a/src/helpers/whisper.js b/src/helpers/whisper.jsnew file mode 100644index 000000000..117fbbb20--- /dev/null+++ b/src/helpers/whisper.js@@ -0,0 +1,578 @@+const { spawn } = require("child_process");+const fs = require("fs");+const os = require("os");+const path = require("path");+const crypto = require("crypto");++class WhisperManager {+ constructor() {+ // Initialize whisper manager+ }++ async transcribeLocalWhisper(audioBlob, options = {}) {+ try {+ console.log("🎤 Starting local Whisper transcription...");++ const tempDir = os.tmpdir();+ const filename = `whisper_audio_${crypto.randomUUID()}.wav`;+ const tempAudioPath = path.join(tempDir, filename);++ console.log("💾 Writing audio to temp file:", tempAudioPath);++ // Handle different audio data formats+ 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}`);+ }++ fs.writeFileSync(tempAudioPath, buffer);++ const model = options.model || "base";+ const language = options.language || null;++ const pythonCmd = await this.findPythonExecutable();+ const whisperScriptPath = path.join(+ __dirname,+ "..",+ "..",+ "whisper_bridge.py"+ );++ const args = [whisperScriptPath, tempAudioPath, "--model", model];+ if (language) {+ args.push("--language", language);+ }+ args.push("--output-format", "json");++ console.log("🔧 Running Whisper command:", pythonCmd, args.join(" "));++ return new Promise((resolve, reject) => {+ const whisperProcess = spawn(pythonCmd, args);++ let stdout = "";+ let stderr = "";++ whisperProcess.stdout.on("data", (data) => {+ stdout += data.toString();+ });++ whisperProcess.stderr.on("data", (data) => {+ stderr += data.toString();+ console.log("Whisper log:", data.toString());+ });++ whisperProcess.on("close", (code) => {+ // Clean up temp file+ try {+ fs.unlinkSync(tempAudioPath);+ console.log("🗑️ Cleaned up temp audio file");+ } catch (cleanupError) {+ console.warn(+ "⚠️ Could not clean up temp file:",+ cleanupError.message+ );+ }++ if (code === 0) {+ try {+ const result = JSON.parse(stdout);++ // Check if the transcription is empty or just whitespace+ if (!result.text || result.text.trim().length === 0) {+ console.log(+ "🛑 No meaningful audio content detected (empty transcription)"+ );+ resolve({ success: false, message: "No audio detected" });+ return;+ }++ console.log(+ "✅ Whisper transcription successful:",+ result.text?.substring(0, 50) + "..."+ );+ resolve(result);+ } catch (parseError) {+ console.error("❌ Failed to parse Whisper output:", parseError);+ reject(+ new Error(+ `Failed to parse Whisper output: ${parseError.message}`+ )+ );+ }+ } else {+ if (stderr.includes("no audio") || stderr.includes("empty")) {+ console.log("🛑 No audio content detected");+ resolve({ success: false, message: "No audio detected" });+ } else {+ console.error("❌ Whisper process failed with code:", code);+ console.error("Stderr:", stderr);+ reject(+ new Error(+ `Whisper transcription failed (code ${code}): ${stderr}`+ )+ );+ }+ }+ });++ whisperProcess.on("error", (error) => {+ try {+ fs.unlinkSync(tempAudioPath);+ } catch (cleanupError) {+ console.warn(+ "⚠️ Could not clean up temp file:",+ cleanupError.message+ );+ }++ console.error("❌ Whisper process error:", error);+ reject(new Error(`Whisper process error: ${error.message}`));+ });++ setTimeout(() => {+ whisperProcess.kill("SIGTERM");+ try {+ fs.unlinkSync(tempAudioPath);+ } catch (cleanupError) {+ console.warn(+ "⚠️ Could not clean up temp file:",+ cleanupError.message+ );+ }+ reject(new Error("Whisper transcription timed out (30 seconds)"));+ }, 30000);+ });+ } catch (error) {+ console.error("❌ Local Whisper transcription error:", error);+ throw error;+ }+ }++ async findPythonExecutable() {+ const possiblePaths = [+ "python3",+ "python",+ "/usr/bin/python3",+ "/usr/local/bin/python3",+ "/opt/homebrew/bin/python3",+ "/usr/bin/python",+ "/usr/local/bin/python",+ ];++ for (const pythonPath of possiblePaths) {+ try {+ const result = await new Promise((resolve, reject) => {+ const testProcess = spawn(pythonPath, ["--version"]);+ testProcess.on("close", (code) => {+ resolve(code === 0);+ });+ testProcess.on("error", () => {+ resolve(false);+ });+ });++ if (result) {+ console.log("🐍 Found Python at:", pythonPath);+ return pythonPath;+ }+ } catch (error) {+ continue;+ }+ }++ throw new Error(+ "Python executable not found. Please ensure Python 3 is installed."+ );+ }++ async checkWhisperInstallation() {+ try {+ const pythonCmd = await this.findPythonExecutable();++ return new Promise((resolve) => {+ const checkProcess = spawn(pythonCmd, [+ "-c",+ 'import whisper; print("OK")',+ ]);++ let output = "";+ checkProcess.stdout.on("data", (data) => {+ output += data.toString();+ });++ checkProcess.on("close", (code) => {+ if (code === 0 && output.includes("OK")) {+ console.log("✅ Whisper is installed and working");+ resolve({ installed: true, working: true });+ } else {+ console.log("❌ Whisper is not properly installed");+ resolve({ installed: false, working: false });+ }+ });++ checkProcess.on("error", (error) => {+ console.log("❌ Error checking Whisper:", error.message);+ resolve({ installed: false, working: false, error: error.message });+ });+ });+ } catch (error) {+ console.log("❌ Error finding Python:", error.message);+ return { installed: false, working: false, error: error.message };+ }+ }++ async installWhisper() {+ try {+ console.log("🔧 Starting automatic Whisper installation...");++ const pythonCmd = await this.findPythonExecutable();+ console.log("🐍 Using Python:", pythonCmd);++ const args = ["-m", "pip", "install", "-U", "openai-whisper"];++ console.log(+ "📦 Running installation command:",+ pythonCmd,+ args.join(" ")+ );++ return new Promise((resolve, reject) => {+ const installProcess = spawn(pythonCmd, args);++ let stdout = "";+ let stderr = "";++ installProcess.stdout.on("data", (data) => {+ const output = data.toString();+ stdout += output;+ console.log("Install output:", output);+ });++ installProcess.stderr.on("data", (data) => {+ const output = data.toString();+ stderr += output;+ console.log("Install stderr:", output);+ });++ installProcess.on("close", (code) => {+ if (code === 0) {+ console.log("✅ Whisper installation completed successfully");+ resolve({+ success: true,+ message: "Whisper installed successfully!",+ output: stdout,+ });+ } else {+ console.error("❌ Whisper installation failed with code:", code);+ console.error("Installation stderr:", stderr);+ reject(+ new Error(`Whisper installation failed (code ${code}): ${stderr}`)+ );+ }+ });++ installProcess.on("error", (error) => {+ console.error("❌ Whisper installation process error:", error);+ reject(+ new Error(`Whisper installation process error: ${error.message}`)+ );+ });++ setTimeout(() => {+ installProcess.kill("SIGTERM");+ reject(new Error("Whisper installation timed out (10 minutes)"));+ }, 600000);+ });+ } catch (error) {+ console.error("❌ Whisper installation error:", error);+ throw error;+ }+ }++ async downloadWhisperModel(modelName) {+ try {+ console.log(`📥 Starting download of Whisper model: ${modelName}`);++ const pythonCmd = await this.findPythonExecutable();+ const whisperScriptPath = path.join(+ __dirname,+ "..",+ "..",+ "whisper_bridge.py"+ );++ const args = [+ whisperScriptPath,+ "--mode",+ "download",+ "--model",+ modelName,+ ];++ console.log(+ "🔧 Running model download command:",+ pythonCmd,+ args.join(" ")+ );++ return new Promise((resolve, reject) => {+ const downloadProcess = spawn(pythonCmd, args);++ let stdout = "";+ let stderr = "";++ downloadProcess.stdout.on("data", (data) => {+ stdout += data.toString();+ });++ downloadProcess.stderr.on("data", (data) => {+ const output = data.toString();+ stderr += output;+ console.log("Model download log:", output);+ });++ downloadProcess.on("close", (code) => {+ if (code === 0) {+ try {+ const result = JSON.parse(stdout);+ console.log(`✅ Model ${modelName} download completed:`, result);+ resolve(result);+ } catch (parseError) {+ console.error("❌ Failed to parse download result:", parseError);+ reject(+ new Error(+ `Failed to parse download result: ${parseError.message}`+ )+ );+ }+ } else {+ console.error("❌ Model download failed with code:", code);+ console.error("Stderr:", stderr);+ reject(+ new Error(`Model download failed (code ${code}): ${stderr}`)+ );+ }+ });++ downloadProcess.on("error", (error) => {+ console.error("❌ Model download process error:", error);+ reject(new Error(`Model download process error: ${error.message}`));+ });++ const timeout = setTimeout(() => {+ console.warn(+ "⚠️ Model download timed out, attempting graceful shutdown..."+ );+ downloadProcess.kill("SIGTERM");++ setTimeout(() => {+ if (!downloadProcess.killed) {+ console.warn("⚠️ Force killing download process...");+ downloadProcess.kill("SIGKILL");+ }+ }, 5000);++ reject(new Error("Model download timed out (20 minutes)"));+ }, 1200000);++ downloadProcess.on("close", () => {+ clearTimeout(timeout);+ });+ });+ } catch (error) {+ console.error("❌ Model download error:", error);+ throw error;+ }+ }++ async checkModelStatus(modelName) {+ try {+ console.log(`🔍 Checking status of Whisper model: ${modelName}`);++ const pythonCmd = await this.findPythonExecutable();+ const whisperScriptPath = path.join(+ __dirname,+ "..",+ "..",+ "whisper_bridge.py"+ );++ const args = [whisperScriptPath, "--mode", "check", "--model", modelName];++ return new Promise((resolve, reject) => {+ const checkProcess = spawn(pythonCmd, args);++ let stdout = "";+ let stderr = "";++ checkProcess.stdout.on("data", (data) => {+ stdout += data.toString();+ });++ checkProcess.stderr.on("data", (data) => {+ stderr += data.toString();+ });++ checkProcess.on("close", (code) => {+ if (code === 0) {+ try {+ const result = JSON.parse(stdout);+ console.log(`📊 Model ${modelName} status:`, result);+ resolve(result);+ } catch (parseError) {+ console.error("❌ Failed to parse model status:", parseError);+ reject(+ new Error(`Failed to parse model status: ${parseError.message}`)+ );+ }+ } else {+ console.error("❌ Model status check failed with code:", code);+ reject(+ new Error(`Model status check failed (code ${code}): ${stderr}`)+ );+ }+ });++ checkProcess.on("error", (error) => {+ console.error("❌ Model status check error:", error);+ reject(new Error(`Model status check error: ${error.message}`));+ });+ });+ } catch (error) {+ console.error("❌ Model status check error:", error);+ throw error;+ }+ }++ async listWhisperModels() {+ try {+ console.log("📋 Listing all Whisper models...");++ const pythonCmd = await this.findPythonExecutable();+ const whisperScriptPath = path.join(+ __dirname,+ "..",+ "..",+ "whisper_bridge.py"+ );++ const args = [whisperScriptPath, "--mode", "list"];++ return new Promise((resolve, reject) => {+ const listProcess = spawn(pythonCmd, args);++ let stdout = "";+ let stderr = "";++ listProcess.stdout.on("data", (data) => {+ stdout += data.toString();+ });++ listProcess.stderr.on("data", (data) => {+ stderr += data.toString();+ });++ listProcess.on("close", (code) => {+ if (code === 0) {+ try {+ const result = JSON.parse(stdout);+ console.log("📋 Model list retrieved:", result);+ resolve(result);+ } catch (parseError) {+ console.error("❌ Failed to parse model list:", parseError);+ reject(+ new Error(`Failed to parse model list: ${parseError.message}`)+ );+ }+ } else {+ console.error("❌ Model list failed with code:", code);+ reject(new Error(`Model list failed (code ${code}): ${stderr}`));+ }+ });++ listProcess.on("error", (error) => {+ console.error("❌ Model list error:", error);+ reject(new Error(`Model list error: ${error.message}`));+ });+ });+ } catch (error) {+ console.error("❌ Model list error:", error);+ throw error;+ }+ }++ async deleteWhisperModel(modelName) {+ try {+ console.log(`🗑️ Deleting Whisper model: ${modelName}`);++ const pythonCmd = await this.findPythonExecutable();+ const whisperScriptPath = path.join(+ __dirname,+ "..",+ "..",+ "whisper_bridge.py"+ );++ const args = [+ whisperScriptPath,+ "--mode",+ "delete",+ "--model",+ modelName,+ ];++ return new Promise((resolve, reject) => {+ const deleteProcess = spawn(pythonCmd, args);++ let stdout = "";+ let stderr = "";++ deleteProcess.stdout.on("data", (data) => {+ stdout += data.toString();+ });++ deleteProcess.stderr.on("data", (data) => {+ stderr += data.toString();+ });++ deleteProcess.on("close", (code) => {+ if (code === 0) {+ try {+ const result = JSON.parse(stdout);+ console.log(`🗑️ Model ${modelName} delete result:`, result);+ resolve(result);+ } catch (parseError) {+ console.error("❌ Failed to parse delete result:", parseError);+ reject(+ new Error(+ `Failed to parse delete result: ${parseError.message}`+ )+ );+ }+ } else {+ console.error("❌ Model delete failed with code:", code);+ reject(new Error(`Model delete failed (code ${code}): ${stderr}`));+ }+ });++ deleteProcess.on("error", (error) => {+ console.error("❌ Model delete error:", error);+ reject(new Error(`Model delete error: ${error.message}`));+ });+ });+ } catch (error) {+ console.error("❌ Model delete error:", error);+ throw error;+ }+ }+}++module.exports = WhisperManager;diff --git a/src/helpers/windowManager.js b/src/helpers/windowManager.jsnew file mode 100644index 000000000..995583a82--- /dev/null+++ b/src/helpers/windowManager.js@@ -0,0 +1,246 @@+const {+ screen,+ BrowserWindow,+ globalShortcut,+ Menu,+ Tray,+} = require("electron");+const path = require("path");++class WindowManager {+ constructor() {+ this.mainWindow = null;+ this.controlPanelWindow = null;+ this.tray = null;+ }++ async createMainWindow() {+ const display = screen.getPrimaryDisplay();+ const width = 100;+ const height = 100;+ const x = display.bounds.x + display.workArea.width - width;+ const y = display.bounds.y + display.workArea.height;++ this.mainWindow = new BrowserWindow({+ width,+ height,+ x,+ y,+ webPreferences: {+ preload: path.join(__dirname, "..", "..", "preload.js"),+ nodeIntegration: false,+ contextIsolation: true,+ enableRemoteModule: false,+ sandbox: true,+ },+ frame: false,+ alwaysOnTop: true,+ resizable: false,+ transparent: true,+ show: true,+ });++ await this.loadMainWindow();+ this.setupShortcuts();+ this.setupMenu();++ this.mainWindow.webContents.on(+ "did-fail-load",+ async (event, errorCode, errorDescription, validatedURL) => {+ console.error(+ "Failed to load main window:",+ errorCode,+ errorDescription,+ validatedURL+ );+ if (+ process.env.NODE_ENV === "development" &&+ validatedURL.includes("localhost:5174")+ ) {+ console.log("Retrying connection to dev server in 2 seconds...");+ setTimeout(async () => {+ const isReady = await this.waitForDevServer();+ if (isReady) {+ console.log("Dev server ready, reloading...");+ this.mainWindow.reload();+ }+ }, 2000);+ }+ }+ );++ this.mainWindow.webContents.on("did-finish-load", () => {+ setTimeout(() => {+ if (!this.mainWindow.isVisible()) {+ console.log("Window not visible, forcing show...");+ this.mainWindow.show();+ this.mainWindow.focus();+ }+ }, 1000);+ });++ // Ensure window is always on top, even above fullscreen apps+ this.mainWindow.setAlwaysOnTop(true, "screen-saver");+ this.mainWindow.setVisibleOnAllWorkspaces(true, {+ visibleOnFullScreen: true,+ });+ this.mainWindow.on("show", () => {+ this.mainWindow.setAlwaysOnTop(true, "screen-saver");+ this.mainWindow.setVisibleOnAllWorkspaces(true, {+ visibleOnFullScreen: true,+ });+ });+ }++ async loadMainWindow() {+ const appUrl = this.getAppUrl(false);+ if (process.env.NODE_ENV === "development") {+ const isReady = await this.waitForDevServer();+ if (!isReady) {+ console.error("Dev server not ready, loading anyway...");+ }+ }+ this.mainWindow.loadURL(appUrl);+ }++ setupShortcuts() {+ globalShortcut.unregisterAll();+ globalShortcut.register("`", () => {+ if (!this.mainWindow.isVisible()) {+ this.mainWindow.show();+ }+ this.mainWindow.webContents.send("toggle-dictation");+ });+ }++ setupMenu() {+ if (process.platform === "darwin") {+ const template = [+ {+ label: "OpenWispr",+ submenu: [+ { role: "about" },+ { type: "separator" },+ { role: "quit", label: "Quit OpenWispr" },+ ],+ },+ ];+ const menu = Menu.buildFromTemplate(template);+ Menu.setApplicationMenu(menu);+ }+ }++ async createControlPanelWindow() {+ if (this.controlPanelWindow) {+ this.controlPanelWindow.focus();+ return;+ }+ this.controlPanelWindow = new BrowserWindow({+ width: 800,+ height: 700,+ webPreferences: {+ preload: path.join(__dirname, "..", "..", "preload.js"),+ nodeIntegration: false,+ contextIsolation: true,+ enableRemoteModule: false,+ sandbox: false,+ webSecurity: false,+ },+ title: "OpenWispr Control Panel",+ resizable: true,+ show: true,+ titleBarStyle: "hiddenInset",+ frame: false,+ transparent: false,+ backgroundColor: "#ffffff",+ minimizable: true,+ maximizable: true,+ closable: true,+ fullscreenable: true,+ });+ await this.loadControlPanel();+ this.controlPanelWindow.on("closed", () => {+ this.controlPanelWindow = null;+ });+ }++ async loadControlPanel() {+ const appUrl = this.getAppUrl(true);+ if (process.env.NODE_ENV === "development") {+ const isReady = await this.waitForDevServer();+ if (!isReady) {+ console.error(+ "Dev server not ready for control panel, loading anyway..."+ );+ }+ }+ this.controlPanelWindow.loadURL(appUrl);+ }++ async waitForDevServer(+ url = "http://localhost:5174/",+ maxAttempts = 30,+ delay = 1000+ ) {+ for (let i = 0; i < maxAttempts; i++) {+ try {+ const http = require("http");+ const urlObj = new URL(url);++ const result = await new Promise((resolve) => {+ const req = http.get(+ {+ hostname: urlObj.hostname,+ port: urlObj.port || 80,+ path: urlObj.pathname,+ timeout: 2000,+ },+ (res) => {+ resolve(res.statusCode >= 200 && res.statusCode < 400);+ }+ );++ req.on("error", () => resolve(false));+ req.on("timeout", () => {+ req.destroy();+ resolve(false);+ });+ });++ if (result) {+ console.log(`Dev server ready after ${i + 1} attempts`);+ return true;+ }+ } catch (error) {+ console.log(+ `Waiting for dev server... attempt ${i + 1}/${maxAttempts}`+ );+ }+ await new Promise((resolve) => setTimeout(resolve, delay));+ }+ console.error("Dev server failed to start within timeout");+ return false;+ }++ getAppUrl(isControlPanel = false) {+ if (process.env.NODE_ENV === "development") {+ return isControlPanel+ ? "http://localhost:5174/?panel=true"+ : "http://localhost:5174/";+ } else {+ const htmlPath = path.join(+ __dirname,+ "..",+ "..",+ "src",+ "dist",+ "index.html"+ );+ return isControlPanel+ ? `file://${htmlPath}?panel=true`+ : `file://${htmlPath}`;+ }+ }+}++module.exports = WindowManager;diff --git a/src/utils.js b/src/utils.jsnew file mode 100644index 000000000..9dfd1252a--- /dev/null+++ b/src/utils.js@@ -0,0 +1,69 @@+const fs = require("fs");+const path = require("path");+const os = require("os");+const { app } = require("electron");++class AppUtils {+ static cleanup(mainWindow) {+ console.log("Starting cleanup process...");++ // Database file deletion+ try {+ const dbPath = path.join(+ app.getPath("userData"),+ process.env.NODE_ENV === "development"+ ? "transcriptions-dev.db"+ : "transcriptions.db"+ );+ if (fs.existsSync(dbPath)) {+ fs.unlinkSync(dbPath);+ console.log("✅ Database file deleted:", dbPath);+ }+ } catch (error) {+ console.error("❌ Error deleting database file:", error);+ }++ // Local storage clearing+ if (mainWindow && mainWindow.webContents) {+ mainWindow.webContents+ .executeJavaScript("localStorage.clear()")+ .then(() => {+ console.log("✅ Local storage cleared");+ })+ .catch((error) => {+ console.error("❌ Error clearing local storage:", error);+ });+ }++ // Local Whisper model deletion+ try {+ const modelCacheDir = path.join(os.homedir(), ".cache", "whisper");+ if (fs.existsSync(modelCacheDir)) {+ fs.rmSync(modelCacheDir, { recursive: true, force: true });+ console.log("✅ Local Whisper models deleted:", modelCacheDir);+ }+ } catch (error) {+ console.error("❌ Error deleting Whisper models:", error);+ }++ // Permissions instruction+ console.log(+ "ℹ️ Please manually remove accessibility and microphone permissions via System Preferences if needed."+ );++ // Env file deletion+ try {+ const envPath = path.join(app.getPath("userData"), ".env");+ if (fs.existsSync(envPath)) {+ fs.unlinkSync(envPath);+ console.log("✅ .env file deleted:", envPath);+ }+ } catch (error) {+ console.error("❌ Error deleting .env file:", error);+ }++ console.log("Cleanup process completed.");+ }+}++module.exports = AppUtils;