| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   memo, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   useCallback, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   useMemo, | 
					
						
							|  |  |  | } from 'react' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							|  |  |  | import { useNodes } from 'reactflow' | 
					
						
							|  |  |  | import FormItem from '../nodes/_base/components/before-run-form/form-item' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   BlockEnum, | 
					
						
							|  |  |  |   InputVarType, | 
					
						
							|  |  |  |   WorkflowRunningStatus, | 
					
						
							|  |  |  | } from '../types' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   useStore, | 
					
						
							|  |  |  |   useWorkflowStore, | 
					
						
							|  |  |  | } from '../store' | 
					
						
							|  |  |  | import { useWorkflowRun } from '../hooks' | 
					
						
							|  |  |  | import type { StartNodeType } from '../nodes/start/types' | 
					
						
							| 
									
										
										
										
											2024-04-09 15:54:36 +08:00
										 |  |  | import { TransferMethod } from '../../base/text-generation/types' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import Button from '@/app/components/base/button' | 
					
						
							|  |  |  | import { useFeatures } from '@/app/components/base/features/hooks' | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   getProcessedInputs, | 
					
						
							|  |  |  | } from '@/app/components/base/chat/chat/utils' | 
					
						
							|  |  |  | import { useCheckInputsForms } from '@/app/components/base/chat/chat/check-input-forms-hooks' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  | type Props = { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   onRun: () => void | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const InputsPanel = ({ onRun }: Props) => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const workflowStore = useWorkflowStore() | 
					
						
							|  |  |  |   const fileSettings = useFeatures(s => s.features.file) | 
					
						
							|  |  |  |   const nodes = useNodes<StartNodeType>() | 
					
						
							|  |  |  |   const inputs = useStore(s => s.inputs) | 
					
						
							|  |  |  |   const files = useStore(s => s.files) | 
					
						
							|  |  |  |   const workflowRunningData = useStore(s => s.workflowRunningData) | 
					
						
							|  |  |  |   const { | 
					
						
							|  |  |  |     handleRun, | 
					
						
							|  |  |  |   } = useWorkflowRun() | 
					
						
							|  |  |  |   const startNode = nodes.find(node => node.data.type === BlockEnum.Start) | 
					
						
							|  |  |  |   const startVariables = startNode?.data.variables | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   const { checkInputsForm } = useCheckInputsForms() | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const variables = useMemo(() => { | 
					
						
							|  |  |  |     const data = startVariables || [] | 
					
						
							|  |  |  |     if (fileSettings?.image?.enabled) { | 
					
						
							|  |  |  |       return [ | 
					
						
							|  |  |  |         ...data, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           type: InputVarType.files, | 
					
						
							|  |  |  |           variable: '__image', | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |           required: false, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           label: 'files', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       ] | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return data | 
					
						
							|  |  |  |   }, [fileSettings?.image?.enabled, startVariables]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleValueChange = (variable: string, v: any) => { | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |     const { | 
					
						
							|  |  |  |       inputs, | 
					
						
							|  |  |  |       setInputs, | 
					
						
							|  |  |  |     } = workflowStore.getState() | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     if (variable === '__image') { | 
					
						
							|  |  |  |       workflowStore.setState({ | 
					
						
							|  |  |  |         files: v, | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |       setInputs({ | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         ...inputs, | 
					
						
							|  |  |  |         [variable]: v, | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   const doRun = useCallback(() => { | 
					
						
							|  |  |  |     if (!checkInputsForm(inputs, variables as any)) | 
					
						
							|  |  |  |       return | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     onRun() | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |     handleRun({ inputs: getProcessedInputs(inputs, variables as any), files }) | 
					
						
							|  |  |  |   }, [files, handleRun, inputs, onRun, variables, checkInputsForm]) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   const canRun = useMemo(() => { | 
					
						
							| 
									
										
										
										
											2024-04-09 15:54:36 +08:00
										 |  |  |     if (files?.some(item => (item.transfer_method as any) === TransferMethod.local_file && !item.upload_file_id)) | 
					
						
							|  |  |  |       return false | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return true | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   }, [files]) | 
					
						
							| 
									
										
										
										
											2024-04-09 15:54:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   return ( | 
					
						
							|  |  |  |     <> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |       <div className='px-4 pb-2 pt-3'> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |           variables.map((variable, index) => ( | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             <div | 
					
						
							|  |  |  |               key={variable.variable} | 
					
						
							|  |  |  |               className='mb-2 last-of-type:mb-0' | 
					
						
							|  |  |  |             > | 
					
						
							|  |  |  |               <FormItem | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |                 autoFocus={index === 0} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                 className='!block' | 
					
						
							|  |  |  |                 payload={variable} | 
					
						
							|  |  |  |                 value={inputs[variable.variable]} | 
					
						
							|  |  |  |                 onChange={v => handleValueChange(variable.variable, v)} | 
					
						
							|  |  |  |               /> | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |           )) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |       <div className='flex items-center justify-between px-4 py-2'> | 
					
						
							|  |  |  |         <Button | 
					
						
							| 
									
										
										
										
											2024-06-19 14:13:16 +08:00
										 |  |  |           variant='primary' | 
					
						
							| 
									
										
										
										
											2024-04-09 15:54:36 +08:00
										 |  |  |           disabled={!canRun || workflowRunningData?.result?.status === WorkflowRunningStatus.Running} | 
					
						
							| 
									
										
										
										
											2024-06-21 14:17:45 +08:00
										 |  |  |           className='w-full' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           onClick={doRun} | 
					
						
							|  |  |  |         > | 
					
						
							|  |  |  |           {t('workflow.singleRun.startRun')} | 
					
						
							|  |  |  |         </Button> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     </> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default memo(InputsPanel) |