Skip to content

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

refactor(filters): relocate the observed-metadata suggestions out of the search bar (LFE-11030) (#15881)

Nikita Kabardine66271a7b088
diff --git a/web/src/features/events/components/EventsTable.tsx b/web/src/features/events/components/EventsTable.tsx
index 28e4dc9eb7..041b62d7ed 100644
--- a/web/src/features/events/components/EventsTable.tsx
+++ b/web/src/features/events/components/EventsTable.tsx
@@ -129,6 +129,7 @@ import { buildAiContext } from "@/src/features/search-bar/lib/ai-context";
import {
observedScoreNamesFromOptions,
toObservedOptions,
+ withMetadataPathOptions,
} from "@/src/features/search-bar/lib/observed-options";
import { CategoryPresetChips } from "@/src/features/events/components/CategoryPresetChips";
import { TableViewPresetsDrawer } from "@/src/components/table/table-view-presets/components/data-table-view-presets-drawer";
@@ -141,12 +142,11 @@ import {
chartSearchFieldReason,
CHART_SEARCH_QUERY_REASON,
} from "@/src/features/chart-view/lib/chartFilterCompatibility";
-import { withMetadataPathOptions } from "@/src/features/search-bar/lib/metadata-paths";
import { getEventsTableStatePolicy } from "@/src/features/events/lib/eventsTableStatePolicy";
import {
useObservedMetadataPaths,
useObservedMetadataRecorder,
-} from "@/src/features/search-bar/hooks/useObservedMetadata";
+} from "@/src/hooks/useObservedMetadata";
export type EventsTableRow = {
// Identity fields
diff --git a/web/src/features/search-bar/README.md b/web/src/features/search-bar/README.md
index f7b26dee77..2828452aa0 100644
--- a/web/src/features/search-bar/README.md
+++ b/web/src/features/search-bar/README.md
@@ -184,9 +184,9 @@ committedText ──resetTo──▶ store.draft ──(type/pick/remove)──
committed text — the derive direction), `completions.ts` (pure completion
planner), `composer-segments.ts` (draft text → renderable token segments),
`edits.ts` (span-local chip removal with AST-surgery fallback),
- `observed-options.ts` (filterOptions → per-column observed values),
- `metadata-paths.ts` (client-side metadata structure analysis; see "Metadata
- key suggestions"), `searchBarInvariants.ts` (pure, registry-shaped
+ `observed-options.ts` (filterOptions → per-column observed values, plus
+ `withMetadataPathOptions` — see "Metadata key suggestions"),
+ `searchBarInvariants.ts` (pure, registry-shaped
property-test harness — the universal safety net reused per view; see
Hardening).
- `store/searchBarStore.ts` — per-mount vanilla zustand store, **draft only**
@@ -194,9 +194,12 @@ committedText ──resetTo──▶ store.draft ──(type/pick/remove)──
no commit workflow. Provided with the container's `commit` via
`store/SearchBarStoreProvider.tsx` (`useSearchBarStore` selector,
`useSearchBarCommit`).
-- `store/observedMetadataStore.ts` — global zustand store persisted to
- localStorage: per-project map of observed metadata keys → types (the
- suggestions cache behind "Metadata key suggestions" below).
+- The observed-metadata suggestions cache is **shared with the filter sidebar**,
+ so it lives outside this feature: `src/stores/observedMetadataStore.ts`
+ (global zustand store persisted to localStorage, per-project map of observed
+ metadata keys → types), `src/fns/observedMetadata/metadataPaths.ts` (the pure
+ analysis + option projection) and `src/hooks/useObservedMetadata.ts` (the
+ harvest/read bridge). See "Metadata key suggestions" below.
- `hooks/useEventsSearchBar.ts` — the container/bridge. Derives `committedText`
(memo), runs the one `resetTo` effect, and owns the `commit()` workflow
(planCommit → write filter state + record recent). No URL param of its own;
@@ -309,11 +312,11 @@ The API does not enumerate metadata keys, and backend metadata-structure
analysis is deferred (until CH26), so `metadata.` completions are fed
**client-side from rows the user has already loaded**:
-- On each fetch, `hooks/useObservedMetadata.ts` samples the visible rows'
+- On each fetch, `src/hooks/useObservedMetadata.ts` samples the visible rows'
metadata (same first-30 sampling as the AI-context path), records their
**top-level keys** with the observed JSON value type
- (`lib/metadata-paths.ts`), and unions the result into
- `store/observedMetadataStore.ts` — persisted to localStorage, **per
+ (`src/fns/observedMetadata/metadataPaths.ts`), and unions the result into
+ `src/stores/observedMetadataStore.ts` — persisted to localStorage, **per
project** (one global `Record<projectId, …>` map, the globalDateRangeStore
shape).
- `EventsTable` merges the project's map into the observed options where the
diff --git a/web/src/features/search-bar/lib/observed-options.clienttest.ts b/web/src/features/search-bar/lib/observed-options.clienttest.ts
index fca98432c3..08cc10a39b 100644
--- a/web/src/features/search-bar/lib/observed-options.clienttest.ts
+++ b/web/src/features/search-bar/lib/observed-options.clienttest.ts
@@ -4,6 +4,8 @@ import {
MAX_SCORE_NAME_LENGTH,
MAX_SCORE_NAMES_PER_TYPE,
observedScoreNamesFromOptions,
+ withMetadataPathOptions,
+ type ObservedOptions,
} from "./observed-options";
describe("observedScoreNamesFromOptions", () => {
@@ -50,3 +52,27 @@ describe("observedScoreNamesFromOptions", () => {
expect(names?.numeric).toBeUndefined();
});
});
+
+describe("withMetadataPathOptions", () => {
+ const observed: ObservedOptions = { level: [{ value: "ERROR" }] };
+
+ it("keeps undefined observed undefined (loading semantics untouched)", () => {
+ expect(
+ withMetadataPathOptions(undefined, { a: { type: "string" } }),
+ ).toBeUndefined();
+ });
+
+ it("returns the observed map unchanged without observed paths", () => {
+ expect(withMetadataPathOptions(observed, undefined)).toBe(observed);
+ expect(withMetadataPathOptions(observed, {})).toBe(observed);
+ });
+
+ it("merges the metadata suggestions in, leaving other columns alone", () => {
+ const out = withMetadataPathOptions(observed, {
+ region: { type: "string", values: ["eu"] },
+ });
+ expect(out?.level).toBe(observed.level);
+ expect(out?.metadata).toEqual([{ value: "region", type: "string" }]);
+ expect(out?.["metadata.region"]).toEqual([{ value: "eu" }]);
+ });
+});
diff --git a/web/src/features/search-bar/lib/observed-options.ts b/web/src/features/search-bar/lib/observed-options.ts
index b3cb98cca4..3b05b80cb5 100644
--- a/web/src/features/search-bar/lib/observed-options.ts
+++ b/web/src/features/search-bar/lib/observed-options.ts
@@ -6,11 +6,15 @@
// scores_avg / trace_scores_avg → numeric score NAMES
// score_categories[.<name>] → categorical score names / their values
// (metadata key paths are not enumerated by the API — they are merged in
-// client-side from the observed-metadata store; see lib/metadata-paths.ts
-// withMetadataPathOptions, which fills the `metadata` key)
+// client-side from the observed-metadata store by withMetadataPathOptions
+// below; see fns/observedMetadata/metadataPaths.ts)
import type { ScoreTypeContext } from "./adapter";
import { SCORE_COLUMNS } from "./fields";
+import {
+ observedMetadataOptions,
+ type StoredKeyInfo,
+} from "../../../fns/observedMetadata/metadataPaths";
export type ObservedValue = {
value: string;
@@ -101,6 +105,23 @@ export function toObservedOptions(
return out;
}
+/**
+ * Merge the observed-metadata suggestions into the map the completion planner
+ * reads (see fns/observedMetadata/metadataPaths.ts). `undefined` observed
+ * (filter options still loading) stays `undefined` so the planner's loading
+ * semantics are untouched.
+ */
+export function withMetadataPathOptions(
+ observed: ObservedOptions | undefined,
+ paths: Record<string, StoredKeyInfo> | undefined,
+): ObservedOptions | undefined {
+ if (observed === undefined) return observed;
+ const suggestions = observedMetadataOptions(paths);
+ return Object.keys(suggestions).length === 0
+ ? observed
+ : { ...observed, ...suggestions };
+}
+
/**
* Derive the score-name→type sets the adapter needs to route
* `scores.<name>:<value>` to the numeric (`scores_avg`) vs categorical
diff --git a/web/src/features/search-bar/lib/metadata-paths.clienttest.ts b/web/src/fns/observedMetadata/metadataPaths.clienttest.ts
similarity index 85%
rename from web/src/features/search-bar/lib/metadata-paths.clienttest.ts
rename to web/src/fns/observedMetadata/metadataPaths.clienttest.ts
index ad42b0e82a..fb56e5ddaa 100644
--- a/web/src/features/search-bar/lib/metadata-paths.clienttest.ts
+++ b/web/src/fns/observedMetadata/metadataPaths.clienttest.ts
@@ -5,10 +5,9 @@ import {
MAX_PATHS_PER_PROJECT,
MAX_VALUES_PER_KEY,
mergePathType,
- withMetadataPathOptions,
+ observedMetadataOptions,
type StoredKeyInfo,
-} from "./metadata-paths";
-import type { ObservedOptions } from "./observed-options";
+} from "./metadataPaths";
describe("collectMetadataPathTypes", () => {
it("records top-level keys with observed value types", () => {
@@ -163,21 +162,13 @@ describe("mergePathType", () => {
});
});
-describe("withMetadataPathOptions", () => {
- const observed: ObservedOptions = { level: [{ value: "ERROR" }] };
-
- it("keeps undefined observed undefined (loading semantics untouched)", () => {
- expect(
- withMetadataPathOptions(undefined, { a: { type: "string" } }),
- ).toBeUndefined();
- });
-
- it("returns the observed map unchanged without paths", () => {
- expect(withMetadataPathOptions(observed, undefined)).toBe(observed);
- expect(withMetadataPathOptions(observed, {})).toBe(observed);
+describe("observedMetadataOptions", () => {
+ it("is empty without observed paths, so callers can skip the merge", () => {
+ expect(observedMetadataOptions(undefined)).toEqual({});
+ expect(observedMetadataOptions({})).toEqual({});
});
- it("merges sorted keys under `metadata`, omitting mixed/unknown types", () => {
+ it("offers sorted keys under `metadata`, omitting mixed/unknown types", () => {
const paths: Record<string, StoredKeyInfo> = {
"routing.queue": { type: "string" },
hej: { type: "number" },
@@ -185,9 +176,7 @@ describe("withMetadataPathOptions", () => {
both: { type: "mixed" },
nullish: { type: "" },
};
- const out = withMetadataPathOptions(observed, paths);
- expect(out?.level).toBe(observed.level);
- expect(out?.metadata).toEqual([
+ expect(observedMetadataOptions(paths).metadata).toEqual([
{ value: "both" },
{ value: "hej", type: "number" },
{ value: "nullish" },
@@ -197,15 +186,11 @@ describe("withMetadataPathOptions", () => {
});
it("exposes per-key values under `metadata.<key>` for the value stage", () => {
- const paths: Record<string, StoredKeyInfo> = {
+ const out = observedMetadataOptions({
region: { type: "string", values: ["eu", "us"] },
scope: { type: "object" },
- };
- const out = withMetadataPathOptions(observed, paths);
- expect(out?.["metadata.region"]).toEqual([
- { value: "eu" },
- { value: "us" },
- ]);
- expect(out?.["metadata.scope"]).toBeUndefined();
+ });
+ expect(out["metadata.region"]).toEqual([{ value: "eu" }, { value: "us" }]);
+ expect(out["metadata.scope"]).toBeUndefined();
});
});
diff --git a/web/src/features/search-bar/lib/metadata-paths.ts b/web/src/fns/observedMetadata/metadataPaths.ts
similarity index 86%
rename from web/src/features/search-bar/lib/metadata-paths.ts
rename to web/src/fns/observedMetadata/metadataPaths.ts
index e0f604f602..bcfb32bc5b 100644
--- a/web/src/features/search-bar/lib/metadata-paths.ts
+++ b/web/src/fns/observedMetadata/metadataPaths.ts
@@ -4,7 +4,7 @@
// of observed keys is built OPPORTUNISTICALLY from rows the user has already
// loaded: each fetch samples the visible rows, records their TOP-LEVEL
// metadata keys with the observed JSON value type, and unions the result into
-// the persisted per-project store (store/observedMetadataStore.ts). Accepted
+// the persisted per-project store (stores/observedMetadataStore.ts). Accepted
// caveat: metadata the user has never loaded is never suggested.
//
// Top-level keys ONLY — deliberately not flattened into nested dot-paths.
@@ -25,8 +25,6 @@
// are rejected), so a key observed with more than one type simply drops its
// hint rather than changing any filter behavior.
-import type { ObservedOptions, ObservedValue } from "./observed-options";
-
export type MetadataLeafType =
| "string"
| "number"
@@ -149,31 +147,30 @@ export function collectMetadataPathTypes(
return out;
}
+/** One offered metadata key or value; `type` is the display-only hint. */
+export type MetadataSuggestion = { value: string; type?: MetadataLeafType };
+
/**
- * Merge the persisted per-project key map into the observed-options map where
- * the completion planner reads it: keys under `metadata` (keyPathOptions) and
- * each key's observed values under `metadata.<key>` (the value stage). Keys
- * are sorted so related dotted keys group together in the dropdown;
- * `"mixed"`/null-only keys carry no type hint. `undefined` observed (filter
- * options still loading) stays `undefined` so the planner's loading semantics
- * are untouched.
+ * Project the persisted per-project key map into option-map entries, keyed the
+ * way both suggestion surfaces look them up: observed keys under `metadata`,
+ * each key's observed values under `metadata.<key>`. Keys are sorted so
+ * related dotted keys group together; `"mixed"`/null-only keys carry no type
+ * hint. Empty when nothing has been observed, so callers can skip the merge.
*/
-export function withMetadataPathOptions(
- observed: ObservedOptions | undefined,
+export function observedMetadataOptions(
paths: Record<string, StoredKeyInfo> | undefined,
-): ObservedOptions | undefined {
- if (observed === undefined || paths === undefined) return observed;
- const entries = Object.entries(paths);
- if (entries.length === 0) return observed;
- const out: ObservedOptions = { ...observed };
- out.metadata = entries
- .sort(([a], [b]) => a.localeCompare(b))
- .map(
- ([path, { type }]): ObservedValue =>
+): Record<string, MetadataSuggestion[]> {
+ const entries = Object.entries(paths ?? {});
+ if (entries.length === 0) return {};
+ const out: Record<string, MetadataSuggestion[]> = {
+ metadata: [...entries]
+ .sort(([a], [b]) => a.localeCompare(b))
+ .map(([path, { type }]) =>
type === "" || type === "mixed"
? { value: path }
: { value: path, type },
- );
+ ),
+ };
for (const [path, { values }] of entries) {
if (values !== undefined && values.length > 0) {
out[`metadata.${path}`] = values.map((v) => ({ value: v }));
diff --git a/web/src/features/search-bar/hooks/useObservedMetadata.ts b/web/src/hooks/useObservedMetadata.ts
similarity index 69%
rename from web/src/features/search-bar/hooks/useObservedMetadata.ts
rename to web/src/hooks/useObservedMetadata.ts
index 3454037782..9963c55b4f 100644
--- a/web/src/features/search-bar/hooks/useObservedMetadata.ts
+++ b/web/src/hooks/useObservedMetadata.ts
@@ -1,18 +1,18 @@
-// Bridge between the events table and the observed-metadata store: the
-// recorder samples the currently visible rows' metadata into the persisted
-// per-project key map (once per fetch — the rows identity only changes when
-// new data lands), and the reader selects that project's map for merging into
-// the bar's observed options (lib/metadata-paths.ts withMetadataPathOptions).
-// Split in two because EventsTable derives its observed options before the
-// table data hook runs, while the recorder needs the loaded rows.
+// Bridge between a table and the observed-metadata store: the recorder samples
+// the currently visible rows' metadata into the persisted per-project key map
+// (once per fetch — the rows identity only changes when new data lands), and
+// the reader selects that project's map for merging into a surface's option
+// map (fns/observedMetadata/metadataPaths.ts observedMetadataOptions). Split in
+// two because EventsTable derives its observed options before the table data
+// hook runs, while the recorder needs the loaded rows.
import { useEffect } from "react";
import {
collectMetadataPathTypes,
METADATA_SAMPLE_ROWS,
type StoredKeyInfo,
-} from "../lib/metadata-paths";
-import { useObservedMetadataStore } from "../store/observedMetadataStore";
+} from "@/src/fns/observedMetadata/metadataPaths";
+import { useObservedMetadataStore } from "@/src/stores/observedMetadataStore";
/**
* The project's persisted metadata key→info map (type + sample values), or
diff --git a/web/src/features/search-bar/store/observedMetadataStore.clienttest.ts b/web/src/stores/observedMetadataStore.clienttest.ts
similarity index 99%
rename from web/src/features/search-bar/store/observedMetadataStore.clienttest.ts
rename to web/src/stores/observedMetadataStore.clienttest.ts
index 95d44fb379..f46ba58f28 100644
--- a/web/src/features/search-bar/store/observedMetadataStore.clienttest.ts
+++ b/web/src/stores/observedMetadataStore.clienttest.ts
@@ -9,7 +9,7 @@ import {
MAX_VALUES_PER_KEY,
MAX_VALUES_PER_PROJECT,
type StoredKeyInfo,
-} from "../lib/metadata-paths";
+} from "@/src/fns/observedMetadata/metadataPaths";
const paths = (entries: Record<string, StoredKeyInfo>) =>
new Map(Object.entries(entries));
diff --git a/web/src/features/search-bar/store/observedMetadataStore.ts b/web/src/stores/observedMetadataStore.ts
similarity index 96%
rename from web/src/features/search-bar/store/observedMetadataStore.ts
rename to web/src/stores/observedMetadataStore.ts
index 441be6879f..8fc6a9009c 100644
--- a/web/src/features/search-bar/store/observedMetadataStore.ts
+++ b/web/src/stores/observedMetadataStore.ts
@@ -1,6 +1,7 @@
// Persisted per-project map of observed metadata paths → types, feeding the
-// search bar's `metadata.<path>` key suggestions (lib/metadata-paths.ts owns
-// the analysis; hooks/useObservedMetadata.ts is the bridge).
+// `metadata.<path>` key/value suggestions of every filter surface
+// (fns/observedMetadata/metadataPaths.ts owns the analysis;
+// hooks/useObservedMetadata.ts is the bridge).
//
// Same shape rationale as globalDateRangeStore: one global store holding a
// `Record<projectId, …>` map, so switching projects selects a different field
@@ -26,7 +27,7 @@ import {
MAX_VALUES_PER_PROJECT,
mergePathType,
type StoredKeyInfo,
-} from "../lib/metadata-paths";
+} from "@/src/fns/observedMetadata/metadataPaths";
export const OBSERVED_METADATA_STORAGE_KEY = "langfuse-observed-metadata";