Skip to content

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

feat(prompts): Add search bar to prompts table (#7603)

Marc Klingenfb43e0317189
diff --git a/web/src/features/prompts/components/prompts-table.tsx b/web/src/features/prompts/components/prompts-table.tsx
index f1151dfae7..a112b69ec2 100644
--- a/web/src/features/prompts/components/prompts-table.tsx
+++ b/web/src/features/prompts/components/prompts-table.tsx
@@ -12,7 +12,13 @@ import { DataTableToolbar } from "@/src/components/table/data-table-toolbar";
import { useQueryFilterState } from "@/src/features/filters/hooks/useFilterState";
import { useOrderByState } from "@/src/features/orderBy/hooks/useOrderByState";
import { promptsTableColsWithOptions } from "@/src/server/api/definitions/promptsTable";
-import { NumberParam, StringParam, useQueryParams, withDefault } from "use-query-params";
+import {
+ NumberParam,
+ StringParam,
+ useQueryParams,
+ withDefault,
+ useQueryParam,
+} from "use-query-params";
import { createColumnHelper } from "@tanstack/react-table";
import { joinTableCoreAndMetrics } from "@/src/components/table/utils/joinTableCoreAndMetrics";
import { Skeleton } from "@/src/components/ui/skeleton";
@@ -40,7 +46,11 @@ type PromptTableRow = {
};
function createRow(
- data: Partial<PromptTableRow> & { id: string; name: string; type: "folder" | "text" | "chat" }
+ data: Partial<PromptTableRow> & {
+ id: string;
+ name: string;
+ type: "folder" | "text" | "chat";
+ },
): PromptTableRow {
return {
version: undefined,
@@ -52,12 +62,14 @@ function createRow(
};
}
-function isFolder(row: PromptTableRow): row is PromptTableRow & { type: "folder" } {
+function isFolder(
+ row: PromptTableRow,
+): row is PromptTableRow & { type: "folder" } {
return row.type === "folder";
}
function getDisplayName(fullPath: string, currentFolderPath: string): string {
- return currentFolderPath === ''
+ return currentFolderPath === ""
? fullPath
: fullPath.substring(currentFolderPath.length + 1);
}
@@ -65,9 +77,9 @@ function getDisplayName(fullPath: string, currentFolderPath: string): string {
function createBreadcrumbItems(currentFolderPath: string) {
if (!currentFolderPath) return [];
- const segments = currentFolderPath.split('/');
+ const segments = currentFolderPath.split("/");
return segments.map((name, i) => {
- const folderPath = segments.slice(0, i + 1).join('/');
+ const folderPath = segments.slice(0, i + 1).join("/");
return {
name,
folderPath,
@@ -95,12 +107,27 @@ export function PromptTable() {
folder: StringParam,
});
+ const [searchQuery, setSearchQuery] = useQueryParam(
+ "search",
+ withDefault(StringParam, null),
+ );
+
+ // Reset pagination when search query changes
+ useEffect(() => {
+ setQueryParams({
+ pageIndex: 0,
+ pageSize: queryParams.pageSize,
+ folder: queryParams.folder,
+ });
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [searchQuery]);
+
const paginationState = {
pageIndex: queryParams.pageIndex,
pageSize: queryParams.pageSize,
};
- const currentFolderPath = queryParams.folder || '';
+ const currentFolderPath = queryParams.folder || "";
const prompts = api.prompts.all.useQuery(
{
@@ -110,6 +137,7 @@ export function PromptTable() {
filter: filterState,
orderBy: orderByState,
pathPrefix: currentFolderPath,
+ searchQuery: searchQuery || undefined,
},
{
enabled: Boolean(projectId),
@@ -167,7 +195,7 @@ export function PromptTable() {
const prefix = `${currentFolderPath}/`;
if (promptName.startsWith(prefix)) {
const remainingPath = promptName.substring(prefix.length);
- const slashIndex = remainingPath.indexOf('/');
+ const slashIndex = remainingPath.indexOf("/");
if (slashIndex > 0) {
// Subfolder
@@ -181,7 +209,7 @@ export function PromptTable() {
}
} else {
// Root level
- const slashIndex = promptName.indexOf('/');
+ const slashIndex = promptName.indexOf("/");
if (slashIndex > 0) {
const folderName = promptName.substring(0, slashIndex);
uniqueFolders.add(folderName);
@@ -197,11 +225,13 @@ export function PromptTable() {
// Add folder rows
for (const folderPath of uniqueFolders) {
const folderName = getDisplayName(folderPath, currentFolderPath);
- combinedRows.push(createRow({
- id: folderPath,
- name: folderName,
- type: "folder",
- }));
+ combinedRows.push(
+ createRow({
+ id: folderPath,
+ name: folderName,
+ type: "folder",
+ }),
+ );
}
// Add matching prompts
@@ -216,7 +246,7 @@ export function PromptTable() {
labels: prompt.labels,
tags: prompt.tags,
numberOfObservations: Number(prompt.observationCount ?? 0),
- })
+ }),
);
}
@@ -284,7 +314,7 @@ export function PromptTable() {
setQueryParams({
folder: rowData.id,
pageIndex: 0,
- pageSize: queryParams.pageSize
+ pageSize: queryParams.pageSize,
});
}}
title={displayName || ""}
@@ -397,7 +427,7 @@ export function PromptTable() {
return (
<>
{currentFolderPath && (
- <div className="pt-2 ml-2">
+ <div className="ml-2 pt-2">
<Breadcrumb>
<BreadcrumbList>
<BreadcrumbItem>
@@ -407,38 +437,40 @@ export function PromptTable() {
setQueryParams({
folder: undefined,
pageIndex: 0,
- pageSize: queryParams.pageSize
+ pageSize: queryParams.pageSize,
});
}}
>
<Home className="h-4 w-4" />
</BreadcrumbLink>
</BreadcrumbItem>
- {createBreadcrumbItems(currentFolderPath).flatMap((item, index, array) => [
- index > 0 && (
- <BreadcrumbSeparator key={`sep-${item.folderPath}`}>
- <Slash />
- </BreadcrumbSeparator>
- ),
- <BreadcrumbItem key={item.folderPath}>
- {index === array.length - 1 ? (
- <BreadcrumbPage>{item.name}</BreadcrumbPage>
- ) : (
- <BreadcrumbLink
- className="cursor-pointer hover:underline"
- onClick={() => {
- setQueryParams({
- folder: item.folderPath,
- pageIndex: 0,
- pageSize: queryParams.pageSize
- });
- }}
- >
- {item.name}
- </BreadcrumbLink>
- )}
- </BreadcrumbItem>
- ])}
+ {createBreadcrumbItems(currentFolderPath).flatMap(
+ (item, index, array) => [
+ index > 0 && (
+ <BreadcrumbSeparator key={`sep-${item.folderPath}`}>
+ <Slash />
+ </BreadcrumbSeparator>
+ ),
+ <BreadcrumbItem key={item.folderPath}>
+ {index === array.length - 1 ? (
+ <BreadcrumbPage>{item.name}</BreadcrumbPage>
+ ) : (
+ <BreadcrumbLink
+ className="cursor-pointer hover:underline"
+ onClick={() => {
+ setQueryParams({
+ folder: item.folderPath,
+ pageIndex: 0,
+ pageSize: queryParams.pageSize,
+ });
+ }}
+ >
+ {item.name}
+ </BreadcrumbLink>
+ )}
+ </BreadcrumbItem>,
+ ],
+ )}
</BreadcrumbList>
</Breadcrumb>
</div>
@@ -451,6 +483,14 @@ export function PromptTable() {
filterState={filterState}
setFilterState={useDebounce(setFilterState)}
columnsWithCustomSelect={["labels", "tags"]}
+ searchConfig={{
+ metadataSearchFields: ["Name"],
+ updateQuery: useDebounce(setSearchQuery, 300),
+ currentQuery: searchQuery ?? undefined,
+ tableAllowsFullTextSearch: false,
+ setSearchType: undefined,
+ searchType: undefined,
+ }}
/>
<DataTable
columns={promptColumns}
@@ -468,11 +508,12 @@ export function PromptTable() {
isError: false,
data: processedRowData.rows?.map((item) => ({
id: item.id,
- name: item.type === 'folder'
- ? item.name
- : currentFolderPath
- ? item.name.substring(currentFolderPath.length + 1)
- : item.name,
+ name:
+ item.type === "folder"
+ ? item.name
+ : currentFolderPath
+ ? item.name.substring(currentFolderPath.length + 1)
+ : item.name,
version: item.version,
createdAt: item.createdAt,
type: item.type,
diff --git a/web/src/features/prompts/server/routers/promptRouter.ts b/web/src/features/prompts/server/routers/promptRouter.ts
index 73bc046a0c..d00c186ade 100644
--- a/web/src/features/prompts/server/routers/promptRouter.ts
+++ b/web/src/features/prompts/server/routers/promptRouter.ts
@@ -40,6 +40,7 @@ const PromptFilterOptions = z.object({
orderBy: orderBy,
...paginationZod,
pathPrefix: z.string().optional(),
+ searchQuery: z.string().optional(),
});
export const promptRouter = createTRPCRouter({
@@ -90,6 +91,10 @@ export const promptRouter = createTRPCRouter({
? Prisma.sql` AND (p.name LIKE ${input.pathPrefix + "/%"} OR p.name = ${input.pathPrefix})`
: Prisma.empty;
+ const searchFilter = input.searchQuery
+ ? Prisma.sql` AND p.name ILIKE ${`%${input.searchQuery}%`}`
+ : Prisma.empty;
+
const [prompts, promptCount] = await Promise.all([
// prompts
ctx.prisma.$queryRaw<Array<Prompt>>(
@@ -111,20 +116,22 @@ export const promptRouter = createTRPCRouter({
input.limit,
input.page,
pathFilter,
+ searchFilter,
),
),
- // promptCount
- ctx.prisma.$queryRaw<Array<{ totalCount: bigint }>>(
- generatePromptQuery(
- Prisma.sql` count(*) AS "totalCount"`,
- input.projectId,
- filterCondition,
- Prisma.empty,
- 1, // limit
- 0, // page,
- pathFilter,
+ // promptCount
+ ctx.prisma.$queryRaw<Array<{ totalCount: bigint }>>(
+ generatePromptQuery(
+ Prisma.sql` count(*) AS "totalCount"`,
+ input.projectId,
+ filterCondition,
+ Prisma.empty,
+ 1, // limit
+ 0, // page,
+ pathFilter,
+ searchFilter,
+ ),
),
- ),
]);
return {
@@ -1233,6 +1240,7 @@ const generatePromptQuery = (
limit: number,
page: number,
pathFilter: Prisma.Sql = Prisma.empty,
+ searchFilter: Prisma.Sql = Prisma.empty,
) => {
return Prisma.sql`
SELECT
@@ -1244,11 +1252,13 @@ const generatePromptQuery = (
WHERE "project_id" = ${projectId}
${filterCondition}
${pathFilter}
+ ${searchFilter}
GROUP BY name
)
AND "project_id" = ${projectId}
${filterCondition}
${pathFilter}
+ ${searchFilter}
${orderCondition}
LIMIT ${limit} OFFSET ${page * limit};
`;