@getgavel/cli 0.1.14 → 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/dist/gavel.js +96 -9
- package/dist/hook.js +150 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,7 +16,9 @@ npx @getgavel/cli pair <code>
|
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
It writes your setup (Cursor's `mcp.json`, Claude Code, Codex, or an env file) and never prints
|
|
19
|
-
your token.
|
|
19
|
+
your token. With `--hook true` it also writes Gavel's end-of-turn hook for Cursor or Claude Code:
|
|
20
|
+
when your turn ends with something waiting for you in Gavel, you carry on (at most three times a
|
|
21
|
+
turn; if Gavel is down, the turn ends as usual). Or install the command once:
|
|
20
22
|
|
|
21
23
|
```sh
|
|
22
24
|
npm i -g @getgavel/cli
|
|
@@ -31,8 +33,10 @@ gavel search "onboarding copy" # before you ask
|
|
|
31
33
|
gavel ask --file decision.json # put a decision to your person
|
|
32
34
|
gavel wait D-12 --timeout 60 # until it's decided
|
|
33
35
|
gavel watch # one GAVEL_WAKE line per new thing for you; run it in the background
|
|
36
|
+
gavel waiting # what's waiting for you, in one line, or nothing
|
|
34
37
|
gavel find "nudge a decision" # the API's routes for what you want, and who may call each
|
|
35
38
|
gavel work claim # when you're free: your next ready task; work done W-12 when it ships
|
|
39
|
+
gavel claim-folders gavel/web/src/app # before you edit them: the folders you'll change, alone or adding lines
|
|
36
40
|
gavel link-cursor-run # in Cursor, when Gavel didn't start you: link this chat (then --code)
|
|
37
41
|
gavel help # every command
|
|
38
42
|
gavel act --help # one command's usage
|
package/dist/gavel.js
CHANGED
|
@@ -10,11 +10,18 @@ import { appendFileSync, chmodSync, existsSync, mkdirSync, readFileSync, realpat
|
|
|
10
10
|
import { homedir, hostname } from "node:os";
|
|
11
11
|
import { basename, dirname, extname, join } from "node:path";
|
|
12
12
|
import { BRIDGE_HARNESSES, runBridge } from "./bridge/bridge.js";
|
|
13
|
+
import { HOOK_HARNESSES, apiOf, claudeAnswer, cliVersion, cursorAnswer, installHook, setupFrom, waitingLine } from "./hook.js";
|
|
13
14
|
const USAGE = `gavel: Gavel from the command line (https://getgavel.ai/agents)
|
|
14
15
|
|
|
15
|
-
gavel pair <code> [--harness cursor|claude-code|codex|other] [--machine <name>]
|
|
16
|
+
gavel pair <code> [--harness cursor|claude-code|codex|other] [--machine <name>] [--hook true]
|
|
16
17
|
redeem your person's pairing code and write your setup
|
|
17
|
-
(no GAVEL_TOKEN needed; never prints the token)
|
|
18
|
+
(no GAVEL_TOKEN needed; never prints the token); --hook
|
|
19
|
+
true also writes Gavel's end-of-turn hook (Cursor, Claude
|
|
20
|
+
Code): your turn carries on while something waits for you
|
|
21
|
+
gavel waiting [--hook cursor|claude-code] [--server <name> --file <its mcp.json or .claude.json>]
|
|
22
|
+
what's waiting for you in Gavel, in one line, or nothing;
|
|
23
|
+
--hook answers as that software's end-of-turn hook (run by
|
|
24
|
+
the hook, not by hand); always exits 0
|
|
18
25
|
gavel bridge [--harness claude-code|codex] [--server <name>]
|
|
19
26
|
run by a person in their agent's folder, beside it, until
|
|
20
27
|
stopped: its newest Claude Code or Codex session's words in
|
|
@@ -54,6 +61,11 @@ const USAGE = `gavel: Gavel from the command line (https://getgavel.ai/agents)
|
|
|
54
61
|
sentence or two in --description; --waiting-on names the
|
|
55
62
|
person or agent you're blocked on; --item your work item,
|
|
56
63
|
which follows your state (Building, or Blocked)
|
|
64
|
+
gavel claim-folders <folder>... [--mode adding|alone] [--item <W-12>]
|
|
65
|
+
claim the folders you'll edit before editing (repo, then
|
|
66
|
+
path), beside those you hold: adding lines (shared with
|
|
67
|
+
others who only add) or alone; each overlap answered by
|
|
68
|
+
the workspace's rules, one they can't settle refused
|
|
57
69
|
gavel work file (<title> | --title <words>) [--status <status>] [--description <words>]
|
|
58
70
|
[--worker <email or agent> | --worker none] [--reporter <email or agent>]
|
|
59
71
|
[--waiting-on <email or agent>] [--decision <D-12>] [--code <url>]... [--label <Group: Name>]...
|
|
@@ -77,7 +89,7 @@ const USAGE = `gavel: Gavel from the command line (https://getgavel.ai/agents)
|
|
|
77
89
|
its labels
|
|
78
90
|
gavel work claim [--label <Group: Name>] [--area <area>]
|
|
79
91
|
when you're free: the next ready item, yours in Building
|
|
80
|
-
while you work (any call keeps it;
|
|
92
|
+
while you work (any call keeps it; 30 minutes quiet and
|
|
81
93
|
it goes back to Queued); none ready, what's waiting
|
|
82
94
|
gavel work renew <W-12> keep your claim through a long quiet stretch
|
|
83
95
|
gavel work release <W-12> [--why <words>] let it go: back to Queued, the why in its history
|
|
@@ -190,6 +202,7 @@ const USAGE = `gavel: Gavel from the command line (https://getgavel.ai/agents)
|
|
|
190
202
|
gavel workspace-settings its name, triage person, depth and untrusted areas
|
|
191
203
|
gavel edit-workspace [--name <name>] [--triage-person <person id>|none] [--sub-agent-depth <n>]
|
|
192
204
|
[--never-trusted <area id,area id>|none] [--coordinator <agent id>|none]
|
|
205
|
+
[--first-in-line <agent id,agent id>|none] [--adding-shares true|false]
|
|
193
206
|
Admins and Owners; only what you give changes
|
|
194
207
|
gavel workspace-people everyone, with their role
|
|
195
208
|
gavel set-role <person id> --role owner|admin|member
|
|
@@ -239,7 +252,8 @@ when its repository asks in more than one).`;
|
|
|
239
252
|
// What each command takes besides its words, as its usage above names them: anything else is
|
|
240
253
|
// refused with that usage, never dropped.
|
|
241
254
|
const FLAGS = {
|
|
242
|
-
pair: ["harness", "machine"],
|
|
255
|
+
pair: ["harness", "machine", "hook"],
|
|
256
|
+
waiting: ["hook", "server", "file"],
|
|
243
257
|
bridge: ["harness", "server"],
|
|
244
258
|
"actions-env": [],
|
|
245
259
|
search: ["query", "area", "status", "limit"],
|
|
@@ -255,6 +269,7 @@ const FLAGS = {
|
|
|
255
269
|
act: ["note"],
|
|
256
270
|
inbox: [],
|
|
257
271
|
status: ["text", "description", "folders", "state", "issue", "waiting-on", "item"],
|
|
272
|
+
"claim-folders": ["mode", "item"],
|
|
258
273
|
work: ["title", "status", "description", "worker", "reporter", "waiting-on", "decision", "code", "label", "open", "waits-on", "area", "why"],
|
|
259
274
|
message: ["to-agent", "to-person", "conversation", "text", "decision"],
|
|
260
275
|
conversations: [],
|
|
@@ -314,7 +329,7 @@ const FLAGS = {
|
|
|
314
329
|
"nudge-mine": [],
|
|
315
330
|
reassign: ["to-person", "to-agent", "note"],
|
|
316
331
|
"workspace-settings": [],
|
|
317
|
-
"edit-workspace": ["name", "triage-person", "sub-agent-depth", "never-trusted", "coordinator"],
|
|
332
|
+
"edit-workspace": ["name", "triage-person", "sub-agent-depth", "never-trusted", "coordinator", "first-in-line", "adding-shares"],
|
|
318
333
|
"workspace-people": [],
|
|
319
334
|
"set-role": ["role"],
|
|
320
335
|
invitations: [],
|
|
@@ -746,6 +761,8 @@ function personRequest(command, p, f, options) {
|
|
|
746
761
|
subAgentMaxDepth: depth === undefined ? undefined : Number(depth),
|
|
747
762
|
neverTrustableAreaIds: untrusted === undefined ? undefined : untrusted === "none" ? [] : list(untrusted),
|
|
748
763
|
coordinatorAgentId: orNone("coordinator"),
|
|
764
|
+
firstInLineAgentIds: f["first-in-line"] === undefined ? undefined : f["first-in-line"] === "none" ? [] : list(f["first-in-line"]),
|
|
765
|
+
addingShares: yesNo("adding-shares"),
|
|
749
766
|
});
|
|
750
767
|
if (!Object.keys(body).length)
|
|
751
768
|
throw new UsageError("edit-workspace needs something to change");
|
|
@@ -953,7 +970,18 @@ export function toRequest(argv) {
|
|
|
953
970
|
const harness = f.harness;
|
|
954
971
|
if (harness !== undefined && !HARNESSES.includes(harness))
|
|
955
972
|
throw new UsageError(`--harness is one of ${HARNESSES.join(", ")}`);
|
|
956
|
-
|
|
973
|
+
if (f.hook !== undefined && f.hook !== "true" && f.hook !== "false")
|
|
974
|
+
throw new UsageError("--hook is true or false");
|
|
975
|
+
return { pair: { code: need(p.join(""), "the pairing code (like 4F7K-9QX2)"), harness, machine: f.machine, hook: f.hook === "true" } };
|
|
976
|
+
}
|
|
977
|
+
case "waiting": {
|
|
978
|
+
upTo(0);
|
|
979
|
+
const hook = f.hook;
|
|
980
|
+
if (hook !== undefined && !HOOK_HARNESSES.includes(hook))
|
|
981
|
+
throw new UsageError(`--hook is one of ${HOOK_HARNESSES.join(", ")}`);
|
|
982
|
+
if ((f.server === undefined) !== (f.file === undefined))
|
|
983
|
+
throw new UsageError("--server and --file go together: the entry pairing wrote, and the file it's in");
|
|
984
|
+
return { waiting: { hook, server: f.server, file: f.file } };
|
|
957
985
|
}
|
|
958
986
|
case "bridge": {
|
|
959
987
|
upTo(1);
|
|
@@ -1029,6 +1057,13 @@ export function toRequest(argv) {
|
|
|
1029
1057
|
const description = f.description === undefined ? undefined : readText(f.description);
|
|
1030
1058
|
return { method: "PATCH", path: "/v1/agents/me/status", body: compact({ text: readText(said) || null, description, folders, state: f.state, linearIssue: f.issue, waitingOn: f["waiting-on"], item: f.item }) };
|
|
1031
1059
|
}
|
|
1060
|
+
case "claim-folders": {
|
|
1061
|
+
if (!p.length)
|
|
1062
|
+
throw new UsageError("missing the folders: each its repo's name, then the path (gavel/server/src/features/org)");
|
|
1063
|
+
if (f.mode !== undefined && f.mode !== "adding" && f.mode !== "alone")
|
|
1064
|
+
throw new UsageError("--mode is adding or alone");
|
|
1065
|
+
return { method: "POST", path: "/v1/agents/me/folders", body: compact({ folders: p, mode: f.mode, item: f.item }) };
|
|
1066
|
+
}
|
|
1032
1067
|
case "work":
|
|
1033
1068
|
return workRequest(p, f, lists.code ?? [], lists.label ?? []);
|
|
1034
1069
|
case "message": {
|
|
@@ -1449,9 +1484,59 @@ async function pair(input) {
|
|
|
1449
1484
|
}
|
|
1450
1485
|
if (body.legacy && body.legacy.server !== body.setup.server)
|
|
1451
1486
|
done.push(...(await dropLegacy(body.setup, body.legacy, { ...opts, apiUrl: base })));
|
|
1487
|
+
const setup = body.setup;
|
|
1488
|
+
if (input.hook) {
|
|
1489
|
+
const scope = setup.harness === "claude-code" ? (setup.ifExists.split(" ").pop() ?? null) : null;
|
|
1490
|
+
done.push(installHook({ harness: setup.harness, server: setup.server, file: setup.harness === "claude-code" ? null : setup.file, scope }, { ...opts, version: cliVersion() }));
|
|
1491
|
+
}
|
|
1492
|
+
else if (setup.harness === "cursor" || setup.harness === "claude-code") {
|
|
1493
|
+
done.push("Gavel's end-of-turn hook isn't on: pair with --hook true to have your turn carry on while something waits for you in Gavel");
|
|
1494
|
+
}
|
|
1452
1495
|
console.log(JSON.stringify({ paired: true, kind: body.kind, agent: body.agent, harness, done, then: body.then }, null, 2));
|
|
1453
1496
|
return 0;
|
|
1454
1497
|
}
|
|
1498
|
+
// ---------- gavel waiting ----------
|
|
1499
|
+
// What the hook's software wrote on stdin, if anything, without waiting on a stdin that stays open.
|
|
1500
|
+
async function hookInput() {
|
|
1501
|
+
if (process.stdin.isTTY)
|
|
1502
|
+
return null;
|
|
1503
|
+
const read = (async () => {
|
|
1504
|
+
let text = "";
|
|
1505
|
+
for await (const chunk of process.stdin)
|
|
1506
|
+
text += String(chunk);
|
|
1507
|
+
return text;
|
|
1508
|
+
})();
|
|
1509
|
+
const text = await Promise.race([read, new Promise((resolve) => setTimeout(() => resolve(""), 2_000).unref())]).catch(() => "");
|
|
1510
|
+
try {
|
|
1511
|
+
return text.trim() ? JSON.parse(text) : null;
|
|
1512
|
+
}
|
|
1513
|
+
catch {
|
|
1514
|
+
return null;
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
// What's waiting for the agent, or nothing; as a hook, that software's answer. The token is
|
|
1518
|
+
// GAVEL_TOKEN, else the entry pairing wrote (--server, --file), never printed. Always exits 0: a
|
|
1519
|
+
// failure lets the turn end.
|
|
1520
|
+
async function waiting(input, as) {
|
|
1521
|
+
const given = input.hook ? await hookInput() : null;
|
|
1522
|
+
const entry = !process.env.GAVEL_TOKEN && input.server && input.file ? setupFrom(input.file, input.server, process.cwd(), homedir()) : null;
|
|
1523
|
+
const token = process.env.GAVEL_TOKEN ?? entry?.token ?? null;
|
|
1524
|
+
let line = null;
|
|
1525
|
+
try {
|
|
1526
|
+
const api = (process.env.GAVEL_API_URL ?? (entry ? apiOf(entry.url) : "https://api.getgavel.ai")).replace(/\/+$/, "");
|
|
1527
|
+
line = token ? await waitingLine(api, token, agentHeader(as)) : null;
|
|
1528
|
+
}
|
|
1529
|
+
catch {
|
|
1530
|
+
line = null;
|
|
1531
|
+
}
|
|
1532
|
+
if (input.hook === "cursor")
|
|
1533
|
+
console.log(JSON.stringify(cursorAnswer(given, line)));
|
|
1534
|
+
else if (input.hook === "claude-code")
|
|
1535
|
+
console.log(JSON.stringify(claudeAnswer(given, line)));
|
|
1536
|
+
else if (line)
|
|
1537
|
+
console.log(line);
|
|
1538
|
+
return 0;
|
|
1539
|
+
}
|
|
1455
1540
|
// ---------- gavel upload ----------
|
|
1456
1541
|
const TYPES = {
|
|
1457
1542
|
".png": "image/png",
|
|
@@ -1676,6 +1761,8 @@ async function main(argvIn) {
|
|
|
1676
1761
|
}
|
|
1677
1762
|
if ("pair" in request)
|
|
1678
1763
|
return pair(request.pair);
|
|
1764
|
+
if ("waiting" in request)
|
|
1765
|
+
return waiting(request.waiting, as);
|
|
1679
1766
|
if ("bridge" in request)
|
|
1680
1767
|
return runBridge(request.bridge, { cwd: process.cwd(), home: homedir(), env: process.env, as });
|
|
1681
1768
|
if ("actionsEnv" in request)
|
|
@@ -1719,9 +1806,9 @@ async function main(argvIn) {
|
|
|
1719
1806
|
console.log(out);
|
|
1720
1807
|
// What's waiting for you (the server's own words), on stderr so stdout stays the answer's JSON. A
|
|
1721
1808
|
// failed call's stderr stays its error alone.
|
|
1722
|
-
const
|
|
1723
|
-
if (
|
|
1724
|
-
console.error(
|
|
1809
|
+
const waitingNow = res.headers.get("x-gavel-waiting");
|
|
1810
|
+
if (waitingNow)
|
|
1811
|
+
console.error(waitingNow);
|
|
1725
1812
|
return 0;
|
|
1726
1813
|
}
|
|
1727
1814
|
main(process.argv.slice(2)).then((code) => process.exit(code), (err) => {
|
package/dist/hook.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, realpathSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
// Gavel's end-of-turn hook (D-1024): when an agent's turn ends with something waiting for it in
|
|
7
|
+
// Gavel, the hook asks its software to carry on, with Gavel's own counts and tool names alone, at most
|
|
8
|
+
// three times a turn; anything going wrong lets the turn end as it would have. `gavel waiting --hook`
|
|
9
|
+
// is what the hook runs; `gavel pair --hook true` writes it.
|
|
10
|
+
export const HOOK_HARNESSES = ["cursor", "claude-code"];
|
|
11
|
+
export const HOOK_TRIES = 3;
|
|
12
|
+
const HOOK_SECONDS = 15;
|
|
13
|
+
const OPENING = "Gavel: something is waiting for you.";
|
|
14
|
+
const FETCH_MS = 5_000;
|
|
15
|
+
// This command's own version, so a hook runs the release that wrote it, never whatever npm has next.
|
|
16
|
+
export function cliVersion() {
|
|
17
|
+
const file = join(dirname(fileURLToPath(import.meta.url)), "..", "package.json");
|
|
18
|
+
return JSON.parse(readFileSync(file, "utf8")).version;
|
|
19
|
+
}
|
|
20
|
+
const expand = (file, home) => (file.startsWith("~/") ? join(home, file.slice(2)) : file);
|
|
21
|
+
function realpath(path) {
|
|
22
|
+
try {
|
|
23
|
+
return realpathSync(path);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return path;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// The named server's entry where pairing wrote it (Cursor's mcp.json, Claude Code's .claude.json: this
|
|
30
|
+
// folder's own first, then the user's), and only that entry. Nothing of the file is ever printed: a
|
|
31
|
+
// file that's missing, isn't JSON or lacks the entry gives null.
|
|
32
|
+
export function setupFrom(file, server, cwd, home) {
|
|
33
|
+
try {
|
|
34
|
+
const json = JSON.parse(readFileSync(expand(file, home), "utf8"));
|
|
35
|
+
const own = (json.projects?.[cwd] ?? json.projects?.[realpath(cwd)])?.mcpServers?.[server];
|
|
36
|
+
const entry = own ?? json.mcpServers?.[server];
|
|
37
|
+
const auth = entry?.headers?.Authorization;
|
|
38
|
+
const token = typeof auth === "string" ? (/^Bearer (gvl_\S+)$/.exec(auth.trim())?.[1] ?? null) : null;
|
|
39
|
+
return token && typeof entry?.url === "string" ? { token, url: entry.url } : null;
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// Gavel's API from the server's own address: mcp.<host> answers as api.<host>, a local one as itself.
|
|
46
|
+
export function apiOf(url) {
|
|
47
|
+
const parsed = new URL(url);
|
|
48
|
+
if (parsed.hostname.startsWith("mcp."))
|
|
49
|
+
parsed.hostname = `api.${parsed.hostname.slice(4)}`;
|
|
50
|
+
return parsed.origin;
|
|
51
|
+
}
|
|
52
|
+
// The line the server gives with any agent call that asks (X-Gavel-Waiting), or null: nothing waits,
|
|
53
|
+
// or anything went wrong.
|
|
54
|
+
export async function waitingLine(apiUrl, token, agent = {}) {
|
|
55
|
+
try {
|
|
56
|
+
const res = await fetch(`${apiUrl}/v1/me`, {
|
|
57
|
+
headers: { authorization: `Bearer ${token}`, accept: "application/json", "x-gavel-waiting": "1", ...agent },
|
|
58
|
+
signal: AbortSignal.timeout(FETCH_MS),
|
|
59
|
+
});
|
|
60
|
+
void res.body?.cancel();
|
|
61
|
+
return res.ok ? res.headers.get("x-gavel-waiting") : null;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// Cursor's stop hook: carry on with the line when the turn ended on its own (never after its person
|
|
68
|
+
// stopped it); its loop_limit holds it to three.
|
|
69
|
+
export function cursorAnswer(input, line) {
|
|
70
|
+
const { status, loop_count: loops } = (input ?? {});
|
|
71
|
+
if (!line || (status !== undefined && status !== "completed") || (typeof loops === "number" && loops >= HOOK_TRIES))
|
|
72
|
+
return {};
|
|
73
|
+
return { followup_message: `${OPENING} ${line}` };
|
|
74
|
+
}
|
|
75
|
+
// Claude Code's Stop hook: block the stop with the line, at most three times in a row a session (a
|
|
76
|
+
// count in the temp folder, by the session id's hash, cleared once nothing waits).
|
|
77
|
+
export function claudeAnswer(input, line, dir = tmpdir()) {
|
|
78
|
+
const session = input?.session_id;
|
|
79
|
+
const count = join(dir, `gavel-stop-${createHash("sha256").update(typeof session === "string" ? session : "").digest("hex").slice(0, 32)}`);
|
|
80
|
+
try {
|
|
81
|
+
if (!line) {
|
|
82
|
+
rmSync(count, { force: true });
|
|
83
|
+
return {};
|
|
84
|
+
}
|
|
85
|
+
const sofar = existsSync(count) ? Number(readFileSync(count, "utf8")) || 0 : 0;
|
|
86
|
+
if (sofar >= HOOK_TRIES)
|
|
87
|
+
return {};
|
|
88
|
+
writeFileSync(count, String(sofar + 1));
|
|
89
|
+
return { decision: "block", reason: `${OPENING} ${line}` };
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return {};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// A file written whole, through a temporary file and a rename, so a reader never sees half of it.
|
|
96
|
+
function writeWhole(path, text) {
|
|
97
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
98
|
+
const temp = `${path}.gavel-${process.pid}.tmp`;
|
|
99
|
+
writeFileSync(temp, text);
|
|
100
|
+
renameSync(temp, path);
|
|
101
|
+
}
|
|
102
|
+
const isOurs = (command) => typeof command === "string" && /@getgavel\/cli(@\S+)? waiting --hook /.test(command);
|
|
103
|
+
function parsed(existing, name) {
|
|
104
|
+
if (!existing?.trim())
|
|
105
|
+
return {};
|
|
106
|
+
try {
|
|
107
|
+
return JSON.parse(existing);
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
throw new Error(`${name} isn't plain JSON, so Gavel left its hook out: add it by hand, or fix the file and pair again`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// Cursor's hooks.json with Gavel's stop hook in: every other hook kept, an earlier one of Gavel's replaced.
|
|
114
|
+
export function withCursorHook(existing, command, name = "hooks.json") {
|
|
115
|
+
const json = parsed(existing, name);
|
|
116
|
+
const stop = (json.hooks?.stop ?? []).filter((h) => !isOurs(h.command));
|
|
117
|
+
return `${JSON.stringify({ ...json, version: json.version ?? 1, hooks: { ...(json.hooks ?? {}), stop: [...stop, { command, loop_limit: HOOK_TRIES, timeout: HOOK_SECONDS }] } }, null, 2)}\n`;
|
|
118
|
+
}
|
|
119
|
+
// Claude Code's settings with Gavel's Stop hook in: everything else kept, an earlier one of Gavel's replaced.
|
|
120
|
+
export function withClaudeHook(existing, command, name = "settings.json") {
|
|
121
|
+
const json = parsed(existing, name);
|
|
122
|
+
const groups = (json.hooks?.Stop ?? []).map((g) => ({ ...g, hooks: (g.hooks ?? []).filter((h) => !isOurs(h.command)) })).filter((g) => g.hooks.length);
|
|
123
|
+
return `${JSON.stringify({ ...json, hooks: { ...(json.hooks ?? {}), Stop: [...groups, { hooks: [{ type: "command", command, timeout: HOOK_SECONDS }] }] } }, null, 2)}\n`;
|
|
124
|
+
}
|
|
125
|
+
// Writes Gavel's hook beside the setup pairing wrote, in the same scope; says what it did, or why not.
|
|
126
|
+
export function installHook(place, opts) {
|
|
127
|
+
const at = (f) => {
|
|
128
|
+
const expanded = expand(f, opts.home);
|
|
129
|
+
return expanded.startsWith("/") ? expanded : join(opts.cwd, expanded);
|
|
130
|
+
};
|
|
131
|
+
const npx = `npx -y @getgavel/cli@${opts.version} waiting`;
|
|
132
|
+
try {
|
|
133
|
+
if (place.harness === "cursor" && place.file) {
|
|
134
|
+
const file = join(dirname(place.file), "hooks.json");
|
|
135
|
+
const path = at(file);
|
|
136
|
+
writeWhole(path, withCursorHook(existsSync(path) ? readFileSync(path, "utf8") : null, `${npx} --hook cursor --server ${place.server} --file ${place.file}`, file));
|
|
137
|
+
return `wrote Gavel's end-of-turn hook into ${file} (stop, at most ${HOOK_TRIES} a turn)`;
|
|
138
|
+
}
|
|
139
|
+
if (place.harness === "claude-code") {
|
|
140
|
+
const file = place.scope === "user" ? "~/.claude/settings.json" : place.scope === "project" ? ".claude/settings.json" : ".claude/settings.local.json";
|
|
141
|
+
const path = at(file);
|
|
142
|
+
writeWhole(path, withClaudeHook(existsSync(path) ? readFileSync(path, "utf8") : null, `${npx} --hook claude-code --server ${place.server} --file ~/.claude.json`, file));
|
|
143
|
+
return `wrote Gavel's end-of-turn hook into ${file} (Stop, at most ${HOOK_TRIES} in a row)`;
|
|
144
|
+
}
|
|
145
|
+
return `left out Gavel's end-of-turn hook: ${place.harness === "codex" ? "Codex" : "this software"} has none`;
|
|
146
|
+
}
|
|
147
|
+
catch (err) {
|
|
148
|
+
return `left out Gavel's end-of-turn hook: ${err instanceof Error ? err.message : String(err)}`;
|
|
149
|
+
}
|
|
150
|
+
}
|
package/package.json
CHANGED