use-workflow.ts 19 KB

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