All files / web/src/components/agents/skills manage-skill-evaluations-dialog.tsx

20.67% Statements 43/208
100% Branches 0/0
0% Functions 0/1
20.67% Lines 43/208

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277      1x 1x 1x 1x 1x 1x               1x 1x 1x   1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x               1x                                                                                                                                                                                                                                                                                                                                                                                                                                          
'use client';
 
import type { SkillOptimizationEvaluation } from '@shared/types/data';
import { EvaluationMethodName } from '@shared/types/evaluations';
import { useQueryClient } from '@tanstack/react-query';
import { Button } from '@web/components/ui/button';
import { Card, CardContent } from '@web/components/ui/card';
import { Checkbox } from '@web/components/ui/checkbox';
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from '@web/components/ui/dialog';
import { Label } from '@web/components/ui/label';
import { useSkillOptimizationEvaluations } from '@web/providers/skill-optimization-evaluations';
import { CheckCircle2, Clock, Loader2 } from 'lucide-react';
import type { ReactElement } from 'react';
import { useEffect, useState } from 'react';
 
// Only show evaluation methods that are enabled on the server
// See lib/server/api/v1/index.ts for the list of enabled connectors
const EVALUATION_METHODS = [
  {
    name: EvaluationMethodName.TASK_COMPLETION,
    label: 'Task Completion',
    description:
      'Evaluates whether the AI successfully completed the requested task',
  },
  {
    name: EvaluationMethodName.TURN_RELEVANCY,
    label: 'Turn Relevancy',
    description:
      'Assesses whether responses are relevant to the conversation context',
  },
  {
    name: EvaluationMethodName.TOOL_CORRECTNESS,
    label: 'Tool Correctness',
    description:
      'Validates that tools are used appropriately and produce correct results',
  },
  {
    name: EvaluationMethodName.KNOWLEDGE_RETENTION,
    label: 'Knowledge Retention',
    description:
      'Evaluates how well the AI retains and recalls information from previous interactions',
  },
  {
    name: EvaluationMethodName.CONVERSATION_COMPLETENESS,
    label: 'Conversation Completeness',
    description:
      'Assesses whether conversations are properly concluded and all topics are addressed',
  },
];
 
interface ManageSkillEvaluationsDialogProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  skillId: string;
}
 
