portable-agent-layer 0.61.4 → 0.62.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.
@@ -8,12 +8,14 @@
8
8
  * Invoked via `pal cli usage [--today|--week|--month|--all] [--project <name>]`.
9
9
  */
10
10
 
11
+ import { spawnSync } from "node:child_process";
11
12
  import { existsSync, readdirSync, readFileSync } from "node:fs";
12
13
  import { homedir } from "node:os";
13
14
  import { resolve } from "node:path";
14
15
  import { parseArgs } from "node:util";
15
- import { MODEL_PRICING } from "../hooks/lib/models";
16
+ import { costOfUsage } from "../hooks/lib/models";
16
17
  import { palHome } from "../hooks/lib/paths";
18
+ import { findBinaryOnPath } from "../hooks/lib/which";
17
19
 
18
20
  // ── Types ──
19
21
 
@@ -57,34 +59,6 @@ function emptyTimeBuckets(): TimeBuckets {
57
59
 
58
60
  // ── Helpers ──
59
61
 
60
- function findPricing(model: string): (typeof MODEL_PRICING)[string] | null {
61
- if (MODEL_PRICING[model]) return MODEL_PRICING[model];
62
- for (const key of Object.keys(MODEL_PRICING)) {
63
- if (model.startsWith(key)) return MODEL_PRICING[key];
64
- }
65
- return null;
66
- }
67
-
68
- function costForUsage(
69
- model: string,
70
- input: number,
71
- output: number,
72
- cacheWrite5m: number,
73
- cacheWrite1h: number,
74
- cacheRead: number
75
- ): number {
76
- const p = findPricing(model);
77
- if (!p) return 0;
78
- return (
79
- (input * p.input +
80
- output * p.output +
81
- cacheWrite5m * p.cacheWrite5m +
82
- cacheWrite1h * p.cacheWrite1h +
83
- cacheRead * p.cacheRead) /
84
- 1_000_000
85
- );
86
- }
87
-
88
62
  function addToBucket(
89
63
  bucket: Bucket,
90
64
  model: string,
@@ -99,14 +73,13 @@ function addToBucket(
99
73
  bucket.cacheWrite5m += cacheWrite5m;
100
74
  bucket.cacheWrite1h += cacheWrite1h;
101
75
  bucket.cacheRead += cacheRead;
102
- bucket.cost += costForUsage(
103
- model,
76
+ bucket.cost += costOfUsage(model, {
104
77
  input,
105
78
  output,
106
79
  cacheWrite5m,
107
80
  cacheWrite1h,
108
- cacheRead
109
- );
81
+ cacheRead,
82
+ });
110
83
  bucket.calls++;
111
84
  }
112
85
 
@@ -370,6 +343,51 @@ function readPalInference(): {
370
343
  return { buckets, byModel, byCaller };
371
344
  }
372
345
 
346
+ // ── rtk compression savings ──
347
+
348
+ interface RtkSummary {
349
+ total_commands: number;
350
+ total_saved: number;
351
+ avg_savings_pct: number;
352
+ }
353
+
354
+ /**
355
+ * Query rtk's own savings ledger. `installed: false` means rtk isn't on PATH;
356
+ * `summary: null` with `installed: true` means rtk is present but has no data
357
+ * (or errored) — the two cases print differently in the usage report.
358
+ */
359
+ function readRtkGain(): { installed: boolean; summary: RtkSummary | null } {
360
+ const rtk = findBinaryOnPath("rtk");
361
+ if (!rtk) return { installed: false, summary: null };
362
+ try {
363
+ const r = spawnSync(rtk, ["gain", "--format", "json"], {
364
+ encoding: "utf8",
365
+ stdio: ["ignore", "pipe", "ignore"],
366
+ });
367
+ if (r.status !== 0 || !r.stdout) return { installed: true, summary: null };
368
+ const parsed = JSON.parse(r.stdout) as { summary?: RtkSummary };
369
+ return { installed: true, summary: parsed.summary ?? null };
370
+ } catch {
371
+ return { installed: true, summary: null };
372
+ }
373
+ }
374
+
375
+ function printRtkGain(): void {
376
+ const { installed, summary } = readRtkGain();
377
+ console.log("\n rtk Compression\n");
378
+ if (!installed) {
379
+ console.log(" rtk not installed");
380
+ return;
381
+ }
382
+ if (!summary || summary.total_commands === 0) {
383
+ console.log(" rtk installed — no savings recorded yet");
384
+ return;
385
+ }
386
+ console.log(
387
+ ` Tokens saved ${fmt(summary.total_saved).padStart(8)} tok ${summary.avg_savings_pct.toFixed(1)}% avg across ${fmt(summary.total_commands)} commands`
388
+ );
389
+ }
390
+
373
391
  // ── CLI ──
374
392
 
375
393
  export function usage() {
@@ -426,6 +444,8 @@ export function usage() {
426
444
  printRow("Total", tb.total);
427
445
  }
428
446
 
447
+ printRtkGain();
448
+
429
449
  const grand = emptyBucket();
430
450
  for (const b of [cc.buckets.total, pal.buckets.total]) {
431
451
  grand.input += b.input;