diff --git a/changelog.d/fixes/8326-compatible-id-regex.md b/changelog.d/fixes/8326-compatible-id-regex.md new file mode 100644 index 0000000000..a637f4cef5 --- /dev/null +++ b/changelog.d/fixes/8326-compatible-id-regex.md @@ -0,0 +1 @@ +- fix(api): accept the current compatible-provider connection id scheme in the models test route (#8326) diff --git a/src/app/api/v1/providers/[provider]/models/route.ts b/src/app/api/v1/providers/[provider]/models/route.ts index 3618c782ab..9e5450be90 100644 --- a/src/app/api/v1/providers/[provider]/models/route.ts +++ b/src/app/api/v1/providers/[provider]/models/route.ts @@ -3,6 +3,7 @@ import { getServiceModels } from "@/lib/db/serviceModels"; import { isServiceBackendPluginId } from "@/lib/services/serviceBackends"; import { getRegistryEntry } from "@omniroute/open-sse/config/providerRegistry.ts"; import { getProviderById, getProviderByAlias } from "@/shared/constants/providers"; +import { isCompatibleProviderConnectionId } from "@/shared/utils/compatibleProviderId"; /** * Handle CORS preflight @@ -51,9 +52,7 @@ export async function GET(request: Request, { params }: { params: Promise<{ prov providerAlias = catalogEntry.alias || providerId; } else { // Allow fetching models by connection ID for compatible providers - const isCompatibleConnectionId = /^(openai|anthropic)-compatible-chat-[a-f0-9-]+$/.test( - rawProvider - ); + const isCompatibleConnectionId = isCompatibleProviderConnectionId(rawProvider); if (!isCompatibleConnectionId) { return Response.json( { diff --git a/src/lib/display/names.ts b/src/lib/display/names.ts index b06a650bba..522c725756 100644 --- a/src/lib/display/names.ts +++ b/src/lib/display/names.ts @@ -12,6 +12,13 @@ * @module lib/display/names */ +import { isCompatibleProviderConnectionId } from "@/shared/utils/compatibleProviderId"; +import { + isClaudeCodeCompatibleProvider, + isOpenAICompatibleProvider, + isAnthropicCompatibleProvider, +} from "@/shared/constants/providers"; + export interface ConnectionLike { id?: string | null; name?: string | null; @@ -59,14 +66,11 @@ export function getProviderDisplayName( if (providerNode?.prefix?.trim()) return providerNode.prefix.trim(); if (!providerId) return "Unknown Provider"; - // Simplify dynamic compatible provider IDs - const match = providerId.match( - /^(openai|anthropic)-compatible-(?:chat|responses)-[0-9a-f-]{10,}$/i - ); - if (match) return `Compatible (${match[1]})`; - - if (/^anthropic-compatible-cc-[0-9a-f-]{10,}$/i.test(providerId)) { - return "CC Compatible"; + // Simplify dynamic compatible provider IDs (all 4 generated shapes — #8326) + if (isCompatibleProviderConnectionId(providerId)) { + if (isClaudeCodeCompatibleProvider(providerId)) return "CC Compatible"; + if (isOpenAICompatibleProvider(providerId)) return "Compatible (openai)"; + if (isAnthropicCompatibleProvider(providerId)) return "Compatible (anthropic)"; } return providerId; diff --git a/src/shared/utils/compatibleProviderId.ts b/src/shared/utils/compatibleProviderId.ts new file mode 100644 index 0000000000..cd421a2bd2 --- /dev/null +++ b/src/shared/utils/compatibleProviderId.ts @@ -0,0 +1,36 @@ +/** + * Single source of truth for recognizing a "compatible provider" connection + * ID — the dynamic IDs generated for openai-compatible / anthropic-compatible + * custom nodes (src/app/api/provider-nodes/route.ts). + * + * Generated shapes (all four must match): + * - openai-compatible-chat- + * - openai-compatible-responses- + * - anthropic-compatible- + * - anthropic-compatible-cc- + * + * Built from the same prefix constants used at ID-generation time + * (src/shared/constants/providers.ts) so the generator and the validator can + * never drift apart again. See #8326: the previous inline regex required a + * literal "-chat-" segment, rejecting 3 of the 4 shapes the system actually + * generates. + * + * @module shared/utils/compatibleProviderId + */ + +import { OPENAI_COMPATIBLE_PREFIX, ANTHROPIC_COMPATIBLE_PREFIX } from "@/shared/constants/providers"; + +const COMPATIBLE_PROVIDER_ID_PATTERN = new RegExp( + `^(?:${OPENAI_COMPATIBLE_PREFIX}(?:chat|responses)-|${ANTHROPIC_COMPATIBLE_PREFIX}(?:cc-)?)[0-9a-f-]+$`, + "i" +); + +/** + * True when `providerId` matches one of the four generated compatible- + * provider connection ID shapes. Rejects plain built-in provider IDs (e.g. + * "openai", "anthropic") and unrelated look-alikes (e.g. + * "custom-compatible-chat-..."). + */ +export function isCompatibleProviderConnectionId(providerId: string | null | undefined): boolean { + return typeof providerId === "string" && COMPATIBLE_PROVIDER_ID_PATTERN.test(providerId); +} diff --git a/tests/unit/8326-compatible-id-regex.test.ts b/tests/unit/8326-compatible-id-regex.test.ts new file mode 100644 index 0000000000..35901f861b --- /dev/null +++ b/tests/unit/8326-compatible-id-regex.test.ts @@ -0,0 +1,123 @@ +/** + * Regression test for #8326 — the "compatible provider" connection id + * validator/matcher required a literal "-chat-" segment, so it rejected 3 + * of the 4 id shapes actually generated by + * src/app/api/provider-nodes/route.ts: + * - openai-compatible-chat- (accepted before the fix — control) + * - openai-compatible-responses- (rejected before the fix) + * - anthropic-compatible- (rejected before the fix) + * - anthropic-compatible-cc- (rejected before the fix) + * + * Covers both call sites that shared the drifted regexes: + * - GET /v1/providers/:provider/models (src/app/api/v1/providers/[provider]/models/route.ts) + * - getProviderDisplayName (src/lib/display/names.ts) + * and the new shared matcher itself (src/shared/utils/compatibleProviderId.ts), + * including the landmine check that widening the matcher must NOT start + * matching plain built-in provider ids like "openai" / "anthropic". + * + * Run: node --import tsx/esm --test tests/unit/8326-compatible-id-regex.test.ts + */ + +import test from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; + +const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-8326-")); +process.env.DATA_DIR = TEST_DATA_DIR; + +const core = await import("../../src/lib/db/core.ts"); +const routeModule = await import("../../src/app/api/v1/providers/[provider]/models/route.ts"); +const { isCompatibleProviderConnectionId } = await import( + "../../src/shared/utils/compatibleProviderId.ts" +); +const { getProviderDisplayName } = await import("../../src/lib/display/names.ts"); + +function makeRequest(provider: string) { + return new Request(`http://localhost/api/v1/providers/${encodeURIComponent(provider)}/models`); +} + +async function callGET(provider: string) { + return routeModule.GET(makeRequest(provider), { + params: Promise.resolve({ provider }), + }); +} + +test.beforeEach(() => { + core.resetDbInstance(); +}); + +test.after(() => { + core.resetDbInstance(); + fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true }); +}); + +const UUID = "02669115-2545-4896-b003-cb4dac09d441"; +const GENERATED_SHAPES = [ + ["openai-compatible-chat-" + UUID, "openai/chat (control — worked before the fix)"], + ["openai-compatible-responses-" + UUID, "openai/responses"], + ["anthropic-compatible-" + UUID, "anthropic (segment-less)"], + ["anthropic-compatible-cc-" + UUID, "anthropic/cc"], +] as const; + +test("isCompatibleProviderConnectionId matches all 4 shapes actually generated by provider-nodes/route.ts", () => { + for (const [id, label] of GENERATED_SHAPES) { + assert.equal(isCompatibleProviderConnectionId(id), true, `expected match for ${label}: ${id}`); + } +}); + +test("isCompatibleProviderConnectionId does NOT widen to match plain built-in provider ids", () => { + for (const id of ["openai", "anthropic", "gemini", "openrouter"]) { + assert.equal( + isCompatibleProviderConnectionId(id), + false, + `must not match plain built-in provider id: ${id}` + ); + } +}); + +test("isCompatibleProviderConnectionId does NOT match unrelated look-alike prefixes", () => { + assert.equal(isCompatibleProviderConnectionId("custom-compatible-chat-" + UUID), false); + assert.equal(isCompatibleProviderConnectionId(null), false); + assert.equal(isCompatibleProviderConnectionId(undefined), false); +}); + +test("GET /v1/providers/:provider/models accepts all 4 generated compatible connection id shapes", async () => { + for (const [id, label] of GENERATED_SHAPES) { + const res = await callGET(id); + if (res.status === 400) { + const body = await res.json(); + assert.notEqual( + body.error?.code, + "invalid_provider", + `${label} (${id}) must bypass the unknown-provider 400 gate` + ); + } + } +}); + +test("GET /v1/providers/:provider/models still rejects unrelated look-alike prefixes", async () => { + const res = await callGET("custom-compatible-chat-" + UUID); + assert.equal(res.status, 400); + const body = await res.json(); + assert.equal(body.error?.code, "invalid_provider"); +}); + +test("getProviderDisplayName simplifies all 4 generated compatible id shapes", () => { + assert.equal( + getProviderDisplayName("openai-compatible-chat-" + UUID), + "Compatible (openai)" + ); + assert.equal( + getProviderDisplayName("openai-compatible-responses-" + UUID), + "Compatible (openai)" + ); + assert.equal(getProviderDisplayName("anthropic-compatible-" + UUID), "Compatible (anthropic)"); + assert.equal(getProviderDisplayName("anthropic-compatible-cc-" + UUID), "CC Compatible"); +}); + +test("getProviderDisplayName does not simplify plain built-in provider ids", () => { + assert.equal(getProviderDisplayName("openai"), "openai"); + assert.equal(getProviderDisplayName("anthropic"), "anthropic"); +}); diff --git a/tests/unit/display-and-error-utils.test.ts b/tests/unit/display-and-error-utils.test.ts index b97ec91bbc..e7236d6191 100644 --- a/tests/unit/display-and-error-utils.test.ts +++ b/tests/unit/display-and-error-utils.test.ts @@ -237,7 +237,7 @@ test("getProviderDisplayName: prefers node metadata and simplifies compatible ID "Friendly Node" ); assert.equal( - getProviderDisplayName("anthropic-compatible-responses-02669115-2545-4896-b003-cb4dac09d441", { + getProviderDisplayName("anthropic-compatible-02669115-2545-4896-b003-cb4dac09d441", { name: " ", prefix: "Anthropic Prefix", }), @@ -248,9 +248,19 @@ test("getProviderDisplayName: prefers node metadata and simplifies compatible ID "Compatible (openai)" ); assert.equal( - getProviderDisplayName("anthropic-compatible-responses-02669115-2545-4896-b003-cb4dac09d441"), + getProviderDisplayName("openai-compatible-responses-02669115-2545-4896-b003-cb4dac09d441"), + "Compatible (openai)" + ); + // Segment-less anthropic-compatible ids (the actual generated shape, #8326) now + // resolve to a friendly label too, instead of falling through to the raw ID. + assert.equal( + getProviderDisplayName("anthropic-compatible-02669115-2545-4896-b003-cb4dac09d441"), "Compatible (anthropic)" ); + assert.equal( + getProviderDisplayName("anthropic-compatible-cc-02669115-2545-4896-b003-cb4dac09d441"), + "CC Compatible" + ); assert.equal(getProviderDisplayName(undefined), "Unknown Provider"); assert.equal(getProviderDisplayName("plain-provider-id"), "plain-provider-id"); }); diff --git a/tests/unit/provider-models-v1-route.test.ts b/tests/unit/provider-models-v1-route.test.ts index 4e92af04cf..c20eda3544 100644 --- a/tests/unit/provider-models-v1-route.test.ts +++ b/tests/unit/provider-models-v1-route.test.ts @@ -64,7 +64,27 @@ test("GET /v1/providers/:provider/models accepts openai-compatible connection ID }); test("GET /v1/providers/:provider/models accepts anthropic-compatible connection ID format", async () => { - const connectionId = "anthropic-compatible-chat-deadbeef-0000-0000-0000-000000000000"; + // anthropic-compatible ids never embed a "-chat-" segment (#8326) — this is + // the actual shape generated by src/app/api/provider-nodes/route.ts. + const connectionId = "anthropic-compatible-deadbeef-0000-0000-0000-000000000000"; + const res = await callGET(connectionId); + if (res.status === 400) { + const body = await res.json(); + assert.notEqual(body.error?.code, "invalid_provider"); + } +}); + +test("GET /v1/providers/:provider/models accepts anthropic-compatible-cc connection ID format", async () => { + const connectionId = "anthropic-compatible-cc-deadbeef-0000-0000-0000-000000000000"; + const res = await callGET(connectionId); + if (res.status === 400) { + const body = await res.json(); + assert.notEqual(body.error?.code, "invalid_provider"); + } +}); + +test("GET /v1/providers/:provider/models accepts openai-compatible-responses connection ID format", async () => { + const connectionId = "openai-compatible-responses-deadbeef-0000-0000-0000-000000000000"; const res = await callGET(connectionId); if (res.status === 400) { const body = await res.json();