The platform for continuously improving AI agents.
import { useQuery } from "@tanstack/react-query";
import api from "@/client";
import { InspectorField, inspectorControl } from "@/components/ai-components/step-inspector";
import { ModelOptionLabel } from "@/components/model-option-label";
import { Button } from "@/components/ui/button";
import { Icon } from "@/components/ui/icons";
import { SearchableSelect } from "@/components/ui/searchable-select";
type Participant = {
key: string;
name: string;
kind: "external" | "trained" | "foundation";
job?: string;
model?: string;
};
export function BenchmarkModels({
projectId,
settings,
locked,
onChange,
}: {
projectId: string;
settings: Record<string, unknown>;
locked: boolean;
onChange: (selection: { participants: Participant[]; baseline: string }) => void;
}) {
const options = useQuery({
enabled: !locked,
queryFn: async (): Promise<Omit<Participant, "key">[]> => {
const catalogue = api.models.modelsCatalogRetrieve();
const checkpoints: Omit<Participant, "key">[] = [];
const completed = async () => {
for (let page = 1; ; page += 1) {
const jobs = await api.finetuningJobs.finetuningJobsList({
ordering: "-created_at",
page,
pageSize: 100,
project: projectId,
status: "succeeded",
});
for (const job of jobs.results) {
if (job.trainingContract?.evaluation === "chat_generation") {
checkpoints.push({
job: job.id,
kind: "trained",
model: job.baseModel,
name: job.name || job.baseModel,
});
}
}
if (!jobs.next) return checkpoints;
}
};
const [external, trained] = await Promise.all([catalogue, completed()]);
return [
...external.models.map((entry) => ({
kind: "external" as const,
model: entry.id,
name: entry.name || entry.id,
})),
...trained,
];
},
queryKey: ["benchmark-models", projectId],
});
const participants = Array.isArray(settings.participants)
? (settings.participants as Participant[])
: [];
const available = (options.data ?? []).filter(
(option) =>
!participants.some(
(participant) =>
participant.kind === option.kind &&
(option.job ? participant.job === option.job : participant.model === option.model)
)
);
const identify = (option: Omit<Participant, "key">) => option.job || option.model || "";
const update = (next: Participant[]) =>
onChange({
baseline: next.some(({ key }) => key === settings.baseline)
? String(settings.baseline)
: (next[0]?.key ?? ""),
participants: next,
});
return (
<InspectorField label="Benchmark models">
<div className="space-y-1.5">
{participants.map((participant) => (
<div className="flex items-center gap-2 text-xs" key={participant.key}>
<span className="min-w-0 flex-1 truncate">
{typeof participant.name === "string" ? participant.name : "New OpenRouter model"}
</span>
<span className="shrink-0 text-muted-foreground">
{participant.kind === "trained"
? "Fine-tuned"
: participant.kind === "external"
? "OpenRouter"
: "Unavailable"}
</span>
{!locked && (
<Button
aria-label={`Remove ${participant.name}`}
onClick={() => update(participants.filter(({ key }) => key !== participant.key))}
size="icon-xs"
variant="ghost"
>
<Icon.close />
</Button>
)}
</div>
))}
{!locked && participants.length < 8 && (
<SearchableSelect
ariaLabel="Add benchmark model"
emptyMessage={
options.isError ? "Could not load benchmark models." : "No models available."
}
isLoading={options.isPending}
onChange={(id) => {
const match = available.find((option) => identify(option) === id);
if (match)
update([...participants, { ...match, key: `model-${crypto.randomUUID()}` }]);
}}
options={available.map((option) => ({
label: option.name,
tag: option.kind === "trained" ? "Fine-tuned" : "OpenRouter",
value: identify(option),
}))}
placeholder="Add model"
renderOption={(option) => {
const model = available.find((entry) => identify(entry) === option.value)?.model;
return model ? <ModelOptionLabel model={model} name={option.label} /> : option.label;
}}
searchPlaceholder="Search benchmark models…"
triggerClassName={inspectorControl}
value=""
/>
)}
{participants.length > 1 && (
<SearchableSelect
ariaLabel="Benchmark baseline"
disabled={locked}
onChange={(baseline) => onChange({ baseline, participants })}
options={participants.map(({ key, name }) => ({
label: typeof name === "string" ? name : "New OpenRouter model",
value: key,
}))}
placeholder="Baseline"
triggerClassName={inspectorControl}
value={String(settings.baseline || participants[0].key)}
/>
)}
</div>
</InspectorField>
);
}