"use client"; import { useAtomValue } from "jotai"; import { AlertCircle, Bot, CheckCircle, CircleDashed, FileText, RefreshCw, RotateCcw, Save, Shuffle, } from "lucide-react"; import { AnimatePresence, motion } from "motion/react"; import { useEffect, useState } from "react"; import { toast } from "sonner"; import { updateLLMPreferencesMutationAtom } from "@/atoms/new-llm-config/new-llm-config-mutation.atoms"; import { globalNewLLMConfigsAtom, llmPreferencesAtom, newLLMConfigsAtom, } from "@/atoms/new-llm-config/new-llm-config-query.atoms"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Skeleton } from "@/components/ui/skeleton"; import { cn } from "@/lib/utils"; import { getProviderIcon } from "@/lib/provider-icons"; const ROLE_DESCRIPTIONS = { agent: { icon: Bot, title: "Agent LLM", description: "Primary LLM for chat interactions and agent operations", color: "text-blue-600 dark:text-blue-400", bgColor: "bg-blue-500/10", }, document_summary: { icon: FileText, title: "Document Summary LLM", description: "Handles document summarization and research synthesis", color: "text-purple-600 dark:text-purple-400", bgColor: "bg-purple-500/10", }, }; interface LLMRoleManagerProps { searchSpaceId: number; } export function LLMRoleManager({ searchSpaceId }: LLMRoleManagerProps) { const { data: newLLMConfigs = [], isFetching: configsLoading, error: configsError, refetch: refreshConfigs, } = useAtomValue(newLLMConfigsAtom); const { data: globalConfigs = [], isFetching: globalConfigsLoading, error: globalConfigsError, } = useAtomValue(globalNewLLMConfigsAtom); const { data: preferences = {}, isFetching: preferencesLoading, error: preferencesError, } = useAtomValue(llmPreferencesAtom); const { mutateAsync: updatePreferences } = useAtomValue(updateLLMPreferencesMutationAtom); const [assignments, setAssignments] = useState({ agent_llm_id: preferences.agent_llm_id ?? "", document_summary_llm_id: preferences.document_summary_llm_id ?? "", }); const [hasChanges, setHasChanges] = useState(false); const [isSaving, setIsSaving] = useState(false); useEffect(() => { const newAssignments = { agent_llm_id: preferences.agent_llm_id ?? "", document_summary_llm_id: preferences.document_summary_llm_id ?? "", }; setAssignments(newAssignments); setHasChanges(false); }, [preferences]); const handleRoleAssignment = (role: string, configId: string) => { const newAssignments = { ...assignments, [role]: configId === "unassigned" ? "" : parseInt(configId), }; setAssignments(newAssignments); const currentPrefs = { agent_llm_id: preferences.agent_llm_id ?? "", document_summary_llm_id: preferences.document_summary_llm_id ?? "", }; const hasChangesNow = Object.keys(newAssignments).some( (key) => newAssignments[key as keyof typeof newAssignments] !== currentPrefs[key as keyof typeof currentPrefs] ); setHasChanges(hasChangesNow); }; const handleSave = async () => { setIsSaving(true); const numericAssignments = { agent_llm_id: typeof assignments.agent_llm_id === "string" ? assignments.agent_llm_id ? parseInt(assignments.agent_llm_id) : undefined : assignments.agent_llm_id, document_summary_llm_id: typeof assignments.document_summary_llm_id === "string" ? assignments.document_summary_llm_id ? parseInt(assignments.document_summary_llm_id) : undefined : assignments.document_summary_llm_id, }; await updatePreferences({ search_space_id: searchSpaceId, data: numericAssignments, }); setHasChanges(false); toast.success("LLM role assignments saved successfully!"); setIsSaving(false); }; const handleReset = () => { setAssignments({ agent_llm_id: preferences.agent_llm_id ?? "", document_summary_llm_id: preferences.document_summary_llm_id ?? "", }); setHasChanges(false); }; const isAssignmentComplete = assignments.agent_llm_id !== "" && assignments.agent_llm_id !== null && assignments.agent_llm_id !== undefined && assignments.document_summary_llm_id !== "" && assignments.document_summary_llm_id !== null && assignments.document_summary_llm_id !== undefined; // Combine global and custom configs const allConfigs = [ ...globalConfigs.map((config) => ({ ...config, is_global: true })), ...newLLMConfigs.filter((config) => config.id && config.id.toString().trim() !== ""), ]; const isLoading = configsLoading || preferencesLoading || globalConfigsLoading; const hasError = configsError || preferencesError || globalConfigsError; return (
{/* Header actions */}
{isAssignmentComplete && !isLoading && !hasError && ( All roles assigned )}
{/* Error Alert */} {hasError && ( {(configsError?.message ?? "Failed to load LLM configurations") || (preferencesError?.message ?? "Failed to load preferences") || (globalConfigsError?.message ?? "Failed to load global configurations")} )} {/* Loading Skeleton */} {isLoading && (
{["skeleton-a", "skeleton-b"].map((key) => ( {/* Header: icon + title + status */}
{/* Label */}
{/* Summary block */}
))}
)} {/* No configs warning */} {!isLoading && !hasError && allConfigs.length === 0 && ( No LLM configurations found. Please add at least one LLM provider in the Agent Configs tab before assigning roles. )} {/* Role Assignment Cards */} {!isLoading && !hasError && allConfigs.length > 0 && ( {Object.entries(ROLE_DESCRIPTIONS).map(([key, role], index) => { const IconComponent = role.icon; const currentAssignment = assignments[`${key}_llm_id` as keyof typeof assignments]; const assignedConfig = allConfigs.find( (config) => config.id === currentAssignment ); const isAssigned = currentAssignment !== "" && currentAssignment !== null && currentAssignment !== undefined; const isAutoMode = assignedConfig && "is_auto_mode" in assignedConfig && assignedConfig.is_auto_mode; return ( {/* Role Header */}

{role.title}

{role.description}

{isAssigned ? ( ) : ( )}
{/* Selector */}
{/* Assigned Config Summary */} {assignedConfig && (
{isAutoMode ? (

Auto Load Balanced

Routes across all available providers

) : (
{assignedConfig.name} {"is_global" in assignedConfig && assignedConfig.is_global && ( 🌐 Global )}
{getProviderIcon(assignedConfig.provider, { className: "size-3 shrink-0" })} {assignedConfig.model_name}
{assignedConfig.api_base && (

{assignedConfig.api_base}

)}
)}
)}
); })}
)} {/* Save / Reset Bar */} {hasChanges && (

You have unsaved changes

)}
); }