pi-usage-hub 1.0.1 → 1.0.3

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
@@ -101,7 +101,7 @@ Built-in providers are the ones already used and smoke-tested by the author. The
101
101
 
102
102
  ## Pull API
103
103
 
104
- The hub caches results for 60 seconds and deduplicates concurrent requests. It never pushes footer text: consumers refresh data, read the cached summary, and re-render when notified.
104
+ The hub caches results for 60 seconds and deduplicates concurrent requests. It never pushes footer text: consumers start refreshes without blocking lifecycle events, read the cached summary, and re-render when notified.
105
105
 
106
106
  | API | Role |
107
107
  |-----|------|
@@ -135,14 +135,15 @@ const offUpdated = pi.events.on("pi-usage-hub:updated", () => {
135
135
  requestRender?.();
136
136
  });
137
137
 
138
- pi.on("session_start", async (_event, ctx) => {
138
+ // Pi awaits lifecycle handlers; refresh in the background and re-render on pi-usage-hub:updated.
139
+ pi.on("session_start", (_event, ctx) => {
139
140
  void usageHub?.refresh({ model: ctx.model, force: true });
140
141
  });
141
- pi.on("model_select", async (event) => {
142
- await usageHub?.refresh({ model: event.model, force: true });
142
+ pi.on("model_select", (event) => {
143
+ void usageHub?.refresh({ model: event.model, force: true });
143
144
  });
144
- pi.on("agent_end", async (_event, ctx) => {
145
- await usageHub?.refresh({ model: ctx.model, force: true });
145
+ pi.on("agent_end", (_event, ctx) => {
146
+ void usageHub?.refresh({ model: ctx.model, force: true });
146
147
  });
147
148
 
148
149
  // Inside footer render():
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-usage-hub",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Usage hub for pi — provider quota/balance registry with cache, /usage-hub panel, and pull API",
5
5
  "keywords": [
6
6
  "pi",
@@ -20,7 +20,8 @@
20
20
  "url": "https://github.com/ouzhenkun/pi-usage-hub/issues"
21
21
  },
22
22
  "peerDependencies": {
23
- "@earendil-works/pi-coding-agent": "*"
23
+ "@earendil-works/pi-coding-agent": "*",
24
+ "@earendil-works/pi-tui": "*"
24
25
  },
25
26
  "pi": {
26
27
  "extensions": [
@@ -3,6 +3,7 @@
3
3
  // input handling, and rendering so index.ts only wires it into the overlay.
4
4
 
5
5
  import type { Theme } from "@earendil-works/pi-coding-agent";
6
+ import { Key, matchesKey } from "@earendil-works/pi-tui";
6
7
  import {
7
8
  collectSessionUsage,
8
9
  type ProviderSessionUsage,
@@ -83,8 +84,8 @@ export class SessionPanel {
83
84
  /** Handle a key. Returns true if the panel consumed it. */
84
85
  handleInput(data: string): boolean {
85
86
  // Left/right: switch period tab
86
- if (data === "\x1b[C" || data === "\x1b[D") {
87
- const delta = data === "\x1b[C" ? 1 : -1;
87
+ if (matchesKey(data, Key.left) || matchesKey(data, Key.right)) {
88
+ const delta = matchesKey(data, Key.right) ? 1 : -1;
88
89
  this.periodIndex = (this.periodIndex + delta + PERIODS.length) % PERIODS.length;
89
90
  this.providerIndex = 0;
90
91
  this.expanded.clear();
@@ -93,8 +94,8 @@ export class SessionPanel {
93
94
  }
94
95
 
95
96
  // Up/down: navigate provider rows
96
- if (data === "\x1b[A" || data === "\x1b[B") {
97
- const delta = data === "\x1b[A" ? -1 : 1;
97
+ if (matchesKey(data, Key.up) || matchesKey(data, Key.down)) {
98
+ const delta = matchesKey(data, Key.up) ? -1 : 1;
98
99
  const n = this.providers().length;
99
100
  if (n > 0) this.providerIndex = (this.providerIndex + delta + n) % n;
100
101
  this.requestRender();
@@ -102,7 +103,7 @@ export class SessionPanel {
102
103
  }
103
104
 
104
105
  // Space/Enter: expand selected provider
105
- if (data === " " || data === "\r") {
106
+ if (matchesKey(data, Key.space) || matchesKey(data, Key.enter)) {
106
107
  const name = this.providers()[this.providerIndex]?.provider;
107
108
  if (name) {
108
109
  if (this.expanded.has(name)) this.expanded.delete(name);
@@ -1,4 +1,5 @@
1
1
  import type { Theme } from "@earendil-works/pi-coding-agent";
2
+ import { Key, matchesKey } from "@earendil-works/pi-tui";
2
3
  import type { UsageProvider, UsageReport } from "../types";
3
4
  import { fullReport } from "./format-report";
4
5
  import { SessionPanel } from "./session-panel";
@@ -57,7 +58,7 @@ export class UsageWindow {
57
58
  this.done();
58
59
  return;
59
60
  }
60
- if (data === "\t") {
61
+ if (matchesKey(data, Key.tab)) {
61
62
  this.mode = this.mode === "quota" ? "session" : "quota";
62
63
  if (this.mode === "session") this.session.ensureLoaded();
63
64
  if (this.mode === "quota") this.ensureQuotaLoaded?.();
@@ -89,7 +90,7 @@ export class UsageWindow {
89
90
  for (const line of content) lines.push(row(line));
90
91
  if (this.mode === "quota") {
91
92
  lines.push(row());
92
- lines.push(row(` ${th.fg("dim", "←→/u switch view · Esc/q close")}`));
93
+ lines.push(row(` ${th.fg("dim", "Tab switch view · Esc/q close")}`));
93
94
  }
94
95
  lines.push(th.fg("border", `╰${"─".repeat(innerW)}╯`));
95
96
  return lines;