use-workflow-run.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. })
  182. })
  183. setNodes(newNodes)
  184. const newEdges = produce(edges, (draft) => {
  185. draft.forEach((edge) => {
  186. edge.data = {
  187. ...edge.data,
  188. _sourceRunningStatus: undefined,
  189. _targetRunningStatus: undefined,
  190. _waitingRun: true,
  191. }
  192. })
  193. })
  194. setEdges(newEdges)
  195. if (onWorkflowStarted)
  196. onWorkflowStarted(params)
  197. },
  198. onWorkflowFinished: (params) => {
  199. const { data } = params
  200. const {
  201. workflowRunningData,
  202. setWorkflowRunningData,
  203. } = workflowStore.getState()
  204. const isStringOutput = data.outputs && Object.keys(data.outputs).length === 1 && typeof data.outputs[Object.keys(data.outputs)[0]] === 'string'
  205. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  206. draft.result = {
  207. ...draft.result,
  208. ...data,
  209. files: getFilesInLogs(data.outputs),
  210. } as any
  211. if (isStringOutput) {
  212. draft.resultTabActive = true
  213. draft.resultText = data.outputs[Object.keys(data.outputs)[0]]
  214. }
  215. }))
  216. prevNodeId = ''
  217. if (onWorkflowFinished)
  218. onWorkflowFinished(params)
  219. },
  220. onError: (params) => {
  221. const {
  222. workflowRunningData,
  223. setWorkflowRunningData,
  224. } = workflowStore.getState()
  225. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  226. draft.result = {
  227. ...draft.result,
  228. status: WorkflowRunningStatus.Failed,
  229. }
  230. }))
  231. if (onError)
  232. onError(params)
  233. },
  234. onNodeStarted: (params) => {
  235. const { data } = params
  236. const {
  237. workflowRunningData,
  238. setWorkflowRunningData,
  239. iterParallelLogMap,
  240. setIterParallelLogMap,
  241. } = workflowStore.getState()
  242. const {
  243. getNodes,
  244. setNodes,
  245. edges,
  246. setEdges,
  247. transform,
  248. } = store.getState()
  249. const nodes = getNodes()
  250. const node = nodes.find(node => node.id === data.node_id)
  251. if (node?.parentId) {
  252. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  253. const tracing = draft.tracing!
  254. const iterations = tracing.find(trace => trace.node_id === node?.parentId)
  255. const currIteration = iterations?.details![node.data.iteration_index] || iterations?.details![iterations.details!.length - 1]
  256. if (!data.parallel_run_id) {
  257. currIteration?.push({
  258. ...data,
  259. status: NodeRunningStatus.Running,
  260. } as any)
  261. }
  262. else {
  263. const nodeId = iterations?.node_id as string
  264. if (!iterParallelLogMap.has(nodeId as string))
  265. iterParallelLogMap.set(iterations?.node_id as string, new Map())
  266. const currentIterLogMap = iterParallelLogMap.get(nodeId)!
  267. if (!currentIterLogMap.has(data.parallel_run_id))
  268. currentIterLogMap.set(data.parallel_run_id, [{ ...data, status: NodeRunningStatus.Running } as any])
  269. else
  270. currentIterLogMap.get(data.parallel_run_id)!.push({ ...data, status: NodeRunningStatus.Running } as any)
  271. setIterParallelLogMap(iterParallelLogMap)
  272. if (iterations)
  273. iterations.details = Array.from(currentIterLogMap.values())
  274. }
  275. }))
  276. }
  277. else {
  278. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  279. draft.tracing!.push({
  280. ...data,
  281. status: NodeRunningStatus.Running,
  282. } as any)
  283. }))
  284. const {
  285. setViewport,
  286. } = reactflow
  287. const currentNodeIndex = nodes.findIndex(node => node.id === data.node_id)
  288. const currentNode = nodes[currentNodeIndex]
  289. const position = currentNode.position
  290. const zoom = transform[2]
  291. if (!currentNode.parentId) {
  292. setViewport({
  293. x: (clientWidth - 400 - currentNode.width! * zoom) / 2 - position.x * zoom,
  294. y: (clientHeight - currentNode.height! * zoom) / 2 - position.y * zoom,
  295. zoom: transform[2],
  296. })
  297. }
  298. const newNodes = produce(nodes, (draft) => {
  299. draft[currentNodeIndex].data._runningStatus = NodeRunningStatus.Running
  300. draft[currentNodeIndex].data._waitingRun = false
  301. })
  302. setNodes(newNodes)
  303. const newEdges = produce(edges, (draft) => {
  304. const incomeEdges = draft.filter((edge) => {
  305. return edge.target === data.node_id
  306. })
  307. incomeEdges.forEach((edge) => {
  308. const incomeNode = nodes.find(node => node.id === edge.source)!
  309. if (
  310. (!incomeNode.data._runningBranchId && edge.sourceHandle === 'source')
  311. || (incomeNode.data._runningBranchId && edge.sourceHandle === incomeNode.data._runningBranchId)
  312. ) {
  313. edge.data = {
  314. ...edge.data,
  315. _sourceRunningStatus: incomeNode.data._runningStatus,
  316. _targetRunningStatus: NodeRunningStatus.Running,
  317. _waitingRun: false,
  318. }
  319. }
  320. })
  321. })
  322. setEdges(newEdges)
  323. }
  324. if (onNodeStarted)
  325. onNodeStarted(params)
  326. },
  327. onNodeFinished: (params) => {
  328. const { data } = params
  329. const {
  330. workflowRunningData,
  331. setWorkflowRunningData,
  332. iterParallelLogMap,
  333. setIterParallelLogMap,
  334. } = workflowStore.getState()
  335. const {
  336. getNodes,
  337. setNodes,
  338. edges,
  339. setEdges,
  340. } = store.getState()
  341. const nodes = getNodes()
  342. const nodeParentId = nodes.find(node => node.id === data.node_id)!.parentId
  343. if (nodeParentId) {
  344. if (!data.execution_metadata.parallel_mode_run_id) {
  345. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  346. const tracing = draft.tracing!
  347. const iterations = tracing.find(trace => trace.node_id === nodeParentId) // the iteration node
  348. if (iterations && iterations.details) {
  349. const iterationIndex = data.execution_metadata?.iteration_index || 0
  350. if (!iterations.details[iterationIndex])
  351. iterations.details[iterationIndex] = []
  352. const currIteration = iterations.details[iterationIndex]
  353. const nodeIndex = currIteration.findIndex(node =>
  354. node.node_id === data.node_id && (
  355. node.execution_metadata?.parallel_id === data.execution_metadata?.parallel_id || node.parallel_id === data.execution_metadata?.parallel_id),
  356. )
  357. if (nodeIndex !== -1) {
  358. currIteration[nodeIndex] = {
  359. ...currIteration[nodeIndex],
  360. ...data,
  361. } as any
  362. }
  363. else {
  364. currIteration.push({
  365. ...data,
  366. } as any)
  367. }
  368. }
  369. }))
  370. }
  371. else {
  372. // open parallel mode
  373. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  374. const tracing = draft.tracing!
  375. const iterations = tracing.find(trace => trace.node_id === nodeParentId) // the iteration node
  376. if (iterations && iterations.details) {
  377. const iterRunID = data.execution_metadata?.parallel_mode_run_id
  378. const currIteration = iterParallelLogMap.get(iterations.node_id)?.get(iterRunID)
  379. const nodeIndex = currIteration?.findIndex(node =>
  380. node.node_id === data.node_id && (
  381. node?.parallel_run_id === data.execution_metadata?.parallel_mode_run_id),
  382. )
  383. if (currIteration) {
  384. if (nodeIndex !== undefined && nodeIndex !== -1) {
  385. currIteration[nodeIndex] = {
  386. ...currIteration[nodeIndex],
  387. ...data,
  388. } as any
  389. }
  390. else {
  391. currIteration.push({
  392. ...data,
  393. } as any)
  394. }
  395. }
  396. setIterParallelLogMap(iterParallelLogMap)
  397. const iterLogMap = iterParallelLogMap.get(iterations.node_id)
  398. if (iterLogMap)
  399. iterations.details = Array.from(iterLogMap.values())
  400. }
  401. }))
  402. }
  403. }
  404. else {
  405. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  406. const currentIndex = draft.tracing!.findIndex((trace) => {
  407. if (!trace.execution_metadata?.parallel_id)
  408. return trace.node_id === data.node_id
  409. return trace.node_id === data.node_id && trace.execution_metadata?.parallel_id === data.execution_metadata?.parallel_id
  410. })
  411. if (currentIndex > -1 && draft.tracing) {
  412. draft.tracing[currentIndex] = {
  413. ...(draft.tracing[currentIndex].extras
  414. ? { extras: draft.tracing[currentIndex].extras }
  415. : {}),
  416. ...data,
  417. } as any
  418. }
  419. }))
  420. const newNodes = produce(nodes, (draft) => {
  421. const currentNode = draft.find(node => node.id === data.node_id)!
  422. currentNode.data._runningStatus = data.status as any
  423. if (data.status === NodeRunningStatus.Exception) {
  424. if (data.execution_metadata.error_strategy === ErrorHandleTypeEnum.failBranch)
  425. currentNode.data._runningBranchId = ErrorHandleTypeEnum.failBranch
  426. }
  427. else {
  428. if (data.node_type === BlockEnum.IfElse)
  429. currentNode.data._runningBranchId = data?.outputs?.selected_case_id
  430. if (data.node_type === BlockEnum.QuestionClassifier)
  431. currentNode.data._runningBranchId = data?.outputs?.class_id
  432. }
  433. })
  434. setNodes(newNodes)
  435. const newEdges = produce(edges, (draft) => {
  436. const incomeEdges = draft.filter((edge) => {
  437. return edge.target === data.node_id
  438. })
  439. incomeEdges.forEach((edge) => {
  440. edge.data = {
  441. ...edge.data,
  442. _targetRunningStatus: data.status as any,
  443. }
  444. })
  445. })
  446. setEdges(newEdges)
  447. prevNodeId = data.node_id
  448. }
  449. if (onNodeFinished)
  450. onNodeFinished(params)
  451. },
  452. onIterationStart: (params) => {
  453. const { data } = params
  454. const {
  455. workflowRunningData,
  456. setWorkflowRunningData,
  457. setIterTimes,
  458. } = workflowStore.getState()
  459. const {
  460. getNodes,
  461. setNodes,
  462. edges,
  463. setEdges,
  464. transform,
  465. } = store.getState()
  466. const nodes = getNodes()
  467. setIterTimes(DEFAULT_ITER_TIMES)
  468. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  469. draft.tracing!.push({
  470. ...data,
  471. status: NodeRunningStatus.Running,
  472. details: [],
  473. iterDurationMap: {},
  474. } as any)
  475. }))
  476. const {
  477. setViewport,
  478. } = reactflow
  479. const currentNodeIndex = nodes.findIndex(node => node.id === data.node_id)
  480. const currentNode = nodes[currentNodeIndex]
  481. const position = currentNode.position
  482. const zoom = transform[2]
  483. if (!currentNode.parentId) {
  484. setViewport({
  485. x: (clientWidth - 400 - currentNode.width! * zoom) / 2 - position.x * zoom,
  486. y: (clientHeight - currentNode.height! * zoom) / 2 - position.y * zoom,
  487. zoom: transform[2],
  488. })
  489. }
  490. const newNodes = produce(nodes, (draft) => {
  491. draft[currentNodeIndex].data._runningStatus = NodeRunningStatus.Running
  492. draft[currentNodeIndex].data._iterationLength = data.metadata.iterator_length
  493. draft[currentNodeIndex].data._waitingRun = false
  494. })
  495. setNodes(newNodes)
  496. const newEdges = produce(edges, (draft) => {
  497. const incomeEdges = draft.filter(edge => edge.target === data.node_id)
  498. incomeEdges.forEach((edge) => {
  499. edge.data = {
  500. ...edge.data,
  501. _sourceRunningStatus: nodes.find(node => node.id === edge.source)!.data._runningStatus,
  502. _targetRunningStatus: NodeRunningStatus.Running,
  503. _waitingRun: false,
  504. }
  505. })
  506. })
  507. setEdges(newEdges)
  508. if (onIterationStart)
  509. onIterationStart(params)
  510. },
  511. onIterationNext: (params) => {
  512. const {
  513. workflowRunningData,
  514. setWorkflowRunningData,
  515. iterTimes,
  516. setIterTimes,
  517. } = workflowStore.getState()
  518. const { data } = params
  519. const {
  520. getNodes,
  521. setNodes,
  522. } = store.getState()
  523. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  524. const iteration = draft.tracing!.find(trace => trace.node_id === data.node_id)
  525. if (iteration) {
  526. if (iteration.iterDurationMap && data.duration)
  527. iteration.iterDurationMap[data.parallel_mode_run_id ?? `${data.index - 1}`] = data.duration
  528. if (iteration.details!.length >= iteration.metadata.iterator_length!)
  529. return
  530. }
  531. if (!data.parallel_mode_run_id)
  532. iteration?.details!.push([])
  533. }))
  534. const nodes = getNodes()
  535. const newNodes = produce(nodes, (draft) => {
  536. const currentNode = draft.find(node => node.id === data.node_id)!
  537. currentNode.data._iterationIndex = iterTimes
  538. setIterTimes(iterTimes + 1)
  539. })
  540. setNodes(newNodes)
  541. if (onIterationNext)
  542. onIterationNext(params)
  543. },
  544. onIterationFinish: (params) => {
  545. const { data } = params
  546. const {
  547. workflowRunningData,
  548. setWorkflowRunningData,
  549. setIterTimes,
  550. } = workflowStore.getState()
  551. const {
  552. getNodes,
  553. setNodes,
  554. } = store.getState()
  555. const nodes = getNodes()
  556. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  557. const tracing = draft.tracing!
  558. const currIterationNode = tracing.find(trace => trace.node_id === data.node_id)
  559. if (currIterationNode) {
  560. Object.assign(currIterationNode, {
  561. ...data,
  562. status: NodeRunningStatus.Succeeded,
  563. })
  564. }
  565. }))
  566. setIterTimes(DEFAULT_ITER_TIMES)
  567. const newNodes = produce(nodes, (draft) => {
  568. const currentNode = draft.find(node => node.id === data.node_id)!
  569. currentNode.data._runningStatus = data.status
  570. })
  571. setNodes(newNodes)
  572. prevNodeId = data.node_id
  573. if (onIterationFinish)
  574. onIterationFinish(params)
  575. },
  576. onParallelBranchStarted: (params) => {
  577. // console.log(params, 'parallel start')
  578. },
  579. onParallelBranchFinished: (params) => {
  580. // console.log(params, 'finished')
  581. },
  582. onTextChunk: (params) => {
  583. const { data: { text } } = params
  584. const {
  585. workflowRunningData,
  586. setWorkflowRunningData,
  587. } = workflowStore.getState()
  588. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  589. draft.resultTabActive = true
  590. draft.resultText += text
  591. }))
  592. },
  593. onTextReplace: (params) => {
  594. const { data: { text } } = params
  595. const {
  596. workflowRunningData,
  597. setWorkflowRunningData,
  598. } = workflowStore.getState()
  599. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  600. draft.resultText = text
  601. }))
  602. },
  603. onTTSChunk: (messageId: string, audio: string, audioType?: string) => {
  604. if (!audio || audio === '')
  605. return
  606. player.playAudioWithAudio(audio, true)
  607. AudioPlayerManager.getInstance().resetMsgId(messageId)
  608. },
  609. onTTSEnd: (messageId: string, audio: string, audioType?: string) => {
  610. player.playAudioWithAudio(audio, false)
  611. },
  612. ...restCallback,
  613. },
  614. )
  615. }, [store, reactflow, workflowStore, doSyncWorkflowDraft])
  616. const handleStopRun = useCallback((taskId: string) => {
  617. const appId = useAppStore.getState().appDetail?.id
  618. stopWorkflowRun(`/apps/${appId}/workflow-runs/tasks/${taskId}/stop`)
  619. }, [])
  620. const handleRestoreFromPublishedWorkflow = useCallback(async () => {
  621. const appDetail = useAppStore.getState().appDetail
  622. const publishedWorkflow = await fetchPublishedWorkflow(`/apps/${appDetail?.id}/workflows/publish`)
  623. if (publishedWorkflow) {
  624. const nodes = publishedWorkflow.graph.nodes
  625. const edges = publishedWorkflow.graph.edges
  626. const viewport = publishedWorkflow.graph.viewport!
  627. handleUpdateWorkflowCanvas({
  628. nodes,
  629. edges,
  630. viewport,
  631. })
  632. featuresStore?.setState({ features: publishedWorkflow.features })
  633. workflowStore.getState().setPublishedAt(publishedWorkflow.created_at)
  634. workflowStore.getState().setEnvironmentVariables(publishedWorkflow.environment_variables || [])
  635. }
  636. }, [featuresStore, handleUpdateWorkflowCanvas, workflowStore])
  637. return {
  638. handleBackupDraft,
  639. handleLoadBackupDraft,
  640. handleRun,
  641. handleStopRun,
  642. handleRestoreFromPublishedWorkflow,
  643. }
  644. }