Skip to content

Commit 93d994f

Browse files
byongshintvcodex
andcommitted
fix(quota): harden A6API billing validation
Co-authored-by: OpenAI Codex <[email protected]>
1 parent 2903177 commit 93d994f

3 files changed

Lines changed: 72 additions & 5 deletions

File tree

‎docs-site/src/content/docs/guides/providers.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,31 @@ validates the key, and stores it. Notable entries:
221221
Most use the `openai-chat` adapter with a bearer key; a few that expose only an Anthropic-compatible
222222
endpoint (e.g. **Xiaomi MiMo**) use the `anthropic` adapter (`x-api-key`).
223223

224+
### A6API credit quota
225+
226+
A custom `openai-chat` provider using `authMode: "key"` and the canonical
227+
`https://api.a6api.com` or `https://api.a6api.com/v1` base URL receives an A6API credit meter in
228+
the dashboard and from `ocx account refresh <provider>`. The provider name is arbitrary; detection
229+
uses the canonical HTTPS endpoint. The meter converts A6API token units into USD using the account's
230+
hard credit limit and displays the percentage consumed plus remaining credit. Token expiration is
231+
not shown as a quota reset because expiration does not imply that credit replenishes.
232+
233+
```json
234+
{
235+
"providers": {
236+
"my-a6": {
237+
"adapter": "openai-chat",
238+
"authMode": "key",
239+
"baseUrl": "https://api.a6api.com/v1",
240+
"apiKey": "${A6API_API_KEY}"
241+
}
242+
}
243+
}
244+
```
245+
246+
Quota probes send the active key only to the canonical A6API host and reject redirects. Malformed,
247+
negative, or internally inconsistent billing totals produce no report rather than a misleading bar.
248+
224249
> **Tencent Cloud Coding Plan usage restriction:** Tencent documents this subscription for
225250
> interactive coding tools only. General API automation, custom application backends, and
226251
> non-interactive batch use are prohibited and may cause the plan key to be suspended.

‎src/providers/quota.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ export function clearProviderQuotaCache(): void {
6565
function cacheKey(config: OcxConfig): string {
6666
const providers = Object.entries(config.providers)
6767
.map(([name, provider]) => {
68-
const resolvedKey = resolveEnvValue(provider.apiKey)?.trim();
68+
const resolvedKey = typeof provider.apiKey === "string"
69+
? resolveEnvValue(provider.apiKey)?.trim()
70+
: undefined;
6971
const activeKeyId = resolvedKey ? apiKeyPoolEntryId(resolvedKey) : "none";
7072
return `${name}:${provider.adapter}:${provider.authMode ?? "key"}:${providerCodexAccountMode(name, provider) ?? "none"}:${provider.disabled === true ? "off" : "on"}:${provider.baseUrl}:${activeKeyId}`;
7173
})
@@ -125,7 +127,7 @@ function isBuiltInChatGptForwardProvider(name: string, provider: OcxProviderConf
125127
}
126128

127129
function isCanonicalA6apiBaseUrl(baseUrl: string): boolean {
128-
const normalized = baseUrl.trim().replace(/\/+$/, "");
130+
const normalized = normalizedBaseUrl(baseUrl);
129131
return normalized === A6API_BASE_URL || normalized === `${A6API_BASE_URL}/v1`;
130132
}
131133

@@ -166,7 +168,8 @@ async function fetchA6apiQuota(provider: string, config: OcxProviderConfig): Pro
166168
const availableUnits = firstFinite(token, ["total_available"]);
167169
if (limitUsd === undefined || grantedUnits === undefined || usedUnits === undefined
168170
|| availableUnits === undefined || limitUsd <= 0 || grantedUnits <= 0
169-
|| usedUnits < 0 || availableUnits < 0) return null;
171+
|| usedUnits < 0 || availableUnits < 0
172+
|| usedUnits + availableUnits > grantedUnits) return null;
170173
const usdPerUnit = limitUsd / grantedUnits;
171174
const usedUsd = usedUnits * usdPerUnit;
172175
const remainingUsd = Math.max(0, availableUnits * usdPerUnit);
@@ -493,7 +496,7 @@ export async function fetchProviderAccountQuotas(
493496
function normalizedBaseUrl(value: string): string | null {
494497
try {
495498
const url = new URL(value);
496-
if (url.search || url.hash) return null;
499+
if (url.username || url.password || url.search || url.hash) return null;
497500
return `${url.origin.toLowerCase()}${url.pathname.replace(/\/+$/, "")}`;
498501
} catch {
499502
return null;

‎tests/provider-quota.test.ts‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ describe("fetchProviderQuotaReports", () => {
308308
test.each([
309309
{ total_used: -1, total_available: 101 },
310310
{ total_used: 1, total_available: -1 },
311-
])("A6API quota drops negative usage totals", async usage => {
311+
{ total_used: 80, total_available: 80 },
312+
])("A6API quota drops malformed usage totals", async usage => {
312313
globalThis.fetch = (async (input: RequestInfo | URL) => {
313314
const url = String(input);
314315
return new Response(JSON.stringify(url.includes("subscription")
@@ -321,6 +322,44 @@ describe("fetchProviderQuotaReports", () => {
321322
expect(result.reports).toEqual([]);
322323
});
323324

325+
test("A6API quota accepts equivalent canonical HTTPS URLs only", async () => {
326+
const seen: string[] = [];
327+
globalThis.fetch = (async (input: RequestInfo | URL) => {
328+
seen.push(String(input));
329+
return new Response(JSON.stringify(String(input).includes("subscription")
330+
? { data: { hard_limit_usd: 10 } }
331+
: { data: { total_granted: 100, total_used: 25, total_available: 75 } }), { status: 200 });
332+
}) as typeof fetch;
333+
334+
const equivalent = await fetchProviderQuotaReports(a6apiOnlyConfig("https://API.A6API.COM:443/v1/"), true);
335+
const credentialedUrl = "https://user" + "@api.a6api.com/v1";
336+
const credentialed = await fetchProviderQuotaReports(a6apiOnlyConfig(credentialedUrl), true);
337+
338+
expect(equivalent.reports).toHaveLength(1);
339+
expect(credentialed.reports).toEqual([]);
340+
expect(seen).toHaveLength(2);
341+
});
342+
343+
test("malformed API-key fields do not break unrelated quota reports", async () => {
344+
globalThis.fetch = (async (input: RequestInfo | URL) => new Response(JSON.stringify(
345+
String(input).includes("subscription")
346+
? { data: { hard_limit_usd: 10 } }
347+
: { data: { total_granted: 100, total_used: 25, total_available: 75 } },
348+
), { status: 200 })) as typeof fetch;
349+
const config = a6apiOnlyConfig();
350+
config.providers.broken = {
351+
adapter: "openai-chat",
352+
authMode: "key",
353+
baseUrl: "https://example.com/v1",
354+
apiKey: 42,
355+
} as unknown as OcxConfig["providers"][string];
356+
357+
const result = await fetchProviderQuotaReports(config, true);
358+
359+
expect(result.reports).toHaveLength(1);
360+
expect(result.reports[0]?.provider).toBe("a6api");
361+
});
362+
324363
test("A6API quota is detected by canonical base URL for custom provider names", async () => {
325364
globalThis.fetch = (async (input: RequestInfo | URL) => new Response(JSON.stringify(
326365
String(input).includes("subscription")

0 commit comments

Comments
 (0)