refactor(providers): one place for env var names and the server credential check

Step 3 of the provider refactor plan. envVarsOf(provider) spells the key and
base URL variable names from the record's envPrefix and keyEnv; the three
copies of the gateway/AI_GATEWAY spelling (getAIModel's default branch,
globalBaseUrl, the admin panel's credEnvNames) read it. serverCredentialError
answers why the server lacks a provider's credentials, or null; detectProvider
asks for null and validateProviderCredentials throws the text, so the
Anthropic auth token and the Azure URL-or-resource rules exist once. The four
error texts and their order are unchanged (tests/unit/server-credentials.test.ts
was written against the old code first). credEnvNames reads fixedCredentials
and envPrefix and is exported, with a table test over all 24 providers.
Behavior snapshot unchanged.
This commit is contained in:
dayuan.jiang
2026-10-12 21:17:51 +09:00
parent b0e95a5ef4
commit 932fd03a7c
4 changed files with 331 additions and 106 deletions
+10 -14
View File
@@ -1,14 +1,11 @@
import { z } from "zod"
import { PROVIDERS, type ProviderName } from "@/lib/providers"
import {
ProviderNameSchema,
type ServerModelsConfig,
slugify,
} from "@/lib/server-model-config"
import {
FIXED_CRED_PROVIDERS,
PROVIDER_INFO,
type ProviderName,
} from "@/lib/types/model-config"
import { FIXED_CRED_PROVIDERS, PROVIDER_INFO } from "@/lib/types/model-config"
import { type MaskedSecret, maskSecret } from "./auth"
import { loadSettings } from "./settings"
@@ -80,20 +77,19 @@ const SECRET_FIELDS = [
"vertexApiKey",
] as const
// ADMIN_-prefixed env var names for instance `index` (0-based) of a provider
function credEnvNames(
// ADMIN_-prefixed env var names for instance `index` (0-based) of a
// provider. None for providers whose credentials live in fixed variables
// (Bedrock, Vertex AI, Ollama) or that have none (EdgeOne).
export function credEnvNames(
provider: ProviderName,
index: number,
): { key?: string; url?: string } {
if (FIXED_CRED_PROVIDERS.includes(provider) || provider === "edgeone") {
return {}
}
const prefix =
provider === "gateway" ? "AI_GATEWAY" : provider.toUpperCase()
const { fixedCredentials, envPrefix } = PROVIDERS[provider]
if (fixedCredentials || envPrefix === null) return {}
const suffix = index === 0 ? "" : `_${index + 1}`
return {
key: `ADMIN_${prefix}_API_KEY${suffix}`,
url: `ADMIN_${prefix}_BASE_URL${suffix}`,
key: `ADMIN_${envPrefix}_API_KEY${suffix}`,
url: `ADMIN_${envPrefix}_BASE_URL${suffix}`,
}
}