πͺ’ 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(dashboards): add all filter options + value suggestions to widget and monitor forms (LFE-9570) (#15166)
Mark Salpeter77e8a2346a60
diff --git a/packages/shared/src/server/queries/clickhouse-sql/event-filter-options.ts b/packages/shared/src/server/queries/clickhouse-sql/event-filter-options.tsindex acee07d3cd..5961bdcadc 100644--- a/packages/shared/src/server/queries/clickhouse-sql/event-filter-options.ts+++ b/packages/shared/src/server/queries/clickhouse-sql/event-filter-options.ts@@ -18,11 +18,23 @@ export const EVENTS_FILTER_OPTION_TOP_N = 1000;// Sentinel "column" carrying the approx total observation count in the facet result.export const EVENTS_APPROX_TOTAL_COUNT_MARKER = "__approxTotalCount__";-// Facet tuple format: (option name, option value, value-occurrence count, sort order).-const EVENTS_APPROX_TOTAL_COUNT_TUPLE = `tuple('${EVENTS_APPROX_TOTAL_COUNT_MARKER}', '', toUInt64(approx_total_count), toInt64(0))`;+// Facet tuple format: (option name, option value, value-occurrence count, sort order, display value).+const EVENTS_APPROX_TOTAL_COUNT_TUPLE = `tuple('${EVENTS_APPROX_TOTAL_COUNT_MARKER}', '', toUInt64(approx_total_count), toInt64(0), '')`;const EVENTS_FILTER_OPTION_TOP_K_MAX_N = 65_536;+/** EVENTS_FILTER_OPTION_SAMPLE_ROWS caps the events read behind the UI filter option queries. */+export const EVENTS_FILTER_OPTION_SAMPLE_ROWS = 6_000_000;++// MergeTree virtual column = 1/fraction, exactly 1.0 when unsampled.+const EVENTS_SAMPLE_FACTOR_SELECT = "any(e._sample_factor) AS sample_factor";++/** eventFilterOptionCountExpression scales a grouped event count back to full-table scale. */+const eventFilterOptionCountExpression = (sampleRows: number) =>+ sampleRows > 0+ ? "toUInt64(round(count() * any(e._sample_factor)))"+ : "count()";+type EventFilterOptionSort = "countDesc" | "alpha" | "booleanAsc";type EventFilterOptionDefinition =@@ -32,6 +44,13 @@ type EventFilterOptionDefinition =includeWhen: string;sort: EventFilterOptionSort;}+ | {+ kind: "labeledScalar";+ expression: string;+ labelExpression: string;+ includeWhen: string;+ sort: EventFilterOptionSort;+ }| {kind: "array";expression: string;@@ -160,8 +179,9 @@ const EVENTS_FILTER_OPTION_DEFINITIONS = {sort: "countDesc",},experimentId: {- kind: "scalar",+ kind: "labeledScalar",expression: "e.experiment_id",+ labelExpression: "e.experiment_name",includeWhen: "e.experiment_id IS NOT NULL AND length(e.experiment_id) > 0",sort: "countDesc",},@@ -192,6 +212,12 @@ const EVENTS_FILTER_OPTION_DEFINITIONS = {expression: "e.tool_call_names",sort: "countDesc",},+ metadataKeys: {+ kind: "array",+ expression: "e.metadata_names",+ sort: "countDesc",+ distinct: true,+ },} satisfies Record<string, EventFilterOptionDefinition>;export type EventFilterOptionColumn =@@ -201,6 +227,7 @@ export type EventFilterOptionRow = {column: EventFilterOptionColumn;value: string;count: number;+ displayValue?: string;};export type EventFilterOptionScope = "scoredTraces";@@ -261,7 +288,7 @@ const optionValuesArrayExpression = (): string => {const definition = EVENTS_FILTER_OPTION_DEFINITIONS[column];- if (definition.kind === "scalar") {+ if (definition.kind === "scalar" || definition.kind === "labeledScalar") {return `if(${definition.includeWhen}, [${stringValueExpression(definition.expression)}], CAST([], 'Array(String)'))`;}@@ -280,7 +307,7 @@ const optionValuesArrayExpression = (const optionPresenceCondition = (column: EventFilterOptionColumn): string => {const definition = EVENTS_FILTER_OPTION_DEFINITIONS[column];- if (definition.kind === "scalar") {+ if (definition.kind === "scalar" || definition.kind === "labeledScalar") {return definition.includeWhen;}@@ -311,6 +338,10 @@ const optionTopKSelectExpression = (column: EventFilterOptionColumn) => {return `approx_top_kIf({optionLimit: UInt64})(${stringValueExpression(definition.expression)}, ${definition.includeWhen}) AS ${optionTopAlias(column)}`;}+ if (definition.kind === "labeledScalar") {+ return `approx_top_kIf({optionLimit: UInt64})(tuple(${stringValueExpression(definition.expression)}, ${stringValueExpression(definition.labelExpression)}), ${definition.includeWhen}) AS ${optionTopAlias(column)}`;+ }+if (definition.kind === "boolean") {return `arrayFilter(option -> tupleElement(option, 2) > 0, [tuple('false', countIf(NOT (${definition.expression})), toUInt64(0)), tuple('true', countIf(${definition.expression}), toUInt64(0))]) AS ${optionTopAlias(column)}`;}@@ -329,8 +360,17 @@ const optionRowsArrayExpression = (column: EventFilterOptionColumn) => {: definition.sort === "booleanAsc"? "if(tupleElement(option, 1) = 'true', toInt64(1), toInt64(0))": "toInt64(0)";-- return `arrayMap(option -> tuple(${eventFilterOptionColumnSqlLiteral(column)}, tupleElement(option, 1), tupleElement(option, 2), ${sortKeyExpression}), ${topAlias})`;+ // labeledScalar top-k entries nest (value, label) in element 1; scalar/array/+ // boolean entries put the value directly in element 1 and carry no label.+ const isLabeled = definition.kind === "labeledScalar";+ const valueExpression = isLabeled+ ? "tupleElement(tupleElement(option, 1), 1)"+ : "tupleElement(option, 1)";+ const displayValueExpression = isLabeled+ ? "tupleElement(tupleElement(option, 1), 2)"+ : "''";++ return `arrayMap(option -> tuple(${eventFilterOptionColumnSqlLiteral(column)}, ${valueExpression}, tupleElement(option, 2), ${sortKeyExpression}, ${displayValueExpression}), ${topAlias})`;};const eventFilterOptionScopeCondition = (@@ -349,11 +389,14 @@ export const buildEventsFilterOptionColumnQuery = (params: {limit: number;offset?: number;scope?: EventFilterOptionScope;+ sampleRows?: number;}): { query: string; params: Record<string, unknown> } | null => {if (params.limit <= 0) {return null;}+ const sampleRows = params.sampleRows ?? 0;+const column = normalizeEventFilterOptionColumn(params.column);const definition = EVENTS_FILTER_OPTION_DEFINITIONS[column];const eventsFilter = new FilterList(@@ -365,7 +408,7 @@ export const buildEventsFilterOptionColumnQuery = (params: {);const valueExpression =- definition.kind === "scalar"+ definition.kind === "scalar" || definition.kind === "labeledScalar"? `toString(${definition.expression})`: definition.kind === "boolean"? `if(${definition.expression}, 'true', 'false')`@@ -374,12 +417,13 @@ export const buildEventsFilterOptionColumnQuery = (params: {const queryBuilder = new EventsAggQueryBuilder({projectId: params.projectId,groupByColumn: "value",- selectExpression: `${eventFilterOptionColumnSqlLiteral(column)} AS column, ${valueExpression} AS value, count() AS count`,+ selectExpression: `${eventFilterOptionColumnSqlLiteral(column)} AS column, ${valueExpression} AS value, ${eventFilterOptionCountExpression(sampleRows)} AS count`,}).where(eventsFilter.apply()).whereRaw(optionPresenceCondition(column)).orderBy(singleColumnOrderBy(column))- .limit(params.limit, params.offset ?? 0);+ .limit(params.limit, params.offset ?? 0)+ .sampleRows(sampleRows);if (params.scope) {queryBuilder.whereRaw(eventFilterOptionScopeCondition(params.scope));@@ -396,6 +440,7 @@ export const buildEventsFilterOptionsForColumnsQuery = (params: {scope?: EventFilterOptionScope;// approx total = uniq(span_id) over the bulk scan's full-filter WHERE; re-verify if the scan ever drops predicatesincludeApproxCount?: boolean;+ sampleRows?: number;}): { query: string; params: Record<string, unknown> } | null => {const columns = uniqueEventFilterOptionColumns(params.columns);if (columns.length === 0 || params.limit <= 0) {@@ -410,11 +455,15 @@ export const buildEventsFilterOptionsForColumnsQuery = (params: {});const includeApproxTotal = params.includeApproxCount === true;+ const sampleRows = params.sampleRows ?? 0;+ const sampled = sampleRows > 0;aggregatedOptionsBuilder.selectRaw(...columns.map(optionTopKSelectExpression),...(includeApproxTotal ? ["uniq(e.span_id) AS approx_total_count"] : []),+ ...(sampled ? [EVENTS_SAMPLE_FACTOR_SELECT] : []),);+ aggregatedOptionsBuilder.sampleRows(sampleRows);if (params.scope) {aggregatedOptionsBuilder.whereRaw(@@ -430,6 +479,12 @@ export const buildEventsFilterOptionsForColumnsQuery = (params: {? `,\n [${EVENTS_APPROX_TOTAL_COUNT_TUPLE}]`: "";+ // approx_top_k counts live inside the tuple, so scale in the outer projection.+ const sampleFactorRow = sampled ? ",\n sample_factor" : "";+ const countExpression = sampled+ ? "toUInt64(round(tupleElement(option, 3) * sample_factor))"+ : "tupleElement(option, 3)";+const query = `WITH aggregated_options AS (${aggregatedOptionsQuery}@@ -438,13 +493,14 @@ option_rows AS (SELECTarrayJoin(arrayConcat(${columns.map(optionRowsArrayExpression).join(",\n ")}${approxTotalCountRow}- )) AS option+ )) AS option${sampleFactorRow}FROM aggregated_options)SELECTtupleElement(option, 1) AS column,tupleElement(option, 2) AS value,- tupleElement(option, 3) AS count+ ${countExpression} AS count,+ tupleElement(option, 5) AS displayValueFROM option_rowsORDER BY column ASC, tupleElement(option, 4) ASC, tupleElement(option, 2) ASC`.trim();@@ -457,3 +513,43 @@ ORDER BY column ASC, tupleElement(option, 4) ASC, tupleElement(option, 2) ASC},};};++/** buildEventsMetadataValuesQuery builds the top-N distinct value query for one metadata key on the events table. */+export const buildEventsMetadataValuesQuery = (params: {+ projectId: string;+ filter: FilterState;+ key: string;+ limit: number;+ sampleRows?: number;+}): { query: string; params: Record<string, unknown> } | null => {+ if (params.limit <= 0 || params.key.length === 0) {+ return null;+ }++ const sampleRows = params.sampleRows ?? 0;+ const eventsFilter = new FilterList(+ createFilterFromFilterState(+ params.filter,+ eventsTableUiColumnDefinitions,+ eventsTableCols,+ ),+ );++ const valueAccessor =+ "e.metadata_values[indexOf(e.metadata_names, {metadataKey: String})]";+ const queryBuilder = new EventsAggQueryBuilder({+ projectId: params.projectId,+ groupByColumn: "value",+ selectExpression: `${valueAccessor} AS value, ${eventFilterOptionCountExpression(sampleRows)} AS count`,+ })+ .where(eventsFilter.apply())+ .whereRaw("has(e.metadata_names, {metadataKey: String})", {+ metadataKey: params.key,+ })+ .whereRaw(`length(${valueAccessor}) > 0`)+ .orderBy("ORDER BY count() DESC, value ASC")+ .limit(params.limit, 0)+ .sampleRows(sampleRows);++ return queryBuilder.buildWithParams();+};diff --git a/packages/shared/src/server/queries/clickhouse-sql/event-query-builder.ts b/packages/shared/src/server/queries/clickhouse-sql/event-query-builder.tsindex 53347e3479..87f8dcb451 100644--- a/packages/shared/src/server/queries/clickhouse-sql/event-query-builder.ts+++ b/packages/shared/src/server/queries/clickhouse-sql/event-query-builder.ts@@ -782,6 +782,19 @@ abstract class AbstractQueryBuilder {abstract class AbstractCTEQueryBuilder extends AbstractQueryBuilder {protected ctes: string[] = [];protected joins: string[] = [];+ protected sampleRowCount: number | null = null;++ /** sampleRows caps the base table read at `rows` events via ClickHouse SAMPLE. */+ sampleRows(rows: number): this {+ if (rows > 0) {+ this.sampleRowCount = Math.floor(rows);+ }+ return this;+ }++ protected buildSampleSection(): string {+ return this.sampleRowCount === null ? "" : ` SAMPLE ${this.sampleRowCount}`;+ }/*** Add a CTE (Common Table Expression) to the query@@ -948,7 +961,7 @@ abstract class BaseEventsQueryBuilder<// FROM - choose table based on data requirementsconst tableName = this.getTableName();- parts.push(`FROM ${tableName} e`);+ parts.push(`FROM ${tableName} e${this.buildSampleSection()}`);// JOINsconst joinSection = this.buildJoinSection();@@ -1867,7 +1880,7 @@ export class EventsAggQueryBuilder extends AbstractCTEQueryBuilder {parts.push(`SELECT ${this.selectExpression}`);// FROM - use events_core for reads (lightweight table with truncated I/O)- parts.push("FROM events_core e");+ parts.push(`FROM events_core e${this.buildSampleSection()}`);// JOINsconst joinSection = this.buildJoinSection();diff --git a/packages/shared/src/server/queries/index.ts b/packages/shared/src/server/queries/index.tsindex 0d1909905a..5a35767abb 100644--- a/packages/shared/src/server/queries/index.ts+++ b/packages/shared/src/server/queries/index.ts@@ -74,6 +74,7 @@ export {export {buildEventsFilterOptionColumnQuery,buildEventsFilterOptionsForColumnsQuery,+ buildEventsMetadataValuesQuery,EVENTS_FILTER_OPTION_TOP_N,EVENTS_APPROX_TOTAL_COUNT_MARKER,normalizeEventFilterOptionColumn,diff --git a/packages/shared/src/server/repositories/events.ts b/packages/shared/src/server/repositories/events.tsindex fe3c28dd6f..1fcb2c60ae 100644--- a/packages/shared/src/server/repositories/events.ts+++ b/packages/shared/src/server/repositories/events.ts@@ -113,6 +113,8 @@ import {import {buildEventsFilterOptionColumnQuery,buildEventsFilterOptionsForColumnsQuery,+ buildEventsMetadataValuesQuery,+ EVENTS_FILTER_OPTION_SAMPLE_ROWS,EVENTS_FILTER_OPTION_TOP_N,type EventFilterOptionRow,type EventFilterOptionColumn,@@ -2048,6 +2050,7 @@ const queryEventsFilterOptionsForColumns = async (params: {limit: params.limit,scope: params.scope,includeApproxCount: params.includeApproxCount,+ sampleRows: EVENTS_FILTER_OPTION_SAMPLE_ROWS,});if (!queryWithParams) {@@ -2064,6 +2067,7 @@ const queryEventsFilterOptionColumn = async (params: {limit: number;offset?: number;scope?: EventFilterOptionScope;+ sampleRows?: number;}) => {const queryWithParams = buildEventsFilterOptionColumnQuery({projectId: params.projectId,@@ -2072,6 +2076,7 @@ const queryEventsFilterOptionColumn = async (params: {limit: params.limit,offset: params.offset,scope: params.scope,+ sampleRows: params.sampleRows,});if (!queryWithParams) {@@ -2094,6 +2099,7 @@ export const getEventsFilterOptionsForColumns = async (params: {limit: params.topN ?? EVENTS_FILTER_OPTION_TOP_N,});+// Unsampled: the cursor contract lets MCP agents page the true distinct set.export const getEventsFilterOptionValuesPage = async (params: {projectId: string;filter: FilterState;@@ -2109,6 +2115,36 @@ export const getEventsFilterOptionValuesPage = async (params: {offset: params.offset,});+/** EventsMetadataOptionRow is one metadata key or value with its event count. */+export type EventsMetadataOptionRow = { value: string; count: number };++/** getEventsMetadataValues returns the top-N distinct values for one metadata key on the events table. */+export const getEventsMetadataValues = async (params: {+ projectId: string;+ filter: FilterState;+ key: string;+ topN?: number;+}): Promise<EventsMetadataOptionRow[]> => {+ const queryWithParams = buildEventsMetadataValuesQuery({+ projectId: params.projectId,+ filter: params.filter,+ key: params.key,+ limit: params.topN ?? EVENTS_FILTER_OPTION_TOP_N,+ sampleRows: EVENTS_FILTER_OPTION_SAMPLE_ROWS,+ });++ if (!queryWithParams) {+ return [];+ }++ return queryClickhouse<EventsMetadataOptionRow>({+ query: queryWithParams.query,+ params: queryWithParams.params,+ tags: { projectId: params.projectId },+ preferredClickhouseService: "EventsReadOnly",+ });+};+const getSingleEventsFilterOptionColumn = async (projectId: string,filter: FilterState,@@ -2121,6 +2157,7 @@ const getSingleEventsFilterOptionColumn = async (column,limit: opts?.limit ?? EVENTS_FILTER_OPTION_TOP_N,scope: opts?.scope,+ sampleRows: EVENTS_FILTER_OPTION_SAMPLE_ROWS,});export const getEventsGroupedByTraceName = async (@@ -2165,6 +2202,7 @@ export const getEventsGroupedByUserId = async (return rows.map((row) => ({ userId: row.value, count: row.count }));};+// Unsampled: a sampled max() under-reports, hiding real outliers from MCP agents.export const getEventsNumericStatsByFilterColumn = async (projectId: string,filter: FilterState,diff --git a/web/src/__tests__/server/dashboard-widget-version.servertest.ts b/web/src/__tests__/server/dashboard-widget-version.servertest.tsindex bdcf558179..f28cdb7dc9 100644--- a/web/src/__tests__/server/dashboard-widget-version.servertest.ts+++ b/web/src/__tests__/server/dashboard-widget-version.servertest.ts@@ -220,9 +220,9 @@ describe("dashboard widget minVersion", () => {]);});- it("maps Observation Release to the v2-only release field", () => {+ it("keeps the retired Observation Release label on the release field", () => {expect(- mapWidgetUiTableFilterToView("observations", [+ mapLegacyUiTableFilterToView("observations", [{column: "Observation Release",operator: "=",@@ -239,6 +239,26 @@ describe("dashboard widget minVersion", () => {},]);});++ it("maps an editor Release row to the v2 release field", () => {+ expect(+ mapWidgetUiTableFilterToView("observations", [+ {+ column: "Release",+ operator: "=",+ value: "2026.04",+ type: "string",+ },+ ]),+ ).toEqual([+ {+ column: "release",+ operator: "=",+ value: "2026.04",+ type: "string",+ },+ ]);+ });});// ββ Service layer tests βββββββββββββββββββββββββββββββββββββββββββββdiff --git a/web/src/__tests__/server/event-query-builder.servertest.ts b/web/src/__tests__/server/event-query-builder.servertest.tsindex 472f688295..db02aff67b 100644--- a/web/src/__tests__/server/event-query-builder.servertest.ts+++ b/web/src/__tests__/server/event-query-builder.servertest.ts@@ -1,6 +1,7 @@import {buildEventsFilterOptionColumnQuery,buildEventsFilterOptionsForColumnsQuery,+ buildEventsMetadataValuesQuery,CTEQueryBuilder,createFilterFromFilterState,EventsAggregationQueryBuilder,@@ -45,6 +46,44 @@ describe("buildEventsFilterOptionsForColumnsQuery", () => {expect(built.params).not.toHaveProperty("optionReserved");});+ it("samples the base events scan and scales counts by the sample factor", () => {+ const built = buildEventsFilterOptionsForColumnsQuery({+ projectId: "test-project",+ filter: [],+ columns: ["name"],+ limit: 1000,+ sampleRows: 6_000_000,+ includeApproxCount: true,+ });++ expect(built).not.toBeNull();+ if (!built) throw new Error("expected query");++ expect(built.query).toContain("FROM events_core e SAMPLE 6000000");+ expect(built.query).toContain("any(e._sample_factor) AS sample_factor");+ expect(built.query).toContain(+ "toUInt64(round(tupleElement(option, 3) * sample_factor)) AS count",+ );+ });++ it("leaves the bulk facet scan exact without a sample size", () => {+ const built = buildEventsFilterOptionsForColumnsQuery({+ projectId: "test-project",+ filter: [],+ columns: ["name"],+ limit: 1000,+ includeApproxCount: true,+ });++ expect(built).not.toBeNull();+ if (!built) throw new Error("expected query");++ expect(built.query).not.toContain("SAMPLE");+ expect(built.query).not.toContain("_sample_factor");+ expect(built.query).not.toContain("sample_factor");+ expect(built.query).toContain("tupleElement(option, 3) AS count");+ });+it("orders bulk filter option rows by per-column sort key and value", () => {const built = buildEventsFilterOptionsForColumnsQuery({projectId: "test-project",@@ -57,19 +96,55 @@ describe("buildEventsFilterOptionsForColumnsQuery", () => {if (!built) throw new Error("expected query");expect(built.query).toContain(- "tuple('name', tupleElement(option, 1), tupleElement(option, 2), -toInt64(tupleElement(option, 2)))",+ "tuple('name', tupleElement(option, 1), tupleElement(option, 2), -toInt64(tupleElement(option, 2)), '')",);expect(built.query).toContain(- "tuple('traceTags', tupleElement(option, 1), tupleElement(option, 2), toInt64(0))",+ "tuple('traceTags', tupleElement(option, 1), tupleElement(option, 2), toInt64(0), '')",);expect(built.query).toContain(- "tuple('isRootObservation', tupleElement(option, 1), tupleElement(option, 2), if(tupleElement(option, 1) = 'true', toInt64(1), toInt64(0)))",+ "tuple('isRootObservation', tupleElement(option, 1), tupleElement(option, 2), if(tupleElement(option, 1) = 'true', toInt64(1), toInt64(0)), '')",);expect(built.query).toContain("ORDER BY column ASC, tupleElement(option, 4) ASC, tupleElement(option, 2) ASC",);});+ it("scans e.release for the release filter option column", () => {+ const built = buildEventsFilterOptionsForColumnsQuery({+ projectId: "test-project",+ filter: [],+ columns: ["release"],+ limit: 1000,+ });++ expect(built).not.toBeNull();+ if (!built) throw new Error("expected query");++ expect(built.query).toContain("e.release");+ expect(built.query).toContain("tuple('release'");+ expect(built.query).toContain("AS displayValue");+ });++ it("labels experimentId options with experiment_name via displayValue", () => {+ const built = buildEventsFilterOptionsForColumnsQuery({+ projectId: "test-project",+ filter: [],+ columns: ["experimentId"],+ limit: 1000,+ });++ expect(built).not.toBeNull();+ if (!built) throw new Error("expected query");++ expect(built.query).toContain(+ "tuple(toString(ifNull(e.experiment_id, '')), toString(ifNull(e.experiment_name, '')))",+ );+ expect(built.query).toContain(+ "tuple('experimentId', tupleElement(tupleElement(option, 1), 1), tupleElement(option, 2), -toInt64(tupleElement(option, 2)), tupleElement(tupleElement(option, 1), 2))",+ );+ expect(built.query).toContain("tupleElement(option, 5) AS displayValue");+ });+it("applies events filters to the single base scan", () => {const built = buildEventsFilterOptionsForColumnsQuery({projectId: "test-project",@@ -194,6 +269,40 @@ describe("buildEventsFilterOptionsForColumnsQuery", () => {});});+ it("samples the grouped column scan and scales its counts", () => {+ const built = buildEventsFilterOptionColumnQuery({+ projectId: "test-project",+ filter: [],+ column: "level",+ limit: 10,+ sampleRows: 6_000_000,+ });++ expect(built).not.toBeNull();+ if (!built) throw new Error("expected query");++ expect(built.query).toContain("FROM events_core e SAMPLE 6000000");+ expect(built.query).toContain(+ "toUInt64(round(count() * any(e._sample_factor))) AS count",+ );+ });++ it("leaves the grouped column scan exact without a sample size", () => {+ const built = buildEventsFilterOptionColumnQuery({+ projectId: "test-project",+ filter: [],+ column: "level",+ limit: 10,+ });++ expect(built).not.toBeNull();+ if (!built) throw new Error("expected query");++ expect(built.query).not.toContain("SAMPLE");+ expect(built.query).not.toContain("_sample_factor");+ expect(built.query).toContain("count() AS count");+ });+it("builds release filter options from the observation release column", () => {const built = buildEventsFilterOptionColumnQuery({projectId: "test-project",@@ -397,6 +506,29 @@ describe("buildEventsFilterOptionsForColumnsQuery", () => {});});+ it("folds distinct metadata key names into the bulk facet scan", () => {+ const built = buildEventsFilterOptionsForColumnsQuery({+ projectId: "test-project",+ filter: [],+ columns: ["metadataKeys"],+ limit: 100,+ });++ expect(built).not.toBeNull();+ if (!built) throw new Error("expected query");++ expect(built.query.match(/FROM events_core e/g)).toHaveLength(1);+ expect(built.query).toContain("e.project_id = {projectId: String}");+ expect(built.query).toContain("arrayDistinct(e.metadata_names)");+ expect(built.query).toContain("approx_top_kArray");+ expect(built.query).toContain("tuple('metadataKeys'");+ expect(built.query).not.toMatch(/\bJOIN\b/i);+ expect(built.params).toMatchObject({+ projectId: "test-project",+ optionLimit: 100,+ });+ });+it("rejects runtime values outside the filter option column registry", () => {expect(() =>buildEventsFilterOptionsForColumnsQuery({@@ -418,6 +550,64 @@ describe("buildEventsFilterOptionsForColumnsQuery", () => {});});+describe("buildEventsMetadataValuesQuery", () => {+ it("aggregates values for a specific metadata key", () => {+ const built = buildEventsMetadataValuesQuery({+ projectId: "test-project",+ filter: [],+ key: "region",+ limit: 100,+ });++ expect(built).not.toBeNull();+ if (!built) throw new Error("expected query");++ expect(built.query).toContain("FROM events_core e");+ expect(built.query).toContain(+ "e.metadata_values[indexOf(e.metadata_names, {metadataKey: String})]",+ );+ expect(built.query).toContain(+ "has(e.metadata_names, {metadataKey: String})",+ );+ expect(built.query).toContain("GROUP BY value");+ expect(built.query).toContain("ORDER BY count() DESC, value ASC");+ expect(built.params).toMatchObject({+ projectId: "test-project",+ metadataKey: "region",+ limit: 100,+ });+ });++ it("samples the value scan and scales its counts", () => {+ const built = buildEventsMetadataValuesQuery({+ projectId: "test-project",+ filter: [],+ key: "region",+ limit: 100,+ sampleRows: 6_000_000,+ });++ expect(built).not.toBeNull();+ if (!built) throw new Error("expected query");++ expect(built.query).toContain("FROM events_core e SAMPLE 6000000");+ expect(built.query).toContain(+ "toUInt64(round(count() * any(e._sample_factor))) AS count",+ );+ });++ it("returns null for an empty key", () => {+ expect(+ buildEventsMetadataValuesQuery({+ projectId: "test-project",+ filter: [],+ key: "",+ limit: 100,+ }),+ ).toBeNull();+ });+});+describe("CTEQueryBuilder", () => {it("should compose multiple CTEs with type-safe references", () => {const tracesBuilder = new EventsAggregationQueryBuilder({diff --git a/web/src/__tests__/server/repositories/event-repository.servertest.ts b/web/src/__tests__/server/repositories/event-repository.servertest.tsindex 56f42932b1..7efe2804de 100644--- a/web/src/__tests__/server/repositories/event-repository.servertest.ts+++ b/web/src/__tests__/server/repositories/event-repository.servertest.ts@@ -1024,6 +1024,40 @@ describe("Clickhouse Events Repository Test", () => {expect(options.trace_scores_avg).toBeUndefined();expect(options.trace_score_categories).toBeUndefined();expect(options.trace_score_booleans).toBeUndefined();+ expect(options.metadataKeys).toBeUndefined();+ });+ });++ it("loads requested metadata key filter option column", async () => {+ const uniqueProjectId = randomUUID();+ const traceId = randomUUID();+ const now = Date.now();++ await createEventsCh([+ createEvent({+ id: randomUUID(),+ span_id: randomUUID(),+ project_id: uniqueProjectId,+ trace_id: traceId,+ type: "SPAN",+ name: "metadata-filter-option-event",+ metadata_names: ["region", "tier"],+ metadata_values: ["us-east", "gold"],+ start_time: now * 1000,+ }),+ ]);++ await waitForExpect(async () => {+ const options = await getEventFilterOptions({+ projectId: uniqueProjectId,+ columns: ["metadataKeys"],+ });++ expect(options.metadataKeys?.map((key) => key.value)).toEqual(+ expect.arrayContaining(["region", "tier"]),+ );+ expect(options.level).toBeUndefined();+ expect(options.name).toBeUndefined();});});diff --git a/web/src/features/chart-view/lib/chartFilterCompatibility.clienttest.ts b/web/src/features/chart-view/lib/chartFilterCompatibility.clienttest.tsindex a4db478549..b52a9e6c25 100644--- a/web/src/features/chart-view/lib/chartFilterCompatibility.clienttest.ts+++ b/web/src/features/chart-view/lib/chartFilterCompatibility.clienttest.ts@@ -17,6 +17,8 @@ describe("chartFilterExclusionReason", () => {"userId","sessionId","traceName",+ "version",+ "release","traceTags","toolNames","experimentId",diff --git a/web/src/features/chart-view/lib/chartFilterCompatibility.ts b/web/src/features/chart-view/lib/chartFilterCompatibility.tsindex 76d0d332f1..44fe4d4e39 100644--- a/web/src/features/chart-view/lib/chartFilterCompatibility.ts+++ b/web/src/features/chart-view/lib/chartFilterCompatibility.ts@@ -36,6 +36,7 @@ export const FORWARDABLE_CHART_FILTER_COLUMNS: ReadonlySet<string> = new Set(["userId","sessionId","version",+ "release","promptName",// NOTE: promptVersion is intentionally NOT forwardable. The search-bar// grammar registers it as a numeric field, so `promptVersion:>2` lowers to adiff --git a/web/src/features/dashboard/lib/dashboardUiTableToViewMapping.clienttest.ts b/web/src/features/dashboard/lib/dashboardUiTableToViewMapping.clienttest.tsindex 38b9132b9d..0b74211bc6 100644--- a/web/src/features/dashboard/lib/dashboardUiTableToViewMapping.clienttest.ts+++ b/web/src/features/dashboard/lib/dashboardUiTableToViewMapping.clienttest.ts@@ -1,6 +1,7 @@import { describe, expect, it } from "vitest";import {+ displayNameForFilterColumn,mapViewFilterToUiTableFilter,mapWidgetUiTableFilterToView,} from "./dashboardUiTableToViewMapping";@@ -29,3 +30,22 @@ describe("widget filter mappings", () => {},);});++describe("displayNameForFilterColumn", () => {+ it.each([+ ["providedModelName", "Model"],+ ["model", "Model"],+ ["Model", "Model"],+ ["isRootObservation", "Is Root Observation"],+ ["userId", "User"],+ ["calledToolNames", "Tool Names (Called)"],+ ])("resolves %s to %s", (column, expected) => {+ expect(displayNameForFilterColumn(column)).toBe(expected);+ });++ it("unmapped column: falls back to the raw column", () => {+ expect(displayNameForFilterColumn("totallyUnknownColumn")).toBe(+ "totallyUnknownColumn",+ );+ });+});diff --git a/web/src/features/dashboard/lib/dashboardUiTableToViewMapping.ts b/web/src/features/dashboard/lib/dashboardUiTableToViewMapping.tsindex f6420be0f3..c5b15d0d32 100644--- a/web/src/features/dashboard/lib/dashboardUiTableToViewMapping.ts+++ b/web/src/features/dashboard/lib/dashboardUiTableToViewMapping.ts@@ -146,7 +146,10 @@ const viewFilterDefinitions: Record<),defineField("release",- sourceSpec("Observation Release", { uiTableId: "release" }),+ sourceSpec("Release", {+ uiTableId: "release",+ aliases: ["Observation Release"],+ }),),defineField("version", sourceSpec("Version", { uiTableId: "version" })),// Experiment fields (v2 only - experiment data only exists in events table)@@ -485,6 +488,13 @@ export const normalizeStoredWidgetFiltersForEditor = (};};+/** displayNameForFilterColumn resolves any filter column spelling to the label the filter builder shows. */+export const displayNameForFilterColumn = (column: string): string =>+ allWidgetFilterMappings.find(+ (mapping) =>+ mapping.viewName === column || matchesFilterMapping(mapping, column),+ )?.uiTableName ?? column;+export const mapViewFilterToUiTableFilter = (view: z.infer<typeof views>,filters: z.infer<typeof FilterArray>,diff --git a/web/src/features/events/hooks/useMetadataValueOptions.clienttest.ts b/web/src/features/events/hooks/useMetadataValueOptions.clienttest.tsnew file mode 100644index 0000000000..a9cfda1ebe--- /dev/null+++ b/web/src/features/events/hooks/useMetadataValueOptions.clienttest.ts@@ -0,0 +1,55 @@+import { describe, expect, it } from "vitest";+import { type FilterState } from "@langfuse/shared";++import { __test } from "./useMetadataValueOptions";++const { zipMetadataValueOptions, metadataKeysInUse } = __test;++describe("zipMetadataValueOptions", () => {+ it("maps every in-use key to its own value options, not just the last", () => {+ const map = zipMetadataValueOptions(+ ["region", "tier"],+ [+ { data: [{ value: "us" }, { value: "eu" }] },+ { data: [{ value: "gold" }] },+ ],+ );+ expect(Object.keys(map)).toEqual(["region", "tier"]);+ expect(map.region?.map((o) => o.value)).toEqual(["us", "eu"]);+ expect(map.tier?.map((o) => o.value)).toEqual(["gold"]);+ });++ it("skips keys whose query has not resolved", () => {+ const map = zipMetadataValueOptions(+ ["region", "tier"],+ [{ data: undefined }, { data: [{ value: "gold" }] }],+ );+ expect(map).not.toHaveProperty("region");+ expect(map.tier?.map((o) => o.value)).toEqual(["gold"]);+ });+});++describe("metadataKeysInUse", () => {+ it("unions committed stringObject keys with keys edited this session, deduped", () => {+ const filterState = [+ {+ type: "stringObject",+ column: "metadata",+ operator: "=",+ key: "region",+ value: "us",+ },+ { type: "string", column: "name", operator: "=", value: "x" },+ ] as unknown as FilterState;+ expect(metadataKeysInUse(filterState, ["tier", "region"]).sort()).toEqual([+ "region",+ "tier",+ ]);+ });++ it("includes an edited key that is not yet a committed filter row", () => {+ expect(metadataKeysInUse([] as FilterState, ["region"])).toEqual([+ "region",+ ]);+ });+});diff --git a/web/src/features/events/hooks/useMetadataValueOptions.ts b/web/src/features/events/hooks/useMetadataValueOptions.tsnew file mode 100644index 0000000000..f5c127b610--- /dev/null+++ b/web/src/features/events/hooks/useMetadataValueOptions.ts@@ -0,0 +1,101 @@+import { useCallback, useMemo, useState } from "react";+import {+ type FilterState,+ type SingleValueOption,+ type TimeFilter,+} from "@langfuse/shared";++import { api, type RouterOutputs } from "@/src/utils/api";+import { normalizeSingleValueOptions } from "@/src/features/filters/lib/filter-transform";++/** metadataValueQueryOptions is the shared react-query config for each per-key metadata-value query. */+const metadataValueQueryOptions = {+ trpc: { context: { skipBatch: true } },+ staleTime: 60 * 1000,+ refetchOnWindowFocus: false,+ refetchOnReconnect: false,+} as const;++/** useMetadataValueOptions fetches observed values for every in-use metadata key into a keyβoptions map. */+export const useMetadataValueOptions = ({+ projectId,+ filterState,+ startTimeFilter,+ enabled = true,+}: {+ projectId: string;+ filterState: FilterState;+ startTimeFilter?: TimeFilter[];+ enabled?: boolean;+}): {+ metadataValueOptions: Record<string, SingleValueOption[]>;+ onMetadataKeyChange: (key: string) => void;+} => {+ // A metadata row is not a committed filter until it also has a value, so its+ // key is absent from filterState while the user is still picking that value.+ // Track every key touched this session so each row keeps its suggestions.+ const [editedKeys, setEditedKeys] = useState<readonly string[]>([]);++ const onMetadataKeyChange = useCallback((key: string) => {+ setEditedKeys((prev) =>+ key && !prev.includes(key) ? [...prev, key] : prev,+ );+ }, []);++ const keys = useMemo(+ () => metadataKeysInUse(filterState, editedKeys),+ [filterState, editedKeys],+ );++ const combine = useCallback(+ (results: readonly MetadataValueResult[]) =>+ zipMetadataValueOptions(keys, results),+ [keys],+ );++ const metadataValueOptions = api.useQueries(+ (t) =>+ keys.map((key) =>+ t.events.metadataValues(+ { projectId, key, startTimeFilter },+ { ...metadataValueQueryOptions, enabled: enabled && Boolean(key) },+ ),+ ),+ { combine },+ );++ return { metadataValueOptions, onMetadataKeyChange };+};++/** metadataKeysInUse unions committed stringObject filter keys with keys edited this session. */+const metadataKeysInUse = (+ filterState: FilterState,+ editedKeys: readonly string[],+): string[] => {+ const keys = new Set<string>(editedKeys);+ for (const filter of filterState) {+ if (filter.type === "stringObject" && filter.key) keys.add(filter.key);+ }+ return [...keys];+};++/** zipMetadataValueOptions maps each key to its query result's normalized options, skipping unresolved keys. */+const zipMetadataValueOptions = (+ keys: string[],+ results: readonly MetadataValueResult[],+): Record<string, SingleValueOption[]> => {+ const map: Record<string, SingleValueOption[]> = {};+ results.forEach((result, i) => {+ const key = keys[i];+ if (key === undefined || !result.data) return;+ map[key] = normalizeSingleValueOptions(result.data);+ });+ return map;+};++/** MetadataValueResult is the per-key query slice zipMetadataValueOptions reads. */+type MetadataValueResult = {+ data: RouterOutputs["events"]["metadataValues"] | undefined;+};++export const __test = { zipMetadataValueOptions, metadataKeysInUse };diff --git a/web/src/features/events/server/eventsRouter.ts b/web/src/features/events/server/eventsRouter.tsindex a3d4039016..e643cda8f7 100644--- a/web/src/features/events/server/eventsRouter.ts+++ b/web/src/features/events/server/eventsRouter.ts@@ -22,6 +22,7 @@ import {getEventList,getEventCount,getEventFilterOptions,+ getEventMetadataValues,getEventBatchIO,EVENT_FILTER_OPTIONS_COLUMNS,} from "./eventsService";@@ -74,6 +75,12 @@ export type GetEventFilterOptionsInput = z.infer<typeof GetEventFilterOptionsInput>;+const GetEventMetadataValuesInput = zodSchema.object({+ projectId: zodSchema.string(),+ key: zodSchema.string().min(1),+ startTimeFilter: zodSchema.array(timeFilter).optional(),+});+export const BatchIOInput = zodSchema.object({projectId: zodSchema.string(),observations: zodSchema@@ -197,6 +204,21 @@ export const eventsRouter = createTRPCRouter({},);}),+ metadataValues: protectedProjectProcedure+ .input(GetEventMetadataValuesInput)+ .query(async ({ input }) => {+ return instrumentAsync(+ { name: "get-event-metadata-values-trpc" },+ async (span) => {+ span.setAttribute("project_id", input.projectId);+ return getEventMetadataValues({+ projectId: input.projectId,+ key: input.key,+ startTimeFilter: input.startTimeFilter,+ });+ },+ );+ }),batchIO: protectedGetEventsTraceProcedure.input(BatchIOInput).query(async ({ input }) => {diff --git a/web/src/features/events/server/eventsService.ts b/web/src/features/events/server/eventsService.tsindex b580b5fc61..329f3aeac3 100644--- a/web/src/features/events/server/eventsService.ts+++ b/web/src/features/events/server/eventsService.ts@@ -11,6 +11,7 @@ import {getCategoricalScoresGroupedByName,getEventsFilterOptionsForColumns,getEventsFilterOptionValuesPage,+ getEventsMetadataValues,getEventsNumericStatsByFilterColumn,getNumericScoresGroupedByName,getBooleanScoresGroupedByName,@@ -121,6 +122,7 @@ interface GetEventFilterOptionsParams extends GetObservationsFilterOptionsParamstype EventFilterValueOption = {value: string;count?: number;+ displayValue?: string;};// Subset of event filter option columns returned by the bulk filter-options response.@@ -148,6 +150,7 @@ const EVENT_FILTER_OPTION_COLUMNS = ["isRootObservation","toolNames","calledToolNames",+ "metadataKeys",] as const satisfies readonly EventFilterOptionColumn[];const EVENT_SCORE_FILTER_OPTION_COLUMNS = [@@ -406,7 +409,13 @@ const toFilterValueOptions = (): EventFilterValueOption[] =>items.filter((item) => item.column === column)- .map((item) => ({ value: item.value, count: item.count }));+ .map((item) => ({+ value: item.value,+ count: item.count,+ ...(item.displayValue && item.displayValue.length > 0+ ? { displayValue: item.displayValue }+ : {}),+ }));const EVENT_FILTER_VALUE_ONLY_COLUMNS = new Set<EventFilterOptionColumn>(["traceTags",@@ -837,6 +846,23 @@ export async function getEventFilterOptions(};}+/** getEventMetadataValues returns the most common values observed for one metadata key. */+export async function getEventMetadataValues(+ params: GetObservationsFilterOptionsParams & { key: string },+): Promise<EventFilterValueOption[]> {+ const scopedParams = ensureStartTimeFilterForEventFilterOptions(params);+ const { projectId, key } = scopedParams;+ const { eventsFilter } = getEventFilterOptionsScope(scopedParams);++ const rows = await getEventsMetadataValues({+ projectId,+ filter: eventsFilter,+ key,+ });++ return rows.map((row) => ({ value: row.value, count: row.count }));+}+interface GetEventBatchIOParams<TIncludeExperiment extends boolean = false,TIncludeToolCalls extends boolean = false,diff --git a/web/src/features/filters/components/filter-builder.tsx b/web/src/features/filters/components/filter-builder.tsxindex f008f438ef..257ac32c7a 100644--- a/web/src/features/filters/components/filter-builder.tsx+++ b/web/src/features/filters/components/filter-builder.tsx@@ -40,6 +40,7 @@ import {TooltipTrigger,} from "@/src/components/ui/tooltip";import { MultiSelect } from "@/src/features/filters/components/multi-select";+import { SingleSelect } from "@/src/features/filters/components/single-select";import { FilterToken } from "@/src/features/filters/components/FilterToken";import {type WipFilterState,@@ -47,6 +48,7 @@ import {type FilterState,type FilterCondition,type ColumnDefinition,+ type SingleValueOption,filterOperators,singleFilter,NonEmptyString,@@ -443,6 +445,9 @@ export function InlineFilterBuilder({columnIdentifier = "name",disabled,columnsWithCustomSelect,+ stringObjectValueOptions,+ onStringObjectKeyChange,+ compact = false,}: {columns: ColumnDefinitionWithAlert[];filterState: FilterState;@@ -453,6 +458,12 @@ export function InlineFilterBuilder({columnIdentifier?: ColumnIdentifier;disabled?: boolean;columnsWithCustomSelect?: string[];+ /** Per-key value suggestions for opt-in stringObject columns (metadata). */+ stringObjectValueOptions?: Record<string, SingleValueOption[]>;+ /** Fires when a suggestion-enabled stringObject key changes, so the parent can load its values. */+ onStringObjectKeyChange?: (key: string) => void;+ /** compact renders the width-constrained two-line layout instead of the legacy single-row table. */+ compact?: boolean;}) {const [wipFilterState, _setWipFilterState] =useState<WipFilterState>(filterState);@@ -498,6 +509,9 @@ export function InlineFilterBuilder({onChange={setWipFilterState}disabled={disabled}columnsWithCustomSelect={columnsWithCustomSelect}+ stringObjectValueOptions={stringObjectValueOptions}+ onStringObjectKeyChange={onStringObjectKeyChange}+ compact={compact}/></div>);@@ -540,6 +554,9 @@ function FilterBuilderForm({onChange,disabled,columnsWithCustomSelect = [],+ stringObjectValueOptions = {},+ onStringObjectKeyChange,+ compact = false,aiFilter,}: {columnIdentifier: ColumnIdentifier;@@ -548,11 +565,18 @@ function FilterBuilderForm({onChange: Dispatch<SetStateAction<WipFilterState>>;disabled?: boolean;columnsWithCustomSelect?: string[];+ /** Per-key value suggestions for opt-in stringObject columns (metadata). */+ stringObjectValueOptions?: Record<string, SingleValueOption[]>;+ /** Fires when a suggestion-enabled stringObject key changes, so the parent can load its values. */+ onStringObjectKeyChange?: (key: string) => void;+ /** compact renders each condition as a wrapping block for width-constrained inline builders instead of the wide table. */+ compact?: boolean;aiFilter?: AiFilterConfig;}) {const [showAiFilter, setShowAiFilter] = useState(false);const [aiPrompt, setAiPrompt] = useState("");const [aiError, setAiError] = useState<string | null>(null);+ const [openColumnRow, setOpenColumnRow] = useState<number | null>(null);const handleFilterChange = (filter: WipFilterCondition, i: number) => {onChange((prev) => {const newState = [...prev];@@ -611,6 +635,482 @@ function FilterBuilderForm({}};+ const conditionRows = filterState.map((filter, i) => {+ const column = columns.find(+ (c) =>+ c.id === filter.column ||+ c.name === filter.column ||+ (filter.column !== undefined && c.aliases?.includes(filter.column)),+ );+ const keyOptions =+ column?.type === "numberObject" ||+ column?.type === "stringObject" ||+ column?.type === "booleanObject"+ ? column.keyOptions?.filter((o) => NonEmptyString.safeParse(o).success)+ : undefined;+ // Opt-in metadata suggestions: a stringObject column listed in+ // columnsWithCustomSelect renders searchable key/value combos.+ const stringObjectSuggest =+ column?.type === "stringObject" &&+ columnsWithCustomSelect.includes(column.id);+ const columnLabel = column ? column.name : "Column";++ const columnCombobox = (+ <Popover+ open={openColumnRow === i}+ onOpenChange={(open) => setOpenColumnRow(open ? i : null)}+ >+ <PopoverTrigger asChild>+ <Button+ variant="outline"+ role="combobox"+ type="button"+ disabled={disabled}+ className={cn(+ "flex items-center justify-between gap-2",+ compact ? "w-full min-w-0" : "w-full min-w-32",+ )}+ >+ <span className="min-w-0 truncate" title={columnLabel}>+ {columnLabel}+ </span>+ <ChevronDown className="h-4 w-4 shrink-0 opacity-50" />+ </Button>+ </PopoverTrigger>+ <PopoverContent+ align="start"+ className="max-w-fit p-0"+ onWheel={(e) => {+ e.stopPropagation();+ }}+ onTouchMove={(e) => {+ e.stopPropagation();+ }}+ >+ <InputCommand>+ <InputCommandInput+ placeholder="Search for column"+ variant="bottom"+ />+ <InputCommandList>+ <InputCommandEmpty>No options found.</InputCommandEmpty>+ <InputCommandGroup>+ {columns.map((option) => {+ const hasAlert = !!option.alert;+ const severity = option.alert?.severity ?? "warning";+ const alertStyles = getAlertStyles(severity);++ return (+ <InputCommandItem+ key={option.id}+ value={option.id}+ onSelect={(value) => {+ const col = columns.find((c) => c.id === value);+ const defaultOperator = col?.type+ ? getOperator(col.type)+ : undefined;++ handleFilterChange(+ {+ column: col?.[columnIdentifier],+ type: col?.type,+ operator: defaultOperator,+ value:+ col?.type === "null"+ ? ""+ : col?.type === "boolean" ||+ col?.type === "booleanObject"+ ? true+ : undefined,+ key:+ col?.type === "positionInTrace"+ ? "last"+ : undefined,+ } as WipFilterCondition,+ i,+ );+ setOpenColumnRow(null);+ }}+ >+ <Check+ className={cn(+ "mr-2 h-4 w-4",+ option.id === column?.id ? "visible" : "invisible",+ )}+ />+ <span className="flex-1">{option.name}</span>+ {hasAlert && (+ <Tooltip>+ <TooltipTrigger asChild>+ <Info+ className={cn(+ "ml-2 h-4 w-4",+ alertStyles.iconColor,+ )}+ />+ </TooltipTrigger>+ <TooltipContent+ className={cn("max-w-xs", alertStyles.tooltipBg)}+ >+ {option.alert?.content}+ </TooltipContent>+ </Tooltip>+ )}+ </InputCommandItem>+ );+ })}+ </InputCommandGroup>+ </InputCommandList>+ </InputCommand>+ </PopoverContent>+ </Popover>+ );++ const keyControl =+ filter.type &&+ (filter.type === "numberObject" ||+ filter.type === "stringObject" ||+ filter.type === "booleanObject") &&+ (column?.type === "numberObject" ||+ column?.type === "stringObject" ||+ column?.type === "booleanObject") ? (+ stringObjectSuggest ? (+ // Case 0: opt-in stringObject - single key select with free text+ <SingleSelect+ title="Key"+ className="min-w-[100px]"+ options={+ column?.type === "stringObject" && column.keyOptions+ ? column.keyOptions.map((value) => ({ value }))+ : []+ }+ value={filter.key ?? undefined}+ onValueChange={(nextKey) => {+ handleFilterChange({ ...filter, key: nextKey }, i);+ onStringObjectKeyChange?.(nextKey);+ }}+ disabled={disabled}+ isCustomSelectEnabled+ />+ ) : keyOptions?.length ? (+ // Case 1: object with keyOptions - selector of the key of the object+ <Select+ disabled={!filter.column}+ onValueChange={(value) => {+ handleFilterChange({ ...filter, key: value }, i);+ }}+ value={filter.key ?? ""}+ >+ <SelectTrigger className="min-w-[60px]">+ <SelectValue placeholder="" />+ </SelectTrigger>+ <SelectContent>+ {keyOptions.map((option) => (+ <SelectItem key={option} value={option}>+ {option}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ ) : (+ // Case 2: object without keyOptions - text input+ <Input+ value={filter.key ?? ""}+ placeholder="key"+ disabled={disabled}+ onChange={(e) =>+ handleFilterChange({ ...filter, key: e.target.value }, i)+ }+ />+ )+ ) : filter.type === "categoryOptions" &&+ column?.type === "categoryOptions" ? (+ // Case 3: categoryOptions+ <Select+ onValueChange={(value) => {+ handleFilterChange({ ...filter, key: value }, i);+ }}+ value={filter.key ?? ""}+ >+ <SelectTrigger className="min-w-[60px]">+ <SelectValue placeholder="" />+ </SelectTrigger>+ <SelectContent>+ {column?.options.map((option) => (+ <SelectItem key={option.label} value={option.label}>+ {option.label}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ ) : filter.type === "positionInTrace" ? (+ <Select+ onValueChange={(value) => {+ const needsValue =+ value === "nthFromEnd" || value === "nthFromStart";+ handleFilterChange(+ {+ ...filter,+ key: value,+ value: needsValue+ ? typeof filter.value === "number" && filter.value >= 1+ ? filter.value+ : 1+ : undefined,+ } as WipFilterCondition,+ i,+ );+ }}+ value={getSessionPositionInTraceFilterMode(filter)}+ >+ <SelectTrigger className="min-w-[140px]">+ <SelectValue placeholder="" />+ </SelectTrigger>+ <SelectContent>+ <SelectItem value="first">1st</SelectItem>+ <SelectItem value="last">last</SelectItem>+ <SelectItem value="nthFromStart">nth from start</SelectItem>+ <SelectItem value="nthFromEnd">nth from end</SelectItem>+ </SelectContent>+ </Select>+ ) : null;++ // Object/category columns need a key before the operator/value mean anything;+ // in compact rows keep those controls inert until the key is chosen.+ const keyPending =+ compact && keyControl !== null && !("key" in filter && filter.key);++ const operatorSelect = (+ <Select+ disabled={!filter.column || disabled || keyPending}+ onValueChange={(value) => {+ // protect against invalid empty operator values+ if (value === "") return;+ handleFilterChange(+ {+ ...filter,+ operator: value as any,+ // Ensure null filters always have empty string value+ value: filter.type === "null" ? "" : (filter.value as any),+ },+ i,+ );+ }}+ value={keyPending ? "" : (filter.operator ?? "")}+ >+ <SelectTrigger className={cn("min-w-[60px]", compact && "w-auto")}>+ <SelectValue placeholder="" />+ </SelectTrigger>+ <SelectContent>+ {filter.type !== undefined+ ? filterOperators[filter.type].map((option) => (+ <SelectItem key={option} value={option}>+ {option}+ </SelectItem>+ ))+ : null}+ </SelectContent>+ </Select>+ );++ const valueControl = keyPending ? (+ <Input disabled />+ ) : stringObjectSuggest && filter.type === "stringObject" ? (+ <SingleSelect+ title="Value"+ className="min-w-[100px]"+ options={(filter.key ? stringObjectValueOptions[filter.key] : []) ?? []}+ value={filter.value ?? undefined}+ onValueChange={(value) => handleFilterChange({ ...filter, value }, i)}+ disabled={disabled}+ isCustomSelectEnabled+ />+ ) : filter.type === "string" || filter.type === "stringObject" ? (+ <Input+ disabled={disabled}+ value={filter.value ?? ""}+ placeholder="string"+ onChange={(e) =>+ handleFilterChange({ ...filter, value: e.target.value }, i)+ }+ />+ ) : filter.type === "number" || filter.type === "numberObject" ? (+ <Input+ value={filter.value ?? undefined}+ disabled={disabled}+ type="number"+ step={(column?.type === "number" && column.step) || 0.01}+ min={column?.type === "number" ? column.min : undefined}+ placeholder="number"+ lang="en-US"+ onChange={(e) =>+ handleFilterChange(+ {+ ...filter,+ value: isNaN(Number(e.target.value))+ ? e.target.value+ : Number(e.target.value),+ },+ i,+ )+ }+ />+ ) : filter.type === "datetime" ? (+ <DatePicker+ className="w-full"+ disabled={disabled}+ date={filter.value ? new Date(filter.value) : undefined}+ onChange={(date) => {+ handleFilterChange(+ {+ ...filter,+ value: date,+ },+ i,+ );+ }}+ includeTimePicker+ />+ ) : filter.type === "stringOptions" || filter.type === "arrayOptions" ? (+ <MultiSelect+ title="Value"+ chipsOnly={compact}+ className="min-w-[100px]"+ options={column?.type === filter.type ? column.options : []}+ onValueChange={(value) => handleFilterChange({ ...filter, value }, i)}+ values={Array.isArray(filter.value) ? filter.value : []}+ disabled={disabled}+ isCustomSelectEnabled={+ column?.type === filter.type &&+ columnsWithCustomSelect.includes(column.id)+ }+ />+ ) : filter.type === "categoryOptions" &&+ column?.type === "categoryOptions" ? (+ <MultiSelect+ title="Value"+ chipsOnly={compact}+ className="min-w-[100px]"+ options={+ column?.options+ .find((o) => o.label === filter.key)+ ?.values?.map((v) => ({ value: v })) ?? []+ }+ onValueChange={(value) => handleFilterChange({ ...filter, value }, i)}+ values={Array.isArray(filter.value) ? filter.value : []}+ disabled={disabled}+ isCustomSelectEnabled={+ column?.type === filter.type &&+ columnsWithCustomSelect.includes(column.id)+ }+ />+ ) : filter.type === "boolean" || filter.type === "booleanObject" ? (+ <Select+ disabled={disabled}+ onValueChange={(value) => {+ handleFilterChange(+ {+ ...filter,+ value: value !== "" ? value === "true" : undefined,+ },+ i,+ );+ }}+ value={filter.value?.toString() ?? ""}+ >+ <SelectTrigger className="min-w-[60px]">+ <SelectValue placeholder="" />+ </SelectTrigger>+ <SelectContent>+ {["true", "false"].map((option) => (+ <SelectItem key={option} value={option}>+ {option}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ ) : filter.type === "positionInTrace" ? (+ filter.key === "nthFromStart" || filter.key === "nthFromEnd" ? (+ <Input+ value={filter.value ?? ""}+ disabled={disabled}+ type="number"+ min={1}+ step={1}+ onChange={(e) =>+ handleFilterChange(+ {+ ...filter,+ value: isNaN(Number(e.target.value))+ ? undefined+ : Math.max(1, Number(e.target.value)),+ } as WipFilterCondition,+ i,+ )+ }+ />+ ) : (+ <Input disabled placeholder="-" />+ )+ ) : filter.type === "null" ? null : (+ <Input disabled />+ );++ const removeButton = (+ <Button+ onClick={() => removeFilter(i)}+ variant="ghost"+ type="button"+ disabled={disabled}+ size="xs"+ >+ <X className="h-4 w-4" />+ </Button>+ );++ const connector = i === 0 ? "Where" : "And";++ return compact ? (+ <div key={i} className="-mr-2 flex flex-col">+ <div className="flex items-center gap-1.5">+ <span className="text-muted-foreground w-11 shrink-0 text-sm">+ {connector}+ </span>+ <div className={cn("min-w-0", keyControl ? "w-28" : "flex-1")}>+ {columnCombobox}+ </div>+ {keyControl ? (+ <div className="min-w-0 flex-1">{keyControl}</div>+ ) : null}+ <div className="flex w-5 shrink-0 justify-end">{removeButton}</div>+ </div>+ <div className="flex items-start">+ <span className="w-16 shrink-0" aria-hidden="true" />+ <div className="border-input h-6 w-3 shrink-0 rounded-bl-md border-b-2 border-l-2" />+ <div className="flex min-w-0 flex-1 items-center gap-1.5 pt-2">+ <div className="shrink-0">{operatorSelect}</div>+ {valueControl ? (+ <div className="min-w-0 flex-1">{valueControl}</div>+ ) : null}+ <span className="w-5 shrink-0" aria-hidden="true" />+ </div>+ </div>+ </div>+ ) : (+ <tr key={i}>+ <td className="p-1 text-sm">{connector}</td>+ <td className="flex gap-2 p-1">+ {/* selector of the column to be filtered */}+ {columnCombobox}+ {keyControl}+ </td>+ <td className="p-1">{operatorSelect}</td>+ <td className="p-1">{valueControl}</td>+ <td>{removeButton}</td>+ </tr>+ );+ });+return (<>{/* AI Filter Section at the top */}@@ -698,452 +1198,18 @@ function FilterBuilderForm({{/* Hide filter builder UI while AI filter is open */}{!showAiFilter && (<>- <table className="table-auto">- <tbody>- {filterState.map((filter, i) => {- const column = columns.find(- (c) =>- c.id === filter.column ||- c.name === filter.column ||- (filter.column !== undefined &&- c.aliases?.includes(filter.column)),- );- const keyOptions =- column?.type === "numberObject" ||- column?.type === "stringObject" ||- column?.type === "booleanObject"- ? column.keyOptions?.filter(- (o) => NonEmptyString.safeParse(o).success,- )- : undefined;- const columnLabel = column ? column.name : "Column";- return (- <tr key={i}>- <td className="p-1 text-sm">{i === 0 ? "Where" : "And"}</td>- <td className="flex gap-2 p-1">- {/* selector of the column to be filtered */}- <Popover>- <PopoverTrigger asChild>- <Button- variant="outline"- role="combobox"- type="button"- disabled={disabled}- className="flex w-full min-w-32 items-center justify-between gap-2"- >- <span className="truncate" title={columnLabel}>- {columnLabel}- </span>- <ChevronDown className="h-4 w-4 shrink-0 opacity-50" />- </Button>- </PopoverTrigger>- <PopoverContent- className="max-w-fit p-0"- onWheel={(e) => {- e.stopPropagation();- }}- onTouchMove={(e) => {- e.stopPropagation();- }}- >- <InputCommand>- <InputCommandInput- placeholder="Search for column"- variant="bottom"- />- <InputCommandList>- <InputCommandEmpty>- No options found.- </InputCommandEmpty>- <InputCommandGroup>- {columns.map((option) => {- const hasAlert = !!option.alert;- const severity =- option.alert?.severity ?? "warning";- const alertStyles = getAlertStyles(severity);-- return (- <InputCommandItem- key={option.id}- value={option.id}- onSelect={(value) => {- const col = columns.find(- (c) => c.id === value,- );- const defaultOperator = col?.type- ? getOperator(col.type)- : undefined;-- handleFilterChange(- {- column: col?.[columnIdentifier],- type: col?.type,- operator: defaultOperator,- value:- col?.type === "null"- ? ""- : undefined,- key:- col?.type === "positionInTrace"- ? "last"- : undefined,- } as WipFilterCondition,- i,- );- }}- >- <Check- className={cn(- "mr-2 h-4 w-4",- option.id === column?.id- ? "visible"- : "invisible",- )}- />- <span className="flex-1">- {option.name}- </span>- {hasAlert && (- <Tooltip>- <TooltipTrigger asChild>- <Info- className={cn(- "ml-2 h-4 w-4",- alertStyles.iconColor,- )}- />- </TooltipTrigger>- <TooltipContent- className={cn(- "max-w-xs",- alertStyles.tooltipBg,- )}- >- {option.alert?.content}- </TooltipContent>- </Tooltip>- )}- </InputCommandItem>- );- })}- </InputCommandGroup>- </InputCommandList>- </InputCommand>- </PopoverContent>- </Popover>- {filter.type &&- (filter.type === "numberObject" ||- filter.type === "stringObject" ||- filter.type === "booleanObject") &&- (column?.type === "numberObject" ||- column?.type === "stringObject" ||- column?.type === "booleanObject") ? (- keyOptions?.length ? (- // Case 1: object with keyOptions - selector of the key of the object- <Select- disabled={!filter.column}- onValueChange={(value) => {- handleFilterChange({ ...filter, key: value }, i);- }}- value={filter.key ?? ""}- >- <SelectTrigger className="min-w-[60px]">- <SelectValue placeholder="" />- </SelectTrigger>- <SelectContent>- {keyOptions.map((option) => (- <SelectItem key={option} value={option}>- {option}- </SelectItem>- ))}- </SelectContent>- </Select>- ) : (- // Case 2: object without keyOptions - text input- <Input- value={filter.key ?? ""}- placeholder="key"- disabled={disabled}- onChange={(e) =>- handleFilterChange(- { ...filter, key: e.target.value },- i,- )- }- />- )- ) : filter.type === "categoryOptions" &&- column?.type === "categoryOptions" ? (- // Case 3: categoryOptions- <Select- onValueChange={(value) => {- handleFilterChange({ ...filter, key: value }, i);- }}- value={filter.key ?? ""}- >- <SelectTrigger className="min-w-[60px]">- <SelectValue placeholder="" />- </SelectTrigger>- <SelectContent>- {column?.options.map((option) => (- <SelectItem- key={option.label}- value={option.label}- >- {option.label}- </SelectItem>- ))}- </SelectContent>- </Select>- ) : filter.type === "positionInTrace" ? (- <Select- onValueChange={(value) => {- const needsValue =- value === "nthFromEnd" ||- value === "nthFromStart";- handleFilterChange(- {- ...filter,- key: value,- value: needsValue- ? typeof filter.value === "number" &&- filter.value >= 1- ? filter.value- : 1- : undefined,- } as WipFilterCondition,- i,- );- }}- value={getSessionPositionInTraceFilterMode(filter)}- >- <SelectTrigger className="min-w-[140px]">- <SelectValue placeholder="" />- </SelectTrigger>- <SelectContent>- <SelectItem value="first">1st</SelectItem>- <SelectItem value="last">last</SelectItem>- <SelectItem value="nthFromStart">- nth from start- </SelectItem>- <SelectItem value="nthFromEnd">- nth from end- </SelectItem>- </SelectContent>- </Select>- ) : null}- </td>- <td className="p-1">- <Select- disabled={!filter.column || disabled}- onValueChange={(value) => {- // protect against invalid empty operator values- if (value === "") return;- handleFilterChange(- {- ...filter,- operator: value as any,- // Ensure null filters always have empty string value- value:- filter.type === "null"- ? ""- : (filter.value as any),- },- i,- );- }}- value={filter.operator ?? ""}- >- <SelectTrigger className="min-w-[60px]">- <SelectValue placeholder="" />- </SelectTrigger>- <SelectContent>- {filter.type !== undefined- ? filterOperators[filter.type].map((option) => (- <SelectItem key={option} value={option}>- {option}- </SelectItem>- ))- : null}- </SelectContent>- </Select>- </td>- <td className="p-1">- {filter.type === "string" ||- filter.type === "stringObject" ? (- <Input- disabled={disabled}- value={filter.value ?? ""}- placeholder="string"- onChange={(e) =>- handleFilterChange(- { ...filter, value: e.target.value },- i,- )- }- />- ) : filter.type === "number" ||- filter.type === "numberObject" ? (- <Input- value={filter.value ?? undefined}- disabled={disabled}- type="number"- step={- (column?.type === "number" && column.step) || 0.01- }- min={- column?.type === "number" ? column.min : undefined- }- placeholder="number"- lang="en-US"- onChange={(e) =>- handleFilterChange(- {- ...filter,- value: isNaN(Number(e.target.value))- ? e.target.value- : Number(e.target.value),- },- i,- )- }- />- ) : filter.type === "datetime" ? (- <DatePicker- className="w-full"- disabled={disabled}- date={- filter.value ? new Date(filter.value) : undefined- }- onChange={(date) => {- handleFilterChange(- {- ...filter,- value: date,- },- i,- );- }}- includeTimePicker- />- ) : filter.type === "stringOptions" ||- filter.type === "arrayOptions" ? (- <MultiSelect- title="Value"- className="min-w-[100px]"- options={- column?.type === filter.type ? column.options : []- }- onValueChange={(value) =>- handleFilterChange({ ...filter, value }, i)- }- values={- Array.isArray(filter.value) ? filter.value : []- }- disabled={disabled}- isCustomSelectEnabled={- column?.type === filter.type &&- columnsWithCustomSelect.includes(column.id)- }- />- ) : filter.type === "categoryOptions" &&- column?.type === "categoryOptions" ? (- <MultiSelect- title="Value"- className="min-w-[100px]"- options={- column?.options- .find((o) => o.label === filter.key)- ?.values?.map((v) => ({ value: v })) ?? []- }- onValueChange={(value) =>- handleFilterChange({ ...filter, value }, i)- }- values={- Array.isArray(filter.value) ? filter.value : []- }- disabled={disabled}- isCustomSelectEnabled={- column?.type === filter.type &&- columnsWithCustomSelect.includes(column.id)- }- />- ) : filter.type === "boolean" ||- filter.type === "booleanObject" ? (- <Select- disabled={disabled}- onValueChange={(value) => {- handleFilterChange(- {- ...filter,- value:- value !== "" ? value === "true" : undefined,- },- i,- );- }}- value={filter.value?.toString() ?? ""}- >- <SelectTrigger className="min-w-[60px]">- <SelectValue placeholder="" />- </SelectTrigger>- <SelectContent>- {["true", "false"].map((option) => (- <SelectItem key={option} value={option}>- {option}- </SelectItem>- ))}- </SelectContent>- </Select>- ) : filter.type === "positionInTrace" ? (- filter.key === "nthFromStart" ||- filter.key === "nthFromEnd" ? (- <Input- value={filter.value ?? ""}- disabled={disabled}- type="number"- min={1}- step={1}- onChange={(e) =>- handleFilterChange(- {- ...filter,- value: isNaN(Number(e.target.value))- ? undefined- : Math.max(1, Number(e.target.value)),- } as WipFilterCondition,- i,- )- }- />- ) : (- <Input disabled placeholder="-" />- )- ) : filter.type === "null" ? null : (- <Input disabled />- )}- </td>- <td>- <Button- onClick={() => removeFilter(i)}- variant="ghost"- type="button"- disabled={disabled}- size="xs"- >- <X className="h-4 w-4" />- </Button>- </td>- </tr>- );- })}- </tbody>- </table>+ {compact ? (+ <div className="flex flex-col gap-4">{conditionRows}</div>+ ) : (+ <table className="table-auto">+ <tbody>{conditionRows}</tbody>+ </table>+ )}{!disabled ? (<ButtononClick={() => addNewFilter()}type="button" // required as it will otherwise submit forms where this component is used- className="mt-2"+ className={cn("mt-2", compact && "mt-4 self-start")}variant="outline"size="sm">diff --git a/web/src/features/filters/components/multi-select.tsx b/web/src/features/filters/components/multi-select.tsxindex fdeeb99572..a4be749197 100644--- a/web/src/features/filters/components/multi-select.tsx+++ b/web/src/features/filters/components/multi-select.tsx@@ -25,6 +25,9 @@ import { Input } from "@/src/components/ui/input";import { useRef, useState, useMemo, useCallback } from "react";import { PropertyHoverCard } from "@/src/features/widgets/components/WidgetPropertySelectItem";+/** compactSelectAllLimit is the most options a compact select shows Select All for. */+const compactSelectAllLimit = 100;+const getFreeTextInput = (isCustomSelectEnabled: boolean,values: string[],@@ -44,6 +47,7 @@ export function MultiSelect({disabled,isCustomSelectEnabled = false,labelTruncateCutOff = 2,+ chipsOnly = false,}: {title?: string;label?: string;@@ -54,6 +58,8 @@ export function MultiSelect({disabled?: boolean;isCustomSelectEnabled?: boolean;labelTruncateCutOff?: number;+ /** chipsOnly hides the placeholder/separator once values are selected, showing just the chips and chevron. */+ chipsOnly?: boolean;}) {const selectedValues = useMemo(() => new Set(values), [values]);const optionValues = new Set(options.map((option) => option.value));@@ -79,6 +85,10 @@ export function MultiSelect({[mergedOptions],);+ const showSelectAll =+ selectableOptions.length > 0 &&+ !(chipsOnly && selectableOptions.length > compactSelectAllLimit);+const allSelectedState = useMemo(() => {if (selectableOptions.length === 0) return false;return selectableOptions.every((option) =>@@ -135,6 +145,33 @@ export function MultiSelect({return [...selectedOptions, ...customOption];}+ const selectedBadges =+ selectedValues.size > labelTruncateCutOff ? (+ <Badge variant="secondary" className="rounded-sm px-1 font-normal">+ {selectedValues.size} selected+ </Badge>+ ) : (+ getSelectedOptions().map((option) => {+ const displayValue =+ option.displayValue ??+ (option.value === "" ? "(empty)" : option.value);+ return (+ <Badge+ variant="secondary"+ key={option.value}+ className={cn(+ "min-w-0 rounded-sm px-1 font-normal",+ option.value === "" && "italic",+ )}+ >+ <span className="truncate" title={displayValue}>+ {displayValue}+ </span>+ </Badge>+ );+ })+ );+return (<Popover><PopoverTrigger asChild>@@ -148,47 +185,31 @@ export function MultiSelect({)}disabled={disabled}>- {label ?? "Select"}- <ChevronDown className="h-4 w-4 shrink-0 opacity-50" />- {selectedValues.size > 0 && (+ {chipsOnly && selectedValues.size > 0 ? (+ <>+ <div className="flex min-w-0 flex-1 items-center gap-1 overflow-hidden">+ {selectedBadges}+ </div>+ <ChevronDown className="h-4 w-4 shrink-0 opacity-50" />+ </>+ ) : (<>- <Separator orientation="vertical" className="mr-auto h-4" />- <Badge- variant="secondary"- className="rounded-sm px-1 font-normal lg:hidden"- >- {selectedValues.size}- </Badge>- <div className="hidden min-w-0 space-x-1 overflow-hidden lg:flex">- {selectedValues.size > labelTruncateCutOff ? (+ {label ?? "Select"}+ <ChevronDown className="h-4 w-4 shrink-0 opacity-50" />+ {selectedValues.size > 0 && (+ <>+ <Separator orientation="vertical" className="mr-auto h-4" /><Badgevariant="secondary"- className="rounded-sm px-1 font-normal"+ className="rounded-sm px-1 font-normal lg:hidden">- {selectedValues.size} selected+ {selectedValues.size}</Badge>- ) : (- getSelectedOptions().map((option) => {- const displayValue =- option.displayValue ??- (option.value === "" ? "(empty)" : option.value);- return (- <Badge- variant="secondary"- key={option.value}- className={cn(- "min-w-0 rounded-sm px-1 font-normal",- option.value === "" && "italic",- )}- >- <span className="truncate" title={displayValue}>- {displayValue}- </span>- </Badge>- );- })- )}- </div>+ <div className="hidden min-w-0 space-x-1 overflow-hidden lg:flex">+ {selectedBadges}+ </div>+ </>+ )}</>)}</Button>@@ -202,7 +223,7 @@ export function MultiSelect({<InputCommandEmpty>No results found.</InputCommandEmpty>)}<InputCommandGroup>- {selectableOptions.length > 0 && (+ {showSelectAll && (<><InputCommandItem key="select-all" onSelect={handleSelectAll}><divdiff --git a/web/src/features/filters/components/single-select.clienttest.tsx b/web/src/features/filters/components/single-select.clienttest.tsxnew file mode 100644index 0000000000..ad2e6854d6--- /dev/null+++ b/web/src/features/filters/components/single-select.clienttest.tsx@@ -0,0 +1,57 @@+/**+ * An empty key or value passes `singleFilter.safeParse`, so anything+ * `SingleSelect` commits reaches the applied filter state. Committing `""` would+ * apply a filter that matches nothing.+ */+import { fireEvent, render, screen } from "@testing-library/react";+import { SingleSelect } from "./single-select";++describe("SingleSelect", () => {+ beforeAll(() => {+ vi.stubGlobal(+ "ResizeObserver",+ class {+ observe() {}+ unobserve() {}+ disconnect() {}+ },+ );+ Element.prototype.scrollIntoView = vi.fn();+ });++ const open = () => fireEvent.click(screen.getByRole("button"));++ it("re-clicking the selected option: no commit", () => {+ const onValueChange = vi.fn();+ render(+ <SingleSelect+ title="Key"+ value="environment"+ options={[{ value: "environment" }, { value: "release" }]}+ onValueChange={onValueChange}+ />,+ );++ open();+ fireEvent.click(screen.getByRole("option", { name: "environment" }));++ expect(onValueChange).not.toHaveBeenCalled();+ });++ it("clicking another option: commits that value", () => {+ const onValueChange = vi.fn();+ render(+ <SingleSelect+ title="Key"+ value="environment"+ options={[{ value: "environment" }, { value: "release" }]}+ onValueChange={onValueChange}+ />,+ );++ open();+ fireEvent.click(screen.getByRole("option", { name: "release" }));++ expect(onValueChange).toHaveBeenCalledWith("release");+ });+});diff --git a/web/src/features/filters/components/single-select.tsx b/web/src/features/filters/components/single-select.tsxnew file mode 100644index 0000000000..60c19e36e8--- /dev/null+++ b/web/src/features/filters/components/single-select.tsx@@ -0,0 +1,209 @@+/* eslint-disable @repo/no-style-props, @repo/no-abstracted-overlay-trigger */+import * as React from "react";+import { Check, ChevronDown, Plus } from "lucide-react";+import { useMemo, useState } from "react";++import { Button } from "@/src/components/ui/button";+import {+ InputCommand,+ InputCommandGroup,+ InputCommandInput,+ InputCommandItem,+ InputCommandList,+ InputCommandSeparator,+} from "@/src/components/ui/input-command";+import {+ Popover,+ PopoverContent,+ PopoverTrigger,+} from "@/src/components/ui/popover";+import { cn } from "@/src/utils/tailwind";+import { type FilterOption } from "@langfuse/shared";+import { PropertyHoverCard } from "@/src/features/widgets/components/WidgetPropertySelectItem";++/** SingleSelect picks exactly one option or one custom value from a searchable dropdown. */+export function SingleSelect({+ title,+ value,+ onValueChange,+ options,+ className,+ disabled,+ isCustomSelectEnabled = false,+}: {+ /** title labels the search box and stands in as the empty-state placeholder. */+ title?: string;+ value?: string;+ onValueChange: (value: string) => void;+ options: FilterOption[] | readonly FilterOption[];+ className?: string;+ disabled?: boolean;+ /** isCustomSelectEnabled offers the typed search text as a custom value. */+ isCustomSelectEnabled?: boolean;+}) {+ const [open, setOpen] = useState(false);+ const [search, setSearch] = useState("");++ const selectedOption = useMemo(+ () => options.find((option) => option.value === value),+ [options, value],+ );+ const hasValue = value !== undefined && value !== "";+ const isCustomValue = hasValue && selectedOption === undefined;+ const label = hasValue ? (selectedOption?.displayValue ?? value) : undefined;++ // Hoist the selected option to the top so it reads as current.+ const displayOptions = useMemo<FilterOption[]>(() => {+ const base =+ isCustomValue && value ? [{ value }, ...options] : [...options];+ const index = base.findIndex((option) => option.value === value);+ if (index <= 0) return base;+ const [selected] = base.splice(index, 1);+ return [selected, ...base];+ }, [isCustomValue, value, options]);++ const query = search.trim().toLowerCase();+ const filteredOptions = useMemo(+ () =>+ displayOptions.filter((option) => {+ if (option.value.length === 0) return false;+ if (!query) return true;+ return (option.displayValue ?? option.value)+ .toLowerCase()+ .includes(query);+ }),+ [displayOptions, query],+ );++ // Offer the typed text as a custom value unless it already names an option.+ const showCustomValue =+ isCustomSelectEnabled &&+ query.length > 0 &&+ !displayOptions.some(+ (option) =>+ option.value.toLowerCase() === query ||+ (option.displayValue ?? "").toLowerCase() === query,+ );++ const commit = (next: string) => {+ onValueChange(next);+ setSearch("");+ setOpen(false);+ };++ return (+ <Popover+ open={open}+ onOpenChange={(next) => {+ setOpen(next);+ if (!next) setSearch("");+ }}+ >+ <PopoverTrigger asChild>+ <Button+ variant="outline"+ disabled={disabled}+ className={cn(+ "border-input ring-offset-background placeholder:text-foreground-tertiary focus:ring-ring flex h-8 w-full items-center justify-between gap-x-2 rounded-md border px-3 py-2 text-sm focus:ring-2 focus:ring-offset-2 focus:outline-hidden disabled:cursor-not-allowed disabled:opacity-50",+ className,+ )}+ >+ <span+ className={cn("truncate", !hasValue && "text-muted-foreground")}+ title={label ?? title}+ >+ {label ?? title ?? "Select"}+ </span>+ <ChevronDown className="h-4 w-4 shrink-0 opacity-50" />+ </Button>+ </PopoverTrigger>+ <PopoverContent className="w-[200px] p-0" align="start">+ <InputCommand shouldFilter={false}>+ <InputCommandInput+ placeholder={title}+ variant="bottom"+ value={search}+ onValueChange={setSearch}+ />+ <InputCommandList>+ {filteredOptions.length === 0 && !showCustomValue && (+ <div className="text-muted-foreground py-6 text-center text-sm">+ {isCustomSelectEnabled+ ? "Enter custom value"+ : "No results found."}+ </div>+ )}+ <InputCommandGroup>+ {filteredOptions.map((option) => {+ const isSelected = option.value === value;+ const displayValue = option.displayValue ?? option.value;++ const commandItem = (+ <InputCommandItem+ key={option.value}+ value={option.value}+ // Re-click is inert: an empty key or value is schema-valid,+ // so committing "" would apply an unmatchable filter.+ onSelect={() => {+ if (isSelected) {+ setSearch("");+ setOpen(false);+ return;+ }+ commit(option.value);+ }}+ >+ <Check+ className={cn(+ "mr-2 h-4 w-4",+ isSelected ? "visible" : "invisible",+ )}+ />+ <div+ className="overflow-x-hidden text-ellipsis whitespace-nowrap"+ title={displayValue}+ >+ {displayValue}+ </div>+ {option.count !== undefined ? (+ <span className="ml-auto flex h-4 w-4 items-center justify-center pl-1 font-mono text-xs">+ {option.count}+ </span>+ ) : null}+ </InputCommandItem>+ );++ return option.description ? (+ <PropertyHoverCard+ key={option.value}+ label={displayValue}+ description={option.description}+ >+ {commandItem}+ </PropertyHoverCard>+ ) : (+ commandItem+ );+ })}+ </InputCommandGroup>+ {showCustomValue && (+ <InputCommandGroup>+ <InputCommandSeparator />+ <InputCommandItem+ key="__custom__"+ value={search.trim()}+ onSelect={() => commit(search.trim())}+ >+ <Plus className="mr-2 h-4 w-4 shrink-0 opacity-50" />+ <div className="overflow-x-hidden text-ellipsis whitespace-nowrap">+ Use “{search.trim()}”+ </div>+ </InputCommandItem>+ </InputCommandGroup>+ )}+ </InputCommandList>+ </InputCommand>+ </PopoverContent>+ </Popover>+ );+}diff --git a/web/src/features/metrics/components/MetricsFilterBuilder.clienttest.ts b/web/src/features/metrics/components/MetricsFilterBuilder.clienttest.tsnew file mode 100644index 0000000000..89d389a719--- /dev/null+++ b/web/src/features/metrics/components/MetricsFilterBuilder.clienttest.ts@@ -0,0 +1,295 @@+import { describe, expect, it } from "vitest";++import { type FilterState } from "@langfuse/shared";++import {+ getMetricsColumnsWithCustomSelect,+ getMetricsFilterColumns,+} from "@/src/features/metrics/metricsFilterColumns";++import { __test } from "./MetricsFilterBuilder";++const {+ buildV2FilterColumnsParams,+ viewFiltersToEditorFilters,+ editorFiltersToViewFilters,+ resolvesToColumn,+} = __test;++const modelFilter = (column: string): FilterState[number] => ({+ column,+ type: "stringOptions",+ operator: "any of",+ value: ["gpt-4"],+});++describe("editorFiltersToViewFilters", () => {+ it("canonical view row: round-trips through the editor unchanged", () => {+ const filters: FilterState = [modelFilter("providedModelName")];+ expect(+ editorFiltersToViewFilters(+ "observations",+ viewFiltersToEditorFilters("observations", filters),+ ),+ ).toEqual(filters);+ });++ it("legacy UI-table column: canonicalizes to the view dimension name", () => {+ expect(+ editorFiltersToViewFilters("observations", [modelFilter("Model")]),+ ).toEqual([modelFilter("providedModelName")]);+ });++ it("unmapped column: preserved verbatim, never dropped", () => {+ const filters: FilterState = [modelFilter("totallyUnknownColumn")];+ expect(editorFiltersToViewFilters("observations", filters)).toEqual(+ filters,+ );+ expect(+ editorFiltersToViewFilters(+ "observations",+ viewFiltersToEditorFilters("observations", filters),+ ),+ ).toEqual(filters);+ });+});++describe("resolvesToColumn", () => {+ const columnsFor = (+ view: "observations" | "scores-categorical",+ viewVersion: "v1" | "v2" = "v2",+ ) =>+ getMetricsFilterColumns({+ ...buildV2FilterColumnsParams({+ view,+ filterOptions: undefined,+ datasets: undefined,+ }),+ viewVersion,+ });++ const resolves = (+ column: string,+ view: "observations" | "scores-categorical",+ viewVersion: "v1" | "v2" = "v2",+ ) => resolvesToColumn(modelFilter(column), columnsFor(view, viewVersion));++ it("column on the view: resolves by canonical id and by display name", () => {+ expect(resolves("providedModelName", "observations")).toBe(true);+ expect(resolves("Model", "observations")).toBe(true);+ });++ it("column absent from the view: does not resolve in either spelling", () => {+ expect(resolves("providedModelName", "scores-categorical")).toBe(false);+ expect(resolves("Model", "scores-categorical")).toBe(false);+ });++ it("v2-only column on v1: does not resolve", () => {+ expect(resolves("isRootObservation", "observations", "v2")).toBe(true);+ expect(resolves("isRootObservation", "observations", "v1")).toBe(false);+ });++ it("unmapped column: does not resolve", () => {+ expect(resolves("totallyUnknownColumn", "observations")).toBe(false);+ });++ it("saved trace-spelling rows: resolve onto the v2 observations pair", () => {+ for (const column of [+ "traceRelease",+ "Trace Release",+ "traceVersion",+ "Trace Version",+ ]) {+ expect(resolves(column, "observations")).toBe(true);+ }+ });+});++describe("buildV2FilterColumnsParams", () => {+ const getColumn = (+ view: "observations",+ id: string,+ viewVersion?: "v1" | "v2",+ ) => {+ const params = buildV2FilterColumnsParams({+ view,+ filterOptions: undefined,+ datasets: undefined,+ });+ return getMetricsFilterColumns({+ ...params,+ viewVersion: viewVersion ?? params.viewVersion,+ }).find((c) => c.id === id);+ };++ it("offers every Observation Type value even when discovery data is empty", () => {+ const typeColumn = getColumn("observations", "type");+ expect(typeColumn?.type).toBe("stringOptions");+ const values =+ typeColumn?.type === "stringOptions"+ ? typeColumn.options.map((o) => o.value)+ : [];+ expect(values).toContain("TOOL");+ expect(values).toContain("GENERATION");+ expect(values.length).toBeGreaterThan(0);+ });++ it("offers every Observation Level value even when discovery data is empty", () => {+ const levelColumn = getColumn("observations", "level");+ expect(levelColumn?.type).toBe("stringOptions");+ const values =+ levelColumn?.type === "stringOptions"+ ? levelColumn.options.map((o) => o.value)+ : [];+ expect(values).toContain("ERROR");+ expect(values).toContain("WARNING");+ expect(values.length).toBeGreaterThan(0);+ });++ it("maps filterOptions.name into a searchable Observation Name stringOptions column", () => {+ const params = buildV2FilterColumnsParams({+ view: "observations",+ filterOptions: {+ name: [{ value: "generation-alpha" }, { value: "generation-beta" }],+ } as Parameters<typeof buildV2FilterColumnsParams>[0]["filterOptions"],+ datasets: undefined,+ });+ const column = getMetricsFilterColumns(params).find(+ (c) => c.id === "observationName",+ );+ expect(column?.type).toBe("stringOptions");+ const values =+ column?.type === "stringOptions"+ ? column.options.map((o) => o.value)+ : [];+ expect(values).toEqual(["generation-alpha", "generation-beta"]);+ expect(getMetricsColumnsWithCustomSelect(params)).toContain(+ "observationName",+ );+ });++ it("maps user/session/version/release into searchable stringOptions columns", () => {+ const params = buildV2FilterColumnsParams({+ view: "observations",+ filterOptions: {+ userId: [{ value: "user-1" }],+ sessionId: [{ value: "session-1" }],+ version: [{ value: "1.0.0" }],+ release: [{ value: "2024-01" }],+ } as Parameters<typeof buildV2FilterColumnsParams>[0]["filterOptions"],+ datasets: undefined,+ });+ const columns = getMetricsFilterColumns(params);+ const custom = getMetricsColumnsWithCustomSelect(params);++ for (const id of ["user", "session", "version", "release"]) {+ const column = columns.find((c) => c.id === id);+ expect(column?.type).toBe("stringOptions");+ expect(custom).toContain(id);+ }+ const userColumn = columns.find((c) => c.id === "user");+ const userValues =+ userColumn?.type === "stringOptions"+ ? userColumn.options.map((o) => o.value)+ : [];+ expect(userValues).toEqual(["user-1"]);+ });++ it("labels Experiment ID options by name via displayValue", () => {+ const params = buildV2FilterColumnsParams({+ view: "observations",+ filterOptions: {+ experimentId: [{ value: "exp-1", displayValue: "My Experiment" }],+ } as Parameters<typeof buildV2FilterColumnsParams>[0]["filterOptions"],+ datasets: undefined,+ });+ const column = getMetricsFilterColumns(params).find(+ (c) => c.id === "experimentId",+ );+ expect(column?.type).toBe("stringOptions");+ const options = column?.type === "stringOptions" ? column.options : [];+ expect(options).toEqual([+ { value: "exp-1", displayValue: "My Experiment" },+ ]);+ expect(getMetricsColumnsWithCustomSelect(params)).toContain("experimentId");+ });++ it("wires metadata key suggestions into the Metadata column", () => {+ const params = buildV2FilterColumnsParams({+ view: "observations",+ filterOptions: undefined,+ datasets: undefined,+ metadataKeys: ["region", "tier"],+ });+ const column = getMetricsFilterColumns(params).find(+ (c) => c.id === "metadata",+ );+ expect(column?.type).toBe("stringObject");+ const keyOptions =+ column?.type === "stringObject" ? column.keyOptions : undefined;+ expect(keyOptions).toEqual(["region", "tier"]);+ expect(getMetricsColumnsWithCustomSelect(params)).toContain("metadata");+ });++ it("keeps Type/Level as non-searchable columns", () => {+ const params = buildV2FilterColumnsParams({+ view: "observations",+ filterOptions: undefined,+ datasets: undefined,+ });+ const custom = getMetricsColumnsWithCustomSelect(params);+ expect(custom).not.toContain("type");+ expect(custom).not.toContain("level");+ });++ it("boolean score view: offers booleanValue instead of value", () => {+ const booleanParams = buildV2FilterColumnsParams({+ view: "scores-boolean",+ filterOptions: undefined,+ datasets: undefined,+ });+ const booleanColumns = getMetricsFilterColumns(booleanParams);+ expect(+ booleanColumns.find((column) => column.id === "booleanValue")?.type,+ ).toBe("boolean");+ expect(booleanColumns.some((column) => column.id === "value")).toBe(false);++ const numericParams = buildV2FilterColumnsParams({+ view: "scores-numeric",+ filterOptions: undefined,+ datasets: undefined,+ });+ expect(+ getMetricsFilterColumns(numericParams).some(+ (column) => column.id === "booleanValue",+ ),+ ).toBe(false);+ });++ it("trace-level release/version pair: offered on v1 only", () => {+ expect(getColumn("observations", "traceRelease", "v1")).toMatchObject({+ name: "Trace Release",+ });+ expect(getColumn("observations", "traceVersion", "v1")).toMatchObject({+ name: "Trace Version",+ });+ expect(getColumn("observations", "traceRelease")).toBeUndefined();+ expect(getColumn("observations", "traceVersion")).toBeUndefined();+ expect(getColumn("observations", "release")).toMatchObject({+ name: "Release",+ });+ expect(getColumn("observations", "version")).toMatchObject({+ name: "Version",+ });+ });++ it("semantic-root filtering: offered for v2 observations only", () => {+ expect(getColumn("observations", "isRootObservation")).toMatchObject({+ name: "Is Root Observation",+ type: "boolean",+ });+ expect(+ getColumn("observations", "isRootObservation", "v1"),+ ).toBeUndefined();+ });+});diff --git a/web/src/features/metrics/components/MetricsFilterBuilder.tsx b/web/src/features/metrics/components/MetricsFilterBuilder.tsxnew file mode 100644index 0000000000..1ae8f28d44--- /dev/null+++ b/web/src/features/metrics/components/MetricsFilterBuilder.tsx@@ -0,0 +1,439 @@+import { startCase } from "lodash";+import { AlertCircle } from "lucide-react";++import {+ type ColumnDefinition,+ type FilterState,+ ObservationLevelDomain,+ ObservationTypeDomain,+ type SingleValueOption,+ type TimeFilter,+} from "@langfuse/shared";+import { type views, type ViewVersion } from "@langfuse/shared/query";+import { type z } from "zod";++import { Alert, AlertDescription, AlertTitle } from "@/src/components/ui/alert";+import { api, type RouterInputs, type RouterOutputs } from "@/src/utils/api";+import {+ displayNameForFilterColumn,+ mapViewFilterToUiTableFilter,+ partitionWidgetUiTableFiltersToView,+} from "@/src/features/dashboard/lib/dashboardUiTableToViewMapping";+import { useMetadataValueOptions } from "@/src/features/events/hooks/useMetadataValueOptions";+import { InlineFilterBuilder } from "@/src/features/filters/components/filter-builder";+import { normalizeSingleValueOptions } from "@/src/features/filters/lib/filter-transform";+import { sortOptionValues } from "@/src/features/filters/lib/option-sort";+import {+ getMetricsColumnsWithCustomSelect,+ getMetricsFilterColumns,+ type GetMetricsFilterColumnsParams,+} from "@/src/features/metrics/metricsFilterColumns";++const observationLevelOptions = ObservationLevelDomain.options.map((value) => ({+ value,+}));+const observationTypeOptions = ObservationTypeDomain.options.map((value) => ({+ value,+}));++const v1FilterOptionsQueryConfig = {+ trpc: { context: { skipBatch: true } },+ refetchOnMount: false,+ refetchOnWindowFocus: false,+ refetchOnReconnect: false,+ staleTime: Infinity,+} as const;++const v2FilterOptionsQueryConfig = {+ trpc: { context: { skipBatch: true } },+ staleTime: 60 * 1000,+ refetchOnWindowFocus: false,+ refetchOnReconnect: false,+} as const;++/** eventsFilterOptionsColumns lists the v2 facets fast enough to load in one request. */+const eventsFilterOptionsColumns = [+ "providedModelName",+ "modelId",+ "name",+ "promptName",+ "traceTags",+ "traceName",+ "type",+ "userId",+ "version",+ "release",+ "sessionId",+ "level",+ "environment",+ "ingestionApiKey",+ "experimentDatasetId",+ "experimentId",+ "experimentName",+ "isRootObservation",+ "calledToolNames",+ "metadataKeys",+ "scores_avg",+ "score_categories",+ "score_booleans",+ "trace_scores_avg",+ "trace_score_categories",+ "trace_score_booleans",+] satisfies EventFilterOptionsColumn[];++/** slowEventsFilterOptionsColumns lists the v2 facets that scan slowly enough to need their own request. */+const slowEventsFilterOptionsColumns = [+ "toolNames",+] satisfies EventFilterOptionsColumn[];++/** MetricsFilterBuilder filters metrics by the dimensions of the data model, dispatching to the version-specific fetcher. */+export const MetricsFilterBuilder = ({+ version,+ ...props+}: MetricsFilterFetcherProps & { version: ViewVersion }) => {+ if (version === "v1") return <MetricsFilterBuilderV1 {...props} />;+ return <MetricsFilterBuilderV2 {...props} />;+};++/** MetricsFilterDateRange is the preview/lookback window used to scope filter-value discovery. */+type MetricsFilterDateRange = { from: Date; to?: Date };++/** MetricsFilterFetcherProps is the version-agnostic contract shared by both fetchers. */+type MetricsFilterFetcherProps = {+ view: z.infer<typeof views>;+ projectId: string;+ dateRange?: MetricsFilterDateRange;+ filters: FilterState;+ onChange: (filters: FilterState) => void;+};++/** MetricsFilterBuilderV1 loads the v1 (traces + generations + project) filter options and renders the filter view. */+const MetricsFilterBuilderV1 = ({+ view,+ projectId,+ dateRange,+ filters,+ onChange,+}: MetricsFilterFetcherProps) => {+ const traceFilterOptions = api.traces.filterOptions.useQuery(+ {+ projectId,+ timestampFilter: metricsFilterTimeFilter("timestamp", dateRange),+ },+ v1FilterOptionsQueryConfig,+ );++ const generationsFilterOptions = api.generations.filterOptions.useQuery(+ {+ projectId,+ startTimeFilter: metricsFilterTimeFilter("startTime", dateRange),+ observationType: "ALL",+ },+ v1FilterOptionsQueryConfig,+ );++ const environmentFilterOptions =+ api.projects.environmentFilterOptions.useQuery(+ { projectId, fromTimestamp: dateRange?.from },+ v1FilterOptionsQueryConfig,+ );++ const params = buildV1FilterColumnsParams({+ view,+ traceFilterOptions: traceFilterOptions.data,+ generationsFilterOptions: generationsFilterOptions.data,+ environmentFilterOptions: environmentFilterOptions.data,+ });++ return (+ <MetricsFilterView+ view={view}+ columns={getMetricsFilterColumns(params)}+ columnsWithCustomSelect={getMetricsColumnsWithCustomSelect(params)}+ filters={filters}+ onChange={onChange}+ />+ );+};++/** MetricsFilterBuilderV2 loads the v2 (events) filter options and renders the filter view with metadata value suggestions. */+const MetricsFilterBuilderV2 = ({+ view,+ projectId,+ dateRange,+ filters,+ onChange,+}: MetricsFilterFetcherProps) => {+ const startTimeFilter = metricsFilterTimeFilter("startTime", dateRange);++ const eventsFilterOptions = api.events.filterOptions.useQuery(+ { projectId, startTimeFilter, columns: eventsFilterOptionsColumns },+ v2FilterOptionsQueryConfig,+ );++ const slowEventsFilterOptions = api.events.filterOptions.useQuery(+ { projectId, startTimeFilter, columns: slowEventsFilterOptionsColumns },+ v2FilterOptionsQueryConfig,+ );++ const datasets = api.datasets.allDatasetMeta.useQuery({ projectId });++ const { metadataValueOptions, onMetadataKeyChange } = useMetadataValueOptions(+ { projectId, filterState: filters, startTimeFilter },+ );++ const params = buildV2FilterColumnsParams({+ view,+ filterOptions: eventsFilterOptions.data,+ slowFilterOptions: slowEventsFilterOptions.data,+ datasets: datasets.data,+ metadataKeys: eventsFilterOptions.data?.metadataKeys?.map(+ (row) => row.value,+ ),+ });++ return (+ <MetricsFilterView+ view={view}+ columns={getMetricsFilterColumns(params)}+ columnsWithCustomSelect={getMetricsColumnsWithCustomSelect(params)}+ stringObjectValueOptions={metadataValueOptions}+ onStringObjectKeyChange={onMetadataKeyChange}+ filters={filters}+ onChange={onChange}+ />+ );+};++/** MetricsFilterView renders the metric filter builder, translating between view-dimension space and UI-table labels and surfacing rows that are not valid for the view. */+const MetricsFilterView = ({+ view,+ columns,+ columnsWithCustomSelect,+ stringObjectValueOptions,+ onStringObjectKeyChange,+ filters,+ onChange,+}: {+ view: z.infer<typeof views>;+ columns: ColumnDefinition[];+ columnsWithCustomSelect: string[];+ stringObjectValueOptions?: Record<string, SingleValueOption[]>;+ onStringObjectKeyChange?: (key: string) => void;+ filters: FilterState;+ onChange: (filters: FilterState) => void;+}) => {+ const editorFilters = viewFiltersToEditorFilters(view, filters);+ const renderable = editorFilters.filter((filter) =>+ resolvesToColumn(filter, columns),+ );+ const unsupportedColumns = Array.from(+ new Set(+ editorFilters+ .filter((filter) => !resolvesToColumn(filter, columns))+ .map((filter) => displayNameForFilterColumn(filter.column)),+ ),+ ).join(", ");++ return (+ <div className="space-y-2">+ {unsupportedColumns.length > 0 && (+ <Alert+ variant="default"+ className="border-yellow-500/50 bg-yellow-50 dark:bg-yellow-950/20"+ >+ <AlertCircle className="h-4 w-4 text-yellow-600 dark:text-yellow-500" />+ <AlertTitle className="text-yellow-800 dark:text-yellow-400">+ Unsupported filters+ </AlertTitle>+ <AlertDescription className="text-yellow-700 dark:text-yellow-500">+ {`These filter columns are not supported for ${startCase(view)} and were dropped: ${unsupportedColumns}. Switch back to a compatible view to restore them.`}+ </AlertDescription>+ </Alert>+ )}+ <InlineFilterBuilder+ columns={columns}+ filterState={renderable}+ onChange={(next: FilterState) =>+ onChange(editorFiltersToViewFilters(view, next))+ }+ columnsWithCustomSelect={columnsWithCustomSelect}+ stringObjectValueOptions={stringObjectValueOptions}+ onStringObjectKeyChange={onStringObjectKeyChange}+ compact+ />+ </div>+ );+};++/** metricsFilterTimeFilter keys a {from, to?} range to a column as the TimeFilter[] the filter-options endpoints expect. */+const metricsFilterTimeFilter = (+ column: "timestamp" | "startTime",+ dateRange?: MetricsFilterDateRange,+): TimeFilter[] | undefined => {+ if (!dateRange) return undefined;+ const filters: TimeFilter[] = [+ { column, type: "datetime", operator: ">=", value: dateRange.from },+ ];+ if (dateRange.to) {+ filters.push({+ column,+ type: "datetime",+ operator: "<=",+ value: dateRange.to,+ });+ }+ return filters;+};++/** buildV1FilterColumnsParams assembles the metric filter column options from the v1 endpoints; v1 keeps plain-string columns, so the events-only suggestion lists stay empty. */+const buildV1FilterColumnsParams = ({+ view,+ traceFilterOptions,+ generationsFilterOptions,+ environmentFilterOptions,+}: {+ view: MetricsFilterFetcherProps["view"];+ traceFilterOptions: RouterOutputs["traces"]["filterOptions"] | undefined;+ generationsFilterOptions:+ | RouterOutputs["generations"]["filterOptions"]+ | undefined;+ environmentFilterOptions:+ | RouterOutputs["projects"]["environmentFilterOptions"]+ | undefined;+}): GetMetricsFilterColumnsParams => ({+ selectedView: view,+ viewVersion: "v1",+ environmentOptions:+ environmentFilterOptions?.map((value) => ({+ value: value.environment,+ })) ?? [],+ nameOptions: normalizeSingleValueOptions(traceFilterOptions?.name),+ observationNameOptions: normalizeSingleValueOptions(+ generationsFilterOptions?.name,+ ),+ tagsOptions: traceFilterOptions?.tags ?? [],+ modelOptions: generationsFilterOptions?.model ?? [],+ toolNamesOptions: generationsFilterOptions?.toolNames ?? [],+ calledToolNamesOptions: generationsFilterOptions?.calledToolNames ?? [],+ observationLevelOptions,+ experimentNameOptions: [],+ experimentDatasetOptions: [],+ observationTypeOptions,+ userOptions: [],+ sessionOptions: [],+ versionOptions: [],+ releaseOptions: [],+ scoreNameOptions: [],+ experimentIdOptions: [],+ metadataKeyOptions: [],+});++/** buildV2FilterColumnsParams assembles the metric filter column options from the v2 events filter-options discovery; closed Type/Level enums come from the domain schemas. */+const buildV2FilterColumnsParams = ({+ view,+ filterOptions,+ slowFilterOptions,+ datasets,+ metadataKeys,+}: {+ view: z.infer<typeof views>;+ filterOptions: RouterOutputs["events"]["filterOptions"] | undefined;+ slowFilterOptions?: RouterOutputs["events"]["filterOptions"];+ datasets: Array<{ id: string; name: string }> | undefined;+ metadataKeys?: string[];+}): GetMetricsFilterColumnsParams => {+ const datasetIds = new Set(+ (filterOptions?.experimentDatasetId ?? []).map((e) => e.value),+ );+ return {+ selectedView: view,+ viewVersion: "v2",+ environmentOptions: filterOptions?.environment ?? [],+ nameOptions: normalizeSingleValueOptions(filterOptions?.traceName),+ observationNameOptions: normalizeSingleValueOptions(filterOptions?.name),+ tagsOptions: sortOptionValues(filterOptions?.traceTags ?? []),+ modelOptions: filterOptions?.providedModelName ?? [],+ toolNamesOptions: slowFilterOptions?.toolNames ?? [],+ calledToolNamesOptions: filterOptions?.calledToolNames ?? [],+ observationLevelOptions,+ experimentNameOptions: filterOptions?.experimentName ?? [],+ experimentDatasetOptions:+ datasets+ ?.filter((d) => datasetIds.has(d.id))+ .map((d) => ({ value: d.id, displayValue: d.name })) ?? [],+ observationTypeOptions,+ userOptions: normalizeSingleValueOptions(filterOptions?.userId),+ sessionOptions: normalizeSingleValueOptions(filterOptions?.sessionId),+ versionOptions: normalizeSingleValueOptions(filterOptions?.version),+ releaseOptions: normalizeSingleValueOptions(filterOptions?.release),+ scoreNameOptions: scoreNameOptionsForView(view, filterOptions),+ experimentIdOptions: normalizeSingleValueOptions(+ filterOptions?.experimentId,+ ),+ metadataKeyOptions: metadataKeys ?? [],+ };+};++/** scoreNameOptionsForView sources Score Name suggestions from the view's score facet. */+const scoreNameOptionsForView = (+ view: z.infer<typeof views>,+ filterOptions: RouterOutputs["events"]["filterOptions"] | undefined,+): SingleValueOption[] => {+ if (view === "scores-numeric") {+ return (filterOptions?.scores_avg ?? []).map((value) => ({ value }));+ }+ if (view === "scores-categorical") {+ return (filterOptions?.score_categories ?? []).map((category) => ({+ value: category.label,+ }));+ }+ return [];+};++/** viewFiltersToEditorFilters relabels canonical view-dimension rows into UI-table labels for the builder, preserving unmapped rows. */+const viewFiltersToEditorFilters = (+ view: z.infer<typeof views>,+ filters: FilterState,+): FilterState => {+ const { mappedFilters, unsupportedFilters } =+ partitionWidgetUiTableFiltersToView(view, filters);+ return [+ ...mapViewFilterToUiTableFilter(view, mappedFilters),+ ...unsupportedFilters,+ ];+};++/** editorFiltersToViewFilters canonicalizes edited UI-table rows back into view-dimension space, preserving unmapped rows. */+const editorFiltersToViewFilters = (+ view: z.infer<typeof views>,+ filters: FilterState,+): FilterState => {+ const { mappedFilters, unsupportedFilters } =+ partitionWidgetUiTableFiltersToView(view, filters);+ return [...mappedFilters, ...unsupportedFilters];+};++/** resolvesToColumn reports whether the builder has a column definition able to render the row. */+const resolvesToColumn = (+ filter: FilterState[number],+ columns: ColumnDefinition[],+): boolean =>+ columns.some(+ (column) =>+ column.id === filter.column ||+ column.name === filter.column ||+ column.aliases?.includes(filter.column) === true,+ );++/** EventFilterOptionsColumn is one facet column the events filter-options endpoint understands. */+type EventFilterOptionsColumn = NonNullable<+ RouterInputs["events"]["filterOptions"]["columns"]+>[number];++export const __test = {+ buildV2FilterColumnsParams,+ viewFiltersToEditorFilters,+ editorFiltersToViewFilters,+ resolvesToColumn,+};diff --git a/web/src/features/widgets/components/widgetFilterColumns.ts b/web/src/features/metrics/metricsFilterColumns.tssimilarity index 63%rename from web/src/features/widgets/components/widgetFilterColumns.tsrename to web/src/features/metrics/metricsFilterColumns.tsindex 8904ad7df8..a912057324 100644--- a/web/src/features/widgets/components/widgetFilterColumns.ts+++ b/web/src/features/metrics/metricsFilterColumns.ts@@ -6,7 +6,8 @@ import { type ViewVersion, type views } from "@langfuse/shared/query";import { type z } from "zod";-type GetWidgetFilterColumnsParams = {+/** GetMetricsFilterColumnsParams is the option dictionary the metric filter column specs are built from. */+export type GetMetricsFilterColumnsParams = {selectedView: z.infer<typeof views>;viewVersion: ViewVersion;environmentOptions: SingleValueOption[];@@ -20,14 +21,21 @@ type GetWidgetFilterColumnsParams = {experimentNameOptions: SingleValueOption[];experimentDatasetOptions: SingleValueOption[];observationTypeOptions: SingleValueOption[];+ userOptions: SingleValueOption[];+ sessionOptions: SingleValueOption[];+ versionOptions: SingleValueOption[];+ releaseOptions: SingleValueOption[];+ scoreNameOptions: SingleValueOption[];+ experimentIdOptions: SingleValueOption[];+ metadataKeyOptions: string[];};-type WidgetFilterColumnSpec = {+type MetricsFilterColumnSpec = {column: ColumnDefinition;customSelect?: boolean;};-const getWidgetFilterColumnSpecs = ({+const getMetricsFilterColumnSpecs = ({selectedView,viewVersion,environmentOptions,@@ -41,8 +49,44 @@ const getWidgetFilterColumnSpecs = ({experimentNameOptions,experimentDatasetOptions,observationTypeOptions,-}: GetWidgetFilterColumnsParams): WidgetFilterColumnSpec[] => {- const filterColumns: WidgetFilterColumnSpec[] = [+ userOptions,+ sessionOptions,+ versionOptions,+ releaseOptions,+ scoreNameOptions,+ experimentIdOptions,+ metadataKeyOptions,+}: GetMetricsFilterColumnsParams): MetricsFilterColumnSpec[] => {+ // Value suggestions come from the v2 events filter-options; v1 keeps manual+ // entry (plain string columns), per LFE-9570 decision to leave v1 plain.+ const suggestString = (+ name: string,+ id: string,+ options: SingleValueOption[],+ aliases?: string[],+ ): MetricsFilterColumnSpec =>+ viewVersion === "v2"+ ? {+ column: {+ name,+ id,+ type: "stringOptions",+ options,+ internal: "internalValue",+ ...(aliases ? { aliases } : {}),+ },+ customSelect: true,+ }+ : {+ column: { name, id, type: "string", internal: "internalValue" },+ };+ // v4 events hold one column per pair (e.version / e.release), so the v2+ // observations view offers a single Version / Release and the "Trace"+ // spellings of saved widgets alias onto it.+ const aliasesTraceSpelling =+ viewVersion === "v2" && selectedView === "observations";+ const metadataSuggest = viewVersion === "v2";+ const filterColumns: MetricsFilterColumnSpec[] = [{column: {name: "Environment",@@ -78,49 +122,36 @@ const getWidgetFilterColumnSpecs = ({},customSelect: true,},- {- column: {- name: "User",- id: "user",- type: "string",- internal: "internalValue",- },- },- {- column: {- name: "Session",- id: "session",- type: "string",- internal: "internalValue",- },- },+ suggestString("User", "user", userOptions),+ suggestString("Session", "session", sessionOptions),{column: {name: "Metadata",id: "metadata",type: "stringObject",internal: "internalValue",+ ...(metadataSuggest ? { keyOptions: metadataKeyOptions } : {}),},+ customSelect: metadataSuggest,},- {- column: {- name: "Version",- id: "version",- type: "string",- internal: "internalValue",- },- },+ suggestString(+ "Version",+ "version",+ versionOptions,+ aliasesTraceSpelling ? ["traceVersion", "Trace Version"] : undefined,+ ),];- if (selectedView !== "observations") {- filterColumns.push({- column: {- name: "Release",- id: "release",- type: "string",- internal: "internalValue",- },- });+ // v1 observationsView has no release dimension; only traceRelease.+ if (viewVersion === "v2" || selectedView !== "observations") {+ filterColumns.push(+ suggestString(+ "Release",+ "release",+ releaseOptions,+ aliasesTraceSpelling ? ["traceRelease", "Trace Release"] : undefined,+ ),+ );}if (selectedView === "observations") {@@ -145,14 +176,7 @@ const getWidgetFilterColumnSpecs = ({selectedView === "scores-categorical") {filterColumns.push(- {- column: {- name: "Score Name",- id: "scoreName",- type: "string",- internal: "internalValue",- },- },+ suggestString("Score Name", "scoreName", scoreNameOptions),{column: {name: "Observation Name",@@ -171,14 +195,6 @@ const getWidgetFilterColumnSpecs = ({// v2-only filter columns (experiment data only exists in events table)...(viewVersion === "v2"? [- {- column: {- name: "Observation Release",- id: "release",- type: "string",- internal: "internalValue",- },- } satisfies WidgetFilterColumnSpec,{column: {name: "Experiment Name",@@ -188,7 +204,7 @@ const getWidgetFilterColumnSpecs = ({internal: "internalValue",},customSelect: true,- } satisfies WidgetFilterColumnSpec,+ } satisfies MetricsFilterColumnSpec,{column: {name: "Experiment Dataset",@@ -198,15 +214,17 @@ const getWidgetFilterColumnSpecs = ({internal: "internalValue",},customSelect: true,- } satisfies WidgetFilterColumnSpec,+ } satisfies MetricsFilterColumnSpec,{column: {name: "Experiment ID",id: "experimentId",- type: "null",+ type: "stringOptions",+ options: experimentIdOptions,internal: "internalValue",},- } satisfies WidgetFilterColumnSpec,+ customSelect: true,+ } satisfies MetricsFilterColumnSpec,{column: {name: "Is Root Observation",@@ -214,7 +232,7 @@ const getWidgetFilterColumnSpecs = ({type: "boolean",internal: "internalValue",},- } satisfies WidgetFilterColumnSpec,+ } satisfies MetricsFilterColumnSpec,]: []),{@@ -237,22 +255,13 @@ const getWidgetFilterColumnSpecs = ({},customSelect: true,},- {- column: {- name: "Trace Release",- id: "traceRelease",- type: "string",- internal: "internalValue",- },- },- {- column: {- name: "Trace Version",- id: "traceVersion",- type: "string",- internal: "internalValue",- },- },+ // v1 joins traces for these; v4 has no separate trace-level pair.+ ...(viewVersion === "v1"+ ? [+ suggestString("Trace Release", "traceRelease", []),+ suggestString("Trace Version", "traceVersion", []),+ ]+ : []),{column: {name: "Model",@@ -320,14 +329,16 @@ const getWidgetFilterColumnSpecs = ({return filterColumns;};-export const getWidgetFilterColumns = (- params: GetWidgetFilterColumnsParams,+/** getMetricsFilterColumns builds the InlineFilterBuilder column definitions for a metric view. */+export const getMetricsFilterColumns = (+ params: GetMetricsFilterColumnsParams,): ColumnDefinition[] =>- getWidgetFilterColumnSpecs(params).map((spec) => spec.column);+ getMetricsFilterColumnSpecs(params).map((spec) => spec.column);-export const getWidgetColumnsWithCustomSelect = (- params: GetWidgetFilterColumnsParams,+/** getMetricsColumnsWithCustomSelect lists the column ids that render a custom (searchable) select control. */+export const getMetricsColumnsWithCustomSelect = (+ params: GetMetricsFilterColumnsParams,): string[] =>- getWidgetFilterColumnSpecs(params)+ getMetricsFilterColumnSpecs(params).filter((spec) => spec.customSelect).map((spec) => spec.column.id);diff --git a/web/src/features/monitors/components/MonitorForm.clienttest.ts b/web/src/features/monitors/components/MonitorForm.clienttest.tsindex fdd0ef4ba9..6b388e80a6 100644--- a/web/src/features/monitors/components/MonitorForm.clienttest.ts+++ b/web/src/features/monitors/components/MonitorForm.clienttest.ts@@ -10,18 +10,13 @@ import {MonitorThresholdOperatorSchema,} from "@langfuse/shared/monitors";-import {- getWidgetColumnsWithCustomSelect,- getWidgetFilterColumns,-} from "@/src/features/widgets/components/widgetFilterColumns";-import { __test } from "./MonitorForm";const {createDefaults,monitorToDefaults,nameOrPlaceholder,- buildFilterColumnsParams,+ resolveViewChangePatch,} = __test;describe("createDefaults", () => {@@ -99,121 +94,33 @@ describe("nameOrPlaceholder", () => {});});-describe("buildFilterColumnsParams", () => {- // A monitor's filter-option discovery is scoped to its (default 5m) evaluation- // window, so it is often empty β the whole point of an alert like- // "type=TOOL AND level=ERROR" is to catch events that have NOT happened yet.- // Type and Level are closed enums and their value pickers are not searchable- // (no free-text fallback), so they must list every domain value regardless of- // what the discovery window returned, otherwise they dead-end on- // "No results found" (LFE-10616).- const getColumn = (- view: "observations",- id: string,- viewVersion?: "v1" | "v2",- ) => {- const params = buildFilterColumnsParams({- view,- filterOptions: undefined, // empty discovery window- datasets: undefined,- });- return getWidgetFilterColumns({- ...params,- viewVersion: viewVersion ?? params.viewVersion,- }).find((c) => c.id === id);- };-- it("offers every Observation Type value even when discovery data is empty", () => {- const typeColumn = getColumn("observations", "type");- expect(typeColumn?.type).toBe("stringOptions");- const values =- typeColumn?.type === "stringOptions"- ? typeColumn.options.map((o) => o.value)- : [];- expect(values).toContain("TOOL");- expect(values).toContain("GENERATION");- expect(values.length).toBeGreaterThan(0);- });-- it("offers every Observation Level value even when discovery data is empty", () => {- const levelColumn = getColumn("observations", "level");- expect(levelColumn?.type).toBe("stringOptions");- const values =- levelColumn?.type === "stringOptions"- ? levelColumn.options.map((o) => o.value)- : [];- expect(values).toContain("ERROR");- expect(values).toContain("WARNING");- expect(values.length).toBeGreaterThan(0);- });-- it("maps filterOptions.name into a searchable Observation Name stringOptions column", () => {- const params = buildFilterColumnsParams({- view: "observations",- filterOptions: {- name: [{ value: "generation-alpha" }, { value: "generation-beta" }],- } as Parameters<typeof buildFilterColumnsParams>[0]["filterOptions"],- datasets: undefined,- });- const column = getWidgetFilterColumns(params).find(- (c) => c.id === "observationName",- );- expect(column?.type).toBe("stringOptions");- const values =- column?.type === "stringOptions"- ? column.options.map((o) => o.value)- : [];- expect(values).toEqual(["generation-alpha", "generation-beta"]);- expect(getWidgetColumnsWithCustomSelect(params)).toContain(- "observationName",+describe("resolveViewChangePatch", () => {+ it("view change: never patches filters", () => {+ expect(+ resolveViewChangePatch("scores-numeric", "count"),+ ).not.toHaveProperty("filters");+ expect(+ resolveViewChangePatch("scores-numeric", "latency"),+ ).not.toHaveProperty("filters");+ expect(resolveViewChangePatch("observations", "value")).not.toHaveProperty(+ "filters",);});- it("keeps Type/Level as non-searchable columns (they rely on complete option lists)", () => {- // Confirms the fix must be complete enum lists: Type/Level are NOT custom- // (searchable/free-text) selects, so an empty option list is a hard dead-end.- const params = buildFilterColumnsParams({- view: "observations",- filterOptions: undefined,- datasets: undefined,+ it("measure absent on the new view: resets the metric to count", () => {+ expect(resolveViewChangePatch("scores-numeric", "latency").metric).toEqual({+ measure: "count",+ aggregation: "count",});- const custom = getWidgetColumnsWithCustomSelect(params);- expect(custom).not.toContain("type");- expect(custom).not.toContain("level");});- it("offers a boolean value filter only for boolean score monitors", () => {- const booleanParams = buildFilterColumnsParams({- view: "scores-boolean",- filterOptions: undefined,- datasets: undefined,- });- const booleanColumns = getWidgetFilterColumns(booleanParams);+ it("measure present on the new view: leaves the metric alone", () => {expect(- booleanColumns.find((column) => column.id === "booleanValue")?.type,- ).toBe("boolean");- expect(booleanColumns.some((column) => column.id === "value")).toBe(false);-- const numericParams = buildFilterColumnsParams({- view: "scores-numeric",- filterOptions: undefined,- datasets: undefined,- });- expect(- getWidgetFilterColumns(numericParams).some(- (column) => column.id === "booleanValue",- ),- ).toBe(false);- });-- it("offers semantic-root filtering only for v2 observations widgets", () => {- expect(getColumn("observations", "isRootObservation")).toMatchObject({- name: "Is Root Observation",- type: "boolean",- });+ resolveViewChangePatch("observations", "latency"),+ ).not.toHaveProperty("metric");expect(- getColumn("observations", "isRootObservation", "v1"),- ).toBeUndefined();+ resolveViewChangePatch("scores-numeric", "value"),+ ).not.toHaveProperty("metric");});});diff --git a/web/src/features/monitors/components/MonitorForm.tsx b/web/src/features/monitors/components/MonitorForm.tsxindex a54df7b71e..46453e3cae 100644--- a/web/src/features/monitors/components/MonitorForm.tsx+++ b/web/src/features/monitors/components/MonitorForm.tsx@@ -6,7 +6,7 @@ import { useForm, useWatch } from "react-hook-form";import { zodResolver } from "@hookform/resolvers/zod";import { startCase } from "lodash";-import { api, type RouterOutputs } from "@/src/utils/api";+import { api } from "@/src/utils/api";import { Button } from "@/src/components/ui/button";import {Accordion,@@ -43,25 +43,15 @@ import {import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";import { showSuccessToast } from "@/src/features/notifications/showSuccessToast";import { showErrorToast } from "@/src/features/notifications/showErrorToast";-import {- mapWidgetUiTableFilterToView,- normalizeStoredWidgetFiltersForEditor,-} from "@/src/features/dashboard/lib/dashboardUiTableToViewMapping";-import { InlineFilterBuilder } from "@/src/features/filters/components/filter-builder";import { WidgetPropertySelectItem } from "@/src/features/widgets/components/WidgetPropertySelectItem";-import {- getWidgetColumnsWithCustomSelect,- getWidgetFilterColumns,-} from "@/src/features/widgets/components/widgetFilterColumns";-import { normalizeSingleValueOptions } from "@/src/features/filters/lib/filter-transform";-import { sortOptionValues } from "@/src/features/filters/lib/option-sort";+import { MetricsFilterBuilder } from "@/src/features/metrics/components/MetricsFilterBuilder";+import { partitionWidgetUiTableFiltersToView } from "@/src/features/dashboard/lib/dashboardUiTableToViewMapping";import { cn } from "@/src/utils/tailwind";import {CreateMonitorSchema,type CreateMonitor,getValidMonitorAggregationsForMeasure,- getValidMonitorFilterColumns,type Monitor,type MonitorNoData,MonitorNoDataModeSchema,@@ -77,13 +67,7 @@ import {UpdateMonitorSchema,type UpdateMonitor,} from "@langfuse/shared/monitors";-import {- ObservationLevelDomain,- ObservationTypeDomain,- viewDeclarations,- type FilterState,- type TimeFilter,-} from "@langfuse/shared";+import { viewDeclarations, type FilterState } from "@langfuse/shared";import TagManager from "@/src/features/tag/components/TagManager";@@ -124,11 +108,7 @@ const monitorToDefaults = (monitor: Monitor): UpdateMonitor => ({id: monitor.id,projectId: monitor.projectId,view: monitor.view,- // Stored filters use the view's dimension names (e.g. "environment"); the- // InlineFilterBuilder works in UI-table column space (e.g. "Environment").- // Translate on load so the builder shows the right rows.- filters: normalizeStoredWidgetFiltersForEditor(monitor.view, monitor.filters)- .editorFilters,+ filters: monitor.filters,metric: monitor.metric,window: monitor.window,thresholdOperator: monitor.thresholdOperator,@@ -142,64 +122,24 @@ const monitorToDefaults = (monitor: Monitor): UpdateMonitor => ({// status omitted: the pause/resume toolbar owns it.});+/** resolveViewChangePatch returns the form fields that must change when the view changes. */+const resolveViewChangePatch = (+ nextView: keyof (typeof viewDeclarations)["v2"],+ currentMeasure: string,+): Partial<Pick<CreateMonitor, "metric">> => {+ const measures = viewDeclarations.v2[nextView]?.measures ?? {};+ // Filters are never patched; MetricsFilterBuilder drops rows the view cannot render.+ return currentMeasure in measures+ ? {}+ : { metric: { measure: "count", aggregation: "count" } };+};+/** nameOrPlaceholder falls back to the placeholder when the name is blank. */const nameOrPlaceholder = (name: string | undefined,placeholder: string,): string => name || placeholder;-// Observation Level and Type are closed enums, so their filter value pickers-// list every domain value. Deriving them from the monitor's (short, default 5m)-// evaluation window instead left them empty whenever that window happened to-// contain no matching events β e.g. a fresh monitor whose lookback saw no errors-// or tool calls yet β so the non-searchable "Level"/"Type" value dropdowns-// showed "No results found" with no way to pick a value (LFE-10616). Mirrors-// WidgetForm, which builds these two option lists from the domain enums too.-const observationLevelOptions = ObservationLevelDomain.options.map((value) => ({- value,-}));-const observationTypeOptions = ObservationTypeDomain.options.map((value) => ({- value,-}));--/**- * buildFilterColumnsParams assembles the InlineFilterBuilder option dictionaries- * for the picked view. Open-ended facets (environment, model, tags, β¦) come from- * the time-windowed events filter-options discovery; the closed Type/Level enums- * come from the domain schemas so their value pickers are always complete.- */-const buildFilterColumnsParams = ({- view,- filterOptions,- datasets,-}: {- view: MonitorView;- filterOptions: RouterOutputs["events"]["filterOptions"] | undefined;- datasets: Array<{ id: string; name: string }> | undefined;-}) => {- const datasetIds = new Set(- (filterOptions?.experimentDatasetId ?? []).map((e) => e.value),- );- return {- selectedView: view,- viewVersion: "v2" as const,- environmentOptions: filterOptions?.environment ?? [],- nameOptions: normalizeSingleValueOptions(filterOptions?.traceName),- observationNameOptions: normalizeSingleValueOptions(filterOptions?.name),- tagsOptions: sortOptionValues(filterOptions?.traceTags ?? []),- modelOptions: filterOptions?.providedModelName ?? [],- toolNamesOptions: filterOptions?.toolNames ?? [],- calledToolNamesOptions: filterOptions?.calledToolNames ?? [],- observationLevelOptions,- experimentNameOptions: filterOptions?.experimentName ?? [],- experimentDatasetOptions:- datasets- ?.filter((d) => datasetIds.has(d.id))- .map((d) => ({ value: d.id, displayValue: d.name })) ?? [],- observationTypeOptions,- };-};-/** MonitorForm renders the create/edit form for a Monitor. */export const MonitorForm = ({projectId,@@ -230,19 +170,14 @@ export const MonitorForm = ({/** namePlaceholderRef holds the latest computed name placeholder for the resolver. */const namePlaceholderRef = useRef("");- /** resolver wraps zodResolver, mapping filter columns into view-space and filling a blank name with the computed placeholder before validation. */+ /** resolver wraps zodResolver, filling a blank name with the computed placeholder before validation. */const resolver = useMemo(() => {const base = zodResolver(schema as any);return ((values, context, options) => {- const v = values as {- view: MonitorView;- filters?: FilterState;- name?: string;- };+ const v = values as { name?: string };const mapped = {...values,name: nameOrPlaceholder(v.name, namePlaceholderRef.current),- filters: mapWidgetUiTableFilterToView(v.view, v.filters ?? []),};return base(mapped as any, context, options);}) as typeof base;@@ -281,16 +216,17 @@ export const MonitorForm = ({onError: (e) => showErrorToast("Failed to save alert", e.message),});- /** onSubmit normalizes filter columns into view-space and dispatches the create or update mutation. */+ /** onSubmit strips unsupported filter rows and dispatches the create or update mutation. */const onSubmit = form.handleSubmit(- /** onValid normalize filter values before updating or saving the monitor */(values) => {const normalizedValues = {...values,- filters: mapWidgetUiTableFilterToView(- values.view as Parameters<typeof mapWidgetUiTableFilterToView>[0],+ filters: partitionWidgetUiTableFiltersToView(+ values.view as Parameters<+ typeof partitionWidgetUiTableFiltersToView+ >[0],(values.filters ?? []) as FilterState,- ),+ ).mappedFilters,} as typeof values;if (isEdit && monitor) {@@ -317,66 +253,18 @@ export const MonitorForm = ({const watched = useWatch({ control: form.control });const monitorWindow = (watched.window ?? "5m") as MonitorWindow;- /** filterOptionsStartTimeFilter lower-bounds discovery at max(20Γwindow, 7d) so even a small monitor window still yields value suggestions. */- const filterOptionsStartTimeFilter = useMemo<TimeFilter[]>(- () => [- {- column: "startTime",- type: "datetime",- operator: ">=",- value: getMonitorFilterOptionsLookbackFrom(monitorWindow, Date.now()),- },- ],+ /** filterOptionsLookbackFrom lower-bounds discovery at max(20Γwindow, 7d) so even a small monitor window still yields value suggestions. */+ const filterOptionsLookbackFrom = useMemo(+ () => getMonitorFilterOptionsLookbackFrom(monitorWindow, Date.now()),[monitorWindow],);- /** eventsFilterOptions loads the events v2 filter dictionary (environments, tags, models, β¦) for the picked view. */- const eventsFilterOptions = api.events.filterOptions.useQuery(- {- projectId,- startTimeFilter: filterOptionsStartTimeFilter,- },- {- trpc: { context: { skipBatch: true } },- staleTime: 60 * 1000,- refetchOnWindowFocus: false,- refetchOnReconnect: false,- },- );-- /** datasets loads dataset metadata for the project; used to label experiment-dataset filter options. */- const datasets = api.datasets.allDatasetMeta.useQuery({ projectId });-/** monitorFilterOptions loads the project's existing monitor tags for the tag picker's available-options list. */const monitorFilterOptions = api.monitors.getFilterOptions.useQuery({ projectId },{ staleTime: Infinity, refetchOnWindowFocus: false },);- /** filterColumnsParams collects the filter-column descriptor for InlineFilterBuilder, derived from the picked view and live option dictionaries. */- const filterColumnsParams = useMemo(- () =>- buildFilterColumnsParams({- view: (watched.view ?? "observations") as MonitorView,- filterOptions: eventsFilterOptions.data,- datasets: datasets.data,- }),- [eventsFilterOptions.data, datasets.data, watched.view],- );-- /** filterColumns is the InlineFilterBuilder column schema for the picked view. */- const filterColumns = useMemo(- () =>- getValidMonitorFilterColumns(getWidgetFilterColumns(filterColumnsParams)),- [filterColumnsParams],- );-- /** customSelectColumnIds is the set of filter columns that render a custom select control. */- const customSelectColumnIds = useMemo(- () => getWidgetColumnsWithCustomSelect(filterColumnsParams),- [filterColumnsParams],- );-/** measureOptions is the list of measure names available on the currently picked view. */const measureOptions = useMemo(() => {const view = (watched.view ??@@ -418,15 +306,15 @@ export const MonitorForm = ({namePlaceholderRef.current = namePlaceholder;- /** previewFilters translates the UI-table column filters into the view's dimension space for the preview query. */+ /** previewFilters strips unsupported rows from the picked view's filters for the preview query. */const previewFilters = useMemo<FilterState>(() =>- mapWidgetUiTableFilterToView(+ partitionWidgetUiTableFiltersToView((watched.view ?? "observations") as Parameters<- typeof mapWidgetUiTableFilterToView+ typeof partitionWidgetUiTableFiltersToView>[0],(watched.filters ?? []) as FilterState,- ),+ ).mappedFilters,[watched.view, watched.filters],);@@ -466,29 +354,15 @@ export const MonitorForm = ({value={field.value}onValueChange={(next) => {field.onChange(next);- // Reset the metric whenever the view changes: the- // selected measure may not exist on the new view,- // which would leave the form in an unsubmittable- // state until the user manually picks one. "count"- // is always present and is the safe default.- const view =- next as keyof (typeof viewDeclarations)["v2"];- const measures =- viewDeclarations.v2[view]?.measures ?? {};- const currentMeasure =- form.getValues("metric.measure");- if (!(currentMeasure in measures)) {- form.setValue(- "metric",- { measure: "count", aggregation: "count" },- { shouldValidate: true },- );+ const patch = resolveViewChangePatch(+ next as keyof (typeof viewDeclarations)["v2"],+ form.getValues("metric.measure"),+ );+ if (patch.metric) {+ form.setValue("metric", patch.metric, {+ shouldValidate: true,+ });}- // Clear filters too β UI-cased column ids only resolve- // against the view that's currently selected.- form.setValue("filters", [], {- shouldValidate: true,- });}}disabled={!hasAccess}>@@ -612,11 +486,13 @@ export const MonitorForm = ({<FormItem><FormLabel>Filters</FormLabel><FormControl>- <InlineFilterBuilder- columns={filterColumns}- filterState={(field.value ?? []) as FilterState}+ <MetricsFilterBuilder+ version="v2"+ view={(watched.view ?? "observations") as MonitorView}+ projectId={projectId}+ dateRange={{ from: filterOptionsLookbackFrom }}+ filters={(field.value ?? []) as FilterState}onChange={(next: FilterState) => field.onChange(next)}- columnsWithCustomSelect={customSelectColumnIds}/></FormControl><FormMessage />@@ -1140,5 +1016,5 @@ export const __test = {createDefaults,monitorToDefaults,nameOrPlaceholder,- buildFilterColumnsParams,+ resolveViewChangePatch,};diff --git a/web/src/features/widgets/components/WidgetForm.tsx b/web/src/features/widgets/components/WidgetForm.tsxindex 04acd5ae10..0f4750635c 100644--- a/web/src/features/widgets/components/WidgetForm.tsx+++ b/web/src/features/widgets/components/WidgetForm.tsx@@ -7,7 +7,8 @@ import {CardFooter,} from "@/src/components/ui/card";import { api } from "@/src/utils/api";-import { importWidgetFile } from "@/src/features/widgets/utils/import-export-utils";+import { WidgetImporter } from "@/src/features/widgets/components/WidgetImporter";+import { type ImportedWidgetFormSnapshot } from "@/src/features/widgets/utils/import-export-utils";import {buildWidgetOrderBy,getResultUnit,@@ -53,26 +54,18 @@ import { useV4Beta } from "@/src/features/events/hooks/useV4Beta";import { Input } from "@/src/components/ui/input";import startCase from "lodash/startCase";import { DatePickerWithRange } from "@/src/components/date-picker";-import { InlineFilterBuilder } from "@/src/features/filters/components/filter-builder";+import { MetricsFilterBuilder } from "@/src/features/metrics/components/MetricsFilterBuilder";import { useDashboardDateRange } from "@/src/hooks/useDashboardDateRange";import {toAbsoluteTimeRange,type DashboardDateRangeOptions,} from "@/src/utils/date-range-utils";-import { normalizeSingleValueOptions } from "@/src/features/filters/lib/filter-transform";-import { sortOptionValues } from "@/src/features/filters/lib/option-sort";import { Chart } from "@/src/features/widgets/chart-library/Chart";import { type DataPoint } from "@/src/features/widgets/chart-library/chart-props";import { Button } from "@/src/components/ui/button";import { type DashboardWidgetChartType } from "@langfuse/shared/src/db";import { showErrorToast } from "@/src/features/notifications/showErrorToast";-import { showSuccessToast } from "@/src/features/notifications/showSuccessToast";-import {- type FilterState,- type TimeFilter,- ObservationLevelDomain,- ObservationTypeDomain,-} from "@langfuse/shared";+import { type FilterState } from "@langfuse/shared";import { isTimeSeriesChart } from "@/src/features/widgets/chart-library/utils";import {BarChart,@@ -85,7 +78,6 @@ import {Plus,X,AlertCircle,- Upload,Sparkles,} from "lucide-react";import {@@ -108,10 +100,6 @@ import {getChartLoadingProgress,getChartLoadingStateProps,} from "@/src/features/widgets/chart-library/chartLoadingStateUtils";-import {- getWidgetColumnsWithCustomSelect,- getWidgetFilterColumns,-} from "./widgetFilterColumns";import { WIDGET_FILTER_PRESETS } from "@/src/features/widgets/constants/widgetFilterPresets";import { useCaptureWidgetHighCardinalityError } from "@/src/features/widgets/hooks/useWidgetQueryErrorCapture";import {@@ -144,17 +132,6 @@ type ChartType = {icon: React.ElementType;};-const getDateRangeFilter = (- column: "timestamp" | "startTime",- dateRange?: { from: Date; to: Date },-): TimeFilter[] | undefined =>- dateRange- ? [- { column, type: "datetime", operator: ">=", value: dateRange.from },- { column, type: "datetime", operator: "<=", value: dateRange.to },- ]- : undefined;-// chartTypes drives the chart-type SelectGroup rendering (group/name/value/// icon). Whether a type supports a breakdown dimension is derived on demand via// widgetChartTypeSupportsBreakdown(chartType) at each gate, not stored here.@@ -209,13 +186,6 @@ const chartTypes: ChartType[] = [},];-const observationLevelOptions = ObservationLevelDomain.options.map((value) => ({- value,-}));-const observationTypeOptions = ObservationTypeDomain.options.map((value) => ({- value,-}));-/*** A small read-only context passed DOWN to field subcomponents. It lets a field* render meta (descriptions/units) and gate options without any child ever@@ -253,7 +223,6 @@ export function WidgetForm({widgetId?: string;}) {const { isBetaEnabled } = useV4Beta();- const importInputRef = useRef<HTMLInputElement>(null);// The initial widget's persisted version is a local hint frozen at mount.// The resolver and live viewVersion additionally derive v2 from the current@@ -406,7 +375,6 @@ export function WidgetForm({setTimeRange({ range: option });}};-const unsupportedFilters = useMemo(() =>partitionWidgetUiTableFiltersToView(selectedView, values.filters)@@ -425,143 +393,6 @@ export function WidgetForm({[selectedView, values.filters],);- // v1: traces/generations filter options (old normalized tables)- const traceFilterOptions = api.traces.filterOptions.useQuery(- {- projectId,- timestampFilter: getDateRangeFilter("timestamp", dateRange),- },- {- trpc: { context: { skipBatch: true } },- refetchOnMount: false,- refetchOnWindowFocus: false,- refetchOnReconnect: false,- staleTime: Infinity,- enabled: viewVersion === "v1",- },- );- const generationsFilterOptions = api.generations.filterOptions.useQuery(- {- projectId,- startTimeFilter: getDateRangeFilter("startTime", dateRange),- observationType: "ALL",- },- {- trpc: { context: { skipBatch: true } },- refetchOnMount: false,- refetchOnWindowFocus: false,- refetchOnReconnect: false,- staleTime: Infinity,- enabled: viewVersion === "v1",- },- );- const eventsFilterOptions = api.events.filterOptions.useQuery(- {- projectId,- startTimeFilter: getDateRangeFilter("startTime", dateRange),- },- {- trpc: { context: { skipBatch: true } },- refetchOnMount: false,- refetchOnWindowFocus: false,- refetchOnReconnect: false,- staleTime: Infinity,- enabled: viewVersion === "v2",- },- );- const environmentFilterOptions =- api.projects.environmentFilterOptions.useQuery(- {- projectId,- fromTimestamp: dateRange?.from,- },- {- trpc: { context: { skipBatch: true } },- refetchOnMount: false,- refetchOnWindowFocus: false,- refetchOnReconnect: false,- staleTime: Infinity,- enabled: viewVersion === "v1",- },- );- const datasets = api.datasets.allDatasetMeta.useQuery(- { projectId },- { enabled: viewVersion === "v2" },- );-- // Resolve filter options based on viewVersion- const environmentOptions =- viewVersion === "v2"- ? eventsFilterOptions.data?.environment || []- : environmentFilterOptions.data?.map((value) => ({- value: value.environment,- })) || [];- const nameOptions =- viewVersion === "v2"- ? normalizeSingleValueOptions(eventsFilterOptions.data?.traceName)- : normalizeSingleValueOptions(traceFilterOptions.data?.name);- const observationNameOptions =- viewVersion === "v2"- ? normalizeSingleValueOptions(eventsFilterOptions.data?.name)- : normalizeSingleValueOptions(generationsFilterOptions.data?.name);- const tagsOptions = sortOptionValues(- viewVersion === "v2"- ? eventsFilterOptions.data?.traceTags || []- : traceFilterOptions.data?.tags || [],- );- const modelOptions =- viewVersion === "v2"- ? eventsFilterOptions.data?.providedModelName || []- : generationsFilterOptions.data?.model || [];- const toolNamesOptions =- viewVersion === "v2"- ? eventsFilterOptions.data?.toolNames || []- : generationsFilterOptions.data?.toolNames || [];- const calledToolNamesOptions =- viewVersion === "v2"- ? eventsFilterOptions.data?.calledToolNames || []- : generationsFilterOptions.data?.calledToolNames || [];- const experimentNameOptions =- viewVersion === "v2" ? eventsFilterOptions.data?.experimentName || [] : [];- const experimentDatasetIdSet = new Set(- eventsFilterOptions.data?.experimentDatasetId?.map((e) => e.value),- );- const experimentDatasetIdOptions =- datasets.data- ?.filter((d) => experimentDatasetIdSet.has(d.id))- .map((d) => ({ value: d.id, displayValue: d.name })) ?? [];-- const filterColumnsParams = {- selectedView,- viewVersion,- environmentOptions,- nameOptions,- observationNameOptions,- tagsOptions,- modelOptions,- toolNamesOptions,- calledToolNamesOptions,- observationLevelOptions,- experimentNameOptions,- experimentDatasetOptions: experimentDatasetIdOptions,- observationTypeOptions,- };- const filterColumns = getWidgetFilterColumns(filterColumnsParams);- const columnsWithCustomSelect =- getWidgetColumnsWithCustomSelect(filterColumnsParams);-- const getValidFilterColumnIds = (- view: z.infer<typeof views>,- version: ViewVersion,- ): Set<string> => {- const columns = getWidgetFilterColumns({- ...filterColumnsParams,- selectedView: view,- viewVersion: version,- });- return new Set(columns.flatMap((col) => [col.id, col.name]));- };-// Available measures for the single (non-pivot) metric picker.const singleChartMetrics = useMemo(() => {const measures = viewDeclarations[viewVersion][selectedView].measures;@@ -902,16 +733,13 @@ export function WidgetForm({dimensions = [];}- const validColumns = getValidFilterColumnIds(newView, newViewVersion);- const filters = values.filters.filter((filter) =>- validColumns.has(filter.column),- );-+ // Filters are kept across view changes; MetricsFilterBuilder surfaces any+ // now-invalid rows in its banner rather than silently dropping them.const candidate = normalizeWidgetFormValues(- { ...values, view: newView, metrics, dimensions, filters },+ { ...values, view: newView, metrics, dimensions },newViewVersion,);- commitHealed(candidate, { view: true, filters: true });+ commitHealed(candidate, { view: true });};// Chart-type change (ports breakdown-wipe / pivot-dims-reset / trim-metrics@@ -944,125 +772,60 @@ export function WidgetForm({form.setValue("filters", [...preset.filters], { shouldValidate: true });};- const handleImportWidget = async (- event: React.ChangeEvent<HTMLInputElement>,- ) => {- const file = event.target.files?.[0];- event.target.value = "";- if (!file) return;-- const showMalformedImportToast = () =>- showErrorToast(- "Malformed input",- "This operation can't be done due to the malformed input",- "WARNING",- );-- try {- const result = await importWidgetFile({- file,- optionSets: {- environmentValues: environmentFilterOptions.data?.map(- (option) => option.environment,- ),- traceNames: traceFilterOptions.data- ? normalizeSingleValueOptions(traceFilterOptions.data.name).map(- (option) => option.value,- )- : undefined,- tags: traceFilterOptions.data- ? traceFilterOptions.data.tags.map((option) => option.value)- : undefined,- toolNames: generationsFilterOptions.data- ? generationsFilterOptions.data.toolNames.map(- (option) => option.value,- )- : undefined,- calledToolNames: generationsFilterOptions.data- ? generationsFilterOptions.data.calledToolNames.map(- (option) => option.value,- )- : undefined,- modelNames: generationsFilterOptions.data- ? generationsFilterOptions.data.model.map((option) => option.value)- : undefined,- observationLevels: observationLevelOptions.map(- (option) => option.value,- ),- },- isBetaEnabled,- });-- const snapshot = result.snapshot;- const importIsPivot = snapshot.selectedChartType === "PIVOT_TABLE";- // The preview version for the imported widget is re-derived from the- // snapshot's own version hint (not the mount's).- const importViewVersion = resolveWidgetFormVersion({- view: snapshot.selectedView,- baseMinVersion: snapshot.widgetMinVersion,- activeVersion,- });- // Explicit user-event reset (allowed β not an effect). The imported name- // is a non-empty override, so it sticks and does not auto-update. The- // snapshot's filters are already in editor space, so they are seeded- // directly (no re-normalization); the chart/aggregation/dimension shape- // is healed via normalizeWidgetFormValues so a malformed import mounts- // valid β the same healing the legacy import path's mount effects did.- form.reset(- normalizeWidgetFormValues(- {- name: snapshot.widgetName || null,- description: snapshot.widgetDescription || null,- view: snapshot.selectedView,- filters: snapshot.userFilterState,- metrics: importIsPivot- ? snapshot.selectedMetrics.map((m) => ({- measure: m.measure,- aggregation: m.aggregation,- }))- : [- {- measure: snapshot.selectedMeasure,- aggregation: snapshot.selectedAggregation,- },- ],- dimensions: importIsPivot- ? snapshot.pivotDimensions.map((field) => ({ field }))- : snapshot.selectedDimension !== "none"- ? [{ field: snapshot.selectedDimension }]- : [],- chart: {- type: snapshot.selectedChartType,- bins: snapshot.histogramBins,- rowLimit: snapshot.rowLimit,- sort:- snapshot.defaultSortColumn !== "none"- ? {- column: snapshot.defaultSortColumn,- order: snapshot.defaultSortOrder,- }- : null,- },+ const handleImportedWidget = (snapshot: ImportedWidgetFormSnapshot) => {+ const importIsPivot = snapshot.selectedChartType === "PIVOT_TABLE";+ // The preview version for the imported widget is re-derived from the+ // snapshot's own version hint (not the mount's).+ const importViewVersion = resolveWidgetFormVersion({+ view: snapshot.selectedView,+ baseMinVersion: snapshot.widgetMinVersion,+ activeVersion,+ });+ // Explicit user-event reset (allowed β not an effect). The imported name+ // is a non-empty override, so it sticks and does not auto-update. The+ // snapshot's filters are already in editor space, so they are seeded+ // directly (no re-normalization); the chart/aggregation/dimension shape+ // is healed via normalizeWidgetFormValues so a malformed import mounts+ // valid β the same healing the legacy import path's mount effects did.+ form.reset(+ normalizeWidgetFormValues(+ {+ name: snapshot.widgetName || null,+ description: snapshot.widgetDescription || null,+ view: snapshot.selectedView,+ filters: snapshot.userFilterState,+ metrics: importIsPivot+ ? snapshot.selectedMetrics.map((m) => ({+ measure: m.measure,+ aggregation: m.aggregation,+ }))+ : [+ {+ measure: snapshot.selectedMeasure,+ aggregation: snapshot.selectedAggregation,+ },+ ],+ dimensions: importIsPivot+ ? snapshot.pivotDimensions.map((field) => ({ field }))+ : snapshot.selectedDimension !== "none"+ ? [{ field: snapshot.selectedDimension }]+ : [],+ chart: {+ type: snapshot.selectedChartType,+ bins: snapshot.histogramBins,+ rowLimit: snapshot.rowLimit,+ sort:+ snapshot.defaultSortColumn !== "none"+ ? {+ column: snapshot.defaultSortColumn,+ order: snapshot.defaultSortOrder,+ }+ : null,},- importViewVersion,- ),- );-- showSuccessToast({- title: "Widget uploaded successfully",- description: "Widget configuration has been loaded.",- });-- if (result.removedValues || result.removedFilters) {- showErrorToast(- "Widget filters were adjusted",- "Some imported filters or filter values were removed because they are not available in this project.",- "WARNING",- );- }- } catch {- showMalformedImportToast();- }+ },+ importViewVersion,+ ),+ );};const onSubmit = form.handleSubmit((submitted) => {@@ -1108,23 +871,13 @@ export function WidgetForm({<div className="flex items-start justify-between gap-3"><CardTitle>Widget Configuration</CardTitle>{!widgetId && isBetaEnabled && (- <>- <input- ref={importInputRef}- type="file"- accept="application/json,.json"- className="hidden"- onChange={handleImportWidget}- />- <Button- variant="outline"- size="sm"- onClick={() => importInputRef.current?.click()}- >- <Upload className="mr-2 h-4 w-4" />- Import- </Button>- </>+ <WidgetImporter+ projectId={projectId}+ viewVersion={viewVersion}+ dateRange={dateRange}+ isBetaEnabled={isBetaEnabled}+ onImport={handleImportedWidget}+ />)}</div><CardDescription>@@ -1217,10 +970,9 @@ export function WidgetForm({<FiltersFieldcontrol={form.control}- filterColumns={filterColumns}- columnsWithCustomSelect={columnsWithCustomSelect}- unsupportedFilters={unsupportedFilters}- unsupportedFilterColumns={unsupportedFilterColumns}+ projectId={projectId}+ viewVersion={viewVersion}+ dateRange={dateRange}selectedView={selectedView}/>@@ -1774,45 +1526,31 @@ function PivotMetricsField({function FiltersField({control,- filterColumns,- columnsWithCustomSelect,- unsupportedFilters,- unsupportedFilterColumns,+ projectId,+ viewVersion,+ dateRange,selectedView,}: {control: Control<WidgetFormValues>;- filterColumns: ReturnType<typeof getWidgetFilterColumns>;- columnsWithCustomSelect: ReturnType<typeof getWidgetColumnsWithCustomSelect>;- unsupportedFilters: FilterState;- unsupportedFilterColumns: string;+ projectId: string;+ viewVersion: ViewVersion;+ dateRange: { from: Date; to: Date } | undefined;selectedView: z.infer<typeof views>;}) {+ // MetricsFilterBuilder owns the column schema, the UI-table translation, and+ // the unsupported-column banner.const { field } = useController({ control, name: "filters" });return (<div className="space-y-2"><Label>Filters</Label>- <div className="space-y-2">- {unsupportedFilters.length > 0 && (- <Alert- variant="default"- className="border-yellow-500/50 bg-yellow-50 dark:bg-yellow-950/20"- >- <AlertCircle className="h-4 w-4 text-yellow-600 dark:text-yellow-500" />- <AlertTitle className="text-yellow-800 dark:text-yellow-400">- Unsupported legacy filters- </AlertTitle>- <AlertDescription className="text-yellow-700 dark:text-yellow-500">- {`This widget still contains filter columns that are not supported for ${startCase(selectedView)}: ${unsupportedFilterColumns}. Remove them or switch to a compatible view before saving.`}- </AlertDescription>- </Alert>- )}- <InlineFilterBuilder- columns={filterColumns}- filterState={field.value}- onChange={(next: FilterState) => field.onChange(next)}- columnsWithCustomSelect={columnsWithCustomSelect}- />- </div>+ <MetricsFilterBuilder+ version={viewVersion}+ view={selectedView}+ projectId={projectId}+ dateRange={dateRange}+ filters={field.value}+ onChange={(next: FilterState) => field.onChange(next)}+ /></div>);}diff --git a/web/src/features/widgets/components/WidgetImporter.clienttest.tsx b/web/src/features/widgets/components/WidgetImporter.clienttest.tsxnew file mode 100644index 0000000000..09933d6f8c--- /dev/null+++ b/web/src/features/widgets/components/WidgetImporter.clienttest.tsx@@ -0,0 +1,111 @@+import { describe, expect, it, vi } from "vitest";++import { showErrorToast } from "@/src/features/notifications/showErrorToast";+import { showSuccessToast } from "@/src/features/notifications/showSuccessToast";++import { __test } from "./WidgetImporter";++vi.mock("@/src/features/notifications/showErrorToast", () => ({+ showErrorToast: vi.fn(),+}));+vi.mock("@/src/features/notifications/showSuccessToast", () => ({+ showSuccessToast: vi.fn(),+}));++const { buildImportOptionSets, runImport } = __test;++const baseWidget = {+ name: "Imported widget",+ description: "",+ view: "traces" as const,+ dimensions: [],+ metrics: [{ measure: "count", agg: "count" as const }],+ filters: [] as unknown[],+ chartType: "NUMBER" as const,+ chartConfig: { type: "NUMBER" as const },+ minVersion: 1,+};++const fileFor = (json: unknown): File =>+ ({ text: async () => JSON.stringify(json) }) as File;++describe("buildImportOptionSets", () => {+ it("gates to only observationLevels when the v1 queries are disabled (no data)", () => {+ const optionSets = buildImportOptionSets({+ environmentFilterOptionsData: undefined,+ traceFilterOptionsData: undefined,+ generationsFilterOptionsData: undefined,+ });++ expect(optionSets.environmentValues).toBeUndefined();+ expect(optionSets.traceNames).toBeUndefined();+ expect(optionSets.tags).toBeUndefined();+ expect(optionSets.toolNames).toBeUndefined();+ expect(optionSets.calledToolNames).toBeUndefined();+ expect(optionSets.modelNames).toBeUndefined();+ expect(optionSets.observationLevels.length).toBeGreaterThan(0);+ });+});++describe("runImport", () => {+ it("calls onImport with the snapshot on success", async () => {+ const onImport = vi.fn();++ await runImport({+ file: fileFor({ ...baseWidget, filters: [] }),+ optionSets: { observationLevels: [] },+ isBetaEnabled: false,+ onImport,+ });++ expect(onImport).toHaveBeenCalledTimes(1);+ expect(onImport.mock.calls[0][0].widgetName).toBe("Imported widget");+ expect(showSuccessToast).toHaveBeenCalledTimes(1);+ });++ it("skips onImport and surfaces the malformed toast on invalid input", async () => {+ const onImport = vi.fn();++ await runImport({+ file: { text: async () => "not json" } as File,+ optionSets: { observationLevels: [] },+ isBetaEnabled: false,+ onImport,+ });++ expect(onImport).not.toHaveBeenCalled();+ expect(showErrorToast).toHaveBeenCalledWith(+ "Malformed input",+ expect.any(String),+ "WARNING",+ );+ });++ it("fires the adjusted toast when values are pruned", async () => {+ const onImport = vi.fn();++ await runImport({+ file: fileFor({+ ...baseWidget,+ filters: [+ {+ column: "environment",+ operator: "any of",+ value: ["prod"],+ type: "stringOptions",+ },+ ],+ }),+ optionSets: { observationLevels: [], environmentValues: [] },+ isBetaEnabled: false,+ onImport,+ });++ expect(onImport).toHaveBeenCalledTimes(1);+ expect(showErrorToast).toHaveBeenCalledWith(+ "Widget filters were adjusted",+ expect.any(String),+ "WARNING",+ );+ });+});diff --git a/web/src/features/widgets/components/WidgetImporter.tsx b/web/src/features/widgets/components/WidgetImporter.tsxnew file mode 100644index 0000000000..2f52b2e37a--- /dev/null+++ b/web/src/features/widgets/components/WidgetImporter.tsx@@ -0,0 +1,224 @@+import React, { useRef } from "react";+import { Upload } from "lucide-react";++import { type ViewVersion } from "@langfuse/shared/query";+import { type TimeFilter, ObservationLevelDomain } from "@langfuse/shared";+import { api, type RouterOutputs } from "@/src/utils/api";+import { Button } from "@/src/components/ui/button";+import { normalizeSingleValueOptions } from "@/src/features/filters/lib/filter-transform";+import { showErrorToast } from "@/src/features/notifications/showErrorToast";+import { showSuccessToast } from "@/src/features/notifications/showSuccessToast";+import {+ importWidgetFile,+ type ImportedWidgetFormSnapshot,+ type WidgetImportOptionSets,+} from "@/src/features/widgets/utils/import-export-utils";++/** observationLevelOptions is the static set of observation levels always offered on import. */+const observationLevelOptions = ObservationLevelDomain.options.map((value) => ({+ value,+}));++/** WidgetImporter loads a widget JSON export into a form snapshot, owning the file trigger, its v1 option queries, and all toasts. */+export const WidgetImporter = ({+ projectId,+ viewVersion,+ dateRange,+ isBetaEnabled,+ onImport,+}: {+ projectId: string;+ viewVersion: ViewVersion;+ dateRange: { from: Date; to: Date } | undefined;+ isBetaEnabled: boolean;+ onImport: (snapshot: ImportedWidgetFormSnapshot) => void;+}) => {+ const importInputRef = useRef<HTMLInputElement>(null);+ const utils = api.useUtils();++ const handleImportWidget = async (+ event: React.ChangeEvent<HTMLInputElement>,+ ) => {+ const file = event.target.files?.[0];+ event.target.value = "";++ if (!file) {+ return;+ }++ const optionSets = await fetchImportOptionSets({+ utils,+ projectId,+ viewVersion,+ dateRange,+ });++ await runImport({ file, optionSets, isBetaEnabled, onImport });+ };++ return (+ <>+ <input+ ref={importInputRef}+ type="file"+ accept="application/json,.json"+ className="hidden"+ onChange={handleImportWidget}+ />+ <Button+ variant="outline"+ size="sm"+ onClick={() => importInputRef.current?.click()}+ >+ <Upload className="mr-2 h-4 w-4" />+ Import+ </Button>+ </>+ );+};++/** fetchImportOptionSets lazily loads the v1 filter options on import, gated to v1 views, and maps them into the sanitization option sets. */+async function fetchImportOptionSets(params: {+ utils: ReturnType<typeof api.useUtils>;+ projectId: string;+ viewVersion: ViewVersion;+ dateRange: { from: Date; to: Date } | undefined;+}): Promise<WidgetImportOptionSets> {+ if (params.viewVersion !== "v1") {+ return buildImportOptionSets({+ environmentFilterOptionsData: undefined,+ traceFilterOptionsData: undefined,+ generationsFilterOptionsData: undefined,+ });+ }++ const fetchOptions = {+ trpc: { context: { skipBatch: true } },+ staleTime: Infinity,+ };++ const [+ traceFilterOptionsData,+ generationsFilterOptionsData,+ environmentFilterOptionsData,+ ] = await Promise.all([+ params.utils.traces.filterOptions.fetch(+ {+ projectId: params.projectId,+ timestampFilter: getDateRangeFilter("timestamp", params.dateRange),+ },+ fetchOptions,+ ),+ params.utils.generations.filterOptions.fetch(+ {+ projectId: params.projectId,+ startTimeFilter: getDateRangeFilter("startTime", params.dateRange),+ observationType: "ALL",+ },+ fetchOptions,+ ),+ params.utils.projects.environmentFilterOptions.fetch(+ {+ projectId: params.projectId,+ fromTimestamp: params.dateRange?.from,+ },+ fetchOptions,+ ),+ ]);++ return buildImportOptionSets({+ environmentFilterOptionsData,+ traceFilterOptionsData,+ generationsFilterOptionsData,+ });+}++/** buildImportOptionSets maps the v1 filter-option query data into the allowed-value sets for import sanitization. */+function buildImportOptionSets(params: {+ environmentFilterOptionsData:+ | RouterOutputs["projects"]["environmentFilterOptions"]+ | undefined;+ traceFilterOptionsData: RouterOutputs["traces"]["filterOptions"] | undefined;+ generationsFilterOptionsData:+ | RouterOutputs["generations"]["filterOptions"]+ | undefined;+}): WidgetImportOptionSets {+ return {+ environmentValues: params.environmentFilterOptionsData?.map(+ (option) => option.environment,+ ),+ traceNames: params.traceFilterOptionsData+ ? normalizeSingleValueOptions(params.traceFilterOptionsData.name).map(+ (option) => option.value,+ )+ : undefined,+ tags: params.traceFilterOptionsData+ ? params.traceFilterOptionsData.tags.map((option) => option.value)+ : undefined,+ toolNames: params.generationsFilterOptionsData+ ? params.generationsFilterOptionsData.toolNames.map(+ (option) => option.value,+ )+ : undefined,+ calledToolNames: params.generationsFilterOptionsData+ ? params.generationsFilterOptionsData.calledToolNames.map(+ (option) => option.value,+ )+ : undefined,+ modelNames: params.generationsFilterOptionsData+ ? params.generationsFilterOptionsData.model.map((option) => option.value)+ : undefined,+ observationLevels: observationLevelOptions.map((option) => option.value),+ };+}++/** runImport parses the file into a snapshot, applies it via onImport, and raises the malformed, success, and adjusted toasts. */+async function runImport(params: {+ file: File;+ optionSets: WidgetImportOptionSets;+ isBetaEnabled: boolean;+ onImport: (snapshot: ImportedWidgetFormSnapshot) => void;+}): Promise<void> {+ try {+ const result = await importWidgetFile({+ file: params.file,+ optionSets: params.optionSets,+ isBetaEnabled: params.isBetaEnabled,+ });++ params.onImport(result.snapshot);++ showSuccessToast({+ title: "Widget uploaded successfully",+ description: "Widget configuration has been loaded.",+ });++ if (result.removedValues || result.removedFilters) {+ showErrorToast(+ "Widget filters were adjusted",+ "Some imported filters or filter values were removed because they are not available in this project.",+ "WARNING",+ );+ }+ } catch {+ showErrorToast(+ "Malformed input",+ "This operation can't be done due to the malformed input",+ "WARNING",+ );+ }+}++/** getDateRangeFilter builds a bounded time filter for a column from a date range. */+const getDateRangeFilter = (+ column: "timestamp" | "startTime",+ dateRange?: { from: Date; to: Date },+): TimeFilter[] | undefined =>+ dateRange+ ? [+ { column, type: "datetime", operator: ">=", value: dateRange.from },+ { column, type: "datetime", operator: "<=", value: dateRange.to },+ ]+ : undefined;++export const __test = { buildImportOptionSets, runImport };