Skip to content

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

12.3 KB
Raw Blame
/* eslint-disable no-nested-ternary */
import { useEffect } from "react";
import {
  type ObservationType,
  type ScoreDomain,
  type Prisma,
} from "@langfuse/shared";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
import useLocalStorage from "@/src/components/useLocalStorage";
import { usePreserveRelativeScroll } from "@/src/features/traces/hooks/usePreserveRelativeScroll";
import type { MediaReturnType } from "@/src/features/media/validation";
import { type ExpansionState } from "@/src/features/traces/components/AdvancedJsonViewer/types";

import { ViewModeToggle, type ViewMode } from "./components/ViewModeToggle";
import {
  DEFAULT_JSON_VIEW_PREFERENCE,
  JSON_VIEW_PREFERENCE_STORAGE_KEY,
  normalizeJsonViewPreference,
} from "@/src/components/ui/jsonViewPreference";
import { IOPreviewJSON, type IOPreviewJSONProps } from "./IOPreviewJSON";
import { IOPreviewJSONSimple } from "./IOPreviewJSONSimple";
import { IOPreviewPretty } from "./IOPreviewPretty";
import { type ChatMLParserResult } from "../../hooks/useChatMLParser";
import { Button } from "@/src/components/ui/button";
import { ActionButton } from "@/src/components/ActionButton";
import { BookOpen, X } from "@langfuse/icons";
import type { ObservationStatusMessage } from "./components/statusMessagePresentation";

export type { ViewMode };
export type IOPreviewContentMode = "all" | "conversation";

const EMPTY_IO_ALERT_ID = "empty-io";
const STORAGE_KEY = "dismissed-trace-view-notifications";

export interface ExpansionStateProps {
  // Per-field expansion state (for formatted and json views)
  inputExpansionState?: Record<string, boolean> | boolean;
  outputExpansionState?: Record<string, boolean> | boolean;
  metadataExpansionState?: Record<string, boolean> | boolean;
  onInputExpansionChange?: (
    expansion: Record<string, boolean> | boolean,
  ) => void;
  onOutputExpansionChange?: (
    expansion: Record<string, boolean> | boolean,
  ) => void;
  onMetadataExpansionChange?: (
    expansion: Record<string, boolean> | boolean,
  ) => void;
  // Combined expansion state (for advanced-json view only)
  // Paths are prefixed: "input.foo", "output.bar", "metadata.baz"
  // Input accepts ExpansionState (boolean shorthand allowed), callback receives Record (what viewer emits)
  advancedJsonExpansionState?: ExpansionState;
  onAdvancedJsonExpansionChange?: (expansion: Record<string, boolean>) => void;
  // Simple boolean expansion state (for legacy json view only)
  // true = expanded, false = collapsed
  jsonInputExpanded?: boolean;
  jsonOutputExpanded?: boolean;
  jsonMetadataExpanded?: boolean;
  onJsonInputExpandedChange?: (expanded: boolean) => void;
  onJsonOutputExpandedChange?: (expanded: boolean) => void;
  onJsonMetadataExpandedChange?: (expanded: boolean) => void;
}

export interface IOPreviewProps extends ExpansionStateProps {
  skills?: React.ComponentProps<typeof IOPreviewPretty>["skills"];
  input?: Prisma.JsonValue;
  output?: Prisma.JsonValue;
  status?: ObservationStatusMessage;
  metadata?: Prisma.JsonValue;
  outputCorrection?: ScoreDomain;
  // Pre-parsed data (optional, from useParsedObservation hook for performance)
  parsedInput?: unknown;
  parsedOutput?: unknown;
  parsedMetadata?: unknown;
  chatMLParserResult?: ChatMLParserResult;
  observationName?: string;
  observationType?: ObservationType;
  isLoading?: boolean;
  isParsing?: boolean;
  hideIfNull?: boolean;
  media?: MediaReturnType[];
  hideOutput?: boolean;
  hideInput?: boolean;
  currentView?: ViewMode;
  contentMode?: IOPreviewContentMode;
  showSystemPrompt?: boolean;
  setIsPrettyViewAvailable?: (value: boolean) => void;
  // Inline comment props (JSON Beta view only)
  enableInlineComments?: boolean;
  onAddInlineComment?: IOPreviewJSONProps["onAddInlineComment"];
  commentedPathsByField?: IOPreviewJSONProps["commentedPathsByField"];
  // Callback to inform parent if virtualization is being used (for scroll handling)
  onVirtualizationChange?: (isVirtualized: boolean) => void;
  // For CorrectedOutputField
  observationId?: string;
  projectId: string;
  traceId: string;
  environment?: string;
  showCorrections?: boolean;
}

