@getgavel/cli 0.1.14 → 0.1.16
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 +6 -1
- package/dist/bridge/background.js +366 -0
- package/dist/bridge/bridge.js +6 -2
- package/dist/bridge/claude-live.js +551 -0
- package/dist/bridge/service.js +134 -0
- package/dist/gavel.js +277 -39
- package/dist/hook.js +150 -0
- package/dist/output.js +14 -0
- package/package.json +1 -1
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/dist/output.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// What the gavel command prints: JSON indented for a person at a terminal, compact for an agent that
|
|
2
|
+
// captures it, where indenting cost about 30% more tokens to read (W-117, as MCP's results, W-111).
|
|
3
|
+
export function jsonText(value, stream = process.stdout) {
|
|
4
|
+
return JSON.stringify(value, null, stream.isTTY ? 2 : undefined);
|
|
5
|
+
}
|
|
6
|
+
// An answer's text as JSON for that stream, or as it came when it isn't JSON.
|
|
7
|
+
export function shownText(text, stream = process.stdout) {
|
|
8
|
+
try {
|
|
9
|
+
return jsonText(JSON.parse(text), stream);
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return text;
|
|
13
|
+
}
|
|
14
|
+
}
|
package/package.json
CHANGED