pi-status-footer 1.0.2 → 1.0.4

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/README.md CHANGED
@@ -74,7 +74,7 @@ The extension hooks into six pi lifecycle events:
74
74
  - **`session_start`** — registers the footer via `ctx.ui.setFooter()`, discovers repo root, starts branch-change listener
75
75
  - **`agent_start` / `agent_settled`** — controls the agent-run timer and a 1-second interval that triggers git diff refresh
76
76
  - **`turn_start` / `turn_end`** — drives the per-turn timer
77
- - **`message_start` / `message_update` / `message_end`** — tracks the generation window for live TPS
77
+ - **`message_start` / `message_update` / `message_end`** — tracks the generation window for live TPS. Most providers only report output-token usage at message end, so `message_update` estimates tokens live from the streamed delta characters (text/thinking/toolcall), calibrated against each message's real usage at `message_end`
78
78
  - **`model_select`** — clears stale TPS on model switch
79
79
  - **`session_shutdown`** — cleans up all state
80
80
 
@@ -97,38 +97,6 @@ npm install --save-dev @earendil-works/pi-ai @earendil-works/pi-coding-agent @ea
97
97
  npx tsc --noEmit stats-footer.ts
98
98
  ```
99
99
 
100
- ## Releasing to npm
101
-
102
- A GitHub Action (`.github/workflows/release.yml`) auto-publishes to npm via trusted publishing (OIDC) on tag push.
103
-
104
- 1. Bump the version in `package.json` and commit on `main`.
105
- 2. Tag the commit — the tag must match the version, e.g. `v1.0.1`:
106
-
107
- ```bash
108
- git tag v1.0.1
109
- git push origin v1.0.1
110
- ```
111
-
112
- 3. The workflow verifies the tag/version match, dry-runs `npm pack`, then publishes with [npm provenance](https://docs.npmjs.com/generating-provenance-statements).
113
-
114
- ### One-time setup
115
-
116
- Before the first tag push, bind the GitHub Actions workflow as a trusted publisher. You must be logged into npm:
117
-
118
- ```bash
119
- npm adduser # if not already logged in (2FA required)
120
- npm publish --access public # first publish — creates the package on npm
121
- npm trust github pi-status-footer \
122
- --repo kslamph/pi-status-footer \
123
- --file release.yml -y
124
- ```
125
-
126
- This claims the `pi-status-footer` package name, publishes v1.0.0, and authorises the `release.yml` workflow to publish via OIDC without any token. After this one-time setup, just push a `vX.Y.Z` tag and the action takes over.
127
-
128
- ### Pre-release versions
129
-
130
- Tags like `v1.0.1-beta.1` publish to the `next` dist-tag instead of `latest`.
131
-
132
100
  ## License
133
101
 
134
102
  MIT — see [LICENSE](./LICENSE).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-status-footer",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Compact two-line status footer for pi: model, thinking level, tokens, TPS, cache hit rate, context usage, live git diff, and working/turn timers.",
5
5
  "keywords": [
6
6
  "pi-package",
@@ -28,6 +28,9 @@
28
28
  "monitoring",
29
29
  "streaming"
30
30
  ],
31
+ "scripts": {
32
+ "test": "node --experimental-strip-types test-live-tps.ts"
33
+ },
31
34
  "license": "MIT",
32
35
  "repository": {
33
36
  "type": "git",
package/stats-footer.ts CHANGED
@@ -256,6 +256,13 @@ export default function statsFooter(pi: ExtensionAPI) {
256
256
  let genStart: number | null = null;
257
257
  let liveOutput = 0;
258
258
  let lastTps: number | null = null;
259
+ // Characters streamed for the current assistant message (text + thinking
260
+ // + toolcall deltas). Used to estimate output tokens live, since most
261
+ // providers only report usage.output at message end.
262
+ let deltaChars = 0;
263
+ // Exponentially-averaged chars-per-token ratio, calibrated from each
264
+ // message's real usage.output at message_end. Default 4 ≈ English prose.
265
+ let charsPerToken = 4;
259
266
  // Git status: repo folder name + working-tree diff stats (+N -M), fetched
260
267
  // asynchronously (render is synchronous, so results are cached).
261
268
  let sessionCwd: string | null = null;
@@ -309,6 +316,7 @@ export default function statsFooter(pi: ExtensionAPI) {
309
316
  genStart = null;
310
317
  liveOutput = 0;
311
318
  lastTps = null;
319
+ deltaChars = 0;
312
320
  sessionCwd = ctx.cwd;
313
321
  folderName = basename(ctx.cwd);
314
322
  const repoRoot = findRepoRoot(ctx.cwd);
@@ -481,12 +489,12 @@ export default function statsFooter(pi: ExtensionAPI) {
481
489
  stopTimer();
482
490
  tuiRef = null;
483
491
  runStart = null;
484
- lastRunDuration = 0;
485
492
  turnStart = null;
486
493
  lastTurnDuration = 0;
487
494
  genStart = null;
488
495
  liveOutput = 0;
489
496
  lastTps = null;
497
+ deltaChars = 0;
490
498
  sessionCwd = null;
491
499
  folderName = null;
492
500
  repoName = null;
@@ -498,28 +506,45 @@ export default function statsFooter(pi: ExtensionAPI) {
498
506
  // --- Tokens-per-second tracking ---
499
507
  // Each assistant message's generation window is measured from its
500
508
  // message_start (streaming begins) to message_end (stream done).
501
- // message_update carries progressively updated usage.output, giving a
502
- // live rate while streaming; message_end carries the final usage, whose
503
- // rate is kept as lastTps for display while idle.
509
+ // Each assistant message's generation window is measured from its
510
+ // message_start (streaming begins) to message_end (stream done).
511
+ // message_update carries progressively updated usage.output for providers
512
+ // that report it (e.g. some gateways); most providers (Anthropic, OpenAI)
513
+ // only send output usage at message end, so tokens are estimated live from
514
+ // the streamed delta characters, calibrated by charsPerToken. Real usage
515
+ // is preferred whenever it is reported and has grown past the estimate;
516
+ // message_end keeps the final usage's rate as lastTps for display while
517
+ // idle.
504
518
  pi.on("message_start", (event) => {
505
519
  if (event.message.role !== "assistant") return;
506
520
  genStart = Date.now();
507
521
  liveOutput = (event.message as AssistantMessage).usage?.output ?? 0;
522
+ deltaChars = 0;
508
523
  });
509
524
  pi.on("message_update", (event) => {
510
525
  if (event.message.role !== "assistant") return;
511
- // The raw stream event's partial message carries the most current
512
- // usage.output (Anthropic reports it progressively; OpenAI only at the
513
- // end), so prefer it over the session message's usage field.
514
526
  const assistantEvent = event.assistantMessageEvent;
527
+ if (
528
+ assistantEvent &&
529
+ "delta" in assistantEvent &&
530
+ typeof assistantEvent.delta === "string"
531
+ ) {
532
+ deltaChars += assistantEvent.delta.length;
533
+ }
515
534
  const partial =
516
535
  assistantEvent && "partial" in assistantEvent
517
536
  ? (assistantEvent as { partial?: AssistantMessage }).partial
518
537
  : undefined;
519
- const output =
538
+ const reported =
520
539
  partial?.usage?.output ??
521
540
  (event.message as AssistantMessage).usage?.output;
522
- if (typeof output === "number" && output > liveOutput) liveOutput = output;
541
+ const estimated = Math.round(deltaChars / charsPerToken);
542
+ const output = Math.max(
543
+ typeof reported === "number" ? reported : 0,
544
+ liveOutput,
545
+ estimated,
546
+ );
547
+ if (output > liveOutput) liveOutput = output;
523
548
  });
524
549
  pi.on("message_end", (event) => {
525
550
  if (event.message.role === "assistant") {
@@ -529,8 +554,18 @@ export default function statsFooter(pi: ExtensionAPI) {
529
554
  const tps = computeTps(output, genStart, Date.now());
530
555
  lastTps = tps === null ? null : Math.round(tps);
531
556
  }
557
+ // Calibrate the live chars-per-token estimate against the real
558
+ // output token count so the next message's estimate is accurate.
559
+ if (deltaChars > 0 && output > 0) {
560
+ const ratio = deltaChars / output;
561
+ charsPerToken = Math.min(
562
+ 12,
563
+ Math.max(1.5, charsPerToken * 0.7 + ratio * 0.3),
564
+ );
565
+ }
532
566
  genStart = null;
533
567
  liveOutput = 0;
568
+ deltaChars = 0;
534
569
  }
535
570
  rerender();
536
571
  });
@@ -540,6 +575,7 @@ export default function statsFooter(pi: ExtensionAPI) {
540
575
  lastTps = null;
541
576
  genStart = null;
542
577
  liveOutput = 0;
578
+ deltaChars = 0;
543
579
  rerender();
544
580
  });
545
581
  pi.on("session_info_changed", rerender);