πͺ’ 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
/* eslint-disable @repo/no-style-props */
// Search-bar row: the query composer at full width, with an AI sub-mode.
// EventsTable owns the sticky stack around this row + the toolbar so the toolbar
// cannot scroll under the composer. Time-range + refresh controls live in the
// toolbar row below (next to the filter toggle and views), not here. Left
// padding matches the toolbar row below so the bar's left edge aligns with it.
//
// AI mode (the "Ask AI" affordance) swaps the grammar composer for a
// natural-language prompt. The generated filters are applied
// through `setFilterState` β the SAME path the facet sidebar uses β so the
// composer re-derives them as editable pills when we switch back. When opened
// with filters present, the bar's live draft is the refine context, so the
// model updates the existing filters instead of starting from scratch β read
// live from the store (not snapshotted), since the sidebar can change the
// filters while AI mode is open.
import * as React from "react";
import { type FilterState } from "@langfuse/shared";
import { useQueryProject } from "@/src/features/projects/hooks";
import { cn } from "@/src/utils/tailwind";
import type {
ObservedOptions,
ObservedScoreNames,
} from "@/src/features/search-bar/lib/observed-options";
import { aiContextObservedOptionsKeys } from "@/src/features/search-bar/lib/ai-context";
import {
EVENTS_FIELD_REGISTRY,
type FieldRegistry,
} from "@/src/features/search-bar/lib/fields";
import { ComposerWithPreview } from "@/src/features/search-bar/components/ComposerWithPreview";
import { type ComposerSize } from "@/src/features/search-bar/components/ComposerSurface";
import { SearchBarAiPrompt } from "@/src/features/search-bar/components/SearchBarAiPrompt";
import { SearchBarStoreProvider } from "@/src/features/search-bar/store/SearchBarStoreProvider";
import type { SearchBarStore } from "@/src/features/search-bar/store/searchBarStore";
import type { SearchCommit } from "@/src/features/search-bar/hooks/useEventsSearchBar";
import type { QueryPresetSection } from "@/src/features/search-bar/lib/completions";
type EventsSearchBarRowProps = Omit<
React.ComponentProps<typeof EventsSearchBarRowContent>,
"aiFeaturesEnabled"
>;
export function EventsSearchBarRow(props: EventsSearchBarRowProps) {
const registry = props.registry ?? EVENTS_FIELD_REGISTRY;
return props.projectId && registry.aiFilterPrompt ? (
<ProjectSearchBarRow {...props} />
) : (
<EventsSearchBarRowContent {...props} aiFeaturesEnabled={false} />
);
}
function ProjectSearchBarRow(props: EventsSearchBarRowProps) {
const { organization } = useQueryProject();
return (
<EventsSearchBarRowContent
{...props}
aiFeaturesEnabled={Boolean(organization?.aiFeaturesEnabled)}
/>
);
}
function EventsSearchBarRowContent({
inputId,
aiFeaturesEnabled,
projectId,
tableName,
isV4 = true,
store,
commit,
observed,
erroredColumns,
fieldReason,
freeTextReason,
onApplyFilters,
onRequestColumns,
presetSections,
onQueryPresetPick,
aiDataContext,
aiScoreNames,
className,
registry = EVENTS_FIELD_REGISTRY,
size = "default",
spacing = "table",
}: {
aiFeaturesEnabled: boolean;
inputId?: string;
spacing?: "table" | "flush";
projectId?: string;
/** Table this bar filters β threaded to AI-prompt analytics (LFE-10781). */
tableName: string;
isV4?: boolean;
store: SearchBarStore;
commit: SearchCommit;
observed: ObservedOptions | undefined;
/** Columns whose lazy fetch terminally errored β value-stage loading settles to
* empty (per column) instead of pinning, matching the sidebar's settled-error
* state, without blocking other columns. */
erroredColumns?: ReadonlySet<string>;
/** Given a filter token's field, the reason it is not applied on the current
* surface (e.g. the chart view can't filter on it) β dims the pill + hover.
* Undefined leaves all filters active. */
fieldReason?: (field: string) => string | null;
/** Reason free-text tokens are not applied on the current surface, or null. */
freeTextReason?: string | null;
/**
* Applies AI-generated filters (apply-immediately); the bar re-derives them.
* Preserves filters the grammar can't represent (no-silent-drop contract) β
* comes from `useEventsSearchBar.applyFilters`, not a raw `setFilterState`.
*/
onApplyFilters: (filters: FilterState) => void;
/**
* Lazy filter-options: widen the requested column set on demand. Threaded to
* the composer (request a field's values when typed) and fired on Ask AI open
* (request the observed-options keys so the prompt sees real values).
*/
onRequestColumns?: (columns: readonly string[]) => void;
/** Complete queries supplied by the host view. */
presetSections?: QueryPresetSection[];
onQueryPresetPick?: (presetId: string) => void;
/** Project data context (observed values + metadata keys + result count) for
* the AI prompt β built by EventsTable from filterOptions + visible rows. */
aiDataContext?: string;
/** Observed score names by column type, for the server's score-name
* validation of the generated filters (undefined sets are not enforced). */
aiScoreNames?: ObservedScoreNames;
/** Overrides the wrapper spacing. The default (`px-2 pt-2 pb-1`) aligns the
* bar with the desktop toolbar row; the mobile Filters sheet passes flush
* padding so the bar lines up with the sheet's other sections. */
className?: string;
/** The view-specific grammar and filter contract. */
registry?: FieldRegistry;
/** `large` on full-page lists; embedded toolbars keep the default. */
size?: ComposerSize;
}) {
const [aiOpen, setAiOpen] = React.useState(false);
// Mirror the legacy wand gate: org-level AI features. The server
// enforces it too, so this only governs whether the affordance is offered.
// Org entitlement AND a prompt written for this view β see
// FieldRegistry.aiFilterPrompt. Without the second half a new surface silently
// inherits the events prompt.
const aiRegistryId =
registry.id === "events" ||
registry.id === "evaluationRules" ||
registry.id === "evaluatorSamples" ||
registry.id === "ruleSamples" ||
registry.id === "sessions" ||
registry.id === "scores" ||
registry.id === "experiments" ||
registry.id === "users"
? registry.id
: undefined;
const aiAvailable =
Boolean(projectId && aiRegistryId && aiFeaturesEnabled) &&
registry.aiFilterPrompt;
const activateAi = React.useCallback(() => {
// Ground the model on real project values: lazily request the AI columns so
// they are loaded by the time the user submits a prompt.
onRequestColumns?.(aiContextObservedOptionsKeys(registry));
setAiOpen(true);
}, [onRequestColumns, registry]);
return (
<div
className={cn(
"min-w-0",
spacing === "table" && "px-2 pt-2 pb-1",
className,
)}
>
{aiOpen && aiAvailable && aiRegistryId && projectId ? (
<SearchBarAiPrompt
projectId={projectId}
tableName={tableName}
isV4={isV4}
store={store}
dataContext={aiDataContext}
scoreNames={aiScoreNames}
registryId={aiRegistryId}
size={size}
onApply={onApplyFilters}
onExit={() => setAiOpen(false)}
/>
) : (
<SearchBarStoreProvider store={store} commit={commit}>
<ComposerWithPreview
inputId={inputId}
projectId={projectId}
observed={observed}
erroredColumns={erroredColumns}
fieldReason={fieldReason}
freeTextReason={freeTextReason}
onActivateAi={aiAvailable ? activateAi : undefined}
onRequestColumns={onRequestColumns}
presetSections={presetSections}
onQueryPresetPick={onQueryPresetPick}
registry={registry}
size={size}
/>
</SearchBarStoreProvider>
)}
</div>
);
}