vibebook.dev 1.6.0 → 1.6.1

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.
Files changed (2) hide show
  1. package/cli.mjs +98 -13
  2. package/package.json +1 -1
package/cli.mjs CHANGED
@@ -7,7 +7,7 @@ import { appendFileSync, chmodSync, closeSync, existsSync, fstatSync, mkdirSync,
7
7
  import { createServer } from "node:http";
8
8
  import { homedir, tmpdir } from "node:os";
9
9
  import { basename, dirname, join } from "node:path";
10
- var VERSION = "1.6.0";
10
+ var VERSION = "1.6.1";
11
11
  var HOME = process.env.VIBEBOOK_HOME || join(homedir(), ".vibebook");
12
12
  var SESSION = join(HOME, "session.json");
13
13
  var AGENTS = join(HOME, "agents.json");
@@ -281,10 +281,70 @@ var sessionStatus = (sid) => {
281
281
  const s = readJson(join(STATUS, `${sid}.json`), null);
282
282
  return s && Date.now() - s.at < 6 * 3600000 ? s : undefined;
283
283
  };
284
+ function codexContext() {
285
+ try {
286
+ const root = join(homedir(), ".codex", "sessions");
287
+ if (!existsSync(root))
288
+ return {};
289
+ const day = (d) => {
290
+ const t = new Date(Date.now() - d * 86400000);
291
+ return join(root, String(t.getFullYear()), String(t.getMonth() + 1).padStart(2, "0"), String(t.getDate()).padStart(2, "0"));
292
+ };
293
+ const files = [day(0), day(1)].filter((d) => existsSync(d)).flatMap((d) => readdirSync(d).filter((f) => f.endsWith(".jsonl")).map((f) => join(d, f))).map((f) => ({ f, m: statSync(f).mtimeMs })).filter((x) => Date.now() - x.m < 15 * 60000).sort((a, b) => b.m - a.m).slice(0, 10);
294
+ const cwd = process.cwd();
295
+ const read = (f, from, n) => {
296
+ const fd = openSync(f, "r");
297
+ const size = fstatSync(fd).size;
298
+ const k = Math.min(size, n);
299
+ const buf = Buffer.alloc(k);
300
+ readSync(fd, buf, 0, k, from === "head" ? 0 : size - k);
301
+ closeSync(fd);
302
+ return buf.toString("utf8");
303
+ };
304
+ for (const { f } of files) {
305
+ let meta;
306
+ try {
307
+ meta = JSON.parse(read(f, "head", 1 << 20).split(`
308
+ `)[0]);
309
+ } catch {
310
+ continue;
311
+ }
312
+ const dir = String(meta?.payload?.cwd ?? "");
313
+ if (!dir || cwd !== dir && !cwd.startsWith(dir + "/"))
314
+ continue;
315
+ const lines = read(f, "tail", 2 << 20).split(`
316
+ `);
317
+ for (let i = lines.length - 1;i >= 0; i--) {
318
+ if (!lines[i].includes('"token_count"'))
319
+ continue;
320
+ let p;
321
+ try {
322
+ p = JSON.parse(lines[i])?.payload;
323
+ } catch {
324
+ continue;
325
+ }
326
+ const u = p?.info?.last_token_usage, w = Number(p?.info?.model_context_window);
327
+ if (!u || !w)
328
+ continue;
329
+ const h = { "x-vibebook-context": `${Number(u.input_tokens) || 0}/${w}` };
330
+ const limits = {};
331
+ for (const [k, l] of [["five_hour", p.rate_limits?.primary], ["seven_day", p.rate_limits?.secondary]])
332
+ if (typeof l?.used_percent === "number")
333
+ limits[k] = { p: Math.round(l.used_percent), r: Number(l.resets_at) || 0 };
334
+ if (Object.keys(limits).length)
335
+ h["x-vibebook-limits"] = JSON.stringify(limits);
336
+ return h;
337
+ }
338
+ }
339
+ } catch {}
340
+ return {};
341
+ }
284
342
  var myContext = () => {
285
343
  const sid = process.env.CLAUDE_CODE_SESSION_ID;
286
- const c = sid ? sessionContext(sid) : undefined;
287
- if (!sid || !c)
344
+ if (!sid)
345
+ return codexContext();
346
+ const c = sessionContext(sid);
347
+ if (!c)
288
348
  return {};
289
349
  const st = sessionStatus(sid);
290
350
  const h = { "x-vibebook-context": `${c.tokens}/${st?.window ?? c.window}` };
@@ -488,8 +548,22 @@ function herdrServer(appMode = false) {
488
548
  }).on("error", (e) => process.stderr.write(`vibebook herdr: botão "ir pro Claude" desligado (${e.message})
489
549
  `)).listen(port, "127.0.0.1");
490
550
  }
