|
| 1 | +// Flint — Electron Desktop Wrapper |
| 2 | +// .cjs = guaranteed CommonJS regardless of any "type":"module" |
| 3 | + |
| 4 | +const { app, BrowserWindow, Menu, shell } = require('electron'); |
| 5 | +const path = require('path'); |
| 6 | +const fs = require('fs'); |
| 7 | +const { spawn } = require('child_process'); |
| 8 | + |
| 9 | +let mainWindow = null; |
| 10 | +let agentProcess = null; |
| 11 | + |
| 12 | +const APP_DIR = __dirname; |
| 13 | +const DIST_FILE = path.join(APP_DIR, 'dist', 'index.html'); |
| 14 | +const ICON_FILE = path.join(APP_DIR, 'icon.png'); |
| 15 | + |
| 16 | +// ── Start Python AI Agent ────────────────────────────────── |
| 17 | +function startAgent() { |
| 18 | + const agentDir = path.join(APP_DIR, 'agent'); |
| 19 | + const agentScript = path.join(agentDir, 'agent.py'); |
| 20 | + |
| 21 | + if (!fs.existsSync(agentScript)) { |
| 22 | + console.log('[Flint] No agent found — AI will use browser fallback'); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + console.log('[Flint] Starting Python AI agent...'); |
| 27 | + |
| 28 | + // Try python3 first, then python |
| 29 | + const pythonCmd = process.platform === 'win32' ? 'python' : 'python3'; |
| 30 | + |
| 31 | + agentProcess = spawn(pythonCmd, [agentScript], { |
| 32 | + cwd: agentDir, |
| 33 | + env: { ...process.env }, |
| 34 | + stdio: ['pipe', 'pipe', 'pipe'], |
| 35 | + detached: false, |
| 36 | + }); |
| 37 | + |
| 38 | + agentProcess.stdout.on('data', (data) => { |
| 39 | + const msg = data.toString().trim(); |
| 40 | + if (msg) console.log('[Flint Agent]', msg); |
| 41 | + }); |
| 42 | + |
| 43 | + agentProcess.stderr.on('data', (data) => { |
| 44 | + const msg = data.toString().trim(); |
| 45 | + if (msg && !msg.includes('DeprecationWarning') && !msg.includes('WARNING')) { |
| 46 | + console.log('[Flint Agent]', msg); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + agentProcess.on('error', (err) => { |
| 51 | + console.log('[Flint] Agent failed to start:', err.message); |
| 52 | + console.log('[Flint] Install Python + Flask: pip3 install flask flask-cors requests'); |
| 53 | + }); |
| 54 | + |
| 55 | + agentProcess.on('exit', (code) => { |
| 56 | + console.log('[Flint] Agent stopped (code', code, ')'); |
| 57 | + agentProcess = null; |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +function stopAgent() { |
| 62 | + if (agentProcess) { |
| 63 | + console.log('[Flint] Stopping agent...'); |
| 64 | + agentProcess.kill('SIGTERM'); |
| 65 | + agentProcess = null; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// ── Create Window ────────────────────────────────────────── |
| 70 | + |
| 71 | +function createWindow() { |
| 72 | + if (!fs.existsSync(DIST_FILE)) { |
| 73 | + console.error('[Flint] ERROR: dist/index.html not found at ' + DIST_FILE); |
| 74 | + console.error('[Flint] Run: bash install.sh'); |
| 75 | + app.quit(); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + mainWindow = new BrowserWindow({ |
| 80 | + width: 1400, |
| 81 | + height: 900, |
| 82 | + minWidth: 800, |
| 83 | + minHeight: 600, |
| 84 | + title: 'Flint', |
| 85 | + backgroundColor: '#0a0a0a', |
| 86 | + icon: fs.existsSync(ICON_FILE) ? ICON_FILE : undefined, |
| 87 | + autoHideMenuBar: true, |
| 88 | + show: false, |
| 89 | + webPreferences: { |
| 90 | + nodeIntegration: false, |
| 91 | + contextIsolation: true, |
| 92 | + sandbox: true, |
| 93 | + }, |
| 94 | + }); |
| 95 | + |
| 96 | + Menu.setApplicationMenu(null); |
| 97 | + |
| 98 | + mainWindow.loadFile(DIST_FILE).then(() => { |
| 99 | + console.log('[Flint] Loaded successfully'); |
| 100 | + }).catch(err => { |
| 101 | + console.error('[Flint] Failed to load:', err.message); |
| 102 | + }); |
| 103 | + |
| 104 | + mainWindow.once('ready-to-show', () => { |
| 105 | + if (mainWindow) { |
| 106 | + mainWindow.show(); |
| 107 | + console.log('[Flint] Window displayed'); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + mainWindow.webContents.setWindowOpenHandler(({ url }) => { |
| 112 | + if (url.startsWith('http://localhost') || url.startsWith('http://127.0.0.1')) { |
| 113 | + return { action: 'allow' }; |
| 114 | + } |
| 115 | + shell.openExternal(url); |
| 116 | + return { action: 'deny' }; |
| 117 | + }); |
| 118 | + |
| 119 | + mainWindow.on('closed', () => { |
| 120 | + mainWindow = null; |
| 121 | + }); |
| 122 | +} |
| 123 | + |
| 124 | +// ── App Lifecycle ────────────────────────────────────────── |
| 125 | + |
| 126 | +const gotLock = app.requestSingleInstanceLock(); |
| 127 | +if (!gotLock) { |
| 128 | + app.quit(); |
| 129 | +} else { |
| 130 | + app.on('second-instance', () => { |
| 131 | + if (mainWindow) { |
| 132 | + if (mainWindow.isMinimized()) mainWindow.restore(); |
| 133 | + mainWindow.focus(); |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + app.whenReady().then(() => { |
| 138 | + // Start Python agent before creating window |
| 139 | + startAgent(); |
| 140 | + createWindow(); |
| 141 | + console.log('[Flint] App ready — desktop mode'); |
| 142 | + }); |
| 143 | + |
| 144 | + app.on('window-all-closed', () => { |
| 145 | + stopAgent(); |
| 146 | + app.quit(); |
| 147 | + }); |
| 148 | + |
| 149 | + app.on('before-quit', () => { |
| 150 | + stopAgent(); |
| 151 | + }); |
| 152 | + |
| 153 | + app.on('activate', () => { |
| 154 | + if (mainWindow === null) createWindow(); |
| 155 | + }); |
| 156 | +} |
0 commit comments