Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

8.6 KB
Raw Blame
/* eslint-disable no-nested-ternary */
import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
import * as React from "react";
import { EvalTemplateForm } from "@/src/features/evals/components/EvalTemplateForm";
import { api } from "@/src/utils/api";
import { type EvalTemplate } from "@langfuse/shared";
import { useRouter } from "next/router";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
import Page from "@/src/components/layouts/Page";
import { Switch } from "@/src/components/design-system/Switch/Switch";
import { Command } from "@/src/components/ui/command";
import { Badge } from "@/src/components/design-system/Badge/Badge";
import { StatusBadge } from "@/src/components/ui/StatusBadge/StatusBadge";
import {
  SidePanel,
  SidePanelContent,
  SidePanelHeader,
  SidePanelTitle,
} from "@/src/components/ui/side-panel";
import { LangfuseIcon } from "@/src/components/design-system/LangfuseIcon/LangfuseIcon";
import { DeleteEvalTemplateDialog } from "@/src/features/evals/components/DeleteEvalTemplateDialog";
import { IconOnlyButton } from "@/src/components/IconOnlyButton";
import { TrashIcon } from "@langfuse/icons";

export const EvalTemplateDetail = () => {
  const router = useRouter();
  const projectId = router.query.projectId as string;
  const templateId = router.query.id as string;
  const mode = router.query.mode;
  const isEditing = mode === "edit";
  const [isDeleteDialogOpen, setIsDeleteDialogOpen] = React.useState(false);
  const capture = usePostHogClientCapture();
  const hasDeleteAccess = useHasProjectAccess({
    projectId,
    scope: "evaluator:CUD",
  });

  // get the current template by id
  const template = api.evals.templateById.useQuery({
    projectId: projectId,
    id: templateId,
  });

  // get all templates for the current template name
  const allTemplates = api.evals.allTemplatesForName.useQuery(
    {
      projectId: projectId,
      name: template.data?.name ?? "",
      isUserManaged: template.data?.projectId !== null,
    },
    {
      enabled:
        !template.isPending &&
        !template.isError &&
        template.data?.name !== undefined,
    },
  );

  const handleTemplateSelect = (newTemplate: EvalTemplate) => {
    // Update URL without full page reload
    router.push(
      `/project/${projectId}/evals/templates/${newTemplate.id}`,
      undefined,
      { shallow: true },
    );
  };

  const setIsEditing = (nextIsEditing: boolean) => {
    if (!router.isReady) {
      return;
    }

    const nextQuery = { ...router.query };

    if (nextIsEditing) {
      nextQuery.mode = "edit";
    } else {
      delete nextQuery.mode;
    }

    router.replace(
      {
        pathname: router.pathname,
        query: nextQuery,
      },
      undefined,
      { shallow: true },
    );
  };

  return (
    <Page
      headerProps={{
        title: `${template.data?.name ?? ""}`,
        itemType: "EVALUATOR",
        breadcrumb: [
          {
            name: "Evaluator Library",
            href: `/project/${router.query.projectId as string}/evals/templates`,
          },
        ],
        actionButtonsRight: (
          <>
            <UpdateTemplate
              projectId={projectId}
              isEditing={isEditing}
              setIsEditing={setIsEditing}
              isCustom={!!template.data?.projectId}
            />

            {template.data?.projectId ? (
              <>
                <IconOnlyButton
                  icon={<TrashIcon size="base" color="default" />}
                  label="Delete"
                  aria-label="delete"
                  variant="outline"
                  size="icon"
                  disabledReason={
                    hasDeleteAccess
                      ? undefined
                      : "You don't have permission to delete this evaluator."
                  }
                  onClick={() => {
                    capture("eval_templates:delete_form_open", {
                      source: "template",
                    });
                    setIsDeleteDialogOpen(true);
                  }}
                />
                <DeleteEvalTemplateDialog
                  projectId={projectId}
                  templateId={templateId}
                  templateName={template.data.name}
                  open={isDeleteDialogOpen}
                  onOpenChange={setIsDeleteDialogOpen}
                  onSuccess={() => {
                    capture("eval_templates:delete_template_button_click", {
                      source: "template",
                    });
                    router.push(`/project/${projectId}/evals/templates`);
                  }}
                />
              </>
            ) : null}
          </>
        ),
      }}
    >
      {allTemplates.isLoading || !allTemplates.data || !template.data ? (
        <div className="p-3">Loading...</div>
      ) : isEditing ? (
        <div className="overflow-y-auto p-3 pt-1">
          <EvalTemplateForm
            useDialog={false}
            projectId={projectId}
            existingEvalTemplate={template.data}
            isEditing={isEditing}
            setIsEditing={setIsEditing}
          />
        </div>
      ) : (
        <div className="grid flex-1 grid-cols-[1fr_auto] overflow-hidden contain-layout">
          <div className="flex max-h-full min-h-0 flex-col overflow-y-auto px-3 pt-1">
            <EvalTemplateForm
              useDialog={false}
              projectId={projectId}
              existingEvalTemplate={template.data}
              isEditing={isEditing}
              setIsEditing={setIsEditing}
            />
          </div>
          <SidePanel mobileTitle="Change history" id="change-history">
            <SidePanelHeader>
              <SidePanelTitle className="text-base font-bold">
                Change history
              </SidePanelTitle>
            </SidePanelHeader>
            <SidePanelContent>
              <Command className="flex flex-col gap-2 overflow-y-auto rounded-none font-bold focus:ring-0 focus:outline-hidden focus-visible:ring-0 focus-visible:ring-offset-0 focus-visible:outline-hidden data-focus:ring-0">
                <div className="flex flex-col overflow-y-auto">
                  {allTemplates.data.templates.map((template, index) => (
                    <div
                      key={template.id}
                      className={`hover:bg-accent flex cursor-pointer flex-col rounded-md px-2 py-1.5 ${
                        template.id === templateId ? "bg-accent" : ""
                      }`}
                      onClick={() => handleTemplateSelect(template)}
                    >
                      <div className="flex items-center justify-between">
                        <div className="flex items-center gap-1">
                          <Badge
                            onClick={(e) => {
                              e.stopPropagation();
                            }}
                            color="primary"
                            size="lg"
                            text={`# ${template.version}`}
                            data-version-trigger="false"
                          />
                          {index === 0 && (
                            <StatusBadge type="active" key="active" />
                          )}
                        </div>
                        <span className="text-muted-foreground text-xs">
                          {template.createdAt.toLocaleDateString()}
                        </span>
                      </div>
                    </div>
                  ))}
                </div>
              </Command>
            </SidePanelContent>
          </SidePanel>
        </div>
      )}
    </Page>
  );
};

function UpdateTemplate({
  projectId,
  isEditing,
  setIsEditing,
  isCustom,
}: {
  projectId: string;
  isEditing: boolean;
  setIsEditing: (isEditing: boolean) => void;
  isCustom: boolean;
}) {
  const hasAccess = useHasProjectAccess({
    projectId,
    scope: "evaluator:CUD",
  });
  const capture = usePostHogClientCapture();

  const handlePromptEdit = (checked: boolean) => {
    setIsEditing(checked);
    if (checked) capture("eval_templates:update_form_open");
  };

  if (!isCustom) {
    return (
      <div className="flex items-center gap-2">
        <LangfuseIcon size={16} />
        <span className="text-muted-foreground text-sm font-bold">
          View only
        </span>
      </div>
    );
  }

  return (
    <div className="flex items-center gap-2">
      <span className="text-sm font-bold">Edit mode</span>
      <Switch
        checked={isEditing}
        onCheckedChange={handlePromptEdit}
        disabled={!hasAccess}
      />
    </div>
  );
}