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.

7.0 KB
Raw Blame
'use client'

import type { GoldenQuestionDraft, GoldenQuestionEvidenceOption } from '../quality/types'
import type { RetrievalEvidence } from './model'
import type { BadCaseReason, QualityDecision } from './results'
import { useQueryClient } from '@tanstack/react-query'
import { useAtomValueRawSync } from 'jotai'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { toast } from '@/app/notifications'
import { consoleClient, consoleQuery } from '@/service/console'
import { getHttpErrorStatus } from '@/service/http-error'
import { getErrorMessage } from '@/utils/error-message'
import { GoldenQuestionDialog } from '../quality/golden-question-dialog'
import { goldenQuestionQueriesKey } from '../quality/quality-model'
import { newKnowledgeQualityBadCasesPath, newKnowledgeQualityPath } from '../routes'
import { useKnowledgeSpacePermission } from '../space/context'
import { QualityActions } from './results'
import { retrievalResultFactsAtom, retrievalRuntimeQueryFactsAtom } from './state/graph'
import { retrievalKnowledgeSpaceIdAtom } from './state/inputs'

type GoldenQuestionPromotion = {
  evidenceOptions: GoldenQuestionEvidenceOption[]
  resultKey: string
  value: GoldenQuestionDraft
}

function goldenQuestionEvidenceOptions(
  evidence: readonly RetrievalEvidence[],
): GoldenQuestionEvidenceOption[] {
  const options = new Map<string, GoldenQuestionEvidenceOption>()
  for (const item of evidence) {
    if (!item.chunkId) continue
    const sectionPath = item.documentName
      ? item.title === item.documentName
        ? [item.documentName]
        : [item.documentName, item.title]
      : [item.title]
    options.set(item.chunkId, {
      node_id: item.chunkId,
      ...(item.score === undefined ? {} : { score: item.score }),
      section_path: sectionPath,
      text: item.text,
    })
  }
  return [...options.values()]
}

export function RetrievalQualityWorkflow() {
  const { t } = useTranslation(['dataset'])
  const queryClient = useQueryClient()
  const canEditQuality = useKnowledgeSpacePermission('knowledge_space_edit')
  const knowledgeSpaceId = useAtomValueRawSync(retrievalKnowledgeSpaceIdAtom)
  const {
    currentEvidence,
    resultKey,
    selectedDataError,
    selectedFailed,
    selectedIsLoading,
    selectedOpenBadCaseId,
    selectedQuery,
    selectedResearchActive,
    selectedTraceId,
  } = useAtomValueRawSync(retrievalResultFactsAtom)
  const { refetchTraces } = useAtomValueRawSync(retrievalRuntimeQueryFactsAtom)
  const [qualityDecisions, setQualityDecisions] = useState<Record<string, QualityDecision>>({})
  const [qualityPendingKey, setQualityPendingKey] = useState<string>()
  const [goldenPromotion, setGoldenPromotion] = useState<GoldenQuestionPromotion>()
  const [goldenPromotionError, setGoldenPromotionError] = useState<string>()

  const startGoldenPromotion = () => {
    if (!resultKey || !selectedQuery) return
    setGoldenPromotionError(undefined)
    setGoldenPromotion({
      evidenceOptions: goldenQuestionEvidenceOptions(currentEvidence),
      resultKey,
      value: {
        annotation: '',
        expectedEvidenceIds: [],
        matchPolicy: 'all',
        question: selectedQuery,
        tags: ['retrieval-test'],
      },
    })
  }

  const saveBadCase = async (reason: BadCaseReason) => {
    // A bad case is keyed by its trace; image-only traces have no query text but are still valid.
    if (!resultKey || !selectedTraceId) return
    setQualityPendingKey(resultKey)
    try {
      try {
        await consoleClient.knowledgeFs.spaces.byControlSpaceId.quality.badCases.post(
          {
            body: {
              reason,
              tags: ['retrieval-test'],
              trace_id: selectedTraceId,
            },
            params: { control_space_id: knowledgeSpaceId },
          },
          { context: { silent: true } },
        )
      } catch (error) {
        // 409: the trace already has an unresolved bad case, so the record is in the wanted state.
        if (getHttpErrorStatus(error) !== 409) throw error
      }
      void queryClient.invalidateQueries({
        queryKey: consoleQuery.knowledgeFs.spaces.byControlSpaceId.quality.badCases.get.key({
          input: { params: { control_space_id: knowledgeSpaceId } },
          type: 'infinite',
        }),
      })
      setQualityDecisions((current) => ({ ...current, [resultKey]: 'bad-case' }))
      void refetchTraces()
    } catch (error) {
      toast.error(getErrorMessage(error) ?? t(($) => $.unknownError, { ns: 'dataset' }))
    } finally {
      setQualityPendingKey(undefined)
    }
  }

  const submitGoldenPromotion = async (draft: GoldenQuestionDraft) => {
    if (!goldenPromotion) return
    const promotion = goldenPromotion
    setGoldenPromotionError(undefined)
    setQualityPendingKey(promotion.resultKey)
    try {
      await consoleClient.knowledgeFs.spaces.byControlSpaceId.goldenQuestions.post({
        body: {
          annotation: draft.annotation,
          expected_evidence_ids: draft.expectedEvidenceIds,
          match_policy: draft.matchPolicy,
          question: draft.question,
          tags: draft.tags,
        },
        params: { control_space_id: knowledgeSpaceId },
      })
      await queryClient.invalidateQueries({
        queryKey: goldenQuestionQueriesKey(knowledgeSpaceId),
      })
      setQualityDecisions((current) => ({ ...current, [promotion.resultKey]: 'golden' }))
      setGoldenPromotion(undefined)
    } catch {
      setGoldenPromotionError(t(($) => $.unknownError, { ns: 'dataset' }))
    } finally {
      setQualityPendingKey(undefined)
    }
  }

  const actionsVisible = Boolean(
    canEditQuality &&
    !selectedIsLoading &&
    !selectedFailed &&
    !selectedDataError &&
    !selectedResearchActive &&
    resultKey,
  )
  const qualityDecision = resultKey
    ? (qualityDecisions[resultKey] ?? (selectedOpenBadCaseId ? 'bad-case' : undefined))
    : undefined

  return (
    <>
      {actionsVisible && resultKey && (
        <QualityActions
          badCaseAvailable={Boolean(selectedTraceId)}
          noResults={currentEvidence.length === 0}
          decision={qualityDecision}
          onBadCase={saveBadCase}
          onGolden={startGoldenPromotion}
          pending={qualityPendingKey === resultKey}
          qualityHref={
            qualityDecision === 'bad-case'
              ? newKnowledgeQualityBadCasesPath(knowledgeSpaceId)
              : newKnowledgeQualityPath(knowledgeSpaceId)
          }
        />
      )}
      {canEditQuality && goldenPromotion && (
        <GoldenQuestionDialog
          key={goldenPromotion.resultKey}
          evidenceOptions={goldenPromotion.evidenceOptions}
          error={goldenPromotionError}
          initialValue={goldenPromotion.value}
          knowledgeSpaceId={knowledgeSpaceId}
          mode="promote"
          open
          pending={qualityPendingKey === goldenPromotion.resultKey}
          onOpenChange={(open) => {
            if (!open) {
              setGoldenPromotion(undefined)
              setGoldenPromotionError(undefined)
            }
          }}
          onSubmit={submitGoldenPromotion}
        />
      )}
    </>
  )
}