252 lines
7.9 KiB
TypeScript
252 lines
7.9 KiB
TypeScript
/**
|
|
* Electron Main Process Entry
|
|
* Manages window creation, system tray, and IPC handlers
|
|
*/
|
|
import { app, BrowserWindow, nativeImage, session, shell } from 'electron';
|
|
import { join } from 'path';
|
|
import { GatewayManager } from '../gateway/manager';
|
|
import { registerIpcHandlers } from './ipc-handlers';
|
|
import { createTray } from './tray';
|
|
import { createMenu } from './menu';
|
|
|
|
import { appUpdater, registerUpdateHandlers } from './updater';
|
|
import { logger } from '../utils/logger';
|
|
import { warmupNetworkOptimization } from '../utils/uv-env';
|
|
|
|
import { ClawHubService } from '../gateway/clawhub';
|
|
import { ensureClawXContext, repairClawXOnlyBootstrapFiles } from '../utils/openclaw-workspace';
|
|
|
|
// Disable GPU acceleration for better compatibility
|
|
app.disableHardwareAcceleration();
|
|
|
|
// Global references
|
|
let mainWindow: BrowserWindow | null = null;
|
|
let isQuitting = false;
|
|
const gatewayManager = new GatewayManager();
|
|
const clawHubService = new ClawHubService();
|
|
|
|
/**
|
|
* Resolve the icons directory path (works in both dev and packaged mode)
|
|
*/
|
|
function getIconsDir(): string {
|
|
if (app.isPackaged) {
|
|
// Packaged: icons are in extraResources → process.resourcesPath/resources/icons
|
|
return join(process.resourcesPath, 'resources', 'icons');
|
|
}
|
|
// Development: relative to dist-electron/main/
|
|
return join(__dirname, '../../resources/icons');
|
|
}
|
|
|
|
/**
|
|
* Get the app icon for the current platform
|
|
*/
|
|
function getAppIcon(): Electron.NativeImage | undefined {
|
|
if (process.platform === 'darwin') return undefined; // macOS uses the app bundle icon
|
|
|
|
const iconsDir = getIconsDir();
|
|
const iconPath =
|
|
process.platform === 'win32'
|
|
? join(iconsDir, 'icon.ico')
|
|
: join(iconsDir, 'icon.png');
|
|
const icon = nativeImage.createFromPath(iconPath);
|
|
return icon.isEmpty() ? undefined : icon;
|
|
}
|
|
|
|
/**
|
|
* Create the main application window
|
|
*/
|
|
function createWindow(): BrowserWindow {
|
|
const isMac = process.platform === 'darwin';
|
|
|
|
const win = new BrowserWindow({
|
|
width: 1280,
|
|
height: 800,
|
|
minWidth: 960,
|
|
minHeight: 600,
|
|
icon: getAppIcon(),
|
|
webPreferences: {
|
|
preload: join(__dirname, '../preload/index.js'),
|
|
nodeIntegration: false,
|
|
contextIsolation: true,
|
|
sandbox: false,
|
|
webviewTag: true, // Enable <webview> for embedding OpenClaw Control UI
|
|
},
|
|
titleBarStyle: isMac ? 'hiddenInset' : 'hidden',
|
|
trafficLightPosition: isMac ? { x: 16, y: 16 } : undefined,
|
|
frame: isMac,
|
|
show: false,
|
|
});
|
|
|
|
// Show window when ready to prevent visual flash
|
|
win.once('ready-to-show', () => {
|
|
win.show();
|
|
});
|
|
|
|
// Handle external links
|
|
win.webContents.setWindowOpenHandler(({ url }) => {
|
|
shell.openExternal(url);
|
|
return { action: 'deny' };
|
|
});
|
|
|
|
// Load the app
|
|
if (process.env.VITE_DEV_SERVER_URL) {
|
|
win.loadURL(process.env.VITE_DEV_SERVER_URL);
|
|
win.webContents.openDevTools();
|
|
} else {
|
|
win.loadFile(join(__dirname, '../../dist/index.html'));
|
|
}
|
|
|
|
return win;
|
|
}
|
|
|
|
/**
|
|
* Initialize the application
|
|
*/
|
|
async function initialize(): Promise<void> {
|
|
// Initialize logger first
|
|
logger.init();
|
|
logger.info('=== ClawX Application Starting ===');
|
|
logger.debug(
|
|
`Runtime: platform=${process.platform}/${process.arch}, electron=${process.versions.electron}, node=${process.versions.node}, packaged=${app.isPackaged}`
|
|
);
|
|
|
|
// Warm up network optimization (non-blocking)
|
|
void warmupNetworkOptimization();
|
|
|
|
// Set application menu
|
|
createMenu();
|
|
|
|
// Create the main window
|
|
mainWindow = createWindow();
|
|
|
|
// Create system tray
|
|
createTray(mainWindow);
|
|
|
|
// Inject OpenRouter site headers (HTTP-Referer & X-Title) for rankings on openrouter.ai
|
|
session.defaultSession.webRequest.onBeforeSendHeaders(
|
|
{ urls: ['https://openrouter.ai/*'] },
|
|
(details, callback) => {
|
|
details.requestHeaders['HTTP-Referer'] = 'https://claw-x.com';
|
|
details.requestHeaders['X-Title'] = 'ClawX';
|
|
callback({ requestHeaders: details.requestHeaders });
|
|
},
|
|
);
|
|
|
|
// Override security headers ONLY for the OpenClaw Gateway Control UI
|
|
session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
|
|
const isGatewayUrl = details.url.includes('127.0.0.1:18789') || details.url.includes('localhost:18789');
|
|
|
|
if (!isGatewayUrl) {
|
|
callback({ responseHeaders: details.responseHeaders });
|
|
return;
|
|
}
|
|
|
|
const headers = { ...details.responseHeaders };
|
|
delete headers['X-Frame-Options'];
|
|
delete headers['x-frame-options'];
|
|
if (headers['Content-Security-Policy']) {
|
|
headers['Content-Security-Policy'] = headers['Content-Security-Policy'].map(
|
|
(csp) => csp.replace(/frame-ancestors\s+'none'/g, "frame-ancestors 'self' *")
|
|
);
|
|
}
|
|
if (headers['content-security-policy']) {
|
|
headers['content-security-policy'] = headers['content-security-policy'].map(
|
|
(csp) => csp.replace(/frame-ancestors\s+'none'/g, "frame-ancestors 'self' *")
|
|
);
|
|
}
|
|
callback({ responseHeaders: headers });
|
|
});
|
|
|
|
// Register IPC handlers
|
|
registerIpcHandlers(gatewayManager, clawHubService, mainWindow);
|
|
|
|
// Register update handlers
|
|
registerUpdateHandlers(appUpdater, mainWindow);
|
|
|
|
// Note: Auto-check for updates is driven by the renderer (update store init)
|
|
// so it respects the user's "Auto-check for updates" setting.
|
|
|
|
// Minimize to tray on close instead of quitting (macOS & Windows)
|
|
mainWindow.on('close', (event) => {
|
|
if (!isQuitting) {
|
|
event.preventDefault();
|
|
mainWindow?.hide();
|
|
}
|
|
});
|
|
|
|
mainWindow.on('closed', () => {
|
|
mainWindow = null;
|
|
});
|
|
|
|
// Repair any bootstrap files that only contain ClawX markers (no OpenClaw
|
|
// template content). This fixes a race condition where ensureClawXContext()
|
|
// previously created the file before the gateway could seed the full template.
|
|
try {
|
|
repairClawXOnlyBootstrapFiles();
|
|
} catch (error) {
|
|
logger.warn('Failed to repair bootstrap files:', error);
|
|
}
|
|
|
|
// Start Gateway automatically (this seeds missing bootstrap files with full templates)
|
|
try {
|
|
logger.debug('Auto-starting Gateway...');
|
|
await gatewayManager.start();
|
|
logger.info('Gateway auto-start succeeded');
|
|
} catch (error) {
|
|
logger.error('Gateway auto-start failed:', error);
|
|
mainWindow?.webContents.send('gateway:error', String(error));
|
|
}
|
|
|
|
// Merge ClawX context snippets into the workspace bootstrap files.
|
|
// The gateway seeds workspace files asynchronously after its HTTP server
|
|
// is ready, so ensureClawXContext will retry until the target files appear.
|
|
void ensureClawXContext().catch((error) => {
|
|
logger.warn('Failed to merge ClawX context into workspace:', error);
|
|
});
|
|
|
|
// Re-apply ClawX context after every gateway restart because the gateway
|
|
// may re-seed workspace files with clean templates (losing ClawX markers).
|
|
gatewayManager.on('status', (status: { state: string }) => {
|
|
if (status.state === 'running') {
|
|
void ensureClawXContext().catch((error) => {
|
|
logger.warn('Failed to re-merge ClawX context after gateway reconnect:', error);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
// Application lifecycle
|
|
app.whenReady().then(() => {
|
|
initialize();
|
|
|
|
// Register activate handler AFTER app is ready to prevent
|
|
// "Cannot create BrowserWindow before app is ready" on macOS.
|
|
app.on('activate', () => {
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
mainWindow = createWindow();
|
|
} else if (mainWindow && !mainWindow.isDestroyed()) {
|
|
// On macOS, clicking the dock icon should show the window if it's hidden
|
|
mainWindow.show();
|
|
mainWindow.focus();
|
|
}
|
|
});
|
|
});
|
|
|
|
app.on('window-all-closed', () => {
|
|
if (process.platform !== 'darwin') {
|
|
app.quit();
|
|
}
|
|
});
|
|
|
|
app.on('before-quit', () => {
|
|
isQuitting = true;
|
|
// Fire-and-forget: do not await gatewayManager.stop() here.
|
|
// Awaiting inside before-quit can stall Electron's quit sequence.
|
|
void gatewayManager.stop().catch((err) => {
|
|
logger.warn('gatewayManager.stop() error during quit:', err);
|
|
});
|
|
});
|
|
|
|
// Export for testing
|
|
export { mainWindow, gatewayManager };
|