use-workflow-run.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. import { useCallback } from 'react'
  2. import {
  3. useReactFlow,
  4. useStoreApi,
  5. } from 'reactflow'
  6. import produce from 'immer'
  7. import { v4 as uuidV4 } from 'uuid'
  8. import { usePathname } from 'next/navigation'
  9. import { useWorkflowStore } from '../store'
  10. import { useNodesSyncDraft } from '../hooks'
  11. import {
  12. BlockEnum,
  13. NodeRunningStatus,
  14. WorkflowRunningStatus,
  15. } from '../types'
  16. import { DEFAULT_ITER_TIMES } from '../constants'
  17. import { useWorkflowUpdate } from './use-workflow-interactions'
  18. import { useStore as useAppStore } from '@/app/components/app/store'
  19. import type { IOtherOptions } from '@/service/base'
  20. import { ssePost } from '@/service/base'
  21. import {
  22. fetchPublishedWorkflow,
  23. stopWorkflowRun,
  24. } from '@/service/workflow'
  25. import { useFeaturesStore } from '@/app/components/base/features/hooks'
  26. import { AudioPlayerManager } from '@/app/components/base/audio-btn/audio.player.manager'
  27. import {
  28. getFilesInLogs,
  29. } from '@/app/components/base/file-uploader/utils'
  30. import { ErrorHandleTypeEnum } from '@/app/components/workflow/nodes/_base/components/error-handle/types'
  31. export const useWorkflowRun = () => {
  32. const store = useStoreApi()
  33. const workflowStore = useWorkflowStore()
  34. const reactflow = useReactFlow()
  35. const featuresStore = useFeaturesStore()
  36. const { doSyncWorkflowDraft } = useNodesSyncDraft()
  37. const { handleUpdateWorkflowCanvas } = useWorkflowUpdate()
  38. const pathname = usePathname()
  39. const handleBackupDraft = useCallback(() => {
  40. const {
  41. getNodes,
  42. edges,
  43. } = store.getState()
  44. const { getViewport } = reactflow
  45. const {
  46. backupDraft,
  47. setBackupDraft,
  48. environmentVariables,
  49. } = workflowStore.getState()
  50. const { features } = featuresStore!.getState()
  51. if (!backupDraft) {
  52. setBackupDraft({
  53. nodes: getNodes(),
  54. edges,
  55. viewport: getViewport(),
  56. features,
  57. environmentVariables,
  58. })
  59. doSyncWorkflowDraft()
  60. }
  61. }, [reactflow, workflowStore, store, featuresStore, doSyncWorkflowDraft])
  62. const handleLoadBackupDraft = useCallback(() => {
  63. const {
  64. backupDraft,
  65. setBackupDraft,
  66. setEnvironmentVariables,
  67. } = workflowStore.getState()
  68. if (backupDraft) {
  69. const {
  70. nodes,
  71. edges,
  72. viewport,
  73. features,
  74. environmentVariables,
  75. } = backupDraft
  76. handleUpdateWorkflowCanvas({
  77. nodes,
  78. edges,
  79. viewport,
  80. })
  81. setEnvironmentVariables(environmentVariables)
  82. featuresStore!.setState({ features })
  83. setBackupDraft(undefined)
  84. }
  85. }, [handleUpdateWorkflowCanvas, workflowStore, featuresStore])
  86. const handleRun = useCallback(async (
  87. params: any,
  88. callback?: IOtherOptions,
  89. ) => {
  90. const {
  91. getNodes,
  92. setNodes,
  93. } = store.getState()
  94. const newNodes = produce(getNodes(), (draft) => {
  95. draft.forEach((node) => {
  96. node.data.selected = false
  97. node.data._runningStatus = undefined
  98. })
  99. })
  100. setNodes(newNodes)
  101. await doSyncWorkflowDraft()
  102. const {
  103. onWorkflowStarted,
  104. onWorkflowFinished,
  105. onNodeStarted,
  106. onNodeFinished,
  107. onIterationStart,
  108. onIterationNext,
  109. onIterationFinish,
  110. onError,
  111. ...restCallback
  112. } = callback || {}
  113. workflowStore.setState({ historyWorkflowData: undefined })
  114. const appDetail = useAppStore.getState().appDetail
  115. const workflowContainer = document.getElementById('workflow-container')
  116. const {
  117. clientWidth,
  118. clientHeight,
  119. } = workflowContainer!
  120. let url = ''
  121. if (appDetail?.mode === 'advanced-chat')
  122. url = `/apps/${appDetail.id}/advanced-chat/workflows/draft/run`
  123. if (appDetail?.mode === 'workflow')
  124. url = `/apps/${appDetail.id}/workflows/draft/run`
  125. let prevNodeId = ''
  126. const {
  127. setWorkflowRunningData,
  128. } = workflowStore.getState()
  129. setWorkflowRunningData({
  130. result: {
  131. status: WorkflowRunningStatus.Running,
  132. },
  133. tracing: [],
  134. resultText: '',
  135. })
  136. let ttsUrl = ''
  137. let ttsIsPublic = false
  138. if (params.token) {
  139. ttsUrl = '/text-to-audio'
  140. ttsIsPublic = true
  141. }
  142. else if (params.appId) {
  143. if (pathname.search('explore/installed') > -1)
  144. ttsUrl = `/installed-apps/${params.appId}/text-to-audio`
  145. else
  146. ttsUrl = `/apps/${params.appId}/text-to-audio`
  147. }
  148. const player = AudioPlayerManager.getInstance().getAudioPlayer(ttsUrl, ttsIsPublic, uuidV4(), 'none', 'none', (_: any): any => {})
  149. ssePost(
  150. url,
  151. {
  152. body: params,
  153. },
  154. {
  155. onWorkflowStarted: (params) => {
  156. const { task_id, data } = params
  157. const {
  158. workflowRunningData,
  159. setWorkflowRunningData,
  160. setIterParallelLogMap,
  161. } = workflowStore.getState()
  162. const {
  163. getNodes,
  164. setNodes,
  165. edges,
  166. setEdges,
  167. } = store.getState()
  168. setIterParallelLogMap(new Map())
  169. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  170. draft.task_id = task_id
  171. draft.result = {
  172. ...draft?.result,
  173. ...data,
  174. status: WorkflowRunningStatus.Running,
  175. }
  176. }))
  177. const nodes = getNodes()
  178. const newNodes = produce(nodes, (draft) => {
  179. draft.forEach((node) => {
  180. node.data._waitingRun = true
  181. node.data._runningBranchId = undefined
  182. })
  183. })
  184. setNodes(newNodes)
  185. const newEdges = produce(edges, (draft) => {
  186. draft.forEach((edge) => {
  187. edge.data = {
  188. ...edge.data,
  189. _sourceRunningStatus: undefined,
  190. _targetRunningStatus: undefined,
  191. _waitingRun: true,
  192. }
  193. })
  194. })
  195. setEdges(newEdges)
  196. if (onWorkflowStarted)
  197. onWorkflowStarted(params)
  198. },
  199. onWorkflowFinished: (params) => {
  200. const { data } = params
  201. const {
  202. workflowRunningData,
  203. setWorkflowRunningData,
  204. } = workflowStore.getState()
  205. const isStringOutput = data.outputs && Object.keys(data.outputs).length === 1 && typeof data.outputs[Object.keys(data.outputs)[0]] === 'string'
  206. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  207. draft.result = {
  208. ...draft.result,
  209. ...data,
  210. files: getFilesInLogs(data.outputs),
  211. } as any
  212. if (isStringOutput) {
  213. draft.resultTabActive = true
  214. draft.resultText = data.outputs[Object.keys(data.outputs)[0]]
  215. }
  216. }))
  217. prevNodeId = ''
  218. if (onWorkflowFinished)
  219. onWorkflowFinished(params)
  220. },
  221. onError: (params) => {
  222. const {
  223. workflowRunningData,
  224. setWorkflowRunningData,
  225. } = workflowStore.getState()
  226. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  227. draft.result = {
  228. ...draft.result,
  229. status: WorkflowRunningStatus.Failed,
  230. }
  231. }))
  232. if (onError)
  233. onError(params)
  234. },
  235. onNodeStarted: (params) => {
  236. const { data } = params
  237. const {
  238. workflowRunningData,
  239. setWorkflowRunningData,
  240. iterParallelLogMap,
  241. setIterParallelLogMap,
  242. } = workflowStore.getState()
  243. const {
  244. getNodes,
  245. setNodes,
  246. edges,
  247. setEdges,
  248. transform,
  249. } = store.getState()
  250. const nodes = getNodes()
  251. const node = nodes.find(node => node.id === data.node_id)
  252. if (node?.parentId) {
  253. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  254. const tracing = draft.tracing!
  255. const iterations = tracing.find(trace => trace.node_id === node?.parentId)
  256. const currIteration = iterations?.details![node.data.iteration_index] || iterations?.details![iterations.details!.length - 1]
  257. if (!data.parallel_run_id) {
  258. currIteration?.push({
  259. ...data,
  260. status: NodeRunningStatus.Running,
  261. } as any)
  262. }
  263. else {
  264. const nodeId = iterations?.node_id as string
  265. if (!iterParallelLogMap.has(nodeId as string))
  266. iterParallelLogMap.set(iterations?.node_id as string, new Map())
  267. const currentIterLogMap = iterParallelLogMap.get(nodeId)!
  268. if (!currentIterLogMap.has(data.parallel_run_id))
  269. currentIterLogMap.set(data.parallel_run_id, [{ ...data, status: NodeRunningStatus.Running } as any])
  270. else
  271. currentIterLogMap.get(data.parallel_run_id)!.push({ ...data, status: NodeRunningStatus.Running } as any)
  272. setIterParallelLogMap(iterParallelLogMap)
  273. if (iterations)
  274. iterations.details = Array.from(currentIterLogMap.values())
  275. }
  276. }))
  277. }
  278. else {
  279. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  280. draft.tracing!.push({
  281. ...data,
  282. status: NodeRunningStatus.Running,
  283. } as any)
  284. }))
  285. const {
  286. setViewport,
  287. } = reactflow
  288. const currentNodeIndex = nodes.findIndex(node => node.id === data.node_id)
  289. const currentNode = nodes[currentNodeIndex]
  290. const position = currentNode.position
  291. const zoom = transform[2]
  292. if (!currentNode.parentId) {
  293. setViewport({
  294. x: (clientWidth - 400 - currentNode.width! * zoom) / 2 - position.x * zoom,
  295. y: (clientHeight - currentNode.height! * zoom) / 2 - position.y * zoom,
  296. zoom: transform[2],
  297. })
  298. }
  299. const newNodes = produce(nodes, (draft) => {
  300. draft[currentNodeIndex].data._runningStatus = NodeRunningStatus.Running
  301. draft[currentNodeIndex].data._waitingRun = false
  302. })
  303. setNodes(newNodes)
  304. const newEdges = produce(edges, (draft) => {
  305. const incomeEdges = draft.filter((edge) => {
  306. return edge.target === data.node_id
  307. })
  308. incomeEdges.forEach((edge) => {
  309. const incomeNode = nodes.find(node => node.id === edge.source)!
  310. if (
  311. (!incomeNode.data._runningBranchId && edge.sourceHandle === 'source')
  312. || (incomeNode.data._runningBranchId && edge.sourceHandle === incomeNode.data._runningBranchId)
  313. ) {
  314. edge.data = {
  315. ...edge.data,
  316. _sourceRunningStatus: incomeNode.data._runningStatus,
  317. _targetRunningStatus: NodeRunningStatus.Running,
  318. _waitingRun: false,
  319. }
  320. }
  321. })
  322. })
  323. setEdges(newEdges)
  324. }
  325. if (onNodeStarted)
  326. onNodeStarted(params)
  327. },
  328. onNodeFinished: (params) => {
  329. const { data } = params
  330. const {
  331. workflowRunningData,
  332. setWorkflowRunningData,
  333. iterParallelLogMap,
  334. setIterParallelLogMap,
  335. } = workflowStore.getState()
  336. const {
  337. getNodes,
  338. setNodes,
  339. edges,
  340. setEdges,
  341. } = store.getState()
  342. const nodes = getNodes()
  343. const nodeParentId = nodes.find(node => node.id === data.node_id)!.parentId
  344. if (nodeParentId) {
  345. if (!data.execution_metadata.parallel_mode_run_id) {
  346. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  347. const tracing = draft.tracing!
  348. const iterations = tracing.find(trace => trace.node_id === nodeParentId) // the iteration node
  349. if (iterations && iterations.details) {
  350. const iterationIndex = data.execution_metadata?.iteration_index || 0
  351. if (!iterations.details[iterationIndex])
  352. iterations.details[iterationIndex] = []
  353. const currIteration = iterations.details[iterationIndex]
  354. const nodeIndex = currIteration.findIndex(node =>
  355. node.node_id === data.node_id && (
  356. node.execution_metadata?.parallel_id === data.execution_metadata?.parallel_id || node.parallel_id === data.execution_metadata?.parallel_id),
  357. )
  358. if (nodeIndex !== -1) {
  359. currIteration[nodeIndex] = {
  360. ...currIteration[nodeIndex],
  361. ...data,
  362. } as any
  363. }
  364. else {
  365. currIteration.push({
  366. ...data,
  367. } as any)
  368. }
  369. }
  370. }))
  371. }
  372. else {
  373. // open parallel mode
  374. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  375. const tracing = draft.tracing!
  376. const iterations = tracing.find(trace => trace.node_id === nodeParentId) // the iteration node
  377. if (iterations && iterations.details) {
  378. const iterRunID = data.execution_metadata?.parallel_mode_run_id
  379. const currIteration = iterParallelLogMap.get(iterations.node_id)?.get(iterRunID)
  380. const nodeIndex = currIteration?.findIndex(node =>
  381. node.node_id === data.node_id && (
  382. node?.parallel_run_id === data.execution_metadata?.parallel_mode_run_id),
  383. )
  384. if (currIteration) {
  385. if (nodeIndex !== undefined && nodeIndex !== -1) {
  386. currIteration[nodeIndex] = {
  387. ...currIteration[nodeIndex],
  388. ...data,
  389. } as any
  390. }
  391. else {
  392. currIteration.push({
  393. ...data,
  394. } as any)
  395. }
  396. }
  397. setIterParallelLogMap(iterParallelLogMap)
  398. const iterLogMap = iterParallelLogMap.get(iterations.node_id)
  399. if (iterLogMap)
  400. iterations.details = Array.from(iterLogMap.values())
  401. }
  402. }))
  403. }
  404. }
  405. else {
  406. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  407. const currentIndex = draft.tracing!.findIndex((trace) => {
  408. if (!trace.execution_metadata?.parallel_id)
  409. return trace.node_id === data.node_id
  410. return trace.node_id === data.node_id && trace.execution_metadata?.parallel_id === data.execution_metadata?.parallel_id
  411. })
  412. if (currentIndex > -1 && draft.tracing) {
  413. draft.tracing[currentIndex] = {
  414. ...(draft.tracing[currentIndex].extras
  415. ? { extras: draft.tracing[currentIndex].extras }
  416. : {}),
  417. ...data,
  418. } as any
  419. }
  420. }))
  421. const newNodes = produce(nodes, (draft) => {
  422. const currentNode = draft.find(node => node.id === data.node_id)!
  423. currentNode.data._runningStatus = data.status as any
  424. if (data.status === NodeRunningStatus.Exception) {
  425. if (data.execution_metadata.error_strategy === ErrorHandleTypeEnum.failBranch)
  426. currentNode.data._runningBranchId = ErrorHandleTypeEnum.failBranch
  427. }
  428. else {
  429. if (data.node_type === BlockEnum.IfElse)
  430. currentNode.data._runningBranchId = data?.outputs?.selected_case_id
  431. if (data.node_type === BlockEnum.QuestionClassifier)
  432. currentNode.data._runningBranchId = data?.outputs?.class_id
  433. }
  434. })
  435. setNodes(newNodes)
  436. const newEdges = produce(edges, (draft) => {
  437. const incomeEdges = draft.filter((edge) => {
  438. return edge.target === data.node_id
  439. })
  440. incomeEdges.forEach((edge) => {
  441. edge.data = {
  442. ...edge.data,
  443. _targetRunningStatus: data.status as any,
  444. }
  445. })
  446. })
  447. setEdges(newEdges)
  448. prevNodeId = data.node_id
  449. }
  450. if (onNodeFinished)
  451. onNodeFinished(params)
  452. },
  453. onIterationStart: (params) => {
  454. const { data } = params
  455. const {
  456. workflowRunningData,
  457. setWorkflowRunningData,
  458. setIterTimes,
  459. } = workflowStore.getState()
  460. const {
  461. getNodes,
  462. setNodes,
  463. edges,
  464. setEdges,
  465. transform,
  466. } = store.getState()
  467. const nodes = getNodes()
  468. setIterTimes(DEFAULT_ITER_TIMES)
  469. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  470. draft.tracing!.push({
  471. ...data,
  472. status: NodeRunningStatus.Running,
  473. details: [],
  474. iterDurationMap: {},
  475. } as any)
  476. }))
  477. const {
  478. setViewport,
  479. } = reactflow
  480. const currentNodeIndex = nodes.findIndex(node => node.id === data.node_id)
  481. const currentNode = nodes[currentNodeIndex]
  482. const position = currentNode.position
  483. const zoom = transform[2]
  484. if (!currentNode.parentId) {
  485. setViewport({
  486. x: (clientWidth - 400 - currentNode.width! * zoom) / 2 - position.x * zoom,
  487. y: (clientHeight - currentNode.height! * zoom) / 2 - position.y * zoom,
  488. zoom: transform[2],
  489. })
  490. }
  491. const newNodes = produce(nodes, (draft) => {
  492. draft[currentNodeIndex].data._runningStatus = NodeRunningStatus.Running
  493. draft[currentNodeIndex].data._iterationLength = data.metadata.iterator_length
  494. draft[currentNodeIndex].data._waitingRun = false
  495. })
  496. setNodes(newNodes)
  497. const newEdges = produce(edges, (draft) => {
  498. const incomeEdges = draft.filter(edge => edge.target === data.node_id)
  499. incomeEdges.forEach((edge) => {
  500. edge.data = {
  501. ...edge.data,
  502. _sourceRunningStatus: nodes.find(node => node.id === edge.source)!.data._runningStatus,
  503. _targetRunningStatus: NodeRunningStatus.Running,
  504. _waitingRun: false,
  505. }
  506. })
  507. })
  508. setEdges(newEdges)
  509. if (onIterationStart)
  510. onIterationStart(params)
  511. },
  512. onIterationNext: (params) => {
  513. const {
  514. workflowRunningData,
  515. setWorkflowRunningData,
  516. iterTimes,
  517. setIterTimes,
  518. } = workflowStore.getState()
  519. const { data } = params
  520. const {
  521. getNodes,
  522. setNodes,
  523. } = store.getState()
  524. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  525. const iteration = draft.tracing!.find(trace => trace.node_id === data.node_id)
  526. if (iteration) {
  527. if (iteration.iterDurationMap && data.duration)
  528. iteration.iterDurationMap[data.parallel_mode_run_id ?? `${data.index - 1}`] = data.duration
  529. if (iteration.details!.length >= iteration.metadata.iterator_length!)
  530. return
  531. }
  532. if (!data.parallel_mode_run_id)
  533. iteration?.details!.push([])
  534. }))
  535. const nodes = getNodes()
  536. const newNodes = produce(nodes, (draft) => {
  537. const currentNode = draft.find(node => node.id === data.node_id)!
  538. currentNode.data._iterationIndex = iterTimes
  539. setIterTimes(iterTimes + 1)
  540. })
  541. setNodes(newNodes)
  542. if (onIterationNext)
  543. onIterationNext(params)
  544. },
  545. onIterationFinish: (params) => {
  546. const { data } = params
  547. const {
  548. workflowRunningData,
  549. setWorkflowRunningData,
  550. setIterTimes,
  551. } = workflowStore.getState()
  552. const {
  553. getNodes,
  554. setNodes,
  555. } = store.getState()
  556. const nodes = getNodes()
  557. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  558. const tracing = draft.tracing!
  559. const currIterationNode = tracing.find(trace => trace.node_id === data.node_id)
  560. if (currIterationNode) {
  561. Object.assign(currIterationNode, {
  562. ...data,
  563. status: NodeRunningStatus.Succeeded,
  564. })
  565. }
  566. }))
  567. setIterTimes(DEFAULT_ITER_TIMES)
  568. const newNodes = produce(nodes, (draft) => {
  569. const currentNode = draft.find(node => node.id === data.node_id)!
  570. currentNode.data._runningStatus = data.status
  571. })
  572. setNodes(newNodes)
  573. prevNodeId = data.node_id
  574. if (onIterationFinish)
  575. onIterationFinish(params)
  576. },
  577. onParallelBranchStarted: (params) => {
  578. // console.log(params, 'parallel start')
  579. },
  580. onParallelBranchFinished: (params) => {
  581. // console.log(params, 'finished')
  582. },
  583. onTextChunk: (params) => {
  584. const { data: { text } } = params
  585. const {
  586. workflowRunningData,
  587. setWorkflowRunningData,
  588. } = workflowStore.getState()
  589. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  590. draft.resultTabActive = true
  591. draft.resultText += text
  592. }))
  593. },
  594. onTextReplace: (params) => {
  595. const { data: { text } } = params
  596. const {
  597. workflowRunningData,
  598. setWorkflowRunningData,
  599. } = workflowStore.getState()
  600. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  601. draft.resultText = text
  602. }))
  603. },
  604. onTTSChunk: (messageId: string, audio: string, audioType?: string) => {
  605. if (!audio || audio === '')
  606. return
  607. player.playAudioWithAudio(audio, true)
  608. AudioPlayerManager.getInstance().resetMsgId(messageId)
  609. },
  610. onTTSEnd: (messageId: string, audio: string, audioType?: string) => {
  611. player.playAudioWithAudio(audio, false)
  612. },
  613. ...restCallback,
  614. },
  615. )
  616. }, [store, reactflow, workflowStore, doSyncWorkflowDraft])
  617. const handleStopRun = useCallback((taskId: string) => {
  618. const appId = useAppStore.getState().appDetail?.id
  619. stopWorkflowRun(`/apps/${appId}/workflow-runs/tasks/${taskId}/stop`)
  620. }, [])
  621. const handleRestoreFromPublishedWorkflow = useCallback(async () => {
  622. const appDetail = useAppStore.getState().appDetail
  623. const publishedWorkflow = await fetchPublishedWorkflow(`/apps/${appDetail?.id}/workflows/publish`)
  624. if (publishedWorkflow) {
  625. const nodes = publishedWorkflow.graph.nodes
  626. const edges = publishedWorkflow.graph.edges
  627. const viewport = publishedWorkflow.graph.viewport!
  628. handleUpdateWorkflowCanvas({
  629. nodes,
  630. edges,
  631. viewport,
  632. })
  633. featuresStore?.setState({ features: publishedWorkflow.features })
  634. workflowStore.getState().setPublishedAt(publishedWorkflow.created_at)
  635. workflowStore.getState().setEnvironmentVariables(publishedWorkflow.environment_variables || [])
  636. }
  637. }, [featuresStore, handleUpdateWorkflowCanvas, workflowStore])
  638. return {
  639. handleBackupDraft,
  640. handleLoadBackupDraft,
  641. handleRun,
  642. handleStopRun,
  643. handleRestoreFromPublishedWorkflow,
  644. }
  645. }