Skip to content

Commit

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

* add comment
  • Loading branch information
newfish-cmyk authored and c121914yu committed Oct 14, 2024
1 parent 6251887 commit 64e1e22
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 31 deletions.
8 changes: 7 additions & 1 deletion packages/global/core/workflow/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,13 @@ export function replaceEditorVariable({
}
];
}
return [];
return [
{
id: item.key,
value: item.value,
nodeId: runningNode.nodeId
}
];
});

const allVariables = [...globalVariables, ...nodeVariables, ...customInputs];
Expand Down
1 change: 1 addition & 0 deletions projects/app/src/components/core/app/VariableEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ const VariableEdit = ({
}}
onClick={() => {
setValue('type', item.value);
setValue('defaultValue', undefined);
}}
>
<MyIcon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ const VariableInput = ({
}, [variableList]);

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { storeNodes2RuntimeNodes } from '@fastgpt/global/core/workflow/runtime/utils';
import { StoreNodeItemType } from '@fastgpt/global/core/workflow/type/node';
import { RuntimeEdgeItemType, StoreEdgeItemType } from '@fastgpt/global/core/workflow/type/edge';
import { useCallback, useState, useMemo } from 'react';
import { useCallback, useState, useMemo, useEffect } from 'react';
import { checkWorkflowNodeAndConnection } from '@/web/core/workflow/utils';
import { useTranslation } from 'next-i18next';
import { useToast } from '@fastgpt/web/hooks/useToast';
Expand Down Expand Up @@ -61,6 +61,16 @@ export const useDebug = () => {
return variables?.filter((item) => item.type !== VariableInputEnum.custom) || [];
}, [appDetail.chatConfig.variables]);

const [defaultGlobalVariables, setDefaultGlobalVariables] = useState<Record<string, any>>(
filteredVar.reduce(
(acc, item) => {
acc[item.key] = item.defaultValue;
return acc;
},
{} as Record<string, any>
)
);

const [runtimeNodeId, setRuntimeNodeId] = useState<string>();
const [runtimeNodes, setRuntimeNodes] = useState<RuntimeNodeItemType[]>();
const [runtimeEdges, setRuntimeEdges] = useState<RuntimeEdgeItemType[]>();
Expand Down Expand Up @@ -126,6 +136,19 @@ export const useDebug = () => {
const DebugInputModal = useCallback(() => {
if (!runtimeNodes || !runtimeEdges) return <></>;

const [currentTab, setCurrentTab] = useState<TabEnum>(TabEnum.node);

// Check if the required global variables exist, if missing, switch to the global variable tab
useEffect(() => {
if (
filteredVar.some(
(item) => item.required && defaultGlobalVariables && !defaultGlobalVariables[item.key]
)
) {
setCurrentTab(TabEnum.global);
}
}, []);

const runtimeNode = runtimeNodes.find((node) => node.nodeId === runtimeNodeId);

if (!runtimeNode) return <></>;
Expand All @@ -136,18 +159,21 @@ export const useDebug = () => {
});

const variablesForm = useForm<Record<string, any>>({
defaultValues: renderInputs.reduce((acc: Record<string, any>, input) => {
const isReference = checkInputIsReference(input);
if (isReference) {
acc[input.key] = undefined;
} else if (typeof input.value === 'object') {
acc[input.key] = JSON.stringify(input.value, null, 2);
} else {
acc[input.key] = input.value;
}
defaultValues: {
...renderInputs.reduce((acc: Record<string, any>, input) => {
const isReference = checkInputIsReference(input);
if (isReference) {
acc[input.key] = undefined;
} else if (typeof input.value === 'object') {
acc[input.key] = JSON.stringify(input.value, null, 2);
} else {
acc[input.key] = input.value;
}

return acc;
}, {})
return acc;
}, {}),
...defaultGlobalVariables
}
});
const { register, getValues, setValue, handleSubmit } = variablesForm;

Expand All @@ -158,7 +184,6 @@ export const useDebug = () => {
};

const onClickRun = (data: Record<string, any>) => {
console.log('data', data);
onStartNodeDebug({
entryNodeId: runtimeNode.nodeId,
runtimeNodes: runtimeNodes.map((node) =>
Expand Down Expand Up @@ -196,11 +221,17 @@ export const useDebug = () => {
Object.entries(data).filter(([key]) => filteredVar.some((item) => item.key === key))
)
});

// Filter global variables and set them as default global variable values
setDefaultGlobalVariables(
Object.fromEntries(
Object.entries(data).filter(([key]) => filteredVar.some((item) => item.key === key))
)
);

onClose();
};

const [currentTab, setCurrentTab] = useState<TabEnum>(TabEnum.node);

return (
<MyRightDrawer
onClose={onClose}
Expand Down Expand Up @@ -313,7 +344,15 @@ export const useDebug = () => {
</Flex>
</MyRightDrawer>
);
}, [filteredVar, onStartNodeDebug, runtimeEdges, runtimeNodeId, runtimeNodes, t]);
}, [
defaultGlobalVariables,
filteredVar,
onStartNodeDebug,
runtimeEdges,
runtimeNodeId,
runtimeNodes,
t
]);

return {
DebugInputModal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ const NodeComment = ({ data }: NodeProps<FlowNodeItemType>) => {
menuForbid={{
debug: true
}}
border={'none'}
rounded={'none'}
bg={'#D8E9FF'}
boxShadow={
'0px 4px 10px 0px rgba(19, 51, 107, 0.10), 0px 0px 1px 0px rgba(19, 51, 107, 0.10)'
}
customStyle={{
border: 'none',
rounded: 'none',
bg: '#D8E9FF',
boxShadow:
'0px 4px 10px 0px rgba(19, 51, 107, 0.10), 0px 0px 1px 0px rgba(19, 51, 107, 0.10)'
}}
>
<Box w={'full'} h={'full'} position={'relative'}>
<Box
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ const InputTypeConfig = ({
<NumberInput flex={1} step={1} min={min} max={max} position={'relative'}>
<NumberInputField
{...register('defaultValue', {
required: true,
min: min,
max: max,
valueAsNumber: true
Expand Down Expand Up @@ -328,10 +327,12 @@ const InputTypeConfig = ({
{inputType === FlowNodeInputTypeEnum.switch && <Switch {...register('defaultValue')} />}
{inputType === FlowNodeInputTypeEnum.select && (
<MySelect<string>
list={[defaultListValue, ...listValue].map((item) => ({
label: item.label,
value: item.value
}))}
list={[defaultListValue, ...listValue]
.filter((item) => item.label !== '')
.map((item) => ({
label: item.label,
value: item.value
}))}
value={
defaultValue && listValue.map((item) => item.value).includes(defaultValue)
? defaultValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type Props = FlowNodeItemType & {
copy?: boolean;
delete?: boolean;
};
} & Omit<FlexProps, 'children'>;
customStyle?: FlexProps;
};

const NodeCard = (props: Props) => {
const { t } = useTranslation();
Expand All @@ -65,7 +66,7 @@ const NodeCard = (props: Props) => {
isError = false,
debugResult,
isFolded,
...customStyle
customStyle
} = props;
const nodeList = useContextSelector(WorkflowContext, (v) => v.nodeList);
const setHoverNodeId = useContextSelector(WorkflowContext, (v) => v.setHoverNodeId);
Expand Down

0 comments on commit 64e1e22

Please sign in to comment.