Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
fix: address reset-aware follow-up feedback
Jan Leone0613e660055
diff --git a/open-sse/services/combo.ts b/open-sse/services/combo.tsindex 4f17162dc5..e413029366 100644--- a/open-sse/services/combo.ts+++ b/open-sse/services/combo.ts@@ -92,6 +92,8 @@ const RESET_AWARE_SESSION_WINDOW_MS = 5 * 60 * 60 * 1000;const RESET_AWARE_WEEKLY_WINDOW_MS = 7 * 24 * 60 * 60 * 1000;const RESET_AWARE_REMAINING_WEIGHT = 0.55;const RESET_AWARE_RESET_WEIGHT = 0.45;+const RESET_AWARE_CONNECTION_CACHE_TTL_MS = 30_000;+const RESET_AWARE_QUOTA_FETCH_CONCURRENCY = 5;const RESET_AWARE_DEFAULTS = {sessionWeight: 0.35,weeklyWeight: 0.65,@@ -222,6 +224,11 @@ export async function validateResponseQuality(// Resets on server restart (by design — no stale state)const rrCounters = new Map();+const resetAwareConnectionCache = new Map<+ string,+ { fetchedAt: number; connections: Array<Record<string, unknown>> }+>();+/*** Normalize a model entry to { model, weight }* Supports both legacy string format and new object format@@ -762,6 +769,23 @@ function getResetAwareProvider(target: ResolvedComboTarget): string | null {return provider || null;}+function normalizeResetAt(value: unknown): string | null {+ if (typeof value === "string" && value.trim().length > 0) return value.trim();+ if (typeof value === "number" && Number.isFinite(value)) return String(value);+ return null;+}++function parseResetTimeMs(resetAt: string | null | undefined): number {+ if (!resetAt) return NaN;+ const resetTime = Date.parse(resetAt);+ if (Number.isFinite(resetTime)) return resetTime;++ if (!/^\d+(?:\.\d+)?$/.test(resetAt)) return NaN;+ const numericResetAt = Number(resetAt);+ if (!Number.isFinite(numericResetAt)) return NaN;+ return numericResetAt < 10_000_000_000 ? numericResetAt * 1000 : numericResetAt;+}+function getQuotaWindow(quota: unknown,key: "window5h" | "window7d" | "windowWeekly" | "windowMonthly"@@ -770,14 +794,13 @@ function getQuotaWindow(const window = quota[key];if (!isRecord(window)) return null;const percentUsed = finiteNumberOrNull(window.percentUsed);- const resetAt =- typeof window.resetAt === "string" && window.resetAt.length > 0 ? window.resetAt : null;+ const resetAt = normalizeResetAt(window.resetAt);return { percentUsed, resetAt };}function getResetUrgency(resetAt: string | null | undefined, windowMs: number): number {if (!resetAt) return 0.5;- const resetTime = resetAt ? new Date(resetAt).getTime() : NaN;+ const resetTime = parseResetTimeMs(resetAt);if (!Number.isFinite(resetTime)) return 0.5;const msUntilReset = resetTime - Date.now();if (msUntilReset <= 0) return 1;@@ -811,7 +834,7 @@ function scoreResetAwareQuota(quota: unknown, config: ReturnType<typeof resolveR);const weeklyScore = scoreQuotaWindow(weeklyRemaining,- weeklyWindow?.resetAt ?? (typeof quota.resetAt === "string" ? quota.resetAt : null),+ weeklyWindow?.resetAt ?? normalizeResetAt(quota.resetAt),RESET_AWARE_WEEKLY_WINDOW_MS);let score = config.sessionWeight * sessionScore + config.weeklyWeight * weeklyScore;@@ -832,12 +855,22 @@ async function getQuotaAwareConnectionsForTarget(const provider = getResetAwareProvider(target);if (!provider || !getQuotaFetcher(provider)) return [];if (!connectionCache.has(provider)) {+ const cached = resetAwareConnectionCache.get(provider);+ if (cached && Date.now() - cached.fetchedAt < RESET_AWARE_CONNECTION_CACHE_TTL_MS) {+ connectionCache.set(provider, cached.connections);+ return cached.connections;+ }+try {const connections = await getProviderConnections({ provider, isActive: true });- connectionCache.set(- provider,- Array.isArray(connections) ? (connections as Array<Record<string, unknown>>) : []- );+ const activeConnections = Array.isArray(connections)+ ? (connections as Array<Record<string, unknown>>)+ : [];+ connectionCache.set(provider, activeConnections);+ resetAwareConnectionCache.set(provider, {+ connections: activeConnections,+ fetchedAt: Date.now(),+ });} catch (error) {log.warn?.("COMBO", "Reset-aware failed to load quota-aware connections.", {comboName,@@ -872,27 +905,45 @@ function filterAllowedConnectionIds(function getTargetConnectionIds(target: ResolvedComboTarget,- connections: Array<Record<string, unknown>>,- apiKeyAllowedConnectionIds?: string[] | null+ connections: Array<Record<string, unknown>>): string[] {let connectionIds: string[];if (target.connectionId) {- connectionIds = [target.connectionId];- return filterAllowedConnectionIds(connectionIds, apiKeyAllowedConnectionIds);+ return [target.connectionId];}if (Array.isArray(target.allowedConnectionIds) && target.allowedConnectionIds.length > 0) {- connectionIds = target.allowedConnectionIds.filter(+ return target.allowedConnectionIds.filter((connectionId): connectionId is string =>typeof connectionId === "string" && connectionId.trim().length > 0);- return filterAllowedConnectionIds(connectionIds, apiKeyAllowedConnectionIds);}connectionIds = connections.map((connection) => (typeof connection.id === "string" ? connection.id : null)).filter((connectionId): connectionId is string => !!connectionId);- return filterAllowedConnectionIds(connectionIds, apiKeyAllowedConnectionIds);+ return connectionIds;+}++async function mapWithConcurrency<T, R>(+ items: T[],+ concurrency: number,+ mapper: (item: T, index: number) => Promise<R>+): Promise<R[]> {+ const results = new Array<R>(items.length);+ let nextIndex = 0;+ const workerCount = Math.max(1, Math.min(concurrency, items.length));++ await Promise.all(+ Array.from({ length: workerCount }, async () => {+ while (nextIndex < items.length) {+ const currentIndex = nextIndex++;+ results[currentIndex] = await mapper(items[currentIndex], currentIndex);+ }+ })+ );++ return results;}async function orderTargetsByResetAwareQuota(@@ -949,8 +1000,10 @@ async function orderTargetsByResetAwareQuota(}}- const scoredTargets = await Promise.all(- expandedTargets.map(async (target, index) => {+ const scoredTargets = await mapWithConcurrency(+ expandedTargets,+ RESET_AWARE_QUOTA_FETCH_CONCURRENCY,+ async (target, index) => {let quota: unknown = null;const provider = getResetAwareProvider(target);const fetcher = provider ? getQuotaFetcher(provider) : null;@@ -969,7 +1022,7 @@ async function orderTargetsByResetAwareQuota(}const { score } = scoreResetAwareQuota(quota, config);return { target, score, index };- })+ });scoredTargets.sort((a, b) => {diff --git a/open-sse/services/usage.ts b/open-sse/services/usage.tsindex a7f01440f1..aa96d3afd0 100644--- a/open-sse/services/usage.ts+++ b/open-sse/services/usage.ts@@ -532,7 +532,7 @@ function getGlmQuotaLabel(type: unknown): string | null {async function getGlmUsage(apiKey: string, providerSpecificData?: Record<string, unknown>) {if (!apiKey) {- return { message: "Z.AI API key not available. Add a coding plan API key to view usage." };+ return { message: "API key not available. Add a coding plan API key to view usage." };}const quotaUrl = getGlmQuotaUrl(providerSpecificData);diff --git a/tests/unit/combo-strategies.test.ts b/tests/unit/combo-strategies.test.tsindex 6909faebff..2937ee3595 100644--- a/tests/unit/combo-strategies.test.ts+++ b/tests/unit/combo-strategies.test.ts@@ -426,6 +426,36 @@ test("reset-aware strategy respects API-key allowed connections during expansionassert.deepEqual(fetchedConnectionIds, [allowedId]);});+test("reset-aware strategy parses numeric reset timestamps from quota telemetry", async () => {+ const provider = `timestamp-provider-${randomUUID()}`;+ const soon = `timestamp-soon-${randomUUID()}`;+ const later = `timestamp-later-${randomUUID()}`;+ const soonResetSeconds = Math.floor((Date.now() + 24 * 3600 * 1000) / 1000);+ const laterResetMs = Date.now() + 5 * 24 * 3600 * 1000;++ registerQuotaFetcher(provider, async (connectionId) => ({+ used: connectionId === soon ? 40 : 20,+ total: 100,+ percentUsed: connectionId === soon ? 0.4 : 0.2,+ resetAt: connectionId === soon ? soonResetSeconds : laterResetMs,+ }));++ const combo = {+ name: `reset-aware-timestamps-${randomUUID()}`,+ strategy: "reset-aware",+ models: [soon, later].map((connectionId, index) => ({+ kind: "model",+ provider,+ providerId: provider,+ model: "balanced-model",+ connectionId,+ id: `timestamp-${index}`,+ })),+ };++ assert.equal(await selectedConnectionFor(combo), soon);+});+test("reset-aware strategy scores provider-specific weekly windows when available", async () => {const provider = `weekly-provider-${randomUUID()}`;const soon = `weekly-soon-${randomUUID()}`;diff --git a/tests/unit/usage-service-hardening.test.ts b/tests/unit/usage-service-hardening.test.tsindex 442dc83f0e..d41eaa8644 100644--- a/tests/unit/usage-service-hardening.test.ts+++ b/tests/unit/usage-service-hardening.test.ts@@ -883,6 +883,15 @@ test("usage service covers Qwen, Qoder, GLM, Z.AI and GLMT branches", async () =});assert.match(qoder.message, /Usage tracked per request/i);+ const glmMissingKey: any = await usageService.getUsageForProvider({+ provider: "glm",+ apiKey: "",+ });+ assert.equal(+ glmMissingKey.message,+ "API key not available. Add a coding plan API key to view usage."+ );+globalThis.fetch = async (url, init = {}) => {if (String(url).includes("/api/monitor/usage/quota/limit")) {assert.equal((init as any).headers.Authorization, "Bearer glm-key");