An agent session: `install`'s launcher, `serve --mcp` in the project.
(install: string | null, env: NodeJS.ProcessEnv = {})
| 447 | |
| 448 | /** An agent session: `install`'s launcher, `serve --mcp` in the project. */ |
| 449 | function startSession(install: string | null, env: NodeJS.ProcessEnv = {}): Session { |
| 450 | const bin = install ? path.join(install, 'dist', 'bin', 'codegraph.js') : BIN; |
| 451 | const recorder = recordSpawns(tempDir); |
| 452 | const child = spawn(process.execPath, [...WASM_RUNTIME_FLAGS, ...recorder.args, bin, 'serve', '--mcp'], { |
| 453 | cwd: tempDir, |
| 454 | stdio: ['pipe', 'pipe', 'pipe'], |
| 455 | env: { |
| 456 | ...process.env, |
| 457 | CODEGRAPH_MCP_LOG_ATTACH: '1', |
| 458 | CODEGRAPH_DAEMON_IDLE_TIMEOUT_MS: '30000', |
| 459 | ...recorder.env, |
| 460 | ...env, |
| 461 | }, |
| 462 | }) as ChildProcessWithoutNullStreams; |
| 463 | child.on('error', () => { /* ignore */ }); |
| 464 | child.stdin.on('error', () => { /* ignore */ }); |
| 465 | const session: Session = { child, stdout: [], stderr: [] }; |
| 466 | lines(child.stdout, session.stdout); |
| 467 | lines(child.stderr, session.stderr); |
| 468 | sessions.push(session); |
| 469 | send(session, { |
| 470 | jsonrpc: '2.0', id: 1, method: 'initialize', |
| 471 | params: { protocolVersion: '2024-11-05', capabilities: {}, clientInfo: { name: 'test', version: '0.0.0' }, rootUri: `file://${tempDir}` }, |
| 472 | }); |
| 473 | return session; |
| 474 | } |
| 475 | |
| 476 | function send(session: Session, msg: unknown): void { |
| 477 | try { session.child.stdin.write(JSON.stringify(msg) + '\n'); } catch { /* gone */ } |