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: sync SiliconFlow models from configured endpoint (#3094)
Xiangzhe948f23251798
diff --git a/src/app/api/providers/[id]/models/route.ts b/src/app/api/providers/[id]/models/route.tsindex 44dd345e2f..15f3845885 100755--- a/src/app/api/providers/[id]/models/route.ts+++ b/src/app/api/providers/[id]/models/route.ts@@ -126,7 +126,14 @@ function isLocalOpenAIStyleProvider(provider: string): boolean {return isSelfHostedChatProvider(provider);}-const NAMED_OPENAI_STYLE_PROVIDERS = new Set(["modal", "reka", "empower", "nous-research", "poe"]);+const NAMED_OPENAI_STYLE_PROVIDERS = new Set([+ "modal",+ "reka",+ "empower",+ "nous-research",+ "poe",+ "siliconflow",+]);function isNamedOpenAIStyleProvider(provider: string): boolean {return NAMED_OPENAI_STYLE_PROVIDERS.has(provider);@@ -1966,8 +1973,6 @@ export async function GET(});}--const localCatalog = mergeLocalCatalogModels(registryCatalogModels, specialtyCatalogModels);if (!config && localCatalog.length > 0) {return buildResponse({diff --git a/src/app/api/providers/[id]/sync-models/route.ts b/src/app/api/providers/[id]/sync-models/route.tsindex e089dcfad0..b124b34d3c 100644--- a/src/app/api/providers/[id]/sync-models/route.ts+++ b/src/app/api/providers/[id]/sync-models/route.ts@@ -439,6 +439,40 @@ export async function POST(request: Request, { params }: { params: Promise<{ id:);}+ const modelSource = toNonEmptyString(modelsData.source)?.toLowerCase() || "unknown";+ const modelWarning = toNonEmptyString(modelsData.warning);+ if (modelSource === "local_catalog") {+ const responseError =+ modelWarning || "Remote model discovery failed; local catalog fallback not synced";+ await saveCallLog({+ method: "GET",+ path: `/api/providers/${id}/models`,+ status: 502,+ model: "model-sync",+ provider: logProvider,+ sourceFormat: "-",+ connectionId: id,+ duration,+ error: responseError,+ requestType: "model-sync",+ responseBody: {+ source: modelSource,+ warning: modelWarning,+ provider: logProvider,+ channel: channelLabel,+ },+ });++ return NextResponse.json(+ {+ error: responseError,+ source: modelSource,+ ...(modelWarning ? { warning: modelWarning } : {}),+ },+ { status: 502 }+ );+ }+const fetchedModels = modelsData.models || [];const {previousModels,diff --git a/tests/unit/provider-models-route.test.ts b/tests/unit/provider-models-route.test.tsindex e5dc623c0e..87b662dbae 100644--- a/tests/unit/provider-models-route.test.ts+++ b/tests/unit/provider-models-route.test.ts@@ -196,6 +196,54 @@ test("provider models route retries transient OpenAI-compatible probe failures bassert.deepEqual(body.models, [{ id: "demo-model", name: "Demo Model" }]);});+test("provider models route discovers SiliconFlow models from configured China base URL", async () => {+ const connection = await seedConnection("siliconflow", {+ apiKey: "sf-cn-key",+ providerSpecificData: {+ baseUrl: "https://api.siliconflow.cn/v1",+ },+ });+ const seenRequests: Array<{+ url: string;+ method: string | undefined;+ authorization: string | null;+ }> = [];++ globalThis.fetch = async (url, init) => {+ const headers = new Headers(init?.headers as HeadersInit | undefined);+ seenRequests.push({+ url: String(url),+ method: init?.method,+ authorization: headers.get("authorization"),+ });++ return Response.json({+ data: [{ id: "Qwen/Qwen3-Coder-480B-A35B-Instruct", name: "Qwen3 Coder" }],+ });+ };++ const response = await callRoute(connection.id, "?refresh=true");+ const body = (await response.json()) as any;++ assert.equal(response.status, 200);+ assert.equal(body.provider, "siliconflow");+ assert.equal(body.source, "api");+ assert.deepEqual(seenRequests, [+ {+ url: "https://api.siliconflow.cn/v1/models",+ method: "GET",+ authorization: "Bearer sf-cn-key",+ },+ ]);+ assert.deepEqual(body.models, [+ {+ id: "Qwen/Qwen3-Coder-480B-A35B-Instruct",+ name: "Qwen3 Coder",+ owned_by: "siliconflow",+ },+ ]);+});+test("provider models route returns static catalog entries for providers with hardcoded models", async () => {const connection = await seedConnection("bailian-coding-plan", {apiKey: "bailian-key",diff --git a/tests/unit/siliconflow-model-sync.test.ts b/tests/unit/siliconflow-model-sync.test.tsnew file mode 100644index 0000000000..8b9835c944--- /dev/null+++ b/tests/unit/siliconflow-model-sync.test.ts@@ -0,0 +1,134 @@+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-siliconflow-sync-"));+process.env.DATA_DIR = TEST_DATA_DIR;++const core = await import("../../src/lib/db/core.ts");+const providersDb = await import("../../src/lib/db/providers.ts");+const modelsDb = await import("../../src/lib/db/models.ts");+const syncModelsRoute = await import("../../src/app/api/providers/[id]/sync-models/route.ts");+const { buildModelSyncInternalHeaders } =+ await import("../../src/shared/services/modelSyncScheduler.ts");++const originalFetch = globalThis.fetch;++type JsonBody = Record<string, unknown>;++async function resetStorage() {+ globalThis.fetch = originalFetch;+ core.resetDbInstance();+ fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });+ fs.mkdirSync(TEST_DATA_DIR, { recursive: true });+}++async function seedSiliconFlowConnection() {+ return providersDb.createProviderConnection({+ provider: "siliconflow",+ authType: "apikey",+ name: `siliconflow-${Math.random().toString(16).slice(2, 8)}`,+ apiKey: "sf-key",+ isActive: true,+ testStatus: "active",+ providerSpecificData: { baseUrl: "https://api.siliconflow.cn/v1" },+ });+}++async function callSyncRoute(connectionId: string) {+ return syncModelsRoute.POST(+ new Request(`http://127.0.0.1/api/providers/${connectionId}/sync-models`, {+ method: "POST",+ headers: buildModelSyncInternalHeaders(),+ }),+ { params: Promise.resolve({ id: connectionId }) }+ );+}++test.beforeEach(async () => {+ await resetStorage();+});++test.after(async () => {+ globalThis.fetch = originalFetch;+ core.resetDbInstance();+ fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });+});++test("sync-models rejects local catalog fallback and preserves existing SiliconFlow models", async () => {+ const connection = await seedSiliconFlowConnection();+ await modelsDb.replaceSyncedAvailableModelsForConnection("siliconflow", connection.id, [+ { id: "remote-existing", name: "Remote Existing" },+ ]);++ globalThis.fetch = async (url) => {+ if (String(url).includes("__readiness_probe__")) {+ return new Response("", { status: 404 });+ }++ return Response.json({+ provider: "siliconflow",+ connectionId: connection.id,+ source: "local_catalog",+ warning: "API unavailable — using local catalog",+ models: [{ id: "deepseek-ai/DeepSeek-V3", name: "DeepSeek V3" }],+ });+ };++ const response = await callSyncRoute(connection.id);+ const body = (await response.json()) as JsonBody;++ assert.equal(response.status, 502);+ assert.equal(body.source, "local_catalog");+ assert.equal(body.error, "API unavailable — using local catalog");++ const syncedModels = await modelsDb.getSyncedAvailableModelsForConnection(+ "siliconflow",+ connection.id+ );+ assert.deepEqual(+ syncedModels.map((model) => ({ id: model.id, name: model.name })),+ [{ id: "remote-existing", name: "Remote Existing" }]+ );+});++test("sync-models persists SiliconFlow models from API discovery", async () => {+ const connection = await seedSiliconFlowConnection();++ globalThis.fetch = async (url) => {+ if (String(url).includes("__readiness_probe__")) {+ return new Response("", { status: 404 });+ }++ return Response.json({+ provider: "siliconflow",+ connectionId: connection.id,+ source: "api",+ models: [+ { id: "remote-a", name: "Remote A" },+ { id: "remote-b", name: "Remote B" },+ ],+ });+ };++ const response = await callSyncRoute(connection.id);+ const body = (await response.json()) as JsonBody;++ assert.equal(response.status, 200);+ assert.equal(body.ok, true);+ assert.equal(body.syncedModels, 2);++ const syncedModels = await modelsDb.getSyncedAvailableModelsForConnection(+ "siliconflow",+ connection.id+ );+ assert.deepEqual(+ syncedModels.map((model) => ({ id: model.id, name: model.name })),+ [+ { id: "remote-a", name: "Remote A" },+ { id: "remote-b", name: "Remote B" },+ ]+ );+});