index.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. let iterationIndex = 0
  62. allItems.forEach((item) => {
  63. const { node_type, execution_metadata } = item
  64. if (node_type !== BlockEnum.Iteration) {
  65. const isInIteration = !!execution_metadata?.iteration_id
  66. if (isInIteration) {
  67. const iterationDetails = result[result.length - 1].details!
  68. const currentIterationIndex = execution_metadata?.iteration_index
  69. const isIterationFirstNode = iterationIndex !== currentIterationIndex || iterationDetails.length === 0
  70. if (isIterationFirstNode) {
  71. iterationDetails!.push([item])
  72. iterationIndex = currentIterationIndex!
  73. }
  74. else {
  75. iterationDetails[iterationDetails.length - 1].push(item)
  76. }
  77. return
  78. }
  79. // not in iteration
  80. result.push(item)
  81. return
  82. }
  83. result.push({
  84. ...item,
  85. details: [],
  86. })
  87. })
  88. return result
  89. }, [])
  90. const getTracingList = useCallback(async (appID: string, runID: string) => {
  91. try {
  92. const { data: nodeList } = await fetchTracingList({
  93. url: `/apps/${appID}/workflow-runs/${runID}/node-executions`,
  94. })
  95. setList(formatNodeList(nodeList))
  96. }
  97. catch (err) {
  98. notify({
  99. type: 'error',
  100. message: `${err}`,
  101. })
  102. }
  103. }, [notify])
  104. const getData = async (appID: string, runID: string) => {
  105. setLoading(true)
  106. await getResult(appID, runID)
  107. await getTracingList(appID, runID)
  108. setLoading(false)
  109. }
  110. const switchTab = async (tab: string) => {
  111. setCurrentTab(tab)
  112. if (tab === 'RESULT')
  113. appDetail?.id && await getResult(appDetail.id, runID)
  114. appDetail?.id && await getTracingList(appDetail.id, runID)
  115. }
  116. useEffect(() => {
  117. // fetch data
  118. if (appDetail && runID)
  119. getData(appDetail.id, runID)
  120. }, [appDetail, runID])
  121. const [height, setHieght] = useState(0)
  122. const ref = useRef<HTMLDivElement>(null)
  123. const adjustResultHeight = () => {
  124. if (ref.current)
  125. setHieght(ref.current?.clientHeight - 16 - 16 - 2 - 1)
  126. }
  127. useEffect(() => {
  128. adjustResultHeight()
  129. }, [loading])
  130. const [iterationRunResult, setIterationRunResult] = useState<NodeTracing[][]>([])
  131. const [isShowIterationDetail, {
  132. setTrue: doShowIterationDetail,
  133. setFalse: doHideIterationDetail,
  134. }] = useBoolean(false)
  135. const handleShowIterationDetail = useCallback((detail: NodeTracing[][]) => {
  136. setIterationRunResult(detail)
  137. doShowIterationDetail()
  138. }, [doShowIterationDetail])
  139. if (isShowIterationDetail) {
  140. return (
  141. <div className='grow relative flex flex-col'>
  142. <IterationResultPanel
  143. list={iterationRunResult}
  144. onHide={doHideIterationDetail}
  145. onBack={doHideIterationDetail}
  146. />
  147. </div>
  148. )
  149. }
  150. return (
  151. <div className='grow relative flex flex-col'>
  152. {/* tab */}
  153. <div className='shrink-0 flex items-center px-4 border-b-[0.5px] border-[rgba(0,0,0,0.05)]'>
  154. {!hideResult && (
  155. <div
  156. className={cn(
  157. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  158. currentTab === 'RESULT' && '!border-[rgb(21,94,239)] text-gray-700',
  159. )}
  160. onClick={() => switchTab('RESULT')}
  161. >{t('runLog.result')}</div>
  162. )}
  163. <div
  164. className={cn(
  165. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  166. currentTab === 'DETAIL' && '!border-[rgb(21,94,239)] text-gray-700',
  167. )}
  168. onClick={() => switchTab('DETAIL')}
  169. >{t('runLog.detail')}</div>
  170. <div
  171. className={cn(
  172. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  173. currentTab === 'TRACING' && '!border-[rgb(21,94,239)] text-gray-700',
  174. )}
  175. onClick={() => switchTab('TRACING')}
  176. >{t('runLog.tracing')}</div>
  177. </div>
  178. {/* panel detal */}
  179. <div ref={ref} className={cn('grow bg-white h-0 overflow-y-auto rounded-b-2xl', currentTab !== 'DETAIL' && '!bg-gray-50')}>
  180. {loading && (
  181. <div className='flex h-full items-center justify-center bg-white'>
  182. <Loading />
  183. </div>
  184. )}
  185. {!loading && currentTab === 'RESULT' && runDetail && (
  186. <OutputPanel
  187. outputs={runDetail.outputs}
  188. error={runDetail.error}
  189. height={height}
  190. />
  191. )}
  192. {!loading && currentTab === 'DETAIL' && runDetail && (
  193. <ResultPanel
  194. inputs={runDetail.inputs}
  195. outputs={runDetail.outputs}
  196. status={runDetail.status}
  197. error={runDetail.error}
  198. elapsed_time={runDetail.elapsed_time}
  199. total_tokens={runDetail.total_tokens}
  200. created_at={runDetail.created_at}
  201. created_by={executor}
  202. steps={runDetail.total_steps}
  203. />
  204. )}
  205. {!loading && currentTab === 'TRACING' && (
  206. <TracingPanel
  207. list={list}
  208. onShowIterationDetail={handleShowIterationDetail}
  209. />
  210. )}
  211. </div>
  212. </div>
  213. )
  214. }
  215. export default RunPanel