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