mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-18 07:58:27 +00:00
fix(quota): route remaining console quota reads through the connection proxy (#16344)
Routes the remaining console quota readings (jina, llmgateway, lyceum, moonshot, new-api aggregator, opencode, openrouter, tavily, v0) through the connection's assigned egress path via fetchWithConnectionProxy, completing #15885 (merged first; this branch was stacked on it and now carries only its own delta). Maintainer fixes on top: - open-sse/services/opencodeQuotaFetcher.ts: dropped the stale effectiveConnectionId = connectionId || "noauth" sentinel. Keyless OpenCode Free is gone (#16310), so 'noauth' resolved a proxy for a connection that cannot exist and shared one cache entry/session seed across every empty-id reading. Cache, session seed, proxy lookup and invalidation now use the real connection id; an empty id reports no reading like every other quota fetcher (the policy #16773/#15885 test for the helper). - open-sse/services/grokQuotaFetcher.ts: the OIDC/refresh/billing helpers took an optional id and passed connectionId ?? "" to the helper, which turns an empty id into an unknown reading. Every caller already has the id (fetchGrokWebQuota, xaiOauthQuotaFetcher), so the parameter is now required and the ?? "" fallback is gone. The helper's empty-id policy itself is unchanged (merged + tested in #16773/#15885). - tests/unit/quota-fetchers-connection-proxy.test.ts: the 'shared no-auth reading' case replaced by (a) an empty id makes no fetch outside the proxy — red on the PR head before the fix — and (b) the cache is keyed and invalidated by the connection id, not a shared no-auth key. Evidence: focused node:test 324/324 over 29 quota-fetcher / usage-proxy files, 66/66 re-run after the final tip merge. Gates: typecheck:core, open-sse and API typechecks, file-size, complexity, cognitive-complexity, dead-code, boundary, agent-skills (Generated: 0), changelog-integrity, env-doc-sync, docs-sync, eslint all exit 0; prettier clean on the PR's hunks (the one flagged file, newApiAggregatorQuotaFetcher.ts, is unformatted on the release tip in an untouched function). Remaining CI reds are the inherited base-red #15306. Thanks @maxmad64bis!
This commit is contained in:
@@ -0,0 +1 @@
|
||||
- **fix(quota):** remaining console quota readings use the assigned connection egress path ([#16344](https://github.com/diegosouzapw/OmniRoute/pull/16344)) — thanks @maxmad64bis
|
||||
@@ -208,13 +208,13 @@ function writeRefreshedTokens(
|
||||
async function discoverTokenEndpoint(
|
||||
issuer: string,
|
||||
signal: AbortSignal,
|
||||
connectionId?: string | null
|
||||
connectionId: string
|
||||
): Promise<string> {
|
||||
const discoveryUrl = `${issuer.replace(/\/$/, "")}/.well-known/openid-configuration`;
|
||||
if (!isAllowedXaiUrl(discoveryUrl)) {
|
||||
throw new Error("invalid oidc issuer");
|
||||
}
|
||||
const res = await fetchWithConnectionProxy(connectionId ?? "", discoveryUrl, {
|
||||
const res = await fetchWithConnectionProxy(connectionId, discoveryUrl, {
|
||||
headers: { Accept: "application/json" },
|
||||
signal,
|
||||
});
|
||||
@@ -229,7 +229,7 @@ async function discoverTokenEndpoint(
|
||||
async function refreshAccessToken(
|
||||
auth: ResolvedAuth,
|
||||
signal: AbortSignal,
|
||||
connectionId?: string | null
|
||||
connectionId: string
|
||||
): Promise<ResolvedAuth> {
|
||||
if (!auth.refreshToken) {
|
||||
throw new Error("auth expired — run `grok login`");
|
||||
@@ -239,7 +239,7 @@ async function refreshAccessToken(
|
||||
}
|
||||
|
||||
const tokenEndpoint = await discoverTokenEndpoint(auth.issuer, signal, connectionId);
|
||||
const res = await fetchWithConnectionProxy(connectionId ?? "", tokenEndpoint, {
|
||||
const res = await fetchWithConnectionProxy(connectionId, tokenEndpoint, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
@@ -297,10 +297,7 @@ function needsRefresh(auth: ResolvedAuth): boolean {
|
||||
return auth.expiresAtMs <= Date.now() + EXPIRY_SKEW_MS;
|
||||
}
|
||||
|
||||
async function resolveAuth(
|
||||
signal: AbortSignal,
|
||||
connectionId?: string | null
|
||||
): Promise<ResolvedAuth> {
|
||||
async function resolveAuth(signal: AbortSignal, connectionId: string): Promise<ResolvedAuth> {
|
||||
const file = readAuthFile();
|
||||
if (!file) throw new Error("no grok auth — run `grok login`");
|
||||
const auth = pickAuthEntry(file);
|
||||
@@ -343,8 +340,8 @@ function resetLocalLabel(endIso?: string): string {
|
||||
*/
|
||||
export async function fetchGrokBillingWithToken(
|
||||
token: string,
|
||||
signal?: AbortSignal,
|
||||
connectionId?: string | null
|
||||
signal: AbortSignal | undefined,
|
||||
connectionId: string
|
||||
): Promise<GrokBillingSnapshot> {
|
||||
const controller = signal ? null : new AbortController();
|
||||
const timeout = controller ? setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS) : null;
|
||||
@@ -366,9 +363,9 @@ export function grokBillingSnapshotToQuotaInfo(snap: GrokBillingSnapshot): Quota
|
||||
async function fetchBilling(
|
||||
token: string,
|
||||
signal: AbortSignal,
|
||||
connectionId?: string | null
|
||||
connectionId: string
|
||||
): Promise<UsageSnapshot> {
|
||||
const res = await fetchWithConnectionProxy(connectionId ?? "", BILLING_URL, {
|
||||
const res = await fetchWithConnectionProxy(connectionId, BILLING_URL, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { registerQuotaFetcher, type QuotaInfo } from "./quotaPreflight.ts";
|
||||
import { registerMonitorFetcher } from "./quotaMonitor.ts";
|
||||
import { throttleQuotaFetch } from "./quotaFetchThrottle.ts";
|
||||
import { fetchWithConnectionProxy } from "./connectionProxyFetch.ts";
|
||||
|
||||
/**
|
||||
* Jina's wallet-balance endpoint only accepts the key as the `api_key` QUERY
|
||||
@@ -124,13 +125,14 @@ export async function fetchJinaQuota(
|
||||
await throttleQuotaFetch();
|
||||
// Credential-bearing URL (see module header) — keep it local, never log it.
|
||||
const url = `${JINA_BALANCE_URL}?api_key=${encodeURIComponent(token)}`;
|
||||
const response = await fetch(url, {
|
||||
const response = await fetchWithConnectionProxy(connectionId, url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
},
|
||||
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
||||
});
|
||||
if (!response) return null;
|
||||
|
||||
if (!response.ok) {
|
||||
quotaCache.set(connectionId, { quota: null, fetchedAt: Date.now() });
|
||||
|
||||
@@ -48,6 +48,7 @@ import {
|
||||
} from "./quotaPreflight.ts";
|
||||
import { registerMonitorFetcher } from "./quotaMonitor.ts";
|
||||
import { throttleQuotaFetch } from "./quotaFetchThrottle.ts";
|
||||
import { fetchWithConnectionProxy } from "./connectionProxyFetch.ts";
|
||||
|
||||
const LLMGATEWAY_CONFIG = {
|
||||
baseUrl: "https://api.llmgateway.io/v1",
|
||||
@@ -225,11 +226,12 @@ export function buildLlmgatewayQuota(key: LlmgatewayKeyFields): LlmgatewayQuota
|
||||
// ─── Core Fetcher ────────────────────────────────────────────────────────────
|
||||
|
||||
async function fetchJson(
|
||||
connectionId: string,
|
||||
url: string,
|
||||
apiKey: string
|
||||
): Promise<{ status: number; data: unknown } | null> {
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
const response = await fetchWithConnectionProxy(connectionId, url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${apiKey}`,
|
||||
@@ -237,6 +239,7 @@ async function fetchJson(
|
||||
},
|
||||
signal: AbortSignal.timeout(8_000),
|
||||
});
|
||||
if (!response) return null;
|
||||
if (!response.ok) return { status: response.status, data: null };
|
||||
const data = await response.json();
|
||||
return { status: response.status, data };
|
||||
@@ -272,6 +275,7 @@ export async function fetchLlmgatewayQuota(
|
||||
await throttleQuotaFetch();
|
||||
|
||||
const keyResult = await fetchJson(
|
||||
connectionId,
|
||||
`${LLMGATEWAY_CONFIG.baseUrl}${LLMGATEWAY_CONFIG.keyPath}`,
|
||||
apiKey
|
||||
);
|
||||
|
||||
@@ -37,6 +37,7 @@ import {
|
||||
} from "./quotaPreflight.ts";
|
||||
import { registerMonitorFetcher } from "./quotaMonitor.ts";
|
||||
import { throttleQuotaFetch } from "./quotaFetchThrottle.ts";
|
||||
import { fetchWithConnectionProxy } from "./connectionProxyFetch.ts";
|
||||
|
||||
const LYCEUM_CONFIG = {
|
||||
creditsUrl: "https://api.lyceum.technology/api/v2/external/billing/credits",
|
||||
@@ -153,11 +154,12 @@ export function buildLyceumQuota(fields: LyceumCreditsFields): LyceumQuota {
|
||||
// ─── Core Fetcher ────────────────────────────────────────────────────────────
|
||||
|
||||
async function fetchJson(
|
||||
connectionId: string,
|
||||
url: string,
|
||||
apiKey: string
|
||||
): Promise<{ status: number; data: unknown } | null> {
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
const response = await fetchWithConnectionProxy(connectionId, url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${apiKey}`,
|
||||
@@ -165,6 +167,7 @@ async function fetchJson(
|
||||
},
|
||||
signal: AbortSignal.timeout(8_000),
|
||||
});
|
||||
if (!response) return null;
|
||||
if (!response.ok) return { status: response.status, data: null };
|
||||
const data = await response.json();
|
||||
return { status: response.status, data };
|
||||
@@ -198,7 +201,7 @@ export async function fetchLyceumQuota(
|
||||
try {
|
||||
await throttleQuotaFetch();
|
||||
|
||||
const result = await fetchJson(LYCEUM_CONFIG.creditsUrl, apiKey);
|
||||
const result = await fetchJson(connectionId, LYCEUM_CONFIG.creditsUrl, apiKey);
|
||||
if (!result || result.status !== 200) {
|
||||
quotaCache.delete(connectionId);
|
||||
return null;
|
||||
|
||||
@@ -14,6 +14,7 @@ import { toNumber } from "@/shared/utils/numeric";
|
||||
import { registerQuotaFetcher, type QuotaInfo } from "./quotaPreflight.ts";
|
||||
import { registerMonitorFetcher } from "./quotaMonitor.ts";
|
||||
import { throttleQuotaFetch } from "./quotaFetchThrottle.ts";
|
||||
import { fetchWithConnectionProxy } from "./connectionProxyFetch.ts";
|
||||
import { isUrlHostOrSubdomain } from "../utils/urlHost.ts";
|
||||
import {
|
||||
isKimiCodingBaseUrl,
|
||||
@@ -171,7 +172,7 @@ export async function fetchMoonshotQuota(
|
||||
|
||||
try {
|
||||
await throttleQuotaFetch();
|
||||
const response = await fetch(url, {
|
||||
const response = await fetchWithConnectionProxy(connectionId, url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: authHeader,
|
||||
@@ -180,6 +181,7 @@ export async function fetchMoonshotQuota(
|
||||
},
|
||||
signal: AbortSignal.timeout(8_000),
|
||||
});
|
||||
if (!response) return null;
|
||||
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
quotaCache.delete(connectionId);
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
|
||||
import type { QuotaInfo } from "./quotaPreflight.ts";
|
||||
import { throttleQuotaFetch } from "./quotaFetchThrottle.ts";
|
||||
import { fetchWithConnectionProxy } from "./connectionProxyFetch.ts";
|
||||
import { toNumber } from "@/shared/utils/numeric";
|
||||
|
||||
const SELF_PATH = "/api/user/self";
|
||||
@@ -174,7 +175,7 @@ export async function fetchNewApiAggregatorQuota(
|
||||
try {
|
||||
await throttleQuotaFetch();
|
||||
|
||||
const response = await fetch(url, {
|
||||
const response = await fetchWithConnectionProxy(connectionId, url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${systemAccessToken}`,
|
||||
@@ -184,6 +185,7 @@ export async function fetchNewApiAggregatorQuota(
|
||||
},
|
||||
signal: AbortSignal.timeout(8_000),
|
||||
});
|
||||
if (!response) return null;
|
||||
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
quotaCache.delete(connectionId);
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
import { registerQuotaFetcher, registerQuotaWindows, type QuotaInfo } from "./quotaPreflight.ts";
|
||||
import { registerMonitorFetcher } from "./quotaMonitor.ts";
|
||||
import { throttleQuotaFetch } from "./quotaFetchThrottle.ts";
|
||||
import { fetchWithConnectionProxy } from "./connectionProxyFetch.ts";
|
||||
import { buildOpencodeBackgroundHeaders } from "../utils/opencodeHeaders.ts";
|
||||
|
||||
const OPENCODE_QUOTA_URL =
|
||||
@@ -41,6 +42,15 @@ interface CacheEntry {
|
||||
// In-memory cache: connectionId → successful quota bound to its normalized API key
|
||||
const quotaCache = new Map<string, CacheEntry>();
|
||||
|
||||
function normalizeApiKey(connection?: Record<string, unknown>): string {
|
||||
return typeof connection?.apiKey === "string"
|
||||
? connection.apiKey
|
||||
.trim()
|
||||
.replace(/^Bearer\s+/i, "")
|
||||
.trim()
|
||||
: "";
|
||||
}
|
||||
|
||||
// One-time 404 warning per URL (avoids spamming on every request)
|
||||
const _warned404Urls = new Set<string>();
|
||||
|
||||
@@ -160,13 +170,7 @@ export async function fetchOpencodeQuota(
|
||||
connectionId: string,
|
||||
connection?: Record<string, unknown>
|
||||
): Promise<OpencodeTripleWindowQuota | null> {
|
||||
const apiKey =
|
||||
typeof connection?.apiKey === "string"
|
||||
? connection.apiKey
|
||||
.trim()
|
||||
.replace(/^Bearer\s+/i, "")
|
||||
.trim()
|
||||
: "";
|
||||
const apiKey = normalizeApiKey(connection);
|
||||
if (!apiKey) return null;
|
||||
|
||||
const cached = quotaCache.get(connectionId);
|
||||
@@ -176,7 +180,7 @@ export async function fetchOpencodeQuota(
|
||||
|
||||
try {
|
||||
await throttleQuotaFetch();
|
||||
const response = await fetch(OPENCODE_QUOTA_URL, {
|
||||
const response = await fetchWithConnectionProxy(connectionId, OPENCODE_QUOTA_URL, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${apiKey}`,
|
||||
@@ -190,6 +194,7 @@ export async function fetchOpencodeQuota(
|
||||
},
|
||||
signal: AbortSignal.timeout(8_000),
|
||||
});
|
||||
if (!response) return null;
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 404 && !_warned404Urls.has(OPENCODE_QUOTA_URL)) {
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
import { registerQuotaFetcher, type QuotaInfo } from "./quotaPreflight.ts";
|
||||
import { registerMonitorFetcher } from "./quotaMonitor.ts";
|
||||
import { throttleQuotaFetch } from "./quotaFetchThrottle.ts";
|
||||
import { fetchWithConnectionProxy } from "./connectionProxyFetch.ts";
|
||||
import {
|
||||
getFreeWindowStatus,
|
||||
isFreeVariantModel,
|
||||
@@ -283,11 +284,12 @@ function checkFreeWindowExhausted(
|
||||
// ─── Core Fetcher ────────────────────────────────────────────────────────────
|
||||
|
||||
async function fetchJson(
|
||||
connectionId: string,
|
||||
url: string,
|
||||
apiKey: string
|
||||
): Promise<{ status: number; data: unknown } | null> {
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
const response = await fetchWithConnectionProxy(connectionId, url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${apiKey}`,
|
||||
@@ -295,6 +297,7 @@ async function fetchJson(
|
||||
},
|
||||
signal: AbortSignal.timeout(8_000),
|
||||
});
|
||||
if (!response) return null;
|
||||
if (!response.ok) return { status: response.status, data: null };
|
||||
const data = await response.json();
|
||||
return { status: response.status, data };
|
||||
@@ -385,10 +388,12 @@ export async function fetchOpenrouterQuota(
|
||||
await throttleQuotaFetch();
|
||||
|
||||
const keyResult = await fetchJson(
|
||||
connectionId,
|
||||
`${OPENROUTER_CONFIG.baseUrl}${OPENROUTER_CONFIG.keyPath}`,
|
||||
apiKey
|
||||
);
|
||||
const creditsResult = await fetchJson(
|
||||
connectionId,
|
||||
`${OPENROUTER_CONFIG.baseUrl}${OPENROUTER_CONFIG.creditsPath}`,
|
||||
apiKey
|
||||
);
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
import { registerQuotaFetcher, type QuotaInfo } from "./quotaPreflight.ts";
|
||||
import { registerMonitorFetcher } from "./quotaMonitor.ts";
|
||||
import { throttleQuotaFetch } from "./quotaFetchThrottle.ts";
|
||||
import { fetchWithConnectionProxy } from "./connectionProxyFetch.ts";
|
||||
import { isUrlHost } from "../utils/urlHost.ts";
|
||||
import { toNumberOrNull } from "@/shared/utils/numeric";
|
||||
|
||||
@@ -180,7 +181,7 @@ export async function fetchTavilyQuota(
|
||||
try {
|
||||
await throttleQuotaFetch();
|
||||
|
||||
const response = await fetch(TAVILY_USAGE_URL, {
|
||||
const response = await fetchWithConnectionProxy(connectionId, TAVILY_USAGE_URL, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
@@ -188,6 +189,10 @@ export async function fetchTavilyQuota(
|
||||
},
|
||||
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
||||
});
|
||||
if (!response) {
|
||||
quotaCache.set(connectionId, { quota: null, fetchedAt: Date.now() });
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
quotaCache.set(connectionId, { quota: null, fetchedAt: Date.now() });
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
import { registerQuotaFetcher, registerQuotaWindows, type QuotaInfo } from "./quotaPreflight.ts";
|
||||
import { registerMonitorFetcher } from "./quotaMonitor.ts";
|
||||
import { throttleQuotaFetch } from "./quotaFetchThrottle.ts";
|
||||
import { fetchWithConnectionProxy } from "./connectionProxyFetch.ts";
|
||||
|
||||
const V0_CONFIG = {
|
||||
baseUrl: "https://api.v0.dev",
|
||||
@@ -115,17 +116,19 @@ interface WindowFetchResult {
|
||||
* rate-limits (dailyOps) calls — the only difference is the response shape parser.
|
||||
*/
|
||||
async function fetchWindow(
|
||||
connectionId: string,
|
||||
url: string,
|
||||
headers: Record<string, string>,
|
||||
parse: (data: unknown) => { billingType: string | null; window: WindowFetchResult["window"] }
|
||||
): Promise<WindowFetchResult> {
|
||||
try {
|
||||
await throttleQuotaFetch();
|
||||
const response = await fetch(url, {
|
||||
const response = await fetchWithConnectionProxy(connectionId, url, {
|
||||
method: "GET",
|
||||
headers,
|
||||
signal: AbortSignal.timeout(8_000),
|
||||
});
|
||||
if (!response) return { window: null, billingType: null, invalidCredential: false };
|
||||
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
return { window: null, billingType: null, invalidCredential: true };
|
||||
@@ -219,6 +222,7 @@ export async function fetchV0Quota(
|
||||
let billingType = "unknown";
|
||||
|
||||
const billingResult = await fetchWindow(
|
||||
connectionId,
|
||||
`${V0_CONFIG.baseUrl}${V0_CONFIG.billingPath}`,
|
||||
headers,
|
||||
parseBillingResponse
|
||||
@@ -229,6 +233,7 @@ export async function fetchV0Quota(
|
||||
}
|
||||
|
||||
const rateLimitsResult = await fetchWindow(
|
||||
connectionId,
|
||||
`${V0_CONFIG.baseUrl}${V0_CONFIG.rateLimitsPath}`,
|
||||
headers,
|
||||
parseRateLimitsResponse
|
||||
|
||||
@@ -60,6 +60,40 @@ import {
|
||||
fetchQwenTokenPlanQuota,
|
||||
invalidateQwenTokenPlanQuotaCache,
|
||||
} from "../../open-sse/services/qwenTokenPlanQuotaFetcher.ts";
|
||||
import {
|
||||
fetchJinaQuota,
|
||||
invalidateJinaQuotaCache,
|
||||
} from "../../open-sse/services/jinaQuotaFetcher.ts";
|
||||
import {
|
||||
fetchLlmgatewayQuota,
|
||||
invalidateLlmgatewayQuotaCache,
|
||||
} from "../../open-sse/services/llmgatewayQuotaFetcher.ts";
|
||||
import {
|
||||
fetchLyceumQuota,
|
||||
invalidateLyceumQuotaCache,
|
||||
} from "../../open-sse/services/lyceumQuotaFetcher.ts";
|
||||
import {
|
||||
fetchMoonshotQuota,
|
||||
invalidateMoonshotQuotaCache,
|
||||
} from "../../open-sse/services/moonshotQuotaFetcher.ts";
|
||||
import {
|
||||
fetchNewApiAggregatorQuota,
|
||||
invalidateNewApiAggregatorQuotaCache,
|
||||
} from "../../open-sse/services/newApiAggregatorQuotaFetcher.ts";
|
||||
import {
|
||||
fetchOpencodeQuota,
|
||||
invalidateOpencodeQuotaCache,
|
||||
} from "../../open-sse/services/opencodeQuotaFetcher.ts";
|
||||
import {
|
||||
fetchOpenrouterQuota,
|
||||
invalidateOpenrouterQuotaCache,
|
||||
} from "../../open-sse/services/openrouterQuotaFetcher.ts";
|
||||
import {
|
||||
fetchTavilyQuota,
|
||||
invalidateTavilyQuotaCache,
|
||||
} from "../../open-sse/services/tavilyQuotaFetcher.ts";
|
||||
import { fetchV0Quota, invalidateV0QuotaCache } from "../../open-sse/services/v0QuotaFetcher.ts";
|
||||
import { readFileSync } from "node:fs";
|
||||
|
||||
// socks5 skips the TCP reachability probe in runWithProxyContext, so the mock
|
||||
// fetch below never races the fast-fail path; the proxy context is still set.
|
||||
@@ -578,3 +612,332 @@ test("grok web billing reads inside the proxy context", async () => {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("jina quota reads inside the proxy context", async () => {
|
||||
useAssignedProxy();
|
||||
const connectionId = `jina-proxy-${Date.now()}`;
|
||||
let observed: boolean | null = null;
|
||||
const wrappedFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async () => {
|
||||
observed = hasAmbientProxyContext();
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
code: 200,
|
||||
data: { user_id: "u1", wallet: { total_balance: 5000000 } },
|
||||
}),
|
||||
{ status: 200 }
|
||||
);
|
||||
}) as typeof globalThis.fetch;
|
||||
try {
|
||||
const quota = await fetchJinaQuota(connectionId, { apiKey: "jina-tok" });
|
||||
assert.ok(quota);
|
||||
assert.equal(observed, true);
|
||||
} finally {
|
||||
globalThis.fetch = wrappedFetch;
|
||||
invalidateJinaQuotaCache(connectionId);
|
||||
}
|
||||
});
|
||||
|
||||
test("llmgateway quota reads inside the proxy context", async () => {
|
||||
useAssignedProxy();
|
||||
const connectionId = `llmgw-proxy-${Date.now()}`;
|
||||
let observed: boolean | null = null;
|
||||
const wrappedFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async () => {
|
||||
observed = hasAmbientProxyContext();
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
data: {
|
||||
devPlan: "pro",
|
||||
devPlanCreditsUsed: "25",
|
||||
devPlanCreditsLimit: "237",
|
||||
devPlanCreditsRemaining: "212.00",
|
||||
devPlanPremiumWeeklyLimit: "35.55",
|
||||
devPlanPremiumCreditsUsed: "5.00",
|
||||
devPlanPremiumWeekResetsAt: "2026-08-28T12:00:00.000Z",
|
||||
},
|
||||
}),
|
||||
{ status: 200 }
|
||||
);
|
||||
}) as typeof globalThis.fetch;
|
||||
try {
|
||||
const quota = await fetchLlmgatewayQuota(connectionId, { apiKey: "llmgtwy_test" });
|
||||
assert.ok(quota);
|
||||
assert.equal(observed, true);
|
||||
} finally {
|
||||
globalThis.fetch = wrappedFetch;
|
||||
invalidateLlmgatewayQuotaCache(connectionId);
|
||||
}
|
||||
});
|
||||
|
||||
test("lyceum quota reads inside the proxy context", async () => {
|
||||
useAssignedProxy();
|
||||
const connectionId = `lyceum-proxy-${Date.now()}`;
|
||||
let observed: boolean | null = null;
|
||||
const wrappedFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async () => {
|
||||
observed = hasAmbientProxyContext();
|
||||
return new Response(
|
||||
JSON.stringify({ available_credits: 50, used_credits: 10, remaining_credits: 40 }),
|
||||
{ status: 200 }
|
||||
);
|
||||
}) as typeof globalThis.fetch;
|
||||
try {
|
||||
const quota = await fetchLyceumQuota(connectionId, { apiKey: "lk_test" });
|
||||
assert.ok(quota);
|
||||
assert.equal(observed, true);
|
||||
} finally {
|
||||
globalThis.fetch = wrappedFetch;
|
||||
invalidateLyceumQuotaCache(connectionId);
|
||||
}
|
||||
});
|
||||
|
||||
test("moonshot quota reads inside the proxy context", async () => {
|
||||
useAssignedProxy();
|
||||
const connectionId = `ms-proxy-${Date.now()}`;
|
||||
let observed: boolean | null = null;
|
||||
const wrappedFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async () => {
|
||||
observed = hasAmbientProxyContext();
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
code: 0,
|
||||
data: { available_balance: 2.5, voucher_balance: 0, cash_balance: 2.5 },
|
||||
status: true,
|
||||
}),
|
||||
{ status: 200 }
|
||||
);
|
||||
}) as typeof globalThis.fetch;
|
||||
try {
|
||||
const quota = await fetchMoonshotQuota(connectionId, {
|
||||
apiKey: "sk-test",
|
||||
providerSpecificData: { baseUrl: "https://api.moonshot.cn/v1" },
|
||||
});
|
||||
assert.ok(quota);
|
||||
assert.equal(observed, true);
|
||||
} finally {
|
||||
globalThis.fetch = wrappedFetch;
|
||||
invalidateMoonshotQuotaCache(connectionId);
|
||||
}
|
||||
});
|
||||
|
||||
test("new-api aggregator quota reads inside the proxy context", async () => {
|
||||
useAssignedProxy();
|
||||
const connectionId = `agg-proxy-${Date.now()}`;
|
||||
let observed: boolean | null = null;
|
||||
const wrappedFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async () => {
|
||||
observed = hasAmbientProxyContext();
|
||||
return new Response(JSON.stringify({ data: { quota: 250_000 } }), { status: 200 });
|
||||
}) as typeof globalThis.fetch;
|
||||
try {
|
||||
const quota = await fetchNewApiAggregatorQuota(connectionId, {
|
||||
providerSpecificData: {
|
||||
consoleApiKey: "system-access-token",
|
||||
newApiUserId: "42",
|
||||
newApiAggregatorBalance: true,
|
||||
baseUrl: "https://my-newapi.example.com/v1",
|
||||
},
|
||||
});
|
||||
assert.ok(quota);
|
||||
assert.equal(observed, true);
|
||||
} finally {
|
||||
globalThis.fetch = wrappedFetch;
|
||||
invalidateNewApiAggregatorQuotaCache(connectionId);
|
||||
}
|
||||
});
|
||||
|
||||
test("opencode quota reads inside the proxy context", async () => {
|
||||
useAssignedProxy();
|
||||
const connectionId = `opencode-proxy-${Date.now()}`;
|
||||
let observed: boolean | null = null;
|
||||
const wrappedFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async () => {
|
||||
observed = hasAmbientProxyContext();
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
usage: {
|
||||
rolling: { status: "ok", percent: 25, resetsAt: "2026-09-01T01:02:03.000Z" },
|
||||
weekly: { status: "ok", percent: 50, resetsAt: "2026-09-05T04:05:06.000Z" },
|
||||
monthly: { status: "ok", percent: 10, resetsAt: "2026-09-30T07:08:09.000Z" },
|
||||
},
|
||||
}),
|
||||
{ status: 200 }
|
||||
);
|
||||
}) as typeof globalThis.fetch;
|
||||
try {
|
||||
const quota = await fetchOpencodeQuota(connectionId, { apiKey: "opencode-key" });
|
||||
assert.ok(quota);
|
||||
assert.equal(observed, true);
|
||||
} finally {
|
||||
globalThis.fetch = wrappedFetch;
|
||||
invalidateOpencodeQuotaCache(connectionId);
|
||||
}
|
||||
});
|
||||
|
||||
test("opencode quota without a connection identifier makes no fetch outside the proxy", async () => {
|
||||
// Keyless OpenCode Free is gone (#16310): every OpenCode reading belongs to a
|
||||
// stored connection, so there is no shared "noauth" reading to resolve a proxy
|
||||
// for. An empty id reports no reading, like every other quota fetcher.
|
||||
useAssignedProxy();
|
||||
let calls = 0;
|
||||
const wrappedFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async () => {
|
||||
calls += 1;
|
||||
return new Response("{}", { status: 200 });
|
||||
}) as typeof globalThis.fetch;
|
||||
try {
|
||||
const quota = await fetchOpencodeQuota("", { apiKey: "opencode-key" });
|
||||
assert.equal(quota, null);
|
||||
assert.equal(calls, 0);
|
||||
} finally {
|
||||
globalThis.fetch = wrappedFetch;
|
||||
invalidateOpencodeQuotaCache("");
|
||||
}
|
||||
});
|
||||
|
||||
test("opencode quota caches and invalidates by the connection id, not a shared no-auth key", async () => {
|
||||
useAssignedProxy();
|
||||
const connectionId = `opencode-cache-${Date.now()}`;
|
||||
let calls = 0;
|
||||
const wrappedFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async () => {
|
||||
calls += 1;
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
usage: {
|
||||
rolling: { status: "ok", percent: 25, resetsAt: "2026-09-01T01:02:03.000Z" },
|
||||
weekly: { status: "ok", percent: 50, resetsAt: "2026-09-05T04:05:06.000Z" },
|
||||
monthly: { status: "ok", percent: 10, resetsAt: "2026-09-30T07:08:09.000Z" },
|
||||
},
|
||||
}),
|
||||
{ status: 200 }
|
||||
);
|
||||
}) as typeof globalThis.fetch;
|
||||
try {
|
||||
assert.ok(await fetchOpencodeQuota(connectionId, { apiKey: "opencode-key" }));
|
||||
assert.ok(await fetchOpencodeQuota(connectionId, { apiKey: "opencode-key" }));
|
||||
assert.equal(calls, 1);
|
||||
invalidateOpencodeQuotaCache("noauth");
|
||||
assert.ok(await fetchOpencodeQuota(connectionId, { apiKey: "opencode-key" }));
|
||||
assert.equal(calls, 1);
|
||||
invalidateOpencodeQuotaCache(connectionId);
|
||||
assert.ok(await fetchOpencodeQuota(connectionId, { apiKey: "opencode-key" }));
|
||||
assert.equal(calls, 2);
|
||||
} finally {
|
||||
globalThis.fetch = wrappedFetch;
|
||||
invalidateOpencodeQuotaCache(connectionId);
|
||||
}
|
||||
});
|
||||
|
||||
test("openrouter quota reads inside the proxy context", async () => {
|
||||
useAssignedProxy();
|
||||
const connectionId = `openrouter-proxy-${Date.now()}`;
|
||||
const contexts: boolean[] = [];
|
||||
const wrappedFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async (url: RequestInfo | URL) => {
|
||||
contexts.push(hasAmbientProxyContext());
|
||||
if (String(url).endsWith("/key")) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
data: { limit: 100, limit_remaining: 10, limit_reset: null, is_free_tier: false },
|
||||
}),
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
return new Response(JSON.stringify({ data: { total_credits: 100, total_usage: 90 } }), {
|
||||
status: 200,
|
||||
});
|
||||
}) as typeof globalThis.fetch;
|
||||
try {
|
||||
const quota = await fetchOpenrouterQuota(connectionId, { apiKey: "test-key" });
|
||||
assert.ok(quota);
|
||||
assert.ok(contexts.length === 2);
|
||||
assert.ok(contexts.every(Boolean));
|
||||
} finally {
|
||||
globalThis.fetch = wrappedFetch;
|
||||
invalidateOpenrouterQuotaCache(connectionId);
|
||||
}
|
||||
});
|
||||
|
||||
test("tavily quota reads inside the proxy context", async () => {
|
||||
useAssignedProxy();
|
||||
const connectionId = `tavily-proxy-${Date.now()}`;
|
||||
let observed: boolean | null = null;
|
||||
const wrappedFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async () => {
|
||||
observed = hasAmbientProxyContext();
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
account: { current_plan: "Researcher", plan_usage: 10, plan_limit: 1000 },
|
||||
}),
|
||||
{ status: 200 }
|
||||
);
|
||||
}) as typeof globalThis.fetch;
|
||||
try {
|
||||
const quota = await fetchTavilyQuota(connectionId, { apiKey: "mock-token" });
|
||||
assert.ok(quota);
|
||||
assert.equal(observed, true);
|
||||
} finally {
|
||||
globalThis.fetch = wrappedFetch;
|
||||
invalidateTavilyQuotaCache(connectionId);
|
||||
}
|
||||
});
|
||||
|
||||
test("v0 quota reads inside the proxy context", async () => {
|
||||
useAssignedProxy();
|
||||
const connectionId = `v0-proxy-${Date.now()}`;
|
||||
const contexts: boolean[] = [];
|
||||
const wrappedFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async (url: RequestInfo | URL) => {
|
||||
contexts.push(hasAmbientProxyContext());
|
||||
if (String(url).endsWith("/v1/user/billing")) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
billingType: "credit",
|
||||
data: { remaining: 20, limit: 100, reset: "2026-08-01T00:00:00.000Z" },
|
||||
}),
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
return new Response(JSON.stringify({ remaining: 900, limit: 1000 }), { status: 200 });
|
||||
}) as typeof globalThis.fetch;
|
||||
try {
|
||||
const quota = await fetchV0Quota(connectionId, { apiKey: "v0-key" });
|
||||
assert.ok(quota);
|
||||
assert.ok(contexts.length === 2);
|
||||
assert.ok(contexts.every(Boolean));
|
||||
} finally {
|
||||
globalThis.fetch = wrappedFetch;
|
||||
invalidateV0QuotaCache(connectionId);
|
||||
}
|
||||
});
|
||||
|
||||
test("every remaining quota fetcher routes through the connection proxy helper", async () => {
|
||||
const { readdirSync } = await import("node:fs");
|
||||
const { dirname } = await import("node:path");
|
||||
const { fileURLToPath } = await import("node:url");
|
||||
const here = dirname(fileURLToPath(import.meta.url));
|
||||
const dir = join(here, "..", "..", "open-sse", "services");
|
||||
const targets = [
|
||||
"jinaQuotaFetcher.ts",
|
||||
"llmgatewayQuotaFetcher.ts",
|
||||
"lyceumQuotaFetcher.ts",
|
||||
"moonshotQuotaFetcher.ts",
|
||||
"newApiAggregatorQuotaFetcher.ts",
|
||||
"opencodeQuotaFetcher.ts",
|
||||
"openrouterQuotaFetcher.ts",
|
||||
"tavilyQuotaFetcher.ts",
|
||||
"v0QuotaFetcher.ts",
|
||||
];
|
||||
const onDisk = new Set(readdirSync(dir));
|
||||
const offenders: string[] = [];
|
||||
for (const name of targets) {
|
||||
assert.ok(onDisk.has(name), `missing fetcher file ${name}`);
|
||||
const source = readFileSync(join(dir, name), "utf8");
|
||||
const direct = [...source.matchAll(/await fetch\(/g)].length;
|
||||
const helper = [...source.matchAll(/fetchWithConnectionProxy\(/g)].length;
|
||||
if (direct > 0 || helper < 1) offenders.push(`${name} (direct=${direct} helper=${helper})`);
|
||||
}
|
||||
assert.deepEqual(offenders, []);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user