* Scans for an existing DocsAgent service or an available port.
()
| 58 | * Scans for an existing DocsAgent service or an available port. |
| 59 | */ |
| 60 | private async findPort(): Promise<number> { |
| 61 | const START_PORT = 18688; |
| 62 | // We scan a reasonable range first for an existing service |
| 63 | for (let p = START_PORT; p < START_PORT + 20; p++) { |
| 64 | try { |
| 65 | const controller = new AbortController(); |
| 66 | const timeoutId = setTimeout(() => controller.abort(), 100); |
| 67 | const res = await fetch(`http://localhost:${p}/status`, { signal: controller.signal }); |
| 68 | clearTimeout(timeoutId); |
| 69 | if (res.ok) return p; |
| 70 | } catch { |
| 71 | // Not responding or not DocsAgent |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // If no existing service, find the first available port |
| 76 | let p = START_PORT; |
| 77 | while (p < 65535) { |
| 78 | if (await this.isPortAvailable(p)) return p; |
| 79 | p++; |
| 80 | } |
| 81 | throw new Error("No available ports found"); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Starts the C++ background process and waits for the HTTP interface to be ready. |
no test coverage detected