Skip to content

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

10.8 KB
Raw Blame
import {
  type AnnotationQueueItem,
  type ScoreConfigDomain,
} from "@langfuse/shared";
import { AnnotationDrawerSection } from "../shared/AnnotationDrawerSection";
import { AnnotationProcessingLayout } from "../shared/AnnotationProcessingLayout";
import { SessionIO } from "@/src/features/sessions/SessionPages";
import { LazyTraceEventsRow } from "@/src/features/sessions/TraceEventsRow";
import { asCommentCounts } from "@/src/features/sessions/sessionDetailPageTypes";
import { useState, useMemo, useCallback } from "react";
import { Button } from "@/src/components/ui/button";
import { Button as DsButton } from "@/src/components/design-system/Button/Button";
import { ActionButtonCountBadge } from "@/src/components/ui/ActionButtonCountBadge";
import { ItemBadge } from "@/src/components/ItemBadge";
import { CopyIdsPopover } from "@/src/features/traces/components/CopyIdsPopover";
import { Badge } from "@/src/components/design-system/Badge/Badge";
import { Separator } from "@/src/components/ui/separator";
import Link from "next/link";
import { Card } from "@/src/components/ui/card";
import { useReadPath } from "@/src/features/events/hooks/useReadPath";
import { api, shouldRetryResourceQuery } from "@/src/utils/api";
import { JsonSkeleton } from "@/src/components/ui/CodeJsonViewer";
import { CommentDrawerController } from "@/src/features/comments/CommentDrawerController";
import { getNumberFromMap } from "@/src/utils/map-utils";
import { MessageSquare, MessageSquareOff } from "@langfuse/icons";

type SessionAnnotationQueueItem = AnnotationQueueItem & {
  parentTraceId?: string | null;
  lockedByUser: { name: string | null | undefined } | null;
};

interface SessionAnnotationProcessorProps {
  item: SessionAnnotationQueueItem;
  data: any; // // Session data with scores
  configs: ScoreConfigDomain[];
  projectId: string;
}

// some projects have thousands of traces in a session, paginate to avoid rendering all at once
const PAGE_SIZE = 10;

// Stable empty array to avoid creating new references on every render (defeats React.memo)
const EMPTY_FILTER_STATE: [] = [];

export const SessionAnnotationProcessor: React.FC<
  SessionAnnotationProcessorProps
