mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-13 04:59:51 +08:00
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.
91 lines
3.1 KiB
TypeScript
91 lines
3.1 KiB
TypeScript
// What the model settings show about providers: the groups of the provider
|
|
// picker, and how far a provider's setup has come
|
|
|
|
import {
|
|
PROVIDER_NAMES,
|
|
PROVIDERS,
|
|
type ProviderGroup as ProviderGroupId,
|
|
} from "@/lib/providers"
|
|
import type { ProviderConfig, ProviderName } from "@/lib/types/model-config"
|
|
|
|
/** The provider picker's groups, each provider in exactly one, in record order */
|
|
export const PROVIDER_GROUPS: {
|
|
id: ProviderGroupId
|
|
providers: ProviderName[]
|
|
}[] = (["vendors", "aggregators", "cloud", "selfHosted"] as const).map(
|
|
(id) => ({
|
|
id,
|
|
providers: PROVIDER_NAMES.filter((p) => PROVIDERS[p].group === id),
|
|
}),
|
|
)
|
|
|
|
/** Shown when the user has no provider yet */
|
|
export const POPULAR_PROVIDERS: ProviderName[] = [
|
|
"openai",
|
|
"anthropic",
|
|
"google",
|
|
"deepseek",
|
|
"openrouter",
|
|
"qwen",
|
|
]
|
|
|
|
/** The groups with only the providers whose name matches the search */
|
|
export function filterProviderGroups(query: string) {
|
|
const q = query.trim().toLowerCase()
|
|
if (!q) return PROVIDER_GROUPS
|
|
return PROVIDER_GROUPS.map((group) => ({
|
|
...group,
|
|
providers: group.providers.filter(
|
|
(p) =>
|
|
PROVIDERS[p].label.toLowerCase().includes(q) || p.includes(q),
|
|
),
|
|
})).filter((group) => group.providers.length > 0)
|
|
}
|
|
|
|
const LEGACY_AZURE_PLACEHOLDER = "https://your-resource.openai.azure.com/openai"
|
|
|
|
/** The credentials a test or a chat request needs are filled in */
|
|
export function hasCredentials(p: ProviderConfig): boolean {
|
|
const def = PROVIDERS[p.provider]
|
|
switch (def.credential) {
|
|
case "aws":
|
|
return (
|
|
(!!p.apiKey ||
|
|
(!!p.awsAccessKeyId && !!p.awsSecretAccessKey)) &&
|
|
!!p.awsRegion
|
|
)
|
|
case "vertexKey":
|
|
return !!p.vertexApiKey
|
|
case "none":
|
|
case "optionalKey":
|
|
return true
|
|
case "apiKey": {
|
|
// Without a default URL in the record (Azure, custom) the user's
|
|
// own is part of the credentials. Entries saved while Azure still
|
|
// had a placeholder default hold that placeholder; it names no
|
|
// real resource
|
|
const baseUrl =
|
|
p.baseUrl === LEGACY_AZURE_PLACEHOLDER ? "" : p.baseUrl
|
|
return !!p.apiKey && (!!def.defaultBaseUrl || !!baseUrl)
|
|
}
|
|
}
|
|
}
|
|
|
|
export type ProviderStatus =
|
|
| { kind: "error"; count: number }
|
|
| { kind: "incomplete" }
|
|
| { kind: "untested"; count: number }
|
|
| { kind: "ok"; count: number }
|
|
|
|
/** The one thing about a provider that most needs the user's attention */
|
|
export function providerStatus(p: ProviderConfig): ProviderStatus {
|
|
const failed = p.models.filter((m) => m.validated === false).length
|
|
if (failed > 0) return { kind: "error", count: failed }
|
|
if (!hasCredentials(p) || p.models.length === 0) {
|
|
return { kind: "incomplete" }
|
|
}
|
|
const untested = p.models.filter((m) => m.validated !== true).length
|
|
if (untested > 0) return { kind: "untested", count: untested }
|
|
return { kind: "ok", count: p.models.length }
|
|
}
|