@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
package/src/usage.ts
ADDED
|
@@ -0,0 +1,555 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type {
|
|
3
|
+
ExtensionAPI,
|
|
4
|
+
ExtensionCommandContext,
|
|
5
|
+
ExtensionContext,
|
|
6
|
+
} from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import {
|
|
8
|
+
awaitWithDeadline,
|
|
9
|
+
errorMessage,
|
|
10
|
+
isAbortError,
|
|
11
|
+
modelIdentity,
|
|
12
|
+
UsageCache,
|
|
13
|
+
} from "./core.ts";
|
|
14
|
+
import {
|
|
15
|
+
CODEX_RESET_CONFIRMATION_OPTIONS,
|
|
16
|
+
codexResetCount,
|
|
17
|
+
formatCodexResetOutcome,
|
|
18
|
+
genericCodexResetOption,
|
|
19
|
+
isCodexResetConfirmed,
|
|
20
|
+
resetOptionExpiration,
|
|
21
|
+
type CodexResetOption,
|
|
22
|
+
} from "./codex-reset-core.ts";
|
|
23
|
+
import {
|
|
24
|
+
consumeCodexResetCredit,
|
|
25
|
+
listCodexResetCredits,
|
|
26
|
+
resolveCodexResetAuth,
|
|
27
|
+
} from "./codex-resets.ts";
|
|
28
|
+
import { formatProviderState } from "./format.ts";
|
|
29
|
+
import {
|
|
30
|
+
buildUsageStatusEvent,
|
|
31
|
+
formatUsageStatusline,
|
|
32
|
+
type UsageStatusEvent,
|
|
33
|
+
unavailableUsageStatusEvent,
|
|
34
|
+
USAGE_STATUS_EVENT,
|
|
35
|
+
} from "./status.ts";
|
|
36
|
+
import {
|
|
37
|
+
adapterForProvider,
|
|
38
|
+
queryProviderUsage,
|
|
39
|
+
resolveUsageAuth,
|
|
40
|
+
} from "./query.ts";
|
|
41
|
+
import type {
|
|
42
|
+
ProviderUsageState,
|
|
43
|
+
ResolvedUsageAuth,
|
|
44
|
+
UsageModel,
|
|
45
|
+
UsageProviderAdapter,
|
|
46
|
+
} from "./types.ts";
|
|
47
|
+
|
|
48
|
+
const CACHE_TTL_MS = 5 * 60 * 1_000;
|
|
49
|
+
const QUERY_TIMEOUT_MS = 15_000;
|
|
50
|
+
const FAILURE_BACKOFF_MS = 30_000;
|
|
51
|
+
const STATUS_KEY = "subscription-usage";
|
|
52
|
+
|
|
53
|
+
type QueryOutcome = {
|
|
54
|
+
state: ProviderUsageState;
|
|
55
|
+
fingerprint?: string;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
type StableCurrent = {
|
|
59
|
+
outcome: QueryOutcome;
|
|
60
|
+
model: UsageModel | undefined;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export default function subscriptionUsage(pi: ExtensionAPI): void {
|
|
64
|
+
const cache = new UsageCache(CACHE_TTL_MS);
|
|
65
|
+
const failureBackoff = new Map<string, { until: number; message: string }>();
|
|
66
|
+
const activeControllers = new Set<AbortController>();
|
|
67
|
+
let sessionActive = false;
|
|
68
|
+
let statusGeneration = 0;
|
|
69
|
+
let statusController: AbortController | undefined;
|
|
70
|
+
let statusTimer: ReturnType<typeof setTimeout> | undefined;
|
|
71
|
+
|
|
72
|
+
function emitUsageStatus(event: UsageStatusEvent): void {
|
|
73
|
+
pi.events.emit(USAGE_STATUS_EVENT, event);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function emitUnavailableUsage(): void {
|
|
77
|
+
emitUsageStatus(unavailableUsageStatusEvent());
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function clearStatusTimer(): void {
|
|
81
|
+
if (statusTimer) clearTimeout(statusTimer);
|
|
82
|
+
statusTimer = undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function safeSetStatus(
|
|
86
|
+
ctx: ExtensionContext,
|
|
87
|
+
value: string | undefined,
|
|
88
|
+
): boolean {
|
|
89
|
+
try {
|
|
90
|
+
ctx.ui.setStatus(STATUS_KEY, value);
|
|
91
|
+
return true;
|
|
92
|
+
} catch (error) {
|
|
93
|
+
if (!sessionActive) return false;
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function clearStatus(ctx: ExtensionContext): void {
|
|
99
|
+
statusGeneration += 1;
|
|
100
|
+
statusController?.abort();
|
|
101
|
+
statusController = undefined;
|
|
102
|
+
clearStatusTimer();
|
|
103
|
+
safeSetStatus(ctx, undefined);
|
|
104
|
+
emitUnavailableUsage();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function scheduleStatusRefresh(
|
|
108
|
+
ctx: ExtensionContext,
|
|
109
|
+
model: UsageModel,
|
|
110
|
+
): void {
|
|
111
|
+
clearStatusTimer();
|
|
112
|
+
const generation = statusGeneration;
|
|
113
|
+
statusTimer = setTimeout(() => {
|
|
114
|
+
statusTimer = undefined;
|
|
115
|
+
if (!sessionActive || generation !== statusGeneration) return;
|
|
116
|
+
startStatusRefresh(ctx, model, true);
|
|
117
|
+
}, CACHE_TTL_MS);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function publishStatus(
|
|
121
|
+
ctx: ExtensionContext,
|
|
122
|
+
outcome: QueryOutcome,
|
|
123
|
+
model: UsageModel,
|
|
124
|
+
schedule: boolean,
|
|
125
|
+
): void {
|
|
126
|
+
if (outcome.state.status === "unsupported") {
|
|
127
|
+
clearStatusTimer();
|
|
128
|
+
safeSetStatus(ctx, undefined);
|
|
129
|
+
emitUnavailableUsage();
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (outcome.state.status !== "ready") {
|
|
133
|
+
emitUnavailableUsage();
|
|
134
|
+
if (
|
|
135
|
+
safeSetStatus(
|
|
136
|
+
ctx,
|
|
137
|
+
outcome.state.status === "auth-unavailable"
|
|
138
|
+
? "usage auth ?"
|
|
139
|
+
: "usage error",
|
|
140
|
+
) &&
|
|
141
|
+
schedule &&
|
|
142
|
+
sessionActive
|
|
143
|
+
) {
|
|
144
|
+
scheduleStatusRefresh(ctx, model);
|
|
145
|
+
}
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
emitUsageStatus(buildUsageStatusEvent(outcome.state.report, model));
|
|
149
|
+
const value = formatUsageStatusline(outcome.state.report, model);
|
|
150
|
+
if (!safeSetStatus(ctx, value)) return;
|
|
151
|
+
if (schedule && sessionActive) scheduleStatusRefresh(ctx, model);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async function queryAdapterState(
|
|
155
|
+
ctx: ExtensionContext,
|
|
156
|
+
adapter: UsageProviderAdapter,
|
|
157
|
+
force: boolean,
|
|
158
|
+
signal: AbortSignal,
|
|
159
|
+
): Promise<QueryOutcome> {
|
|
160
|
+
let auth: ResolvedUsageAuth | undefined;
|
|
161
|
+
try {
|
|
162
|
+
auth = await awaitWithDeadline(
|
|
163
|
+
() => resolveUsageAuth(ctx, adapter),
|
|
164
|
+
signal,
|
|
165
|
+
QUERY_TIMEOUT_MS,
|
|
166
|
+
`resolving ${adapter.displayName} authentication`,
|
|
167
|
+
);
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (isAbortError(error) || !sessionActive) throw error;
|
|
170
|
+
return {
|
|
171
|
+
state: {
|
|
172
|
+
providerId: adapter.id,
|
|
173
|
+
providerName: adapter.displayName,
|
|
174
|
+
status: "auth-unavailable",
|
|
175
|
+
message: errorMessage(error),
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
if (!auth) {
|
|
180
|
+
return {
|
|
181
|
+
state: {
|
|
182
|
+
providerId: adapter.id,
|
|
183
|
+
providerName: adapter.displayName,
|
|
184
|
+
status: "auth-unavailable",
|
|
185
|
+
message: `No Pi runtime credential available for ${adapter.displayName}.`,
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
const cached = force ? undefined : cache.get(adapter.id, auth.fingerprint);
|
|
190
|
+
if (cached) {
|
|
191
|
+
return {
|
|
192
|
+
state: {
|
|
193
|
+
providerId: adapter.id,
|
|
194
|
+
providerName: adapter.displayName,
|
|
195
|
+
status: "ready",
|
|
196
|
+
report: cached,
|
|
197
|
+
},
|
|
198
|
+
fingerprint: auth.fingerprint,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
const failureKey = `${adapter.id}:${auth.fingerprint}`;
|
|
202
|
+
const failure = failureBackoff.get(failureKey);
|
|
203
|
+
if (!force && failure && failure.until > Date.now()) {
|
|
204
|
+
return {
|
|
205
|
+
state: {
|
|
206
|
+
providerId: adapter.id,
|
|
207
|
+
providerName: adapter.displayName,
|
|
208
|
+
status: "query-failed",
|
|
209
|
+
message: failure.message,
|
|
210
|
+
},
|
|
211
|
+
fingerprint: auth.fingerprint,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
failureBackoff.delete(failureKey);
|
|
215
|
+
try {
|
|
216
|
+
const report = await queryProviderUsage(
|
|
217
|
+
adapter,
|
|
218
|
+
auth,
|
|
219
|
+
signal,
|
|
220
|
+
QUERY_TIMEOUT_MS,
|
|
221
|
+
);
|
|
222
|
+
cache.set(adapter.id, auth.fingerprint, report);
|
|
223
|
+
return {
|
|
224
|
+
state: {
|
|
225
|
+
providerId: adapter.id,
|
|
226
|
+
providerName: adapter.displayName,
|
|
227
|
+
status: "ready",
|
|
228
|
+
report,
|
|
229
|
+
},
|
|
230
|
+
fingerprint: auth.fingerprint,
|
|
231
|
+
};
|
|
232
|
+
} catch (error) {
|
|
233
|
+
if (isAbortError(error) || !sessionActive) throw error;
|
|
234
|
+
const message = errorMessage(error);
|
|
235
|
+
failureBackoff.set(failureKey, {
|
|
236
|
+
until: Date.now() + FAILURE_BACKOFF_MS,
|
|
237
|
+
message,
|
|
238
|
+
});
|
|
239
|
+
return {
|
|
240
|
+
state: {
|
|
241
|
+
providerId: adapter.id,
|
|
242
|
+
providerName: adapter.displayName,
|
|
243
|
+
status: "query-failed",
|
|
244
|
+
message,
|
|
245
|
+
},
|
|
246
|
+
fingerprint: auth.fingerprint,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
async function queryCurrentState(
|
|
252
|
+
ctx: ExtensionContext,
|
|
253
|
+
model: UsageModel | undefined,
|
|
254
|
+
force: boolean,
|
|
255
|
+
signal: AbortSignal,
|
|
256
|
+
): Promise<QueryOutcome> {
|
|
257
|
+
const adapter = adapterForProvider(model?.provider);
|
|
258
|
+
if (!adapter) {
|
|
259
|
+
return {
|
|
260
|
+
state: {
|
|
261
|
+
providerId: model?.provider ?? "none",
|
|
262
|
+
providerName: model?.provider ?? "No model",
|
|
263
|
+
status: "unsupported",
|
|
264
|
+
message: model
|
|
265
|
+
? `${model.provider} is not supported yet.`
|
|
266
|
+
: "No model selected.",
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
return queryAdapterState(ctx, adapter, force, signal);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async function outcomeStillCurrent(
|
|
274
|
+
ctx: ExtensionContext,
|
|
275
|
+
model: UsageModel | undefined,
|
|
276
|
+
outcome: QueryOutcome,
|
|
277
|
+
signal: AbortSignal,
|
|
278
|
+
): Promise<boolean> {
|
|
279
|
+
if (modelIdentity(ctx.model) !== modelIdentity(model)) return false;
|
|
280
|
+
if (!outcome.fingerprint) return true;
|
|
281
|
+
const adapter = adapterForProvider(model?.provider);
|
|
282
|
+
if (!adapter) return false;
|
|
283
|
+
try {
|
|
284
|
+
const auth = await awaitWithDeadline(
|
|
285
|
+
() => resolveUsageAuth(ctx, adapter),
|
|
286
|
+
signal,
|
|
287
|
+
QUERY_TIMEOUT_MS,
|
|
288
|
+
`revalidating ${adapter.displayName} authentication`,
|
|
289
|
+
);
|
|
290
|
+
return (
|
|
291
|
+
modelIdentity(ctx.model) === modelIdentity(model) &&
|
|
292
|
+
auth?.fingerprint === outcome.fingerprint
|
|
293
|
+
);
|
|
294
|
+
} catch (error) {
|
|
295
|
+
if (isAbortError(error) || !sessionActive) throw error;
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
async function queryStableCurrent(
|
|
301
|
+
ctx: ExtensionContext,
|
|
302
|
+
force: boolean,
|
|
303
|
+
signal: AbortSignal,
|
|
304
|
+
): Promise<StableCurrent | undefined> {
|
|
305
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
306
|
+
const model = ctx.model;
|
|
307
|
+
const outcome = await queryCurrentState(ctx, model, force, signal);
|
|
308
|
+
if (await outcomeStillCurrent(ctx, model, outcome, signal)) {
|
|
309
|
+
return { outcome, model };
|
|
310
|
+
}
|
|
311
|
+
force = false;
|
|
312
|
+
}
|
|
313
|
+
return undefined;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
async function refreshCurrentStatus(
|
|
317
|
+
ctx: ExtensionContext,
|
|
318
|
+
model: UsageModel | undefined,
|
|
319
|
+
force: boolean,
|
|
320
|
+
): Promise<void> {
|
|
321
|
+
if (!adapterForProvider(model?.provider) || !model) {
|
|
322
|
+
clearStatus(ctx);
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
statusGeneration += 1;
|
|
326
|
+
const generation = statusGeneration;
|
|
327
|
+
statusController?.abort();
|
|
328
|
+
const controller = new AbortController();
|
|
329
|
+
statusController = controller;
|
|
330
|
+
activeControllers.add(controller);
|
|
331
|
+
try {
|
|
332
|
+
safeSetStatus(ctx, "usage …");
|
|
333
|
+
const outcome = await queryCurrentState(
|
|
334
|
+
ctx,
|
|
335
|
+
model,
|
|
336
|
+
force,
|
|
337
|
+
controller.signal,
|
|
338
|
+
);
|
|
339
|
+
if (
|
|
340
|
+
!sessionActive ||
|
|
341
|
+
generation !== statusGeneration ||
|
|
342
|
+
controller.signal.aborted ||
|
|
343
|
+
!(await outcomeStillCurrent(ctx, model, outcome, controller.signal))
|
|
344
|
+
) {
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
publishStatus(ctx, outcome, model, true);
|
|
348
|
+
} finally {
|
|
349
|
+
activeControllers.delete(controller);
|
|
350
|
+
if (statusController === controller) statusController = undefined;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function startStatusRefresh(
|
|
355
|
+
ctx: ExtensionContext,
|
|
356
|
+
model: UsageModel | undefined,
|
|
357
|
+
force: boolean,
|
|
358
|
+
): void {
|
|
359
|
+
void refreshCurrentStatus(ctx, model, force).catch((error) => {
|
|
360
|
+
if (isAbortError(error) || !sessionActive) return;
|
|
361
|
+
emitUnavailableUsage();
|
|
362
|
+
safeSetStatus(ctx, "usage error");
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
async function redeemCodexReset(
|
|
367
|
+
ctx: ExtensionCommandContext,
|
|
368
|
+
current: StableCurrent,
|
|
369
|
+
controller: AbortController,
|
|
370
|
+
): Promise<StableCurrent | undefined> {
|
|
371
|
+
if (
|
|
372
|
+
ctx.model?.provider !== "openai-codex" ||
|
|
373
|
+
current.outcome.state.status !== "ready"
|
|
374
|
+
) {
|
|
375
|
+
ctx.ui.notify(
|
|
376
|
+
"Codex resets only work with the current Codex OAuth account.",
|
|
377
|
+
"warning",
|
|
378
|
+
);
|
|
379
|
+
return undefined;
|
|
380
|
+
}
|
|
381
|
+
const summaryCount = codexResetCount(current.outcome.state.report) ?? 0;
|
|
382
|
+
let auth = await awaitWithDeadline(
|
|
383
|
+
() => resolveCodexResetAuth(ctx),
|
|
384
|
+
controller.signal,
|
|
385
|
+
QUERY_TIMEOUT_MS,
|
|
386
|
+
"resolving Codex reset authentication",
|
|
387
|
+
);
|
|
388
|
+
let availability;
|
|
389
|
+
try {
|
|
390
|
+
availability = await listCodexResetCredits(
|
|
391
|
+
auth,
|
|
392
|
+
controller.signal,
|
|
393
|
+
QUERY_TIMEOUT_MS,
|
|
394
|
+
);
|
|
395
|
+
} catch (error) {
|
|
396
|
+
if (summaryCount <= 0) throw error;
|
|
397
|
+
availability = {
|
|
398
|
+
availableCount: summaryCount,
|
|
399
|
+
options: [genericCodexResetOption()],
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
if (availability.availableCount <= 0 || availability.options.length === 0) {
|
|
403
|
+
ctx.ui.notify("No Codex reset credits available.", "info");
|
|
404
|
+
return undefined;
|
|
405
|
+
}
|
|
406
|
+
const labels = availability.options.map(
|
|
407
|
+
(option: CodexResetOption, index: number) =>
|
|
408
|
+
`${index + 1}. ${option.title} · ${resetOptionExpiration(option)}`,
|
|
409
|
+
);
|
|
410
|
+
const selected = await ctx.ui.select("Choose a Codex Reset", labels);
|
|
411
|
+
if (!selected) return undefined;
|
|
412
|
+
const option = availability.options[labels.indexOf(selected)];
|
|
413
|
+
if (!option) return undefined;
|
|
414
|
+
const confirmation = await ctx.ui.select(
|
|
415
|
+
`Redeem one Codex reset?\n${option.title}\n${option.description}\n${resetOptionExpiration(option)}`,
|
|
416
|
+
[...CODEX_RESET_CONFIRMATION_OPTIONS],
|
|
417
|
+
);
|
|
418
|
+
if (!isCodexResetConfirmed(confirmation)) return undefined;
|
|
419
|
+
|
|
420
|
+
const expectedModel = modelIdentity(ctx.model);
|
|
421
|
+
const expectedFingerprint = auth.fingerprint;
|
|
422
|
+
const requestId = randomUUID();
|
|
423
|
+
while (!controller.signal.aborted) {
|
|
424
|
+
auth = await awaitWithDeadline(
|
|
425
|
+
() => resolveCodexResetAuth(ctx),
|
|
426
|
+
controller.signal,
|
|
427
|
+
QUERY_TIMEOUT_MS,
|
|
428
|
+
"revalidating Codex reset authentication",
|
|
429
|
+
);
|
|
430
|
+
if (
|
|
431
|
+
modelIdentity(ctx.model) !== expectedModel ||
|
|
432
|
+
auth.fingerprint !== expectedFingerprint
|
|
433
|
+
) {
|
|
434
|
+
throw new Error("Codex model or account changed; reset not redeemed.");
|
|
435
|
+
}
|
|
436
|
+
try {
|
|
437
|
+
const outcome = await consumeCodexResetCredit(
|
|
438
|
+
auth,
|
|
439
|
+
option,
|
|
440
|
+
requestId,
|
|
441
|
+
controller.signal,
|
|
442
|
+
QUERY_TIMEOUT_MS,
|
|
443
|
+
);
|
|
444
|
+
cache.clearProvider("openai-codex");
|
|
445
|
+
failureBackoff.clear();
|
|
446
|
+
const refreshed = await queryStableCurrent(ctx, true, controller.signal);
|
|
447
|
+
if (refreshed?.model) {
|
|
448
|
+
publishStatus(ctx, refreshed.outcome, refreshed.model, sessionActive);
|
|
449
|
+
}
|
|
450
|
+
const remaining =
|
|
451
|
+
refreshed?.outcome.state.status === "ready"
|
|
452
|
+
? codexResetCount(refreshed.outcome.state.report)
|
|
453
|
+
: undefined;
|
|
454
|
+
ctx.ui.notify(formatCodexResetOutcome(outcome, remaining), "info");
|
|
455
|
+
return refreshed;
|
|
456
|
+
} catch (error) {
|
|
457
|
+
if (isAbortError(error) || !sessionActive) throw error;
|
|
458
|
+
const retryAction = "Retry with Same Request ID";
|
|
459
|
+
const retry = await ctx.ui.select("Reset Result Uncertain", [
|
|
460
|
+
retryAction,
|
|
461
|
+
"Cancel",
|
|
462
|
+
]);
|
|
463
|
+
if (retry !== retryAction) {
|
|
464
|
+
ctx.ui.notify(`Reset not confirmed: ${errorMessage(error)}`, "warning");
|
|
465
|
+
return undefined;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return undefined;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
async function showUsage(ctx: ExtensionCommandContext): Promise<void> {
|
|
473
|
+
if (!ctx.hasUI) throw new Error("/usage requires TUI or RPC mode.");
|
|
474
|
+
const controller = new AbortController();
|
|
475
|
+
activeControllers.add(controller);
|
|
476
|
+
try {
|
|
477
|
+
const current = await queryStableCurrent(ctx, true, controller.signal);
|
|
478
|
+
if (!current) {
|
|
479
|
+
ctx.ui.notify(
|
|
480
|
+
"Model or account keeps changing; run /usage again.",
|
|
481
|
+
"warning",
|
|
482
|
+
);
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
ctx.ui.notify(formatProviderState(current.outcome.state), "info");
|
|
486
|
+
if (current.model) {
|
|
487
|
+
publishStatus(ctx, current.outcome, current.model, sessionActive);
|
|
488
|
+
}
|
|
489
|
+
if (
|
|
490
|
+
ctx.model?.provider !== "openai-codex" ||
|
|
491
|
+
current.outcome.state.status !== "ready"
|
|
492
|
+
) {
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
const resetCount = codexResetCount(current.outcome.state.report) ?? 0;
|
|
496
|
+
if (resetCount <= 0) return;
|
|
497
|
+
const action = await ctx.ui.select(
|
|
498
|
+
`Reset Credits: ${resetCount} Available`,
|
|
499
|
+
["Redeem 1 Reset"],
|
|
500
|
+
);
|
|
501
|
+
if (!action) return;
|
|
502
|
+
const refreshed = await redeemCodexReset(ctx, current, controller);
|
|
503
|
+
if (refreshed?.outcome.state.status === "ready") {
|
|
504
|
+
ctx.ui.notify(formatProviderState(refreshed.outcome.state), "info");
|
|
505
|
+
}
|
|
506
|
+
} finally {
|
|
507
|
+
controller.abort();
|
|
508
|
+
activeControllers.delete(controller);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
pi.registerCommand("usage", {
|
|
513
|
+
description: "Show subscription usage for the current provider",
|
|
514
|
+
handler: async (args, ctx) => {
|
|
515
|
+
if (args.trim()) {
|
|
516
|
+
ctx.ui.notify("/usage takes no arguments.", "warning");
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
try {
|
|
520
|
+
await showUsage(ctx);
|
|
521
|
+
} catch (error) {
|
|
522
|
+
if (isAbortError(error) || !sessionActive) return;
|
|
523
|
+
ctx.ui.notify(`Usage query failed: ${errorMessage(error)}`, "error");
|
|
524
|
+
}
|
|
525
|
+
},
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
pi.on("session_start", (_event, ctx) => {
|
|
529
|
+
sessionActive = ctx.hasUI;
|
|
530
|
+
if (ctx.hasUI) startStatusRefresh(ctx, ctx.model, false);
|
|
531
|
+
});
|
|
532
|
+
pi.on("session_tree", (_event, ctx) => {
|
|
533
|
+
if (ctx.hasUI) startStatusRefresh(ctx, ctx.model, false);
|
|
534
|
+
});
|
|
535
|
+
pi.on("model_select", (event, ctx) => {
|
|
536
|
+
if (!ctx.hasUI) return;
|
|
537
|
+
emitUnavailableUsage();
|
|
538
|
+
startStatusRefresh(ctx, event.model, false);
|
|
539
|
+
});
|
|
540
|
+
pi.on("agent_settled", (_event, ctx) => {
|
|
541
|
+
if (ctx.hasUI) startStatusRefresh(ctx, ctx.model, false);
|
|
542
|
+
});
|
|
543
|
+
pi.on("session_shutdown", (_event, ctx) => {
|
|
544
|
+
sessionActive = false;
|
|
545
|
+
statusGeneration += 1;
|
|
546
|
+
clearStatusTimer();
|
|
547
|
+
for (const controller of activeControllers) controller.abort();
|
|
548
|
+
activeControllers.clear();
|
|
549
|
+
statusController = undefined;
|
|
550
|
+
cache.clear();
|
|
551
|
+
failureBackoff.clear();
|
|
552
|
+
safeSetStatus(ctx, undefined);
|
|
553
|
+
emitUnavailableUsage();
|
|
554
|
+
});
|
|
555
|
+
}
|