|
|
|
|
|
|
|
|
| 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('');
|
|
|
|
|
| console.log(' Detecting Antigravity CLI...');
|
|
|
|
|
| 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('');
|
|
|
|
|
| const toolRegistry = setupToolRegistry();
|
| console.log(` Tools loaded: ${toolRegistry.listTools().length}`);
|
| console.log('');
|
|
|
|
|
| const app = express();
|
| const server = http.createServer(app);
|
|
|
| app.use(cors());
|
| app.use(express.json());
|
|
|
|
|
| const clientDir = path.join(__dirname, '..', 'client');
|
| app.use(express.static(clientDir));
|
|
|
|
|
| app.use('/output', express.static(path.join(__dirname, '..', 'output')));
|
|
|
|
|
| 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.' });
|
| }
|
| });
|
| });
|
|
|
|
|
| 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(),
|
| });
|
| });
|
|
|
|
|
| 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 });
|
| });
|
|
|
|
|
| app.post('/api/redetect', async (_req, res) => {
|
| const result = await redetectCLI();
|
| res.json(result);
|
| });
|
|
|
|
|
| const io = new SocketServer(server, {
|
| cors: { origin: '*', methods: ['GET', 'POST'] },
|
| maxHttpBufferSize: 10e6,
|
| });
|
|
|
| setupBridge(io, toolRegistry, cliStatus);
|
|
|
|
|
| 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);
|
| });
|
|
|