import { IconDownload, IconLoader2, IconPlugConnected, } from "@tabler/icons-react" import { useCallback, useEffect, useRef, useState } from "react" import { useTranslation } from "react-i18next" import { type ModelInfo, type ModelProviderOption, getCatalogs, setDefaultModel, updateModel, } from "@/api/models" import { ConfigChangeNotice } from "@/components/config-change-notice" import { maskedSecretPlaceholder } from "@/components/secret-placeholder" import { AdvancedSection, Field, KeyInput, SwitchCardField, } from "@/components/shared-form" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Sheet, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetTitle, } from "@/components/ui/sheet" import { Textarea } from "@/components/ui/textarea" 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 { getCanonicalProviderKey, getProviderCatalogEntry, getProviderCatalogMap, getProviderDefaultAPIBase, getProviderDefaultAuthMethod, isProviderAuthMethodLocked, providerSupportsFetch, } from "./provider-registry" import { TestModelDialog } from "./test-model-dialog" interface EditForm { provider: string modelId: string apiKey: string apiBase: string proxy: string authMethod: string connectMode: string workspace: string rpm: string maxTokensField: string requestTimeout: string thinkingLevel: string toolSchemaTransform: string streamingEnabled: boolean extraBody: string customHeaders: string } interface EditModelSheetProps { model: ModelInfo | null open: boolean onClose: () => void onSaved: () => void providerOptions?: ModelProviderOption[] } function buildInitialEditForm(model: ModelInfo): EditForm { return { provider: getCanonicalProviderKey(model.provider), modelId: model.model, apiKey: "", apiBase: model.api_base ?? "", proxy: model.proxy ?? "", authMethod: model.auth_method ?? "", connectMode: model.connect_mode ?? "", workspace: model.workspace ?? "", rpm: model.rpm ? String(model.rpm) : "", maxTokensField: model.max_tokens_field ?? "", requestTimeout: model.request_timeout ? String(model.request_timeout) : "", thinkingLevel: model.thinking_level ?? "", toolSchemaTransform: model.tool_schema_transform ?? "", streamingEnabled: model.streaming?.enabled === true, extraBody: model.extra_body ? JSON.stringify(model.extra_body, null, 2) : "", customHeaders: model.custom_headers ? JSON.stringify(model.custom_headers, null, 2) : "", } } export function EditModelSheet({ model, open, onClose, onSaved, providerOptions, }: EditModelSheetProps) { const { t } = useTranslation() const [form, setForm] = useState({ provider: "", modelId: "", apiKey: "", apiBase: "", proxy: "", authMethod: "", connectMode: "", workspace: "", rpm: "", maxTokensField: "", requestTimeout: "", thinkingLevel: "", toolSchemaTransform: "", streamingEnabled: false, extraBody: "", customHeaders: "", }) const [saving, setSaving] = useState(false) const [setAsDefault, setSetAsDefault] = useState(false) const [error, setError] = useState("") const [modelValidation, setModelValidation] = useState(null) const [testOpen, setTestOpen] = useState(false) const [fetchOpen, setFetchOpen] = useState(false) const [fetchedModels, setFetchedModels] = useState([]) const [catalogModels, setCatalogModels] = useState([]) const debounceRef = useRef>(undefined) const scrollContainerRef = useRef(null) const providerMap = getProviderCatalogMap(providerOptions) const initialForm = model ? buildInitialEditForm(model) : null const isDirty = model != null && (JSON.stringify(form) !== JSON.stringify(initialForm) || setAsDefault !== model.is_default) useEffect(() => { if (model) { setForm(buildInitialEditForm(model)) setSetAsDefault(model.is_default) setError("") setModelValidation(null) setFetchedModels([]) setCatalogModels([]) // Load matching catalog models 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 = getCanonicalProviderKey(e.provider, providerOptions) const eb = (e.api_base ?? "").trim().replace(/\/+$/, "") return ep === providerKey && eb === apiBase }) const ids = matched.flatMap((e) => e.models.map((m) => m.id)) const unique = [...new Set(ids)] if (unique.length > 0) setCatalogModels(unique) }) .catch(() => {}) } }, [model, providerOptions]) const setField = (key: keyof EditForm) => (e: React.ChangeEvent) => { 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, providerOptions, ) setModelValidation(result) }, 300) }, [providerOptions], ) const handleModelChange = (e: React.ChangeEvent) => { const value = e.target.value setForm((f) => ({ ...f, modelId: value })) debouncedValidateModel(value, form.provider) } const handleProviderChange = (provider: string) => { 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 = getProviderCatalogEntry(provider, providerOptions)?.defaultModelAllowed ?? false if (!allowed) { setSetAsDefault(false) } } const applyFix = () => { if (modelValidation?.fix) { setForm((f) => ({ ...f, modelId: modelValidation.fix! })) setModelValidation(null) } } const handleCommonModel = (modelId: string) => { setForm((f) => ({ ...f, modelId })) setModelValidation(null) } const handleFetchFill = (models: string[]) => { setFetchedModels(models) if (models.length >= 1) { setForm((f) => ({ ...f, modelId: models[0] })) setModelValidation(null) } } const canonicalProvider = getCanonicalProviderKey( form.provider, providerOptions, ) const providerDef = canonicalProvider ? providerMap.get(canonicalProvider) : undefined const commonModels = providerDef?.commonModels || [] 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 } if (modelValidation?.level === "error") return let extraBody: Record | undefined let customHeaders: Record | undefined try { if (form.extraBody.trim()) { extraBody = JSON.parse(form.extraBody.trim()) } else { extraBody = {} } } catch { setError( t("models.field.extraBody") + ": " + t("models.field.invalidJson"), ) return } try { if (form.customHeaders.trim()) { customHeaders = JSON.parse(form.customHeaders.trim()) } else { customHeaders = {} } } catch { setError( t("models.field.customHeaders") + ": " + t("models.field.invalidJson"), ) return } setSaving(true) setError("") try { const modelId = form.modelId.trim() const provider = canonicalProvider const streaming = model.streaming?.enabled === true || form.streamingEnabled ? { enabled: form.streamingEnabled } : undefined await updateModel(model.index, { model_name: model.model_name, provider: provider, model: modelId, 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.trim() || undefined, request_timeout: form.requestTimeout ? Number(form.requestTimeout) : undefined, thinking_level: form.thinkingLevel.trim() || undefined, tool_schema_transform: form.toolSchemaTransform.trim() || undefined, streaming, extra_body: extraBody, custom_headers: customHeaders, }) if (setAsDefault && !model.is_default) { await setDefaultModel(model.model_name) } const gateway = await refreshGatewayState({ force: true }) showSaveSuccessOrRestartToast( t, t("models.edit.saveSuccess"), model.model_name, gateway?.restartRequired === true, ) onSaved() onClose() } catch (e) { setError(e instanceof Error ? e.message : t("models.edit.saveError")) } finally { setSaving(false) } } const hasSavedAPIKey = Boolean(model?.api_key) const apiKeyPlaceholder = hasSavedAPIKey ? maskedSecretPlaceholder( model?.api_key ?? "", t("models.field.apiKeyPlaceholderSet"), ) : t("models.field.apiKeyPlaceholder") return ( <> !v && onClose()}> {t("models.edit.title", { name: model?.model_name })} {model?.model}
{modelValidation && modelValidation.messageKey && (
{t( modelValidation.messageKey, modelValidation.messageParams, )} {modelValidation.fix && ( )}
)} {commonModels.length > 0 && (
{commonModels.map((m) => ( handleCommonModel(m)} > {m} ))}
)} {catalogModels.length > 0 && (
{catalogModels.map((m) => ( handleCommonModel(m)} > {m} ))}
)} {fetchedModels.length > 0 && (
{fetchedModels.map((m) => ( handleCommonModel(m)} > {m} ))}
)}
{providerSupportsFetch(form.provider, providerOptions) && ( )}
{!isOAuth && ( setForm((f) => ({ ...f, apiKey: v }))} placeholder={apiKeyPlaceholder} /> )}