🪢 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
fix(api): handle legacy traces widgets on unstable dashboard-widgets API (#15154)
Lysander Kiesel51cf9d983091
diff --git a/fern/apis/server/definition/unstable/dashboard-widgets.yml b/fern/apis/server/definition/unstable/dashboard-widgets.ymlindex d1e5b1cda7..44a8d7187d 100644--- a/fern/apis/server/definition/unstable/dashboard-widgets.yml+++ b/fern/apis/server/definition/unstable/dashboard-widgets.yml@@ -11,6 +11,9 @@ service:docs: |List dashboard widgets in the project, ordered by most recentlyupdated first.++ Responses may include legacy `traces` widgets created before this+ API existed. New widgets cannot be created with `view: traces`.method: GETpath: /dashboard-widgetsrequest:@@ -93,7 +96,10 @@ service:type: HORIZONTAL_BARrow_limit: 10get:- docs: Get a dashboard widget by id.+ docs: |+ Get a dashboard widget by id.++ The response may use `view: traces` for legacy widgets.method: GETpath: /dashboard-widgets/{widgetId}path-parameters:@@ -115,6 +121,9 @@ service:Changing `chartType` without sending `chartConfig` resets the configto the new chart type's defaults. When `chartConfig.type` is givenit must match the widget's (possibly updated) `chartType`.++ `view` cannot be changed to the legacy `traces` value. Existing+ `traces` widgets may be updated on other fields.method: PATCHpath: /dashboard-widgets/{widgetId}path-parameters:@@ -159,6 +168,18 @@ types:- name: ScoresCategoricalvalue: scores-categorical+ DashboardWidgetViewWithLegacy:+ docs: |+ Widget data view. Responses may include the legacy `traces` value for+ widgets created before this API existed.+ enum:+ - observations+ - name: ScoresNumeric+ value: scores-numeric+ - name: ScoresCategorical+ value: scores-categorical+ - traces+DashboardWidgetChartType:enum:- LINE_TIME_SERIES@@ -341,7 +362,7 @@ types:description:type: stringview:- type: DashboardWidgetView+ type: DashboardWidgetViewWithLegacydimensions:type: list<DashboardWidgetDimension>metrics:diff --git a/web/public/generated/api/openapi.yml b/web/public/generated/api/openapi.ymlindex b0f05c7da0..ec9f54dadd 100644--- a/web/public/generated/api/openapi.yml+++ b/web/public/generated/api/openapi.yml@@ -6538,6 +6538,9 @@ paths:description: |-List dashboard widgets in the project, ordered by most recentlyupdated first.++ Responses may include legacy `traces` widgets created before this+ API existed. New widgets cannot be created with `view: traces`.operationId: unstable_dashboardWidgets_listtags:- UnstableDashboardWidgets@@ -6689,7 +6692,10 @@ paths:$ref: '#/components/schemas/unstableCreateDashboardWidgetRequest'/api/public/unstable/dashboard-widgets/{widgetId}:get:- description: Get a dashboard widget by id.+ description: |-+ Get a dashboard widget by id.++ The response may use `view: traces` for legacy widgets.operationId: unstable_dashboardWidgets_gettags:- UnstableDashboardWidgets@@ -6752,6 +6758,9 @@ paths:Changing `chartType` without sending `chartConfig` resets the configto the new chart type's defaults. When `chartConfig.type` is givenit must match the widget's (possibly updated) `chartType`.++ `view` cannot be changed to the legacy `traces` value. Existing+ `traces` widgets may be updated on other fields.operationId: unstable_dashboardWidgets_updatetags:- UnstableDashboardWidgets@@ -14811,6 +14820,17 @@ components:- observations- scores-numeric- scores-categorical+ unstableDashboardWidgetViewWithLegacy:+ title: unstableDashboardWidgetViewWithLegacy+ type: string+ enum:+ - observations+ - scores-numeric+ - scores-categorical+ - traces+ description: |-+ Widget data view. Responses may include the legacy `traces` value for+ widgets created before this API existed.unstableDashboardWidgetChartType:title: unstableDashboardWidgetChartTypetype: string@@ -15088,7 +15108,7 @@ components:description:type: stringview:- $ref: '#/components/schemas/unstableDashboardWidgetView'+ $ref: '#/components/schemas/unstableDashboardWidgetViewWithLegacy'dimensions:type: arrayitems:diff --git a/web/src/__tests__/server/unstable-dashboard-widgets-api.servertest.ts b/web/src/__tests__/server/unstable-dashboard-widgets-api.servertest.tsindex a68366f474..9503c58c90 100644--- a/web/src/__tests__/server/unstable-dashboard-widgets-api.servertest.ts+++ b/web/src/__tests__/server/unstable-dashboard-widgets-api.servertest.ts@@ -2,10 +2,19 @@ import {makeAPICall,makeZodVerifiedAPICall,} from "@/src/__tests__/test-utils";-import { PostUnstableDashboardWidgetResponse } from "@/src/features/public-api/types/unstable-dashboard-widgets";+import {+ DeleteUnstableDashboardWidgetResponse,+ GetUnstableDashboardWidgetsResponse,+ PatchUnstableDashboardWidgetResponse,+ PostUnstableDashboardWidgetResponse,+} from "@/src/features/public-api/types/unstable-dashboard-widgets";import { UnstablePublicApiErrorResponse } from "@/src/features/public-api/types/unstable-public-evals-contract";+import { DashboardWidgetViews } from "@langfuse/shared/src/db";import { prisma } from "@langfuse/shared/src/db";-import { createOrgProjectAndApiKey } from "@langfuse/shared/src/server";+import {+ createOrgProjectAndApiKey,+ DashboardService,+} from "@langfuse/shared/src/server";import type { z } from "zod";const baseWidgetBody = {@@ -18,6 +27,18 @@ const baseWidgetBody = {chartType: "NUMBER" as const,};+const legacyTracesWidgetInput = {+ name: "Legacy traces widget",+ description: "Seeded outside the unstable API",+ view: DashboardWidgetViews.TRACES,+ dimensions: [{ field: "name" }],+ metrics: [{ measure: "count", agg: "count" as const }],+ filters: [],+ chartType: "NUMBER" as const,+ chartConfig: { type: "NUMBER" as const },+ minVersion: 1,+};+const expectUnstableError = (response: Awaited<ReturnType<typeof makeAPICall>>,params: {@@ -99,6 +120,109 @@ describe("/api/public/unstable/dashboard-widgets API", () => {});});+ it("lists, updates, and deletes legacy traces widgets", async () => {+ const { auth, projectId } = await createOrgProjectAndApiKey();+ const legacyWidget = await DashboardService.createWidget(+ projectId,+ legacyTracesWidgetInput,+ );++ const listResponse = await makeZodVerifiedAPICall(+ GetUnstableDashboardWidgetsResponse,+ "GET",+ "/api/public/unstable/dashboard-widgets",+ undefined,+ auth,+ );+ expect(listResponse.body.data).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ id: legacyWidget.id,+ view: "traces",+ }),+ ]),+ );++ const patchResponse = await makeZodVerifiedAPICall(+ PatchUnstableDashboardWidgetResponse,+ "PATCH",+ `/api/public/unstable/dashboard-widgets/${legacyWidget.id}`,+ { name: "Renamed legacy traces widget" },+ auth,+ );+ expect(patchResponse.body).toMatchObject({+ id: legacyWidget.id,+ name: "Renamed legacy traces widget",+ view: "traces",+ });++ await makeZodVerifiedAPICall(+ DeleteUnstableDashboardWidgetResponse,+ "DELETE",+ `/api/public/unstable/dashboard-widgets/${legacyWidget.id}`,+ undefined,+ auth,+ );+ });++ it("preserves minVersion on patches that do not change the view", async () => {+ const { auth, projectId } = await createOrgProjectAndApiKey();+ const v1Widget = await DashboardService.createWidget(projectId, {+ ...legacyTracesWidgetInput,+ name: "Legacy v1 observations widget",+ view: DashboardWidgetViews.OBSERVATIONS,+ });++ await makeZodVerifiedAPICall(+ PatchUnstableDashboardWidgetResponse,+ "PATCH",+ `/api/public/unstable/dashboard-widgets/${v1Widget.id}`,+ { name: "Renamed v1 observations widget" },+ auth,+ );+ const afterRename = await prisma.dashboardWidget.findUniqueOrThrow({+ where: { id: v1Widget.id, projectId },+ });+ expect(afterRename.minVersion).toBe(1);++ const patchViewResponse = await makeZodVerifiedAPICall(+ PatchUnstableDashboardWidgetResponse,+ "PATCH",+ `/api/public/unstable/dashboard-widgets/${v1Widget.id}`,+ { view: "scores-numeric" },+ auth,+ );+ expect(patchViewResponse.body.view).toBe("scores-numeric");+ const afterViewChange = await prisma.dashboardWidget.findUniqueOrThrow({+ where: { id: v1Widget.id, projectId },+ });+ expect(afterViewChange.minVersion).toBe(2);+ });++ it("rejects patching a widget into the legacy traces view", async () => {+ const { auth } = await createOrgProjectAndApiKey();++ const createResponse = await makeZodVerifiedAPICall(+ PostUnstableDashboardWidgetResponse,+ "POST",+ "/api/public/unstable/dashboard-widgets",+ baseWidgetBody,+ auth,+ );++ const response = await makeAPICall(+ "PATCH",+ `/api/public/unstable/dashboard-widgets/${createResponse.body.id}`,+ { view: "traces" },+ auth,+ );++ expectUnstableError(response, {+ status: 400,+ code: "invalid_body",+ });+ });+it("rejects a chartConfig.type that contradicts chartType", async () => {const { auth } = await createOrgProjectAndApiKey();diff --git a/web/src/features/public-api/types/unstable-dashboard-widgets.ts b/web/src/features/public-api/types/unstable-dashboard-widgets.tsindex 7230fe8d2f..f3c3b86850 100644--- a/web/src/features/public-api/types/unstable-dashboard-widgets.ts+++ b/web/src/features/public-api/types/unstable-dashboard-widgets.ts@@ -14,6 +14,13 @@ export const PostUnstableDashboardWidgetView = z.enum(["scores-categorical",]);+export const DashboardWidgetViewOutput = z.enum([+ "observations",+ "scores-numeric",+ "scores-categorical",+ "traces",+]);+const DashboardWidgetMetricSchema = MetricSchema.extend({measure: z.string().min(1),agg: metricAggregations,@@ -55,7 +62,7 @@ export const PublicDashboardWidget = zupdatedAt: z.coerce.date(),name: z.string(),description: z.string(),- view: PostUnstableDashboardWidgetView,+ view: DashboardWidgetViewOutput,dimensions: z.array(DashboardWidgetDimensionSchema),metrics: z.array(DashboardWidgetMetricSchema),filters: z.array(singleFilter),@@ -94,3 +101,6 @@ export const DeleteUnstableDashboardWidgetResponse = z.object({export type PostUnstableDashboardWidgetBodyType = z.infer<typeof PostUnstableDashboardWidgetBody>;+export type DashboardWidgetViewOutputType = z.infer<+ typeof DashboardWidgetViewOutput+>;diff --git a/web/src/features/widgets/server/public-dashboard-widget-service.ts b/web/src/features/widgets/server/public-dashboard-widget-service.tsindex c5c30e5068..9453ea456d 100644--- a/web/src/features/widgets/server/public-dashboard-widget-service.ts+++ b/web/src/features/widgets/server/public-dashboard-widget-service.ts@@ -18,6 +18,7 @@ import { auditLog } from "@/src/features/audit-logs/auditLog";import { createUnstablePublicApiError } from "@/src/features/public-api/server/unstable-public-api-error-contract";import {PostUnstableDashboardWidgetResponse,+ type DashboardWidgetViewOutputType,type PostUnstableDashboardWidgetBodyType,} from "@/src/features/public-api/types/unstable-dashboard-widgets";import { ChartConfigSchema, LangfuseNotFoundError } from "@langfuse/shared";@@ -37,14 +38,13 @@ type NormalizedWidgetInput = Omit<"id" | "createdAt" | "updatedAt"> & { minVersion: number };-const viewMapping: Record<- PostUnstableDashboardWidgetBodyType["view"],- DashboardWidgetViews-> = {- observations: DashboardWidgetViews.OBSERVATIONS,- "scores-numeric": DashboardWidgetViews.SCORES_NUMERIC,- "scores-categorical": DashboardWidgetViews.SCORES_CATEGORICAL,-};+const viewMapping: Record<DashboardWidgetViewOutputType, DashboardWidgetViews> =+ {+ observations: DashboardWidgetViews.OBSERVATIONS,+ "scores-numeric": DashboardWidgetViews.SCORES_NUMERIC,+ "scores-categorical": DashboardWidgetViews.SCORES_CATEGORICAL,+ traces: DashboardWidgetViews.TRACES,+ };const reverseViewMapping: Record<DashboardWidgetViews,@@ -94,8 +94,13 @@ function getPublicDashboardWidgetViewDeclaration(}}+type PublicDashboardWidgetInput = Omit<+ PostUnstableDashboardWidgetBodyType,+ "view"+> & { view: DashboardWidgetViewOutputType };+export function normalizePublicDashboardWidgetInput(- input: PostUnstableDashboardWidgetBodyType,+ input: PublicDashboardWidgetInput,minVersion = 2,): NormalizedWidgetInput {const { mappedFilters, unsupportedFilters } =@@ -336,6 +341,10 @@ export async function updatePublicDashboardWidget(params: {const chartTypeChanged =params.input.chartType !== undefined &¶ms.input.chartType !== currentPublic.chartType;+ // Keep the stored minVersion (and thus v1/v2 query semantics) unless the+ // caller explicitly changes the view; view changes land on v2 like create.+ const mergedView = params.input.view ?? currentPublic.view;+ const minVersion = mergedView === currentPublic.view ? current.minVersion : 2;const input = normalizePublicDashboardWidgetInput({...currentPublic,@@ -349,7 +358,7 @@ export async function updatePublicDashboardWidget(params: {? { type: params.input.chartType }: currentPublic.chartConfig),},- current.minVersion,+ minVersion,);validatePublicDashboardWidgetInput(input);const updated = await DashboardService.updateWidget(