| 416 | } |
| 417 | |
| 418 | char *cbm_client_adapter_opencode(const char *binary_path) { |
| 419 | if (!binary_path || !binary_path[0]) { |
| 420 | return NULL; |
| 421 | } |
| 422 | char bin[CBM_SZ_1K]; |
| 423 | if (!cbm_client_adapter_escape_js(binary_path, bin, sizeof(bin))) { |
| 424 | return NULL; |
| 425 | } |
| 426 | |
| 427 | adapter_sb_t sb = {0}; |
| 428 | emit_header(&sb, "OpenCode"); |
| 429 | sb_append(&sb, "// OpenCode already reaches every tool over MCP; this module adds the\n" |
| 430 | "// context surfaces other clients get through hook configuration: graph\n" |
| 431 | "// lookup after grep/glob, index-coverage notes after read, session-start\n" |
| 432 | "// tier routing (carried on the first tool result of each session, since\n" |
| 433 | "// OpenCode documents no context-output lifecycle hook), and reinjection\n" |
| 434 | "// after compaction via the documented experimental surface.\n"); |
| 435 | sb_append(&sb, "import { spawn } from 'node:child_process';\n\n"); |
| 436 | sb_append(&sb, "const BIN = '"); |
| 437 | sb_append(&sb, bin); |
| 438 | sb_append(&sb, "';\n\n"); |
| 439 | |
| 440 | /* hook-augment requires hook_event_name; its default dialect accepts |
| 441 | * Grep/Glob under PreToolUse, Read under PostToolUse, and the |
| 442 | * SessionStart lifecycle event — and emits the Claude JSON envelope, so |
| 443 | * the plugin unwraps additionalContext instead of pasting raw JSON into |
| 444 | * the tool output. Every failure path resolves to '' (fail open). */ |
| 445 | sb_append(&sb, "function augment(payload) {\n" |
| 446 | " return new Promise((resolve) => {\n" |
| 447 | " const child = spawn(BIN, ['hook-augment'], {\n" |
| 448 | " stdio: ['pipe', 'pipe', 'ignore'],\n" |
| 449 | " env: { ...process.env, CBM_LOG_LEVEL: 'error' },\n" |
| 450 | " });\n" |
| 451 | " let out = '';\n" |
| 452 | " child.stdout.on('data', (d) => (out += d.toString()));\n" |
| 453 | " child.on('error', () => resolve(''));\n" |
| 454 | " child.on('close', () => {\n" |
| 455 | " try {\n" |
| 456 | " const ctx = JSON.parse(out)?.hookSpecificOutput?.additionalContext;\n" |
| 457 | " resolve(typeof ctx === 'string' ? ctx : '');\n" |
| 458 | " } catch { resolve(''); }\n" |
| 459 | " });\n" |
| 460 | " child.stdin.end(JSON.stringify(payload));\n" |
| 461 | " });\n" |
| 462 | "}\n\n"); |
| 463 | |
| 464 | sb_append( |
| 465 | &sb, "export default {\n" |
| 466 | " id: 'codebase-memory-augment',\n" |
| 467 | " // V2 config loader: requires id + setup|effect; no tool domain yet\n" |
| 468 | " // at this point, so there is nothing to register here.\n" |
| 469 | " setup() {},\n" |
| 470 | " // Server runtime: reads default.server and dispatches the hooks it returns.\n" |
| 471 | " server: async (ctx) => {\n" |
| 472 | " const dir = ctx?.directory;\n" |
| 473 | " const seen = new Set();\n" |
| 474 | " const lifecycle = () =>\n" |
| 475 | " augment({ hook_event_name: 'SessionStart', cwd: dir });\n" |
no test coverage detected