Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

index.tsx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useMemo, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import { useRouter } from 'next/navigation'
  6. import { RiArrowLeftLine, RiLayoutLeft2Line, RiLayoutRight2Line } from '@remixicon/react'
  7. import Operations from '../operations'
  8. import StatusItem from '../status-item'
  9. import Completed from './completed'
  10. import Embedding from './embedding'
  11. import Metadata from '@/app/components/datasets/metadata/metadata-document'
  12. import SegmentAdd, { ProcessStatus } from './segment-add'
  13. import BatchModal from './batch-modal'
  14. import style from './style.module.css'
  15. import cn from '@/utils/classnames'
  16. import Divider from '@/app/components/base/divider'
  17. import Loading from '@/app/components/base/loading'
  18. import Toast from '@/app/components/base/toast'
  19. import { ChunkingMode } from '@/models/datasets'
  20. import type { FileItem } from '@/models/datasets'
  21. import { useDatasetDetailContextWithSelector } from '@/context/dataset-detail'
  22. import FloatRightContainer from '@/app/components/base/float-right-container'
  23. import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
  24. import { useCheckSegmentBatchImportProgress, useChildSegmentListKey, useSegmentBatchImport, useSegmentListKey } from '@/service/knowledge/use-segment'
  25. import { useDocumentDetail, useDocumentMetadata, useInvalidDocumentList } from '@/service/knowledge/use-document'
  26. import { useInvalid } from '@/service/use-base'
  27. import { DocumentContext } from './context'
  28. import { DocumentTitle } from './document-title'
  29. type DocumentDetailProps = {
  30. datasetId: string
  31. documentId: string
  32. }
  33. const DocumentDetail: FC<DocumentDetailProps> = ({ datasetId, documentId }) => {
  34. const router = useRouter()
  35. const { t } = useTranslation()
  36. const media = useBreakpoints()
  37. const isMobile = media === MediaType.mobile
  38. const dataset = useDatasetDetailContextWithSelector(s => s.dataset)
  39. const embeddingAvailable = !!dataset?.embedding_available
  40. const [showMetadata, setShowMetadata] = useState(!isMobile)
  41. const [newSegmentModalVisible, setNewSegmentModalVisible] = useState(false)
  42. const [batchModalVisible, setBatchModalVisible] = useState(false)
  43. const [importStatus, setImportStatus] = useState<ProcessStatus | string>()
  44. const showNewSegmentModal = () => setNewSegmentModalVisible(true)
  45. const showBatchModal = () => setBatchModalVisible(true)
  46. const hideBatchModal = () => setBatchModalVisible(false)
  47. const resetProcessStatus = () => setImportStatus('')
  48. const { mutateAsync: checkSegmentBatchImportProgress } = useCheckSegmentBatchImportProgress()
  49. const checkProcess = async (jobID: string) => {
  50. await checkSegmentBatchImportProgress({ jobID }, {
  51. onSuccess: (res) => {
  52. setImportStatus(res.job_status)
  53. if (res.job_status === ProcessStatus.WAITING || res.job_status === ProcessStatus.PROCESSING)
  54. setTimeout(() => checkProcess(res.job_id), 2500)
  55. if (res.job_status === ProcessStatus.ERROR)
  56. Toast.notify({ type: 'error', message: `${t('datasetDocuments.list.batchModal.runError')}` })
  57. },
  58. onError: (e) => {
  59. const message = 'message' in e ? `: ${e.message}` : ''
  60. Toast.notify({ type: 'error', message: `${t('datasetDocuments.list.batchModal.runError')}${message}` })
  61. },
  62. })
  63. }
  64. const { mutateAsync: segmentBatchImport } = useSegmentBatchImport()
  65. const runBatch = async (csv: FileItem) => {
  66. await segmentBatchImport({
  67. url: `/datasets/${datasetId}/documents/${documentId}/segments/batch_import`,
  68. body: { upload_file_id: csv.file.id! },
  69. }, {
  70. onSuccess: (res) => {
  71. setImportStatus(res.job_status)
  72. checkProcess(res.job_id)
  73. },
  74. onError: (e) => {
  75. const message = 'message' in e ? `: ${e.message}` : ''
  76. Toast.notify({ type: 'error', message: `${t('datasetDocuments.list.batchModal.runError')}${message}` })
  77. },
  78. })
  79. }
  80. const { data: documentDetail, error, refetch: detailMutate } = useDocumentDetail({
  81. datasetId,
  82. documentId,
  83. params: { metadata: 'without' },
  84. })
  85. const { data: documentMetadata } = useDocumentMetadata({
  86. datasetId,
  87. documentId,
  88. params: { metadata: 'only' },
  89. })
  90. const backToPrev = () => {
  91. // Preserve pagination and filter states when navigating back
  92. const searchParams = new URLSearchParams(window.location.search)
  93. const queryString = searchParams.toString()
  94. const separator = queryString ? '?' : ''
  95. const backPath = `/datasets/${datasetId}/documents${separator}${queryString}`
  96. router.push(backPath)
  97. }
  98. const isDetailLoading = !documentDetail && !error
  99. const embedding = ['queuing', 'indexing', 'paused'].includes((documentDetail?.display_status || '').toLowerCase())
  100. const invalidChunkList = useInvalid(useSegmentListKey)
  101. const invalidChildChunkList = useInvalid(useChildSegmentListKey)
  102. const invalidDocumentList = useInvalidDocumentList(datasetId)
  103. const handleOperate = (operateName?: string) => {
  104. invalidDocumentList()
  105. if (operateName === 'delete') {
  106. backToPrev()
  107. }
  108. else {
  109. detailMutate()
  110. // If operation is not rename, refresh the chunk list after 5 seconds
  111. if (operateName) {
  112. setTimeout(() => {
  113. invalidChunkList()
  114. invalidChildChunkList()
  115. }, 5000)
  116. }
  117. }
  118. }
  119. const parentMode = useMemo(() => {
  120. return documentDetail?.document_process_rule?.rules?.parent_mode || documentDetail?.dataset_process_rule?.rules?.parent_mode || 'paragraph'
  121. }, [documentDetail?.document_process_rule?.rules?.parent_mode, documentDetail?.dataset_process_rule?.rules?.parent_mode])
  122. const isFullDocMode = useMemo(() => {
  123. const chunkMode = documentDetail?.doc_form
  124. return chunkMode === ChunkingMode.parentChild && parentMode === 'full-doc'
  125. }, [documentDetail?.doc_form, parentMode])
  126. return (
  127. <DocumentContext.Provider value={{
  128. datasetId,
  129. documentId,
  130. docForm: documentDetail?.doc_form as ChunkingMode,
  131. parentMode,
  132. }}>
  133. <div className='flex h-full flex-col bg-background-default'>
  134. <div className='flex min-h-16 flex-wrap items-center justify-between border-b border-b-divider-subtle py-2.5 pl-3 pr-4'>
  135. <div onClick={backToPrev} className={'flex h-8 w-8 shrink-0 cursor-pointer items-center justify-center rounded-full hover:bg-components-button-tertiary-bg'}>
  136. <RiArrowLeftLine className='h-4 w-4 text-components-button-ghost-text hover:text-text-tertiary' />
  137. </div>
  138. <DocumentTitle
  139. datasetId={datasetId}
  140. extension={documentDetail?.data_source_info?.upload_file?.extension}
  141. name={documentDetail?.name}
  142. wrapperCls='mr-2'
  143. parent_mode={parentMode}
  144. chunkingMode={documentDetail?.doc_form as ChunkingMode}
  145. />
  146. <div className='flex flex-wrap items-center'>
  147. {embeddingAvailable && documentDetail && !documentDetail.archived && !isFullDocMode && (
  148. <>
  149. <SegmentAdd
  150. importStatus={importStatus}
  151. clearProcessStatus={resetProcessStatus}
  152. showNewSegmentModal={showNewSegmentModal}
  153. showBatchModal={showBatchModal}
  154. embedding={embedding}
  155. />
  156. <Divider type='vertical' className='!mx-3 !h-[14px] !bg-divider-regular' />
  157. </>
  158. )}
  159. <StatusItem
  160. status={documentDetail?.display_status || 'available'}
  161. scene='detail'
  162. errorMessage={documentDetail?.error || ''}
  163. textCls='font-semibold text-xs uppercase'
  164. detail={{
  165. enabled: documentDetail?.enabled || false,
  166. archived: documentDetail?.archived || false,
  167. id: documentId,
  168. }}
  169. datasetId={datasetId}
  170. onUpdate={handleOperate}
  171. />
  172. <Operations
  173. scene='detail'
  174. embeddingAvailable={embeddingAvailable}
  175. detail={{
  176. name: documentDetail?.name || '',
  177. enabled: documentDetail?.enabled || false,
  178. archived: documentDetail?.archived || false,
  179. id: documentId,
  180. data_source_type: documentDetail?.data_source_type || '',
  181. doc_form: documentDetail?.doc_form || '',
  182. }}
  183. datasetId={datasetId}
  184. onUpdate={handleOperate}
  185. className='!w-[200px]'
  186. />
  187. <button
  188. className={style.layoutRightIcon}
  189. onClick={() => setShowMetadata(!showMetadata)}
  190. >
  191. {
  192. showMetadata
  193. ? <RiLayoutLeft2Line className='h-4 w-4 text-components-button-secondary-text' />
  194. : <RiLayoutRight2Line className='h-4 w-4 text-components-button-secondary-text' />
  195. }
  196. </button>
  197. </div>
  198. </div>
  199. <div className='flex flex-1 flex-row' style={{ height: 'calc(100% - 4rem)' }}>
  200. {isDetailLoading
  201. ? <Loading type='app' />
  202. : <div className={cn('flex h-full min-w-0 grow flex-col',
  203. !embedding && isFullDocMode && 'relative pl-11 pr-11 pt-4',
  204. !embedding && !isFullDocMode && 'relative pl-5 pr-11 pt-3',
  205. )}>
  206. {embedding
  207. ? <Embedding
  208. detailUpdate={detailMutate}
  209. indexingType={dataset?.indexing_technique}
  210. retrievalMethod={dataset?.retrieval_model_dict?.search_method}
  211. />
  212. : <Completed
  213. embeddingAvailable={embeddingAvailable}
  214. showNewSegmentModal={newSegmentModalVisible}
  215. onNewSegmentModalChange={setNewSegmentModalVisible}
  216. importStatus={importStatus}
  217. archived={documentDetail?.archived}
  218. />
  219. }
  220. </div>
  221. }
  222. <FloatRightContainer showClose isOpen={showMetadata} onClose={() => setShowMetadata(false)} isMobile={isMobile} panelClassName='!justify-start' footer={null}>
  223. <Metadata
  224. className='mr-2 mt-3'
  225. datasetId={datasetId}
  226. documentId={documentId}
  227. docDetail={{ ...documentDetail, ...documentMetadata, doc_type: documentMetadata?.doc_type === 'others' ? '' : documentMetadata?.doc_type } as any}
  228. />
  229. </FloatRightContainer>
  230. </div>
  231. <BatchModal
  232. isShow={batchModalVisible}
  233. onCancel={hideBatchModal}
  234. onConfirm={runBatch}
  235. docForm={documentDetail?.doc_form as ChunkingMode}
  236. />
  237. </div>
  238. </DocumentContext.Provider>
  239. )
  240. }
  241. export default DocumentDetail