workflow-preview.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import {
  2. memo,
  3. useCallback,
  4. useEffect,
  5. // useRef,
  6. useState,
  7. } from 'react'
  8. import {
  9. RiClipboardLine,
  10. RiCloseLine,
  11. } from '@remixicon/react'
  12. import { useTranslation } from 'react-i18next'
  13. import copy from 'copy-to-clipboard'
  14. import { useBoolean } from 'ahooks'
  15. import ResultText from '../run/result-text'
  16. import ResultPanel from '../run/result-panel'
  17. import TracingPanel from '../run/tracing-panel'
  18. import {
  19. useWorkflowInteractions,
  20. } from '../hooks'
  21. import { useStore } from '../store'
  22. import {
  23. WorkflowRunningStatus,
  24. } from '../types'
  25. import { SimpleBtn } from '../../app/text-generate/item'
  26. import Toast from '../../base/toast'
  27. import IterationResultPanel from '../run/iteration-result-panel'
  28. import RetryResultPanel from '../run/retry-result-panel'
  29. import InputsPanel from './inputs-panel'
  30. import cn from '@/utils/classnames'
  31. import Loading from '@/app/components/base/loading'
  32. import type { IterationDurationMap, NodeTracing } from '@/types/workflow'
  33. const WorkflowPreview = () => {
  34. const { t } = useTranslation()
  35. const { handleCancelDebugAndPreviewPanel } = useWorkflowInteractions()
  36. const workflowRunningData = useStore(s => s.workflowRunningData)
  37. const showInputsPanel = useStore(s => s.showInputsPanel)
  38. const showDebugAndPreviewPanel = useStore(s => s.showDebugAndPreviewPanel)
  39. const [currentTab, setCurrentTab] = useState<string>(showInputsPanel ? 'INPUT' : 'TRACING')
  40. const switchTab = async (tab: string) => {
  41. setCurrentTab(tab)
  42. }
  43. useEffect(() => {
  44. if (showDebugAndPreviewPanel && showInputsPanel)
  45. setCurrentTab('INPUT')
  46. }, [showDebugAndPreviewPanel, showInputsPanel])
  47. useEffect(() => {
  48. if ((workflowRunningData?.result.status === WorkflowRunningStatus.Succeeded || workflowRunningData?.result.status === WorkflowRunningStatus.Failed) && !workflowRunningData.resultText && !workflowRunningData.result.files?.length)
  49. switchTab('DETAIL')
  50. }, [workflowRunningData])
  51. const [iterationRunResult, setIterationRunResult] = useState<NodeTracing[][]>([])
  52. const [retryRunResult, setRetryRunResult] = useState<NodeTracing[]>([])
  53. const [iterDurationMap, setIterDurationMap] = useState<IterationDurationMap>({})
  54. const [isShowIterationDetail, {
  55. setTrue: doShowIterationDetail,
  56. setFalse: doHideIterationDetail,
  57. }] = useBoolean(false)
  58. const [isShowRetryDetail, {
  59. setTrue: doShowRetryDetail,
  60. setFalse: doHideRetryDetail,
  61. }] = useBoolean(false)
  62. const handleShowIterationDetail = useCallback((detail: NodeTracing[][], iterationDurationMap: IterationDurationMap) => {
  63. setIterDurationMap(iterationDurationMap)
  64. setIterationRunResult(detail)
  65. doShowIterationDetail()
  66. }, [doShowIterationDetail])
  67. const handleRetryDetail = useCallback((detail: NodeTracing[]) => {
  68. setRetryRunResult(detail)
  69. doShowRetryDetail()
  70. }, [doShowRetryDetail])
  71. if (isShowIterationDetail) {
  72. return (
  73. <div className={`
  74. flex flex-col w-[420px] h-full rounded-l-2xl border-[0.5px] border-gray-200 shadow-xl bg-white
  75. `}>
  76. <IterationResultPanel
  77. list={iterationRunResult}
  78. onHide={doHideIterationDetail}
  79. onBack={doHideIterationDetail}
  80. iterDurationMap={iterDurationMap}
  81. />
  82. </div>
  83. )
  84. }
  85. return (
  86. <div className={`
  87. flex flex-col w-[420px] h-full rounded-l-2xl border-[0.5px] border-gray-200 shadow-xl bg-white
  88. `}>
  89. <div className='flex items-center justify-between p-4 pb-1 text-base font-semibold text-gray-900'>
  90. {`Test Run${!workflowRunningData?.result.sequence_number ? '' : `#${workflowRunningData?.result.sequence_number}`}`}
  91. <div className='p-1 cursor-pointer' onClick={() => handleCancelDebugAndPreviewPanel()}>
  92. <RiCloseLine className='w-4 h-4 text-gray-500' />
  93. </div>
  94. </div>
  95. <div className='grow relative flex flex-col'>
  96. {isShowIterationDetail
  97. ? (
  98. <IterationResultPanel
  99. list={iterationRunResult}
  100. onHide={doHideIterationDetail}
  101. onBack={doHideIterationDetail}
  102. iterDurationMap={iterDurationMap}
  103. />
  104. )
  105. : (
  106. <>
  107. <div className='shrink-0 flex items-center px-4 border-b-[0.5px] border-[rgba(0,0,0,0.05)]'>
  108. {showInputsPanel && (
  109. <div
  110. className={cn(
  111. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  112. currentTab === 'INPUT' && '!border-[rgb(21,94,239)] text-gray-700',
  113. )}
  114. onClick={() => switchTab('INPUT')}
  115. >{t('runLog.input')}</div>
  116. )}
  117. <div
  118. className={cn(
  119. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  120. currentTab === 'RESULT' && '!border-[rgb(21,94,239)] text-gray-700',
  121. !workflowRunningData && 'opacity-30 !cursor-not-allowed',
  122. )}
  123. onClick={() => {
  124. if (!workflowRunningData)
  125. return
  126. switchTab('RESULT')
  127. }}
  128. >{t('runLog.result')}</div>
  129. <div
  130. className={cn(
  131. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  132. currentTab === 'DETAIL' && '!border-[rgb(21,94,239)] text-gray-700',
  133. !workflowRunningData && 'opacity-30 !cursor-not-allowed',
  134. )}
  135. onClick={() => {
  136. if (!workflowRunningData)
  137. return
  138. switchTab('DETAIL')
  139. }}
  140. >{t('runLog.detail')}</div>
  141. <div
  142. className={cn(
  143. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  144. currentTab === 'TRACING' && '!border-[rgb(21,94,239)] text-gray-700',
  145. !workflowRunningData && 'opacity-30 !cursor-not-allowed',
  146. )}
  147. onClick={() => {
  148. if (!workflowRunningData)
  149. return
  150. switchTab('TRACING')
  151. }}
  152. >{t('runLog.tracing')}</div>
  153. </div>
  154. <div className={cn(
  155. 'grow bg-components-panel-bg h-0 overflow-y-auto rounded-b-2xl',
  156. (currentTab === 'RESULT' || currentTab === 'TRACING') && '!bg-background-section-burn',
  157. )}>
  158. {currentTab === 'INPUT' && showInputsPanel && (
  159. <InputsPanel onRun={() => switchTab('RESULT')} />
  160. )}
  161. {currentTab === 'RESULT' && (
  162. <>
  163. <ResultText
  164. isRunning={workflowRunningData?.result?.status === WorkflowRunningStatus.Running || !workflowRunningData?.result}
  165. outputs={workflowRunningData?.resultText}
  166. allFiles={workflowRunningData?.result?.files as any}
  167. error={workflowRunningData?.result?.error}
  168. onClick={() => switchTab('DETAIL')}
  169. />
  170. {(workflowRunningData?.result.status === WorkflowRunningStatus.Succeeded && workflowRunningData?.resultText && typeof workflowRunningData?.resultText === 'string') && (
  171. <SimpleBtn
  172. className={cn('ml-4 mb-4 inline-flex space-x-1')}
  173. onClick={() => {
  174. const content = workflowRunningData?.resultText
  175. if (typeof content === 'string')
  176. copy(content)
  177. else
  178. copy(JSON.stringify(content))
  179. Toast.notify({ type: 'success', message: t('common.actionMsg.copySuccessfully') })
  180. }}>
  181. <RiClipboardLine className='w-3.5 h-3.5' />
  182. <div>{t('common.operation.copy')}</div>
  183. </SimpleBtn>
  184. )}
  185. </>
  186. )}
  187. {currentTab === 'DETAIL' && (
  188. <ResultPanel
  189. inputs={workflowRunningData?.result?.inputs}
  190. outputs={workflowRunningData?.result?.outputs}
  191. status={workflowRunningData?.result?.status || ''}
  192. error={workflowRunningData?.result?.error}
  193. elapsed_time={workflowRunningData?.result?.elapsed_time}
  194. total_tokens={workflowRunningData?.result?.total_tokens}
  195. created_at={workflowRunningData?.result?.created_at}
  196. created_by={(workflowRunningData?.result?.created_by as any)?.name}
  197. steps={workflowRunningData?.result?.total_steps}
  198. exceptionCounts={workflowRunningData?.result?.exceptions_count}
  199. />
  200. )}
  201. {currentTab === 'DETAIL' && !workflowRunningData?.result && (
  202. <div className='flex h-full items-center justify-center bg-components-panel-bg'>
  203. <Loading />
  204. </div>
  205. )}
  206. {currentTab === 'TRACING' && !isShowRetryDetail && (
  207. <TracingPanel
  208. className='bg-background-section-burn'
  209. list={workflowRunningData?.tracing || []}
  210. onShowIterationDetail={handleShowIterationDetail}
  211. onShowRetryDetail={handleRetryDetail}
  212. />
  213. )}
  214. {currentTab === 'TRACING' && !workflowRunningData?.tracing?.length && (
  215. <div className='flex h-full items-center justify-center !bg-background-section-burn'>
  216. <Loading />
  217. </div>
  218. )}
  219. {
  220. currentTab === 'TRACING' && isShowRetryDetail && (
  221. <RetryResultPanel
  222. list={retryRunResult}
  223. onBack={doHideRetryDetail}
  224. />
  225. )
  226. }
  227. </div>
  228. </>
  229. )}
  230. </div>
  231. </div>
  232. )
  233. }
  234. export default memo(WorkflowPreview)