Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

11.0 KB
Raw Blame
/**
 * The manual New Benchmark form: title, id, description, runs per case and a list of cases, each
 * a statement and a rubric. No Agent is chosen here — a Benchmark belongs to the Project and
 * records the Agent it tested on each evaluation instead. The server writes the on-disk layout;
 * the form keeps a first-timer on the rails — format hints stay visible while typing, and the
 * semantics (what a rubric is, why it never reaches the tested agent, what makes one
 * discriminating) sit behind the "?" marks. Mounted fresh on every open.
 */
import { useRef, useState } from "react";
import type { BenchmarkSummary } from "@adelie/server/api";
import {
  Button,
  FieldLabel,
  GlyphIcon,
  ICONS,
  ICON_SIZE,
  InfoPopover,
  Input,
  Modal,
  PlusIcon,
  Textarea,
  toastSuccess,
} from "@adelie/ui";
import * as api from "../../api/endpoints";
import { ApiError } from "../../api/client";
import { S } from "../../lib/strings";
import { apiErrorText } from "../../lib/api-error";
import { toneInk } from "../../lib/tone";
import { SemanticIdField } from "../semantic-id/semantic-id-field";
import { ID_PATTERN, caseId, isValidRuns } from "./benchmark-prompts";

interface CaseDraft {
  /** Stable React key, independent of position, so removing a case keeps the others' state. */
  key: number;
  slug: string;
  title: string;
  statement: string;
  rubric: string;
}

type CaseErrors = Partial<Record<"slug" | "title" | "statement" | "rubric", string>>;

interface FormErrors {
  id?: string;
  title?: string;
  runs?: string;
  /** Keyed by CaseDraft.key, not by position: removing a case must not move another one's errors onto it. */
  cases: Record<number, CaseErrors>;
}

export interface CreateBenchmarkModalProps {
  open: boolean;
  onClose: () => void;
  projectId: string;
  onCreated: (benchmark: BenchmarkSummary) => void;
}

export function CreateBenchmarkModal(props: CreateBenchmarkModalProps) {
  return props.open ? <CreateBenchmarkDialog {...props} /> : null;
}

const digits = (v: string) => v.replace(/[^\d]/g, "");
/** Spreads an error message into a field only when there is one (an explicit undefined is not an absent prop). */
const errorProp = (message: string | undefined) =>
  message !== undefined ? { error: message } : {};

