pi-usage-hub 1.0.5 → 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 +9 -9
- package/examples/custom-provider.ts +2 -2
- package/index.ts +14 -6
- package/package.json +1 -1
- package/ui/usage-window.ts +46 -2
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
|

|
|
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
|
|
|
@@ -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", (
|
|
71
|
-
|
|
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", (
|
|
53
|
-
|
|
54
|
-
|
|
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:
|
|
58
|
-
|
|
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
package/ui/usage-window.ts
CHANGED
|
@@ -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
|
-
|
|
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",
|
|
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;
|