"use client"; import { useEffect, useRef } from "react"; import type { UseFormReturn } from "react-hook-form"; import { Check, ChevronDown, HelpCircle, Info } from "lucide-react"; import { Button } from "@/components/ui/button"; import { useSubspaceOptional } from "@/contexts/subspace-context"; import { FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Collapsible, CollapsibleContent, CollapsibleTrigger, } from "@/components/ui/collapsible"; import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "@/components/ui/tooltip"; import { TagInput } from "@/components/shared/tag-input"; import { RetrievalResultCountField } from "@/components/knowledge-base/retrieval-result-count-field"; import { FilterBuilder, type FilterBuilderHandle } from "./filter-builder"; import { RetrievalRerankFields } from "./retrieval-rerank-fields"; import { useI18n } from "@/i18n/i18n-context"; import type { RetrievalInput } from "@/schemas/retrieval"; import type { KnowledgeBase } from "@/types/knowledge-base"; /** * Shared retrieval parameter fields used by both the retrieval-test form * and the QA playground form. Keeping them in a single component avoids * drift between the two call sites — the retrieval configuration shipped * from QA must exactly mirror what the retrieval-test panel produces so * users can debug one using the other. * * Owner wiring: * - The parent form owns `useForm` (including `defaultValues` * derived from the knowledge base). * - The parent passes a ref to the embedded `FilterBuilder` so it can * extract the metadata filter when assembling a request body. * * This component renders only the parameter fields; the query input and * submit buttons are specific to each caller and stay outside. */ export interface RetrievalParamsFieldsProps { form: UseFormReturn; knowledgeBase?: KnowledgeBase; filterRef: React.RefObject; /** Optional label override for the "number of results" field. */ topKLabel?: string; /** Optional tooltip rendered behind a small info icon next to the topK * label. Used for verbose explanations that would otherwise wrap the * label across multiple lines and break left/right column alignment. */ topKHint?: string; topKMode?: "split" | "single"; } export function RetrievalParamsFields({ form, knowledgeBase, filterRef, topKLabel, topKHint, topKMode = "split", }: RetrievalParamsFieldsProps) { const { t } = useI18n(); const resolvedTopKLabel = topKLabel ?? t("retrieval.params.resultCount"); const currentSearchType = form.watch("searchType"); const denseEnabled = !!currentSearchType?.includes("DENSE_VECTOR"); const fullTextEnabled = !!currentSearchType?.includes("FULL_TEXT"); const hybrid = denseEnabled && fullTextEnabled; const metadataFields = knowledgeBase?.metadata ?? []; // Pre-fill the subspace field with the active scope, but only once per // distinct activeSubspace value so that an intentional user clear sticks. const activeSubspace = useSubspaceOptional()?.subspace ?? null; const prefilledForRef = useRef(null); useEffect(() => { if (!knowledgeBase?.subspace || !activeSubspace) return; if (prefilledForRef.current === activeSubspace) return; const current = (form.getValues("subspace") as string[] | undefined) ?? []; if (current.length === 0) { form.setValue("subspace", [activeSubspace], { shouldDirty: false }); } prefilledForRef.current = activeSubspace; }, [activeSubspace, knowledgeBase?.subspace, form]); return ( <> {/* Subspace: only meaningful for KBs that opted into subspace isolation. */} {knowledgeBase?.subspace && ( ( {t("retrieval.params.subspace")}

{t("retrieval.params.subspaceHint")}

)} /> )}
{topKMode === "single" ? ( ) : null} ( {t("retrieval.params.searchType")} { if (v?.length) field.onChange(v); }} className="grid w-full grid-cols-2 gap-0 overflow-hidden rounded-lg border border-border bg-[hsl(var(--muted)/0.4)] p-0" > {field.value?.includes("DENSE_VECTOR") && ( )} {t("retrieval.params.dense")} {field.value?.includes("FULL_TEXT") && ( )} {t("retrieval.params.fullText")} )} />
{topKMode === "split" ? (

{t("retrieval.params.resultCount")}

{t("retrieval.params.resultCountDesc")}

) : null}
( {t("retrieval.params.rerankStrategy")} { if (v) field.onChange(v); }} className="grid w-full grid-cols-3 gap-0 overflow-hidden rounded-lg border border-border bg-[hsl(var(--muted)/0.4)] p-0" > Model Weight RRF {!hybrid ? (
{t("retrieval.params.needHybridPrefix")}{" "} {t("retrieval.params.needHybridHighlight")} {" "} {t("retrieval.params.needHybridSuffix")}
) : null}
)} />
{/* Metadata filter */} ); } function TopKNumberField({ form, label, hint, }: { form: UseFormReturn; label: string; hint?: string; }) { const { t } = useI18n(); return ( (
{label} {hint ? ( {hint} ) : null}
field.onChange( event.target.value ? Number(event.target.value) : undefined, ) } />
)} /> ); }