Spaces:
Running
Running
File size: 8,134 Bytes
c2ea5ed |
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 |
import React, { useState, useEffect, useMemo } from "react";
import { ExampleTraceLite, ExampleTrace } from "@/types";
import { api } from "@/lib/api";
import { Button } from "@/components/ui/button";
import { useToast } from "@/hooks/use-toast";
import { useAgentGraph } from "@/context/AgentGraphContext";
import { CheckCircle, AlertTriangle } from "lucide-react";
import ExampleTraceDetailModal from "./ExampleTraceDetailModal";
import { AgentMultiSelect } from "@/components/ui/agent-multi-select";
interface Props {
data?: any;
onClose: () => void;
}
// Remove subset concept - load all traces as "Who_and_When"
const SUBSETS = ["Who_and_When"] as const;
type CountFilter = 1 | 2 | 3 | null;
export const ExampleTraceModal: React.FC<Props> = ({ onClose: _onClose }) => {
const [subset, setSubset] = useState<(typeof SUBSETS)[number]>(SUBSETS[0]);
const [examples, setExamples] = useState<ExampleTraceLite[]>([]);
const [loading, setLoading] = useState(false);
const [selectedAgents, setSelectedAgents] = useState<string[]>([]);
const [countFilter, setCountFilter] = useState<CountFilter>(null);
const [detailOpen, setDetailOpen] = useState(false);
const [selectedExample, setSelectedExample] = useState<ExampleTrace | null>(
null
);
const { toast } = useToast();
const { actions } = useAgentGraph();
const load = async (s: string) => {
setLoading(true);
try {
// For "Who_and_When", load all traces (no subset filter)
const subsetParam = s === "Who_and_When" ? undefined : s;
const list = await api.exampleTraces.list(subsetParam);
setExamples(list);
} catch (e: any) {
toast({ title: "Error", description: e.message });
} finally {
setLoading(false);
}
};
useEffect(() => {
load(subset);
}, [subset]);
// Unique agent options for current examples
const agentOptions = useMemo(() => {
const set = new Set<string>();
examples.forEach((ex) => ex.agents?.forEach((a) => set.add(a)));
return Array.from(set).sort();
}, [examples]);
// Apply filters
const filteredExamples = useMemo(() => {
return examples.filter((ex) => {
// agent multi-select filter
if (selectedAgents.length) {
const hasAll = selectedAgents.every((a) => ex.agents?.includes(a));
if (!hasAll) return false;
}
// count filter
if (countFilter) {
const c = ex.agents?.length || 0;
if (countFilter === 3) {
if (c < 3) return false;
} else if (c !== countFilter) {
return false;
}
}
return true;
});
}, [examples, selectedAgents, countFilter]);
const handleImport = async (ex: ExampleTrace) => {
setLoading(true);
try {
await api.exampleTraces.import(ex.subset, ex.id);
toast({ title: "Imported", description: "Trace added to workspace" });
const tracesData = await api.traces.list();
actions.setTraces(Array.isArray(tracesData) ? tracesData : []);
} catch (e: any) {
toast({ title: "Import failed", description: e.message });
} finally {
setLoading(false);
}
};
const clearFilters = () => {
setSelectedAgents([]);
setCountFilter(null);
};
// UI helpers
const countBtnVariant = (val: CountFilter) =>
val === countFilter ? "default" : "outline";
return (
<div className="flex flex-col h-[90vh]">
{/* Toolbar */}
<div className="flex flex-wrap items-center gap-4 pb-4 border-b p-4">
{/* Subset buttons */}
<div className="flex gap-2">
{SUBSETS.map((s) => (
<Button
key={s}
variant={s === subset ? "default" : "outline"}
size="sm"
onClick={() => setSubset(s)}
>
{s}
</Button>
))}
</div>
{/* Agent multiselect */}
<AgentMultiSelect
options={agentOptions}
value={selectedAgents}
onChange={setSelectedAgents}
/>
{/* Count filter */}
<div className="flex items-center gap-2 ml-auto">
<span className="text-sm text-muted-foreground">Agents:</span>
{[1, 2, 3].map((n) => (
<Button
key={n}
variant={countBtnVariant(n as CountFilter)}
size="sm"
onClick={() =>
setCountFilter((prev) =>
prev === n ? null : (n as CountFilter)
)
}
>
{n === 3 ? "3+" : n}
</Button>
))}
{(selectedAgents.length > 0 || countFilter) && (
<Button variant="ghost" size="sm" onClick={clearFilters}>
Clear
</Button>
)}
</div>
</div>
{/* Content */}
<div className="flex-1 overflow-y-auto p-4">
{loading ? (
<p className="p-4 text-muted-foreground">Loading...</p>
) : filteredExamples.length === 0 ? (
<p className="p-4 text-muted-foreground">No examples found</p>
) : (
<div className="grid gap-6 grid-cols-1 sm:grid-cols-2 xl:grid-cols-3">
{filteredExamples.map((ex) => (
<div
key={ex.id}
className="border rounded-lg p-5 hover:shadow-md transition cursor-pointer flex flex-col gap-3 min-h-[150px] hover:border-primary bg-white"
onClick={async () => {
try {
const full = await api.exampleTraces.get(ex.subset, ex.id);
setSelectedExample(full);
setDetailOpen(true);
} catch (err: any) {
toast({ title: "Error", description: err.message });
}
}}
>
{/* Header with correctness indicator */}
<div className="flex items-start justify-between gap-2">
<div className="font-medium line-clamp-3 flex-1">
{ex.question}
</div>
<div className="flex items-center gap-1 flex-shrink-0">
{ex.is_correct === true && (
<CheckCircle className="h-4 w-4 text-green-600" />
)}
</div>
</div>
{/* Agent info */}
<div className="text-xs text-muted-foreground">
<div className="flex flex-wrap gap-1">
<span className="text-muted-foreground">agents:</span>
{ex.agents?.map((agent, index) => (
<span
key={agent}
className={`${
agent === ex.mistake_agent
? "text-orange-700 font-medium"
: "text-gray-700"
}`}
>
{agent}
{agent === ex.mistake_agent ? " ⚠️" : ""}
{index < (ex.agents?.length || 0) - 1 ? "," : ""}
</span>
)) || <span>?</span>}
</div>
</div>
{/* Failure reason preview */}
{ex.mistake_reason && (
<div className="flex items-start gap-2 bg-blue-50/50 rounded-md p-2 border border-blue-200">
<AlertTriangle className="h-3 w-3 text-blue-600 mt-0.5 flex-shrink-0" />
<p className="text-xs text-blue-700 line-clamp-2">
{ex.mistake_reason}
</p>
</div>
)}
</div>
))}
</div>
)}
</div>
{/* Detail Drawer */}
{selectedExample && (
<ExampleTraceDetailModal
open={detailOpen}
example={selectedExample}
onOpenChange={(o) => setDetailOpen(o)}
onImport={() => handleImport(selectedExample)}
/>
)}
</div>
);
};
export default ExampleTraceModal;
|