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 { NextResponse } from "next/server";
import { getCachedSettings } from "@/lib/db/readCache";
import { getSettings, updateSettings } from "@/lib/db/settings";
import {
buildLegacyResilienceCompat,
mergeResilienceSettings,
pruneResilienceSettingsToProvided,
resolveResilienceSettings,
type ResilienceSettings,
type ResilienceSettingsPatch,
} from "@/lib/resilience/settings";
import { updateResilienceSchema } from "@/shared/validation/schemas";
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
import { resetAllCircuitBreakers } from "@/shared/utils/circuitBreaker";
import { sanitizeErrorMessage } from "@omniroute/open-sse/utils/error";
type JsonRecord = Record<string, unknown>;
function asRecord(value: unknown): JsonRecord {
return value && typeof value === "object" && !Array.isArray(value) ? (value as JsonRecord) : {};
}
function getErrorMessage(error: unknown, fallback: string): string {
return sanitizeErrorMessage(error) || fallback;
}
function normalizeLegacyPatch(body: JsonRecord): ResilienceSettingsPatch {
const profiles = asRecord(body.profiles);
const defaults = asRecord(body.defaults);
const oauth = asRecord(profiles.oauth);
const apikey = asRecord(profiles.apikey);
const patch: ResilienceSettingsPatch = {};
if (Object.keys(defaults).length > 0) {
patch.requestQueue = {
...(typeof defaults.requestsPerMinute === "number"
? { requestsPerMinute: defaults.requestsPerMinute }
: {}),
...(typeof defaults.minTimeBetweenRequests === "number"
? { minTimeBetweenRequestsMs: defaults.minTimeBetweenRequests }
: {}),
...(typeof defaults.concurrentRequests === "number"
? { concurrentRequests: defaults.concurrentRequests }
: {}),
};
}
if (Object.keys(oauth).length > 0 || Object.keys(apikey).length > 0) {
const buildLegacyCooldownPatch = (profile: JsonRecord) => {
const cooldownCandidates = [
typeof profile.transientCooldown === "number" ? profile.transientCooldown : null,
typeof profile.rateLimitCooldown === "number" && profile.rateLimitCooldown > 0
? profile.rateLimitCooldown
: null,
].filter((value): value is number => typeof value === "number");
return {
...(cooldownCandidates.length > 0
? { baseCooldownMs: Math.max(...cooldownCandidates) }
: {}),
...(typeof profile.rateLimitCooldown === "number"
? { useUpstreamRetryHints: profile.rateLimitCooldown === 0 }
: {}),
...(typeof profile.maxBackoffLevel === "number"
? { maxBackoffSteps: profile.maxBackoffLevel }
: {}),
};
};
patch.connectionCooldown = {
...(Object.keys(oauth).length > 0
? {
oauth: buildLegacyCooldownPatch(oauth),
}
: {}),
...(Object.keys(apikey).length > 0
? {
apikey: buildLegacyCooldownPatch(apikey),
}
: {}),
};
patch.providerBreaker = {
...(Object.keys(oauth).length > 0
? {
oauth: {
...(typeof oauth.circuitBreakerThreshold === "number"
? { failureThreshold: oauth.circuitBreakerThreshold }
: {}),
...(typeof oauth.circuitBreakerReset === "number"
? { resetTimeoutMs: oauth.circuitBreakerReset }
: {}),
},
}
: {}),
...(Object.keys(apikey).length > 0
? {
apikey: {
...(typeof apikey.circuitBreakerThreshold === "number"
? { failureThreshold: apikey.circuitBreakerThreshold }
: {}),
...(typeof apikey.circuitBreakerReset === "number"
? { resetTimeoutMs: apikey.circuitBreakerReset }
: {}),
},
}
: {}),
};
}
return patch;
}
async function syncRuntimeSettings(resilienceSettings: ResilienceSettings) {
const [{ applyRequestQueueSettings }, { setProviderQuotaOverrides }] = await Promise.all([
import("@omniroute/open-sse/services/rateLimitManager"),
import("@omniroute/open-sse/services/providerDefaultRateLimit"),
]);
await applyRequestQueueSettings(resilienceSettings.requestQueue);
// #6846 Phase 2: re-apply per-provider RPM/concurrency overrides on the hot
// path so a PATCH takes effect without a process restart. Mirrors the call in
// rateLimitManager.ts::initializeRateLimits() (startup).
setProviderQuotaOverrides(resilienceSettings.providerQuotaOverrides);
}
/**
* GET /api/resilience — Get current resilience configuration
*/
export async function GET() {
try {
const settings = await getCachedSettings();
const resilience = resolveResilienceSettings(settings);
return NextResponse.json({
requestQueue: resilience.requestQueue,
connectionCooldown: resilience.connectionCooldown,
providerBreaker: resilience.providerBreaker,
tokenRefreshBreaker: resilience.tokenRefreshBreaker,
waitForCooldown: {
enabled: resilience.waitForCooldown.enabled,
maxRetries: resilience.waitForCooldown.maxRetries,
maxRetryWaitSec: resilience.waitForCooldown.maxRetryWaitSec,
},
comboCooldownWait: resilience.comboCooldownWait,
quotaShareConcurrencyLimit: resilience.quotaShareConcurrencyLimit,
streamStallCooldown: resilience.streamStallCooldown,
streamRecovery: resilience.streamRecovery,
providerCooldown: resilience.providerCooldown,
quotaPreflight: resilience.quotaPreflight,
providerQuotaOverrides: resilience.providerQuotaOverrides,
credentialHealthCheck: resilience.credentialHealthCheck,
legacy: buildLegacyResilienceCompat(resilience),
});
} catch (err: unknown) {
console.error("[API] GET /api/resilience error:", err);
return NextResponse.json(
{ error: getErrorMessage(err, "Failed to load resilience settings") },
{ status: 500 }
);
}
}
/**
* PATCH /api/resilience — Update resilience configuration
*/
export async function PATCH(request) {
let rawBody;
try {
rawBody = await request.json();
} catch {
return NextResponse.json(
{
error: {
message: "Invalid request",
details: [{ field: "body", message: "Invalid JSON body" }],
},
},
{ status: 400 }
);
}
try {
const validation = validateBody(updateResilienceSchema, rawBody);
if (isValidationFailure(validation)) {
return NextResponse.json({ error: validation.error }, { status: 400 });
}
const body = validation.data as JsonRecord;
const currentSettings = await getSettings();
// Store only operator-provided values: merge the patch over the resolved
// view of the stored partial (legacy top-level keys included), then prune
// every leaf that still equals the fresh default so absent keys keep
// following the environment and flags on every read.
const storedResilience = asRecord(currentSettings.resilienceSettings);
const sparseBase = resolveResilienceSettings({
...currentSettings,
resilienceSettings: storedResilience,
});
const mergedFull = mergeResilienceSettings(sparseBase, {
...(body.requestQueue
? { requestQueue: body.requestQueue as ResilienceSettingsPatch["requestQueue"] }
: {}),
...(body.connectionCooldown
? {
connectionCooldown:
body.connectionCooldown as ResilienceSettingsPatch["connectionCooldown"],
}
: {}),
...(body.providerBreaker
? { providerBreaker: body.providerBreaker as ResilienceSettingsPatch["providerBreaker"] }
: {}),
...(body.tokenRefreshBreaker
? {
tokenRefreshBreaker:
body.tokenRefreshBreaker as ResilienceSettingsPatch["tokenRefreshBreaker"],
}
: {}),
...(body.waitForCooldown
? { waitForCooldown: body.waitForCooldown as ResilienceSettingsPatch["waitForCooldown"] }
: {}),
...(body.comboCooldownWait
? {
comboCooldownWait:
body.comboCooldownWait as ResilienceSettingsPatch["comboCooldownWait"],
}
: {}),
...(body.quotaShareConcurrencyLimit
? {
quotaShareConcurrencyLimit:
body.quotaShareConcurrencyLimit as ResilienceSettingsPatch["quotaShareConcurrencyLimit"],
}
: {}),
...(body.streamStallCooldown
? {
streamStallCooldown:
body.streamStallCooldown as ResilienceSettingsPatch["streamStallCooldown"],
}
: {}),
...(body.streamRecovery
? {
streamRecovery: body.streamRecovery as ResilienceSettingsPatch["streamRecovery"],
}
: {}),
...(body.providerCooldown
? {
providerCooldown: body.providerCooldown as ResilienceSettingsPatch["providerCooldown"],
}
: {}),
...(body.quotaPreflight
? { quotaPreflight: body.quotaPreflight as ResilienceSettingsPatch["quotaPreflight"] }
: {}),
...(body.providerQuotaOverrides
? {
providerQuotaOverrides:
body.providerQuotaOverrides as ResilienceSettingsPatch["providerQuotaOverrides"],
}
: {}),
...(body.credentialHealthCheck
? {
credentialHealthCheck:
body.credentialHealthCheck as ResilienceSettingsPatch["credentialHealthCheck"],
}
: {}),
...normalizeLegacyPatch(body),
});
const nextSparse = pruneResilienceSettingsToProvided(
mergedFull,
resolveResilienceSettings(null)
);
const nextResilience = resolveResilienceSettings({
...currentSettings,
resilienceSettings: nextSparse,
});
await updateSettings({
resilienceSettings: nextSparse,
requestRetry: nextResilience.waitForCooldown.maxRetries,
maxRetryIntervalSec: nextResilience.waitForCooldown.maxRetryWaitSec,
});
await syncRuntimeSettings(nextResilience);
// Issue #2100 follow-up: detect transitions in useUpstream429BreakerHints
// and reset breakers so the registry stops serving cached options.
// Compared on STORED override transition (boolean | undefined) so that
// `null` (PATCH input) → undefined (stored) is correctly detected as
// "unset request" when the previous stored value was a boolean.
const breakerHintsChanged =
sparseBase.connectionCooldown.oauth.useUpstream429BreakerHints !==
nextResilience.connectionCooldown.oauth.useUpstream429BreakerHints ||
sparseBase.connectionCooldown.apikey.useUpstream429BreakerHints !==
nextResilience.connectionCooldown.apikey.useUpstream429BreakerHints;
if (breakerHintsChanged) {
resetAllCircuitBreakers();
}
return NextResponse.json({
ok: true,
requestQueue: nextResilience.requestQueue,
connectionCooldown: nextResilience.connectionCooldown,
providerBreaker: nextResilience.providerBreaker,
tokenRefreshBreaker: nextResilience.tokenRefreshBreaker,
waitForCooldown: {
enabled: nextResilience.waitForCooldown.enabled,
maxRetries: nextResilience.waitForCooldown.maxRetries,
maxRetryWaitSec: nextResilience.waitForCooldown.maxRetryWaitSec,
},
comboCooldownWait: nextResilience.comboCooldownWait,
quotaShareConcurrencyLimit: nextResilience.quotaShareConcurrencyLimit,
streamStallCooldown: nextResilience.streamStallCooldown,
streamRecovery: nextResilience.streamRecovery,
providerCooldown: nextResilience.providerCooldown,
quotaPreflight: nextResilience.quotaPreflight,
providerQuotaOverrides: nextResilience.providerQuotaOverrides,
credentialHealthCheck: nextResilience.credentialHealthCheck,
legacy: buildLegacyResilienceCompat(nextResilience),
});
} catch (err: unknown) {
console.error("[API] PATCH /api/resilience error:", err);
return NextResponse.json(
{ error: getErrorMessage(err, "Failed to save resilience settings") },
{ status: 500 }
);
}
}