491
- async function herdrSync(agent, tools) {
551
+ var roomsCache = null;
552
+ async function claudeRooms(agent, tools) {
553
+ const port = await ensureHub().catch(() => null);
554
+ let gen = -1;
555
+ if (port)
556
+ try {
557
+ gen = (await (await fetch(`http://127.0.0.1:${port}/wait?book=agents&since=-1`)).json()).agents ?? -1;
558
+ } catch {}
559
+ if (roomsCache && gen >= 0 && roomsCache.gen === gen && Date.now() - roomsCache.at < 5 * 60000)
560
+ return roomsCache.res;
492
561
  const res = await callTool("claude_rooms", {}, agent, tools);
562
+ roomsCache = { gen, at: Date.now(), res };
563
+ return res;
564
+ }
565
+ async function herdrSync(agent, tools) {
566
+ const res = await claudeRooms(agent, tools);
493
567
  const rooms = JSON.parse(res.content[0]?.text ?? "{}").rooms ?? [];
494
568
  const deaf = new Set(rooms.filter((r) => r.on && r.listening === false).map((r) => r.room_id));
495
569
  const map = readJson(HERDR, {});
@@ -642,7 +716,7 @@ var happ = (...args) => {
642
716
  };
643
717
  var appClaude = (sp) => sp.tabs.flatMap((t) => t.terminals).find((t) => /claude/i.test(t.app ?? ""));
644
718
  async function herdrAppSync(agent, tools) {
645
- const res = await callTool("claude_rooms", {}, agent, tools);
719
+ const res = await claudeRooms(agent, tools);
646
720
  const rooms = JSON.parse(res.content[0]?.text ?? "{}").rooms ?? [];
647
721
  const deaf = new Set(rooms.filter((r) => r.on && r.listening === false).map((r) => r.room_id));
648
722
  const map = readJson(HERDR_APP, {});
@@ -771,7 +845,7 @@ var HELP = `vibebook ${VERSION}: your vibebook rooms from the terminal (for you
771
845
  vibebook call <tool> ['{json}' | -d @file | -d @-]
772
846
  run a tool; room_id, repo and model are filled in from this folder
773
847
  vibebook listen [--room <id>] wait until the room has messages you haven't read, print them, exit
774
- vibebook connect <mcp url> [--token-cmd "op read op://…" | --token-env VAR | --token-file path] [--scope room|me] [--title ..]
848
+ vibebook connect <mcp url> [--token-cmd "op read op://…" | --token-env VAR | --token-file path] [--scope me|room] [--title ..]
775
849
  link an MCP server to this folder's room (its apps show in the Apps tab); the token
776
850
  goes from your secret store to vibebook without being printed
777
851
  vibebook upload <file> [--message ".."] [--thread <epic>] [--room <id>] ['{"thread":"<epic>"}']
@@ -811,10 +885,18 @@ async function ensureHub() {
811
885
  async function hub() {
812
886
  if (hubPort())
813
887
  return;
814
- let gen = 0, allGen = 0, lastUse = Date.now(), backoff = 1000;
888
+ let gen = 0, allGen = 0, agentsGen = 0, lastUse = Date.now(), backoff = 1000;
815
889
  const books = new Map;
816
890
  const waiters = new Set;
817
- const seen = (book) => book === "*" ? gen : Math.max(books.get(book) ?? 0, allGen);
891
+ const seen = (book) => book === "agents" ? agentsGen : book === "*" ? gen : Math.max(books.get(book) ?? 0, allGen);
892
+ const bumpAgents = () => {
893
+ agentsGen = ++gen;
894
+ for (const w of [...waiters])
895
+ if (w.book === "agents") {
896
+ waiters.delete(w);
897
+ w.done();
898
+ }
899
+ };
818
900
  const bump = (book) => {
819
901
  gen++;
820
902
  if (book)
@@ -822,7 +904,7 @@ async function hub() {
822
904
  else
823
905
  allGen = gen;
824
906
  for (const w of [...waiters])
825
- if (!book || w.book === "*" || w.book === book) {
907
+ if (w.book !== "agents" && (!book || w.book === "*" || w.book === book)) {
826
908
  waiters.delete(w);
827
909
  w.done();
828
910
  }
@@ -833,7 +915,7 @@ async function hub() {
833
915
  const book = u.searchParams.get("book") || "*", since = Number(u.searchParams.get("since") ?? -1);
834
916
  const answer = (timeout = false) => {
835
917
  res.writeHead(200, { "content-type": "application/json" });
836
- res.end(JSON.stringify({ gen, timeout }));
918
+ res.end(JSON.stringify({ gen, agents: agentsGen, timeout }));
837
919
  };
838
920
  if (seen(book) > since)
839
921
  return answer();
@@ -882,6 +964,7 @@ async function hub() {
882
964
  ws.onopen = () => {
883
965
  backoff = 1000;
884
966
  bump();
967
+ bumpAgents();
885
968
  ping = setInterval(() => {
886
969
  try {
887
970
  ws.send("ping");
@@ -893,6 +976,8 @@ async function hub() {
893
976
  const m = JSON.parse(String(e.data));
894
977
  if (m.t === "me" && m.book)
895
978
  bump(m.book);
979
+ if (m.t === "agents")
980
+ bumpAgents();
896
981
  } catch {}
897
982
  };
898
983
  ws.onclose = again;
@@ -1088,9 +1173,9 @@ async function main() {
1088
1173
  if (!file || !existsSync(file))
1089
1174
  return die("usage: vibebook upload <file> [--message ..] [--thread ..]");
1090
1175
  const ext = file.toLowerCase().split(".").pop() ?? "";
1091
- const type = { png: "image/png", jpg: "image/jpeg", jpeg: "image/jpeg", webp: "image/webp", gif: "image/gif", pdf: "application/pdf", html: "text/html", md: "text/markdown" }[ext];
1176
+ const type = { png: "image/png", jpg: "image/jpeg", jpeg: "image/jpeg", webp: "image/webp", gif: "image/gif", pdf: "application/pdf", html: "text/html", md: "text/markdown", m4a: "audio/x-m4a", mp3: "audio/mpeg", wav: "audio/wav", aac: "audio/aac", ogg: "audio/ogg", opus: "audio/ogg", webm: "audio/webm", flac: "audio/flac" }[ext];
1092
1177
  if (!type)
1093
- return die("only png, jpg, webp, gif, pdf, html or md");
1178
+ return die("only png, jpg, webp, gif, pdf, html, md or audio (m4a, mp3, wav, aac, ogg, webm, flac)");
1094
1179
  const args = { filename: basename(file), content_type: type };
1095
1180
  if (flag("room"))
1096
1181
  args.room_id = flag("room");
@@ -1137,7 +1222,7 @@ async function main() {
1137
1222
  token = existsSync(flag("token-file")) ? readFileSync(flag("token-file"), "utf8").trim() : "";
1138
1223
  if ((flag("token-cmd") || flag("token-env") || flag("token-file")) && !token)
1139
1224
  return die("the token came back empty (nothing was sent)");
1140
- const args = { url, scope: flag("scope") === "me" ? "me" : "room" };
1225
+ const args = { url, scope: flag("scope") === "room" ? "room" : "me" };
1141
1226
  if (token)
1142
1227
  args.token = token;
1143
1228
  if (flag("title"))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vibebook.dev",
3
- "version": "1.6.0",
3
+ "version": "1.6.1",
4
4
  "description": "vibebook CLI: your vibebook 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": { "vibebook": "cli.mjs" },