Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

12.3 KB
Raw Blame
'use client'

import type {
  DatasetQueryContentResponse,
  DatasetQueryDetailResponse,
  ExternalHitTestingRecordResponse,
  ExternalHitTestingResponse,
  HitTestingRecord,
  HitTestingResponse,
} from '@dify/contracts/api/console/datasets/types.gen'
import type { FC } from 'react'
import type { ExternalKnowledgeBaseHitTestingRequest, HitTestingRequest } from '@/models/datasets'
import type { RetrievalConfig } from '@/types/app'
import { cn } from '@langgenius/dify-ui/cn'
import {
  Drawer,
  DrawerBackdrop,
  DrawerContent,
  DrawerPopup,
  DrawerPortal,
  DrawerViewport,
} from '@langgenius/dify-ui/drawer'
import { Pagination } from '@langgenius/dify-ui/pagination'
import { useMediaQuery } from '@langgenius/dify-ui/use-media-query'
import { keepPreviousData, useMutation, useQuery, useSuspenseQuery } from '@tanstack/react-query'
import { useBoolean } from 'ahooks'
import { useAtomValue } from 'jotai'
import * as React from 'react'
import { useCallback, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useContext } from 'use-context-selector'
import FloatRightContainer from '@/app/components/base/float-right-container'
import { LoadingPlaceholder } from '@/app/components/base/loading-placeholder'
import { toRetrievalConfig } from '@/app/components/datasets/dataset-fields'
import docStyle from '@/app/components/datasets/documents/detail/completed/style.module.css'
import DatasetDetailContext from '@/context/dataset-detail'
import { workspacePermissionKeysAtom } from '@/context/permission-state'
import { userProfileQueryOptions } from '@/features/account-profile/client'
import { consoleClient, consoleQuery } from '@/service/console'
import { getDatasetACLCapabilities } from '@/utils/permission'
import { CardSkelton } from '../documents/detail/completed/skeleton/general-list-skeleton'
import EmptyRecords from './components/empty-records'
import QueryInput from './components/query-input'
import Records from './components/records'
import { ResultItem } from './components/result-item'
import { ResultItemExternal } from './components/result-item-external'
import ModifyRetrievalModal from './modify-retrieval-modal'

const limit = 10

type Props = Readonly<{
  datasetId: string
}>

