Skip to content

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

1.5 KB
Raw Blame
import { api, sendAsPostOption } from "@/src/utils/api";
import { useReadPath } from "@/src/features/events/hooks/useReadPath";

export type UsePrefetchObservationParams = {
  projectId: string;
};

/**
 * Hook to prefetch observation data on hover.
 * Matches the old trace component's prefetch behavior with 5-minute staleTime.
 */
export function usePrefetchObservation({
  projectId,
}: UsePrefetchObservationParams) {
  const utils = api.useUtils();
  const { isV4 } = useReadPath();

  const prefetch = (
    observationId: string,
    traceId: string,
    startTime?: Date,
  ) => {
    if (isV4) {
      // Beta ON: prefetch from events table via batchIO
      if (!startTime) return;
      utils.events.batchIO.prefetch(
        {
          projectId,
          traceId, // Must match useLogViewObservationIO for cache hit
          observations: [{ id: observationId, traceId }],
          minStartTime: startTime,
          maxStartTime: startTime,
          truncated: false, // Must match useLogViewObservationIO for cache hit
        },
        {
          ...sendAsPostOption,
          staleTime: 5 * 60 * 1000, // 5 minutes
        },
      );
    } else {
      // Beta OFF: prefetch from observations table
      utils.observations.byId.prefetch(
        {
          observationId,
          traceId,
          projectId,
          startTime,
        },
        {
          staleTime: 5 * 60 * 1000, // 5 minutes
        },
      );
    }
  };

  return { prefetch };
}