Skip to content

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

fix(sessions): Include trace name when searching (#19095)

Ben Bachem6b3b69cb3f0e
diff --git a/web/src/components/HighlightedText/HighlightedText.tsx b/web/src/components/HighlightedText/HighlightedText.tsx
new file mode 100644
index 0000000000..fe207d6912
--- /dev/null
+++ b/web/src/components/HighlightedText/HighlightedText.tsx
@@ -0,0 +1,16 @@
+import { useMemo, type ReactNode } from "react";
+import { splitHighlightedText } from "./fns/splitHighlightedText";
+
+export function HighlightedText({
+ text,
+ query,
+ children,
+}: {
+ text: string;
+ query: string;
+ children: (parts: ReturnType<typeof splitHighlightedText>) => ReactNode;
+}) {
+ const parts = useMemo(() => splitHighlightedText(text, query), [text, query]);
+
+ return children(parts);
+}
diff --git a/web/src/components/HighlightedText/fns/splitHighlightedText.clienttest.ts b/web/src/components/HighlightedText/fns/splitHighlightedText.clienttest.ts
new file mode 100644
index 0000000000..054d1f7673
--- /dev/null
+++ b/web/src/components/HighlightedText/fns/splitHighlightedText.clienttest.ts
@@ -0,0 +1,52 @@
+import { describe, expect, it } from "vitest";
+import { splitHighlightedText } from "./splitHighlightedText";
+
+describe("splitHighlightedText", () => {
+ it("matches case-insensitively and preserves original text", () => {
+ expect(splitHighlightedText("One TWO two!", "two")).toEqual([
+ { text: "One ", isMatch: false },
+ { text: "TWO", isMatch: true },
+ { text: " ", isMatch: false },
+ { text: "two", isMatch: true },
+ { text: "!", isMatch: false },
+ ]);
+ });
+
+ it("treats regex metacharacters as literal text", () => {
+ expect(splitHighlightedText("a [b]+ c", "[b]+")).toEqual([
+ { text: "a ", isMatch: false },
+ { text: "[b]+", isMatch: true },
+ { text: " c", isMatch: false },
+ ]);
+ });
+
+ it("preserves original offsets when lowercasing expands a character", () => {
+ expect(splitHighlightedText("İ hello", "hello")).toEqual([
+ { text: "İ ", isMatch: false },
+ { text: "hello", isMatch: true },
+ { text: "", isMatch: false },
+ ]);
+ });
+
+ it("highlights İstanbul when searching for i, matching the search filter", () => {
+ const text = "İstanbul";
+ const query = "i";
+
+ expect(text.toLowerCase().includes(query.toLowerCase())).toBe(true);
+ expect(splitHighlightedText(text, query)).toEqual([
+ { text: "", isMatch: false },
+ { text: "İ", isMatch: true },
+ { text: "stanbul", isMatch: false },
+ ]);
+ });
+
+ it.each([
+ ["hello", ""],
+ ["hello", "absent"],
+ ["", "query"],
+ ])("leaves %j unchanged for query %j", (text, query) => {
+ expect(splitHighlightedText(text, query)).toEqual([
+ { text, isMatch: false },
+ ]);
+ });
+});
diff --git a/web/src/components/HighlightedText/fns/splitHighlightedText.ts b/web/src/components/HighlightedText/fns/splitHighlightedText.ts
new file mode 100644
index 0000000000..e564535d1e
--- /dev/null
+++ b/web/src/components/HighlightedText/fns/splitHighlightedText.ts
@@ -0,0 +1,38 @@
+export function splitHighlightedText(text: string, query: string) {
+ if (!query) return [{ text, isMatch: false }];
+
+ const normalizedText = text.toLowerCase();
+ const normalizedQuery = query.toLowerCase();
+ // Case folding can expand characters; slices must use original UTF-16 offsets.
+ const offsets: { start: number; end: number }[] = [];
+ if (normalizedText.length !== text.length) {
+ let offset = 0;
+ for (const character of text) {
+ const end = offset + character.length;
+ for (let index = 0; index < character.toLowerCase().length; index++) {
+ offsets.push({ start: offset, end });
+ }
+ offset = end;
+ }
+ }
+ const parts: { text: string; isMatch: boolean }[] = [];
+ let position = 0;
+ let matchIndex = normalizedText.indexOf(normalizedQuery);
+
+ while (matchIndex !== -1) {
+ const start = offsets[matchIndex]?.start ?? matchIndex;
+ const normalizedEnd = matchIndex + normalizedQuery.length;
+ const end = offsets[normalizedEnd - 1]?.end ?? normalizedEnd;
+ if (start < position) {
+ matchIndex = normalizedText.indexOf(normalizedQuery, normalizedEnd);
+ continue;
+ }
+ parts.push({ text: text.slice(position, start), isMatch: false });
+ parts.push({ text: text.slice(start, end), isMatch: true });
+ position = end;
+ matchIndex = normalizedText.indexOf(normalizedQuery, normalizedEnd);
+ }
+
+ parts.push({ text: text.slice(position), isMatch: false });
+ return parts;
+}
diff --git a/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/SessionConversationalView.tsx b/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/SessionConversationalView.tsx
index 982e3cb4a6..bf42273dfb 100644
--- a/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/SessionConversationalView.tsx
+++ b/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/SessionConversationalView.tsx
@@ -199,15 +199,21 @@ export function SessionConversationalView(
];
});
})();
+ const matchesTraceName =
+ props.searchQuery.length > 0 &&
+ (item.trace.name ?? "")
+ .toLowerCase()
+ .includes(props.searchQuery.toLowerCase());
const matchingRows =
- props.searchQuery && transcriptRows
+ props.searchQuery && !matchesTraceName && transcriptRows
? transcriptRows.filter((row) =>
`${row.role} ${row.label}`
.toLowerCase()
.includes(props.searchQuery.toLowerCase()),
)
: transcriptRows;
- if (props.searchQuery && matchingRows?.length === 0) continue;
+ if (props.searchQuery && !matchesTraceName && matchingRows?.length === 0)
+ continue;
const threadVisibility =
item.state.type === "transcript"
? getSessionTranscriptThreads(item.state.result.transcript)
diff --git a/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/components/SessionConversationSidebar/SessionConversationSidebar.tsx b/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/components/SessionConversationSidebar/SessionConversationSidebar.tsx
index 7fc70a9681..cadf42b6e3 100644
--- a/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/components/SessionConversationSidebar/SessionConversationSidebar.tsx
+++ b/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/components/SessionConversationSidebar/SessionConversationSidebar.tsx
@@ -19,6 +19,7 @@ import {
IDLE_GAP_THRESHOLD_SECONDS,
} from "@/src/features/sessions/sessionIdleGap";
import { cn } from "@/src/utils/tailwind";
+import { HighlightedText } from "@/src/components/HighlightedText/HighlightedText";
import { groupConsecutiveTools } from "@/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/fns/groupConsecutiveTools";
export type SessionConversationSidebarTrace = {
@@ -427,7 +428,25 @@ export function SessionConversationSidebar(
className="min-w-0 flex-1 truncate text-[13px] font-bold"
title={trace.name ?? "Trace"}
>
- {trace.name ?? "Trace"}
+ <HighlightedText
+ text={trace.name ?? "Trace"}
+ query={searchQuery}
+ >
+ {(parts) =>
+ parts.map(({ text, isMatch }, index) =>
+ isMatch ? (
+ <mark
+ key={index}
+ className="bg-find-match-background text-foreground"
+ >
+ {text}
+ </mark>
+ ) : (
+ text
+ ),
+ )
+ }
+ </HighlightedText>
</span>
</button>
<button
@@ -631,34 +650,30 @@ export function SessionConversationSidebar(
),
);
const text = row.label.slice(start, end);
- const normalizedText = text.toLowerCase();
if (start > 0) excerpt.push("…");
- let position = 0;
- while (position < text.length) {
- const nextMatch = normalizedText.indexOf(
- normalizedQuery,
- position,
- );
- if (nextMatch === -1) {
- excerpt.push(text.slice(position));
- break;
- }
- excerpt.push(
- text.slice(position, nextMatch),
- );
- excerpt.push(
- <mark
- key={nextMatch}
- className="bg-find-match-background text-foreground"
- >
- {text.slice(
- nextMatch,
- nextMatch + searchQuery.length,
- )}
- </mark>,
- );
- position = nextMatch + searchQuery.length;
- }
+ excerpt.push(
+ <HighlightedText
+ key="text"
+ text={text}
+ query={searchQuery}
+ >
+ {(parts) =>
+ parts.map(
+ ({ text, isMatch }, index) =>
+ isMatch ? (
+ <mark
+ key={index}
+ className="bg-find-match-background text-foreground"
+ >
+ {text}
+ </mark>
+ ) : (
+ text
+ ),
+ )
+ }
+ </HighlightedText>,
+ );
if (end < row.label.length)
excerpt.push("…");
}
diff --git a/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/components/SessionConversationTimeline/components/SessionConversationTimelineTrace/SessionConversationTimelineTrace.tsx b/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/components/SessionConversationTimeline/components/SessionConversationTimelineTrace/SessionConversationTimelineTrace.tsx
index a025e6de53..387cbe142f 100644
--- a/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/components/SessionConversationTimeline/components/SessionConversationTimelineTrace/SessionConversationTimelineTrace.tsx
+++ b/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/components/SessionConversationTimeline/components/SessionConversationTimelineTrace/SessionConversationTimelineTrace.tsx
@@ -80,7 +80,9 @@ export function SessionConversationTimelineTrace({
className="truncate"
title={`${trace.name ?? "Trace"} · ${trace.id}`}
>
- {trace.name ?? "Trace"} · {trace.id}
+ <span data-session-search-content>{trace.name ?? "Trace"}</span>
+ {" · "}
+ {trace.id}
</span>
</button>
<div className="border-border min-w-0 flex-1 border-t border-dashed" />