vibebook.dev 1.5.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 +130 -11
  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.5.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,6 +845,9 @@ 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
848
+ vibebook connect <mcp url> [--token-cmd "op read op://…" | --token-env VAR | --token-file path] [--scope me|room] [--title ..]
849
+ link an MCP server to this folder's room (its apps show in the Apps tab); the token
850
+ goes from your secret store to vibebook without being printed
774
851
  vibebook upload <file> [--message ".."] [--thread <epic>] [--room <id>] ['{"thread":"<epic>"}']
775
852
  vibebook herdr [--once] keep one Claude Code per room in herdr: opens it when you switch
776
853
  "Meu Claude" on in the app, closes it when you switch it off
@@ -808,10 +885,18 @@ async function ensureHub() {
808
885
  async function hub() {
809
886
  if (hubPort())
810
887
  return;
811
- let gen = 0, allGen = 0, lastUse = Date.now(), backoff = 1000;
888
+ let gen = 0, allGen = 0, agentsGen = 0, lastUse = Date.now(), backoff = 1000;
812
889
  const books = new Map;
813
890
  const waiters = new Set;
814
- 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
+ };
815
900
  const bump = (book) => {
816
901
  gen++;
817
902
  if (book)
@@ -819,7 +904,7 @@ async function hub() {
819
904
  else
820
905
  allGen = gen;
821
906
  for (const w of [...waiters])
822
- if (!book || w.book === "*" || w.book === book) {
907
+ if (w.book !== "agents" && (!book || w.book === "*" || w.book === book)) {
823
908
  waiters.delete(w);
824
909
  w.done();
825
910
  }
@@ -830,7 +915,7 @@ async function hub() {
830
915
  const book = u.searchParams.get("book") || "*", since = Number(u.searchParams.get("since") ?? -1);
831
916
  const answer = (timeout = false) => {
832
917
  res.writeHead(200, { "content-type": "application/json" });
833
- res.end(JSON.stringify({ gen, timeout }));
918
+ res.end(JSON.stringify({ gen, agents: agentsGen, timeout }));
834
919
  };
835
920
  if (seen(book) > since)
836
921
  return answer();
@@ -879,6 +964,7 @@ async function hub() {
879
964
  ws.onopen = () => {
880
965
  backoff = 1000;
881
966
  bump();
967
+ bumpAgents();
882
968
  ping = setInterval(() => {
883
969
  try {
884
970
  ws.send("ping");
@@ -890,6 +976,8 @@ async function hub() {
890
976
  const m = JSON.parse(String(e.data));
891
977
  if (m.t === "me" && m.book)
892
978
  bump(m.book);
979
+ if (m.t === "agents")
980
+ bumpAgents();
893
981
  } catch {}
894
982
  };
895
983
  ws.onclose = again;
@@ -1085,9 +1173,9 @@ async function main() {
1085
1173
  if (!file || !existsSync(file))
1086
1174
  return die("usage: vibebook upload <file> [--message ..] [--thread ..]");
1087
1175
  const ext = file.toLowerCase().split(".").pop() ?? "";
1088
- 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];
1089
1177
  if (!type)
1090
- 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)");
1091
1179
  const args = { filename: basename(file), content_type: type };
1092
1180
  if (flag("room"))
1093
1181
  args.room_id = flag("room");
@@ -1117,6 +1205,37 @@ async function main() {
1117
1205
  process.exit(1);
1118
1206
  return;
1119
1207
  }
1208
+ case "connect": {
1209
+ const url = pos[1];
1210
+ if (!url)
1211
+ return die('usage: vibebook connect <mcp url> [--token-cmd "op read op://Vault/Item/field" | --token-env VAR | --token-file path] [--scope room|me] [--title ..] [--room <id>]');
1212
+ let token = "";
1213
+ if (flag("token-cmd")) {
1214
+ try {
1215
+ token = execSync(flag("token-cmd"), { stdio: ["inherit", "pipe", "inherit"] }).toString().trim();
1216
+ } catch {
1217
+ return die("--token-cmd failed (nothing was sent)");
1218
+ }
1219
+ } else if (flag("token-env"))
1220
+ token = process.env[flag("token-env")] ?? "";
1221
+ else if (flag("token-file"))
1222
+ token = existsSync(flag("token-file")) ? readFileSync(flag("token-file"), "utf8").trim() : "";
1223
+ if ((flag("token-cmd") || flag("token-env") || flag("token-file")) && !token)
1224
+ return die("the token came back empty (nothing was sent)");
1225
+ const args = { url, scope: flag("scope") === "room" ? "room" : "me" };
1226
+ if (token)
1227
+ args.token = token;
1228
+ if (flag("title"))
1229
+ args.title = flag("title");
1230
+ if (flag("room"))
1231
+ args.room_id = flag("room");
1232
+ const res = await callTool("connect_mcp", args, agent);
1233
+ process.stdout.write(`${res.content[0]?.text ?? ""}
1234
+ `);
1235
+ if (res.isError)
1236
+ process.exit(1);
1237
+ return;
1238
+ }
1120
1239
  case "update": {
1121
1240
  if (!import.meta.url.startsWith(`file://${HOME}`)) {
1122
1241
  process.stdout.write(`Instalado pelo npm: rode npm i -g vibebook.dev@latest (ou bun add -g vibebook.dev@latest).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vibebook.dev",
3
- "version": "1.5.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" },