use-workflow.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. import {
  2. useCallback,
  3. useEffect,
  4. useMemo,
  5. useState,
  6. } from 'react'
  7. import dayjs from 'dayjs'
  8. import { uniqBy } from 'lodash-es'
  9. import { useTranslation } from 'react-i18next'
  10. import { useContext } from 'use-context-selector'
  11. import {
  12. getIncomers,
  13. getOutgoers,
  14. useStoreApi,
  15. } from 'reactflow'
  16. import type {
  17. Connection,
  18. } from 'reactflow'
  19. import type {
  20. Edge,
  21. Node,
  22. ValueSelector,
  23. } from '../types'
  24. import {
  25. BlockEnum,
  26. WorkflowRunningStatus,
  27. } from '../types'
  28. import {
  29. useStore,
  30. useWorkflowStore,
  31. } from '../store'
  32. import {
  33. getParallelInfo,
  34. } from '../utils'
  35. import {
  36. PARALLEL_DEPTH_LIMIT,
  37. PARALLEL_LIMIT,
  38. SUPPORT_OUTPUT_VARS_NODE,
  39. } from '../constants'
  40. import { CUSTOM_NOTE_NODE } from '../note-node/constants'
  41. import { findUsedVarNodes, getNodeOutputVars, updateNodeVars } from '../nodes/_base/components/variable/utils'
  42. import { useNodesExtraData } from './use-nodes-data'
  43. import { useWorkflowTemplate } from './use-workflow-template'
  44. import { useStore as useAppStore } from '@/app/components/app/store'
  45. import {
  46. fetchNodesDefaultConfigs,
  47. fetchPublishedWorkflow,
  48. fetchWorkflowDraft,
  49. syncWorkflowDraft,
  50. } from '@/service/workflow'
  51. import type { FetchWorkflowDraftResponse } from '@/types/workflow'
  52. import {
  53. fetchAllBuiltInTools,
  54. fetchAllCustomTools,
  55. fetchAllWorkflowTools,
  56. } from '@/service/tools'
  57. import I18n from '@/context/i18n'
  58. import { CollectionType } from '@/app/components/tools/types'
  59. import { CUSTOM_ITERATION_START_NODE } from '@/app/components/workflow/nodes/iteration-start/constants'
  60. import { CUSTOM_LOOP_START_NODE } from '@/app/components/workflow/nodes/loop-start/constants'
  61. import { useWorkflowConfig } from '@/service/use-workflow'
  62. import { canFindTool } from '@/utils'
  63. export const useIsChatMode = () => {
  64. const appDetail = useAppStore(s => s.appDetail)
  65. return appDetail?.mode === 'advanced-chat'
  66. }
  67. export const useWorkflow = () => {
  68. const { t } = useTranslation()
  69. const { locale } = useContext(I18n)
  70. const store = useStoreApi()
  71. const workflowStore = useWorkflowStore()
  72. const appId = useStore(s => s.appId)
  73. const nodesExtraData = useNodesExtraData()
  74. const { data: workflowConfig } = useWorkflowConfig(appId)
  75. const setPanelWidth = useCallback((width: number) => {
  76. localStorage.setItem('workflow-node-panel-width', `${width}`)
  77. workflowStore.setState({ panelWidth: width })
  78. }, [workflowStore])
  79. const getTreeLeafNodes = useCallback((nodeId: string) => {
  80. const {
  81. getNodes,
  82. edges,
  83. } = store.getState()
  84. const nodes = getNodes()
  85. let startNode = nodes.find(node => node.data.type === BlockEnum.Start)
  86. const currentNode = nodes.find(node => node.id === nodeId)
  87. if (currentNode?.parentId)
  88. startNode = nodes.find(node => node.parentId === currentNode.parentId && (node.type === CUSTOM_ITERATION_START_NODE || node.type === CUSTOM_LOOP_START_NODE))
  89. if (!startNode)
  90. return []
  91. const list: Node[] = []
  92. const preOrder = (root: Node, callback: (node: Node) => void) => {
  93. if (root.id === nodeId)
  94. return
  95. const outgoers = getOutgoers(root, nodes, edges)
  96. if (outgoers.length) {
  97. outgoers.forEach((outgoer) => {
  98. preOrder(outgoer, callback)
  99. })
  100. }
  101. else {
  102. if (root.id !== nodeId)
  103. callback(root)
  104. }
  105. }
  106. preOrder(startNode, (node) => {
  107. list.push(node)
  108. })
  109. const incomers = getIncomers({ id: nodeId } as Node, nodes, edges)
  110. list.push(...incomers)
  111. return uniqBy(list, 'id').filter((item) => {
  112. return SUPPORT_OUTPUT_VARS_NODE.includes(item.data.type)
  113. })
  114. }, [store])
  115. const getBeforeNodesInSameBranch = useCallback((nodeId: string, newNodes?: Node[], newEdges?: Edge[]) => {
  116. const {
  117. getNodes,
  118. edges,
  119. } = store.getState()
  120. const nodes = newNodes || getNodes()
  121. const currentNode = nodes.find(node => node.id === nodeId)
  122. const list: Node[] = []
  123. if (!currentNode)
  124. return list
  125. if (currentNode.parentId) {
  126. const parentNode = nodes.find(node => node.id === currentNode.parentId)
  127. if (parentNode) {
  128. const parentList = getBeforeNodesInSameBranch(parentNode.id)
  129. list.push(...parentList)
  130. }
  131. }
  132. const traverse = (root: Node, callback: (node: Node) => void) => {
  133. if (root) {
  134. const incomers = getIncomers(root, nodes, newEdges || edges)
  135. if (incomers.length) {
  136. incomers.forEach((node) => {
  137. if (!list.find(n => node.id === n.id)) {
  138. callback(node)
  139. traverse(node, callback)
  140. }
  141. })
  142. }
  143. }
  144. }
  145. traverse(currentNode, (node) => {
  146. list.push(node)
  147. })
  148. const length = list.length
  149. if (length) {
  150. return uniqBy(list, 'id').reverse().filter((item) => {
  151. return SUPPORT_OUTPUT_VARS_NODE.includes(item.data.type)
  152. })
  153. }
  154. return []
  155. }, [store])
  156. const getBeforeNodesInSameBranchIncludeParent = useCallback((nodeId: string, newNodes?: Node[], newEdges?: Edge[]) => {
  157. const nodes = getBeforeNodesInSameBranch(nodeId, newNodes, newEdges)
  158. const {
  159. getNodes,
  160. } = store.getState()
  161. const allNodes = getNodes()
  162. const node = allNodes.find(n => n.id === nodeId)
  163. const parentNodeId = node?.parentId
  164. const parentNode = allNodes.find(n => n.id === parentNodeId)
  165. if (parentNode)
  166. nodes.push(parentNode)
  167. return nodes
  168. }, [getBeforeNodesInSameBranch, store])
  169. const getAfterNodesInSameBranch = useCallback((nodeId: string) => {
  170. const {
  171. getNodes,
  172. edges,
  173. } = store.getState()
  174. const nodes = getNodes()
  175. const currentNode = nodes.find(node => node.id === nodeId)!
  176. if (!currentNode)
  177. return []
  178. const list: Node[] = [currentNode]
  179. const traverse = (root: Node, callback: (node: Node) => void) => {
  180. if (root) {
  181. const outgoers = getOutgoers(root, nodes, edges)
  182. if (outgoers.length) {
  183. outgoers.forEach((node) => {
  184. callback(node)
  185. traverse(node, callback)
  186. })
  187. }
  188. }
  189. }
  190. traverse(currentNode, (node) => {
  191. list.push(node)
  192. })
  193. return uniqBy(list, 'id')
  194. }, [store])
  195. const getBeforeNodeById = useCallback((nodeId: string) => {
  196. const {
  197. getNodes,
  198. edges,
  199. } = store.getState()
  200. const nodes = getNodes()
  201. const node = nodes.find(node => node.id === nodeId)!
  202. return getIncomers(node, nodes, edges)
  203. }, [store])
  204. const getIterationNodeChildren = useCallback((nodeId: string) => {
  205. const {
  206. getNodes,
  207. } = store.getState()
  208. const nodes = getNodes()
  209. return nodes.filter(node => node.parentId === nodeId)
  210. }, [store])
  211. const getLoopNodeChildren = useCallback((nodeId: string) => {
  212. const {
  213. getNodes,
  214. } = store.getState()
  215. const nodes = getNodes()
  216. return nodes.filter(node => node.parentId === nodeId)
  217. }, [store])
  218. const isFromStartNode = useCallback((nodeId: string) => {
  219. const { getNodes } = store.getState()
  220. const nodes = getNodes()
  221. const currentNode = nodes.find(node => node.id === nodeId)
  222. if (!currentNode)
  223. return false
  224. if (currentNode.data.type === BlockEnum.Start)
  225. return true
  226. const checkPreviousNodes = (node: Node) => {
  227. const previousNodes = getBeforeNodeById(node.id)
  228. for (const prevNode of previousNodes) {
  229. if (prevNode.data.type === BlockEnum.Start)
  230. return true
  231. if (checkPreviousNodes(prevNode))
  232. return true
  233. }
  234. return false
  235. }
  236. return checkPreviousNodes(currentNode)
  237. }, [store, getBeforeNodeById])
  238. const handleOutVarRenameChange = useCallback((nodeId: string, oldValeSelector: ValueSelector, newVarSelector: ValueSelector) => {
  239. const { getNodes, setNodes } = store.getState()
  240. const afterNodes = getAfterNodesInSameBranch(nodeId)
  241. const effectNodes = findUsedVarNodes(oldValeSelector, afterNodes)
  242. if (effectNodes.length > 0) {
  243. const newNodes = getNodes().map((node) => {
  244. if (effectNodes.find(n => n.id === node.id))
  245. return updateNodeVars(node, oldValeSelector, newVarSelector)
  246. return node
  247. })
  248. setNodes(newNodes)
  249. }
  250. // eslint-disable-next-line react-hooks/exhaustive-deps
  251. }, [store])
  252. const isVarUsedInNodes = useCallback((varSelector: ValueSelector) => {
  253. const nodeId = varSelector[0]
  254. const afterNodes = getAfterNodesInSameBranch(nodeId)
  255. const effectNodes = findUsedVarNodes(varSelector, afterNodes)
  256. return effectNodes.length > 0
  257. }, [getAfterNodesInSameBranch])
  258. const removeUsedVarInNodes = useCallback((varSelector: ValueSelector) => {
  259. const nodeId = varSelector[0]
  260. const { getNodes, setNodes } = store.getState()
  261. const afterNodes = getAfterNodesInSameBranch(nodeId)
  262. const effectNodes = findUsedVarNodes(varSelector, afterNodes)
  263. if (effectNodes.length > 0) {
  264. const newNodes = getNodes().map((node) => {
  265. if (effectNodes.find(n => n.id === node.id))
  266. return updateNodeVars(node, varSelector, [])
  267. return node
  268. })
  269. setNodes(newNodes)
  270. }
  271. }, [getAfterNodesInSameBranch, store])
  272. const isNodeVarsUsedInNodes = useCallback((node: Node, isChatMode: boolean) => {
  273. const outputVars = getNodeOutputVars(node, isChatMode)
  274. const isUsed = outputVars.some((varSelector) => {
  275. return isVarUsedInNodes(varSelector)
  276. })
  277. return isUsed
  278. }, [isVarUsedInNodes])
  279. const checkParallelLimit = useCallback((nodeId: string, nodeHandle = 'source') => {
  280. const {
  281. edges,
  282. } = store.getState()
  283. const connectedEdges = edges.filter(edge => edge.source === nodeId && edge.sourceHandle === nodeHandle)
  284. if (connectedEdges.length > PARALLEL_LIMIT - 1) {
  285. const { setShowTips } = workflowStore.getState()
  286. setShowTips(t('workflow.common.parallelTip.limit', { num: PARALLEL_LIMIT }))
  287. return false
  288. }
  289. return true
  290. }, [store, workflowStore, t])
  291. const checkNestedParallelLimit = useCallback((nodes: Node[], edges: Edge[], parentNodeId?: string) => {
  292. const {
  293. parallelList,
  294. hasAbnormalEdges,
  295. } = getParallelInfo(nodes, edges, parentNodeId)
  296. if (hasAbnormalEdges)
  297. return false
  298. for (let i = 0; i < parallelList.length; i++) {
  299. const parallel = parallelList[i]
  300. if (parallel.depth > (workflowConfig?.parallel_depth_limit || PARALLEL_DEPTH_LIMIT)) {
  301. const { setShowTips } = workflowStore.getState()
  302. setShowTips(t('workflow.common.parallelTip.depthLimit', { num: (workflowConfig?.parallel_depth_limit || PARALLEL_DEPTH_LIMIT) }))
  303. return false
  304. }
  305. }
  306. return true
  307. }, [t, workflowStore, workflowConfig?.parallel_depth_limit])
  308. const isValidConnection = useCallback(({ source, sourceHandle, target }: Connection) => {
  309. const {
  310. edges,
  311. getNodes,
  312. } = store.getState()
  313. const nodes = getNodes()
  314. const sourceNode: Node = nodes.find(node => node.id === source)!
  315. const targetNode: Node = nodes.find(node => node.id === target)!
  316. if (!checkParallelLimit(source!, sourceHandle || 'source'))
  317. return false
  318. if (sourceNode.type === CUSTOM_NOTE_NODE || targetNode.type === CUSTOM_NOTE_NODE)
  319. return false
  320. if (sourceNode.parentId !== targetNode.parentId)
  321. return false
  322. if (sourceNode && targetNode) {
  323. const sourceNodeAvailableNextNodes = nodesExtraData[sourceNode.data.type].availableNextNodes
  324. const targetNodeAvailablePrevNodes = [...nodesExtraData[targetNode.data.type].availablePrevNodes, BlockEnum.Start]
  325. if (!sourceNodeAvailableNextNodes.includes(targetNode.data.type))
  326. return false
  327. if (!targetNodeAvailablePrevNodes.includes(sourceNode.data.type))
  328. return false
  329. }
  330. const hasCycle = (node: Node, visited = new Set()) => {
  331. if (visited.has(node.id))
  332. return false
  333. visited.add(node.id)
  334. for (const outgoer of getOutgoers(node, nodes, edges)) {
  335. if (outgoer.id === source)
  336. return true
  337. if (hasCycle(outgoer, visited))
  338. return true
  339. }
  340. }
  341. return !hasCycle(targetNode)
  342. }, [store, nodesExtraData, checkParallelLimit])
  343. const formatTimeFromNow = useCallback((time: number) => {
  344. return dayjs(time).locale(locale === 'zh-Hans' ? 'zh-cn' : locale).fromNow()
  345. }, [locale])
  346. const getNode = useCallback((nodeId?: string) => {
  347. const { getNodes } = store.getState()
  348. const nodes = getNodes()
  349. return nodes.find(node => node.id === nodeId) || nodes.find(node => node.data.type === BlockEnum.Start)
  350. }, [store])
  351. return {
  352. setPanelWidth,
  353. getTreeLeafNodes,
  354. getBeforeNodesInSameBranch,
  355. getBeforeNodesInSameBranchIncludeParent,
  356. getAfterNodesInSameBranch,
  357. handleOutVarRenameChange,
  358. isVarUsedInNodes,
  359. removeUsedVarInNodes,
  360. isNodeVarsUsedInNodes,
  361. checkParallelLimit,
  362. checkNestedParallelLimit,
  363. isValidConnection,
  364. isFromStartNode,
  365. formatTimeFromNow,
  366. getNode,
  367. getBeforeNodeById,
  368. getIterationNodeChildren,
  369. getLoopNodeChildren,
  370. }
  371. }
  372. export const useFetchToolsData = () => {
  373. const workflowStore = useWorkflowStore()
  374. const handleFetchAllTools = useCallback(async (type: string) => {
  375. if (type === 'builtin') {
  376. const buildInTools = await fetchAllBuiltInTools()
  377. workflowStore.setState({
  378. buildInTools: buildInTools || [],
  379. })
  380. }
  381. if (type === 'custom') {
  382. const customTools = await fetchAllCustomTools()
  383. workflowStore.setState({
  384. customTools: customTools || [],
  385. })
  386. }
  387. if (type === 'workflow') {
  388. const workflowTools = await fetchAllWorkflowTools()
  389. workflowStore.setState({
  390. workflowTools: workflowTools || [],
  391. })
  392. }
  393. }, [workflowStore])
  394. return {
  395. handleFetchAllTools,
  396. }
  397. }
  398. export const useWorkflowInit = () => {
  399. const workflowStore = useWorkflowStore()
  400. const {
  401. nodes: nodesTemplate,
  402. edges: edgesTemplate,
  403. } = useWorkflowTemplate()
  404. const { handleFetchAllTools } = useFetchToolsData()
  405. const appDetail = useAppStore(state => state.appDetail)!
  406. const setSyncWorkflowDraftHash = useStore(s => s.setSyncWorkflowDraftHash)
  407. const [data, setData] = useState<FetchWorkflowDraftResponse>()
  408. const [isLoading, setIsLoading] = useState(true)
  409. useEffect(() => {
  410. workflowStore.setState({ appId: appDetail.id })
  411. }, [appDetail.id, workflowStore])
  412. const handleGetInitialWorkflowData = useCallback(async () => {
  413. try {
  414. const res = await fetchWorkflowDraft(`/apps/${appDetail.id}/workflows/draft`)
  415. setData(res)
  416. workflowStore.setState({
  417. envSecrets: (res.environment_variables || []).filter(env => env.value_type === 'secret').reduce((acc, env) => {
  418. acc[env.id] = env.value
  419. return acc
  420. }, {} as Record<string, string>),
  421. environmentVariables: res.environment_variables?.map(env => env.value_type === 'secret' ? { ...env, value: '[__HIDDEN__]' } : env) || [],
  422. conversationVariables: res.conversation_variables || [],
  423. })
  424. setSyncWorkflowDraftHash(res.hash)
  425. setIsLoading(false)
  426. }
  427. catch (error: any) {
  428. if (error && error.json && !error.bodyUsed && appDetail) {
  429. error.json().then((err: any) => {
  430. if (err.code === 'draft_workflow_not_exist') {
  431. workflowStore.setState({ notInitialWorkflow: true })
  432. syncWorkflowDraft({
  433. url: `/apps/${appDetail.id}/workflows/draft`,
  434. params: {
  435. graph: {
  436. nodes: nodesTemplate,
  437. edges: edgesTemplate,
  438. },
  439. features: {
  440. retriever_resource: { enabled: true },
  441. },
  442. environment_variables: [],
  443. conversation_variables: [],
  444. },
  445. }).then((res) => {
  446. workflowStore.getState().setDraftUpdatedAt(res.updated_at)
  447. handleGetInitialWorkflowData()
  448. })
  449. }
  450. })
  451. }
  452. }
  453. }, [appDetail, nodesTemplate, edgesTemplate, workflowStore, setSyncWorkflowDraftHash])
  454. useEffect(() => {
  455. handleGetInitialWorkflowData()
  456. // eslint-disable-next-line react-hooks/exhaustive-deps
  457. }, [])
  458. const handleFetchPreloadData = useCallback(async () => {
  459. try {
  460. const nodesDefaultConfigsData = await fetchNodesDefaultConfigs(`/apps/${appDetail?.id}/workflows/default-workflow-block-configs`)
  461. const publishedWorkflow = await fetchPublishedWorkflow(`/apps/${appDetail?.id}/workflows/publish`)
  462. workflowStore.setState({
  463. nodesDefaultConfigs: nodesDefaultConfigsData.reduce((acc, block) => {
  464. if (!acc[block.type])
  465. acc[block.type] = { ...block.config }
  466. return acc
  467. }, {} as Record<string, any>),
  468. })
  469. workflowStore.getState().setPublishedAt(publishedWorkflow?.created_at)
  470. }
  471. catch (e) {
  472. console.error(e)
  473. }
  474. }, [workflowStore, appDetail])
  475. useEffect(() => {
  476. handleFetchPreloadData()
  477. handleFetchAllTools('builtin')
  478. handleFetchAllTools('custom')
  479. handleFetchAllTools('workflow')
  480. }, [handleFetchPreloadData, handleFetchAllTools])
  481. useEffect(() => {
  482. if (data) {
  483. workflowStore.getState().setDraftUpdatedAt(data.updated_at)
  484. workflowStore.getState().setToolPublished(data.tool_published)
  485. }
  486. }, [data, workflowStore])
  487. return {
  488. data,
  489. isLoading,
  490. }
  491. }
  492. export const useWorkflowReadOnly = () => {
  493. const workflowStore = useWorkflowStore()
  494. const workflowRunningData = useStore(s => s.workflowRunningData)
  495. const getWorkflowReadOnly = useCallback(() => {
  496. return workflowStore.getState().workflowRunningData?.result.status === WorkflowRunningStatus.Running
  497. }, [workflowStore])
  498. return {
  499. workflowReadOnly: workflowRunningData?.result.status === WorkflowRunningStatus.Running,
  500. getWorkflowReadOnly,
  501. }
  502. }
  503. export const useNodesReadOnly = () => {
  504. const workflowStore = useWorkflowStore()
  505. const workflowRunningData = useStore(s => s.workflowRunningData)
  506. const historyWorkflowData = useStore(s => s.historyWorkflowData)
  507. const isRestoring = useStore(s => s.isRestoring)
  508. const getNodesReadOnly = useCallback(() => {
  509. const {
  510. workflowRunningData,
  511. historyWorkflowData,
  512. isRestoring,
  513. } = workflowStore.getState()
  514. return workflowRunningData?.result.status === WorkflowRunningStatus.Running || historyWorkflowData || isRestoring
  515. }, [workflowStore])
  516. return {
  517. nodesReadOnly: !!(workflowRunningData?.result.status === WorkflowRunningStatus.Running || historyWorkflowData || isRestoring),
  518. getNodesReadOnly,
  519. }
  520. }
  521. export const useToolIcon = (data: Node['data']) => {
  522. const buildInTools = useStore(s => s.buildInTools)
  523. const customTools = useStore(s => s.customTools)
  524. const workflowTools = useStore(s => s.workflowTools)
  525. const toolIcon = useMemo(() => {
  526. if (data.type === BlockEnum.Tool) {
  527. let targetTools = buildInTools
  528. if (data.provider_type === CollectionType.builtIn)
  529. targetTools = buildInTools
  530. else if (data.provider_type === CollectionType.custom)
  531. targetTools = customTools
  532. else
  533. targetTools = workflowTools
  534. return targetTools.find(toolWithProvider => canFindTool(toolWithProvider.id, data.provider_id))?.icon
  535. }
  536. }, [data, buildInTools, customTools, workflowTools])
  537. return toolIcon
  538. }
  539. export const useIsNodeInIteration = (iterationId: string) => {
  540. const store = useStoreApi()
  541. const isNodeInIteration = useCallback((nodeId: string) => {
  542. const {
  543. getNodes,
  544. } = store.getState()
  545. const nodes = getNodes()
  546. const node = nodes.find(node => node.id === nodeId)
  547. if (!node)
  548. return false
  549. if (node.parentId === iterationId)
  550. return true
  551. return false
  552. }, [iterationId, store])
  553. return {
  554. isNodeInIteration,
  555. }
  556. }
  557. export const useIsNodeInLoop = (loopId: string) => {
  558. const store = useStoreApi()
  559. const isNodeInLoop = useCallback((nodeId: string) => {
  560. const {
  561. getNodes,
  562. } = store.getState()
  563. const nodes = getNodes()
  564. const node = nodes.find(node => node.id === nodeId)
  565. if (!node)
  566. return false
  567. if (node.parentId === loopId)
  568. return true
  569. return false
  570. }, [loopId, store])
  571. return {
  572. isNodeInLoop,
  573. }
  574. }