Skip to content

Commit

Permalink
fix: global variables (#2892)
Browse files Browse the repository at this point in the history
* fix global variables

* fix type
  • Loading branch information
newfish-cmyk authored Oct 12, 2024
1 parent ebda977 commit 07be4cc
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 152 deletions.
7 changes: 0 additions & 7 deletions packages/global/core/workflow/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ export enum VariableInputEnum {
textarea = 'textarea',
numberInput = 'numberInput',
select = 'select',
switch = 'switch',
custom = 'custom'
}
export const variableMap: Record<
Expand Down Expand Up @@ -301,12 +300,6 @@ export const variableMap: Record<
value: VariableInputEnum.numberInput,
defaultValueType: WorkflowIOValueTypeEnum.number
},
[VariableInputEnum.switch]: {
icon: 'core/workflow/inputType/switch',
label: i18nT('common:core.workflow.inputType.switch'),
value: VariableInputEnum.switch,
defaultValueType: WorkflowIOValueTypeEnum.boolean
},
[VariableInputEnum.select]: {
icon: 'core/workflow/inputType/option',
label: i18nT('common:core.workflow.inputType.select'),
Expand Down
1 change: 0 additions & 1 deletion packages/global/core/workflow/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ export const appData2FlowNodeIO = ({
],
[VariableInputEnum.numberInput]: [FlowNodeInputTypeEnum.numberInput],
[VariableInputEnum.select]: [FlowNodeInputTypeEnum.select],
[VariableInputEnum.switch]: [FlowNodeInputTypeEnum.switch],
[VariableInputEnum.custom]: [
FlowNodeInputTypeEnum.input,
FlowNodeInputTypeEnum.reference
Expand Down
2 changes: 1 addition & 1 deletion packages/web/i18n/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"Login": "Login",
"Move": "Move",
"Name": "Name",
"None": "None",
"Rename": "Rename",
"Resume": "Resume",
"Running": "Running",
Expand Down Expand Up @@ -780,7 +781,6 @@
"core.workflow.Stop debug": "Stop Debugging",
"core.workflow.Success": "Run Successful",
"core.workflow.Value type": "Data Type",
"core.workflow.Variable.Variable type": "Variable Type",
"core.workflow.debug.Done": "Debugging Completed",
"core.workflow.debug.Hide result": "Hide Result",
"core.workflow.debug.Not result": "No Run Result",
Expand Down
2 changes: 2 additions & 0 deletions packages/web/i18n/en/workflow.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"Array_element": "Array element",
"Code": "Code",
"Confirm_sync_node": "It will be updated to the latest node configuration and fields that do not exist in the template will be deleted (including all custom fields).\n\nIf the fields are complex, it is recommended that you copy a node first and then update the original node to facilitate parameter copying.",
"Node_variables": "Node variables",
"Quote_prompt_setting": "Quote prompt",
"Variable.Variable type": "Variable type",
"Variable_name": "Variable name",
"add_new_input": "Add New Input",
"add_new_output": "New output",
Expand Down
2 changes: 1 addition & 1 deletion packages/web/i18n/zh/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"Login": "登录",
"Move": "移动",
"Name": "名称",
"None": "",
"Rename": "重命名",
"Resume": "恢复",
"Running": "运行中",
Expand Down Expand Up @@ -785,7 +786,6 @@
"core.workflow.Stop debug": "停止调试",
"core.workflow.Success": "运行成功",
"core.workflow.Value type": "数据类型",
"core.workflow.Variable.Variable type": "变量类型",
"core.workflow.debug.Done": "完成调试",
"core.workflow.debug.Hide result": "隐藏结果",
"core.workflow.debug.Not result": "无运行结果",
Expand Down
2 changes: 2 additions & 0 deletions packages/web/i18n/zh/workflow.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"Array_element": "数组元素",
"Code": "代码",
"Confirm_sync_node": "将会更新至最新的节点配置,不存在模板中的字段将会被删除(包括所有自定义字段)。\n如果字段较为复杂,建议您先复制一份节点,再更新原来的节点,便于参数复制。",
"Node_variables": "节点变量",
"Quote_prompt_setting": "引用提示词配置",
"Variable.Variable type": "变量类型",
"Variable_name": "变量名",
"add_new_input": "新增输入",
"add_new_output": "新增输出",
Expand Down
7 changes: 2 additions & 5 deletions projects/app/src/components/core/app/VariableEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ const VariableEdit = ({
if (data.type === VariableInputEnum.numberInput) {
data.valueType = WorkflowIOValueTypeEnum.number;
}
if (data.type === VariableInputEnum.switch) {
data.valueType = WorkflowIOValueTypeEnum.boolean;
}

const onChangeVariable = [...variables];
if (data.id) {
Expand Down Expand Up @@ -261,7 +258,7 @@ const VariableEdit = ({
<Flex h={'560px'}>
<Stack gap={4} p={8}>
<FormLabel color={'myGray.600'} fontWeight={'medium'}>
{t('common:core.module.Input Type')}
{t('workflow:Variable.Variable type')}
</FormLabel>
<Flex flexDirection={'column'} gap={4}></Flex>
<Box display={'grid'} gridTemplateColumns={'repeat(2, 1fr)'} gap={4}>
Expand Down Expand Up @@ -318,7 +315,7 @@ const VariableEdit = ({
<InputTypeConfig
form={form}
type={'variable'}
isEdit={!!value.label}
isEdit={!!value.key}
inputType={type}
valueType={valueType}
defaultValue={defaultValue}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useCallback, useEffect, useMemo } from 'react';
import { Controller, UseFormReturn } from 'react-hook-form';
import { useTranslation } from 'next-i18next';
import {
Expand All @@ -11,7 +11,6 @@ import {
NumberInput,
NumberInputField,
NumberInputStepper,
Switch,
Textarea
} from '@chakra-ui/react';
import ChatAvatar from './ChatAvatar';
Expand All @@ -23,6 +22,7 @@ import { ChatBoxInputFormType } from '../type.d';
import { useContextSelector } from 'use-context-selector';
import { ChatBoxContext } from '../Provider';
import QuestionTip from '@fastgpt/web/components/common/MyTooltip/QuestionTip';
import { useDeepCompareEffect } from 'ahooks';

export const VariableInputItem = ({
item,
Expand Down Expand Up @@ -98,32 +98,30 @@ export const VariableInputItem = ({
/>
)}
{item.type === VariableInputEnum.numberInput && (
<NumberInput
step={1}
min={item.min}
max={item.max}
bg={'white'}
rounded={'md'}
clampValueOnBlur={false}
defaultValue={item.defaultValue}
>
<NumberInputField
bg={'white'}
{...register(item.key, {
required: item.required,
min: item.min,
max: item.max,
valueAsNumber: true
})}
/>
<NumberInputStepper>
<NumberIncrementStepper />
<NumberDecrementStepper />
</NumberInputStepper>
</NumberInput>
)}
{item.type === VariableInputEnum.switch && (
<Switch {...register(item.key)} defaultChecked={!!item.defaultValue} />
<Controller
key={item.key}
control={control}
name={item.key}
rules={{ required: item.required, min: item.min, max: item.max }}
render={({ field: { ref, value, onChange } }) => (
<NumberInput
step={1}
min={item.min}
max={item.max}
bg={'white'}
rounded={'md'}
clampValueOnBlur={false}
value={value}
onChange={(valueString) => onChange(Number(valueString))}
>
<NumberInputField ref={ref} bg={'white'} />
<NumberInputStepper>
<NumberIncrementStepper />
<NumberDecrementStepper />
</NumberInputStepper>
</NumberInput>
)}
/>
)}
</Box>
);
Expand All @@ -141,20 +139,17 @@ const VariableInput = ({
const { appAvatar, variableList, variablesForm } = useContextSelector(ChatBoxContext, (v) => v);
const { reset, handleSubmit: handleSubmitChat } = variablesForm;

React.useEffect(() => {
const defaultValues: Record<string, any> = {};
variableList.forEach((item) => {
if (item.defaultValue !== undefined && !variablesForm.getValues(item.key)) {
defaultValues[item.key] = item.defaultValue;
}
});
if (Object.keys(defaultValues).length > 0) {
reset((formValues) => ({
...formValues,
...defaultValues
}));
}
}, [variableList, reset, variablesForm]);
const defaultValues = useMemo(() => {
return variableList.reduce((acc: Record<string, any>, item) => {
acc[item.key] = item.defaultValue;
return acc;
}, {});
}, [variableList]);

useDeepCompareEffect(() => {
console.log('defaultValues', defaultValues);
reset(defaultValues);
}, [defaultValues]);

return (
<Box py={3}>
Expand Down
Loading

0 comments on commit 07be4cc

Please sign in to comment.