const fs = require('fs');
const http = require('http');

const logPath = 'C:\\Users\\Variet-Worker\\.gemini\\antigravity\\bridge\\extension.log';
const log = fs.readFileSync(logPath, 'utf8');
const match = [...log.matchAll(/port:(\d+)/g)].pop();
if (!match) {
    console.error('No port found');
    process.exit(1);
}
const port = match[1];
console.log(`Port: ${port}`);

const req = http.request(`http://127.0.0.1:${port}/test-rpc`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' }
}, (res) => {
    let data = '';
    res.on('data', c => data += c);
    res.on('end', () => {
        try {
            const json = JSON.parse(data);
            const recent = json.recentTrajectories || [];
            console.log(`recentTrajectories count: ${recent.length}`);
            recent.forEach((t, i) => {
                console.log(`[${i}] googleAgentId: ${t.googleAgentId} summary: ${t.summary} ws: ${t.trajectoryMetadata?.workspaces?.[0]?.workspaceFolderAbsoluteUri}`);
            });
        } catch(e) {
            console.log(`Error parsing json: ${e.message}`);
            console.log(`Raw data: ${data.substring(0, 200)}`);
        }
    });
});
req.write(JSON.stringify({ method: 'GetDiagnostics', args: {} }));
req.end();
