workflow-preview.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import {
  2. memo,
  3. useCallback,
  4. useEffect,
  5. // useRef,
  6. useState,
  7. } from 'react'
  8. import cn from 'classnames'
  9. import {
  10. RiClipboardLine,
  11. RiCloseLine,
  12. } from '@remixicon/react'
  13. import { useTranslation } from 'react-i18next'
  14. import copy from 'copy-to-clipboard'
  15. import { useBoolean } from 'ahooks'
  16. import ResultText from '../run/result-text'
  17. import ResultPanel from '../run/result-panel'
  18. import TracingPanel from '../run/tracing-panel'
  19. import {
  20. useWorkflowInteractions,
  21. } from '../hooks'
  22. import { useStore } from '../store'
  23. import {
  24. WorkflowRunningStatus,
  25. } from '../types'
  26. import { SimpleBtn } from '../../app/text-generate/item'
  27. import Toast from '../../base/toast'
  28. import IterationResultPanel from '../run/iteration-result-panel'
  29. import InputsPanel from './inputs-panel'
  30. import Loading from '@/app/components/base/loading'
  31. import type { NodeTracing } from '@/types/workflow'
  32. const WorkflowPreview = ({
  33. onShowIterationDetail,
  34. }: {
  35. onShowIterationDetail: (detail: NodeTracing[][]) => void
  36. }) => {
  37. const { t } = useTranslation()
  38. const { handleCancelDebugAndPreviewPanel } = useWorkflowInteractions()
  39. const workflowRunningData = useStore(s => s.workflowRunningData)
  40. const showInputsPanel = useStore(s => s.showInputsPanel)
  41. const showDebugAndPreviewPanel = useStore(s => s.showDebugAndPreviewPanel)
  42. const [currentTab, setCurrentTab] = useState<string>(showInputsPanel ? 'INPUT' : 'TRACING')
  43. const switchTab = async (tab: string) => {
  44. setCurrentTab(tab)
  45. }
  46. useEffect(() => {
  47. if (showDebugAndPreviewPanel && showInputsPanel)
  48. setCurrentTab('INPUT')
  49. }, [showDebugAndPreviewPanel, showInputsPanel])
  50. useEffect(() => {
  51. if ((workflowRunningData?.result.status === WorkflowRunningStatus.Succeeded || workflowRunningData?.result.status === WorkflowRunningStatus.Failed) && !workflowRunningData.resultText)
  52. switchTab('DETAIL')
  53. }, [workflowRunningData])
  54. const [iterationRunResult, setIterationRunResult] = useState<NodeTracing[][]>([])
  55. const [isShowIterationDetail, {
  56. setTrue: doShowIterationDetail,
  57. setFalse: doHideIterationDetail,
  58. }] = useBoolean(false)
  59. const handleShowIterationDetail = useCallback((detail: NodeTracing[][]) => {
  60. setIterationRunResult(detail)
  61. doShowIterationDetail()
  62. }, [doShowIterationDetail])
  63. if (isShowIterationDetail) {
  64. return (
  65. <div className={`
  66. flex flex-col w-[420px] h-full rounded-l-2xl border-[0.5px] border-gray-200 shadow-xl bg-white
  67. `}>
  68. <IterationResultPanel
  69. list={iterationRunResult}
  70. onHide={doHideIterationDetail}
  71. onBack={doHideIterationDetail}
  72. />
  73. </div>
  74. )
  75. }
  76. return (
  77. <div className={`
  78. flex flex-col w-[420px] h-full rounded-l-2xl border-[0.5px] border-gray-200 shadow-xl bg-white
  79. `}>
  80. <div className='flex items-center justify-between p-4 pb-1 text-base font-semibold text-gray-900'>
  81. {`Test Run${!workflowRunningData?.result.sequence_number ? '' : `#${workflowRunningData?.result.sequence_number}`}`}
  82. <div className='p-1 cursor-pointer' onClick={() => handleCancelDebugAndPreviewPanel()}>
  83. <RiCloseLine className='w-4 h-4 text-gray-500' />
  84. </div>
  85. </div>
  86. <div className='grow relative flex flex-col'>
  87. {isShowIterationDetail
  88. ? (
  89. <IterationResultPanel
  90. list={iterationRunResult}
  91. onHide={doHideIterationDetail}
  92. onBack={doHideIterationDetail}
  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-white h-0 overflow-y-auto rounded-b-2xl',
  146. (currentTab === 'RESULT' || currentTab === 'TRACING') && '!bg-gray-50',
  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. error={workflowRunningData?.result?.error}
  157. onClick={() => switchTab('DETAIL')}
  158. />
  159. {(workflowRunningData?.result.status === WorkflowRunningStatus.Succeeded && workflowRunningData?.resultText && typeof workflowRunningData?.resultText === 'string') && (
  160. <SimpleBtn
  161. className={cn('ml-4 mb-4 inline-flex space-x-1')}
  162. onClick={() => {
  163. const content = workflowRunningData?.resultText
  164. if (typeof content === 'string')
  165. copy(content)
  166. else
  167. copy(JSON.stringify(content))
  168. Toast.notify({ type: 'success', message: t('common.actionMsg.copySuccessfully') })
  169. }}>
  170. <RiClipboardLine className='w-3.5 h-3.5' />
  171. <div>{t('common.operation.copy')}</div>
  172. </SimpleBtn>
  173. )}
  174. </>
  175. )}
  176. {currentTab === 'DETAIL' && (
  177. <ResultPanel
  178. inputs={workflowRunningData?.result?.inputs}
  179. outputs={workflowRunningData?.result?.outputs}
  180. status={workflowRunningData?.result?.status || ''}
  181. error={workflowRunningData?.result?.error}
  182. elapsed_time={workflowRunningData?.result?.elapsed_time}
  183. total_tokens={workflowRunningData?.result?.total_tokens}
  184. created_at={workflowRunningData?.result?.created_at}
  185. created_by={(workflowRunningData?.result?.created_by as any)?.name}
  186. steps={workflowRunningData?.result?.total_steps}
  187. />
  188. )}
  189. {currentTab === 'DETAIL' && !workflowRunningData?.result && (
  190. <div className='flex h-full items-center justify-center bg-white'>
  191. <Loading />
  192. </div>
  193. )}
  194. {currentTab === 'TRACING' && (
  195. <TracingPanel
  196. list={workflowRunningData?.tracing || []}
  197. onShowIterationDetail={handleShowIterationDetail}
  198. />
  199. )}
  200. {currentTab === 'TRACING' && !workflowRunningData?.tracing?.length && (
  201. <div className='flex h-full items-center justify-center bg-gray-50'>
  202. <Loading />
  203. </div>
  204. )}
  205. </div>
  206. </>
  207. )}
  208. </div>
  209. </div>
  210. )
  211. }
  212. export default memo(WorkflowPreview)