pi-usage-hub 1.0.6 → 1.0.8
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 +2 -1
- package/core/config.ts +1 -0
- package/core/registry.ts +1 -0
- package/index.ts +13 -11
- package/package.json +1 -1
- package/providers/ark.ts +1 -0
- package/providers/deepseek.ts +1 -0
- package/providers/kiro.ts +1 -0
- package/providers/newapi.ts +1 -0
- package/providers/opencode-go.ts +1 -0
- package/providers/xai.ts +72 -86
- package/types.ts +4 -0
- package/ui/format-report.ts +10 -5
- package/ui/session-panel.ts +14 -9
- package/ui/usage-window.ts +4 -4
package/README.md
CHANGED
|
@@ -70,6 +70,7 @@ Create `~/.pi/agent/pi-usage-hub.json`. Provider order is preserved in the panel
|
|
|
70
70
|
| `name` | no | Instance key; defaults to `type`, then `type-2`, `type-3`, and so on |
|
|
71
71
|
| `matchProviders` | no | Additional `model.provider` values mapped to this entry |
|
|
72
72
|
| `shortLabel` / `label` | no | Override the footer label or panel title |
|
|
73
|
+
| `hidden` | no | Exclude from panel and footer; `/usage-hub login` still works |
|
|
73
74
|
|
|
74
75
|
### Provider types
|
|
75
76
|
|
|
@@ -158,7 +159,7 @@ pi.on("agent_end", (_event, ctx) => {
|
|
|
158
159
|
|
|
159
160
|
// Inside footer render():
|
|
160
161
|
// const usageText = usageHub?.getSummary(ctx.model);
|
|
161
|
-
// "XAI 87% · ↻ 3d 16h
|
|
162
|
+
// "XAI 87% · ↻ 3d 16h"
|
|
162
163
|
|
|
163
164
|
pi.on("session_shutdown", async () => {
|
|
164
165
|
offReady();
|
package/core/config.ts
CHANGED
|
@@ -65,6 +65,7 @@ function parseProviderEntry(
|
|
|
65
65
|
matchProviders: asStringArray(raw.matchProviders),
|
|
66
66
|
shortLabel: typeof raw.shortLabel === "string" ? raw.shortLabel : undefined,
|
|
67
67
|
label: typeof raw.label === "string" ? raw.label : undefined,
|
|
68
|
+
hidden: typeof raw.hidden === "boolean" ? raw.hidden : undefined,
|
|
68
69
|
};
|
|
69
70
|
|
|
70
71
|
switch (type) {
|
package/core/registry.ts
CHANGED
package/index.ts
CHANGED
|
@@ -60,7 +60,7 @@ export default function register(pi: ExtensionAPI) {
|
|
|
60
60
|
if (d?.key) hub.unregister(d.key);
|
|
61
61
|
});
|
|
62
62
|
|
|
63
|
-
// Emit now and again on session_start so late listeners
|
|
63
|
+
// Emit now and again on session_start so late listeners still get the hub.
|
|
64
64
|
pi.events.emit("pi-usage-hub:ready", hub);
|
|
65
65
|
pi.on("session_start", async () => {
|
|
66
66
|
pi.events.emit("pi-usage-hub:ready", hub);
|
|
@@ -185,16 +185,18 @@ export default function register(pi: ExtensionAPI) {
|
|
|
185
185
|
return panel;
|
|
186
186
|
}, {
|
|
187
187
|
overlay: true,
|
|
188
|
-
overlayOptions: {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
+
};
|
|
198
200
|
},
|
|
199
201
|
onHandle: overlayHandle => {
|
|
200
202
|
handle = overlayHandle;
|
package/package.json
CHANGED
package/providers/ark.ts
CHANGED
|
@@ -108,6 +108,7 @@ export function makeArkProvider(name: string, cfg: ArkConfig = {}): UsageProvide
|
|
|
108
108
|
matchProviders: cfg.matchProviders ?? [name, "ark"],
|
|
109
109
|
shortLabel: cfg.shortLabel ?? "ARK",
|
|
110
110
|
label: cfg.label ?? "ARK Coding",
|
|
111
|
+
hidden: cfg.hidden,
|
|
111
112
|
detect: () => resolveCookies(cfg) !== null,
|
|
112
113
|
|
|
113
114
|
fetchUsage: async (): Promise<UsageReport> => {
|
package/providers/deepseek.ts
CHANGED
|
@@ -10,6 +10,7 @@ export function makeDeepseekProvider(name: string, cfg: DeepseekConfig): UsagePr
|
|
|
10
10
|
matchProviders: cfg.matchProviders ?? [name, "deepseek"],
|
|
11
11
|
shortLabel: cfg.shortLabel ?? "DS",
|
|
12
12
|
label: cfg.label ?? "DeepSeek",
|
|
13
|
+
hidden: cfg.hidden,
|
|
13
14
|
detect: () => !!apiKey,
|
|
14
15
|
|
|
15
16
|
fetchUsage: async (): Promise<UsageReport> => {
|
package/providers/kiro.ts
CHANGED
|
@@ -272,6 +272,7 @@ export function makeKiroProvider(name: string, cfg: KiroConfig = {}): UsageProvi
|
|
|
272
272
|
matchProviders: cfg.matchProviders ?? [name, "kiro"],
|
|
273
273
|
shortLabel: cfg.shortLabel ?? "KIRO",
|
|
274
274
|
label: cfg.label ?? "Kiro",
|
|
275
|
+
hidden: cfg.hidden,
|
|
275
276
|
// Presence in config enables the provider; auth comes from auth.json.
|
|
276
277
|
detect: () => true,
|
|
277
278
|
|
package/providers/newapi.ts
CHANGED
|
@@ -60,6 +60,7 @@ export function makeNewApiProvider(name: string, cfg: NewApiConfig): UsageProvid
|
|
|
60
60
|
matchProviders: cfg.matchProviders ?? [name],
|
|
61
61
|
shortLabel,
|
|
62
62
|
label: cfg.label ?? `NewAPI ${shortLabel}`,
|
|
63
|
+
hidden: cfg.hidden,
|
|
63
64
|
detect: () => !!(cfg.host && cfg.token && cfg.userId),
|
|
64
65
|
|
|
65
66
|
fetchUsage: async (): Promise<UsageReport> => {
|
package/providers/opencode-go.ts
CHANGED
|
@@ -47,6 +47,7 @@ export function makeOpencodeGoProvider(name: string, cfg: OpencodeGoConfig): Usa
|
|
|
47
47
|
matchProviders: cfg.matchProviders ?? [name, "opencode-go"],
|
|
48
48
|
shortLabel: cfg.shortLabel ?? "OCG",
|
|
49
49
|
label: cfg.label ?? "OpenCode Go",
|
|
50
|
+
hidden: cfg.hidden,
|
|
50
51
|
detect: () => !!workspaceId && (!!resolveAuth(cfg) || !manual),
|
|
51
52
|
|
|
52
53
|
fetchUsage: async (): Promise<UsageReport> => {
|
package/providers/xai.ts
CHANGED
|
@@ -22,12 +22,6 @@ interface XaiCredentials {
|
|
|
22
22
|
tokenEndpoint?: string;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
interface MonthlyUsage {
|
|
26
|
-
monthlyLimit: number;
|
|
27
|
-
used: number;
|
|
28
|
-
billingPeriodEnd: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
25
|
interface WeeklyUsage {
|
|
32
26
|
creditUsagePercent: number;
|
|
33
27
|
billingPeriodEnd: string;
|
|
@@ -102,12 +96,11 @@ function isFresh(credentials: XaiCredentials): boolean {
|
|
|
102
96
|
}
|
|
103
97
|
|
|
104
98
|
function validateTokenEndpoint(url: string): string {
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
throw new Error(`unexpected xAI token endpoint: ${url}`);
|
|
99
|
+
const normalized = url.replace(/\/$/, "");
|
|
100
|
+
if (normalized !== DEFAULT_TOKEN_ENDPOINT) {
|
|
101
|
+
throw new Error("unexpected xAI token endpoint — run /login xai-auth");
|
|
109
102
|
}
|
|
110
|
-
return
|
|
103
|
+
return DEFAULT_TOKEN_ENDPOINT;
|
|
111
104
|
}
|
|
112
105
|
|
|
113
106
|
async function refreshCredentials(credentials: XaiCredentials): Promise<XaiCredentials> {
|
|
@@ -156,38 +149,17 @@ function resetsInFromIso(iso: string | undefined): string | undefined {
|
|
|
156
149
|
return formatDurationMs(end - Date.now());
|
|
157
150
|
}
|
|
158
151
|
|
|
159
|
-
function
|
|
152
|
+
function parseWeeklyUsage(payload: unknown): WeeklyUsage {
|
|
160
153
|
if (!payload || typeof payload !== "object") throw new Error("invalid billing payload");
|
|
161
154
|
const config = (payload as Record<string, unknown>).config;
|
|
162
155
|
if (!config || typeof config !== "object") throw new Error("invalid billing payload");
|
|
163
156
|
|
|
164
|
-
const monthlyLimit = ((config as Record<string, unknown>).monthlyLimit as Record<string, unknown>)?.val;
|
|
165
|
-
const used = ((config as Record<string, unknown>).used as Record<string, unknown>)?.val;
|
|
166
|
-
const billingPeriodEnd = (config as Record<string, unknown>).billingPeriodEnd;
|
|
167
|
-
|
|
168
|
-
if (
|
|
169
|
-
typeof monthlyLimit !== "number" ||
|
|
170
|
-
!Number.isFinite(monthlyLimit) ||
|
|
171
|
-
typeof used !== "number" ||
|
|
172
|
-
!Number.isFinite(used) ||
|
|
173
|
-
typeof billingPeriodEnd !== "string" ||
|
|
174
|
-
!Number.isFinite(new Date(billingPeriodEnd).getTime())
|
|
175
|
-
) {
|
|
176
|
-
throw new Error("invalid billing payload");
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
return { monthlyLimit, used, billingPeriodEnd };
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function parseWeeklyUsage(payload: unknown): WeeklyUsage | undefined {
|
|
183
|
-
if (!payload || typeof payload !== "object") return undefined;
|
|
184
|
-
const config = (payload as Record<string, unknown>).config;
|
|
185
|
-
if (!config || typeof config !== "object") return undefined;
|
|
186
|
-
|
|
187
157
|
const currentPeriod = (config as Record<string, unknown>).currentPeriod as
|
|
188
158
|
| Record<string, unknown>
|
|
189
159
|
| undefined;
|
|
190
|
-
if (currentPeriod?.type !== "USAGE_PERIOD_TYPE_WEEKLY")
|
|
160
|
+
if (currentPeriod?.type !== "USAGE_PERIOD_TYPE_WEEKLY") {
|
|
161
|
+
throw new Error("could not parse usage");
|
|
162
|
+
}
|
|
191
163
|
|
|
192
164
|
const creditUsagePercent = (config as Record<string, unknown>).creditUsagePercent;
|
|
193
165
|
const billingPeriodEnd = (config as Record<string, unknown>).billingPeriodEnd;
|
|
@@ -197,7 +169,7 @@ function parseWeeklyUsage(payload: unknown): WeeklyUsage | undefined {
|
|
|
197
169
|
typeof billingPeriodEnd !== "string" ||
|
|
198
170
|
!Number.isFinite(new Date(billingPeriodEnd).getTime())
|
|
199
171
|
) {
|
|
200
|
-
|
|
172
|
+
throw new Error("could not parse usage");
|
|
201
173
|
}
|
|
202
174
|
|
|
203
175
|
return { creditUsagePercent, billingPeriodEnd };
|
|
@@ -216,76 +188,83 @@ async function fetchJson(url: string, token: string): Promise<unknown> {
|
|
|
216
188
|
},
|
|
217
189
|
signal: controller.signal,
|
|
218
190
|
});
|
|
219
|
-
if (!res.ok)
|
|
191
|
+
if (!res.ok) {
|
|
192
|
+
const err = new Error(`HTTP ${res.status}`) as Error & { status?: number };
|
|
193
|
+
err.status = res.status;
|
|
194
|
+
throw err;
|
|
195
|
+
}
|
|
220
196
|
return await res.json();
|
|
221
197
|
} finally {
|
|
222
198
|
clearTimeout(timer);
|
|
223
199
|
}
|
|
224
200
|
}
|
|
225
201
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
let weekly: WeeklyUsage | undefined;
|
|
229
|
-
try {
|
|
230
|
-
const weeklyPayload = await fetchJson(`${BILLING_BASE_URL}/billing?format=credits`, token);
|
|
231
|
-
weekly = parseWeeklyUsage(weeklyPayload);
|
|
232
|
-
if (!weekly && weeklyPayload) {
|
|
233
|
-
const config = (weeklyPayload as any)?.config;
|
|
234
|
-
weekly = {
|
|
235
|
-
creditUsagePercent: 0,
|
|
236
|
-
billingPeriodEnd: config?.billingPeriodEnd || new Date().toISOString(),
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
} catch {
|
|
240
|
-
weekly = undefined;
|
|
241
|
-
}
|
|
242
|
-
return { monthly, weekly };
|
|
202
|
+
function isUnauthorized(err: unknown): boolean {
|
|
203
|
+
return !!err && typeof err === "object" && (err as { status?: number }).status === 401;
|
|
243
204
|
}
|
|
244
205
|
|
|
245
|
-
function
|
|
246
|
-
|
|
206
|
+
async function fetchBillingUsage(token: string): Promise<WeeklyUsage> {
|
|
207
|
+
return parseWeeklyUsage(await fetchJson(`${BILLING_BASE_URL}/billing?format=credits`, token));
|
|
208
|
+
}
|
|
247
209
|
|
|
248
|
-
|
|
249
|
-
|
|
210
|
+
function mapUsage(weekly: WeeklyUsage): UsageReport {
|
|
211
|
+
return {
|
|
212
|
+
weekly: {
|
|
250
213
|
pct: weekly.creditUsagePercent,
|
|
251
214
|
resetsIn: resetsInFromIso(weekly.billingPeriodEnd),
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
if (monthly.monthlyLimit > 0) {
|
|
256
|
-
report.monthly = {
|
|
257
|
-
pct: (monthly.used / monthly.monthlyLimit) * 100,
|
|
258
|
-
resetsIn: resetsInFromIso(monthly.billingPeriodEnd),
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
if (!report.weekly && !report.monthly) {
|
|
263
|
-
return { error: "could not parse usage" };
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
return report;
|
|
215
|
+
},
|
|
216
|
+
};
|
|
267
217
|
}
|
|
268
218
|
|
|
269
219
|
export function makeXaiProvider(name: string, cfg: XaiConfig = {}): UsageProvider {
|
|
220
|
+
// In-memory only — intentionally not written back to auth.json.
|
|
270
221
|
let cachedCredentials: XaiCredentials | null = null;
|
|
222
|
+
let diskRefreshAtCache: string | undefined;
|
|
271
223
|
|
|
272
|
-
async function getCredentials(): Promise<XaiCredentials | null> {
|
|
224
|
+
async function getCredentials(forceRefresh = false): Promise<XaiCredentials | null> {
|
|
273
225
|
const stored = readStoredCredentials();
|
|
274
|
-
if (!stored)
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
return
|
|
226
|
+
if (!stored) {
|
|
227
|
+
cachedCredentials = null;
|
|
228
|
+
diskRefreshAtCache = undefined;
|
|
229
|
+
return null;
|
|
278
230
|
}
|
|
279
231
|
|
|
232
|
+
// Drop cache if disk was re-logged to a different refresh token.
|
|
280
233
|
if (
|
|
281
234
|
cachedCredentials &&
|
|
282
|
-
|
|
283
|
-
|
|
235
|
+
stored.refresh &&
|
|
236
|
+
diskRefreshAtCache &&
|
|
237
|
+
stored.refresh !== diskRefreshAtCache &&
|
|
238
|
+
stored.refresh !== cachedCredentials.refresh
|
|
284
239
|
) {
|
|
285
|
-
|
|
240
|
+
cachedCredentials = null;
|
|
241
|
+
diskRefreshAtCache = undefined;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (!forceRefresh) {
|
|
245
|
+
if (isFresh(stored)) {
|
|
246
|
+
cachedCredentials = stored;
|
|
247
|
+
diskRefreshAtCache = stored.refresh;
|
|
248
|
+
return stored;
|
|
249
|
+
}
|
|
250
|
+
// Prefer session cache after a prior refresh (tokens are not persisted).
|
|
251
|
+
if (cachedCredentials && isFresh(cachedCredentials)) {
|
|
252
|
+
return cachedCredentials;
|
|
253
|
+
}
|
|
286
254
|
}
|
|
287
255
|
|
|
288
|
-
|
|
256
|
+
const base =
|
|
257
|
+
cachedCredentials?.refresh
|
|
258
|
+
? {
|
|
259
|
+
access: cachedCredentials.access,
|
|
260
|
+
refresh: cachedCredentials.refresh,
|
|
261
|
+
expires: cachedCredentials.expires,
|
|
262
|
+
tokenEndpoint: cachedCredentials.tokenEndpoint ?? stored.tokenEndpoint,
|
|
263
|
+
}
|
|
264
|
+
: stored;
|
|
265
|
+
|
|
266
|
+
cachedCredentials = await refreshCredentials(base);
|
|
267
|
+
diskRefreshAtCache = stored.refresh;
|
|
289
268
|
return cachedCredentials;
|
|
290
269
|
}
|
|
291
270
|
|
|
@@ -294,14 +273,21 @@ export function makeXaiProvider(name: string, cfg: XaiConfig = {}): UsageProvide
|
|
|
294
273
|
matchProviders: cfg.matchProviders ?? [name, "xai-auth", "xai", "grok-cli"],
|
|
295
274
|
shortLabel: cfg.shortLabel ?? "XAI",
|
|
296
275
|
label: cfg.label ?? "xAI (SuperGrok)",
|
|
276
|
+
hidden: cfg.hidden,
|
|
297
277
|
detect: () => readStoredCredentials() !== null,
|
|
298
278
|
|
|
299
279
|
fetchUsage: async (): Promise<UsageReport> => {
|
|
300
280
|
try {
|
|
301
|
-
const credentials = await getCredentials();
|
|
281
|
+
const credentials = await getCredentials(false);
|
|
302
282
|
if (!credentials) return { error: "not logged in — run /login xai-auth" };
|
|
303
|
-
|
|
304
|
-
|
|
283
|
+
try {
|
|
284
|
+
return mapUsage(await fetchBillingUsage(credentials.access));
|
|
285
|
+
} catch (err) {
|
|
286
|
+
if (!isUnauthorized(err)) throw err;
|
|
287
|
+
const refreshed = await getCredentials(true);
|
|
288
|
+
if (!refreshed) return { error: "session expired — run /login xai-auth" };
|
|
289
|
+
return mapUsage(await fetchBillingUsage(refreshed.access));
|
|
290
|
+
}
|
|
305
291
|
} catch (err: any) {
|
|
306
292
|
if (err?.name === "AbortError") return { error: "timeout" };
|
|
307
293
|
return { error: err?.message ?? String(err) };
|
package/types.ts
CHANGED
|
@@ -17,6 +17,8 @@ export interface UsageProvider {
|
|
|
17
17
|
matchProviders?: string[];
|
|
18
18
|
shortLabel: string;
|
|
19
19
|
label: string;
|
|
20
|
+
/** If true, excluded from active providers (quota panel and footer). Login still works. */
|
|
21
|
+
hidden?: boolean;
|
|
20
22
|
detect(): boolean;
|
|
21
23
|
fetchUsage(): Promise<UsageReport>;
|
|
22
24
|
login?(): Promise<boolean>;
|
|
@@ -36,6 +38,8 @@ export interface ProviderMeta {
|
|
|
36
38
|
matchProviders?: string[];
|
|
37
39
|
shortLabel?: string;
|
|
38
40
|
label?: string;
|
|
41
|
+
/** If true, exclude from active providers (quota panel, footer). Login still works. */
|
|
42
|
+
hidden?: boolean;
|
|
39
43
|
}
|
|
40
44
|
|
|
41
45
|
/** Factory input — no type (caller already picked the factory). */
|
package/ui/format-report.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { UsageProvider, UsageReport } from "../types";
|
|
2
2
|
|
|
3
3
|
export function bar(pct: number, width = 24): string {
|
|
4
|
-
const filled = Math.round((pct / 100) * width);
|
|
4
|
+
const filled = Math.min(width, Math.max(0, Math.round((pct / 100) * width)));
|
|
5
5
|
const empty = width - filled;
|
|
6
6
|
const color = pct < 70 ? "32" : pct < 90 ? "33" : "31";
|
|
7
7
|
const blocks = "\u2588".repeat(filled) + "\u2591".repeat(empty);
|
|
@@ -20,6 +20,7 @@ export function shortReport(report: UsageReport, provider: UsageProvider): strin
|
|
|
20
20
|
|
|
21
21
|
const resetStr = primary.resetsIn ? ` · ↻ ${primary.resetsIn}` : "";
|
|
22
22
|
let text = `${primary.pct.toFixed(0)}%${resetStr}`;
|
|
23
|
+
if (primary.pct >= 100) text += " OVER";
|
|
23
24
|
|
|
24
25
|
const secondary: string[] = [];
|
|
25
26
|
if (report.session != null) {
|
|
@@ -33,7 +34,8 @@ export function shortReport(report: UsageReport, provider: UsageProvider): strin
|
|
|
33
34
|
return `${provider.shortLabel} ${text}`;
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
export function fullReport(report: UsageReport, provider: UsageProvider): string {
|
|
37
|
+
export function fullReport(report: UsageReport, provider: UsageProvider, contentWidth?: number): string {
|
|
38
|
+
const barWidth = contentWidth ? Math.max(20, Math.min(50, contentWidth - 32)) : 24;
|
|
37
39
|
const lines: string[] = [];
|
|
38
40
|
lines.push(`\x1b[1m${provider.label}\x1b[0m`);
|
|
39
41
|
|
|
@@ -52,16 +54,19 @@ export function fullReport(report: UsageReport, provider: UsageProvider): string
|
|
|
52
54
|
|
|
53
55
|
if (report.session) {
|
|
54
56
|
const r = report.session.resetsIn ? ` \u23F1 ${report.session.resetsIn}` : "";
|
|
55
|
-
lines.push(` Rolling: ${bar(report.session.pct)} ${report.session.pct.toFixed(1)}%${r}`);
|
|
57
|
+
lines.push(` Rolling: ${bar(report.session.pct, barWidth)} ${report.session.pct.toFixed(1)}%${r}`);
|
|
56
58
|
}
|
|
57
59
|
if (report.weekly) {
|
|
58
60
|
const r = report.weekly.resetsIn ? ` \u23F1 ${report.weekly.resetsIn}` : "";
|
|
59
|
-
lines.push(` Weekly: ${bar(report.weekly.pct)} ${report.weekly.pct.toFixed(1)}%${r}`);
|
|
61
|
+
lines.push(` Weekly: ${bar(report.weekly.pct, barWidth)} ${report.weekly.pct.toFixed(1)}%${r}`);
|
|
60
62
|
}
|
|
61
63
|
if (report.monthly) {
|
|
62
64
|
const r = report.monthly.resetsIn ? ` \u23F1 ${report.monthly.resetsIn}` : "";
|
|
63
|
-
lines.push(` Monthly: ${bar(report.monthly.pct)} ${report.monthly.pct.toFixed(1)}%${r}`);
|
|
65
|
+
lines.push(` Monthly: ${bar(report.monthly.pct, barWidth)} ${report.monthly.pct.toFixed(1)}%${r}`);
|
|
64
66
|
}
|
|
65
67
|
|
|
68
|
+
const over = [report.session, report.weekly, report.monthly].some(d => d != null && d.pct >= 100);
|
|
69
|
+
if (over) lines.push(` \x1b[31m\u26A0 over limit\x1b[0m`);
|
|
70
|
+
|
|
66
71
|
return lines.join("\n");
|
|
67
72
|
}
|
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
|
@@ -116,13 +116,13 @@ export class UsageWindow {
|
|
|
116
116
|
|
|
117
117
|
let content: string[];
|
|
118
118
|
if (this.mode === "quota") {
|
|
119
|
-
const allLines = this.renderQuotaLines();
|
|
119
|
+
const allLines = this.renderQuotaLines(bodyW);
|
|
120
120
|
this.lastContentLines = allLines.length;
|
|
121
121
|
const maxScroll = Math.max(0, this.lastContentLines - this.viewHeight);
|
|
122
122
|
this.scrollOffset = Math.min(this.scrollOffset, maxScroll);
|
|
123
123
|
content = allLines.slice(this.scrollOffset, this.scrollOffset + this.viewHeight);
|
|
124
124
|
} else {
|
|
125
|
-
content = this.session.render(th);
|
|
125
|
+
content = this.session.render(th, bodyW);
|
|
126
126
|
}
|
|
127
127
|
|
|
128
128
|
const lines: string[] = [];
|
|
@@ -140,7 +140,7 @@ export class UsageWindow {
|
|
|
140
140
|
return lines;
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
-
private renderQuotaLines(): string[] {
|
|
143
|
+
private renderQuotaLines(contentWidth: number): string[] {
|
|
144
144
|
const th = this.theme;
|
|
145
145
|
const lines: string[] = [];
|
|
146
146
|
|
|
@@ -156,7 +156,7 @@ export class UsageWindow {
|
|
|
156
156
|
const provider = this.orderedProviders[i]!;
|
|
157
157
|
const report = this.reports.get(provider);
|
|
158
158
|
if (report) {
|
|
159
|
-
for (const line of fullReport(report, provider).split("\n")) {
|
|
159
|
+
for (const line of fullReport(report, provider, contentWidth).split("\n")) {
|
|
160
160
|
lines.push(` ${line}`);
|
|
161
161
|
}
|
|
162
162
|
} else {
|