workflow-preview.tsx 9.5 KB

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