import fs from 'node:fs'; import path from 'node:path'; import os from 'node:os'; import { fileURLToPath } from 'node:url'; import { HostController, HOST_ENVIRONMENT } from '../core/desktop/host-controller.mjs'; const args = process.argv.slice(2); if (args.includes('--help')) { console.log('node host-launcher.mjs [--app-root ] [--data-root ] [--node ] [--port <1..65535>]'); process.exit(0); } const command = args.shift(); if (!['start', 'status', 'stop'].includes(command)) throw new Error('Use --help for a supported desktop host operation.'); const options = {}; while (args.length) { const flag = args.shift(), value = args.shift(); if (!['--app-root', '--data-root', '--node', '--port'].includes(flag) || !value || options[flag] !== undefined) throw new Error('Invalid or repeated host option.'); options[flag] = value; } const defaultRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const appRoot = options['--app-root'] || process.env.AGENT_WORKBENCH_APP_DIR || defaultRoot; const dataRoot = options['--data-root'] || process.env.AGENT_WORKBENCH_DATA_DIR || path.join(process.env.LOCALAPPDATA || path.join(os.homedir(), '.local/share'), 'AgentWorkbench'); const nodePath = options['--node'] || (fs.existsSync(path.join(appRoot, 'runtime/node.exe')) ? path.join(appRoot, 'runtime/node.exe') : process.execPath); try { const controller = new HostController({ appRoot, dataRoot, nodePath, port: Number(options['--port'] || process.env.AGENT_WORKBENCH_PORT || 4318), environment: Object.fromEntries(HOST_ENVIRONMENT.filter(key => process.env[key]).map(key => [key, process.env[key]])) }); console.log(JSON.stringify(await controller[command]())); } catch (cause) { console.error(JSON.stringify({ success: false, code: cause.code || 'HOST_FAILED', message: cause.message })); process.exitCode = 1; }