Skip to content

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

feat(api): add prompt name prefix filtering (#17779)

tobi_kl5b59af472bbd
diff --git a/fern/apis/server/definition/prompts.yml b/fern/apis/server/definition/prompts.yml
index 075ffe798a..df3d3c6606 100644
--- a/fern/apis/server/definition/prompts.yml
+++ b/fern/apis/server/definition/prompts.yml
@@ -38,7 +38,8 @@ service:
request:
name: ListPromptsMetaRequest
query-parameters:
- name: optional<string>
+ name:
+ type: optional<string>
label: optional<string>
tag: optional<string>
page:
@@ -53,6 +54,33 @@ service:
toUpdatedAt:
type: optional<datetime>
docs: Optional filter to only include prompt versions created/updated before a certain datetime (ISO 8601)
+ filter:
+ type: optional<string>
+ docs: |
+ JSON string containing an array of filter conditions, combined with AND.
+ When provided, this takes precedence over name, version,
+ label, tag, fromUpdatedAt, and toUpdatedAt. An empty array applies no
+ filters. Project scoping and pagination always apply.
+
+ Each condition requires `type`, `column`, `operator`, and `value`.
+ Supported types and columns:
+ - `string`: `id`, `name`, `type`. Operators: `=`, `contains`, `does not contain`, `starts with`, `ends with`, `is not empty`.
+ - `stringOptions`: `id`, `name`, `type`. Operators: `any of`, `none of`. Value: non-empty string array.
+ - `number`: `version`. Operators: `=`, `>`, `<`, `>=`, `<=`. Value: number.
+ - `datetime`: `createdAt`, `updatedAt`. Operators: `>`, `<`, `>=`, `<=`. Value: ISO 8601 timestamp.
+ - `arrayOptions`: `labels`, `tags`. Operators: `any of`, `all of`, `none of`. Value: non-empty string array.
+ - `stringObject`: `config`. Requires an additional `key` field. Operators: `=`, `contains`, `does not contain`, `starts with`, `ends with`, `is set`, `is not set`. Value: string (use an empty string for key-presence operators).
+
+ String equality is case-sensitive. String search operators use
+ case-insensitive SQL pattern matching: `%` matches any sequence and
+ `_` matches a single character. A backslash escapes these characters;
+ JSON-escape the backslash in the filter value. Asterisks are literal.
+ For config, an empty search value is treated as `is set`.
+
+ Filters apply to prompt versions before aggregation: returned versions,
+ labels, and lastConfig reflect only matching versions.
+
+ Example: [{"type":"string","column":"name","operator":"starts with","value":"tools/"},{"type":"string","column":"name","operator":"ends with","value":"/description"}]
response: PromptMetaListResponse
diff --git a/packages/shared/src/features/prompts/types.ts b/packages/shared/src/features/prompts/types.ts
index b440fae2ed..3321ba6e64 100644
--- a/packages/shared/src/features/prompts/types.ts
+++ b/packages/shared/src/features/prompts/types.ts
@@ -1,6 +1,15 @@
import { z } from "zod";
import type { Prompt } from "@prisma/client";
-import { jsonSchema } from "../../utils/zod";
+import { jsonSchema, optionalJsonParam } from "../../utils/zod";
+import {
+ arrayOptionsFilter,
+ coerceLegacyEmptyMetadataFilters,
+ numberFilter,
+ stringFilter,
+ stringObjectFilter,
+ stringOptionsFilter,
+ timeFilter,
+} from "../../interfaces/filters";
import {
COMMIT_MESSAGE_MAX_LENGTH,
PROMPT_LABEL_MAX_LENGTH,
@@ -76,6 +85,25 @@ export const CreatePromptTRPCSchema = z.union([
export type CreatePromptTRPCType = z.infer<typeof CreatePromptTRPCSchema>;
+const promptFilterSchema = z.preprocess(
+ coerceLegacyEmptyMetadataFilters,
+ z.array(
+ z.discriminatedUnion("type", [
+ stringFilter.extend({ column: z.enum(["id", "name", "type"]) }),
+ stringOptionsFilter.safeExtend({
+ column: z.enum(["id", "name", "type"]),
+ }),
+ numberFilter.extend({ column: z.literal("version") }),
+ timeFilter.extend({ column: z.enum(["createdAt", "updatedAt"]) }),
+ arrayOptionsFilter.safeExtend({
+ column: z.enum(["labels", "tags"]),
+ value: z.array(z.string()).min(1),
+ }),
+ stringObjectFilter.extend({ column: z.literal("config") }),
+ ]),
+ ),
+);
+
export const GetPromptsMetaSchema = z.object({
name: z.string().optional(),
version: z.coerce.number().int().nullish(),
@@ -85,6 +113,7 @@ export const GetPromptsMetaSchema = z.object({
limit: z.coerce.number().int().min(1).max(100).default(10),
fromUpdatedAt: z.iso.datetime({ offset: true }).nullish(),
toUpdatedAt: z.iso.datetime({ offset: true }).nullish(),
+ filter: optionalJsonParam(promptFilterSchema, "filter"),
});
export type GetPromptsMetaType = z.infer<typeof GetPromptsMetaSchema>;
diff --git a/web/public/generated/api/openapi.yml b/web/public/generated/api/openapi.yml
index b20e583b79..df11a0de97 100644
--- a/web/public/generated/api/openapi.yml
+++ b/web/public/generated/api/openapi.yml
@@ -5895,6 +5895,71 @@ paths:
type: string
format: date-time
nullable: true
+ - name: filter
+ in: query
+ description: >-
+ JSON string containing an array of filter conditions, combined with
+ AND.
+
+ When provided, this takes precedence over name, version,
+
+ label, tag, fromUpdatedAt, and toUpdatedAt. An empty array applies
+ no
+
+ filters. Project scoping and pagination always apply.
+
+
+ Each condition requires `type`, `column`, `operator`, and `value`.
+
+ Supported types and columns:
+
+ - `string`: `id`, `name`, `type`. Operators: `=`, `contains`, `does
+ not contain`, `starts with`, `ends with`, `is not empty`.
+
+ - `stringOptions`: `id`, `name`, `type`. Operators: `any of`, `none
+ of`. Value: non-empty string array.
+
+ - `number`: `version`. Operators: `=`, `>`, `<`, `>=`, `<=`. Value:
+ number.
+
+ - `datetime`: `createdAt`, `updatedAt`. Operators: `>`, `<`, `>=`,
+ `<=`. Value: ISO 8601 timestamp.
+
+ - `arrayOptions`: `labels`, `tags`. Operators: `any of`, `all of`,
+ `none of`. Value: non-empty string array.
+
+ - `stringObject`: `config`. Requires an additional `key` field.
+ Operators: `=`, `contains`, `does not contain`, `starts with`, `ends
+ with`, `is set`, `is not set`. Value: string (use an empty string
+ for key-presence operators).
+
+
+ String equality is case-sensitive. String search operators use
+
+ case-insensitive SQL pattern matching: `%` matches any sequence and
+
+ `_` matches a single character. A backslash escapes these
+ characters;
+
+ JSON-escape the backslash in the filter value. Asterisks are
+ literal.
+
+ For config, an empty search value is treated as `is set`.
+
+
+ Filters apply to prompt versions before aggregation: returned
+ versions,
+
+ labels, and lastConfig reflect only matching versions.
+
+
+ Example: [{"type":"string","column":"name","operator":"starts
+ with","value":"tools/"},{"type":"string","column":"name","operator":"ends
+ with","value":"/description"}]
+ required: false
+ schema:
+ type: string
+ nullable: true
responses:
'200':
description: ''
diff --git a/web/src/__tests__/server/prompts.v2.servertest.ts b/web/src/__tests__/server/prompts.v2.servertest.ts
index 8ea13b93ac..e520605fd9 100644
--- a/web/src/__tests__/server/prompts.v2.servertest.ts
+++ b/web/src/__tests__/server/prompts.v2.servertest.ts
@@ -31,6 +31,7 @@ import { randomUUID } from "node:crypto";
import waitForExpect from "wait-for-expect";
import { createPrompt } from "@/src/features/prompts/server/actions/createPrompt";
import { promptNameHandler } from "@/src/features/prompts/server/handlers/promptNameHandler";
+import { promptsHandler } from "@/src/features/prompts/server/handlers/promptsHandler";
const projectId = "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a";
const baseURI = "/api/public/v2/prompts";
@@ -1215,6 +1216,161 @@ describe("/api/public/v2/prompts API Endpoint", () => {
});
});
+ describe("when filtering a prompt list with structured filters", () => {
+ let projectId: string;
+ let auth: string;
+
+ const request = async (query: Record<string, string>) => {
+ const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
+ method: "GET",
+ query,
+ headers: { authorization: auth },
+ });
+ await promptsHandler(req, res);
+ return res;
+ };
+
+ const list = async (
+ filter: unknown[],
+ query: Record<string, string> = {},
+ ) => {
+ const res = await request({ ...query, filter: JSON.stringify(filter) });
+ expect(res._getStatusCode()).toBe(200);
+ return res._getJSONData() as PromptsMetaResponse;
+ };
+
+ beforeAll(async () => {
+ ({ projectId, auth } = await createOrgProjectAndApiKey());
+ const { projectId: otherProjectId } = await createOrgProjectAndApiKey();
+ await Promise.all(
+ [
+ { name: "tools/a/description", version: 1, labels: ["production"] },
+ { name: "tools/a/description", version: 2, labels: ["latest"] },
+ { name: "tools/b/description", version: 1, labels: ["production"] },
+ { name: "tools/c/instructions", version: 1 },
+ { name: "other/description", version: 1 },
+ {
+ name: "tools/foreign/description",
+ version: 1,
+ projectId: otherProjectId,
+ },
+ ].map((prompt) =>
+ createPromptInDB({
+ prompt: "test",
+ labels: [],
+ tags: ["tool"],
+ config: { model: "test-model", version: prompt.version },
+ projectId,
+ createdBy: "user-test",
+ type: PromptType.Text,
+ createdAt: new Date("2026-01-01T00:00:00Z"),
+ updatedAt: new Date("2026-01-02T00:00:00Z"),
+ ...prompt,
+ }),
+ ),
+ );
+ });
+
+ it("combines name operators before pagination and preserves project isolation", async () => {
+ const filters = [
+ {
+ type: "string",
+ column: "name",
+ operator: "starts with",
+ value: "tools/",
+ },
+ {
+ type: "string",
+ column: "name",
+ operator: "ends with",
+ value: "/description",
+ },
+ ];
+ const first = await list(filters, {
+ limit: "1",
+ name: "ignored",
+ label: "ignored",
+ tag: "ignored",
+ version: "999",
+ fromUpdatedAt: "2030-01-01T00:00:00Z",
+ toUpdatedAt: "2020-01-01T00:00:00Z",
+ });
+ expect(first.data.map((prompt) => prompt.name)).toEqual([
+ "tools/a/description",
+ ]);
+ expect(first.data[0].versions).toEqual([1, 2]);
+ expect(first.data[0].lastConfig).toEqual({
+ model: "test-model",
+ version: 2,
+ });
+ expect(first.meta).toEqual({
+ page: 1,
+ limit: 1,
+ totalItems: 2,
+ totalPages: 2,
+ });
+ expect(first.pagination).toEqual(first.meta);
+ const second = await list(filters, { limit: "1", page: "2" });
+ expect(second.data.map((prompt) => prompt.name)).toEqual([
+ "tools/b/description",
+ ]);
+ expect(second.meta).toEqual({ ...first.meta, page: 2 });
+ });
+
+ it("filters versions, arrays, timestamps, type, and config before aggregating metadata", async () => {
+ const result = await list([
+ { type: "string", column: "name", operator: "contains", value: "/a/" },
+ { type: "number", column: "version", operator: "<=", value: 1 },
+ {
+ type: "arrayOptions",
+ column: "labels",
+ operator: "any of",
+ value: ["production"],
+ },
+ {
+ type: "arrayOptions",
+ column: "tags",
+ operator: "all of",
+ value: ["tool"],
+ },
+ {
+ type: "datetime",
+ column: "createdAt",
+ operator: ">=",
+ value: "2026-01-01T00:00:00Z",
+ },
+ {
+ type: "datetime",
+ column: "updatedAt",
+ operator: "<",
+ value: "2026-01-03T00:00:00Z",
+ },
+ {
+ type: "stringOptions",
+ column: "type",
+ operator: "any of",
+ value: ["text"],
+ },
+ {
+ type: "stringObject",
+ column: "config",
+ key: "model",
+ operator: "=",
+ value: "test-model",
+ },
+ ]);
+ expect(result.data).toEqual([
+ expect.objectContaining({
+ name: "tools/a/description",
+ versions: [1],
+ labels: ["production"],
+ lastConfig: { model: "test-model", version: 1 },
+ }),
+ ]);
+ expect(result.meta.totalItems).toBe(1);
+ });
+ });
+
describe("when fetching a prompt list", () => {
const otherProjectPromptName = "prompt-5";
let otherProjectId: string;
diff --git a/web/src/features/prompts/server/actions/getPromptsMeta.ts b/web/src/features/prompts/server/actions/getPromptsMeta.ts
index 6287dc64e5..112008ba90 100644
--- a/web/src/features/prompts/server/actions/getPromptsMeta.ts
+++ b/web/src/features/prompts/server/actions/getPromptsMeta.ts
@@ -114,6 +114,14 @@ export type PromptsMetaResponse = {
};
const getPromptsFilterCondition = (params: GetPromptsMetaType) => {
+ if (params.filter !== undefined) {
+ return tableColumnsToSqlFilterAndPrefix(
+ params.filter,
+ promptsTableCols,
+ "prompts",
+ );
+ }
+
const { name, version, label, tag, fromUpdatedAt, toUpdatedAt } = params;
const filters: FilterState = [];