@specode/pi-subscription-usage 0.2.1 → 0.2.2
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 +2 -2
- package/README.zh-CN.md +2 -2
- package/package.json +1 -1
- package/src/providers/grok.ts +152 -31
- package/src/query.ts +28 -5
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ Supported providers:
|
|
|
8
8
|
|
|
9
9
|
- **OpenAI Codex** — 5-hour and weekly quota, model-specific quota, and confirmed reset-credit redemption.
|
|
10
10
|
- **OpenCode Go** — 5-hour, weekly, and monthly windows.
|
|
11
|
-
- **Grok** — weekly or monthly quota using only Pi's `xai` / `xai-auth` OAuth credentials, with account identity verification.
|
|
11
|
+
- **Grok** — weekly and/or monthly quota using only Pi's `xai` / `xai-auth` OAuth credentials, with account identity verification. Unified SuperGrok billing is read from the default monthly endpoint when `?format=credits` omits window percents. Windows use the same `5h / 1w / 1m` status format as other providers.
|
|
12
12
|
- **Kimi Coding** — 5-hour and weekly windows.
|
|
13
13
|
|
|
14
14
|
The extension does not implement or modify Codex Fast mode and never rewrites model requests.
|
|
@@ -53,7 +53,7 @@ Codex results are grouped by quota domain in this order:
|
|
|
53
53
|
|
|
54
54
|
Windows from different domains are never interleaved. Run `/usage` again whenever you want to refresh; the command does not show refresh, provider-switching, or all-provider menus.
|
|
55
55
|
|
|
56
|
-
The reset menu appears only when Codex reports redeemable reset credits. Grok's current API exposes quota windows and natural reset times, but no verified manual-reset endpoint or reset-credit count, so the extension never invents a reset action.
|
|
56
|
+
The reset menu appears only when Codex reports redeemable reset credits. Grok's current API exposes quota windows and natural reset times, but no verified manual-reset endpoint or reset-credit count, so the extension never invents a reset action. Grok windows still render through the same `/usage` bars and status event as Codex, OpenCode Go, and Kimi.
|
|
57
57
|
|
|
58
58
|
## Configuration
|
|
59
59
|
|
package/README.zh-CN.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
- **OpenAI Codex**:5 小时与每周额度、模型专属额度,以及需要确认的重置次数兑换。
|
|
10
10
|
- **OpenCode Go**:5 小时、每周和每月窗口。
|
|
11
|
-
- **Grok
|
|
11
|
+
- **Grok**:每周和/或每月额度;只使用 Pi 的 `xai` / `xai-auth` OAuth 凭据,并先验证账户身份。统一账单账户在 `?format=credits` 缺少窗口百分比时,会再读取默认的月度账单接口。窗口与其他提供商一样使用 `5h / 1w / 1m` 状态格式。
|
|
12
12
|
- **Kimi Coding**:5 小时和每周窗口。
|
|
13
13
|
|
|
14
14
|
本扩展不实现或修改 Codex Fast 模式,也不会改写模型请求。
|
|
@@ -53,7 +53,7 @@ Codex 结果会按以下额度域分组:
|
|
|
53
53
|
|
|
54
54
|
不同额度域的窗口不会交错。需要刷新时再次运行 `/usage` 即可;命令不会显示刷新、切换提供商或查询所有提供商的菜单。
|
|
55
55
|
|
|
56
|
-
只有当 Codex 返回可兑换的重置次数时,才会显示重置菜单。Grok 当前 API 只公开额度窗口和自然重置时间,没有经过验证的手动重置端点或重置次数,因此本扩展不会虚构重置操作。
|
|
56
|
+
只有当 Codex 返回可兑换的重置次数时,才会显示重置菜单。Grok 当前 API 只公开额度窗口和自然重置时间,没有经过验证的手动重置端点或重置次数,因此本扩展不会虚构重置操作。Grok 窗口仍通过与 Codex、OpenCode Go、Kimi 相同的 `/usage` 进度条和状态事件展示。
|
|
57
57
|
|
|
58
58
|
## 配置
|
|
59
59
|
|
package/package.json
CHANGED
package/src/providers/grok.ts
CHANGED
|
@@ -6,6 +6,8 @@ const MAX_USER_ID_LENGTH = 256;
|
|
|
6
6
|
const MAX_LABEL_LENGTH = 80;
|
|
7
7
|
const MAX_CENTS = 1_000_000_000_000;
|
|
8
8
|
const USER_ID_PATTERN = /^[\x21-\x7e]+$/u;
|
|
9
|
+
const WEEKLY_WINDOW_MINUTES = 10_080;
|
|
10
|
+
const MONTHLY_WINDOW_MINUTES = 43_200;
|
|
9
11
|
// Grok CLI proxy protocol version; independent of this extension's package version.
|
|
10
12
|
const GROK_CLIENT_VERSION = "0.1.0";
|
|
11
13
|
|
|
@@ -34,23 +36,59 @@ export function normalizeGrokIdentity(payload: unknown): string {
|
|
|
34
36
|
return userId;
|
|
35
37
|
}
|
|
36
38
|
|
|
39
|
+
/** Unified SuperGrok accounts may expose their quota only on monthly billing. */
|
|
40
|
+
export function shouldProbeGrokMonthly(creditsPayload: unknown): boolean {
|
|
41
|
+
const config = billingConfig(creditsPayload);
|
|
42
|
+
if (!config) return true;
|
|
43
|
+
return (
|
|
44
|
+
config.isUnifiedBillingUser === true ||
|
|
45
|
+
parseCreditsWindow(config) === undefined
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A failed monthly request is fatal when credits contain no reliable quota. */
|
|
50
|
+
export function requiresGrokMonthlyQuota(creditsPayload: unknown): boolean {
|
|
51
|
+
return parseCreditsWindow(billingConfig(creditsPayload)) === undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
37
54
|
export function normalizeGrokBilling(
|
|
38
|
-
|
|
55
|
+
creditsPayload: unknown,
|
|
39
56
|
capturedAt: number,
|
|
57
|
+
monthlyPayload?: unknown | null,
|
|
40
58
|
): UsageReport {
|
|
41
|
-
const
|
|
42
|
-
if (!
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
59
|
+
const creditsRoot = asObject(creditsPayload);
|
|
60
|
+
if (!creditsRoot) throw new Error("Grok billing response was not an object.");
|
|
61
|
+
const creditsConfig = billingConfig(creditsPayload);
|
|
62
|
+
const weekly = parseCreditsWindow(creditsConfig);
|
|
63
|
+
|
|
64
|
+
const monthlyRoot = readOptionalPayload(
|
|
65
|
+
monthlyPayload,
|
|
66
|
+
"Grok monthly billing response was not an object.",
|
|
67
|
+
);
|
|
68
|
+
let monthlyConfig: Record<string, unknown> | undefined;
|
|
69
|
+
if (monthlyRoot !== undefined) {
|
|
70
|
+
monthlyConfig = billingConfig(monthlyRoot, "skip");
|
|
71
|
+
} else if (monthlyPayload === undefined && !weekly) {
|
|
72
|
+
monthlyConfig = creditsConfig;
|
|
46
73
|
}
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
if (buckets.
|
|
51
|
-
|
|
74
|
+
const monthly = parseMonthlyWindow(monthlyConfig);
|
|
75
|
+
|
|
76
|
+
const buckets: UsageBucket[] = [];
|
|
77
|
+
if (weekly) buckets.push(weekly);
|
|
78
|
+
if (monthly) buckets.push(monthly);
|
|
79
|
+
if (buckets.length === 0) {
|
|
80
|
+
throw new Error("Grok billing endpoint returned no displayable quota window.");
|
|
52
81
|
}
|
|
53
|
-
|
|
82
|
+
|
|
83
|
+
const primaryMetrics = collectMetrics(creditsConfig);
|
|
84
|
+
addAccountMetrics(primaryMetrics, creditsRoot);
|
|
85
|
+
const extraMetrics =
|
|
86
|
+
monthlyConfig && monthlyConfig !== creditsConfig
|
|
87
|
+
? collectMetrics(monthlyConfig)
|
|
88
|
+
: [];
|
|
89
|
+
if (monthlyRoot) addAccountMetrics(extraMetrics, monthlyRoot);
|
|
90
|
+
const metrics = mergeMetrics(primaryMetrics, extraMetrics);
|
|
91
|
+
|
|
54
92
|
return {
|
|
55
93
|
providerId: "xai",
|
|
56
94
|
providerName: "Grok",
|
|
@@ -65,34 +103,94 @@ export function normalizeGrokBilling(
|
|
|
65
103
|
};
|
|
66
104
|
}
|
|
67
105
|
|
|
68
|
-
function
|
|
106
|
+
function readOptionalPayload(
|
|
107
|
+
payload: unknown | null | undefined,
|
|
108
|
+
invalidMessage: string,
|
|
109
|
+
): Record<string, unknown> | undefined {
|
|
110
|
+
if (payload === undefined || payload === null) return undefined;
|
|
111
|
+
const root = asObject(payload);
|
|
112
|
+
if (!root) throw new Error(invalidMessage);
|
|
113
|
+
return root;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function billingConfig(
|
|
117
|
+
payload: unknown,
|
|
118
|
+
invalidConfig: "throw" | "skip" = "throw",
|
|
119
|
+
): Record<string, unknown> | undefined {
|
|
120
|
+
const root = asObject(payload);
|
|
121
|
+
if (!root) return undefined;
|
|
122
|
+
if (root.config === undefined) return undefined;
|
|
123
|
+
const config = asObject(root.config);
|
|
124
|
+
if (!config) {
|
|
125
|
+
if (invalidConfig === "skip") return undefined;
|
|
126
|
+
throw new Error("Grok billing response contained an invalid config object.");
|
|
127
|
+
}
|
|
128
|
+
return config;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function parseCreditsWindow(
|
|
69
132
|
config: Record<string, unknown> | undefined,
|
|
70
133
|
): UsageBucket | undefined {
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
usedCents !== undefined &&
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
) {
|
|
81
|
-
used = Math.min(100, (usedCents / limitCents) * 100);
|
|
134
|
+
if (!config) return undefined;
|
|
135
|
+
const period = asObject(config.currentPeriod);
|
|
136
|
+
let used = boundedPercent(config.creditUsagePercent);
|
|
137
|
+
if (used === undefined && isWeeklyPeriod(period)) {
|
|
138
|
+
const usedCents = boundedCents(config.used);
|
|
139
|
+
const limitCents = boundedCents(config.monthlyLimit);
|
|
140
|
+
if (usedCents !== undefined && limitCents !== undefined && limitCents > 0) {
|
|
141
|
+
used = Math.min(100, (usedCents / limitCents) * 100);
|
|
142
|
+
}
|
|
82
143
|
}
|
|
83
144
|
if (used === undefined) return undefined;
|
|
84
|
-
|
|
85
|
-
|
|
145
|
+
return percentWindow("weekly", used, config, period, WEEKLY_WINDOW_MINUTES);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function parseMonthlyWindow(
|
|
149
|
+
config: Record<string, unknown> | undefined,
|
|
150
|
+
): UsageBucket | undefined {
|
|
151
|
+
if (!config) return undefined;
|
|
152
|
+
const usedCents = boundedCents(config.used);
|
|
153
|
+
const limitCents = boundedCents(config.monthlyLimit);
|
|
154
|
+
if (usedCents === undefined || limitCents === undefined || limitCents <= 0) {
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
const used = Math.min(100, (usedCents / limitCents) * 100);
|
|
158
|
+
const period = asObject(config.currentPeriod);
|
|
159
|
+
return percentWindow(
|
|
160
|
+
"monthly",
|
|
161
|
+
used,
|
|
162
|
+
config,
|
|
163
|
+
isWeeklyPeriod(period) ? undefined : period,
|
|
164
|
+
MONTHLY_WINDOW_MINUTES,
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function percentWindow(
|
|
169
|
+
kind: "weekly" | "monthly",
|
|
170
|
+
used: number,
|
|
171
|
+
config: Record<string, unknown>,
|
|
172
|
+
period: Record<string, unknown> | undefined,
|
|
173
|
+
fallbackMinutes: number,
|
|
174
|
+
): UsageBucket {
|
|
86
175
|
const periodInfo = parseUsagePeriod(config, period);
|
|
87
176
|
const bucket: UsageBucket = {
|
|
88
|
-
id:
|
|
89
|
-
label: "
|
|
177
|
+
id: kind,
|
|
178
|
+
label: kind === "monthly" ? "Monthly window" : "Weekly window",
|
|
90
179
|
used,
|
|
91
180
|
remaining: 100 - used,
|
|
92
181
|
limit: 100,
|
|
93
182
|
unit: "percent",
|
|
94
|
-
|
|
183
|
+
period: kind,
|
|
184
|
+
windowMinutes:
|
|
185
|
+
periodInfo.period === kind
|
|
186
|
+
? (periodInfo.windowMinutes ?? fallbackMinutes)
|
|
187
|
+
: fallbackMinutes,
|
|
95
188
|
};
|
|
189
|
+
const resetsAt = parseTimestamp(
|
|
190
|
+
kind === "monthly"
|
|
191
|
+
? (config.billingPeriodEnd ?? period?.end)
|
|
192
|
+
: (period?.end ?? config.billingPeriodEnd),
|
|
193
|
+
);
|
|
96
194
|
if (resetsAt !== undefined) bucket.resetsAt = resetsAt;
|
|
97
195
|
return bucket;
|
|
98
196
|
}
|
|
@@ -104,10 +202,10 @@ function parseUsagePeriod(
|
|
|
104
202
|
const type = currentPeriod?.type;
|
|
105
203
|
if (typeof type === "string") {
|
|
106
204
|
if (type.toUpperCase().includes("WEEKLY")) {
|
|
107
|
-
return { period: "weekly", windowMinutes:
|
|
205
|
+
return { period: "weekly", windowMinutes: WEEKLY_WINDOW_MINUTES };
|
|
108
206
|
}
|
|
109
207
|
if (type.toUpperCase().includes("MONTHLY")) {
|
|
110
|
-
return { period: "monthly", windowMinutes:
|
|
208
|
+
return { period: "monthly", windowMinutes: MONTHLY_WINDOW_MINUTES };
|
|
111
209
|
}
|
|
112
210
|
}
|
|
113
211
|
const start = parseTimestamp(
|
|
@@ -125,6 +223,14 @@ function parseUsagePeriod(
|
|
|
125
223
|
return { windowMinutes };
|
|
126
224
|
}
|
|
127
225
|
|
|
226
|
+
function isWeeklyPeriod(
|
|
227
|
+
period: Record<string, unknown> | undefined,
|
|
228
|
+
): period is Record<string, unknown> {
|
|
229
|
+
return (
|
|
230
|
+
typeof period?.type === "string" && period.type.toUpperCase().includes("WEEK")
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
128
234
|
function collectMetrics(
|
|
129
235
|
config: Record<string, unknown> | undefined,
|
|
130
236
|
): UsageMetric[] {
|
|
@@ -177,6 +283,21 @@ function addAccountMetrics(
|
|
|
177
283
|
}
|
|
178
284
|
}
|
|
179
285
|
|
|
286
|
+
function mergeMetrics(
|
|
287
|
+
primary: UsageMetric[],
|
|
288
|
+
extra: UsageMetric[],
|
|
289
|
+
): UsageMetric[] {
|
|
290
|
+
const seen = new Set(primary.map((metric) => metric.id));
|
|
291
|
+
return [
|
|
292
|
+
...primary,
|
|
293
|
+
...extra.filter((metric) => {
|
|
294
|
+
if (seen.has(metric.id)) return false;
|
|
295
|
+
seen.add(metric.id);
|
|
296
|
+
return true;
|
|
297
|
+
}),
|
|
298
|
+
];
|
|
299
|
+
}
|
|
300
|
+
|
|
180
301
|
function addUsdMetric(
|
|
181
302
|
metrics: UsageMetric[],
|
|
182
303
|
id: string,
|
package/src/query.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
|
5
5
|
import {
|
|
6
6
|
errorMessage,
|
|
7
7
|
fingerprintResolvedAuth,
|
|
8
|
+
isAbortError,
|
|
8
9
|
redactUsageError,
|
|
9
10
|
} from "./core.ts";
|
|
10
11
|
import { CODEX_PROVIDER_ID } from "./providers/codex-constants.ts";
|
|
@@ -13,6 +14,8 @@ import {
|
|
|
13
14
|
grokRequestHeaders,
|
|
14
15
|
normalizeGrokBilling,
|
|
15
16
|
normalizeGrokIdentity,
|
|
17
|
+
requiresGrokMonthlyQuota,
|
|
18
|
+
shouldProbeGrokMonthly,
|
|
16
19
|
} from "./providers/grok.ts";
|
|
17
20
|
import { normalizeKimiUsage } from "./providers/kimi.ts";
|
|
18
21
|
import { normalizeOpenCodeGoUsage } from "./providers/opencode-go.ts";
|
|
@@ -26,8 +29,9 @@ const CODEX_USAGE_URL = "https://chatgpt.com/backend-api/wham/usage";
|
|
|
26
29
|
const OPENCODE_GO_USAGE_URL = "https://opencode.ai/zen/go/v1/usage";
|
|
27
30
|
const KIMI_USAGE_URL = "https://api.kimi.com/coding/v1/usages";
|
|
28
31
|
const GROK_USER_URL = "https://cli-chat-proxy.grok.com/v1/user";
|
|
29
|
-
const
|
|
32
|
+
const GROK_CREDITS_URL =
|
|
30
33
|
"https://cli-chat-proxy.grok.com/v1/billing?format=credits";
|
|
34
|
+
const GROK_MONTHLY_URL = "https://cli-chat-proxy.grok.com/v1/billing";
|
|
31
35
|
const MAX_SUCCESS_BODY_BYTES = 64 * 1024;
|
|
32
36
|
const MAX_ERROR_BODY_BYTES = 4 * 1024;
|
|
33
37
|
const PACKAGE_METADATA = createRequire(import.meta.url)("../package.json") as {
|
|
@@ -121,15 +125,34 @@ export const SUPPORTED_ADAPTERS: readonly UsageProviderAdapter[] = [
|
|
|
121
125
|
{ headers: grokRequestHeaders() },
|
|
122
126
|
);
|
|
123
127
|
const userId = normalizeGrokIdentity(identity);
|
|
124
|
-
const
|
|
125
|
-
|
|
128
|
+
const request = { headers: grokRequestHeaders(userId) };
|
|
129
|
+
const credits = await fetchProviderJson(
|
|
130
|
+
GROK_CREDITS_URL,
|
|
126
131
|
auth,
|
|
127
132
|
signal,
|
|
128
133
|
timeoutMs,
|
|
129
134
|
"Grok billing endpoint",
|
|
130
|
-
|
|
135
|
+
request,
|
|
131
136
|
);
|
|
132
|
-
|
|
137
|
+
const capturedAt = Date.now();
|
|
138
|
+
const monthlyRequired = requiresGrokMonthlyQuota(credits);
|
|
139
|
+
let monthly: Record<string, unknown> | null | undefined;
|
|
140
|
+
if (shouldProbeGrokMonthly(credits)) {
|
|
141
|
+
try {
|
|
142
|
+
monthly = await fetchProviderJson(
|
|
143
|
+
GROK_MONTHLY_URL,
|
|
144
|
+
auth,
|
|
145
|
+
signal,
|
|
146
|
+
timeoutMs,
|
|
147
|
+
"Grok monthly billing endpoint",
|
|
148
|
+
request,
|
|
149
|
+
);
|
|
150
|
+
} catch (error) {
|
|
151
|
+
if (isAbortError(error) || monthlyRequired) throw error;
|
|
152
|
+
monthly = null;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return normalizeGrokBilling(credits, capturedAt, monthly);
|
|
133
156
|
},
|
|
134
157
|
},
|
|
135
158
|
];
|