| 301 | } |
| 302 | |
| 303 | char *cbm_client_adapter_pi(const char *binary_path) { |
| 304 | if (!binary_path || !binary_path[0]) { |
| 305 | return NULL; |
| 306 | } |
| 307 | char bin[CBM_SZ_1K]; |
| 308 | if (!cbm_client_adapter_escape_js(binary_path, bin, sizeof(bin))) { |
| 309 | return NULL; |
| 310 | } |
| 311 | int count = cbm_mcp_tool_count(); |
| 312 | if (count <= 0) { |
| 313 | return NULL; |
| 314 | } |
| 315 | |
| 316 | adapter_sb_t sb = {0}; |
| 317 | emit_header(&sb, "pi"); |
| 318 | /* Pin the coding-agent contract so a probe against @mariozechner/pi |
| 319 | * (pods CLI) or an old AgentTool arity cannot be mistaken for this file. */ |
| 320 | sb_append(&sb, "// Target: @earendil-works/pi-coding-agent >= 0.74.0 (verified 0.84.2)\n" |
| 321 | "// ToolDefinition.execute(toolCallId, params, signal, onUpdate, ctx)\n"); |
| 322 | sb_append(&sb, "import { spawn } from 'node:child_process';\n"); |
| 323 | sb_append(&sb, "import { Type } from 'typebox';\n\n"); |
| 324 | sb_append(&sb, "const BIN = '"); |
| 325 | sb_append(&sb, bin); |
| 326 | sb_append(&sb, "';\n\n"); |
| 327 | |
| 328 | /* One shared invoker: the binary prints JSON on stdout, so take the last |
| 329 | * line that parses rather than trusting the first — a stray log line on |
| 330 | * stdout must not break the bridge. */ |
| 331 | sb_append( |
| 332 | &sb, "async function call(tool, args, signal) {\n" |
| 333 | " return new Promise((resolve) => {\n" |
| 334 | " const child = spawn(BIN, ['cli', '--json', tool], {\n" |
| 335 | " stdio: ['pipe', 'pipe', 'pipe'],\n" |
| 336 | " env: { ...process.env, CBM_LOG_LEVEL: 'error' },\n" |
| 337 | " });\n" |
| 338 | " let out = '';\n" |
| 339 | " let stdinError;\n" |
| 340 | " const onAbort = () => { if (!child.killed) child.kill(); };\n" |
| 341 | " signal?.addEventListener('abort', onAbort, { once: true });\n" |
| 342 | " child.stdout.on('data', (d) => (out += d.toString()));\n" |
| 343 | " child.stdin.on('error', (e) => { stdinError = e; });\n" |
| 344 | " child.on('error', (e) => {\n" |
| 345 | " signal?.removeEventListener('abort', onAbort);\n" |
| 346 | " resolve({ error: String(e && e.message ? e.message : e) });\n" |
| 347 | " });\n" |
| 348 | " child.on('close', () => {\n" |
| 349 | " signal?.removeEventListener('abort', onAbort);\n" |
| 350 | " const lines = out.split('\\n').map((l) => l.trim()).filter(Boolean);\n" |
| 351 | " for (let i = lines.length - 1; i >= 0; i--) {\n" |
| 352 | " try { return resolve(JSON.parse(lines[i])); } catch { /* keep scanning */ }\n" |
| 353 | " }\n" |
| 354 | " if (stdinError) {\n" |
| 355 | " return resolve({ error: String(stdinError.message || stdinError) });\n" |
| 356 | " }\n" |
| 357 | " resolve({ error: 'no JSON response from codebase-memory-mcp' });\n" |
| 358 | " });\n" |
| 359 | " child.stdin.end(JSON.stringify(args ?? {}));\n" |
| 360 | " });\n" |
no test coverage detected