Skip to content

Commit

Permalink
fix code run node reset template
Browse files Browse the repository at this point in the history
  • Loading branch information
newfish-cmyk committed Jan 20, 2025
1 parent f44c8ad commit ccb628f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 67 deletions.
3 changes: 2 additions & 1 deletion docSite/content/zh-cn/docs/development/upgrading/4819.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ weight: 806
4. 优化 - 统一分页加载代码。
5. 优化 - 对话页面加载时,可配置是否为独立页面。
6. 修复 - 语雀文件库导入时,嵌套文件内容无法展开的问题。
7. 修复 - 工作流编排中,LLM 参数无法关闭问题。
7. 修复 - 工作流编排中,LLM 参数无法关闭问题。
8. 修复 - 工作流编排中,代码运行节点还原模板问题。
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@ import RenderToolInput from './render/RenderToolInput';
import RenderOutput from './render/RenderOutput';
import CodeEditor from '@fastgpt/web/components/common/Textarea/CodeEditor';
import { Box, Flex } from '@chakra-ui/react';
import { useI18n } from '@/web/context/I18n';
import { useConfirm } from '@fastgpt/web/hooks/useConfirm';
import { getLatestNodeTemplate } from '@/web/core/workflow/utils';
import { CodeNode } from '@fastgpt/global/core/workflow/template/system/sandbox';

const NodeCode = ({ data, selected }: NodeProps<FlowNodeItemType>) => {
const { t } = useTranslation();
const { workflowT } = useI18n();
const { nodeId, inputs, outputs } = data;
const { splitToolInputs, onChangeNode, onResetNode } = useContextSelector(
WorkflowContext,
(ctx) => ctx
);

const splitToolInputs = useContextSelector(WorkflowContext, (ctx) => ctx.splitToolInputs);
const onChangeNode = useContextSelector(WorkflowContext, (ctx) => ctx.onChangeNode);
const onResetNode = useContextSelector(WorkflowContext, (ctx) => ctx.onResetNode);

const { ConfirmModal, openConfirm } = useConfirm({
content: workflowT('code.Reset template confirm')
content: t('workflow:code.Reset template confirm')
});

const CustomComponent = useMemo(() => {
Expand All @@ -38,19 +36,20 @@ const NodeCode = ({ data, selected }: NodeProps<FlowNodeItemType>) => {
return (
<Box mt={-3}>
<Flex mb={2} alignItems={'flex-end'}>
<Box flex={'1'}>{'Javascript ' + workflowT('Code')}</Box>
<Box flex={'1'}>{'Javascript ' + t('workflow:Code')}</Box>
<Box
cursor={'pointer'}
color={'primary.500'}
fontSize={'xs'}
onClick={openConfirm(() => {
onResetNode({
id: nodeId,
node: getLatestNodeTemplate(data, CodeNode)
node: getLatestNodeTemplate(data, CodeNode),
isResetValue: true
});
})}
>
{workflowT('code.Reset template')}
{t('workflow:code.Reset template')}
</Box>
</Flex>
<CodeEditor
Expand All @@ -73,37 +72,33 @@ const NodeCode = ({ data, selected }: NodeProps<FlowNodeItemType>) => {
);
}
};
}, [data, nodeId, onChangeNode, onResetNode, openConfirm, workflowT]);
}, [data, nodeId, onChangeNode, onResetNode, openConfirm, t]);

const Render = useMemo(() => {
const { isTool, commonInputs } = splitToolInputs(inputs, nodeId);
const { isTool, commonInputs } = splitToolInputs(inputs, nodeId);

return (
<NodeCard minW={'400px'} selected={selected} {...data}>
{isTool && (
<>
<Container>
<RenderToolInput nodeId={nodeId} inputs={inputs} />
</Container>
</>
)}
<Container>
<IOTitle text={t('common:common.Input')} mb={-1} />
<RenderInput
nodeId={nodeId}
flowInputList={commonInputs}
CustomComponent={CustomComponent}
/>
</Container>
<Container>
<IOTitle text={t('common:common.Output')} />
<RenderOutput nodeId={nodeId} flowOutputList={outputs} />
</Container>
<ConfirmModal />
</NodeCard>
);
}, [ConfirmModal, CustomComponent, data, inputs, nodeId, outputs, selected, splitToolInputs, t]);

return Render;
return (
<NodeCard minW={'400px'} selected={selected} {...data}>
{isTool && (
<>
<Container>
<RenderToolInput nodeId={nodeId} inputs={inputs} />
</Container>
</>
)}
<Container>
<IOTitle text={t('common:common.Input')} mb={-1} />
<RenderInput
nodeId={nodeId}
flowInputList={commonInputs}
CustomComponent={CustomComponent}
/>
</Container>
<Container>
<IOTitle text={t('common:common.Output')} />
<RenderOutput nodeId={nodeId} flowOutputList={outputs} />
</Container>
<ConfirmModal />
</NodeCard>
);
};
export default React.memo(NodeCode);
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type WorkflowContextType = {
hasToolNode: boolean;

onUpdateNodeError: (node: string, isError: Boolean) => void;
onResetNode: (e: { id: string; node: FlowNodeTemplateType }) => void;
onResetNode: (e: { id: string; node: FlowNodeTemplateType; isResetValue?: boolean }) => void;
onChangeNode: (e: FlowNodeChangeProps) => void;
getNodeDynamicInputs: (nodeId: string) => FlowNodeInputItemType[];

Expand Down Expand Up @@ -387,30 +387,42 @@ const WorkflowContextProvider = ({
});

// reset a node data. delete edge and replace it
const onResetNode = useMemoizedFn(({ id, node }: { id: string; node: FlowNodeTemplateType }) => {
setNodes((state) =>
state.map((item) => {
if (item.id === id) {
return {
...item,
data: {
...item.data,
...node,
inputs: node.inputs.map((input) => {
const value =
item.data.inputs.find((i) => i.key === input.key)?.value ?? input.value;
return {
...input,
value
};
})
}
};
}
return item;
})
);
});
const onResetNode = useMemoizedFn(
({
id,
node,
isResetValue = false
}: {
id: string;
node: FlowNodeTemplateType;
isResetValue?: boolean;
}) => {
setNodes((state) =>
state.map((item) => {
if (item.id === id) {
return {
...item,
data: {
...item.data,
...node,
inputs: node.inputs.map((input) => {
const value = isResetValue
? input.value
: item.data.inputs.find((i) => i.key === input.key)?.value ?? input.value;

return {
...input,
value
};
})
}
};
}
return item;
})
);
}
);

const onChangeNode = useMemoizedFn((props: FlowNodeChangeProps) => {
const { nodeId, type } = props;
Expand Down
4 changes: 3 additions & 1 deletion projects/app/src/web/core/workflow/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,9 @@ export const getLatestNodeTemplate = (
...node,
...template,
inputs: template.inputs.map((templateItem) => {
const nodeItem = node.inputs.find((item) => item.key === templateItem.key);
const nodeItem = node.inputs
.filter((item) => item.key !== NodeInputKeyEnum.code)
.find((item) => item.key === templateItem.key);
if (nodeItem) {
return {
...templateItem,
Expand Down

0 comments on commit ccb628f

Please sign in to comment.