index.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
  4. import { useContext } from 'use-context-selector'
  5. import { useTranslation } from 'react-i18next'
  6. import { useBoolean } from 'ahooks'
  7. import { BlockEnum } from '../types'
  8. import OutputPanel from './output-panel'
  9. import ResultPanel from './result-panel'
  10. import TracingPanel from './tracing-panel'
  11. import IterationResultPanel from './iteration-result-panel'
  12. import cn from '@/utils/classnames'
  13. import { ToastContext } from '@/app/components/base/toast'
  14. import Loading from '@/app/components/base/loading'
  15. import { fetchRunDetail, fetchTracingList } from '@/service/log'
  16. import type { NodeTracing } from '@/types/workflow'
  17. import type { WorkflowRunDetailResponse } from '@/models/log'
  18. import { useStore as useAppStore } from '@/app/components/app/store'
  19. export type RunProps = {
  20. hideResult?: boolean
  21. activeTab?: 'RESULT' | 'DETAIL' | 'TRACING'
  22. runID: string
  23. getResultCallback?: (result: WorkflowRunDetailResponse) => void
  24. onShowIterationDetail: (detail: NodeTracing[][]) => void
  25. }
  26. const RunPanel: FC<RunProps> = ({ hideResult, activeTab = 'RESULT', runID, getResultCallback, onShowIterationDetail }) => {
  27. const { t } = useTranslation()
  28. const { notify } = useContext(ToastContext)
  29. const [currentTab, setCurrentTab] = useState<string>(activeTab)
  30. const appDetail = useAppStore(state => state.appDetail)
  31. const [loading, setLoading] = useState<boolean>(true)
  32. const [runDetail, setRunDetail] = useState<WorkflowRunDetailResponse>()
  33. const [list, setList] = useState<NodeTracing[]>([])
  34. const executor = useMemo(() => {
  35. if (runDetail?.created_by_role === 'account')
  36. return runDetail.created_by_account?.name || ''
  37. if (runDetail?.created_by_role === 'end_user')
  38. return runDetail.created_by_end_user?.session_id || ''
  39. return 'N/A'
  40. }, [runDetail])
  41. const getResult = useCallback(async (appID: string, runID: string) => {
  42. try {
  43. const res = await fetchRunDetail({
  44. appID,
  45. runID,
  46. })
  47. setRunDetail(res)
  48. if (getResultCallback)
  49. getResultCallback(res)
  50. }
  51. catch (err) {
  52. notify({
  53. type: 'error',
  54. message: `${err}`,
  55. })
  56. }
  57. }, [notify, getResultCallback])
  58. const formatNodeList = useCallback((list: NodeTracing[]) => {
  59. const allItems = list.reverse()
  60. const result: NodeTracing[] = []
  61. allItems.forEach((item) => {
  62. const { node_type, execution_metadata } = item
  63. if (node_type !== BlockEnum.Iteration) {
  64. const isInIteration = !!execution_metadata?.iteration_id
  65. if (isInIteration) {
  66. const iterationNode = result.find(node => node.node_id === execution_metadata?.iteration_id)
  67. const iterationDetails = iterationNode?.details
  68. const currentIterationIndex = execution_metadata?.iteration_index ?? 0
  69. if (Array.isArray(iterationDetails)) {
  70. if (iterationDetails.length === 0 || !iterationDetails[currentIterationIndex])
  71. iterationDetails[currentIterationIndex] = [item]
  72. else
  73. iterationDetails[currentIterationIndex].push(item)
  74. }
  75. return
  76. }
  77. // not in iteration
  78. result.push(item)
  79. return
  80. }
  81. result.push({
  82. ...item,
  83. details: [],
  84. })
  85. })
  86. return result
  87. }, [])
  88. const getTracingList = useCallback(async (appID: string, runID: string) => {
  89. try {
  90. const { data: nodeList } = await fetchTracingList({
  91. url: `/apps/${appID}/workflow-runs/${runID}/node-executions`,
  92. })
  93. setList(formatNodeList(nodeList))
  94. }
  95. catch (err) {
  96. notify({
  97. type: 'error',
  98. message: `${err}`,
  99. })
  100. }
  101. }, [notify])
  102. const getData = async (appID: string, runID: string) => {
  103. setLoading(true)
  104. await getResult(appID, runID)
  105. await getTracingList(appID, runID)
  106. setLoading(false)
  107. }
  108. const switchTab = async (tab: string) => {
  109. setCurrentTab(tab)
  110. if (tab === 'RESULT')
  111. appDetail?.id && await getResult(appDetail.id, runID)
  112. appDetail?.id && await getTracingList(appDetail.id, runID)
  113. }
  114. useEffect(() => {
  115. // fetch data
  116. if (appDetail && runID)
  117. getData(appDetail.id, runID)
  118. }, [appDetail, runID])
  119. const [height, setHeight] = useState(0)
  120. const ref = useRef<HTMLDivElement>(null)
  121. const adjustResultHeight = () => {
  122. if (ref.current)
  123. setHeight(ref.current?.clientHeight - 16 - 16 - 2 - 1)
  124. }
  125. useEffect(() => {
  126. adjustResultHeight()
  127. }, [loading])
  128. const [iterationRunResult, setIterationRunResult] = useState<NodeTracing[][]>([])
  129. const [isShowIterationDetail, {
  130. setTrue: doShowIterationDetail,
  131. setFalse: doHideIterationDetail,
  132. }] = useBoolean(false)
  133. const handleShowIterationDetail = useCallback((detail: NodeTracing[][]) => {
  134. setIterationRunResult(detail)
  135. doShowIterationDetail()
  136. }, [doShowIterationDetail])
  137. if (isShowIterationDetail) {
  138. return (
  139. <div className='grow relative flex flex-col'>
  140. <IterationResultPanel
  141. list={iterationRunResult}
  142. onHide={doHideIterationDetail}
  143. onBack={doHideIterationDetail}
  144. />
  145. </div>
  146. )
  147. }
  148. return (
  149. <div className='grow relative flex flex-col'>
  150. {/* tab */}
  151. <div className='shrink-0 flex items-center px-4 border-b-[0.5px] border-[rgba(0,0,0,0.05)]'>
  152. {!hideResult && (
  153. <div
  154. className={cn(
  155. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  156. currentTab === 'RESULT' && '!border-[rgb(21,94,239)] text-gray-700',
  157. )}
  158. onClick={() => switchTab('RESULT')}
  159. >{t('runLog.result')}</div>
  160. )}
  161. <div
  162. className={cn(
  163. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  164. currentTab === 'DETAIL' && '!border-[rgb(21,94,239)] text-gray-700',
  165. )}
  166. onClick={() => switchTab('DETAIL')}
  167. >{t('runLog.detail')}</div>
  168. <div
  169. className={cn(
  170. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  171. currentTab === 'TRACING' && '!border-[rgb(21,94,239)] text-gray-700',
  172. )}
  173. onClick={() => switchTab('TRACING')}
  174. >{t('runLog.tracing')}</div>
  175. </div>
  176. {/* panel detail */}
  177. <div ref={ref} className={cn('grow bg-white h-0 overflow-y-auto rounded-b-2xl', currentTab !== 'DETAIL' && '!bg-gray-50')}>
  178. {loading && (
  179. <div className='flex h-full items-center justify-center bg-white'>
  180. <Loading />
  181. </div>
  182. )}
  183. {!loading && currentTab === 'RESULT' && runDetail && (
  184. <OutputPanel
  185. outputs={runDetail.outputs}
  186. error={runDetail.error}
  187. height={height}
  188. />
  189. )}
  190. {!loading && currentTab === 'DETAIL' && runDetail && (
  191. <ResultPanel
  192. inputs={runDetail.inputs}
  193. outputs={runDetail.outputs}
  194. status={runDetail.status}
  195. error={runDetail.error}
  196. elapsed_time={runDetail.elapsed_time}
  197. total_tokens={runDetail.total_tokens}
  198. created_at={runDetail.created_at}
  199. created_by={executor}
  200. steps={runDetail.total_steps}
  201. />
  202. )}
  203. {!loading && currentTab === 'TRACING' && (
  204. <TracingPanel
  205. list={list}
  206. onShowIterationDetail={handleShowIterationDetail}
  207. />
  208. )}
  209. </div>
  210. </div>
  211. )
  212. }
  213. export default RunPanel