import { CaretDownOutlined, InfoCircleOutlined } from '@ant-design/icons'; import { Select as SealSelect, TooltipList, useAppUtils } from '@gpustack/core-ui'; import { useIntl, useNavigate } from '@umijs/max'; import { Form, Select } from 'antd'; import React, { useMemo } from 'react'; import styled from 'styled-components'; import { backendTipsList } from '../config'; import { useFormContext } from '../config/form-context'; import { backendOptionsMap } from '../constants/backend-parameters'; import useCompareEnvs from '../hooks/use-compare-envs'; import EnvsOverridePopover from './envs-override-popover'; const CaretDownWrapper = styled.span` display: flex; align-items: center; gap: 4px; cursor: pointer; &:hover { .anticon { color: var(--ant-color-text); } } `; const BackendFields: React.FC = () => { const intl = useIntl(); const navigate = useNavigate(); const { getRuleMessage } = useAppUtils(); const form = Form.useFormInstance(); const { action, initialValues, onValuesChange, backendOptions, flatBackendOptions, onBackendChange } = useFormContext(); const backend = Form.useWatch('backend', form); const [showDeprecated, setShowDeprecated] = React.useState(false); const { openTips, diffEnvs, handleCloseTips, handleCompareEnvs } = useCompareEnvs(); const handleBackendVersionOnChange = (value: any, option: any) => { if (Object.keys(option.data?.env || {}).length > 0) { form.setFieldValue('env', { ...(option?.data?.env || {}) }); } onValuesChange?.({}, form.getFieldsValue()); }; const backendVersions = useMemo((): { builtIn: any[]; custom: any[]; deprecated: any[]; } => { if (!backend || backend === backendOptionsMap.custom) { return { builtIn: [], custom: [], deprecated: [] }; } const selectedBackend = flatBackendOptions.find( (item) => item.value === backend ); const versions = selectedBackend?.versions || []; // if it's a custom backend, if (selectedBackend && !selectedBackend.isBuiltIn) { return { builtIn: [], custom: versions.filter((item) => !item.is_deprecated), deprecated: versions.filter((item) => item.is_deprecated) }; } // check the value if endts with '-custom', if true, remove the suffix add to "Cutom" group, if not , add to "Built-in" group // ============ Built-in Versions ============ const builtInVersions = versions.filter( (item) => !item.value?.endsWith('-custom') && !item.is_deprecated ); // ============ Custom Versions ============ const customVersions = versions.filter( (item) => item.value?.endsWith('-custom') && !item.is_deprecated ); // ============ Deprecated Versions ============ const deprecatedVersions = versions.filter((item) => item.is_deprecated); return { builtIn: builtInVersions, custom: customVersions, deprecated: deprecatedVersions }; }, [backend, flatBackendOptions, intl]); const backendVersionLabelRender = (option: any) => { return option.title; }; const renderVersionOptions = (values: any[], label: string) => { if (!values || values.length === 0) { return null; } return ( {values.map((item) => ( {item.label} ))} ); }; const handleOnBackendChange = (value: any, option: any) => { form.setFieldsValue({ backend: value }); form.setFieldValue('env', { ...(option.default_env || {}) }); onBackendChange?.(value, option); }; const renderDeprecatedVersionOptions = (values: any[]) => { if (!values || values.length === 0) { return null; } return ( setShowDeprecated(!showDeprecated)}> {intl.formatMessage({ id: 'models.form.backendVersion.deprecated' })} } > {showDeprecated && values.map((item) => ( {item.label} ))} ); }; const handleOnSaveEnvsOverride = (envs: Record) => { form.setFieldsValue({ env: { ...envs } }); onValuesChange?.({}, form.getFieldsValue()); handleCloseTips(); }; const optionRender = (option: any) => { return option.data.title; }; const labelRender = (option: any) => { return option.title; }; const backendGroupList = useMemo(() => { if (backendOptions.length === 1) { return [...backendOptions[0].options]; } return backendOptions; }, [backendOptions]); return ( <> } options={backendGroupList} optionRender={optionRender} labelRender={labelRender} > {backendOptionsMap.custom !== backend && ( ) } > } onChange={handleBackendVersionOnChange} label={intl.formatMessage({ id: 'models.form.backendVersion' })} footer={
{intl.formatMessage( { id: 'models.form.backendVersions.tips' }, { link: ( navigate('/models/backends')}> {intl.formatMessage({ id: 'backends.title' })} ) } )}
} > {(backendVersions.builtIn.length > 0 || backendVersions.custom.length > 0) && ( {intl.formatMessage({ id: 'common.options.auto' })} )} {renderVersionOptions( backendVersions.builtIn, intl.formatMessage({ id: 'backend.builtin' }) )} {renderVersionOptions( backendVersions.custom, intl.formatMessage({ id: 'models.form.backend.custom' }) )} {renderDeprecatedVersionOptions(backendVersions.deprecated)}
)} ); }; export default BackendFields;