refactor: Extract nested node ID parsing into shared utility

This commit is contained in:
zhsama
2026-01-30 22:21:08 +08:00
parent 618dde1e3d
commit bc1d3bdf57
4 changed files with 35 additions and 21 deletions

View File

@ -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

View File

@ -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<Props> = ({
}, [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<Props> = ({
paramKey: parsed.paramKey,
codeNodeId: nodeId,
}
}, [nodeId, nodes, parseExtractorNodeId])
}, [nodeId, nodes])
const handleOpenAutomatic = useCallback(() => {
showAutomaticTrue()

View File

@ -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'

View File

@ -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))