ai-battery 0.1.20 → 0.1.21

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.
@@ -1,37 +1,37 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" width="1040" height="150" viewBox="0 0 1040 150" role="img" aria-labelledby="title desc">
2
- <title id="title">Claude statusLine preview</title>
3
- <desc id="desc">A two-line Claude statusLine preview showing context remaining and colored AI Battery bars.</desc>
4
- <rect width="1040" height="150" rx="8" fill="#111318"/>
5
- <rect x="1" y="1" width="1038" height="148" rx="7" fill="none" stroke="#2a2f3a"/>
6
- <defs>
7
- <pattern id="greenShade" width="4" height="4" patternUnits="userSpaceOnUse">
8
- <rect width="4" height="4" fill="#18251d"/>
9
- <circle cx="1" cy="1" r="0.7" fill="#315c3d"/>
10
- <circle cx="3" cy="3" r="0.7" fill="#315c3d"/>
11
- </pattern>
12
- </defs>
13
- <g font-family="ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace" font-size="18">
14
- <text x="24" y="54" fill="#8b949e">Opus high · ~/Projects · main</text>
15
- <text x="828" y="54" fill="#8b949e">83% context left</text>
16
- <text x="24" y="105" fill="#8b949e">Codex</text>
17
- <g shape-rendering="crispEdges">
18
- <rect x="92" y="88" width="100" height="18" fill="url(#greenShade)"/>
19
- <rect x="92" y="88" width="70" height="18" fill="#4ade80"/>
20
- </g>
21
- <text x="204" y="105" fill="#8b949e">71%</text>
22
- <text x="250" y="105" fill="#8b949e">│</text>
23
- <text x="272" y="105" fill="#8b949e">5h 00:47</text>
24
- <text x="365" y="105" fill="#8b949e">│</text>
25
- <text x="388" y="105" fill="#8b949e">7d 90%</text>
26
- <text x="500" y="105" fill="#f2f2f2">Claude</text>
27
- <g shape-rendering="crispEdges">
28
- <rect x="585" y="88" width="100" height="18" fill="url(#greenShade)"/>
29
- <rect x="585" y="88" width="80" height="18" fill="#4ade80"/>
30
- </g>
31
- <text x="697" y="105" fill="#f2f2f2">76%</text>
32
- <text x="743" y="105" fill="#8b949e">│</text>
33
- <text x="765" y="105" fill="#f2f2f2">5h 00:47</text>
34
- <text x="858" y="105" fill="#8b949e">│</text>
35
- <text x="881" y="105" fill="#f2f2f2">7d 59%</text>
36
- </g>
37
- </svg>
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="1040" height="150" viewBox="0 0 1040 150" role="img" aria-labelledby="title desc">
2
+ <title id="title">Claude statusLine preview</title>
3
+ <desc id="desc">A two-line Claude statusLine preview showing context remaining and colored AI Battery bars.</desc>
4
+ <rect width="1040" height="150" rx="8" fill="#111318"/>
5
+ <rect x="1" y="1" width="1038" height="148" rx="7" fill="none" stroke="#2a2f3a"/>
6
+ <defs>
7
+ <pattern id="greenShade" width="4" height="4" patternUnits="userSpaceOnUse">
8
+ <rect width="4" height="4" fill="#18251d"/>
9
+ <circle cx="1" cy="1" r="0.7" fill="#315c3d"/>
10
+ <circle cx="3" cy="3" r="0.7" fill="#315c3d"/>
11
+ </pattern>
12
+ </defs>
13
+ <g font-family="ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace" font-size="18">
14
+ <text x="24" y="54" fill="#8b949e">Opus high · ~/Projects · main</text>
15
+ <text x="828" y="54" fill="#8b949e">83% context left</text>
16
+ <text x="24" y="105" fill="#8b949e">Codex</text>
17
+ <g shape-rendering="crispEdges">
18
+ <rect x="92" y="88" width="100" height="18" fill="url(#greenShade)"/>
19
+ <rect x="92" y="88" width="70" height="18" fill="#4ade80"/>
20
+ </g>
21
+ <text x="204" y="105" fill="#8b949e">71%</text>
22
+ <text x="250" y="105" fill="#8b949e">│</text>
23
+ <text x="272" y="105" fill="#8b949e">5h 00:47</text>
24
+ <text x="365" y="105" fill="#8b949e">│</text>
25
+ <text x="388" y="105" fill="#8b949e">7d 90%</text>
26
+ <text x="500" y="105" fill="#f2f2f2">Claude</text>
27
+ <g shape-rendering="crispEdges">
28
+ <rect x="585" y="88" width="100" height="18" fill="url(#greenShade)"/>
29
+ <rect x="585" y="88" width="80" height="18" fill="#4ade80"/>
30
+ </g>
31
+ <text x="697" y="105" fill="#f2f2f2">76%</text>
32
+ <text x="743" y="105" fill="#8b949e">│</text>
33
+ <text x="765" y="105" fill="#f2f2f2">5h 00:47</text>
34
+ <text x="858" y="105" fill="#8b949e">│</text>
35
+ <text x="881" y="105" fill="#f2f2f2">7d 59%</text>
36
+ </g>
37
+ </svg>
@@ -1,44 +1,44 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" width="1040" height="118" viewBox="0 0 1040 118" role="img" aria-labelledby="title desc">
2
- <title id="title">AI Battery terminal preview</title>
3
- <desc id="desc">A terminal preview showing Codex with a green battery bar and Claude with a red battery bar.</desc>
4
- <rect width="1040" height="118" rx="8" fill="#111318"/>
5
- <rect x="1" y="1" width="1038" height="116" rx="7" fill="none" stroke="#2a2f3a"/>
6
- <defs>
7
- <pattern id="greenShade" width="4" height="4" patternUnits="userSpaceOnUse">
8
- <rect width="4" height="4" fill="#18251d"/>
9
- <circle cx="1" cy="1" r="0.7" fill="#315c3d"/>
10
- <circle cx="3" cy="3" r="0.7" fill="#315c3d"/>
11
- </pattern>
12
- <pattern id="redShade" width="4" height="4" patternUnits="userSpaceOnUse">
13
- <rect width="4" height="4" fill="#29191d"/>
14
- <circle cx="1" cy="1" r="0.7" fill="#7f3038"/>
15
- <circle cx="3" cy="3" r="0.7" fill="#7f3038"/>
16
- </pattern>
17
- </defs>
18
- <circle cx="24" cy="22" r="5" fill="#ff5f57"/>
19
- <circle cx="42" cy="22" r="5" fill="#ffbd2e"/>
20
- <circle cx="60" cy="22" r="5" fill="#28c840"/>
21
- <g font-family="ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace" font-size="18">
22
- <text x="24" y="70" fill="#f2f2f2">Codex</text>
23
- <g shape-rendering="crispEdges">
24
- <rect x="92" y="53" width="100" height="18" fill="url(#greenShade)"/>
25
- <rect x="92" y="53" width="90" height="18" fill="#4ade80"/>
26
- </g>
27
- <text x="204" y="70" fill="#f2f2f2">86%</text>
28
- <text x="250" y="70" fill="#8b949e">│</text>
29
- <text x="272" y="70" fill="#f2f2f2">5h 18:09</text>
30
- <text x="365" y="70" fill="#8b949e">│</text>
31
- <text x="388" y="70" fill="#f2f2f2">7d 82%</text>
32
- <text x="480" y="70" fill="#8b949e">┃</text>
33
- <text x="505" y="70" fill="#8b949e">Claude</text>
34
- <g shape-rendering="crispEdges">
35
- <rect x="590" y="53" width="100" height="18" fill="url(#redShade)"/>
36
- <rect x="590" y="53" width="10" height="18" fill="#ff5c5c"/>
37
- </g>
38
- <text x="702" y="70" fill="#8b949e">4%</text>
39
- <text x="748" y="70" fill="#8b949e">│</text>
40
- <text x="770" y="70" fill="#8b949e">5h 18:10</text>
41
- <text x="863" y="70" fill="#8b949e">│</text>
42
- <text x="886" y="70" fill="#8b949e">7d 71%</text>
43
- </g>
44
- </svg>
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="1040" height="118" viewBox="0 0 1040 118" role="img" aria-labelledby="title desc">
2
+ <title id="title">AI Battery terminal preview</title>
3
+ <desc id="desc">A terminal preview showing Codex with a green battery bar and Claude with a red battery bar.</desc>
4
+ <rect width="1040" height="118" rx="8" fill="#111318"/>
5
+ <rect x="1" y="1" width="1038" height="116" rx="7" fill="none" stroke="#2a2f3a"/>
6
+ <defs>
7
+ <pattern id="greenShade" width="4" height="4" patternUnits="userSpaceOnUse">
8
+ <rect width="4" height="4" fill="#18251d"/>
9
+ <circle cx="1" cy="1" r="0.7" fill="#315c3d"/>
10
+ <circle cx="3" cy="3" r="0.7" fill="#315c3d"/>
11
+ </pattern>
12
+ <pattern id="redShade" width="4" height="4" patternUnits="userSpaceOnUse">
13
+ <rect width="4" height="4" fill="#29191d"/>
14
+ <circle cx="1" cy="1" r="0.7" fill="#7f3038"/>
15
+ <circle cx="3" cy="3" r="0.7" fill="#7f3038"/>
16
+ </pattern>
17
+ </defs>
18
+ <circle cx="24" cy="22" r="5" fill="#ff5f57"/>
19
+ <circle cx="42" cy="22" r="5" fill="#ffbd2e"/>
20
+ <circle cx="60" cy="22" r="5" fill="#28c840"/>
21
+ <g font-family="ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace" font-size="18">
22
+ <text x="24" y="70" fill="#f2f2f2">Codex</text>
23
+ <g shape-rendering="crispEdges">
24
+ <rect x="92" y="53" width="100" height="18" fill="url(#greenShade)"/>
25
+ <rect x="92" y="53" width="90" height="18" fill="#4ade80"/>
26
+ </g>
27
+ <text x="204" y="70" fill="#f2f2f2">86%</text>
28
+ <text x="250" y="70" fill="#8b949e">│</text>
29
+ <text x="272" y="70" fill="#f2f2f2">5h 18:09</text>
30
+ <text x="365" y="70" fill="#8b949e">│</text>
31
+ <text x="388" y="70" fill="#f2f2f2">7d 82%</text>
32
+ <text x="480" y="70" fill="#8b949e">┃</text>
33
+ <text x="505" y="70" fill="#8b949e">Claude</text>
34
+ <g shape-rendering="crispEdges">
35
+ <rect x="590" y="53" width="100" height="18" fill="url(#redShade)"/>
36
+ <rect x="590" y="53" width="10" height="18" fill="#ff5c5c"/>
37
+ </g>
38
+ <text x="702" y="70" fill="#8b949e">4%</text>
39
+ <text x="748" y="70" fill="#8b949e">│</text>
40
+ <text x="770" y="70" fill="#8b949e">5h 18:10</text>
41
+ <text x="863" y="70" fill="#8b949e">│</text>
42
+ <text x="886" y="70" fill="#8b949e">7d 71%</text>
43
+ </g>
44
+ </svg>
package/lib/cli.js CHANGED
@@ -2640,10 +2640,11 @@ function readScanCache(name, maxAgeSeconds) {
2640
2640
  return cached;
2641
2641
  }
