import { stdin as processStdin } from "node:process"; import { disposeDefaultLspManager } from "@code-yeongyu/lsp-tools-mcp/dist/lsp/manager.js"; 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; let parsed: unknown; try { parsed = JSON.parse(raw); } catch (error) { if (error instanceof SyntaxError) return; throw error; } const input = isRecord(parsed) ? parsed : {}; const output = await runHook(input); if (output) process.stdout.write(output); } finally { await disposeDefaultLspManager(); } } async function readStdin(stdin: NodeJS.ReadStream): Promise { stdin.setEncoding("utf8"); let raw = ""; for await (const chunk of stdin) { raw += chunk; } return raw; }