index.tsx 9.1 KB

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