File size: 4,164 Bytes
b9a3ef2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// ---------------------------------------------------------------------------
// 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);
});