> = ({ item, data, configs, projectId }) => {
  const [visibleTraces, setVisibleTraces] = useState(PAGE_SIZE);
  const handleLoadMoreTraces = () => {
    setVisibleTraces((prev) => prev + PAGE_SIZE);
  };
  const { isV4 } = useReadPath();

  // Fetch traces separately when v4 beta is enabled (events table path)
  // The byIdWithScoresFromEvents endpoint doesn't include traces array
  const tracesFromEventsQuery = api.sessions.tracesFromEvents.useQuery(
    { projectId, sessionId: item.objectId },
    {
      enabled: isV4,
      retry: shouldRetryResourceQuery,
    },
  );

  const traceCommentCounts =
    api.comments.getTraceCommentCountsBySessionId.useQuery(
      {
        projectId,
        sessionId: item.objectId,
      },
      { enabled: isV4 },
    );

  const sessionCommentCounts = api.comments.getCountByObjectId.useQuery({
    projectId,
    objectId: item.objectId,
    objectType: "SESSION",
  });

  // Unify traces from both paths:
  // - v4 beta OFF: traces come from data.traces (byIdWithScores endpoint)
  // - v4 beta ON: traces come from separate tracesFromEvents query
  const traces = useMemo(() => {
    if (isV4) {
      return tracesFromEventsQuery.data ?? [];
    }
    return data?.traces ?? [];
  }, [isV4, tracesFromEventsQuery.data, data?.traces]);

  // For the "Total traces" badge, show countTraces from session metadata when available (v4),
  // or fall back to loaded traces length
  const totalTracesForBadge = useMemo(() => {
    if (isV4) {
      return data?.countTraces ?? traces.length;
    }
    return traces.length;
  }, [isV4, data?.countTraces, traces.length]);

  // Stable callback to avoid creating new function reference on every render (defeats React.memo)
  const openPeek = useCallback(
    (traceId: string, row?: { observationId?: string }) => {
      // observationId: a truncated observation's "Open in trace view" deep-links
      // to that observation (LFE-10958).
      const observationParam = row?.observationId
        ? `?observation=${encodeURIComponent(row.observationId)}`
        : "";
      window.open(
        `/project/${projectId}/traces/${traceId}${observationParam}`,
        "_blank",
      );
    },
    [projectId],
  );

  const leftPanel = (
    <div className="flex h-full flex-col overflow-hidden">
      {/* Sticky Header */}
      <div className="bg-background shrink-0">
        <div className="mt-3 grid w-full grid-cols-[auto_auto] items-start justify-between gap-2 px-4">
          <div className="flex w-full flex-row items-start gap-1">
            <div className="mt-1.5">
              <ItemBadge type="SESSION" isSmall />
            </div>
            <Link
              href={`/project/${projectId}/sessions/${encodeURIComponent(item.objectId)}`}
              target="_blank"
              className="mb-0 ml-1 line-clamp-2 min-w-0 font-bold break-all hover:underline md:break-normal md:wrap-break-word"
            >
              {item.objectId}
            </Link>
            <CopyIdsPopover
              idItems={[{ id: item.objectId, name: "Session ID" }]}
            />
          </div>
          <CommentDrawerController
            projectId={projectId}
            count={getNumberFromMap(sessionCommentCounts.data, item.objectId)}
          >
            {({ disabled, openDrawer }) => (
              <Button
                type="button"
                variant="outline"
                disabled={disabled}
                onClick={() =>
                  openDrawer({
                    type: "comments",
                    objectId: item.objectId,
                    objectType: "SESSION",
                  })
                }
                className="gap-1"
              >
                {disabled ? (
                  <MessageSquareOff size="base" color="muted" />
                ) : (
                  <>
                    <MessageSquare size="base" color="default" />
                    <span>Add comment</span>
                    {getNumberFromMap(
                      sessionCommentCounts.data,
                      item.objectId,
                    ) ? (
                      <ActionButtonCountBadge
                        count={
                          getNumberFromMap(
                            sessionCommentCounts.data,
                            item.objectId,
                          ) ?? 0
                        }
                      />
                    ) : null}
                  </>
                )}
              </Button>
            )}
          </CommentDrawerController>
        </div>
        <div className="mt-2 mb-4 grid w-full min-w-0 items-center justify-between px-4">
          <div className="flex max-w-full min-w-0 shrink flex-col">
            <div className="flex max-w-full min-w-0 flex-wrap items-center gap-1">
              {data?.environment && (
                <Badge color="filled" text={`Env: ${data.environment}`} />
              )}
              <Badge
                color="primary"
                text={`Total traces: ${totalTracesForBadge}`}
              />
            </div>
          </div>
        </div>
        <Separator />
      </div>

      {/* Scrollable Content */}
      <div className="flex-1 overflow-y-auto">
        <div className="p-4">
          {/* Loading state for v4 beta traces */}
          {isV4 && tracesFromEventsQuery.isLoading && (
            <div className="space-y-4">
              {Array.from({ length: 3 }).map((_, i) => (
                <Card
                  key={i}
                  className="border-border mb-2 grid gap-2 p-2 shadow-none"
                >
                  <JsonSkeleton
                    className="h-full w-full overflow-hidden"
                    numRows={4}
                  />
                </Card>
              ))}
            </div>
          )}
          {/* Error state for v4 beta traces */}
          {isV4 && tracesFromEventsQuery.isError && (
            <div className="text-destructive p-2 text-sm">
              Failed to load traces for this session.
            </div>
          )}
          {/* Trace list - v4 path uses LazyTraceEventsRow for deferred loading */}
          {isV4 &&
            tracesFromEventsQuery.isSuccess &&
            traces
              .slice(0, visibleTraces)
              .map((trace: any, index: number) => (
                <LazyTraceEventsRow
                  key={trace.id}
                  trace={trace}
                  projectId={projectId}
                  sessionId={item.objectId}
                  openPeek={openPeek}
                  traceCommentCounts={asCommentCounts(traceCommentCounts.data)}
                  showCorrections
                  filterState={EMPTY_FILTER_STATE}
                  viewLabel={null}
                  hideTracePanel
                  index={index}
                />
              ))}
          {/* Trace list - v3 path uses SessionIO */}
          {!isV4 &&
            traces.slice(0, visibleTraces).map((trace: any) => (
              <Card
                className="border-border hover:border-ring group mb-2 grid gap-2 p-2 shadow-none"
                key={trace.id}
              >
                <div className="-mt-1 p-1 pt-0 opacity-50 transition-opacity group-hover:opacity-100">
                  <Link
                    href={`/project/${projectId}/traces/${trace.id}`}
                    className="text-xs hover:underline"
                  >
                    Trace: {trace.name} ({trace.id})&nbsp;↗
                  </Link>
                  <div className="text-muted-foreground text-xs">
                    {trace.timestamp.toLocaleString()}
                  </div>
                </div>
                <SessionIO
                  traceId={trace.id}
                  projectId={projectId}
                  timestamp={trace.timestamp}
                  environment={trace.environment}
                  showCorrections
                />
              </Card>
            ))}
          {(!isV4 || tracesFromEventsQuery.isSuccess) &&
            traces.length > visibleTraces && (
              <div className="flex justify-center py-4">
                <DsButton
                  variant="text"
                  size="sm"
                  text={`Load ${Math.min(traces.length - visibleTraces, PAGE_SIZE)} More`}
                  onClick={handleLoadMoreTraces}
                />
              </div>
            )}
        </div>
      </div>
    </div>
  );

  const rightPanel = (
    <AnnotationDrawerSection
      item={item}
      isV4={isV4}
      scoreTarget={{
        type: "session",
        sessionId: item.objectId,
      }}
      scores={data?.scores ?? []}
      configs={configs}
      environment={data?.environment}
    />
  );

  return (
    <AnnotationProcessingLayout
      leftPanel={leftPanel}
      rightPanel={rightPanel}
      projectId={projectId}
    />
  );
};