Skip to content

Commit

Permalink
fix: global variable key repeat & value type (#2540)
Browse files Browse the repository at this point in the history
  • Loading branch information
newfish-cmyk authored Aug 27, 2024
1 parent d3731d2 commit 67445b4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
7 changes: 6 additions & 1 deletion packages/global/core/app/type.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { FlowNodeTemplateType, StoreNodeItemType } from '../workflow/type/node';
import { AppTypeEnum } from './constants';
import { PermissionTypeEnum } from '../../support/permission/constant';
import { NodeInputKeyEnum, VariableInputEnum } from '../workflow/constants';
import {
NodeInputKeyEnum,
VariableInputEnum,
WorkflowIOValueTypeEnum
} from '../workflow/constants';
import { SelectedDatasetType } from '../workflow/api';
import { DatasetSearchModeEnum } from '../dataset/constants';
import { TeamTagSchema as TeamTagsSchemaType } from '@fastgpt/global/support/user/team/type.d';
Expand Down Expand Up @@ -111,6 +115,7 @@ export type VariableItemType = {
required: boolean;
maxLen: number;
enums: { value: string }[];
valueType: WorkflowIOValueTypeEnum;
};
// tts
export type AppTTSConfigType = {
Expand Down
1 change: 1 addition & 0 deletions packages/web/i18n/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@
"add option": "Add option",
"input type": "Text",
"key": "Variable key",
"key already exists": "Key already exist",
"key is required": "Variable key is required",
"select type": "Dropdown single select",
"text max length": "Maximum length",
Expand Down
1 change: 1 addition & 0 deletions packages/web/i18n/zh/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@
"add option": "添加选项",
"input type": "文本",
"key": "变量 key",
"key already exists": "Key 已经存在",
"key is required": "变量 key 是必须的",
"select type": "下拉单选",
"text max length": "最大长度",
Expand Down
36 changes: 34 additions & 2 deletions projects/app/src/components/core/app/VariableEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import {
useDisclosure
} from '@chakra-ui/react';
import { SmallAddIcon } from '@chakra-ui/icons';
import { VariableInputEnum, variableMap } from '@fastgpt/global/core/workflow/constants';
import {
VariableInputEnum,
variableMap,
WorkflowIOValueTypeEnum
} from '@fastgpt/global/core/workflow/constants';
import type { VariableItemType } from '@fastgpt/global/core/app/type.d';
import MyIcon from '@fastgpt/web/components/common/Icon';
import { useForm } from 'react-hook-form';
Expand Down Expand Up @@ -310,6 +314,27 @@ const VariableEdit = ({
return;
}
}

// check repeat key
const existingVariable = variables.find(
(item) => item.key === variable.key && item.id !== variable.id
);
if (existingVariable) {
toast({
status: 'warning',
title: t('common:core.module.variable.key already exists')
});
return;
}

// set valuetype based on variable.type
variable.valueType = valueTypeMap[variable.type];

// set default required value based on variableType
if (variable.type === VariableInputEnum.custom) {
variable.required = false;
}

const onChangeVariable = [...variables];
// update
if (variable.id) {
Expand Down Expand Up @@ -344,9 +369,16 @@ export const defaultVariable: VariableItemType = {
type: VariableInputEnum.input,
required: true,
maxLen: 50,
enums: [{ value: '' }]
enums: [{ value: '' }],
valueType: WorkflowIOValueTypeEnum.string
};
export const addVariable = () => {
const newVariable = { ...defaultVariable, key: '', id: '' };
return newVariable;
};
const valueTypeMap = {
[VariableInputEnum.input]: WorkflowIOValueTypeEnum.string,
[VariableInputEnum.select]: WorkflowIOValueTypeEnum.string,
[VariableInputEnum.textarea]: WorkflowIOValueTypeEnum.string,
[VariableInputEnum.custom]: WorkflowIOValueTypeEnum.any
};
5 changes: 1 addition & 4 deletions projects/app/src/web/core/workflow/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,7 @@ export const getWorkflowGlobalVariables = ({
systemConfigNode: getGuideModule(nodes),
isPublicFetch: true
})?.variables || []
).map((item) => ({
...item,
valueType: WorkflowIOValueTypeEnum.any
}));
);

const systemVariables = getSystemVariables(t);

Expand Down

0 comments on commit 67445b4

Please sign in to comment.