Skip to content

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

7.8 KB
Raw Blame
import { useRouter } from "next/router";
import { ActionButton } from "@/src/components/ActionButton";
import Page from "@/src/components/layouts/Page";
import { PromptTable } from "@/src/features/prompts/components/PromptTable";
import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
import { Download, UploadIcon, PlusIcon } from "@langfuse/icons";
import { api } from "@/src/utils/api";
import { PromptsOnboarding } from "@/src/components/onboarding/PromptsOnboarding";
import { useEntitlementLimit } from "@/src/features/entitlements/hooks";
import { PromptDetail } from "@/src/features/prompts/components/PromptDetail";
import PromptMetrics from "@/src/features/prompts/PromptMetricsPage";
import { useQueryParams, StringParam } from "use-query-params";
import { useState } from "react";
import { AutomationButton } from "@/src/features/automations/components/AutomationButton";
import { ImportPromptsButtonDialogController } from "@/src/features/prompts/components/ImportPromptsButtonDialogController";
import { Button } from "@/src/components/ui/button";
import { DropdownMenu } from "@/src/components/design-system/DropdownMenu/DropdownMenu";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
import { showToast } from "@/src/features/notifications/showToast";
import { classifyTrpcToastError } from "@/src/utils/trpcErrorClassification";

export default function PromptsPage() {
  const router = useRouter();
  const projectId = router.query.projectId as string;
  const routeSegments = router.query.folder;
  const [queryParams] = useQueryParams({ folder: StringParam });
  const folderQueryParam = queryParams.folder || "";

  // Determine view type based on route segments
  // NOTE: there is a bug here, that if the user directly accesses a prompt name which ends in `/metrics`,
  // the prompt metrics page will be shown for a non-existing prompt name. Doesn't happen if the user clicks
  // this in the UI (we URL encode here and don't strip metrics). We could resolve this with another API call
  // to check the prompt name existence.
  const segmentsArray = Array.isArray(routeSegments) ? routeSegments : [];
  const isMetricsPage =
    segmentsArray.length > 0 &&
    segmentsArray[segmentsArray.length - 1] === "metrics";
  const promptNameFromRoute = (() => {
    if (segmentsArray.length > 0) {
      if (isMetricsPage) {
        return segmentsArray.slice(0, -1).join("/");
      }
      return segmentsArray.join("/");
    }
    return "";
  })();

  const hasCUDAccess = useHasProjectAccess({
    projectId,
    scope: "prompts:CUD",
  });
  const hasReadAccess = useHasProjectAccess({
    projectId,
    scope: "prompts:read",
  });
  const promptLimit = useEntitlementLimit("prompt-management-count-prompts");
  const utils = api.useUtils();
  const [isExporting, setIsExporting] = useState(false);
  const capture = usePostHogClientCapture();

  const handleExport = async (mode: "latest" | "all") => {
    setIsExporting(true);
    try {
      const data = await utils.prompts.exportAll.fetch({
        projectId,
        includeAllVersions: mode === "all",
      });
      const json = JSON.stringify(data, null, 2);
      const blob = new Blob([json], { type: "application/json" });
      const url = URL.createObjectURL(blob);
      const a = document.createElement("a");
      a.href = url;
      a.download = `langfuse-prompts-${mode}-${new Date().toISOString().slice(0, 10)}.json`;
      a.click();
      URL.revokeObjectURL(url);
      capture("prompts:bulk_export", { mode });
    } catch (error) {
      showToast({
        type: "ERROR",
        title: "Failed to export prompts. Please try again.",
        analytics: classifyTrpcToastError(error, "prompt.export"),
      });
    } finally {
      setIsExporting(false);
    }
  };

  // Check if the project has any prompts
  const { data: hasAnyPrompt, isLoading } = api.prompts.hasAny.useQuery(
    { projectId },
    {
      enabled: !!projectId,
      trpc: {
        context: {
          skipBatch: true,
        },
      },
    },
  );

  const { data: count } = api.prompts.count.useQuery(
    { projectId },
    {
      enabled: !!projectId && !promptNameFromRoute, // Only count when on folder view
      trpc: {
        context: {
          skipBatch: true,
        },
      },
    },
  );

  const showOnboarding = !isLoading && !hasAnyPrompt;

  // Decide what to render: metrics, detail, or folder view
  if (promptNameFromRoute.length > 0) {
    if (isMetricsPage) {
      return <PromptMetrics promptName={promptNameFromRoute} />;
    }
    return <PromptDetail promptName={promptNameFromRoute} />;
  }

  return (
    <Page
      headerProps={{
        title: "Prompts",
        help: {
          description:
            "Manage and version your prompts in Langfuse. Edit and update them via the UI and SDK. Retrieve the production version via the SDKs. Learn more in the docs.",
          href: "https://langfuse.com/docs/prompt-management/get-started",
        },
        actionButtonsRight: (
          <>
            {projectId && <AutomationButton projectId={projectId} />}
            {hasReadAccess && (
              <DropdownMenu
                disabled={isExporting}
                placement="bottom-end"
                items={[
                  {
                    type: "item",
                    id: "latest",
                    title: "Latest version per prompt",
                    onClick: () => handleExport("latest"),
                  },
                  {
                    type: "item",
                    id: "all",
                    title: "All versions",
                    onClick: () => handleExport("all"),
                  },
                ]}
              >
                {({ getTriggerProps }) => (
                  <Button
                    variant="outline"
                    disabled={isExporting}
                    {...getTriggerProps()}
                  >
                    <UploadIcon
                      size="base"
                      color="default"
                      __rawClassName="mr-1"
                    />
                    {isExporting ? "Exporting…" : "Export"}
                  </Button>
                )}
              </DropdownMenu>
            )}
            {projectId && (
              <ImportPromptsButtonDialogController projectId={projectId}>
                {({ disabled, openDialog }) => (
                  <Button
                    variant="outline"
                    disabled={Boolean(disabled)}
                    title={disabled?.reason}
                    onClick={openDialog}
                  >
                    <Download
                      size="base"
                      color="default"
                      __rawClassName="mr-1"
                    />
                    Import
                  </Button>
                )}
              </ImportPromptsButtonDialogController>
            )}
            <ActionButton
              icon={<PlusIcon size="base" color="default" aria-hidden="true" />}
              hasAccess={hasCUDAccess}
              href={`/project/${projectId}/prompts/new${folderQueryParam ? `?folder=${encodeURIComponent(folderQueryParam)}` : ""}`}
              trackingEventName="prompts:new_form_open"
              variant="default"
              usageLimit={
                typeof promptLimit === "number"
                  ? {
                      current: Number(count?.totalCount ?? 0),
                      max: promptLimit,
                    }
                  : undefined
              }
            >
              New prompt
            </ActionButton>
          </>
        ),
      }}
      scrollable={showOnboarding}
    >
      {/* Show onboarding screen if project has no prompts */}
      {showOnboarding ? (
        <PromptsOnboarding projectId={projectId} />
      ) : (
        <PromptTable key={folderQueryParam} />
      )}
    </Page>
  );
}