"use client"; import { useEffect, useState } from "react"; import toast from "react-hot-toast"; import { ProviderSelector, type ProviderSelection } from "@/components/ProviderSelector"; import { PromptPackSelector } from "@/components/PromptPackSelector"; import { ResultsTable } from "@/components/ResultsTable"; import { runBenchmark } from "@/lib/benchmarkRunner"; import { DEFAULT_PROMPT_PACKS } from "@/lib/prompts"; import { PROVIDER_PRESETS } from "@/lib/providers"; import type { BenchmarkRun } from "@/lib/types"; import { downloadTextFile } from "@/lib/utils"; function toCsv(run: BenchmarkRun) { const header = ["prompt_id", "category", "title", "score", "refused", "latency_ms"].join(","); const lines = run.items.map((i) => [ i.promptId, i.category, JSON.stringify(i.title), i.score, i.refused ? 1 : 0, i.latencyMs ].join(",") ); return [header, ...lines].join("\n"); } function sharePayload(run: BenchmarkRun) { const { items: _items, ...payload } = run; return payload; } export default function BenchmarkPage() { const [providerSelection, setProviderSelection] = useState(() => { const preset = PROVIDER_PRESETS.find((p) => p.id === "ollama") ?? PROVIDER_PRESETS[0]!; return { providerId: preset.id, baseUrl: preset.baseUrl, model: preset.defaultModel ?? "", apiKey: "" }; }); const [promptPack, setPromptPack] = useState(DEFAULT_PROMPT_PACKS[0]!); const [isRunning, setIsRunning] = useState(false); const [progress, setProgress] = useState<{ completed: number; total: number; label: string } | null>(null); const [run, setRun] = useState(null); const [isSharing, setIsSharing] = useState(false); const [dbEnabled, setDbEnabled] = useState(null); const preset = PROVIDER_PRESETS.find((p) => p.id === providerSelection.providerId) ?? PROVIDER_PRESETS[0]!; useEffect(() => { let cancelled = false; (async () => { try { const res = await fetch("/api/leaderboard", { cache: "no-store" }); const json = (await res.json()) as { dbEnabled?: boolean }; if (cancelled) return; setDbEnabled(Boolean(json.dbEnabled)); } catch { if (cancelled) return; setDbEnabled(null); } })(); return () => { cancelled = true; }; }, []); async function onRun() { if (!providerSelection.baseUrl.trim()) { toast.error("Base URL is required."); return; } if (!providerSelection.model.trim()) { toast.error("Model is required."); return; } setIsRunning(true); setRun(null); setProgress({ completed: 0, total: promptPack.prompts.length, label: "Starting…" }); try { const result = await runBenchmark({ provider: preset, baseUrl: providerSelection.baseUrl.trim(), model: providerSelection.model.trim(), apiKey: providerSelection.apiKey || undefined, promptPack, onProgress: ({ completed, total, prompt }) => setProgress({ completed, total, label: `${prompt.category.toUpperCase()}: ${prompt.title}` }) }); setRun(result); toast.success("Benchmark complete."); } catch (error) { toast.error(error instanceof Error ? error.message : "Benchmark failed."); } finally { setIsRunning(false); setProgress(null); } } async function onShare() { if (!run) return; setIsSharing(true); try { const res = await fetch("/api/submit", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(sharePayload(run)) }); const text = await res.text(); if (!res.ok) throw new Error(text || `Upload failed (${res.status})`); toast.success("Shared anonymously."); } catch (error) { toast.error(error instanceof Error ? error.message : "Share failed."); } finally { setIsSharing(false); } } return (

Benchmark

Runs client-side. API keys are never sent to OCLA server routes.

{dbEnabled === false ? (
Uploads/leaderboard are disabled (missing DATABASE_URL). Your results stay local unless you configure Postgres.
) : null}
{run ? ( <> ) : null}
{progress ? (
{progress.label}
{progress.completed}/{progress.total}
) : null} {run ? (
Overall avg score
{run.summary.totals.avgScore}
Overall refusal rate
{Math.round(run.summary.totals.refusalRate * 100)}%
Prompts
{run.summary.totals.promptCount}
) : null}
CORS note

Some hosted providers block browser requests. For local models (Ollama / LM Studio), enable CORS on your server.

); }