Skip to content

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

29.6 KB
Raw Blame
import { useState, useEffect, useCallback, useRef, useMemo } from "react";
import { useTheme } from "next-themes";
import { countJsonRows } from "@/src/features/traces/components/AdvancedJsonViewer/utils/rowCount";
import {
  MultiSectionJsonViewer,
  type MultiSectionJsonViewerHandle,
} from "@/src/features/traces/components/AdvancedJsonViewer/MultiSectionJsonViewer";
import { Command, CommandInput } from "@/src/components/ui/command";
import { Button } from "@/src/components/ui/button";
import { ChevronUp, ChevronDown, WrapText, Minus, Copy } from "@langfuse/icons";
import { useJsonViewPreferences } from "@/src/features/traces/components/AdvancedJsonViewer/hooks/useJsonViewPreferences";
import type { MediaReturnType } from "@/src/features/media/validation";
import { HoverCard } from "@/src/components/design-system/HoverCard/HoverCard";
import {
  InlineCommentSelectionProvider,
  useInlineCommentSelectionOptional,
  type SelectionData,
} from "@/src/features/comments/contexts/InlineCommentSelectionProvider";
import { CommentableJsonView } from "@/src/features/comments/components/CommentableJsonView";
import { InlineCommentBubble } from "@/src/features/comments/components/InlineCommentBubble";
import { type CommentedPathsByField } from "@/src/features/traces/components/AdvancedJsonViewer/utils/commentRanges";
import { type ExpansionState } from "@/src/features/traces/components/AdvancedJsonViewer/types";
import { type Prisma, type ScoreDomain, deepParseJson } from "@langfuse/shared";
import {
  decodeUnicodeInJson,
  DECODE_UNICODE_MAX_NODES,
} from "@/src/utils/decodeUnicodeInJson";
import { CorrectedOutputField } from "./components/CorrectedOutputField";
import { LargeJsonFieldFallback } from "./components/LargeJsonFieldFallback";
import { LazyJsonViewer } from "@/src/features/traces/components/AdvancedJsonViewer/lazy/react/LazyJsonViewer";
import {
  JSON_VIEW_RENDER_ROW_LIMIT,
  probeJsonField,
} from "./fns/jsonViewSizeGate";
import {
  getStatusMessagePresentation,
  parseStructuredStatusMessage,
  type ObservationStatusMessage,
} from "./components/statusMessagePresentation";

// A field needing windowing is gated to the lazy byte-engine viewer, so the
// gate row limit IS the virtualization threshold (single source of truth). The
// eager virtualized viewer is therefore unreachable for trace I/O — see the
// `needsVirtualization` note below.
const VIRTUALIZATION_THRESHOLD = JSON_VIEW_RENDER_ROW_LIMIT;

// Light values are Tailwind red-50 / yellow-50, matching status IOSection tones.
function statusMessageBackgroundColor(
  level: ObservationStatusMessage["level"],
  isDark: boolean,
) {
  if (level === "ERROR")
    return isDark ? "var(--light-red)" : "oklch(97.1% 0.013 17.38)";
  if (level === "WARNING")
    return isDark ? "var(--light-yellow)" : "oklch(98.7% 0.026 102.212)";
  return "hsl(var(--surface-section))";
}

/**
 * Decode a field's \uXXXX escapes, but only when it fits under the decoder's
 * node budget. `decodeUnicodeInJson` caps at DECODE_UNICODE_MAX_NODES and copies
 * the remainder through un-decoded; since the same value backs both the viewer
 * and the raw download, a larger field would export a traversal-order-dependent
 * MIX of decoded/escaped unicode. Past the budget, return it raw (fully
 * un-decoded) — consistent and faithful — rather than partially decoded.
 */
function decodeIfWithinBudget(value: unknown, rowCount: number): unknown {
  return rowCount > DECODE_UNICODE_MAX_NODES
    ? value
    : decodeUnicodeInJson(value);
}

