Skip to content

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

fix(auto-routing): replace bare getSettings() with getCachedSettings (#2346)

diegosouzapw5da9b1b778cf
diff --git a/src/sse/handlers/chat.ts b/src/sse/handlers/chat.ts
index dc8b473e69..60430920fc 100644
--- a/src/sse/handlers/chat.ts
+++ b/src/sse/handlers/chat.ts
@@ -314,8 +314,12 @@ export async function handleChat(request: any, clientRawRequest: any = null) {
let autoVariant: AutoVariant | undefined;
let isAutoRouting = resolvedModelStr === "auto" || resolvedModelStr.startsWith("auto/");
if (isAutoRouting) {
- // C2: Enforce autoRoutingEnabled setting
- const settings = await getSettings();
+ // C2: Enforce autoRoutingEnabled setting.
+ // Issue #2346: `getSettings` was never imported in this module; only
+ // `getCachedSettings` is. Calling the bare name caused a ReferenceError
+ // on every auto-routed request. The cached variant has the same shape
+ // and benefits the auto-routing hot path.
+ const settings = await getCachedSettings().catch(() => ({}) as Record<string, unknown>);
if (settings?.autoRoutingEnabled === false) {
return errorResponse(
HTTP_STATUS.BAD_REQUEST,
diff --git a/tests/unit/auto-routing-settings-resolve.test.ts b/tests/unit/auto-routing-settings-resolve.test.ts
new file mode 100644
index 0000000000..b3f15e5e54
--- /dev/null
+++ b/tests/unit/auto-routing-settings-resolve.test.ts
@@ -0,0 +1,39 @@
+/**
+ * Issue #2346 — Regression guard: src/sse/handlers/chat.ts used the bare name
+ * `getSettings` without importing it, causing a ReferenceError on every
+ * `auto` / `auto/*` request. Make sure the module loads cleanly AND that any
+ * `await getSettings()` invocation in the source uses an imported binding
+ * (not a global that will resolve to undefined at runtime).
+ */
+import test from "node:test";
+import assert from "node:assert/strict";
+import fs from "node:fs";
+import path from "node:path";
+import { fileURLToPath } from "node:url";
+
+const __dirname = path.dirname(fileURLToPath(import.meta.url));
+const CHAT_PATH = path.resolve(__dirname, "../../src/sse/handlers/chat.ts");
+
+test("#2346 chat.ts does not call bare getSettings() without importing it", () => {
+ const source = fs.readFileSync(CHAT_PATH, "utf8");
+
+ // Strip comment lines so doc references like "Issue #2346: `getSettings`..."
+ // don't trip the regex.
+ const stripped = source.replace(/\/\*[\s\S]*?\*\//g, "").replace(/^\s*\/\/.*$/gm, "");
+
+ // Find any call to a bare `getSettings(` identifier (not preceded by . or `).
+ const callRe = /(?<![.\w`])getSettings\s*\(/g;
+ const hasCall = callRe.test(stripped);
+
+ if (!hasCall) {
+ // No call at all — also fine.
+ return;
+ }
+
+ // If there's a call, make sure `getSettings` was imported (named import).
+ const importsGetSettings = /import\s*\{[^}]*\bgetSettings\b[^}]*\}/.test(stripped);
+ assert.ok(
+ importsGetSettings,
+ "chat.ts calls getSettings() but never imports it — this is the regression that caused #2346"
+ );
+});