index.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import useSWR from 'swr'
  6. import { omit } from 'lodash-es'
  7. import { useBoolean } from 'ahooks'
  8. import { useContext } from 'use-context-selector'
  9. import { RiApps2Line, RiFocus2Line } from '@remixicon/react'
  10. import Textarea from './textarea'
  11. import s from './style.module.css'
  12. import ModifyRetrievalModal from './modify-retrieval-modal'
  13. import ResultItem from './components/result-item'
  14. import ResultItemExternal from './components/result-item-external'
  15. import cn from '@/utils/classnames'
  16. import type { ExternalKnowledgeBaseHitTesting, ExternalKnowledgeBaseHitTestingResponse, HitTesting, HitTestingResponse } from '@/models/datasets'
  17. import Loading from '@/app/components/base/loading'
  18. import Drawer from '@/app/components/base/drawer'
  19. import Pagination from '@/app/components/base/pagination'
  20. import FloatRightContainer from '@/app/components/base/float-right-container'
  21. import { fetchTestingRecords } from '@/service/datasets'
  22. import DatasetDetailContext from '@/context/dataset-detail'
  23. import type { RetrievalConfig } from '@/types/app'
  24. import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
  25. import useTimestamp from '@/hooks/use-timestamp'
  26. import docStyle from '@/app/components/datasets/documents/detail/completed/style.module.css'
  27. import { CardSkelton } from '../documents/detail/completed/skeleton/general-list-skeleton'
  28. const limit = 10
  29. type Props = {
  30. datasetId: string
  31. }
  32. const RecordsEmpty: FC = () => {
  33. const { t } = useTranslation()
  34. return <div className='bg-gray-50 rounded-2xl p-5'>
  35. <div className={s.clockWrapper}>
  36. <div className={cn(s.clockIcon, 'w-5 h-5')}></div>
  37. </div>
  38. <div className='my-2 text-gray-500 text-sm'>{t('datasetHitTesting.noRecentTip')}</div>
  39. </div>
  40. }
  41. const HitTestingPage: FC<Props> = ({ datasetId }: Props) => {
  42. const { t } = useTranslation()
  43. const { formatTime } = useTimestamp()
  44. const media = useBreakpoints()
  45. const isMobile = media === MediaType.mobile
  46. const [hitResult, setHitResult] = useState<HitTestingResponse | undefined>() // 初始化记录为空数组
  47. const [externalHitResult, setExternalHitResult] = useState<ExternalKnowledgeBaseHitTestingResponse | undefined>()
  48. const [submitLoading, setSubmitLoading] = useState(false)
  49. const [text, setText] = useState('')
  50. const [currPage, setCurrPage] = React.useState<number>(0)
  51. const { data: recordsRes, error, mutate: recordsMutate } = useSWR({
  52. action: 'fetchTestingRecords',
  53. datasetId,
  54. params: { limit, page: currPage + 1 },
  55. }, apiParams => fetchTestingRecords(omit(apiParams, 'action')))
  56. const total = recordsRes?.total || 0
  57. const { dataset: currentDataset } = useContext(DatasetDetailContext)
  58. const isExternal = currentDataset?.provider === 'external'
  59. const [retrievalConfig, setRetrievalConfig] = useState(currentDataset?.retrieval_model_dict as RetrievalConfig)
  60. const [isShowModifyRetrievalModal, setIsShowModifyRetrievalModal] = useState(false)
  61. const [isShowRightPanel, { setTrue: showRightPanel, setFalse: hideRightPanel, set: setShowRightPanel }] = useBoolean(!isMobile)
  62. const renderHitResults = (results: HitTesting[] | ExternalKnowledgeBaseHitTesting[]) => (
  63. <div className='h-full flex flex-col py-3 px-4 rounded-t-2xl bg-background-body'>
  64. <div className='shrink-0 pl-2 text-text-primary font-semibold leading-6 mb-2'>
  65. {t('datasetHitTesting.hit.title', { num: results.length })}
  66. </div>
  67. <div className='grow overflow-y-auto space-y-2'>
  68. {results.map((record, idx) =>
  69. isExternal
  70. ? (
  71. <ResultItemExternal
  72. key={idx}
  73. positionId={idx + 1}
  74. payload={record as ExternalKnowledgeBaseHitTesting}
  75. />
  76. )
  77. : (
  78. <ResultItem key={idx} payload={record as HitTesting} />
  79. ),
  80. )}
  81. </div>
  82. </div>
  83. )
  84. const renderEmptyState = () => (
  85. <div className='h-full flex flex-col justify-center items-center py-3 px-4 rounded-t-2xl bg-background-body'>
  86. <div className={cn(docStyle.commonIcon, docStyle.targetIcon, '!bg-text-quaternary !h-14 !w-14')} />
  87. <div className='text-text-quaternary text-[13px] mt-3'>
  88. {t('datasetHitTesting.hit.emptyTip')}
  89. </div>
  90. </div>
  91. )
  92. useEffect(() => {
  93. setShowRightPanel(!isMobile)
  94. }, [isMobile, setShowRightPanel])
  95. return (
  96. <div className={s.container}>
  97. <div className='px-6 py-3 flex flex-col'>
  98. <div className='flex flex-col justify-center mb-4'>
  99. <h1 className='text-base font-semibold text-text-primary'>{t('datasetHitTesting.title')}</h1>
  100. <p className='mt-0.5 text-[13px] leading-4 font-normal text-text-tertiary'>{t('datasetHitTesting.desc')}</p>
  101. </div>
  102. <Textarea
  103. datasetId={datasetId}
  104. setHitResult={setHitResult}
  105. setExternalHitResult={setExternalHitResult}
  106. onSubmit={showRightPanel}
  107. onUpdateList={recordsMutate}
  108. loading={submitLoading}
  109. setLoading={setSubmitLoading}
  110. setText={setText}
  111. text={text}
  112. isExternal={isExternal}
  113. onClickRetrievalMethod={() => setIsShowModifyRetrievalModal(true)}
  114. retrievalConfig={retrievalConfig}
  115. isEconomy={currentDataset?.indexing_technique === 'economy'}
  116. />
  117. <div className='text-base font-semibold text-text-primary mt-6 mb-3'>{t('datasetHitTesting.records')}</div>
  118. {(!recordsRes && !error)
  119. ? (
  120. <div className='flex-1'><Loading type='app' /></div>
  121. )
  122. : recordsRes?.data?.length
  123. ? (
  124. <>
  125. <div className='grow overflow-y-auto'>
  126. <table className={'w-full border-collapse border-0 text-[13px] leading-4 text-text-secondary '}>
  127. <thead className='sticky top-0 h-7 leading-7 text-xs text-text-tertiary font-medium uppercase'>
  128. <tr>
  129. <td className='pl-3 w-[128px] rounded-l-lg bg-background-section-burn'>{t('datasetHitTesting.table.header.source')}</td>
  130. <td className='bg-background-section-burn'>{t('datasetHitTesting.table.header.text')}</td>
  131. <td className='pl-2 w-48 rounded-r-lg bg-background-section-burn'>{t('datasetHitTesting.table.header.time')}</td>
  132. </tr>
  133. </thead>
  134. <tbody>
  135. {recordsRes?.data?.map((record) => {
  136. const SourceIcon = record.source === 'app' ? RiApps2Line : RiFocus2Line
  137. return <tr
  138. key={record.id}
  139. className='group border-b border-divider-subtle h-10 hover:bg-background-default-hover cursor-pointer'
  140. onClick={() => setText(record.content)}
  141. >
  142. <td className='pl-3 w-[128px]'>
  143. <div className='flex items-center'>
  144. <SourceIcon className='mr-1 size-4 text-text-tertiary' />
  145. <span className='capitalize'>{record.source.replace('_', ' ').replace('hit testing', 'retrieval test')}</span>
  146. </div>
  147. </td>
  148. <td className='max-w-xs py-2'>{record.content}</td>
  149. <td className='pl-2 w-36'>
  150. {formatTime(record.created_at, t('datasetHitTesting.dateTimeFormat') as string)}
  151. </td>
  152. </tr>
  153. })}
  154. </tbody>
  155. </table>
  156. </div>
  157. {(total && total > limit)
  158. ? <Pagination current={currPage} onChange={setCurrPage} total={total} limit={limit} />
  159. : null}
  160. </>
  161. )
  162. : (
  163. <RecordsEmpty />
  164. )}
  165. </div>
  166. <FloatRightContainer panelClassname='!justify-start !overflow-y-auto' showClose isMobile={isMobile} isOpen={isShowRightPanel} onClose={hideRightPanel} footer={null}>
  167. <div className='flex flex-col pt-3'>
  168. {/* {renderHitResults(generalResultData)} */}
  169. {submitLoading
  170. ? <div className='h-full flex flex-col py-3 px-4 rounded-t-2xl bg-background-body'>
  171. <CardSkelton />
  172. </div>
  173. : (
  174. (() => {
  175. if (!hitResult?.records.length && !externalHitResult?.records.length)
  176. return renderEmptyState()
  177. if (hitResult?.records.length)
  178. return renderHitResults(hitResult.records)
  179. return renderHitResults(externalHitResult?.records || [])
  180. })()
  181. )
  182. }
  183. </div>
  184. </FloatRightContainer>
  185. <Drawer unmount={true} isOpen={isShowModifyRetrievalModal} onClose={() => setIsShowModifyRetrievalModal(false)} footer={null} mask={isMobile} panelClassname='mt-16 mx-2 sm:mr-2 mb-3 !p-0 !max-w-[640px] rounded-xl'>
  186. <ModifyRetrievalModal
  187. indexMethod={currentDataset?.indexing_technique || ''}
  188. value={retrievalConfig}
  189. isShow={isShowModifyRetrievalModal}
  190. onHide={() => setIsShowModifyRetrievalModal(false)}
  191. onSave={(value) => {
  192. setRetrievalConfig(value)
  193. setIsShowModifyRetrievalModal(false)
  194. }}
  195. />
  196. </Drawer>
  197. </div>
  198. )
  199. }
  200. export default HitTestingPage