| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 'use client' | 
					
						
							|  |  |  | import type { FC } from 'react' | 
					
						
							|  |  |  | import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react' | 
					
						
							|  |  |  | import { useContext } from 'use-context-selector' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							|  |  |  | import OutputPanel from './output-panel' | 
					
						
							|  |  |  | import ResultPanel from './result-panel' | 
					
						
							|  |  |  | import TracingPanel from './tracing-panel' | 
					
						
							| 
									
										
										
										
											2024-07-09 15:05:40 +08:00
										 |  |  | import cn from '@/utils/classnames' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { ToastContext } from '@/app/components/base/toast' | 
					
						
							|  |  |  | import Loading from '@/app/components/base/loading' | 
					
						
							|  |  |  | import { fetchRunDetail, fetchTracingList } from '@/service/log' | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | import type { NodeTracing } from '@/types/workflow' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import type { WorkflowRunDetailResponse } from '@/models/log' | 
					
						
							|  |  |  | import { useStore as useAppStore } from '@/app/components/app/store' | 
					
						
							|  |  |  | export type RunProps = { | 
					
						
							|  |  |  |   hideResult?: boolean | 
					
						
							|  |  |  |   activeTab?: 'RESULT' | 'DETAIL' | 'TRACING' | 
					
						
							|  |  |  |   runID: string | 
					
						
							|  |  |  |   getResultCallback?: (result: WorkflowRunDetailResponse) => void | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  | const RunPanel: FC<RunProps> = ({ hideResult, activeTab = 'RESULT', runID, getResultCallback }) => { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const { notify } = useContext(ToastContext) | 
					
						
							|  |  |  |   const [currentTab, setCurrentTab] = useState<string>(activeTab) | 
					
						
							| 
									
										
										
										
											2024-04-24 04:07:28 +00:00
										 |  |  |   const appDetail = useAppStore(state => state.appDetail) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const [loading, setLoading] = useState<boolean>(true) | 
					
						
							|  |  |  |   const [runDetail, setRunDetail] = useState<WorkflowRunDetailResponse>() | 
					
						
							|  |  |  |   const [list, setList] = useState<NodeTracing[]>([]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const executor = useMemo(() => { | 
					
						
							|  |  |  |     if (runDetail?.created_by_role === 'account') | 
					
						
							|  |  |  |       return runDetail.created_by_account?.name || '' | 
					
						
							|  |  |  |     if (runDetail?.created_by_role === 'end_user') | 
					
						
							|  |  |  |       return runDetail.created_by_end_user?.session_id || '' | 
					
						
							|  |  |  |     return 'N/A' | 
					
						
							|  |  |  |   }, [runDetail]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const getResult = useCallback(async (appID: string, runID: string) => { | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       const res = await fetchRunDetail({ | 
					
						
							|  |  |  |         appID, | 
					
						
							|  |  |  |         runID, | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       setRunDetail(res) | 
					
						
							|  |  |  |       if (getResultCallback) | 
					
						
							|  |  |  |         getResultCallback(res) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     catch (err) { | 
					
						
							|  |  |  |       notify({ | 
					
						
							|  |  |  |         type: 'error', | 
					
						
							|  |  |  |         message: `${err}`, | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, [notify, getResultCallback]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const getTracingList = useCallback(async (appID: string, runID: string) => { | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       const { data: nodeList } = await fetchTracingList({ | 
					
						
							|  |  |  |         url: `/apps/${appID}/workflow-runs/${runID}/node-executions`, | 
					
						
							|  |  |  |       }) | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |       setList(nodeList) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     catch (err) { | 
					
						
							|  |  |  |       notify({ | 
					
						
							|  |  |  |         type: 'error', | 
					
						
							|  |  |  |         message: `${err}`, | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, [notify]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const getData = async (appID: string, runID: string) => { | 
					
						
							|  |  |  |     setLoading(true) | 
					
						
							|  |  |  |     await getResult(appID, runID) | 
					
						
							|  |  |  |     await getTracingList(appID, runID) | 
					
						
							|  |  |  |     setLoading(false) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const switchTab = async (tab: string) => { | 
					
						
							|  |  |  |     setCurrentTab(tab) | 
					
						
							|  |  |  |     if (tab === 'RESULT') | 
					
						
							|  |  |  |       appDetail?.id && await getResult(appDetail.id, runID) | 
					
						
							|  |  |  |     appDetail?.id && await getTracingList(appDetail.id, runID) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     // fetch data
 | 
					
						
							|  |  |  |     if (appDetail && runID) | 
					
						
							|  |  |  |       getData(appDetail.id, runID) | 
					
						
							|  |  |  |   }, [appDetail, runID]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-08 12:14:11 +07:00
										 |  |  |   const [height, setHeight] = useState(0) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const ref = useRef<HTMLDivElement>(null) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const adjustResultHeight = () => { | 
					
						
							|  |  |  |     if (ref.current) | 
					
						
							| 
									
										
										
										
											2024-09-08 12:14:11 +07:00
										 |  |  |       setHeight(ref.current?.clientHeight - 16 - 16 - 2 - 1) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     adjustResultHeight() | 
					
						
							|  |  |  |   }, [loading]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |     <div className='relative flex grow flex-col'> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       {/* tab */} | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |       <div className='flex shrink-0 items-center border-b-[0.5px] border-divider-subtle px-4'> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         {!hideResult && ( | 
					
						
							|  |  |  |           <div | 
					
						
							|  |  |  |             className={cn( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |               'system-sm-semibold-uppercase mr-6 cursor-pointer border-b-2 border-transparent py-3 text-text-tertiary', | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |               currentTab === 'RESULT' && '!border-util-colors-blue-brand-blue-brand-600 text-text-primary', | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             )} | 
					
						
							|  |  |  |             onClick={() => switchTab('RESULT')} | 
					
						
							|  |  |  |           >{t('runLog.result')}</div> | 
					
						
							|  |  |  |         )} | 
					
						
							|  |  |  |         <div | 
					
						
							|  |  |  |           className={cn( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |             'system-sm-semibold-uppercase mr-6 cursor-pointer border-b-2 border-transparent py-3 text-text-tertiary', | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |             currentTab === 'DETAIL' && '!border-util-colors-blue-brand-blue-brand-600 text-text-primary', | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           )} | 
					
						
							|  |  |  |           onClick={() => switchTab('DETAIL')} | 
					
						
							|  |  |  |         >{t('runLog.detail')}</div> | 
					
						
							|  |  |  |         <div | 
					
						
							|  |  |  |           className={cn( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |             'system-sm-semibold-uppercase mr-6 cursor-pointer border-b-2 border-transparent py-3 text-text-tertiary', | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |             currentTab === 'TRACING' && '!border-util-colors-blue-brand-blue-brand-600 text-text-primary', | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           )} | 
					
						
							|  |  |  |           onClick={() => switchTab('TRACING')} | 
					
						
							|  |  |  |         >{t('runLog.tracing')}</div> | 
					
						
							|  |  |  |       </div> | 
					
						
							| 
									
										
										
										
											2024-09-08 12:14:11 +07:00
										 |  |  |       {/* panel detail */} | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |       <div ref={ref} className={cn('relative h-0 grow overflow-y-auto rounded-b-2xl bg-components-panel-bg')}> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         {loading && ( | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |           <div className='flex h-full items-center justify-center bg-components-panel-bg'> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             <Loading /> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         )} | 
					
						
							|  |  |  |         {!loading && currentTab === 'RESULT' && runDetail && ( | 
					
						
							|  |  |  |           <OutputPanel | 
					
						
							|  |  |  |             outputs={runDetail.outputs} | 
					
						
							|  |  |  |             error={runDetail.error} | 
					
						
							|  |  |  |             height={height} | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |         )} | 
					
						
							|  |  |  |         {!loading && currentTab === 'DETAIL' && runDetail && ( | 
					
						
							|  |  |  |           <ResultPanel | 
					
						
							|  |  |  |             inputs={runDetail.inputs} | 
					
						
							|  |  |  |             outputs={runDetail.outputs} | 
					
						
							|  |  |  |             status={runDetail.status} | 
					
						
							|  |  |  |             error={runDetail.error} | 
					
						
							|  |  |  |             elapsed_time={runDetail.elapsed_time} | 
					
						
							|  |  |  |             total_tokens={runDetail.total_tokens} | 
					
						
							|  |  |  |             created_at={runDetail.created_at} | 
					
						
							|  |  |  |             created_by={executor} | 
					
						
							|  |  |  |             steps={runDetail.total_steps} | 
					
						
							| 
									
										
										
										
											2024-12-11 14:21:38 +08:00
										 |  |  |             exceptionCounts={runDetail.exceptions_count} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           /> | 
					
						
							|  |  |  |         )} | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         {!loading && currentTab === 'TRACING' && ( | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           <TracingPanel | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |             className='bg-background-section-burn' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             list={list} | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |         )} | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default RunPanel |