2642
2642
 
2643
- function writeScanCache(name, value) {
2643
+ function writeScanCache(name, value, metadata = {}) {
2644
2644
  try {
2645
2645
  writeJsonAtomic(scanCachePath(name), {
2646
2646
  version: SCAN_CACHE_VERSION,
2647
+ ...metadata,
2647
2648
  capturedAt: new Date().toISOString(),
2648
2649
  value: value ?? null
2649
2650
  });
@@ -3473,11 +3474,20 @@ function uninstallClaudeStatusline(options = {}) {
3473
3474
 
3474
3475
  function readCodex(options = {}) {
3475
3476
  const maxAgeSeconds = options.maxAgeSeconds ?? 4;
3476
- let scan = readScanCache("codex-status", scanCacheSeconds(maxAgeSeconds))?.value;
3477
+ const cached = readScanCache("codex-status", scanCacheSeconds(maxAgeSeconds));
3478
+ const roots = [codexRuntimeHome()];
3479
+ const files = codexSessionFiles(roots);
3480
+ const inputFingerprint = crypto.createHash("sha256")
3481
+ .update(JSON.stringify([roots, files.map((file) => [file.path, file.size, file.mtimeMs])]))
3482
+ .digest("hex");
3483
+ // Changed usage logs must be visible on the next poll, even within the TTL.
3484
+ let scan = cached?.value;
3485
+ if (cached?.inputFingerprint && cached.inputFingerprint !== inputFingerprint) scan = null;
3486
+ if (!options.cachedOnly && cached?.inputFingerprint !== inputFingerprint) scan = null;
3477
3487
  if (!scan || typeof scan !== "object") {
3478
3488
  if (options.cachedOnly) return null;
3479
- scan = scanCodexStatus();
3480
- writeScanCache("codex-status", scan);
3489
+ scan = scanCodexStatus(files, roots);
3490
+ writeScanCache("codex-status", scan, { inputFingerprint });
3481
3491
  }
3482
3492
  writeCodexAccountUsage(scan);
3483
3493
  scan = mergeCodexAccountUsage(scan, readCodexAccountUsage());
@@ -3512,14 +3522,13 @@ function readCodexForClaudeCapture() {
3512
3522
  });
3513
3523
  }
3514
3524
 
3515
- function scanCodexStatus() {
3516
- // CODEX_HOME belongs to Codex itself and determines where /resume looks.
3517
- // Never reinterpret it as a list or redirect it for cross-environment usage.
3518
- const roots = [codexRuntimeHome()];
3519
- const files = prioritizeCodexSessionFiles(roots
3525
+ function codexSessionFiles(roots) {
3526
+ return prioritizeCodexSessionFiles(roots
3520
3527
  .flatMap((root) => listJsonlFiles(path.join(root, "sessions")))
3521
3528
  .sort((a, b) => b.mtimeMs - a.mtimeMs));
3529
+ }
3522
3530
 
3531
+ function scanCodexStatus(files, roots) {
3523
3532
  if (!files.length) {
3524
3533
  return {
3525
3534
  provider: "codex",
@@ -4389,7 +4398,10 @@ function collect(args) {
4389
4398
  const results = [];
4390
4399
  const includeHidden = args.provider !== "all";
4391
4400
  if ((args.provider === "all" || args.provider === "codex") && (includeHidden || providerVisible("codex"))) {
4392
- results.push(readCodex({ runningOverride: runningOverrideForProvider(args, "codex") }));
4401
+ results.push(readCodex({
4402
+ runningOverride: runningOverrideForProvider(args, "codex"),
4403
+ includeRunning: args.activeProvider !== "codex"
4404
+ }));
4393
4405
  }
4394
4406
  if ((args.provider === "all" || args.provider === "claude") && (includeHidden || providerVisible("claude"))) {
4395
4407
  results.push(readClaude({ runningOverride: runningOverrideForProvider(args, "claude") }));
@@ -338,10 +338,13 @@ function scanProcessCommands() {
338
338
  // WSL shares a visible desktop with native Windows apps. Pull in the
339
339
  // Windows process list too, while keeping the Linux /proc scan as the
340
340
  // source for WSL TTY ownership.
341
- commands = [
342
- ...commands,
343
- ...(listWindowsProcessCommands() || [])
344
- ];
341
+ const cached = readScanCache("wsl-windows-processes", scanCacheSeconds(3));
342
+ let windowsCommands = cached?.value;
343
+ if (!Array.isArray(windowsCommands)) {
344
+ windowsCommands = listWindowsProcessCommands() || [];
345
+ writeScanCache("wsl-windows-processes", windowsCommands);
346
+ }
347
+ commands = [...commands, ...windowsCommands];
345
348
  }
346
349
  }
347
350
 
@@ -60,7 +60,7 @@ function debugLog(event, details = {}) {
60
60
 
61
61
  function parseArgs(argv) {
62
62
  const args = {
63
- interval: Number(process.env.AI_BATTERY_INTERVAL || process.env.CLAUDEX_BATTERY_INTERVAL || 10),
63
+ interval: Number(process.env.AI_BATTERY_INTERVAL || process.env.CLAUDEX_BATTERY_INTERVAL || 2),
64
64
  barWidth: process.env.AI_BATTERY_BAR_WIDTH || process.env.CLAUDEX_BATTERY_BAR_WIDTH || "10",
65
65
  provider: process.env.AI_BATTERY_PROVIDER || process.env.CLAUDEX_BATTERY_PROVIDER || "auto",
66
66
  layout: normalizeLayout(process.env.AI_BATTERY_WIN_LAYOUT || process.env.CLAUDEX_BATTERY_WIN_LAYOUT || process.env.AI_BATTERY_LAYOUT),
@@ -106,7 +106,7 @@ function parseArgs(argv) {
106
106
  usage();
107
107
  process.exit(2);
108
108
  }
109
- if (!Number.isFinite(args.interval)) args.interval = 10;
109
+ if (!Number.isFinite(args.interval)) args.interval = 2;
110
110
  args.layout = normalizeLayout(args.layout);
111
111
  return args;
112
112
  }
@@ -345,7 +345,7 @@ function conPtyRepaintIntervalMs() {
345
345
  function statusCommandTimeoutMs() {
346
346
  const raw = Number(process.env.AI_BATTERY_STATUS_TIMEOUT_MS || process.env.CLAUDEX_BATTERY_STATUS_TIMEOUT_MS);
347
347
  if (Number.isFinite(raw)) return Math.max(1000, Math.min(15000, Math.floor(raw)));
348
- return 5000;
348
+ return 10000;
349
349
  }
350
350
 
351
351
  function outputMayClearDisplay(data) {
@@ -376,6 +376,13 @@ class StatusLine {
376
376
  this.refreshInFlight = false;
377
377
  this.refreshQueued = false;
378
378
  this.disposed = false;
379
+ this.refreshChild = null;
380
+ this.refreshTimeout = null;
381
+ this.retryTimer = null;
382
+ this.lastReading = "";
383
+ this.fetchFailures = 0;
384
+ this.command = options.command;
385
+ this.fetchTimeoutMs = options.fetchTimeoutMs ?? statusCommandTimeoutMs();
379
386
  this.onRefresh = null;
380
387
  this.resize();
381
388
  }
@@ -403,20 +410,31 @@ class StatusLine {
403
410
  }
404
411
 
405
412
  this.refreshInFlight = true;
413
+ clearTimeout(this.retryTimer);
414
+ this.retryTimer = null;
406
415
  this.nextFetch = now + (this.args.interval * 1000);
407
416
  const maxWidth = Math.max(20, this.cols - columnGuard());
408
- const command = batteryCommand(this.args, this.activeProvider, maxWidth);
417
+ const command = this.command ?? batteryCommand(this.args, this.activeProvider, maxWidth);
409
418
  const child = spawn(command[0], command.slice(1), {
410
419
  stdio: ["ignore", "pipe", "ignore"],
411
420
  windowsHide: true
412
421
  });
422
+ this.refreshChild = child;
413
423
 
414
424
  let stdout = "";
415
- const timeout = setTimeout(() => child.kill(), statusCommandTimeoutMs());
425
+ let failure = "";
426
+ const timeout = setTimeout(() => {
427
+ failure = "timeout";
428
+ child.kill();
429
+ }, this.fetchTimeoutMs);
430
+ this.refreshTimeout = timeout;
416
431
  child.stdout?.setEncoding("utf8");
417
432
  child.stdout?.on("data", (chunk) => {
418
433
  stdout += chunk;
419
- if (stdout.length > 64 * 1024) child.kill();
434
+ if (Buffer.byteLength(stdout) > 64 * 1024) {
435
+ failure = "output limit";
436
+ child.kill();
437
+ }
420
438
  });
421
439
 
422
440
  let settled = false;
@@ -424,12 +442,25 @@ class StatusLine {
424
442
  if (settled) return;
425
443
  settled = true;
426
444
  clearTimeout(timeout);
445
+ if (this.refreshChild === child) this.refreshChild = null;
446
+ if (this.refreshTimeout === timeout) this.refreshTimeout = null;
427
447
  this.refreshInFlight = false;
428
448
  if (this.disposed) return;
429
449
  const text = statusOutputText(stdout);
430
- const changed = status === 0 && text && text !== this.text;
431
- if (changed) {
450
+ const previous = this.text;
451
+ if (status === 0 && text && !failure) {
452
+ this.lastReading = text;
432
453
  this.text = text;
454
+ this.fetchFailures = 0;
455
+ } else {
456
+ this.fetchFailures += 1;
457
+ this.text = this.lastReading ? `[stale] ${this.lastReading.trimStart()}` : "AI Battery unavailable";
458
+ const delay = Math.min(this.args.interval * 1000, 1000 * (2 ** Math.min(this.fetchFailures - 1, 4)));
459
+ this.nextFetch = Date.now() + delay;
460
+ this.retryTimer = setTimeout(() => this.kickRefresh(true), delay);
461
+ debugLog("status:fetch-failed", { status, reason: failure || "empty output or command error", retryMs: delay });
462
+ }
463
+ if (this.text !== previous) {
433
464
  if (typeof this.onRefresh === "function") {
434
465
  this.onRefresh();
435
466
  } else {
@@ -474,6 +505,15 @@ class StatusLine {
474
505
 
475
506
  dispose() {
476
507
  this.disposed = true;
508
+ this.refreshQueued = false;
509
+ clearTimeout(this.refreshTimeout);
510
+ clearTimeout(this.retryTimer);
511
+ this.refreshTimeout = null;
512
+ this.retryTimer = null;
513
+ try {
514
+ this.refreshChild?.kill?.();
515
+ } catch {}
516
+ this.refreshChild = null;
477
517
  }
478
518
 
479
519
  clear() {
@@ -1355,6 +1395,7 @@ async function main() {
1355
1395
  }
1356
1396
 
1357
1397
  export {
1398
+ StatusLine,
1358
1399
  terminalSize,
1359
1400
  codexNoAltScreenEnabled,
1360
1401
  commandArgsLookLikeCodex,