use-workflow.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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 { useContext } from 'use-context-selector'
  10. import produce from 'immer'
  11. import {
  12. getIncomers,
  13. getOutgoers,
  14. useReactFlow,
  15. useStoreApi,
  16. } from 'reactflow'
  17. import type {
  18. Connection,
  19. } from 'reactflow'
  20. import {
  21. getLayoutByDagre,
  22. } from '../utils'
  23. import type {
  24. Edge,
  25. Node,
  26. ValueSelector,
  27. } from '../types'
  28. import {
  29. BlockEnum,
  30. WorkflowRunningStatus,
  31. } from '../types'
  32. import {
  33. useStore,
  34. useWorkflowStore,
  35. } from '../store'
  36. import {
  37. CUSTOM_NODE,
  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 { useNodesSyncDraft } from './use-nodes-sync-draft'
  45. import { WorkflowHistoryEvent, useWorkflowHistory } from './use-workflow-history'
  46. import { useStore as useAppStore } from '@/app/components/app/store'
  47. import {
  48. fetchNodesDefaultConfigs,
  49. fetchPublishedWorkflow,
  50. fetchWorkflowDraft,
  51. syncWorkflowDraft,
  52. } from '@/service/workflow'
  53. import type { FetchWorkflowDraftResponse } from '@/types/workflow'
  54. import {
  55. fetchAllBuiltInTools,
  56. fetchAllCustomTools,
  57. fetchAllWorkflowTools,
  58. } from '@/service/tools'
  59. import I18n from '@/context/i18n'
  60. import { CollectionType } from '@/app/components/tools/types'
  61. export const useIsChatMode = () => {
  62. const appDetail = useAppStore(s => s.appDetail)
  63. return appDetail?.mode === 'advanced-chat'
  64. }
  65. export const useWorkflow = () => {
  66. const { locale } = useContext(I18n)
  67. const store = useStoreApi()
  68. const reactflow = useReactFlow()
  69. const workflowStore = useWorkflowStore()
  70. const nodesExtraData = useNodesExtraData()
  71. const { handleSyncWorkflowDraft } = useNodesSyncDraft()
  72. const { saveStateToHistory } = useWorkflowHistory()
  73. const setPanelWidth = useCallback((width: number) => {
  74. localStorage.setItem('workflow-node-panel-width', `${width}`)
  75. workflowStore.setState({ panelWidth: width })
  76. }, [workflowStore])
  77. const handleLayout = useCallback(async () => {
  78. workflowStore.setState({ nodeAnimation: true })
  79. const {
  80. getNodes,
  81. edges,
  82. setNodes,
  83. } = store.getState()
  84. const { setViewport } = reactflow
  85. const nodes = getNodes()
  86. const layout = getLayoutByDagre(nodes, edges)
  87. const rankMap = {} as Record<string, Node>
  88. nodes.forEach((node) => {
  89. if (!node.parentId && node.type === CUSTOM_NODE) {
  90. const rank = layout.node(node.id).rank!
  91. if (!rankMap[rank]) {
  92. rankMap[rank] = node
  93. }
  94. else {
  95. if (rankMap[rank].position.y > node.position.y)
  96. rankMap[rank] = node
  97. }
  98. }
  99. })
  100. const newNodes = produce(nodes, (draft) => {
  101. draft.forEach((node) => {
  102. if (!node.parentId && node.type === CUSTOM_NODE) {
  103. const nodeWithPosition = layout.node(node.id)
  104. node.position = {
  105. x: nodeWithPosition.x - node.width! / 2,
  106. y: nodeWithPosition.y - node.height! / 2 + rankMap[nodeWithPosition.rank!].height! / 2,
  107. }
  108. }
  109. })
  110. })
  111. setNodes(newNodes)
  112. const zoom = 0.7
  113. setViewport({
  114. x: 0,
  115. y: 0,
  116. zoom,
  117. })
  118. saveStateToHistory(WorkflowHistoryEvent.LayoutOrganize)
  119. setTimeout(() => {
  120. handleSyncWorkflowDraft()
  121. })
  122. }, [workflowStore, store, reactflow, saveStateToHistory, handleSyncWorkflowDraft])
  123. const getTreeLeafNodes = useCallback((nodeId: string) => {
  124. const {
  125. getNodes,
  126. edges,
  127. } = store.getState()
  128. const nodes = getNodes()
  129. let startNode = nodes.find(node => node.data.type === BlockEnum.Start)
  130. const currentNode = nodes.find(node => node.id === nodeId)
  131. if (currentNode?.parentId)
  132. startNode = nodes.find(node => node.parentId === currentNode.parentId && node.data.isIterationStart)
  133. if (!startNode)
  134. return []
  135. const list: Node[] = []
  136. const preOrder = (root: Node, callback: (node: Node) => void) => {
  137. if (root.id === nodeId)
  138. return
  139. const outgoers = getOutgoers(root, nodes, edges)
  140. if (outgoers.length) {
  141. outgoers.forEach((outgoer) => {
  142. preOrder(outgoer, callback)
  143. })
  144. }
  145. else {
  146. if (root.id !== nodeId)
  147. callback(root)
  148. }
  149. }
  150. preOrder(startNode, (node) => {
  151. list.push(node)
  152. })
  153. const incomers = getIncomers({ id: nodeId } as Node, nodes, edges)
  154. list.push(...incomers)
  155. return uniqBy(list, 'id').filter((item) => {
  156. return SUPPORT_OUTPUT_VARS_NODE.includes(item.data.type)
  157. })
  158. }, [store])
  159. const getBeforeNodesInSameBranch = useCallback((nodeId: string, newNodes?: Node[], newEdges?: Edge[]) => {
  160. const {
  161. getNodes,
  162. edges,
  163. } = store.getState()
  164. const nodes = newNodes || getNodes()
  165. const currentNode = nodes.find(node => node.id === nodeId)
  166. const list: Node[] = []
  167. if (!currentNode)
  168. return list
  169. if (currentNode.parentId) {
  170. const parentNode = nodes.find(node => node.id === currentNode.parentId)
  171. if (parentNode) {
  172. const parentList = getBeforeNodesInSameBranch(parentNode.id)
  173. list.push(...parentList)
  174. }
  175. }
  176. const traverse = (root: Node, callback: (node: Node) => void) => {
  177. if (root) {
  178. const incomers = getIncomers(root, nodes, newEdges || edges)
  179. if (incomers.length) {
  180. incomers.forEach((node) => {
  181. if (!list.find(n => node.id === n.id)) {
  182. callback(node)
  183. traverse(node, callback)
  184. }
  185. })
  186. }
  187. }
  188. }
  189. traverse(currentNode, (node) => {
  190. list.push(node)
  191. })
  192. const length = list.length
  193. if (length) {
  194. return uniqBy(list, 'id').reverse().filter((item) => {
  195. return SUPPORT_OUTPUT_VARS_NODE.includes(item.data.type)
  196. })
  197. }
  198. return []
  199. }, [store])
  200. const getBeforeNodesInSameBranchIncludeParent = useCallback((nodeId: string, newNodes?: Node[], newEdges?: Edge[]) => {
  201. const nodes = getBeforeNodesInSameBranch(nodeId, newNodes, newEdges)
  202. const {
  203. getNodes,
  204. } = store.getState()
  205. const allNodes = getNodes()
  206. const node = allNodes.find(n => n.id === nodeId)
  207. const parentNodeId = node?.parentId
  208. const parentNode = allNodes.find(n => n.id === parentNodeId)
  209. if (parentNode)
  210. nodes.push(parentNode)
  211. return nodes
  212. }, [getBeforeNodesInSameBranch, store])
  213. const getAfterNodesInSameBranch = useCallback((nodeId: string) => {
  214. const {
  215. getNodes,
  216. edges,
  217. } = store.getState()
  218. const nodes = getNodes()
  219. const currentNode = nodes.find(node => node.id === nodeId)!
  220. if (!currentNode)
  221. return []
  222. const list: Node[] = [currentNode]
  223. const traverse = (root: Node, callback: (node: Node) => void) => {
  224. if (root) {
  225. const outgoers = getOutgoers(root, nodes, edges)
  226. if (outgoers.length) {
  227. outgoers.forEach((node) => {
  228. callback(node)
  229. traverse(node, callback)
  230. })
  231. }
  232. }
  233. }
  234. traverse(currentNode, (node) => {
  235. list.push(node)
  236. })
  237. return uniqBy(list, 'id')
  238. }, [store])
  239. const getBeforeNodeById = useCallback((nodeId: string) => {
  240. const {
  241. getNodes,
  242. edges,
  243. } = store.getState()
  244. const nodes = getNodes()
  245. const node = nodes.find(node => node.id === nodeId)!
  246. return getIncomers(node, nodes, edges)
  247. }, [store])
  248. const getIterationNodeChildren = useCallback((nodeId: string) => {
  249. const {
  250. getNodes,
  251. } = store.getState()
  252. const nodes = getNodes()
  253. return nodes.filter(node => node.parentId === nodeId)
  254. }, [store])
  255. const handleOutVarRenameChange = useCallback((nodeId: string, oldValeSelector: ValueSelector, newVarSelector: ValueSelector) => {
  256. const { getNodes, setNodes } = store.getState()
  257. const afterNodes = getAfterNodesInSameBranch(nodeId)
  258. const effectNodes = findUsedVarNodes(oldValeSelector, afterNodes)
  259. if (effectNodes.length > 0) {
  260. const newNodes = getNodes().map((node) => {
  261. if (effectNodes.find(n => n.id === node.id))
  262. return updateNodeVars(node, oldValeSelector, newVarSelector)
  263. return node
  264. })
  265. setNodes(newNodes)
  266. }
  267. // eslint-disable-next-line react-hooks/exhaustive-deps
  268. }, [store])
  269. const isVarUsedInNodes = useCallback((varSelector: ValueSelector) => {
  270. const nodeId = varSelector[0]
  271. const afterNodes = getAfterNodesInSameBranch(nodeId)
  272. const effectNodes = findUsedVarNodes(varSelector, afterNodes)
  273. return effectNodes.length > 0
  274. }, [getAfterNodesInSameBranch])
  275. const removeUsedVarInNodes = useCallback((varSelector: ValueSelector) => {
  276. const nodeId = varSelector[0]
  277. const { getNodes, setNodes } = store.getState()
  278. const afterNodes = getAfterNodesInSameBranch(nodeId)
  279. const effectNodes = findUsedVarNodes(varSelector, afterNodes)
  280. if (effectNodes.length > 0) {
  281. const newNodes = getNodes().map((node) => {
  282. if (effectNodes.find(n => n.id === node.id))
  283. return updateNodeVars(node, varSelector, [])
  284. return node
  285. })
  286. setNodes(newNodes)
  287. }
  288. }, [getAfterNodesInSameBranch, store])
  289. const isNodeVarsUsedInNodes = useCallback((node: Node, isChatMode: boolean) => {
  290. const outputVars = getNodeOutputVars(node, isChatMode)
  291. const isUsed = outputVars.some((varSelector) => {
  292. return isVarUsedInNodes(varSelector)
  293. })
  294. return isUsed
  295. }, [isVarUsedInNodes])
  296. const isValidConnection = useCallback(({ source, 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 (targetNode.data.isIterationStart)
  305. return false
  306. if (sourceNode.type === CUSTOM_NOTE_NODE || targetNode.type === CUSTOM_NOTE_NODE)
  307. return false
  308. if (sourceNode && targetNode) {
  309. const sourceNodeAvailableNextNodes = nodesExtraData[sourceNode.data.type].availableNextNodes
  310. const targetNodeAvailablePrevNodes = [...nodesExtraData[targetNode.data.type].availablePrevNodes, BlockEnum.Start]
  311. if (!sourceNodeAvailableNextNodes.includes(targetNode.data.type))
  312. return false
  313. if (!targetNodeAvailablePrevNodes.includes(sourceNode.data.type))
  314. return false
  315. }
  316. const hasCycle = (node: Node, visited = new Set()) => {
  317. if (visited.has(node.id))
  318. return false
  319. visited.add(node.id)
  320. for (const outgoer of getOutgoers(node, nodes, edges)) {
  321. if (outgoer.id === source)
  322. return true
  323. if (hasCycle(outgoer, visited))
  324. return true
  325. }
  326. }
  327. return !hasCycle(targetNode)
  328. }, [store, nodesExtraData])
  329. const formatTimeFromNow = useCallback((time: number) => {
  330. return dayjs(time).locale(locale === 'zh-Hans' ? 'zh-cn' : locale).fromNow()
  331. }, [locale])
  332. const getNode = useCallback((nodeId?: string) => {
  333. const { getNodes } = store.getState()
  334. const nodes = getNodes()
  335. return nodes.find(node => node.id === nodeId) || nodes.find(node => node.data.type === BlockEnum.Start)
  336. }, [store])
  337. const enableShortcuts = useCallback(() => {
  338. const { setShortcutsDisabled } = workflowStore.getState()
  339. setShortcutsDisabled(false)
  340. }, [workflowStore])
  341. const disableShortcuts = useCallback(() => {
  342. const { setShortcutsDisabled } = workflowStore.getState()
  343. setShortcutsDisabled(true)
  344. }, [workflowStore])
  345. return {
  346. setPanelWidth,
  347. handleLayout,
  348. getTreeLeafNodes,
  349. getBeforeNodesInSameBranch,
  350. getBeforeNodesInSameBranchIncludeParent,
  351. getAfterNodesInSameBranch,
  352. handleOutVarRenameChange,
  353. isVarUsedInNodes,
  354. removeUsedVarInNodes,
  355. isNodeVarsUsedInNodes,
  356. isValidConnection,
  357. formatTimeFromNow,
  358. getNode,
  359. getBeforeNodeById,
  360. getIterationNodeChildren,
  361. enableShortcuts,
  362. disableShortcuts,
  363. }
  364. }
  365. export const useFetchToolsData = () => {
  366. const workflowStore = useWorkflowStore()
  367. const handleFetchAllTools = useCallback(async (type: string) => {
  368. if (type === 'builtin') {
  369. const buildInTools = await fetchAllBuiltInTools()
  370. workflowStore.setState({
  371. buildInTools: buildInTools || [],
  372. })
  373. }
  374. if (type === 'custom') {
  375. const customTools = await fetchAllCustomTools()
  376. workflowStore.setState({
  377. customTools: customTools || [],
  378. })
  379. }
  380. if (type === 'workflow') {
  381. const workflowTools = await fetchAllWorkflowTools()
  382. workflowStore.setState({
  383. workflowTools: workflowTools || [],
  384. })
  385. }
  386. }, [workflowStore])
  387. return {
  388. handleFetchAllTools,
  389. }
  390. }
  391. export const useWorkflowInit = () => {
  392. const workflowStore = useWorkflowStore()
  393. const {
  394. nodes: nodesTemplate,
  395. edges: edgesTemplate,
  396. } = useWorkflowTemplate()
  397. const { handleFetchAllTools } = useFetchToolsData()
  398. const appDetail = useAppStore(state => state.appDetail)!
  399. const setSyncWorkflowDraftHash = useStore(s => s.setSyncWorkflowDraftHash)
  400. const [data, setData] = useState<FetchWorkflowDraftResponse>()
  401. const [isLoading, setIsLoading] = useState(true)
  402. workflowStore.setState({ appId: appDetail.id })
  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. // #TODO chatVar sync#
  414. conversationVariables: res.conversation_variables || [],
  415. })
  416. setSyncWorkflowDraftHash(res.hash)
  417. setIsLoading(false)
  418. }
  419. catch (error: any) {
  420. if (error && error.json && !error.bodyUsed && appDetail) {
  421. error.json().then((err: any) => {
  422. if (err.code === 'draft_workflow_not_exist') {
  423. workflowStore.setState({ notInitialWorkflow: true })
  424. syncWorkflowDraft({
  425. url: `/apps/${appDetail.id}/workflows/draft`,
  426. params: {
  427. graph: {
  428. nodes: nodesTemplate,
  429. edges: edgesTemplate,
  430. },
  431. features: {
  432. retriever_resource: { enabled: true },
  433. },
  434. environment_variables: [],
  435. conversation_variables: [],
  436. },
  437. }).then((res) => {
  438. workflowStore.getState().setDraftUpdatedAt(res.updated_at)
  439. handleGetInitialWorkflowData()
  440. })
  441. }
  442. })
  443. }
  444. }
  445. }, [appDetail, nodesTemplate, edgesTemplate, workflowStore, setSyncWorkflowDraftHash])
  446. useEffect(() => {
  447. handleGetInitialWorkflowData()
  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 => 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. }