agents-deck 1.21.0 → 1.23.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.
@@ -5,8 +5,8 @@
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1" />
6
6
  <title>agents-deck</title>
7
7
  <link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Ctext y='84' font-size='84'%3E%E2%97%89%3C/text%3E%3C/svg%3E" />
8
- <script type="module" crossorigin src="/assets/index-B-TDYmpb.js"></script>
9
- <link rel="stylesheet" crossorigin href="/assets/index-BDP3vsJ3.css">
8
+ <script type="module" crossorigin src="/assets/index-DK3danWy.js"></script>
9
+ <link rel="stylesheet" crossorigin href="/assets/index-BRoNoC-Z.css">
10
10
  </head>
11
11
  <body>
12
12
  <div id="root"></div>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agents-deck",
3
- "version": "1.21.0",
3
+ "version": "1.23.0",
4
4
  "description": "Live deck of Claude Code and Codex agents — watch parallel subagents fork, call tools, and return on one calm canvas.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -0,0 +1,217 @@
1
+ // Fetches historical usage from the `ccusage` CLI (https://github.com/ccusage/ccusage).
2
+ // ccusage reads the local ~/.claude (and other agent) logs and reports cost +
3
+ // token usage grouped by day.
4
+ //
5
+ // Performance: we do NOT run `npx -y ccusage@latest` on every call — that hits
6
+ // the npm registry to resolve `@latest` (and re-downloads when the npx cache is
7
+ // cold), so each modal open waited seconds. Instead we keep our OWN managed
8
+ // install under ~/.agents-deck/ccusage and invoke it directly with
9
+ // `node <pkg>/src/cli.js` (no npx, no registry round-trip). A throttled
10
+ // once-per-day background check upgrades it when a newer ccusage ships, while
11
+ // the current call always serves from the already-installed copy. If the
12
+ // managed install is missing/broken we fall back to the old npx path so the
13
+ // feature still works on a fresh machine.
14
+ import { spawn, spawnSync } from "node:child_process";
15
+ import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
16
+ import path from "node:path";
17
+ import os from "node:os";
18
+
19
+ const CACHE_MS = 120_000; // 2 min — modal is manual-open; cheap to keep warm
20
+ const TIMEOUT_MS = 90_000;
21
+ const INSTALL_TIMEOUT_MS = 120_000; // first-run npm install can be slow
22
+ const UPDATE_CHECK_MS = 24 * 3600_000; // check npm for a newer ccusage once/day
23
+
24
+ const CACHE_DIR = path.join(os.homedir(), ".agents-deck", "ccusage");
25
+ const PKG_DIR = path.join(CACHE_DIR, "node_modules", "ccusage");
26
+ const MARKER = path.join(CACHE_DIR, ".last-update-check");
27
+
28
+ const _cache = new Map(); // key `${since}|${until}` → { result, at }
29
+
30
+ let _installing = null; // Promise guard so concurrent calls share one install
31
+ let _checkedThisRun = false; // only kick the daily check once per process boot
32
+
33
+ // ── managed install ─────────────────────────────────────────────────────────
34
+
35
+ // Absolute path to ccusage's CLI entry inside our managed install, or null if
36
+ // not installed. Reads the package's `bin` field (currently "./src/cli.js").
37
+ function resolveEntry() {
38
+ try {
39
+ const pkg = JSON.parse(readFileSync(path.join(PKG_DIR, "package.json"), "utf8"));
40
+ let rel = pkg.bin;
41
+ if (rel && typeof rel === "object") rel = rel.ccusage ?? Object.values(rel)[0];
42
+ if (typeof rel !== "string") return null;
43
+ const entry = path.join(PKG_DIR, rel);
44
+ return existsSync(entry) ? { entry, version: pkg.version } : null;
45
+ } catch {
46
+ return null;
47
+ }
48
+ }
49
+
50
+ // Run `npm install ccusage@<spec> --prefix CACHE_DIR`. Synchronous variant for
51
+ // the first-run cold path (we must have a binary before we can answer).
52
+ function installSync(spec = "latest") {
53
+ mkdirSync(CACHE_DIR, { recursive: true });
54
+ const r = spawnSync(
55
+ "npm",
56
+ ["install", `ccusage@${spec}`, "--prefix", CACHE_DIR,
57
+ "--no-save", "--no-audit", "--no-fund", "--loglevel", "error"],
58
+ { shell: true, windowsHide: true, timeout: INSTALL_TIMEOUT_MS, encoding: "utf8" },
59
+ );
60
+ if (r.status !== 0) {
61
+ throw new Error(`npm install ccusage failed: ${(r.stderr || "").trim() || r.status}`);
62
+ }
63
+ }
64
+
65
+ // Background, non-blocking install (used by the daily update path).
66
+ function installAsync(spec = "latest") {
67
+ try {
68
+ mkdirSync(CACHE_DIR, { recursive: true });
69
+ const child = spawn(
70
+ "npm",
71
+ ["install", `ccusage@${spec}`, "--prefix", CACHE_DIR,
72
+ "--no-save", "--no-audit", "--no-fund", "--loglevel", "error"],
73
+ { shell: true, windowsHide: true, detached: false, stdio: "ignore" },
74
+ );
75
+ child.on("error", () => {});
76
+ child.unref?.();
77
+ } catch { /* best-effort */ }
78
+ }
79
+
80
+ // True at most once per UPDATE_CHECK_MS, gated by the marker file's mtime so the
81
+ // throttle survives restarts.
82
+ function updateCheckDue() {
83
+ try {
84
+ return Date.now() - statSync(MARKER).mtimeMs > UPDATE_CHECK_MS;
85
+ } catch {
86
+ return true; // no marker yet → due
87
+ }
88
+ }
89
+ // (Re)write the marker so its mtime marks "now" as the last check time.
90
+ function touchMarker() {
91
+ try {
92
+ mkdirSync(CACHE_DIR, { recursive: true });
93
+ writeFileSync(MARKER, String(Date.now()));
94
+ } catch { /* ignore */ }
95
+ }
96
+
97
+ // Non-blocking: compare installed version to npm `latest`; install if newer.
98
+ function maybeBackgroundUpdate(installedVersion) {
99
+ if (_checkedThisRun || !updateCheckDue()) return;
100
+ _checkedThisRun = true;
101
+ touchMarker();
102
+ try {
103
+ const child = spawn("npm", ["view", "ccusage", "version"],
104
+ { shell: true, windowsHide: true });
105
+ let out = "";
106
+ child.stdout.on("data", d => { out += d; });
107
+ child.on("error", () => {});
108
+ child.on("close", () => {
109
+ const latest = out.trim();
110
+ if (latest && latest !== installedVersion) installAsync(latest);
111
+ });
112
+ } catch { /* ignore */ }
113
+ }
114
+
115
+ // Ensure a runnable ccusage. Returns { kind:"node", entry } for the managed
116
+ // install, or { kind:"npx" } as the portable fallback.
117
+ async function getRunner() {
118
+ let resolved = resolveEntry();
119
+ if (resolved) {
120
+ maybeBackgroundUpdate(resolved.version);
121
+ return { kind: "node", entry: resolved.entry };
122
+ }
123
+ // Cold: install once (deduped across concurrent callers).
124
+ if (!_installing) {
125
+ _installing = (async () => { installSync("latest"); })()
126
+ .catch(e => { console.error("agents-deck ccusage: install failed:", e?.message ?? e); })
127
+ .finally(() => { _installing = null; });
128
+ }
129
+ await _installing;
130
+ resolved = resolveEntry();
131
+ if (resolved) { touchMarker(); return { kind: "node", entry: resolved.entry }; }
132
+ return { kind: "npx" }; // npm unavailable / offline → fall back to npx
133
+ }
134
+
135
+ // ── invocation ──────────────────────────────────────────────────────────────
136
+
137
+ // Run ccusage with the given args, resolve raw stdout.
138
+ async function runCcusage(args) {
139
+ const runner = await getRunner();
140
+ const [cmd, full] = runner.kind === "node"
141
+ ? [process.execPath, [runner.entry, ...args]]
142
+ : ["npx", ["-y", "ccusage@latest", ...args]];
143
+ return new Promise((resolve, reject) => {
144
+ const child = spawn(cmd, full, {
145
+ // node path needs no shell; npx fallback does (Windows .cmd shim).
146
+ shell: runner.kind === "npx",
147
+ windowsHide: true,
148
+ });
149
+ let out = "", err = "";
150
+ const timer = setTimeout(() => {
151
+ child.kill();
152
+ reject(new Error("ccusage timed out"));
153
+ }, TIMEOUT_MS);
154
+ child.stdout.on("data", d => { out += d; });
155
+ child.stderr.on("data", d => { err += d; });
156
+ child.on("error", e => { clearTimeout(timer); reject(e); });
157
+ child.on("close", code => {
158
+ clearTimeout(timer);
159
+ if (code === 0) resolve(out);
160
+ else reject(new Error(err.trim() || `ccusage exited ${code}`));
161
+ });
162
+ });
163
+ }
164
+
165
+ // ccusage prints the JSON object somewhere in stdout; slice first { to last }.
166
+ function extractJson(out) {
167
+ const start = out.indexOf("{");
168
+ const end = out.lastIndexOf("}");
169
+ if (start === -1 || end === -1) throw new Error("no JSON in ccusage output");
170
+ return JSON.parse(out.slice(start, end + 1));
171
+ }
172
+
173
+ // YYYYMMDD for the CLI's --since/--until.
174
+ function toCliDate(d) {
175
+ return d.toISOString().slice(0, 10).replace(/-/g, "");
176
+ }
177
+
178
+ /**
179
+ * Fetch daily usage from ccusage for a date range.
180
+ * @param {{ since?: string, until?: string, force?: boolean }} opts
181
+ * since/until are YYYYMMDD strings (CLI format). Defaults to last 30 days.
182
+ * @returns {{ ok, days, totals, since, until, fetchedAt } | { ok:false, error }}
183
+ */
184
+ export async function fetchCcusageDaily({ since, until, force = false } = {}) {
185
+ const now = Date.now();
186
+ const sinceArg = since || toCliDate(new Date(now - 30 * 86400_000));
187
+ const key = `${sinceArg}|${until ?? ""}`;
188
+
189
+ const cached = _cache.get(key);
190
+ if (!force && cached && now - cached.at < CACHE_MS) return cached.result;
191
+
192
+ let result;
193
+ try {
194
+ const args = ["daily", "--json", "--since", sinceArg];
195
+ if (until) args.push("--until", until);
196
+ const raw = extractJson(await runCcusage(args));
197
+ const days = Array.isArray(raw.daily) ? raw.daily : [];
198
+ result = {
199
+ ok: true,
200
+ days,
201
+ totals: raw.totals ?? null,
202
+ since: sinceArg,
203
+ until: until ?? null,
204
+ fetchedAt: now,
205
+ };
206
+ } catch (err) {
207
+ console.error("agents-deck ccusage: fetch failed:", err?.message ?? err);
208
+ result = { ok: false, error: String(err?.message ?? err), fetchedAt: now };
209
+ }
210
+
211
+ _cache.set(key, { result, at: now });
212
+ return result;
213
+ }
214
+
215
+ export function invalidateCcusageCache() {
216
+ _cache.clear();
217
+ }
@@ -1023,6 +1023,18 @@ async function handleCodexQuota(req, res) {
1023
1023
  send(res, 200, quota);
1024
1024
  }
