Skip to content

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

3.3 KB
Raw Blame
/* eslint-disable @typescript-eslint/no-deprecated -- Legacy UI usage retained during migration to design-system components. */
import { Webhook } from "@langfuse/icons";

import { Button } from "@/src/components/ui/button";
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from "@/src/components/ui/tooltip";
import { showErrorToast } from "@/src/features/notifications/showErrorToast";
import { showSuccessToast } from "@/src/features/notifications/showSuccessToast";
import { api } from "@/src/utils/api";
import { classifyTrpcToastError } from "@/src/utils/trpcErrorClassification";

type WebCalloutTarget = {
  projectId: string;
  traceId: string | null;
  observationId?: string | null;
  sessionId?: string | null;
};

export function useWebCalloutAction(props: WebCalloutTarget, enabled: boolean) {
  const endpoint = api.webCallouts.enabled.useQuery(
    { projectId: props.projectId },
    {
      staleTime: 60_000,
      enabled,
    },
  );
  const invokeMutation = api.webCallouts.invoke.useMutation({
    onSuccess: () => {
      const callout = endpoint.data;
      if (!callout?.enabled) return;

      showSuccessToast({
        operation: "web_callout.invoke",
        title: callout.toastMessage,
        description: callout.name,
      });
    },
    onError: (error) => {
      showErrorToast(
        "Web callout failed",
        error.message,
        classifyTrpcToastError(error, "web_callout.invoke"),
      );
    },
  });

  const invokeCallout = async () => {
    const callout = endpoint.data;

    if (!callout?.enabled) {
      return;
    }

    await invokeMutation.mutateAsync({
      projectId: props.projectId,
      traceId: props.traceId,
      observationId: props.observationId ?? null,
      sessionId: props.sessionId ?? null,
    });
  };

  if (!enabled || endpoint.data?.enabled !== true) {
    return undefined;
  }

  return {
    endpointName: endpoint.data?.name ?? "Web callout",
    isLoading: invokeMutation.isPending,
    invokeCallout,
  };
}

type WebCalloutAction = NonNullable<ReturnType<typeof useWebCalloutAction>>;

export function WebCalloutButton({
  action,
  layout = "toolbar",
}: {
  action: WebCalloutAction;
  /**
   * "toolbar" (default) is the inline icon button; "menu" renders the same
   * action as a full-width labeled row for the mobile header overflow popover.
   */
  layout?: "toolbar" | "menu";
}) {
  const label = `Call ${action.endpointName}`;

  if (layout === "menu") {
    return (
      <Button
        aria-label={label}
        variant="ghost"
        size="sm"
        loading={action.isLoading}
        className="w-full justify-start gap-2 font-normal"
        onClick={() => {
          action.invokeCallout().catch(() => undefined);
        }}
      >
        <Webhook size="base" color="default" />
        <span className="min-w-0 truncate text-sm" title={label}>
          {label}
        </span>
      </Button>
    );
  }

  return (
    <Tooltip>
      <TooltipTrigger asChild>
        <Button
          aria-label={label}
          title={label}
          variant="outline"
          size="icon"
          loading={action.isLoading}
          onClick={() => {
            action.invokeCallout().catch(() => undefined);
          }}
        >
          <Webhook size="base" color="default" />
        </Button>
      </TooltipTrigger>
      <TooltipContent>{label}</TooltipContent>
    </Tooltip>
  );
}