@aaroncarry/pi-usage 0.1.0
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/LICENSE +21 -0
- package/README.md +151 -0
- package/README.zh-CN.md +152 -0
- package/package.json +48 -0
- package/src/config.ts +156 -0
- package/src/credentials.ts +217 -0
- package/src/format.ts +35 -0
- package/src/index.ts +168 -0
- package/src/parse.ts +21 -0
- package/src/providers/anthropic.ts +76 -0
- package/src/providers/auto-detect.ts +343 -0
- package/src/providers/codex.ts +81 -0
- package/src/providers/custom.ts +100 -0
- package/src/providers/deepseek.ts +68 -0
- package/src/providers/github-copilot.ts +101 -0
- package/src/providers/index.ts +11 -0
- package/src/providers/openrouter.ts +52 -0
- package/src/providers/zai.ts +200 -0
- package/src/service.ts +275 -0
- package/src/session-usage.ts +57 -0
- package/src/types.ts +83 -0
- package/src/ui/card.ts +82 -0
- package/src/ui/statusline.ts +77 -0
package/src/ui/card.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* /usage result card: a custom session entry rendered inline in the chat
|
|
3
|
+
* transcript (pi convention for info commands — no focus capture, survives
|
|
4
|
+
* reload/session restore). Content mirrors what the old overlay showed:
|
|
5
|
+
* per-account plan, usage window bars, monetary balances, and errors, with
|
|
6
|
+
* the active account first.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { Box, Text, type Component } from "@earendil-works/pi-tui";
|
|
10
|
+
import { formatBar, formatMoney, formatResetSuffix } from "../format.ts";
|
|
11
|
+
import type { AccountBalance } from "../types.ts";
|
|
12
|
+
import type { ThemeLike } from "./statusline.ts";
|
|
13
|
+
|
|
14
|
+
export interface UsageCardData {
|
|
15
|
+
balances: AccountBalance[];
|
|
16
|
+
activeProviderId?: string;
|
|
17
|
+
/** Epoch ms when the snapshot was taken. */
|
|
18
|
+
generatedAt: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function displayTitle(balance: AccountBalance): string {
|
|
22
|
+
return balance.plan ? `${balance.label} (${balance.plan})` : balance.label;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function buildUsageCard(data: UsageCardData, theme: ThemeLike): Component {
|
|
26
|
+
const box = new Box(1, 1, (text) => theme.bg("customMessageBg", text));
|
|
27
|
+
const time = new Date(data.generatedAt).toLocaleTimeString(undefined, { hour: "2-digit", minute: "2-digit" });
|
|
28
|
+
box.addChild(new Text(`${theme.fg("accent", theme.bold("Usage"))} ${theme.fg("dim", `· ${time}`)}`, 0, 0));
|
|
29
|
+
|
|
30
|
+
const ids = data.balances.map((balance) => balance.providerId);
|
|
31
|
+
if (ids.length === 0) {
|
|
32
|
+
box.addChild(new Text(theme.fg("muted", "No configured accounts (nothing usable in auth.json)"), 0, 1));
|
|
33
|
+
}
|
|
34
|
+
const ordered = orderActiveFirst(data.balances, data.activeProviderId);
|
|
35
|
+
for (const balance of ordered) {
|
|
36
|
+
const marker = balance.providerId === data.activeProviderId ? theme.fg("accent", "●") : theme.fg("dim", "○");
|
|
37
|
+
const titleText =
|
|
38
|
+
balance.providerId === data.activeProviderId
|
|
39
|
+
? theme.fg("accent", theme.bold(displayTitle(balance)))
|
|
40
|
+
: theme.fg("muted", displayTitle(balance));
|
|
41
|
+
box.addChild(new Text(`${marker} ${titleText}`, 0, 1));
|
|
42
|
+
for (const line of renderAccount(balance, theme)) {
|
|
43
|
+
box.addChild(line);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return box;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function orderActiveFirst(balances: AccountBalance[], activeProviderId?: string): AccountBalance[] {
|
|
50
|
+
if (!activeProviderId) return [...balances];
|
|
51
|
+
const active = balances.filter((balance) => balance.providerId === activeProviderId);
|
|
52
|
+
const rest = balances.filter((balance) => balance.providerId !== activeProviderId);
|
|
53
|
+
return [...active, ...rest];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function renderAccount(balance: AccountBalance, theme: ThemeLike): Text[] {
|
|
57
|
+
if (balance.error) {
|
|
58
|
+
return [new Text(theme.fg("error", ` ${balance.error}`), 0, 0)];
|
|
59
|
+
}
|
|
60
|
+
const children: Text[] = [];
|
|
61
|
+
for (const window of balance.windows) {
|
|
62
|
+
const color = window.usedPercent >= 90 ? "error" : window.usedPercent >= 70 ? "warning" : "accent";
|
|
63
|
+
const barLine =
|
|
64
|
+
` ${theme.fg("dim", window.label.padEnd(8))}${theme.fg(color, formatBar(window.usedPercent))}` +
|
|
65
|
+
` ${String(Math.round(window.usedPercent)).padStart(3)}%${theme.fg("dim", formatResetSuffix(window.resetsAt))}`;
|
|
66
|
+
children.push(new Text(barLine, 0, 0));
|
|
67
|
+
}
|
|
68
|
+
if (balance.balance) {
|
|
69
|
+
const note = balance.balance.note ? ` ${theme.fg("dim", balance.balance.note)}` : "";
|
|
70
|
+
children.push(
|
|
71
|
+
new Text(
|
|
72
|
+
` ${theme.fg("dim", "Balance")}${theme.fg("success", ` ${formatMoney(balance.balance)}`)}${note}`,
|
|
73
|
+
0,
|
|
74
|
+
0,
|
|
75
|
+
),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
for (const noteEntry of balance.notes) {
|
|
79
|
+
children.push(new Text(theme.fg("dim", ` ${noteEntry}`), 0, 0));
|
|
80
|
+
}
|
|
81
|
+
return children;
|
|
82
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Footer status line rendering: one dense row injected via
|
|
3
|
+
* ctx.ui.setStatus("usage", ...). The default ("all") shows every configured
|
|
4
|
+
* account on that row — active account first and unstyled, the rest dimmed —
|
|
5
|
+
* so the dedicated footer line carries full value. "active" shows only the
|
|
6
|
+
* active account with all of its windows; "off" hides the row.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { StatusMode } from "../config.ts";
|
|
10
|
+
import { formatMoney } from "../format.ts";
|
|
11
|
+
import { formatTokens, type SessionUsageTotals } from "../session-usage.ts";
|
|
12
|
+
import type { AccountBalance } from "../types.ts";
|
|
13
|
+
|
|
14
|
+
/** Structural subset of pi's Theme used by the status line/panel. */
|
|
15
|
+
export interface ThemeLike {
|
|
16
|
+
fg(color: "dim" | "muted" | "accent" | "success" | "warning" | "error", text: string): string;
|
|
17
|
+
bold(text: string): string;
|
|
18
|
+
bg(color: "customMessageBg", text: string): string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const MAX_STATUS_SEGMENTS = 6;
|
|
22
|
+
|
|
23
|
+
export function orderActiveFirst(balances: AccountBalance[], activeProviderId?: string): AccountBalance[] {
|
|
24
|
+
if (!activeProviderId) return [...balances];
|
|
25
|
+
const active = balances.filter((balance) => balance.providerId === activeProviderId);
|
|
26
|
+
const rest = balances.filter((balance) => balance.providerId !== activeProviderId);
|
|
27
|
+
return [...active, ...rest];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Compact per-account summary, e.g. "Codex 5h 13%" or "GLM ¥21.46". */
|
|
31
|
+
export function accountSummary(balance: AccountBalance, options?: { allWindows?: boolean }): string {
|
|
32
|
+
if (balance.error) return `${balance.label} !`;
|
|
33
|
+
const parts: string[] = [];
|
|
34
|
+
const windows = options?.allWindows ? balance.windows : balance.windows.slice(0, 1);
|
|
35
|
+
for (const window of windows) {
|
|
36
|
+
parts.push(`${window.label} ${Math.round(window.usedPercent)}%`);
|
|
37
|
+
}
|
|
38
|
+
if (balance.balance) parts.push(formatMoney(balance.balance));
|
|
39
|
+
return parts.length > 0 ? `${balance.label} ${parts.join(" · ")}` : balance.label;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Consumption segment (plan B): session totals, tokens always and real cost
|
|
44
|
+
* only when non-zero, e.g. "session 87k tok · $0.020".
|
|
45
|
+
*/
|
|
46
|
+
export function formatConsumptionSegment(consumption: SessionUsageTotals | undefined, theme: ThemeLike): string | undefined {
|
|
47
|
+
if (!consumption || (consumption.tokens === 0 && consumption.cost === 0)) return undefined;
|
|
48
|
+
let text = theme.fg("dim", `session ${formatTokens(consumption.tokens)} tok`);
|
|
49
|
+
if (consumption.cost > 0) {
|
|
50
|
+
text += ` ${theme.fg("success", `$${consumption.cost.toFixed(3)}`)}`;
|
|
51
|
+
}
|
|
52
|
+
return text;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function formatStatusLine(options: {
|
|
56
|
+
balances: AccountBalance[];
|
|
57
|
+
mode: StatusMode;
|
|
58
|
+
activeProviderId?: string;
|
|
59
|
+
theme: ThemeLike;
|
|
60
|
+
consumption?: SessionUsageTotals;
|
|
61
|
+
}): string | undefined {
|
|
62
|
+
if (options.mode === "off") return undefined;
|
|
63
|
+
const ordered = orderActiveFirst(options.balances, options.activeProviderId);
|
|
64
|
+
const selected =
|
|
65
|
+
options.mode === "active"
|
|
66
|
+
? ordered.filter((balance) => balance.providerId === options.activeProviderId)
|
|
67
|
+
: ordered;
|
|
68
|
+
const segments = selected.slice(0, MAX_STATUS_SEGMENTS).map((balance) => {
|
|
69
|
+
const text = accountSummary(balance, { allWindows: options.mode === "active" });
|
|
70
|
+
if (balance.error) return options.theme.fg("error", text);
|
|
71
|
+
return balance.providerId === options.activeProviderId ? text : options.theme.fg("dim", text);
|
|
72
|
+
});
|
|
73
|
+
const consumptionSegment = formatConsumptionSegment(options.consumption, options.theme);
|
|
74
|
+
if (consumptionSegment) segments.push(consumptionSegment);
|
|
75
|
+
if (segments.length === 0) return undefined;
|
|
76
|
+
return segments.join(options.theme.fg("dim", " · "));
|
|
77
|
+
}
|