pantheon-opencode 1.6.1 → 1.6.2-beta.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/AGENTS.global.md +11 -11
- package/AGENTS.md +2 -2
- package/CHANGELOG.md +29 -0
- package/README.md +100 -19
- package/README.pt-BR.md +27 -11
- package/bin/pantheon-init.mjs +106 -19
- package/package.json +3 -2
- package/plugin.json +1 -1
- package/pyproject.toml +1 -1
- package/scripts/build-agents-md.mjs +1 -1
- package/scripts/doctor.mjs +213 -45
- package/scripts/install/agents-md.mjs +2 -1
- package/scripts/install/config-migration.mjs +344 -393
- package/scripts/install/config-paths.mjs +15 -0
- package/scripts/install/mcp-permissions.mjs +164 -0
- package/scripts/install/opencode-version.mjs +31 -13
- package/scripts/install/opencode-wildcard.mjs +106 -0
- package/scripts/install/opencode.mjs +240 -69
- package/scripts/install/plan-artifact.mjs +12 -1
- package/scripts/install/plugin.mjs +17 -1
- package/scripts/install/v2-capabilities.mjs +5 -6
- package/scripts/package-evidence.mjs +1 -0
- package/scripts/sandbox-env.sh +174 -0
- package/scripts/setup-v2-config.mjs +1703 -0
- package/scripts/setup.mjs +415 -0
- package/scripts/sync-tui.mjs +9 -1
- package/scripts/test-opencode-v2-sandbox.sh +438 -151
- package/scripts/update-skills-lock.mjs +30 -0
- package/skills-lock.json +10 -10
- package/src/agents/aphrodite.md +5 -2
- package/src/agents/apollo.md +5 -0
- package/src/agents/athena.md +6 -1
- package/src/agents/demeter.md +5 -1
- package/src/agents/gaia.md +4 -0
- package/src/agents/hephaestus.md +6 -1
- package/src/agents/hermes.md +6 -3
- package/src/agents/iris.md +5 -1
- package/src/agents/mnemosyne.md +20 -50
- package/src/agents/nyx.md +5 -1
- package/src/agents/prometheus.md +4 -0
- package/src/agents/talos.md +4 -0
- package/src/agents/themis.md +4 -0
- package/src/agents/zeus.md +9 -3
- package/src/instructions/agent-return-format.instructions.md +1 -1
- package/src/instructions/memory-protocol.instructions.md +3 -3
- package/src/instructions/test-economy.instructions.md +6 -6
- package/src/mcp/pantheon_vision.py +37 -36
- package/src/pantheon/cost-command.ts +30 -133
- package/src/pantheon/model-pricing.ts +1 -536
- package/src/pantheon/v2-vision.ts +34 -29
- package/src/pantheon/vision.ts +43 -10
- package/src/plugin-v2.ts +30 -53
- package/src/plugins/tui/THIRD_PARTY_NOTICES.md +24 -0
- package/src/plugins/tui/dist/cost-data-CCZUs1aJ.js +749 -0
- package/src/plugins/tui/dist/cost-data-CCZUs1aJ.js.map +1 -0
- package/src/plugins/tui/dist/quota-adapters.d.ts +89 -0
- package/src/plugins/tui/dist/quota-adapters.d.ts.map +1 -0
- package/src/plugins/tui/dist/quota-adapters.js +1142 -0
- package/src/plugins/tui/dist/quota-adapters.js.map +1 -0
- package/src/plugins/tui/dist/quota-cli.d.ts +17 -0
- package/src/plugins/tui/dist/quota-cli.d.ts.map +1 -0
- package/src/plugins/tui/dist/quota-cli.js +184 -0
- package/src/plugins/tui/dist/quota-cli.js.map +1 -0
- package/src/plugins/tui/dist/quota-rpc-DMmXBhBe.js +684 -0
- package/src/plugins/tui/dist/quota-rpc-DMmXBhBe.js.map +1 -0
- package/src/plugins/tui/dist/server-v2.d.ts +14 -0
- package/src/plugins/tui/dist/server-v2.d.ts.map +1 -0
- package/src/plugins/tui/dist/server-v2.js +796 -0
- package/src/plugins/tui/dist/server-v2.js.map +1 -0
- package/src/plugins/tui/dist/tui-v2.d.ts +375 -53
- package/src/plugins/tui/dist/tui-v2.d.ts.map +1 -1
- package/src/plugins/tui/dist/tui-v2.js +5876 -530
- package/src/plugins/tui/dist/tui-v2.js.map +1 -1
- package/src/plugins/tui/dist/tui.js +1 -1
- package/src/plugins/tui/dist/usage-history-contract-nt1rnBPE.js +118 -0
- package/src/plugins/tui/dist/usage-history-contract-nt1rnBPE.js.map +1 -0
- package/src/plugins/tui/dist/usage-history-worker.js +25 -0
- package/src/plugins/tui/dist/usage-history-worker.js.map +1 -0
- package/src/plugins/tui/index-v2.ts +3 -6
- package/src/plugins/tui/package-lock.json +17 -2
- package/src/plugins/tui/package.json +6 -3
- package/src/plugins/tui/src/context-gauge.ts +59 -0
- package/src/plugins/tui/src/cost-data.ts +607 -0
- package/src/plugins/tui/src/go-key-diagnostic-ui.ts +100 -0
- package/src/plugins/tui/src/go-key-diagnostic.ts +125 -0
- package/src/plugins/tui/src/index-v2.tsx +231 -240
- package/src/plugins/tui/src/model-pricing.ts +536 -0
- package/src/plugins/tui/src/model-usage-limits.ts +4 -0
- package/src/plugins/tui/src/quota-adapters.ts +1598 -0
- package/src/plugins/tui/src/quota-cli.ts +237 -0
- package/src/plugins/tui/src/quota-rpc.ts +373 -0
- package/src/plugins/tui/src/quota-v2-model.ts +1032 -0
- package/src/plugins/tui/src/quota-v2.tsx +1238 -0
- package/src/plugins/tui/src/server-v2.ts +985 -0
- package/src/plugins/tui/src/sidebar-width.ts +129 -0
- package/src/plugins/tui/src/usage-history-contract.ts +217 -0
- package/src/plugins/tui/src/usage-history-validation.ts +104 -0
- package/src/plugins/tui/src/usage-history-view.tsx +236 -0
- package/src/plugins/tui/src/usage-history-worker.ts +25 -0
- package/src/plugins/tui/src/usage-view-model.ts +272 -0
- package/src/plugins/tui/tsconfig.typecheck.json +6 -1
- package/src/plugins/tui/tsdown.config.ts +44 -5
- package/src/routing.yml +72 -13
- package/src/skills/README.md +4 -0
- package/src/skills/agent-coordination/SKILL.md +0 -3
- package/src/skills/artifact-management/SKILL.md +0 -3
- package/src/skills/auto-continue/SKILL.md +0 -3
- package/src/skills/code-review-checklist/SKILL.md +0 -3
- package/src/skills/context-compression/SKILL.md +0 -3
- package/src/skills/memory-bank/SKILL.md +6 -28
- package/src/skills/session-goal/SKILL.md +0 -3
- package/src/skills/simplify/SKILL.md +5 -4
- package/src/skills/tdd-with-agents/SKILL.md +0 -3
- package/src/skills/visual-review-pipeline/SKILL.md +0 -1
- package/src/pantheon/memory-context.ts +0 -1017
|
@@ -0,0 +1,1142 @@
|
|
|
1
|
+
import "./quota-rpc-DMmXBhBe.js";
|
|
2
|
+
import { queryAlibabaTokenPlanUsage } from "./quota-cli.js";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
//#region src/quota-adapters.ts
|
|
5
|
+
const OPENAI_CODEX_OAUTH_METHOD_IDS = ["chatgpt-browser", "chatgpt-headless"];
|
|
6
|
+
function isOpenAICodexOAuthMethod(value) {
|
|
7
|
+
return typeof value === "string" && OPENAI_CODEX_OAUTH_METHOD_IDS.includes(value);
|
|
8
|
+
}
|
|
9
|
+
const OPENCODE_CONSOLE_OAUTH_METHOD_IDS = ["device"];
|
|
10
|
+
function isOpenCodeConsoleOAuthMethod(value) {
|
|
11
|
+
return typeof value === "string" && OPENCODE_CONSOLE_OAUTH_METHOD_IDS.includes(value);
|
|
12
|
+
}
|
|
13
|
+
const usBoth = ["us_personal", "us_business"];
|
|
14
|
+
const chinaPersonal = ["china_personal"];
|
|
15
|
+
const chinaBoth = ["china_personal", "china_business"];
|
|
16
|
+
const blocked = (provider, blockedReason) => ({
|
|
17
|
+
...provider,
|
|
18
|
+
blockedReason
|
|
19
|
+
});
|
|
20
|
+
/**
|
|
21
|
+
* Closed provider/integration/method allowlist. Only rows with a reviewed
|
|
22
|
+
* adapter may make a network request; remaining rows are explicitly blocked.
|
|
23
|
+
* Audience lists describe the approved matrix and do not claim that account
|
|
24
|
+
* type was inferred from credentials.
|
|
25
|
+
*/
|
|
26
|
+
const QUOTA_PROVIDER_CATALOG = {
|
|
27
|
+
anthropic: {
|
|
28
|
+
label: "Anthropic / Claude",
|
|
29
|
+
integrationID: "anthropic",
|
|
30
|
+
audiences: usBoth,
|
|
31
|
+
scope: "account",
|
|
32
|
+
methods: ["oauth"],
|
|
33
|
+
adapter: "anthropic"
|
|
34
|
+
},
|
|
35
|
+
chutes: {
|
|
36
|
+
label: "Chutes AI",
|
|
37
|
+
integrationID: "chutes",
|
|
38
|
+
audiences: usBoth,
|
|
39
|
+
scope: "account",
|
|
40
|
+
methods: ["key"],
|
|
41
|
+
adapter: "chutes"
|
|
42
|
+
},
|
|
43
|
+
cursor: blocked({
|
|
44
|
+
label: "Cursor",
|
|
45
|
+
integrationID: "cursor",
|
|
46
|
+
audiences: usBoth,
|
|
47
|
+
scope: "account",
|
|
48
|
+
methods: ["oauth", "key"]
|
|
49
|
+
}, "runtime_accounting_unavailable"),
|
|
50
|
+
"github-copilot": {
|
|
51
|
+
label: "GitHub Copilot",
|
|
52
|
+
integrationID: "github-copilot",
|
|
53
|
+
audiences: ["us_personal"],
|
|
54
|
+
scope: "account",
|
|
55
|
+
methods: ["oauth"],
|
|
56
|
+
adapter: "copilot"
|
|
57
|
+
},
|
|
58
|
+
"google-agy": {
|
|
59
|
+
label: "Google AGY",
|
|
60
|
+
integrationID: "google-agy",
|
|
61
|
+
audiences: usBoth,
|
|
62
|
+
scope: "account",
|
|
63
|
+
methods: ["oauth"],
|
|
64
|
+
adapter: "google-agy"
|
|
65
|
+
},
|
|
66
|
+
"kilo-gateway": {
|
|
67
|
+
label: "Kilo Gateway",
|
|
68
|
+
integrationID: "kilo-gateway",
|
|
69
|
+
audiences: ["us_personal"],
|
|
70
|
+
scope: "account",
|
|
71
|
+
methods: ["key"],
|
|
72
|
+
adapter: "kilo-gateway"
|
|
73
|
+
},
|
|
74
|
+
nanogpt: {
|
|
75
|
+
label: "NanoGPT",
|
|
76
|
+
integrationID: "nanogpt",
|
|
77
|
+
audiences: usBoth,
|
|
78
|
+
scope: "account",
|
|
79
|
+
methods: ["key"],
|
|
80
|
+
adapter: "nanogpt"
|
|
81
|
+
},
|
|
82
|
+
"ollama-cloud": {
|
|
83
|
+
label: "Ollama Cloud",
|
|
84
|
+
integrationID: "ollama-cloud",
|
|
85
|
+
audiences: ["us_personal"],
|
|
86
|
+
scope: "account",
|
|
87
|
+
methods: ["key"],
|
|
88
|
+
adapter: "ollama-cloud"
|
|
89
|
+
},
|
|
90
|
+
openai: {
|
|
91
|
+
label: "OpenAI / Codex",
|
|
92
|
+
integrationID: "openai",
|
|
93
|
+
audiences: usBoth,
|
|
94
|
+
scope: "account",
|
|
95
|
+
methods: ["oauth"],
|
|
96
|
+
adapter: "openai"
|
|
97
|
+
},
|
|
98
|
+
"opencode-go": {
|
|
99
|
+
label: "OpenCode Go",
|
|
100
|
+
integrationID: "opencode-go",
|
|
101
|
+
fallbackIntegrationIDs: ["opencode"],
|
|
102
|
+
audiences: ["us_personal"],
|
|
103
|
+
scope: "account",
|
|
104
|
+
methods: ["key", "oauth"],
|
|
105
|
+
adapter: "opencode-go"
|
|
106
|
+
},
|
|
107
|
+
"opencode-zen": {
|
|
108
|
+
label: "OpenCode Zen",
|
|
109
|
+
integrationID: "opencode",
|
|
110
|
+
audiences: usBoth,
|
|
111
|
+
scope: "account",
|
|
112
|
+
methods: ["oauth"],
|
|
113
|
+
adapter: "opencode-zen"
|
|
114
|
+
},
|
|
115
|
+
openrouter: {
|
|
116
|
+
label: "OpenRouter",
|
|
117
|
+
integrationID: "openrouter",
|
|
118
|
+
audiences: usBoth,
|
|
119
|
+
scope: "selected_key",
|
|
120
|
+
methods: ["key"],
|
|
121
|
+
adapter: "openrouter"
|
|
122
|
+
},
|
|
123
|
+
synthetic: {
|
|
124
|
+
label: "Synthetic",
|
|
125
|
+
integrationID: "synthetic",
|
|
126
|
+
audiences: usBoth,
|
|
127
|
+
scope: "account",
|
|
128
|
+
methods: ["key"],
|
|
129
|
+
adapter: "synthetic"
|
|
130
|
+
},
|
|
131
|
+
"xai-supergrok": {
|
|
132
|
+
label: "xAI SuperGrok",
|
|
133
|
+
integrationID: "xai",
|
|
134
|
+
audiences: usBoth,
|
|
135
|
+
scope: "account",
|
|
136
|
+
methods: ["oauth"],
|
|
137
|
+
adapter: "xai-supergrok"
|
|
138
|
+
},
|
|
139
|
+
"gemini-cli": blocked({
|
|
140
|
+
label: "Gemini CLI / Code Assist",
|
|
141
|
+
integrationID: "google-gemini-cli",
|
|
142
|
+
audiences: ["us_business"],
|
|
143
|
+
scope: "team",
|
|
144
|
+
methods: ["oauth"]
|
|
145
|
+
}, "eligibility_unverified"),
|
|
146
|
+
"alibaba-coding-plan": blocked({
|
|
147
|
+
label: "Alibaba Coding Plan",
|
|
148
|
+
integrationID: "alibaba-coding-plan",
|
|
149
|
+
audiences: chinaPersonal,
|
|
150
|
+
scope: "account",
|
|
151
|
+
methods: ["key"]
|
|
152
|
+
}, "runtime_accounting_unavailable"),
|
|
153
|
+
"alibaba-token-plan": {
|
|
154
|
+
label: "Alibaba Token Plan",
|
|
155
|
+
integrationID: "alibaba-token-plan",
|
|
156
|
+
audiences: chinaPersonal,
|
|
157
|
+
scope: "account",
|
|
158
|
+
methods: ["cli"],
|
|
159
|
+
source: "local-cli",
|
|
160
|
+
adapter: "alibaba-token-plan-cli"
|
|
161
|
+
},
|
|
162
|
+
deepseek: {
|
|
163
|
+
label: "DeepSeek",
|
|
164
|
+
integrationID: "deepseek",
|
|
165
|
+
audiences: chinaPersonal,
|
|
166
|
+
scope: "account",
|
|
167
|
+
methods: ["key"],
|
|
168
|
+
adapter: "deepseek"
|
|
169
|
+
},
|
|
170
|
+
"kimi-code-global": {
|
|
171
|
+
label: "Kimi Code (Global)",
|
|
172
|
+
integrationID: "kimi-code-global",
|
|
173
|
+
audiences: chinaBoth,
|
|
174
|
+
scope: "account",
|
|
175
|
+
methods: ["key"],
|
|
176
|
+
adapter: "kimi-global"
|
|
177
|
+
},
|
|
178
|
+
"kimi-code-cn": {
|
|
179
|
+
label: "Kimi Code (China)",
|
|
180
|
+
integrationID: "kimi-code-cn",
|
|
181
|
+
audiences: chinaBoth,
|
|
182
|
+
scope: "account",
|
|
183
|
+
methods: ["key"],
|
|
184
|
+
adapter: "kimi-cn"
|
|
185
|
+
},
|
|
186
|
+
"minimax-plan-global": {
|
|
187
|
+
label: "MiniMax Token Plan (Global)",
|
|
188
|
+
integrationID: "minimax-coding-plan",
|
|
189
|
+
audiences: chinaBoth,
|
|
190
|
+
scope: "account",
|
|
191
|
+
methods: ["key"],
|
|
192
|
+
adapter: "minimax-global"
|
|
193
|
+
},
|
|
194
|
+
"minimax-plan-cn": {
|
|
195
|
+
label: "MiniMax Token Plan (China)",
|
|
196
|
+
integrationID: "minimax-china-coding-plan",
|
|
197
|
+
audiences: chinaBoth,
|
|
198
|
+
scope: "account",
|
|
199
|
+
methods: ["key"],
|
|
200
|
+
adapter: "minimax-cn"
|
|
201
|
+
},
|
|
202
|
+
"xiaomi-mimo": blocked({
|
|
203
|
+
label: "Xiaomi MiMo",
|
|
204
|
+
integrationID: "xiaomi-mimo",
|
|
205
|
+
audiences: chinaPersonal,
|
|
206
|
+
scope: "account",
|
|
207
|
+
methods: ["key"]
|
|
208
|
+
}, "unsupported"),
|
|
209
|
+
"zai-coding-plan": {
|
|
210
|
+
label: "Z.ai Coding Plan",
|
|
211
|
+
integrationID: "zai-coding-plan",
|
|
212
|
+
audiences: chinaPersonal,
|
|
213
|
+
scope: "account",
|
|
214
|
+
methods: ["key"],
|
|
215
|
+
adapter: "zai"
|
|
216
|
+
},
|
|
217
|
+
"zhipu-coding-plan": {
|
|
218
|
+
label: "Zhipu Coding Plan",
|
|
219
|
+
integrationID: "zhipu-coding-plan",
|
|
220
|
+
audiences: chinaBoth,
|
|
221
|
+
scope: "account",
|
|
222
|
+
methods: ["key"],
|
|
223
|
+
adapter: "zhipu"
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
const MAX_RESPONSE_BYTES = 65536;
|
|
227
|
+
const REQUEST_TIMEOUT_MS = 5e3;
|
|
228
|
+
const MAX_AMOUNT = 0xe8d4a51000;
|
|
229
|
+
var SafeAdapterError = class extends Error {
|
|
230
|
+
reason;
|
|
231
|
+
retryAfterMs;
|
|
232
|
+
httpStatus;
|
|
233
|
+
constructor(reason, retryAfterMs, httpStatus) {
|
|
234
|
+
super(reason);
|
|
235
|
+
this.reason = reason;
|
|
236
|
+
this.retryAfterMs = retryAfterMs;
|
|
237
|
+
this.httpStatus = httpStatus;
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
function isRecord(value) {
|
|
241
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
242
|
+
}
|
|
243
|
+
function available(metrics) {
|
|
244
|
+
if (metrics.length === 0 || metrics.length > 16) throw new SafeAdapterError("provider_error");
|
|
245
|
+
return {
|
|
246
|
+
state: "available",
|
|
247
|
+
metrics
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
function finiteAmount(value) {
|
|
251
|
+
const amount = typeof value === "string" && /^\d+(?:\.\d+)?$/.test(value) ? Number(value) : value;
|
|
252
|
+
return typeof amount === "number" && Number.isFinite(amount) && amount >= 0 && amount <= MAX_AMOUNT ? amount : void 0;
|
|
253
|
+
}
|
|
254
|
+
function retryAfterMs(value, now = Date.now()) {
|
|
255
|
+
if (!value) return void 0;
|
|
256
|
+
const seconds = Number(value);
|
|
257
|
+
const retryAt = Number.isFinite(seconds) && seconds >= 0 ? now + seconds * 1e3 : Date.parse(value);
|
|
258
|
+
if (!Number.isFinite(retryAt)) return void 0;
|
|
259
|
+
return Math.ceil(Math.min(36e5, Math.max(0, retryAt - now)));
|
|
260
|
+
}
|
|
261
|
+
async function readJson(response) {
|
|
262
|
+
const announced = Number(response.headers.get("content-length"));
|
|
263
|
+
if (Number.isFinite(announced) && announced > MAX_RESPONSE_BYTES) {
|
|
264
|
+
await response.body?.cancel();
|
|
265
|
+
throw new SafeAdapterError("invalid_response");
|
|
266
|
+
}
|
|
267
|
+
if (!response.body) throw new SafeAdapterError("invalid_response");
|
|
268
|
+
const reader = response.body.getReader();
|
|
269
|
+
const chunks = [];
|
|
270
|
+
let size = 0;
|
|
271
|
+
try {
|
|
272
|
+
while (true) {
|
|
273
|
+
const { done, value } = await reader.read();
|
|
274
|
+
if (done) break;
|
|
275
|
+
size += value.byteLength;
|
|
276
|
+
if (size > MAX_RESPONSE_BYTES) {
|
|
277
|
+
await reader.cancel();
|
|
278
|
+
throw new SafeAdapterError("invalid_response");
|
|
279
|
+
}
|
|
280
|
+
chunks.push(value);
|
|
281
|
+
}
|
|
282
|
+
} finally {
|
|
283
|
+
reader.releaseLock();
|
|
284
|
+
}
|
|
285
|
+
const bytes = new Uint8Array(size);
|
|
286
|
+
let offset = 0;
|
|
287
|
+
for (const chunk of chunks) {
|
|
288
|
+
bytes.set(chunk, offset);
|
|
289
|
+
offset += chunk.byteLength;
|
|
290
|
+
}
|
|
291
|
+
try {
|
|
292
|
+
return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
293
|
+
} catch {
|
|
294
|
+
throw new SafeAdapterError("invalid_response");
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
function transportFailure(error, timeoutSignal, shutdownSignal) {
|
|
298
|
+
if (error instanceof SafeAdapterError) return error;
|
|
299
|
+
if (shutdownSignal.aborted) return new SafeAdapterError("provider_error");
|
|
300
|
+
const errorName = typeof error === "object" && error !== null && "name" in error ? error.name : void 0;
|
|
301
|
+
return new SafeAdapterError(timeoutSignal.aborted || errorName === "TimeoutError" ? "timeout" : "network_error");
|
|
302
|
+
}
|
|
303
|
+
async function requestJson(url, secret, shutdownSignal, authStyle = "bearer", options = {}) {
|
|
304
|
+
const timeoutSignal = AbortSignal.timeout(REQUEST_TIMEOUT_MS);
|
|
305
|
+
const signal = AbortSignal.any([shutdownSignal, timeoutSignal]);
|
|
306
|
+
let response;
|
|
307
|
+
try {
|
|
308
|
+
response = await fetch(url, {
|
|
309
|
+
method: options.method ?? "GET",
|
|
310
|
+
headers: {
|
|
311
|
+
accept: "application/json",
|
|
312
|
+
authorization: authStyle === "bearer" ? `Bearer ${secret}` : secret,
|
|
313
|
+
...options.headers
|
|
314
|
+
},
|
|
315
|
+
...options.body === void 0 ? {} : { body: options.body },
|
|
316
|
+
redirect: "error",
|
|
317
|
+
signal
|
|
318
|
+
});
|
|
319
|
+
} catch (error) {
|
|
320
|
+
throw transportFailure(error, timeoutSignal, shutdownSignal);
|
|
321
|
+
}
|
|
322
|
+
if (response.status === 401) {
|
|
323
|
+
await response.body?.cancel();
|
|
324
|
+
throw new SafeAdapterError("unauthorized");
|
|
325
|
+
}
|
|
326
|
+
if (response.status === 403) {
|
|
327
|
+
await response.body?.cancel();
|
|
328
|
+
throw new SafeAdapterError("forbidden");
|
|
329
|
+
}
|
|
330
|
+
if (response.status === 429) {
|
|
331
|
+
await response.body?.cancel();
|
|
332
|
+
throw new SafeAdapterError("rate_limited", retryAfterMs(response.headers.get("retry-after")) ?? 3e4);
|
|
333
|
+
}
|
|
334
|
+
if (response.status === 404) {
|
|
335
|
+
await response.body?.cancel();
|
|
336
|
+
throw new SafeAdapterError("provider_error", void 0, 404);
|
|
337
|
+
}
|
|
338
|
+
if (!response.ok) {
|
|
339
|
+
await response.body?.cancel();
|
|
340
|
+
throw new SafeAdapterError("provider_error");
|
|
341
|
+
}
|
|
342
|
+
try {
|
|
343
|
+
return await readJson(response);
|
|
344
|
+
} catch (error) {
|
|
345
|
+
throw transportFailure(error, timeoutSignal, shutdownSignal);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
function deepseekMetrics(payload) {
|
|
349
|
+
if (!isRecord(payload) || typeof payload.is_available !== "boolean" || !Array.isArray(payload.balance_infos)) throw new SafeAdapterError("provider_error");
|
|
350
|
+
if (payload.balance_infos.length > 8) throw new SafeAdapterError("provider_error");
|
|
351
|
+
const metrics = [{
|
|
352
|
+
kind: "provider_status",
|
|
353
|
+
status: payload.is_available ? "available" : "unavailable",
|
|
354
|
+
label: "Service status"
|
|
355
|
+
}];
|
|
356
|
+
for (const value of payload.balance_infos) {
|
|
357
|
+
if (!isRecord(value) || value.currency !== "USD" && value.currency !== "CNY") throw new SafeAdapterError("provider_error");
|
|
358
|
+
const amount = finiteAmount(value.total_balance);
|
|
359
|
+
if (amount === void 0) throw new SafeAdapterError("provider_error");
|
|
360
|
+
metrics.push({
|
|
361
|
+
kind: "balance",
|
|
362
|
+
amount,
|
|
363
|
+
currency: value.currency,
|
|
364
|
+
label: "Total balance"
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
return metrics;
|
|
368
|
+
}
|
|
369
|
+
function ollamaMetrics(payload) {
|
|
370
|
+
if (!isRecord(payload) || !isRecord(payload.included) || !isRecord(payload.purchased)) throw new SafeAdapterError("provider_error");
|
|
371
|
+
const metrics = [];
|
|
372
|
+
const included = finiteAmount(payload.included.balance_usd);
|
|
373
|
+
const purchased = finiteAmount(payload.purchased.balance_usd);
|
|
374
|
+
if (included !== void 0) metrics.push({
|
|
375
|
+
kind: "balance",
|
|
376
|
+
amount: included,
|
|
377
|
+
currency: "USD",
|
|
378
|
+
label: "Included balance"
|
|
379
|
+
});
|
|
380
|
+
if (purchased !== void 0) metrics.push({
|
|
381
|
+
kind: "balance",
|
|
382
|
+
amount: purchased,
|
|
383
|
+
currency: "USD",
|
|
384
|
+
label: "Purchased balance"
|
|
385
|
+
});
|
|
386
|
+
for (const window of ["session", "weekly"]) {
|
|
387
|
+
const item = payload.included[window];
|
|
388
|
+
if (!isRecord(item)) continue;
|
|
389
|
+
const remainingPercent = finiteAmount(item.remaining_percent);
|
|
390
|
+
if (remainingPercent === void 0 || remainingPercent > 100) throw new SafeAdapterError("provider_error");
|
|
391
|
+
const resetsAt = isoResetAt(item.resets_at);
|
|
392
|
+
metrics.push({
|
|
393
|
+
kind: "provider_quota",
|
|
394
|
+
used: 100 - remainingPercent,
|
|
395
|
+
remaining: remainingPercent,
|
|
396
|
+
unit: "%",
|
|
397
|
+
label: window === "session" ? "Session quota" : "Weekly quota",
|
|
398
|
+
period: window,
|
|
399
|
+
...resetsAt === void 0 ? {} : { resetsAt }
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
if (metrics.length === 0) throw new SafeAdapterError("provider_error");
|
|
403
|
+
return metrics;
|
|
404
|
+
}
|
|
405
|
+
function openRouterMetrics(payload) {
|
|
406
|
+
if (!isRecord(payload) || !isRecord(payload.data)) throw new SafeAdapterError("provider_error");
|
|
407
|
+
const data = payload.data;
|
|
408
|
+
const usage = finiteAmount(data.usage);
|
|
409
|
+
if (usage === void 0) throw new SafeAdapterError("provider_error");
|
|
410
|
+
const limit = data.limit === null || data.limit === void 0 ? void 0 : finiteAmount(data.limit);
|
|
411
|
+
const remaining = data.limit_remaining === null || data.limit_remaining === void 0 ? void 0 : finiteAmount(data.limit_remaining);
|
|
412
|
+
if (data.limit !== null && data.limit !== void 0 && limit === void 0) throw new SafeAdapterError("provider_error");
|
|
413
|
+
if (data.limit_remaining !== null && data.limit_remaining !== void 0 && remaining === void 0) throw new SafeAdapterError("provider_error");
|
|
414
|
+
if (limit !== void 0 && remaining !== void 0) {
|
|
415
|
+
if (remaining > limit || usage > limit + .01) throw new SafeAdapterError("provider_error");
|
|
416
|
+
return [{
|
|
417
|
+
kind: "provider_quota",
|
|
418
|
+
used: usage,
|
|
419
|
+
remaining,
|
|
420
|
+
unit: "USD",
|
|
421
|
+
label: "Selected key budget"
|
|
422
|
+
}];
|
|
423
|
+
}
|
|
424
|
+
return [{
|
|
425
|
+
kind: "key_usage",
|
|
426
|
+
amount: usage,
|
|
427
|
+
currency: "USD",
|
|
428
|
+
label: "Selected key spend",
|
|
429
|
+
period: "reported"
|
|
430
|
+
}];
|
|
431
|
+
}
|
|
432
|
+
function chutesMetrics(payload) {
|
|
433
|
+
if (!isRecord(payload)) throw new SafeAdapterError("provider_error");
|
|
434
|
+
const quota = finiteAmount(payload.quota);
|
|
435
|
+
const used = finiteAmount(payload.used);
|
|
436
|
+
if (quota === void 0 || used === void 0) throw new SafeAdapterError("provider_error");
|
|
437
|
+
return [{
|
|
438
|
+
kind: "provider_quota",
|
|
439
|
+
used,
|
|
440
|
+
remaining: Math.max(0, quota - used),
|
|
441
|
+
unit: "units",
|
|
442
|
+
label: "Daily quota"
|
|
443
|
+
}];
|
|
444
|
+
}
|
|
445
|
+
function kimiMetrics(payload) {
|
|
446
|
+
if (!isRecord(payload)) throw new SafeAdapterError("provider_error");
|
|
447
|
+
const data = isRecord(payload.data) ? payload.data : payload;
|
|
448
|
+
const rows = [];
|
|
449
|
+
if (isRecord(data.usage)) rows.push(data.usage);
|
|
450
|
+
if (Array.isArray(data.limits)) {
|
|
451
|
+
if (data.limits.length > 12) throw new SafeAdapterError("provider_error");
|
|
452
|
+
for (const item of data.limits) {
|
|
453
|
+
if (!isRecord(item)) continue;
|
|
454
|
+
rows.push(isRecord(item.detail) ? item.detail : item);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
const metrics = [];
|
|
458
|
+
for (const row of rows) {
|
|
459
|
+
const limit = finiteAmount(row.limit);
|
|
460
|
+
let used = finiteAmount(row.used);
|
|
461
|
+
const remaining = finiteAmount(row.remaining);
|
|
462
|
+
if (used === void 0 && limit !== void 0 && remaining !== void 0 && remaining <= limit) used = limit - remaining;
|
|
463
|
+
if (limit === void 0 || used === void 0 || limit <= 0) continue;
|
|
464
|
+
metrics.push({
|
|
465
|
+
kind: "provider_quota",
|
|
466
|
+
used,
|
|
467
|
+
remaining: Math.max(0, limit - used),
|
|
468
|
+
unit: "units",
|
|
469
|
+
label: "Provider-reported window"
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
if (metrics.length === 0) throw new SafeAdapterError("provider_error");
|
|
473
|
+
return metrics;
|
|
474
|
+
}
|
|
475
|
+
function minimaxMetrics(payload, endpoint) {
|
|
476
|
+
if (!isRecord(payload) || !isRecord(payload.base_resp) || payload.base_resp.status_code !== 0 || !Array.isArray(payload.model_remains) || payload.model_remains.length > 64) throw new SafeAdapterError("provider_error");
|
|
477
|
+
const candidates = payload.model_remains.filter((row) => isRecord(row) && typeof row.model_name === "string" && (/^minimax-m/i.test(row.model_name.trim()) || row.model_name.trim().toLowerCase() === "general"));
|
|
478
|
+
candidates.sort((left, right) => {
|
|
479
|
+
return (left.model_name === "minimax-m*" ? 0 : 1) - (right.model_name === "minimax-m*" ? 0 : 1);
|
|
480
|
+
});
|
|
481
|
+
const model = candidates[0];
|
|
482
|
+
if (!model) throw new SafeAdapterError("provider_error");
|
|
483
|
+
const specs = [{
|
|
484
|
+
total: model.current_interval_total_count,
|
|
485
|
+
count: model.current_interval_usage_count,
|
|
486
|
+
percent: model.current_interval_remaining_percent,
|
|
487
|
+
label: "5-hour token-plan quota"
|
|
488
|
+
}, {
|
|
489
|
+
total: model.current_weekly_total_count,
|
|
490
|
+
count: model.current_weekly_usage_count,
|
|
491
|
+
percent: model.current_weekly_remaining_percent,
|
|
492
|
+
label: "Weekly token-plan quota"
|
|
493
|
+
}];
|
|
494
|
+
const metrics = [];
|
|
495
|
+
for (const spec of specs) {
|
|
496
|
+
const total = finiteAmount(spec.total);
|
|
497
|
+
const count = finiteAmount(spec.count);
|
|
498
|
+
const percent = finiteAmount(spec.percent);
|
|
499
|
+
if (total !== void 0 && total > 0 && count !== void 0) {
|
|
500
|
+
const used = endpoint === "cn" ? count : Math.max(0, total - Math.min(total, count));
|
|
501
|
+
const remaining = endpoint === "cn" ? Math.max(0, total - count) : Math.min(total, count);
|
|
502
|
+
metrics.push({
|
|
503
|
+
kind: "provider_quota",
|
|
504
|
+
used,
|
|
505
|
+
remaining,
|
|
506
|
+
unit: "requests",
|
|
507
|
+
label: spec.label
|
|
508
|
+
});
|
|
509
|
+
} else if (percent !== void 0 && percent <= 100) metrics.push({
|
|
510
|
+
kind: "provider_quota",
|
|
511
|
+
used: 100 - percent,
|
|
512
|
+
remaining: percent,
|
|
513
|
+
unit: "%",
|
|
514
|
+
label: spec.label
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
if (metrics.length === 0) throw new SafeAdapterError("provider_error");
|
|
518
|
+
return metrics;
|
|
519
|
+
}
|
|
520
|
+
function glmMetrics(payload, provider) {
|
|
521
|
+
if (!isRecord(payload)) throw new SafeAdapterError("provider_error");
|
|
522
|
+
const envelope = isRecord(payload.data) ? payload.data : provider === "zai" ? payload : void 0;
|
|
523
|
+
if (!envelope || !Array.isArray(envelope.limits) || envelope.limits.length > 16) throw new SafeAdapterError("provider_error");
|
|
524
|
+
const metrics = [];
|
|
525
|
+
for (const limit of envelope.limits) {
|
|
526
|
+
if (!isRecord(limit) || limit.type !== "TOKENS_LIMIT" && limit.type !== "CREDIT_LIMIT") continue;
|
|
527
|
+
const percentage = finiteAmount(limit.percentage);
|
|
528
|
+
if (percentage === void 0 || percentage > 100) continue;
|
|
529
|
+
const label = limit.unit === 3 ? "5-hour coding-plan quota" : limit.unit === 6 ? "Weekly coding-plan quota" : "Coding-plan quota";
|
|
530
|
+
const resetsAt = epochResetAt(limit.nextResetTime, "milliseconds");
|
|
531
|
+
metrics.push({
|
|
532
|
+
kind: "provider_quota",
|
|
533
|
+
used: percentage,
|
|
534
|
+
remaining: 100 - percentage,
|
|
535
|
+
unit: "%",
|
|
536
|
+
label,
|
|
537
|
+
...resetsAt === void 0 ? {} : { resetsAt }
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
if (metrics.length === 0) throw new SafeAdapterError("provider_error");
|
|
541
|
+
return metrics;
|
|
542
|
+
}
|
|
543
|
+
function kiloPassMetrics(payload) {
|
|
544
|
+
const item = Array.isArray(payload) ? payload[0] : payload;
|
|
545
|
+
if (!isRecord(item) || !isRecord(item.result) || !isRecord(item.result.data)) throw new SafeAdapterError("provider_error");
|
|
546
|
+
const data = item.result.data;
|
|
547
|
+
if ("json" in data && !isRecord(data.json)) throw new SafeAdapterError("provider_error");
|
|
548
|
+
const root = isRecord(data.json) ? data.json : data;
|
|
549
|
+
if (!("subscription" in root) || root.subscription === null) return null;
|
|
550
|
+
if (!isRecord(root.subscription)) throw new SafeAdapterError("provider_error");
|
|
551
|
+
const subscription = root.subscription;
|
|
552
|
+
const base = finiteAmount(subscription.currentPeriodBaseCreditsUsd);
|
|
553
|
+
const usage = finiteAmount(subscription.currentPeriodUsageUsd);
|
|
554
|
+
const bonus = subscription.currentPeriodBonusCreditsUsd === void 0 || subscription.currentPeriodBonusCreditsUsd === null ? 0 : finiteAmount(subscription.currentPeriodBonusCreditsUsd);
|
|
555
|
+
if (base === void 0 || usage === void 0 || bonus === void 0) throw new SafeAdapterError("provider_error");
|
|
556
|
+
const total = base + bonus;
|
|
557
|
+
if (total > MAX_AMOUNT) throw new SafeAdapterError("provider_error");
|
|
558
|
+
return [{
|
|
559
|
+
kind: "provider_quota",
|
|
560
|
+
used: usage,
|
|
561
|
+
remaining: Math.max(0, total - usage),
|
|
562
|
+
unit: "USD",
|
|
563
|
+
label: "Kilo Pass credits"
|
|
564
|
+
}];
|
|
565
|
+
}
|
|
566
|
+
function kiloBalanceMetrics(payload) {
|
|
567
|
+
if (!isRecord(payload)) throw new SafeAdapterError("provider_error");
|
|
568
|
+
const balance = finiteAmount(payload.balance);
|
|
569
|
+
if (balance === void 0) throw new SafeAdapterError("provider_error");
|
|
570
|
+
return [{
|
|
571
|
+
kind: "balance",
|
|
572
|
+
amount: balance,
|
|
573
|
+
currency: "USD",
|
|
574
|
+
label: "Gateway balance"
|
|
575
|
+
}];
|
|
576
|
+
}
|
|
577
|
+
async function nanoGptMetrics(secret, shutdownSignal) {
|
|
578
|
+
const usagePayload = await requestJson("https://nano-gpt.com/api/subscription/v1/usage", secret, shutdownSignal);
|
|
579
|
+
const balancePayload = await requestJson("https://nano-gpt.com/api/check-balance", secret, shutdownSignal);
|
|
580
|
+
if (!isRecord(usagePayload) || !isRecord(balancePayload)) throw new SafeAdapterError("provider_error");
|
|
581
|
+
const metrics = [];
|
|
582
|
+
if (typeof usagePayload.active === "boolean") metrics.push({
|
|
583
|
+
kind: "provider_status",
|
|
584
|
+
status: usagePayload.active ? "active" : "inactive",
|
|
585
|
+
label: "Subscription status"
|
|
586
|
+
});
|
|
587
|
+
const limits = isRecord(usagePayload.limits) ? usagePayload.limits : {};
|
|
588
|
+
for (const window of ["daily", "monthly"]) {
|
|
589
|
+
const details = isRecord(usagePayload[window]) ? usagePayload[window] : void 0;
|
|
590
|
+
const limit = finiteAmount(limits[window]);
|
|
591
|
+
if (!details || limit === void 0 || limit <= 0) continue;
|
|
592
|
+
let used = finiteAmount(details.used);
|
|
593
|
+
let remaining = finiteAmount(details.remaining);
|
|
594
|
+
const percentUsed = finiteAmount(details.percentUsed);
|
|
595
|
+
if (used === void 0 && remaining !== void 0 && remaining <= limit) used = limit - remaining;
|
|
596
|
+
if (used === void 0 && percentUsed !== void 0 && percentUsed <= 1) used = limit * percentUsed;
|
|
597
|
+
if (remaining === void 0 && used !== void 0 && used <= limit) remaining = limit - used;
|
|
598
|
+
if (used === void 0 || remaining === void 0 || used > limit || remaining > limit) throw new SafeAdapterError("provider_error");
|
|
599
|
+
metrics.push({
|
|
600
|
+
kind: "provider_quota",
|
|
601
|
+
used,
|
|
602
|
+
remaining,
|
|
603
|
+
unit: "units",
|
|
604
|
+
label: window === "daily" ? "Daily subscription limit" : "Monthly subscription limit",
|
|
605
|
+
period: window
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
const usdBalance = finiteAmount(balancePayload.usd_balance);
|
|
609
|
+
if (usdBalance !== void 0) metrics.push({
|
|
610
|
+
kind: "balance",
|
|
611
|
+
amount: usdBalance,
|
|
612
|
+
currency: "USD",
|
|
613
|
+
label: "Account balance"
|
|
614
|
+
});
|
|
615
|
+
if (metrics.length === 0) throw new SafeAdapterError("provider_error");
|
|
616
|
+
return metrics;
|
|
617
|
+
}
|
|
618
|
+
function syntheticMetrics(payload) {
|
|
619
|
+
if (!isRecord(payload) || !isRecord(payload.rollingFiveHourLimit) || !isRecord(payload.weeklyTokenLimit)) throw new SafeAdapterError("provider_error");
|
|
620
|
+
const fiveHourLimit = finiteAmount(payload.rollingFiveHourLimit.max);
|
|
621
|
+
const fiveHourRemaining = finiteAmount(payload.rollingFiveHourLimit.remaining);
|
|
622
|
+
if (fiveHourLimit === void 0 || fiveHourLimit <= 0 || fiveHourRemaining === void 0 || fiveHourRemaining > fiveHourLimit) throw new SafeAdapterError("provider_error");
|
|
623
|
+
const creditAmount = (value) => {
|
|
624
|
+
if (typeof value !== "string") return void 0;
|
|
625
|
+
const match = /^\$(\d+)(?:\.(\d{1,2}))?$/.exec(value.trim());
|
|
626
|
+
if (!match) return void 0;
|
|
627
|
+
return finiteAmount(`${match[1]}.${(match[2] ?? "").padEnd(2, "0")}`);
|
|
628
|
+
};
|
|
629
|
+
const weeklyLimit = creditAmount(payload.weeklyTokenLimit.maxCredits);
|
|
630
|
+
const weeklyRemaining = creditAmount(payload.weeklyTokenLimit.remainingCredits);
|
|
631
|
+
if (weeklyLimit === void 0 || weeklyLimit <= 0 || weeklyRemaining === void 0 || weeklyRemaining > weeklyLimit) throw new SafeAdapterError("provider_error");
|
|
632
|
+
return [{
|
|
633
|
+
kind: "provider_quota",
|
|
634
|
+
used: fiveHourLimit - fiveHourRemaining,
|
|
635
|
+
remaining: fiveHourRemaining,
|
|
636
|
+
unit: "units",
|
|
637
|
+
label: "Rolling five-hour limit"
|
|
638
|
+
}, {
|
|
639
|
+
kind: "provider_quota",
|
|
640
|
+
used: weeklyLimit - weeklyRemaining,
|
|
641
|
+
remaining: weeklyRemaining,
|
|
642
|
+
unit: "USD",
|
|
643
|
+
label: "Weekly credit limit"
|
|
644
|
+
}];
|
|
645
|
+
}
|
|
646
|
+
function percentage(value) {
|
|
647
|
+
const parsed = finiteAmount(value);
|
|
648
|
+
return parsed !== void 0 && parsed <= 100 ? parsed : void 0;
|
|
649
|
+
}
|
|
650
|
+
function isoResetAt(value) {
|
|
651
|
+
return typeof value === "string" && isValidResetDate(value) ? Date.parse(value) : void 0;
|
|
652
|
+
}
|
|
653
|
+
function epochResetAt(value, unit) {
|
|
654
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) return void 0;
|
|
655
|
+
const timestamp = unit === "seconds" ? value * 1e3 : value;
|
|
656
|
+
return Number.isFinite(new Date(timestamp).getTime()) ? timestamp : void 0;
|
|
657
|
+
}
|
|
658
|
+
function anthropicMetrics(payload) {
|
|
659
|
+
if (!isRecord(payload)) throw new SafeAdapterError("provider_error");
|
|
660
|
+
const metrics = [];
|
|
661
|
+
for (const window of [{
|
|
662
|
+
key: "five_hour",
|
|
663
|
+
label: "5-hour quota",
|
|
664
|
+
period: "5h"
|
|
665
|
+
}, {
|
|
666
|
+
key: "seven_day",
|
|
667
|
+
label: "Weekly quota",
|
|
668
|
+
period: "week"
|
|
669
|
+
}]) {
|
|
670
|
+
const value = payload[window.key];
|
|
671
|
+
if (value === void 0 || value === null) continue;
|
|
672
|
+
if (!isRecord(value)) throw new SafeAdapterError("provider_error");
|
|
673
|
+
const used = percentage(value.utilization ?? value.used_percentage ?? value.usedPercentage ?? value.used_percent ?? value.usedPercent ?? value.percent_used ?? value.percentUsed);
|
|
674
|
+
if (used === void 0) throw new SafeAdapterError("provider_error");
|
|
675
|
+
const resetsAt = isoResetAt(value.resets_at);
|
|
676
|
+
metrics.push({
|
|
677
|
+
kind: "provider_quota",
|
|
678
|
+
used,
|
|
679
|
+
remaining: 100 - used,
|
|
680
|
+
unit: "%",
|
|
681
|
+
label: window.label,
|
|
682
|
+
period: window.period,
|
|
683
|
+
...resetsAt === void 0 ? {} : { resetsAt }
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
const extra = payload.extra_usage;
|
|
687
|
+
if (isRecord(extra) && extra.is_enabled === true) {
|
|
688
|
+
const used = percentage(extra.utilization);
|
|
689
|
+
if (used === void 0) throw new SafeAdapterError("provider_error");
|
|
690
|
+
metrics.push({
|
|
691
|
+
kind: "provider_quota",
|
|
692
|
+
used,
|
|
693
|
+
remaining: 100 - used,
|
|
694
|
+
unit: "%",
|
|
695
|
+
label: "Extra usage limit",
|
|
696
|
+
period: "reported"
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
return metrics;
|
|
700
|
+
}
|
|
701
|
+
function copilotMetrics(payload) {
|
|
702
|
+
if (!isRecord(payload)) throw new SafeAdapterError("provider_error");
|
|
703
|
+
const snapshots = isRecord(payload.quota_snapshots) ? payload.quota_snapshots : void 0;
|
|
704
|
+
const quota = snapshots && isRecord(snapshots.premium_interactions) ? snapshots.premium_interactions : void 0;
|
|
705
|
+
if (!quota) throw new SafeAdapterError("provider_error");
|
|
706
|
+
if (quota.unlimited === true) return [{
|
|
707
|
+
kind: "provider_status",
|
|
708
|
+
status: "active",
|
|
709
|
+
label: "Unlimited Copilot plan"
|
|
710
|
+
}];
|
|
711
|
+
const total = finiteAmount(quota.entitlement);
|
|
712
|
+
const remaining = finiteAmount(quota.quota_remaining ?? quota.remaining);
|
|
713
|
+
const resetsAt = isoResetAt(payload.quota_reset_date_utc);
|
|
714
|
+
if (total !== void 0 && total > 0 && remaining !== void 0 && remaining <= total) return [{
|
|
715
|
+
kind: "provider_quota",
|
|
716
|
+
used: total - remaining,
|
|
717
|
+
remaining,
|
|
718
|
+
unit: "premium interactions",
|
|
719
|
+
label: "Copilot user quota",
|
|
720
|
+
period: "month",
|
|
721
|
+
...resetsAt === void 0 ? {} : { resetsAt }
|
|
722
|
+
}];
|
|
723
|
+
if (quota.percent_remaining !== void 0) {
|
|
724
|
+
const percentRemaining = percentage(quota.percent_remaining);
|
|
725
|
+
if (percentRemaining !== void 0) return [{
|
|
726
|
+
kind: "provider_quota",
|
|
727
|
+
used: 100 - percentRemaining,
|
|
728
|
+
remaining: percentRemaining,
|
|
729
|
+
unit: "%",
|
|
730
|
+
label: "Copilot user quota",
|
|
731
|
+
period: "month",
|
|
732
|
+
...resetsAt === void 0 ? {} : { resetsAt }
|
|
733
|
+
}];
|
|
734
|
+
}
|
|
735
|
+
throw new SafeAdapterError("provider_error");
|
|
736
|
+
}
|
|
737
|
+
function googleAgyMetrics(payload) {
|
|
738
|
+
if (!isRecord(payload)) throw new SafeAdapterError("provider_error");
|
|
739
|
+
const rows = [];
|
|
740
|
+
if (Array.isArray(payload.buckets)) {
|
|
741
|
+
if (payload.buckets.length > 16) throw new SafeAdapterError("provider_error");
|
|
742
|
+
for (const bucket of payload.buckets) if (isRecord(bucket)) rows.push(bucket);
|
|
743
|
+
}
|
|
744
|
+
if (Array.isArray(payload.groups)) {
|
|
745
|
+
if (payload.groups.length > 16) throw new SafeAdapterError("provider_error");
|
|
746
|
+
for (const group of payload.groups) {
|
|
747
|
+
if (!isRecord(group) || !Array.isArray(group.buckets)) continue;
|
|
748
|
+
if (group.buckets.length > 16) throw new SafeAdapterError("provider_error");
|
|
749
|
+
for (const bucket of group.buckets) if (isRecord(bucket)) rows.push(bucket);
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
if (rows.length > 16) throw new SafeAdapterError("provider_error");
|
|
753
|
+
const metrics = [];
|
|
754
|
+
for (const bucket of rows) {
|
|
755
|
+
const remainingFraction = bucket.remainingFraction;
|
|
756
|
+
if (typeof remainingFraction !== "number" || !Number.isFinite(remainingFraction) || remainingFraction < 0 || remainingFraction > 1) continue;
|
|
757
|
+
const label = `AGY quota ${metrics.length + 1}`;
|
|
758
|
+
const period = typeof bucket.window === "string" && [
|
|
759
|
+
"weekly",
|
|
760
|
+
"five_hour",
|
|
761
|
+
"monthly",
|
|
762
|
+
"daily"
|
|
763
|
+
].includes(bucket.window.trim()) ? bucket.window.trim() : void 0;
|
|
764
|
+
metrics.push({
|
|
765
|
+
kind: "provider_quota",
|
|
766
|
+
used: 100 - Math.round(remainingFraction * 100),
|
|
767
|
+
remaining: Math.round(remainingFraction * 100),
|
|
768
|
+
unit: "%",
|
|
769
|
+
label,
|
|
770
|
+
...period ? { period } : {}
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
if (metrics.length === 0) throw new SafeAdapterError("provider_error");
|
|
774
|
+
return metrics;
|
|
775
|
+
}
|
|
776
|
+
function openCodeGoMetrics(payload) {
|
|
777
|
+
if (!isRecord(payload) || !isRecord(payload.usage)) throw new SafeAdapterError("invalid_response");
|
|
778
|
+
const windows = [
|
|
779
|
+
{
|
|
780
|
+
key: "rolling",
|
|
781
|
+
label: "5-hour quota"
|
|
782
|
+
},
|
|
783
|
+
{
|
|
784
|
+
key: "weekly",
|
|
785
|
+
label: "Weekly quota"
|
|
786
|
+
},
|
|
787
|
+
{
|
|
788
|
+
key: "monthly",
|
|
789
|
+
label: "Monthly quota"
|
|
790
|
+
}
|
|
791
|
+
];
|
|
792
|
+
const metrics = [];
|
|
793
|
+
for (const window of windows) {
|
|
794
|
+
const value = payload.usage[window.key];
|
|
795
|
+
if (!isRecord(value) || value.status !== "ok" && value.status !== "rate-limited") throw new SafeAdapterError("invalid_response");
|
|
796
|
+
const used = percentage(value.percent);
|
|
797
|
+
if (used === void 0) throw new SafeAdapterError("invalid_response");
|
|
798
|
+
const resetValue = value.resetsAt;
|
|
799
|
+
const resetsAt = typeof resetValue === "string" && isValidResetDate(resetValue) ? Date.parse(resetValue) : void 0;
|
|
800
|
+
const normalizedUsed = value.status === "rate-limited" ? 100 : used;
|
|
801
|
+
metrics.push({
|
|
802
|
+
kind: "provider_quota",
|
|
803
|
+
used: normalizedUsed,
|
|
804
|
+
remaining: 100 - normalizedUsed,
|
|
805
|
+
unit: "%",
|
|
806
|
+
label: window.label,
|
|
807
|
+
period: window.key,
|
|
808
|
+
...resetsAt === void 0 ? {} : { resetsAt }
|
|
809
|
+
});
|
|
810
|
+
}
|
|
811
|
+
return metrics;
|
|
812
|
+
}
|
|
813
|
+
const OPENCODE_GO_KEY_USAGE_URL = "https://opencode.ai/zen/go/v1/usage";
|
|
814
|
+
const OPENCODE_GO_KEY_USAGE_HEADERS = {
|
|
815
|
+
"x-opencode-session": "pantheon-quota",
|
|
816
|
+
"user-agent": "Pantheon-Quota/1.0"
|
|
817
|
+
};
|
|
818
|
+
const OPENCODE_GO_CONSOLE_STATUS_URL = "https://opencode.ai/console/api/go/status";
|
|
819
|
+
const RESET_DATE_PATTERN = /^(\d{4})-(\d{2})-(\d{2})T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;
|
|
820
|
+
function isValidResetDate(value) {
|
|
821
|
+
const match = RESET_DATE_PATTERN.exec(value);
|
|
822
|
+
if (!match) return false;
|
|
823
|
+
const year = Number(match[1]);
|
|
824
|
+
const month = Number(match[2]);
|
|
825
|
+
const day = Number(match[3]);
|
|
826
|
+
const maxDay = [
|
|
827
|
+
31,
|
|
828
|
+
year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0) ? 29 : 28,
|
|
829
|
+
31,
|
|
830
|
+
30,
|
|
831
|
+
31,
|
|
832
|
+
30,
|
|
833
|
+
31,
|
|
834
|
+
31,
|
|
835
|
+
30,
|
|
836
|
+
31,
|
|
837
|
+
30,
|
|
838
|
+
31
|
|
839
|
+
][month - 1];
|
|
840
|
+
return month >= 1 && month <= 12 && day >= 1 && maxDay !== void 0 && day <= maxDay && Number.isFinite(Date.parse(value));
|
|
841
|
+
}
|
|
842
|
+
function openCodeGoConsoleMetrics(payload) {
|
|
843
|
+
if (!isRecord(payload)) throw new SafeAdapterError("invalid_response");
|
|
844
|
+
if (!Object.hasOwn(payload, "access") || payload.access === null) throw new SafeAdapterError("not_subscribed");
|
|
845
|
+
if (!isRecord(payload.access)) throw new SafeAdapterError("invalid_response");
|
|
846
|
+
if (!isRecord(payload.access.meters)) throw new SafeAdapterError("invalid_response");
|
|
847
|
+
const accessEndsAt = payload.access.endsAt;
|
|
848
|
+
const monthlyFallbackReset = typeof accessEndsAt === "string" && isValidResetDate(accessEndsAt) ? Date.parse(accessEndsAt) : void 0;
|
|
849
|
+
const meters = payload.access.meters;
|
|
850
|
+
const windows = [
|
|
851
|
+
{
|
|
852
|
+
key: "fiveHour",
|
|
853
|
+
label: "5-hour quota",
|
|
854
|
+
period: "rolling"
|
|
855
|
+
},
|
|
856
|
+
{
|
|
857
|
+
key: "week",
|
|
858
|
+
label: "Weekly quota",
|
|
859
|
+
period: "weekly"
|
|
860
|
+
},
|
|
861
|
+
{
|
|
862
|
+
key: "month",
|
|
863
|
+
label: "Monthly quota",
|
|
864
|
+
period: "monthly"
|
|
865
|
+
}
|
|
866
|
+
];
|
|
867
|
+
const metrics = [];
|
|
868
|
+
for (const window of windows) {
|
|
869
|
+
const meter = meters[window.key];
|
|
870
|
+
if (!isRecord(meter)) throw new SafeAdapterError("invalid_response");
|
|
871
|
+
const limit = finiteAmount(meter.limitMicroCents);
|
|
872
|
+
const used = finiteAmount(meter.usedMicroCents);
|
|
873
|
+
if (limit === void 0 || used === void 0) throw new SafeAdapterError("invalid_response");
|
|
874
|
+
const resetValue = meter.resetsAt;
|
|
875
|
+
const resetsAt = (typeof resetValue === "string" && isValidResetDate(resetValue) ? Date.parse(resetValue) : void 0) ?? (window.period === "monthly" ? monthlyFallbackReset : void 0);
|
|
876
|
+
const usedPercent = limit === 0 ? used > 0 ? 100 : 0 : Math.min(100, Math.max(0, Math.round(used / limit * 100)));
|
|
877
|
+
metrics.push({
|
|
878
|
+
kind: "provider_quota",
|
|
879
|
+
used: usedPercent,
|
|
880
|
+
remaining: 100 - usedPercent,
|
|
881
|
+
unit: "%",
|
|
882
|
+
label: window.label,
|
|
883
|
+
period: window.period,
|
|
884
|
+
...resetsAt === void 0 ? {} : { resetsAt }
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
return metrics;
|
|
888
|
+
}
|
|
889
|
+
function openAIMetrics(payload) {
|
|
890
|
+
if (!isRecord(payload) || !isRecord(payload.rate_limit)) throw new SafeAdapterError("invalid_response");
|
|
891
|
+
const windows = [{
|
|
892
|
+
key: "primary_window",
|
|
893
|
+
fallback: "5-hour ChatGPT quota",
|
|
894
|
+
period: "5h"
|
|
895
|
+
}, {
|
|
896
|
+
key: "secondary_window",
|
|
897
|
+
fallback: "Weekly ChatGPT quota",
|
|
898
|
+
period: "week"
|
|
899
|
+
}];
|
|
900
|
+
const metrics = [];
|
|
901
|
+
for (const window of windows) {
|
|
902
|
+
const value = payload.rate_limit[window.key];
|
|
903
|
+
if (value === void 0 || value === null) continue;
|
|
904
|
+
if (!isRecord(value)) throw new SafeAdapterError("invalid_response");
|
|
905
|
+
const used = percentage(value.used_percent);
|
|
906
|
+
if (used === void 0) continue;
|
|
907
|
+
const period = value.limit_window_seconds === 18e3 ? "5h" : value.limit_window_seconds === 604800 ? "week" : value.limit_window_seconds === 2628e3 ? "month" : window.period;
|
|
908
|
+
const label = period === "month" ? "Monthly ChatGPT quota" : period === "week" ? "Weekly ChatGPT quota" : window.fallback;
|
|
909
|
+
const resetsAt = epochResetAt(value.reset_at, "seconds");
|
|
910
|
+
metrics.push({
|
|
911
|
+
kind: "provider_quota",
|
|
912
|
+
used,
|
|
913
|
+
remaining: 100 - used,
|
|
914
|
+
unit: "%",
|
|
915
|
+
label,
|
|
916
|
+
period,
|
|
917
|
+
...resetsAt === void 0 ? {} : { resetsAt }
|
|
918
|
+
});
|
|
919
|
+
}
|
|
920
|
+
if (metrics.length === 0) throw new SafeAdapterError("invalid_response");
|
|
921
|
+
return metrics;
|
|
922
|
+
}
|
|
923
|
+
const OPENCODE_ZEN_BASE_URL = "https://opencode.ai/console/api";
|
|
924
|
+
const BILLING_UNITS_PER_DOLLAR = 1e8;
|
|
925
|
+
function parseConsoleBalance(payload) {
|
|
926
|
+
if (!isRecord(payload)) throw new SafeAdapterError("provider_error");
|
|
927
|
+
const value = finiteAmount(payload.balanceMicroCents);
|
|
928
|
+
if (value === void 0) throw new SafeAdapterError("provider_error");
|
|
929
|
+
return value / BILLING_UNITS_PER_DOLLAR;
|
|
930
|
+
}
|
|
931
|
+
function parseConsoleCreditLimit(payload) {
|
|
932
|
+
if (!isRecord(payload)) throw new SafeAdapterError("provider_error");
|
|
933
|
+
if (payload.creditLimitMicroCents === null) return void 0;
|
|
934
|
+
const value = finiteAmount(payload.creditLimitMicroCents);
|
|
935
|
+
if (value === void 0) throw new SafeAdapterError("provider_error");
|
|
936
|
+
return value / BILLING_UNITS_PER_DOLLAR;
|
|
937
|
+
}
|
|
938
|
+
function parseConsoleMonthlyUsage(payload, now = /* @__PURE__ */ new Date()) {
|
|
939
|
+
if (!Array.isArray(payload) || payload.length > 366) throw new SafeAdapterError("provider_error");
|
|
940
|
+
const month = `${now.getUTCFullYear()}-${String(now.getUTCMonth() + 1).padStart(2, "0")}`;
|
|
941
|
+
let total = 0;
|
|
942
|
+
for (const row of payload) {
|
|
943
|
+
if (!isRecord(row) || typeof row.date !== "string" || !/^\d{4}-\d{2}-\d{2}$/.test(row.date)) throw new SafeAdapterError("provider_error");
|
|
944
|
+
const amount = finiteAmount(row.totalCostMicroCents);
|
|
945
|
+
if (amount === void 0) throw new SafeAdapterError("provider_error");
|
|
946
|
+
if (row.date.startsWith(month)) total += amount;
|
|
947
|
+
}
|
|
948
|
+
const amount = finiteAmount(total);
|
|
949
|
+
if (amount === void 0) throw new SafeAdapterError("provider_error");
|
|
950
|
+
return amount / BILLING_UNITS_PER_DOLLAR;
|
|
951
|
+
}
|
|
952
|
+
async function openCodeZenMetrics(secret, shutdownSignal, context) {
|
|
953
|
+
const headers = context.orgId && /^[A-Za-z0-9_-]{1,128}$/.test(context.orgId) ? { "x-org-id": context.orgId } : {};
|
|
954
|
+
const routes = await Promise.allSettled([
|
|
955
|
+
requestJson(`${OPENCODE_ZEN_BASE_URL}/billing/status`, secret, shutdownSignal, "bearer", { headers }),
|
|
956
|
+
requestJson(`${OPENCODE_ZEN_BASE_URL}/billing/account`, secret, shutdownSignal, "bearer", { headers }),
|
|
957
|
+
requestJson(`${OPENCODE_ZEN_BASE_URL}/usage/cost-by-day`, secret, shutdownSignal, "bearer", { headers })
|
|
958
|
+
]);
|
|
959
|
+
const balance = routes[0];
|
|
960
|
+
if (balance?.status !== "fulfilled") throw new SafeAdapterError("provider_error");
|
|
961
|
+
const metrics = [{
|
|
962
|
+
kind: "balance",
|
|
963
|
+
amount: parseConsoleBalance(balance.value),
|
|
964
|
+
currency: "USD",
|
|
965
|
+
label: "Console balance"
|
|
966
|
+
}];
|
|
967
|
+
const account = routes[1];
|
|
968
|
+
const usage = routes[2];
|
|
969
|
+
if (account?.status === "fulfilled" && usage?.status === "fulfilled") {
|
|
970
|
+
const limit = parseConsoleCreditLimit(account.value);
|
|
971
|
+
if (limit !== void 0) {
|
|
972
|
+
const spent = parseConsoleMonthlyUsage(usage.value);
|
|
973
|
+
metrics.push({
|
|
974
|
+
kind: "provider_quota",
|
|
975
|
+
used: spent,
|
|
976
|
+
remaining: Math.max(0, limit - spent),
|
|
977
|
+
unit: "USD",
|
|
978
|
+
label: "Monthly credit limit",
|
|
979
|
+
period: "month"
|
|
980
|
+
});
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
return metrics;
|
|
984
|
+
}
|
|
985
|
+
const XAI_SUBSCRIPTIONS_URL = "https://grok.com/rest/subscriptions";
|
|
986
|
+
const XAI_BILLING_URL = "https://cli-chat-proxy.grok.com/v1/billing?format=credits";
|
|
987
|
+
const XAI_HEADERS = {
|
|
988
|
+
"user-agent": "OpenCode-Quota-Toast/1.0",
|
|
989
|
+
"x-grok-client-surface": "grok-build",
|
|
990
|
+
"x-grok-client-version": "1.0.0"
|
|
991
|
+
};
|
|
992
|
+
async function xaiSuperGrokMetrics(secret, shutdownSignal) {
|
|
993
|
+
const subscriptionPayload = await requestJson(XAI_SUBSCRIPTIONS_URL, secret, shutdownSignal, "bearer", { headers: XAI_HEADERS });
|
|
994
|
+
if (!isRecord(subscriptionPayload) || !Array.isArray(subscriptionPayload.subscriptions)) throw new SafeAdapterError("eligibility_unverified");
|
|
995
|
+
if (!subscriptionPayload.subscriptions.some((subscription) => isRecord(subscription) && subscription.status === "SUBSCRIPTION_STATUS_ACTIVE" && typeof subscription.tier === "string" && /^SUBSCRIPTION_TIER_SUPER_GROK(?:_PRO|_LITE|_HEAVY)?$/.test(subscription.tier))) throw new SafeAdapterError("eligibility_unverified");
|
|
996
|
+
const billingPayload = await requestJson(XAI_BILLING_URL, secret, shutdownSignal, "bearer", { headers: XAI_HEADERS });
|
|
997
|
+
if (!isRecord(billingPayload) || !isRecord(billingPayload.config)) throw new SafeAdapterError("provider_error");
|
|
998
|
+
const usage = billingPayload.config.creditUsagePercent;
|
|
999
|
+
const used = usage === void 0 ? void 0 : percentage(usage);
|
|
1000
|
+
if (used === void 0) throw new SafeAdapterError("provider_error");
|
|
1001
|
+
return [{
|
|
1002
|
+
kind: "provider_quota",
|
|
1003
|
+
used,
|
|
1004
|
+
remaining: 100 - used,
|
|
1005
|
+
unit: "%",
|
|
1006
|
+
label: "SuperGrok billing period",
|
|
1007
|
+
period: "billing period"
|
|
1008
|
+
}];
|
|
1009
|
+
}
|
|
1010
|
+
async function runQuotaAdapter(provider, secret, shutdownSignal, queryCli = queryAlibabaTokenPlanUsage, adapterContext = {}, fallbackSecret) {
|
|
1011
|
+
const definition = QUOTA_PROVIDER_CATALOG[provider];
|
|
1012
|
+
if (!definition.adapter) return { reason: "provider_error" };
|
|
1013
|
+
if (definition.adapter === "openai" && !isOpenAICodexOAuthMethod(adapterContext?.methodID)) return { reason: "eligibility_unverified" };
|
|
1014
|
+
if (definition.adapter === "alibaba-token-plan-cli") {
|
|
1015
|
+
const result = await queryCli(shutdownSignal);
|
|
1016
|
+
if ("reason" in result) return result;
|
|
1017
|
+
return available([{
|
|
1018
|
+
kind: "provider_quota",
|
|
1019
|
+
used: result.percentage * 100,
|
|
1020
|
+
remaining: (1 - result.percentage) * 100,
|
|
1021
|
+
unit: "%",
|
|
1022
|
+
label: "Monthly token-plan quota",
|
|
1023
|
+
period: "month"
|
|
1024
|
+
}]);
|
|
1025
|
+
}
|
|
1026
|
+
if (!secret) return { reason: "provider_error" };
|
|
1027
|
+
try {
|
|
1028
|
+
if (definition.adapter === "anthropic") return available(anthropicMetrics(await requestJson("https://api.anthropic.com/api/oauth/usage", secret, shutdownSignal, "bearer", { headers: { "anthropic-beta": "oauth-2025-04-20" } })));
|
|
1029
|
+
if (definition.adapter === "copilot") return available(copilotMetrics(await requestJson("https://api.github.com/copilot_internal/user", secret, shutdownSignal, "bearer", { headers: {
|
|
1030
|
+
"x-github-api-version": "2026-03-10",
|
|
1031
|
+
"user-agent": "opencode-quota/copilot-billing"
|
|
1032
|
+
} })));
|
|
1033
|
+
if (definition.adapter === "google-agy") {
|
|
1034
|
+
if (!adapterContext.projectId || !/^[A-Za-z0-9._:-]{1,128}$/.test(adapterContext.projectId)) throw new SafeAdapterError("eligibility_unverified");
|
|
1035
|
+
return available(googleAgyMetrics(await requestJson("https://daily-cloudcode-pa.googleapis.com/v1internal:retrieveUserQuotaSummary", secret, shutdownSignal, "bearer", {
|
|
1036
|
+
method: "POST",
|
|
1037
|
+
body: JSON.stringify({ project: adapterContext.projectId }),
|
|
1038
|
+
headers: {
|
|
1039
|
+
"content-type": "application/json",
|
|
1040
|
+
"user-agent": "antigravity/cli/1.3.5",
|
|
1041
|
+
"x-activity-request-id": randomUUID()
|
|
1042
|
+
}
|
|
1043
|
+
})));
|
|
1044
|
+
}
|
|
1045
|
+
if (definition.adapter === "opencode-go") {
|
|
1046
|
+
if (adapterContext?.methodID !== void 0 && !isOpenCodeConsoleOAuthMethod(adapterContext.methodID)) throw new SafeAdapterError("eligibility_unverified");
|
|
1047
|
+
if (isOpenCodeConsoleOAuthMethod(adapterContext?.methodID)) {
|
|
1048
|
+
const headers = adapterContext.orgId ? { "x-org-id": adapterContext.orgId } : {};
|
|
1049
|
+
try {
|
|
1050
|
+
return {
|
|
1051
|
+
...available(openCodeGoConsoleMetrics(await requestJson(OPENCODE_GO_CONSOLE_STATUS_URL, secret, shutdownSignal, "bearer", { headers }))),
|
|
1052
|
+
routeKind: "console_oauth"
|
|
1053
|
+
};
|
|
1054
|
+
} catch (error) {
|
|
1055
|
+
const failure = error instanceof SafeAdapterError ? error : new SafeAdapterError("provider_error");
|
|
1056
|
+
if (failure.reason === "not_subscribed" || failure.httpStatus === 404) return {
|
|
1057
|
+
reason: "not_subscribed",
|
|
1058
|
+
routeKind: "console_oauth"
|
|
1059
|
+
};
|
|
1060
|
+
if (!fallbackSecret || shutdownSignal.aborted) return {
|
|
1061
|
+
reason: failure.reason,
|
|
1062
|
+
...failure.retryAfterMs === void 0 ? {} : { retryAfterMs: failure.retryAfterMs },
|
|
1063
|
+
routeKind: "console_oauth"
|
|
1064
|
+
};
|
|
1065
|
+
}
|
|
1066
|
+
try {
|
|
1067
|
+
return {
|
|
1068
|
+
...available(openCodeGoMetrics(await requestJson(OPENCODE_GO_KEY_USAGE_URL, fallbackSecret, shutdownSignal, "bearer", { headers: OPENCODE_GO_KEY_USAGE_HEADERS }))),
|
|
1069
|
+
routeKind: "api_key"
|
|
1070
|
+
};
|
|
1071
|
+
} catch (error) {
|
|
1072
|
+
const failure = error instanceof SafeAdapterError ? error : new SafeAdapterError("provider_error");
|
|
1073
|
+
return {
|
|
1074
|
+
reason: failure.reason,
|
|
1075
|
+
...failure.retryAfterMs === void 0 ? {} : { retryAfterMs: failure.retryAfterMs },
|
|
1076
|
+
routeKind: "api_key"
|
|
1077
|
+
};
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
return {
|
|
1081
|
+
...available(openCodeGoMetrics(await requestJson(OPENCODE_GO_KEY_USAGE_URL, secret, shutdownSignal, "bearer", { headers: OPENCODE_GO_KEY_USAGE_HEADERS }))),
|
|
1082
|
+
routeKind: "api_key"
|
|
1083
|
+
};
|
|
1084
|
+
}
|
|
1085
|
+
if (definition.adapter === "openai") return available(openAIMetrics(await requestJson("https://chatgpt.com/backend-api/wham/usage", secret, shutdownSignal, "bearer", { headers: { "user-agent": "OpenCode-Quota-Toast/1.0" } })));
|
|
1086
|
+
if (definition.adapter === "opencode-zen") return available(await openCodeZenMetrics(secret, shutdownSignal, adapterContext));
|
|
1087
|
+
if (definition.adapter === "xai-supergrok") return available(await xaiSuperGrokMetrics(secret, shutdownSignal));
|
|
1088
|
+
} catch (error) {
|
|
1089
|
+
if (error instanceof SafeAdapterError) return {
|
|
1090
|
+
reason: error.reason,
|
|
1091
|
+
...error.retryAfterMs === void 0 ? {} : { retryAfterMs: error.retryAfterMs }
|
|
1092
|
+
};
|
|
1093
|
+
return { reason: "provider_error" };
|
|
1094
|
+
}
|
|
1095
|
+
const url = {
|
|
1096
|
+
chutes: "https://api.chutes.ai/users/me/quota_usage/me",
|
|
1097
|
+
deepseek: "https://api.deepseek.com/user/balance",
|
|
1098
|
+
"kilo-gateway": "https://app.kilo.ai/api/trpc/kiloPass.getState?batch=1&input=%7B%220%22%3Anull%7D",
|
|
1099
|
+
"kimi-global": "https://api.kimi.ai/coding/v1/usages",
|
|
1100
|
+
"kimi-cn": "https://api.kimi.com/coding/v1/usages",
|
|
1101
|
+
"minimax-global": "https://api.minimax.io/v1/api/openplatform/coding_plan/remains",
|
|
1102
|
+
"minimax-cn": "https://api.minimaxi.com/v1/token_plan/remains",
|
|
1103
|
+
nanogpt: "https://nano-gpt.com/api/subscription/v1/usage",
|
|
1104
|
+
"ollama-cloud": "https://ollama.com/api/balance",
|
|
1105
|
+
openrouter: "https://openrouter.ai/api/v1/key",
|
|
1106
|
+
synthetic: "https://api.synthetic.new/v2/quotas",
|
|
1107
|
+
zai: "https://api.z.ai/api/monitor/usage/quota/limit",
|
|
1108
|
+
zhipu: "https://bigmodel.cn/api/monitor/usage/quota/limit"
|
|
1109
|
+
}[definition.adapter];
|
|
1110
|
+
try {
|
|
1111
|
+
if (definition.adapter === "kilo-gateway") {
|
|
1112
|
+
const passMetrics = kiloPassMetrics(await requestJson(url, secret, shutdownSignal));
|
|
1113
|
+
if (passMetrics) return available(passMetrics);
|
|
1114
|
+
return available(kiloBalanceMetrics(await requestJson("https://api.kilo.ai/api/profile/balance", secret, shutdownSignal)));
|
|
1115
|
+
}
|
|
1116
|
+
if (definition.adapter === "nanogpt") return available(await nanoGptMetrics(secret, shutdownSignal));
|
|
1117
|
+
const payload = await requestJson(url, secret, shutdownSignal, definition.adapter === "zai" || definition.adapter === "zhipu" ? "direct" : "bearer");
|
|
1118
|
+
return available({
|
|
1119
|
+
chutes: chutesMetrics,
|
|
1120
|
+
deepseek: deepseekMetrics,
|
|
1121
|
+
"kimi-global": kimiMetrics,
|
|
1122
|
+
"kimi-cn": kimiMetrics,
|
|
1123
|
+
"minimax-global": (value) => minimaxMetrics(value, "global"),
|
|
1124
|
+
"minimax-cn": (value) => minimaxMetrics(value, "cn"),
|
|
1125
|
+
"ollama-cloud": ollamaMetrics,
|
|
1126
|
+
openrouter: openRouterMetrics,
|
|
1127
|
+
synthetic: syntheticMetrics,
|
|
1128
|
+
zai: (value) => glmMetrics(value, "zai"),
|
|
1129
|
+
zhipu: (value) => glmMetrics(value, "zhipu")
|
|
1130
|
+
}[definition.adapter](payload));
|
|
1131
|
+
} catch (error) {
|
|
1132
|
+
if (error instanceof SafeAdapterError) return {
|
|
1133
|
+
reason: error.reason,
|
|
1134
|
+
...error.retryAfterMs === void 0 ? {} : { retryAfterMs: error.retryAfterMs }
|
|
1135
|
+
};
|
|
1136
|
+
return { reason: "provider_error" };
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
//#endregion
|
|
1140
|
+
export { OPENAI_CODEX_OAUTH_METHOD_IDS, OPENCODE_CONSOLE_OAUTH_METHOD_IDS, QUOTA_PROVIDER_CATALOG, isOpenAICodexOAuthMethod, isOpenCodeConsoleOAuthMethod, runQuotaAdapter };
|
|
1141
|
+
|
|
1142
|
+
//# sourceMappingURL=quota-adapters.js.map
|