@narumitw/pi-usage 0.60.6 → 0.60.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/dist/index.ts +104 -363
- package/dist/index.ts.map +2 -2
- package/docs/providers.md +2 -1
- package/package.json +73 -74
- package/src/codex-fast-runtime.ts +148 -184
- package/src/codex-fast.ts +74 -83
- package/src/codex-resets.ts +249 -278
- package/src/core.ts +204 -206
- package/src/format.ts +519 -574
- package/src/index.ts +77 -77
- package/src/oauth-credential-source.ts +57 -65
- package/src/providers/baseten.ts +32 -36
- package/src/providers/codex.ts +109 -120
- package/src/providers/deepseek.ts +59 -64
- package/src/providers/fireworks.ts +219 -238
- package/src/providers/github-copilot.ts +89 -95
- package/src/providers/kimi-coding.ts +202 -213
- package/src/providers/minimax.ts +214 -219
- package/src/providers/moonshot.ts +41 -46
- package/src/providers/opencode-zen.ts +56 -59
- package/src/providers/openrouter.ts +50 -53
- package/src/providers/vercel-ai-gateway.ts +33 -33
- package/src/providers/xai.ts +141 -148
- package/src/providers/zai-errors.ts +75 -75
- package/src/providers/zai.ts +173 -189
- package/src/query.ts +887 -967
- package/src/settings.ts +275 -304
- package/src/types.ts +145 -145
- package/src/usage-helpers.ts +18 -18
- package/src/usage-settings-ui.ts +118 -145
- package/src/usage-targets.ts +98 -114
- package/src/usage.ts +1264 -1426
|
@@ -2,82 +2,79 @@ import { sanitizeDisplayText } from "../core.js";
|
|
|
2
2
|
import type { OpenCodeZenPayload, UsageBucket, UsageReport } from "../types.js";
|
|
3
3
|
|
|
4
4
|
const ZEN_WINDOWS = [
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
{ key: "rolling", label: "Rolling" },
|
|
6
|
+
{ key: "weekly", label: "Weekly" },
|
|
7
|
+
{ key: "monthly", label: "Monthly" },
|
|
8
8
|
] as const;
|
|
9
9
|
|
|
10
|
-
export function normalizeOpenCodeZenPayload(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
): UsageReport {
|
|
14
|
-
const usage = asObject(payload.usage);
|
|
15
|
-
if (!usage) throw new Error("OpenCode Zen usage response was not an object.");
|
|
10
|
+
export function normalizeOpenCodeZenPayload(payload: OpenCodeZenPayload, capturedAt: number): UsageReport {
|
|
11
|
+
const usage = asObject(payload.usage);
|
|
12
|
+
if (!usage) throw new Error("OpenCode Zen usage response was not an object.");
|
|
16
13
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
14
|
+
const buckets: UsageBucket[] = [];
|
|
15
|
+
const notes: string[] = [];
|
|
16
|
+
for (const window of ZEN_WINDOWS) {
|
|
17
|
+
const raw = asObject(usage[window.key]);
|
|
18
|
+
if (!raw) continue;
|
|
19
|
+
const status = asString(raw.status);
|
|
20
|
+
if (status !== "ok" && status !== "rate-limited") {
|
|
21
|
+
notes.push(`${window.label} window unavailable (${status ?? "unknown status"}).`);
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const used = asNonnegativeNumber(raw.percent);
|
|
25
|
+
if (used === undefined) continue;
|
|
26
|
+
const resetsAt = asEpochSeconds(raw.resetsAt);
|
|
27
|
+
buckets.push({
|
|
28
|
+
id: window.key,
|
|
29
|
+
label: `${window.label} window`,
|
|
30
|
+
used,
|
|
31
|
+
remaining: 100 - clampPercent(used),
|
|
32
|
+
limit: 100,
|
|
33
|
+
unit: "percent",
|
|
34
|
+
...(resetsAt !== undefined ? { resetsAt } : {}),
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (buckets.length === 0) {
|
|
38
|
+
throw new Error("OpenCode Zen usage endpoint returned no displayable usage data.");
|
|
39
|
+
}
|
|
43
40
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
41
|
+
return {
|
|
42
|
+
providerId: "opencode-go",
|
|
43
|
+
providerName: "OpenCode Go",
|
|
44
|
+
capturedAt,
|
|
45
|
+
source: "opencode-zen-usage",
|
|
46
|
+
semantics: {
|
|
47
|
+
kind: "consumer-subscription",
|
|
48
|
+
label: "OpenCode Zen plan usage",
|
|
49
|
+
},
|
|
50
|
+
buckets,
|
|
51
|
+
metrics: [],
|
|
52
|
+
...(notes.length > 0 ? { notes } : {}),
|
|
53
|
+
};
|
|
57
54
|
}
|
|
58
55
|
|
|
59
56
|
function asObject(value: unknown): Record<string, unknown> | undefined {
|
|
60
|
-
|
|
61
|
-
|
|
57
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
|
|
58
|
+
return value as Record<string, unknown>;
|
|
62
59
|
}
|
|
63
60
|
|
|
64
61
|
function asString(value: unknown): string | undefined {
|
|
65
|
-
|
|
66
|
-
|
|
62
|
+
if (typeof value !== "string") return undefined;
|
|
63
|
+
return sanitizeDisplayText(value, 80) || undefined;
|
|
67
64
|
}
|
|
68
65
|
|
|
69
66
|
function asNonnegativeNumber(value: unknown): number | undefined {
|
|
70
|
-
|
|
71
|
-
|
|
67
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 0) return undefined;
|
|
68
|
+
return value;
|
|
72
69
|
}
|
|
73
70
|
|
|
74
71
|
function asEpochSeconds(value: unknown): number | undefined {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
72
|
+
if (typeof value !== "string" || !value.trim()) return undefined;
|
|
73
|
+
const parsed = Date.parse(value);
|
|
74
|
+
if (Number.isNaN(parsed)) return undefined;
|
|
75
|
+
return Math.floor(parsed / 1000);
|
|
79
76
|
}
|
|
80
77
|
|
|
81
78
|
function clampPercent(value: number): number {
|
|
82
|
-
|
|
79
|
+
return Math.min(100, Math.max(0, value));
|
|
83
80
|
}
|
|
@@ -1,73 +1,70 @@
|
|
|
1
1
|
import { sanitizeDisplayText } from "../core.js";
|
|
2
2
|
import type { OpenRouterKeyPayload, UsageBucket, UsageMetric, UsageReport } from "../types.js";
|
|
3
3
|
|
|
4
|
-
export function normalizeOpenRouterKeyPayload(
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
): UsageReport {
|
|
8
|
-
const data = asObject(payload.data);
|
|
9
|
-
if (!data) throw new Error("OpenRouter key response data was not an object.");
|
|
4
|
+
export function normalizeOpenRouterKeyPayload(payload: OpenRouterKeyPayload, capturedAt: number): UsageReport {
|
|
5
|
+
const data = asObject(payload.data);
|
|
6
|
+
if (!data) throw new Error("OpenRouter key response data was not an object.");
|
|
10
7
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
8
|
+
const limit = asNonnegativeNumber(data.limit);
|
|
9
|
+
const remaining = asNonnegativeNumber(data.limit_remaining);
|
|
10
|
+
const period = asString(data.limit_reset);
|
|
11
|
+
const totalUsage = asNonnegativeNumber(data.usage);
|
|
12
|
+
const buckets: UsageBucket[] = [];
|
|
13
|
+
if (limit !== undefined) {
|
|
14
|
+
buckets.push({
|
|
15
|
+
id: "key-limit",
|
|
16
|
+
label: "Key limit",
|
|
17
|
+
...(remaining !== undefined ? { used: Math.max(0, limit - remaining), remaining } : {}),
|
|
18
|
+
limit,
|
|
19
|
+
unit: "usd",
|
|
20
|
+
...(period ? { period } : {}),
|
|
21
|
+
});
|
|
22
|
+
}
|
|
26
23
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
24
|
+
const metrics: UsageMetric[] = [];
|
|
25
|
+
addUsageMetric(metrics, "usage-daily", "Usage today", data.usage_daily);
|
|
26
|
+
addUsageMetric(metrics, "usage-weekly", "Usage this week", data.usage_weekly);
|
|
27
|
+
addUsageMetric(metrics, "usage-monthly", "Usage this month", data.usage_monthly);
|
|
28
|
+
addUsageMetric(metrics, "usage-total", "All-time usage", totalUsage);
|
|
29
|
+
if (buckets.length === 0 && metrics.length === 0) {
|
|
30
|
+
throw new Error("OpenRouter key response returned no displayable usage data.");
|
|
31
|
+
}
|
|
35
32
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
const notes: string[] = [];
|
|
34
|
+
if (data.limit === null) notes.push("No per-key spend cap");
|
|
35
|
+
else if (limit === undefined) notes.push("Per-key spend cap unavailable");
|
|
36
|
+
if (data.is_free_tier === true) notes.push("Free-tier API key");
|
|
40
37
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
38
|
+
return {
|
|
39
|
+
providerId: "openrouter",
|
|
40
|
+
providerName: "OpenRouter",
|
|
41
|
+
capturedAt,
|
|
42
|
+
source: "openrouter-key",
|
|
43
|
+
semantics: { kind: "api-key", label: "API-key spend limits" },
|
|
44
|
+
accountLabel: asString(data.label),
|
|
45
|
+
buckets,
|
|
46
|
+
metrics,
|
|
47
|
+
...(notes.length > 0 ? { notes } : {}),
|
|
48
|
+
};
|
|
52
49
|
}
|
|
53
50
|
|
|
54
51
|
function addUsageMetric(metrics: UsageMetric[], id: string, label: string, value: unknown): void {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
52
|
+
const amount = typeof value === "number" ? asNonnegativeNumber(value) : undefined;
|
|
53
|
+
if (amount === undefined) return;
|
|
54
|
+
metrics.push({ id, label, value: amount, unit: "usd" });
|
|
58
55
|
}
|
|
59
56
|
|
|
60
57
|
function asObject(value: unknown): Record<string, unknown> | undefined {
|
|
61
|
-
|
|
62
|
-
|
|
58
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
|
|
59
|
+
return value as Record<string, unknown>;
|
|
63
60
|
}
|
|
64
61
|
|
|
65
62
|
function asString(value: unknown): string | undefined {
|
|
66
|
-
|
|
67
|
-
|
|
63
|
+
if (typeof value !== "string") return undefined;
|
|
64
|
+
return sanitizeDisplayText(value, 80) || undefined;
|
|
68
65
|
}
|
|
69
66
|
|
|
70
67
|
function asNonnegativeNumber(value: unknown): number | undefined {
|
|
71
|
-
|
|
72
|
-
|
|
68
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 0) return undefined;
|
|
69
|
+
return value;
|
|
73
70
|
}
|
|
@@ -3,41 +3,41 @@ import type { UsageMetric, UsageReport, VercelAIGatewayCreditsPayload } from "..
|
|
|
3
3
|
const DECIMAL_AMOUNT = /^(?:0|[1-9]\d*)(?:\.\d+)?$/u;
|
|
4
4
|
|
|
5
5
|
export function normalizeVercelAIGatewayCreditsPayload(
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
payload: VercelAIGatewayCreditsPayload,
|
|
7
|
+
capturedAt: number,
|
|
8
8
|
): UsageReport {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
9
|
+
const balance = decimalAmount(payload.balance, "balance");
|
|
10
|
+
const totalUsed = decimalAmount(payload.total_used, "total used");
|
|
11
|
+
const metrics: UsageMetric[] = [
|
|
12
|
+
{
|
|
13
|
+
id: "credit-balance",
|
|
14
|
+
label: "Credit balance",
|
|
15
|
+
value: balance,
|
|
16
|
+
unit: "currency",
|
|
17
|
+
currency: "USD",
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
id: "lifetime-spend",
|
|
21
|
+
label: "Lifetime spend",
|
|
22
|
+
value: totalUsed,
|
|
23
|
+
unit: "currency",
|
|
24
|
+
currency: "USD",
|
|
25
|
+
},
|
|
26
|
+
];
|
|
27
|
+
return {
|
|
28
|
+
providerId: "vercel-ai-gateway",
|
|
29
|
+
providerName: "Vercel AI Gateway",
|
|
30
|
+
capturedAt,
|
|
31
|
+
source: "vercel-ai-gateway-credits",
|
|
32
|
+
semantics: { kind: "api-key", label: "AI Gateway credits and lifetime spend" },
|
|
33
|
+
buckets: [],
|
|
34
|
+
metrics,
|
|
35
|
+
};
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
function decimalAmount(value: unknown, label: string): string {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
if (typeof value !== "string" || value.length > 64 || !DECIMAL_AMOUNT.test(value)) {
|
|
40
|
+
throw new Error(`Vercel AI Gateway ${label} was not a valid nonnegative amount.`);
|
|
41
|
+
}
|
|
42
|
+
return value;
|
|
43
43
|
}
|