|
| 1 | +// @reqstool-openspec-hooks: 0.1.1 |
| 2 | +import { spawn, ChildProcess } from "child_process"; |
| 3 | +import type { OnReadDocumentHookV1 } from "openspecui/hooks"; |
| 4 | + |
| 5 | +// Minimal MCP client over stdio (JSON-RPC 2.0, newline-delimited). |
| 6 | +// Uses only Node.js built-ins — no npm packages required. |
| 7 | +class McpStdioClient { |
| 8 | + private proc: ChildProcess; |
| 9 | + private buf = ""; |
| 10 | + private pending = new Map< |
| 11 | + number, |
| 12 | + { resolve: (v: unknown) => void; reject: (e: Error) => void } |
| 13 | + >(); |
| 14 | + private id = 1; |
| 15 | + readonly ready: Promise<void>; |
| 16 | + |
| 17 | + constructor(cwd: string) { |
| 18 | + this.proc = spawn("reqstool", ["mcp"], { |
| 19 | + cwd, |
| 20 | + stdio: ["pipe", "pipe", "pipe"], |
| 21 | + }); |
| 22 | + this.proc.stdout!.on("data", (chunk: Buffer) => { |
| 23 | + this.buf += chunk.toString(); |
| 24 | + let nl: number; |
| 25 | + while ((nl = this.buf.indexOf("\n")) !== -1) { |
| 26 | + const line = this.buf.slice(0, nl).trim(); |
| 27 | + this.buf = this.buf.slice(nl + 1); |
| 28 | + if (line) this.handle(line); |
| 29 | + } |
| 30 | + }); |
| 31 | + this.ready = this.init(); |
| 32 | + } |
| 33 | + |
| 34 | + private handle(line: string) { |
| 35 | + try { |
| 36 | + const msg = JSON.parse(line) as { id?: number; result?: unknown; error?: { message: string } }; |
| 37 | + if (msg.id !== undefined) { |
| 38 | + const p = this.pending.get(msg.id); |
| 39 | + if (p) { |
| 40 | + this.pending.delete(msg.id); |
| 41 | + msg.error ? p.reject(new Error(msg.error.message)) : p.resolve(msg.result); |
| 42 | + } |
| 43 | + } |
| 44 | + } catch (e) { |
| 45 | + console.warn("[reqstool-openspec] Skipping non-JSON line from reqstool mcp:", e instanceof Error ? e.message : e); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private send(method: string, params: unknown, expectReply = true): Promise<unknown> { |
| 50 | + if (!expectReply) { |
| 51 | + this.proc.stdin!.write(JSON.stringify({ jsonrpc: "2.0", method, params }) + "\n"); |
| 52 | + return Promise.resolve(); |
| 53 | + } |
| 54 | + const id = this.id++; |
| 55 | + return new Promise((resolve, reject) => { |
| 56 | + this.pending.set(id, { resolve, reject }); |
| 57 | + this.proc.stdin!.write(JSON.stringify({ jsonrpc: "2.0", id, method, params }) + "\n"); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + private async init(): Promise<void> { |
| 62 | + await this.send("initialize", { |
| 63 | + protocolVersion: "2024-11-05", |
| 64 | + capabilities: { tools: {} }, |
| 65 | + clientInfo: { name: "openspecui", version: "1.0" }, |
| 66 | + }); |
| 67 | + this.send("notifications/initialized", {}, false); |
| 68 | + } |
| 69 | + |
| 70 | + async enrich(content: string, preset: string): Promise<string> { |
| 71 | + await this.ready; |
| 72 | + const result = (await this.send("tools/call", { |
| 73 | + name: "enrich_document", |
| 74 | + arguments: { content, preset }, |
| 75 | + })) as { content: { text: string }[] }; |
| 76 | + return result.content[0].text; |
| 77 | + } |
| 78 | + |
| 79 | + close() { |
| 80 | + this.proc.stdin?.end(); |
| 81 | + this.proc.kill(); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +let client: McpStdioClient | null = null; |
| 86 | + |
| 87 | +export const onReadDocument: OnReadDocumentHookV1 = async (ctx, read) => { |
| 88 | + if (!client) { |
| 89 | + client = new McpStdioClient(ctx.projectDir); |
| 90 | + ctx.lifecycle.onDispose(() => { |
| 91 | + client?.close(); |
| 92 | + client = null; |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + const result = await read(); |
| 97 | + const preset = `openspec:${ctx.document.kind}`; |
| 98 | + |
| 99 | + try { |
| 100 | + const enriched = await client.enrich(result.markdown, preset); |
| 101 | + return { ...result, markdown: enriched, sourceLabel: `reqstool ${preset}` }; |
| 102 | + } catch (e) { |
| 103 | + return { |
| 104 | + ...result, |
| 105 | + diagnostics: [{ level: "warning", message: `reqstool enrich failed: ${e}` }], |
| 106 | + }; |
| 107 | + } |
| 108 | +}; |
0 commit comments