All files / src/apps/user/pages/evaluation CreateExperimentModal.tsx

46.42% Statements 13/28
25% Branches 2/8
12.5% Functions 1/8
48.14% Lines 13/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140                                                              2x             1x 1x 1x 1x 1x 1x 1x 1x 1x   1x                                                           1x   1x                                                                                                                      
/**
 * Create an experiment on a dataset: a name, and how it grades.
 *
 * An experiment is a child of its dataset — it is created from the dataset's
 * page and asks that dataset for life — so the dataset is given, never
 * picked. Running something new on the same questions is another experiment
 * here, not a copy of an old one.
 */
 
import {
  EvaluationScorer,
  EvaluationTarget,
  TestCase,
  TestSuite,
} from "@/objects/Evaluation";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { errorDetail } from "@/util/apiError";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { FlaskConical } from "lucide-react";
import React, { useState } from "react";
import { useNavigate } from "react-router-dom";
import { Modal, ModalActions } from "@/components/detail";
import {
  ErrorNote,
  Field,
  ScorerPicker,
  experimentHref,
  inputCls,
} from "./components";
 
const CreateExperimentModal: React.FC<{
  /** The dataset the experiment will ask, for life. */
  dataset: TestSuite;
  /** Its cases, so the scorer picker can say what they lack before a run. */
  cases?: TestCase[];
  onClose: () => void;
}> = ({ dataset, cases, onClose }) => {
  const { getApiHandler } = useApiHandler();
  const queryClient = useQueryClient();
  const navigate = useNavigate();
  const [name, setName] = useState("");
  const [description, setDescription] = useState("");
  const [scorer, setScorer] = useState<EvaluationScorer>("manual");
  const [scorerConfig, setScorerConfig] = useState<Record<string, unknown>>({});
  const [judge, setJudge] = useState<EvaluationTarget | null>(null);
  const [error, setError] = useState<string | null>(null);
 
  const create = useMutation({
    mutationFn: async () => {
      const created = await getApiHandler().evaluation_router.createExperiment({
        name: name.trim(),
        description: description.trim() || null,
        dataset_id: dataset.id,
        scorer,
        scorer_config: scorerConfig,
        judge,
      });
      // A create that answers without an id is a failure whatever the
      // status said; navigating on it would open `/id/undefined`.
      if (!created) throw new Error("Could not create the experiment.");
      return created;
    },
    onSuccess: (created) => {
      void queryClient.invalidateQueries({
        queryKey: [QUERY_KEYS.GET.EXPERIMENTS],
      });
      void queryClient.invalidateQueries({
        queryKey: [QUERY_KEYS.GET.DATASET_EVALUATIONS, dataset.id],
      });
      onClose();
      // Straight into the workbench: variants to add before it can run.
      void navigate(experimentHref(created.id));
    },
    onError: (e) =>
      setError(errorDetail(e, "Could not create the experiment.")),
  });
 
  const canCreate = !!name.trim() && !create.isPending;
 
  return (
    <Modal
      title="New experiment"
      icon={FlaskConical}
      size="lg"
      busy={create.isPending}
      onClose={onClose}
      description={`Runs every question in ${dataset.name}, grades every answer the same way, and compares the models, agents or settings you add as variants. Re-run it after a change to see what moved.`}
      footer={
        <ModalActions
          onCancel={onClose}
          onConfirm={() => {
            setError(null);
            create.mutate();
          }}
          confirmLabel="Create"
          loadingLabel="Creating…"
          loading={create.isPending}
          disabled={!canCreate}
        />
      }
    >
      <div className="space-y-4">
        <Field label="Name">
          <input
            autoFocus
            className={inputCls}
            placeholder="e.g. Refund tone, with and without policy knowledge"
            value={name}
            onChange={(e) => setName(e.target.value)}
          />
        </Field>
        <Field label="Description" hint="optional">
          <textarea
            rows={2}
            className={`${inputCls} resize-none`}
            placeholder="What are you comparing?"
            value={description}
            onChange={(e) => setDescription(e.target.value)}
          />
        </Field>
        <ScorerPicker
          scorer={scorer}
          config={scorerConfig}
          onChange={(kind, config) => {
            setScorer(kind);
            setScorerConfig(config);
          }}
          judge={judge}
          onJudgeChange={setJudge}
          cases={cases}
        />
        {error && <ErrorNote message={error} />}
      </div>
    </Modal>
  );
};
 
export default CreateExperimentModal;