-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodex-hook.js
More file actions
119 lines (119 loc) · 3.97 KB
/
Copy pathcodex-hook.js
File metadata and controls
119 lines (119 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { stdin as processStdin, stdout as processStdout } from "node:process";
import { extractCommentCheckRequests, isRecord, toHookInput, } from "./core.js";
import { runCommentChecker } from "./runner.js";
export function extractCodexCommentCheckRequests(input) {
return extractCommentCheckRequests(toToolResultLike(input));
}
export async function runCommentCheckerPostToolUse(input, options = {}) {
const requests = extractCodexCommentCheckRequests(input);
if (requests.length === 0)
return "";
const runner = options.run ?? runCommentChecker;
const warnings = [];
for (const request of requests) {
const result = await runner(toHookInput(request, {
sessionId: input.session_id,
cwd: input.cwd,
transcriptPath: input.transcript_path ?? undefined,
}));
if (result.status === "missing" || result.status === "pass")
continue;
if (result.status === "error")
continue;
const message = result.message.trim();
if (message.length > 0) {
warnings.push({ filePath: request.filePath, message });
}
}
if (warnings.length === 0)
return "";
return JSON.stringify({
decision: "block",
reason: formatWarnings(warnings),
});
}
export async function runCodexHookCli() {
const input = await readStdin();
if (input.trim().length === 0)
return;
const parsed = parseCodexPostToolUseInput(input);
if (!parsed)
return;
const output = await runCommentCheckerPostToolUse(parsed);
if (output.length > 0) {
processStdout.write(output);
processStdout.write("\n");
}
}
export function parseCodexPostToolUseInput(input) {
let parsed;
try {
parsed = JSON.parse(input);
}
catch {
return undefined;
}
return isCodexPostToolUseInput(parsed) ? parsed : undefined;
}
function toToolResultLike(input) {
return {
toolName: input.tool_name,
input: normalizeToolInput(input.tool_name, input.tool_input),
content: normalizeToolResponse(input.tool_response),
isError: isErrorResponse(input.tool_response),
details: isRecord(input.tool_response) ? input.tool_response : undefined,
};
}
function normalizeToolInput(toolName, toolInput) {
if (toolName === "apply_patch" && typeof toolInput.command === "string") {
return {
...toolInput,
input: toolInput.command,
patch: toolInput.command,
};
}
return toolInput;
}
function normalizeToolResponse(toolResponse) {
if (typeof toolResponse === "string") {
return [{ type: "text", text: toolResponse }];
}
if (isRecord(toolResponse) && typeof toolResponse.text === "string") {
return [{ type: "text", text: toolResponse.text }];
}
return [];
}
function isErrorResponse(toolResponse) {
return isRecord(toolResponse) && toolResponse.is_error === true;
}
function formatWarnings(warnings) {
return warnings
.map((warning) => `comment-checker found issues in ${warning.filePath}:\n${warning.message}`)
.join("\n\n");
}
function isCodexPostToolUseInput(value) {
return (isRecord(value) &&
value.hook_event_name === "PostToolUse" &&
typeof value.session_id === "string" &&
typeof value.turn_id === "string" &&
typeof value.cwd === "string" &&
typeof value.model === "string" &&
typeof value.permission_mode === "string" &&
typeof value.tool_name === "string" &&
isRecord(value.tool_input) &&
typeof value.tool_use_id === "string");
}
function readStdin() {
return new Promise((resolve, reject) => {
let data = "";
processStdin.setEncoding("utf-8");
processStdin.on("data", (chunk) => {
data += chunk;
});
processStdin.once("error", reject);
processStdin.once("end", () => {
resolve(data);
});
});
}
//# sourceMappingURL=codex-hook.js.map