Skip to content

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

feat(agent-graph): cycle through observations by clicking graph nodes… (#9139)

Nimar07352dbf947c
diff --git a/web/src/features/trace-graph-view/buildGraphCanvasData.ts b/web/src/features/trace-graph-view/buildGraphCanvasData.ts
index 8a304fbb7c..aa224b0c56 100644
--- a/web/src/features/trace-graph-view/buildGraphCanvasData.ts
+++ b/web/src/features/trace-graph-view/buildGraphCanvasData.ts
@@ -10,7 +10,7 @@ import {
export interface GraphParseResult {
graph: GraphCanvasData;
- nodeToParentObservationMap: Record<string, string>;
+ nodeToObservationsMap: Record<string, string[]>;
}
export function transformLanggraphToGeneralized(
@@ -89,12 +89,12 @@ export function buildGraphFromStepData(
if (data.length === 0) {
return {
graph: { nodes: [], edges: [] },
- nodeToParentObservationMap: {},
+ nodeToObservationsMap: {},
};
}
const stepToNodesMap = new Map<number, Set<string>>();
- const nodeToParentObservationMap = new Map<string, string>();
+ const nodeToObservationsMap = new Map<string, string[]>();
data.forEach((obs) => {
const { node, step } = obs;
@@ -108,20 +108,34 @@ export function buildGraphFromStepData(
if (obs.parentObservationId) {
const parent = data.find((o) => o.id === obs.parentObservationId);
- // initialize the end node to point to the top-most span
- if (!parent) {
- nodeToParentObservationMap.set(
- LANGFUSE_END_NODE_NAME,
- obs.parentObservationId,
- );
+ // initialize the end node to point to the top-most span (only if no node field)
+ if (!parent && node === null) {
+ if (!nodeToObservationsMap.has(LANGFUSE_END_NODE_NAME)) {
+ nodeToObservationsMap.set(LANGFUSE_END_NODE_NAME, []);
+ }
+ nodeToObservationsMap.get(LANGFUSE_END_NODE_NAME)!.push(obs.id);
}
// Only register id if it is top-most to allow navigation on node click in graph
if (obs.name !== parent?.name && node !== null) {
- nodeToParentObservationMap.set(node, obs.id);
+ if (!nodeToObservationsMap.has(node)) {
+ nodeToObservationsMap.set(node, []);
+ }
+ nodeToObservationsMap.get(node)!.push(obs.id);
}
} else if (node !== null) {
- nodeToParentObservationMap.set(node, obs.id);
+ const isSystemNode =
+ node === LANGFUSE_START_NODE_NAME ||
+ node === LANGFUSE_END_NODE_NAME ||
+ node === LANGGRAPH_START_NODE_NAME ||
+ node === LANGGRAPH_END_NODE_NAME;
+
+ if (!isSystemNode) {
+ if (!nodeToObservationsMap.has(node)) {
+ nodeToObservationsMap.set(node, []);
+ }
+ nodeToObservationsMap.get(node)!.push(obs.id);
+ }
}
});
@@ -154,9 +168,7 @@ export function buildGraphFromStepData(
return {
graph: { nodes, edges },
- nodeToParentObservationMap: Object.fromEntries(
- nodeToParentObservationMap.entries(),
- ),
+ nodeToObservationsMap: Object.fromEntries(nodeToObservationsMap.entries()),
};
}
diff --git a/web/src/features/trace-graph-view/components/TraceGraphCanvas.tsx b/web/src/features/trace-graph-view/components/TraceGraphCanvas.tsx
index 887ecb6cc1..ac8dd69a73 100644
--- a/web/src/features/trace-graph-view/components/TraceGraphCanvas.tsx
+++ b/web/src/features/trace-graph-view/components/TraceGraphCanvas.tsx
@@ -1,5 +1,5 @@
import React, { useEffect, useRef, useMemo, useState } from "react";
-import { Network } from "vis-network/standalone";
+import { Network, DataSet } from "vis-network/standalone";
import { ZoomIn, ZoomOut, RotateCcw } from "lucide-react";
import type { GraphCanvasData } from "../types";
@@ -16,6 +16,8 @@ type TraceGraphCanvasProps = {
selectedNodeName: string | null;
onCanvasNodeNameChange: (nodeName: string | null) => void;
disablePhysics?: boolean;
+ nodeToObservationsMap?: Record<string, string[]>;
+ currentObservationIndices?: Record<string, number>;
};
export const TraceGraphCanvas: React.FC<TraceGraphCanvasProps> = (props) => {
@@ -24,11 +26,20 @@ export const TraceGraphCanvas: React.FC<TraceGraphCanvasProps> = (props) => {
selectedNodeName,
onCanvasNodeNameChange,
disablePhysics = false,
+ nodeToObservationsMap = {},
+ currentObservationIndices = {},
} = props;
const [isHovering, setIsHovering] = useState(false);
const containerRef = useRef<HTMLDivElement>(null);
const networkRef = useRef<Network | null>(null);
+ const nodesDataSetRef = useRef<DataSet<any> | null>(null);
+ const onCanvasNodeNameChangeRef = useRef(onCanvasNodeNameChange);
+
+ // Keep ref up to date without triggering Network recreation
+ useEffect(() => {
+ onCanvasNodeNameChangeRef.current = onCanvasNodeNameChange;
+ }, [onCanvasNodeNameChange]);
const getNodeStyle = (nodeType: string) => {
switch (nodeType) {
@@ -252,20 +263,27 @@ export const TraceGraphCanvas: React.FC<TraceGraphCanvasProps> = (props) => {
return;
}
+ const nodesDataSet = new DataSet(nodes);
+ nodesDataSetRef.current = nodesDataSet;
+
// Create the network
const network = new Network(
containerRef.current,
- { ...graphData, nodes },
+ { ...graphData, nodes: nodesDataSet },
options,
);
networkRef.current = network;
- network.on("selectNode", (params) => {
- onCanvasNodeNameChange(params.nodes[0]);
- });
-
- network.on("deselectNode", () => {
- onCanvasNodeNameChange(null);
+ // Use click event instead of selectNode/deselectNode to handle cycling properly
+ network.on("click", (params) => {
+ if (params.nodes.length > 0) {
+ // Node was clicked
+ onCanvasNodeNameChangeRef.current(params.nodes[0]);
+ } else {
+ // Empty area was clicked
+ onCanvasNodeNameChangeRef.current(null);
+ network.unselectAll();
+ }
});
// Prevent dragging the view completely out of bounds
@@ -345,9 +363,46 @@ export const TraceGraphCanvas: React.FC<TraceGraphCanvasProps> = (props) => {
return () => {
window.removeEventListener("resize", handleResize);
networkRef.current = null;
+ nodesDataSetRef.current = null;
network.destroy();
};
- }, [graphData, nodes, options, onCanvasNodeNameChange]);
+ }, [graphData, nodes, options]);
+
+ // Update node labels when observation indices change, without recreating network
+ useEffect(() => {
+ const nodesDataSet = nodesDataSetRef.current;
+ if (!nodesDataSet) return;
+
+ try {
+ const updates: { id: string; label: string }[] = [];
+
+ graphData.nodes.forEach((node) => {
+ const isSystemNode =
+ node.id === LANGFUSE_START_NODE_NAME ||
+ node.id === LANGFUSE_END_NODE_NAME ||
+ node.id === LANGGRAPH_START_NODE_NAME ||
+ node.id === LANGGRAPH_END_NODE_NAME;
+
+ if (isSystemNode) return;
+
+ const observations = nodeToObservationsMap[node.id] || [];
+ const currentIndex = currentObservationIndices[node.id] || 0;
+ const counter =
+ observations.length > 1
+ ? ` (${observations.length - currentIndex}/${observations.length})`
+ : "";
+
+ const newLabel = `${node.label}${counter}`;
+ updates.push({ id: node.id, label: newLabel });
+ });
+
+ if (updates.length > 0) {
+ nodesDataSet.update(updates);
+ }
+ } catch (error) {
+ console.error("Error updating node labels:", error);
+ }
+ }, [graphData.nodes, nodeToObservationsMap, currentObservationIndices]);
useEffect(() => {
const network = networkRef.current;
diff --git a/web/src/features/trace-graph-view/components/TraceGraphView.tsx b/web/src/features/trace-graph-view/components/TraceGraphView.tsx
index fc8c3b194d..a452881181 100644
--- a/web/src/features/trace-graph-view/components/TraceGraphView.tsx
+++ b/web/src/features/trace-graph-view/components/TraceGraphView.tsx
@@ -1,4 +1,10 @@
-import React, { useEffect, useState, useMemo, useCallback } from "react";
+import React, {
+ useEffect,
+ useState,
+ useMemo,
+ useCallback,
+ useRef,
+} from "react";
import { StringParam, useQueryParam } from "use-query-params";
import { TraceGraphCanvas } from "./TraceGraphCanvas";
@@ -8,6 +14,12 @@ import {
buildGraphFromStepData,
transformLanggraphToGeneralized,
} from "../buildGraphCanvasData";
+import {
+ LANGFUSE_START_NODE_NAME,
+ LANGFUSE_END_NODE_NAME,
+ LANGGRAPH_START_NODE_NAME,
+ LANGGRAPH_END_NODE_NAME,
+} from "../types";
const MAX_NODE_NUMBER_FOR_PHYSICS = 500;
@@ -23,6 +35,13 @@ export const TraceGraphView: React.FC<TraceGraphViewProps> = ({
"observation",
StringParam,
);
+ const [currentObservationIndices, setCurrentObservationIndices] = useState<{
+ [nodeName: string]: number;
+ }>({});
+ const [previousSelectedNode, setPreviousSelectedNode] = useState<
+ string | null
+ >(null);
+ const isClickNavigationRef = useRef(false);
const normalizedData = useMemo(() => {
const hasStepData = agentGraphData.some(
@@ -44,43 +63,116 @@ export const TraceGraphView: React.FC<TraceGraphViewProps> = ({
}
}, [agentGraphData]);
- const { graph, nodeToParentObservationMap } = useMemo(() => {
+ const { graph, nodeToObservationsMap } = useMemo(() => {
return buildGraphFromStepData(normalizedData);
}, [normalizedData]);
const shouldDisablePhysics =
agentGraphData.length >= MAX_NODE_NUMBER_FOR_PHYSICS;
+ // Reset indices when graph data changes (new trace loaded)
useEffect(() => {
- const nodeName = Object.keys(nodeToParentObservationMap).find(
- (nodeKey) => nodeToParentObservationMap[nodeKey] === currentObservationId,
- );
+ setCurrentObservationIndices({});
+ }, [normalizedData]);
- // Only set selectedNodeName if the node actually exists in the graph
- if (nodeName && graph.nodes.some((node) => node.id === nodeName)) {
- setSelectedNodeName(nodeName);
+ useEffect(() => {
+ // if this observation ID change came from a click -> skip
+ if (isClickNavigationRef.current) {
+ isClickNavigationRef.current = false;
+ return;
+ }
+
+ // Find which node and index corresponds to currentObservationId
+ let foundNodeName = null;
+ let foundIndex = 0;
+
+ for (const [nodeName, observations] of Object.entries(
+ nodeToObservationsMap,
+ )) {
+ const index = observations.findIndex(
+ (obsId) => obsId === currentObservationId,
+ );
+ if (index !== -1) {
+ foundNodeName = nodeName;
+ foundIndex = index;
+ break;
+ }
+ }
+
+ if (
+ foundNodeName &&
+ graph.nodes.some((node) => node.id === foundNodeName)
+ ) {
+ setSelectedNodeName(foundNodeName);
+ setCurrentObservationIndices((prev) => ({
+ ...prev,
+ [foundNodeName]: foundIndex,
+ }));
+ setPreviousSelectedNode(foundNodeName);
} else {
setSelectedNodeName(null);
+ setPreviousSelectedNode(null);
}
}, [
currentObservationId,
agentGraphData,
graph.nodes,
- nodeToParentObservationMap,
+ nodeToObservationsMap,
]);
const onCanvasNodeNameChange = useCallback(
(nodeName: string | null) => {
- setSelectedNodeName(nodeName);
-
if (nodeName) {
- const nodeParentObservationId = nodeToParentObservationMap[nodeName];
+ // Don't cycle through system nodes (start/end nodes)
+ const isSystemNode =
+ nodeName === LANGFUSE_START_NODE_NAME ||
+ nodeName === LANGFUSE_END_NODE_NAME ||
+ nodeName === LANGGRAPH_START_NODE_NAME ||
+ nodeName === LANGGRAPH_END_NODE_NAME;
+
+ if (isSystemNode) {
+ // For system nodes, don't set observation ID (they're synthetic)
+ setPreviousSelectedNode(nodeName);
+ setSelectedNodeName(nodeName);
+ isClickNavigationRef.current = true;
+ setCurrentObservationId(null);
+ return;
+ }
+
+ const observations = nodeToObservationsMap[nodeName] || [];
- if (nodeParentObservationId)
- setCurrentObservationId(nodeParentObservationId);
+ if (observations.length > 0) {
+ let targetIndex = 0;
+
+ // If clicking the same node as before, cycle to next observation
+ if (previousSelectedNode === nodeName && observations.length > 1) {
+ const currentIndex = currentObservationIndices[nodeName] || 0;
+ targetIndex = (currentIndex + 1) % observations.length;
+ }
+
+ setCurrentObservationIndices((prev) => ({
+ ...prev,
+ [nodeName]: targetIndex,
+ }));
+ isClickNavigationRef.current = true;
+ setCurrentObservationId(observations[targetIndex]);
+ } else {
+ isClickNavigationRef.current = true;
+ setCurrentObservationId(null);
+ }
+ setPreviousSelectedNode(nodeName);
+ } else {
+ setPreviousSelectedNode(null);
}
+
+ setSelectedNodeName(nodeName);
},
- [nodeToParentObservationMap, setCurrentObservationId],
+ [
+ nodeToObservationsMap,
+ currentObservationIndices,
+ previousSelectedNode,
+ setCurrentObservationId,
+ ],
);
return (
@@ -90,6 +182,8 @@ export const TraceGraphView: React.FC<TraceGraphViewProps> = ({
selectedNodeName={selectedNodeName}
onCanvasNodeNameChange={onCanvasNodeNameChange}
disablePhysics={shouldDisablePhysics}
+ nodeToObservationsMap={nodeToObservationsMap}
+ currentObservationIndices={currentObservationIndices}
/>
</div>
);