use-workflow.ts 19 KB

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