1025
1025
 
1026
+ async function handleCcusage(req, res) {
1027
+ const { fetchCcusageDaily } = await import(
1028
+ pathToFileURL(join(PKG_ROOT, "src/server/ccusage.mjs")).href
1029
+ );
1030
+ const url = new URL(req.url, "http://localhost");
1031
+ const force = url.searchParams.get("refresh") === "1";
1032
+ const since = url.searchParams.get("since") || undefined;
1033
+ const until = url.searchParams.get("until") || undefined;
1034
+ const data = await fetchCcusageDaily({ since, until, force });
1035
+ send(res, 200, data);
1036
+ }
1037
+
1026
1038
  function handleHealth(_req, res) {
1027
1039
  send(res, 200, {
1028
1040
  ok: true,
@@ -1092,6 +1104,7 @@ export async function startServer({ port = 4317, host = "127.0.0.1", persist = n
1092
1104
  if (req.method === "GET" && url.pathname === "/api/quota") return handleQuota(req, res);
1093
1105
  if (req.method === "GET" && url.pathname === "/api/codex-usage") return handleCodexUsage(req, res);
1094
1106
  if (req.method === "GET" && url.pathname === "/api/codex-quota") return handleCodexQuota(req, res);
1107
+ if (req.method === "GET" && url.pathname === "/api/ccusage") return handleCcusage(req, res);
1095
1108
 
1096
1109
  if (req.method === "GET" && url.pathname === "/api/events") {
1097
1110
  const since = Number(url.searchParams.get("since") ?? 0);