index.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 cn from 'classnames'
  7. import OutputPanel from './output-panel'
  8. import ResultPanel from './result-panel'
  9. import TracingPanel from './tracing-panel'
  10. import { ToastContext } from '@/app/components/base/toast'
  11. import Loading from '@/app/components/base/loading'
  12. import { fetchRunDetail, fetchTracingList } from '@/service/log'
  13. import type { NodeTracing } from '@/types/workflow'
  14. import type { WorkflowRunDetailResponse } from '@/models/log'
  15. import { useStore as useAppStore } from '@/app/components/app/store'
  16. export type RunProps = {
  17. hideResult?: boolean
  18. activeTab?: 'RESULT' | 'DETAIL' | 'TRACING'
  19. runID: string
  20. getResultCallback?: (result: WorkflowRunDetailResponse) => void
  21. }
  22. const RunPanel: FC<RunProps> = ({ hideResult, activeTab = 'RESULT', runID, getResultCallback }) => {
  23. const { t } = useTranslation()
  24. const { notify } = useContext(ToastContext)
  25. const [currentTab, setCurrentTab] = useState<string>(activeTab)
  26. const { appDetail } = useAppStore()
  27. const [loading, setLoading] = useState<boolean>(true)
  28. const [runDetail, setRunDetail] = useState<WorkflowRunDetailResponse>()
  29. const [list, setList] = useState<NodeTracing[]>([])
  30. const executor = useMemo(() => {
  31. if (runDetail?.created_by_role === 'account')
  32. return runDetail.created_by_account?.name || ''
  33. if (runDetail?.created_by_role === 'end_user')
  34. return runDetail.created_by_end_user?.session_id || ''
  35. return 'N/A'
  36. }, [runDetail])
  37. const getResult = useCallback(async (appID: string, runID: string) => {
  38. try {
  39. const res = await fetchRunDetail({
  40. appID,
  41. runID,
  42. })
  43. setRunDetail(res)
  44. if (getResultCallback)
  45. getResultCallback(res)
  46. }
  47. catch (err) {
  48. notify({
  49. type: 'error',
  50. message: `${err}`,
  51. })
  52. }
  53. }, [notify, getResultCallback])
  54. const getTracingList = useCallback(async (appID: string, runID: string) => {
  55. try {
  56. const { data: nodeList } = await fetchTracingList({
  57. url: `/apps/${appID}/workflow-runs/${runID}/node-executions`,
  58. })
  59. setList(nodeList.reverse())
  60. }
  61. catch (err) {
  62. notify({
  63. type: 'error',
  64. message: `${err}`,
  65. })
  66. }
  67. }, [notify])
  68. const getData = async (appID: string, runID: string) => {
  69. setLoading(true)
  70. await getResult(appID, runID)
  71. await getTracingList(appID, runID)
  72. setLoading(false)
  73. }
  74. const switchTab = async (tab: string) => {
  75. setCurrentTab(tab)
  76. if (tab === 'RESULT')
  77. appDetail?.id && await getResult(appDetail.id, runID)
  78. appDetail?.id && await getTracingList(appDetail.id, runID)
  79. }
  80. useEffect(() => {
  81. // fetch data
  82. if (appDetail && runID)
  83. getData(appDetail.id, runID)
  84. }, [appDetail, runID])
  85. const [height, setHieght] = useState(0)
  86. const ref = useRef<HTMLDivElement>(null)
  87. const adjustResultHeight = () => {
  88. if (ref.current)
  89. setHieght(ref.current?.clientHeight - 16 - 16 - 2 - 1)
  90. }
  91. useEffect(() => {
  92. adjustResultHeight()
  93. }, [loading])
  94. return (
  95. <div className='grow relative flex flex-col'>
  96. {/* tab */}
  97. <div className='shrink-0 flex items-center px-4 border-b-[0.5px] border-[rgba(0,0,0,0.05)]'>
  98. {!hideResult && (
  99. <div
  100. className={cn(
  101. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  102. currentTab === 'RESULT' && '!border-[rgb(21,94,239)] text-gray-700',
  103. )}
  104. onClick={() => switchTab('RESULT')}
  105. >{t('runLog.result')}</div>
  106. )}
  107. <div
  108. className={cn(
  109. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  110. currentTab === 'DETAIL' && '!border-[rgb(21,94,239)] text-gray-700',
  111. )}
  112. onClick={() => switchTab('DETAIL')}
  113. >{t('runLog.detail')}</div>
  114. <div
  115. className={cn(
  116. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  117. currentTab === 'TRACING' && '!border-[rgb(21,94,239)] text-gray-700',
  118. )}
  119. onClick={() => switchTab('TRACING')}
  120. >{t('runLog.tracing')}</div>
  121. </div>
  122. {/* panel detal */}
  123. <div ref={ref} className={cn('grow bg-white h-0 overflow-y-auto rounded-b-2xl', currentTab !== 'DETAIL' && '!bg-gray-50')}>
  124. {loading && (
  125. <div className='flex h-full items-center justify-center bg-white'>
  126. <Loading />
  127. </div>
  128. )}
  129. {!loading && currentTab === 'RESULT' && runDetail && (
  130. <OutputPanel
  131. outputs={runDetail.outputs}
  132. error={runDetail.error}
  133. height={height}
  134. />
  135. )}
  136. {!loading && currentTab === 'DETAIL' && runDetail && (
  137. <ResultPanel
  138. inputs={runDetail.inputs}
  139. outputs={runDetail.outputs}
  140. status={runDetail.status}
  141. error={runDetail.error}
  142. elapsed_time={runDetail.elapsed_time}
  143. total_tokens={runDetail.total_tokens}
  144. created_at={runDetail.created_at}
  145. created_by={executor}
  146. steps={runDetail.total_steps}
  147. />
  148. )}
  149. {!loading && currentTab === 'TRACING' && (
  150. <TracingPanel
  151. list={list}
  152. />
  153. )}
  154. </div>
  155. </div>
  156. )
  157. }
  158. export default RunPanel