@open-bus/cli 0.0.0-stage → 0.42.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.
@@ -0,0 +1,68 @@
1
+ // OpenBus OpenCode TUI plugin: publishes which root sessions this TUI has
2
+ // opened, so the daemon delivers to a session only while a TUI has it.
3
+ // Ported from agent-bus's opencode presence plugin; only the home changed.
4
+ import fs from "node:fs";
5
+ import os from "node:os";
6
+ import path from "node:path";
7
+
8
+ const POLL_MS = 500;
9
+
10
+ function setup(api) {
11
+ try {
12
+ const home = path.resolve(
13
+ process.env.OPENBUS_HOME || path.join(os.homedir(), ".openbus"),
14
+ );
15
+ const dir = path.join(home, "opencode-tui");
16
+ const file = path.join(dir, `${process.pid}.json`);
17
+ const roots = new Set();
18
+
19
+ const root = (id) => {
20
+ const seen = new Set();
21
+ while (typeof id === "string" && !seen.has(id)) {
22
+ seen.add(id);
23
+ const session = api.data.session.get(id);
24
+ if (!session) return undefined;
25
+ if (!session.parentID) return id;
26
+ id = session.parentID;
27
+ }
28
+ return undefined;
29
+ };
30
+
31
+ // True once the file lists `ids`; false on any fs error, so the caller
32
+ // records nothing and the next tick tries again.
33
+ const write = (ids) => {
34
+ try {
35
+ fs.mkdirSync(dir, { recursive: true });
36
+ const tmp = `${file}.${process.pid}.tmp`;
37
+ const body = {
38
+ pid: process.pid,
39
+ sessionIDs: ids,
40
+ updatedAt: new Date().toISOString(),
41
+ };
42
+ fs.writeFileSync(tmp, JSON.stringify(body));
43
+ fs.renameSync(tmp, file);
44
+ return true;
45
+ } catch {
46
+ return false;
47
+ }
48
+ };
49
+
50
+ const tick = () => {
51
+ try {
52
+ const route = api.ui.router.current();
53
+ if (route?.type !== "session") return;
54
+ const id = root(route.sessionID);
55
+ if (id && !roots.has(id) && write([...roots, id])) roots.add(id);
56
+ } catch {}
57
+ };
58
+
59
+ process.once("exit", () => {
60
+ try {
61
+ fs.rmSync(file, { force: true });
62
+ } catch {}
63
+ });
64
+ setInterval(tick, POLL_MS).unref?.();
65
+ } catch {}
66
+ }
67
+
68
+ export default { id: "openbus.opencode.presence", setup };