()
| 72 | |
| 73 | // Start the SPARC2 API server |
| 74 | async function startSPARC2API() { |
| 75 | console.log('Finding available port for SPARC2 API server...'); |
| 76 | |
| 77 | // Find an available port |
| 78 | SPARC2_API_PORT = await findAvailablePort(SPARC2_API_PORT_START); |
| 79 | console.log(`Starting SPARC2 API server on port ${SPARC2_API_PORT}...`); |
| 80 | |
| 81 | const denoProcess = spawn('deno', [ |
| 82 | 'run', |
| 83 | '--allow-read', |
| 84 | '--allow-write', |
| 85 | '--allow-env', |
| 86 | '--allow-net', |
| 87 | '--allow-run', |
| 88 | SPARC2_CLI_PATH, |
| 89 | 'api', |
| 90 | '--port', SPARC2_API_PORT.toString() |
| 91 | ], { |
| 92 | stdio: 'pipe', |
| 93 | cwd: path.resolve(__dirname, '..') // Set working directory to SPARC2 root |
| 94 | }); |
| 95 | |
| 96 | denoProcess.stdout.on('data', (data) => { |
| 97 | const message = data.toString().trim(); |
| 98 | console.log(`[SPARC2 API] ${message}`); |
| 99 | |
| 100 | // Broadcast log messages to all connected clients |
| 101 | broadcastToAll({ |
| 102 | type: 'log', |
| 103 | message |
| 104 | }); |
| 105 | }); |
| 106 | |
| 107 | denoProcess.stderr.on('data', (data) => { |
| 108 | const message = data.toString().trim(); |
| 109 | console.log(`[SPARC2 API Error] ${message}`); |
| 110 | |
| 111 | // Broadcast error messages to all connected clients |
| 112 | broadcastToAll({ |
| 113 | type: 'error', |
| 114 | message |
| 115 | }); |
| 116 | }); |
| 117 | |
| 118 | // Wait for the API server to start |
| 119 | return new Promise((resolve, reject) => { |
| 120 | const maxAttempts = 30; |
| 121 | let attempts = 0; |
| 122 | |
| 123 | const checkServer = () => { |
| 124 | attempts++; |
| 125 | http.get(`http://localhost:${SPARC2_API_PORT}/discover`, (res) => { |
| 126 | if (res.statusCode === 200) { |
| 127 | console.log(`SPARC2 API server is running on port ${SPARC2_API_PORT}`); |
| 128 | resolve(); |
| 129 | } else if (attempts < maxAttempts) { |
| 130 | setTimeout(checkServer, 1000); |
| 131 | } else { |
no test coverage detected