48 lines
2.6 KiB
TypeScript
48 lines
2.6 KiB
TypeScript
// mock-reviewer (PEP-26): stands in for `claude -p --output-format json` as the reviewer agent.
|
||
// Reads the prompt (story + checklist + PR diff) on stdin, answers with the CLI's JSON envelope
|
||
// whose `result` is the verdict. Behaviour comes from MOCK_REVIEW_SCENARIO (a JSON file):
|
||
// { "responses": [{ "verdict": "changes", "summary": "…", "findings": [{ "file": "a.ts", "line": 3, "text": "…" }] },
|
||
// { "verdict": "ok", "summary": "…" }], // n-th call → n-th response (last one repeats)
|
||
// "fail": false, "hang": false, "garbage": false }
|
||
// Every call is appended to <scenario>.log.jsonl (args, story, whether the diff and PM rules came through, token).
|
||
import { appendFileSync, existsSync, readFileSync } from "node:fs";
|
||
|
||
interface Scenario {
|
||
responses?: Array<{ verdict: string; summary?: string; findings?: unknown[] }>;
|
||
fail?: boolean;
|
||
hang?: boolean;
|
||
garbage?: boolean;
|
||
}
|
||
|
||
const file = process.env.MOCK_REVIEW_SCENARIO;
|
||
const sc: Scenario = file && existsSync(file) ? JSON.parse(readFileSync(file, "utf8")) : {};
|
||
const prompt = readFileSync(0, "utf8");
|
||
const logFile = `${file}.log.jsonl`;
|
||
const calls = file && existsSync(logFile) ? readFileSync(logFile, "utf8").trim().split("\n").filter(Boolean).length : 0;
|
||
if (file) {
|
||
appendFileSync(
|
||
logFile,
|
||
JSON.stringify({
|
||
args: process.argv.slice(2),
|
||
story: /^Стори (\S+):/m.exec(prompt)?.[1] ?? null,
|
||
diff: /```diff\n[\s\S]*diff --git/.test(prompt),
|
||
diffText: /```diff\n([\s\S]*?)\n```/.exec(prompt)?.[1] ?? "",
|
||
rules: /Дополнительные правила PM:\n([\s\S]*?)\n\nПроект:/.exec(prompt)?.[1] ?? null,
|
||
token: process.env.CLAUDE_CODE_OAUTH_TOKEN ? "set" : null,
|
||
}) + "\n",
|
||
);
|
||
}
|
||
if (!process.argv.includes("-p")) {
|
||
console.error("mock-reviewer: expected -p (non-interactive print mode)");
|
||
process.exit(2);
|
||
}
|
||
if (sc.hang) await new Promise(() => setInterval(() => {}, 1000)); // never answers
|
||
if (sc.fail) {
|
||
console.error("mock-reviewer: API Error: 529 overloaded");
|
||
process.exit(1);
|
||
}
|
||
const responses = sc.responses?.length ? sc.responses : [{ verdict: "ok", summary: "mock: замечаний нет" }];
|
||
const verdict = responses[Math.min(calls, responses.length - 1)]!;
|
||
const result = sc.garbage ? "Выглядит неплохо, но я не уверен." : `Вот результат ревью:\n\`\`\`json\n${JSON.stringify({ findings: [], ...verdict })}\n\`\`\``;
|
||
process.stdout.write(JSON.stringify({ type: "result", subtype: "success", is_error: false, num_turns: 1, result, session_id: "mock-reviewer" }) + "\n");
|