refactor(providers): one record per provider, one place for the key-and-URL decision (#988)

Every fact about a provider lives in one record in lib/providers.ts (SDK kind,
credential kind, env var prefix, default URL, picker group, four-language
notes, and a short model list for the six providers that cannot list models).
The thirteen tables keyed by provider name are gone. resolveEndpoint decides
whose key a call uses and where it goes; usesServerCredentials reads its
answer instead of mirroring getAIModel's branches; serverCredentialError holds
the server credential check once. The UI, the connection test and the routes
read the record's credential and sdk fields instead of comparing provider
names (44 such checks before, 6 after, in the admin panel's fixed-variable
branches).

Intended behavior changes: providers that can list their models have no
hand-written list; the settings dialog fetches the live list in the background
once credentials are entered for a provider without models and offers it
behind "Browse models". Azure has no placeholder default URL (old entries that
hold it still count as unset); the base URL field of Azure and of the new
"custom" OpenAI-compatible preset (CUSTOM_API_KEY, CUSTOM_BASE_URL) is
labelled required. The connection test answers 400 for an unknown provider
name; prototype names are refused everywhere. Provider lists in error messages
and the admin "add provider" dropdown follow the picker's order.

Also fixed, from before the refactor: the redirect guard drops x-access-code
on cross-origin redirects, and edgeOneEndpoint accepts the Origin header only
when it matches Host or X-Forwarded-Host.

Verification: a behavior snapshot of getAIModel's SDK factory calls and the
quota functions over 25 providers, 22 client override shapes and 9 server
environments (5843 rows), recorded on the old code and unchanged apart from
the intended changes; 752 unit tests, 65 provider e2e tests, tsc, biome,
next build. Reviewed by five models from one brief; their findings and the
resulting fixes are recorded in docs/plans/2026-10-provider-refactor.md.
This commit is contained in:
Dayuan Jiang
2026-10-12 21:31:03 +09:00
committed by GitHub
parent 5b27d30194
commit 458d8050cc
40 changed files with 9272 additions and 1267 deletions
+35 -17
View File
@@ -1,17 +1,13 @@
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_INFO, type ProviderConfig } from "@/lib/types/model-config"
import { PROVIDERS } from "@/lib/providers"
import type { ProviderConfig } from "@/lib/types/model-config"
import { useUiStore } from "@/stores/ui-store"
const provider = (p: Partial<ProviderConfig>): ProviderConfig => ({
@@ -25,21 +21,12 @@ const provider = (p: Partial<ProviderConfig>): ProviderConfig => ({
describe("provider groups", () => {
it("hold every provider exactly once", () => {
const grouped = PROVIDER_GROUPS.flatMap((g) => g.providers)
expect([...grouped].sort()).toEqual(Object.keys(PROVIDER_INFO).sort())
expect([...grouped].sort()).toEqual(Object.keys(PROVIDERS).sort())
expect(new Set(grouped).size).toBe(grouped.length)
})
it("offer known providers as popular ones", () => {
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()
}
}
for (const p of POPULAR_PROVIDERS) expect(PROVIDERS[p]).toBeTruthy()
})
it("are searched by label or name, ignoring case and spaces", () => {
@@ -63,6 +50,37 @@ 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,
],
// The placeholder Azure once wrote into new entries names no resource
[
{
provider: "azure",
apiKey: "k",
baseUrl: "https://your-resource.openai.azure.com/openai",
},
false,
],
// The OpenAI-compatible preset has no default URL either
[{ provider: "custom", apiKey: "k" }, false],
[
{
provider: "custom",
apiKey: "k",
baseUrl: "http://127.0.0.1:1234/v1",
},
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" },