diff --git a/web/app/components/workflow/hooks/use-nodes-interactions.ts b/web/app/components/workflow/hooks/use-nodes-interactions.ts index a4372aef32..1835096533 100644 --- a/web/app/components/workflow/hooks/use-nodes-interactions.ts +++ b/web/app/components/workflow/hooks/use-nodes-interactions.ts @@ -52,6 +52,7 @@ import { getNodeCustomTypeByNodeDataType, getNodesConnectedSourceOrTargetHandleIdsMap, getTopLeftNodePosition, + parseNestedNodeId, } from '../utils' import { useWorkflowHistoryStore } from '../workflow-history-store' import { useAutoGenerateWebhookUrl } from './use-auto-generate-webhook-url' @@ -350,13 +351,9 @@ export const useNodesInteractions = () => { }, [configsMap?.flowId]) const cleanupContextGenStorage = useCallback((nodeId: string, nodeData?: Node['data']) => { - const extSeparator = '_ext_' - const extIndex = nodeId.indexOf(extSeparator) - if (extIndex > 0) { - const toolNodeId = nodeId.slice(0, extIndex) - const paramKey = nodeId.slice(extIndex + extSeparator.length) - clearContextGenStorageByParam(toolNodeId, paramKey) - } + const parsed = parseNestedNodeId(nodeId) + if (parsed) + clearContextGenStorageByParam(parsed.parentId, parsed.paramKey) if (nodeData?.type !== BlockEnum.Tool) return diff --git a/web/app/components/workflow/nodes/_base/components/code-generator-button.tsx b/web/app/components/workflow/nodes/_base/components/code-generator-button.tsx index df5cb55b79..e65a3d431d 100644 --- a/web/app/components/workflow/nodes/_base/components/code-generator-button.tsx +++ b/web/app/components/workflow/nodes/_base/components/code-generator-button.tsx @@ -14,6 +14,7 @@ import { cn } from '@/utils/classnames' import { useHooksStore } from '../../../hooks-store' import { useStore } from '../../../store' import { BlockEnum } from '../../../types' +import { parseNestedNodeId } from '../../../utils' import ContextGenerateModal from '../../tool/components/context-generate-modal' type Props = { @@ -40,23 +41,11 @@ const CodeGenerateBtn: FC = ({ }, [onGenerated, showAutomaticFalse]) const configsMap = useHooksStore(s => s.configsMap) - const parseExtractorNodeId = useCallback((id: string) => { - const marker = '_ext_' - const index = id.lastIndexOf(marker) - if (index < 0) - return null - const parentId = id.slice(0, index) - const paramKey = id.slice(index + marker.length) - if (!parentId || !paramKey) - return null - return { parentId, paramKey } - }, []) - const contextGenerateConfig = useMemo(() => { const targetNode = nodes.find(node => node.id === nodeId) const isCodeNode = targetNode?.data?.type === BlockEnum.Code const parentNodeId = (targetNode?.data as { parent_node_id?: string })?.parent_node_id - const parsed = parseExtractorNodeId(nodeId) + const parsed = parseNestedNodeId(nodeId) if (!isCodeNode || !parentNodeId || !parsed?.paramKey) return null return { @@ -64,7 +53,7 @@ const CodeGenerateBtn: FC = ({ paramKey: parsed.paramKey, codeNodeId: nodeId, } - }, [nodeId, nodes, parseExtractorNodeId]) + }, [nodeId, nodes]) const handleOpenAutomatic = useCallback(() => { showAutomaticTrue() diff --git a/web/app/components/workflow/utils/index.ts b/web/app/components/workflow/utils/index.ts index 3fd8e5d604..5445144696 100644 --- a/web/app/components/workflow/utils/index.ts +++ b/web/app/components/workflow/utils/index.ts @@ -3,6 +3,7 @@ export * from './data-source' export * from './edge' export * from './elk-layout' export * from './gen-node-meta-data' +export * from './nested-node-id' export * from './node' export * from './tool' export * from './tool-token' diff --git a/web/app/components/workflow/utils/nested-node-id.ts b/web/app/components/workflow/utils/nested-node-id.ts new file mode 100644 index 0000000000..890a98c49c --- /dev/null +++ b/web/app/components/workflow/utils/nested-node-id.ts @@ -0,0 +1,27 @@ +export const NESTED_NODE_SEPARATOR = '_ext_' + +export type NestedNodeIdParts = { + parentId: string + paramKey: string +} + +export const buildNestedNodeId = (parentId: string, paramKey: string) => { + if (!parentId || !paramKey) + return '' + return `${parentId}${NESTED_NODE_SEPARATOR}${paramKey}` +} + +export const parseNestedNodeId = (id?: string): NestedNodeIdParts | null => { + if (!id) + return null + const index = id.lastIndexOf(NESTED_NODE_SEPARATOR) + if (index <= 0) + return null + const parentId = id.slice(0, index) + const paramKey = id.slice(index + NESTED_NODE_SEPARATOR.length) + if (!parentId || !paramKey) + return null + return { parentId, paramKey } +} + +export const isNestedNodeId = (id?: string) => Boolean(parseNestedNodeId(id))