export interface IOPreviewJSONProps {
  input?: Prisma.JsonValue;
  output?: Prisma.JsonValue;
  status?: ObservationStatusMessage;
  metadata?: Prisma.JsonValue;
  outputCorrection?: ScoreDomain;
  // Pre-parsed data (from useParsedObservation hook)
  parsedInput?: unknown;
  parsedOutput?: unknown;
  parsedMetadata?: unknown;
  isParsing?: boolean;
  hideIfNull?: boolean;
  hideOutput?: boolean;
  hideInput?: boolean;
  hideMetadata?: boolean;
  // Media attachments
  media?: MediaReturnType[];
  // Callback to inform parent if virtualization is being used (for scroll handling)
  onVirtualizationChange?: (isVirtualized: boolean) => void;
  // Inline comment props
  enableInlineComments?: boolean;
  onAddInlineComment?: (selection: SelectionData) => void;
  commentedPathsByField?: CommentedPathsByField;
  // Correction props
  observationId?: string;
  projectId: string;
  traceId: string;
  environment?: string;
  // Combined expansion state (paths are prefixed: "input.foo", "output.bar", etc.)
  // Input accepts ExpansionState (boolean shorthand), callback receives Record (what viewer emits)
  expansionState?: ExpansionState;
  onExpansionChange?: (expansion: Record<string, boolean>) => void;
  showCorrections?: boolean;
}

/**
 * IOPreviewJSON - Renders input/output in JSON view mode only.
 *
 * Optimizations:
 * - No ChatML parsing (not needed for JSON view)
 * - No markdown rendering checks (not applicable)
 * - No tool definitions (only visible in pretty view)
 * - Accepts pre-parsed data to avoid duplicate parsing
 *
 * This component is ~150ms faster than the full IOPreview for large data
 * because it skips all ChatML processing.
 */
