Skip to content

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

chore(events-table): auto switch to obs view mode if there are no roo… (#11894)

Nimar35190a308dfd
diff --git a/web/src/features/events/components/EventsTable.tsx b/web/src/features/events/components/EventsTable.tsx
index 5bd29c9246..9e3eef1781 100644
--- a/web/src/features/events/components/EventsTable.tsx
+++ b/web/src/features/events/components/EventsTable.tsx
@@ -61,8 +61,12 @@ import useColumnVisibility from "@/src/features/column-visibility/hooks/useColum
import { MemoizedIOTableCell } from "@/src/components/ui/IOTableCell";
import { useEventsTableData } from "@/src/features/events/hooks/useEventsTableData";
import { useEventsFilterOptions } from "@/src/features/events/hooks/useEventsFilterOptions";
-import { useEventsViewMode } from "@/src/features/events/hooks/useEventsViewMode";
+import {
+ useEventsViewMode,
+ type EventsViewMode,
+} from "@/src/features/events/hooks/useEventsViewMode";
import { EventsViewModeToggle } from "@/src/features/events/components/EventsViewModeToggle";
+import { useObservationCountCheck } from "@/src/features/events/hooks/useObservationCountCheck";
import { JsonSkeleton } from "@/src/components/ui/CodeJsonViewer";
import {
type RefreshInterval,
@@ -203,16 +207,29 @@ export default function ObservationsEventsTable({
const { viewMode, setViewMode: setViewModeRaw } =
useEventsViewMode(projectId);
+ // if the user explicitly choose to filter for trace, we don't want to auto switch them even if there are no "traces" aka root observations
+ const [userExplicitChoice, setUserExplicitChoice] =
+ useSessionStorage<EventsViewMode | null>(
+ `eventsViewModeUserChoice-${projectId}`,
+ null,
+ );
+
+ // Track date range(s) for which we've auto-switched (to avoid repeated switches)
+ const [autoSwitchedForRange, setAutoSwitchedForRange] = useSessionStorage<
+ string | null
+ >(`eventsAutoSwitchRange-${projectId}`, null);
+
// For filter options: trace mode filters to root items, observation mode shows all
const hasParentObservation = viewMode === "observation" ? undefined : false;
- // Wrap setViewMode to reset pagination when view mode changes
+ // Wrap setViewMode to reset pagination and track explicit user choice
const setViewMode = useCallback(
- (mode: typeof viewMode) => {
+ (mode: EventsViewMode) => {
+ setUserExplicitChoice(mode); // Track explicit choice
setViewModeRaw(mode);
setPaginationState({ page: 1 });
},
- [setViewModeRaw, setPaginationState],
+ [setUserExplicitChoice, setViewModeRaw, setPaginationState],
);
// for auto data refresh
@@ -353,6 +370,52 @@ export default function ObservationsEventsTable({
.concat(userIdFilter)
.concat(sessionIdFilter);
+ // Filter state WITHOUT viewModeFilter - for auto-switch observation count check
+ const filterStateWithoutViewMode = useMemo(() => {
+ const filters: FilterState = [...queryFilter.filterState];
+
+ if (dateRange?.from) {
+ filters.push({
+ column: "startTime",
+ type: "datetime",
+ operator: ">=",
+ value: dateRange.from,
+ });
+ }
+ if (dateRange?.to) {
+ filters.push({
+ column: "startTime",
+ type: "datetime",
+ operator: "<=",
+ value: dateRange.to,
+ });
+ }
+ if (userId) {
+ filters.push({
+ column: "User ID",
+ type: "string",
+ operator: "=",
+ value: userId,
+ });
+ }
+ if (sessionId) {
+ filters.push({
+ column: "Session ID",
+ type: "string",
+ operator: "=",
+ value: sessionId,
+ });
+ }
+
+ return filters;
+ }, [
+ queryFilter.filterState,
+ dateRange?.from,
+ dateRange?.to,
+ userId,
+ sessionId,
+ ]);
+
// Use the custom hook for observations data fetching
const {
observations,
@@ -372,6 +435,66 @@ export default function ObservationsEventsTable({
setSelectedRows,
});
+ // === Auto-switch to observation mode when trace view is empty ===
+
+ const dateRangeKey = useMemo(() => {
+ if (!dateRange?.from) return null;
+ return `${dateRange.from.getTime()}-${dateRange.to?.getTime() ?? "now"}`;
+ }, [dateRange]);
+
+ // Reset auto-switch tracking when date range changes
+ useEffect(() => {
+ if (
+ dateRangeKey &&
+ autoSwitchedForRange &&
+ dateRangeKey !== autoSwitchedForRange
+ ) {
+ setAutoSwitchedForRange(null);
+ }
+ }, [dateRangeKey, autoSwitchedForRange, setAutoSwitchedForRange]);
+
+ // Determine if we should check observation count for auto-switch
+ const shouldCheckObservationCount =
+ viewMode === "trace" &&
+ totalCount === 0 &&
+ observations.status === "success" &&
+ userExplicitChoice !== "trace" &&
+ dateRangeKey !== null &&
+ autoSwitchedForRange !== dateRangeKey;
+
+ const {
+ observationCount,
+ isSuccess: obsCountSuccess,
+ isPending: obsCountPending,
+ } = useObservationCountCheck({
+ projectId,
+ filterStateWithoutViewMode,
+ enabled: shouldCheckObservationCount,
+ });
+
+ // Auto-switch to observation mode when conditions are met
+ useEffect(() => {
+ if (
+ shouldCheckObservationCount &&
+ obsCountSuccess &&
+ observationCount !== null &&
+ observationCount > 0 &&
+ dateRangeKey
+ ) {
+ setViewModeRaw("observation");
+ setAutoSwitchedForRange(dateRangeKey);
+ }
+ }, [
+ shouldCheckObservationCount,
+ obsCountSuccess,
+ observationCount,
+ dateRangeKey,
+ setViewModeRaw,
+ setAutoSwitchedForRange,
+ ]);
+
+ const isAutoSwitchPending = shouldCheckObservationCount && obsCountPending;
+
useEffect(() => {
if (observations.status === "success") {
setDetailPageList(
@@ -1173,7 +1296,9 @@ export default function ObservationsEventsTable({
columns={columns}
peekView={peekConfig}
data={
- observations.status === "loading" || isViewLoading
+ observations.status === "loading" ||
+ isViewLoading ||
+ isAutoSwitchPending
? { isLoading: true, isError: false }
: observations.status === "error"
? {
diff --git a/web/src/features/events/hooks/useObservationCountCheck.ts b/web/src/features/events/hooks/useObservationCountCheck.ts
new file mode 100644
index 0000000000..5176257ba6
--- /dev/null
+++ b/web/src/features/events/hooks/useObservationCountCheck.ts
@@ -0,0 +1,38 @@
+import { api } from "@/src/utils/api";
+import { type FilterState } from "@langfuse/shared";
+
+/**
+ * Checks if there are root observations for current filter conditions.
+ * If no, default to the observation view mode instead.
+ */
+export function useObservationCountCheck({
+ projectId,
+ filterStateWithoutViewMode,
+ enabled,
+}: {
+ projectId: string;
+ filterStateWithoutViewMode: FilterState;
+ enabled: boolean;
+}) {
+ const query = api.events.countAll.useQuery(
+ {
+ projectId,
+ filter: filterStateWithoutViewMode,
+ searchQuery: null,
+ searchType: ["id", "content"],
+ page: 1,
+ limit: 1,
+ orderBy: null,
+ },
+ {
+ enabled,
+ staleTime: 60_000, // 1 minute stale time to prevent spam
+ },
+ );
+
+ return {
+ observationCount: query.data?.totalCount ?? null,
+ isSuccess: query.isSuccess,
+ isPending: query.isPending,
+ };
+}