@pify/usage 0.2.0 → 0.3.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/README.md +13 -1
- package/extensions/usage.ts +34 -6
- package/package.json +1 -1
- package/src/quota.ts +124 -0
package/README.md
CHANGED
|
@@ -31,7 +31,19 @@ By project (all time)
|
|
|
31
31
|
- **Per-project spend** (v0.2): pi stores sessions one directory per project, so the dashboard can show where the money actually went — the top 5 projects by cost, all time.
|
|
32
32
|
- **`usage_status` tool**: the agent can check session + today totals before committing to expensive work (subagent fan-outs, large reads).
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
## `/usage quota` (v0.3)
|
|
35
|
+
|
|
36
|
+
The one command in this package that touches the network, and only when you run it:
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
Quota (openrouter · sk-or-v1-395...563)
|
|
40
|
+
spent $0.32 (no credit limit on this key)
|
|
41
|
+
window day $0.32 · week $0.32 · month $0.32
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The key is read from the same `~/.pi/agent/auth.json` pi already uses (or `OPENROUTER_API_KEY`) — no second place to configure credentials — and only the provider's own masked label is ever printed. An 8-second timeout, and any failure renders as `unavailable — HTTP 401` rather than throwing.
|
|
45
|
+
|
|
46
|
+
Only OpenRouter is implemented. The other providers report opaque rate-limit windows rather than a balance, and chasing all of them costs ~18k lines of per-provider contract maintenance (see `@narumitw/pi-usage` if you need them today).
|
|
35
47
|
|
|
36
48
|
## License
|
|
37
49
|
|
package/extensions/usage.ts
CHANGED
|
@@ -10,9 +10,12 @@
|
|
|
10
10
|
* tmustier's lesson) with a per-file mtime cache. The usage_status tool
|
|
11
11
|
* lets the agent itself check consumption mid-session.
|
|
12
12
|
*
|
|
13
|
-
*
|
|
14
|
-
* v0.
|
|
15
|
-
*
|
|
13
|
+
* Everything above is local: no network, no LLM tokens. The single exception
|
|
14
|
+
* is /usage quota (v0.3), which asks OpenRouter what this key has spent —
|
|
15
|
+
* opt-in per call, 8s timeout, and a failure prints as "unavailable" beside
|
|
16
|
+
* the local numbers. Other providers stay out: @narumitw/pi-usage shows the
|
|
17
|
+
* full set costs ~18k lines of per-provider contract chasing, and OpenRouter
|
|
18
|
+
* is the one that reports a real balance rather than an opaque window.
|
|
16
19
|
*/
|
|
17
20
|
import {
|
|
18
21
|
getAgentDir,
|
|
@@ -21,9 +24,11 @@ import {
|
|
|
21
24
|
} from "@earendil-works/pi-coding-agent";
|
|
22
25
|
import { Type } from "typebox";
|
|
23
26
|
import { join } from "node:path";
|
|
27
|
+
import { readFileSync } from "node:fs";
|
|
24
28
|
|
|
25
29
|
import { addRecord, aggregate, recordFromEntry, windowTotals } from "../src/aggregate.ts";
|
|
26
30
|
import { footerText, formatCost, formatTokens, historyBlock, sessionBlock } from "../src/format.ts";
|
|
31
|
+
import { fetchOpenRouterQuota, quotaBlock } from "../src/quota.ts";
|
|
27
32
|
import { scanSessions } from "../src/sessions.ts";
|
|
28
33
|
import { emptyTotals, isRecord, type UsageTotals } from "../src/types.ts";
|
|
29
34
|
|
|
@@ -86,10 +91,33 @@ export default function usage(pi: ExtensionAPI) {
|
|
|
86
91
|
|
|
87
92
|
// ── Command & tool ───────────────────────────────────────────────────
|
|
88
93
|
|
|
94
|
+
/**
|
|
95
|
+
* The key pi itself uses, read from the same auth.json — no second place to
|
|
96
|
+
* configure credentials, and no key is ever printed.
|
|
97
|
+
*/
|
|
98
|
+
function providerKey(provider: string): string {
|
|
99
|
+
try {
|
|
100
|
+
const auth = JSON.parse(readFileSync(join(getAgentDir(), "auth.json"), "utf8")) as Record<string, unknown>;
|
|
101
|
+
const entry = auth[provider];
|
|
102
|
+
if (isRecord(entry) && typeof entry.key === "string") return entry.key;
|
|
103
|
+
} catch {
|
|
104
|
+
// no auth.json, or unreadable — fall through to the environment
|
|
105
|
+
}
|
|
106
|
+
return process.env.OPENROUTER_API_KEY ?? "";
|
|
107
|
+
}
|
|
108
|
+
|
|
89
109
|
pi.registerCommand("usage", {
|
|
90
|
-
description: "Token and cost dashboard:
|
|
91
|
-
handler: async (
|
|
92
|
-
if (ctx.hasUI)
|
|
110
|
+
description: "Token and cost dashboard: /usage [quota]",
|
|
111
|
+
handler: async (args, ctx) => {
|
|
112
|
+
if (!ctx.hasUI) return;
|
|
113
|
+
if ((args ?? "").trim().toLowerCase() === "quota") {
|
|
114
|
+
// The one networked call in this package, and only when asked for.
|
|
115
|
+
ctx.ui.notify("Checking provider quota…", "info");
|
|
116
|
+
const result = await fetchOpenRouterQuota(providerKey("openrouter"));
|
|
117
|
+
ctx.ui.notify(quotaBlock(result), result.ok ? "info" : "warning");
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
ctx.ui.notify(dashboard(ctx), "info");
|
|
93
121
|
},
|
|
94
122
|
});
|
|
95
123
|
|
package/package.json
CHANGED
package/src/quota.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider quota (v0.3). Everything else in this package is computed from
|
|
3
|
+
* local files — this is the one place that talks to a network, so it is
|
|
4
|
+
* opt-in per call, short-timeout, and never blocks the dashboard: a provider
|
|
5
|
+
* that is slow or down shows as unavailable next to the local numbers.
|
|
6
|
+
*
|
|
7
|
+
* Only OpenRouter is implemented. @narumitw/pi-usage shows what the full set
|
|
8
|
+
* costs — roughly 18k lines of per-provider contract chasing — and OpenRouter
|
|
9
|
+
* is the one endpoint that reports a real balance rather than an opaque
|
|
10
|
+
* rate-limit window.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { finite, isRecord } from "./types.ts";
|
|
14
|
+
|
|
15
|
+
export interface QuotaInfo {
|
|
16
|
+
provider: string;
|
|
17
|
+
/** Spend on this key, in USD, as the provider reports it. */
|
|
18
|
+
used: number | null;
|
|
19
|
+
/** Hard credit limit, when the key has one. */
|
|
20
|
+
limit: number | null;
|
|
21
|
+
remaining: number | null;
|
|
22
|
+
/** Rolling-window spend, when reported. */
|
|
23
|
+
daily: number | null;
|
|
24
|
+
weekly: number | null;
|
|
25
|
+
monthly: number | null;
|
|
26
|
+
label: string | null;
|
|
27
|
+
freeTier: boolean | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type QuotaResult =
|
|
31
|
+
| { ok: true; quota: QuotaInfo }
|
|
32
|
+
| { ok: false; provider: string; reason: string };
|
|
33
|
+
|
|
34
|
+
export const QUOTA_TIMEOUT_MS = 8000;
|
|
35
|
+
|
|
36
|
+
function num(value: unknown): number | null {
|
|
37
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Shape the /api/v1/key payload. OpenRouter reports `limit: null` for keys
|
|
42
|
+
* with no cap, so "no limit" and "limit of zero" must not collapse together.
|
|
43
|
+
*/
|
|
44
|
+
export function parseOpenRouterKey(payload: unknown): QuotaInfo | null {
|
|
45
|
+
if (!isRecord(payload)) return null;
|
|
46
|
+
// The endpoint wraps its fields in `data`; a flat body is accepted too, but
|
|
47
|
+
// a `data` that is present and not an object means the shape changed.
|
|
48
|
+
if ("data" in payload && !isRecord(payload.data)) return null;
|
|
49
|
+
const data = isRecord(payload.data) ? payload.data : payload;
|
|
50
|
+
|
|
51
|
+
const used = num(data.usage);
|
|
52
|
+
const limit = num(data.limit);
|
|
53
|
+
const remaining = num(data.limit_remaining) ?? (limit !== null && used !== null ? limit - used : null);
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
provider: "openrouter",
|
|
57
|
+
used: used === null ? null : finite(used),
|
|
58
|
+
limit,
|
|
59
|
+
remaining,
|
|
60
|
+
daily: num(data.usage_daily),
|
|
61
|
+
weekly: num(data.usage_weekly),
|
|
62
|
+
monthly: num(data.usage_monthly),
|
|
63
|
+
label: typeof data.label === "string" ? data.label : null,
|
|
64
|
+
freeTier: typeof data.is_free_tier === "boolean" ? data.is_free_tier : null,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export type Fetcher = (url: string, init: { headers: Record<string, string>; signal: AbortSignal }) => Promise<{
|
|
69
|
+
ok: boolean;
|
|
70
|
+
status: number;
|
|
71
|
+
json(): Promise<unknown>;
|
|
72
|
+
}>;
|
|
73
|
+
|
|
74
|
+
/** Fetch the OpenRouter key status. Never throws — failure is a result. */
|
|
75
|
+
export async function fetchOpenRouterQuota(
|
|
76
|
+
apiKey: string,
|
|
77
|
+
fetcher: Fetcher = globalThis.fetch as unknown as Fetcher,
|
|
78
|
+
timeoutMs = QUOTA_TIMEOUT_MS,
|
|
79
|
+
): Promise<QuotaResult> {
|
|
80
|
+
if (!apiKey.trim()) return { ok: false, provider: "openrouter", reason: "no API key configured" };
|
|
81
|
+
try {
|
|
82
|
+
const response = await fetcher("https://openrouter.ai/api/v1/key", {
|
|
83
|
+
headers: { Authorization: `Bearer ${apiKey.trim()}` },
|
|
84
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
85
|
+
});
|
|
86
|
+
if (!response.ok) {
|
|
87
|
+
return { ok: false, provider: "openrouter", reason: `HTTP ${response.status}` };
|
|
88
|
+
}
|
|
89
|
+
const quota = parseOpenRouterKey(await response.json());
|
|
90
|
+
if (!quota) return { ok: false, provider: "openrouter", reason: "unexpected response shape" };
|
|
91
|
+
return { ok: true, quota };
|
|
92
|
+
} catch (err) {
|
|
93
|
+
return {
|
|
94
|
+
ok: false,
|
|
95
|
+
provider: "openrouter",
|
|
96
|
+
reason: err instanceof Error ? err.message : String(err),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function money(value: number | null): string {
|
|
102
|
+
if (value === null) return "—";
|
|
103
|
+
if (value === 0) return "$0";
|
|
104
|
+
if (Math.abs(value) < 0.01) return "<$0.01";
|
|
105
|
+
return `$${value.toFixed(2)}`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function quotaBlock(result: QuotaResult): string {
|
|
109
|
+
if (!result.ok) {
|
|
110
|
+
return `Quota (${result.provider})\n unavailable — ${result.reason}`;
|
|
111
|
+
}
|
|
112
|
+
const q = result.quota;
|
|
113
|
+
const lines = [`Quota (${q.provider}${q.label ? ` · ${q.label}` : ""})`];
|
|
114
|
+
lines.push(
|
|
115
|
+
q.limit === null
|
|
116
|
+
? ` spent ${money(q.used)} (no credit limit on this key)`
|
|
117
|
+
: ` spent ${money(q.used)} of ${money(q.limit)} · ${money(q.remaining)} left`,
|
|
118
|
+
);
|
|
119
|
+
if (q.daily !== null || q.weekly !== null || q.monthly !== null) {
|
|
120
|
+
lines.push(` window day ${money(q.daily)} · week ${money(q.weekly)} · month ${money(q.monthly)}`);
|
|
121
|
+
}
|
|
122
|
+
if (q.freeTier) lines.push(" tier free");
|
|
123
|
+
return lines.join("\n");
|
|
124
|
+
}
|