Skip to content

The platform for continuously improving AI agents.

1.6 KB
Raw Blame
import { useMutation, useQueryClient } from "@tanstack/react-query";

import apiClient from "@/client";
import { Button } from "@/components/ui/button";
import { ConfirmDialog } from "@/components/ui/confirm-dialog";
import { Icon } from "@/components/ui/icons";
import type { NativeEvaluation } from "@/openapi";

const NOT_CANCELLABLE = new Set(["draft", "prepared", "completed", "cancelling", "cancelled"]);

/** Ends an evaluation for good; pause is the way to stop and continue later. */
export function CancelEvaluation({ plan }: { plan: NativeEvaluation }) {
  const queryClient = useQueryClient();
  const cancel = useMutation({
    mutationFn: () => apiClient.nativeEvaluations.nativeEvaluationsCancelCreate({ id: plan.id }),
    onSuccess: (next) => {
      queryClient.setQueryData(["decision-comparison", plan.id], next);
      return queryClient.invalidateQueries({ queryKey: ["eval-runs"] });
    },
  });
  if (plan.deletedAt || NOT_CANCELLABLE.has(plan.state)) return null;
  return (
    <ConfirmDialog
      cancelLabel="Keep running"
      confirmLabel="Cancel evaluation"
      description="Its provider calls stop and completed results stay available. A cancelled evaluation cannot resume."
      destructive
      error={cancel.error}
      isPending={cancel.isPending}
      keepOpenOnError
      onConfirm={async () => {
        await cancel.mutateAsync();
      }}
      title="Cancel this evaluation?"
      trigger={
        <Button className="ml-auto" size="sm" variant="secondary">
          <Icon.close />
          Cancel
        </Button>
      }
    />
  );
}