forked from petergyang/human-review
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·319 lines (289 loc) · 11.2 KB
/
Copy pathcli.js
File metadata and controls
executable file
·319 lines (289 loc) · 11.2 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env node
import fs from "node:fs";
import http from "node:http";
import path from "node:path";
import { spawn } from "node:child_process";
import { fileURLToPath } from "node:url";
import { canonicalTarget, ensureStateDir, SERVER_PROTOCOL, serverPath, statePath, targetKey } from "./paths.js";
import { installSkills, shellQuote } from "./setup.js";
const here = path.dirname(fileURLToPath(import.meta.url));
const pkg = JSON.parse(fs.readFileSync(path.join(here, "..", "package.json"), "utf8"));
const HELP = `human-review ${pkg.version}
human-review <file-or-localhost-url> Open a file or localhost page for review
human-review poll <target> Wait for feedback, print it as JSON (for agents)
--ack Acknowledge the last batch, then keep waiting
--timeout <secs> Exit with {"status":"timeout"} if nothing arrives
human-review status <target> Report whether feedback is waiting, without blocking
human-review setup Teach Claude Code / Codex how to use human-review
human-review setup --global ...for every project, not just this one
Everything runs locally. No account, no cloud, no database.
`;
// --------------------------------------------------------------- server glue
function readServerRecord() {
try {
return JSON.parse(fs.readFileSync(serverPath(), "utf8"));
} catch {
return null;
}
}
function request(server, options, body) {
const port = typeof server === "number" ? server : server.port;
const token = typeof server === "number" ? "" : server.token || "";
return new Promise((resolve, reject) => {
const req = http.request(
{
host: "127.0.0.1",
port,
...options,
headers: { ...(token ? { "x-human-review-token": token } : {}), ...(options.headers || {}) },
},
(res) => {
let raw = "";
res.setEncoding("utf8");
res.on("data", (chunk) => {
raw += chunk;
});
res.on("end", () => resolve({ status: res.statusCode, raw }));
}
);
req.on("error", reject);
if (options.timeout) req.setTimeout(options.timeout, () => req.destroy(new Error("timeout")));
if (body) req.write(JSON.stringify(body));
req.end();
});
}
async function alive(port) {
try {
const res = await request(port, { method: "GET", path: "/health", timeout: 1200 });
return res.status === 200;
} catch {
return false;
}
}
async function ensureServer() {
ensureStateDir();
const saved = readServerRecord();
if (saved?.protocol === SERVER_PROTOCOL && saved.port && (await alive(saved.port))) return saved;
const child = spawn(process.execPath, [path.join(here, "server-entry.js")], {
detached: true,
stdio: "ignore",
});
child.unref();
for (let attempt = 0; attempt < 60; attempt += 1) {
await new Promise((r) => setTimeout(r, 100));
const record = readServerRecord();
// Same protocol gate as above: a still-running server from an older
// version answers /health too, and must not be adopted here.
if (record?.protocol === SERVER_PROTOCOL && record.port && (await alive(record.port))) return record;
}
throw new Error("Could not start the local human-review server.");
}
function openBrowser(url) {
const command =
process.platform === "darwin" ? ["open", [url]] : process.platform === "win32" ? ["cmd", ["/c", "start", "", url]] : ["xdg-open", [url]];
const child = spawn(command[0], command[1], { detached: true, stdio: "ignore" });
// A missing opener (headless Linux without xdg-open) surfaces as an async
// 'error' event, not a throw. Printing the URL below is the fallback.
child.on("error", () => {});
child.unref();
}
// ------------------------------------------------------------------ commands
async function openCommand(input) {
const target = canonicalTarget(input);
if (target.kind === "file" && !fs.existsSync(target.value)) {
console.error(`File not found: ${target.value}`);
process.exit(1);
}
const server = await ensureServer();
const res = await request(server, { method: "POST", path: "/api/session", headers: { "content-type": "application/json" } }, { target: target.value });
const body = JSON.parse(res.raw);
if (res.status !== 200) {
console.error(body.error || "Could not open that file.");
process.exit(1);
}
const url = `http://127.0.0.1:${server.port}${body.path}`;
openBrowser(url);
console.log(`Reviewing ${target.kind === "url" ? target.value : path.basename(target.value)}`);
console.log(url);
console.log(`\nWaiting for feedback? Run:\n human-review poll ${shellQuote(target.value)}`);
}
/**
* One long-poll attempt. Resolves { kind: "data", raw } when the server
* answers, or { kind: "timeout" } when the caller's deadline passes first.
*/
function pollOnce(server, target, ack, timeoutMs) {
const query = `target=${encodeURIComponent(target)}${ack ? "&ack=1" : ""}`;
return new Promise((resolve, reject) => {
let done = false;
const settle = (fn, value) => {
if (done) return;
done = true;
clearTimeout(timer);
fn(value);
};
const req = http.request(
{
host: "127.0.0.1",
port: server.port,
method: "GET",
path: `/api/poll?${query}`,
headers: { "x-human-review-token": server.token || "" },
},
(res) => {
let raw = "";
res.setEncoding("utf8");
res.on("data", (chunk) => {
raw += chunk;
});
res.on("end", () => settle(resolve, { kind: "data", raw: raw.trim() }));
}
);
const timer = timeoutMs
? setTimeout(() => {
settle(resolve, { kind: "timeout" });
req.destroy();
}, timeoutMs)
: null;
req.on("error", (err) => settle(reject, err));
req.end();
});
}
/**
* The consumer is an agent reading a pipe. process.exit() does not wait for
* pending stdout writes, so a large payload could arrive truncated — always
* wait for the write to hand off before returning.
*/
function writeStdout(text) {
return new Promise((resolve) => process.stdout.write(text, resolve));
}
function printTimeout(waitedSecs) {
const payload = {
status: "timeout",
waited_seconds: waitedSecs,
next_step:
"No feedback yet. Run the same poll command again to keep waiting, or `human-review status <target>` to check without blocking.",
};
return writeStdout(`${JSON.stringify(payload, null, 2)}\n`);
}
async function pollCommand(input, { ack = false, timeoutSecs = 0 } = {}) {
const target = canonicalTarget(input).value;
const server = await ensureServer();
const label = /^https?:\/\//i.test(target) ? target : path.basename(target);
process.stderr.write(`Waiting for feedback on ${label} — comment in the browser, then hit Send.\n`);
const deadline = timeoutSecs ? Date.now() + timeoutSecs * 1000 : null;
for (let attempt = 0; attempt < 3; attempt += 1) {
const remaining = deadline ? deadline - Date.now() : 0;
if (deadline && remaining <= 0) return printTimeout(timeoutSecs);
let result;
try {
result = await pollOnce(server, target, ack && attempt === 0, remaining);
} catch (err) {
process.stderr.write(`Lost the connection (${err.message}); retrying.\n`);
continue;
}
if (result.kind === "timeout") return printTimeout(timeoutSecs);
if (!result.raw) continue;
try {
const batch = JSON.parse(result.raw);
await writeStdout(`${JSON.stringify(batch, null, 2)}\n`);
return;
} catch {
process.stderr.write("Unexpected response from the human-review server; retrying.\n");
}
}
process.stderr.write("Gave up waiting for feedback.\n");
process.exit(1);
}
/**
* Instant answer, no blocking. Asks the running server when there is one;
* otherwise reads the persisted state directly, so a dead server still
* reports feedback that is waiting for a fresh poll.
*/
async function statusCommand(input) {
const target = canonicalTarget(input).value;
const saved = readServerRecord();
if (saved?.protocol === SERVER_PROTOCOL && saved.port && (await alive(saved.port))) {
const res = await request(saved, { method: "GET", path: `/api/status?target=${encodeURIComponent(target)}` });
if (res.status === 200) {
process.stdout.write(`${JSON.stringify(JSON.parse(res.raw), null, 2)}\n`);
return;
}
}
let data = { pages: {}, batches: {} };
try {
data = JSON.parse(fs.readFileSync(statePath(), "utf8"));
} catch {
// No state yet: everything below reads as empty.
}
const key = targetKey(target);
const pending = (data.batches || {})[key];
const page = (data.pages || {})[key];
const payload = {
status: pending ? "feedback-waiting" : "idle",
feedback_waiting: !!pending,
agent_listening: false,
server_running: false,
unsent: {
comments: page ? page.comments.length : 0,
edits: page ? page.edits.length : 0,
},
};
process.stdout.write(`${JSON.stringify(payload, null, 2)}\n`);
}
// ---------------------------------------------------------------------- main
const argv = process.argv.slice(2);
if (argv.length === 0 || argv[0] === "--help" || argv[0] === "-h" || argv[0] === "help") {
console.log(HELP);
process.exit(0);
}
if (argv[0] === "--version" || argv[0] === "-v") {
console.log(pkg.version);
process.exit(0);
}
process.on("SIGINT", () => {
process.stderr.write("\nStopped waiting. Your feedback is safe — run the same command again to pick it up.\n");
process.exit(130);
});
function parsePollArgs(rest) {
const parsed = { file: "", ack: false, timeoutSecs: 0 };
let sawTimeout = false;
for (let i = 0; i < rest.length; i += 1) {
const arg = rest[i];
if (arg === "--ack") parsed.ack = true;
else if (arg === "--timeout") {
sawTimeout = true;
parsed.timeoutSecs = Number(rest[(i += 1)]);
} else if (arg.startsWith("--timeout=")) {
sawTimeout = true;
parsed.timeoutSecs = Number(arg.slice("--timeout=".length));
} else if (!arg.startsWith("-") && !parsed.file) parsed.file = arg;
}
// A malformed value must fail loudly — NaN or 0 silently waiting forever is
// the exact hang the flag exists to prevent.
if (sawTimeout && (!Number.isFinite(parsed.timeoutSecs) || parsed.timeoutSecs <= 0)) {
throw new Error("--timeout wants a number of seconds, e.g. --timeout 300");
}
return parsed;
}
try {
if (argv[0] === "poll") {
const { file, ack, timeoutSecs } = parsePollArgs(argv.slice(1));
if (!file) throw new Error("Usage: human-review poll <file-or-localhost-url> [--ack] [--timeout <secs>]");
await pollCommand(file, { ack, timeoutSecs });
} else if (argv[0] === "status") {
const file = argv.find((a, i) => i > 0 && !a.startsWith("-"));
if (!file) throw new Error("Usage: human-review status <file-or-localhost-url>");
await statusCommand(file);
} else if (argv[0] === "setup") {
const isGlobal = argv.includes("--global") || argv.includes("-g");
installSkills(process.cwd(), { global: isGlobal }).forEach((line) => console.log(line));
} else {
await openCommand(argv[0]);
}
} catch (err) {
console.error(err.message || String(err));
process.exit(1);
}
// A finished command must never linger on stray handles (keep-alive sockets
// from health checks, for one) — on Windows that hangs the calling shell.
process.exit(0);