Skip to content

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

feat(web): jump to prompts, datasets, queues and evaluators from Cmd+K (#18921)

Trang Nguyen07e45044a5ab
diff --git a/web/src/features/command-k-menu/CommandMenu.clienttest.ts b/web/src/features/command-k-menu/CommandMenu.clienttest.ts
index 150c59e2c8..66085c5108 100644
--- a/web/src/features/command-k-menu/CommandMenu.clienttest.ts
+++ b/web/src/features/command-k-menu/CommandMenu.clienttest.ts
@@ -1,9 +1,13 @@
import {
addRecentEntry,
buildCreateActions,
+ buildPageShortcuts,
+ getEntityBaseUrl,
getIdNavigationItem,
+ getRecentPickAnalytics,
getSearchAnalytics,
getVisibleRecents,
+ toEntityGroup,
} from "./CommandMenu";
describe("getIdNavigationItem", () => {
@@ -89,7 +93,7 @@ describe("addRecentEntry", () => {
});
describe("getVisibleRecents", () => {
- it("hides pages and create actions the menu no longer offers", () => {
+ it("hides pages, create actions and entities the menu no longer offers", () => {
const recents = [
{ title: "Prompts", url: "/p/prompts", type: "main_navigation" },
{ title: "Alerts", url: "/p/alerts", type: "main_navigation" },
@@ -100,14 +104,29 @@ describe("getVisibleRecents", () => {
},
{ title: "New prompt", url: "/p/prompts/new", type: "create_action" },
{ title: "Cost", url: "/p/dashboards/1", type: "dashboard" },
+ { title: "greeting", url: "/p/prompts/greeting", type: "prompt" },
+ { title: "golden", url: "/p/datasets/d1", type: "dataset" },
];
expect(
getVisibleRecents(recents, {
navUrls: new Set(["/p/prompts"]),
createUrls: new Set(["/p/prompts/new"]),
+ entityBaseUrls: ["/p/prompts"],
}).map((e) => e.title),
- ).toEqual(["Prompts", "New prompt", "Cost"]);
+ ).toEqual(["Prompts", "New prompt", "Cost", "greeting"]);
+ });
+});
+
+describe("getRecentPickAnalytics", () => {
+ it("reports entity picks by type only", () => {
+ expect(
+ getRecentPickAnalytics({
+ title: "acme/secret-prompt",
+ url: "/p/prompts/acme%2Fsecret-prompt",
+ type: "prompt",
+ }),
+ ).toEqual({ type: "recent", entityType: "prompt" });
});
});
@@ -145,3 +164,127 @@ describe("buildCreateActions", () => {
]);
});
});
+
+describe("buildPageShortcuts", () => {
+ const navItems = [
+ { href: "/project/[projectId]/dashboards", url: "/project/p1/dashboards" },
+ { href: "/project/[projectId]/evals", url: "/project/p1/evals" },
+ ];
+
+ it("lists hidden pages under their sidebar page, gated by read scope", () => {
+ const shortcuts = buildPageShortcuts({
+ navItems,
+ projectId: "p1",
+ canRead: (scope) => scope !== "evalDefaultModel:read",
+ });
+
+ expect(
+ [...shortcuts.byNavUrl.entries()].map(([navUrl, items]) => [
+ navUrl,
+ items.map((item) => item.url),
+ ]),
+ ).toEqual([
+ ["/project/p1/dashboards", ["/project/p1/widgets"]],
+ ["/project/p1/evals", ["/project/p1/evals/rules"]],
+ ]);
+ expect(shortcuts.standalone.map((item) => item.url)).toEqual([
+ "/project/p1/automations",
+ ]);
+ });
+
+ it("skips v4-only pages when the sidebar links to the legacy page", () => {
+ const shortcuts = buildPageShortcuts({
+ navItems: [
+ { href: "/project/[projectId]/evals", url: "/project/p1/evals/legacy" },
+ ],
+ projectId: "p1",
+ canRead: () => true,
+ });
+
+ expect(
+ shortcuts.byNavUrl
+ .get("/project/p1/evals/legacy")
+ ?.map((item) => item.url),
+ ).toEqual(["/project/p1/evals/default-model"]);
+ });
+
+ it("offers nothing the user cannot read", () => {
+ const shortcuts = buildPageShortcuts({
+ navItems,
+ projectId: "p1",
+ canRead: () => false,
+ });
+
+ expect(shortcuts.byNavUrl.size).toBe(0);
+ expect(shortcuts.standalone).toEqual([]);
+ });
+});
+
+describe("getEntityBaseUrl", () => {
+ const navItems = [
+ { href: "/project/[projectId]/prompts", url: "/project/p1/prompts" },
+ { href: "/project/[projectId]/evals", url: "/project/p1/evals/legacy" },
+ ];
+
+ it("uses the sidebar URL when the page is visible and readable", () => {
+ expect(
+ getEntityBaseUrl({
+ type: "prompt",
+ navItems,
+ projectId: "p1",
+ canRead: (scope) => scope === "prompts:read",
+ }),
+ ).toBe("/project/p1/prompts");
+ });
+
+ it.each<[Parameters<typeof getEntityBaseUrl>[0]["type"], boolean]>([
+ ["prompt", false],
+ ["dataset", true],
+ ["evaluator", true],
+ ])("is null for %s when unreadable, hidden or legacy", (type, readable) => {
+ expect(
+ getEntityBaseUrl({
+ type,
+ navItems,
+ projectId: "p1",
+ canRead: () => readable,
+ }),
+ ).toBeNull();
+ });
+});
+
+describe("toEntityGroup", () => {
+ it("links prompts by encoded name and other entities by ID", () => {
+ const row = { id: "c1", name: "support/greeting v2" };
+
+ expect(
+ toEntityGroup("prompt", "/project/p1/prompts", [row], "").items[0]?.url,
+ ).toBe("/project/p1/prompts/support%2Fgreeting%20v2");
+ expect(
+ toEntityGroup("dataset", "/project/p1/datasets", [row], "").items[0]?.url,
+ ).toBe("/project/p1/datasets/c1");
+ });
+
+ it("caps matches at 100, so rows past the cap stay findable", () => {
+ const rows = Array.from({ length: 150 }, (_, i) => ({
+ id: `id-${i}`,
+ name: `n-${i}`,
+ }));
+
+ expect(toEntityGroup("annotation_queue", "/q", rows, "").items.length).toBe(
+ 100,
+ );
+ expect(
+ toEntityGroup("annotation_queue", "/q", rows, "queue n-149").items.map(
+ (item) => item.name,
+ ),
+ ).toEqual(["n-149"]);
+ });
+
+ it("is empty when the page is gated off, even with cached rows", () => {
+ expect(
+ toEntityGroup("evaluator", null, [{ id: "e1", name: "Toxicity" }], "")
+ .items,
+ ).toEqual([]);
+ });
+});
diff --git a/web/src/features/command-k-menu/CommandMenu.tsx b/web/src/features/command-k-menu/CommandMenu.tsx
index 0c80306492..baef94727b 100644
--- a/web/src/features/command-k-menu/CommandMenu.tsx
+++ b/web/src/features/command-k-menu/CommandMenu.tsx
@@ -12,16 +12,22 @@ import {
import { useRouter } from "next/router";
import {
BotMessageSquare,
+ ClipboardPen,
+ Database,
+ FileJson,
Grid2X2,
LayoutDashboard,
+ Lightbulb,
ListTree,
Moon,
Plus,
Settings,
Sun,
+ Zap,
type IconComponent,
} from "@langfuse/icons";
import { useTheme } from "next-themes";
+import { keepPreviousData } from "@tanstack/react-query";
import { Fragment, useEffect, memo, useState } from "react";
import { useSession } from "next-auth/react";
import { env } from "@/src/env.mjs";
@@ -97,9 +103,64 @@ export const getSearchAnalytics = (search: string) => ({
const RECENTS_LIMIT = 4;
+const ENTITY_LIMIT = 100;
+
+const ENTITY_GROUPS: Record<
+ EntityType,
+ {
+ heading: string;
+ navHref: string;
+ scope: ProjectScope;
+ icon: IconComponent;
+ keywords: string[];
+ /** Path segment after the page URL that identifies one item. */
+ pathKey: "id" | "name";
+ v4Only?: boolean;
+ }
+> = {
+ prompt: {
+ heading: "Prompts",
+ navHref: "/project/[projectId]/prompts",
+ scope: "prompts:read",
+ icon: FileJson,
+ keywords: ["prompt"],
+ pathKey: "name",
+ },
+ dataset: {
+ heading: "Datasets",
+ navHref: "/project/[projectId]/datasets",
+ scope: "datasets:read",
+ icon: Database,
+ keywords: ["dataset"],
+ pathKey: "id",
+ },
+ annotation_queue: {
+ heading: "Annotation queues",
+ navHref: "/project/[projectId]/annotation-queues",
+ scope: "annotationQueues:read",
+ icon: ClipboardPen,
+ keywords: ["annotation", "queue", "human"],
+ pathKey: "id",
+ },
+ evaluator: {
+ heading: "Evaluators",
+ navHref: "/project/[projectId]/evals",
+ scope: "evaluator:read",
+ icon: Lightbulb,
+ keywords: ["evaluator", "eval"],
+ pathKey: "id",
+ // Legacy projects list job configs, which live under other IDs.
+ v4Only: true,
+ },
+};
+
const RECENT_ICON_BY_TYPE: Record<string, IconComponent> = {
create_action: Plus,
dashboard: LayoutDashboard,
+ prompt: FileJson,
+ dataset: Database,
+ annotation_queue: ClipboardPen,
+ evaluator: Lightbulb,
project: Grid2X2,
project_settings: Settings,
organization_settings: Settings,
@@ -140,6 +201,39 @@ const NAV_CREATE_ACTIONS: Array<{
},
];
+// Pages without a sidebar entry, listed under the sidebar page they belong to.
+const NAV_PAGE_SHORTCUTS: Array<{
+ navHref: string;
+ path: string;
+ title: string;
+ scope: ProjectScope;
+ keywords: string[];
+ v4Only?: boolean;
+}> = [
+ {
+ navHref: "/project/[projectId]/dashboards",
+ path: "widgets",
+ title: "Widgets",
+ scope: "dashboards:read",
+ keywords: ["widget", "chart", "dashboard"],
+ },
+ {
+ navHref: "/project/[projectId]/evals",
+ path: "evals/rules",
+ title: "Evaluator rules",
+ scope: "evaluationRule:read",
+ keywords: ["evaluator", "eval", "rule", "filter", "sampling"],
+ v4Only: true,
+ },
+ {
+ navHref: "/project/[projectId]/evals",
+ path: "evals/default-model",
+ title: "Default evaluation model",
+ scope: "evalDefaultModel:read",
+ keywords: ["evaluator", "eval", "default", "model", "llm"],
+ },
+];
+
function RecentGroup({
recents,
iconByUrl,
@@ -183,11 +277,7 @@ function RecentItem({
const handleSelect = () => {
router.push(entry.url);
- capture("cmd_k_menu:navigated", {
- type: "recent",
- title: entry.title,
- url: entry.url,
- });
+ capture("cmd_k_menu:navigated", getRecentPickAnalytics(entry));
onNavigate(entry);
};
@@ -289,14 +379,16 @@ function ThemeItem({ onDone }: { onDone: () => void }) {
function MainNavigationGroup({
navItems,
+ pageShortcuts,
createActions,
- showCreateActions,
+ isSearching,
onNavigate,
onDone,
}: {
- navItems: Array<{ title: string; url: string; icon?: IconComponent }>;
+ navItems: NavMenuItem[];
+ pageShortcuts: PageShortcuts;
createActions: CreateActions;
- showCreateActions: boolean;
+ isSearching: boolean;
onNavigate: (entry: RecentEntry) => void;
onDone: () => void;
}) {
@@ -306,36 +398,61 @@ function MainNavigationGroup({
{navItems.map((item) => (
<Fragment key={item.url}>
<MainNavigationItem item={item} onNavigate={onNavigate} />
- {showCreateActions &&
- createActions.byNavUrl
- .get(item.url)
- ?.map((action) => (
- <CreateActionItem
- key={action.url}
- action={action}
- onNavigate={onNavigate}
- />
- ))}
+ {isSearching && (
+ <NavItemExtras
+ shortcuts={pageShortcuts.byNavUrl.get(item.url) ?? []}
+ actions={createActions.byNavUrl.get(item.url) ?? []}
+ onNavigate={onNavigate}
+ />
+ )}
</Fragment>
))}
- {showCreateActions &&
- createActions.standalone.map((action) => (
- <CreateActionItem
- key={action.url}
- action={action}
- onNavigate={onNavigate}
- />
- ))}
- {showCreateActions && <ThemeItem onDone={onDone} />}
+ {isSearching && (
+ <NavItemExtras
+ shortcuts={pageShortcuts.standalone}
+ actions={createActions.standalone}
+ onNavigate={onNavigate}
+ />
+ )}
+ {isSearching && <ThemeItem onDone={onDone} />}
</CommandGroup>
);
}
+function NavItemExtras({
+ shortcuts,
+ actions,
+ onNavigate,
+}: {
+ shortcuts: NavMenuItem[];
+ actions: CreateAction[];
+ onNavigate: (entry: RecentEntry) => void;
+}) {
+ return (
+ <>
+ {shortcuts.map((shortcut) => (
+ <MainNavigationItem
+ key={shortcut.url}
+ item={shortcut}
+ onNavigate={onNavigate}
+ />
+ ))}
+ {actions.map((action) => (
+ <CreateActionItem
+ key={action.url}
+ action={action}
+ onNavigate={onNavigate}
+ />
+ ))}
+ </>
+ );
+}
+
function MainNavigationItem({
item,
onNavigate,
}: {
- item: { title: string; url: string; icon?: IconComponent };
+ item: NavMenuItem;
onNavigate: (entry: RecentEntry) => void;
}) {
const router = useRouter();
@@ -354,7 +471,7 @@ function MainNavigationItem({
return (
<CommandItem
value={item.url}
- keywords={[item.title]}
+ keywords={[item.title, ...(item.keywords ?? [])]}
onSelect={handleSelect}
>
{item.icon && <item.icon size="base" />}
@@ -470,6 +587,93 @@ function DashboardsGroup({
);
}
+function EntityGroup({
+ group,
+ onNavigate,
+}: {
+ group: EntityGroupData;
+ onNavigate: (entry: RecentEntry) => void;
+}) {
+ return (
+ <>
+ <CommandSeparator />
+ <CommandGroup heading={ENTITY_GROUPS[group.type].heading}>
+ {group.items.map((item) => (
+ <EntityItem
+ key={item.url}
+ type={group.type}
+ item={item}
+ onNavigate={onNavigate}
+ />
+ ))}
+ </CommandGroup>
+ </>
+ );
+}
+
+function EntityItem({
+ type,
+ item,
+ onNavigate,
+}: {
+ type: EntityType;
+ item: EntityItemData;
+ onNavigate: (entry: RecentEntry) => void;
+}) {
+ const router = useRouter();
+ const capture = usePostHogClientCapture();
+ const { icon: Icon, keywords } = ENTITY_GROUPS[type];
+
+ const handleSelect = () => {
+ router.push(item.url);
+ capture("cmd_k_menu:navigated", { type });
+ onNavigate({ title: item.name, url: item.url, type });
+ };
+
+ return (
+ <CommandItem
+ value={`${type}:${item.id}`}
+ keywords={[...keywords, item.name.toLowerCase()]}
+ onSelect={handleSelect}
+ >
+ <Icon size="base" />
+ {item.name}
+ </CommandItem>
+ );
+}
+
+function IdNavigationGroup({
+ heading,
+ item,
+ icon: Icon,
+ search,
+ onNavigate,
+}: {
+ heading: string;
+ item: IdNavigationItem;
+ icon: IconComponent;
+ search: string;
+ onNavigate: () => void;
+}) {
+ const router = useRouter();
+ const capture = usePostHogClientCapture();
+
+ const handleSelect = () => {
+ router.push(item.url);
+ capture("cmd_k_menu:navigated", { type: item.type });
+ onNavigate();
+ };
+
+ return (
+ <CommandGroup heading={heading}>
+ <CommandItem value={`${item.title} ${search}`} onSelect={handleSelect}>
+ <Icon size="base" />
+ {item.title}
+ </CommandItem>
+ </CommandGroup>
+ );
+}
+
function ProjectSettingsGroup({
onNavigate,
}: {
@@ -632,16 +836,25 @@ function CommandMenuComponent({
}) {
const { open, setOpen } = useCommandMenu();
const capture = usePostHogClientCapture();
- const router = useRouter();
const { project } = useQueryProjectOrOrganization();
const { isV4 } = useReadPath();
const [search, setSearch] = useState("");
+ const [debouncedSearch, setDebouncedSearch] = useState("");
const { recents, addRecent } = useCommandMenuRecents(project?.id);
+ const flatNavigation = flattenNavigation(mainNavigation);
const createActions = useCreateActions(mainNavigation);
+ const pageShortcuts = usePageShortcuts(mainNavigation);
+ const { entityGroups, entityBaseUrls } = useEntityGroups(
+ mainNavigation,
+ search,
+ debouncedSearch,
+ );
+ const isSearching = search !== "";
const idNavigationItem = getIdNavigationItem(search, project?.id, isV4);
const debouncedSearchChange = useDebounce(
(value: string) => {
+ setDebouncedSearch(value);
capture("cmd_k_menu:search_entered", getSearchAnalytics(value));
},
500,
@@ -687,18 +900,24 @@ function CommandMenuComponent({
return () => document.removeEventListener("keydown", down);
}, [capture, setOpen, open]);
- const flatNavigation = flattenNavigation(mainNavigation);
+ const shortcutItems = [
+ ...[...pageShortcuts.byNavUrl.values()].flat(),
+ ...pageShortcuts.standalone,
+ ];
const iconByUrl = new Map(
- flatNavigation.map((item) => [item.url, item.icon]),
+ [...flatNavigation, ...shortcutItems].map((item) => [item.url, item.icon]),
);
const visibleRecents = getVisibleRecents(recents, {
- navUrls: new Set(flatNavigation.map((item) => item.url)),
+ navUrls: new Set(
+ [...flatNavigation, ...shortcutItems].map((item) => item.url),
+ ),
createUrls: new Set(
[
...[...createActions.byNavUrl.values()].flat(),
...createActions.standalone,
].map((action) => action.url),
),
+ entityBaseUrls,
});
const handleNavigate = (entry?: RecentEntry) => {
@@ -714,15 +933,9 @@ function CommandMenuComponent({
setOpen(nextOpen);
if (!nextOpen) setSearch("");
}}
- filter={(value, search, keywords) => {
- const extendValue = value + " " + keywords?.join(" ");
- const searchTerms = search.toLowerCase().split(" ");
- return searchTerms.every((term) =>
- extendValue.toLowerCase().includes(term),
- )
- ? 1
- : 0;
- }}
+ filter={(value, search, keywords) =>
+ matchesSearch(`${value} ${keywords?.join(" ")}`, search) ? 1 : 0
+ }
>
<CommandInput
placeholder="Search pages, projects, dashboards…"
@@ -735,24 +948,16 @@ function CommandMenuComponent({
/>
<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();
- }}
- >
- <ListTree size="base" />
- {idNavigationItem.title}
- </CommandItem>
- </CommandGroup>
- ) : null}
- {search === "" && (
+ {idNavigationItem && (
+ <IdNavigationGroup
+ heading="Tracing"
+ item={idNavigationItem}
+ icon={ListTree}
+ search={search}
+ onNavigate={handleNavigate}
+ />
+ )}
+ {!isSearching && (
<RecentGroup
recents={visibleRecents}
iconByUrl={iconByUrl}
@@ -761,13 +966,22 @@ function CommandMenuComponent({
)}
<MainNavigationGroup
navItems={navItems}
+ pageShortcuts={pageShortcuts}
createActions={createActions}
- showCreateActions={search !== ""}
+ isSearching={isSearching}
onNavigate={handleNavigate}
onDone={() => handleNavigate()}
/>
<ProjectsGroup onNavigate={handleNavigate} />
<DashboardsGroup onNavigate={handleNavigate} />
+ {isSearching &&
+ entityGroups.map((group) => (
+ <EntityGroup
+ key={group.type}
+ group={group}
+ onNavigate={handleNavigate}
+ />
+ ))}
<ProjectSettingsGroup onNavigate={handleNavigate} />
<OrganizationSettingsGroup onNavigate={handleNavigate} />
<AccountSettingsGroup onNavigate={handleNavigate} />
@@ -908,6 +1122,88 @@ function useCreateActions(mainNavigation: NavigationItem[]): CreateActions {
});
}
+function usePageShortcuts(mainNavigation: NavigationItem[]): PageShortcuts {
+ const session = useSession();
+ const { project } = useQueryProjectOrOrganization();
+
+ if (!project) return { byNavUrl: new Map(), standalone: [] };
+
+ return buildPageShortcuts({
+ navItems: flattenNavigation(mainNavigation),
+ projectId: project.id,
+ canRead: (scope) =>
+ hasProjectAccess({ session: session.data, scope, projectId: project.id }),
+ });
+}
+
+/**
+ * Fetches names while the menu is open, only for pages the user can see and
+ * read. Evaluators are searched on the server since their list is capped.
+ */
+function useEntityGroups(
+ mainNavigation: NavigationItem[],
+ search: string,
+ debouncedSearch: string,
+): {
+ entityGroups: EntityGroupData[];
+ entityBaseUrls: string[];
+} {
+ const session = useSession();
+ const { project } = useQueryProjectOrOrganization();
+ const { open } = useCommandMenu();
+ const projectId = project?.id ?? "";
+ const navItems = flattenNavigation(mainNavigation);
+
+ const getBaseUrl = (type: EntityType) =>
+ getEntityBaseUrl({
+ type,
+ navItems,
+ projectId,
+ canRead: (scope) =>
+ hasProjectAccess({ session: session.data, scope, projectId }),
+ });
+ const promptsUrl = getBaseUrl("prompt");
+ const datasetsUrl = getBaseUrl("dataset");
+ const queuesUrl = getBaseUrl("annotation_queue");
+ const evaluatorsUrl = getBaseUrl("evaluator");
+
+ const prompts = api.prompts.allNames.useQuery(
+ { projectId },
+ { enabled: open && promptsUrl !== null },
+ );
+ const datasets = api.datasets.allNamesAndIds.useQuery(
+ { projectId },
+ { enabled: open && datasetsUrl !== null },
+ );
+ const queues = api.annotationQueues.allNamesAndIds.useQuery(
+ { projectId },
+ { enabled: open && queuesUrl !== null },
+ );
+ const evaluators = api.evalsV2.options.useQuery(
+ {
+ projectId,
+ limit: ENTITY_LIMIT,
+ search: debouncedSearch.trim() || undefined,
+ },
+ {
+ enabled: open && evaluatorsUrl !== null,
+ placeholderData: keepPreviousData,
+ },
+ );
+
+ return {
+ entityGroups: [
+ toEntityGroup("prompt", promptsUrl, prompts.data, search),
+ toEntityGroup("dataset", datasetsUrl, datasets.data, search),
+ toEntityGroup("annotation_queue", queuesUrl, queues.data, search),
+ toEntityGroup("evaluator", evaluatorsUrl, evaluators.data, search),
+ ].filter((group) => group.items.length > 0),
+ entityBaseUrls: [promptsUrl, datasetsUrl, queuesUrl, evaluatorsUrl].filter(
+ (url): url is string => url !== null,
+ ),
+ };
+}
+
/** Most recent first, one entry per URL, capped at RECENTS_LIMIT. */
export function addRecentEntry(
recents: RecentEntry[],
@@ -922,16 +1218,31 @@ export function addRecentEntry(
/** Drops saved pages and create actions the live menu no longer offers. */
export function getVisibleRecents(
recents: RecentEntry[],
- offered: { navUrls: Set<string>; createUrls: Set<string> },
+ offered: {
+ navUrls: Set<string>;
+ createUrls: Set<string>;
+ entityBaseUrls: string[];
+ },
): RecentEntry[] {
return recents.filter((entry) => {
if (entry.type === "main_navigation") return offered.navUrls.has(entry.url);
if (entry.type === "create_action")
return offered.createUrls.has(entry.url);
+ if (isEntityType(entry.type))
+ return offered.entityBaseUrls.some((baseUrl) =>
+ entry.url.startsWith(`${baseUrl}/`),
+ );
return true;
});
}
+/** Entity names are customer content, so entity picks report only their type. */
+export function getRecentPickAnalytics(entry: RecentEntry) {
+ if (isEntityType(entry.type))
+ return { type: "recent", entityType: entry.type };
+ return { type: "recent", title: entry.title, url: entry.url };
+}
+
export function buildCreateActions({
navItems,
projectId,
@@ -967,6 +1278,109 @@ export function buildCreateActions({
return { byNavUrl, standalone };
}
+export function buildPageShortcuts({
+ navItems,
+ projectId,
+ canRead,
+}: {
+ navItems: Pick<NavigationItem, "href" | "url" | "icon">[];
+ projectId: string;
+ canRead: (scope: ProjectScope) => boolean;
+}): PageShortcuts {
+ const byNavUrl = new Map<string, NavMenuItem[]>();
+ for (const navItem of navItems) {
+ if (!navItem.url) continue;
+ const shortcuts = NAV_PAGE_SHORTCUTS.filter(
+ (shortcut) =>
+ shortcut.navHref === navItem.href &&
+ canRead(shortcut.scope) &&
+ !(shortcut.v4Only && isLegacyNavItem(navItem, projectId)),
+ ).map((shortcut) => ({
+ title: shortcut.title,
+ url: `/project/${projectId}/${shortcut.path}`,
+ icon: navItem.icon,
+ keywords: shortcut.keywords,
+ }));
+ if (shortcuts.length > 0) byNavUrl.set(navItem.url, shortcuts);
+ }
+
+ const standalone: NavMenuItem[] = canRead("automations:read")
+ ? [
+ {
+ title: "Automations",
+ url: `/project/${projectId}/automations`,
+ icon: Zap,
+ keywords: ["automation", "webhook", "slack", "trigger"],
+ },
+ ]
+ : [];
+
+ return { byNavUrl, standalone };
+}
+
+/** URL of the entity's sidebar page, or null when that page is hidden or unreadable. */
+export function getEntityBaseUrl({
+ type,
+ navItems,
+ projectId,
+ canRead,
+}: {
+ type: EntityType;
+ navItems: Pick<NavigationItem, "href" | "url">[];
+ projectId: string;
+ canRead: (scope: ProjectScope) => boolean;
+}): string | null {
+ const config = ENTITY_GROUPS[type];
+ const navItem = navItems.find((item) => item.href === config.navHref);
+ if (!projectId || !navItem?.url || !canRead(config.scope)) return null;
+ if (config.v4Only && isLegacyNavItem(navItem, projectId)) return null;
+ return navItem.url;
+}
+
+/** Caps the rows matching the search, so any row stays findable. */
+export function toEntityGroup(
+ type: EntityType,
+ baseUrl: string | null,
+ rows: Array<{ id: string; name: string }> | undefined,
+ search: string,
+): EntityGroupData {
+ if (baseUrl === null || !rows) return { type, items: [] };
+
+ const { pathKey, keywords } = ENTITY_GROUPS[type];
+ const matches = rows.filter((row) =>
+ matchesSearch(`${keywords.join(" ")} ${row.name}`, search),
+ );
+ return {
+ type,
+ items: matches.slice(0, ENTITY_LIMIT).map((row) => ({
+ id: row.id,
+ name: row.name,
+ url: `${baseUrl}/${encodeURIComponent(row[pathKey])}`,
+ })),
+ };
+}
+
+/** Every space-separated search term appears in the text, ignoring case. */
+function matchesSearch(text: string, search: string): boolean {
+ const lowerText = text.toLowerCase();
+ return search
+ .toLowerCase()
+ .split(" ")
+ .every((term) => lowerText.includes(term));
+}
+
+function isEntityType(type: string): type is EntityType {
+ return Object.hasOwn(ENTITY_GROUPS, type);
+}
+
+/** True when the sidebar links to the page's legacy URL instead of its href. */
+function isLegacyNavItem(
+ navItem: Pick<NavigationItem, "href" | "url">,
+ projectId: string,
+): boolean {
+ return navItem.url !== navItem.href.replace("[projectId]", projectId);
+}
+
function flattenNavigation(items: NavigationItem[]): NavigationItem[] {
return items.flatMap((item) => [
item,
@@ -982,3 +1396,21 @@ type CreateActions = {
byNavUrl: Map<string, CreateAction[]>;
standalone: CreateAction[];
};
+
+type NavMenuItem = {
+ title: string;
+ url: string;
+ icon?: IconComponent;
+ keywords?: string[];
+};
+
+type PageShortcuts = {
+ byNavUrl: Map<string, NavMenuItem[]>;
+ standalone: NavMenuItem[];
+};
+
+type EntityType = "prompt" | "dataset" | "annotation_queue" | "evaluator";
+
+type EntityItemData = { id: string; name: string; url: string };
+
+type EntityGroupData = { type: EntityType; items: EntityItemData[] };
diff --git a/web/src/features/datasets/server/dataset-router.ts b/web/src/features/datasets/server/dataset-router.ts
index fae27cabc4..a6e9359b88 100644
--- a/web/src/features/datasets/server/dataset-router.ts
+++ b/web/src/features/datasets/server/dataset-router.ts
@@ -407,6 +407,25 @@ export const datasetRouter = createTRPCRouter({
},
});
}),
+ allNamesAndIds: protectedProjectProcedure
+ .input(
+ z.object({
+ projectId: z.string(),
+ }),
+ )
+ .query(async ({ input, ctx }) => {
+ throwIfNoProjectAccess({
+ session: ctx.session,
+ projectId: input.projectId,
+ scope: "datasets:read",
+ });
+
+ return ctx.prisma.dataset.findMany({
+ where: { projectId: input.projectId },
+ select: { id: true, name: true },
+ orderBy: { updatedAt: "desc" },
+ });
+ }),
allDatasets: protectedProjectProcedure
.input(
z.object({