@specode/pi-subscription-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 +23 -0
- package/README.md +116 -0
- package/README.zh-CN.md +116 -0
- package/THIRD_PARTY_NOTICES.md +22 -0
- package/index.ts +1 -0
- package/package.json +51 -0
- package/src/codex-reset-core.ts +249 -0
- package/src/codex-resets.ts +131 -0
- package/src/core.ts +182 -0
- package/src/format.ts +230 -0
- package/src/providers/codex.ts +157 -0
- package/src/providers/grok.ts +213 -0
- package/src/providers/kimi.ts +120 -0
- package/src/providers/opencode-go.ts +87 -0
- package/src/query.ts +390 -0
- package/src/status.ts +213 -0
- package/src/types.ts +83 -0
- package/src/usage.ts +555 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// Adapted from pi-grok-usage@1.0.4 (MIT).
|
|
2
|
+
import { sanitizeDisplayText } from "../core.ts";
|
|
3
|
+
import type { UsageBucket, UsageMetric, UsageReport } from "../types.ts";
|
|
4
|
+
|
|
5
|
+
const MAX_USER_ID_LENGTH = 256;
|
|
6
|
+
const MAX_LABEL_LENGTH = 80;
|
|
7
|
+
const MAX_CENTS = 1_000_000_000_000;
|
|
8
|
+
const USER_ID_PATTERN = /^[\x21-\x7e]+$/u;
|
|
9
|
+
|
|
10
|
+
export function normalizeGrokIdentity(payload: unknown): string {
|
|
11
|
+
const userId = asObject(payload)?.userId;
|
|
12
|
+
if (
|
|
13
|
+
typeof userId !== "string" ||
|
|
14
|
+
!userId ||
|
|
15
|
+
userId.length > MAX_USER_ID_LENGTH ||
|
|
16
|
+
!USER_ID_PATTERN.test(userId)
|
|
17
|
+
) {
|
|
18
|
+
throw new Error(
|
|
19
|
+
"xAI account identity could not be verified; billing was not requested.",
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
return userId;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function normalizeGrokBilling(
|
|
26
|
+
payload: unknown,
|
|
27
|
+
capturedAt: number,
|
|
28
|
+
): UsageReport {
|
|
29
|
+
const root = asObject(payload);
|
|
30
|
+
if (!root) throw new Error("Grok billing response was not an object.");
|
|
31
|
+
const config = root.config === undefined ? undefined : asObject(root.config);
|
|
32
|
+
if (root.config !== undefined && !config) {
|
|
33
|
+
throw new Error("Grok billing response contained an invalid config object.");
|
|
34
|
+
}
|
|
35
|
+
const included = createIncludedBucket(config);
|
|
36
|
+
const buckets = included ? [included] : [];
|
|
37
|
+
const metrics = collectMetrics(config);
|
|
38
|
+
if (buckets.length === 0 && metrics.length === 0) {
|
|
39
|
+
throw new Error("Grok billing endpoint returned no displayable usage data.");
|
|
40
|
+
}
|
|
41
|
+
const notes = collectNotes(root);
|
|
42
|
+
const report: UsageReport = {
|
|
43
|
+
providerId: "xai",
|
|
44
|
+
providerName: "Grok",
|
|
45
|
+
capturedAt,
|
|
46
|
+
source: "grok-cli-billing",
|
|
47
|
+
semantics: {
|
|
48
|
+
kind: "consumer-subscription",
|
|
49
|
+
label: "SuperGrok subscription usage",
|
|
50
|
+
},
|
|
51
|
+
buckets,
|
|
52
|
+
metrics,
|
|
53
|
+
};
|
|
54
|
+
if (notes.length > 0) report.notes = notes;
|
|
55
|
+
return report;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function createIncludedBucket(
|
|
59
|
+
config: Record<string, unknown> | undefined,
|
|
60
|
+
): UsageBucket | undefined {
|
|
61
|
+
const percent = boundedPercent(config?.creditUsagePercent);
|
|
62
|
+
const usedCents = boundedCents(config?.used);
|
|
63
|
+
const limitCents = boundedCents(config?.monthlyLimit);
|
|
64
|
+
let used = percent;
|
|
65
|
+
if (
|
|
66
|
+
used === undefined &&
|
|
67
|
+
usedCents !== undefined &&
|
|
68
|
+
limitCents !== undefined &&
|
|
69
|
+
limitCents > 0
|
|
70
|
+
) {
|
|
71
|
+
used = Math.min(100, (usedCents / limitCents) * 100);
|
|
72
|
+
}
|
|
73
|
+
if (used === undefined) return undefined;
|
|
74
|
+
const period = asObject(config?.currentPeriod);
|
|
75
|
+
const resetsAt = parseTimestamp(period?.end ?? config?.billingPeriodEnd);
|
|
76
|
+
const periodInfo = parseUsagePeriod(config, period);
|
|
77
|
+
const bucket: UsageBucket = {
|
|
78
|
+
id: "included",
|
|
79
|
+
label: "Included usage",
|
|
80
|
+
used,
|
|
81
|
+
remaining: 100 - used,
|
|
82
|
+
limit: 100,
|
|
83
|
+
unit: "percent",
|
|
84
|
+
...periodInfo,
|
|
85
|
+
};
|
|
86
|
+
if (resetsAt !== undefined) bucket.resetsAt = resetsAt;
|
|
87
|
+
return bucket;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function parseUsagePeriod(
|
|
91
|
+
config: Record<string, unknown> | undefined,
|
|
92
|
+
currentPeriod: Record<string, unknown> | undefined,
|
|
93
|
+
): Pick<UsageBucket, "period" | "windowMinutes"> {
|
|
94
|
+
const type = currentPeriod?.type;
|
|
95
|
+
if (typeof type === "string") {
|
|
96
|
+
if (type.toUpperCase().includes("WEEKLY")) {
|
|
97
|
+
return { period: "weekly", windowMinutes: 10_080 };
|
|
98
|
+
}
|
|
99
|
+
if (type.toUpperCase().includes("MONTHLY")) {
|
|
100
|
+
return { period: "monthly", windowMinutes: 43_200 };
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const start = parseTimestamp(
|
|
104
|
+
currentPeriod?.start ?? config?.billingPeriodStart,
|
|
105
|
+
);
|
|
106
|
+
const end = parseTimestamp(currentPeriod?.end ?? config?.billingPeriodEnd);
|
|
107
|
+
if (start === undefined || end === undefined || end <= start) return {};
|
|
108
|
+
const windowMinutes = Math.round((end - start) / 60);
|
|
109
|
+
if (windowMinutes >= 6 * 24 * 60 && windowMinutes <= 8 * 24 * 60) {
|
|
110
|
+
return { period: "weekly", windowMinutes };
|
|
111
|
+
}
|
|
112
|
+
if (windowMinutes >= 28 * 24 * 60 && windowMinutes <= 31 * 24 * 60) {
|
|
113
|
+
return { period: "monthly", windowMinutes };
|
|
114
|
+
}
|
|
115
|
+
return { windowMinutes };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function collectMetrics(
|
|
119
|
+
config: Record<string, unknown> | undefined,
|
|
120
|
+
): UsageMetric[] {
|
|
121
|
+
const metrics: UsageMetric[] = [];
|
|
122
|
+
addUsdMetric(
|
|
123
|
+
metrics,
|
|
124
|
+
"included-used",
|
|
125
|
+
"Included credits used",
|
|
126
|
+
boundedCents(config?.used),
|
|
127
|
+
);
|
|
128
|
+
addUsdMetric(
|
|
129
|
+
metrics,
|
|
130
|
+
"included-limit",
|
|
131
|
+
"Included credits limit",
|
|
132
|
+
boundedCents(config?.monthlyLimit),
|
|
133
|
+
);
|
|
134
|
+
addUsdMetric(
|
|
135
|
+
metrics,
|
|
136
|
+
"on-demand-used",
|
|
137
|
+
"On-demand credits used",
|
|
138
|
+
boundedCents(config?.onDemandUsed),
|
|
139
|
+
);
|
|
140
|
+
addUsdMetric(
|
|
141
|
+
metrics,
|
|
142
|
+
"on-demand-cap",
|
|
143
|
+
"On-demand credits cap",
|
|
144
|
+
boundedCents(config?.onDemandCap),
|
|
145
|
+
);
|
|
146
|
+
addUsdMetric(
|
|
147
|
+
metrics,
|
|
148
|
+
"prepaid-balance",
|
|
149
|
+
"Prepaid balance",
|
|
150
|
+
boundedCents(config?.prepaidBalance),
|
|
151
|
+
);
|
|
152
|
+
return metrics;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function collectNotes(root: Record<string, unknown>): string[] {
|
|
156
|
+
const notes: string[] = [];
|
|
157
|
+
const tier = boundedLabel(root.subscriptionTier);
|
|
158
|
+
if (tier) notes.push(`Plan: ${tier}`);
|
|
159
|
+
if (typeof root.onDemandEnabled === "boolean") {
|
|
160
|
+
notes.push(
|
|
161
|
+
`On-demand billing: ${root.onDemandEnabled ? "enabled" : "disabled"}`,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
return notes;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function addUsdMetric(
|
|
168
|
+
metrics: UsageMetric[],
|
|
169
|
+
id: string,
|
|
170
|
+
label: string,
|
|
171
|
+
cents: number | undefined,
|
|
172
|
+
): void {
|
|
173
|
+
if (cents === undefined) return;
|
|
174
|
+
metrics.push({ id, label, value: cents / 100, unit: "usd" });
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function asObject(value: unknown): Record<string, unknown> | undefined {
|
|
178
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
179
|
+
? (value as Record<string, unknown>)
|
|
180
|
+
: undefined;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function boundedLabel(value: unknown): string | undefined {
|
|
184
|
+
if (typeof value !== "string") return undefined;
|
|
185
|
+
const label = sanitizeDisplayText(value, MAX_LABEL_LENGTH);
|
|
186
|
+
return label || undefined;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function boundedPercent(value: unknown): number | undefined {
|
|
190
|
+
return typeof value === "number" &&
|
|
191
|
+
Number.isFinite(value) &&
|
|
192
|
+
value >= 0 &&
|
|
193
|
+
value <= 100
|
|
194
|
+
? value
|
|
195
|
+
: undefined;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function boundedCents(value: unknown): number | undefined {
|
|
199
|
+
const wrapper = asObject(value);
|
|
200
|
+
const raw = wrapper ? wrapper.val : value;
|
|
201
|
+
return typeof raw === "number" &&
|
|
202
|
+
Number.isSafeInteger(raw) &&
|
|
203
|
+
raw >= 0 &&
|
|
204
|
+
raw <= MAX_CENTS
|
|
205
|
+
? raw
|
|
206
|
+
: undefined;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function parseTimestamp(value: unknown): number | undefined {
|
|
210
|
+
if (typeof value !== "string" || value.length > 64) return undefined;
|
|
211
|
+
const parsed = Date.parse(value);
|
|
212
|
+
return Number.isNaN(parsed) ? undefined : Math.floor(parsed / 1000);
|
|
213
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// Adapted from @sumhou/pi-kimi-usage@1.0.1 (MIT).
|
|
2
|
+
import type { UsageBucket, UsageReport } from "../types.ts";
|
|
3
|
+
|
|
4
|
+
export function normalizeKimiUsage(
|
|
5
|
+
payload: unknown,
|
|
6
|
+
capturedAt: number,
|
|
7
|
+
): UsageReport {
|
|
8
|
+
const root = asObject(payload);
|
|
9
|
+
if (!root) throw new Error("Kimi usage response was not an object.");
|
|
10
|
+
const buckets: UsageBucket[] = [];
|
|
11
|
+
const weekly = parseQuota(root.usage);
|
|
12
|
+
if (weekly) {
|
|
13
|
+
buckets.push({
|
|
14
|
+
id: "weekly",
|
|
15
|
+
label: "Weekly window",
|
|
16
|
+
...weekly,
|
|
17
|
+
unit: "count",
|
|
18
|
+
windowMinutes: 10_080,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
const limits = Array.isArray(root.limits) ? root.limits : [];
|
|
22
|
+
for (const [index, item] of limits.entries()) {
|
|
23
|
+
const entry = asObject(item);
|
|
24
|
+
const detail = parseQuota(entry?.detail ?? entry);
|
|
25
|
+
if (!detail) continue;
|
|
26
|
+
const window = asObject(entry?.window);
|
|
27
|
+
const windowInfo = parseWindow(window);
|
|
28
|
+
buckets.push({
|
|
29
|
+
id: `rolling-${index}`,
|
|
30
|
+
label: `${windowInfo.label} window`,
|
|
31
|
+
...detail,
|
|
32
|
+
unit: "count",
|
|
33
|
+
...(windowInfo.minutes === undefined
|
|
34
|
+
? {}
|
|
35
|
+
: { windowMinutes: windowInfo.minutes }),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
if (buckets.length === 0) {
|
|
39
|
+
throw new Error("Kimi usage endpoint returned no displayable quota data.");
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
providerId: "kimi-coding",
|
|
43
|
+
providerName: "Kimi Coding",
|
|
44
|
+
capturedAt,
|
|
45
|
+
source: "kimi-coding-usage",
|
|
46
|
+
semantics: { kind: "consumer-subscription", label: "Kimi Coding plan usage" },
|
|
47
|
+
buckets,
|
|
48
|
+
metrics: [],
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function parseQuota(
|
|
53
|
+
value: unknown,
|
|
54
|
+
): Omit<UsageBucket, "id" | "label" | "unit"> | undefined {
|
|
55
|
+
const record = asObject(value);
|
|
56
|
+
if (!record) return undefined;
|
|
57
|
+
const limit = asFiniteNumber(record.limit);
|
|
58
|
+
const used = asFiniteNumber(record.used);
|
|
59
|
+
const remaining = asFiniteNumber(record.remaining);
|
|
60
|
+
if (
|
|
61
|
+
limit === undefined ||
|
|
62
|
+
used === undefined ||
|
|
63
|
+
remaining === undefined ||
|
|
64
|
+
limit <= 0 ||
|
|
65
|
+
used < 0 ||
|
|
66
|
+
remaining < 0
|
|
67
|
+
) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
const resetsAt = parseDate(record.resetTime);
|
|
71
|
+
return {
|
|
72
|
+
used,
|
|
73
|
+
remaining,
|
|
74
|
+
limit,
|
|
75
|
+
...(resetsAt === undefined ? {} : { resetsAt }),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function parseWindow(value: Record<string, unknown> | undefined): {
|
|
80
|
+
label: string;
|
|
81
|
+
minutes?: number;
|
|
82
|
+
} {
|
|
83
|
+
const duration = asFiniteNumber(value?.duration);
|
|
84
|
+
const unit = value?.timeUnit;
|
|
85
|
+
if (duration === undefined || duration <= 0 || typeof unit !== "string") {
|
|
86
|
+
return { label: "Rolling" };
|
|
87
|
+
}
|
|
88
|
+
if (unit === "TIME_UNIT_SECOND") {
|
|
89
|
+
return { label: `${duration}s`, minutes: duration / 60 };
|
|
90
|
+
}
|
|
91
|
+
if (unit === "TIME_UNIT_MINUTE") {
|
|
92
|
+
return {
|
|
93
|
+
label: duration % 60 === 0 ? `${duration / 60}h` : `${duration}m`,
|
|
94
|
+
minutes: duration,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
if (unit === "TIME_UNIT_HOUR")
|
|
98
|
+
return { label: `${duration}h`, minutes: duration * 60 };
|
|
99
|
+
if (unit === "TIME_UNIT_DAY")
|
|
100
|
+
return { label: `${duration}d`, minutes: duration * 1_440 };
|
|
101
|
+
return { label: "Rolling" };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function asObject(value: unknown): Record<string, unknown> | undefined {
|
|
105
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
106
|
+
? (value as Record<string, unknown>)
|
|
107
|
+
: undefined;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function asFiniteNumber(value: unknown): number | undefined {
|
|
111
|
+
if (typeof value !== "number" && typeof value !== "string") return undefined;
|
|
112
|
+
const parsed = Number(value);
|
|
113
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function parseDate(value: unknown): number | undefined {
|
|
117
|
+
if (typeof value !== "string" || !value.trim()) return undefined;
|
|
118
|
+
const parsed = Date.parse(value);
|
|
119
|
+
return Number.isNaN(parsed) ? undefined : Math.floor(parsed / 1000);
|
|
120
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// Adapted from @narumitw/pi-usage@0.53.0 (MIT).
|
|
2
|
+
import { sanitizeDisplayText } from "../core.ts";
|
|
3
|
+
import type { UsageBucket, UsageReport } from "../types.ts";
|
|
4
|
+
|
|
5
|
+
const WINDOWS = [
|
|
6
|
+
{ key: "rolling", label: "Rolling", windowMinutes: 300 },
|
|
7
|
+
{ key: "weekly", label: "Weekly", windowMinutes: 10_080 },
|
|
8
|
+
{ key: "monthly", label: "Monthly", windowMinutes: 43_200 },
|
|
9
|
+
] as const;
|
|
10
|
+
|
|
11
|
+
export function normalizeOpenCodeGoUsage(
|
|
12
|
+
payload: unknown,
|
|
13
|
+
capturedAt: number,
|
|
14
|
+
): UsageReport {
|
|
15
|
+
const root = asObject(payload);
|
|
16
|
+
const usage = asObject(root?.usage);
|
|
17
|
+
if (!usage) throw new Error("OpenCode Go usage response was not an object.");
|
|
18
|
+
const buckets: UsageBucket[] = [];
|
|
19
|
+
const notes: string[] = [];
|
|
20
|
+
for (const window of WINDOWS) {
|
|
21
|
+
const raw = asObject(usage[window.key]);
|
|
22
|
+
if (!raw) continue;
|
|
23
|
+
const status = asString(raw.status);
|
|
24
|
+
if (status !== "ok" && status !== "rate-limited") {
|
|
25
|
+
notes.push(
|
|
26
|
+
`${window.label} window unavailable (${status ?? "unknown status"}).`,
|
|
27
|
+
);
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
const used = asNonnegativeNumber(raw.percent);
|
|
31
|
+
if (used === undefined) continue;
|
|
32
|
+
const resetsAt = asEpochSeconds(raw.resetsAt);
|
|
33
|
+
buckets.push({
|
|
34
|
+
id: window.key,
|
|
35
|
+
label: `${window.label} window`,
|
|
36
|
+
used,
|
|
37
|
+
remaining: 100 - clampPercent(used),
|
|
38
|
+
limit: 100,
|
|
39
|
+
unit: "percent",
|
|
40
|
+
windowMinutes: window.windowMinutes,
|
|
41
|
+
...(resetsAt === undefined ? {} : { resetsAt }),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
if (buckets.length === 0) {
|
|
45
|
+
throw new Error(
|
|
46
|
+
"OpenCode Go usage endpoint returned no displayable usage data.",
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
providerId: "opencode-go",
|
|
51
|
+
providerName: "OpenCode Go",
|
|
52
|
+
capturedAt,
|
|
53
|
+
source: "opencode-go-usage",
|
|
54
|
+
semantics: { kind: "consumer-subscription", label: "OpenCode Go plan usage" },
|
|
55
|
+
buckets,
|
|
56
|
+
metrics: [],
|
|
57
|
+
...(notes.length > 0 ? { notes } : {}),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function asObject(value: unknown): Record<string, unknown> | undefined {
|
|
62
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
63
|
+
? (value as Record<string, unknown>)
|
|
64
|
+
: undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function asString(value: unknown): string | undefined {
|
|
68
|
+
return typeof value === "string"
|
|
69
|
+
? sanitizeDisplayText(value, 80) || undefined
|
|
70
|
+
: undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function asNonnegativeNumber(value: unknown): number | undefined {
|
|
74
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0
|
|
75
|
+
? value
|
|
76
|
+
: undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function asEpochSeconds(value: unknown): number | undefined {
|
|
80
|
+
if (typeof value !== "string" || !value.trim()) return undefined;
|
|
81
|
+
const parsed = Date.parse(value);
|
|
82
|
+
return Number.isNaN(parsed) ? undefined : Math.floor(parsed / 1000);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function clampPercent(value: number): number {
|
|
86
|
+
return Math.min(100, Math.max(0, value));
|
|
87
|
+
}
|