import { stdin as processStdin } from "node:process"; import { disposeDefaultLspManager } from "@code-yeongyu/lsp-daemon"; import { isRecord, runLspPostCompactHook, runLspPostToolUseHook } from "./codex-hook.js"; export async function runPostToolUseHookCli(stdin: NodeJS.ReadStream = processStdin): Promise { await runHookCli((input) => runLspPostToolUseHook(input), stdin); } export async function runPostCompactHookCli(stdin: NodeJS.ReadStream = processStdin): Promise { await runHookCli((input) => runLspPostCompactHook(input), stdin); } async function runHookCli( runHook: (input: Record) => Promise, stdin: NodeJS.ReadStream, ): Promise { try { const raw = await readStdin(stdin); if (!raw.trim()) return; const parsed: unknown = JSON.parse(raw); const input = isRecord(parsed) ? parsed : {}; const output = await runHook(input); if (output) process.stdout.write(output); } catch { // LSP feedback is best-effort: stderr or a non-zero exit here surfaces as harness noise on every edit. return; } finally { await disposeDefaultLspManager().catch(() => undefined); } } async function readStdin(stdin: NodeJS.ReadStream): Promise { stdin.setEncoding("utf8"); let raw = ""; for await (const chunk of stdin) { raw += chunk; } return raw; }