| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   memo, | 
					
						
							|  |  |  |   useEffect, | 
					
						
							|  |  |  |   useState, | 
					
						
							|  |  |  | } from 'react' | 
					
						
							| 
									
										
										
										
											2024-06-20 11:05:08 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   RiClipboardLine, | 
					
						
							|  |  |  |   RiCloseLine, | 
					
						
							|  |  |  | } from '@remixicon/react' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							| 
									
										
										
										
											2024-05-11 16:59:17 +08:00
										 |  |  | import copy from 'copy-to-clipboard' | 
					
						
							| 
									
										
										
										
											2024-04-28 17:37:00 +08:00
										 |  |  | import ResultText from '../run/result-text' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import ResultPanel from '../run/result-panel' | 
					
						
							|  |  |  | import TracingPanel from '../run/tracing-panel' | 
					
						
							|  |  |  | import { | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |   useWorkflowInteractions, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | } from '../hooks' | 
					
						
							|  |  |  | import { useStore } from '../store' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   WorkflowRunningStatus, | 
					
						
							|  |  |  | } from '../types' | 
					
						
							| 
									
										
										
										
											2024-05-11 16:59:17 +08:00
										 |  |  | import Toast from '../../base/toast' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import InputsPanel from './inputs-panel' | 
					
						
							| 
									
										
										
										
											2024-07-09 15:05:40 +08:00
										 |  |  | import cn from '@/utils/classnames' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import Loading from '@/app/components/base/loading' | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  | import Button from '@/app/components/base/button' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  | const WorkflowPreview = () => { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const { t } = useTranslation() | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |   const { handleCancelDebugAndPreviewPanel } = useWorkflowInteractions() | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const workflowRunningData = useStore(s => s.workflowRunningData) | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |   const showInputsPanel = useStore(s => s.showInputsPanel) | 
					
						
							|  |  |  |   const showDebugAndPreviewPanel = useStore(s => s.showDebugAndPreviewPanel) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const [currentTab, setCurrentTab] = useState<string>(showInputsPanel ? 'INPUT' : 'TRACING') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const switchTab = async (tab: string) => { | 
					
						
							|  |  |  |     setCurrentTab(tab) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     if (showDebugAndPreviewPanel && showInputsPanel) | 
					
						
							|  |  |  |       setCurrentTab('INPUT') | 
					
						
							|  |  |  |   }, [showDebugAndPreviewPanel, showInputsPanel]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   useEffect(() => { | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |     if ((workflowRunningData?.result.status === WorkflowRunningStatus.Succeeded || workflowRunningData?.result.status === WorkflowRunningStatus.Failed) && !workflowRunningData.resultText && !workflowRunningData.result.files?.length) | 
					
						
							| 
									
										
										
										
											2024-04-28 17:37:00 +08:00
										 |  |  |       switchTab('DETAIL') | 
					
						
							|  |  |  |   }, [workflowRunningData]) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <div className={`
 | 
					
						
							| 
									
										
										
										
											2025-04-10 17:15:48 +08:00
										 |  |  |       flex h-full w-[420px] flex-col rounded-l-2xl border-[0.5px] border-components-panel-border bg-components-panel-bg shadow-xl | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     `}>
 | 
					
						
							| 
									
										
										
										
											2025-04-10 17:15:48 +08:00
										 |  |  |       <div className='flex items-center justify-between p-4 pb-1 text-base font-semibold text-text-primary'> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         {`Test Run${!workflowRunningData?.result.sequence_number ? '' : `#${workflowRunningData?.result.sequence_number}`}`} | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |         <div className='cursor-pointer p-1' onClick={() => handleCancelDebugAndPreviewPanel()}> | 
					
						
							| 
									
										
										
										
											2025-04-10 17:15:48 +08:00
										 |  |  |           <RiCloseLine className='h-4 w-4 text-text-tertiary' /> | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |         </div> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       </div> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |       <div className='relative flex grow flex-col'> | 
					
						
							| 
									
										
										
										
											2025-04-10 17:15:48 +08:00
										 |  |  |         <div className='flex shrink-0 items-center border-b-[0.5px] border-divider-subtle px-4'> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |           {showInputsPanel && ( | 
					
						
							|  |  |  |             <div | 
					
						
							|  |  |  |               className={cn( | 
					
						
							| 
									
										
										
										
											2025-04-10 17:15:48 +08:00
										 |  |  |                 'mr-6 cursor-pointer border-b-2 border-transparent py-3 text-[13px] font-semibold leading-[18px] text-text-tertiary', | 
					
						
							|  |  |  |                 currentTab === 'INPUT' && '!border-[rgb(21,94,239)] text-text-secondary', | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |               )} | 
					
						
							|  |  |  |               onClick={() => switchTab('INPUT')} | 
					
						
							|  |  |  |             >{t('runLog.input')}</div> | 
					
						
							|  |  |  |           )} | 
					
						
							|  |  |  |           <div | 
					
						
							|  |  |  |             className={cn( | 
					
						
							| 
									
										
										
										
											2025-04-10 17:15:48 +08:00
										 |  |  |               'mr-6 cursor-pointer border-b-2 border-transparent py-3 text-[13px] font-semibold leading-[18px] text-text-tertiary', | 
					
						
							|  |  |  |               currentTab === 'RESULT' && '!border-[rgb(21,94,239)] text-text-secondary', | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |               !workflowRunningData && '!cursor-not-allowed opacity-30', | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             )} | 
					
						
							|  |  |  |             onClick={() => { | 
					
						
							|  |  |  |               if (!workflowRunningData) | 
					
						
							|  |  |  |                 return | 
					
						
							|  |  |  |               switchTab('RESULT') | 
					
						
							|  |  |  |             }} | 
					
						
							|  |  |  |           >{t('runLog.result')}</div> | 
					
						
							|  |  |  |           <div | 
					
						
							|  |  |  |             className={cn( | 
					
						
							| 
									
										
										
										
											2025-04-10 17:15:48 +08:00
										 |  |  |               'mr-6 cursor-pointer border-b-2 border-transparent py-3 text-[13px] font-semibold leading-[18px] text-text-tertiary', | 
					
						
							|  |  |  |               currentTab === 'DETAIL' && '!border-[rgb(21,94,239)] text-text-secondary', | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |               !workflowRunningData && '!cursor-not-allowed opacity-30', | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             )} | 
					
						
							|  |  |  |             onClick={() => { | 
					
						
							|  |  |  |               if (!workflowRunningData) | 
					
						
							|  |  |  |                 return | 
					
						
							|  |  |  |               switchTab('DETAIL') | 
					
						
							|  |  |  |             }} | 
					
						
							|  |  |  |           >{t('runLog.detail')}</div> | 
					
						
							|  |  |  |           <div | 
					
						
							|  |  |  |             className={cn( | 
					
						
							| 
									
										
										
										
											2025-04-10 17:15:48 +08:00
										 |  |  |               'mr-6 cursor-pointer border-b-2 border-transparent py-3 text-[13px] font-semibold leading-[18px] text-text-tertiary', | 
					
						
							|  |  |  |               currentTab === 'TRACING' && '!border-[rgb(21,94,239)] text-text-secondary', | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |               !workflowRunningData && '!cursor-not-allowed opacity-30', | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             )} | 
					
						
							|  |  |  |             onClick={() => { | 
					
						
							|  |  |  |               if (!workflowRunningData) | 
					
						
							|  |  |  |                 return | 
					
						
							|  |  |  |               switchTab('TRACING') | 
					
						
							|  |  |  |             }} | 
					
						
							|  |  |  |           >{t('runLog.tracing')}</div> | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |         <div className={cn( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           'h-0 grow overflow-y-auto rounded-b-2xl bg-components-panel-bg', | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |           (currentTab === 'RESULT' || currentTab === 'TRACING') && '!bg-background-section-burn', | 
					
						
							|  |  |  |         )}> | 
					
						
							|  |  |  |           {currentTab === 'INPUT' && showInputsPanel && ( | 
					
						
							|  |  |  |             <InputsPanel onRun={() => switchTab('RESULT')} /> | 
					
						
							|  |  |  |           )} | 
					
						
							|  |  |  |           {currentTab === 'RESULT' && ( | 
					
						
							| 
									
										
										
										
											2024-05-11 16:59:17 +08:00
										 |  |  |             <> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |               <ResultText | 
					
						
							|  |  |  |                 isRunning={workflowRunningData?.result?.status === WorkflowRunningStatus.Running || !workflowRunningData?.result} | 
					
						
							|  |  |  |                 outputs={workflowRunningData?.resultText} | 
					
						
							| 
									
										
										
										
											2025-04-14 16:06:10 +08:00
										 |  |  |                 allFiles={workflowRunningData?.result?.files} | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                 error={workflowRunningData?.result?.error} | 
					
						
							|  |  |  |                 onClick={() => switchTab('DETAIL')} | 
					
						
							|  |  |  |               /> | 
					
						
							|  |  |  |               {(workflowRunningData?.result.status === WorkflowRunningStatus.Succeeded && workflowRunningData?.resultText && typeof workflowRunningData?.resultText === 'string') && ( | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |                 <Button | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                   className={cn('mb-4 ml-4 space-x-1')} | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |                   onClick={() => { | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                     const content = workflowRunningData?.resultText | 
					
						
							|  |  |  |                     if (typeof content === 'string') | 
					
						
							|  |  |  |                       copy(content) | 
					
						
							|  |  |  |                     else | 
					
						
							|  |  |  |                       copy(JSON.stringify(content)) | 
					
						
							|  |  |  |                     Toast.notify({ type: 'success', message: t('common.actionMsg.copySuccessfully') }) | 
					
						
							|  |  |  |                   }}> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                   <RiClipboardLine className='h-3.5 w-3.5' /> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                   <div>{t('common.operation.copy')}</div> | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |                 </Button> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |               )} | 
					
						
							| 
									
										
										
										
											2024-05-11 16:59:17 +08:00
										 |  |  |             </> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           )} | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |           {currentTab === 'DETAIL' && ( | 
					
						
							|  |  |  |             <ResultPanel | 
					
						
							|  |  |  |               inputs={workflowRunningData?.result?.inputs} | 
					
						
							|  |  |  |               outputs={workflowRunningData?.result?.outputs} | 
					
						
							|  |  |  |               status={workflowRunningData?.result?.status || ''} | 
					
						
							|  |  |  |               error={workflowRunningData?.result?.error} | 
					
						
							|  |  |  |               elapsed_time={workflowRunningData?.result?.elapsed_time} | 
					
						
							|  |  |  |               total_tokens={workflowRunningData?.result?.total_tokens} | 
					
						
							|  |  |  |               created_at={workflowRunningData?.result?.created_at} | 
					
						
							|  |  |  |               created_by={(workflowRunningData?.result?.created_by as any)?.name} | 
					
						
							|  |  |  |               steps={workflowRunningData?.result?.total_steps} | 
					
						
							|  |  |  |               exceptionCounts={workflowRunningData?.result?.exceptions_count} | 
					
						
							|  |  |  |             /> | 
					
						
							|  |  |  |           )} | 
					
						
							|  |  |  |           {currentTab === 'DETAIL' && !workflowRunningData?.result && ( | 
					
						
							|  |  |  |             <div className='flex h-full items-center justify-center bg-components-panel-bg'> | 
					
						
							|  |  |  |               <Loading /> | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |           )} | 
					
						
							|  |  |  |           {currentTab === 'TRACING' && ( | 
					
						
							|  |  |  |             <TracingPanel | 
					
						
							|  |  |  |               className='bg-background-section-burn' | 
					
						
							|  |  |  |               list={workflowRunningData?.tracing || []} | 
					
						
							|  |  |  |             /> | 
					
						
							|  |  |  |           )} | 
					
						
							|  |  |  |           {currentTab === 'TRACING' && !workflowRunningData?.tracing?.length && ( | 
					
						
							|  |  |  |             <div className='flex h-full items-center justify-center !bg-background-section-burn'> | 
					
						
							|  |  |  |               <Loading /> | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |           )} | 
					
						
							|  |  |  |         </div> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       </div> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default memo(WorkflowPreview) |