function IOPreviewJSONInner({
  input,
  output,
  status,
  metadata,
  parsedInput,
  parsedOutput,
  parsedMetadata,
  outputCorrection,
  isParsing = false,
  hideIfNull = false,
  hideOutput = false,
  hideInput = false,
  hideMetadata = false,
  media,
  onVirtualizationChange,
  enableInlineComments = false,
  onAddInlineComment,
  commentedPathsByField,
  observationId,
  projectId,
  traceId,
  environment = "default",
  expansionState,
  onExpansionChange,
  showCorrections = true,
}: IOPreviewJSONProps) {
  const selectionContext = useInlineCommentSelectionOptional();
  const inlineCommentPositionRect = selectionContext?.selection?.anchorRect
    ? (selectionContext.selection.startRect ??
      selectionContext.selection.anchorRect)
    : null;

  const handleAddComment = useCallback(() => {
    if (selectionContext?.selection && onAddInlineComment) {
      onAddInlineComment(selectionContext.selection);
    }
  }, [selectionContext?.selection, onAddInlineComment]);

  const { resolvedTheme } = useTheme();
  const isDark = resolvedTheme === "dark";

  // Background colors that adapt to theme (memoized to prevent tree rebuilds)
  const { inputBgColor, outputBgColor, metadataBgColor } = useMemo(
    () => ({
      inputBgColor: isDark ? "rgb(15, 23, 42)" : "rgb(249, 252, 255)", // Dark slate vs light blue
      outputBgColor: "hsl(var(--surface-section))",
      metadataBgColor: isDark ? "rgb(30, 20, 40)" : "rgb(253, 251, 254)", // Dark purple vs light purple
    }),
    [isDark],
  );
  const statusPresentation = status
    ? getStatusMessagePresentation(status.level)
    : null;

  // Fall back to raw values when caller does not provide pre-parsed fields
  // (e.g. session events rows in v4 mode). Parse once here, BEFORE the decode
  // and tree-build, so the node-count gate below can act on the parsed shape.
  const inputParsed = useMemo(
    () => (isParsing ? undefined : (parsedInput ?? deepParseJson(input))),
    [parsedInput, input, isParsing],
  );
  const outputParsed = useMemo(
    () => (isParsing ? undefined : (parsedOutput ?? deepParseJson(output))),
    [parsedOutput, output, isParsing],
  );
  const metadataParsed = useMemo(
    () => (isParsing ? undefined : (parsedMetadata ?? deepParseJson(metadata))),
    [parsedMetadata, metadata, isParsing],
  );

  // Node-count gate (LFE-10847): the Beta viewer virtualizes the DOM but still
  // builds the full node tree on the main thread, so a field with too many
  // nodes (a large conversation / deeply nested JSON) freezes the tab even
  // here. Count rows once per field — cheap (~integer add per node) and, unlike
  // a char limit, correctly lets a huge single string (one node, e.g. a base64
  // data-URI) through while gating million-node payloads. `countJsonRows` also
  // feeds the virtualization decision below, so a gated field contributes 0.
  const inputRows = useMemo(() => countJsonRows(inputParsed), [inputParsed]);
  const outputRows = useMemo(() => countJsonRows(outputParsed), [outputParsed]);
  const metadataRows = useMemo(
    () => countJsonRows(metadataParsed),
    [metadataParsed],
  );

  const inputTooLarge = inputRows > JSON_VIEW_RENDER_ROW_LIMIT;
  const outputTooLarge = outputRows > JSON_VIEW_RENDER_ROW_LIMIT;
  const metadataTooLarge = metadataRows > JSON_VIEW_RENDER_ROW_LIMIT;

  // Decode \uXXXX escapes (e.g. Japanese ingested with Python
  // ensure_ascii=True) at the data source so that search-match offsets, comment
  // ranges, rendering and copy-to-clipboard all operate on the same decoded
  // strings. Decoding at the leaf renderer instead would desync highlight
  // offsets. Already-decoded strings are a no-op. We decode over-render-limit
  // fields too (they render lazily over this decoded value), so a field's
  // display doesn't change just because it crossed the render gate.
  //
  // BUT `decodeUnicodeInJson` caps its walk at DECODE_UNICODE_MAX_NODES and
  // copies the remainder through un-decoded. A field bigger than that would be
  // PARTIALLY decoded, and since the same value backs both the viewer and the
  // raw download, the download would carry a traversal-order-dependent MIX of
  // decoded/escaped unicode (LFE-10847 review). Decode all-or-nothing: past the
  // budget, show it fully raw — consistent and faithful — rather than partial.
  const effectiveInput = useMemo(
    () =>
      isParsing ? undefined : decodeIfWithinBudget(inputParsed, inputRows),
    [inputParsed, inputRows, isParsing],
  );
  const effectiveOutput = useMemo(
    () =>
      isParsing ? undefined : decodeIfWithinBudget(outputParsed, outputRows),
    [outputParsed, outputRows, isParsing],
  );
  const effectiveMetadata = useMemo(
    () =>
      isParsing
        ? undefined
        : decodeIfWithinBudget(metadataParsed, metadataRows),
    [metadataParsed, metadataRows, isParsing],
  );

  // Probe over-limit fields once for the bounded fallback (preview + download).
  // Probe the PARSED value — the same value the row-count gate ran on — so the
  // preview, download and reported size all reflect what tripped the gate (a
  // string field that deep-parses into a large tree would otherwise show its
  // short raw length).
  // Probe the DECODED value (the same one the lazy viewer renders), so its
  // serialized string is reused for BOTH the download and the byte engine — one
  // JSON.stringify, not two (the lazy branch below passes `probe.serialized`).
  const inputProbe = useMemo(
    () =>
      inputTooLarge && effectiveInput !== undefined
        ? probeJsonField(effectiveInput)
        : null,
    [effectiveInput, inputTooLarge],
  );
  const outputProbe = useMemo(
    () =>
      outputTooLarge && effectiveOutput !== undefined
        ? probeJsonField(effectiveOutput)
        : null,
    [effectiveOutput, outputTooLarge],
  );
  const metadataProbe = useMemo(
    () =>
      metadataTooLarge && effectiveMetadata !== undefined
        ? probeJsonField(effectiveMetadata)
        : null,
    [effectiveMetadata, metadataTooLarge],
  );

  // A gated field parses to undefined above but is not empty — it is too big.
  // Treat it as present so hideIfNull callers still show the fallback instead
  // of silently dropping the field.
  const showInput =
    !hideInput &&
    (inputTooLarge || !(hideIfNull && effectiveInput === undefined));
  const showOutput =
    !hideOutput &&
    (outputTooLarge || !(hideIfNull && effectiveOutput === undefined));
  const showMetadata =
    !hideMetadata &&
    (metadataTooLarge || !(hideIfNull && effectiveMetadata === undefined));

  const downloadName = observationId ?? traceId;

  // Row counts drive the virtualization decision. Gated fields render as a
  // fallback (not inside the viewer), so they contribute 0.
  const rowCounts = useMemo(() => {
    return {
      input: inputTooLarge ? 0 : inputRows,
      output: outputTooLarge ? 0 : outputRows,
      metadata: metadataTooLarge ? 0 : metadataRows,
    };
  }, [
    inputTooLarge,
    outputTooLarge,
    metadataTooLarge,
    inputRows,
    outputRows,
    metadataRows,
  ]);

  // Whether the eager MultiSectionJsonViewer must virtualize its own DOM. This
  // is now ALWAYS false: a field with more rows than VIRTUALIZATION_THRESHOLD is
  // gated to the lazy viewer above (so it contributes 0 to rowCounts), and a
  // field with fewer never crosses the threshold. The eager virtualized path is
  // effectively retired for trace I/O (it froze the tab at tens of thousands of
  // nodes — LFE-10847); the residual small data renders eagerly non-virtualized.
  // Left in place until that path is removed wholesale in a follow-up.
  const needsVirtualization = useMemo(() => {
    return (
      rowCounts.input > VIRTUALIZATION_THRESHOLD ||
      rowCounts.output > VIRTUALIZATION_THRESHOLD ||
      rowCounts.metadata > VIRTUALIZATION_THRESHOLD
    );
  }, [rowCounts]);

  // Hooks for multi-section viewer - must be called unconditionally
  const { stringWrapMode, setStringWrapMode } = useJsonViewPreferences();
  const [searchQuery, setSearchQuery] = useState("");
  const [debouncedSearchQuery, setDebouncedSearchQuery] = useState("");
  const [currentMatchIndex, setCurrentMatchIndex] = useState(0);
  const [searchMatchCount, setSearchMatchCount] = useState(0);
  const scrollContainerRef = useRef<HTMLDivElement>(null);
  const viewerRef = useRef<MultiSectionJsonViewerHandle>(null);

  // Debounce search query
  useEffect(() => {
    const timer = setTimeout(() => {
      setDebouncedSearchQuery(searchQuery);
      setCurrentMatchIndex(0);
    }, 300);
    return () => clearTimeout(timer);
  }, [searchQuery]);

  // Notify parent about virtualization state
  useEffect(() => {
    onVirtualizationChange?.(needsVirtualization);
  }, [needsVirtualization, onVirtualizationChange]);

  // Search navigation callbacks
  const handleNextMatch = useCallback(() => {
    if (searchMatchCount === 0) return;
    setCurrentMatchIndex((prev) => (prev + 1) % searchMatchCount);
  }, [searchMatchCount]);

  const handlePreviousMatch = useCallback(() => {
    if (searchMatchCount === 0) return;
    setCurrentMatchIndex((prev) =>
      prev === 0 ? searchMatchCount - 1 : prev - 1,
    );
  }, [searchMatchCount]);

  const handleClearSearch = useCallback(() => {
    setSearchQuery("");
    setDebouncedSearchQuery("");
    setCurrentMatchIndex(0);
  }, []);

  // Handle string wrap mode cycling
  const handleCycleWrapMode = useCallback(() => {
    if (stringWrapMode === "truncate") {
      setStringWrapMode("wrap");
    } else if (stringWrapMode === "wrap") {
      setStringWrapMode("nowrap");
    } else {
      setStringWrapMode("truncate");
    }
  }, [stringWrapMode, setStringWrapMode]);

  // Handle scrolling to a specific section
  const handleScrollToSection = useCallback((sectionKey: string) => {
    viewerRef.current?.scrollToSection(sectionKey);
  }, []);

  const handleCopy = useCallback(() => {
    // Gated fields have no materialized value (effective* is undefined), so
    // copy a placeholder rather than silently dropping the key — the field is
    // visibly present on screen (fallback + download), it just can't be inlined.
    const TOO_LARGE = "<omitted: too large to render — use the field download>";
    const dataObj: Record<string, unknown> = {};
    if (showInput) dataObj.input = inputTooLarge ? TOO_LARGE : effectiveInput;
    if (showOutput)
      dataObj.output = outputTooLarge ? TOO_LARGE : effectiveOutput;
    if (showMetadata)
      dataObj.metadata = metadataTooLarge ? TOO_LARGE : effectiveMetadata;
    const jsonString = JSON.stringify(dataObj, null, 2);
    navigator.clipboard.writeText(jsonString);
  }, [
    showInput,
    showOutput,
    showMetadata,
    inputTooLarge,
    outputTooLarge,
    metadataTooLarge,
    effectiveInput,
    effectiveOutput,
    effectiveMetadata,
  ]);

  const wrapIcon = useMemo(() => {
    if (stringWrapMode === "truncate") {
      return <Minus size="base" />;
    }
    if (stringWrapMode === "wrap") {
      return <WrapText size="base" />;
    }
    return <ChevronDown size="base" __rawClassName="-rotate-90" />;
  }, [stringWrapMode]);

  // Build sections - memoized to prevent re-creation. A gated field renders as
  // a section with no data (hideData → the viewer builds no tree for it, so it
  // can't freeze) whose footer is the bounded fallback. Keeping it a section —
  // rather than a block outside the viewer — preserves the natural
  // Input → Output → Metadata order and the shared search/scroll. The section
  // header shows the field name, so the fallback hides its own title.
  const sections = useMemo(() => {
    const gatedSection = (
      fieldKey: string,
      title: string,
      backgroundColor: string,
      probe: ReturnType<typeof probeJsonField> | null,
      rowCount: number,
    ) => ({
      // Use a key distinct from the field's normal section key so the fallback
      // does not inherit a persisted collapsed state from an earlier trace
      // where the same field rendered normally — that would silently hide the
      // download escape hatch (its collapse persists per section key). A gated
      // field gets its own key, so it always defaults to expanded.
      key: `${fieldKey}__oversized`,
      title,
      data: null,
      hideData: true,
      backgroundColor,
      minHeight: "4px",
      // A gated field has no tree to expand/collapse — only the download escape
      // hatch below. Render a header WITHOUT a collapse toggle, so the section
      // can never be collapsed: its collapse is therefore never persisted, and
      // the download can't be hidden — not silently (inherited state) and not
      // by a user collapsing one gated field then viewing another of the same
      // name (the gated→gated leak the plain key still allowed).
      renderHeader: () => (
        <div className="flex items-center px-2 py-1.5">
          <span className="text-xs font-bold">{title}</span>
        </div>
      ),
      renderFooter: () => {
        if (!probe) return null;
        // A huge STRING (e.g. a base64 image) is a single leaf — the lazy tree
        // adds nothing, so keep the bounded preview + download. A huge
        // STRUCTURED value is where the eager tree-build froze the tab: render
        // it through the lazy byte-engine viewer (main thread, no eager tree,
        // cost proportional to what's expanded), with download as a secondary
        // escape hatch.
        if (probe.isString) {
          return (
            <LargeJsonFieldFallback
              title={title}
              hideTitle
              serialized={probe.serialized}
              isString={probe.isString}
              charCount={probe.size}
              rowCount={rowCount}
              downloadFileBase={`${fieldKey}-${downloadName}`}
            />
          );
        }
        return (
          <div className="io-message-content flex flex-col gap-1">
            <div
              style={{ height: 400 }}
              className="overflow-hidden rounded-md border"
            >
              {/* Feed the byte engine the probe's serialized JSON, so the field
                  is stringified once (probe) and reused here — not a second time
                  inside the viewer. */}
              <LazyJsonViewer serialized={probe.serialized} />
            </div>
            <LargeJsonFieldFallback
              title={title}
              hideTitle
              serialized={probe.serialized}
              isString={probe.isString}
              charCount={probe.size}
              rowCount={rowCount}
              downloadFileBase={`${fieldKey}-${downloadName}`}
              downloadOnly
            />
          </div>
        );
      },
    });

    const result = [];
    if (status && statusPresentation) {
      result.push({
        key: "status-message",
        title: statusPresentation.title,
        data: parseStructuredStatusMessage(status.message) ?? status.message,
        backgroundColor: statusMessageBackgroundColor(status.level, isDark),
        minHeight: "4px",
      });
    }
    if (showInput) {
      result.push(
        inputTooLarge
          ? gatedSection("input", "Input", inputBgColor, inputProbe, inputRows)
          : {
              key: "input",
              title: "Input",
              data: effectiveInput,
              backgroundColor: inputBgColor,
              minHeight: "200px",
            },
      );
    }
    if (showOutput) {
      result.push(
        outputTooLarge
          ? gatedSection(
              "output",
              "Output",
              outputBgColor,
              outputProbe,
              outputRows,
            )
          : {
              key: "output",
              title: "Output",
              data: effectiveOutput,
              backgroundColor: outputBgColor,
              minHeight: "200px",
            },
      );
    }
    if (showCorrections) {
      result.push({
        key: "corrections",
        title: "Output correction",
        data: null,
        hideData: true, // Hide key/value display, only show header/footer
        backgroundColor: outputBgColor,
        minHeight: "4px",
        // Add corrected output as footer when corrections are enabled
        renderFooter: () => (
          <CorrectedOutputField
            // Don't hand the full oversized object to the diff dialog: it
            // `JSON.stringify`s `actualOutput` unmemoized in its render body on
            // every render (incl. every keystroke while editing a correction),
            // which would re-serialize megabytes on exactly the large-output
            // case this viewer gates. `actualOutputTooLarge` drives the
            // "too large to diff" branch, so `undefined` here is sufficient
            // (and `effectiveOutput` stays decoded for the lazy viewer/probe).
            actualOutput={outputTooLarge ? undefined : effectiveOutput}
            actualOutputTooLarge={outputTooLarge}
            existingCorrection={outputCorrection}
            observationId={observationId}
            projectId={projectId}
            traceId={traceId}
            environment={environment}
            compact={true}
          />
        ),
      });
    }
    if (showMetadata) {
      result.push(
        metadataTooLarge
          ? gatedSection(
              "metadata",
              "Metadata",
              metadataBgColor,
              metadataProbe,
              metadataRows,
            )
          : {
              key: "metadata",
              title: "Metadata",
              data: effectiveMetadata,
              backgroundColor: metadataBgColor,
              minHeight: "200px",
            },
      );
    }
    return result;
  }, [
    isDark,
    showInput,
    showOutput,
    status,
    statusPresentation,
    showMetadata,
    inputTooLarge,
    outputTooLarge,
    metadataTooLarge,
    effectiveInput,
    effectiveOutput,
    effectiveMetadata,
    inputProbe,
    outputProbe,
    metadataProbe,
    inputRows,
    outputRows,
    metadataRows,
    downloadName,
    inputBgColor,
    outputBgColor,
    metadataBgColor,
    showCorrections,
    observationId,
    outputCorrection,
    projectId,
    traceId,
    environment,
  ]);

  // Wait for parsing to complete before rendering to avoid flicker
  if (isParsing) {
    return (
      <div className="flex min-h-0 flex-1 flex-col border-t border-b">
        <div className="flex h-full items-center justify-center">
          <div className="text-muted-foreground text-base">Parsing data...</div>
        </div>
      </div>
    );
  }

  // The viewer content - wrapped in CommentableJsonView when comments are enabled
  const viewerContent = (
    <MultiSectionJsonViewer
      ref={viewerRef}
      sections={sections}
      virtualized={needsVirtualization}
      showLineNumbers={true}
      enableCopy={true}
      stringWrapMode={stringWrapMode}
      truncateStringsAt={stringWrapMode === "truncate" ? 100 : null}
      searchQuery={debouncedSearchQuery}
      currentMatchIndex={currentMatchIndex}
      onSearchResults={setSearchMatchCount}
      scrollContainerRef={scrollContainerRef as React.RefObject<HTMLDivElement>}
      media={media}
      commentedPathsByField={commentedPathsByField}
      externalExpansionState={expansionState}
      onExpansionChange={onExpansionChange}
      theme={{
        fontSize: "0.7rem",
        lineHeight: 14,
        indentSize: 12,
      }}
    />
  );

  return (
    <div className="ph-no-capture flex min-h-0 flex-1 flex-col border-t border-b">
      {/* Inline comment bubble - shows when text is selected */}
      {enableInlineComments && inlineCommentPositionRect && (
        <InlineCommentBubble
          onAddComment={handleAddComment}
          positionRect={inlineCommentPositionRect}
        />
      )}

      {/* Header - matches LogViewToolbar styling */}
      <div className="bg-background flex h-9 shrink-0 items-center gap-1.5 border-b px-2">
        {/* Search input - expands to fill available width */}
        <Command className="flex-1 rounded-none border-0 bg-transparent">
          <CommandInput
            showBorder={false}
            placeholder="Search across all sections..."
            className="h-7 border-0 focus:ring-0"
            value={searchQuery}
            onValueChange={setSearchQuery}
            onKeyDown={(e) => {
              if (e.key === "Enter") {
                e.preventDefault();
                if (e.shiftKey) {
                  handlePreviousMatch();
                } else {
                  handleNextMatch();
                }
              } else if (e.key === "Escape") {
                handleClearSearch();
              }
            }}
          />
        </Command>

        {/* Match counter - inline text (only when searching) */}
        {searchQuery && (
          <span className="text-muted-foreground text-xs whitespace-nowrap">
            {searchMatchCount > 0
              ? `${currentMatchIndex + 1} of ${searchMatchCount}`
              : "No matches"}
          </span>
        )}

        {/* Navigation buttons (only when matches exist) */}
        {searchQuery && searchMatchCount > 0 && (
          <>
            <Button
              variant="ghost"
              size="icon"
              className="h-7 w-7"
              onClick={handlePreviousMatch}
              title="Previous match (Shift+Enter)"
            >
              <ChevronUp size="base" color="default" />
            </Button>
            <Button
              variant="ghost"
              size="icon"
              className="h-7 w-7"
              onClick={handleNextMatch}
              title="Next match (Enter)"
            >
              <ChevronDown size="base" color="default" />
            </Button>
          </>
        )}

        {/* Wrap mode toggle */}
        <Button
          variant="ghost"
          size="icon"
          className="h-7 w-7"
          onClick={handleCycleWrapMode}
          title={`String wrap mode: ${stringWrapMode}`}
        >
          {wrapIcon}
        </Button>

        {/* Copy button */}
        <Button
          variant="ghost"
          size="icon"
          className="h-7 w-7"
          onClick={handleCopy}
          title="Copy to clipboard"
        >
          <Copy size="base" color="default" />
        </Button>
      </div>

      {/* Section navigation hint bar */}
      <div className="bg-background flex h-6 shrink-0 items-center gap-1.5 border-b px-2">
        <span className="text-muted-foreground text-xs">Jump to:</span>
        {sections.map((section, index) => (
          <span key={section.key} className="flex items-center">
            <button
              onClick={() => handleScrollToSection(section.key)}
              className="text-primary cursor-pointer text-xs hover:underline"
            >
              {section.title}
            </button>
            {index < sections.length - 1 && (
              <span className="text-muted-foreground text-xs">,&nbsp;</span>
            )}
          </span>
        ))}
        {needsVirtualization && (
          <HoverCard
            placement="bottom-end"
            content={
              <div className="w-80 p-3">
                <div className="space-y-2">
                  <p className="text-base font-bold">Virtualized View</p>
                  <p className="text-muted-foreground text-xs">
                    This view is using virtualization due to a large number of
                    keys ({rowCounts.input.toLocaleString()} input,{" "}
                    {rowCounts.output.toLocaleString()} output,{" "}
                    {rowCounts.metadata.toLocaleString()} metadata). Only
                    visible rows are rendered for optimal performance.
                  </p>
                </div>
              </div>
            }
          >
            {({ getTriggerProps }) => (
              <span
                className="bg-muted text-muted-foreground ml-auto cursor-help rounded px-1.5 py-px text-[10px] font-bold"
                tabIndex={0}
                {...getTriggerProps()}
              >
                Virtualized
              </span>
            )}
          </HoverCard>
        )}
      </div>

      {/* Body with MultiSectionJsonViewer */}
      <div className="min-h-0 flex-1 overflow-auto" ref={scrollContainerRef}>
        {enableInlineComments ? (
          <CommentableJsonView enabled={enableInlineComments}>
            {viewerContent}
          </CommentableJsonView>
        ) : (
          viewerContent
        )}
      </div>
    </div>
  );
}

/**
 * IOPreviewJSON - Wrapper that conditionally adds InlineCommentSelectionProvider.
 */
export function IOPreviewJSON(props: IOPreviewJSONProps) {
  // Wrap with selection provider if inline comments are enabled
  if (props.enableInlineComments) {
    return (
      <InlineCommentSelectionProvider>
        <IOPreviewJSONInner {...props} />
      </InlineCommentSelectionProvider>
    );
  }
  return <IOPreviewJSONInner {...props} />;
}