export function ManageSkillEvaluationsDialog({
  open,
  onOpenChange,
  skillId,
}: ManageSkillEvaluationsDialogProps): ReactElement {
  const queryClient = useQueryClient();
  const {
    evaluations,
    setSkillId: setEvaluationsSkillId,
    createEvaluation,
    deleteEvaluation,
    isCreating,
    isDeleting,
    isLoading,
  } = useSkillOptimizationEvaluations();
 
  const [initialMethods, setInitialMethods] = useState<EvaluationMethodName[]>(
    [],
  );
  const [selectedMethods, setSelectedMethods] = useState<
    EvaluationMethodName[]
  >([]);
  const [evaluationMap, setEvaluationMap] = useState<
    Map<EvaluationMethodName, SkillOptimizationEvaluation>
  >(new Map());
  const [isSaving, setIsSaving] = useState(false);
 
  // Set skill ID to trigger evaluation fetch when dialog opens
  useEffect(() => {
    if (open && skillId) {
      // Reset state before fetching
      setSelectedMethods([]);
      setInitialMethods([]);
      setEvaluationMap(new Map());
      setEvaluationsSkillId(skillId);
    } else if (!open) {
      // Clear skill ID when dialog closes
      setEvaluationsSkillId(null);
    }
  }, [open, skillId, setEvaluationsSkillId]);
 
  // Update selected methods and map when evaluations change
  useEffect(() => {
    if (!open) return; // Only update when dialog is open
 
    const map = new Map<EvaluationMethodName, SkillOptimizationEvaluation>();
    const methods: EvaluationMethodName[] = [];
 
    for (const evaluation of evaluations) {
      map.set(evaluation.evaluation_method, evaluation);
      methods.push(evaluation.evaluation_method);
    }
 
    setEvaluationMap(map);
    setSelectedMethods(methods);
    setInitialMethods(methods);
  }, [evaluations, open]);
 
  const handleToggleMethod = (method: EvaluationMethodName) => {
    const isCurrentlySelected = selectedMethods.includes(method);
 
    if (isCurrentlySelected) {
      setSelectedMethods((prev) => prev.filter((m) => m !== method));
    } else {
      setSelectedMethods((prev) => [...prev, method]);
    }
  };
 
  const handleAccept = async () => {
    setIsSaving(true);
    try {
      // Determine what to add and what to remove
      const methodsToAdd = selectedMethods.filter(
        (method) => !initialMethods.includes(method),
      );
      const methodsToRemove = initialMethods.filter(
        (method) => !selectedMethods.includes(method),
      );
 
      // Delete evaluations that were removed
      const deletePromises = methodsToRemove.map(async (method) => {
        const evaluation = evaluationMap.get(method);
        if (evaluation) {
          await deleteEvaluation(skillId, evaluation.id);
        }
      });
 
      // Create evaluations that were added
      const createPromises =
        methodsToAdd.length > 0
          ? [createEvaluation(skillId, methodsToAdd)]
          : [];
 
      // Execute all operations in parallel
      await Promise.all([...deletePromises, ...createPromises]);
 
      // Invalidate the skill validation cache to refresh the UI
      await queryClient.invalidateQueries({
        queryKey: ['skill-validation-evaluations', skillId],
      });
 
      // Close dialog on success
      onOpenChange(false);
    } catch (error) {
      console.error('Failed to save evaluation changes:', error);
    } finally {
      setIsSaving(false);
    }
  };
 
  const handleCancel = () => {
    // Reset to initial state
    setSelectedMethods(initialMethods);
    onOpenChange(false);
  };
 
  const hasChanges =
    JSON.stringify([...selectedMethods].sort()) !==
    JSON.stringify([...initialMethods].sort());
 
  const isProcessing = isCreating || isDeleting || isSaving;
 
  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="max-w-2xl max-h-[80vh] overflow-y-auto">
        <DialogHeader>
          <DialogTitle className="flex items-center gap-2">
            <CheckCircle2 size={20} />
            Manage Evaluation Methods
          </DialogTitle>
          <DialogDescription>
            Evaluations help measure and improve your skill's performance. You
            can add or remove them anytime.
          </DialogDescription>
        </DialogHeader>
 
        <div className="space-y-3 border rounded-md p-3 max-h-[400px] overflow-y-auto">
          {isLoading ? (
            <div className="flex items-center justify-center py-8">
              <Loader2
                size={24}
                className="animate-spin text-muted-foreground"
              />
            </div>
          ) : (
            EVALUATION_METHODS.map((method) => {
              const isSelected = selectedMethods.includes(method.name);
              return (
                <Card
                  key={method.name}
                  className={`cursor-pointer transition-all ${
                    isSelected
                      ? 'border-primary bg-primary/5'
                      : 'hover:border-primary/50'
                  } ${isProcessing ? 'opacity-50 pointer-events-none' : ''}`}
                  onClick={() => handleToggleMethod(method.name)}
                >
                  <CardContent className="p-4">
                    <div className="flex gap-3">
                      <div className="pt-0.5">
                        <Checkbox
                          checked={isSelected}
                          onCheckedChange={() =>
                            handleToggleMethod(method.name)
                          }
                          onClick={(e) => e.stopPropagation()}
                          disabled={isProcessing}
                        />
                      </div>
                      <div className="flex-1">
                        <Label className="text-base font-medium cursor-pointer">
                          {method.label}
                        </Label>
                        <p className="text-sm text-muted-foreground mt-1">
                          {method.description}
                        </p>
                      </div>
                    </div>
                  </CardContent>
                </Card>
              );
            })
          )}
        </div>
 
        <div className="flex items-center gap-2 text-sm text-amber-600 dark:text-amber-500 mb-4">
          <Clock size={16} />
          <span>This process may take 1-2 minutes to complete.</span>
        </div>
 
        <DialogFooter>
          <Button
            variant="outline"
            onClick={handleCancel}
            disabled={isProcessing}
          >
            Cancel
          </Button>
          <Button onClick={handleAccept} disabled={!hasChanges || isProcessing}>
            {isSaving ? (
              <>
                <Loader2 size={16} className="mr-2 animate-spin" />
                Saving...
              </>
            ) : (
              'Save Changes'
            )}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}