@fenlyin/pi-subscription-usage 0.0.0-stage → 2.0.1
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 +24 -0
- package/README.md +156 -3
- package/README.zh-CN.md +156 -0
- package/THIRD_PARTY_NOTICES.md +28 -0
- package/index.ts +1 -0
- package/package.json +52 -6
- package/src/codex-reset-core.ts +249 -0
- package/src/codex-resets.ts +145 -0
- package/src/config-core.ts +43 -0
- package/src/config.ts +48 -0
- package/src/core.ts +182 -0
- package/src/display.ts +55 -0
- package/src/format.ts +225 -0
- package/src/providers/claude-bridge.ts +118 -0
- package/src/providers/codex-constants.ts +2 -0
- package/src/providers/codex.ts +227 -0
- package/src/providers/grok.ts +358 -0
- package/src/providers/kimi.ts +155 -0
- package/src/providers/openai.ts +145 -0
- package/src/providers/opencode-go.ts +88 -0
- package/src/query.ts +511 -0
- package/src/status.ts +255 -0
- package/src/types.ts +90 -0
- package/src/usage.ts +679 -0
package/src/status.ts
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_USAGE_DISPLAY_MODE,
|
|
3
|
+
formatUsagePercent,
|
|
4
|
+
usagePercent,
|
|
5
|
+
usagePercentRemaining,
|
|
6
|
+
usagePercentUsed,
|
|
7
|
+
type UsageDisplayMode,
|
|
8
|
+
} from "./display.ts";
|
|
9
|
+
import type { UsageBucket, UsageModel, UsageReport } from "./types.ts";
|
|
10
|
+
|
|
11
|
+
export { usagePercentRemaining, usagePercentUsed } from "./display.ts";
|
|
12
|
+
|
|
13
|
+
export const USAGE_STATUS_EVENT = "subscription-usage/status/v1";
|
|
14
|
+
|
|
15
|
+
export type UsageStatusWindowKind =
|
|
16
|
+
| "hourly"
|
|
17
|
+
| "weekly"
|
|
18
|
+
| "monthly"
|
|
19
|
+
| "rolling"
|
|
20
|
+
| "quota";
|
|
21
|
+
|
|
22
|
+
export interface UsageStatusWindow {
|
|
23
|
+
kind: UsageStatusWindowKind;
|
|
24
|
+
label: string;
|
|
25
|
+
remainingPercent: number;
|
|
26
|
+
usedPercent: number;
|
|
27
|
+
displayPercent: number;
|
|
28
|
+
windowMinutes?: number;
|
|
29
|
+
resetsAt?: number;
|
|
30
|
+
/** Countdown to `resetsAt` at publish time, such as `2h13m`; absent once the reset has passed. */
|
|
31
|
+
resetCountdown?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type UsageStatusEvent =
|
|
35
|
+
| {
|
|
36
|
+
v: 1;
|
|
37
|
+
status: "ready";
|
|
38
|
+
providerId: string;
|
|
39
|
+
capturedAt: number;
|
|
40
|
+
displayMode: UsageDisplayMode;
|
|
41
|
+
windows: UsageStatusWindow[];
|
|
42
|
+
}
|
|
43
|
+
| {
|
|
44
|
+
v: 1;
|
|
45
|
+
status: "unavailable";
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export function unavailableUsageStatusEvent(): UsageStatusEvent {
|
|
49
|
+
return { v: 1, status: "unavailable" };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function buildUsageStatusEvent(
|
|
53
|
+
report: UsageReport,
|
|
54
|
+
model?: UsageModel,
|
|
55
|
+
displayMode: UsageDisplayMode = DEFAULT_USAGE_DISPLAY_MODE,
|
|
56
|
+
nowMs: number = Date.now(),
|
|
57
|
+
): UsageStatusEvent {
|
|
58
|
+
const buckets = bucketsForModel(report, model);
|
|
59
|
+
const windows = sortUsageBuckets(buckets).flatMap((bucket) => {
|
|
60
|
+
const remainingPercent = usagePercentRemaining(bucket);
|
|
61
|
+
const usedPercent = usagePercentUsed(bucket);
|
|
62
|
+
const displayPercent = usagePercent(bucket, displayMode);
|
|
63
|
+
if (
|
|
64
|
+
remainingPercent === undefined ||
|
|
65
|
+
usedPercent === undefined ||
|
|
66
|
+
displayPercent === undefined
|
|
67
|
+
) {
|
|
68
|
+
return [];
|
|
69
|
+
}
|
|
70
|
+
const resetCountdown =
|
|
71
|
+
bucket.resetsAt === undefined
|
|
72
|
+
? undefined
|
|
73
|
+
: formatResetCountdown(bucket.resetsAt * 1_000 - nowMs);
|
|
74
|
+
return [
|
|
75
|
+
{
|
|
76
|
+
kind: usageWindowKind(bucket),
|
|
77
|
+
label: compactUsageWindowLabel(bucket),
|
|
78
|
+
remainingPercent,
|
|
79
|
+
usedPercent,
|
|
80
|
+
displayPercent,
|
|
81
|
+
...(bucket.windowMinutes === undefined
|
|
82
|
+
? {}
|
|
83
|
+
: { windowMinutes: bucket.windowMinutes }),
|
|
84
|
+
...(bucket.resetsAt === undefined ? {} : { resetsAt: bucket.resetsAt }),
|
|
85
|
+
...(resetCountdown === undefined ? {} : { resetCountdown }),
|
|
86
|
+
} satisfies UsageStatusWindow,
|
|
87
|
+
];
|
|
88
|
+
});
|
|
89
|
+
return {
|
|
90
|
+
v: 1,
|
|
91
|
+
status: "ready",
|
|
92
|
+
providerId: report.providerId,
|
|
93
|
+
capturedAt: report.capturedAt,
|
|
94
|
+
displayMode,
|
|
95
|
+
windows,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Plain fallback for Pi's default footer. Rich rendering belongs to session-ui. */
|
|
100
|
+
export function formatUsageStatusline(
|
|
101
|
+
report: UsageReport,
|
|
102
|
+
model?: UsageModel,
|
|
103
|
+
displayMode: UsageDisplayMode = DEFAULT_USAGE_DISPLAY_MODE,
|
|
104
|
+
nowMs: number = Date.now(),
|
|
105
|
+
): string | undefined {
|
|
106
|
+
const event = buildUsageStatusEvent(report, model, displayMode, nowMs);
|
|
107
|
+
if (event.status !== "ready" || event.windows.length === 0) return undefined;
|
|
108
|
+
return event.windows
|
|
109
|
+
.map((window) => {
|
|
110
|
+
const percent = `${window.label} ${formatUsagePercent(window.displayPercent)}%`;
|
|
111
|
+
return window.resetCountdown
|
|
112
|
+
? `${percent} ↻ ${window.resetCountdown}`
|
|
113
|
+
: percent;
|
|
114
|
+
})
|
|
115
|
+
.join(" · ");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Compact two-unit countdown such as `3d4h`, `2h13m`, or `<1m`; past resets yield nothing. */
|
|
119
|
+
export function formatResetCountdown(remainingMs: number): string | undefined {
|
|
120
|
+
if (!Number.isFinite(remainingMs) || remainingMs <= 0) return undefined;
|
|
121
|
+
const totalMinutes = Math.floor(remainingMs / 60_000);
|
|
122
|
+
if (totalMinutes < 1) return "<1m";
|
|
123
|
+
const days = Math.floor(totalMinutes / 1_440);
|
|
124
|
+
const hours = Math.floor((totalMinutes % 1_440) / 60);
|
|
125
|
+
const minutes = totalMinutes % 60;
|
|
126
|
+
if (days > 0) return hours > 0 ? `${days}d${hours}h` : `${days}d`;
|
|
127
|
+
if (hours > 0) return minutes > 0 ? `${hours}h${minutes}m` : `${hours}h`;
|
|
128
|
+
return `${minutes}m`;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function usageWindowLabel(bucket: UsageBucket): string {
|
|
132
|
+
const kind = usageWindowKind(bucket);
|
|
133
|
+
if (kind === "weekly") return "1w Window";
|
|
134
|
+
if (kind === "monthly") return "1m Window";
|
|
135
|
+
if (kind === "rolling") return "Rolling Window";
|
|
136
|
+
if (bucket.windowMinutes && bucket.windowMinutes > 0) {
|
|
137
|
+
if (bucket.windowMinutes % 60 === 0) {
|
|
138
|
+
return `${bucket.windowMinutes / 60}h Window`;
|
|
139
|
+
}
|
|
140
|
+
return `${Math.round(bucket.windowMinutes)}m Window`;
|
|
141
|
+
}
|
|
142
|
+
return "Quota";
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function compactUsageWindowLabel(bucket: UsageBucket): string {
|
|
146
|
+
const kind = usageWindowKind(bucket);
|
|
147
|
+
if (kind === "weekly") return "1w";
|
|
148
|
+
if (kind === "monthly") return "1m";
|
|
149
|
+
if (kind === "rolling") return "rolling";
|
|
150
|
+
if (bucket.windowMinutes && bucket.windowMinutes > 0) {
|
|
151
|
+
if (bucket.windowMinutes % 60 === 0) return `${bucket.windowMinutes / 60}h`;
|
|
152
|
+
return `${Math.round(bucket.windowMinutes)}m`;
|
|
153
|
+
}
|
|
154
|
+
return "quota";
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function usageWindowKind(bucket: UsageBucket): UsageStatusWindowKind {
|
|
158
|
+
const key =
|
|
159
|
+
`${bucket.id} ${bucket.label} ${bucket.period ?? ""}`.toLowerCase();
|
|
160
|
+
if (key.includes("month")) return "monthly";
|
|
161
|
+
if (key.includes("week")) return "weekly";
|
|
162
|
+
if (bucket.windowMinutes === 10_080) return "weekly";
|
|
163
|
+
if (
|
|
164
|
+
bucket.windowMinutes !== undefined &&
|
|
165
|
+
bucket.windowMinutes >= 28 * 24 * 60 &&
|
|
166
|
+
bucket.windowMinutes <= 31 * 24 * 60
|
|
167
|
+
) {
|
|
168
|
+
return "monthly";
|
|
169
|
+
}
|
|
170
|
+
if (bucket.windowMinutes !== undefined && bucket.windowMinutes > 0) {
|
|
171
|
+
return "hourly";
|
|
172
|
+
}
|
|
173
|
+
if (key.includes("rolling")) return "rolling";
|
|
174
|
+
return "quota";
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Canonical status order: hourly/rolling, weekly, monthly, then other quota. */
|
|
178
|
+
export function sortUsageBuckets(
|
|
179
|
+
buckets: readonly UsageBucket[],
|
|
180
|
+
): UsageBucket[] {
|
|
181
|
+
return [...buckets].sort((left, right) => {
|
|
182
|
+
const rankComparison = usageWindowRank(left) - usageWindowRank(right);
|
|
183
|
+
if (rankComparison !== 0) return rankComparison;
|
|
184
|
+
const durationComparison =
|
|
185
|
+
(left.windowMinutes ?? Number.POSITIVE_INFINITY) -
|
|
186
|
+
(right.windowMinutes ?? Number.POSITIVE_INFINITY);
|
|
187
|
+
if (durationComparison !== 0) return durationComparison;
|
|
188
|
+
return (left.groupId ?? "").localeCompare(right.groupId ?? "");
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function usageWindowRank(bucket: UsageBucket): number {
|
|
193
|
+
const kind = usageWindowKind(bucket);
|
|
194
|
+
if (kind === "hourly" || kind === "rolling") return 0;
|
|
195
|
+
if (kind === "weekly") return 1;
|
|
196
|
+
if (kind === "monthly") return 2;
|
|
197
|
+
return 3;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function bucketsForModel(
|
|
201
|
+
report: UsageReport,
|
|
202
|
+
model?: UsageModel,
|
|
203
|
+
): UsageBucket[] {
|
|
204
|
+
const hasGroups = report.buckets.some((bucket) => bucket.groupId);
|
|
205
|
+
const hasModelGroups = report.buckets.some(
|
|
206
|
+
(bucket) => bucket.modelKeys && bucket.modelKeys.length > 0,
|
|
207
|
+
);
|
|
208
|
+
if (!hasGroups || (!report.defaultGroupId && !hasModelGroups)) {
|
|
209
|
+
return report.buckets;
|
|
210
|
+
}
|
|
211
|
+
const group = !hasModelGroups && report.defaultGroupId
|
|
212
|
+
? report.defaultGroupId
|
|
213
|
+
: selectUsageGroup(report, model);
|
|
214
|
+
return report.buckets.filter(
|
|
215
|
+
(bucket) => (bucket.groupId ?? bucket.id) === group,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function selectUsageGroup(
|
|
220
|
+
report: UsageReport,
|
|
221
|
+
model?: UsageModel,
|
|
222
|
+
): string | undefined {
|
|
223
|
+
const groups = [
|
|
224
|
+
...new Set(report.buckets.map((bucket) => bucket.groupId ?? bucket.id)),
|
|
225
|
+
];
|
|
226
|
+
const keys = [model?.id, model?.name]
|
|
227
|
+
.map(normalizeKey)
|
|
228
|
+
.filter((value): value is string => Boolean(value));
|
|
229
|
+
const specificGroups = groups
|
|
230
|
+
.filter((group) => group !== report.defaultGroupId)
|
|
231
|
+
.sort((left, right) => right.length - left.length);
|
|
232
|
+
for (const group of specificGroups) {
|
|
233
|
+
const bucket = report.buckets.find(
|
|
234
|
+
(candidate) => (candidate.groupId ?? candidate.id) === group,
|
|
235
|
+
);
|
|
236
|
+
const candidates = [group, bucket?.groupLabel, ...(bucket?.modelKeys ?? [])]
|
|
237
|
+
.map(normalizeKey)
|
|
238
|
+
.filter((value): value is string => Boolean(value));
|
|
239
|
+
if (
|
|
240
|
+
candidates.some((candidate) => keys.some((key) => key.includes(candidate)))
|
|
241
|
+
) {
|
|
242
|
+
return group;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return groups.find((group) => group === report.defaultGroupId) ?? groups[0];
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function normalizeKey(value: string | undefined): string | undefined {
|
|
249
|
+
return (
|
|
250
|
+
value
|
|
251
|
+
?.toLowerCase()
|
|
252
|
+
.replace(/[^a-z0-9]+/gu, "-")
|
|
253
|
+
.replace(/^-+|-+$/gu, "") || undefined
|
|
254
|
+
);
|
|
255
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
export type UsageSemanticsKind = "consumer-subscription";
|
|
2
|
+
export type UsageUnit = "percent" | "usd" | "count";
|
|
3
|
+
|
|
4
|
+
export interface UsageModel {
|
|
5
|
+
provider: string;
|
|
6
|
+
id: string;
|
|
7
|
+
name?: string;
|
|
8
|
+
baseUrl?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface UsageSemantics {
|
|
12
|
+
kind: UsageSemanticsKind;
|
|
13
|
+
label: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface UsageBucket {
|
|
17
|
+
id: string;
|
|
18
|
+
label: string;
|
|
19
|
+
groupId?: string;
|
|
20
|
+
groupLabel?: string;
|
|
21
|
+
modelKeys?: string[];
|
|
22
|
+
used?: number;
|
|
23
|
+
remaining?: number;
|
|
24
|
+
limit?: number;
|
|
25
|
+
unit: UsageUnit;
|
|
26
|
+
period?: string;
|
|
27
|
+
windowMinutes?: number;
|
|
28
|
+
resetsAt?: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface UsageMetric {
|
|
32
|
+
id: string;
|
|
33
|
+
label: string;
|
|
34
|
+
value: number | string;
|
|
35
|
+
unit?: UsageUnit;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface UsageReport {
|
|
39
|
+
providerId: string;
|
|
40
|
+
providerName: string;
|
|
41
|
+
capturedAt: number;
|
|
42
|
+
source: string;
|
|
43
|
+
semantics: UsageSemantics;
|
|
44
|
+
buckets: UsageBucket[];
|
|
45
|
+
defaultGroupId?: string;
|
|
46
|
+
metrics: UsageMetric[];
|
|
47
|
+
notes?: string[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface ResolvedUsageAuth {
|
|
51
|
+
actualProviderId: string;
|
|
52
|
+
/** Active OpenAI OAuth registration, matched against the ChatGPT app list. */
|
|
53
|
+
openaiClientId?: string;
|
|
54
|
+
/** Original usage identity, retained when reset auth adds account headers. */
|
|
55
|
+
usageFingerprint?: string;
|
|
56
|
+
apiKey?: string;
|
|
57
|
+
headers: Record<string, string>;
|
|
58
|
+
fingerprint: string;
|
|
59
|
+
secrets: string[];
|
|
60
|
+
model: UsageModel;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface UsageProviderAdapter {
|
|
64
|
+
id: string;
|
|
65
|
+
displayName: string;
|
|
66
|
+
providerIds: readonly string[];
|
|
67
|
+
officialOrigins: readonly string[];
|
|
68
|
+
semantics: UsageSemantics;
|
|
69
|
+
requiresOAuth?: boolean;
|
|
70
|
+
query(
|
|
71
|
+
auth: ResolvedUsageAuth,
|
|
72
|
+
signal: AbortSignal,
|
|
73
|
+
timeoutMs: number,
|
|
74
|
+
): Promise<UsageReport>;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export type ProviderUsageState =
|
|
78
|
+
| {
|
|
79
|
+
providerId: string;
|
|
80
|
+
providerName: string;
|
|
81
|
+
status: "ready";
|
|
82
|
+
report: UsageReport;
|
|
83
|
+
}
|
|
84
|
+
| {
|
|
85
|
+
providerId: string;
|
|
86
|
+
providerName: string;
|
|
87
|
+
/** `pending`: an observed provider has not reported usage yet. */
|
|
88
|
+
status: "unsupported" | "auth-unavailable" | "query-failed" | "pending";
|
|
89
|
+
message: string;
|
|
90
|
+
};
|