"use client"; import React, { useState, useEffect } from 'react'; import { motion } from 'framer-motion'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Brain, Zap, Bot, AlertCircle, CheckCircle, Settings2, RefreshCw, Save, RotateCcw, Loader2 } from 'lucide-react'; import { useLLMConfigs, useLLMPreferences } from '@/hooks/use-llm-configs'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { toast } from 'sonner'; const ROLE_DESCRIPTIONS = { long_context: { icon: Brain, title: 'Long Context LLM', description: 'Handles complex tasks requiring extensive context understanding and reasoning', color: 'bg-blue-100 text-blue-800 border-blue-200', examples: 'Document analysis, research synthesis, complex Q&A', characteristics: ['Large context window', 'Deep reasoning', 'Complex analysis'] }, fast: { icon: Zap, title: 'Fast LLM', description: 'Optimized for quick responses and real-time interactions', color: 'bg-green-100 text-green-800 border-green-200', examples: 'Quick searches, simple questions, instant responses', characteristics: ['Low latency', 'Quick responses', 'Real-time chat'] }, strategic: { icon: Bot, title: 'Strategic LLM', description: 'Advanced reasoning for planning and strategic decision making', color: 'bg-purple-100 text-purple-800 border-purple-200', examples: 'Planning workflows, strategic analysis, complex problem solving', characteristics: ['Strategic thinking', 'Long-term planning', 'Complex reasoning'] } }; export function LLMRoleManager() { const { llmConfigs, loading: configsLoading, error: configsError, refreshConfigs } = useLLMConfigs(); const { preferences, loading: preferencesLoading, error: preferencesError, updatePreferences, refreshPreferences } = useLLMPreferences(); const [assignments, setAssignments] = useState({ long_context_llm_id: preferences.long_context_llm_id || '', fast_llm_id: preferences.fast_llm_id || '', strategic_llm_id: preferences.strategic_llm_id || '' }); const [hasChanges, setHasChanges] = useState(false); const [isSaving, setIsSaving] = useState(false); useEffect(() => { const newAssignments = { long_context_llm_id: preferences.long_context_llm_id || '', fast_llm_id: preferences.fast_llm_id || '', strategic_llm_id: preferences.strategic_llm_id || '' }; setAssignments(newAssignments); setHasChanges(false); }, [preferences]); const handleRoleAssignment = (role: string, configId: string) => { const newAssignments = { ...assignments, [role]: configId === 'unassigned' ? '' : parseInt(configId) }; setAssignments(newAssignments); // Check if there are changes compared to current preferences const currentPrefs = { long_context_llm_id: preferences.long_context_llm_id || '', fast_llm_id: preferences.fast_llm_id || '', strategic_llm_id: preferences.strategic_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 = { long_context_llm_id: typeof assignments.long_context_llm_id === 'string' ? (assignments.long_context_llm_id ? parseInt(assignments.long_context_llm_id) : undefined) : assignments.long_context_llm_id, fast_llm_id: typeof assignments.fast_llm_id === 'string' ? (assignments.fast_llm_id ? parseInt(assignments.fast_llm_id) : undefined) : assignments.fast_llm_id, strategic_llm_id: typeof assignments.strategic_llm_id === 'string' ? (assignments.strategic_llm_id ? parseInt(assignments.strategic_llm_id) : undefined) : assignments.strategic_llm_id, }; const success = await updatePreferences(numericAssignments); if (success) { setHasChanges(false); toast.success('LLM role assignments saved successfully!'); } setIsSaving(false); }; const handleReset = () => { setAssignments({ long_context_llm_id: preferences.long_context_llm_id || '', fast_llm_id: preferences.fast_llm_id || '', strategic_llm_id: preferences.strategic_llm_id || '' }); setHasChanges(false); }; const isAssignmentComplete = assignments.long_context_llm_id && assignments.fast_llm_id && assignments.strategic_llm_id; const assignedConfigIds = Object.values(assignments).filter(id => id !== ''); const availableConfigs = llmConfigs.filter(config => config.id && config.id.toString().trim() !== ''); const isLoading = configsLoading || preferencesLoading; const hasError = configsError || preferencesError; return (
{/* Header */}

LLM Role Management

Assign your LLM configurations to specific roles for different purposes.

{/* Error Alert */} {hasError && ( {configsError || preferencesError} )} {/* Loading State */} {isLoading && (
{configsLoading && preferencesLoading ? 'Loading configurations and preferences...' : configsLoading ? 'Loading configurations...' : 'Loading preferences...'}
)} {/* Stats Overview */} {!isLoading && !hasError && (

{availableConfigs.length}

Available Models

{assignedConfigIds.length}

Assigned Roles

{Math.round((assignedConfigIds.length / 3) * 100)}%

Completion

{isAssignmentComplete ? ( ) : ( )}

{isAssignmentComplete ? 'Ready' : 'Setup'}

Status

{isAssignmentComplete ? ( ) : ( )}
)} {/* Info Alert */} {!isLoading && !hasError && (
{availableConfigs.length === 0 ? ( No LLM configurations found. Please add at least one LLM provider in the Model Configs tab before assigning roles. ) : !isAssignmentComplete ? ( Complete all role assignments to enable full functionality. Each role serves different purposes in your workflow. ) : ( All roles are assigned and ready to use! Your LLM configuration is complete. )} {/* Role Assignment Cards */} {availableConfigs.length > 0 && (
{Object.entries(ROLE_DESCRIPTIONS).map(([key, role]) => { const IconComponent = role.icon; const currentAssignment = assignments[`${key}_llm_id` as keyof typeof assignments]; const assignedConfig = availableConfigs.find(config => config.id === currentAssignment); return (
{role.title} {role.description}
{currentAssignment && ( )}
Use cases: {role.examples}
{role.characteristics.map((char, idx) => ( {char} ))}
{assignedConfig && (
Assigned: {assignedConfig.provider} {assignedConfig.name}
Model: {assignedConfig.model_name}
{assignedConfig.api_base && (
Base: {assignedConfig.api_base}
)}
)}
); })}
)} {/* Action Buttons */} {hasChanges && (
)} {/* Status Indicator */} {isAssignmentComplete && !hasChanges && (
All roles assigned and saved!
)} {/* Progress Indicator */}
Progress:
{Object.keys(ROLE_DESCRIPTIONS).map((key, index) => (
))}
{assignedConfigIds.length} of {Object.keys(ROLE_DESCRIPTIONS).length} roles assigned
)}
); }