/**
 * IOPreview - Router component for rendering observation input/output.
 *
 * Architecture:
 * - This component handles view state management and routing only
 * - Routes to IOPreviewJSON for JSON view (no ChatML parsing)
 * - Routes to IOPreviewPretty for pretty view (with ChatML parsing)
 *
 * Performance benefits:
 * - JSON view: skips ~150ms of ChatML parsing overhead
 * - Pretty view: only parses when needed for display
 * - Pre-parsed data from Web Worker eliminates duplicate parsing
 */
export function IOPreview({
  skills,
  input,
  output,
  status,
  outputCorrection,
  metadata,
  parsedInput,
  parsedOutput,
  parsedMetadata,
  chatMLParserResult,
  observationName,
  observationType,
  isLoading = false,
  isParsing = false,
  hideIfNull = false,
  hideOutput = false,
  hideInput = false,
  media,
  currentView,
  contentMode = "all",
  showSystemPrompt,
  inputExpansionState,
  outputExpansionState,
  metadataExpansionState,
  onInputExpansionChange,
  onOutputExpansionChange,
  onMetadataExpansionChange,
  advancedJsonExpansionState,
  onAdvancedJsonExpansionChange,
  jsonInputExpanded,
  jsonOutputExpanded,
  jsonMetadataExpanded,
  onJsonInputExpandedChange,
  onJsonOutputExpandedChange,
  onJsonMetadataExpandedChange,
  setIsPrettyViewAvailable,
  enableInlineComments,
  onAddInlineComment,
  commentedPathsByField,
  onVirtualizationChange,
  observationId,
  projectId,
  traceId,
  environment = "default",
  showCorrections = true,
}: IOPreviewProps) {
  const capture = usePostHogClientCapture();
  const [dismissedTraceViewNotifications, setDismissedTraceViewNotifications] =
    useLocalStorage<string[]>(STORAGE_KEY, []);

  // View state management
  const [localCurrentView, setLocalCurrentView] = useLocalStorage<ViewMode>(
    JSON_VIEW_PREFERENCE_STORAGE_KEY,
    DEFAULT_JSON_VIEW_PREFERENCE,
  );
  const selectedView =
    currentView ?? normalizeJsonViewPreference(localCurrentView);
  const showViewToggle = currentView === undefined;

  const [compensateScrollRef, startPreserveScroll] =
    usePreserveRelativeScroll<HTMLDivElement>([selectedView]);

  // Notify parent about pretty view availability
  // Always true - we always show the toggle and let components decide rendering
  useEffect(() => {
    setIsPrettyViewAvailable?.(true);
  }, [setIsPrettyViewAvailable]);

  // Handle view change with analytics
  const handleViewChange = (view: ViewMode) => {
    startPreserveScroll();
    capture("trace_detail:io_mode_switch", { view });
    setLocalCurrentView(view);
  };

  // Shared props for both view components
  const sharedProps = {
    skills,
    input,
    output,
    status,
    outputCorrection,
    metadata,
    parsedInput,
    parsedOutput,
    parsedMetadata,
    chatMLParserResult,
    isLoading,
    isParsing,
    hideIfNull,
    hideInput,
    hideOutput,
    media,
    inputExpansionState,
    outputExpansionState,
    metadataExpansionState,
    onInputExpansionChange,
    onOutputExpansionChange,
    onMetadataExpansionChange,
    observationId,
    observationType,
    projectId,
    traceId,
    environment,
    showCorrections,
  };

  // Only show empty state popup for traces (not observations) when there's no input/output
  // Check both parsed and raw props since not all callers provide parsedInput/parsedOutput
  const hasInput = input !== null && input !== undefined;
  const hasOutput = output !== null && output !== undefined;
  const showEmptyState =
    !hasInput &&
    !hasOutput &&
    !observationName && // Only show for traces, not observations
    !isLoading &&
    !hideIfNull &&
    !dismissedTraceViewNotifications.includes(EMPTY_IO_ALERT_ID);

  return (
    <>
      {showViewToggle && (
        <ViewModeToggle
          selectedView={selectedView}
          onViewChange={handleViewChange}
          compensateScrollRef={compensateScrollRef}
        />
      )}

      {/*
       * Conditional rendering based on view mode:
       * - JSON Beta view: IOPreviewJSON (advanced viewer with virtualization, search, inline comments)
       * - JSON view: IOPreviewJSONSimple (simple react18-json-view, no virtualization, no comments)
       * - Pretty view: IOPreviewPretty (with ChatML parsing, markdown, tools)
       *
       * Only render the active view to prevent dual DOM tree construction.
       * Trade-off: scroll/expansion state is lost when toggling views,
       * but this eliminates UI freeze with large observations.
       */}
      {selectedView === "json-beta" ? (
        <IOPreviewJSON
          hideMetadata
          input={input}
          output={output}
          status={status}
          parsedInput={parsedInput}
          parsedOutput={parsedOutput}
          outputCorrection={outputCorrection}
          isParsing={isParsing}
          hideIfNull={hideIfNull}
          hideInput={hideInput}
          hideOutput={hideOutput}
          media={media}
          expansionState={advancedJsonExpansionState}
          onExpansionChange={onAdvancedJsonExpansionChange}
          onVirtualizationChange={onVirtualizationChange}
          enableInlineComments={enableInlineComments}
          onAddInlineComment={onAddInlineComment}
          commentedPathsByField={commentedPathsByField}
          observationId={observationId}
          projectId={projectId}
          traceId={traceId}
          environment={environment}
          showCorrections={showCorrections}
        />
      ) : selectedView === "json" ? (
        <IOPreviewJSONSimple
          hideMetadata
          input={input}
          output={output}
          status={status}
          outputCorrection={outputCorrection}
          parsedInput={parsedInput}
          parsedOutput={parsedOutput}
          isLoading={isLoading}
          isParsing={isParsing}
          hideIfNull={hideIfNull}
          hideInput={hideInput}
          hideOutput={hideOutput}
          media={media}
          inputExpanded={jsonInputExpanded}
          outputExpanded={jsonOutputExpanded}
          metadataExpanded={jsonMetadataExpanded}
          onInputExpandedChange={onJsonInputExpandedChange}
          onOutputExpandedChange={onJsonOutputExpandedChange}
          onMetadataExpandedChange={onJsonMetadataExpandedChange}
          observationId={observationId}
          projectId={projectId}
          traceId={traceId}
          environment={environment}
          showCorrections={showCorrections}
        />
      ) : (
        <IOPreviewPretty
          {...sharedProps}
          contentMode={contentMode}
          showSystemPrompt={showSystemPrompt}
        />
      )}

      {showEmptyState && (
        <div className="py-2">
          <div className="relative flex flex-col items-start gap-2 rounded-lg border border-dashed p-4">
            <Button
              variant="ghost"
              size="sm"
              className="absolute top-1.5 right-1.5 h-5 w-5 p-0"
              onClick={() => {
                capture("notification:dismiss_notification", {
                  notification_id: EMPTY_IO_ALERT_ID,
                });
                setDismissedTraceViewNotifications((prev) =>
                  prev.includes(EMPTY_IO_ALERT_ID)
                    ? prev
                    : [...prev, EMPTY_IO_ALERT_ID],
                );
              }}
              title="Dismiss"
            >
              <X size="base" color="default" />
            </Button>
            <div className="flex w-full flex-row items-center gap-2 pr-6">
              <div className="bg-accent flex h-8 w-8 items-center justify-center rounded-full">
                <BookOpen size="base" color="muted" />
              </div>
              <h3 className="text-base font-bold">
                Looks like this trace didn&apos;t receive an input or output.
              </h3>
            </div>
            <p className="text-muted-foreground max-w-sm text-base">
              Add it in your code to make debugging a lot easier.
            </p>
            <ActionButton
              variant="outline"
              size="sm"
              href="https://langfuse.com/faq/all/empty-trace-input-and-output"
              trackingEventName="notification:click_link"
              trackingProps={{ notification_id: EMPTY_IO_ALERT_ID }}
            >
              View Documentation
            </ActionButton>
          </div>
        </div>
      )}
    </>
  );
}