const HitTestingPage: FC<Props> = ({ datasetId }: Props) => {
  const { t } = useTranslation(['common', 'datasetHitTesting'])

  const isBelowSm = useMediaQuery('(width < 40rem)', {})

  const [hitResult, setHitResult] = useState<HitTestingResponse | undefined>()
  const [externalHitResult, setExternalHitResult] = useState<
    ExternalHitTestingResponse | undefined
  >()
  const [queries, setQueries] = useState<DatasetQueryContentResponse[]>([])
  const [queryInputKey, setQueryInputKey] = useState(Date.now())

  const [currPage, setCurrPage] = useState<number>(0)
  const { dataset: currentDataset } = useContext(DatasetDetailContext)
  const { data: currentUserId } = useSuspenseQuery({
    ...userProfileQueryOptions(),
    select: (data) => data.profile.id,
  })
  const workspacePermissionKeys = useAtomValue(workspacePermissionKeysAtom)
  const canRunRetrievalRecall = getDatasetACLCapabilities(currentDataset?.permission_keys, {
    currentUserId,
    resourceMaintainer: currentDataset?.maintainer,
    workspacePermissionKeys,
  }).canRetrievalRecall
  const {
    data: recordsRes,
    refetch: recordsRefetch,
    isLoading: isRecordsLoading,
  } = useQuery(
    consoleQuery.datasets.byDatasetId.queries.get.queryOptions({
      input: { params: { dataset_id: datasetId }, query: { limit, page: currPage + 1 } },
      enabled: !!datasetId && canRunRetrievalRecall,
      placeholderData: keepPreviousData,
    }),
  )

  const total = recordsRes?.total || 0
  const totalPages = total ? Math.max(Math.ceil(total / limit), 1) : 1

  const isExternal = currentDataset?.provider === 'external'

  const [retrievalConfig, setRetrievalConfig] = useState<RetrievalConfig | undefined>(
    () => currentDataset && toRetrievalConfig(currentDataset.retrieval_model_dict),
  )
  const [isShowModifyRetrievalModal, setIsShowModifyRetrievalModal] = useState(false)
  const [isResultDrawerOpen, { setTrue: openResultDrawer, setFalse: closeResultDrawer }] =
    useBoolean(false)

  const {
    mutate: hitTestingMutation,
    isPending: isHitTestingPending,
    isError: isHitTestingError,
  } = useMutation({
    mutationKey: ['hit-testing', datasetId],
    mutationFn: (body: HitTestingRequest) =>
      consoleClient.datasets.byDatasetId.hitTesting.post({
        params: { dataset_id: datasetId },
        body,
      }),
  })
  const {
    mutate: externalKnowledgeBaseHitTestingMutation,
    isPending: isExternalKnowledgeBaseHitTestingPending,
    isError: isExternalKnowledgeBaseHitTestingError,
  } = useMutation({
    mutationKey: ['external-hit-testing', datasetId],
    mutationFn: (body: ExternalKnowledgeBaseHitTestingRequest) =>
      consoleClient.datasets.byDatasetId.externalHitTesting.post({
        params: { dataset_id: datasetId },
        body,
      }),
  })

  const isRetrievalLoading = isHitTestingPending || isExternalKnowledgeBaseHitTestingPending
  const result = isExternal ? externalHitResult : hitResult
  const isRetrievalError = isExternal ? isExternalKnowledgeBaseHitTestingError : isHitTestingError
  const resultTitle = t(($) => $['hit.title'], {
    ns: 'datasetHitTesting',
    num: result?.records.length ?? 0,
  })
  const retrievalErrorMessage = t(($) => $['api.actionFailed'], { ns: 'common' })
  const panelTitle =
    !isRetrievalLoading && !isRetrievalError && result
      ? resultTitle
      : t(($) => $.title, { ns: 'datasetHitTesting' })
  const retrievalStatus = isRetrievalLoading
    ? t(($) => $.loading, { ns: 'common' })
    : isRetrievalError
      ? retrievalErrorMessage
      : result
        ? resultTitle
        : ''

  const renderHitResults = (results: HitTestingRecord[] | ExternalHitTestingRecordResponse[]) => (
    <div className="flex h-full flex-col rounded-ss-2xl bg-background-body px-4 py-3">
      <h2 className="mb-2 shrink-0 ps-2 leading-6 font-semibold text-text-primary">
        {t(($) => $['hit.title'], { ns: 'datasetHitTesting', num: results.length })}
      </h2>
      <div className="grow space-y-2 overflow-y-auto">
        {results.map((record, idx) =>
          isExternal ? (
            <ResultItemExternal
              key={idx}
              positionId={idx + 1}
              payload={record as ExternalHitTestingRecordResponse}
            />
          ) : (
            <ResultItem key={idx} payload={record as HitTestingRecord} />
          ),
        )}
      </div>
    </div>
  )

  const renderEmptyState = () => (
    <div className="flex h-full flex-col items-center justify-center rounded-ss-2xl bg-background-body px-4 py-3">
      <div
        className={cn(docStyle.commonIcon, docStyle.targetIcon, 'size-14! bg-text-quaternary!')}
      />
      <div className="mt-3 text-[13px] text-text-quaternary">
        {t(($) => $['hit.emptyTip'], { ns: 'datasetHitTesting' })}
      </div>
    </div>
  )

  const handleClickRecord = useCallback((record: DatasetQueryDetailResponse) => {
    setQueries(record.queries)
    setQueryInputKey(Date.now())
  }, [])

  if (!canRunRetrievalRecall) return <LoadingPlaceholder className="h-full" />

  return (
    <div className="relative flex size-full gap-x-6 overflow-y-auto ps-6">
      <div role="status" aria-atomic="true" className="sr-only">
        {retrievalStatus}
      </div>
      <div className="flex min-w-0 flex-1 flex-col py-3">
        <div className="mb-4 flex flex-col justify-center">
          <h1 className="text-base font-semibold text-text-primary">
            {t(($) => $.title, { ns: 'datasetHitTesting' })}
          </h1>
          <p className="mt-0.5 text-[13px] leading-4 font-normal text-text-tertiary">
            {t(($) => $.desc, { ns: 'datasetHitTesting' })}
          </p>
        </div>
        {retrievalConfig && (
          <QueryInput
            key={queryInputKey}
            setHitResult={setHitResult}
            setExternalHitResult={setExternalHitResult}
            onSubmit={() => {
              if (isBelowSm) openResultDrawer()
            }}
            onUpdateList={recordsRefetch}
            loading={isRetrievalLoading}
            queries={queries}
            setQueries={setQueries}
            isExternal={isExternal}
            onClickRetrievalMethod={() => setIsShowModifyRetrievalModal(true)}
            retrievalConfig={retrievalConfig}
            isEconomy={currentDataset?.indexing_technique === 'economy'}
            hitTestingMutation={hitTestingMutation}
            externalKnowledgeBaseHitTestingMutation={externalKnowledgeBaseHitTestingMutation}
            canRunRetrievalRecall={canRunRetrievalRecall}
          />
        )}
        <h2 className="mt-6 mb-3 text-base font-semibold text-text-primary">
          {t(($) => $.records, { ns: 'datasetHitTesting' })}
        </h2>
        {isRecordsLoading && (
          <div className="flex-1">
            <LoadingPlaceholder className="h-full" />
          </div>
        )}
        {!isRecordsLoading && recordsRes?.data && recordsRes.data.length > 0 && (
          <>
            <Records records={recordsRes?.data} onClickRecord={handleClickRecord} />
            {total && total > limit ? (
              <Pagination
                page={currPage + 1}
                totalPages={totalPages}
                onPageChange={(page) => setCurrPage(page - 1)}
                labels={{
                  previous: t(($) => $['pagination.previous'], { ns: 'common' }),
                  next: t(($) => $['pagination.next'], { ns: 'common' }),
                  editPageNumber: (page, totalPages) =>
                    t(($) => $['pagination.editPageNumber'], { ns: 'common', page, totalPages }),
                  pageNumberInput: t(($) => $['pagination.pageNumber'], { ns: 'common' }),
                }}
              />
            ) : null}
          </>
        )}
        {!isRecordsLoading && !recordsRes?.data?.length && <EmptyRecords />}
      </div>
      <FloatRightContainer
        panelClassName="justify-start! overflow-y-auto!"
        showClose
        presentation={isBelowSm ? 'drawer' : 'inline'}
        isOpen={!isBelowSm || isResultDrawerOpen}
        onClose={closeResultDrawer}
        title={panelTitle}
      >
        <div className="flex min-w-0 flex-1 flex-col pt-3">
          {isRetrievalLoading ? (
            <div className="flex h-full flex-col rounded-ss-2xl bg-background-body px-4 py-3">
              <CardSkelton />
            </div>
          ) : isRetrievalError ? (
            <div className="flex h-full items-center justify-center rounded-ss-2xl bg-background-body px-4 py-3 text-sm text-text-secondary">
              {retrievalErrorMessage}
            </div>
          ) : (
            (() => {
              if (!hitResult?.records.length && !externalHitResult?.records.length)
                return renderEmptyState()

              if (hitResult?.records.length) return renderHitResults(hitResult.records)

              return renderHitResults(externalHitResult?.records || [])
            })()
          )}
        </div>
      </FloatRightContainer>
      <Drawer
        open={isShowModifyRetrievalModal}
        modal
        swipeDirection="right"
        onOpenChange={(open) => {
          if (!open) setIsShowModifyRetrievalModal(false)
        }}
      >
        <DrawerPortal>
          <DrawerBackdrop className="sm:bg-transparent" />
          <DrawerViewport>
            <DrawerPopup className="p-0! data-[swipe-direction=right]:top-16 data-[swipe-direction=right]:right-2 data-[swipe-direction=right]:bottom-3 data-[swipe-direction=right]:h-auto data-[swipe-direction=right]:w-full data-[swipe-direction=right]:max-w-160 data-[swipe-direction=right]:rounded-xl">
              <DrawerContent>
                {retrievalConfig && (
                  <ModifyRetrievalModal
                    indexMethod={currentDataset?.indexing_technique || ''}
                    value={retrievalConfig}
                    isShow={isShowModifyRetrievalModal}
                    onHide={() => setIsShowModifyRetrievalModal(false)}
                    onSave={(value) => {
                      setRetrievalConfig(value)
                      setIsShowModifyRetrievalModal(false)
                    }}
                  />
                )}
              </DrawerContent>
            </DrawerPopup>
          </DrawerViewport>
        </DrawerPortal>
      </Drawer>
    </div>
  )
}

export default HitTestingPage