import { useEffect, useState, useMemo } from "react"; import { useAppState } from "./store"; import type { IterationSummary } from "./types"; const ADAPTATION_COLORS: Record = { L1_explore: { bg: "bg-blue-900/40", text: "text-blue-400", bar: "bg-blue-500" }, L1_exploit: { bg: "bg-green-900/40", text: "text-green-400", bar: "bg-green-500" }, L2_migrate: { bg: "bg-amber-900/40", text: "text-amber-400", bar: "bg-amber-500" }, L3_meta: { bg: "bg-red-900/40", text: "text-red-400", bar: "bg-red-500" }, }; const DEFAULT_COLOR = { bg: "bg-gray-900/40", text: "text-gray-400", bar: "bg-gray-500" }; function getAdaptationColor(type: string) { return ADAPTATION_COLORS[type] || DEFAULT_COLOR; } type SortKey = "iteration" | "score" | "best_score" | "delta" | "island_id" | "adaptation_type"; type SortDir = "asc" | "desc"; export default function AdaevolveApp() { const store = useAppState(); const { state } = store; const [repoInput, setRepoInput] = useState(""); const [sortKey, setSortKey] = useState("iteration"); const [sortDir, setSortDir] = useState("asc"); useEffect(() => { store.loadPresets(); }, []); // Get the currently selected dataset const selectedDataset = useMemo( () => state.datasets.find((d) => d.id === state.selectedDatasetId) ?? null, [state.datasets, state.selectedDatasetId] ); // Filter iterations by adaptation type const filteredIterations = useMemo(() => { if (!selectedDataset) return []; let iters = selectedDataset.iterations; if (state.filterAdaptation) { iters = iters.filter((it) => it.adaptation_type === state.filterAdaptation); } return iters; }, [selectedDataset, state.filterAdaptation]); // Sort iterations const sortedIterations = useMemo(() => { const sorted = [...filteredIterations]; sorted.sort((a, b) => { const av = a[sortKey]; const bv = b[sortKey]; if (typeof av === "number" && typeof bv === "number") { return sortDir === "asc" ? av - bv : bv - av; } const sa = String(av); const sb = String(bv); return sortDir === "asc" ? sa.localeCompare(sb) : sb.localeCompare(sa); }); return sorted; }, [filteredIterations, sortKey, sortDir]); // Get all unique adaptation types across selected dataset const adaptationTypes = useMemo(() => { if (!selectedDataset) return []; const types = new Set(selectedDataset.iterations.map((it) => it.adaptation_type)); return Array.from(types).sort(); }, [selectedDataset]); function handleSort(key: SortKey) { if (sortKey === key) { setSortDir((d) => (d === "asc" ? "desc" : "asc")); } else { setSortKey(key); setSortDir("asc"); } } function handleLoad() { if (repoInput.trim()) { store.loadDataset(repoInput.trim()); setRepoInput(""); } } // Compute max score for bar chart scaling const maxScore = useMemo(() => { if (!filteredIterations.length) return 1; return Math.max(...filteredIterations.map((it) => Math.abs(it.score)), 0.001); }, [filteredIterations]); const sortIndicator = (key: SortKey) => { if (sortKey !== key) return ""; return sortDir === "asc" ? " ^" : " v"; }; return (
{/* Sidebar */}
{/* Repo input */}
setRepoInput(e.target.value)} onKeyDown={(e) => e.key === "Enter" && handleLoad()} placeholder="org/dataset-name" className="flex-1 bg-gray-800 text-sm px-2 py-1.5 rounded border border-gray-700 focus:border-rose-500 focus:outline-none text-gray-200" />
{/* Presets */}
Presets
{state.presets.length === 0 && (
No presets available
)}
{state.presets.map((p) => (
))}
{/* Loaded datasets */}
Loaded Datasets ({state.datasets.length})
{state.datasets.map((ds) => (
store.selectDataset(ds.id)} >
{ds.name}
{ds.n_iterations} iterations | {ds.summary_stats.n_islands} islands
Best: {ds.summary_stats.global_best.toFixed(4)}
{/* Adaptation breakdown */}
{Object.entries(ds.summary_stats.adaptation_counts).map(([type, count]) => { const color = getAdaptationColor(type); return (
{type} {count}
); })}
))}
{/* Filter by adaptation type */} {selectedDataset && (
Filter by Adaptation
)}
{/* Main content */}
{state.error && (
{state.error}
)} {state.loading && (
Loading...
)} {/* Timeline view */} {state.viewMode === "timeline" && selectedDataset && (
{/* Bar chart */}
Score Timeline ({filteredIterations.length} iterations)
{filteredIterations.map((it) => { const height = Math.max((Math.abs(it.score) / maxScore) * 100, 2); const color = getAdaptationColor(it.adaptation_type); return (
store.selectIteration(selectedDataset.id, it.index)} title={`iter ${it.iteration} | score ${it.score.toFixed(4)} | ${it.adaptation_type}`} >
); })}
{/* Legend */}
{Object.entries(ADAPTATION_COLORS).map(([type, color]) => (
{type}
))}
{/* Iteration table */}
{( [ ["iteration", "Iter"], ["island_id", "Island"], ["score", "Score"], ["best_score", "Best"], ["delta", "Delta"], ["adaptation_type", "Adaptation"], ] as [SortKey, string][] ).map(([key, label]) => ( ))} {sortedIterations.map((it) => { const color = getAdaptationColor(it.adaptation_type); return ( store.selectIteration(selectedDataset.id, it.index)} className="border-b border-gray-800/50 hover:bg-gray-800/50 cursor-pointer" > ); })}
handleSort(key)} className="px-3 py-2 text-left text-gray-400 font-medium cursor-pointer hover:text-gray-200 select-none" > {label} {sortIndicator(key)} Valid Task
{it.iteration} {it.island_id} {it.score.toFixed(4)} {it.best_score.toFixed(4)} 0 ? "text-green-400" : it.delta < 0 ? "text-red-400" : "text-gray-500" }`} > {it.delta > 0 ? "+" : ""} {it.delta.toFixed(4)} {it.adaptation_type} {it.is_valid ? ( yes ) : ( no )} {it.task_id}
)} {/* Detail view */} {state.viewMode === "detail" && state.iterationDetail && (
{/* Detail header */}
Iteration {state.iterationDetail.iteration} | Island{" "} {state.iterationDetail.island_id}
{state.iterationDetail.adaptation_type} score: {state.iterationDetail.score.toFixed(4)} 0 ? "text-green-400" : state.iterationDetail.delta < 0 ? "text-red-400" : "text-gray-500" }`} > ({state.iterationDetail.delta > 0 ? "+" : ""} {state.iterationDetail.delta.toFixed(4)}) {state.iterationDetail.is_valid ? ( valid ) : ( invalid )}
{/* Detail grid */}
{/* Meta info */}
Task ID
{state.iterationDetail.task_id || "-"}
Exploration Intensity
{state.iterationDetail.exploration_intensity.toFixed(4)}
Meta-Guidance Tactic
{state.iterationDetail.meta_guidance_tactic || "-"}
Tactic Approach
{state.iterationDetail.tactic_approach_type || "-"}
{/* Prompt */}
Prompt
                    {state.iterationDetail.prompt_text || "(empty)"}
                  
{/* Reasoning trace */}
Reasoning Trace
                    {state.iterationDetail.reasoning_trace || "(empty)"}
                  
{/* Generated code */}
Generated Code
                    {state.iterationDetail.program_code || "(empty)"}
                  
{/* Navigation */} {selectedDataset && (
{state.iterationDetail.index + 1} / {selectedDataset.n_iterations}
)}
)} {/* Empty state */} {state.datasets.length === 0 && state.viewMode === "timeline" && (
No datasets loaded
Load a HuggingFace repo from the sidebar or select a preset
)} {state.datasets.length > 0 && !selectedDataset && state.viewMode === "timeline" && (
Select a dataset
Click a dataset in the sidebar to view its iterations
)}
); }