pi-usage-hub 1.0.4 → 1.0.6

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
@@ -23,7 +23,7 @@ pi install npm:pi-usage-hub
23
23
  | `/usage-hub session` | Show local session token and cost stats |
24
24
  | `/usage-hub login <name>` | Open browser login for a cookie-based provider |
25
25
 
26
- Inside the panel, **Tab** switches between Quota and Session.
26
+ Inside the panel, **Tab** switches between Quota and Session. In the Quota view, **↑↓** scrolls when content overflows.
27
27
 
28
28
  ![pi-usage-hub quota panel](assets/usage-hub-panel.png)
29
29
 
@@ -73,14 +73,14 @@ Create `~/.pi/agent/pi-usage-hub.json`. Provider order is preserved in the panel
73
73
 
74
74
  ### Provider types
75
75
 
76
- | Type | Credentials | Notes |
77
- |------|-------------|-------|
78
- | `deepseek` | `apiKey` | Account balance |
79
- | `newapi` | `host`, `token`, `userId` | Balance and today's spend; supports multiple instances |
80
- | `opencode-go` | `workspaceId`; optional `auth` | Uses the configured auth cookie or macOS Chrome |
81
- | `ark` | optional `cookie`, `csrfToken` | Uses the configured Cookie header or macOS Chrome |
82
- | `xai` | — | Reads `auth.json` or `~/.grok/auth.json` |
83
- | `kiro` | — | Reads the Kiro OAuth entry from `auth.json` |
76
+ | Type | Credentials | Notes | Website |
77
+ |------|-------------|-------|---------|
78
+ | `deepseek` | `apiKey` | Account balance | [deepseek](https://platform.deepseek.com) |
79
+ | `newapi` | `host`, `token`, `userId` | Balance and today's spend; supports multiple instances | [newapi](https://www.newapi.ai) |
80
+ | `opencode-go` | `workspaceId`; optional `auth` | Uses the configured auth cookie or macOS Chrome | [opencode](https://opencode.ai) |
81
+ | `ark` | optional `cookie`, `csrfToken` | Uses the configured Cookie header or macOS Chrome | [火山引擎](https://console.volcengine.com/ark) |
82
+ | `xai` | — | Reads `auth.json` or `~/.grok/auth.json` | [x.ai/grok](https://x.ai/grok) |
83
+ | `kiro` | — | Reads the Kiro OAuth entry from `auth.json` | [kiro.dev](https://kiro.dev) |
84
84
 
85
85
  For NewAPI, `token` is the **system access token** (not a chat `sk-` token), and `userId` is sent as `New-Api-User`. See [Authentication](https://www.newapi.ai/en/docs/api/management/auth) and [Generate access token](https://www.newapi.ai/en/docs/api/management/user-management/user-token-get).
86
86
 
@@ -97,7 +97,17 @@ These are companion packages, not npm peer dependencies:
97
97
 
98
98
  pi-usage-hub only reads those credentials; it does not run their OAuth flows.
99
99
 
100
- Built-in providers are the ones already used and smoke-tested by the author. The provider ecosystem is too broad to cover exhaustively. Add another provider through L2 registration, or fork the package and submit a PR.
100
+ Built-in providers cover the integrations used and smoke-tested by the author. To support another provider, register a custom provider from an extension, or fork the package and submit a PR.
101
+
102
+ ### Add a custom provider
103
+
104
+ See [`examples/custom-provider.ts`](./examples/custom-provider.ts) for a self-contained extension that implements and registers a provider. Copy it into `~/.pi/agent/extensions/`, then adapt the endpoint, credentials, response shape, and labels.
105
+
106
+ ```ts
107
+ pi.events.on("pi-usage-hub:ready", (hub) => hub.register(myProvider));
108
+ pi.events.emit("pi-usage-hub:register", myProvider);
109
+ pi.events.emit("pi-usage-hub:unregister", { key: "my-relay" });
110
+ ```
101
111
 
102
112
  ## Pull API
103
113
 
@@ -157,16 +167,6 @@ pi.on("session_shutdown", async () => {
157
167
  });
158
168
  ```
159
169
 
160
- ### L2 provider registration
161
-
162
- See [`examples/custom-provider.ts`](./examples/custom-provider.ts) for a self-contained extension that implements and registers a provider. Copy it into `~/.pi/agent/extensions/`, then adapt the endpoint, credentials, response shape, and labels.
163
-
164
- ```ts
165
- pi.events.on("pi-usage-hub:ready", (hub) => hub.register(myProvider));
166
- pi.events.emit("pi-usage-hub:register", myProvider);
167
- pi.events.emit("pi-usage-hub:unregister", { key: "my-relay" });
168
- ```
169
-
170
170
  ## License
171
171
 
172
172
  MIT
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Example: register a custom usage provider through the L2 event API.
2
+ * Example: register a custom usage provider through the event API.
3
3
  *
4
4
  * Copy this file into ~/.pi/agent/extensions/ and adapt the endpoint,
5
5
  * credentials, response shape, and labels for your provider.
@@ -67,8 +67,8 @@ const provider: UsageProvider = {
67
67
  };
68
68
 
69
69
  export default function register(pi: ExtensionAPI) {
70
- const offReady = pi.events.on("pi-usage-hub:ready", (hub: UsageHub) => {
71
- hub.register(provider);
70
+ const offReady = pi.events.on("pi-usage-hub:ready", (data: unknown) => {
71
+ (data as UsageHub).register(provider);
72
72
  });
73
73
 
74
74
  // Covers the case where pi-usage-hub loaded before this extension.
package/index.ts CHANGED
@@ -49,13 +49,15 @@ export default function register(pi: ExtensionAPI) {
49
49
  },
50
50
  };
51
51
 
52
- const offRegister = pi.events.on("pi-usage-hub:register", (p: UsageProvider) => {
53
- if (p && typeof p === "object" && typeof (p as UsageProvider).key === "string") {
54
- hub.register(p as UsageProvider);
52
+ const offRegister = pi.events.on("pi-usage-hub:register", (data: unknown) => {
53
+ const p = data as UsageProvider;
54
+ if (p && typeof p === "object" && typeof p.key === "string") {
55
+ hub.register(p);
55
56
  }
56
57
  });
57
- const offUnregister = pi.events.on("pi-usage-hub:unregister", (data: { key?: string }) => {
58
- if (data?.key) hub.unregister(data.key);
58
+ const offUnregister = pi.events.on("pi-usage-hub:unregister", (data: unknown) => {
59
+ const d = data as { key?: string };
60
+ if (d?.key) hub.unregister(d.key);
59
61
  });
60
62
 
61
63
  // Emit now and again on session_start so late listeners (footer-hub) still get the hub.
@@ -137,6 +139,7 @@ export default function register(pi: ExtensionAPI) {
137
139
  const initialMode = args === "session" ? "session" : "quota";
138
140
 
139
141
  let handle: { focus: () => void; isFocused: () => boolean } | undefined;
142
+ const panelRef = { current: undefined as UsageWindow | undefined };
140
143
  await ctx.ui.custom<void>((tui, theme, _kb, done) => {
141
144
  const unsubscribeInput = ctx.ui.onTerminalInput((data) => {
142
145
  if (!isCloseKey(data) || handle?.isFocused()) return;
@@ -177,6 +180,7 @@ export default function register(pi: ExtensionAPI) {
177
180
  initialMode,
178
181
  loadQuota,
179
182
  );
183
+ panelRef.current = panel;
180
184
  if (initialMode === "quota") loadQuota();
181
185
  return panel;
182
186
  }, {
@@ -184,9 +188,13 @@ export default function register(pi: ExtensionAPI) {
184
188
  overlayOptions: {
185
189
  width: "80%",
186
190
  minWidth: 74,
187
- maxWidth: 96,
188
191
  maxHeight: "80%",
189
192
  anchor: "center",
193
+ // Pass terminal height each frame so the quota view can scroll correctly.
194
+ visible: (_tw, termHeight) => {
195
+ panelRef.current?.setViewHeight(termHeight);
196
+ return true;
197
+ },
190
198
  },
191
199
  onHandle: overlayHandle => {
192
200
  handle = overlayHandle;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-usage-hub",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
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",
@@ -14,6 +14,12 @@ export class UsageWindow {
14
14
  private message = "Fetching usage data...";
15
15
  private session: SessionPanel;
16
16
 
17
+ /** Scroll state for the quota view. */
18
+ private scrollOffset = 0;
19
+ private lastContentLines = 0;
20
+ /** Available content lines (border/header/footer excluded). Set via setViewHeight(). */
21
+ private viewHeight = 15;
22
+
17
23
  constructor(
18
24
  private theme: Theme,
19
25
  private done: () => void,
@@ -27,10 +33,22 @@ export class UsageWindow {
27
33
  if (initialMode === "session") this.session.ensureLoaded();
28
34
  }
29
35
 
36
+ /**
37
+ * Called each render cycle by the overlay's `visible` callback so the quota
38
+ * view knows how many content lines it can show without overflowing.
39
+ */
40
+ setViewHeight(termHeight: number): void {
41
+ // maxHeight: "80%" → overlay inner height ≈ floor(termHeight * 0.8)
42
+ // Fixed overhead: top-border + title + separator + empty + hints + bottom-border = 6
43
+ const overlayH = Math.floor(termHeight * 0.8);
44
+ this.viewHeight = Math.max(4, overlayH - 6);
45
+ }
46
+
30
47
  setPendingProviders(pendingProviders: UsageProvider[]): void {
31
48
  this.orderedProviders = pendingProviders;
32
49
  this.reports.clear();
33
50
  this.message = "";
51
+ this.scrollOffset = 0;
34
52
  this.invalidate();
35
53
  }
36
54
 
@@ -62,9 +80,23 @@ export class UsageWindow {
62
80
  this.mode = this.mode === "quota" ? "session" : "quota";
63
81
  if (this.mode === "session") this.session.ensureLoaded();
64
82
  if (this.mode === "quota") this.ensureQuotaLoaded?.();
83
+ this.scrollOffset = 0;
65
84
  this.requestRender();
66
85
  return;
67
86
  }
87
+ if (this.mode === "quota") {
88
+ if (matchesKey(data, Key.up)) {
89
+ this.scrollOffset = Math.max(0, this.scrollOffset - 1);
90
+ this.requestRender();
91
+ return;
92
+ }
93
+ if (matchesKey(data, Key.down)) {
94
+ const maxScroll = Math.max(0, this.lastContentLines - this.viewHeight);
95
+ this.scrollOffset = Math.min(maxScroll, this.scrollOffset + 1);
96
+ this.requestRender();
97
+ return;
98
+ }
99
+ }
68
100
  if (this.mode === "session") this.session.handleInput(data);
69
101
  }
70
102
 
@@ -81,7 +113,17 @@ export class UsageWindow {
81
113
  const active = this.mode === "quota"
82
114
  ? `${th.fg("accent", "[Quota]")} ${th.fg("dim", "[Session]")}`
83
115
  : `${th.fg("dim", "[Quota]")} ${th.fg("accent", "[Session]")}`;
84
- const content = this.mode === "quota" ? this.renderQuotaLines() : this.session.render(th);
116
+
117
+ let content: string[];
118
+ if (this.mode === "quota") {
119
+ const allLines = this.renderQuotaLines();
120
+ this.lastContentLines = allLines.length;
121
+ const maxScroll = Math.max(0, this.lastContentLines - this.viewHeight);
122
+ this.scrollOffset = Math.min(this.scrollOffset, maxScroll);
123
+ content = allLines.slice(this.scrollOffset, this.scrollOffset + this.viewHeight);
124
+ } else {
125
+ content = this.session.render(th);
126
+ }
85
127
 
86
128
  const lines: string[] = [];
87
129
  lines.push(th.fg("border", `╭${"─".repeat(innerW)}╮`));
@@ -89,8 +131,10 @@ export class UsageWindow {
89
131
  lines.push(row(th.fg("borderMuted", "─".repeat(innerW))));
90
132
  for (const line of content) lines.push(row(line));
91
133
  if (this.mode === "quota") {
134
+ const canScroll = this.lastContentLines > this.viewHeight;
135
+ const scrollHint = canScroll ? "↑↓ scroll · " : "";
92
136
  lines.push(row());
93
- lines.push(row(` ${th.fg("dim", "Tab switch view · Esc/q close")}`));
137
+ lines.push(row(` ${th.fg("dim", `${scrollHint}Tab switch · Esc close`)}`));
94
138
  }
95
139
  lines.push(th.fg("border", `╰${"─".repeat(innerW)}╯`));
96
140
  return lines;