| 2516 | } catch (err) { |
| 2517 | console.error('Server mode: failed to bind:', err.message); |
| 2518 | process.exit(1); |
| 2519 | } |
| 2520 | setImmediate(() => { |
| 2521 | maybeRenewLetsEncryptCertificate().catch((renewErr) => { |
| 2522 | console.warn('[TLS] Startup renewal skipped:', renewErr.message); |
| 2523 | }); |
| 2524 | }); |
| 2525 | // Create a hidden BrowserWindow to handle IPC (preload script needs a window) |
| 2526 | await createHiddenWindow(); |
| 2527 | // Schedule background hash generation for any existing models with missing hashes |
| 2528 | scheduleBackgroundHashGeneration('startup'); |
| 2529 | scheduleBackgroundThumbnailCompression('startup'); |
| 2530 | setTimeout(() => { |
| 2531 | try { |
| 2532 | verifyDatabaseIntegrity(); |
| 2533 | } catch (e) { |
| 2534 | console.error('Deferred database integrity check failed:', e); |
| 2535 | } |
| 2536 | }, 3000); |
| 2537 | // Don't quit when all windows are closed in server mode |
| 2538 | app.on('window-all-closed', () => { |
| 2539 | // Keep the app running in server mode |
| 2540 | }); |
| 2541 | } else { |
| 2542 | // Normal mode: start localhost-only HTTP server when MCP is enabled |
| 2543 | const extPort = getConfiguredHttpPort(); |
| 2544 | if (localHttpServerShouldRun()) { |
| 2545 | const mcpOn = getSettingValueOr('enableMcpServer', '0') === '1'; |
| 2546 | console.log('[Local HTTP] Starting at startup on port', extPort, '(mcp:', mcpOn, ')'); |
| 2547 | startHttpServer(extPort, true).then(() => { |
| 2548 | console.log('[Local HTTP] Server started successfully at startup'); |
| 2549 | setImmediate(() => { |
| 2550 | maybeRenewLetsEncryptCertificate().catch((renewErr) => { |
| 2551 | console.warn('[TLS] Startup renewal skipped:', renewErr.message); |
| 2552 | }); |
| 2553 | }); |
| 2554 | }).catch((err) => { |
| 2555 | console.error('[Local HTTP] Failed to start server at startup:', err.message); |
| 2556 | console.error('[Local HTTP] Run from Terminal to see this, or check entitlements (com.apple.security.network.server) and rebuild.'); |
| 2557 | if (dialog && dialog.showErrorBox) { |
| 2558 | dialog.showErrorBox('Local HTTP Server', `Could not start server on port ${extPort}: ${err.message}\n\nOn macOS, the app needs the "Allow incoming network connections" entitlement. Rebuild the app after adding com.apple.security.network.server to build/entitlements.mac.plist.`); |
| 2559 | } |
| 2560 | }); |
| 2561 | } |
| 2562 | // Normal mode: create window (UI served over localhost HTTP for Puter.js compatibility) |
| 2563 | await createWindow(); |
| 2564 | |
| 2565 | app.on('activate', () => { |
| 2566 | if (BrowserWindow.getAllWindows().length === 0) { |
| 2567 | createWindow().catch((err) => { |