|
| 1 | +import React, { useEffect, useRef } from 'react'; |
| 2 | +import { useTraceCriteria, useEvalResults, useCreateCriterionEvaluation } from '@/hooks/useWorkshopApi'; |
| 3 | +import { Badge } from '@/components/ui/badge'; |
| 4 | +import { Check, X, AlertTriangle, ChevronRight } from 'lucide-react'; |
| 5 | +import ReactMarkdown from 'react-markdown'; |
| 6 | +import remarkGfm from 'remark-gfm'; |
| 7 | + |
| 8 | +interface EvalGradingPanelProps { |
| 9 | + workshopId: string; |
| 10 | + traceId: string; |
| 11 | + activeMilestoneRef?: string | null; |
| 12 | + onHoverCriterion?: (milestoneRef: string | null) => void; |
| 13 | + onClose?: () => void; |
| 14 | +} |
| 15 | + |
| 16 | +export const EvalGradingPanel: React.FC<EvalGradingPanelProps> = ({ |
| 17 | + workshopId, |
| 18 | + traceId, |
| 19 | + activeMilestoneRef, |
| 20 | + onHoverCriterion, |
| 21 | + onClose, |
| 22 | +}) => { |
| 23 | + const { data: criteria = [], isLoading: criteriaLoading } = useTraceCriteria(workshopId, traceId); |
| 24 | + const { data: evalResults = [], isLoading: resultsLoading } = useEvalResults(workshopId, traceId, 'HUMAN'); |
| 25 | + const createEval = useCreateCriterionEvaluation(workshopId, traceId); |
| 26 | + const scrollContainerRef = useRef<HTMLDivElement>(null); |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + if (activeMilestoneRef && scrollContainerRef.current) { |
| 30 | + const el = scrollContainerRef.current.querySelector(`[data-milestone-ref="${activeMilestoneRef}"]`); |
| 31 | + if (el) { |
| 32 | + el.scrollIntoView({ behavior: 'smooth', block: 'center' }); |
| 33 | + } |
| 34 | + } |
| 35 | + }, [activeMilestoneRef]); |
| 36 | + |
| 37 | + const traceScore = evalResults.find(r => r.trace_id === traceId); |
| 38 | + const criteriaResults = traceScore?.criteria_results || []; |
| 39 | + const hurdleResults = traceScore?.hurdle_results || []; |
| 40 | + |
| 41 | + // Combine all results for easy lookup |
| 42 | + const allResults = [...criteriaResults, ...hurdleResults]; |
| 43 | + const resultsByCriterionId = new Map(allResults.map(r => [r.criterion_id, r])); |
| 44 | + |
| 45 | + const handleToggle = (criterionId: string, met: boolean) => { |
| 46 | + createEval.mutate({ |
| 47 | + criterion_id: criterionId, |
| 48 | + judge_model: 'HUMAN', |
| 49 | + met, |
| 50 | + }); |
| 51 | + }; |
| 52 | + |
| 53 | + if (criteriaLoading || resultsLoading) { |
| 54 | + return ( |
| 55 | + <div className="flex items-center justify-center h-full text-slate-400"> |
| 56 | + Loading criteria... |
| 57 | + </div> |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + if (criteria.length === 0) { |
| 62 | + return ( |
| 63 | + <div className="flex flex-col items-center justify-center h-full p-6 text-center text-slate-500"> |
| 64 | + <AlertTriangle className="w-8 h-8 mb-3 text-slate-300" /> |
| 65 | + <p className="text-sm font-medium text-slate-600">No criteria defined</p> |
| 66 | + <p className="text-xs mt-1 max-w-[200px]"> |
| 67 | + Create criteria in the Discussion tab to start grading. |
| 68 | + </p> |
| 69 | + </div> |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + // Calculate scores for the slider |
| 74 | + const rawScore = traceScore?.raw_score || 0; |
| 75 | + const maxPossible = traceScore?.max_possible || 0; |
| 76 | + const normalizedScore = traceScore?.normalized_score || 0; |
| 77 | + const hurdlePassed = traceScore?.hurdle_passed ?? true; |
| 78 | + |
| 79 | + return ( |
| 80 | + <div className="flex flex-col h-full overflow-hidden bg-white/80 backdrop-blur-2xl rounded-2xl"> |
| 81 | + <div className="flex items-center justify-between px-4 pt-4 pb-2"> |
| 82 | + <h3 className="text-lg font-bold text-slate-900 tracking-tight"> |
| 83 | + Grading |
| 84 | + </h3> |
| 85 | + {onClose && ( |
| 86 | + <button onClick={onClose} className="p-1.5 hover:bg-slate-100 rounded-full text-slate-400 hover:text-slate-600 transition-colors"> |
| 87 | + <ChevronRight className="w-5 h-5" /> |
| 88 | + </button> |
| 89 | + )} |
| 90 | + </div> |
| 91 | + <div |
| 92 | + ref={scrollContainerRef} |
| 93 | + className="flex-1 overflow-y-auto px-4 py-4 custom-scrollbar" |
| 94 | + > |
| 95 | + <table className="w-full text-sm text-left"> |
| 96 | + <thead className="text-xs text-slate-500 uppercase bg-slate-50/50 sticky top-0 z-10 backdrop-blur-md"> |
| 97 | + <tr> |
| 98 | + <th className="px-4 py-3 font-semibold rounded-tl-lg">Criterion</th> |
| 99 | + <th className="px-4 py-3 font-semibold w-24 text-center">Points</th> |
| 100 | + <th className="px-4 py-3 font-semibold w-32 text-center rounded-tr-lg">Present</th> |
| 101 | + </tr> |
| 102 | + </thead> |
| 103 | + <tbody className="divide-y divide-slate-100"> |
| 104 | + {criteria.map((criterion) => { |
| 105 | + const result = resultsByCriterionId.get(criterion.id); |
| 106 | + const isHurdle = criterion.criterion_type === 'hurdle'; |
| 107 | + const isMet = result?.met; |
| 108 | + |
| 109 | + // Extract milestone ref from text if it exists (e.g. [m2](m2)) |
| 110 | + const milestoneMatch = criterion.text.match(/\[m(\d+)\]\(m\d+\)/); |
| 111 | + const milestoneRef = milestoneMatch ? `m${milestoneMatch[1]}` : null; |
| 112 | + |
| 113 | + return ( |
| 114 | + <tr |
| 115 | + key={criterion.id} |
| 116 | + data-milestone-ref={milestoneRef} |
| 117 | + className="hover:bg-slate-50/50 transition-colors group" |
| 118 | + onMouseEnter={() => onHoverCriterion?.(milestoneRef)} |
| 119 | + onMouseLeave={() => onHoverCriterion?.(null)} |
| 120 | + > |
| 121 | + <td className="px-4 py-4"> |
| 122 | + <div className="prose prose-sm prose-slate max-w-none"> |
| 123 | + <ReactMarkdown |
| 124 | + remarkPlugins={[remarkGfm]} |
| 125 | + components={{ |
| 126 | + p: ({ children }) => <p className="m-0 leading-relaxed font-medium text-slate-700">{children}</p>, |
| 127 | + a: ({ children }) => <span className="text-indigo-600 font-semibold">{children}</span> |
| 128 | + }} |
| 129 | + > |
| 130 | + {criterion.text} |
| 131 | + </ReactMarkdown> |
| 132 | + </div> |
| 133 | + </td> |
| 134 | + <td className="px-4 py-4 text-center"> |
| 135 | + {isHurdle ? ( |
| 136 | + <Badge variant="outline" className="bg-rose-50 text-rose-700 border-rose-200 uppercase tracking-wider text-[10px]"> |
| 137 | + Gate |
| 138 | + </Badge> |
| 139 | + ) : ( |
| 140 | + <span className={`font-mono font-bold ${criterion.weight > 0 ? 'text-emerald-600' : 'text-rose-600'}`}> |
| 141 | + {criterion.weight > 0 ? '+' : ''}{criterion.weight} |
| 142 | + </span> |
| 143 | + )} |
| 144 | + </td> |
| 145 | + <td className="px-4 py-4"> |
| 146 | + <div className="flex items-center justify-center gap-1 bg-slate-100/50 p-1 rounded-lg border border-slate-200/50"> |
| 147 | + <button |
| 148 | + type="button" |
| 149 | + onClick={() => handleToggle(criterion.id, true)} |
| 150 | + className={`flex-1 flex items-center justify-center py-1.5 rounded-md transition-all ${ |
| 151 | + isMet === true |
| 152 | + ? 'bg-emerald-500 text-white shadow-sm' |
| 153 | + : 'text-slate-400 hover:text-emerald-600 hover:bg-emerald-50' |
| 154 | + }`} |
| 155 | + > |
| 156 | + <Check className="w-4 h-4" /> |
| 157 | + </button> |
| 158 | + <button |
| 159 | + type="button" |
| 160 | + onClick={() => handleToggle(criterion.id, false)} |
| 161 | + className={`flex-1 flex items-center justify-center py-1.5 rounded-md transition-all ${ |
| 162 | + isMet === false |
| 163 | + ? 'bg-rose-500 text-white shadow-sm' |
| 164 | + : 'text-slate-400 hover:text-rose-600 hover:bg-rose-50' |
| 165 | + }`} |
| 166 | + > |
| 167 | + <X className="w-4 h-4" /> |
| 168 | + </button> |
| 169 | + </div> |
| 170 | + </td> |
| 171 | + </tr> |
| 172 | + ); |
| 173 | + })} |
| 174 | + </tbody> |
| 175 | + </table> |
| 176 | + </div> |
| 177 | + |
| 178 | + {/* Score Bar (HealthBench style) */} |
| 179 | + <div className="mt-auto border-t border-slate-200 bg-slate-50/80 p-6"> |
| 180 | + <div className="flex items-center justify-between mb-2"> |
| 181 | + <span className="text-xs font-bold text-slate-500 uppercase tracking-wider">Actual Score</span> |
| 182 | + <span className="text-xs font-bold text-slate-500 uppercase tracking-wider">Max Score</span> |
| 183 | + </div> |
| 184 | + |
| 185 | + <div className="relative h-2 bg-slate-200 rounded-full overflow-hidden mb-2"> |
| 186 | + <div |
| 187 | + className={`absolute top-0 left-0 h-full rounded-full transition-all duration-500 ${ |
| 188 | + !hurdlePassed ? 'bg-rose-500' : 'bg-emerald-500' |
| 189 | + }`} |
| 190 | + style={{ width: `${!hurdlePassed ? 0 : normalizedScore * 100}%` }} |
| 191 | + /> |
| 192 | + </div> |
| 193 | + |
| 194 | + <div className="flex items-center justify-between"> |
| 195 | + <span className={`text-lg font-bold font-mono ${!hurdlePassed ? 'text-rose-600' : 'text-slate-900'}`}> |
| 196 | + {!hurdlePassed ? '0 (Gate Failed)' : rawScore} |
| 197 | + </span> |
| 198 | + <span className="text-sm font-bold font-mono text-slate-400"> |
| 199 | + {maxPossible} |
| 200 | + </span> |
| 201 | + </div> |
| 202 | + </div> |
| 203 | + </div> |
| 204 | + ); |
| 205 | +}; |
0 commit comments