| 
									
										
										
										
											2024-04-17 10:30:52 +08:00
										 |  |  | 'use client' | 
					
						
							|  |  |  | import type { FC } from 'react' | 
					
						
							|  |  |  | import React, { useCallback, useEffect, useMemo, useState } from 'react' | 
					
						
							|  |  |  | import { useContext } from 'use-context-selector' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							|  |  |  | import { flatten, uniq } from 'lodash-es' | 
					
						
							|  |  |  | import ResultPanel from './result' | 
					
						
							|  |  |  | import TracingPanel from './tracing' | 
					
						
							| 
									
										
										
										
											2024-07-09 15:05:40 +08:00
										 |  |  | import cn from '@/utils/classnames' | 
					
						
							| 
									
										
										
										
											2024-04-17 10:30:52 +08:00
										 |  |  | import { ToastContext } from '@/app/components/base/toast' | 
					
						
							|  |  |  | import Loading from '@/app/components/base/loading' | 
					
						
							|  |  |  | import { fetchAgentLogDetail } from '@/service/log' | 
					
						
							|  |  |  | import type { AgentIteration, AgentLogDetailResponse } from '@/models/log' | 
					
						
							|  |  |  | import { useStore as useAppStore } from '@/app/components/app/store' | 
					
						
							| 
									
										
										
										
											2024-06-24 12:29:14 +08:00
										 |  |  | import type { IChatItem } from '@/app/components/base/chat/chat/type' | 
					
						
							| 
									
										
										
										
											2024-04-17 10:30:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export type AgentLogDetailProps = { | 
					
						
							|  |  |  |   activeTab?: 'DETAIL' | 'TRACING' | 
					
						
							|  |  |  |   conversationID: string | 
					
						
							|  |  |  |   log: IChatItem | 
					
						
							|  |  |  |   messageID: string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const AgentLogDetail: FC<AgentLogDetailProps> = ({ | 
					
						
							|  |  |  |   activeTab = 'DETAIL', | 
					
						
							|  |  |  |   conversationID, | 
					
						
							|  |  |  |   messageID, | 
					
						
							|  |  |  |   log, | 
					
						
							|  |  |  | }) => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const { notify } = useContext(ToastContext) | 
					
						
							|  |  |  |   const [currentTab, setCurrentTab] = useState<string>(activeTab) | 
					
						
							| 
									
										
										
										
											2024-04-24 13:07:20 +08:00
										 |  |  |   const appDetail = useAppStore(s => s.appDetail) | 
					
						
							| 
									
										
										
										
											2024-04-17 10:30:52 +08:00
										 |  |  |   const [loading, setLoading] = useState<boolean>(true) | 
					
						
							|  |  |  |   const [runDetail, setRunDetail] = useState<AgentLogDetailResponse>() | 
					
						
							|  |  |  |   const [list, setList] = useState<AgentIteration[]>([]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const tools = useMemo(() => { | 
					
						
							| 
									
										
										
										
											2025-04-14 16:06:10 +08:00
										 |  |  |     const res = uniq(flatten(runDetail?.iterations.map((iteration) => { | 
					
						
							| 
									
										
										
										
											2024-04-17 10:30:52 +08:00
										 |  |  |       return iteration.tool_calls.map((tool: any) => tool.tool_name).filter(Boolean) | 
					
						
							|  |  |  |     })).filter(Boolean)) | 
					
						
							|  |  |  |     return res | 
					
						
							|  |  |  |   }, [runDetail]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const getLogDetail = useCallback(async (appID: string, conversationID: string, messageID: string) => { | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       const res = await fetchAgentLogDetail({ | 
					
						
							|  |  |  |         appID, | 
					
						
							|  |  |  |         params: { | 
					
						
							|  |  |  |           conversation_id: conversationID, | 
					
						
							|  |  |  |           message_id: messageID, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       setRunDetail(res) | 
					
						
							|  |  |  |       setList(res.iterations) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     catch (err) { | 
					
						
							|  |  |  |       notify({ | 
					
						
							|  |  |  |         type: 'error', | 
					
						
							|  |  |  |         message: `${err}`, | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, [notify]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const getData = async (appID: string, conversationID: string, messageID: string) => { | 
					
						
							|  |  |  |     setLoading(true) | 
					
						
							|  |  |  |     await getLogDetail(appID, conversationID, messageID) | 
					
						
							|  |  |  |     setLoading(false) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const switchTab = async (tab: string) => { | 
					
						
							|  |  |  |     setCurrentTab(tab) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     // fetch data
 | 
					
						
							|  |  |  |     if (appDetail) | 
					
						
							|  |  |  |       getData(appDetail.id, conversationID, messageID) | 
					
						
							|  |  |  |   }, [appDetail, conversationID, messageID]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |     <div className='relative flex grow flex-col'> | 
					
						
							| 
									
										
										
										
											2024-04-17 10:30:52 +08:00
										 |  |  |       {/* tab */} | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |       <div className='flex shrink-0 items-center border-b-[0.5px] border-divider-regular px-4'> | 
					
						
							| 
									
										
										
										
											2024-04-17 10:30:52 +08:00
										 |  |  |         <div | 
					
						
							|  |  |  |           className={cn( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |             'mr-6 cursor-pointer border-b-2 border-transparent py-3 text-[13px] font-semibold leading-[18px] text-text-tertiary', | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             currentTab === 'DETAIL' && '!border-[rgb(21,94,239)] text-text-secondary', | 
					
						
							| 
									
										
										
										
											2024-04-17 10:30:52 +08:00
										 |  |  |           )} | 
					
						
							|  |  |  |           onClick={() => switchTab('DETAIL')} | 
					
						
							|  |  |  |         >{t('runLog.detail')}</div> | 
					
						
							|  |  |  |         <div | 
					
						
							|  |  |  |           className={cn( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |             'mr-6 cursor-pointer border-b-2 border-transparent py-3 text-[13px] font-semibold leading-[18px] text-text-tertiary', | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             currentTab === 'TRACING' && '!border-[rgb(21,94,239)] text-text-secondary', | 
					
						
							| 
									
										
										
										
											2024-04-17 10:30:52 +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 className={cn('h-0 grow overflow-y-auto rounded-b-2xl bg-components-panel-bg', currentTab !== 'DETAIL' && '!bg-background-section')}> | 
					
						
							| 
									
										
										
										
											2024-04-17 10:30:52 +08:00
										 |  |  |         {loading && ( | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |           <div className='flex h-full items-center justify-center bg-components-panel-bg'> | 
					
						
							| 
									
										
										
										
											2024-04-17 10:30:52 +08:00
										 |  |  |             <Loading /> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         )} | 
					
						
							|  |  |  |         {!loading && currentTab === 'DETAIL' && runDetail && ( | 
					
						
							|  |  |  |           <ResultPanel | 
					
						
							|  |  |  |             inputs={log.input} | 
					
						
							|  |  |  |             outputs={log.content} | 
					
						
							|  |  |  |             status={runDetail.meta.status} | 
					
						
							|  |  |  |             error={runDetail.meta.error} | 
					
						
							|  |  |  |             elapsed_time={runDetail.meta.elapsed_time} | 
					
						
							|  |  |  |             total_tokens={runDetail.meta.total_tokens} | 
					
						
							|  |  |  |             created_at={runDetail.meta.start_time} | 
					
						
							|  |  |  |             created_by={runDetail.meta.executor} | 
					
						
							|  |  |  |             agentMode={runDetail.meta.agent_mode} | 
					
						
							|  |  |  |             tools={tools} | 
					
						
							|  |  |  |             iterations={runDetail.iterations.length} | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |         )} | 
					
						
							|  |  |  |         {!loading && currentTab === 'TRACING' && ( | 
					
						
							|  |  |  |           <TracingPanel | 
					
						
							|  |  |  |             list={list} | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |         )} | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default AgentLogDetail |