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
import { isEgressBucketedLockScope } from "@omniroute/open-sse/config/providerErrorRules.ts";
import {
hasProxyRefusals,
noteProxyRecovered,
noteProxyRefusal,
noteProxyServed,
proxyEgressKey,
} from "@omniroute/open-sse/utils/proxyRefusalMemory.ts";
import { isProxySkipRecentlyFailedEnabled } from "@/shared/utils/featureFlags";
import { maybeSwitchOnSetAside } from "@/lib/proxySubscription/selectorTrigger";
/**
* Feed the outcome the provider actually returned through a proxy (captured around the
* patched fetch and carried on proxyInfo.upstreamStatus) back to proxy selection. Opt-in:
* only with PROXY_SKIP_RECENTLY_FAILED on does a refusal received through a member set it
* aside for the existing cooldown; a success through such a member clears it. A success
* from another provider proves the proxy is reachable but says nothing about that refusal
* (a global pool is shared across providers), so it only ends an unreachable period. No
* outcome (local refusal, network error), an edge relay (the outcome is the relay's, and
* its key is null) or a direct request writes nothing. Never reads result.status: several
* failures are generated locally.
*
* Synchronous on purpose: the caller runs it as soon as the upstream status is known, so
* a request picking from the pool right after already sees the member set aside. The flag
* is read only for a refusal (a write); a success only clears what an earlier opt-in wrote.
*/
export function noteProxyOutcome(
provider: string | null,
proxyInfo: { proxy?: unknown; upstreamStatus?: number | null } | null | undefined
): void {
const status = proxyInfo?.upstreamStatus;
if (typeof status !== "number") return;
const inRefusalScope = isEgressBucketedLockScope(provider);
if (status === 429 && inRefusalScope) {
const key = proxyEgressKey(proxyInfo?.proxy);
if (key === null || !isProxySkipRecentlyFailedEnabled()) return;
const period = noteProxyRefusal(key, "ip_quota_429");
if (period === null) return;
// Steer the local core selector off the set-aside member. Fire-and-
// forget, off the request path, never blocking, never throwing: the callee
// owns a defensive catch, this one guards the sync caller.
void maybeSwitchOnSetAside(key).catch((e) => {
console.warn(`[SelectorControl] trigger failed: ${e instanceof Error ? e.message : e}`);
});
return;
}
if (status < 200 || status >= 300 || !hasProxyRefusals()) return;
const key = proxyEgressKey(proxyInfo?.proxy);
if (inRefusalScope) noteProxyServed(key);
else noteProxyRecovered(key, "proxy_unreachable");
}
// The transport cross-evidence decision lives next to its store in open-sse (the
// proxy dispatcher calls it without reaching into src/sse); re-exported here so
// the outcome feedback for proxies stays discoverable from one place.
export { noteTransportOutcome } from "@omniroute/open-sse/utils/proxyTransportOutcome.ts";