@decocms/chat 1.8.1 → 1.8.3
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/cli.mjs +135 -95
- package/package.json +1 -1
package/cli.mjs
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
// cli/vibebook.ts
|
|
4
4
|
import { execFileSync, execSync, spawn } from "node:child_process";
|
|
5
5
|
import { createHash, randomBytes } from "node:crypto";
|
|
6
|
-
import { appendFileSync, chmodSync, closeSync, existsSync as existsSync2, fstatSync, mkdirSync, openSync, readdirSync as
|
|
6
|
+
import { appendFileSync, chmodSync, closeSync, existsSync as existsSync2, fstatSync, mkdirSync, openSync, readdirSync as readdirSync3, readFileSync as readFileSync2, readSync, realpathSync, renameSync, rmSync, statSync as statSync3, unlinkSync, writeFileSync } from "node:fs";
|
|
7
7
|
import { createServer } from "node:http";
|
|
8
8
|
import { homedir, hostname, tmpdir } from "node:os";
|
|
9
|
-
import { basename, delimiter, dirname, join as
|
|
9
|
+
import { basename, delimiter, dirname, join as join3 } from "node:path";
|
|
10
10
|
|
|
11
11
|
// cli/backfill.ts
|
|
12
12
|
import { createReadStream, existsSync, readdirSync, statSync } from "node:fs";
|
|
@@ -166,6 +166,31 @@ function menuOf(screen) {
|
|
|
166
166
|
return question ? { question, options } : null;
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
+
// cli/listeningRoom.ts
|
|
170
|
+
import { readdirSync as readdirSync2, readFileSync, statSync as statSync2 } from "node:fs";
|
|
171
|
+
import { join as join2 } from "node:path";
|
|
172
|
+
function listeningRoom(home, agent) {
|
|
173
|
+
const prefix = `listen-${agent}-`;
|
|
174
|
+
let files = [];
|
|
175
|
+
try {
|
|
176
|
+
files = readdirSync2(home).filter((f) => f.startsWith(prefix) && f.endsWith(".pid") && f !== `${prefix}all.pid`);
|
|
177
|
+
} catch {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const live = files.flatMap((f) => {
|
|
181
|
+
try {
|
|
182
|
+
const pid = Number(readFileSync(join2(home, f), "utf8"));
|
|
183
|
+
if (!(pid > 1))
|
|
184
|
+
return [];
|
|
185
|
+
process.kill(pid, 0);
|
|
186
|
+
return [{ room: f.slice(prefix.length, -4), at: statSync2(join2(home, f)).mtimeMs }];
|
|
187
|
+
} catch {
|
|
188
|
+
return [];
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
return live.sort((a, b) => b.at - a.at).find((x) => /^[a-z0-9]+$/.test(x.room))?.room;
|
|
192
|
+
}
|
|
193
|
+
|
|
169
194
|
// shared/agentKinds.ts
|
|
170
195
|
var AGENT_KINDS = ["claude", "codex", "agy", "opencode"];
|
|
171
196
|
function agentVersion(raw) {
|
|
@@ -173,15 +198,15 @@ function agentVersion(raw) {
|
|
|
173
198
|
}
|
|
174
199
|
|
|
175
200
|
// cli/vibebook.ts
|
|
176
|
-
var VERSION = "1.8.
|
|
201
|
+
var VERSION = "1.8.3";
|
|
177
202
|
var PKG = "@decocms/chat";
|
|
178
203
|
var OLD_PKG = "vibebook.dev";
|
|
179
204
|
for (const [k, v] of Object.entries(process.env))
|
|
180
205
|
if (k.startsWith("DECO_CHAT_") && v !== undefined)
|
|
181
206
|
process.env[`VIBEBOOK_${k.slice(10)}`] = v;
|
|
182
|
-
var HOME = process.env.VIBEBOOK_HOME || (existsSync2(
|
|
183
|
-
var SESSION =
|
|
184
|
-
var AGENTS =
|
|
207
|
+
var HOME = process.env.VIBEBOOK_HOME || (existsSync2(join3(homedir(), ".deco-chat")) ? join3(homedir(), ".deco-chat") : join3(homedir(), ".vibebook"));
|
|
208
|
+
var SESSION = join3(HOME, "session.json");
|
|
209
|
+
var AGENTS = join3(HOME, "agents.json");
|
|
185
210
|
var DEFAULT_ORIGIN = "https://chat.decocms.com";
|
|
186
211
|
var MOVED = { "https://vibebook.dev": DEFAULT_ORIGIN };
|
|
187
212
|
var jsonErrors = false;
|
|
@@ -196,7 +221,7 @@ var die = (msg, code = 1) => {
|
|
|
196
221
|
};
|
|
197
222
|
var readJson = (f, fallback) => {
|
|
198
223
|
try {
|
|
199
|
-
return JSON.parse(
|
|
224
|
+
return JSON.parse(readFileSync2(f, "utf8"));
|
|
200
225
|
} catch {
|
|
201
226
|
return fallback;
|
|
202
227
|
}
|
|
@@ -264,8 +289,8 @@ function repoRoot() {
|
|
|
264
289
|
}
|
|
265
290
|
var ROOM_FILES = [".deco-chat", ".vibebook"];
|
|
266
291
|
var linkedRoom = (root) => {
|
|
267
|
-
const f = ROOM_FILES.map((n) =>
|
|
268
|
-
return
|
|
292
|
+
const f = ROOM_FILES.map((n) => join3(root, n)).find((p) => existsSync2(p)) ?? join3(root, ".vibebook");
|
|
293
|
+
return readFileSync2(f, "utf8").match(/^room_id=([a-z0-9]+)/m)?.[1];
|
|
269
294
|
};
|
|
270
295
|
function roomHere(root) {
|
|
271
296
|
try {
|
|
@@ -275,7 +300,7 @@ function roomHere(root) {
|
|
|
275
300
|
} catch {}
|
|
276
301
|
for (const f of ["README.md", "AGENTS.md", "CLAUDE.md"]) {
|
|
277
302
|
try {
|
|
278
|
-
const m =
|
|
303
|
+
const m = readFileSync2(join3(root, f), "utf8").match(/(?:chat\.decocms\.com|vibebook\.dev)\/b\/([a-z0-9]{12})/);
|
|
279
304
|
if (m)
|
|
280
305
|
return m[1];
|
|
281
306
|
} catch {}
|
|
@@ -412,7 +437,7 @@ async function session() {
|
|
|
412
437
|
break;
|
|
413
438
|
} catch {
|
|
414
439
|
try {
|
|
415
|
-
if (Date.now() -
|
|
440
|
+
if (Date.now() - statSync3(lock).mtimeMs > 30000)
|
|
416
441
|
rmSync(lock, { recursive: true, force: true });
|
|
417
442
|
} catch {}
|
|
418
443
|
if (i > 100)
|
|
@@ -452,11 +477,11 @@ function transcriptTail(sid, max = 4 << 20) {
|
|
|
452
477
|
if (!/^[\w-]{8,80}$/.test(sid))
|
|
453
478
|
return;
|
|
454
479
|
try {
|
|
455
|
-
const root =
|
|
456
|
-
const dir =
|
|
480
|
+
const root = join3(homedir(), ".claude", "projects");
|
|
481
|
+
const dir = readdirSync3(root).find((d) => existsSync2(join3(root, d, `${sid}.jsonl`)));
|
|
457
482
|
if (!dir)
|
|
458
483
|
return;
|
|
459
|
-
const fd = openSync(
|
|
484
|
+
const fd = openSync(join3(root, dir, `${sid}.jsonl`), "r");
|
|
460
485
|
const size = fstatSync(fd).size;
|
|
461
486
|
const n = Math.min(size, max);
|
|
462
487
|
const buf = Buffer.alloc(n);
|
|
@@ -484,18 +509,18 @@ function sessionContext(sid) {
|
|
|
484
509
|
}
|
|
485
510
|
} catch {}
|
|
486
511
|
}
|
|
487
|
-
var USAGE =
|
|
512
|
+
var USAGE = join3(HOME, "usage");
|
|
488
513
|
function sessionUsage(sid) {
|
|
489
514
|
if (!/^[\w-]{8,80}$/.test(sid))
|
|
490
515
|
return;
|
|
491
516
|
try {
|
|
492
|
-
const root =
|
|
493
|
-
const dir =
|
|
517
|
+
const root = join3(homedir(), ".claude", "projects");
|
|
518
|
+
const dir = readdirSync3(root).find((d) => existsSync2(join3(root, d, `${sid}.jsonl`)));
|
|
494
519
|
if (!dir)
|
|
495
520
|
return;
|
|
496
|
-
const file =
|
|
521
|
+
const file = join3(USAGE, `${sid}.json`);
|
|
497
522
|
const st = readJson(file, { off: 0, total: 0, last: "" });
|
|
498
|
-
const fd = openSync(
|
|
523
|
+
const fd = openSync(join3(root, dir, `${sid}.jsonl`), "r");
|
|
499
524
|
const size = fstatSync(fd).size;
|
|
500
525
|
if (size < st.off)
|
|
501
526
|
Object.assign(st, { off: 0, total: 0, last: "" });
|
|
@@ -525,9 +550,9 @@ function sessionUsage(sid) {
|
|
|
525
550
|
return st.total;
|
|
526
551
|
} catch {}
|
|
527
552
|
}
|
|
528
|
-
var STATUS =
|
|
553
|
+
var STATUS = join3(HOME, "status");
|
|
529
554
|
function statusline() {
|
|
530
|
-
const input =
|
|
555
|
+
const input = readFileSync2(0);
|
|
531
556
|
try {
|
|
532
557
|
const j = JSON.parse(input.toString());
|
|
533
558
|
if (/^[\w-]{8,80}$/.test(j.session_id ?? "")) {
|
|
@@ -538,7 +563,7 @@ function statusline() {
|
|
|
538
563
|
limits[k] = { p: Math.round(l.used_percentage), r: Number(l.resets_at) || 0 };
|
|
539
564
|
}
|
|
540
565
|
mkdirSync(STATUS, { recursive: true });
|
|
541
|
-
writeFileSync(
|
|
566
|
+
writeFileSync(join3(STATUS, `${j.session_id}.json`), JSON.stringify({ window: Number(j.context_window?.context_window_size) || undefined, limits, at: Date.now() }));
|
|
542
567
|
}
|
|
543
568
|
} catch {}
|
|
544
569
|
const rest = process.argv.slice(process.argv.indexOf("statusline") + 1).join(" ").trim();
|
|
@@ -548,20 +573,20 @@ function statusline() {
|
|
|
548
573
|
var sessionStatus = (sid) => {
|
|
549
574
|
if (!/^[\w-]{8,80}$/.test(sid))
|
|
550
575
|
return;
|
|
551
|
-
const s = readJson(
|
|
576
|
+
const s = readJson(join3(STATUS, `${sid}.json`), null);
|
|
552
577
|
return s && Date.now() - s.at < 6 * 3600000 ? s : undefined;
|
|
553
578
|
};
|
|
554
|
-
var CODEX_SESSIONS =
|
|
579
|
+
var CODEX_SESSIONS = join3(homedir(), ".codex", "sessions");
|
|
555
580
|
var codexDay = (d) => {
|
|
556
581
|
const t = new Date(Date.now() - d * 86400000);
|
|
557
|
-
return
|
|
582
|
+
return join3(CODEX_SESSIONS, String(t.getFullYear()), String(t.getMonth() + 1).padStart(2, "0"), String(t.getDate()).padStart(2, "0"));
|
|
558
583
|
};
|
|
559
584
|
function codexRollout(tid) {
|
|
560
585
|
for (let d = 0;d < 30; d++) {
|
|
561
586
|
try {
|
|
562
|
-
const f =
|
|
587
|
+
const f = readdirSync3(codexDay(d)).find((n) => n.endsWith(`-${tid}.jsonl`));
|
|
563
588
|
if (f)
|
|
564
|
-
return
|
|
589
|
+
return join3(codexDay(d), f);
|
|
565
590
|
} catch {}
|
|
566
591
|
}
|
|
567
592
|
return;
|
|
@@ -571,7 +596,7 @@ function codexContext() {
|
|
|
571
596
|
if (!existsSync2(CODEX_SESSIONS))
|
|
572
597
|
return {};
|
|
573
598
|
const tid = codexThread(), own = tid ? codexRollout(tid) : undefined;
|
|
574
|
-
const files = own ? [{ f: own, m: 0 }] : [codexDay(0), codexDay(1)].filter((d) => existsSync2(d)).flatMap((d) =>
|
|
599
|
+
const files = own ? [{ f: own, m: 0 }] : [codexDay(0), codexDay(1)].filter((d) => existsSync2(d)).flatMap((d) => readdirSync3(d).filter((f) => f.endsWith(".jsonl")).map((f) => join3(d, f))).map((f) => ({ f, m: statSync3(f).mtimeMs })).filter((x) => Date.now() - x.m < 15 * 60000).sort((a, b) => b.m - a.m).slice(0, 10);
|
|
575
600
|
const cwd = process.cwd();
|
|
576
601
|
const read = (f, from, n) => {
|
|
577
602
|
const fd = openSync(f, "r");
|
|
@@ -643,7 +668,7 @@ function machineHeaders() {
|
|
|
643
668
|
return {};
|
|
644
669
|
}
|
|
645
670
|
}
|
|
646
|
-
var ROLES =
|
|
671
|
+
var ROLES = join3(HOME, "roles.json");
|
|
647
672
|
function roleHeaders(agent) {
|
|
648
673
|
const given = flag("role") ?? process.env.VIBEBOOK_ROLE;
|
|
649
674
|
const all = readJson(ROLES, {});
|
|
@@ -685,8 +710,11 @@ async function callTool(name, args, agent, tools) {
|
|
|
685
710
|
return die(`unknown tool ${name}; see: vibebook tools`);
|
|
686
711
|
const props = Object.keys(t.inputSchema.properties ?? {});
|
|
687
712
|
const root = repoRoot();
|
|
688
|
-
if (props.includes("room_id") && args.room_id === undefined && name !== "search"
|
|
689
|
-
|
|
713
|
+
if (props.includes("room_id") && args.room_id === undefined && name !== "search") {
|
|
714
|
+
const room = roomHere(root) ?? listeningRoom(HOME, agent);
|
|
715
|
+
if (room)
|
|
716
|
+
args.room_id = room;
|
|
717
|
+
}
|
|
690
718
|
if (props.includes("repo") && args.repo === undefined)
|
|
691
719
|
args.repo = basename(root);
|
|
692
720
|
const model = flag("model") ?? process.env.VIBEBOOK_MODEL;
|
|
@@ -717,9 +745,21 @@ var printResult = (res) => {
|
|
|
717
745
|
`);
|
|
718
746
|
} catch {}
|
|
719
747
|
} else if (c.data) {
|
|
720
|
-
const f =
|
|
748
|
+
const f = join3(tmpdir(), `vibebook-${randomBytes(4).toString("hex")}.${(c.mimeType ?? "").split("/")[1] || "bin"}`);
|
|
721
749
|
writeFileSync(f, Buffer.from(c.data, "base64"));
|
|
722
750
|
process.stdout.write(`${c.type} saved to ${f}
|
|
751
|
+
`);
|
|
752
|
+
} else if (c.resource?.blob) {
|
|
753
|
+
let name = c.resource.uri?.split("/").pop() ?? "";
|
|
754
|
+
try {
|
|
755
|
+
name = decodeURIComponent(name);
|
|
756
|
+
} catch {}
|
|
757
|
+
name = basename(name).replace(/^\.+/, "") || "file";
|
|
758
|
+
const dir = join3(tmpdir(), `vibebook-${randomBytes(4).toString("hex")}`);
|
|
759
|
+
mkdirSync(dir, { recursive: true });
|
|
760
|
+
const f = join3(dir, name);
|
|
761
|
+
writeFileSync(f, Buffer.from(c.resource.blob, "base64"));
|
|
762
|
+
process.stdout.write(`file saved to ${f}
|
|
723
763
|
`);
|
|
724
764
|
}
|
|
725
765
|
}
|
|
@@ -730,7 +770,7 @@ function readData() {
|
|
|
730
770
|
const d = flag("data") ?? pos[2];
|
|
731
771
|
if (d === undefined)
|
|
732
772
|
return {};
|
|
733
|
-
const raw = d === "@-" ?
|
|
773
|
+
const raw = d === "@-" ? readFileSync2(0, "utf8") : d.startsWith("@") ? readFileSync2(d.slice(1), "utf8") : d;
|
|
734
774
|
try {
|
|
735
775
|
const v = JSON.parse(raw);
|
|
736
776
|
if (!v || typeof v !== "object" || Array.isArray(v))
|
|
@@ -743,7 +783,7 @@ function readData() {
|
|
|
743
783
|
var COMPACT_AT = Number(process.env.VIBEBOOK_COMPACT_AT) || 0.75;
|
|
744
784
|
function pluginVersion() {
|
|
745
785
|
try {
|
|
746
|
-
const j = JSON.parse(
|
|
786
|
+
const j = JSON.parse(readFileSync2(join3(homedir(), ".claude", "plugins", "installed_plugins.json"), "utf8"));
|
|
747
787
|
return String((j.plugins ?? j)["vibebook@vibebook"]?.[0]?.version ?? "");
|
|
748
788
|
} catch {
|
|
749
789
|
return "";
|
|
@@ -751,11 +791,11 @@ function pluginVersion() {
|
|
|
751
791
|
}
|
|
752
792
|
function pluginHooks() {
|
|
753
793
|
try {
|
|
754
|
-
const j = JSON.parse(
|
|
755
|
-
const dir =
|
|
794
|
+
const j = JSON.parse(readFileSync2(join3(homedir(), ".claude", "plugins", "installed_plugins.json"), "utf8"));
|
|
795
|
+
const dir = join3(String((j.plugins ?? j)["vibebook@vibebook"]?.[0]?.installPath ?? ""), "hooks");
|
|
756
796
|
const h = createHash("sha256");
|
|
757
|
-
for (const f of
|
|
758
|
-
h.update(f).update(
|
|
797
|
+
for (const f of readdirSync3(dir).sort())
|
|
798
|
+
h.update(f).update(readFileSync2(join3(dir, f)));
|
|
759
799
|
return `hooks:${h.digest("hex").slice(0, 16)}`;
|
|
760
800
|
} catch {
|
|
761
801
|
return "";
|
|
@@ -764,7 +804,7 @@ function pluginHooks() {
|
|
|
764
804
|
var lastPluginRestart = 0;
|
|
765
805
|
var RESTART_GAP_MS = 3 * 60000;
|
|
766
806
|
var herdr = (...args) => JSON.parse(execFileSync("herdr", args, { stdio: ["ignore", "pipe", "pipe"] }).toString()).result;
|
|
767
|
-
var PROJECTS =
|
|
807
|
+
var PROJECTS = join3(homedir(), "Projects");
|
|
768
808
|
var LISTEN_PROMPT = 'Ouça a sala do vibebook desta pasta: rode `vibebook listen --model "<seu modelo>"` em background (Bash run_in_background); quando ele sair, `vibebook call read_room`, aja nas mensagens from_your_human e volte a ouvir. Pergunte na sala (com choices), nunca neste terminal: ninguém olha ele. (Com o plugin, é o /vibebook:listen.)';
|
|
769
809
|
var CODEX_LISTEN_PROMPT = 'Ouça a sala do vibebook desta pasta: rode `vibebook listen --model "<seu modelo>"` uma vez (no Codex ele fica em segundo plano e manda uma mensagem nesta conversa quando a sala tiver novidade); depois `vibebook call read_room` e aja nas mensagens from_your_human. Não rode listen de novo a cada turno.';
|
|
770
810
|
var RUN_AGENT = (flag("agent") ?? process.env.VIBEBOOK_AGENT) === "codex" ? "codex" : "claude";
|
|
@@ -772,7 +812,7 @@ var RUN_NAME = RUN_AGENT === "codex" ? "Codex" : "Claude";
|
|
|
772
812
|
var PERSONA = /^p_[a-f0-9]{32}$/.test(flag("persona") ?? "") ? flag("persona") : "";
|
|
773
813
|
var PERSONA_AS = (flag("as") ?? "").replace(/[^a-z0-9-]/g, "").slice(0, 24);
|
|
774
814
|
var TAG = PERSONA ? `-${PERSONA.slice(2, 10)}` : "";
|
|
775
|
-
var HERDR_FILE =
|
|
815
|
+
var HERDR_FILE = join3(HOME, `${RUN_AGENT === "codex" ? "herdr-codex" : "herdr"}${TAG}.json`);
|
|
776
816
|
var roomLabel = (title) => PERSONA_AS ? `${title.slice(0, 32)} · @${PERSONA_AS}` : RUN_AGENT === "codex" ? `${title.slice(0, 32)} · Codex` : title.slice(0, 40);
|
|
777
817
|
var listenWords = () => RUN_AGENT === "codex" ? CODEX_LISTEN_PROMPT : LISTEN_PROMPT;
|
|
778
818
|
var BYPASS = { claude: "--dangerously-skip-permissions", codex: "--dangerously-bypass-approvals-and-sandbox" };
|
|
@@ -780,7 +820,7 @@ var NO_AUTO_MODE = JSON.stringify({ permissions: { disableAutoMode: "disable" }
|
|
|
780
820
|
var agentArgs = (session) => RUN_AGENT === "codex" ? session ? ["resume", BYPASS.codex, session] : [BYPASS.codex] : [BYPASS.claude, "--settings", NO_AUTO_MODE, ...session ? ["--resume", session] : []];
|
|
781
821
|
function editJson(file, edit) {
|
|
782
822
|
try {
|
|
783
|
-
const cfg = existsSync2(file) ? JSON.parse(
|
|
823
|
+
const cfg = existsSync2(file) ? JSON.parse(readFileSync2(file, "utf8")) : {};
|
|
784
824
|
if (!edit(cfg))
|
|
785
825
|
return;
|
|
786
826
|
mkdirSync(dirname(file), { recursive: true });
|
|
@@ -792,7 +832,7 @@ function editJson(file, edit) {
|
|
|
792
832
|
function acceptBypass() {
|
|
793
833
|
if (RUN_AGENT !== "claude")
|
|
794
834
|
return;
|
|
795
|
-
editJson(
|
|
835
|
+
editJson(join3(homedir(), ".claude", "settings.json"), (cfg) => {
|
|
796
836
|
if (cfg.skipDangerousModePermissionPrompt === true)
|
|
797
837
|
return false;
|
|
798
838
|
cfg.skipDangerousModePermissionPrompt = true;
|
|
@@ -808,7 +848,7 @@ function trustFolder(folder) {
|
|
|
808
848
|
}
|
|
809
849
|
if (RUN_AGENT === "claude") {
|
|
810
850
|
acceptBypass();
|
|
811
|
-
editJson(
|
|
851
|
+
editJson(join3(homedir(), ".claude.json"), (cfg) => {
|
|
812
852
|
const projects = cfg.projects && typeof cfg.projects === "object" ? cfg.projects : {};
|
|
813
853
|
if (projects[folder]?.hasTrustDialogAccepted === true)
|
|
814
854
|
return false;
|
|
@@ -819,9 +859,9 @@ function trustFolder(folder) {
|
|
|
819
859
|
return;
|
|
820
860
|
}
|
|
821
861
|
try {
|
|
822
|
-
const file =
|
|
862
|
+
const file = join3(homedir(), ".codex", "config.toml");
|
|
823
863
|
const key = `[projects.${JSON.stringify(folder)}]`;
|
|
824
|
-
const toml = existsSync2(file) ?
|
|
864
|
+
const toml = existsSync2(file) ? readFileSync2(file, "utf8") : "";
|
|
825
865
|
if (toml.split(`
|
|
826
866
|
`).some((line) => line.trim() === key))
|
|
827
867
|
return;
|
|
@@ -845,7 +885,7 @@ function roomFolder(room, title) {
|
|
|
845
885
|
const look = (dir, depth) => {
|
|
846
886
|
let names = [];
|
|
847
887
|
try {
|
|
848
|
-
names =
|
|
888
|
+
names = readdirSync3(dir);
|
|
849
889
|
} catch {
|
|
850
890
|
return;
|
|
851
891
|
}
|
|
@@ -859,16 +899,16 @@ function roomFolder(room, title) {
|
|
|
859
899
|
return;
|
|
860
900
|
for (const n of names)
|
|
861
901
|
if (!n.startsWith(".") && n !== "node_modules" && n !== "_archive") {
|
|
862
|
-
const f = look(
|
|
902
|
+
const f = look(join3(dir, n), depth - 1);
|
|
863
903
|
if (f)
|
|
864
904
|
return f;
|
|
865
905
|
}
|
|
866
906
|
};
|
|
867
|
-
return look(PROJECTS, 2) ??
|
|
907
|
+
return look(PROJECTS, 2) ?? join3(PROJECTS, slug(title));
|
|
868
908
|
}
|
|
869
909
|
function linkFolder(folder, room) {
|
|
870
910
|
mkdirSync(folder, { recursive: true });
|
|
871
|
-
const f =
|
|
911
|
+
const f = join3(folder, existsSync2(join3(folder, ".deco-chat")) ? ".deco-chat" : ".vibebook");
|
|
872
912
|
let linked = false;
|
|
873
913
|
try {
|
|
874
914
|
linked = linkedRoom(folder) === room;
|
|
@@ -876,8 +916,8 @@ function linkFolder(folder, room) {
|
|
|
876
916
|
if (!linked) {
|
|
877
917
|
writeFileSync(f, `room_id=${room}
|
|
878
918
|
`);
|
|
879
|
-
const exclude =
|
|
880
|
-
if (existsSync2(exclude) && !
|
|
919
|
+
const exclude = join3(folder, ".git", "info", "exclude");
|
|
920
|
+
if (existsSync2(exclude) && !readFileSync2(exclude, "utf8").split(`
|
|
881
921
|
`).includes(".vibebook"))
|
|
882
922
|
appendFileSync(exclude, `
|
|
883
923
|
.vibebook
|
|
@@ -990,7 +1030,7 @@ function herdrServer(mode = "terminal") {
|
|
|
990
1030
|
}
|
|
991
1031
|
}).on("error", (e) => log("warn", "focus", `the app's ↗ button is off: ${e.message}`, { port })).on("listening", () => log("debug", "focus", "local endpoint for the app's ↗ button is up", { addr: `127.0.0.1:${port}` })).listen(port, "127.0.0.1");
|
|
992
1032
|
}
|
|
993
|
-
var BACKFILL =
|
|
1033
|
+
var BACKFILL = join3(HOME, "backfill.json");
|
|
994
1034
|
async function backfill(agent, tools, dry = false) {
|
|
995
1035
|
const mark = readJson(BACKFILL, {});
|
|
996
1036
|
if (!dry && (mark.done || mark.pid && mark.pid !== process.pid && Date.now() - (mark.at ?? 0) < 3600000 && alive(mark.pid)))
|
|
@@ -999,8 +1039,8 @@ async function backfill(agent, tools, dry = false) {
|
|
|
999
1039
|
return;
|
|
1000
1040
|
if (!dry)
|
|
1001
1041
|
writePrivate(BACKFILL, { pid: process.pid, at: Date.now() });
|
|
1002
|
-
const root =
|
|
1003
|
-
const scan = await scanTranscripts(root, (sid) => existsSync2(
|
|
1042
|
+
const root = join3(homedir(), ".claude", "projects");
|
|
1043
|
+
const scan = await scanTranscripts(root, (sid) => existsSync2(join3(USAGE, `${sid}.json`)));
|
|
1004
1044
|
const oldest = new Date(Date.now() - 730 * 86400000).toISOString().slice(0, 10);
|
|
1005
1045
|
const days = [...scan.days].filter(([d]) => d >= oldest).sort(([a], [b]) => a < b ? -1 : 1).map(([day, tokens]) => ({ day, tokens }));
|
|
1006
1046
|
if (dry) {
|
|
@@ -1018,14 +1058,14 @@ async function backfill(agent, tools, dry = false) {
|
|
|
1018
1058
|
}
|
|
1019
1059
|
mkdirSync(USAGE, { recursive: true });
|
|
1020
1060
|
for (const x of scan.sessions) {
|
|
1021
|
-
const file =
|
|
1061
|
+
const file = join3(USAGE, `${x.sid}.json`);
|
|
1022
1062
|
if (existsSync2(file))
|
|
1023
1063
|
continue;
|
|
1024
1064
|
let size = x.bytes;
|
|
1025
1065
|
try {
|
|
1026
|
-
const dir =
|
|
1066
|
+
const dir = readdirSync3(root).find((d) => existsSync2(join3(root, d, `${x.sid}.jsonl`)));
|
|
1027
1067
|
if (dir)
|
|
1028
|
-
size = Math.min(size,
|
|
1068
|
+
size = Math.min(size, statSync3(join3(root, dir, `${x.sid}.jsonl`)).size);
|
|
1029
1069
|
} catch {}
|
|
1030
1070
|
writeFileSync(file, JSON.stringify({ off: size, total: 0, last: x.last }));
|
|
1031
1071
|
}
|
|
@@ -1039,8 +1079,8 @@ var alive = (pid) => {
|
|
|
1039
1079
|
return false;
|
|
1040
1080
|
}
|
|
1041
1081
|
};
|
|
1042
|
-
var UPDATE =
|
|
1043
|
-
var APP_UPDATED =
|
|
1082
|
+
var UPDATE = join3(HOME, "update.json");
|
|
1083
|
+
var APP_UPDATED = join3(HOME, "app-updated.json");
|
|
1044
1084
|
var newer = (a, b) => {
|
|
1045
1085
|
const x = a.split(".").map(Number), y = b.split(".").map(Number);
|
|
1046
1086
|
for (let i = 0;i < 3; i++)
|
|
@@ -1068,12 +1108,12 @@ async function autoUpdate() {
|
|
|
1068
1108
|
writePrivate(UPDATE, { at: Date.now(), latest: v });
|
|
1069
1109
|
return;
|
|
1070
1110
|
}
|
|
1071
|
-
const tmp =
|
|
1111
|
+
const tmp = join3(HOME, `cli.mjs.${process.pid}`);
|
|
1072
1112
|
writeFileSync(tmp, text);
|
|
1073
|
-
renameSync(tmp,
|
|
1113
|
+
renameSync(tmp, join3(HOME, "cli.mjs"));
|
|
1074
1114
|
} catch {}
|
|
1075
1115
|
}
|
|
1076
|
-
var MACHINE =
|
|
1116
|
+
var MACHINE = join3(HOME, "machine.json");
|
|
1077
1117
|
function machineId() {
|
|
1078
1118
|
const m = readJson(MACHINE, {});
|
|
1079
1119
|
if (m.id && /^m_[a-f0-9]{32}$/.test(m.id))
|
|
@@ -1349,7 +1389,7 @@ async function herdrSync(agent, tools) {
|
|
|
1349
1389
|
writePrivate(HERDR_FILE, map);
|
|
1350
1390
|
herdrOrder([...on.keys()], map);
|
|
1351
1391
|
}
|
|
1352
|
-
var HERDR_APP =
|
|
1392
|
+
var HERDR_APP = join3(HOME, `${RUN_AGENT === "codex" ? "herdr-app-codex" : "herdr-app"}${TAG}.json`);
|
|
1353
1393
|
var happ = (...args) => {
|
|
1354
1394
|
const r = JSON.parse(execFileSync(findAgent("herdr-app") ?? "herdr-app", args, { stdio: ["ignore", "pipe", "pipe"] }).toString());
|
|
1355
1395
|
if (r?.error)
|
|
@@ -1364,7 +1404,7 @@ function appSession(terminal) {
|
|
|
1364
1404
|
const pid = JSON.parse(execFileSync("hc", ["get", terminal], { stdio: ["ignore", "pipe", "pipe"] }).toString()).foreground?.pid;
|
|
1365
1405
|
if (!pid)
|
|
1366
1406
|
return;
|
|
1367
|
-
const s = readJson(
|
|
1407
|
+
const s = readJson(join3(homedir(), ".claude", "sessions", `${pid}.json`), {}).sessionId;
|
|
1368
1408
|
return s && /^[0-9a-f-]{36}$/.test(s) ? s : undefined;
|
|
1369
1409
|
} catch {
|
|
1370
1410
|
return;
|
|
@@ -1450,7 +1490,7 @@ async function herdrAppSync(agent, tools) {
|
|
|
1450
1490
|
spaces = happ("ls").spaces;
|
|
1451
1491
|
});
|
|
1452
1492
|
}
|
|
1453
|
-
var HERDR_CORE =
|
|
1493
|
+
var HERDR_CORE = join3(HOME, `${RUN_AGENT === "codex" ? "herdr-core-codex" : "herdr-core"}${TAG}.json`);
|
|
1454
1494
|
function hc(...args) {
|
|
1455
1495
|
const env = { ...process.env };
|
|
1456
1496
|
for (const k of ["HC_TERMINAL_ID", "HC_TERMINAL_SOCKET", "HC_SCOPE"])
|
|
@@ -1471,7 +1511,7 @@ function coreSession(terminal) {
|
|
|
1471
1511
|
const pid = hc("get", terminal).foreground?.pid;
|
|
1472
1512
|
if (!pid)
|
|
1473
1513
|
return;
|
|
1474
|
-
const s = readJson(
|
|
1514
|
+
const s = readJson(join3(homedir(), ".claude", "sessions", `${pid}.json`), {}).sessionId;
|
|
1475
1515
|
return s && /^[0-9a-f-]{36}$/.test(s) ? s : undefined;
|
|
1476
1516
|
} catch {
|
|
1477
1517
|
return;
|
|
@@ -1533,12 +1573,12 @@ function herdrLeftovers() {
|
|
|
1533
1573
|
}
|
|
1534
1574
|
}
|
|
1535
1575
|
function claudeSessionAlive(sid) {
|
|
1536
|
-
const dir =
|
|
1576
|
+
const dir = join3(homedir(), ".claude", "sessions");
|
|
1537
1577
|
try {
|
|
1538
|
-
for (const f of
|
|
1578
|
+
for (const f of readdirSync3(dir)) {
|
|
1539
1579
|
if (!/^\d+\.json$/.test(f))
|
|
1540
1580
|
continue;
|
|
1541
|
-
const s = readJson(
|
|
1581
|
+
const s = readJson(join3(dir, f), {});
|
|
1542
1582
|
if (s.sessionId === sid && s.pid && alive(s.pid))
|
|
1543
1583
|
return true;
|
|
1544
1584
|
}
|
|
@@ -1548,13 +1588,13 @@ function claudeSessionAlive(sid) {
|
|
|
1548
1588
|
function claudeSessionIn(folder) {
|
|
1549
1589
|
if (!folder || RUN_AGENT !== "claude")
|
|
1550
1590
|
return;
|
|
1551
|
-
const dir =
|
|
1591
|
+
const dir = join3(homedir(), ".claude", "sessions");
|
|
1552
1592
|
let best;
|
|
1553
1593
|
try {
|
|
1554
|
-
for (const f of
|
|
1594
|
+
for (const f of readdirSync3(dir)) {
|
|
1555
1595
|
if (!/^\d+\.json$/.test(f))
|
|
1556
1596
|
continue;
|
|
1557
|
-
const s = readJson(
|
|
1597
|
+
const s = readJson(join3(dir, f), {});
|
|
1558
1598
|
if (s.cwd !== folder || !s.sessionId || !/^[0-9a-f-]{36}$/.test(s.sessionId) || !s.pid || !alive(s.pid))
|
|
1559
1599
|
continue;
|
|
1560
1600
|
if (!best || (s.startedAt ?? 0) > best.at)
|
|
@@ -1897,11 +1937,11 @@ var agentDirs = () => {
|
|
|
1897
1937
|
const h = homedir();
|
|
1898
1938
|
return [
|
|
1899
1939
|
...(process.env.PATH ?? "").split(process.platform === "win32" ? ";" : ":"),
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1940
|
+
join3(h, ".local", "bin"),
|
|
1941
|
+
join3(h, ".claude", "local"),
|
|
1942
|
+
join3(h, ".bun", "bin"),
|
|
1943
|
+
join3(h, ".npm-global", "bin"),
|
|
1944
|
+
join3(h, ".vibebook", "bin"),
|
|
1905
1945
|
"/opt/homebrew/bin",
|
|
1906
1946
|
"/usr/local/bin",
|
|
1907
1947
|
"/usr/bin"
|
|
@@ -1911,8 +1951,8 @@ function findAgent(cmd) {
|
|
|
1911
1951
|
const names = process.platform === "win32" ? [`${cmd}.exe`, `${cmd}.cmd`] : [cmd];
|
|
1912
1952
|
for (const d of agentDirs())
|
|
1913
1953
|
for (const n of names)
|
|
1914
|
-
if (existsSync2(
|
|
1915
|
-
return
|
|
1954
|
+
if (existsSync2(join3(d, n)))
|
|
1955
|
+
return join3(d, n);
|
|
1916
1956
|
return;
|
|
1917
1957
|
}
|
|
1918
1958
|
var agentRunners = createAgentRunners({
|
|
@@ -2008,7 +2048,7 @@ function hubNotice(raw) {
|
|
|
2008
2048
|
}
|
|
2009
2049
|
return typeof o.t === "string" ? o : null;
|
|
2010
2050
|
}
|
|
2011
|
-
var HUB =
|
|
2051
|
+
var HUB = join3(HOME, "hub.json");
|
|
2012
2052
|
var hubPort = () => {
|
|
2013
2053
|
const h = readJson(HUB, null);
|
|
2014
2054
|
if (!h)
|
|
@@ -2285,7 +2325,7 @@ async function main() {
|
|
|
2285
2325
|
mkdirSync(HOME, { recursive: true });
|
|
2286
2326
|
let log = "ignore";
|
|
2287
2327
|
try {
|
|
2288
|
-
log = openSync(
|
|
2328
|
+
log = openSync(join3(HOME, "codex-wake.log"), "a");
|
|
2289
2329
|
} catch {}
|
|
2290
2330
|
spawn(process.execPath, [process.argv[1], ...argv, "--codex-wake", tid], { detached: true, stdio: ["ignore", log, log], env: process.env, cwd: process.cwd() }).unref();
|
|
2291
2331
|
process.stdout.write(`deco.chat listen: ouvindo em segundo plano. Quando a sala tiver novidade, esta conversa do Codex recebe uma mensagem (codex queue) e você começa um turno. Não rode listen de novo depois de cada turno.
|
|
@@ -2297,7 +2337,7 @@ async function main() {
|
|
|
2297
2337
|
const parent = process.ppid;
|
|
2298
2338
|
const self = process.argv[1], born = (() => {
|
|
2299
2339
|
try {
|
|
2300
|
-
return
|
|
2340
|
+
return statSync3(self).mtimeMs;
|
|
2301
2341
|
} catch {
|
|
2302
2342
|
return 0;
|
|
2303
2343
|
}
|
|
@@ -2314,7 +2354,7 @@ async function main() {
|
|
|
2314
2354
|
}
|
|
2315
2355
|
let now = born;
|
|
2316
2356
|
try {
|
|
2317
|
-
now =
|
|
2357
|
+
now = statSync3(self).mtimeMs;
|
|
2318
2358
|
} catch {}
|
|
2319
2359
|
if (born && now !== born) {
|
|
2320
2360
|
if (wake) {
|
|
@@ -2343,9 +2383,9 @@ async function main() {
|
|
|
2343
2383
|
} catch {}
|
|
2344
2384
|
process.exit(0);
|
|
2345
2385
|
});
|
|
2346
|
-
const lock =
|
|
2386
|
+
const lock = join3(HOME, `listen-${agent}-${room ?? "all"}.pid`);
|
|
2347
2387
|
try {
|
|
2348
|
-
const old = Number(
|
|
2388
|
+
const old = Number(readFileSync2(lock, "utf8"));
|
|
2349
2389
|
if (old > 1 && old !== process.pid && /vibebook.*listen|cli\.mjs.*listen/.test(execFileSync("ps", ["-p", String(old), "-o", "command="], { encoding: "utf8" })))
|
|
2350
2390
|
process.kill(old, "SIGUSR2");
|
|
2351
2391
|
} catch {}
|
|
@@ -2446,7 +2486,7 @@ Rode \`vibebook call read_room${model ? ` --model "${model.replace(/["`$\\]/g, "
|
|
|
2446
2486
|
}
|
|
2447
2487
|
}
|
|
2448
2488
|
case "watch": {
|
|
2449
|
-
const shipped = process.env.VIBEBOOK_HERDR_DIR ?
|
|
2489
|
+
const shipped = process.env.VIBEBOOK_HERDR_DIR ? join3(process.env.VIBEBOOK_HERDR_DIR, `hc${process.platform === "win32" ? ".exe" : ""}`) : "";
|
|
2450
2490
|
process.env.VIBEBOOK_HC ||= findAgent("hc") ?? (shipped && existsSync2(shipped) ? shipped : "");
|
|
2451
2491
|
if (process.env.VIBEBOOK_HERDR_DIR)
|
|
2452
2492
|
process.env.PATH = `${process.env.VIBEBOOK_HERDR_DIR}${delimiter}${process.env.PATH ?? ""}`;
|
|
@@ -2530,7 +2570,7 @@ Rode \`vibebook call read_room${model ? ` --model "${model.replace(/["`$\\]/g, "
|
|
|
2530
2570
|
if (!bundle || !appVersion)
|
|
2531
2571
|
return "";
|
|
2532
2572
|
try {
|
|
2533
|
-
const v =
|
|
2573
|
+
const v = readFileSync2(join3(bundle, "Contents", "Info.plist"), "utf8").match(/<key>CFBundleShortVersionString<\/key>\s*<string>(\d+\.\d+\.\d+)<\/string>/)?.[1] ?? "";
|
|
2534
2574
|
return v && olderVersion(appVersion, v) ? v : "";
|
|
2535
2575
|
} catch {
|
|
2536
2576
|
return "";
|
|
@@ -2589,7 +2629,7 @@ Rode \`vibebook call read_room${model ? ` --model "${model.replace(/["`$\\]/g, "
|
|
|
2589
2629
|
openBrowser(`https://github.com/decocms/chat/releases/tag/tray-v${deco.appLatest}`);
|
|
2590
2630
|
else {
|
|
2591
2631
|
writePrivate(APP_UPDATED, { to: deco.appLatest, at: Date.now() });
|
|
2592
|
-
const log = openSync(
|
|
2632
|
+
const log = openSync(join3(HOME, "app-update.log"), "w", 384);
|
|
2593
2633
|
const setup = spawn("sh", ["-c", `curl -fsSL ${origin}/setup.sh | sh`], {
|
|
2594
2634
|
stdio: ["ignore", log, log],
|
|
2595
2635
|
detached: true,
|
|
@@ -2689,8 +2729,8 @@ Rode \`vibebook call read_room${model ? ` --model "${model.replace(/["`$\\]/g, "
|
|
|
2689
2729
|
return die(e.code === "ENOENT" ? `herdr isn't installed. It's the terminal your room Claudes open in. Set everything up with one line:
|
|
2690
2730
|
curl -fsSL https://chat.decocms.com/setup.sh | sh` : 'herdr isn\'t open. Run `herdr` in a terminal: with the vibebook plugin (herdr plugin install decocms/chat/herdr-plugin) its "vibebook" tab runs this for you.');
|
|
2691
2731
|
}
|
|
2692
|
-
const lock =
|
|
2693
|
-
const other = Number(existsSync2(lock) ?
|
|
2732
|
+
const lock = join3(HOME, `herdr${TAG}.pid`);
|
|
2733
|
+
const other = Number(existsSync2(lock) ? readFileSync2(lock, "utf8") : 0);
|
|
2694
2734
|
if (other && other !== process.pid) {
|
|
2695
2735
|
let alive = true;
|
|
2696
2736
|
try {
|
|
@@ -2704,7 +2744,7 @@ Rode \`vibebook call read_room${model ? ` --model "${model.replace(/["`$\\]/g, "
|
|
|
2704
2744
|
writeFileSync(lock, String(process.pid));
|
|
2705
2745
|
const release = () => {
|
|
2706
2746
|
try {
|
|
2707
|
-
if (Number(
|
|
2747
|
+
if (Number(readFileSync2(lock, "utf8")) === process.pid)
|
|
2708
2748
|
unlinkSync(lock);
|
|
2709
2749
|
} catch {}
|
|
2710
2750
|
};
|
|
@@ -2730,10 +2770,10 @@ Rode \`vibebook call read_room${model ? ` --model "${model.replace(/["`$\\]/g, "
|
|
|
2730
2770
|
if (RUN_AGENT === "codex") {
|
|
2731
2771
|
log("info", "start", "opens Codex in each room (--agent codex)");
|
|
2732
2772
|
try {
|
|
2733
|
-
const rules =
|
|
2773
|
+
const rules = join3(homedir(), ".codex", "rules"), f = join3(rules, "vibebook.rules");
|
|
2734
2774
|
const rule = `prefix_rule(pattern=["vibebook"], decision="allow")
|
|
2735
2775
|
`;
|
|
2736
|
-
if (!existsSync2(f) || !
|
|
2776
|
+
if (!existsSync2(f) || !readFileSync2(f, "utf8").includes('pattern=["vibebook"]')) {
|
|
2737
2777
|
mkdirSync(rules, { recursive: true });
|
|
2738
2778
|
writeFileSync(f, `# vibebook herdr --agent codex: the room's Codex runs the vibebook CLI outside the sandbox (network, ~/.vibebook).
|
|
2739
2779
|
${rule}`);
|
|
@@ -2829,7 +2869,7 @@ ${rule}`);
|
|
|
2829
2869
|
const link = JSON.parse(res.content[0]?.text ?? "{}");
|
|
2830
2870
|
if (!link.url)
|
|
2831
2871
|
return die(res.content[0]?.text ?? "no upload link");
|
|
2832
|
-
const put = await fetch(link.url, { method: "PUT", headers: { "content-type": type }, body:
|
|
2872
|
+
const put = await fetch(link.url, { method: "PUT", headers: { "content-type": type }, body: readFileSync2(file) });
|
|
2833
2873
|
process.stdout.write(`${await put.text()}
|
|
2834
2874
|
`);
|
|
2835
2875
|
if (!put.ok)
|
|
@@ -2877,7 +2917,7 @@ ${rule}`);
|
|
|
2877
2917
|
const r = await fetch(`${origin}/cli.mjs`);
|
|
2878
2918
|
if (!r.ok)
|
|
2879
2919
|
return die(`could not download: ${r.status}`);
|
|
2880
|
-
writeFileSync(
|
|
2920
|
+
writeFileSync(join3(HOME, "cli.mjs"), await r.text());
|
|
2881
2921
|
process.stdout.write(`Atualizado.
|
|
2882
2922
|
`);
|
|
2883
2923
|
return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decocms/chat",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.3",
|
|
4
4
|
"description": "deco.chat CLI: your deco.chat rooms from the terminal, for you and your Claude. One login per machine; room tools discovered at run time.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": { "deco-chat": "cli.mjs", "vibebook": "cli.mjs" },
|