@getgavel/cli 0.1.15 → 0.1.17
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/.claude-plugin/plugin.json +9 -0
- package/README.md +1 -0
- package/dist/bridge/background.js +397 -0
- package/dist/bridge/bridge.js +324 -247
- package/dist/bridge/claude-code.js +0 -96
- package/dist/bridge/claude-live.js +625 -0
- package/dist/bridge/codex-app.js +135 -0
- package/dist/bridge/codex.js +440 -77
- package/dist/bridge/harness.js +1 -0
- package/dist/bridge/service.js +134 -0
- package/dist/bridge/setup.js +27 -18
- package/dist/bridge/websocket.js +166 -0
- package/dist/gavel.js +251 -42
- package/dist/hook.js +67 -7
- package/dist/output.js +14 -0
- package/hooks/hooks.json +30 -0
- package/package.json +5 -2
- package/skills/gavel/SKILL.md +30 -0
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { execFile, spawn } from "node:child_process";
|
|
2
1
|
import { existsSync, readdirSync, realpathSync, statSync } from "node:fs";
|
|
3
2
|
import { join } from "node:path";
|
|
4
|
-
import { promisify } from "node:util";
|
|
5
3
|
import { cut } from "./words.js";
|
|
6
4
|
// Claude Code keeps each session as <its folder>/projects/<project>/<session id>.jsonl, the project
|
|
7
5
|
// its working folder's path with every character but a letter or a digit as a dash.
|
|
@@ -90,97 +88,3 @@ export function claudeWords(jsonl) {
|
|
|
90
88
|
}
|
|
91
89
|
return out;
|
|
92
90
|
}
|
|
93
|
-
// Whether the session is open, in a terminal or in the background: Claude Code's own list of live
|
|
94
|
-
// sessions, its supported way to read them from outside. An older Claude Code without it counts as
|
|
95
|
-
// closed.
|
|
96
|
-
export async function claudeOpen(session, cwd) {
|
|
97
|
-
try {
|
|
98
|
-
const { stdout } = await promisify(execFile)("claude", ["agents", "--json"], { cwd, timeout: 30_000 });
|
|
99
|
-
const live = JSON.parse(stdout);
|
|
100
|
-
return Array.isArray(live) && live.some((s) => s?.sessionId === session);
|
|
101
|
-
}
|
|
102
|
-
catch {
|
|
103
|
-
return false;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
const firstLine = (text) => text.split("\n").map((l) => l.trim()).find(Boolean) ?? "";
|
|
107
|
-
// The person's words as a turn of their own in the session (or in a new one when there's none),
|
|
108
|
-
// with nobody there to answer a permission prompt: whatever would ask is refused, and the session's
|
|
109
|
-
// own permissions decide the rest (D-905, Gavel Master's call). The words go on stdin, never on the
|
|
110
|
-
// command line, where other processes see them.
|
|
111
|
-
export function claudeResume(session, text, cwd) {
|
|
112
|
-
return new Promise((resolve) => {
|
|
113
|
-
const args = ["-p", ...(session ? ["--resume", session] : []), "--permission-prompts", "none", "--output-format", "json"];
|
|
114
|
-
const child = spawn("claude", args, { cwd, stdio: ["pipe", "pipe", "pipe"] });
|
|
115
|
-
let out = "";
|
|
116
|
-
let err = "";
|
|
117
|
-
child.stdout.on("data", (d) => (out += d));
|
|
118
|
-
child.stderr.on("data", (d) => (err += d));
|
|
119
|
-
child.on("error", (e) => resolve({ ok: false, reason: e.code === "ENOENT" ? "Claude Code's claude command isn't on the laptop's PATH where the bridge runs." : `Claude Code didn't start: ${e.message}` }));
|
|
120
|
-
child.on("close", (code) => {
|
|
121
|
-
let result = null;
|
|
122
|
-
try {
|
|
123
|
-
result = JSON.parse(out);
|
|
124
|
-
}
|
|
125
|
-
catch {
|
|
126
|
-
// no answer: the reason is on stderr
|
|
127
|
-
}
|
|
128
|
-
if (code === 0 && result && !result.is_error) {
|
|
129
|
-
const refused = [...new Set((result.permission_denials ?? []).map((d) => d?.tool_name).filter((n) => typeof n === "string"))];
|
|
130
|
-
return resolve({ ok: true, refused });
|
|
131
|
-
}
|
|
132
|
-
const said = (typeof result?.result === "string" ? firstLine(result.result) : "") || firstLine(err);
|
|
133
|
-
const reason = /unknown option.*permission-prompts/i.test(err)
|
|
134
|
-
? "The laptop's Claude Code is too old for the bridge: run claude update there."
|
|
135
|
-
: said
|
|
136
|
-
? `Claude Code said: ${said}`
|
|
137
|
-
: `Claude Code stopped without answering (code ${code}).`;
|
|
138
|
-
resolve({ ok: false, reason: reason.slice(0, 300) });
|
|
139
|
-
});
|
|
140
|
-
child.stdin.end(text);
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
// The wake hook, while the bridge is on: Claude Code runs it in the background as a session starts
|
|
144
|
-
// and after each turn, and it wakes the session, even idle, with what the person writes in Gavel
|
|
145
|
-
// (asyncRewake: exit code 2, its stderr shown to Claude). In the project's local settings, never a
|
|
146
|
-
// committed file.
|
|
147
|
-
export const claudeSettingsFile = (cwd) => join(cwd, ".claude", "settings.local.json");
|
|
148
|
-
const WAKE_EVENTS = ["SessionStart", "Stop"];
|
|
149
|
-
const WAKE_SECONDS = 24 * 60 * 60;
|
|
150
|
-
const isWake = (hook) => typeof hook.command === "string" && /\bbridge wake( |$)/.test(hook.command);
|
|
151
|
-
// The settings with the wake hook in (command) or out (null), everything else kept; the same text
|
|
152
|
-
// when nothing changes.
|
|
153
|
-
export function withWakeHook(existing, command) {
|
|
154
|
-
let settings = {};
|
|
155
|
-
if (existing?.trim()) {
|
|
156
|
-
try {
|
|
157
|
-
settings = JSON.parse(existing);
|
|
158
|
-
}
|
|
159
|
-
catch {
|
|
160
|
-
throw new Error(".claude/settings.local.json isn't plain JSON, so the bridge can't put its wake hook in it");
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
const hooks = { ...(settings.hooks ?? {}) };
|
|
164
|
-
let changed = false;
|
|
165
|
-
for (const event of Object.keys(hooks)) {
|
|
166
|
-
const groups = (hooks[event] ?? []).map((g) => ({ ...g, hooks: (g.hooks ?? []).filter((h) => !isWake(h)) }));
|
|
167
|
-
const kept = groups.filter((g) => g.hooks.length);
|
|
168
|
-
if (JSON.stringify(kept) !== JSON.stringify(hooks[event]))
|
|
169
|
-
changed = true;
|
|
170
|
-
if (kept.length)
|
|
171
|
-
hooks[event] = kept;
|
|
172
|
-
else
|
|
173
|
-
delete hooks[event];
|
|
174
|
-
}
|
|
175
|
-
if (command) {
|
|
176
|
-
for (const event of WAKE_EVENTS)
|
|
177
|
-
hooks[event] = [...(hooks[event] ?? []), { hooks: [{ type: "command", command, asyncRewake: true, timeout: WAKE_SECONDS }] }];
|
|
178
|
-
changed = true;
|
|
179
|
-
}
|
|
180
|
-
if (!changed)
|
|
181
|
-
return existing;
|
|
182
|
-
const { hooks: _old, ...rest } = settings;
|
|
183
|
-
const next = Object.keys(hooks).length ? { ...rest, hooks } : rest;
|
|
184
|
-
const text = `${JSON.stringify(next, null, 2)}\n`;
|
|
185
|
-
return text === existing ? existing : text;
|
|
186
|
-
}
|