// --------------------------------------------------------------------------- // Server Entry Point // --------------------------------------------------------------------------- import express from 'express'; import http from 'http'; import { Server as SocketServer } from 'socket.io'; import path from 'path'; import cors from 'cors'; import { setupToolRegistry } from './toolRegistry'; import { setupBridge, getSettings, updateSettings, redetectCLI } from './bridge'; import { detectCLI } from './cliDetector'; const PORT = parseInt(process.env.PORT || '3777', 10); async function main() { console.log(''); console.log(' =========================================='); console.log(' Agent Bridge v2.1'); console.log(' =========================================='); console.log(''); // --- CLI Detection --- console.log(' Detecting Antigravity CLI...'); // Respect saved settings const savedSettings = getSettings(); if (savedSettings.cliPath) { console.log(` Using saved CLI path: ${savedSettings.cliPath}`); process.env.ANTIGRAVITY_CLI_PATH = savedSettings.cliPath; } const cliStatus = await detectCLI(); if (cliStatus.detected) { console.log(` CLI detected: ${cliStatus.path}`); console.log(` Version: ${cliStatus.version}`); console.log(` Method: ${cliStatus.method}`); } else { console.log(' CLI not found.'); console.log(' Searched:', cliStatus.candidates.length, 'locations'); console.log(''); console.log(' Configure the CLI path via the webapp Settings panel'); console.log(' or set ANTIGRAVITY_CLI_PATH environment variable.'); } console.log(''); // --- Tool Registry --- const toolRegistry = setupToolRegistry(); console.log(` Tools loaded: ${toolRegistry.listTools().length}`); console.log(''); // --- Express --- const app = express(); const server = http.createServer(app); app.use(cors()); app.use(express.json()); // Serve client files const clientDir = path.join(__dirname, '..', 'client'); app.use(express.static(clientDir)); // Output files app.use('/output', express.static(path.join(__dirname, '..', 'output'))); // Download endpoint app.get('/api/download/:filename', (req, res) => { const filePath = path.join(__dirname, '..', 'output', req.params.filename); res.download(filePath, (err) => { if (err && !res.headersSent) { res.status(404).json({ error: 'File not found.' }); } }); }); // Health / Status app.get('/api/health', async (_req, res) => { res.json({ status: 'ok', cli: { detected: cliStatus.detected, version: cliStatus.version, method: cliStatus.method }, tools: toolRegistry.listTools().length, timestamp: new Date().toISOString(), }); }); // Settings API (REST) app.get('/api/settings', (_req, res) => { res.json(getSettings()); }); app.post('/api/settings', async (req, res) => { const updated = updateSettings(req.body); const newCli = await redetectCLI(); res.json({ settings: updated, cli: newCli }); }); // Re-detect CLI app.post('/api/redetect', async (_req, res) => { const result = await redetectCLI(); res.json(result); }); // --- WebSocket --- const io = new SocketServer(server, { cors: { origin: '*', methods: ['GET', 'POST'] }, maxHttpBufferSize: 10e6, }); setupBridge(io, toolRegistry, cliStatus); // --- Start --- server.listen(PORT, () => { console.log(' =========================================='); console.log(` Running at http://localhost:${PORT}`); console.log(' =========================================='); console.log(''); }); } main().catch((err) => { console.error('Fatal error:', err); process.exit(1); });