refactor(providers): the UI, the connection test and the routes read the record's fields

Step 5 of the provider refactor plan. Provider names gave way to the
record's credential and sdk fields in: hasCredentials (one rule per
credential kind), the connection test (an Object.hasOwn check first, then a
credential switch; EdgeOne's site endpoint and cookie forwarding follow
credential none), the chat route (the on-site function and the single-system
rule for the OpenAI SDK), the credentials form (which fields to show, the
optional-key label, the base URL hint from the record), the picker (the note
from the record), the logo (icon and logo fields), the settings dialog (test
blocker, error hints, EdgeOne origin), the admin panel (fixedCredentials),
chatRequestUrl (sdk), the model list (sdk aihubmix) and the zod schema
(Object.hasOwn instead of 'in'). The eight provider-specific dictionary
entries moved into the record as four-language objects; needAzure became a
generic needUrl.

Two intended behavior changes: the connection test answers 400 'Invalid
provider' for an unknown name whether or not a key is sent (before: 400
'API key is required' without a key, 200 with an error text with one); and
Azure has no placeholder default URL any more, so a new Azure entry starts
with an empty URL and the generic 'needs a URL' rule applies. The e2e
Azure test seeds no URL and expects the new wording. Out-of-layer
provider-name checks: 44 before, 6 after (the admin panel's fixed-variable
branches for Bedrock, Vertex AI and Ollama, kept on purpose). Behavior
snapshot unchanged.
This commit is contained in:
dayuan.jiang
2026-10-12 21:17:51 +09:00
parent e0a196dded
commit f811242568
22 changed files with 303 additions and 266 deletions
+30 -14
View File
@@ -1,16 +1,12 @@
import { describe, expect, it } from "vitest"
import enDict from "@/lib/i18n/dictionaries/en.json"
import jaDict from "@/lib/i18n/dictionaries/ja.json"
import zhDict from "@/lib/i18n/dictionaries/zh.json"
import zhHantDict from "@/lib/i18n/dictionaries/zh-Hant.json"
import {
filterProviderGroups,
hasCredentials,
POPULAR_PROVIDERS,
PROVIDER_GROUPS,
PROVIDER_NOTE_KEYS,
providerStatus,
} from "@/lib/provider-setup"
import { PROVIDER_NAMES, PROVIDERS } from "@/lib/providers"
import { PROVIDER_INFO, type ProviderConfig } from "@/lib/types/model-config"
import { useUiStore } from "@/stores/ui-store"
@@ -33,15 +29,6 @@ describe("provider groups", () => {
for (const p of POPULAR_PROVIDERS) expect(PROVIDER_INFO[p]).toBeTruthy()
})
it("have their notes in every language", () => {
for (const dict of [enDict, zhDict, jaDict, zhHantDict]) {
const t = dict.modelConfig as Record<string, string>
for (const key of Object.values(PROVIDER_NOTE_KEYS)) {
expect(t[key as string]).toBeTruthy()
}
}
})
it("are searched by label or name, ignoring case and spaces", () => {
expect(filterProviderGroups(" ")).toBe(PROVIDER_GROUPS)
const kimi = filterProviderGroups("MOONSHOT")
@@ -63,6 +50,18 @@ describe("hasCredentials", () => {
[{ provider: "edgeone" }, true],
[{ provider: "vertexai", apiKey: "k" }, false],
[{ provider: "vertexai", vertexApiKey: "k" }, true],
// Azure has no default URL: the user's own is part of signing in
[{ provider: "azure", apiKey: "k" }, false],
[
{
provider: "azure",
apiKey: "k",
baseUrl: "https://contoso.openai.azure.com/openai",
},
true,
],
// SGLang's local default fills the form, so a key is enough here
[{ provider: "sglang", apiKey: "k" }, true],
[{ provider: "bedrock", apiKey: "k" }, false],
[
{ provider: "bedrock", apiKey: "k", awsRegion: "us-east-1" },
@@ -90,6 +89,23 @@ describe("hasCredentials", () => {
expect([p, hasCredentials(provider(p))]).toEqual([p, expected])
}
})
it("asks every provider for a key, an AWS pair, a Vertex key or nothing", () => {
// With only a key filled in, by the record's credential kind
for (const p of PROVIDER_NAMES) {
const { credential, defaultBaseUrl } = PROVIDERS[p]
const expected =
credential === "none" || credential === "optionalKey"
? true
: credential === "apiKey"
? !!defaultBaseUrl
: false
expect([
p,
hasCredentials(provider({ provider: p, apiKey: "k" })),
]).toEqual([p, expected])
}
})
})
describe("providerStatus", () => {
+72 -42
View File
@@ -8,10 +8,6 @@
import { describe, expect, it, vi } from "vitest"
import { SINGLE_SYSTEM_PROVIDERS } from "@/lib/ai-providers"
import { i18n } from "@/lib/i18n/config"
import enDict from "@/lib/i18n/dictionaries/en.json"
import jaDict from "@/lib/i18n/dictionaries/ja.json"
import zhDict from "@/lib/i18n/dictionaries/zh.json"
import zhHantDict from "@/lib/i18n/dictionaries/zh-Hant.json"
import {
OPTIONS_OPEN,
POPULAR_PROVIDERS,
@@ -309,18 +305,63 @@ const fixture = {
"openrouter",
"qwen",
],
providerNoteKeys: {
bedrock: "noteBedrock",
vertexai: "noteVertex",
azure: "noteAzure",
edgeone: "noteEdgeone",
ollama: "noteOllama",
sglang: "noteSglang",
} as Record<string, string>,
baseUrlHintKeys: {
minimax: "minimaxBaseUrlHint",
mimo: "mimoBaseUrlHint",
} as Record<string, string>,
// The dictionaries' noteBedrock, noteVertex, noteAzure, noteEdgeone,
// noteOllama and noteSglang entries
notes: {
bedrock: {
en: "AWS credentials",
zh: "AWS 凭证",
ja: "AWS 認証情報",
"zh-Hant": "AWS 憑證",
},
vertexai: {
en: "Express mode key",
zh: "Express 模式 Key",
ja: "Express モードのキー",
"zh-Hant": "Express 模式 Key",
},
azure: {
en: "Key and resource URL",
zh: "Key 和资源地址",
ja: "キーとリソース URL",
"zh-Hant": "Key 和資源位址",
},
edgeone: {
en: "No key needed",
zh: "不用 Key",
ja: "キー不要",
"zh-Hant": "不用 Key",
},
ollama: {
en: "Key optional",
zh: "Key 可不填",
ja: "キーは任意",
"zh-Hant": "Key 可不填",
},
sglang: {
en: "Your server's URL",
zh: "填服务地址",
ja: "サーバーの URL",
"zh-Hant": "填服務位址",
},
} as Record<string, Record<string, string>>,
// The dictionaries' minimaxBaseUrlHint and mimoBaseUrlHint entries
baseUrlHints: {
minimax: {
en: "Use /anthropic for Anthropic-compatible API (recommended), or /v1 for OpenAI-compatible API",
zh: "使用 /anthropic 端点为 Anthropic 兼容 API(推荐),或使用 /v1 端点为 OpenAI 兼容 API",
ja: "/anthropic で Anthropic 互換 API(推奨)、または /v1 で OpenAI 互換 API を使用",
"zh-Hant":
"使用 /anthropic 端點為 Anthropic 相容 API(推薦),或使用 /v1 端點為 OpenAI 相容 API",
},
mimo: {
en: "Default works with pay-as-you-go keys (sk-...). Token Plan subscribers (tp-... keys) must set https://token-plan-cn.xiaomimimo.com/v1",
zh: "默认地址适用于按量付费密钥(sk-...)。Token Plan 订阅用户(tp-... 密钥)请设置为 https://token-plan-cn.xiaomimimo.com/v1",
ja: "デフォルトは従量課金キー(sk-...)用です。Token Plan 加入者(tp-... キー)は https://token-plan-cn.xiaomimimo.com/v1 を設定してください",
"zh-Hant":
"預設地址適用於按量付費金鑰(sk-...)。Token Plan 訂閱用戶(tp-... 金鑰)請設定為 https://token-plan-cn.xiaomimimo.com/v1",
},
} as Record<string, Record<string, string>>,
optionsOpen: ["azure", "ollama", "sglang", "minimax", "mimo"],
noKeyNeeded: ["ollama", "openrouter", "aihubmix"],
}
@@ -336,10 +377,14 @@ describe("the provider record derives the old tables", () => {
it("PROVIDER_INFO: label, defaultBaseUrl, apiKeyUrl and modelList", () => {
for (const p of PROVIDER_NAMES) {
const { label, defaultBaseUrl, apiKeyUrl, modelList } = PROVIDERS[p]
const expected = { ...fixture.providerInfo[p] }
// The one intended difference: Azure's placeholder URL named no
// real resource and is gone; the form treats no URL as unset
if (p === "azure") delete expected.defaultBaseUrl
expect([
p,
{ label, defaultBaseUrl, apiKeyUrl, modelList },
]).toEqual([p, { ...fixture.providerInfo[p] }])
]).toEqual([p, expected])
}
})
@@ -410,37 +455,22 @@ describe("the provider record derives the old tables", () => {
})
it("the picker notes and base URL hints, word for word in every language", () => {
const dicts: Record<string, Record<string, string>> = {
en: enDict.modelConfig,
zh: zhDict.modelConfig,
ja: jaDict.modelConfig,
"zh-Hant": zhHantDict.modelConfig,
}
const noted = PROVIDER_NAMES.filter((p) => PROVIDERS[p].note)
expect(sorted(noted)).toEqual(
sorted(Object.keys(fixture.providerNoteKeys)),
)
expect(sorted(noted)).toEqual(sorted(Object.keys(fixture.notes)))
for (const p of noted) {
for (const [lang, dict] of Object.entries(dicts)) {
expect([p, lang, PROVIDERS[p].note?.[lang as never]]).toEqual([
p,
lang,
dict[fixture.providerNoteKeys[p]],
])
}
expect([p, PROVIDERS[p].note]).toEqual([p, fixture.notes[p]])
}
// Azure's hint is new (it replaces the placeholder URL); the other
// two came from the dictionaries
const hinted = PROVIDER_NAMES.filter((p) => PROVIDERS[p].baseUrlHint)
expect(sorted(hinted)).toEqual(
sorted(Object.keys(fixture.baseUrlHintKeys)),
sorted([...Object.keys(fixture.baseUrlHints), "azure"]),
)
for (const p of hinted) {
for (const [lang, dict] of Object.entries(dicts)) {
expect([
p,
lang,
PROVIDERS[p].baseUrlHint?.[lang as never],
]).toEqual([p, lang, dict[fixture.baseUrlHintKeys[p]]])
}
for (const p of Object.keys(fixture.baseUrlHints) as typeof hinted) {
expect([p, PROVIDERS[p].baseUrlHint]).toEqual([
p,
fixture.baseUrlHints[p],
])
}
})
+25
View File
@@ -382,3 +382,28 @@ describe("the Test on the deployment's own endpoints", () => {
expect(quota.checks).toBe(0)
})
})
describe("POST /api/validate-model with an unknown provider", () => {
const send = (body: object) =>
validateModel(
new Request("http://localhost/api/validate-model", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ modelId: "m", ...body }),
}),
)
it("refuses a name that is not a provider, with or without a key", async () => {
for (const body of [
{ provider: "nope", apiKey: "k" },
{ provider: "nope" },
// A prototype property is not a provider either
{ provider: "toString", apiKey: "k" },
]) {
const res = await send(body)
expect(res.status).toBe(400)
const { error } = await res.json()
expect(error).toMatch(/^Invalid provider: /)
}
})
})