function CreateBenchmarkDialog({ onClose, projectId, onCreated }: CreateBenchmarkModalProps) {
  const keyRef = useRef(0);
  const newCase = (): CaseDraft => ({
    key: keyRef.current++,
    slug: "",
    title: "",
    statement: "",
    rubric: "",
  });
  const [title, setTitle] = useState("");
  const [id, setId] = useState("");
  const [description, setDescription] = useState("");
  const [runs, setRuns] = useState("1");
  const [cases, setCases] = useState<CaseDraft[]>(() => [newCase()]);
  const [errors, setErrors] = useState<FormErrors>({ cases: {} });
  const [busy, setBusy] = useState(false);
  const [submitError, setSubmitError] = useState<string | null>(null);

  const updateCase = (key: number, patch: Partial<CaseDraft>) =>
    setCases((prev) => prev.map((c) => (c.key === key ? { ...c, ...patch } : c)));

  const validate = (): FormErrors => {
    const next: FormErrors = { cases: {} };
    if (title.trim() === "") next.title = S.common.requiredField;
    if (id.trim() === "") next.id = S.common.requiredField;
    else if (!ID_PATTERN.test(id.trim())) next.id = S.benchmark.invalidId;
    if (!isValidRuns(runs)) next.runs = S.benchmark.invalidRuns;
    for (const c of cases) {
      const e: CaseErrors = {};
      if (c.slug.trim() === "") e.slug = S.common.requiredField;
      else if (!ID_PATTERN.test(c.slug.trim())) e.slug = S.benchmark.invalidId;
      if (c.title.trim() === "") e.title = S.common.requiredField;
      if (c.statement.trim() === "") e.statement = S.common.requiredField;
      if (c.rubric.trim() === "") e.rubric = S.common.requiredField;
      next.cases[c.key] = e;
    }
    return next;
  };
  const hasErrors = (e: FormErrors) =>
    e.id !== undefined ||
    e.title !== undefined ||
    e.runs !== undefined ||
    Object.values(e.cases).some((c) => Object.keys(c).length > 0);

  const submit = async () => {
    const next = validate();
    setErrors(next);
    if (hasErrors(next)) return;
    setBusy(true);
    setSubmitError(null);
    try {
      const trimmedDescription = description.trim();
      const res = await api.createBenchmark(projectId, {
        id: id.trim(),
        title: title.trim(),
        ...(trimmedDescription !== "" ? { description: trimmedDescription } : {}),
        runs: Number.parseInt(runs, 10),
        cases: cases.map((c, i) => ({
          id: caseId(i + 1, c.slug.trim()),
          title: c.title.trim(),
          statement: c.statement,
          rubric: c.rubric,
        })),
      });
      toastSuccess(S.benchmark.created);
      onCreated(res.benchmark);
      onClose();
    } catch (e) {
      if (e instanceof ApiError && e.status === 409) {
        setErrors((prev) => ({ ...prev, id: S.benchmark.idExists }));
      } else {
        setSubmitError(apiErrorText(e));
      }
    } finally {
      setBusy(false);
    }
  };

  return (
    <Modal
      open
      title={S.benchmark.manualCreateTitle}
      onClose={onClose}
      widthClass="sm:max-w-2xl"
      footer={
        <>
          <Button size="sm" onClick={onClose} disabled={busy}>
            {S.common.cancel}
          </Button>
          <Button size="sm" variant="primary" disabled={busy} onClick={() => void submit()}>
            {busy ? S.common.saving : S.benchmark.createSubmit}
          </Button>
        </>
      }
    >
      <div className="space-y-4">
        <p className="text-sm text-gray-600 dark:text-gray-300">{S.benchmark.manualCreateIntro}</p>
        {/* The title comes first and the id is derived from it on request — by the model, so
            a Chinese title gets English words — never rewritten while the title is typed. */}
        <Input
          label={S.benchmark.titleField}
          required
          value={title}
          onChange={(e) => setTitle(e.target.value)}
          {...errorProp(errors.title)}
        />
        <SemanticIdField
          projectId={projectId}
          kind="benchmark"
          label={S.benchmark.idField}
          hint={S.benchmark.idHint}
          generateHint={S.benchmark.idGenerateHint}
          value={id}
          source={title.trim() || description}
          error={errors.id}
          disabled={busy}
          onChange={(next) => {
            setId(next);
            setErrors((prev) => ({ ...prev, id: undefined }));
          }}
        />
        <Textarea
          label={S.benchmark.descriptionField}
          hint={S.benchmark.descriptionHint}
          rows={2}
          value={description}
          onChange={(e) => setDescription(e.target.value)}
        />
        <Input
          label={S.benchmark.runsField}
          hint={S.benchmark.runsHint}
          info={S.benchmark.runsInfo}
          infoLabel={S.benchmark.runsField}
          inputMode="numeric"
          value={runs}
          onChange={(e) => setRuns(digits(e.target.value))}
          {...errorProp(errors.runs)}
        />

        <div>
          <div className="mb-2 flex items-center gap-1">
            <FieldLabel block={false}>{S.benchmark.casesTitle}</FieldLabel>
            <InfoPopover label={S.benchmark.casesTitle}>{S.benchmark.casesInfo}</InfoPopover>
          </div>
          <div className="space-y-3">
            {cases.map((c, i) => {
              const e = errors.cases[c.key] ?? {};
              const dirName = caseId(i + 1, c.slug.trim() || "<slug>");
              return (
                <div
                  key={c.key}
                  className="rounded-md border border-gray-200 p-3 dark:border-gray-800"
                >
                  <div className="mb-2 flex items-center gap-2">
                    <span className="text-xs font-semibold text-gray-600 dark:text-gray-300">
                      {S.benchmark.caseHeading(i + 1)}
                    </span>
                    <span className="min-w-0 flex-1 truncate font-mono text-xs text-gray-400">
                      {dirName}
                    </span>
                    {cases.length > 1 && (
                      <Button
                        size="icon"
                        variant="ghost"
                        title={S.benchmark.removeCase}
                        aria-label={S.benchmark.removeCase}
                        onClick={() => setCases((prev) => prev.filter((x) => x.key !== c.key))}
                      >
                        <GlyphIcon d={ICONS.trash} size={ICON_SIZE.iconButton} />
                      </Button>
                    )}
                  </div>
                  <div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
                    <Input
                      size="sm"
                      label={S.benchmark.caseSlugField}
                      required
                      hint={S.benchmark.caseSlugHint(dirName)}
                      value={c.slug}
                      className="font-mono"
                      onChange={(event) => updateCase(c.key, { slug: event.target.value })}
                      {...errorProp(e.slug)}
                    />
                    <Input
                      size="sm"
                      label={S.benchmark.caseTitleField}
                      required
                      value={c.title}
                      onChange={(event) => updateCase(c.key, { title: event.target.value })}
                      {...errorProp(e.title)}
                    />
                  </div>
                  <div className="mt-3">
                    <Textarea
                      size="sm"
                      label={S.benchmark.caseStatementField}
                      required
                      hint={S.benchmark.caseStatementHint}
                      rows={4}
                      value={c.statement}
                      onChange={(event) => updateCase(c.key, { statement: event.target.value })}
                      {...errorProp(e.statement)}
                    />
                  </div>
                  <div className="mt-3">
                    <Textarea
                      size="sm"
                      label={S.benchmark.caseRubricField}
                      required
                      hint={S.benchmark.caseRubricHint}
                      info={S.benchmark.rubricInfo}
                      infoLabel={S.benchmark.caseRubricField}
                      rows={4}
                      value={c.rubric}
                      onChange={(event) => updateCase(c.key, { rubric: event.target.value })}
                      {...errorProp(e.rubric)}
                    />
                  </div>
                </div>
              );
            })}
          </div>
          <Button
            size="sm"
            className="mt-2"
            onClick={() => setCases((prev) => [...prev, newCase()])}
          >
            <PlusIcon />
            {S.benchmark.addCase}
          </Button>
        </div>
        {submitError !== null && <p className={`text-xs ${toneInk.danger}`}>{submitError}</p>}
      </div>
    </Modal>
  );
}