pi-usage-hub 1.0.5 → 1.0.7
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 +21 -11
- package/package.json +1 -1
- package/ui/format-report.ts +5 -4
- package/ui/session-panel.ts +14 -9
- package/ui/usage-window.ts +48 -4
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,16 +180,23 @@ 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
|
}, {
|
|
183
187
|
overlay: true,
|
|
184
|
-
overlayOptions: {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
188
|
+
overlayOptions: () => {
|
|
189
|
+
const termWidth = process.stdout.columns ?? 80;
|
|
190
|
+
return {
|
|
191
|
+
width: Math.min(Math.floor(termWidth * 0.65), 100),
|
|
192
|
+
minWidth: 74,
|
|
193
|
+
maxHeight: "80%",
|
|
194
|
+
anchor: "center",
|
|
195
|
+
visible: (_tw, termHeight) => {
|
|
196
|
+
panelRef.current?.setViewHeight(termHeight);
|
|
197
|
+
return true;
|
|
198
|
+
},
|
|
199
|
+
};
|
|
190
200
|
},
|
|
191
201
|
onHandle: overlayHandle => {
|
|
192
202
|
handle = overlayHandle;
|
package/package.json
CHANGED
package/ui/format-report.ts
CHANGED
|
@@ -33,7 +33,8 @@ export function shortReport(report: UsageReport, provider: UsageProvider): strin
|
|
|
33
33
|
return `${provider.shortLabel} ${text}`;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
export function fullReport(report: UsageReport, provider: UsageProvider): string {
|
|
36
|
+
export function fullReport(report: UsageReport, provider: UsageProvider, contentWidth?: number): string {
|
|
37
|
+
const barWidth = contentWidth ? Math.max(20, Math.min(50, contentWidth - 32)) : 24;
|
|
37
38
|
const lines: string[] = [];
|
|
38
39
|
lines.push(`\x1b[1m${provider.label}\x1b[0m`);
|
|
39
40
|
|
|
@@ -52,15 +53,15 @@ export function fullReport(report: UsageReport, provider: UsageProvider): string
|
|
|
52
53
|
|
|
53
54
|
if (report.session) {
|
|
54
55
|
const r = report.session.resetsIn ? ` \u23F1 ${report.session.resetsIn}` : "";
|
|
55
|
-
lines.push(` Rolling: ${bar(report.session.pct)} ${report.session.pct.toFixed(1)}%${r}`);
|
|
56
|
+
lines.push(` Rolling: ${bar(report.session.pct, barWidth)} ${report.session.pct.toFixed(1)}%${r}`);
|
|
56
57
|
}
|
|
57
58
|
if (report.weekly) {
|
|
58
59
|
const r = report.weekly.resetsIn ? ` \u23F1 ${report.weekly.resetsIn}` : "";
|
|
59
|
-
lines.push(` Weekly: ${bar(report.weekly.pct)} ${report.weekly.pct.toFixed(1)}%${r}`);
|
|
60
|
+
lines.push(` Weekly: ${bar(report.weekly.pct, barWidth)} ${report.weekly.pct.toFixed(1)}%${r}`);
|
|
60
61
|
}
|
|
61
62
|
if (report.monthly) {
|
|
62
63
|
const r = report.monthly.resetsIn ? ` \u23F1 ${report.monthly.resetsIn}` : "";
|
|
63
|
-
lines.push(` Monthly: ${bar(report.monthly.pct)} ${report.monthly.pct.toFixed(1)}%${r}`);
|
|
64
|
+
lines.push(` Monthly: ${bar(report.monthly.pct, barWidth)} ${report.monthly.pct.toFixed(1)}%${r}`);
|
|
64
65
|
}
|
|
65
66
|
|
|
66
67
|
return lines.join("\n");
|
package/ui/session-panel.ts
CHANGED
|
@@ -23,13 +23,13 @@ const MAX_PROVIDERS = 5;
|
|
|
23
23
|
|
|
24
24
|
// Column widths shared by period, provider and model rows.
|
|
25
25
|
const nameW = 18;
|
|
26
|
-
const costW =
|
|
26
|
+
const costW = 8;
|
|
27
27
|
const tokW = 8;
|
|
28
28
|
const inW = 8;
|
|
29
29
|
const outW = 7;
|
|
30
30
|
const cacheW = 8;
|
|
31
31
|
const msgW = 7;
|
|
32
|
-
const sesW =
|
|
32
|
+
const sesW = 6;
|
|
33
33
|
|
|
34
34
|
// Model rows are indented 3 extra spaces, so their name field is narrower to
|
|
35
35
|
// keep every numeric column aligned with the provider rows above.
|
|
@@ -124,12 +124,17 @@ export class SessionPanel {
|
|
|
124
124
|
return this.currentPeriod()?.providers.slice(0, MAX_PROVIDERS) ?? [];
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
-
render(theme: Theme): string[] {
|
|
127
|
+
render(theme: Theme, contentWidth?: number): string[] {
|
|
128
128
|
const th = theme;
|
|
129
129
|
if (this.loading) return [` ${th.fg("muted", "Loading local session usage...")}`];
|
|
130
130
|
if (this.error) return [` ${th.fg("warning", this.error)}`];
|
|
131
131
|
if (!this.usage) return [` ${th.fg("muted", "Loading...")}`];
|
|
132
132
|
|
|
133
|
+
// Dynamic column widths
|
|
134
|
+
const fixedCols = sesW + msgW + inW + outW + cacheW + tokW + costW; // = 52
|
|
135
|
+
const dynamicNameW = contentWidth ? Math.max(15, Math.min(36, contentWidth - 3 - fixedCols)) : nameW;
|
|
136
|
+
const sepWidth = contentWidth ?? 72;
|
|
137
|
+
|
|
133
138
|
const lines: string[] = [];
|
|
134
139
|
|
|
135
140
|
// ── Period tab bar (2nd-level, row 1) ──
|
|
@@ -140,11 +145,11 @@ export class SessionPanel {
|
|
|
140
145
|
: th.fg("dim", ` ${label} `);
|
|
141
146
|
});
|
|
142
147
|
lines.push(` ${tabParts.join(th.fg("borderMuted", "│"))}`);
|
|
143
|
-
lines.push(th.fg("borderMuted", " " + "─".repeat(
|
|
148
|
+
lines.push(th.fg("borderMuted", " " + "─".repeat(sepWidth)));
|
|
144
149
|
|
|
145
150
|
// ── Provider table (always MAX_PROVIDERS rows tall for fixed height) ──
|
|
146
151
|
const providers = this.providers();
|
|
147
|
-
lines.push(th.fg("muted", ` ${cols("Provider", "Sess", "Msgs", "↑In", "↓Out", "Cache", "Tokens", "Cost")}`));
|
|
152
|
+
lines.push(th.fg("muted", ` ${cols("Provider", "Sess", "Msgs", "↑In", "↓Out", "Cache", "Tokens", "Cost", dynamicNameW)}`));
|
|
148
153
|
|
|
149
154
|
for (let i = 0; i < MAX_PROVIDERS; i++) {
|
|
150
155
|
const p = providers[i];
|
|
@@ -155,12 +160,12 @@ export class SessionPanel {
|
|
|
155
160
|
const selected = i === this.providerIndex;
|
|
156
161
|
const isExpanded = this.expanded.has(p.provider);
|
|
157
162
|
const prefix = selected ? (isExpanded ? " ▾ " : " ▸ ") : " ";
|
|
158
|
-
const text = `${prefix}${statCols(p.provider, p)}`;
|
|
163
|
+
const text = `${prefix}${statCols(p.provider, p, dynamicNameW)}`;
|
|
159
164
|
lines.push(selected ? th.fg("accent", text) : th.fg("dim", text));
|
|
160
165
|
|
|
161
166
|
if (isExpanded) {
|
|
162
167
|
for (const m of p.models) {
|
|
163
|
-
lines.push(th.fg("dim", ` └ ${statCols(m.model, m,
|
|
168
|
+
lines.push(th.fg("dim", ` └ ${statCols(m.model, m, dynamicNameW - 2)}`));
|
|
164
169
|
}
|
|
165
170
|
}
|
|
166
171
|
}
|
|
@@ -169,8 +174,8 @@ export class SessionPanel {
|
|
|
169
174
|
const period = PERIODS[this.periodIndex]!;
|
|
170
175
|
const totals = this.usage[period.key].totals;
|
|
171
176
|
lines.push("");
|
|
172
|
-
lines.push(th.fg("borderMuted", " " + "─".repeat(
|
|
173
|
-
lines.push(th.fg("accent", ` ${statCols("Total:", totals)}`));
|
|
177
|
+
lines.push(th.fg("borderMuted", " " + "─".repeat(sepWidth)));
|
|
178
|
+
lines.push(th.fg("accent", ` ${statCols("Total:", totals, dynamicNameW)}`));
|
|
174
179
|
|
|
175
180
|
lines.push("");
|
|
176
181
|
lines.push(` ${th.fg("dim", `←→ period · ↑↓ provider · Space expand · Tab switch · Esc close`)}`);
|
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(bodyW);
|
|
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, bodyW);
|
|
126
|
+
}
|
|
85
127
|
|
|
86
128
|
const lines: string[] = [];
|
|
87
129
|
lines.push(th.fg("border", `╭${"─".repeat(innerW)}╮`));
|
|
@@ -89,14 +131,16 @@ 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;
|
|
97
141
|
}
|
|
98
142
|
|
|
99
|
-
private renderQuotaLines(): string[] {
|
|
143
|
+
private renderQuotaLines(contentWidth: number): string[] {
|
|
100
144
|
const th = this.theme;
|
|
101
145
|
const lines: string[] = [];
|
|
102
146
|
|
|
@@ -112,7 +156,7 @@ export class UsageWindow {
|
|
|
112
156
|
const provider = this.orderedProviders[i]!;
|
|
113
157
|
const report = this.reports.get(provider);
|
|
114
158
|
if (report) {
|
|
115
|
-
for (const line of fullReport(report, provider).split("\n")) {
|
|
159
|
+
for (const line of fullReport(report, provider, contentWidth).split("\n")) {
|
|
116
160
|
lines.push(` ${line}`);
|
|
117
161
|
}
|
|
118
162
|
} else {
|