dify/web/app/components/rag-pipeline/hooks/use-pipeline-start-run.tsx

68 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-05-07 16:30:24 +08:00
import { useCallback } from 'react'
import { useStoreApi } from 'reactflow'
import { useWorkflowStore } from '@/app/components/workflow/store'
import {
BlockEnum,
WorkflowRunningStatus,
} from '@/app/components/workflow/types'
import { useWorkflowInteractions } from '@/app/components/workflow/hooks'
import {
useNodesSyncDraft,
2025-05-09 15:53:31 +08:00
usePipelineRun,
2025-05-07 16:30:24 +08:00
} from '.'
2025-05-09 15:53:31 +08:00
export const usePipelineStartRun = () => {
2025-05-07 16:30:24 +08:00
const store = useStoreApi()
const workflowStore = useWorkflowStore()
const { handleCancelDebugAndPreviewPanel } = useWorkflowInteractions()
2025-05-09 15:53:31 +08:00
const { handleRun } = usePipelineRun()
2025-05-07 16:30:24 +08:00
const { doSyncWorkflowDraft } = useNodesSyncDraft()
const handleWorkflowStartRunInWorkflow = useCallback(async () => {
const {
workflowRunningData,
} = workflowStore.getState()
if (workflowRunningData?.result.status === WorkflowRunningStatus.Running)
return
const { getNodes } = store.getState()
const nodes = getNodes()
const startNode = nodes.find(node => node.data.type === BlockEnum.Start)
const startVariables = startNode?.data.variables || []
const {
2025-05-07 17:31:06 +08:00
showDebugAndPreviewPanel,
2025-05-07 16:30:24 +08:00
setShowInputsPanel,
setShowEnvPanel,
2025-05-07 17:31:06 +08:00
setShowDebugAndPreviewPanel,
2025-05-07 16:30:24 +08:00
} = workflowStore.getState()
setShowEnvPanel(false)
2025-05-07 17:31:06 +08:00
if (showDebugAndPreviewPanel) {
2025-05-07 16:30:24 +08:00
handleCancelDebugAndPreviewPanel()
return
}
if (!startVariables.length) {
await doSyncWorkflowDraft()
handleRun({ inputs: {}, files: [] })
2025-05-07 17:31:06 +08:00
setShowDebugAndPreviewPanel(true)
2025-05-07 16:30:24 +08:00
setShowInputsPanel(false)
}
else {
2025-05-07 17:31:06 +08:00
setShowDebugAndPreviewPanel(true)
2025-05-07 16:30:24 +08:00
setShowInputsPanel(true)
}
}, [store, workflowStore, handleCancelDebugAndPreviewPanel, handleRun, doSyncWorkflowDraft])
const handleStartWorkflowRun = useCallback(() => {
handleWorkflowStartRunInWorkflow()
}, [handleWorkflowStartRunInWorkflow])
return {
handleStartWorkflowRun,
handleWorkflowStartRunInWorkflow,
}
}