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(combo): guard target.modelStr against non-string before .startsWith (#2359)
diegosouzapwf2e368830a5c
diff --git a/open-sse/services/combo.ts b/open-sse/services/combo.tsindex 40fd49c3a4..70481b0eab 100644--- a/open-sse/services/combo.ts+++ b/open-sse/services/combo.ts@@ -1840,7 +1840,17 @@ export async function handleComboChat({if (lkgpIndex < 0) {lkgpIndex = orderedTargets.findIndex((target) =>- target.provider === providerName || target.modelStr.startsWith(`${providerName}/`)+ target.provider === providerName ||+ // Issue #2359: Defensive guard. The `target.modelStr` type+ // annotation is `string`, but malformed combo entries (e.g.,+ // local-provider rows whose `modelStr` failed to resolve when+ // the executor catalogue was being rebuilt) have leaked+ // through and surfaced as `e.startsWith is not a function`+ // 500s on combo test/dispatch. The fast path stays+ // unchanged for the common case; this only avoids the+ // crash when the field is unexpectedly non-string.+ (typeof target.modelStr === "string" &&+ target.modelStr.startsWith(`${providerName}/`)));}diff --git a/src/app/api/combos/test/route.ts b/src/app/api/combos/test/route.tsindex 7aa613e519..7b37cfcd86 100644--- a/src/app/api/combos/test/route.ts+++ b/src/app/api/combos/test/route.ts@@ -35,12 +35,25 @@ function buildComboTestResult(target, partial = {}) {async function testComboTarget(target, baseInternalUrl, internalApiKey: string | null) {const startTime = Date.now();try {+ // Issue #2359: combo entries with a malformed/missing modelStr surfaced+ // as `e.startsWith is not a function` / similar TypeError 500s. Coerce+ // defensively at the boundary so the test path returns a clean error+ // instead of crashing the request handler.+ const modelStr = typeof target?.modelStr === "string" ? target.modelStr : "";+ if (!modelStr) {+ return buildComboTestResult(target, {+ status: "error",+ error: "Combo step is missing a model id (modelStr). Re-save the combo to refresh it.",+ latencyMs: 0,+ });+ }+ const modelLower = modelStr.toLowerCase();const isEmbedding =- target.modelStr.toLowerCase().includes("embedding") ||- target.modelStr.toLowerCase().includes("bge-") ||- target.modelStr.toLowerCase().includes("text-embed");+ modelLower.includes("embedding") ||+ modelLower.includes("bge-") ||+ modelLower.includes("text-embed");const internalUrl = `${baseInternalUrl}/v1/${isEmbedding ? "embeddings" : "chat/completions"}`;- const testBody = buildComboTestRequestBody(target.modelStr, isEmbedding);+ const testBody = buildComboTestRequestBody(modelStr, isEmbedding);const controller = new AbortController();const timeout = setTimeout(() => controller.abort(), 20000);diff --git a/tests/unit/combo-target-defensive-modelstr.test.ts b/tests/unit/combo-target-defensive-modelstr.test.tsnew file mode 100644index 0000000000..f26b86c8cc--- /dev/null+++ b/tests/unit/combo-target-defensive-modelstr.test.ts@@ -0,0 +1,59 @@+/**+ * Issue #2359 — defensive null/non-string guards on `target.modelStr`.+ *+ * Combo entries with a malformed `modelStr` (regression after #2338 added+ * per-account LKGP routing) were crashing the combo dispatch path with+ * `TypeError: e.startsWith is not a function`. Add guards at the two+ * boundaries that consume `target.modelStr` and make sure neither throws+ * on a missing/non-string value.+ */+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 COMBO_SRC = path.resolve(__dirname, "../../open-sse/services/combo.ts");+const TEST_ROUTE_SRC = path.resolve(__dirname, "../../src/app/api/combos/test/route.ts");++test("#2359 combo.ts LKGP findIndex guards modelStr against non-string", () => {+ const src = fs.readFileSync(COMBO_SRC, "utf8");+ // The findIndex on orderedTargets must check `typeof target.modelStr === "string"`+ // before calling .startsWith. Anchor on the LKGP fallback branch.+ assert.ok(+ /typeof target\.modelStr === "string"[\s\S]{0,80}target\.modelStr\.startsWith/.test(src),+ "LKGP fallback in combo.ts must type-check target.modelStr before calling .startsWith"+ );+});++test("#2359 combo test route falls back instead of throwing on missing modelStr", () => {+ const src = fs.readFileSync(TEST_ROUTE_SRC, "utf8");+ // We expect the coerced local `modelStr` binding and a graceful early+ // return when the combo step is malformed.+ assert.ok(+ /typeof target\?\.modelStr === "string"/.test(src),+ "testComboTarget must coerce target.modelStr before lowercasing"+ );+ assert.ok(+ /Combo step is missing a model id/i.test(src),+ "testComboTarget must surface a helpful error on missing modelStr"+ );+});++test("#2359 combo.ts has no remaining unguarded target.modelStr.<method> usages", () => {+ const src = fs.readFileSync(COMBO_SRC, "utf8");+ // Strip the line that contains the guard so the regex below only catches+ // direct, unguarded method calls.+ const stripped = src.replace(/typeof target\.modelStr === "string"[^\n]*\n[^\n]*/g, "");++ // Any `target.modelStr.<method>(` call that survives the strip means+ // there's still a code path that could explode on a non-string value.+ const RE =+ /target\.modelStr\.(?:startsWith|endsWith|includes|toLowerCase|toUpperCase|slice|trim)\b/;+ assert.ok(+ !RE.test(stripped),+ `Found an unguarded target.modelStr.<method> call. ` ++ `Audit the LKGP / sortTargets paths and add a typeof guard before calling string methods.`+ );+});