opencode-quota-gui 1.3.0 → 1.3.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/README.md +27 -17
- package/dist/bin/opencode-quota-gui.js +0 -0
- package/dist/contracts/build-identity.js +1 -1
- package/dist/contracts/dashboard.d.ts +8 -1
- package/dist/contracts/dto-guards.js +2 -0
- package/dist/contracts/openai-quota.d.ts +3 -0
- package/dist/contracts/openai-quota.js +3 -0
- package/dist/data/modelsdev-temporal.min.json +57 -0
- package/dist/gui/config.d.ts +3 -3
- package/dist/gui/config.js +31 -2
- package/dist/gui/lifecycle.d.ts +5 -3
- package/dist/gui/lifecycle.js +3 -3
- package/dist/gui/primary-quota-windows.js +16 -14
- package/dist/gui/provider-cards.d.ts +10 -2
- package/dist/gui/provider-cards.js +51 -23
- package/dist/gui/quota-notification-detector.d.ts +4 -4
- package/dist/gui/quota-notification-detector.js +76 -42
- package/dist/gui/quota-notification-observer.d.ts +2 -3
- package/dist/gui/quota-notification-observer.js +68 -19
- package/dist/gui/rpc.d.ts +2 -0
- package/dist/gui/rpc.js +11 -3
- package/dist/gui/server.js +11 -2
- package/dist/gui/snapshot-service.d.ts +3 -1
- package/dist/gui/snapshot-service.js +126 -18
- package/dist/gui/sources.js +98 -56
- package/dist/gui-experimental.cjs +230 -225
- package/dist/lib/accounting-format.js +8 -0
- package/dist/lib/alibaba-coding-plan-cli.d.ts +79 -0
- package/dist/lib/alibaba-coding-plan-cli.js +539 -0
- package/dist/lib/anthropic-auth.d.ts +2 -3
- package/dist/lib/anthropic-auth.js +2 -3
- package/dist/lib/anthropic.d.ts +48 -12
- package/dist/lib/anthropic.js +610 -275
- package/dist/lib/api-key-retry-after.d.ts +7 -0
- package/dist/lib/api-key-retry-after.js +62 -0
- package/dist/lib/atomic-json.d.ts +2 -0
- package/dist/lib/atomic-json.js +1 -0
- package/dist/lib/cache-observer-ingestion.d.ts +90 -0
- package/dist/lib/cache-observer-ingestion.js +981 -0
- package/dist/lib/cache-observer.d.ts +138 -0
- package/dist/lib/cache-observer.js +317 -0
- package/dist/lib/canonical-config-lock.d.ts +9 -0
- package/dist/lib/canonical-config-lock.js +36 -0
- package/dist/lib/chutes.js +62 -51
- package/dist/lib/claude-agent-transcript-ttl.d.ts +48 -0
- package/dist/lib/claude-agent-transcript-ttl.js +154 -0
- package/dist/lib/claude-agent-ttl-tracker.d.ts +21 -0
- package/dist/lib/claude-agent-ttl-tracker.js +152 -0
- package/dist/lib/cli-show.js +5 -3
- package/dist/lib/config-write-target.d.ts +1 -1
- package/dist/lib/config-write-target.js +5 -2
- package/dist/lib/config.d.ts +2 -1
- package/dist/lib/config.js +193 -2
- package/dist/lib/copilot.js +171 -86
- package/dist/lib/dashboard-web-policy.d.ts +4 -4
- package/dist/lib/dashboard-web-policy.js +2 -2
- package/dist/lib/deepseek.js +25 -13
- package/dist/lib/display-sanitize.js +1 -0
- package/dist/lib/entries.d.ts +23 -0
- package/dist/lib/entries.js +2 -0
- package/dist/lib/glm-coding-plan.js +98 -74
- package/dist/lib/google-agy.js +90 -12
- package/dist/lib/google-gemini-cli.js +84 -11
- package/dist/lib/http.d.ts +4 -0
- package/dist/lib/http.js +16 -2
- package/dist/lib/init-installer.js +5 -3
- package/dist/lib/kilo.js +37 -18
- package/dist/lib/kimi.js +35 -18
- package/dist/lib/mimo.d.ts +2 -0
- package/dist/lib/mimo.js +36 -0
- package/dist/lib/nanogpt.d.ts +1 -0
- package/dist/lib/nanogpt.js +26 -3
- package/dist/lib/ollama-cloud.d.ts +1 -1
- package/dist/lib/ollama-cloud.js +48 -35
- package/dist/lib/openai.js +84 -38
- package/dist/lib/opencode-auth-sqlite.d.ts +2 -2
- package/dist/lib/opencode-auth-sqlite.js +2 -2
- package/dist/lib/opencode-config-editor.d.ts +1 -0
- package/dist/lib/opencode-config-editor.js +6 -1
- package/dist/lib/opencode-go.js +29 -2
- package/dist/lib/opencode-version-core.d.ts +23 -0
- package/dist/lib/opencode-version-core.js +49 -0
- package/dist/lib/opencode-version.d.ts +2 -22
- package/dist/lib/opencode-version.js +4 -48
- package/dist/lib/opencode-zen.d.ts +3 -0
- package/dist/lib/opencode-zen.js +71 -5
- package/dist/lib/provider-add.js +5 -2
- package/dist/lib/provider-registration.d.ts +19 -9
- package/dist/lib/provider-registration.js +17 -6
- package/dist/lib/quota-command-format.js +1 -1
- package/dist/lib/quota-exhaustion-projection.d.ts +11 -0
- package/dist/lib/quota-exhaustion-projection.js +25 -0
- package/dist/lib/quota-providers-remote.d.ts +2 -1
- package/dist/lib/quota-providers-remote.js +59 -3
- package/dist/lib/quota-providers.d.ts +1 -1
- package/dist/lib/quota-providers.js +12 -35
- package/dist/lib/quota-render-data.d.ts +6 -0
- package/dist/lib/quota-render-data.js +43 -17
- package/dist/lib/quota-state-codec.js +17 -1
- package/dist/lib/quota-state.d.ts +7 -1
- package/dist/lib/quota-state.js +208 -34
- package/dist/lib/quota-stats.js +21 -0
- package/dist/lib/quota-status.js +3 -51
- package/dist/lib/scoped-update.d.ts +1 -1
- package/dist/lib/synthetic.js +65 -54
- package/dist/lib/types.d.ts +31 -1
- package/dist/lib/types.js +13 -0
- package/dist/lib/xai.js +48 -22
- package/dist/plugin.js +261 -13
- package/dist/provider-logos/NOTICE.md +1 -1
- package/dist/provider-logos/OPENCODE-NOTICE.md +4 -3
- package/dist/providers/alibaba-coding-plan.js +107 -104
- package/dist/providers/anthropic.d.ts +12 -3
- package/dist/providers/anthropic.js +71 -228
- package/dist/providers/cache-policies.d.ts +11 -4
- package/dist/providers/cache-policies.js +19 -13
- package/dist/providers/claude-agent.d.ts +19 -0
- package/dist/providers/claude-agent.js +196 -0
- package/dist/providers/copilot.js +2 -0
- package/dist/providers/glm-coding-plan-provider.d.ts +1 -0
- package/dist/providers/google-account-format.js +1 -0
- package/dist/providers/google-agy.d.ts +6 -5
- package/dist/providers/google-agy.js +43 -15
- package/dist/providers/google-gemini-cli.js +1 -0
- package/dist/providers/kilo.js +1 -0
- package/dist/providers/kimi-code.js +2 -0
- package/dist/providers/mimo.js +1 -0
- package/dist/providers/nanogpt.js +1 -0
- package/dist/providers/opencode-go.js +3 -0
- package/dist/providers/opencode-zen.js +2 -0
- package/dist/providers/openrouter.js +1 -0
- package/dist/providers/quota-providers.js +16 -5
- package/dist/providers/registry.js +2 -0
- package/dist/providers/result-helpers.d.ts +2 -0
- package/dist/providers/result-helpers.js +2 -0
- package/dist/rpc.d.ts +121 -8
- package/dist/rpc.js +204 -12
- package/dist/ui/assets/{index-cDd9jUVY.css → index-BcueIm3Z.css} +1 -1
- package/dist/ui/assets/index-C6qRSoh8.js +10 -0
- package/dist/ui/assets/{popover-BGkgv9s0.js → popover-4bFSvRZ7.js} +1 -1
- package/dist/ui/assets/providers/claude-agent.svg +3 -0
- package/dist/ui/index.html +2 -2
- package/package.json +44 -45
- package/dist/lib/alibaba-coding-plan-local-quota.d.ts +0 -47
- package/dist/lib/alibaba-coding-plan-local-quota.js +0 -146
- package/dist/ui/assets/index-DLHeXYff.js +0 -9
package/dist/lib/nanogpt.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { providerFailureCategoryForHttpStatus, } from "../contracts/provider-failure.js";
|
|
9
9
|
import { isCanonicalAccountingDecimal } from "./accounting-format.js";
|
|
10
|
+
import { createApiKeyRetryGuard, retryAfterDeadline } from "./api-key-retry-after.js";
|
|
10
11
|
import { sanitizeDisplaySnippet, sanitizeDisplayText } from "./display-sanitize.js";
|
|
11
12
|
import { clampPercent } from "./format-utils.js";
|
|
12
13
|
import { fetchWithTimeout } from "./http.js";
|
|
@@ -14,6 +15,16 @@ import { resolveNanoGptApiKey } from "./nanogpt-config.js";
|
|
|
14
15
|
const USER_AGENT = "opencode-quota-gui/1.0";
|
|
15
16
|
const NANOGPT_USAGE_URL = "https://nano-gpt.com/api/subscription/v1/usage";
|
|
16
17
|
const NANOGPT_BALANCE_URL = "https://nano-gpt.com/api/check-balance";
|
|
18
|
+
// Per-endpoint cooldown: a limited endpoint must not block the healthy one.
|
|
19
|
+
const blockedEndpoint = (retryAtMs) => ({
|
|
20
|
+
success: false,
|
|
21
|
+
message: "NanoGPT API error 429",
|
|
22
|
+
retryable: true,
|
|
23
|
+
failureCategory: "provider_api",
|
|
24
|
+
retryAtMs,
|
|
25
|
+
});
|
|
26
|
+
const usageGuard = createApiKeyRetryGuard(blockedEndpoint);
|
|
27
|
+
const balanceGuard = createApiKeyRetryGuard(blockedEndpoint);
|
|
17
28
|
function isRetryableHttpStatus(status) {
|
|
18
29
|
return status === 408 || status === 425 || status === 429 || status >= 500;
|
|
19
30
|
}
|
|
@@ -144,6 +155,7 @@ async function fetchNanoGptUsage(headers, requestTimeoutMs) {
|
|
|
144
155
|
timeoutMs: requestTimeoutMs,
|
|
145
156
|
consume: async (response) => {
|
|
146
157
|
if (!response.ok) {
|
|
158
|
+
const retryAtMs = retryAfterDeadline(response);
|
|
147
159
|
let text;
|
|
148
160
|
try {
|
|
149
161
|
text = await response.text();
|
|
@@ -156,6 +168,7 @@ async function fetchNanoGptUsage(headers, requestTimeoutMs) {
|
|
|
156
168
|
message: `NanoGPT API error ${response.status}: ${sanitizeDisplaySnippet(text, 120)}`,
|
|
157
169
|
retryable: isRetryableHttpStatus(response.status),
|
|
158
170
|
failureCategory: providerFailureCategoryForHttpStatus(response.status),
|
|
171
|
+
...(retryAtMs !== undefined ? { retryAtMs } : {}),
|
|
159
172
|
};
|
|
160
173
|
}
|
|
161
174
|
return {
|
|
@@ -184,6 +197,7 @@ async function fetchNanoGptBalance(headers, requestTimeoutMs) {
|
|
|
184
197
|
timeoutMs: requestTimeoutMs,
|
|
185
198
|
consume: async (response) => {
|
|
186
199
|
if (!response.ok) {
|
|
200
|
+
const retryAtMs = retryAfterDeadline(response);
|
|
187
201
|
let text;
|
|
188
202
|
try {
|
|
189
203
|
text = await response.text();
|
|
@@ -196,6 +210,7 @@ async function fetchNanoGptBalance(headers, requestTimeoutMs) {
|
|
|
196
210
|
message: `NanoGPT API error ${response.status}: ${sanitizeDisplaySnippet(text, 120)}`,
|
|
197
211
|
retryable: isRetryableHttpStatus(response.status),
|
|
198
212
|
failureCategory: providerFailureCategoryForHttpStatus(response.status),
|
|
213
|
+
...(retryAtMs !== undefined ? { retryAtMs } : {}),
|
|
199
214
|
};
|
|
200
215
|
}
|
|
201
216
|
return {
|
|
@@ -219,13 +234,14 @@ export async function queryNanoGptQuota(options = {}) {
|
|
|
219
234
|
const resolved = await resolveNanoGptApiKey();
|
|
220
235
|
if (!resolved)
|
|
221
236
|
return null;
|
|
237
|
+
const key = resolved.key;
|
|
222
238
|
const headers = {
|
|
223
|
-
"x-api-key":
|
|
239
|
+
"x-api-key": key,
|
|
224
240
|
"User-Agent": USER_AGENT,
|
|
225
241
|
};
|
|
226
242
|
const [usageResult, balanceResult] = await Promise.all([
|
|
227
|
-
fetchNanoGptUsage(headers, options.requestTimeoutMs),
|
|
228
|
-
fetchNanoGptBalance(headers, options.requestTimeoutMs),
|
|
243
|
+
usageGuard(key, () => fetchNanoGptUsage(headers, options.requestTimeoutMs), options.requestTimeoutMs),
|
|
244
|
+
balanceGuard(key, () => fetchNanoGptBalance(headers, options.requestTimeoutMs), options.requestTimeoutMs),
|
|
229
245
|
]);
|
|
230
246
|
const endpointErrors = [];
|
|
231
247
|
if (!usageResult.success) {
|
|
@@ -233,6 +249,7 @@ export async function queryNanoGptQuota(options = {}) {
|
|
|
233
249
|
endpoint: "usage",
|
|
234
250
|
message: usageResult.message,
|
|
235
251
|
failureCategory: usageResult.failureCategory,
|
|
252
|
+
...(usageResult.retryAtMs !== undefined ? { retryAtMs: usageResult.retryAtMs } : {}),
|
|
236
253
|
});
|
|
237
254
|
}
|
|
238
255
|
if (!balanceResult.success) {
|
|
@@ -240,6 +257,7 @@ export async function queryNanoGptQuota(options = {}) {
|
|
|
240
257
|
endpoint: "balance",
|
|
241
258
|
message: balanceResult.message,
|
|
242
259
|
failureCategory: balanceResult.failureCategory,
|
|
260
|
+
...(balanceResult.retryAtMs !== undefined ? { retryAtMs: balanceResult.retryAtMs } : {}),
|
|
243
261
|
});
|
|
244
262
|
}
|
|
245
263
|
else if (balanceResult.fieldErrors.length > 0) {
|
|
@@ -250,7 +268,12 @@ export async function queryNanoGptQuota(options = {}) {
|
|
|
250
268
|
});
|
|
251
269
|
}
|
|
252
270
|
if (!usageResult.success && !balanceResult.success) {
|
|
271
|
+
// A whole-query deadline exists only when both endpoints reported one.
|
|
272
|
+
const retryAtMs = usageResult.retryAtMs !== undefined && balanceResult.retryAtMs !== undefined
|
|
273
|
+
? Math.max(usageResult.retryAtMs, balanceResult.retryAtMs)
|
|
274
|
+
: undefined;
|
|
253
275
|
return {
|
|
276
|
+
...(retryAtMs !== undefined ? { retryAtMs } : {}),
|
|
254
277
|
success: false,
|
|
255
278
|
error: endpointErrors
|
|
256
279
|
.map((entry) => `${entry.endpoint === "usage" ? "Usage" : "Balance"}: ${entry.message}`)
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* endpoint.
|
|
7
7
|
*/
|
|
8
8
|
import type { OllamaCloudResult } from "./types.js";
|
|
9
|
-
declare function parseOllamaCloudBalance(payload: unknown): OllamaCloudResult
|
|
9
|
+
declare function parseOllamaCloudBalance(payload: unknown): Exclude<OllamaCloudResult, null>;
|
|
10
10
|
export declare function queryOllamaCloudQuota(options?: {
|
|
11
11
|
requestTimeoutMs?: number;
|
|
12
12
|
}): Promise<OllamaCloudResult>;
|
package/dist/lib/ollama-cloud.js
CHANGED
|
@@ -6,11 +6,18 @@
|
|
|
6
6
|
* endpoint.
|
|
7
7
|
*/
|
|
8
8
|
import { providerFailureCategoryForHttpStatus } from "../contracts/provider-failure.js";
|
|
9
|
+
import { createApiKeyRetryGuard, retryAfterDeadline } from "./api-key-retry-after.js";
|
|
9
10
|
import { sanitizeSingleLineDisplayText } from "./display-sanitize.js";
|
|
10
11
|
import { fetchWithTimeout } from "./http.js";
|
|
11
12
|
import { resolveOllamaCloudApiKey } from "./ollama-cloud-config.js";
|
|
12
13
|
const OLLAMA_CLOUD_BALANCE_URL = "https://ollama.com/api/balance";
|
|
13
14
|
const MAX_RESPONSE_BYTES = 256 * 1024;
|
|
15
|
+
const guard = createApiKeyRetryGuard((retryAtMs) => ({
|
|
16
|
+
success: false,
|
|
17
|
+
error: "Ollama Cloud balance API error 429",
|
|
18
|
+
failureCategory: "provider_api",
|
|
19
|
+
retryAtMs,
|
|
20
|
+
}));
|
|
14
21
|
function isRecord(value) {
|
|
15
22
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
16
23
|
}
|
|
@@ -154,42 +161,48 @@ export async function queryOllamaCloudQuota(options = {}) {
|
|
|
154
161
|
const resolved = await resolveOllamaCloudApiKey();
|
|
155
162
|
if (!resolved)
|
|
156
163
|
return null;
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
164
|
+
return guard(resolved.key, async () => {
|
|
165
|
+
let responseStatus;
|
|
166
|
+
let retryAtMs;
|
|
167
|
+
try {
|
|
168
|
+
return await fetchWithTimeout(OLLAMA_CLOUD_BALANCE_URL, {
|
|
169
|
+
request: {
|
|
170
|
+
method: "GET",
|
|
171
|
+
headers: {
|
|
172
|
+
Accept: "application/json",
|
|
173
|
+
Authorization: `Bearer ${resolved.key}`,
|
|
174
|
+
},
|
|
175
|
+
redirect: "manual",
|
|
165
176
|
},
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
177
|
+
timeoutMs: options.requestTimeoutMs,
|
|
178
|
+
consume: async (response) => {
|
|
179
|
+
responseStatus = response.status;
|
|
180
|
+
retryAtMs = retryAfterDeadline(response);
|
|
181
|
+
const text = await readBoundedText(response, MAX_RESPONSE_BYTES);
|
|
182
|
+
if (!response.ok) {
|
|
183
|
+
const snippet = sanitizeMessage(text, resolved.key);
|
|
184
|
+
return {
|
|
185
|
+
success: false,
|
|
186
|
+
error: `Ollama Cloud balance API error ${response.status}: ${snippet}`,
|
|
187
|
+
failureCategory: providerFailureCategoryForHttpStatus(response.status),
|
|
188
|
+
...(retryAtMs !== undefined ? { retryAtMs } : {}),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
return parseOllamaCloudBalance(JSON.parse(text));
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
return {
|
|
197
|
+
success: false,
|
|
198
|
+
error: sanitizeMessage(error instanceof Error ? error.message : String(error), resolved.key),
|
|
199
|
+
failureCategory: responseStatus === undefined
|
|
200
|
+
? "provider_api"
|
|
201
|
+
: providerFailureCategoryForHttpStatus(responseStatus),
|
|
202
|
+
...(retryAtMs !== undefined ? { retryAtMs } : {}),
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
}, options.requestTimeoutMs);
|
|
193
206
|
}
|
|
194
207
|
export { parseOllamaCloudBalance as _parseOllamaCloudBalance };
|
|
195
208
|
//# sourceMappingURL=ollama-cloud.js.map
|
package/dist/lib/openai.js
CHANGED
|
@@ -5,10 +5,12 @@
|
|
|
5
5
|
* https://chatgpt.com/backend-api/wham/usage
|
|
6
6
|
*/
|
|
7
7
|
import { providerFailureCategoryForHttpStatus } from "../contracts/provider-failure.js";
|
|
8
|
+
import { createApiKeyRetryGuard, retryAfterDeadline } from "./api-key-retry-after.js";
|
|
8
9
|
import { sanitizeDisplayText, sanitizeSingleLineDisplayText } from "./display-sanitize.js";
|
|
9
10
|
import { clampPercent } from "./format-utils.js";
|
|
10
|
-
import { fetchWithTimeout } from "./http.js";
|
|
11
|
+
import { fetchWithTimeout, RequestTimeoutError } from "./http.js";
|
|
11
12
|
import { readAuthFileCached } from "./opencode-auth.js";
|
|
13
|
+
import { buildFixedWindowEvidence } from "./quota-exhaustion-projection.js";
|
|
12
14
|
import { deriveResolvedAuthIdentity } from "./resolved-auth-identity.js";
|
|
13
15
|
function base64UrlDecode(input) {
|
|
14
16
|
const base64 = input.replace(/-/g, "+").replace(/_/g, "/");
|
|
@@ -95,17 +97,13 @@ function parseRateLimitWindow(window, observedAtMs) {
|
|
|
95
97
|
const value = parseWindowValue(window, observedAtMs);
|
|
96
98
|
if (!value)
|
|
97
99
|
return null;
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
endsAtIso: new Date(endsAtMs).toISOString(),
|
|
106
|
-
fullReset: true,
|
|
107
|
-
};
|
|
108
|
-
}
|
|
100
|
+
const fixedWindow = buildFixedWindowEvidence({
|
|
101
|
+
durationMs: raw.limit_window_seconds * 1000,
|
|
102
|
+
resetTimeIso: value.resetTimeIso,
|
|
103
|
+
observedAtMs,
|
|
104
|
+
});
|
|
105
|
+
if (fixedWindow)
|
|
106
|
+
value.fixedWindow = fixedWindow;
|
|
109
107
|
return { kind, value };
|
|
110
108
|
}
|
|
111
109
|
function derivePlanLabel(planType) {
|
|
@@ -121,6 +119,12 @@ function derivePlanLabel(planType) {
|
|
|
121
119
|
return "OpenAI";
|
|
122
120
|
}
|
|
123
121
|
const OPENAI_USAGE_URL = "https://chatgpt.com/backend-api/wham/usage";
|
|
122
|
+
const guard = createApiKeyRetryGuard((retryAtMs) => ({
|
|
123
|
+
success: false,
|
|
124
|
+
error: "OpenAI API error 429",
|
|
125
|
+
failureCategory: "provider_api",
|
|
126
|
+
retryAtMs,
|
|
127
|
+
}));
|
|
124
128
|
const OPENAI_RESET_CREDITS_URL = "https://chatgpt.com/backend-api/wham/rate-limit-reset-credits";
|
|
125
129
|
export const DEFAULT_OPENAI_AUTH_CACHE_MAX_AGE_MS = 5_000;
|
|
126
130
|
// Not `opencode`: in OpenCode 2 its OAuth entry is the Console login, which
|
|
@@ -257,27 +261,47 @@ function parseAvailableResetCredits(payload) {
|
|
|
257
261
|
: Date.parse(credit.expiresAtIso);
|
|
258
262
|
return credits.sort((left, right) => rank(left) - rank(right));
|
|
259
263
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
264
|
+
// Separate from the usage guard: a limited detail endpoint must not block usage.
|
|
265
|
+
const resetCreditsGuard = createApiKeyRetryGuard((retryAtMs) => ({
|
|
266
|
+
success: false,
|
|
267
|
+
credits: [],
|
|
268
|
+
retryAtMs,
|
|
269
|
+
}));
|
|
270
|
+
async function fetchOpenAIResetCredits(headers, timeoutMs, cooldownKey) {
|
|
271
|
+
const result = await resetCreditsGuard(cooldownKey, async () => {
|
|
272
|
+
try {
|
|
273
|
+
return await fetchWithTimeout(OPENAI_RESET_CREDITS_URL, {
|
|
274
|
+
request: { headers },
|
|
275
|
+
timeoutMs,
|
|
276
|
+
consume: async (resp) => {
|
|
277
|
+
if (!resp.ok) {
|
|
278
|
+
const retryAtMs = retryAfterDeadline(resp);
|
|
279
|
+
return {
|
|
280
|
+
success: false,
|
|
281
|
+
credits: [],
|
|
282
|
+
...(retryAtMs !== undefined ? { retryAtMs } : {}),
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
try {
|
|
286
|
+
return { success: true, credits: parseAvailableResetCredits(await resp.json()) };
|
|
287
|
+
}
|
|
288
|
+
catch {
|
|
289
|
+
return { success: false, credits: [] };
|
|
290
|
+
}
|
|
291
|
+
},
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
catch (error) {
|
|
295
|
+
return {
|
|
296
|
+
success: false,
|
|
297
|
+
credits: [],
|
|
298
|
+
...(error instanceof RequestTimeoutError && error.retryAtMs !== undefined
|
|
299
|
+
? { retryAtMs: error.retryAtMs }
|
|
300
|
+
: {}),
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
}, timeoutMs);
|
|
304
|
+
return result.credits;
|
|
281
305
|
}
|
|
282
306
|
export async function queryOpenAIQuota(options = {}) {
|
|
283
307
|
const resolvedAuth = options.auth ??
|
|
@@ -313,16 +337,24 @@ export async function queryOpenAIQuota(options = {}) {
|
|
|
313
337
|
if (accountId) {
|
|
314
338
|
headers["ChatGPT-Account-Id"] = accountId;
|
|
315
339
|
}
|
|
316
|
-
|
|
340
|
+
// The ChatGPT account id names a workspace shared by its members, so it
|
|
341
|
+
// cannot identify a credential (and an email claim does not prove identity
|
|
342
|
+
// either). Key on the credential itself: the refresh token survives access
|
|
343
|
+
// token rotation, the access token is the fallback when there is none. The
|
|
344
|
+
// guard hashes the key and never retains it.
|
|
345
|
+
const cooldownKey = `${accountId ?? ""}\0${resolvedAuth.refreshToken ?? resolvedAuth.accessToken}`;
|
|
346
|
+
const result = await guard(cooldownKey, () => fetchWithTimeout(OPENAI_USAGE_URL, {
|
|
317
347
|
request: { headers },
|
|
318
348
|
timeoutMs: options.requestTimeoutMs,
|
|
319
349
|
consume: async (resp) => {
|
|
320
350
|
if (!resp.ok) {
|
|
351
|
+
const retryAtMs = retryAfterDeadline(resp);
|
|
321
352
|
const code = await readOpenAIErrorCode(resp);
|
|
322
353
|
return {
|
|
323
354
|
success: false,
|
|
324
355
|
error: `OpenAI API error ${resp.status}${code ? ` (${code})` : ""}`,
|
|
325
356
|
failureCategory: providerFailureCategoryForHttpStatus(resp.status),
|
|
357
|
+
...(retryAtMs !== undefined ? { retryAtMs } : {}),
|
|
326
358
|
};
|
|
327
359
|
}
|
|
328
360
|
const data = (await resp.json());
|
|
@@ -366,7 +398,9 @@ export async function queryOpenAIQuota(options = {}) {
|
|
|
366
398
|
Number(mappedCredits.balance) > 0));
|
|
367
399
|
// Empty bodies stay invalid; a useful Credits-only/reset-count-only
|
|
368
400
|
// payload is a real measurement even without a percent window.
|
|
369
|
-
if (Object.keys(windows).length === 0 &&
|
|
401
|
+
if (Object.keys(windows).length === 0 &&
|
|
402
|
+
!positiveCredits &&
|
|
403
|
+
availableResetCount === null) {
|
|
370
404
|
return { success: false, error: "No quota data", failureCategory: "provider_api" };
|
|
371
405
|
}
|
|
372
406
|
return {
|
|
@@ -380,11 +414,20 @@ export async function queryOpenAIQuota(options = {}) {
|
|
|
380
414
|
: {}),
|
|
381
415
|
};
|
|
382
416
|
},
|
|
383
|
-
})
|
|
384
|
-
|
|
417
|
+
}).catch((err) => {
|
|
418
|
+
if (!(err instanceof RequestTimeoutError) || err.retryAtMs === undefined)
|
|
419
|
+
throw err;
|
|
420
|
+
return {
|
|
421
|
+
success: false,
|
|
422
|
+
error: sanitizeDisplayText(err.message),
|
|
423
|
+
failureCategory: "provider_api",
|
|
424
|
+
retryAtMs: err.retryAtMs,
|
|
425
|
+
};
|
|
426
|
+
}), options.requestTimeoutMs);
|
|
427
|
+
if (!result.success || !result.bankedResets || !options.includeResetDetails) {
|
|
385
428
|
return result;
|
|
386
429
|
}
|
|
387
|
-
result.bankedResets.credits = await fetchOpenAIResetCredits(headers, options.requestTimeoutMs);
|
|
430
|
+
result.bankedResets.credits = await fetchOpenAIResetCredits(headers, options.requestTimeoutMs, `reset-credits:${cooldownKey}`);
|
|
388
431
|
return result;
|
|
389
432
|
}
|
|
390
433
|
catch (err) {
|
|
@@ -392,6 +435,9 @@ export async function queryOpenAIQuota(options = {}) {
|
|
|
392
435
|
success: false,
|
|
393
436
|
error: sanitizeDisplayText(err instanceof Error ? err.message : String(err)),
|
|
394
437
|
failureCategory: "provider_api",
|
|
438
|
+
...(err instanceof RequestTimeoutError && err.retryAtMs !== undefined
|
|
439
|
+
? { retryAtMs: err.retryAtMs }
|
|
440
|
+
: {}),
|
|
395
441
|
};
|
|
396
442
|
}
|
|
397
443
|
}
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* `opencode-quota-gui show` and `status` bind this source while one report runs, so they work
|
|
5
5
|
* with OpenCode closed. It is the only code that queries OpenCode's `credential` table. It
|
|
6
6
|
* never refreshes or writes a login: a sign-in whose token expired stays expired here until
|
|
7
|
-
* OpenCode refreshes it. The server plugin
|
|
8
|
-
* (`tests/
|
|
7
|
+
* OpenCode refreshes it. The server plugin must never import this file
|
|
8
|
+
* (`tests/plugin-dist-import-graph.test.ts`).
|
|
9
9
|
*/
|
|
10
10
|
import { type CredentialSource } from "./opencode-auth.js";
|
|
11
11
|
/**
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* `opencode-quota-gui show` and `status` bind this source while one report runs, so they work
|
|
5
5
|
* with OpenCode closed. It is the only code that queries OpenCode's `credential` table. It
|
|
6
6
|
* never refreshes or writes a login: a sign-in whose token expired stays expired here until
|
|
7
|
-
* OpenCode refreshes it. The server plugin
|
|
8
|
-
* (`tests/
|
|
7
|
+
* OpenCode refreshes it. The server plugin must never import this file
|
|
8
|
+
* (`tests/plugin-dist-import-graph.test.ts`).
|
|
9
9
|
*/
|
|
10
10
|
import { existsSync } from "fs";
|
|
11
11
|
import { getCredentialDatabasePaths, } from "./opencode-auth.js";
|
|
@@ -58,5 +58,6 @@ export declare function applyConfigDocumentEdit(edit: ConfigDocumentEdit, option
|
|
|
58
58
|
pathExists?: (path: string) => boolean;
|
|
59
59
|
writeText?: (path: string, content: string) => Promise<void>;
|
|
60
60
|
removePath?: (path: string) => Promise<void>;
|
|
61
|
+
beforeRename?: () => Promise<void>;
|
|
61
62
|
}): Promise<void>;
|
|
62
63
|
//# sourceMappingURL=opencode-config-editor.d.ts.map
|
|
@@ -352,15 +352,20 @@ export async function applyConfigDocumentEdit(edit, options = {}) {
|
|
|
352
352
|
return;
|
|
353
353
|
}
|
|
354
354
|
await validateConfigDocumentEdit(edit, options);
|
|
355
|
+
const validateBeforeRename = async () => {
|
|
356
|
+
await options.beforeRename?.();
|
|
357
|
+
await validateConfigDocumentEdit(edit, options);
|
|
358
|
+
};
|
|
355
359
|
const removePath = options.removePath ?? ((path) => rm(path));
|
|
356
360
|
try {
|
|
357
361
|
const currentTarget = await resolveConfigWriteTarget(edit.path);
|
|
358
362
|
assertSameConfigWriteTarget(edit.writeTarget, currentTarget);
|
|
359
363
|
if (options.writeText) {
|
|
364
|
+
await validateBeforeRename();
|
|
360
365
|
await options.writeText(currentTarget.writePath, edit.updated);
|
|
361
366
|
}
|
|
362
367
|
else {
|
|
363
|
-
await writeResolvedConfigText(currentTarget, edit.updated);
|
|
368
|
+
await writeResolvedConfigText(currentTarget, edit.updated, validateBeforeRename);
|
|
364
369
|
}
|
|
365
370
|
}
|
|
366
371
|
catch (error) {
|
package/dist/lib/opencode-go.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createApiKeyRetryGuard, retryAfterDeadline } from "./api-key-retry-after.js";
|
|
1
2
|
import { sanitizeDisplayText } from "./display-sanitize.js";
|
|
2
3
|
import { fetchWithTimeout } from "./http.js";
|
|
3
4
|
import { consoleBaseUrl, consoleHeaders, } from "./opencode-console-auth.js";
|
|
@@ -118,7 +119,19 @@ function isNotSubscribedResponse(status, text) {
|
|
|
118
119
|
return false;
|
|
119
120
|
}
|
|
120
121
|
}
|
|
121
|
-
|
|
122
|
+
const rateLimited = (retryAtMs) => ({
|
|
123
|
+
success: false,
|
|
124
|
+
error: "OpenCode Go API error 429",
|
|
125
|
+
retryable: true,
|
|
126
|
+
failureCategory: "provider_api",
|
|
127
|
+
retryAtMs,
|
|
128
|
+
});
|
|
129
|
+
const apiKeyGuard = createApiKeyRetryGuard(rateLimited);
|
|
130
|
+
const consoleGuard = createApiKeyRetryGuard(rateLimited);
|
|
131
|
+
export function queryOpenCodeGoQuota(accessToken, options = {}) {
|
|
132
|
+
return apiKeyGuard(accessToken, () => fetchOpenCodeGoQuota(accessToken, options), options.requestTimeoutMs);
|
|
133
|
+
}
|
|
134
|
+
async function fetchOpenCodeGoQuota(accessToken, options) {
|
|
122
135
|
try {
|
|
123
136
|
return await fetchWithTimeout(OPENCODE_GO_USAGE_URL, {
|
|
124
137
|
request: {
|
|
@@ -131,6 +144,7 @@ export async function queryOpenCodeGoQuota(accessToken, options = {}) {
|
|
|
131
144
|
timeoutMs: options.requestTimeoutMs,
|
|
132
145
|
consume: async (response) => {
|
|
133
146
|
if (!response.ok) {
|
|
147
|
+
const retryAtMs = retryAfterDeadline(response);
|
|
134
148
|
let text;
|
|
135
149
|
try {
|
|
136
150
|
text = await response.text();
|
|
@@ -141,6 +155,7 @@ export async function queryOpenCodeGoQuota(accessToken, options = {}) {
|
|
|
141
155
|
error: `OpenCode Go API error ${response.status}: ${errorMessage(error, accessToken)}`,
|
|
142
156
|
retryable: isRetryableHttpStatus(response.status),
|
|
143
157
|
failureCategory: httpFailureCategory(response.status),
|
|
158
|
+
...(retryAtMs !== undefined ? { retryAtMs } : {}),
|
|
144
159
|
};
|
|
145
160
|
}
|
|
146
161
|
if (isNotSubscribedResponse(response.status, text)) {
|
|
@@ -156,6 +171,7 @@ export async function queryOpenCodeGoQuota(accessToken, options = {}) {
|
|
|
156
171
|
error: `OpenCode Go API error ${response.status}: ${sanitizeMessage(text, accessToken)}`,
|
|
157
172
|
retryable: isRetryableHttpStatus(response.status),
|
|
158
173
|
failureCategory: httpFailureCategory(response.status),
|
|
174
|
+
...(retryAtMs !== undefined ? { retryAtMs } : {}),
|
|
159
175
|
};
|
|
160
176
|
}
|
|
161
177
|
let payload;
|
|
@@ -219,7 +235,16 @@ function normalizeConsoleMeter(windowKey, meter) {
|
|
|
219
235
|
* accounts without any pre-2.0 workspace API key. The request goes to the
|
|
220
236
|
* login's Console server, scoped to the login's org when it has one.
|
|
221
237
|
*/
|
|
222
|
-
export
|
|
238
|
+
export function queryOpenCodeGoConsoleStatus(credential, options = {}) {
|
|
239
|
+
// Same token under another org or server is a different account.
|
|
240
|
+
const identity = JSON.stringify([
|
|
241
|
+
credential.accessToken,
|
|
242
|
+
credential.orgId ?? null,
|
|
243
|
+
consoleBaseUrl(credential),
|
|
244
|
+
]);
|
|
245
|
+
return consoleGuard(identity, () => fetchOpenCodeGoConsoleStatus(credential, options), options.requestTimeoutMs);
|
|
246
|
+
}
|
|
247
|
+
async function fetchOpenCodeGoConsoleStatus(credential, options) {
|
|
223
248
|
try {
|
|
224
249
|
return await fetchWithTimeout(`${consoleBaseUrl(credential)}/api/go/status`, {
|
|
225
250
|
request: {
|
|
@@ -240,11 +265,13 @@ export async function queryOpenCodeGoConsoleStatus(credential, options = {}) {
|
|
|
240
265
|
notSubscribed: true,
|
|
241
266
|
};
|
|
242
267
|
}
|
|
268
|
+
const retryAtMs = retryAfterDeadline(response);
|
|
243
269
|
return {
|
|
244
270
|
success: false,
|
|
245
271
|
error: `OpenCode Console API error ${response.status} (/api/go/status)`,
|
|
246
272
|
retryable: isRetryableHttpStatus(response.status),
|
|
247
273
|
failureCategory: httpFailureCategory(response.status),
|
|
274
|
+
...(retryAtMs !== undefined ? { retryAtMs } : {}),
|
|
248
275
|
};
|
|
249
276
|
}
|
|
250
277
|
let payload;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type OpenCodeMajor = 1 | 2;
|
|
2
|
+
/** Numeric host version; a prerelease/build suffix is ignored. */
|
|
3
|
+
export interface OpenCodeVersion {
|
|
4
|
+
major: number;
|
|
5
|
+
minor: number;
|
|
6
|
+
patch: number;
|
|
7
|
+
}
|
|
8
|
+
/** Parses the numeric version core; a prerelease or build suffix is ignored. */
|
|
9
|
+
export declare function parseOpenCodeVersion(output: string): OpenCodeVersion | undefined;
|
|
10
|
+
/** OpenCode 1 prints a bare `1.18.32`; OpenCode 2 prints `opencode v2.0.16`. */
|
|
11
|
+
export declare function parseOpenCodeMajor(output: string): OpenCodeMajor | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Whether the host has the full Native GUI baseline (2.2.1+). Missing or
|
|
14
|
+
* unparseable versions are not assumed capable.
|
|
15
|
+
*/
|
|
16
|
+
export declare function supportsNativeGui(version: string | OpenCodeVersion | undefined): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Whether the host predates the Native GUI and therefore keeps the compatibility
|
|
19
|
+
* web dashboard on by default. Only a parseable version below 2.2.1 qualifies;
|
|
20
|
+
* missing or unparseable versions fail closed.
|
|
21
|
+
*/
|
|
22
|
+
export declare function isPreNativeGuiVersion(version: string | OpenCodeVersion | undefined): boolean;
|
|
23
|
+
//# sourceMappingURL=opencode-version-core.d.ts.map
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** The numeric core of `1.18.32`, `opencode v2.2.0`, or `2.2.1-beta.1`. */
|
|
2
|
+
const OPENCODE_VERSION_PATTERN = /^(?:opencode\s+)?v?(\d+)\.(\d+)\.(\d+)/;
|
|
3
|
+
/** Parses the numeric version core; a prerelease or build suffix is ignored. */
|
|
4
|
+
export function parseOpenCodeVersion(output) {
|
|
5
|
+
const match = OPENCODE_VERSION_PATTERN.exec(output.trim());
|
|
6
|
+
if (!match)
|
|
7
|
+
return undefined;
|
|
8
|
+
const major = Number(match[1]);
|
|
9
|
+
const minor = Number(match[2]);
|
|
10
|
+
const patch = Number(match[3]);
|
|
11
|
+
if (![major, minor, patch].every((part) => Number.isSafeInteger(part)))
|
|
12
|
+
return undefined;
|
|
13
|
+
return { major, minor, patch };
|
|
14
|
+
}
|
|
15
|
+
/** OpenCode 1 prints a bare `1.18.32`; OpenCode 2 prints `opencode v2.0.16`. */
|
|
16
|
+
export function parseOpenCodeMajor(output) {
|
|
17
|
+
const version = parseOpenCodeVersion(output);
|
|
18
|
+
if (version?.major === 1)
|
|
19
|
+
return 1;
|
|
20
|
+
if (version?.major === 2)
|
|
21
|
+
return 2;
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
/** Compares the numeric parts only; a prerelease suffix never lowers the result. */
|
|
25
|
+
function isAtLeast(version, major, minor, patch) {
|
|
26
|
+
if (version.major !== major)
|
|
27
|
+
return version.major > major;
|
|
28
|
+
if (version.minor !== minor)
|
|
29
|
+
return version.minor > minor;
|
|
30
|
+
return version.patch >= patch;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Whether the host has the full Native GUI baseline (2.2.1+). Missing or
|
|
34
|
+
* unparseable versions are not assumed capable.
|
|
35
|
+
*/
|
|
36
|
+
export function supportsNativeGui(version) {
|
|
37
|
+
const parsed = typeof version === "string" ? parseOpenCodeVersion(version) : version;
|
|
38
|
+
return parsed !== undefined && isAtLeast(parsed, 2, 2, 1);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Whether the host predates the Native GUI and therefore keeps the compatibility
|
|
42
|
+
* web dashboard on by default. Only a parseable version below 2.2.1 qualifies;
|
|
43
|
+
* missing or unparseable versions fail closed.
|
|
44
|
+
*/
|
|
45
|
+
export function isPreNativeGuiVersion(version) {
|
|
46
|
+
const parsed = typeof version === "string" ? parseOpenCodeVersion(version) : version;
|
|
47
|
+
return parsed !== undefined && !isAtLeast(parsed, 2, 2, 1);
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=opencode-version-core.js.map
|
|
@@ -1,25 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export interface OpenCodeVersion {
|
|
4
|
-
major: number;
|
|
5
|
-
minor: number;
|
|
6
|
-
patch: number;
|
|
7
|
-
}
|
|
8
|
-
/** Parses the numeric version core; a prerelease or build suffix is ignored. */
|
|
9
|
-
export declare function parseOpenCodeVersion(output: string): OpenCodeVersion | undefined;
|
|
10
|
-
/** OpenCode 1 prints a bare `1.18.32`; OpenCode 2 prints `opencode v2.0.16`. */
|
|
11
|
-
export declare function parseOpenCodeMajor(output: string): OpenCodeMajor | undefined;
|
|
12
|
-
/**
|
|
13
|
-
* Whether the host has the Native GUI baseline (2.2.0+). Missing or
|
|
14
|
-
* unparseable versions are not assumed capable.
|
|
15
|
-
*/
|
|
16
|
-
export declare function supportsNativeGui(version: string | OpenCodeVersion | undefined): boolean;
|
|
17
|
-
/**
|
|
18
|
-
* Whether the host predates the Native GUI and therefore keeps the compatibility
|
|
19
|
-
* web dashboard on by default. Only a parseable version below 2.2.0 qualifies;
|
|
20
|
-
* missing or unparseable versions fail closed.
|
|
21
|
-
*/
|
|
22
|
-
export declare function isPreNativeGuiVersion(version: string | OpenCodeVersion | undefined): boolean;
|
|
1
|
+
import { type OpenCodeMajor, type OpenCodeVersion } from "./opencode-version-core.js";
|
|
2
|
+
export * from "./opencode-version-core.js";
|
|
23
3
|
/** Returns undefined when `opencode` is not on PATH, fails, times out, or prints an unknown version. */
|
|
24
4
|
export declare function detectOpenCodeVersion(runVersion?: () => Promise<string>): Promise<OpenCodeVersion | undefined>;
|
|
25
5
|
/** Returns undefined when `opencode` is not on PATH, fails, times out, or prints an unknown version. */
|