🪢 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(api): add `lastConfig` attribute and `fromUpdatedAt` and `toUpdatedAt` query params to `GET /v2/prompts` (#2824)
Marc Klingened4d7ceb40ec
diff --git a/fern/apis/server/definition/prompts.yml b/fern/apis/server/definition/prompts.ymlindex c92aa506b4..2cf2291bf9 100644--- a/fern/apis/server/definition/prompts.yml+++ b/fern/apis/server/definition/prompts.yml@@ -41,6 +41,12 @@ service:limit:type: optional<integer>docs: limit of items per page+ fromUpdatedAt:+ type: optional<datetime>+ docs: Optional filter to only include prompt versions created/updated on or after a certain datetime (ISO 8601)+ toUpdatedAt:+ type: optional<datetime>+ docs: Optional filter to only include prompt versions created/updated before a certain datetime (ISO 8601)response: PromptMetaListResponse@@ -64,6 +70,9 @@ types:labels: list<string>tags: list<string>lastUpdatedAt: datetime+ lastConfig:+ type: unknown+ docs: Config object of the most recent prompt version that matches the filters (if any are provided)CreatePromptRequest:union:diff --git a/web/public/generated/api/openapi.yml b/web/public/generated/api/openapi.ymlindex a0e8892600..090c88c0ac 100644--- a/web/public/generated/api/openapi.yml+++ b/web/public/generated/api/openapi.yml@@ -1188,6 +1188,26 @@ paths:schema:type: integernullable: true+ - name: fromUpdatedAt+ in: query+ description: >-+ Optional filter to only include prompt versions created/updated on+ or after a certain datetime (ISO 8601)+ required: false+ schema:+ type: string+ format: date-time+ nullable: true+ - name: toUpdatedAt+ in: query+ description: >-+ Optional filter to only include prompt versions created/updated+ before a certain datetime (ISO 8601)+ required: false+ schema:+ type: string+ format: date-time+ nullable: trueresponses:'200':description: ''@@ -3690,12 +3710,17 @@ components:lastUpdatedAt:type: stringformat: date-time+ lastConfig:+ description: >-+ Config object of the most recent prompt version that matches the+ filters (if any are provided)required:- name- versions- labels- tags- lastUpdatedAt+ - lastConfigCreatePromptRequest:title: CreatePromptRequestoneOf:diff --git a/web/public/generated/postman/collection.json b/web/public/generated/postman/collection.jsonindex d922ef05a0..8b6d92ca5a 100644--- a/web/public/generated/postman/collection.json+++ b/web/public/generated/postman/collection.json@@ -877,7 +877,7 @@"request": {"description": "Get a list of prompt names with versions and labels","url": {- "raw": "{{baseUrl}}/api/public/v2/prompts?name=&label=&tag=&page=&limit=",+ "raw": "{{baseUrl}}/api/public/v2/prompts?name=&label=&tag=&page=&limit=&fromUpdatedAt=&toUpdatedAt=","host": ["{{baseUrl}}"],@@ -912,6 +912,16 @@"key": "limit","value": "","description": "limit of items per page"+ },+ {+ "key": "fromUpdatedAt",+ "value": "",+ "description": "Optional filter to only include prompt versions created/updated on or after a certain datetime (ISO 8601)"+ },+ {+ "key": "toUpdatedAt",+ "value": "",+ "description": "Optional filter to only include prompt versions created/updated before a certain datetime (ISO 8601)"}],"variable": []diff --git a/web/src/__tests__/prompts.v2.servertest.ts b/web/src/__tests__/prompts.v2.servertest.tsindex 569165de0d..65ee1291d1 100644--- a/web/src/__tests__/prompts.v2.servertest.ts+++ b/web/src/__tests__/prompts.v2.servertest.ts@@ -23,11 +23,13 @@ type CreatePromptInDBParams = {prompt: string;labels: string[];version: number;- config: Record<string, object | number | string>;+ config: any;projectId: string;createdBy: string;type?: PromptType;tags?: string[];+ createdAt?: Date;+ updatedAt?: Date;};const createPromptInDB = async (params: CreatePromptInDBParams) => {return await prisma.prompt.create({@@ -44,6 +46,8 @@ const createPromptInDB = async (params: CreatePromptInDBParams) => {createdBy: params.createdBy,type: params.type,tags: params.tags,+ createdAt: params.createdAt,+ updatedAt: params.updatedAt,},});};@@ -833,7 +837,7 @@ describe("/api/public/v2/prompts API Endpoint", () => {// Validate prompt-1 metaexpect(promptMeta1.name).toBe("prompt-1");expect(promptMeta1.versions).toEqual([1, 2, 4]);- expect(promptMeta1.labels).toEqual(["production"]);+ expect(promptMeta1.labels).toEqual(["production", "version2"]);expect(promptMeta1.tags).toEqual([]);expect(promptMeta1.lastUpdatedAt).toBeDefined();@@ -873,7 +877,7 @@ describe("/api/public/v2/prompts API Endpoint", () => {expect(body.data).toHaveLength(1);expect(body.data[0].name).toBe("prompt-1");expect(body.data[0].versions).toEqual([1, 2, 4]);- expect(body.data[0].labels).toEqual(["production"]);+ expect(body.data[0].labels).toEqual(["production", "version2"]);expect(body.data[0].tags).toEqual([]);// Validate pagination@@ -994,6 +998,112 @@ describe("/api/public/v2/prompts API Endpoint", () => {expect(body.meta.totalPages).toBe(3);expect(body.meta.totalItems).toBe(3);});++ it("should fetch lastConfig correctly for a prompt with multiple versions", async () => {+ // no filters+ const response = await makeAPICall("GET", `${baseURI}`);+ expect(response.status).toBe(200);+ const body = response.body as unknown as PromptsMetaResponse;++ expect(body.data).toHaveLength(3);+ expect(body.data.some((promptMeta) => promptMeta.name === "prompt-1")).toBe(+ true,+ );+ expect(body.data.some((promptMeta) => promptMeta.name === "prompt-2")).toBe(+ true,+ );+ expect(body.data.some((promptMeta) => promptMeta.name === "prompt-3")).toBe(+ true,+ );+ const prompt1 = body.data.find(+ (promptMeta) => promptMeta.name === "prompt-1",+ );+ expect(prompt1).toBeDefined();+ expect(prompt1?.lastConfig).toEqual({ version: 4 });++ const prompt2 = body.data.find(+ (promptMeta) => promptMeta.name === "prompt-2",+ );+ expect(prompt2).toBeDefined();+ expect(prompt2?.lastConfig).toEqual({});++ // validate with label filter+ const response2 = await makeAPICall("GET", `${baseURI}?label=version2`);+ expect(response2.status).toBe(200);+ const body2 = response2.body as unknown as PromptsMetaResponse;++ expect(body2.data).toHaveLength(1);+ expect(body2.data[0].name).toBe("prompt-1");+ expect(body2.data[0].lastConfig).toEqual({ version: 2 });++ // validate with version filter+ const response3 = await makeAPICall("GET", `${baseURI}?version=1`);+ expect(response3.status).toBe(200);+ const body3 = response3.body as unknown as PromptsMetaResponse;++ expect(body3.data).toHaveLength(3);+ const prompt1v1 = body3.data.find(+ (promptMeta) => promptMeta.name === "prompt-1",+ );+ expect(prompt1v1?.lastConfig).toEqual({ version: 1 });+ });++ it("should respect the fromUpdatedAt and toUpdatedAt filters on GET /prompts", async () => {+ // to and from+ const from = new Date("2024-01-02T00:00:00.000Z");+ const to = new Date("2024-01-04T00:00:00.000Z");+ const response = await makeAPICall(+ "GET",+ `${baseURI}?fromUpdatedAt=${from.toISOString()}&toUpdatedAt=${to.toISOString()}`,+ );+ expect(response.status).toBe(200);+ const body = response.body as unknown as PromptsMetaResponse;++ expect(body.data).toHaveLength(1);+ expect(body.data[0].name).toBe("prompt-1");+ expect(body.data[0].lastUpdatedAt).toBe("2024-01-02T00:00:00.000Z");+ expect(body.data[0].versions.length).toBe(1);++ expect(body.meta.totalItems).toBe(1);++ // only from+ const response2 = await makeAPICall(+ "GET",+ `${baseURI}?fromUpdatedAt=${from.toISOString()}`,+ );+ expect(response2.status).toBe(200);+ const body2 = response2.body as unknown as PromptsMetaResponse;++ expect(body2.data).toHaveLength(1);+ expect(body2.data[0].name).toBe("prompt-1");+ expect(body2.data[0].lastUpdatedAt).toBe("2024-01-04T00:00:00.000Z");+ expect(body2.data[0].versions.length).toBe(2);++ expect(body2.meta.totalItems).toBe(1);++ // only to+ const response3 = await makeAPICall(+ "GET",+ `${baseURI}?toUpdatedAt=${to.toISOString()}`,+ );+ expect(response3.status).toBe(200);+ const body3 = response3.body as unknown as PromptsMetaResponse;++ expect(body3.data).toHaveLength(3);+ expect(body3.data[0].name).toBe("prompt-1");+ expect(body3.data[0].lastUpdatedAt).toBe("2024-01-02T00:00:00.000Z");+ expect(body3.data[0].versions.length).toBe(2);++ expect(body3.data[1].name).toBe("prompt-2");+ expect(body3.data[1].lastUpdatedAt).toBe("2000-03-01T00:00:00.000Z");+ expect(body3.data[1].versions.length).toBe(3);++ expect(body3.data[2].name).toBe("prompt-3");+ expect(body3.data[2].lastUpdatedAt).toBe("2000-01-01T00:00:00.000Z");+ expect(body3.data[2].versions.length).toBe(1);++ expect(body3.meta.totalItems).toBe(3);+ });});const isPrompt = (x: unknown): x is Prompt => {@@ -1032,17 +1142,19 @@ const mockPrompts = [prompt: "prompt-1",createdBy: "user-test",projectId,- config: {},+ config: { version: 1 },version: 1,+ updatedAt: new Date("2024-01-01T00:00:00.000Z"),},{name: "prompt-1",- labels: ["production"],+ labels: ["production", "version2"],prompt: "prompt-1",createdBy: "user-test",projectId,- config: {},+ config: { version: 2 },version: 2,+ updatedAt: new Date("2024-01-02T00:00:00.000Z"),},{name: "prompt-1",@@ -1050,8 +1162,9 @@ const mockPrompts = [prompt: "prompt-1",createdBy: "user-test",projectId,- config: {},+ config: { version: 4 },version: 4,+ updatedAt: new Date("2024-01-04T00:00:00.000Z"),},// Prompt with different labels@@ -1063,6 +1176,7 @@ const mockPrompts = [projectId,config: {},version: 1,+ updatedAt: new Date("2000-01-01T00:00:00.000Z"),},{name: "prompt-2",@@ -1072,6 +1186,7 @@ const mockPrompts = [projectId,config: {},version: 2,+ updatedAt: new Date("2000-03-01T00:00:00.000Z"),},{name: "prompt-2",@@ -1081,6 +1196,7 @@ const mockPrompts = [projectId,config: {},version: 3,+ updatedAt: new Date("2000-02-01T00:00:00.000Z"),},// Prompt with different labels@@ -1093,6 +1209,7 @@ const mockPrompts = [config: {},tags: ["tag-1"],version: 1,+ updatedAt: new Date("2000-01-01T00:00:00.000Z"),},// Prompt in different project@@ -1104,5 +1221,6 @@ const mockPrompts = [projectId: "239ad00f-562f-411d-af14-831c75ddd875",config: {},version: 1,+ updatedAt: new Date("2000-01-01T00:00:00.000Z"),},];diff --git a/web/src/features/prompts/server/actions/getPromptsMeta.ts b/web/src/features/prompts/server/actions/getPromptsMeta.tsindex 0f9bd59f06..9975876a2f 100644--- a/web/src/features/prompts/server/actions/getPromptsMeta.ts+++ b/web/src/features/prompts/server/actions/getPromptsMeta.ts@@ -14,26 +14,55 @@ export const getPromptsMeta = async (const { projectId, page, limit } = params;const promptsMeta = (await prisma.$queryRaw`- SELECT+ WITH latest_version_config AS (+ SELECT+ p.name,+ p.config+ FROM+ prompts p+ WHERE+ (p.name, p.version) IN (+ SELECT+ p.name,+ MAX(p.version)+ FROM+ prompts p -- needs to be p for filter conditions+ WHERE+ p."project_id" = ${projectId}+ ${getPromptsFilterCondition(params)}+ GROUP BY+ p.name+ )+ AND p."project_id" = ${projectId}+ ), versions AS (+ SELECTp.name AS name,MAX(p.tags) AS tags, -- use max to get tags, they are the same for all versions of a promptMAX(p.updated_at) as "lastUpdatedAt",array_agg(DISTINCT p.version) AS versions,COALESCE(array_agg(DISTINCT label) FILTER (WHERE label IS NOT NULL), '{}'::text[]) AS labels --- COALESCE is necessary to return an empty array if there are no labels and remove NULLs+ FROM+ prompts p -- needs to be p for filter conditions+ LEFT JOIN LATERAL unnest(p.labels) AS label ON true+ WHERE+ p."project_id" = ${projectId}+ ${getPromptsFilterCondition(params)}+ GROUP BY+ p.name+ ORDER BY+ p.name --- necessary for consistent pagination+ LIMIT+ ${limit}+ OFFSET+ ${limit * (page - 1)}+ )++ SELECT+ v.*,+ l.config AS "lastConfig"FROM- prompts p- LEFT JOIN LATERAL unnest(p.labels) AS label ON true- WHERE- p."project_id" = ${projectId}- ${getPromptsFilterCondition(params)}- GROUP BY- p.name- ORDER BY- p.name --- necessary for consistent pagination- LIMIT- ${limit}- OFFSET- ${limit * (page - 1)}+ versions v+ LEFT JOIN latest_version_config l ON v.name = l.name`) as PromptsMeta[];const [{ count: totalItemsCount }] = (await prisma.$queryRaw`@@ -62,6 +91,7 @@ type PromptsMeta = {labels: string[];tags: string[];lastUpdatedAt: Date;+ lastConfig: unknown; // json object};export type PromptsMetaResponse = {@@ -83,7 +113,7 @@ export type PromptsMetaResponse = {};const getPromptsFilterCondition = (params: GetPromptsMetaType) => {- const { name, version, label, tag } = params;+ const { name, version, label, tag, fromUpdatedAt, toUpdatedAt } = params;const filters: FilterState = [];if (name) {@@ -122,5 +152,23 @@ const getPromptsFilterCondition = (params: GetPromptsMetaType) => {});}+ if (fromUpdatedAt) {+ filters.push({+ column: "updatedAt",+ type: "datetime",+ operator: ">=",+ value: new Date(fromUpdatedAt),+ });+ }++ if (toUpdatedAt) {+ filters.push({+ column: "updatedAt",+ type: "datetime",+ operator: "<",+ value: new Date(toUpdatedAt),+ });+ }+return tableColumnsToSqlFilterAndPrefix(filters, promptsTableCols, "prompts");};diff --git a/web/src/features/prompts/server/utils/validation.ts b/web/src/features/prompts/server/utils/validation.tsindex d911ee5737..5ea7f7e7c8 100644--- a/web/src/features/prompts/server/utils/validation.ts+++ b/web/src/features/prompts/server/utils/validation.ts@@ -67,6 +67,8 @@ export const GetPromptsMetaSchema = z.object({tag: z.string().optional(),page: z.coerce.number().int().min(1).default(1),limit: z.coerce.number().int().min(1).max(100).default(10),+ fromUpdatedAt: z.string().datetime({ offset: true }).nullish(),+ toUpdatedAt: z.string().datetime({ offset: true }).nullish(),});export type GetPromptsMetaType = z.infer<typeof GetPromptsMetaSchema>;diff --git a/web/src/server/api/definitions/promptsTable.ts b/web/src/server/api/definitions/promptsTable.tsindex 9dc8d5bdf5..e81f9f76f7 100644--- a/web/src/server/api/definitions/promptsTable.ts+++ b/web/src/server/api/definitions/promptsTable.ts@@ -23,6 +23,12 @@ export const promptsTableCols: ColumnDefinition[] = [type: "datetime",internal: 'p."created_at"',},+ {+ name: "Updated At",+ id: "updatedAt",+ type: "datetime",+ internal: 'p."updated_at"',+ },{name: "Type",id: "type",