@getgavel/cli 0.1.21 → 0.1.22
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 +1 -1
- package/dist/bridge/claude-live.js +89 -16
- package/dist/gavel.js +10 -1
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gavel",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.22",
|
|
4
4
|
"displayName": "Gavel",
|
|
5
5
|
"description": "Gavel in Claude Code: your person's chat in Gavel reaches your sessions at once (Gavel's bridge, started with each session), the end-of-turn reminder of what's waiting for you, and how to work with Gavel.",
|
|
6
6
|
"author": { "name": "Gavel", "url": "https://getgavel.ai" },
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { execFile, execFileSync, spawn } from "node:child_process";
|
|
2
2
|
import { createHash, randomBytes } from "node:crypto";
|
|
3
|
-
import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, unlinkSync, watch, writeFileSync } from "node:fs";
|
|
3
|
+
import { closeSync, copyFileSync, existsSync, fstatSync, mkdirSync, openSync, readdirSync, readFileSync, readSync, renameSync, rmSync, statSync, unlinkSync, watch, writeFileSync } from "node:fs";
|
|
4
4
|
import { createServer } from "node:http";
|
|
5
5
|
import { connect } from "node:net";
|
|
6
6
|
import { tmpdir } from "node:os";
|
|
@@ -234,6 +234,69 @@ export function sessionWords(jsonl, fromGavel, prompts = new Map()) {
|
|
|
234
234
|
}
|
|
235
235
|
return words.sort((a, b) => Date.parse(a.at) - Date.parse(b.at));
|
|
236
236
|
}
|
|
237
|
+
const sizeOf = (file) => {
|
|
238
|
+
try {
|
|
239
|
+
return statSync(file).size;
|
|
240
|
+
}
|
|
241
|
+
catch {
|
|
242
|
+
return 0;
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
const READ_MAX = 8 * 1024 * 1024;
|
|
246
|
+
// A file from an offset to its end (READ_MAX at most), without reading the whole transcript; null
|
|
247
|
+
// when it can't be read.
|
|
248
|
+
function readFrom(file, offset, max = READ_MAX) {
|
|
249
|
+
try {
|
|
250
|
+
const fd = openSync(file, "r");
|
|
251
|
+
try {
|
|
252
|
+
const length = Math.max(0, Math.min(fstatSync(fd).size - offset, max));
|
|
253
|
+
const buffer = Buffer.alloc(length);
|
|
254
|
+
readSync(fd, buffer, 0, length, offset);
|
|
255
|
+
return buffer;
|
|
256
|
+
}
|
|
257
|
+
finally {
|
|
258
|
+
closeSync(fd);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
catch {
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
// The first and the last of an answer's words as its transcript line has them (JSON), each within
|
|
266
|
+
// one line of it: Claude Code joins a message's parts with a newline for the Stop hook, so neither
|
|
267
|
+
// spans two parts.
|
|
268
|
+
function wordsOf(last) {
|
|
269
|
+
const lines = last.split("\n");
|
|
270
|
+
const json = (text) => Array.from(JSON.stringify(text).slice(1, -1));
|
|
271
|
+
return [Buffer.from(json(lines[0]).slice(0, 59).join("")), Buffer.from(json(lines.at(-1)).slice(-59).join(""))];
|
|
272
|
+
}
|
|
273
|
+
// Where a turn's prompt line starts (Claude Code keeps the prompt's id in it) in the transcript's
|
|
274
|
+
// last 16 MB; null when it isn't there.
|
|
275
|
+
function promptLine(file, prompt) {
|
|
276
|
+
const from = Math.max(0, sizeOf(file) - 2 * READ_MAX);
|
|
277
|
+
const read = readFrom(file, from, 2 * READ_MAX);
|
|
278
|
+
const at = read?.lastIndexOf(`"promptId":${JSON.stringify(prompt)}`) ?? -1;
|
|
279
|
+
return read && at >= 0 ? from + read.lastIndexOf(10, at) + 1 : null;
|
|
280
|
+
}
|
|
281
|
+
// Whether a transcript read's whole lines hold the agent's line with these words (a line Claude Code
|
|
282
|
+
// is still writing doesn't count), and how far its whole lines go.
|
|
283
|
+
function agentLine(read, words) {
|
|
284
|
+
let start = 0;
|
|
285
|
+
for (let end = read.indexOf(10); end >= 0; start = end + 1, end = read.indexOf(10, start)) {
|
|
286
|
+
const raw = read.subarray(start, end);
|
|
287
|
+
if (!words.every((w) => raw.includes(w)))
|
|
288
|
+
continue;
|
|
289
|
+
try {
|
|
290
|
+
const line = JSON.parse(raw.toString("utf8"));
|
|
291
|
+
if (line.type === "assistant" && !line.isSidechain)
|
|
292
|
+
return { found: true, through: end + 1 };
|
|
293
|
+
}
|
|
294
|
+
catch {
|
|
295
|
+
// a line read from its middle
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return { found: false, through: start };
|
|
299
|
+
}
|
|
237
300
|
const PICTURES_HOURS = 1;
|
|
238
301
|
export class ClaudeCodeHarness {
|
|
239
302
|
ctx;
|
|
@@ -336,7 +399,7 @@ export class ClaudeCodeHarness {
|
|
|
336
399
|
session(id) {
|
|
337
400
|
let s = this.sessions.get(id);
|
|
338
401
|
if (!s) {
|
|
339
|
-
s = { id, inbox: null, transcript: null, open: true, state: "idle", seen: Date.now(), run: null, spoken: new Map() };
|
|
402
|
+
s = { id, inbox: null, transcript: null, open: true, hooked: false, state: "idle", seen: Date.now(), run: null, spoken: new Map() };
|
|
340
403
|
this.sessions.set(id, s);
|
|
341
404
|
}
|
|
342
405
|
return s;
|
|
@@ -352,6 +415,7 @@ export class ClaudeCodeHarness {
|
|
|
352
415
|
return;
|
|
353
416
|
const s = this.session(id);
|
|
354
417
|
s.seen = Date.now();
|
|
418
|
+
s.hooked = true;
|
|
355
419
|
if (e.inbox)
|
|
356
420
|
s.inbox = e.inbox;
|
|
357
421
|
if (typeof input.transcript_path === "string")
|
|
@@ -401,7 +465,7 @@ export class ClaudeCodeHarness {
|
|
|
401
465
|
s.run = null;
|
|
402
466
|
const last = typeof input.last_assistant_message === "string" ? input.last_assistant_message.trim() : "";
|
|
403
467
|
if (run)
|
|
404
|
-
void this.endOnceKept(s, run, event === "Stop" ? last : "");
|
|
468
|
+
void this.endOnceKept(s, run, event === "Stop" ? last : "", typeof input.prompt_id === "string" ? input.prompt_id : null);
|
|
405
469
|
break;
|
|
406
470
|
}
|
|
407
471
|
case "SessionEnd":
|
|
@@ -414,19 +478,28 @@ export class ClaudeCodeHarness {
|
|
|
414
478
|
this.soon();
|
|
415
479
|
this.stateNow();
|
|
416
480
|
}
|
|
417
|
-
// A turn's end goes once the transcript has its last words (Claude Code writes the
|
|
418
|
-
// after the Stop hook), so the core's push before
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
481
|
+
// A turn's end goes once the transcript has its last words in a whole line (Claude Code writes the
|
|
482
|
+
// file a moment after the Stop hook, and a line sometimes in two writes), so the core's push before
|
|
483
|
+
// it keeps them (Reach's rule for `end`). Looked for after the turn's own prompt line, so an earlier
|
|
484
|
+
// answer in the same words isn't taken for it; without one, from a little before where the file
|
|
485
|
+
// ended at the Stop hook: Claude Code writes large lines after the last words too (a session's
|
|
486
|
+
// first turn adds a prompt snapshot of tens of KB).
|
|
487
|
+
async endOnceKept(s, run, last, prompt) {
|
|
488
|
+
const file = s.transcript;
|
|
489
|
+
if (last && file) {
|
|
490
|
+
const deadline = Date.now() + 3_000;
|
|
491
|
+
const words = wordsOf(last);
|
|
492
|
+
let from = (prompt ? promptLine(file, prompt) : null) ?? Math.max(0, sizeOf(file) - 8 * last.length - 1_048_576);
|
|
493
|
+
while (Date.now() < deadline) {
|
|
494
|
+
const read = readFrom(file, from);
|
|
495
|
+
if (!read)
|
|
424
496
|
break;
|
|
497
|
+
const scan = agentLine(read, words);
|
|
498
|
+
if (scan.found)
|
|
499
|
+
break;
|
|
500
|
+
from += scan.through || (read.length >= READ_MAX ? read.length : 0);
|
|
501
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
425
502
|
}
|
|
426
|
-
catch {
|
|
427
|
-
break;
|
|
428
|
-
}
|
|
429
|
-
await new Promise((r) => setTimeout(r, 100));
|
|
430
503
|
}
|
|
431
504
|
this.listener?.changed();
|
|
432
505
|
this.listener?.live({ event: "end", runId: run });
|
|
@@ -546,10 +619,10 @@ export class ClaudeCodeHarness {
|
|
|
546
619
|
// missed): the newest one's inbox at its usual address, without the session's key.
|
|
547
620
|
async liveSession() {
|
|
548
621
|
try {
|
|
549
|
-
const { stdout } = await promisify(execFile)("claude", ["agents", "--json"], { cwd: this.ctx.cwd, timeout: 15_000 });
|
|
622
|
+
const { stdout } = await promisify(execFile)("claude", ["agents", "--json"], { cwd: this.ctx.cwd, env: this.ctx.env, timeout: 15_000 });
|
|
550
623
|
const live = JSON.parse(stdout).filter((s) => s.cwd === this.ctx.cwd && typeof s.sessionId === "string" && typeof s.pid === "number");
|
|
551
624
|
for (const s of this.sessions.values())
|
|
552
|
-
if (s.open && !live.some((l) => l.sessionId === s.id)) {
|
|
625
|
+
if (s.open && !s.hooked && !live.some((l) => l.sessionId === s.id)) {
|
|
553
626
|
s.open = false;
|
|
554
627
|
s.inbox = null;
|
|
555
628
|
s.state = "closed";
|
package/dist/gavel.js
CHANGED
|
@@ -1803,6 +1803,13 @@ async function upload(token, as, { file, contentType }) {
|
|
|
1803
1803
|
console.log(jsonText(await finished.json()));
|
|
1804
1804
|
return 0;
|
|
1805
1805
|
}
|
|
1806
|
+
const THREAD_WAKES = {
|
|
1807
|
+
added: "you were added to it",
|
|
1808
|
+
mention: "a message names you",
|
|
1809
|
+
answer: "an answer to what you asked",
|
|
1810
|
+
note: "a note, it needs no answer",
|
|
1811
|
+
acknowledgment: "an acknowledgment",
|
|
1812
|
+
};
|
|
1806
1813
|
// One line per thing that should wake the chat: refs and Gavel's own words only, never a message's
|
|
1807
1814
|
// text, so the line itself is never someone else's instructions. A kind newer than this command
|
|
1808
1815
|
// still wakes it, with a line that sends it to the inbox.
|
|
@@ -1838,6 +1845,8 @@ export function wakeLine(item) {
|
|
|
1838
1845
|
: `GAVEL_WAKE task ${item.ref} ${item.type ?? "changed"} by someone else: gavel work show ${item.ref}`;
|
|
1839
1846
|
case "replaced":
|
|
1840
1847
|
return `GAVEL_WAKE replaced ${item.ref}: it no longer holds, ${item.type ?? "a newer decision"} replaces it: gavel get ${item.type ?? item.ref}, and follow that one`;
|
|
1848
|
+
case "thread":
|
|
1849
|
+
return `GAVEL_WAKE thread ${item.ref}: ${THREAD_WAKES[item.type ?? ""] ?? "a message"}: read it with gavel thread ${item.ref}`;
|
|
1841
1850
|
default:
|
|
1842
1851
|
return "GAVEL_WAKE something new: see gavel inbox";
|
|
1843
1852
|
}
|
|
@@ -1853,7 +1862,7 @@ async function watch(token, as) {
|
|
|
1853
1862
|
for (;;) {
|
|
1854
1863
|
let res;
|
|
1855
1864
|
try {
|
|
1856
|
-
res = await fetch(`${base}/v1/inbox/watch?watch=${id}&timeout=50&kinds=health,claim,work,drift,task,replaced${start ? "&start=true" : ""}`, {
|
|
1865
|
+
res = await fetch(`${base}/v1/inbox/watch?watch=${id}&timeout=50&kinds=health,claim,work,drift,task,replaced,thread${start ? "&start=true" : ""}`, {
|
|
1857
1866
|
headers: { authorization: `Bearer ${token}`, accept: "application/json", ...agentHeader(as) },
|
|
1858
1867
|
});
|
|
1859
1868
|
}
|
package/package.json
CHANGED