🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(command-menu): navigate by tracing IDs (#16714)
Hassieb Pakzad4e90b14008cf
diff --git a/web/src/features/command-k-menu/CommandMenu.clienttest.ts b/web/src/features/command-k-menu/CommandMenu.clienttest.tsnew file mode 100644index 0000000000..5a680deadd--- /dev/null+++ b/web/src/features/command-k-menu/CommandMenu.clienttest.ts@@ -0,0 +1,63 @@+import { getIdNavigationItem, getSearchAnalytics } from "./CommandMenu";++describe("getIdNavigationItem", () => {+ it("filters traces by an exact 32-character hex ID", () => {+ expect(+ getIdNavigationItem("0123456789abcdef0123456789ABCDEF", "project-1"),+ ).toEqual({+ type: "trace_id",+ title: "Find trace by ID",+ url: "/project/project-1/traces?filter=id%3Bstring%3B%3B%3D%3B0123456789abcdef0123456789ABCDEF",+ });+ });++ it("filters v4 traces by the trace ID column", () => {+ expect(+ getIdNavigationItem(+ "0123456789abcdef0123456789ABCDEF",+ "project-1",+ true,+ ),+ ).toEqual({+ type: "trace_id",+ title: "Find trace by ID",+ url: "/project/project-1/traces?filter=traceId%3Bstring%3B%3Bcontains%3B0123456789abcdef0123456789ABCDEF",+ });+ });++ it("filters the v4 traces table by an exact observation ID", () => {+ expect(getIdNavigationItem("0123456789aBCDef", "project-1", true)).toEqual({+ type: "observation_id",+ title: "Find observation by ID",+ url: "/project/project-1/traces?filter=id%3Bstring%3B%3Bcontains%3B0123456789aBCDef",+ });+ });++ it("filters the v3 observations table by an exact observation ID", () => {+ expect(getIdNavigationItem("0123456789aBCDef", "project-1")).toEqual({+ type: "observation_id",+ title: "Find observation by ID",+ url: "/project/project-1/observations?filter=id%3BstringOptions%3B%3Bany%20of%3B0123456789aBCDef",+ });+ });++ it.each([+ ["", "project-1"],+ ["0123456789abcde", "project-1"],+ ["0123456789abcdefg", "project-1"],+ ["not-a-hex-id-123", "project-1"],+ ["0123456789abcdef", undefined],+ ])("does not match %j in project %j", (search, projectId) => {+ expect(getIdNavigationItem(search, projectId)).toBeNull();+ });++ it("classifies searches without retaining their raw value", () => {+ const search = "0123456789abcdef";++ expect(getSearchAnalytics(search)).toEqual({+ queryLength: 16,+ queryType: "observation_id",+ });+ expect(getSearchAnalytics(search)).not.toHaveProperty("search");+ });+});diff --git a/web/src/features/command-k-menu/CommandMenu.tsx b/web/src/features/command-k-menu/CommandMenu.tsxindex 88605ef929..99456bd198 100644--- a/web/src/features/command-k-menu/CommandMenu.tsx+++ b/web/src/features/command-k-menu/CommandMenu.tsx@@ -8,7 +8,7 @@ import {CommandSeparator,} from "@/src/components/ui/command";import { useRouter } from "next/router";-import { useEffect, memo } from "react";+import { useEffect, memo, useState } from "react";import { useSession } from "next-auth/react";import { env } from "@/src/env.mjs";import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";@@ -20,6 +20,59 @@ import { useAccountSettingsPages } from "@/src/pages/account/settings";import { useQueryProjectOrOrganization } from "@/src/features/projects/hooks";import { api } from "@/src/utils/api";import { type NavigationItem } from "@/src/components/layouts/utilities/routes";+import { useV4Beta } from "@/src/features/events/hooks/useV4Beta";++type IdNavigationItem = {+ type: "trace_id" | "observation_id";+ title: string;+ url: string;+};++const getIdType = (search: string): IdNavigationItem["type"] | null => {+ const id = search.trim();+ if (/^[0-9a-f]{32}$/i.test(id)) return "trace_id";+ if (/^[0-9a-f]{16}$/i.test(id)) return "observation_id";+ return null;+};++export const getIdNavigationItem = (+ search: string,+ projectId?: string,+ isV4 = false,+): IdNavigationItem | null => {+ if (!projectId) return null;++ const id = search.trim();+ const encodedProjectId = encodeURIComponent(projectId);+ const idType = getIdType(id);++ if (idType === "trace_id") {+ const filter = `${isV4 ? "traceId;string;;contains" : "id;string;;="};${encodeURIComponent(id)}`;+ return {+ type: "trace_id",+ title: "Find trace by ID",+ url: `/project/${encodedProjectId}/traces?filter=${encodeURIComponent(filter)}`,+ };+ }++ if (idType === "observation_id") {+ const filter = isV4+ ? `id;string;;contains;${encodeURIComponent(id)}`+ : `id;stringOptions;;any of;${encodeURIComponent(id)}`;+ return {+ type: "observation_id",+ title: "Find observation by ID",+ url: `/project/${encodedProjectId}/${isV4 ? "traces" : "observations"}?filter=${encodeURIComponent(filter)}`,+ };+ }++ return null;+};++export const getSearchAnalytics = (search: string) => ({+ queryLength: search.length,+ queryType: getIdType(search) ?? "other",+});function MainNavigationGroup({navItems,@@ -283,12 +336,19 @@ function CommandMenuComponent({}) {const { open, setOpen } = useCommandMenu();const capture = usePostHogClientCapture();+ const router = useRouter();+ const { project } = useQueryProjectOrOrganization();+ const { isBetaEnabled } = useV4Beta();+ const [search, setSearch] = useState("");+ const idNavigationItem = getIdNavigationItem(+ search,+ project?.id,+ isBetaEnabled,+ );const debouncedSearchChange = useDebounce((value: string) => {- capture("cmd_k_menu:search_entered", {- search: value,- });+ capture("cmd_k_menu:search_entered", getSearchAnalytics(value));},500,false,@@ -322,7 +382,9 @@ function CommandMenuComponent({source: "cmd_k",});}- setOpen(!open);+ const nextOpen = !open;+ setOpen(nextOpen);+ if (!nextOpen) setSearch("");}};document.addEventListener("keydown", down);@@ -330,13 +392,17 @@ function CommandMenuComponent({}, [capture, setOpen, open]);const handleNavigate = () => {+ setSearch("");setOpen(false);};return (<CommandDialogopen={open}- onOpenChange={setOpen}+ onOpenChange={(nextOpen) => {+ setOpen(nextOpen);+ if (!nextOpen) setSearch("");+ }}filter={(value, search, keywords) => {const extendValue = value + " " + keywords?.join(" ");const searchTerms = search.toLowerCase().split(" ");@@ -350,10 +416,30 @@ function CommandMenuComponent({<CommandInputplaceholder="Type a command or search..."className="border-none focus:border-none focus:ring-0 focus:ring-transparent focus:outline-hidden"- onValueChange={debouncedSearchChange}+ value={search}+ onValueChange={(value) => {+ setSearch(value);+ debouncedSearchChange(value);+ }}/><CommandList><CommandEmpty>No results found.</CommandEmpty>+ {idNavigationItem ? (+ <CommandGroup heading="Tracing">+ <CommandItem+ value={`${idNavigationItem.title} ${search}`}+ onSelect={() => {+ router.push(idNavigationItem.url);+ capture("cmd_k_menu:navigated", {+ type: idNavigationItem.type,+ });+ handleNavigate();+ }}+ >+ {idNavigationItem.title}+ </CommandItem>+ </CommandGroup>+ ) : null}<MainNavigationGroup navItems={navItems} onNavigate={handleNavigate} /><ProjectsGroup onNavigate={handleNavigate} /><DashboardsGroup onNavigate={handleNavigate} />