"use client"; import { useTranslation } from "react-i18next"; import type { IndexingLLMSelection, LightRagRoleModel, LightRagRoleModels, } from "@/features/knowledge/model/types"; import type { LLMOption } from "@/lib/llm-options"; import IndexingModelSelector from "./IndexingModelSelector"; const ROLE_DESCRIPTIONS = { extract: "Used to extract entities and relationships during indexing. Choose a fast, economical model with reasoning disabled.", keyword: "Used to generate retrieval keywords before a query. Choose a fast model with reasoning disabled.", query: "Used to produce the final answer from long, complex retrieval context. Choose a capable model; reasoning can be enabled.", vlm: "Used to analyze images during indexing. The model must support image input.", } as const; export const inheritedRole = (maxAsync = 4): LightRagRoleModel => ({ mode: "inherit", max_async: maxAsync, timeout: 240, }); export const newRoleModels = ( base: IndexingLLMSelection, vision = false, maxAsync = 4, ): LightRagRoleModels => ({ base, extract: inheritedRole(maxAsync), keyword: inheritedRole(maxAsync), query: inheritedRole(maxAsync), vlm: { ...inheritedRole(maxAsync), mode: vision ? "inherit" : "disabled", }, }); export function roleModelsValidationError( models: LightRagRoleModels | null, options: LLMOption[], ): string | null { if (!models) return "Choose a LightRAG base model before saving."; const findOption = (selection: IndexingLLMSelection | null | undefined) => options.find( (option) => option.profile_id === selection?.profile_id && option.model_id === selection?.model_id, ); const baseOption = findOption(models.base); if (!baseOption) { return "The LightRAG base model is unavailable. Choose an accessible model."; } if ( models.base.reasoning_effort && !baseOption.supported_reasoning_efforts?.includes( models.base.reasoning_effort, ) ) { return "The selected reasoning effort is no longer supported."; } for (const role of ["extract", "keyword", "query", "vlm"] as const) { const value = models[role]; if ( !Number.isInteger(value.max_async) || value.max_async < 1 || value.max_async > 32 || !Number.isInteger(value.timeout) || value.timeout < 1 || value.timeout > 3600 ) { return "Concurrency and timeout values must stay within the displayed limits."; } const effective = resolvedRole(models, role); if (!effective) continue; const option = findOption(effective); if (!option) { return "One or more role models are unavailable. Choose accessible models before saving."; } if (role === "vlm" && option.supports_vision !== true) { return "The selected VLM model does not support image inputs."; } if ( effective.reasoning_effort && !option.supported_reasoning_efforts?.includes( effective.reasoning_effort, ) ) { return "The selected reasoning effort is no longer supported."; } } return null; } export function resolvedRole( models: LightRagRoleModels, role: "extract" | "keyword" | "query" | "vlm", ): IndexingLLMSelection | null { const value = models[role]; if (value.mode === "disabled") return null; const selected = value.mode === "inherit" ? models.base : value.selection; if (!selected) return null; return { ...selected, ...(value.reasoning_effort ? { reasoning_effort: value.reasoning_effort } : {}), }; } export default function LightRagRoleModelsEditor({ models, options, initialMaxAsync, loading, error, disabled, onChange, }: { models: LightRagRoleModels | null; options: LLMOption[]; initialMaxAsync?: number; loading: boolean; error: boolean; disabled?: boolean; onChange: (value: LightRagRoleModels) => void; }) { const { t } = useTranslation(); const baseOption = options.find( (option) => option.profile_id === models?.base.profile_id && option.model_id === models?.base.model_id, ); return (
{ if (base) onChange( models ? { ...models, base } : newRoleModels(base, false, initialMaxAsync), ); }} /> {baseOption && (

{t("Currently effective")}: {baseOption.provider_label || baseOption.profile_name} ·{" "} {baseOption.model_name} ·{" "} {models?.base.reasoning_effort || t("Auto")}

)}
{models && (["extract", "keyword", "query", "vlm"] as const).map((role) => { const value = models[role]; const effective = resolvedRole(models, role); const roleOptions = role === "vlm" ? options.filter((option) => option.supports_vision) : options; const effectiveOption = options.find( (option) => option.profile_id === effective?.profile_id && option.model_id === effective?.model_id, ); const selectionKey = value.mode === "model" && value.selection ? `${value.selection.profile_id}:${value.selection.model_id}` : value.mode; const update = (patch: Partial) => onChange({ ...models, [role]: { ...value, ...patch } }); return (
{role.toUpperCase()}

{t(ROLE_DESCRIPTIONS[role])}

{value.mode !== "disabled" && ( { if (selection) update({ selection: value.mode === "model" ? { profile_id: selection.profile_id, model_id: selection.model_id, } : null, reasoning_effort: selection.reasoning_effort ?? null, }); }} /> )} {effectiveOption && value.mode !== "disabled" && (

{t("Currently effective")}: {effectiveOption.provider_label || effectiveOption.profile_name} ·{" "} {effectiveOption.model_name} ·{" "} {effective?.reasoning_effort || t("Auto")}

)} {role === "vlm" && value.mode !== "disabled" && !options.some( (option) => option.profile_id === effective?.profile_id && option.model_id === effective?.model_id && option.supports_vision, ) && (

{t("The selected VLM model does not support image inputs.")}

)} {value.mode !== "disabled" && (
{t("Advanced")}
)}
); })}
); }