refactor(models): unify provider metadata around backend catalog (#2896)

* feat(models): unify provider metadata around backend catalog

- Move shared provider metadata and alias normalization into backend-owned provider catalog
- Expose display, fetch, auth, and default model metadata through /api/models provider_options
- Replace frontend static provider registry with catalog-driven selection, validation, grouping, and fallback rendering
- Treat provider default api_base as placeholder and effective fetch/test base while keep submitted api_base separate from derived defaults
- Add model page retry handling, touched locale updates, and provider metadata assertions in backend tests

* fix(models): canonicalize backend provider aliases and common models

* fix(models): restore deepseek common model recommendations
This commit is contained in:
LC authored and GitHub committed 2026-05-20 11:50:34 +08:00
1 parent 639b32703a
commit 548dc15acd
28 files changed
+1441 -1084

No files matched your search

@@ -37,13 +37,21 @@ import { showSaveSuccessOrRestartToast } from "@/lib/restart-required"
import { refreshGatewayState } from "@/store/gateway"
import { FetchModelsDialog } from "./fetch-models-dialog"
import {
getEffectiveAPIBase,
getSubmittedAPIBase,
normalizeApiBase,
} from "./model-provider-form-shared"
import { type FieldValidation, validateModelField } from "./model-validation"
import { ProviderCombobox } from "./provider-combobox"
import { getProviderKey } from "./provider-label"
import {
FETCHABLE_PROVIDER_KEYS,
PROVIDER_API_BASES,
PROVIDER_MAP,
getCanonicalProviderKey,
getProviderCatalogEntry,
getProviderCatalogMap,
getProviderDefaultAPIBase,
getProviderDefaultAuthMethod,
isProviderAuthMethodLocked,
providerSupportsFetch,
} from "./provider-registry"
import { TestModelDialog } from "./test-model-dialog"
@@ -74,39 +82,9 @@ interface EditModelSheetProps {
providerOptions?: ModelProviderOption[]
}
function normalizeApiBase(value: string): string {
return value.trim().replace(/\/+$/, "")
}
function getNextApiBaseForProviderChange(
currentApiBase: string,
currentProvider: string,
nextProvider: string,
): string {
const normalizedCurrentApiBase = normalizeApiBase(currentApiBase)
const currentDefaultApiBase = normalizeApiBase(
PROVIDER_API_BASES[currentProvider] || "",
)
const nextDefaultApiBase = PROVIDER_API_BASES[nextProvider] || ""
if (!normalizedCurrentApiBase) {
return nextDefaultApiBase
}
if (
normalizedCurrentApiBase &&
currentDefaultApiBase &&
normalizedCurrentApiBase === currentDefaultApiBase
) {
return nextDefaultApiBase
}
return currentApiBase
}
function buildInitialEditForm(model: ModelInfo): EditForm {
return {
provider: model.provider ?? "",
provider: getCanonicalProviderKey(model.provider),
modelId: model.model,
apiKey: "",
apiBase: model.api_base ?? "",
@@ -166,6 +144,7 @@ export function EditModelSheet({
const [catalogModels, setCatalogModels] = useState<string[]>([])
const debounceRef = useRef<ReturnType<typeof setTimeout>>(undefined)
const scrollContainerRef = useRef<HTMLDivElement>(null)
const providerMap = getProviderCatalogMap(providerOptions)
const initialForm = model ? buildInitialEditForm(model) : null
const isDirty =
@@ -182,12 +161,19 @@ export function EditModelSheet({
setFetchedModels([])
setCatalogModels([])
// Load matching catalog models
const providerKey = getProviderKey(model.provider || undefined)
const apiBase = (model.api_base ?? "").trim().replace(/\/+$/, "")
const providerKey = getCanonicalProviderKey(
model.provider,
providerOptions,
)
const apiBase = getEffectiveAPIBase(
model.provider ?? "",
model.api_base ?? "",
providerOptions,
)
getCatalogs()
.then((res) => {
const matched = (res.entries || []).filter((e) => {
const ep = getProviderKey(e.provider || undefined)
const ep = getCanonicalProviderKey(e.provider, providerOptions)
const eb = (e.api_base ?? "").trim().replace(/\/+$/, "")
return ep === providerKey && eb === apiBase
})
@@ -197,22 +183,28 @@ export function EditModelSheet({
})
.catch(() => {})
}
}, [model])
}, [model, providerOptions])
const setField =
(key: keyof EditForm) =>
(e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) =>
(e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
if (error) setError("")
setForm((f) => ({ ...f, [key]: e.target.value }))
}
const debouncedValidateModel = useCallback(
(value: string, provider: string) => {
if (debounceRef.current) clearTimeout(debounceRef.current)
debounceRef.current = setTimeout(() => {
const result = validateModelField(value, provider || undefined)
const result = validateModelField(
value,
provider || undefined,
providerOptions,
)
setModelValidation(result)
}, 300)
},
[],
[providerOptions],
)
const handleModelChange = (e: React.ChangeEvent<HTMLInputElement>) => {
@@ -222,16 +214,50 @@ export function EditModelSheet({
}
const handleProviderChange = (provider: string) => {
setForm((f) => ({
...f,
provider,
apiBase: getNextApiBaseForProviderChange(f.apiBase, f.provider, provider),
}))
if (error) setError("")
setForm((f) => {
const previousOption = getProviderCatalogEntry(
f.provider,
providerOptions,
)
const nextOption = getProviderCatalogEntry(provider, providerOptions)
const previousDefaultBase = normalizeApiBase(
getProviderDefaultAPIBase(f.provider, providerOptions),
)
const nextDefaultBase = normalizeApiBase(
getProviderDefaultAPIBase(provider, providerOptions),
)
const currentApiBase = normalizeApiBase(f.apiBase)
let authMethod = f.authMethod
let apiBase = f.apiBase
if (nextOption?.authMethodLocked) {
authMethod = nextOption.defaultAuthMethod ?? ""
} else if (
previousOption?.authMethodLocked &&
f.authMethod === (previousOption.defaultAuthMethod ?? "")
) {
authMethod = ""
}
if (
currentApiBase &&
previousDefaultBase &&
currentApiBase === previousDefaultBase &&
currentApiBase !== nextDefaultBase
) {
apiBase = ""
}
return {
...f,
provider: getCanonicalProviderKey(provider, providerOptions),
apiBase,
authMethod,
}
})
if (form.modelId) {
debouncedValidateModel(form.modelId, provider)
}
const allowed =
providerOptions?.find((o) => o.id === provider)?.default_model_allowed ??
getProviderCatalogEntry(provider, providerOptions)?.defaultModelAllowed ??
false
if (!allowed) {
setSetAsDefault(false)
@@ -258,15 +284,45 @@ export function EditModelSheet({
}
}
const providerDef = PROVIDER_MAP.get(form.provider)
const canonicalProvider = getCanonicalProviderKey(
form.provider,
providerOptions,
)
const providerDef = canonicalProvider
? providerMap.get(canonicalProvider)
: undefined
const commonModels = providerDef?.commonModels || []
const defaultModelAllowed = form.provider
? (providerOptions?.find((o) => o.id === form.provider)
?.default_model_allowed ?? false)
: false
const authMethodLocked = isProviderAuthMethodLocked(
form.provider,
providerOptions,
)
const defaultAuthMethod = getProviderDefaultAuthMethod(
form.provider,
providerOptions,
)
const effectiveAuthMethod = (
authMethodLocked ? defaultAuthMethod : form.authMethod
)
.trim()
.toLowerCase()
const isOAuth = effectiveAuthMethod === "oauth"
const defaultModelAllowed = providerDef?.defaultModelAllowed === true
const apiBasePlaceholder =
getProviderDefaultAPIBase(form.provider, providerOptions) ||
"https://api.example.com/v1"
const effectiveApiBase = getEffectiveAPIBase(
form.provider,
form.apiBase,
providerOptions,
)
const submittedApiBase = getSubmittedAPIBase(form.apiBase)
const handleSave = async () => {
if (!model) return
if (!providerDef) {
setError(t("models.field.providerInvalid"))
return
}
if (!form.modelId.trim()) {
setError(t("models.add.errorRequired"))
return
@@ -304,7 +360,7 @@ export function EditModelSheet({
setError("")
try {
const modelId = form.modelId.trim()
const provider = form.provider.trim()
const provider = canonicalProvider
const streaming =
model.streaming?.enabled === true || form.streamingEnabled
? { enabled: form.streamingEnabled }
@@ -313,18 +369,20 @@ export function EditModelSheet({
model_name: model.model_name,
provider: provider,
model: modelId,
api_base: form.apiBase || undefined,
api_key: form.apiKey || undefined,
proxy: form.proxy || undefined,
auth_method: form.authMethod || undefined,
connect_mode: form.connectMode || undefined,
workspace: form.workspace || undefined,
api_base: submittedApiBase,
api_key: form.apiKey.trim() || undefined,
proxy: form.proxy.trim() || undefined,
auth_method: authMethodLocked
? defaultAuthMethod || undefined
: form.authMethod.trim() || undefined,
connect_mode: form.connectMode.trim() || undefined,
workspace: form.workspace.trim() || undefined,
rpm: form.rpm ? Number(form.rpm) : undefined,
max_tokens_field: form.maxTokensField || undefined,
max_tokens_field: form.maxTokensField.trim() || undefined,
request_timeout: form.requestTimeout
? Number(form.requestTimeout)
: undefined,
thinking_level: form.thinkingLevel || undefined,
thinking_level: form.thinkingLevel.trim() || undefined,
tool_schema_transform: form.toolSchemaTransform.trim() || undefined,
streaming,
extra_body: extraBody,
@@ -349,7 +407,6 @@ export function EditModelSheet({
}
}
const isOAuth = model?.auth_method === "oauth"
const hasSavedAPIKey = Boolean(model?.api_key)
const apiKeyPlaceholder = hasSavedAPIKey
? maskedSecretPlaceholder(
@@ -382,6 +439,12 @@ export function EditModelSheet({
<Field
label={t("models.field.provider")}
hint={t("models.field.providerHint")}
error={
!providerDef && form.provider
? t("models.field.providerInvalid")
: undefined
}
required
>
<ProviderCombobox
value={form.provider}
@@ -477,18 +540,17 @@ export function EditModelSheet({
</div>
)}
<div className="flex items-center gap-2">
{form.provider &&
FETCHABLE_PROVIDER_KEYS.has(form.provider) && (
<Button
variant="outline"
size="sm"
className="h-7 text-xs"
onClick={() => setFetchOpen(true)}
>
<IconDownload className="size-3" />
{t("models.fetch.title")}
</Button>
)}
{providerSupportsFetch(form.provider, providerOptions) && (
<Button
variant="outline"
size="sm"
className="h-7 text-xs"
onClick={() => setFetchOpen(true)}
>
<IconDownload className="size-3" />
{t("models.fetch.title")}
</Button>
)}
</div>
</Field>
@@ -514,7 +576,7 @@ export function EditModelSheet({
<Input
value={form.apiBase}
onChange={setField("apiBase")}
placeholder="https://api.example.com/v1"
placeholder={apiBasePlaceholder}
disabled={isOAuth}
/>
</Field>
@@ -557,12 +619,19 @@ export function EditModelSheet({
<Field
label={t("models.field.authMethod")}
hint={t("models.field.authMethodHint")}
hint={
authMethodLocked
? t("models.field.authMethodManagedHint")
: t("models.field.authMethodHint")
}
>
<Input
value={form.authMethod}
value={
authMethodLocked ? defaultAuthMethod : form.authMethod
}
onChange={setField("authMethod")}
placeholder="oauth"
disabled={authMethodLocked}
/>
</Field>
@@ -719,11 +788,11 @@ export function EditModelSheet({
open={testOpen}
onClose={() => setTestOpen(false)}
inlineParams={{
provider: form.provider,
provider: canonicalProvider,
model: form.modelId,
apiBase: form.apiBase,
apiBase: effectiveApiBase,
apiKey: form.apiKey,
authMethod: form.authMethod,
authMethod: effectiveAuthMethod,
modelIndex: model?.index,
}}
/>
@@ -732,9 +801,10 @@ export function EditModelSheet({
open={fetchOpen}
onClose={() => setFetchOpen(false)}
onFill={handleFetchFill}
provider={form.provider}
provider={canonicalProvider}
apiKey={form.apiKey}
apiBase={form.apiBase}
apiBase={effectiveApiBase}
backendOptions={providerOptions}
/>
</>
)