fix: edit community backend
This commit is contained in:
@@ -7,7 +7,7 @@ import { PageActionType } from '@/config/types';
|
||||
import { useIntl } from '@umijs/max';
|
||||
import { Tabs } from 'antd';
|
||||
import _ from 'lodash';
|
||||
import React, { useEffect, useId, useRef, useState } from 'react';
|
||||
import React, { useEffect, useId, useMemo, useRef, useState } from 'react';
|
||||
import styled from 'styled-components';
|
||||
import ColumnWrapper from '../../_components/column-wrapper';
|
||||
import {
|
||||
@@ -41,6 +41,14 @@ interface AddModalProps {
|
||||
title?: string;
|
||||
}
|
||||
|
||||
const versionFields = [
|
||||
'image_name',
|
||||
'run_command',
|
||||
'custom_framework',
|
||||
'entrypoint',
|
||||
'env'
|
||||
];
|
||||
|
||||
const AddModal: React.FC<AddModalProps> = (props) => {
|
||||
const { action, currentData, onClose, onSubmit, onSubmitYaml, open, title } =
|
||||
props;
|
||||
@@ -52,25 +60,29 @@ const AddModal: React.FC<AddModalProps> = (props) => {
|
||||
const [yamlContent, setYamlContent] = useState<string>('');
|
||||
const [formContent, setFormContent] = useState<FormData>({} as FormData);
|
||||
|
||||
const versionFields = [
|
||||
'image_name',
|
||||
'run_command',
|
||||
'custom_framework',
|
||||
'entrypoint',
|
||||
'env'
|
||||
];
|
||||
const showVersionCustomSuffix =
|
||||
currentData?.backend_source === BackendSourceValueMap.BUILTIN ||
|
||||
currentData?.backend_source === BackendSourceValueMap.COMMUNITY;
|
||||
|
||||
const backendSource = useMemo(
|
||||
() => currentData?.backend_source || BackendSourceValueMap.CUSTOM,
|
||||
[currentData]
|
||||
);
|
||||
|
||||
// remove '-custom' suffix from version_no in currentData, when action is EDIT
|
||||
const genertateCurrentVersionData = (values: ListItem): ListItem => {
|
||||
const data = { ...values };
|
||||
data.version_configs = Object.entries(data.version_configs || {}).reduce(
|
||||
(acc, [key, value]) => {
|
||||
const version = key.replace(/-custom$/, '');
|
||||
acc[version] = { ...value, backend_source: data.backend_source };
|
||||
return acc;
|
||||
},
|
||||
{} as any
|
||||
);
|
||||
|
||||
if (showVersionCustomSuffix) {
|
||||
data.version_configs = Object.entries(data.version_configs || {}).reduce(
|
||||
(acc, [key, value]) => {
|
||||
const version = key.replace(/-custom$/, '');
|
||||
acc[version] = { ...value, backend_source: data.backend_source };
|
||||
return acc;
|
||||
},
|
||||
{} as any
|
||||
);
|
||||
}
|
||||
|
||||
return data;
|
||||
};
|
||||
@@ -267,7 +279,7 @@ const AddModal: React.FC<AddModalProps> = (props) => {
|
||||
<ImportYAML
|
||||
actionStatus={{
|
||||
action: action,
|
||||
isBuiltIn: currentData?.is_built_in || false
|
||||
backendSource: backendSource
|
||||
}}
|
||||
ref={editorRef}
|
||||
content={yamlContent}
|
||||
|
||||
@@ -8,7 +8,7 @@ import React, {
|
||||
useRef,
|
||||
useState
|
||||
} from 'react';
|
||||
import { yaml2Json, yamlTemplate } from '../config';
|
||||
import { BackendSourceValueMap, yaml2Json, yamlTemplate } from '../config';
|
||||
import createSchema from '../config/schema/create.json';
|
||||
import updateBuiltinSchema from '../config/schema/update-builtin.json';
|
||||
import udpateCustomSchema from '../config/schema/update-custom.json';
|
||||
@@ -17,7 +17,7 @@ interface ImportYAMLProps {
|
||||
ref?: any;
|
||||
actionStatus: {
|
||||
action: PageActionType;
|
||||
isBuiltIn: boolean;
|
||||
backendSource: string;
|
||||
};
|
||||
content?: string;
|
||||
onSubmit?: (content: string) => void;
|
||||
@@ -53,7 +53,7 @@ const ImportYAML: React.FC<ImportYAMLProps> = forwardRef(
|
||||
console.log('exsistingVersions', jsonData);
|
||||
// Check backend version rules
|
||||
if (
|
||||
actionStatus.isBuiltIn &&
|
||||
actionStatus.backendSource === BackendSourceValueMap.BUILTIN &&
|
||||
exsistingVersions.find((v) => !v?.endsWith('-custom'))
|
||||
) {
|
||||
setError(intl.formatMessage({ id: 'backend.version.no.tips' }));
|
||||
@@ -62,7 +62,7 @@ const ImportYAML: React.FC<ImportYAMLProps> = forwardRef(
|
||||
|
||||
// Check custom backend name rule
|
||||
if (
|
||||
!actionStatus.isBuiltIn &&
|
||||
actionStatus.backendSource === BackendSourceValueMap.CUSTOM &&
|
||||
actionStatus.action === PageAction.CREATE &&
|
||||
!jsonData.backend_name?.endsWith('-custom')
|
||||
) {
|
||||
@@ -74,7 +74,7 @@ const ImportYAML: React.FC<ImportYAMLProps> = forwardRef(
|
||||
if (
|
||||
jsonData.default_version &&
|
||||
!exsistingVersions.includes(jsonData.default_version) &&
|
||||
!actionStatus.isBuiltIn
|
||||
actionStatus.backendSource === BackendSourceValueMap.CUSTOM
|
||||
) {
|
||||
// the default_version must be in [existingVersions]
|
||||
setError(
|
||||
@@ -118,7 +118,7 @@ const ImportYAML: React.FC<ImportYAMLProps> = forwardRef(
|
||||
schema={
|
||||
actionStatus.action === PageAction.CREATE
|
||||
? createSchema
|
||||
: actionStatus.isBuiltIn
|
||||
: actionStatus.backendSource === BackendSourceValueMap.BUILTIN
|
||||
? updateBuiltinSchema
|
||||
: udpateCustomSchema
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { createContext, useContext } from 'react';
|
||||
interface FormContextProps {
|
||||
action: PageActionType;
|
||||
backendSource: string;
|
||||
showCustomSuffix: boolean;
|
||||
}
|
||||
|
||||
export const FormContext = createContext<FormContextProps | null>(
|
||||
|
||||
@@ -22,6 +22,10 @@ const BackendForm: React.FC<AddModalProps> = forwardRef(
|
||||
const [activeKey, setActiveKey] = React.useState<string[]>([]);
|
||||
const backendSource = Form.useWatch('backend_source', form);
|
||||
|
||||
const showCustomSuffix =
|
||||
currentData?.backend_source === BackendSourceValueMap.BUILTIN ||
|
||||
currentData?.backend_source === BackendSourceValueMap.COMMUNITY;
|
||||
|
||||
const onFinishFailed = (errorInfo: any) => {
|
||||
const errorFields = errorInfo.errorFields || [];
|
||||
if (errorFields.length > 0) {
|
||||
@@ -44,14 +48,18 @@ const BackendForm: React.FC<AddModalProps> = forwardRef(
|
||||
? `${values.backend_name}-custom`
|
||||
: values.backend_name
|
||||
};
|
||||
|
||||
// add '-custom' suffix to version_no in version_configs when backendSource is BUILTIN or COMMUNITY
|
||||
data.version_configs = data.version_configs?.map((item) => {
|
||||
if (item.version_no) {
|
||||
return {
|
||||
...item,
|
||||
version_no:
|
||||
backendSource === BackendSourceValueMap.BUILTIN
|
||||
? `${item.version_no}-custom`
|
||||
: item.version_no
|
||||
version_no: [
|
||||
BackendSourceValueMap.BUILTIN,
|
||||
BackendSourceValueMap.COMMUNITY
|
||||
].includes(backendSource)
|
||||
? `${item.version_no}-custom`
|
||||
: item.version_no
|
||||
};
|
||||
}
|
||||
return item;
|
||||
@@ -85,7 +93,11 @@ const BackendForm: React.FC<AddModalProps> = forwardRef(
|
||||
|
||||
return (
|
||||
<FormContext.Provider
|
||||
value={{ action: action, backendSource: backendSource }}
|
||||
value={{
|
||||
action: action,
|
||||
backendSource: backendSource,
|
||||
showCustomSuffix: showCustomSuffix
|
||||
}}
|
||||
>
|
||||
<Form
|
||||
name="basicForm"
|
||||
|
||||
@@ -67,7 +67,7 @@ const VersionsForm: React.FC<AddModalProps> = ({
|
||||
const defaultCollapseKey =
|
||||
action === 'edit' ? new Set<number>() : new Set([0]);
|
||||
const form = Form.useFormInstance();
|
||||
const { backendSource } = useFormContext();
|
||||
const { backendSource, showCustomSuffix } = useFormContext();
|
||||
const version_configs = Form.useWatch('version_configs', form);
|
||||
const [collapseKey, setCollapseKey] =
|
||||
React.useState<Set<number | string>>(defaultCollapseKey);
|
||||
@@ -329,7 +329,7 @@ const VersionsForm: React.FC<AddModalProps> = ({
|
||||
>
|
||||
<SealInput.Input
|
||||
trim
|
||||
addAfter={isBuiltin ? '-custom' : null}
|
||||
addAfter={showCustomSuffix ? '-custom' : null}
|
||||
onChange={handleVersionChange}
|
||||
label={intl.formatMessage({ id: 'backend.version' })}
|
||||
required
|
||||
|
||||
@@ -60,7 +60,7 @@ const useCreateBackend = () => {
|
||||
closeBackendModal: handleCloseModal,
|
||||
addBackend: handleAddBackend,
|
||||
editBackend: handleEditBackend,
|
||||
openBackendModalStatus: openCustomModalStatus, // for create custom backend and edit
|
||||
openBackendModalStatus: openCustomModalStatus, // for create custom backend and edit custom/builtin/community backend
|
||||
openCommunityModalStatus: openCommunityModalStatus, // for create community backend
|
||||
addActions: addItems
|
||||
};
|
||||
|
||||
@@ -175,6 +175,20 @@ const generateEnvArgs = (params: any) => {
|
||||
return envArgs;
|
||||
};
|
||||
|
||||
// concat the args, the args is a key-value
|
||||
const generateExtraArgs = (params: any) => {
|
||||
const args = params.registrationInfo?.args || {};
|
||||
const argsList = Object.entries(args);
|
||||
if (argsList.length === 0) {
|
||||
return '';
|
||||
}
|
||||
let argsStr = '';
|
||||
argsList.forEach(([key, value]) => {
|
||||
argsStr += `${key} ${value} \\\n `;
|
||||
});
|
||||
return argsStr;
|
||||
};
|
||||
|
||||
const generateExtraModelDirArg = (modelDir: string) => {
|
||||
const pathList = modelDir
|
||||
?.split(',')
|
||||
@@ -208,7 +222,8 @@ const setWorkerIPArg = (params: any) => {
|
||||
|
||||
const setImageArgs = (params: any) => {
|
||||
return `${params.image} \\
|
||||
--server-url ${params.server} \\`;
|
||||
--server-url ${params.server} \\
|
||||
${generateExtraArgs(params)}`;
|
||||
};
|
||||
|
||||
// avaliable for NVIDIA、MThreads
|
||||
|
||||
Reference in New Issue
Block a user