use-workflow-start-run.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { useCallback } from 'react'
  2. import { useStoreApi } from 'reactflow'
  3. import { useWorkflowStore } from '../store'
  4. import {
  5. BlockEnum,
  6. WorkflowRunningStatus,
  7. } from '../types'
  8. import {
  9. useIsChatMode,
  10. useNodesSyncDraft,
  11. useWorkflowInteractions,
  12. useWorkflowRun,
  13. } from './index'
  14. import { useFeaturesStore } from '@/app/components/base/features/hooks'
  15. export const useWorkflowStartRun = () => {
  16. const store = useStoreApi()
  17. const workflowStore = useWorkflowStore()
  18. const featuresStore = useFeaturesStore()
  19. const isChatMode = useIsChatMode()
  20. const { handleCancelDebugAndPreviewPanel } = useWorkflowInteractions()
  21. const { handleRun } = useWorkflowRun()
  22. const { doSyncWorkflowDraft } = useNodesSyncDraft()
  23. const handleWorkflowStartRunInWorkflow = useCallback(async () => {
  24. const {
  25. workflowRunningData,
  26. } = workflowStore.getState()
  27. if (workflowRunningData?.result.status === WorkflowRunningStatus.Running)
  28. return
  29. const { getNodes } = store.getState()
  30. const nodes = getNodes()
  31. const startNode = nodes.find(node => node.data.type === BlockEnum.Start)
  32. const startVariables = startNode?.data.variables || []
  33. const fileSettings = featuresStore!.getState().features.file
  34. const {
  35. showDebugAndPreviewPanel,
  36. setShowDebugAndPreviewPanel,
  37. setShowInputsPanel,
  38. setShowEnvPanel,
  39. } = workflowStore.getState()
  40. setShowEnvPanel(false)
  41. if (showDebugAndPreviewPanel) {
  42. handleCancelDebugAndPreviewPanel()
  43. return
  44. }
  45. if (!startVariables.length && !fileSettings?.image?.enabled) {
  46. await doSyncWorkflowDraft()
  47. handleRun({ inputs: {}, files: [] })
  48. setShowDebugAndPreviewPanel(true)
  49. setShowInputsPanel(false)
  50. }
  51. else {
  52. setShowDebugAndPreviewPanel(true)
  53. setShowInputsPanel(true)
  54. }
  55. }, [store, workflowStore, featuresStore, handleCancelDebugAndPreviewPanel, handleRun, doSyncWorkflowDraft])
  56. const handleWorkflowStartRunInChatflow = useCallback(async () => {
  57. const {
  58. showDebugAndPreviewPanel,
  59. setShowDebugAndPreviewPanel,
  60. setHistoryWorkflowData,
  61. setShowEnvPanel,
  62. setShowChatVariablePanel,
  63. } = workflowStore.getState()
  64. setShowEnvPanel(false)
  65. setShowChatVariablePanel(false)
  66. if (showDebugAndPreviewPanel)
  67. handleCancelDebugAndPreviewPanel()
  68. else
  69. setShowDebugAndPreviewPanel(true)
  70. setHistoryWorkflowData(undefined)
  71. }, [workflowStore, handleCancelDebugAndPreviewPanel])
  72. const handleStartWorkflowRun = useCallback(() => {
  73. if (!isChatMode)
  74. handleWorkflowStartRunInWorkflow()
  75. else
  76. handleWorkflowStartRunInChatflow()
  77. }, [isChatMode, handleWorkflowStartRunInWorkflow, handleWorkflowStartRunInChatflow])
  78. return {
  79. handleStartWorkflowRun,
  80. handleWorkflowStartRunInWorkflow,
  81. handleWorkflowStartRunInChatflow,
  82. }
  83. }