The platform for continuously improving AI agents.
import { useQueries } from "@tanstack/react-query";
import api from "@/client";
import { ModelProviderChip } from "@/components/model-provider-chip";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Icon } from "@/components/ui/icons";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import { type BenchmarkTest, benchmarkRows } from "./benchmark-rows";
import type { ExperimentBlock, ExperimentDocument } from "./document";
const partitions: Record<string, string> = {
development: "Development",
final: "Final holdout",
train: "Training",
};
function datasetLabel(dataset: BenchmarkTest["dataset"]) {
const partition = partitions[dataset.partition ?? ""];
return partition ? `${dataset.title} · ${partition}` : dataset.title;
}
function BenchmarkChip({
label,
icon: Glyph,
options,
summary,
}: {
label: string;
icon: typeof Icon.eval;
options: { key: string; label: string; open: () => void }[];
summary: string;
}) {
const chip = (
<Button
aria-label={`${label}: ${summary}`}
className="min-w-0 max-w-full justify-start border-border bg-wash-raised"
onClick={options.length === 1 ? options[0].open : undefined}
size="xs"
variant="outline"
>
<Glyph className="text-muted-foreground" />
<span className="truncate">{summary}</span>
</Button>
);
if (options.length === 1)
return (
<Tooltip>
<TooltipTrigger asChild>{chip}</TooltipTrigger>
<TooltipContent>{summary}</TooltipContent>
</Tooltip>
);
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>{chip}</DropdownMenuTrigger>
<DropdownMenuContent align="start">
{options.map((option) => (
<DropdownMenuItem key={option.key} onSelect={option.open}>
{option.label}
</DropdownMenuItem>
))}
</DropdownMenuContent>
</DropdownMenu>
);
}
export function BenchmarkOverview({
document,
nodes,
onSelect,
onEdit,
}: {
document: ExperimentDocument;
nodes: ExperimentBlock[];
onSelect: (id: string) => void;
onEdit: (id: string) => void;
}) {
const rows = benchmarkRows(document, nodes);
const setIds = [
...new Set(rows.flatMap((row) => row.tests.flatMap(({ evalSet }) => evalSet?.id ?? []))),
];
const sets = useQueries({
queries: setIds.map((id) => ({
queryFn: () => api.evalSets.evalSetsRetrieve({ id }),
queryKey: ["eval-set", id],
staleTime: 30_000,
})),
});
return (
<ul
aria-label="Benchmark entries"
className="nodrag nopan nowheel m-0 grid list-none gap-1.5 p-0"
>
{rows.map(({ candidate, key, tests }) => {
const model = candidate?.model;
const name = candidate?.job ? (candidate.title ?? model) : (model ?? candidate?.title);
const datasets = [
...new Map(
tests.map(({ dataset, node }) => [
JSON.stringify([dataset.node, dataset.partition]),
{ dataset, node },
])
).values(),
];
const datasetTitles = new Set(datasets.map(({ dataset }) => dataset.title));
const evaluations = tests.map(({ dataset, evalSet, node }) => ({
dataset,
evalSet,
node,
title:
(evalSet?.id ? sets[setIds.indexOf(evalSet.id)]?.data?.name : undefined) ??
evalSet?.title ??
"No eval set",
}));
const names = new Map(
evaluations.flatMap(({ evalSet, title }) =>
evalSet ? [[evalSet.id ?? title, title]] : []
)
);
return (
<li
className="grid grid-cols-[minmax(0,1fr)_minmax(0,1fr)_minmax(0,1fr)] items-center gap-1.5"
key={key}
>
<div className="min-w-0">
{model ? (
<ModelProviderChip
compact
model={model}
showModelId={name === model}
showProviderName={false}
>
{name !== model && (
<span className="min-w-0 truncate" title={name}>
{name}
</span>
)}
</ModelProviderChip>
) : (
<Badge
className="max-w-full gap-1 border-border bg-wash-raised font-medium"
size="chip"
variant="outline"
>
<Icon.models className="size-3 text-muted-foreground" />
<span className="truncate" title={name}>
{name ?? "No model"}
</span>
</Badge>
)}
</div>
<div className="min-w-0">
<BenchmarkChip
icon={Icon.dataset}
label="Dataset"
options={datasets.map(({ dataset, node }) => ({
key: JSON.stringify([dataset.node, dataset.partition]),
label: datasetLabel(dataset),
open: () => (dataset.node ? onSelect(dataset.node) : onEdit(node)),
}))}
summary={
datasets.length === 1
? datasetLabel(datasets[0].dataset)
: datasetTitles.size === 1
? `${datasets[0].dataset.title} · ${datasets.length} splits`
: `${datasets.length} datasets`
}
/>
</div>
<div className="min-w-0">
<BenchmarkChip
icon={Icon.eval}
label="Eval set"
options={evaluations.map(({ dataset, node, title }) => ({
key: JSON.stringify([node, dataset.node, dataset.partition]),
label: `${title} · ${datasetLabel(dataset)}`,
open: () => onEdit(node),
}))}
summary={
names.size > 1
? `${names.size} eval sets`
: (names.values().next().value ?? "No eval set")
}
/>
</div>
</li>
);
})}
</ul>
);
}