vibebook.dev 1.1.0 → 1.2.0
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 +346 -37
- package/package.json +1 -1
package/cli.mjs
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
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, existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { appendFileSync, chmodSync, closeSync, existsSync, fstatSync, mkdirSync, openSync, readdirSync, readFileSync, readSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
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.
|
|
10
|
+
var VERSION = "1.2.0";
|
|
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");
|
|
@@ -27,8 +27,10 @@ var readJson = (f, fallback) => {
|
|
|
27
27
|
};
|
|
28
28
|
var writePrivate = (f, data) => {
|
|
29
29
|
mkdirSync(dirname(f), { recursive: true, mode: 448 });
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
const tmp = `${f}.${process.pid}.tmp`;
|
|
31
|
+
writeFileSync(tmp, JSON.stringify(data, null, 2), { mode: 384 });
|
|
32
|
+
chmodSync(tmp, 384);
|
|
33
|
+
renameSync(tmp, f);
|
|
32
34
|
};
|
|
33
35
|
var argv = process.argv.slice(2);
|
|
34
36
|
var flags = {};
|
|
@@ -56,10 +58,7 @@ function repoRoot() {
|
|
|
56
58
|
return process.cwd();
|
|
57
59
|
}
|
|
58
60
|
}
|
|
59
|
-
|
|
60
|
-
const own = readFileSync(join(root, ".vibebook"), { encoding: "utf8", flag: "r" }).match(/^room_id=([a-z0-9]+)/m)?.[1];
|
|
61
|
-
return own;
|
|
62
|
-
}
|
|
61
|
+
var linkedRoom = (root) => readFileSync(join(root, ".vibebook"), "utf8").match(/^room_id=([a-z0-9]+)/m)?.[1];
|
|
63
62
|
function roomHere(root) {
|
|
64
63
|
try {
|
|
65
64
|
const r = linkedRoom(root);
|
|
@@ -140,34 +139,128 @@ ${url}
|
|
|
140
139
|
const t = await tok.json();
|
|
141
140
|
writePrivate(SESSION, { origin, client_id, access_token: t.access_token, refresh_token: t.refresh_token, expires_at: Date.now() + (t.expires_in ?? 3600) * 1000 });
|
|
142
141
|
}
|
|
142
|
+
var fresh = (s) => !!s && Date.now() < s.expires_at - 60000;
|
|
143
143
|
async function session() {
|
|
144
|
-
|
|
144
|
+
let s = readJson(SESSION, null);
|
|
145
145
|
if (!s)
|
|
146
146
|
return die("Not logged in. Ask your human to run, in their own terminal: vibebook login");
|
|
147
|
-
if (
|
|
147
|
+
if (fresh(s))
|
|
148
148
|
return s;
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
149
|
+
const lock = `${SESSION}.lock`;
|
|
150
|
+
for (let i = 0;; i++) {
|
|
151
|
+
try {
|
|
152
|
+
mkdirSync(lock);
|
|
153
|
+
break;
|
|
154
|
+
} catch {
|
|
155
|
+
try {
|
|
156
|
+
if (Date.now() - statSync(lock).mtimeMs > 30000)
|
|
157
|
+
rmSync(lock, { recursive: true, force: true });
|
|
158
|
+
} catch {}
|
|
159
|
+
if (i > 100)
|
|
160
|
+
return die("vibebook session is locked by another process; try again in a moment", 75);
|
|
161
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
162
|
+
const again = readJson(SESSION, null);
|
|
163
|
+
if (fresh(again))
|
|
164
|
+
return again;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const quit = (m, code) => (rmSync(lock, { recursive: true, force: true }), die(m, code));
|
|
168
|
+
try {
|
|
169
|
+
s = readJson(SESSION, null) ?? s;
|
|
170
|
+
if (fresh(s))
|
|
171
|
+
return s;
|
|
172
|
+
if (!s.refresh_token)
|
|
173
|
+
return quit("Session expired. Ask your human to run, in their own terminal: vibebook login");
|
|
174
|
+
const r = await fetch(`${s.origin}/oauth/token`, {
|
|
175
|
+
method: "POST",
|
|
176
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
177
|
+
body: new URLSearchParams({ grant_type: "refresh_token", refresh_token: s.refresh_token, client_id: s.client_id })
|
|
178
|
+
});
|
|
179
|
+
if (r.status === 400 || r.status === 401)
|
|
180
|
+
return quit("Could not refresh the session. Ask your human to run, in their own terminal: vibebook login");
|
|
181
|
+
if (!r.ok)
|
|
182
|
+
return quit(`vibebook unreachable (${r.status}); try again in a moment`, 75);
|
|
183
|
+
const t = await r.json();
|
|
184
|
+
const next = { ...s, access_token: t.access_token, refresh_token: t.refresh_token ?? s.refresh_token, expires_at: Date.now() + (t.expires_in ?? 3600) * 1000 };
|
|
185
|
+
writePrivate(SESSION, next);
|
|
186
|
+
return next;
|
|
187
|
+
} finally {
|
|
188
|
+
rmSync(lock, { recursive: true, force: true });
|
|
189
|
+
}
|
|
164
190
|
}
|
|
191
|
+
var CTX_WINDOW = Number(process.env.VIBEBOOK_CONTEXT_WINDOW) || 1e6;
|
|
192
|
+
function sessionContext(sid) {
|
|
193
|
+
if (!/^[\w-]{8,80}$/.test(sid))
|
|
194
|
+
return;
|
|
195
|
+
try {
|
|
196
|
+
const root = join(homedir(), ".claude", "projects");
|
|
197
|
+
const dir = readdirSync(root).find((d) => existsSync(join(root, d, `${sid}.jsonl`)));
|
|
198
|
+
if (!dir)
|
|
199
|
+
return;
|
|
200
|
+
const fd = openSync(join(root, dir, `${sid}.jsonl`), "r");
|
|
201
|
+
const size = fstatSync(fd).size;
|
|
202
|
+
const n = Math.min(size, 4 << 20);
|
|
203
|
+
const buf = Buffer.alloc(n);
|
|
204
|
+
readSync(fd, buf, 0, n, size - n);
|
|
205
|
+
closeSync(fd);
|
|
206
|
+
const lines = buf.toString("utf8").split(`
|
|
207
|
+
`);
|
|
208
|
+
for (let i = lines.length - 1;i >= 0; i--) {
|
|
209
|
+
if (lines[i].includes('"compact_boundary"'))
|
|
210
|
+
return { tokens: 0, window: CTX_WINDOW };
|
|
211
|
+
if (!lines[i].includes('"usage"'))
|
|
212
|
+
continue;
|
|
213
|
+
try {
|
|
214
|
+
const u = JSON.parse(lines[i])?.message?.usage;
|
|
215
|
+
if (u)
|
|
216
|
+
return { tokens: (u.input_tokens || 0) + (u.cache_read_input_tokens || 0) + (u.cache_creation_input_tokens || 0), window: CTX_WINDOW };
|
|
217
|
+
} catch {}
|
|
218
|
+
}
|
|
219
|
+
} catch {}
|
|
220
|
+
}
|
|
221
|
+
var STATUS = join(HOME, "status");
|
|
222
|
+
function statusline() {
|
|
223
|
+
const input = readFileSync(0);
|
|
224
|
+
try {
|
|
225
|
+
const j = JSON.parse(input.toString());
|
|
226
|
+
if (/^[\w-]{8,80}$/.test(j.session_id ?? "")) {
|
|
227
|
+
const limits = {};
|
|
228
|
+
for (const k of ["five_hour", "seven_day"]) {
|
|
229
|
+
const l = j.rate_limits?.[k];
|
|
230
|
+
if (typeof l?.used_percentage === "number")
|
|
231
|
+
limits[k] = { p: Math.round(l.used_percentage), r: Number(l.resets_at) || 0 };
|
|
232
|
+
}
|
|
233
|
+
mkdirSync(STATUS, { recursive: true });
|
|
234
|
+
writeFileSync(join(STATUS, `${j.session_id}.json`), JSON.stringify({ window: Number(j.context_window?.context_window_size) || undefined, limits, at: Date.now() }));
|
|
235
|
+
}
|
|
236
|
+
} catch {}
|
|
237
|
+
const rest = process.argv.slice(process.argv.indexOf("statusline") + 1).join(" ").trim();
|
|
238
|
+
if (rest)
|
|
239
|
+
process.stdout.write(execSync(rest, { input, stdio: ["pipe", "pipe", "ignore"] }));
|
|
240
|
+
}
|
|
241
|
+
var sessionStatus = (sid) => {
|
|
242
|
+
if (!/^[\w-]{8,80}$/.test(sid))
|
|
243
|
+
return;
|
|
244
|
+
const s = readJson(join(STATUS, `${sid}.json`), null);
|
|
245
|
+
return s && Date.now() - s.at < 6 * 3600000 ? s : undefined;
|
|
246
|
+
};
|
|
247
|
+
var myContext = () => {
|
|
248
|
+
const sid = process.env.CLAUDE_CODE_SESSION_ID;
|
|
249
|
+
const c = sid ? sessionContext(sid) : undefined;
|
|
250
|
+
if (!sid || !c)
|
|
251
|
+
return {};
|
|
252
|
+
const st = sessionStatus(sid);
|
|
253
|
+
const h = { "x-vibebook-context": `${c.tokens}/${st?.window ?? c.window}` };
|
|
254
|
+
if (st?.limits && Object.keys(st.limits).length)
|
|
255
|
+
h["x-vibebook-limits"] = JSON.stringify(st.limits);
|
|
256
|
+
return h;
|
|
257
|
+
};
|
|
165
258
|
var rpcId = 0;
|
|
166
259
|
async function rpc(method, params, agent) {
|
|
167
260
|
const s = await session();
|
|
168
261
|
const r = await fetch(`${s.origin}/mcp`, {
|
|
169
262
|
method: "POST",
|
|
170
|
-
headers: { "content-type": "application/json", authorization: `Bearer ${s.access_token}`, "mcp-session-id": agent },
|
|
263
|
+
headers: { "content-type": "application/json", authorization: `Bearer ${s.access_token}`, "mcp-session-id": agent, ...myContext() },
|
|
171
264
|
body: JSON.stringify({ jsonrpc: "2.0", id: ++rpcId, method, params })
|
|
172
265
|
});
|
|
173
266
|
if (r.status === 401)
|
|
@@ -225,8 +318,10 @@ function readData() {
|
|
|
225
318
|
return die(`arguments must be a JSON object, e.g. '{"room_id":"abc"}' (or -d @file.json, -d @-)`);
|
|
226
319
|
}
|
|
227
320
|
}
|
|
321
|
+
var COMPACT_AT = Number(process.env.VIBEBOOK_COMPACT_AT) || 0.75;
|
|
228
322
|
var herdr = (...args) => JSON.parse(execFileSync("herdr", args, { stdio: ["ignore", "pipe", "pipe"] }).toString()).result;
|
|
229
323
|
var PROJECTS = join(homedir(), "Projects");
|
|
324
|
+
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. (Com o plugin, é o /vibebook:listen.)';
|
|
230
325
|
var slug = (t) => t.normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "").slice(0, 40) || "sala";
|
|
231
326
|
function roomFolder(room, title) {
|
|
232
327
|
const look = (dir, depth) => {
|
|
@@ -269,9 +364,78 @@ function linkFolder(folder, room) {
|
|
|
269
364
|
.vibebook
|
|
270
365
|
`);
|
|
271
366
|
}
|
|
367
|
+
function terminalApp() {
|
|
368
|
+
try {
|
|
369
|
+
for (const pid of execFileSync("pgrep", ["-x", "herdr"]).toString().split(/\s+/).filter(Boolean)) {
|
|
370
|
+
let p = pid;
|
|
371
|
+
for (let i = 0;i < 10 && p && p !== "1"; i++) {
|
|
372
|
+
const [ppid, ...cmd] = execFileSync("ps", ["-o", "ppid=,comm=", "-p", p]).toString().trim().split(/\s+/);
|
|
373
|
+
const app = cmd.join(" ").match(/^(.*?\.app)\/Contents\/MacOS\//)?.[1];
|
|
374
|
+
if (app)
|
|
375
|
+
return app;
|
|
376
|
+
p = ppid;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
} catch {}
|
|
380
|
+
}
|
|
381
|
+
function herdrServer(appMode = false) {
|
|
382
|
+
const allowed = new Set([readJson(SESSION, null)?.origin ?? DEFAULT_ORIGIN, DEFAULT_ORIGIN]);
|
|
383
|
+
const port = Number(process.env.VIBEBOOK_HERDR_PORT || 47321);
|
|
384
|
+
createServer((req, res) => {
|
|
385
|
+
const origin = req.headers.origin ?? "";
|
|
386
|
+
const send = (code, body) => {
|
|
387
|
+
res.writeHead(code, { "content-type": "application/json", ...allowed.has(origin) ? { "access-control-allow-origin": origin, "access-control-allow-private-network": "true", vary: "origin" } : {} });
|
|
388
|
+
res.end(JSON.stringify(body));
|
|
389
|
+
};
|
|
390
|
+
if (!allowed.has(origin) || req.headers.host !== `127.0.0.1:${port}`)
|
|
391
|
+
return send(403, { error: "origin" });
|
|
392
|
+
if (req.method === "OPTIONS")
|
|
393
|
+
return send(204, null);
|
|
394
|
+
const u = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
395
|
+
const map = readJson(appMode ? HERDR_APP : HERDR, {});
|
|
396
|
+
const target = (r) => appMode ? map[r]?.space : map[r]?.workspace;
|
|
397
|
+
if (u.pathname === "/ping")
|
|
398
|
+
return send(200, { ok: true, rooms: Object.keys(map).filter((r) => target(r)) });
|
|
399
|
+
if (u.pathname === "/where") {
|
|
400
|
+
const room = u.searchParams.get("room") ?? "";
|
|
401
|
+
if (!/^[a-z0-9]{6,40}$/.test(room))
|
|
402
|
+
return send(400, { error: "room" });
|
|
403
|
+
const folder = map[room]?.folder ?? roomFolder(room, (u.searchParams.get("title") ?? "").slice(0, 200));
|
|
404
|
+
const how = map[room]?.folder ? "used" : (() => {
|
|
405
|
+
try {
|
|
406
|
+
return linkedRoom(folder) === room;
|
|
407
|
+
} catch {
|
|
408
|
+
return false;
|
|
409
|
+
}
|
|
410
|
+
})() ? "linked" : existsSync(folder) ? "slug" : "new";
|
|
411
|
+
return send(200, { folder, home: homedir(), how });
|
|
412
|
+
}
|
|
413
|
+
if (u.pathname !== "/focus")
|
|
414
|
+
return send(404, { error: "not found" });
|
|
415
|
+
const ws = target(u.searchParams.get("room") ?? "");
|
|
416
|
+
if (!ws)
|
|
417
|
+
return send(404, { error: "Nenhum Claude aberto pra essa sala no herdr." });
|
|
418
|
+
try {
|
|
419
|
+
if (appMode) {
|
|
420
|
+
happ("do", "space.select", "--id", ws);
|
|
421
|
+
execFileSync("open", ["-a", "Herdr"]);
|
|
422
|
+
return send(200, { ok: true });
|
|
423
|
+
}
|
|
424
|
+
herdr("workspace", "focus", ws);
|
|
425
|
+
const app = terminalApp();
|
|
426
|
+
if (app)
|
|
427
|
+
execFileSync("open", ["-a", app]);
|
|
428
|
+
send(200, { ok: true });
|
|
429
|
+
} catch (e) {
|
|
430
|
+
send(500, { error: String(e.message ?? e) });
|
|
431
|
+
}
|
|
432
|
+
}).on("error", (e) => process.stderr.write(`vibebook herdr: botão "ir pro Claude" desligado (${e.message})
|
|
433
|
+
`)).listen(port, "127.0.0.1");
|
|
434
|
+
}
|
|
272
435
|
async function herdrSync(agent, tools) {
|
|
273
436
|
const res = await callTool("claude_rooms", {}, agent, tools);
|
|
274
437
|
const rooms = JSON.parse(res.content[0]?.text ?? "{}").rooms ?? [];
|
|
438
|
+
const deaf = new Set(rooms.filter((r) => r.on && r.listening === false).map((r) => r.room_id));
|
|
275
439
|
const map = readJson(HERDR, {});
|
|
276
440
|
const live = new Set(herdr("workspace", "list").workspaces.map((w) => w.workspace_id));
|
|
277
441
|
const panes = herdr("pane", "list").panes;
|
|
@@ -280,8 +444,39 @@ async function herdrSync(agent, tools) {
|
|
|
280
444
|
`);
|
|
281
445
|
for (const [room, title] of on) {
|
|
282
446
|
const e = map[room] ??= {};
|
|
283
|
-
if (e.workspace && live.has(e.workspace))
|
|
447
|
+
if (e.workspace && live.has(e.workspace)) {
|
|
448
|
+
if (!deaf.has(room))
|
|
449
|
+
delete e.deaf_since;
|
|
450
|
+
else
|
|
451
|
+
e.deaf_since ??= Date.now();
|
|
452
|
+
if (e.listening && e.deaf_since && Date.now() - e.deaf_since > 3 * 60000 && Date.now() - (e.prompted_at ?? 0) > 5 * 60000)
|
|
453
|
+
e.listening = false;
|
|
454
|
+
const cp = panes.find((x) => x.workspace_id === e.workspace && x.agent === "claude");
|
|
455
|
+
const cs = cp?.agent_session?.value;
|
|
456
|
+
const cx0 = cs ? sessionContext(cs) : undefined;
|
|
457
|
+
const cx = cx0 && cs ? { ...cx0, window: sessionStatus(cs)?.window ?? cx0.window } : undefined;
|
|
458
|
+
if (cp && cx && cx.tokens >= cx.window * COMPACT_AT && (cp.agent_status === "idle" || cp.agent_status === "done") && Date.now() - (e.compacted_at ?? 0) > 15 * 60000) {
|
|
459
|
+
try {
|
|
460
|
+
herdr("agent", "prompt", cp.pane_id, "/compact");
|
|
461
|
+
e.compacted_at = Date.now();
|
|
462
|
+
log(`${title}: contexto em ${Math.round(cx.tokens / cx.window * 100)}%, mandei /compact`);
|
|
463
|
+
} catch {}
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
if (!e.listening) {
|
|
467
|
+
const p = panes.find((x) => x.workspace_id === e.workspace && x.agent === "claude");
|
|
468
|
+
if (p && (p.agent_status === "idle" || p.agent_status === "done"))
|
|
469
|
+
try {
|
|
470
|
+
herdr("agent", "prompt", p.pane_id, LISTEN_PROMPT);
|
|
471
|
+
e.listening = true;
|
|
472
|
+
e.prompted_at = Date.now();
|
|
473
|
+
delete e.deaf_since;
|
|
474
|
+
log(`${title}: o Claude ficou livre, pedi pra ele ouvir a sala`);
|
|
475
|
+
} catch {}
|
|
476
|
+
}
|
|
284
477
|
continue;
|
|
478
|
+
}
|
|
479
|
+
e.listening = false;
|
|
285
480
|
e.folder ??= roomFolder(room, title);
|
|
286
481
|
linkFolder(e.folder, room);
|
|
287
482
|
const there = panes.find((p) => p.agent === "claude" && (p.cwd === e.folder || p.foreground_cwd === e.folder));
|
|
@@ -293,7 +488,9 @@ async function herdrSync(agent, tools) {
|
|
|
293
488
|
log(`${title}: adotei o Claude que já estava em ${e.folder}`);
|
|
294
489
|
if (there.agent_status === "idle" || there.agent_status === "done")
|
|
295
490
|
try {
|
|
296
|
-
herdr("agent", "prompt", there.pane_id,
|
|
491
|
+
herdr("agent", "prompt", there.pane_id, LISTEN_PROMPT);
|
|
492
|
+
e.listening = true;
|
|
493
|
+
e.prompted_at = Date.now();
|
|
297
494
|
} catch {}
|
|
298
495
|
continue;
|
|
299
496
|
}
|
|
@@ -302,6 +499,8 @@ async function herdrSync(agent, tools) {
|
|
|
302
499
|
const args = ["agent", "start", `vb-${room}`, "--kind", "claude", "--pane", w.root_pane.pane_id, "--timeout", "60000", "--"];
|
|
303
500
|
if (e.session)
|
|
304
501
|
args.push("--resume", e.session);
|
|
502
|
+
e.listening = true;
|
|
503
|
+
e.prompted_at = Date.now();
|
|
305
504
|
writePrivate(HERDR, map);
|
|
306
505
|
try {
|
|
307
506
|
herdr(...args, "/vibebook:listen");
|
|
@@ -325,11 +524,86 @@ async function herdrSync(agent, tools) {
|
|
|
325
524
|
writePrivate(HERDR, map);
|
|
326
525
|
herdrOrder([...on.keys()], map);
|
|
327
526
|
}
|
|
527
|
+
var HERDR_APP = join(HOME, "herdr-app.json");
|
|
528
|
+
var happ = (...args) => {
|
|
529
|
+
const r = JSON.parse(execFileSync("herdr-app", args, { stdio: ["ignore", "pipe", "pipe"] }).toString());
|
|
530
|
+
if (r?.error)
|
|
531
|
+
throw new Error(r.error);
|
|
532
|
+
return r;
|
|
533
|
+
};
|
|
534
|
+
var appClaude = (sp) => sp.tabs.flatMap((t) => t.terminals).find((t) => /claude/i.test(t.app ?? ""));
|
|
535
|
+
async function herdrAppSync(agent, tools) {
|
|
536
|
+
const res = await callTool("claude_rooms", {}, agent, tools);
|
|
537
|
+
const rooms = JSON.parse(res.content[0]?.text ?? "{}").rooms ?? [];
|
|
538
|
+
const deaf = new Set(rooms.filter((r) => r.on && r.listening === false).map((r) => r.room_id));
|
|
539
|
+
const map = readJson(HERDR_APP, {});
|
|
540
|
+
let spaces = happ("ls").spaces;
|
|
541
|
+
const live = new Set(spaces.map((x) => x.id));
|
|
542
|
+
const on = new Map(rooms.filter((r) => r.on).map((r) => [r.room_id, r.title]));
|
|
543
|
+
const log = (m) => process.stdout.write(`${new Date().toLocaleTimeString()} ${m}
|
|
544
|
+
`);
|
|
545
|
+
for (const [room, title] of on) {
|
|
546
|
+
const e = map[room] ??= {};
|
|
547
|
+
if (e.space && live.has(e.space)) {
|
|
548
|
+
if (!deaf.has(room))
|
|
549
|
+
delete e.deaf_since;
|
|
550
|
+
else
|
|
551
|
+
e.deaf_since ??= Date.now();
|
|
552
|
+
const c = appClaude(spaces.find((x) => x.id === e.space));
|
|
553
|
+
if (c && e.deaf_since && Date.now() - e.deaf_since > 3 * 60000 && Date.now() - (e.prompted_at ?? 0) > 5 * 60000 && (c.status === "idle" || c.status === "done"))
|
|
554
|
+
try {
|
|
555
|
+
execFileSync("hc", ["send", c.terminal, LISTEN_PROMPT], { stdio: "ignore" });
|
|
556
|
+
e.prompted_at = Date.now();
|
|
557
|
+
log(`${title}: ninguém ouvindo, pedi pro Claude ouvir a sala de novo`);
|
|
558
|
+
} catch {}
|
|
559
|
+
continue;
|
|
560
|
+
}
|
|
561
|
+
e.folder ??= roomFolder(room, title);
|
|
562
|
+
linkFolder(e.folder, room);
|
|
563
|
+
const there = spaces.find((x) => x.folder === e.folder && appClaude(x));
|
|
564
|
+
if (there) {
|
|
565
|
+
e.space = there.id;
|
|
566
|
+
try {
|
|
567
|
+
happ("do", "space.rename", "--id", there.id, "--name", title.slice(0, 40));
|
|
568
|
+
} catch {}
|
|
569
|
+
const c = appClaude(there);
|
|
570
|
+
if (c && (c.status === "idle" || c.status === "done"))
|
|
571
|
+
try {
|
|
572
|
+
execFileSync("hc", ["send", c.terminal, LISTEN_PROMPT], { stdio: "ignore" });
|
|
573
|
+
e.prompted_at = Date.now();
|
|
574
|
+
} catch {}
|
|
575
|
+
log(`${title}: adotei o Claude que já estava em ${e.folder}`);
|
|
576
|
+
continue;
|
|
577
|
+
}
|
|
578
|
+
const r = happ("do", "space.new", "--name", title.slice(0, 40), "--folder", e.folder, "--argv", JSON.stringify(["claude"]), "--task", "/vibebook:listen");
|
|
579
|
+
e.space = r.space;
|
|
580
|
+
e.prompted_at = Date.now();
|
|
581
|
+
log(`${title}: abri um Claude em ${e.folder}`);
|
|
582
|
+
}
|
|
583
|
+
for (const [room, e] of Object.entries(map)) {
|
|
584
|
+
if (on.has(room) || !e.space)
|
|
585
|
+
continue;
|
|
586
|
+
if (live.has(e.space)) {
|
|
587
|
+
happ("do", "space.close", "--id", e.space, "--confirmed", "true");
|
|
588
|
+
log(`${rooms.find((r) => r.room_id === room)?.title ?? room}: Claude desligado, fechei o space`);
|
|
589
|
+
}
|
|
590
|
+
delete e.space;
|
|
591
|
+
}
|
|
592
|
+
writePrivate(HERDR_APP, map);
|
|
593
|
+
spaces = happ("ls").spaces;
|
|
594
|
+
const want = [...on.keys()].map((r) => map[r]?.space).filter((x) => !!x && spaces.some((s) => s.id === x));
|
|
595
|
+
want.forEach((id, i) => {
|
|
596
|
+
if (spaces[i]?.id === id)
|
|
597
|
+
return;
|
|
598
|
+
happ("do", "space.move", "--id", id, "--index", String(i + 1));
|
|
599
|
+
spaces = happ("ls").spaces;
|
|
600
|
+
});
|
|
601
|
+
}
|
|
328
602
|
function herdrOrder(order, map) {
|
|
329
603
|
const list = herdr("workspace", "list").workspaces;
|
|
330
604
|
const ours = new Map(order.filter((r) => map[r]?.workspace).map((r) => [map[r].workspace, r]));
|
|
331
|
-
const current = list.
|
|
332
|
-
const desired = order.map((r) => map[r]?.workspace).filter((w) => !!w && current.includes(w));
|
|
605
|
+
const current = list.map((w) => w.workspace_id);
|
|
606
|
+
const desired = [...order.map((r) => map[r]?.workspace).filter((w) => !!w && current.includes(w)), ...current.filter((w) => !ours.has(w))];
|
|
333
607
|
let i = 0;
|
|
334
608
|
while (i < desired.length && current[i] === desired[i])
|
|
335
609
|
i++;
|
|
@@ -347,7 +621,7 @@ function herdrOrder(order, map) {
|
|
|
347
621
|
const tabLabel = tabs.find((t) => t.tab_id === p.tab_id)?.label ?? "1";
|
|
348
622
|
let r;
|
|
349
623
|
if (!newWs)
|
|
350
|
-
r = herdr("pane", "move", p.pane_id, "--new-workspace", "--label", list.find((w) => w.workspace_id === ws)?.label ?? room, "--tab-label", tabLabel);
|
|
624
|
+
r = herdr("pane", "move", p.pane_id, "--new-workspace", "--label", list.find((w) => w.workspace_id === ws)?.label ?? room ?? "1", "--tab-label", tabLabel);
|
|
351
625
|
else if (newTab.has(p.tab_id))
|
|
352
626
|
r = herdr("pane", "move", p.pane_id, "--tab", newTab.get(p.tab_id), "--split", "right", "--no-focus");
|
|
353
627
|
else
|
|
@@ -357,7 +631,8 @@ function herdrOrder(order, map) {
|
|
|
357
631
|
newTab.set(p.tab_id, moved.tab_id);
|
|
358
632
|
}
|
|
359
633
|
if (newWs) {
|
|
360
|
-
|
|
634
|
+
if (room)
|
|
635
|
+
map[room].workspace = newWs;
|
|
361
636
|
if (ws === focused)
|
|
362
637
|
refocus = newWs;
|
|
363
638
|
}
|
|
@@ -368,7 +643,7 @@ function herdrOrder(order, map) {
|
|
|
368
643
|
try {
|
|
369
644
|
herdr("workspace", "focus", back);
|
|
370
645
|
} catch {}
|
|
371
|
-
process.stdout.write(`${new Date().toLocaleTimeString()} reordenei ${desired.length - i} workspace(s) na ordem
|
|
646
|
+
process.stdout.write(`${new Date().toLocaleTimeString()} reordenei ${desired.length - i} workspace(s): salas na ordem do vibebook, depois o resto
|
|
372
647
|
`);
|
|
373
648
|
}
|
|
374
649
|
var HELP = `vibebook ${VERSION}: your vibebook rooms from the terminal (for you and your Claude).
|
|
@@ -384,11 +659,16 @@ var HELP = `vibebook ${VERSION}: your vibebook rooms from the terminal (for you
|
|
|
384
659
|
vibebook upload <file> [--message ".."] [--thread <epic>] [--room <id>]
|
|
385
660
|
vibebook herdr [--once] keep one Claude Code per room in herdr: opens it when you switch
|
|
386
661
|
"Meu Claude" on in the app, closes it when you switch it off
|
|
662
|
+
vibebook herdr --app the same in the Herdr desktop app: one space per room (not with the terminal one)
|
|
663
|
+
vibebook statusline [command] as Claude Code's statusLine: keeps the session's context window and plan
|
|
664
|
+
limits for the room, then runs your own statusline command (if any) with the same input
|
|
387
665
|
vibebook update install the latest version
|
|
388
666
|
|
|
389
667
|
Options: --model "Claude Opus 5.5" (or VIBEBOOK_MODEL) names the agent in the room; --origin for a local server.`;
|
|
390
668
|
async function main() {
|
|
391
669
|
const cmd = flags.version ? "version" : pos[0];
|
|
670
|
+
if (cmd === "statusline")
|
|
671
|
+
return statusline();
|
|
392
672
|
const root = repoRoot();
|
|
393
673
|
const agent = agentId(root);
|
|
394
674
|
switch (cmd) {
|
|
@@ -440,8 +720,9 @@ async function main() {
|
|
|
440
720
|
const res = await callTool("wait_for_messages", { timeout_seconds: 50, ...room ? { room_id: room } : {} }, agent, tools);
|
|
441
721
|
const out = JSON.parse(res.content[0]?.text ?? "{}");
|
|
442
722
|
if (out.new?.length) {
|
|
723
|
+
const title = (t) => (t ?? "").replace(/[\p{Cc}"`]/gu, " ").replace(/\s+/g, " ").trim().slice(0, 60);
|
|
443
724
|
for (const r of out.new)
|
|
444
|
-
process.stdout.write(`vibebook: ${r.new_messages} nova(s) em "${r.title}" room_id=${r.room_id}
|
|
725
|
+
process.stdout.write(`vibebook: ${r.new_messages} nova(s) em "${title(r.title)}" room_id=${r.room_id}
|
|
445
726
|
`);
|
|
446
727
|
return;
|
|
447
728
|
}
|
|
@@ -451,15 +732,43 @@ async function main() {
|
|
|
451
732
|
}
|
|
452
733
|
}
|
|
453
734
|
case "herdr": {
|
|
735
|
+
const appMode = !!flags.app;
|
|
454
736
|
try {
|
|
455
|
-
|
|
737
|
+
if (appMode)
|
|
738
|
+
happ("ls");
|
|
739
|
+
else
|
|
740
|
+
execFileSync("herdr", ["status"], { stdio: "ignore" });
|
|
456
741
|
} catch {
|
|
457
|
-
return die("herdr not found or not running (https://herdr.dev)");
|
|
742
|
+
return die(appMode ? "Herdr (the desktop app) isn't open, or herdr-app isn't installed (https://herdr.dev)" : "herdr not found or not running (https://herdr.dev)");
|
|
458
743
|
}
|
|
744
|
+
const lock = join(HOME, "herdr.pid");
|
|
745
|
+
const other = Number(existsSync(lock) ? readFileSync(lock, "utf8") : 0);
|
|
746
|
+
if (other && other !== process.pid) {
|
|
747
|
+
let alive = true;
|
|
748
|
+
try {
|
|
749
|
+
process.kill(other, 0);
|
|
750
|
+
} catch {
|
|
751
|
+
alive = false;
|
|
752
|
+
}
|
|
753
|
+
if (alive)
|
|
754
|
+
return die(`vibebook herdr is already running (pid ${other}); one per machine is enough`);
|
|
755
|
+
}
|
|
756
|
+
writeFileSync(lock, String(process.pid));
|
|
757
|
+
const release = () => {
|
|
758
|
+
try {
|
|
759
|
+
if (Number(readFileSync(lock, "utf8")) === process.pid)
|
|
760
|
+
unlinkSync(lock);
|
|
761
|
+
} catch {}
|
|
762
|
+
};
|
|
763
|
+
process.on("exit", release);
|
|
764
|
+
for (const sig of ["SIGINT", "SIGTERM", "SIGHUP"])
|
|
765
|
+
process.on(sig, () => process.exit(0));
|
|
459
766
|
const tools = await listTools(agent);
|
|
767
|
+
if (!flags.once)
|
|
768
|
+
herdrServer(appMode);
|
|
460
769
|
for (;; ) {
|
|
461
770
|
try {
|
|
462
|
-
await herdrSync(agent, tools);
|
|
771
|
+
await (appMode ? herdrAppSync : herdrSync)(agent, tools);
|
|
463
772
|
} catch (e) {
|
|
464
773
|
process.stderr.write(`vibebook herdr: ${String(e?.message ?? e)}
|
|
465
774
|
`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vibebook.dev",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
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" },
|