🪢 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(topics): unify manual trace processing and topic generation (#18840)
Lysander Kiesel0d71fbbf9558
diff --git a/web/src/__tests__/server/unit/topics.servertest.ts b/web/src/__tests__/server/unit/topics.servertest.tsindex 628abe2db3..8cb203684a 100644--- a/web/src/__tests__/server/unit/topics.servertest.ts+++ b/web/src/__tests__/server/unit/topics.servertest.ts@@ -347,7 +347,7 @@ describe("Topics filtered trace preview", () => {expect(request.preferredClickhouseService).toBe("EventsReadOnly");});- it("caps the preview query while preserving the full selected count", async () => {+ it("caps manual backfill selection while preserving the full matching count", async () => {mocks.queryClickhouse.mockResolvedValue([{id: "trace-a",@@ -362,20 +362,20 @@ describe("Topics filtered trace preview", () => {limit: null,});expect(response.matchedTraceCount).toBe(1001);- expect(response.selectedTraceCount).toBe(1001);+ expect(response.selectedTraceCount).toBe(1000);expect(mocks.queryClickhouse.mock.calls[0][0].params.limit).toBe(100);- await caller().previewTraces({ ...selection, limit: 10000 });+ await caller().previewTraces({ ...selection, limit: 500 });expect(mocks.queryClickhouse.mock.calls[1][0].params.limit).toBe(100);});- it("resolves filtered triggers on the server without a trace cap and excludes unchecked traces", async () => {+ it("caps filtered triggers before excluding unchecked traces", async () => {mocks.createTopicExecution.mockImplementation(async (resolved) => ({...execution(),input: resolved,status: "queued",}));mocks.queryClickhouse.mockResolvedValue(- Array.from({ length: 1001 }, (_, index) => ({+ Array.from({ length: 1000 }, (_, index) => ({id: `trace-${index}`,timestampMs: "1789430400000",name: "Agent",@@ -401,16 +401,17 @@ describe("Topics filtered trace preview", () => {});const resolved = mocks.createTopicExecution.mock.calls[0][0];expect(resolved.processingConfig.maxInputTokens).toBe(2000);- expect(resolved.traceIds).toHaveLength(1000);+ expect(resolved.traceIds).toHaveLength(999);expect(resolved.traceIds[0]).toBe("trace-1");- expect(resolved.traceIds.at(-1)).toBe("trace-1000");+ expect(resolved.traceIds.at(-1)).toBe("trace-999");expect(mocks.enqueueTopicExecution).toHaveBeenCalledWith(projectId,"execution-a",resolved.traceIds,);expect(resolved).not.toHaveProperty("selection");- expect(mocks.queryClickhouse.mock.calls[0][0].query).not.toContain("LIMIT");+ expect(mocks.queryClickhouse.mock.calls[0][0].query).toContain("LIMIT");+ expect(mocks.queryClickhouse.mock.calls[0][0].params.limit).toBe(1000);expect(mocks.queryClickhouse.mock.calls[0][0].params.samplingSeed).toBe("sample-seed",);@@ -429,6 +430,30 @@ describe("Topics filtered trace preview", () => {expect(mocks.createTopicExecution).not.toHaveBeenCalled();});+ it("rejects oversized manual backfill selections before querying or creating a run", async () => {+ for (const selectionInput of [+ { selection: { ...selection, limit: 1001 } },+ {+ traceIds: Array.from({ length: 1001 }, (_, index) => `trace-${index}`),+ },+ ]) {+ await expect(+ caller().trigger({+ projectId,+ requestId: "oversized-request",+ operation: "process",+ facets: selectedFacets,+ ...selectionInput,+ }),+ ).rejects.toThrow("Select at most 1,000 traces");+ }+ await expect(+ caller().previewTraces({ ...selection, limit: 1001 }),+ ).rejects.toThrow("Select at most 1,000 traces");+ expect(mocks.queryClickhouse).not.toHaveBeenCalled();+ expect(mocks.createTopicExecution).not.toHaveBeenCalled();+ });+it("runs saved rules without creating facet versions and rejects stale or foreign rules", async () => {const rule = {id: "rule-a",diff --git a/web/src/features/topics/AutomaticProcessingControl.tsx b/web/src/features/topics/AutomaticProcessingControl.tsxindex 66d3d3dc3e..8a38d6f7f8 100644--- a/web/src/features/topics/AutomaticProcessingControl.tsx+++ b/web/src/features/topics/AutomaticProcessingControl.tsx@@ -1,18 +1,9 @@import { Tooltip } from "@/src/components/design-system/Tooltip/Tooltip";-import { cn } from "@/src/utils/tailwind";--export type AutomaticProcessingState =- | { status: "unconfigured" }- | { status: "unavailable" }- | { status: "off" }- | { status: "on" }- | { status: "paused"; shortLabel: string };--type AutomaticProcessingControlProps = {- state: AutomaticProcessingState;- canWrite: boolean;- onOpen: () => void;-};+import { Button } from "@/src/components/design-system/Button/Button";+import {+ StatusBadge,+ type Status,+} from "@/src/components/ui/StatusBadge/StatusBadge";/*** Topics header status for automatic processing. Turning it on or off and the@@ -25,40 +16,45 @@ export function AutomaticProcessingControl({}: AutomaticProcessingControlProps) {const reason = disabledReason(state, canWrite);const button = (- // Matches the secondary header buttons; the live dot is StatusBadge's.- <button- type="button"- className="bg-secondary text-secondary-foreground hover:bg-secondary/80 ring-offset-background focus-visible:ring-ring inline-flex h-7 shrink-0 items-center gap-2 rounded-md px-2 text-xs whitespace-nowrap transition-colors focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-hidden disabled:cursor-not-allowed disabled:opacity-50"- aria-label={`Automatic processing: ${statusLabel(state)}`}+ <Button+ text="Automatic processing"+ size="sm"+ variant={+ state.status === "off" || state.status === "paused"+ ? "primary"+ : "secondary"+ }disabled={reason !== null}onClick={onOpen}- >- <span aria-hidden className="relative inline-flex size-2 shrink-0">- {state.status === "on" ? (- <span className="bg-dark-green absolute inline-flex size-full animate-ping rounded-full opacity-75" />- ) : null}- <span- className={cn(- "relative inline-flex size-2 rounded-full",- dotClass(state),- )}- />- </span>- Automatic processing- </button>+ />);- if (!reason) return button;return (- <Tooltip label={state.status === "paused" ? state.shortLabel : reason}>- {({ getTriggerProps }) => <span {...getTriggerProps()}>{button}</span>}- </Tooltip>+ <div className="flex shrink-0 items-center gap-1">+ <div+ role="status"+ aria-label={`Automatic processing: ${statusLabel(state)}`}+ >+ <StatusBadge type={badgeStatus(state)} showText={false}>+ {statusLabel(state)}+ </StatusBadge>+ </div>+ {reason ? (+ <Tooltip label={state.status === "paused" ? state.shortLabel : reason}>+ {({ getTriggerProps }) => (+ <span {...getTriggerProps()}>{button}</span>+ )}+ </Tooltip>+ ) : (+ button+ )}+ </div>);}-function dotClass(state: AutomaticProcessingState) {- if (state.status === "on") return "bg-dark-green";- if (state.status === "paused") return "bg-dark-yellow";- return "bg-muted-foreground";+function badgeStatus(state: AutomaticProcessingState): Status {+ if (state.status === "on") return "active";+ if (state.status === "paused") return "paused";+ return "inactive";}function statusLabel(state: AutomaticProcessingState) {@@ -74,3 +70,16 @@ function disabledReason(state: AutomaticProcessingState, canWrite: boolean) {if (!canWrite) return "You need permission to edit Topics.";return null;}++export type AutomaticProcessingState =+ | { status: "unconfigured" }+ | { status: "unavailable" }+ | { status: "off" }+ | { status: "on" }+ | { status: "paused"; shortLabel: string };++type AutomaticProcessingControlProps = {+ state: AutomaticProcessingState;+ canWrite: boolean;+ onOpen: () => void;+};diff --git a/web/src/features/topics/ManualTopicsDialog.stories.tsx b/web/src/features/topics/ManualTopicsDialog.stories.tsxnew file mode 100644index 0000000000..99a2b535f8--- /dev/null+++ b/web/src/features/topics/ManualTopicsDialog.stories.tsx@@ -0,0 +1,587 @@+import { useState } from "react";+import {+ eventsEvalFilterColumns,+ observationEvalFilterColsWithOptions,+ type FilterState,+} from "@langfuse/shared";+import {+ TopicTraceSelectorView,+ type TopicTraceSelectorViewProps,+} from "./TopicTraceSelectorView";+import { TOPICS_BACKFILL_TRACE_LIMIT } from "./backfill-limit";+import {+ relativeTopicTimeRange,+ topicCalendarDates,+ topicCalendarRange,+} from "./time-range";+import { parseTraceInput } from "./parse-trace-input";+import { manualTopicsSelection } from "./manual-topics-selection";+import { expect, fn, userEvent, waitFor, within } from "storybook/test";+import preview from "../../../.storybook/preview";+import { Button } from "@/src/components/design-system/Button/Button";+import { DialogController } from "@/src/components/design-system/DialogController/DialogController";+import {+ ManualTopicsDialog,+ type ManualTopicsDialogProps,+} from "./ManualTopicsDialog";++const facets: ManualTopicsDialogProps["facets"] = [+ {+ id: "intent",+ name: "Intent",+ summaries: 240,+ kind: "Built-in",+ version: 1,+ description: "What the user wants to accomplish.",+ },+ {+ id: "outcome",+ name: "Outcome",+ summaries: 24,+ kind: "Built-in",+ version: 1,+ description: "How the conversation ended.",+ },+ {+ id: "product",+ name: "Product area",+ summaries: 180,+ kind: "Custom",+ version: 2,+ description: "Which part of the product the conversation concerns.",+ },+];++const defaults: ManualTopicsDialogProps = {+ operation: "process",+ facets,+ selectedFacetIds: facets.map((facet) => facet.id),+ traceControls: null,+ count: 0,+ generationTimeRange: { range: "last7Days" },+ models: {+ summary: { model: "gpt-4.1-mini", connection: "OpenAI" },+ embedding: { model: "text-embedding-3-small", connection: "OpenAI" },+ clustering: { model: "gpt-4.1-mini", connection: "OpenAI" },+ },+ reuse: true,+ smallSample: false,+ canSubmit: true,+ isSubmitting: false,+ disabledReason: null,+ error: null,+ onOperationChange: fn(),+ onToggleFacet: fn(),+ onGenerationTimeRangeChange: fn(),+ onReuseChange: fn(),+ onSmallSampleChange: fn(),+ onSubmit: fn(),+ onConfigure: fn(),+};++const meta = preview.meta({+ component: ManualTopicsDialog,+ parameters: { layout: "fullscreen" },+ args: defaults,+ render: function DialogStory(args) {+ const [draft, setDraft] = useState({+ operation: args.operation,+ selectedFacetIds: args.selectedFacetIds,+ reuse: args.reuse,+ smallSample: args.smallSample,+ generationTimeRange: args.generationTimeRange,+ });+ const [criteria, setCriteria] = useState({+ mode: "filters",+ filter: [] as FilterState,+ timeWindow: "7",+ range: relativeTopicTimeRange(7),+ limit: String(TOPICS_BACKFILL_TRACE_LIMIT),+ sampling: "random" as "random" | "latest",+ paste: "",+ });+ const [filterMode, setFilterMode] = useState<"builder" | "query">(+ "builder",+ );+ const [batch, setBatch] = useState<{+ key: string;+ data: Extract<+ TopicTraceSelectorViewProps["preview"],+ { status: "ready" }+ >["data"];+ } | null>(null);+ const [excluded, setExcluded] = useState<string[]>([]);+ const key = JSON.stringify(criteria);+ const ready = batch?.key === key ? batch : null;+ let pastedCount: number | null = null;+ let pasteError: string | null = null;+ if (criteria.mode === "paste" && criteria.paste.trim()) {+ try {+ pastedCount = parseTraceInput(+ criteria.paste,+ "storybook",+ window.location.origin,+ ).length;+ if (pastedCount > TOPICS_BACKFILL_TRACE_LIMIT)+ throw new Error("Select at most 1,000 traces per run.");+ } catch (error) {+ pastedCount = null;+ pasteError =+ error instanceof Error ? error.message : "Invalid trace IDs.";+ }+ }+ let count = ready ? ready.data.selectedTraceCount - excluded.length : 0;+ if (criteria.mode === "paste") count = pastedCount ?? 0;+ const selectionStatus = manualTopicsSelection({+ operation: draft.operation,+ summaryCounts: args.facets+ .filter((facet) => draft.selectedFacetIds.includes(facet.id))+ .map((facet) => facet.summaries ?? 0),+ traceCount: count,+ smallSample: draft.smallSample,+ });+ function changeCriteria(change: Partial<typeof criteria>) {+ setCriteria((current) => ({ ...current, ...change }));+ setBatch(null);+ setExcluded([]);+ }+ function handleModeChange(mode: string) {+ changeCriteria({ mode });+ }+ function handleFilterChange(filter: FilterState) {+ changeCriteria({ filter });+ }+ function handleTimeWindowChange(timeWindow: string) {+ changeCriteria({+ timeWindow,+ range:+ timeWindow === "custom"+ ? topicCalendarRange(topicCalendarDates(criteria.range))+ : relativeTopicTimeRange(Number(timeWindow)),+ });+ }+ function handleRangeChange(range: TopicTraceSelectorViewProps["range"]) {+ changeCriteria({ range });+ }+ function handleLimitChange(limit: string) {+ changeCriteria({ limit });+ }+ function handleSamplingChange(sampling: "random" | "latest") {+ changeCriteria({ sampling });+ }+ function handlePasteChange(paste: string) {+ changeCriteria({ paste });+ }+ function handlePreview() {+ const traces = [+ {+ id: "trace-support",+ name: "Support conversation",+ timestamp: new Date("2026-10-14T12:00:00Z"),+ environment: "production",+ },+ {+ id: "trace-checkout",+ name: "Checkout question",+ timestamp: new Date("2026-10-14T11:00:00Z"),+ environment: "production",+ },+ ].slice(0, Number(criteria.limit));+ setExcluded([]);+ setBatch({+ key,+ data: {+ traces,+ matchedTraceCount: 2,+ selectedTraceCount: traces.length,+ },+ });+ }+ const traceControls = (+ <TopicTraceSelectorView+ {...criteria}+ projectId="storybook"+ onModeChange={handleModeChange}+ filterMode={filterMode}+ onFilterModeChange={setFilterMode}+ onFilterChange={handleFilterChange}+ onTimeWindowChange={handleTimeWindowChange}+ onRangeChange={handleRangeChange}+ onLimitChange={handleLimitChange}+ onSamplingChange={handleSamplingChange}+ onPasteChange={handlePasteChange}+ pastedCount={pastedCount}+ pasteError={pasteError}+ preview={+ ready+ ? { status: "ready", data: ready.data, seed: ready.key }+ : { status: "idle" }+ }+ onPreview={handlePreview}+ excluded={excluded}+ onExcludedChange={setExcluded}+ builderColumns={observationEvalFilterColsWithOptions(+ {+ environment: [{ value: "production" }],+ name: [+ { value: "Support conversation" },+ { value: "Checkout question" },+ ],+ },+ [...eventsEvalFilterColumns],+ ).filter((column) => column.id !== "isRootObservation")}+ observed={undefined}+ onRequestColumns={undefined}+ erroredColumns={undefined}+ />+ );+ function handleOperationChange(+ operation: ManualTopicsDialogProps["operation"],+ ) {+ setDraft((current) => ({ ...current, operation }));+ args.onOperationChange(operation);+ }+ function handleToggleFacet(id: string, selected: boolean) {+ setDraft((current) => ({+ ...current,+ selectedFacetIds: selected+ ? current.selectedFacetIds.concat(id)+ : current.selectedFacetIds.filter((value) => value !== id),+ }));+ args.onToggleFacet(id, selected);+ }+ function handleReuseChange(reuse: boolean) {+ setDraft((current) => ({ ...current, reuse }));+ args.onReuseChange(reuse);+ }+ function handleSmallSampleChange(smallSample: boolean) {+ setDraft((current) => ({ ...current, smallSample }));+ args.onSmallSampleChange(smallSample);+ }+ function handleTimeRangeChange(+ generationTimeRange: ManualTopicsDialogProps["generationTimeRange"],+ ) {+ setDraft((current) => ({ ...current, generationTimeRange }));+ args.onGenerationTimeRangeChange(generationTimeRange);+ }+ return (+ <DialogController+ initialState={() => true}+ renderDialog={() => (+ <ManualTopicsDialog+ {...args}+ {...draft}+ traceControls={traceControls}+ count={count}+ canSubmit={args.canSubmit && selectionStatus.ready}+ disabledReason={+ args.isCountingSummaries+ ? null+ : (args.disabledReason ?? selectionStatus.disabledReason)+ }+ onOperationChange={handleOperationChange}+ onToggleFacet={handleToggleFacet}+ onReuseChange={handleReuseChange}+ onSmallSampleChange={handleSmallSampleChange}+ onGenerationTimeRangeChange={handleTimeRangeChange}+ />+ )}+ >+ {({ openDialog }) => {+ function handleOpen() {+ openDialog(true);+ }+ return <Button text="Open manual processing" onClick={handleOpen} />;+ }}+ </DialogController>+ );+ },+});++export default meta;++export const SummarizeTraces = meta.story({});+export const GenerateTopics = meta.story({ args: { operation: "update" } });+export const NoSummaries = meta.story({+ args: {+ operation: "update",+ facets: facets.map((facet) => ({ ...facet, summaries: 0 })),+ canSubmit: false,+ },+});+export const SubmissionError = meta.story({+ args: {+ error:+ "Could not start the run. Check your model connection and try again.",+ },+});++export const SkippedFacets = meta.story({+ name: "(Test) Warns about empty facets in an otherwise eligible run",+ args: {+ operation: "update",+ facets: facets.map((facet) => ({+ ...facet,+ summaries: facet.id === "outcome" ? 0 : facet.summaries,+ })),+ },+ play: async ({ canvasElement }) => {+ const view = within(+ within(canvasElement.ownerDocument.body).getByRole("dialog", {+ name: "Manual processing",+ }),+ );+ await expect(+ view.getByRole("button", { name: "Generate topics" }),+ ).toBeEnabled();+ await waitFor(() =>+ expect(+ view.getByText(/Outcome will be skipped: fewer than 100 summaries/),+ ).toBeVisible(),+ );+ await userEvent.click(view.getByRole("checkbox", { name: "Intent" }));+ await userEvent.click(view.getByRole("checkbox", { name: "Product area" }));+ await expect(+ view.getByRole("button", { name: "Generate topics" }),+ ).toBeDisabled();+ await expect(+ view.getByRole("button", { name: "Go to Summarize traces & embed" }),+ ).toBeVisible();+ },+});++export const InsufficientSummaries = meta.story({+ name: "(Test) Shows one readiness warning inside Review & Run",+ args: {+ operation: "update",+ facets: facets.map((facet) => ({ ...facet, summaries: 24 })),+ },+ play: async ({ canvasElement }) => {+ const view = within(+ within(canvasElement.ownerDocument.body).getByRole("dialog", {+ name: "Manual processing",+ }),+ );+ await expect(view.getAllByRole("alert")).toHaveLength(1);+ await expect(view.getByRole("alert")).toHaveTextContent(+ "Each selected facet has fewer than 100 summaries.",+ );+ await expect(+ view.getByRole("button", { name: "Generate topics" }),+ ).toBeDisabled();+ await userEvent.click(+ view.getByRole("button", { name: "Step 3: Review & Run" }),+ );+ await expect(view.queryByRole("alert")).not.toBeInTheDocument();+ await userEvent.click(+ view.getByRole("button", { name: "Step 3: Review & Run" }),+ );+ await userEvent.click(+ view.getByRole("switch", { name: "Small sample mode" }),+ );+ await expect(view.queryByRole("alert")).not.toBeInTheDocument();+ await expect(+ view.getByRole("button", { name: "Generate topics" }),+ ).toBeEnabled();+ },+});++export const ReviewChoices = meta.story({+ name: "(Test) Review choices across steps and operations",+ play: async ({ canvasElement, args }) => {+ const dialog = within(canvasElement.ownerDocument.body).getByRole(+ "dialog",+ { name: "Manual processing" },+ );+ const view = within(dialog);+ await userEvent.click(view.getByRole("checkbox", { name: "Intent" }));+ await expect(args.onToggleFacet).toHaveBeenCalledWith("intent", false);+ await waitFor(() =>+ expect(+ within(view.getByRole("group", { name: "Choose facets" })).getByText(+ "2 of 3 selected",+ ),+ ).toBeVisible(),+ );+ await userEvent.click(+ view.getByRole("button", { name: "Step 1: Choose facets" }),+ );+ await userEvent.click(+ view.getByRole("button", { name: "Step 1: Choose facets" }),+ );+ await expect(+ view.getByRole("checkbox", { name: "Intent" }),+ ).not.toBeChecked();+ await userEvent.click(+ view.getByRole("checkbox", {+ name: "Reuse existing summaries and embeddings",+ }),+ );+ await expect(args.onReuseChange).toHaveBeenCalledWith(false);+ await expect(+ view.getByText(/Fresh results incur model costs/),+ ).toBeVisible();+ await userEvent.click(view.getByRole("tab", { name: "Generate topics" }));+ await expect(args.onOperationChange).toHaveBeenCalledWith("update");+ await expect(+ view.queryByText(args.models.embedding.model),+ ).not.toBeInTheDocument();+ await expect(+ view.getByRole("checkbox", { name: "Intent" }),+ ).not.toBeChecked();+ await userEvent.click(+ view.getByRole("switch", { name: "Small sample mode" }),+ );+ await expect(args.onSmallSampleChange).toHaveBeenCalledWith(true);+ await expect(view.queryByText(/will be skipped/)).not.toBeInTheDocument();+ await userEvent.click(+ view.getByRole("tab", { name: "Summarize traces & embed" }),+ );+ await expect(+ view.getByRole("checkbox", {+ name: "Reuse existing summaries and embeddings",+ }),+ ).not.toBeChecked();+ await expect(view.getByText(args.models.embedding.model)).toBeVisible();+ },+});++export const UnknownSummaryCounts = meta.story({+ name: "(Test) Does not suggest reprocessing while summary counts are unknown",+ args: {+ operation: "update",+ facets: facets.map((facet) => ({ ...facet, summaries: null })),+ canSubmit: false,+ isCountingSummaries: true,+ },+ play: async ({ canvasElement }) => {+ const view = within(+ within(canvasElement.ownerDocument.body).getByRole("dialog", {+ name: "Manual processing",+ }),+ );+ await waitFor(() =>+ expect(view.getByRole("status")).toHaveTextContent(/^3 facets selected$/),+ );+ await expect(view.queryByRole("alert")).not.toBeInTheDocument();+ await expect(view.queryAllByText("No summaries")).toHaveLength(0);+ await expect(+ view.queryByRole("button", { name: "Go to Summarize traces & embed" }),+ ).not.toBeInTheDocument();+ await expect(+ view.getByRole("button", { name: "Generate topics" }),+ ).toBeDisabled();+ },+});++export const Submitting = meta.story({+ name: "(Test) Starting a run does not flash validation warnings",+ args: {+ operation: "update",+ isSubmitting: true,+ canSubmit: false,+ disabledReason: "A Topics run is in progress. Wait for it to finish.",+ },+ play: async ({ canvasElement }) => {+ const view = within(+ within(canvasElement.ownerDocument.body).getByRole("dialog", {+ name: "Manual processing",+ }),+ );+ await expect(view.queryByRole("alert")).not.toBeInTheDocument();+ await expect(+ view.getByRole("button", { name: "Generate topics" }),+ ).toBeDisabled();+ },+});++export const PreviewRequired = meta.story({+ name: "(Test) Preview gates summarizing, independently of topic generation",+ play: async ({ canvasElement, args }) => {+ const view = within(+ within(canvasElement.ownerDocument.body).getByRole("dialog", {+ name: "Manual processing",+ }),+ );+ const summarize = () =>+ view.getByRole("button", { name: "Summarize & embed" });+ await expect(summarize()).toBeDisabled();+ await waitFor(() =>+ expect(view.getByRole("button", { name: "Add filter" })).toBeVisible(),+ );+ await userEvent.click(view.getByRole("button", { name: "Add filter" }));+ await userEvent.click(+ within(view.getByRole("row")).getAllByRole("combobox")[0],+ );+ await userEvent.type(+ within(canvasElement.ownerDocument.body).getByPlaceholderText(+ "Search for column",+ ),+ "User",+ );+ await userEvent.keyboard("{Enter}");+ await userEvent.type(view.getByPlaceholderText("string"), "user-a");+ await userEvent.click(view.getByRole("tab", { name: "Query" }));+ await userEvent.click(view.getByRole("tab", { name: "Builder" }));+ await expect(view.getByPlaceholderText("string")).toHaveValue("user-a");+ await userEvent.click(view.getByRole("tab", { name: "Generate topics" }));+ await expect(+ view.getByRole("button", { name: "Generate topics" }),+ ).toBeEnabled();+ await userEvent.click(+ view.getByRole("tab", { name: "Summarize traces & embed" }),+ );+ await expect(summarize()).toBeDisabled();+ await userEvent.click(view.getByRole("button", { name: "Preview traces" }));+ await expect(+ view.getByRole("link", { name: "Support conversation" }),+ ).toBeVisible();+ await expect(summarize()).toBeEnabled();+ await userEvent.click(view.getByRole("button", { name: /Past 7 days/ }));+ await userEvent.click(+ within(canvasElement.ownerDocument.body).getByText("Past 30 days", {+ exact: true,+ }),+ );+ await expect(summarize()).toBeDisabled();+ await userEvent.click(view.getByRole("button", { name: "Preview traces" }));+ await expect(summarize()).toBeEnabled();+ await userEvent.click(+ view.getByRole("checkbox", { name: "Select trace trace-support" }),+ );+ await expect(view.getByText("Run 3 facets on 1 trace")).toBeVisible();+ const limit = view.getByRole("spinbutton", { name: "Maximum traces" });+ await userEvent.clear(limit);+ await userEvent.type(limit, "1");+ await expect(summarize()).toBeDisabled();+ await expect(+ view.queryByRole("link", { name: "Support conversation" }),+ ).not.toBeInTheDocument();+ await userEvent.click(view.getByRole("button", { name: "Preview traces" }));+ await expect(summarize()).toBeEnabled();+ await userEvent.click(summarize());+ await expect(args.onSubmit).toHaveBeenCalledTimes(1);+ },+});++export const PastedBatch = meta.story({+ name: "(Test) Pasted IDs select a unique batch without previewing filters",+ play: async ({ canvasElement, args }) => {+ const view = within(+ within(canvasElement.ownerDocument.body).getByRole("dialog", {+ name: "Manual processing",+ }),+ );+ await userEvent.click(view.getByRole("tab", { name: "Paste IDs" }));+ const submit = view.getByRole("button", { name: "Summarize & embed" });+ await expect(submit).toBeDisabled();+ await userEvent.type(+ view.getByRole("textbox", { name: "Trace IDs or links" }),+ "trace-a\ntrace-b\ntrace-a",+ );+ await expect(view.getByText("Run 3 facets on 2 traces")).toBeVisible();+ await expect(submit).toBeEnabled();+ await userEvent.click(submit);+ await expect(args.onSubmit).toHaveBeenCalledTimes(1);+ },+});diff --git a/web/src/features/topics/ManualTopicsDialog.tsx b/web/src/features/topics/ManualTopicsDialog.tsxnew file mode 100644index 0000000000..165240a014--- /dev/null+++ b/web/src/features/topics/ManualTopicsDialog.tsx@@ -0,0 +1,417 @@+import { type ReactNode } from "react";+import { Info } from "lucide-react";+import { IconButton } from "@/src/components/design-system/IconButton/IconButton";+import { Tooltip } from "@/src/components/design-system/Tooltip/Tooltip";+import { cn } from "@/src/utils/tailwind";+import { compactNumberFormatter } from "@/src/utils/numbers";+import { type TopicOperation } from "@langfuse/shared/topics";+import { type TimeRange } from "@/src/utils/date-range-utils";+import { TimeRangePicker } from "@/src/components/date-picker";+import { Alert } from "@/src/components/design-system/Alert/Alert";+import { Button } from "@/src/components/design-system/Button/Button";+import { Checkbox } from "@/src/components/design-system/Checkbox/Checkbox";+import { Dialog } from "@/src/components/design-system/Dialog/Dialog";+import { Switch } from "@/src/components/design-system/Switch/Switch";+import { Tabs } from "@/src/components/design-system/Tabs/Tabs";+import { Stepper } from "@/src/features/evals/v2/components/Stepper/Stepper";++export function ManualTopicsDialog({+ operation,+ onOperationChange,+ facets,+ selectedFacetIds,+ onToggleFacet,+ traceControls,+ count,+ generationTimeRange,+ onGenerationTimeRangeChange,+ models,+ reuse,+ onReuseChange,+ smallSample,+ onSmallSampleChange,+ canSubmit,+ isSubmitting,+ isCountingSummaries = false,+ disabledReason,+ error,+ onSubmit,+ onConfigure,+}: ManualTopicsDialogProps) {+ const minimum = smallSample ? 10 : 100;+ const selectedUpdates = facets.filter((facet) =>+ selectedFacetIds.includes(facet.id),+ );+ const noSummaries =+ selectedUpdates.length > 0 &&+ selectedUpdates.every((facet) => facet.summaries === 0);+ const belowMinimum = selectedUpdates.filter(+ (facet) => facet.summaries !== null && facet.summaries < minimum,+ );+ const facetLabel = selectedUpdates.length === 1 ? "facet" : "facets";+ const traceLabel = count === 1 ? "trace" : "traces";+ const processingSummary =+ count > 0 ? (+ `Run ${selectedUpdates.length} ${facetLabel} on ${count.toLocaleString()} ${traceLabel}`+ ) : (+ <>+ Press <em>Preview traces</em> first, then confirm or adjust the batch+ before starting the run.+ </>+ );+ const storedSummaryCount = selectedUpdates.every(+ (facet) => facet.summaries !== null,+ )+ ? selectedUpdates.reduce(+ (total, facet) => total + (facet.summaries ?? 0),+ 0,+ )+ : null;+ const generationSummary = (+ <>+ {selectedUpdates.length} {facetLabel} selected+ {storedSummaryCount !== null ? (+ <> · {storedSummaryCount.toLocaleString()} stored summaries</>+ ) : null}+ </>+ );+ function handleOperationChange(value: string) {+ onOperationChange(value as TopicOperation);+ }+ function handleReuseChange(value: boolean | "indeterminate") {+ onReuseChange(value === true);+ }+ function handleChooseSummarization() {+ onOperationChange("process");+ }+ return (+ <Dialog+ title="Manual processing"+ size="lg"+ actions={[+ {+ label:+ operation === "process" ? "Summarize & embed" : "Generate topics",+ disabled: !canSubmit,+ loading: isSubmitting,+ onClick: onSubmit,+ },+ ]}+ >+ <Dialog.Body>+ <div className="ph-no-capture flex flex-col gap-5 text-sm">+ <Tabs value={operation} onValueChange={handleOperationChange}>+ <Tabs.List+ variant="inset"+ size="md"+ layout="full"+ aria-label="Manual processing operation"+ >+ <Tabs.Trigger value="process" label="Summarize traces & embed" />+ <Tabs.Trigger value="update" label="Generate topics" />+ </Tabs.List>+ </Tabs>+ <p className="text-muted-foreground">+ {operation === "process"+ ? "Prepare historical or unprocessed traces for topic discovery. New summaries are assigned to existing compatible topics; generate topics afterward to discover new groups."+ : "Generate topics from stored summaries and embeddings for the selected facets."}+ </p>+ <div className="flex flex-col">+ <Stepper+ number={1}+ title="Choose facets"+ description={+ operation === "process"+ ? "Choose the facets to summarize and embed your traces for."+ : "Facets to run clustering and generate topics for."+ }+ >+ <fieldset className="flex min-w-0 flex-col gap-3">+ <legend className="sr-only">Choose facets</legend>+ <div className="flex flex-wrap gap-2">+ {facets.map((facet) => (+ <FacetToggle+ key={facet.id}+ facet={facet}+ selected={selectedFacetIds.includes(facet.id)}+ operation={operation}+ onToggleFacet={onToggleFacet}+ />+ ))}+ </div>+ <p className="text-muted-foreground text-xs">+ {selectedUpdates.length} of {facets.length} selected+ </p>+ </fieldset>+ </Stepper>+ <Stepper+ number={2}+ title={+ operation === "process" ? "Select traces" : "Select summaries"+ }+ description={+ operation === "process"+ ? "Set your filters or paste trace IDs. Preview matching traces and choose the batch to run. Previewing does not use a model."+ : "Time range of stored summaries to use for clustering and topic generation."+ }+ >+ <section hidden={operation !== "process"}>+ <div className="flex flex-col gap-4">{traceControls}</div>+ </section>+ {operation === "update" ? (+ <section className="self-start">+ <TimeRangePicker+ timeRange={generationTimeRange}+ onTimeRangeChange={onGenerationTimeRangeChange}+ timeRangePresets={["last1Day", "last7Days", "last30Days"]}+ maxRangeMs={30 * 86_400_000}+ className="my-0"+ />+ </section>+ ) : null}+ </Stepper>+ <Stepper number={3} title="Review & Run">+ <p role="status" className="ph-no-capture text-muted-foreground">+ {operation === "process"+ ? processingSummary+ : generationSummary}+ </p>+ {operation === "process" ? (+ <p>+ Summarize and embed the selected traces, then assign them to+ existing topics.+ </p>+ ) : null}+ <div className="flex flex-wrap items-center gap-x-4 gap-y-2">+ <span className="font-bold">Models</span>+ <dl className="flex flex-wrap gap-x-4 gap-y-2">+ <div className="flex flex-wrap items-baseline gap-1.5">+ <dt className="text-muted-foreground">+ {operation === "process" ? "Summary:" : "Topic naming:"}+ </dt>+ <dd+ title={+ operation === "process"+ ? models.summary.connection+ : models.clustering.connection+ }+ >+ {operation === "process"+ ? models.summary.model+ : models.clustering.model}+ </dd>+ </div>+ {operation === "process" ? (+ <div className="flex flex-wrap items-baseline gap-1.5">+ <dt className="text-muted-foreground">Embedding:</dt>+ <dd title={models.embedding.connection}>+ {models.embedding.model}+ </dd>+ </div>+ ) : null}+ </dl>+ <div className="ml-auto">+ <Button+ text="Configure Topics"+ variant="ghost"+ size="sm"+ onClick={onConfigure}+ />+ </div>+ </div>+ {operation === "process" ? (+ <>+ <div className="flex items-center gap-1 p-1">+ <label className="flex items-center gap-2">+ <Checkbox+ aria-label="Reuse existing summaries and embeddings"+ checked={reuse}+ onCheckedChange={handleReuseChange}+ />+ Reuse existing summaries & embeddings+ </label>+ <Tooltip label="Reuse compatible results for the same facet version and models. Source changes are not checked. Turn off to generate fresh results.">+ {({ getTriggerProps }) => (+ <IconButton+ {...getTriggerProps()}+ icon={Info}+ label="About reusing summaries and embeddings"+ size="sm"+ />+ )}+ </Tooltip>+ </div>+ <p>+ <span className="font-bold">Model costs: </span>+ <span className="text-muted-foreground">+ {reuse+ ? "Only missing or incompatible results incur costs on your linked API keys."+ : "Fresh results incur model costs for every selected trace and facet on your linked API keys."}+ </span>+ </p>+ </>+ ) : null}+ {operation === "update" ? (+ <>+ <p className="text-muted-foreground text-xs">+ Topic naming uses your configured model and incurs model+ costs. New groups replace your current topics once the run+ succeeds.+ </p>+ <label className="flex items-start justify-between gap-4 rounded-md border p-3">+ <span className="flex flex-col gap-1">+ <span className="font-bold">Small sample mode</span>+ <span className="text-muted-foreground text-xs">+ Explore provisional topics with at least 10 summaries+ per facet instead of 100.+ </span>+ </span>+ <Switch+ aria-label="Small sample mode"+ checked={smallSample}+ onCheckedChange={onSmallSampleChange}+ />+ </label>+ {!isSubmitting &&+ !isCountingSummaries &&+ !disabledReason &&+ !noSummaries &&+ belowMinimum.length > 0 ? (+ <Alert variant="warning" size="sm">+ <Alert.Description>+ <p>+ {belowMinimum.map((facet) => facet.name).join(", ")}{" "}+ will be skipped: fewer than {minimum} summaries.+ Summarize more traces, widen the time range, or enable+ small sample mode.+ </p>+ </Alert.Description>+ </Alert>+ ) : null}+ </>+ ) : null}+ {!isSubmitting && disabledReason ? (+ <Alert variant="warning" size="sm">+ <Alert.Description>+ <div className="flex flex-col items-start gap-2">+ <p>{disabledReason}</p>+ {operation === "update" && noSummaries ? (+ <Button+ text="Go to Summarize traces & embed"+ variant="secondary"+ size="sm"+ onClick={handleChooseSummarization}+ />+ ) : null}+ </div>+ </Alert.Description>+ </Alert>+ ) : null}+ {error ? (+ <Alert variant="destructive" size="sm">+ <Alert.Description>+ <p>{error}</p>+ </Alert.Description>+ </Alert>+ ) : null}+ </Stepper>+ </div>+ </div>+ </Dialog.Body>+ </Dialog>+ );+}++function FacetToggle({+ facet,+ selected,+ operation,+ onToggleFacet,+}: FacetToggleProps) {+ function handleCheckedChange(checked: boolean | "indeterminate") {+ onToggleFacet(facet.id, checked === true);+ }+ return (+ <label+ title={`${facet.description} (${facet.kind}, v${facet.version})`}+ className={cn(+ "flex cursor-pointer items-center gap-1.5 rounded-md border px-2 py-2 transition-colors",+ selected+ ? "border-border-contrast bg-secondary"+ : "border-input text-muted-foreground hover:bg-accent",+ )}+ >+ <Checkbox+ aria-label={facet.name}+ checked={selected}+ onCheckedChange={handleCheckedChange}+ />+ <span className="self-baseline font-bold">{facet.name}</span>+ {operation === "update" ? (+ <span+ className="text-muted-foreground w-25 shrink-0 self-baseline truncate text-right text-xs tabular-nums"+ title={+ facet.summaries === null+ ? undefined+ : `${facet.summaries.toLocaleString()} summaries`+ }+ >+ {summaryLabel(facet.summaries)}+ </span>+ ) : null}+ </label>+ );+}++function summaryLabel(summaries: number | null) {+ if (summaries === null) return "Not counted";+ if (summaries === 0) return "No summaries";+ const count =+ summaries > 1_000+ ? compactNumberFormatter(+ summaries,+ summaries < 10_000 ? 1 : 0,+ ).toLowerCase()+ : summaries.toLocaleString();+ return `${count} summaries`;+}++type Model = { model: string; connection: string };+export type ManualTopicsDialogProps = {+ operation: TopicOperation;+ onOperationChange: (operation: TopicOperation) => void;+ facets: {+ id: string;+ name: string;+ summaries: number | null;+ kind: string;+ version: number;+ description: string;+ }[];+ selectedFacetIds: string[];+ onToggleFacet: (id: string, selected: boolean) => void;+ traceControls: ReactNode;+ count: number;+ generationTimeRange: TimeRange;+ onGenerationTimeRangeChange: (range: TimeRange) => void;+ models: { summary: Model; embedding: Model; clustering: Model };+ reuse: boolean;+ onReuseChange: (reuse: boolean) => void;+ smallSample: boolean;+ onSmallSampleChange: (value: boolean) => void;+ canSubmit: boolean;+ isSubmitting: boolean;+ isCountingSummaries?: boolean;+ disabledReason: string | null;+ error: string | null;+ onSubmit: () => void;+ onConfigure: () => void;+};++type FacetToggleProps = Pick<+ ManualTopicsDialogProps,+ "operation" | "onToggleFacet"+> & {+ facet: ManualTopicsDialogProps["facets"][number];+ selected: boolean;+};diff --git a/web/src/features/topics/ManualTopicsFormContent.tsx b/web/src/features/topics/ManualTopicsFormContent.tsxnew file mode 100644index 0000000000..2ec9a18f1d--- /dev/null+++ b/web/src/features/topics/ManualTopicsFormContent.tsx@@ -0,0 +1,245 @@+import { useState } from "react";+import { useForm } from "react-hook-form";+import { skipToken } from "@tanstack/react-query";+import { api } from "@/src/utils/api";+import { type TopicProcessInput } from "./TopicPipelineForm";+import { type TopicOperation } from "@langfuse/shared/topics";+import { type TimeRange } from "@/src/utils/date-range-utils";+import { useTopicTraceSelector } from "./TopicTraceSelector";+import { type RegenerateTopics } from "./useRegenerateTopics";+import { type ConfigureTopicsSettingsInput } from "./TopicModelSettings";+import { ManualTopicsDialog } from "./ManualTopicsDialog";+import { manualTopicsSelection } from "./manual-topics-selection";++export function ManualTopicsFormContent({+ projectId,+ canWrite,+ generationTimeRange,+ onGenerationTimeRangeChange,+ settings,+ configurationError,+ regenerate,+ running,+ processError,+ processPending,+ submitProcess,+ closeDialog,+ onTriggered,+ onConfigure,+}: ManualTopicsFormContentProps) {+ const form = useForm<ManualTopicsDraft>({+ defaultValues: {+ operation: "process",+ selectionIntent: null,+ reuse: true,+ smallSample: false,+ },+ });+ const { operation, selectionIntent, reuse, smallSample } = form.watch();+ const [submitted, setSubmitted] = useState(false);+ const isSubmitting = processPending || regenerate.isSubmitting || submitted;+ const { selection, controls } = useTopicTraceSelector({+ projectId,+ enabled: operation === "process",+ filterOptionsEnabled: operation === "process",+ });+ const summaryCounts = api.topics.summaryCounts.useQuery(+ operation === "update" &&+ (!regenerate.disabledReason || isSubmitting) &&+ regenerate.timeRange+ ? {+ projectId,+ facets: regenerate.facets.map(({ facet, version }) => ({+ facetId: facet.id,+ version: version.version,+ })),+ timeRange: regenerate.timeRange,+ }+ : skipToken,+ );+ const availableCounts =+ summaryCounts.data && !summaryCounts.error ? summaryCounts.data : null;+ const facets = regenerate.facets.map(({ facet, version }) => {+ let summaries: number | null = null;+ if (availableCounts) {+ summaries =+ availableCounts.find(+ (count) =>+ count.facetId === facet.id &&+ count.facetVersion === version.version,+ )?.count ?? 0;+ }+ return {+ id: facet.id,+ name: facet.name,+ kind: facet.isBuiltIn ? "Built-in" : "Custom",+ version: version.version,+ description: facet.description || version.prompt,+ summaries,+ };+ });+ const selectedFacetIds = facets+ .filter(+ (facet) => selectionIntent === null || selectionIntent.includes(facet.id),+ )+ .map((facet) => facet.id);+ const selectedFacets = facets+ .filter((facet) => selectedFacetIds.includes(facet.id))+ .map((facet) => ({ facetId: facet.id, version: facet.version }));+ const configured = Boolean(+ settings?.stored.summary &&+ settings.stored.embedding &&+ settings.stored.clustering,+ );+ const selectionStatus = manualTopicsSelection({+ operation,+ summaryCounts: facets+ .filter((facet) => selectedFacetIds.includes(facet.id))+ .map((facet) => facet.summaries ?? 0),+ traceCount: selection?.count ?? 0,+ smallSample,+ });+ const isCountingSummaries =+ operation === "update" &&+ !regenerate.disabledReason &&+ (summaryCounts.isPending || summaryCounts.isFetching);+ const disabledReason = (() => {+ if (isSubmitting) return null;+ if (!canWrite)+ return "You need permission to manage Topics in this project.";+ if (configurationError) return configurationError;+ if (regenerate.prerequisiteReason) return regenerate.prerequisiteReason;+ if (!settings) return "Loading Topics configuration…";+ if (!configured)+ return "Choose the Topics models in Configure Topics first.";+ if (settings.stored.blockedAt)+ return `Topics is paused. ${settings.stored.blockMessage ?? "Fix the model connection in Configure Topics."}`;+ if (!settings.stored.processingAvailable)+ return "Topics processing is not enabled for this project.";+ if (running) return "A Topics run is in progress. Wait for it to finish.";+ if (operation === "update" && regenerate.disabledReason)+ return regenerate.disabledReason;+ if (operation === "update") {+ if (isCountingSummaries) return null;+ if (summaryCounts.error)+ return `Could not count stored summaries: ${summaryCounts.error.message}`;+ if (!facets.some((facet) => (facet.summaries ?? 0) > 0))+ return "No trace summaries in the selected time range yet. Summarize traces first, then regenerate.";+ }+ return selectionStatus.disabledReason;+ })();+ const canSubmit =+ !disabledReason &&+ !isSubmitting &&+ !isCountingSummaries &&+ selectionStatus.ready;+ const model = (slot: "summary" | "embedding" | "clustering") => {+ const value = settings?.stored[slot];+ return {+ model: value?.model ?? "Not configured",+ connection:+ settings?.connections.find(+ (connection) => connection.id === value?.llmApiKeyId,+ )?.provider ?? "Project connection",+ };+ };+ async function submit() {+ if (!canSubmit) return;+ const id = await (async () => {+ if (operation === "update")+ return regenerate.submit({+ facets: selectedFacets,+ exploratory: smallSample,+ minimumTraceCount: smallSample ? 10 : 100,+ });+ if (!selection) return null;+ return submitProcess({+ projectId,+ operation: "process",+ facets: selectedFacets,+ reuseExistingSummaries: reuse,+ ...("traceIds" in selection+ ? { traceIds: selection.traceIds }+ : { selection: selection.selection }),+ });+ })();+ if (id === null) return;+ setSubmitted(true);+ closeDialog();+ onTriggered(id);+ }+ function handleOperationChange(value: TopicOperation) {+ form.setValue("operation", value);+ }+ function handleToggleFacet(id: string, selected: boolean) {+ form.setValue(+ "selectionIntent",+ selected+ ? [...selectedFacetIds, id]+ : selectedFacetIds.filter((value) => value !== id),+ );+ }+ function handleReuseChange(value: boolean) {+ form.setValue("reuse", value);+ }+ function handleSmallSampleChange(value: boolean) {+ form.setValue("smallSample", value);+ }+ function handleConfigure() {+ closeDialog();+ onConfigure();+ }+ return (+ <ManualTopicsDialog+ operation={operation}+ onOperationChange={handleOperationChange}+ facets={facets}+ selectedFacetIds={selectedFacetIds}+ onToggleFacet={handleToggleFacet}+ traceControls={controls}+ count={selection?.count ?? 0}+ generationTimeRange={generationTimeRange}+ onGenerationTimeRangeChange={onGenerationTimeRangeChange}+ models={{+ summary: model("summary"),+ embedding: model("embedding"),+ clustering: model("clustering"),+ }}+ reuse={reuse}+ onReuseChange={handleReuseChange}+ smallSample={smallSample}+ onSmallSampleChange={handleSmallSampleChange}+ canSubmit={canSubmit}+ isSubmitting={isSubmitting}+ isCountingSummaries={isCountingSummaries}+ disabledReason={disabledReason}+ error={operation === "process" ? processError : regenerate.error}+ onSubmit={submit}+ onConfigure={handleConfigure}+ />+ );+}++type ManualTopicsFormContentProps = {+ projectId: string;+ canWrite: boolean;+ generationTimeRange: TimeRange;+ onGenerationTimeRangeChange: (range: TimeRange) => void;+ settings: ConfigureTopicsSettingsInput | null;+ configurationError: string | null;+ regenerate: RegenerateTopics;+ running: boolean;+ processError: string | null;+ processPending: boolean;+ submitProcess: (input: TopicProcessInput) => Promise<string | null>;+ closeDialog: () => void;+ onTriggered: (id: string) => void;+ onConfigure: () => void;+};++type ManualTopicsDraft = {+ operation: TopicOperation;+ selectionIntent: string[] | null;+ reuse: boolean;+ smallSample: boolean;+};diff --git a/web/src/features/topics/README.md b/web/src/features/topics/README.mdindex 63b2efd2c5..7abeebadf1 100644--- a/web/src/features/topics/README.md+++ b/web/src/features/topics/README.md@@ -41,23 +41,42 @@ project permissions. Processing additionally requires the project allowlist.in-flight poll cannot satisfy completion. The query retains the tRPC prefixfor mutation and manual invalidation.The polling/completion race is covered directly by a frontend hook test.- Configure Topics opens a side sheet for models, facets, and which traces to run.+ Configure Topics opens a dialog for models, facets, and which traces to run.Saving it stores the models, creates the four built-in facets as evaluatorswith a first version, and saves one rule for the filter, idle time, andsampling rate. A project can have only one Topics rule.- Process traces or Update topics opens its own run dialog. Overlay owners stay- outside the responsive header menu. Facet versions contain only prompts;+ Manual processing opens a shared dialog with Summarize traces & embed and+ Generate topics tabs. `DialogController` owns the overlay above the page and+ the mobile actions menu. Once models are configured, Configure Topics lives+ in the overflow menu. `ManualTopicsDialog` is the presentational layout shared+ by the app and its isolated `ManualTopicsDialog.stories.tsx` stories.+ `TopicTraceSelectorView` renders the same filters, batch controls and preview+ table in both places; stories supply fixture results without private API calls.+ `ManualTopicsFormContent` owns the draft and uses the real trace selector.+ `manualTopicsSelection` owns selection readiness for both operations, shared+ by the application and Storybook interaction tests. `useTopicPipelineForm` owns process writes;+ `useRegenerateTopics` owns active/latest facet counts and generate writes.+ Mutation errors and request IDs outlive dialog dismissal, so retrying an+ unchanged request does not create a second run. Submission closes the dialog+ and opens the existing execution progress drawer. Permission, setup, pause,+ availability and running-state gates disable submissions; generation also+ requires compatible stored summaries. Facet versions contain only prompts;processing settings and embedding dimensions are frozen on executions.Built-in facets are read-only; custom facets are added on their own page.-- `useTopicPipelineForm` owns the run dialog, operation, facet versions and- submission. The workspace renders its header action and dialog separately;- closing the dialog preserves the current results and selection. Its state- stays mounted when the dialog closes. Preview trace clicks close the run- dialog before opening the trace peek in the panel layer.- Every available facet starts selected. Process traces is the default operation:- it summarizes, embeds, and assigns only the selected batch to current topics.+- Manual processing defaults to all active built-in and custom facets in the+ Topics rule at their latest version. Summarize traces & embed is the default+ tab: it summarizes, embeds, and assigns only the previewed batch to current+ topics. Trace selection includes filters, IDs, time ranges, a maximum batch size,+ and exclusions. Stored summary and embedding reuse is enabled by default in Review & Run.+ The dialog uses the evaluator stepper: Choose facets, Select traces, and+ Review & Run. Generate topics uses Select summaries for the second step.+ All steps start expanded; model details and reuse settings live in review.+ Preview trace clicks open a new tab, preserving the run dialog and selection.Without a compatible map, summaries wait for an explicit Update topics run.- Update topics reads compatible stored summaries directly; each selected facet+ Generate topics defaults to compatible summaries from the past seven days,+ with its picker limited to a maximum thirty-day range. Its own time-range+ picker uses the same frozen range for counts and submission, independently of+ the page and raw trace filters. Generate topics reads compatible stored summaries directly; each selected facetshows its available summary count. It has clustering settings but no tracepicker, source-execution selector, or target-map selector. Embedding dimensionsare configured per execution; updates select matching stored embeddings and@@ -69,8 +88,9 @@ project permissions. Processing additionally requires the project allowlist.choice. Sampling and its limit stay specific to each execution.These local PoC operations add no product analytics event.- `useTopicTraceSelector` reuses the eval filter builder and query editor, with- a time range, all matching traces selected by default, and optional random/latest- sampling with a user-chosen size. Changing the operation resets the+ a time range and random/latest sampling with a user-chosen size. Manual runs+ default to at most 1,000 traces, enforced at the web API boundary for filters+ and pasted IDs. `backfill-limit.ts` owns this fixed PoC limit. Changing the operation resets thetrace-selection draft while retaining sampling settings; dates stay specificto each execution.The hook supplies selection, criteria and controls so the dialog can unmount@@ -79,7 +99,7 @@ project permissions. Processing additionally requires the project allowlist.The form submits reviewed criteria and exclusions. Paste IDs remains available.`server/traceSelection.ts` applies canonical observation filters within amaximum 93-day window, then deduplicates traces before counting and optional- sampling. There is no total trace cap. Preview rows and current results render+ sampling. Manual processing is capped at 1,000 traces per run. Preview rows and current results renderin pages of 20; selected IDs are frozen by the server at submission. All predicates matchthe same observation; the worker reads thewhole containing trace, including observations outside the selection window.diff --git a/web/src/features/topics/TopicModelSettings.tsx b/web/src/features/topics/TopicModelSettings.tsxindex ac8c5e80a0..a7e784062f 100644--- a/web/src/features/topics/TopicModelSettings.tsx+++ b/web/src/features/topics/TopicModelSettings.tsx@@ -82,9 +82,15 @@ export function useTopicModelSettings({}: null;+ const configurationError =+ settings.error ?? readinessSettings.error ?? connections.error;+return {configure: {settings: configureSettings,+ error: configurationError+ ? `Could not load Topics configuration: ${configurationError.message} Reload the page to retry.`+ : null,triggerVariant:configureSettings && !configured? ("primary" as const)diff --git a/web/src/features/topics/TopicPipelineForm.tsx b/web/src/features/topics/TopicPipelineForm.tsxindex 2f1e4a2096..727bb84f80 100644--- a/web/src/features/topics/TopicPipelineForm.tsx+++ b/web/src/features/topics/TopicPipelineForm.tsx@@ -1,387 +1,93 @@-import { Alert } from "@/src/components/design-system/Alert/Alert";import { useRef, useState } from "react";-import { skipToken } from "@tanstack/react-query";-import * as DialogPrimitive from "@radix-ui/react-dialog";-import { Button } from "@/src/components/design-system/Button/Button";-import { Dialog } from "@/src/components/design-system/Dialog/Dialog";-import { Input as NumericInput } from "@/src/components/ui/input";-import { Checkbox } from "@/src/components/design-system/Checkbox/Checkbox";-import { SelectInput } from "@/src/components/design-system/SelectInput/SelectInput";-import { api } from "@/src/utils/api";-import {- topicMinimumTraceCountSchema,- type TopicFacet,- type TopicOperation,- type TopicTimeRange,-} from "@langfuse/shared/topics";-import { useTopicTraceSelector } from "./TopicTraceSelector";+import { api, type RouterInputs } from "@/src/utils/api";+import { type TopicFacet, type TopicFacetRef } from "@langfuse/shared/topics";+import { type ConfigureTopicsSettingsInput } from "./TopicModelSettings";+import { type RegenerateTopics } from "./useRegenerateTopics";+import { type TimeRange } from "@/src/utils/date-range-utils";export function useTopicPipelineForm({projectId,facets,canWrite,onTriggered,- timeRange,+ generationTimeRange,+ onGenerationTimeRangeChange,+ regenerate,+ settings,+ configurationError,+ running,+ onConfigure,}: {projectId: string;facets: TopicFacet[];canWrite: boolean;onTriggered: (id: string) => void;- timeRange: TopicTimeRange | null;+ generationTimeRange: TimeRange;+ onGenerationTimeRangeChange: (range: TimeRange) => void;+ regenerate: RegenerateTopics;+ settings: ConfigureTopicsSettingsInput | null;+ configurationError: string | null;+ running: boolean;+ onConfigure: () => void;}) {- const [runOpen, setRunOpen] = useState(false);- const [operation, setOperation] = useState<TopicOperation>("process");- const [reuseExistingSummaries, setReuseExistingSummaries] = useState(false);- const {- selection,- controls: traceControls,- reset: resetTraceSelection,- } = useTopicTraceSelector({- projectId,- enabled: facets.length > 0 && operation === "process",- filterOptionsEnabled: runOpen,- onOpenTrace: () => setRunOpen(false),- });- const [selectedFacetIds, setSelectedFacetIds] = useState<string[]>(() =>- facets- .filter((facet) => facet.versions.length > 0)- .map((facet) => facet.id),- );- const [facetVersions, setFacetVersions] = useState<Record<string, number>>(- () =>- Object.fromEntries(- facets.map((facet) => [facet.id, facet.versions[0]?.version ?? 0]),- ),- );- const [exploratory, setExploratory] = useState(false);- const [minimumTraceCount, setMinimumTraceCount] = useState<string | null>(- null,- );- const minimumTraceCountValue =- minimumTraceCount ?? (exploratory ? "10" : "100");- const minimumTraceCountResult = topicMinimumTraceCountSchema.safeParse(- Number(minimumTraceCountValue),- );- const [error, setError] = useState<string | null>(null);const request = useRef<{ key: string; id: string } | null>(null);const trigger = api.topics.trigger.useMutation();- const utils = api.useUtils();- const facetChoices = facets.flatMap((facet) => {- const version =- facet.versions.find(- (version) => version.version === facetVersions[facet.id],- ) ?? facet.versions[0];- return version- ? [{ facet, version, selected: selectedFacetIds.includes(facet.id) }]- : [];- });- const selectedFacets = facetChoices- .filter((choice) => choice.selected)- .map((choice) => ({- facetId: choice.facet.id,- version: choice.version.version,- }));- const summaryCounts = api.topics.summaryCounts.useQuery(- timeRange- ? {- projectId,- facets: selectedFacets,- timeRange,- }- : skipToken,- {- enabled:- operation === "update" &&- timeRange !== null &&- selectedFacets.length > 0,- },- );- const summaryCount = (facetId: string, version: number) =>- summaryCounts.data?.find(- (count) => count.facetId === facetId && count.facetVersion === version,- )?.count ?? 0;- const hasStoredSummaries = selectedFacets.some(- ({ facetId, version }) => summaryCount(facetId, version) > 0,- );- const compatibleSummaryLabel = (facetId: string, version: number) => {- if (summaryCounts.isFetching) return "Counting stored summaries…";- if (summaryCounts.error) return "Could not count stored summaries.";- return `${summaryCount(facetId, version).toLocaleString()} compatible summaries ready`;- };- const toggleFacet = (id: string, checked: boolean) =>- setSelectedFacetIds((current) =>- checked ? [...current, id] : current.filter((item) => item !== id),- );- async function submit() {+ const [error, setError] = useState<string | null>(null);+ async function submitProcess(values: TopicProcessInput) {setError(null);+ const key = JSON.stringify(values);+ if (request.current?.key !== key)+ request.current = { key, id: crypto.randomUUID() };try {- if (!selectedFacets.length) throw new Error("Select at least one facet.");- const base = {- projectId,- facets: selectedFacets,- };- const values = (() => {- if (operation === "update") {- if (!timeRange)- throw new Error("Select a time range of at most 90 days.");- return {- ...base,- operation,- exploratory,- timeRange,- minimumTraceCount: topicMinimumTraceCountSchema.parse(- Number(minimumTraceCountValue),- ),- };- }- if (!selection?.count)- throw new Error("Preview and select traces before processing.");- const traceInput =- "traceIds" in selection- ? { traceIds: selection.traceIds }- : { selection: selection.selection };- return {- ...base,- operation,- reuseExistingSummaries,- ...traceInput,- };- })();- const key = JSON.stringify(values);- if (request.current?.key !== key)- request.current = { key, id: crypto.randomUUID() };const result = await trigger.mutateAsync({...values,requestId: request.current.id,});request.current = null;- setRunOpen(false);- onTriggered(result.id);+ return result.id;} catch (cause) {setError(- cause instanceof Error- ? cause.message- : "Could not trigger the pipeline.",+ cause instanceof Error ? cause.message : "Could not summarize traces.",);+ return null;}}- const operationControls = (- <div className="flex flex-wrap items-center justify-between gap-3">- <div className="w-64 max-w-full">- <SelectInput- aria-label="Pipeline operation"- placeholder="Pipeline operation"- options={[- { value: "process", label: "Process traces" },- { value: "update", label: "Update topics" },- ]}- value={operation}- onValueChange={(value) => {- setOperation(value as TopicOperation);- resetTraceSelection();- setError(null);- if (value === "update")- utils.topics.summaryCounts.invalidate({ projectId });- }}- />- </div>- </div>- );- let actionLabel = "Update topics";- if (operation === "process")- actionLabel = selection?.count- ? `Process ${selection.count.toLocaleString()} traces`- : "Process traces";- const canSubmit =- canWrite &&- !trigger.isPending &&- selectedFacets.length > 0 &&- (operation === "update"- ? Boolean(timeRange) &&- minimumTraceCountResult.success &&- !summaryCounts.isFetching &&- !summaryCounts.error &&- hasStoredSummaries- : Boolean(selection?.count));- const triggerAction = {- label: operation === "process" ? "Process traces" : "Update topics",- disabled: !canWrite,- onSelect: () => setRunOpen(true),- };- const primaryAction = (- <Button- text={triggerAction.label}- size="sm"- disabled={triggerAction.disabled}- onClick={triggerAction.onSelect}- />- );- const configuration = (- <DialogPrimitive.Root open={runOpen} onOpenChange={setRunOpen}>- <Dialog- title={operation === "process" ? "Process traces" : "Update topics"}- size="xxl"- closeOnInteractionOutside- >- <Dialog.Body>- <div className="ph-no-capture flex flex-col gap-6">- <p className="text-muted-foreground">- {operation === "process"- ? "Summarize and embed selected traces, then assign them to current topics."- : "Rebuild topics from stored summaries and embeddings in the selected Topics time range."}- </p>- {operationControls}- {operation === "process" && traceControls}- <fieldset className="flex flex-col gap-3">- <legend className="mb-2 text-sm font-bold">Facets</legend>- {facetChoices.map(({ facet, version, selected }) => (- <div- key={facet.id}- className="bg-muted/30 flex flex-col gap-2 rounded-md p-3"- >- <div className="flex flex-wrap items-center justify-between gap-3">- <label className="flex min-w-0 items-center gap-2 text-sm font-bold">- <Checkbox- checked={selected}- onCheckedChange={(value) =>- toggleFacet(facet.id, value === true)- }- />- {facet.name}- </label>- <div className="w-24">- <SelectInput- aria-label={`Version for ${facet.name}`}- placeholder="Version"- value={String(version.version)}- options={facet.versions.map((candidate) => ({- value: String(candidate.version),- label: `v${candidate.version}`,- }))}- onValueChange={(value) =>- setFacetVersions((current) => ({- ...current,- [facet.id]: Number(value),- }))- }- />- </div>- </div>- <p className="text-muted-foreground text-sm">- {version.prompt}- </p>- {operation === "update" && selected && (- <p className="text-muted-foreground text-sm">- {compatibleSummaryLabel(facet.id, version.version)}- </p>- )}- </div>- ))}- </fieldset>- <div className="flex flex-wrap items-end gap-5">- {operation === "update" && (- <label className="flex flex-col gap-1 text-sm">- Minimum traces for clustering- <NumericInput- aria-label="Minimum traces for clustering"- aria-invalid={!minimumTraceCountResult.success}- className="w-28"- type="number"- min={3}- step={1}- value={minimumTraceCountValue}- onChange={(event) =>- setMinimumTraceCount(event.target.value)- }- />- </label>- )}- {operation === "update" && (- <label className="flex items-center gap-2 text-sm">- <Checkbox- checked={exploratory}- onCheckedChange={(value) => setExploratory(value === true)}- />- Small sample mode (smaller, provisional topics)- </label>- )}- </div>- {operation === "process" && (- <div className="flex flex-col gap-1">- <label className="flex items-center gap-2 text-sm">- <Checkbox- checked={reuseExistingSummaries}- onCheckedChange={(value) =>- setReuseExistingSummaries(value === true)- }- />- Reuse stored summaries- </label>- <p className="text-muted-foreground text-xs">- Reuse saved summaries and embeddings for each trace and facet- version. Source changes are not checked. Leave unchecked to- generate fresh results.- </p>- </div>- )}- {operation === "process" && selection?.count ? (- <p className="text-muted-foreground text-sm">- Process {selection.count.toLocaleString()} traces across{" "}- {selectedFacets.length} facets.{" "}- {reuseExistingSummaries- ? "Matching stored summaries and embeddings are reused. Missing ones use the project's Topics models."- : "Generate fresh summaries and embeddings with the project's Topics models."}- </p>- ) : null}- {operation === "process" && (- <p className="text-muted-foreground text-xs">- New summaries are assigned to the latest compatible topics. If- no topics exist yet, summaries wait until you run Update topics.- </p>- )}- {operation === "update" && summaryCounts.error && (- <Alert variant="destructive" size="sm">- <Alert.Description>- <p className="break-words">{summaryCounts.error.message}</p>- </Alert.Description>- </Alert>- )}- {operation === "update" && (- <p className="text-muted-foreground text-xs">- {minimumTraceCountResult.success- ? `Clustering starts with at least ${minimumTraceCountResult.data.toLocaleString()} compatible stored summaries per facet. Below this minimum, process more traces first.`- : "Enter a whole number of at least 3 traces."}{" "}- Small sample mode lowers the minimum topic size from 15 to 3- traces.- </p>- )}- {error && (- <Alert variant="destructive" size="sm">- <Alert.Description>- <p className="break-words">{error}</p>- </Alert.Description>- </Alert>- )}- </div>- </Dialog.Body>- <div className="flex shrink-0 justify-end gap-2 p-4">- <Button- text="Cancel"- variant="secondary"- onClick={() => setRunOpen(false)}- />- <Button- text={trigger.isPending ? "Starting…" : actionLabel}- disabled={!canSubmit}- onClick={submit}- />- </div>- </Dialog>- </DialogPrimitive.Root>- );+ function clearError() {+ if (!trigger.isPending) setError(null);+ regenerate.clearError();+ }return {- primaryAction: facets.length > 0 ? primaryAction : null,- triggerAction: facets.length > 0 ? triggerAction : null,- configuration: facets.length > 0 ? configuration : null,+ available: facets.length > 0,+ clearError,+ dialogProps: {+ projectId,+ canWrite,+ generationTimeRange,+ onGenerationTimeRangeChange,+ settings,+ configurationError,+ regenerate,+ running,+ processError: error,+ processPending: trigger.isPending,+ submitProcess,+ onTriggered,+ onConfigure,+ },};}++export type TopicProcessInput = {+ projectId: string;+ operation: "process";+ facets: TopicFacetRef[];+ reuseExistingSummaries: boolean;+} & (+ | { traceIds: string[] }+ | {+ selection: Extract<+ RouterInputs["topics"]["trigger"],+ { selection: unknown }+ >["selection"];+ }+);diff --git a/web/src/features/topics/TopicSetupNotice.stories.tsx b/web/src/features/topics/TopicSetupNotice.stories.tsxnew file mode 100644index 0000000000..c2419e4e56--- /dev/null+++ b/web/src/features/topics/TopicSetupNotice.stories.tsx@@ -0,0 +1,16 @@+import { fn } from "storybook/test";+import preview from "../../../.storybook/preview";+import { TopicSetupNotice } from "./TopicSetupNotice";++const meta = preview.meta({ component: TopicSetupNotice });++export const Setup = meta.story({+ args: {+ notice: { unavailable: false, status: { kind: "setup" } },+ canWrite: true,+ canConfigure: true,+ onConfigure: fn(),+ onKeepOff: fn(),+ onTurnBackOn: fn(),+ },+});diff --git a/web/src/features/topics/TopicSetupNotice.tsx b/web/src/features/topics/TopicSetupNotice.tsxindex 4e0c2d8687..9cfdb080fc 100644--- a/web/src/features/topics/TopicSetupNotice.tsx+++ b/web/src/features/topics/TopicSetupNotice.tsx@@ -1,3 +1,4 @@+import { Sparkles } from "lucide-react";import { Alert } from "@/src/components/design-system/Alert/Alert";import { Button } from "@/src/components/design-system/Button/Button";import { SplashScreen } from "@/src/components/ui/splash-screen";@@ -63,37 +64,63 @@ export function TopicSetupNotice({</Alert>) : null}{status?.kind === "setup" ? (- <div className="mx-auto w-full max-w-xl pt-12">+ <div className="mx-auto flex w-full max-w-xl flex-col items-center pt-12">+ <Sparkles+ aria-hidden="true"+ className="icon-xl text-muted-foreground"+ /><SplashScreentitle="Find patterns across your traces"description="Topics analyzes each trace along the questions you choose, called facets, and clusters similar answers, so you can see what users ask for, how runs end, and which issues come up along the way."steps={[{- title: "Set up Topics",+ title: "Configure Topics",description:"Pick the models Topics runs on and the facets to analyze, such as intent, outcome, and issues.",+ content: (+ <Button+ text="Configure Topics"+ size="sm"+ disabled={!canWrite || !canConfigure}+ onClick={onConfigure}+ />+ ),},{title: "Analyze traces",description:- "Topics summarizes new traces automatically, and past traces whenever you process them.",+ "After setup, enable automatic processing for incoming traces or summarize existing traces manually.",+ content: (+ <div className="flex flex-wrap gap-2">+ <Button+ text="Automatic processing"+ variant="secondary"+ size="sm"+ disabled+ />+ <Button+ text="Summarize traces & embed"+ variant="secondary"+ size="sm"+ disabled+ />+ </div>+ ),},{title: "Gather insights",description:- "Similar summaries are clustered into topics, on demand or continuously, so patterns across your traces show up here.",+ "Once summaries are available, generate topics to discover recurring patterns across your traces.",+ content: (+ <Button+ text="Generate topics"+ variant="secondary"+ size="sm"+ disabled+ />+ ),},]}- primaryAction={{- label: "Configure Topics",- component: (- <Button- text="Configure Topics"- disabled={!canWrite || !canConfigure}- onClick={onConfigure}- />- ),- }}/></div>) : null}diff --git a/web/src/features/topics/TopicTraceSelector.clienttest.tsx b/web/src/features/topics/TopicTraceSelector.clienttest.tsxindex 7f3df9de9a..0e3edc3d76 100644--- a/web/src/features/topics/TopicTraceSelector.clienttest.tsx+++ b/web/src/features/topics/TopicTraceSelector.clienttest.tsx@@ -14,11 +14,6 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import { useTopicTraceSelector } from "./TopicTraceSelector";const mocks = vi.hoisted(() => ({ fetch: vi.fn() }));-vi.mock("@/src/components/table/peek/hooks/usePeekNavigation", () => ({- usePeekNavigation: () => ({- openPeek: vi.fn(),- }),-}));vi.mock("@/src/utils/api", () => ({api: {topics: {@@ -54,7 +49,6 @@ vi.mock(function TraceSelector() {const { selection, controls } = useTopicTraceSelector({projectId: "project",- onOpenTrace: vi.fn(),enabled: true,filterOptionsEnabled: true,});@@ -109,6 +103,30 @@ beforeEach(() => {afterEach(() => vi.unstubAllGlobals());describe("Topics trace selection", () => {+ it("opens preview traces in a new tab without changing the selected batch", async () => {+ const open = vi.spyOn(window, "open").mockImplementation(() => null);+ mocks.fetch.mockResolvedValue(result("trace-0", "trace-1"));+ setup();+ fireEvent.click(screen.getByRole("button", { name: "Preview traces" }));+ await screen.findByRole("link", { name: "trace-0" });+ fireEvent.click(+ screen.getByRole("checkbox", { name: "Select trace trace-1" }),+ );+ const batch = selected();+ const link = screen.getByRole("link", { name: "trace-0" });+ fireEvent.click(link);+ expect(open).toHaveBeenCalledWith(+ "/project/project/traces/trace-0",+ "_blank",+ "noopener,noreferrer",+ );+ fireEvent.click(link.closest("tr")!);+ expect(open).toHaveBeenCalledTimes(2);+ expect(selected()).toEqual(batch);+ expect(screen.getByRole("link", { name: "trace-0" })).toBeVisible();+ open.mockRestore();+ });+it("freezes only the latest preview and invalidates it immediately when criteria change", async () => {const pending: Array<(value: ReturnType<typeof result>) => void> = [];mocks.fetch.mockImplementation(@@ -119,7 +137,6 @@ describe("Topics trace selection", () => {expect(selected()).toBeNull();fireEvent.click(screen.getByRole("button", { name: "Preview traces" }));await waitFor(() => expect(pending).toHaveLength(1));- fireEvent.click(screen.getByRole("checkbox", { name: "Sample traces" }));fireEvent.change(screen.getByLabelText("Maximum traces"), {target: { value: "50" },});@@ -194,7 +211,7 @@ describe("Topics trace selection", () => {mocks.fetch.mock.calls[1][0].seed,);expect(mocks.fetch.mock.calls[0][0]).toMatchObject({- limit: null,+ limit: 1000,sampling: "random",});});diff --git a/web/src/features/topics/TopicTraceSelector.tsx b/web/src/features/topics/TopicTraceSelector.tsxindex f6dcb2bc5e..221fb0fdcc 100644--- a/web/src/features/topics/TopicTraceSelector.tsx+++ b/web/src/features/topics/TopicTraceSelector.tsx@@ -1,8 +1,4 @@-import { Alert } from "@/src/components/design-system/Alert/Alert";import { useState } from "react";-import { usePeekNavigation } from "@/src/components/table/peek/hooks/usePeekNavigation";-import { type ColumnDef } from "@tanstack/react-table";-import { format } from "date-fns";import {eventsEvalFilterColumns,experimentEvalFilterColsWithOptions,@@ -10,33 +6,23 @@ import {type FilterState,type TimeFilter,} from "@langfuse/shared";-import { api, type RouterInputs, type RouterOutputs } from "@/src/utils/api";-import { Button } from "@/src/components/design-system/Button/Button";-import { TextLink } from "@/src/components/design-system/TextLink/TextLink";-import { Input } from "@/src/components/ui/input";-import { Textarea } from "@/src/components/ui/textarea";-import { Checkbox } from "@/src/components/design-system/Checkbox/Checkbox";-import { Tabs } from "@/src/components/design-system/Tabs/Tabs";-import { SelectInput } from "@/src/components/design-system/SelectInput/SelectInput";-import { Table } from "@/src/components/design-system/table/Table";-import { PaginationBar } from "@/src/components/design-system/PaginationBar/PaginationBar";-import { TableSearchBar, toObservedOptions } from "@/src/features/search-bar";+import { api, type RouterInputs } from "@/src/utils/api";+import { toObservedOptions } from "@/src/features/search-bar";import { useEventsFilterOptions } from "@/src/features/events/hooks/useEventsFilterOptions";-import { RULE_FIELD_REGISTRY } from "@/src/features/evals/v2/constants/ruleSearchRegistry";-import { FilterModeToggle } from "@/src/features/evals/v2/components/Evaluators/Testing/components/SampleObservationSelectorBase/components/FilterModeToggle";-import { ObservationFilterBuilder } from "@/src/features/evals/v2/components/Evaluators/Testing/components/SampleObservationSelectorBase/components/ObservationFilterBuilder/ObservationFilterBuilder";-import { DateRangeInput } from "@/src/features/evals/v2/components/Evaluators/EvaluatorBackfillSettings/components/DateRangeInput/DateRangeInput";+import {+ TopicTraceSelectorView,+ type TopicTraceSelectorViewProps,+} from "./TopicTraceSelectorView";import { parseTraceInput } from "./parse-trace-input";+import { TOPICS_BACKFILL_TRACE_LIMIT } from "./backfill-limit";import {isValidTopicTimeRange,relativeTopicTimeRange,topicCalendarDates,topicCalendarRange,- topicTimeRangePresets,} from "./time-range";type PreviewInput = RouterInputs["topics"]["previewTraces"];-type TracePreview = RouterOutputs["topics"]["previewTraces"]["traces"][number];type TopicTraceSelection = { count: number } & (| { traceIds: string[] }| {@@ -46,25 +32,12 @@ type TopicTraceSelection = { count: number } & (>["selection"];});-const registry = {- ...RULE_FIELD_REGISTRY,- aiFilterPrompt: false,- fields: RULE_FIELD_REGISTRY.fields.filter(- (field) => field.id !== "isRootObservation",- ),- columns: RULE_FIELD_REGISTRY.columns.filter(- (column) => column.id !== "isRootObservation",- ),-};-export function useTopicTraceSelector({projectId,- onOpenTrace,enabled,filterOptionsEnabled,}: {projectId: string;- onOpenTrace: () => void;enabled: boolean;filterOptionsEnabled: boolean;}) {@@ -73,8 +46,7 @@ export function useTopicTraceSelector({const [filter, setFilter] = useState<FilterState>([]);const [timeWindow, setTimeWindow] = useState("7");const [range, setRange] = useState(() => relativeTopicTimeRange(7));- const [sample, setSample] = useState(false);- const [limit, setLimit] = useState("100");+ const [limit, setLimit] = useState(String(TOPICS_BACKFILL_TRACE_LIMIT));const [sampling, setSampling] = useState<"random" | "latest">("random");const [paste, setPaste] = useState("");const [request, setRequest] = useState<PreviewInput | null>(null);@@ -90,14 +62,16 @@ export function useTopicTraceSelector({setExcluded([]);};const validLimit =- !sample || (Number.isSafeInteger(Number(limit)) && Number(limit) >= 1);+ Number.isSafeInteger(Number(limit)) &&+ Number(limit) >= 1 &&+ Number(limit) <= TOPICS_BACKFILL_TRACE_LIMIT;const validRange = isValidTopicTimeRange(range);const preview = api.topics.previewTraces.useQuery(request ?? {projectId,filter: [],...range,- limit: null,+ limit: TOPICS_BACKFILL_TRACE_LIMIT,sampling: "random",seed: "unselected",},@@ -135,7 +109,13 @@ export function useTopicTraceSelector({projectId,typeof window === "undefined" ? "" : window.location.origin,);+ if (pastedIds.length > TOPICS_BACKFILL_TRACE_LIMIT) {+ throw new Error(+ `Select at most ${TOPICS_BACKFILL_TRACE_LIMIT.toLocaleString()} traces per run.`,+ );+ }} catch (error) {+ pastedIds = null;pasteError =error instanceof Error ? error.message : "Invalid trace IDs.";}@@ -190,250 +170,82 @@ export function useTopicTraceSelector({projectId,filter,...range,- limit: sample ? Number(limit) : null,+ limit: Number(limit),sampling,seed: crypto.randomUUID(),});};- let previewLabel = ready ? "Refresh selection" : "Preview traces";- if (request !== null && preview.isFetching) previewLabel = "Loading preview…";-+ const handleModeChange = (value: string) => {+ setMode(value);+ setRequest(null);+ };+ const handleFilterChange = (value: FilterState) => {+ setFilter(value);+ setRequest(null);+ };+ const handleTimeWindowChange = (value: string) => {+ setTimeWindow(value);+ setRequest(null);+ setRange(+ value === "custom"+ ? topicCalendarRange(topicCalendarDates(range))+ : relativeTopicTimeRange(Number(value)),+ );+ };+ const handleRangeChange: TopicTraceSelectorViewProps["onRangeChange"] = (+ value,+ ) => {+ setRange(value);+ setRequest(null);+ };+ const handleLimitChange = (value: string) => {+ setLimit(value);+ setRequest(null);+ };+ const handleSamplingChange: TopicTraceSelectorViewProps["onSamplingChange"] =+ (value) => {+ setSampling(value);+ setRequest(null);+ };+ let previewState: TopicTraceSelectorViewProps["preview"] = { status: "idle" };+ if (ready && request) {+ previewState = { status: "ready", data: ready, seed: request.seed };+ } else if (request && preview.isFetching) {+ previewState = { status: "loading" };+ } else if (request && preview.error) {+ previewState = { status: "error", message: preview.error.message };+ }const controls = (- <div className="ph-no-capture flex min-w-0 flex-col gap-4">- <Tabs- value={mode}- onValueChange={(value) => {- setMode(value);- setRequest(null);- }}- >- <Tabs.List- variant="inset"- size="md"- aria-label="Trace selection method"- >- <Tabs.Trigger value="filters" label="Filters" />- <Tabs.Trigger value="paste" label="Paste IDs" />- </Tabs.List>- </Tabs>- {mode === "filters" ? (- <>- <div className="flex flex-wrap items-end gap-3">- <label className="flex w-44 flex-col gap-1 text-sm">- Time range- <SelectInput- aria-label="Trace time range"- placeholder="Time range"- options={topicTimeRangePresets}- value={timeWindow}- onValueChange={(value) => {- setTimeWindow(value);- setRequest(null);- setRange(- value === "custom"- ? topicCalendarRange(topicCalendarDates(range))- : relativeTopicTimeRange(Number(value)),- );- }}- />- </label>- <label className="flex h-8 items-center gap-2 text-sm">- <Checkbox- checked={sample}- onCheckedChange={(value) => {- setSample(value === true);- setRequest(null);- }}- />- Sample traces- </label>- {sample && (- <>- <label className="flex flex-col gap-1 text-sm">- Sample up to- <Input- aria-label="Maximum traces"- className="w-28"- type="number"- min={1}- value={limit}- onChange={(event) => {- setLimit(event.target.value);- setRequest(null);- }}- />- </label>- <label className="flex w-44 flex-col gap-1 text-sm">- Selection- <SelectInput- aria-label="Trace sampling method"- placeholder="Selection"- options={[- { value: "random", label: "Random sample" },- { value: "latest", label: "Newest first" },- ]}- value={sampling}- onValueChange={(value) => {- setSampling(value as "random" | "latest");- setRequest(null);- }}- />- </label>- </>- )}- </div>- {timeWindow === "custom" && (- <DateRangeInput- value={topicCalendarDates(range)}- max={format(new Date(), "yyyy-MM-dd")}- fromAriaLabel="Trace start date"- toAriaLabel="Trace end date"- onValueChange={(value) => {- setRange(topicCalendarRange(value));- setRequest(null);- }}- />- )}- <div className="flex flex-wrap items-center justify-between gap-2">- <h3 className="text-sm font-bold">Filter observations</h3>- <FilterModeToggle mode={filterMode} onChange={setFilterMode} />- </div>- {filterMode === "builder" ? (- <ObservationFilterBuilder- columns={builderColumns}- filterState={filter}- queryOnlyColumnIds={registry.fields- .filter((field) => field.directFilter === false)- .map((field) => field.filterColumn ?? field.id)}- onChange={(value) => {- setFilter(value);- setRequest(null);- }}- />- ) : (- <>- <TableSearchBar- projectId={projectId}- tableName="topics-trace-selection"- registry={registry}- filterState={filter}- setFilterState={(value) => {- setFilter(value);- setRequest(null);- }}- observed={- observed- ? { ...observed, tags: observed.traceTags ?? [] }- : undefined- }- isV4- onRequestColumns={options.requestColumns}- erroredColumns={options.erroredColumns}- />- <p className="text-muted-foreground text-xs">- Press Enter to apply your query before previewing.- </p>- </>- )}- <p className="text-muted-foreground text-sm">- A trace is included when an observation matches all filters in this- time range. Topics summarizes the available conversation in the- trace, including messages outside these filters.- </p>- {!validLimit && (- <Alert variant="destructive" size="sm">- <Alert.Description>- <p className="break-words">- Enter a positive whole number of traces.- </p>- </Alert.Description>- </Alert>- )}- {!validRange && (- <Alert variant="destructive" size="sm">- <Alert.Description>- <p className="break-words">- Choose a start before the end, within a 93-day window.- </p>- </Alert.Description>- </Alert>- )}- <div className="self-start">- <Button- text={previewLabel}- type="button"- variant="secondary"- disabled={- !validLimit ||- !validRange ||- (request !== null && preview.isFetching)- }- onClick={previewTraces}- />- </div>- {request !== null && preview.error && (- <Alert variant="destructive" size="sm">- <Alert.Description>- <p className="break-words">{preview.error.message}</p>- </Alert.Description>- </Alert>- )}- {ready ? (- <>- <p role="status" className="text-sm">- {ready.matchedTraceCount.toLocaleString()} matching traces ·{" "}- {selectedCount.toLocaleString()} selected- {request?.limit- ? ` · sample of ${ready.selectedTraceCount.toLocaleString()}`- : " · all matches selected"}- </p>- <p className="text-muted-foreground text-xs">- Showing {ready.traces.length.toLocaleString()} preview traces.- The matching traces are selected when you run Topics; counts may- change as new data arrives. Unchecked traces are excluded.- </p>- <TracePreviewTable- key={request?.seed}- projectId={projectId}- traces={ready.traces}- excluded={excluded}- onExcludedChange={setExcluded}- onOpenTrace={onOpenTrace}- />- </>- ) : (- <p className="text-muted-foreground text-xs">- Preview the matching traces before running topics. Previewing does- not use a model.- </p>- )}- </>- ) : (- <label className="flex flex-col gap-2 text-sm font-bold">- Trace IDs or links- <Textarea- aria-label="Trace IDs or links"- value={paste}- onChange={(event) => setPaste(event.target.value)}- placeholder="One trace ID or local trace link per line"- className="min-h-32 font-mono text-xs"- />- <span className="text-muted-foreground font-normal">- Unique trace IDs from this project.- {pastedIds ? ` ${pastedIds.length} selected.` : ""}- </span>- {pasteError && (- <span- role="alert"- className="text-destructive font-normal break-words"- >- {pasteError}- </span>- )}- </label>- )}- </div>+ <TopicTraceSelectorView+ projectId={projectId}+ mode={mode}+ onModeChange={handleModeChange}+ filterMode={filterMode}+ onFilterModeChange={setFilterMode}+ filter={filter}+ onFilterChange={handleFilterChange}+ timeWindow={timeWindow}+ range={range}+ onTimeWindowChange={handleTimeWindowChange}+ onRangeChange={handleRangeChange}+ limit={limit}+ onLimitChange={handleLimitChange}+ sampling={sampling}+ onSamplingChange={handleSamplingChange}+ paste={paste}+ onPasteChange={setPaste}+ pastedCount={pastedIds?.length ?? null}+ pasteError={pasteError}+ preview={previewState}+ onPreview={previewTraces}+ excluded={excluded}+ onExcludedChange={setExcluded}+ builderColumns={builderColumns}+ observed={observed}+ onRequestColumns={options.requestColumns}+ erroredColumns={options.erroredColumns}+ />);let selection: TopicTraceSelection | null = null;if (mode === "paste") {@@ -461,133 +273,3 @@ export function useTopicTraceSelector({reset,};}--function TracePreviewTable({- projectId,- traces,- excluded,- onExcludedChange,- onOpenTrace,-}: {- projectId: string;- traces: TracePreview[];- excluded: string[];- onExcludedChange: (ids: string[]) => void;- onOpenTrace: () => void;-}) {- const { openPeek } = usePeekNavigation({- tableName: "topics-traces",- isV4: false,- queryParams: ["observation", "display", "timestamp", "traceId"],- });- const openTrace = (traceId: string) => {- onOpenTrace();- openPeek(traceId);- };- const [page, setPage] = useState(0);- const excludedIds = new Set(excluded);- const selectedCount = traces.length - excludedIds.size;- const pageCount = Math.max(1, Math.ceil(traces.length / 20));- const allChecked: boolean | "indeterminate" = (() => {- if (selectedCount === traces.length && traces.length > 0) return true;- if (selectedCount > 0) return "indeterminate";- return false;- })();- const columns: ColumnDef<TracePreview>[] = [- {- id: "selected",- size: 48,- enableResizing: false,- header: () => (- <Checkbox- aria-label="Select all previewed traces"- checked={allChecked}- disabled={traces.length === 0}- onCheckedChange={(checked) =>- onExcludedChange(- checked === true ? [] : traces.map((trace) => trace.id),- )- }- />- ),- cell: ({ row }) => (- <Checkbox- aria-label={`Select trace ${row.original.id}`}- checked={!excludedIds.has(row.original.id)}- onCheckedChange={(checked) =>- onExcludedChange(- checked === true- ? excluded.filter((id) => id !== row.original.id)- : excluded.concat(row.original.id),- )- }- />- ),- },- {- accessorKey: "name",- header: "Trace",- size: 300,- cell: ({ row: { original: trace } }) => (- <div className="min-w-0">- <TextLink- path={`/project/${projectId}/traces/${encodeURIComponent(trace.id)}`}- value={trace.name ?? trace.id}- onClick={() => openTrace(trace.id)}- />- {trace.name && (- <span- title={trace.id}- className="text-muted-foreground block truncate font-mono text-xs"- >- {trace.id}- </span>- )}- </div>- ),- },- {- accessorKey: "timestamp",- header: "Matching time",- size: 176,- cell: ({ row }) => new Date(row.original.timestamp).toLocaleString(),- },- {- accessorKey: "environment",- header: "Environment",- size: 128,- cell: ({ row }) => (- <span title={row.original.environment} className="block truncate">- {row.original.environment}- </span>- ),- },- ];- return (- <div className="flex min-w-0 flex-col gap-2">- <div className="max-h-96 overflow-auto rounded-md border">- <div className="min-w-[36rem]">- <Table- tableName="Topics trace preview"- columns={columns}- data={{- status: "success",- data: traces.slice(page * 20, (page + 1) * 20),- }}- onRowClick={(trace) => openTrace(trace.id)}- noResultsMessage="No traces match these filters and time range."- />- </div>- </div>- {pageCount > 1 && (- <PaginationBar- mode="offset"- totalCount={traces.length}- state={{ pageIndex: page, pageSize: 20 }}- onChange={(next) => setPage(next.pageIndex)}- pageSizeOptions={[20]}- />- )}- </div>- );-}diff --git a/web/src/features/topics/TopicTraceSelectorView.tsx b/web/src/features/topics/TopicTraceSelectorView.tsxnew file mode 100644index 0000000000..1456f89410--- /dev/null+++ b/web/src/features/topics/TopicTraceSelectorView.tsx@@ -0,0 +1,481 @@+import { useState, type ChangeEvent, type ComponentProps } from "react";+import { Alert } from "@/src/components/design-system/Alert/Alert";+import { env } from "@/src/env.mjs";+import { type ColumnDef } from "@tanstack/react-table";+import { type FilterState } from "@langfuse/shared";+import { type RouterOutputs } from "@/src/utils/api";+import { Button } from "@/src/components/design-system/Button/Button";+import { TextLink } from "@/src/components/design-system/TextLink/TextLink";+import { Input } from "@/src/components/design-system/Input/Input";+import { Textarea } from "@/src/components/ui/textarea";+import { Checkbox } from "@/src/components/design-system/Checkbox/Checkbox";+import { Tabs } from "@/src/components/design-system/Tabs/Tabs";+import { SelectInput } from "@/src/components/design-system/SelectInput/SelectInput";+import { Table } from "@/src/components/design-system/table/Table";+import { PaginationBar } from "@/src/components/design-system/PaginationBar/PaginationBar";+import { TableSearchBar } from "@/src/features/search-bar";+import { RULE_FIELD_REGISTRY } from "@/src/features/evals/v2/constants/ruleSearchRegistry";+import { FilterModeToggle } from "@/src/features/evals/v2/components/Evaluators/Testing/components/SampleObservationSelectorBase/components/FilterModeToggle";+import { ObservationFilterBuilder } from "@/src/features/evals/v2/components/Evaluators/Testing/components/SampleObservationSelectorBase/components/ObservationFilterBuilder/ObservationFilterBuilder";+import { TimeRangePicker } from "@/src/components/date-picker";+import { TIME_RANGES, type TimeRange } from "@/src/utils/date-range-utils";+import { TOPICS_BACKFILL_TRACE_LIMIT } from "./backfill-limit";+import { isValidTopicTimeRange } from "./time-range";++const registry = {+ ...RULE_FIELD_REGISTRY,+ aiFilterPrompt: false,+ fields: RULE_FIELD_REGISTRY.fields.filter(+ (field) => field.id !== "isRootObservation",+ ),+ columns: RULE_FIELD_REGISTRY.columns.filter(+ (column) => column.id !== "isRootObservation",+ ),+};++const traceTimeRanges = {+ "1": "last1Day",+ "7": "last7Days",+ "30": "last30Days",+ "90": "last90Days",+} as const;++export function TopicTraceSelectorView({+ projectId,+ mode,+ onModeChange,+ filterMode,+ onFilterModeChange,+ filter,+ onFilterChange,+ timeWindow,+ range,+ onTimeWindowChange,+ onRangeChange,+ limit,+ onLimitChange,+ sampling,+ onSamplingChange,+ paste,+ onPasteChange,+ pastedCount,+ pasteError,+ preview,+ onPreview,+ excluded,+ onExcludedChange,+ builderColumns,+ observed,+ onRequestColumns,+ erroredColumns,+}: TopicTraceSelectorViewProps) {+ const validLimit =+ Number.isSafeInteger(Number(limit)) &&+ Number(limit) >= 1 &&+ Number(limit) <= TOPICS_BACKFILL_TRACE_LIMIT;+ const validRange = isValidTopicTimeRange(range);+ const ready = preview.status === "ready" ? preview.data : undefined;+ const excludedIds = new Set(excluded);+ const selectedCount = ready+ ? ready.selectedTraceCount -+ ready.traces.filter((trace) => excludedIds.has(trace.id)).length+ : 0;+ const previewLabel = {+ idle: "Preview traces",+ loading: "Loading preview…",+ ready: "Refresh selection",+ error: "Preview traces",+ }[preview.status];+ const timeRange: TimeRange =+ timeWindow === "custom"+ ? range+ : {+ range:+ traceTimeRanges[timeWindow as keyof typeof traceTimeRanges] ??+ "last7Days",+ };+ function handleTimeRangeChange(value: TimeRange) {+ if ("from" in value) {+ onTimeWindowChange("custom");+ onRangeChange(value);+ return;+ }+ const minutes =+ TIME_RANGES[value.range as keyof typeof TIME_RANGES].minutes;+ if (minutes === null) return;+ onTimeWindowChange(String(minutes / (24 * 60)));+ }+ function handleLimitChange(event: ChangeEvent<HTMLInputElement>) {+ onLimitChange(event.target.value);+ }+ function handleSamplingChange(value: string) {+ onSamplingChange(value as "random" | "latest");+ }+ function handlePasteChange(event: ChangeEvent<HTMLTextAreaElement>) {+ onPasteChange(event.target.value);+ }+ return (+ <div className="ph-no-capture flex min-w-0 flex-col gap-4">+ <Tabs value={mode} onValueChange={onModeChange}>+ <Tabs.List+ variant="inset"+ size="md"+ aria-label="Trace selection method"+ >+ <Tabs.Trigger value="filters" label="Filters" />+ <Tabs.Trigger value="paste" label="Paste IDs" />+ </Tabs.List>+ </Tabs>+ {mode === "filters" ? (+ <>+ <div className="flex flex-wrap items-end gap-3">+ <div>+ <TimeRangePicker+ timeRange={timeRange}+ onTimeRangeChange={handleTimeRangeChange}+ timeRangePresets={[+ "last1Day",+ "last7Days",+ "last30Days",+ "last90Days",+ ]}+ maxRangeMs={93 * 86_400_000}+ className="my-0"+ />+ </div>+ <label className="flex flex-col gap-1 text-sm">+ Maximum traces+ <div className="w-28">+ <Input+ aria-label="Maximum traces"+ type="number"+ min={1}+ value={limit}+ onChange={handleLimitChange}+ />+ </div>+ </label>+ <label className="flex w-44 flex-col gap-1 text-sm">+ Selection+ <SelectInput+ aria-label="Trace sampling method"+ placeholder="Selection"+ options={[+ { value: "random", label: "Random sample" },+ { value: "latest", label: "Newest first" },+ ]}+ value={sampling}+ onValueChange={handleSamplingChange}+ />+ </label>+ </div>+ <p className="text-muted-foreground text-xs">+ Process up to {TOPICS_BACKFILL_TRACE_LIMIT.toLocaleString()}{" "}+ matching traces per run.+ </p>+ <div className="flex flex-wrap items-center justify-between gap-2">+ <h3 className="text-sm font-bold">Filter observations</h3>+ <FilterModeToggle mode={filterMode} onChange={onFilterModeChange} />+ </div>+ {filterMode === "builder" ? (+ <ObservationFilterBuilder+ columns={builderColumns}+ filterState={filter}+ queryOnlyColumnIds={registry.fields+ .filter((field) => field.directFilter === false)+ .map((field) => field.filterColumn ?? field.id)}+ onChange={onFilterChange}+ />+ ) : (+ <>+ <TableSearchBar+ projectId={projectId}+ tableName="topics-trace-selection"+ registry={registry}+ filterState={filter}+ setFilterState={onFilterChange}+ observed={+ observed+ ? { ...observed, tags: observed.traceTags ?? [] }+ : undefined+ }+ isV4+ onRequestColumns={onRequestColumns}+ erroredColumns={erroredColumns}+ />+ <p className="text-muted-foreground text-xs">+ Press Enter to apply your query before previewing.+ </p>+ </>+ )}+ <p className="text-muted-foreground text-sm">+ A trace is included when an observation matches all filters in this+ time range. Topics summarizes the available conversation in the+ trace, including messages outside these filters.+ </p>+ {!validLimit && (+ <Alert variant="destructive" size="sm">+ <Alert.Description>+ <p className="break-words">+ Enter between 1 and{" "}+ {TOPICS_BACKFILL_TRACE_LIMIT.toLocaleString()} traces.+ </p>+ </Alert.Description>+ </Alert>+ )}+ {!validRange && (+ <Alert variant="destructive" size="sm">+ <Alert.Description>+ <p className="break-words">+ Choose a start before the end, within a 93-day window.+ </p>+ </Alert.Description>+ </Alert>+ )}+ <div className="self-start">+ <Button+ text={previewLabel}+ type="button"+ variant={ready ? "secondary" : "primary"}+ disabled={+ !validLimit || !validRange || preview.status === "loading"+ }+ onClick={onPreview}+ />+ </div>+ {preview.status === "error" && (+ <Alert variant="destructive" size="sm">+ <Alert.Description>+ <p className="break-words">{preview.message}</p>+ </Alert.Description>+ </Alert>+ )}+ {ready ? (+ <>+ <p role="status" className="text-sm">+ {ready.matchedTraceCount.toLocaleString()} matching traces ·{" "}+ {selectedCount.toLocaleString()} selected+ {` · sample of ${ready.selectedTraceCount.toLocaleString()}`}+ </p>+ <p className="text-muted-foreground text-xs">+ Showing {ready.traces.length.toLocaleString()} preview traces.+ The matching traces are selected when you run Topics; counts may+ change as new data arrives. Unchecked traces are excluded.+ </p>+ <TracePreviewTable+ key={preview.status === "ready" ? preview.seed : undefined}+ projectId={projectId}+ traces={ready.traces}+ excluded={excluded}+ onExcludedChange={onExcludedChange}+ />+ </>+ ) : (+ <p className="text-muted-foreground text-xs">+ First preview and select your traces. Then choose Summarize &+ embed to start the run. Previewing does not use a model.+ </p>+ )}+ </>+ ) : (+ <label className="flex flex-col gap-2 text-sm font-bold">+ Trace IDs or links+ <Textarea+ aria-label="Trace IDs or links"+ value={paste}+ onChange={handlePasteChange}+ placeholder="One trace ID or local trace link per line"+ className="min-h-32 font-mono text-xs"+ />+ <span className="text-muted-foreground font-normal">+ Up to {TOPICS_BACKFILL_TRACE_LIMIT.toLocaleString()} unique trace+ IDs from this project.+ {pastedCount !== null ? ` ${pastedCount} selected.` : ""}+ </span>+ {pasteError && (+ <span+ role="alert"+ className="text-destructive font-normal break-words"+ >+ {pasteError}+ </span>+ )}+ </label>+ )}+ </div>+ );+}++function TracePreviewTable({+ projectId,+ traces,+ excluded,+ onExcludedChange,+}: {+ projectId: string;+ traces: TracePreview[];+ excluded: string[];+ onExcludedChange: (ids: string[]) => void;+}) {+ const openTrace = (traceId: string) => {+ const basePath = env.NEXT_PUBLIC_BASE_PATH ?? "";+ window.open(+ `${basePath}/project/${projectId}/traces/${encodeURIComponent(traceId)}`,+ "_blank",+ "noopener,noreferrer",+ );+ };+ const [page, setPage] = useState(0);+ const excludedIds = new Set(excluded);+ const selectedCount = traces.length - excludedIds.size;+ const pageCount = Math.max(1, Math.ceil(traces.length / 20));+ const allChecked: boolean | "indeterminate" = (() => {+ if (selectedCount === traces.length && traces.length > 0) return true;+ if (selectedCount > 0) return "indeterminate";+ return false;+ })();+ function handleSelectAll(checked: boolean | "indeterminate") {+ onExcludedChange(checked === true ? [] : traces.map((trace) => trace.id));+ }+ function handleRowClick(trace: TracePreview) {+ openTrace(trace.id);+ }+ function handlePageChange(next: { pageIndex: number }) {+ setPage(next.pageIndex);+ }+ const columns: ColumnDef<TracePreview>[] = [+ {+ id: "selected",+ size: 48,+ enableResizing: false,+ header: () => (+ <Checkbox+ aria-label="Select all previewed traces"+ checked={allChecked}+ disabled={traces.length === 0}+ onCheckedChange={handleSelectAll}+ />+ ),+ cell: ({ row }) => {+ function handleCheckedChange(checked: boolean | "indeterminate") {+ onExcludedChange(+ checked === true+ ? excluded.filter((id) => id !== row.original.id)+ : excluded.concat(row.original.id),+ );+ }+ return (+ <Checkbox+ aria-label={`Select trace ${row.original.id}`}+ checked={!excludedIds.has(row.original.id)}+ onCheckedChange={handleCheckedChange}+ />+ );+ },+ },+ {+ accessorKey: "name",+ header: "Trace",+ size: 300,+ cell: ({ row: { original: trace } }) => {+ function handleClick() {+ openTrace(trace.id);+ }+ return (+ <div className="min-w-0">+ <TextLink+ path={`/project/${projectId}/traces/${encodeURIComponent(trace.id)}`}+ value={trace.name ?? trace.id}+ onClick={handleClick}+ />+ {trace.name && (+ <span+ title={trace.id}+ className="text-muted-foreground block truncate font-mono text-xs"+ >+ {trace.id}+ </span>+ )}+ </div>+ );+ },+ },+ {+ accessorKey: "timestamp",+ header: "Matching time",+ size: 176,+ cell: ({ row }) => new Date(row.original.timestamp).toLocaleString(),+ },+ {+ accessorKey: "environment",+ header: "Environment",+ size: 128,+ cell: ({ row }) => (+ <span title={row.original.environment} className="block truncate">+ {row.original.environment}+ </span>+ ),+ },+ ];+ return (+ <div className="flex h-96 min-h-0 min-w-0 flex-col overflow-hidden rounded-md border">+ <Table+ tableName="Topics trace preview"+ columns={columns}+ data={{+ status: "success",+ data: traces.slice(page * 20, (page + 1) * 20),+ }}+ onRowClick={handleRowClick}+ noResultsMessage="No traces match these filters and time range."+ />+ {pageCount > 1 && (+ <PaginationBar+ mode="offset"+ totalCount={traces.length}+ state={{ pageIndex: page, pageSize: 20 }}+ onChange={handlePageChange}+ pageSizeOptions={[20]}+ />+ )}+ </div>+ );+}++type PreviewData = RouterOutputs["topics"]["previewTraces"];+type TracePreview = PreviewData["traces"][number];+type Range = { from: Date; to: Date };+type PreviewState =+ | { status: "idle" | "loading" }+ | { status: "error"; message: string }+ | { status: "ready"; data: PreviewData; seed: string };++export type TopicTraceSelectorViewProps = {+ projectId: string;+ mode: string;+ onModeChange: (value: string) => void;+ filterMode: "builder" | "query";+ onFilterModeChange: (value: "builder" | "query") => void;+ filter: FilterState;+ onFilterChange: (value: FilterState) => void;+ timeWindow: string;+ range: Range;+ onTimeWindowChange: (value: string) => void;+ onRangeChange: (value: Range) => void;+ limit: string;+ onLimitChange: (value: string) => void;+ sampling: "random" | "latest";+ onSamplingChange: (value: "random" | "latest") => void;+ paste: string;+ onPasteChange: (value: string) => void;+ pastedCount: number | null;+ pasteError: string | null;+ preview: PreviewState;+ onPreview: () => void;+ excluded: string[];+ onExcludedChange: (value: string[]) => void;+ builderColumns: ComponentProps<typeof ObservationFilterBuilder>["columns"];+ observed: ComponentProps<typeof TableSearchBar>["observed"];+ onRequestColumns: ComponentProps<typeof TableSearchBar>["onRequestColumns"];+ erroredColumns: ComponentProps<typeof TableSearchBar>["erroredColumns"];+};diff --git a/web/src/features/topics/TopicsHeaderActions.tsx b/web/src/features/topics/TopicsHeaderActions.tsxnew file mode 100644index 0000000000..631a95e254--- /dev/null+++ b/web/src/features/topics/TopicsHeaderActions.tsx@@ -0,0 +1,101 @@+import { MoreHorizontal, Settings } from "lucide-react";+import { Button } from "@/src/components/design-system/Button/Button";+import { DropdownMenu } from "@/src/components/design-system/DropdownMenu/DropdownMenu";+import { IconButton } from "@/src/components/design-system/IconButton/IconButton";+import { AutomaticProcessingControl } from "./AutomaticProcessingControl";+import { type useTopicModelSettings } from "./TopicModelSettings";++export function TopicsHeaderActions({+ automaticProcessing,+ configure,+ configured,+ canWrite,+ manualAvailable,+ openManual,+ onAutomaticProcessing,+ onConfigure,+ onHistory,+ onRefresh,+}: TopicsHeaderActionsProps) {+ return (+ <div className="ph-no-capture flex items-center justify-end gap-1">+ <AutomaticProcessingControl+ state={automaticProcessing.state}+ canWrite={canWrite && !automaticProcessing.saving}+ onOpen={onAutomaticProcessing}+ />+ {!configured ? (+ <Button+ text="Configure Topics"+ variant={configure.triggerVariant}+ size="sm"+ disabled={!configure.settings}+ onClick={onConfigure}+ />+ ) : null}+ {manualAvailable ? (+ <Button+ text="Manual processing"+ variant="secondary"+ size="sm"+ onClick={openManual}+ />+ ) : null}+ <DropdownMenu+ ariaLabel="Topics actions"+ placement="bottom-end"+ items={[+ ...(configured+ ? [+ {+ id: "configure",+ type: "item" as const,+ title: "Configure Topics",+ icon: Settings,+ onClick: onConfigure,+ },+ ]+ : []),+ {+ id: "history",+ type: "item",+ title: "History",+ onClick: onHistory,+ },+ {+ id: "refresh",+ type: "item",+ title: "Refresh results",+ onClick: onRefresh,+ },+ ]}+ >+ {({ getTriggerProps }) => (+ <IconButton+ {...getTriggerProps()}+ icon={MoreHorizontal}+ label="Topics actions"+ size="sm"+ />+ )}+ </DropdownMenu>+ </div>+ );+}++type ModelSettings = ReturnType<typeof useTopicModelSettings>;+type TopicsHeaderActionsProps = {+ automaticProcessing: Pick<+ ModelSettings["automaticProcessing"],+ "state" | "saving"+ >;+ configure: Pick<ModelSettings["configure"], "settings" | "triggerVariant">;+ configured: boolean;+ canWrite: boolean;+ manualAvailable: boolean;+ openManual: () => void;+ onAutomaticProcessing: () => void;+ onConfigure: () => void;+ onHistory: () => void;+ onRefresh: () => void;+};diff --git a/web/src/features/topics/TopicsPage.clienttest.tsx b/web/src/features/topics/TopicsPage.clienttest.tsxindex f86512247f..5f6eb63d37 100644--- a/web/src/features/topics/TopicsPage.clienttest.tsx+++ b/web/src/features/topics/TopicsPage.clienttest.tsx@@ -1,9 +1,16 @@import type { ReactNode } from "react";-import { fireEvent, render, screen, within } from "@testing-library/react";+import {+ fireEvent,+ render,+ screen,+ waitFor,+ within,+} from "@testing-library/react";import { afterEach, beforeEach, expect, it, vi } from "vitest";import TopicsPage from "./TopicsPage";import { useGlobalDateRangeStore } from "@/src/features/global-time-range/globalDateRangeStore";import { rangeToString, type TimeRange } from "@/src/utils/date-range-utils";+import { type TopicFacet } from "@langfuse/shared/topics";const state = vi.hoisted(() => ({status: "failed",@@ -15,8 +22,24 @@ const state = vi.hoisted(() => ({executionId: undefined as string | undefined,setQueryParams: vi.fn(),currentTopics: vi.fn(),- pipeline: vi.fn(),picker: vi.fn(),+ push: vi.fn(),+ openConfigure: vi.fn(),+ canWrite: true,+ facets: [] as TopicFacet[],+ executions: [] as unknown[] | undefined,+ executionsError: null as Error | null,+ rulesPending: false,+ rulesError: null as Error | null,+ configurationFailure: null as "settings" | "readiness" | "connections" | null,+ settings: {} as Record<string, unknown>,+ rules: [] as Array<{ facetIds: string[] }>,+ countsPending: false,+ countsFetching: false,+ summaryCountInput: vi.fn(),+ invalidateSummaryCounts: vi.fn(),+ counts: [] as Array<{ facetId: string; facetVersion: number; count: number }>,+ trigger: vi.fn(),}));vi.mock("use-query-params", () => ({@@ -34,19 +57,45 @@ vi.mock("@/src/components/date-picker", () => ({}) => {state.picker(props.timeRange);return (- <button onClick={() => props.onTimeRangeChange({ range: "last30Days" })}>- Select last 30 days- </button>+ <>+ <button+ onClick={() => props.onTimeRangeChange({ range: "last30Days" })}+ >+ Select last 30 days+ </button>+ <button+ onClick={() =>+ props.onTimeRangeChange({+ from: new Date("2026-10-01T00:00:00Z"),+ to: new Date("2026-10-08T00:00:00Z"),+ })+ }+ >+ Select custom dates+ </button>+ </>);},}));vi.mock("next/router", () => ({useRouter: () => ({query: { projectId: "project", executionId: state.executionId },+ push: state.push,}),}));vi.mock("@/src/components/layouts/page", () => ({- default: ({ children }: { children: ReactNode }) => children,+ default: ({+ children,+ headerProps,+ }: {+ children: ReactNode;+ headerProps?: { actionButtonsRight?: ReactNode };+ }) => (+ <>+ {headerProps?.actionButtonsRight}+ {children}+ </>+ ),}));vi.mock("@/src/components/table/peek/hooks/usePeekNavigation", () => ({usePeekNavigation: () => ({}),@@ -55,31 +104,27 @@ vi.mock("@/src/components/table/peek/peek-trace-detail", () => ({TablePeekViewTraceDetail: () => null,}));vi.mock("@/src/features/rbac/utils/checkProjectAccess", () => ({- useHasProjectAccess: () => true,+ useHasProjectAccess: () => state.canWrite,}));vi.mock("@/src/features/feature-flags/hooks/useIsFeatureEnabled", () => ({default: () => true,}));-vi.mock("./TopicPipelineForm", () => ({- useTopicPipelineForm: (input: unknown) => {- state.pipeline(input);- return {- primaryAction: null,- triggerAction: null,- configuration: null,- };- },-}));-vi.mock("./TopicModelSettings", () => ({- useTopicModelSettings: () => ({- configure: { settings: null, triggerVariant: "secondary" },- automaticProcessing: {- state: { status: "unconfigured" },- clearPause: vi.fn(),- saving: false,- dialog: {},+vi.mock("./TopicTraceSelector", () => ({+ useTopicTraceSelector: () => ({+ selection: {+ count: 2,+ selection: {+ filter: [],+ from: new Date("2026-10-01T00:00:00Z"),+ to: new Date("2026-10-09T12:00:00Z"),+ limit: 2,+ sampling: "random",+ seed: "frozen-preview",+ excludedTraceIds: ["excluded-trace"],+ },},- notice: { unavailable: false, status: null },+ controls: <div>Trace selection</div>,+ reset: vi.fn(),}),}));vi.mock("./TopicSetupNotice", () => ({ TopicSetupNotice: () => null }));@@ -88,7 +133,7 @@ vi.mock("./ConfigureTopicsDialogController", () => ({children,}: {children: (control: { openDialog: () => void }) => ReactNode;- }) => children({ openDialog: vi.fn() }),+ }) => children({ openDialog: state.openConfigure }),}));vi.mock("./AutomaticProcessingDialogController", () => ({AutomaticProcessingDialogController: ({@@ -121,8 +166,74 @@ vi.mock("./CurrentTopics", () => ({vi.mock("@/src/utils/api", () => ({api: {topics: {- facets: { useQuery: () => ({ data: [], isLoading: false }) },- executions: { useQuery: () => ({ data: [] }) },+ facets: {+ useQuery: () => ({+ data: state.facets,+ isLoading: false,+ isSuccess: true,+ }),+ },+ executions: {+ useQuery: () => ({+ data: state.executions,+ error: state.executionsError,+ }),+ },+ modelSettings: {+ useQuery: () => ({+ isPending: false,+ data:+ state.configurationFailure === "settings"+ ? undefined+ : state.settings,+ error:+ state.configurationFailure === "settings"+ ? new Error("Configuration unavailable.")+ : null,+ }),+ },+ tracingReadinessSettings: {+ useQuery: () => ({+ data:+ state.configurationFailure === "readiness"+ ? undefined+ : { traceIdleMs: 600_000 },+ error:+ state.configurationFailure === "readiness"+ ? new Error("Configuration unavailable.")+ : null,+ }),+ },+ setAutomaticProcessing: {+ useMutation: () => ({+ isPending: false,+ mutate: vi.fn(),+ mutateAsync: vi.fn(),+ }),+ },+ rules: {+ useQuery: () => ({+ data: state.rulesPending ? undefined : state.rules,+ isPending: state.rulesPending,+ isSuccess: !state.rulesPending && !state.rulesError,+ error: state.rulesError,+ }),+ },+ summaryCounts: {+ useQuery: (input: unknown) => {+ state.summaryCountInput(input);+ return typeof input === "symbol"+ ? { isPending: true }+ : {+ isPending: state.countsPending,+ isFetching: state.countsFetching,+ data: state.countsPending ? undefined : state.counts,+ };+ },+ },+ trigger: {+ useMutation: () => ({ mutateAsync: state.trigger, isPending: false }),+ },execution: {useQuery: () => ({data: {@@ -154,8 +265,27 @@ vi.mock("@/src/utils/api", () => ({}),},},+ llmApiKey: {+ effective: {+ useQuery: () => ({+ data:+ state.configurationFailure === "connections"+ ? undefined+ : { data: [] },+ error:+ state.configurationFailure === "connections"+ ? new Error("Configuration unavailable.")+ : null,+ }),+ },+ },useUtils: () => ({- topics: { executions: { invalidate: vi.fn() } },+ topics: {+ modelSettings: { invalidate: vi.fn() },+ rules: { invalidate: vi.fn() },+ executions: { invalidate: vi.fn() },+ summaryCounts: { invalidate: state.invalidateSummaryCounts },+ },}),},}));@@ -177,6 +307,27 @@ beforeEach(() => {state.updatedAt = "2026-09-23T12:00:00Z";state.dateRange = undefined;state.executionId = undefined;+ state.canWrite = true;+ state.facets = [];+ state.executions = [];+ state.executionsError = null;+ state.rulesPending = false;+ state.rulesError = null;+ state.configurationFailure = null;+ state.settings = {+ summary: { model: "summary" },+ embedding: { model: "embedding" },+ embeddingDimensions: 1024,+ clustering: { model: "clustering" },+ processingAvailable: true,+ blockedAt: null,+ blockMessage: null,+ };+ state.rules = [];+ state.counts = [];+ state.countsPending = false;+ state.countsFetching = false;+ state.trigger.mockReset().mockResolvedValue({ id: "regenerated" });state.setQueryParams.mockImplementation(({ dateRange }: { dateRange: string }) => {state.dateRange = dateRange;@@ -208,9 +359,6 @@ it.each([expect(timeRange.to.getTime() - timeRange.from.getTime()).toBe(minutes * 60_000,);- expect(state.pipeline).toHaveBeenLastCalledWith(- expect.objectContaining({ timeRange }),- );fireEvent.click(screen.getByRole("button", { name: "Select last 30 days" }),@@ -256,11 +404,8 @@ it.each(["stored", "URL"])(to: new Date(),});expect(state.picker).toHaveBeenLastCalledWith({ range: "last90Days" });- expect(state.pipeline).toHaveBeenLastCalledWith(- expect.objectContaining({ timeRange }),- );expect(screen.getByTestId("current-topics")).toBeInTheDocument();- expect(screen.getByRole("status")).toHaveTextContent(+ expect(screen.getByRole("status", { name: "" })).toHaveTextContent("Your selection is preserved on other pages.",);expect(state.setQueryParams).not.toHaveBeenCalled();@@ -277,7 +422,7 @@ it.each(["stored", "URL"])("30d",);expect(state.picker).toHaveBeenLastCalledWith({ range: "last30Days" });- expect(screen.queryByRole("status")).not.toBeInTheDocument();+ expect(screen.queryByRole("status", { name: "" })).not.toBeInTheDocument();},);@@ -297,10 +442,7 @@ it("caps a custom range at its original end and preserves its URL through compleexpect.objectContaining({ timeRange: expected }),);expect(state.picker).toHaveBeenLastCalledWith(expected);- expect(state.pipeline).toHaveBeenLastCalledWith(- expect.objectContaining({ timeRange: expected }),- );- expect(screen.getByRole("status")).toHaveTextContent(+ expect(screen.getByRole("status", { name: "" })).toHaveTextContent("final 90 days of your selected range",);@@ -326,7 +468,7 @@ it.each([state.dateRange = rangeToString(timeRange);render(<TopicsPage />);expect(state.picker).toHaveBeenLastCalledWith(timeRange);- expect(screen.queryByRole("status")).not.toBeInTheDocument();+ expect(screen.queryByRole("status", { name: "" })).not.toBeInTheDocument();expect(state.setQueryParams).not.toHaveBeenCalled();expect(screen.getByTestId("current-topics")).toBeInTheDocument();},@@ -382,3 +524,488 @@ it("holds relative bounds between renders and advances them after completion", (to: new Date("2026-10-09T12:01:00Z"),});});++const facet = (id: string, versions: number[]): TopicFacet => ({+ id,+ projectId: "project",+ name: id,+ description: "",+ isBuiltIn: true,+ versions: versions.map((version) => ({+ projectId: "project",+ facetId: id,+ version,+ prompt: "",+ createdAt: "2026-10-01T00:00:00Z",+ })),+});+const execution = (status: string, operation: "process" | "update") => ({+ id: `${operation}-${status}`,+ status,+ createdAt: "2026-10-09T11:00:00Z",+ updatedAt: "2026-10-09T11:00:00Z",+ input: { operation },+ facets: [],+});+function withSummaries() {+ state.facets = [+ facet("intent", [2, 1]),+ facet("outcome", [1]),+ facet("language", [1]),+ ];+ state.rules = [{ facetIds: ["intent", "outcome"] }];+ state.counts = [+ { facetId: "intent", facetVersion: 2, count: 240 },+ { facetId: "outcome", facetVersion: 1, count: 0 },+ ];+}++it.each([+ {+ name: "without topics:CUD",+ setup: () => {+ state.canWrite = false;+ },+ reason: "You need permission to manage Topics in this project.",+ },+ {+ name: "while Topics is paused",+ setup: () => {+ state.settings.blockedAt = "2026-10-09T10:00:00Z";+ state.settings.blockMessage = "Fix the connection.";+ },+ reason: "Topics is paused. Fix the connection.",+ },+ {+ name: "while any Topics run is in progress",+ setup: () => {+ state.executions = [execution("running", "process")];+ },+ reason: "A Topics run is in progress. Wait for it to finish.",+ },+ {+ name: "before any summaries exist",+ setup: () => {+ state.counts = [];+ },+ reason:+ "No trace summaries in the selected time range yet. Summarize traces first, then regenerate.",+ },+])("disables Generate topics $name", ({ setup, reason }) => {+ withSummaries();+ setup();+ render(<TopicsPage />);+ fireEvent.click(screen.getByRole("button", { name: "Manual processing" }));+ fireEvent.mouseDown(screen.getByRole("tab", { name: "Generate topics" }), {+ button: 0,+ ctrlKey: false,+ });+ expect(+ screen.getByRole("button", { name: "Generate topics" }),+ ).toBeDisabled();+ expect(screen.getByText(reason)).toBeInTheDocument();+});++it("requires an eligible selected facet and applies the small-sample threshold before generating topics", async () => {+ withSummaries();+ state.counts = [+ { facetId: "intent", facetVersion: 2, count: 13 },+ { facetId: "outcome", facetVersion: 1, count: 9 },+ ];+ render(<TopicsPage />);+ fireEvent.click(screen.getByRole("button", { name: "Manual processing" }));+ fireEvent.mouseDown(screen.getByRole("tab", { name: "Generate topics" }), {+ button: 0,+ ctrlKey: false,+ });+ const submit = screen.getByRole("button", { name: "Generate topics" });+ expect(submit).toBeDisabled();+ fireEvent.click(submit);+ expect(state.trigger).not.toHaveBeenCalled();++ fireEvent.click(screen.getByRole("switch", { name: "Small sample mode" }));+ expect(submit).toBeEnabled();+ fireEvent.click(screen.getByRole("checkbox", { name: "intent" }));+ expect(submit).toBeDisabled();+ fireEvent.click(submit);+ expect(state.trigger).not.toHaveBeenCalled();++ fireEvent.click(screen.getByRole("checkbox", { name: "intent" }));+ fireEvent.click(submit);+ await waitFor(() =>+ expect(state.trigger).toHaveBeenCalledWith(+ expect.objectContaining({ exploratory: true, minimumTraceCount: 10 }),+ ),+ );+});++it("counts summaries only while the open manual dialog is generating topics", async () => {+ withSummaries();+ render(<TopicsPage />);+ const countRequests = () =>+ state.summaryCountInput.mock.calls.filter(+ ([input]) => typeof input !== "symbol",+ );+ expect(countRequests()).toHaveLength(0);+ fireEvent.click(screen.getByRole("button", { name: "Manual processing" }));+ expect(countRequests()).toHaveLength(0);+ fireEvent.mouseDown(screen.getByRole("tab", { name: "Generate topics" }), {+ button: 0,+ ctrlKey: false,+ });+ expect(countRequests().length).toBeGreaterThan(0);+ fireEvent.click(screen.getByRole("button", { name: "Cancel" }));+ await waitFor(() => expect(screen.queryByRole("dialog")).toBeNull());+ const completedRequests = countRequests().length;+ fireEvent.click(screen.getByRole("button", { name: "Manual processing" }));+ expect(countRequests()).toHaveLength(completedRequests);+});++it("keeps summary loading and refetching out of validation warnings", () => {+ withSummaries();+ state.counts = state.counts.map((count) => ({ ...count, count: 240 }));+ state.countsPending = true;+ state.countsFetching = true;+ const page = render(<TopicsPage />);+ fireEvent.click(screen.getByRole("button", { name: "Manual processing" }));+ fireEvent.mouseDown(screen.getByRole("tab", { name: "Generate topics" }), {+ button: 0,+ ctrlKey: false,+ });+ const dialog = within(+ screen.getByRole("dialog", { name: "Manual processing" }),+ );+ expect(+ dialog.getByRole("button", { name: "Generate topics" }),+ ).toBeDisabled();+ expect(dialog.queryByRole("alert")).toBeNull();+ expect(dialog.getByRole("status")).toHaveTextContent(/^2 facets selected$/);+ state.countsPending = false;+ state.countsFetching = false;+ page.rerender(<TopicsPage />);+ expect(dialog.getByRole("button", { name: "Generate topics" })).toBeEnabled();+ expect(dialog.getAllByText("240 summaries")[0]).toBeVisible();+ expect(dialog.getByRole("status")).toHaveTextContent(+ "2 facets selected · 480 stored summaries",+ );+ state.countsFetching = true;+ page.rerender(<TopicsPage />);+ expect(dialog.getAllByText("240 summaries")[0]).toBeVisible();+ expect(dialog.getByRole("status")).toHaveTextContent(+ "2 facets selected · 480 stored summaries",+ );+ expect(dialog.queryByRole("alert")).toBeNull();+ expect(+ dialog.getByRole("button", { name: "Generate topics" }),+ ).toBeDisabled();+});++it("regenerates the rule's facets that have summaries, at their latest version, over its default seven-day range, then opens the run", async () => {+ withSummaries();+ state.executions = [execution("completed", "update")];+ render(<TopicsPage />);+ fireEvent.click(screen.getByRole("button", { name: "Manual processing" }));+ fireEvent.mouseDown(screen.getByRole("tab", { name: "Generate topics" }), {+ button: 0,+ ctrlKey: false,+ });++ const dialog = screen.getByRole("dialog", { name: "Manual processing" });+ expect(+ within(dialog).getByRole("checkbox", { name: "outcome" }),+ ).toBeChecked();+ expect(+ within(dialog).queryByRole("checkbox", { name: "language" }),+ ).toBeNull();+ fireEvent.click(+ within(dialog).getByRole("button", { name: "Generate topics" }),+ );++ await waitFor(() =>+ expect(state.push).toHaveBeenCalledWith(+ expect.objectContaining({+ query: expect.objectContaining({ executionId: "regenerated" }),+ }),+ undefined,+ { shallow: true },+ ),+ );+ expect(state.trigger).toHaveBeenCalledWith({+ projectId: "project",+ operation: "update",+ facets: [+ { facetId: "intent", version: 2 },+ { facetId: "outcome", version: 1 },+ ],+ exploratory: false,+ timeRange: {+ from: new Date("2026-10-02T12:00:00Z"),+ to: new Date("2026-10-09T12:00:00Z"),+ },+ minimumTraceCount: 100,+ requestId: expect.any(String),+ });+});++it("shows a regenerate failure that settles after the dialog was closed and reopened, and retries with the same requestId", async () => {+ withSummaries();+ let fail: (error: Error) => void = () => {};+ state.trigger.mockImplementationOnce(+ () => new Promise((_, reject) => (fail = reject)),+ );+ render(<TopicsPage />);+ const open = () => {+ fireEvent.click(screen.getByRole("button", { name: "Manual processing" }));+ fireEvent.mouseDown(screen.getByRole("tab", { name: "Generate topics" }), {+ button: 0,+ ctrlKey: false,+ });+ return screen.getByRole("dialog", { name: "Manual processing" });+ };++ fireEvent.click(+ within(open()).getByRole("button", { name: "Generate topics" }),+ );+ fireEvent.click(screen.getByRole("button", { name: "Cancel" }));+ await waitFor(() => expect(screen.queryByRole("dialog")).toBeNull());+ const reopened = open();+ fail(new Error("Clustering connection failed."));++ expect(+ await within(reopened).findByText("Clustering connection failed."),+ ).toBeInTheDocument();+ fireEvent.click(+ within(reopened).getByRole("button", { name: "Generate topics" }),+ );+ await waitFor(() => expect(state.trigger).toHaveBeenCalledTimes(2));+ const [first, second] = state.trigger.mock.calls.map(+ ([input]) => (input as { requestId: string }).requestId,+ );+ expect(second).toBe(first);+});++it("summarizes the frozen trace selection for active facets and opens the progress drawer", async () => {+ withSummaries();+ render(<TopicsPage />);+ fireEvent.click(screen.getByRole("button", { name: "Manual processing" }));+ const dialog = screen.getByRole("dialog", { name: "Manual processing" });+ fireEvent.click(+ within(dialog).getByRole("button", { name: "Summarize & embed" }),+ );++ await waitFor(() =>+ expect(state.push).toHaveBeenCalledWith(+ expect.objectContaining({+ query: expect.objectContaining({ executionId: "regenerated" }),+ }),+ undefined,+ { shallow: true },+ ),+ );+ expect(state.trigger).toHaveBeenCalledWith({+ projectId: "project",+ operation: "process",+ facets: [+ { facetId: "intent", version: 2 },+ { facetId: "outcome", version: 1 },+ ],+ reuseExistingSummaries: true,+ selection: {+ filter: [],+ from: new Date("2026-10-01T00:00:00Z"),+ to: new Date("2026-10-09T12:00:00Z"),+ limit: 2,+ sampling: "random",+ seed: "frozen-preview",+ excludedTraceIds: ["excluded-trace"],+ },+ requestId: expect.any(String),+ });+ expect(screen.queryByRole("dialog")).toBeNull();+});++it("hands off from manual processing to Topics model configuration", async () => {+ withSummaries();+ render(<TopicsPage />);+ fireEvent.click(screen.getByRole("button", { name: "Manual processing" }));+ const dialog = screen.getByRole("dialog", { name: "Manual processing" });+ fireEvent.click(+ within(dialog).getByRole("button", { name: "Configure Topics" }),+ );+ expect(state.openConfigure).toHaveBeenCalledOnce();+ await waitFor(() =>+ expect(+ screen.queryByRole("dialog", { name: "Manual processing" }),+ ).toBeNull(),+ );+ expect(state.trigger).not.toHaveBeenCalled();+});++it("freezes an independent generation range for summary counts and submission", async () => {+ withSummaries();+ render(<TopicsPage />);+ fireEvent.click(screen.getByRole("button", { name: "Manual processing" }));+ fireEvent.mouseDown(screen.getByRole("tab", { name: "Generate topics" }), {+ button: 0,+ ctrlKey: false,+ });+ const dialog = screen.getByRole("dialog", { name: "Manual processing" });+ fireEvent.click(+ within(dialog).getByRole("button", { name: "Select last 30 days" }),+ );+ const expectedRange = {+ from: new Date("2026-09-09T12:00:00Z"),+ to: new Date("2026-10-09T12:00:00Z"),+ };+ expect(state.summaryCountInput).toHaveBeenLastCalledWith(+ expect.objectContaining({ timeRange: expectedRange }),+ );+ vi.setSystemTime(new Date("2026-10-09T12:05:00Z"));+ fireEvent.click(+ within(dialog).getByRole("button", { name: "Generate topics" }),+ );+ await waitFor(() => expect(state.trigger).toHaveBeenCalled());+ expect(state.trigger.mock.lastCall![0].timeRange).toEqual({+ from: new Date("2026-09-09T12:00:00Z"),+ to: new Date("2026-10-09T12:00:00Z"),+ });+ expect(state.setQueryParams).not.toHaveBeenCalled();+});++it.each(["relative", "absolute"])(+ "refreshes summary counts on reopening and preserves %s range semantics",+ async (rangeKind) => {+ withSummaries();+ state.counts = [];+ const view = render(<TopicsPage />);+ fireEvent.click(screen.getByRole("button", { name: "Manual processing" }));+ if (rangeKind === "absolute") {+ fireEvent.mouseDown(+ screen.getByRole("tab", { name: "Generate topics" }),+ { button: 0, ctrlKey: false },+ );+ fireEvent.click(+ within(screen.getByRole("dialog")).getByRole("button", {+ name: "Select custom dates",+ }),+ );+ }+ expect(state.invalidateSummaryCounts).toHaveBeenCalledWith({+ projectId: "project",+ });+ fireEvent.click(screen.getByRole("button", { name: "Cancel" }));+ await waitFor(() => expect(screen.queryByRole("dialog")).toBeNull());+ state.counts = [{ facetId: "intent", facetVersion: 2, count: 120 }];+ vi.setSystemTime(new Date("2026-10-10T12:00:00Z"));+ view.rerender(<TopicsPage />);+ fireEvent.click(screen.getByRole("button", { name: "Manual processing" }));+ expect(state.invalidateSummaryCounts).toHaveBeenCalledTimes(2);+ fireEvent.mouseDown(screen.getByRole("tab", { name: "Generate topics" }), {+ button: 0,+ ctrlKey: false,+ });+ expect(+ screen.getByRole("button", { name: "Generate topics" }),+ ).toBeEnabled();+ const expectedRange =+ rangeKind === "relative"+ ? {+ from: new Date("2026-10-03T12:00:00Z"),+ to: new Date("2026-10-10T12:00:00Z"),+ }+ : {+ from: new Date("2026-10-01T00:00:00Z"),+ to: new Date("2026-10-08T00:00:00Z"),+ };+ expect(state.summaryCountInput.mock.lastCall![0].timeRange).toEqual(+ expectedRange,+ );+ vi.setSystemTime(new Date("2026-10-10T12:05:00Z"));+ fireEvent.click(screen.getByRole("button", { name: "Generate topics" }));+ await waitFor(() => expect(state.trigger).toHaveBeenCalled());+ expect(state.trigger.mock.lastCall![0].timeRange).toEqual(expectedRange);+ },+);++it.each(["settings", "readiness", "connections"] as const)(+ "shows a failed %s configuration query and recovers after it succeeds",+ (query) => {+ withSummaries();+ state.configurationFailure = query;+ const view = render(<TopicsPage />);+ fireEvent.click(screen.getByRole("button", { name: "Manual processing" }));+ expect(+ screen.getByText(+ /Could not load Topics configuration: Configuration unavailable\./,+ ),+ ).toBeInTheDocument();+ expect(+ screen.queryByText("Loading Topics configuration…"),+ ).not.toBeInTheDocument();+ expect(+ screen.getByRole("button", { name: "Summarize & embed" }),+ ).toBeDisabled();+ fireEvent.mouseDown(screen.getByRole("tab", { name: "Generate topics" }), {+ button: 0,+ ctrlKey: false,+ });+ expect(+ screen.getByRole("button", { name: "Generate topics" }),+ ).toBeDisabled();+ expect(state.trigger).not.toHaveBeenCalled();+ state.configurationFailure = null;+ view.rerender(<TopicsPage />);+ expect(+ screen.getByRole("button", { name: "Generate topics" }),+ ).toBeEnabled();+ expect(+ screen.queryByText(/Could not load Topics configuration/),+ ).not.toBeInTheDocument();+ },+);++it.each([+ ["loading rules", "Checking the active Topics facets…"],+ ["failed rules", "Could not load active Topics facets: Rules unavailable."],+ ["loading runs", "Checking for Topics runs in progress…"],+ ["failed runs", "Could not check Topics runs: Runs unavailable."],+])(+ "blocks both operations while prerequisites are unknown: %s",+ async (condition, reason) => {+ withSummaries();+ if (condition === "loading rules") state.rulesPending = true;+ if (condition === "failed rules")+ state.rulesError = new Error("Rules unavailable.");+ if (condition === "loading runs") state.executions = undefined;+ if (condition === "failed runs")+ state.executionsError = new Error("Runs unavailable.");+ const { rerender } = render(<TopicsPage />);+ fireEvent.click(screen.getByRole("button", { name: "Manual processing" }));+ expect(+ screen.getByRole("button", { name: "Summarize & embed" }),+ ).toBeDisabled();+ expect(screen.getByText(reason)).toBeInTheDocument();+ fireEvent.mouseDown(screen.getByRole("tab", { name: "Generate topics" }), {+ button: 0,+ ctrlKey: false,+ });+ expect(+ screen.getByRole("button", { name: "Generate topics" }),+ ).toBeDisabled();+ expect(state.summaryCountInput.mock.lastCall![0]).toEqual(+ expect.any(Symbol),+ );+ expect(state.trigger).not.toHaveBeenCalled();+ state.rulesPending = false;+ state.rulesError = null;+ state.executions = [];+ state.executionsError = null;+ rerender(<TopicsPage />);+ expect(+ screen.getByRole("button", { name: "Generate topics" }),+ ).toBeEnabled();+ expect(+ screen.queryByRole("checkbox", { name: "language" }),+ ).not.toBeInTheDocument();+ },+);diff --git a/web/src/features/topics/TopicsPage.tsx b/web/src/features/topics/TopicsPage.tsxindex 7662a8b75f..a6dfa3efee 100644--- a/web/src/features/topics/TopicsPage.tsx+++ b/web/src/features/topics/TopicsPage.tsx@@ -1,5 +1,5 @@import { useMemo, useState } from "react";-import { MoreHorizontal } from "lucide-react";+import { Settings } from "lucide-react";import { type UseQueryResult } from "@tanstack/react-query";import { usePeekNavigation } from "@/src/components/table/peek/hooks/usePeekNavigation";import { TablePeekViewTraceDetail } from "@/src/components/table/peek/peek-trace-detail";@@ -13,6 +13,7 @@ import {DASHBOARD_AGGREGATION_OPTIONS,TABLE_AGGREGATION_OPTIONS,TIME_RANGES,+ type TimeRange,} from "@/src/utils/date-range-utils";import { ErrorPage } from "@/src/components/error-page";import { Button } from "@/src/components/design-system/Button/Button";@@ -25,8 +26,6 @@ import {SheetTitle,SheetDescription,} from "@/src/components/ui/sheet";-import { DropdownMenu } from "@/src/components/design-system/DropdownMenu/DropdownMenu";-import { IconButton } from "@/src/components/design-system/IconButton/IconButton";import { PopoverController } from "@/src/components/ui/popover";import { Badge } from "@/src/components/design-system/Badge/Badge";import { api, type RouterOutputs } from "@/src/utils/api";@@ -37,10 +36,15 @@ import {type TopicExecutionStatus,type TopicFacetOutcome,type TopicOperation,+ type TopicTimeRange,} from "@langfuse/shared/topics";import { useTopicPipelineForm } from "./TopicPipelineForm";+import { ManualTopicsFormContent } from "./ManualTopicsFormContent";+import { TopicsHeaderActions } from "./TopicsHeaderActions";+import { DialogController } from "@/src/components/design-system/DialogController/DialogController";+import { HeaderActionMenuRow } from "@/src/components/HeaderActionMenuRow";+import { useRegenerateTopics } from "./useRegenerateTopics";import { useTopicModelSettings } from "./TopicModelSettings";-import { AutomaticProcessingControl } from "./AutomaticProcessingControl";import { AutomaticProcessingDialogController } from "./AutomaticProcessingDialogController";import { ConfigureTopicsDialogController } from "./ConfigureTopicsDialogController";import { TopicSetupNotice } from "./TopicSetupNotice";@@ -183,6 +187,25 @@ function TopicsWorkspaceContent({openAutomaticProcessing,}: TopicsWorkspaceContentProps) {const [historyOpen, setHistoryOpen] = useState(false);+ const [generationRange, setGenerationRange] = useState<{+ selection: TimeRange;+ bounds: TopicTimeRange;+ }>(() => ({+ selection: { range: "last7Days" },+ bounds: { from: new Date(Date.now() - 7 * 86_400_000), to: new Date() },+ }));+ const changeGenerationRange = (selection: TimeRange) => {+ if ("from" in selection) {+ setGenerationRange({ selection, bounds: selection });+ return;+ }+ const to = new Date();+ const minutes =+ TIME_RANGES[selection.range as keyof typeof TIME_RANGES]?.minutes ??+ TIME_RANGES.last7Days.minutes;+ const bounds = { from: new Date(to.getTime() - minutes * 60_000), to };+ setGenerationRange({ selection, bounds });+ };const { timeRange, setTimeRange } = useGlobalDateRange({allowedRanges: sharedTimeRangePresets,fallback: "last7Days",@@ -299,13 +322,32 @@ function TopicsWorkspaceContent({function openHistory() {setHistoryOpen(true);}- const { primaryAction, triggerAction, configuration } = useTopicPipelineForm({+ const regenerate = useRegenerateTopics({+ projectId,+ facets: facets.data ?? [],+ canWrite,+ timeRange: generationRange.bounds,+ executions: executions.data,+ executionsError: executions.error?.message ?? null,+ });+ const pipeline = useTopicPipelineForm({projectId,facets: facets.data ?? [],canWrite,onTriggered: openExecution,- timeRange: validTimeRange ? currentTimeRange : null,+ regenerate,+ settings: modelSettings.configure.settings,+ configurationError: modelSettings.configure.error,+ running,+ onConfigure: openConfigure,+ generationTimeRange: generationRange.selection,+ onGenerationTimeRangeChange: changeGenerationRange,});+ const configured = Boolean(+ modelSettings.configure.settings?.stored.summary &&+ modelSettings.configure.settings.stored.embedding &&+ modelSettings.configure.settings.stored.clustering,+ );const automaticProcessing = modelSettings.automaticProcessing;function openAutomaticProcessingFromHeader() {openAutomaticProcessing(automaticProcessing.state.status === "on");@@ -313,199 +355,217 @@ function TopicsWorkspaceContent({function turnAutomaticProcessingBackOn() {openAutomaticProcessing(true);}- const actions = (- <div className="ph-no-capture flex items-center justify-end gap-1">- <AutomaticProcessingControl- state={automaticProcessing.state}- canWrite={canWrite && !automaticProcessing.saving}- onOpen={openAutomaticProcessingFromHeader}- />- <Button- text="Configure Topics"- variant={modelSettings.configure.triggerVariant}- size="sm"- disabled={!modelSettings.configure.settings}- onClick={openConfigure}- />- {primaryAction}- <DropdownMenu- ariaLabel="Topics actions"- placement="bottom-end"- items={[- {- id: "history",- type: "item",- title: "History",- onClick: openHistory,- },- {- id: "refresh",- type: "item",- title: "Refresh results",- onClick: refreshResults,- },- ]}- >- {({ getTriggerProps }) => (- <IconButton- {...getTriggerProps()}- icon={MoreHorizontal}- label="Topics actions"- size="sm"- />- )}- </DropdownMenu>- </div>- );+return (- <Page- headerProps={{- title: "Topics",- help: {- description:- "Explore recurring themes across traces, one facet at a time.",- },- actionButtonsRight: actions,- actionButtonsMenu: ({ closeMenu }) => (- <TopicsActionsMenu- triggerAction={triggerAction}- closeMenu={closeMenu}- onOpenHistory={openHistory}- onRefresh={refreshResults}- />- ),- }}- >- <PageHeaderControlsPortal>- <TimeRangePicker- timeRange={effectiveTimeRange}- onTimeRangeChange={setTimeRange}- timeRangePresets={topicsTimeRangePresets}- maxRangeMs={maxTimeRangeMs}- className="my-0 max-w-full overflow-x-auto"- triggerClassName="px-2"- />- </PageHeaderControlsPortal>- <div className="ph-no-capture flex min-h-0 flex-1 flex-col gap-2 overflow-hidden p-2">- {isTimeRangeCapped && (- <p role="status" className="text-muted-foreground shrink-0 text-xs">- {"from" in timeRange- ? "Showing the final 90 days of your selected range."- : "Showing the last 90 days."}{" "}- Your selection is preserved on other pages.- </p>- )}- {!validTimeRange && (- <ErrorMessage message="Select a time range of at most 90 days." />- )}- <TopicSetupNotice- notice={modelSettings.notice}- canWrite={canWrite}- canConfigure={modelSettings.configure.settings !== null}- onConfigure={openConfigure}- onKeepOff={automaticProcessing.clearPause}- onTurnBackOn={turnAutomaticProcessingBackOn}+ <DialogController+ renderDialog={({ closeDialog }) => (+ <ManualTopicsFormContent+ {...pipeline.dialogProps}+ closeDialog={closeDialog}/>- {configuration}- <Sheet- open={historyOpen || executionId !== null}- onOpenChange={showExecutionList}- >- <SheetContent className="ph-no-capture flex w-full flex-col gap-4 sm:max-w-xl">- <SheetHeader>- <SheetTitle>- {executionId ? "Run status" : "Past executions"}- </SheetTitle>- <SheetDescription>- Review progress, errors, and retry interrupted runs.- </SheetDescription>- </SheetHeader>- <div className="flex min-h-0 flex-1 flex-col gap-3 overflow-y-auto">- {executionId ? (- <>- <div className="self-start">- <Button- text="All runs"- variant="ghost"- size="sm"- onClick={() => showExecutionList(true)}- />+ )}+ >+ {({ openDialog }) => {+ const openManual = () => {+ if (!("from" in generationRange.selection))+ changeGenerationRange(generationRange.selection);+ utils.topics.summaryCounts.invalidate({ projectId });+ pipeline.clearError();+ openDialog();+ };+ return (+ <Page+ headerProps={{+ title: "Topics",+ help: {+ description:+ "Explore recurring themes across traces, one facet at a time.",+ },+ actionButtonsRight: (+ <TopicsHeaderActions+ automaticProcessing={automaticProcessing}+ configure={modelSettings.configure}+ configured={configured}+ canWrite={canWrite}+ manualAvailable={pipeline.available}+ openManual={openManual}+ onAutomaticProcessing={openAutomaticProcessingFromHeader}+ onConfigure={openConfigure}+ onHistory={openHistory}+ onRefresh={refreshResults}+ />+ ),+ actionButtonsMenu: ({ closeMenu }) => {+ function handleConfigureFromMenu() {+ closeMenu();+ openConfigure();+ }+ return (+ <TopicsActionsMenu+ triggerAction={+ pipeline.available+ ? {+ label: "Manual processing",+ disabled: false,+ onSelect: openManual,+ }+ : null+ }+ closeMenu={closeMenu}+ onOpenHistory={openHistory}+ onRefresh={refreshResults}+ >+ {configured ? (+ <HeaderActionMenuRow+ label="Configure Topics"+ icon={+ <Settings className="icon-base text-icon-foreground" />+ }+ onClick={handleConfigureFromMenu}+ />+ ) : null}+ </TopicsActionsMenu>+ );+ },+ }}+ >+ <PageHeaderControlsPortal>+ <TimeRangePicker+ timeRange={effectiveTimeRange}+ onTimeRangeChange={setTimeRange}+ timeRangePresets={topicsTimeRangePresets}+ maxRangeMs={maxTimeRangeMs}+ className="my-0 max-w-full overflow-x-auto"+ triggerClassName="px-2"+ />+ </PageHeaderControlsPortal>+ <div className="ph-no-capture flex min-h-0 flex-1 flex-col gap-2 overflow-hidden p-2">+ {isTimeRangeCapped && (+ <p+ role="status"+ className="text-muted-foreground shrink-0 text-xs"+ >+ {"from" in timeRange+ ? "Showing the final 90 days of your selected range."+ : "Showing the last 90 days."}{" "}+ Your selection is preserved on other pages.+ </p>+ )}+ {!validTimeRange && (+ <ErrorMessage message="Select a time range of at most 90 days." />+ )}+ <TopicSetupNotice+ notice={modelSettings.notice}+ canWrite={canWrite}+ canConfigure={modelSettings.configure.settings !== null}+ onConfigure={openConfigure}+ onKeepOff={automaticProcessing.clearPause}+ onTurnBackOn={turnAutomaticProcessingBackOn}+ />+ <Sheet+ open={historyOpen || executionId !== null}+ onOpenChange={showExecutionList}+ >+ <SheetContent className="ph-no-capture flex w-full flex-col gap-4 sm:max-w-xl">+ <SheetHeader>+ <SheetTitle>+ {executionId ? "Run status" : "Past executions"}+ </SheetTitle>+ <SheetDescription>+ Review progress, errors, and retry interrupted runs.+ </SheetDescription>+ </SheetHeader>+ <div className="flex min-h-0 flex-1 flex-col gap-3 overflow-y-auto">+ {executionId ? (+ <>+ <div className="self-start">+ <Button+ text="All runs"+ variant="ghost"+ size="sm"+ onClick={() => showExecutionList(true)}+ />+ </div>+ <ExecutionPanel+ key={executionId}+ projectId={projectId}+ executionId={executionId}+ query={selectedExecution}+ facets={facets.data ?? []}+ canWrite={canWrite}+ />+ </>+ ) : (+ <>+ <div className="self-end">+ <Button+ text="Refresh"+ size="sm"+ variant="ghost"+ onClick={() => executions.refetch()}+ />+ </div>+ {executions.error && (+ <ErrorMessage message={executions.error.message} />+ )}+ {!executions.data?.length && (+ <p className="text-muted-foreground text-sm">+ Your triggered batches will appear here.+ </p>+ )}+ {executions.data?.map((execution) => (+ <button+ key={execution.id}+ onClick={() => openExecution(execution.id)}+ className="hover:bg-muted/50 flex flex-wrap items-center justify-between gap-2 rounded-md border p-3 text-left text-sm"+ >+ <span className="capitalize">+ {operationLabels[execution.input.operation]} ·{" "}+ {new Date(execution.createdAt).toLocaleString()}+ </span>+ <span>{execution.facets.length} facets</span>+ <Badge text={executionLabels[execution.status]} />+ </button>+ ))}+ </>+ )}</div>- <ExecutionPanel- key={executionId}- projectId={projectId}- executionId={executionId}- query={selectedExecution}- facets={facets.data ?? []}- canWrite={canWrite}- />- </>- ) : (- <>- <div className="self-end">- <Button- text="Refresh"- size="sm"- variant="ghost"- onClick={() => executions.refetch()}+ </SheetContent>+ </Sheet>+ <div className="flex min-h-0 w-full min-w-0 flex-1 flex-col gap-2 overflow-hidden">+ <TablePeekViewTraceDetail+ {...peekNavigation}+ itemType="TRACE"+ projectId={projectId}+ defaultWidthTarget={tracePanel}+ widthStorageKey="topicsPeekViewWidthFraction"+ />+ {validTimeRange && (+ <TopicsFilters+ facets={results.data ?? []}+ selectedFacetId={selectedFacetId}+ onSelectFacet={traceFilters.selectFacet}+ >+ <CurrentTopics+ projectId={projectId}+ timeRange={currentTimeRange}+ result={results}+ selectedFacetId={selectedFacetId}+ traceFilters={traceFilters}+ tracePanelRef={setTracePanel}/>- </div>- {executions.error && (- <ErrorMessage message={executions.error.message} />- )}- {!executions.data?.length && (- <p className="text-muted-foreground text-sm">- Your triggered batches will appear here.- </p>- )}- {executions.data?.map((execution) => (- <button- key={execution.id}- onClick={() => openExecution(execution.id)}- className="hover:bg-muted/50 flex flex-wrap items-center justify-between gap-2 rounded-md border p-3 text-left text-sm"- >- <span className="capitalize">- {operationLabels[execution.input.operation]} ·{" "}- {new Date(execution.createdAt).toLocaleString()}- </span>- <span>{execution.facets.length} facets</span>- <Badge text={executionLabels[execution.status]} />- </button>- ))}- </>- )}+ </TopicsFilters>+ )}+ {facets.isLoading && <p>Loading facets…</p>}+ {facets.error && (+ <ErrorMessage message={facets.error.message} />+ )}+ </div></div>- </SheetContent>- </Sheet>- <div className="flex min-h-0 w-full min-w-0 flex-1 flex-col gap-2 overflow-hidden">- <TablePeekViewTraceDetail- {...peekNavigation}- itemType="TRACE"- projectId={projectId}- defaultWidthTarget={tracePanel}- widthStorageKey="topicsPeekViewWidthFraction"- />- {validTimeRange && (- <TopicsFilters- facets={results.data ?? []}- selectedFacetId={selectedFacetId}- onSelectFacet={traceFilters.selectFacet}- >- <CurrentTopics- projectId={projectId}- timeRange={currentTimeRange}- result={results}- selectedFacetId={selectedFacetId}- traceFilters={traceFilters}- tracePanelRef={setTracePanel}- />- </TopicsFilters>- )}- {facets.isLoading && <p>Loading facets…</p>}- {facets.error && <ErrorMessage message={facets.error.message} />}- </div>- </div>- </Page>+ </Page>+ );+ }}+ </DialogController>);}diff --git a/web/src/features/topics/backfill-limit.ts b/web/src/features/topics/backfill-limit.tsnew file mode 100644index 0000000000..b41d15b78e--- /dev/null+++ b/web/src/features/topics/backfill-limit.ts@@ -0,0 +1 @@+export const TOPICS_BACKFILL_TRACE_LIMIT = 1_000;diff --git a/web/src/features/topics/manual-topics-selection.ts b/web/src/features/topics/manual-topics-selection.tsnew file mode 100644index 0000000000..f5d28a7dc6--- /dev/null+++ b/web/src/features/topics/manual-topics-selection.ts@@ -0,0 +1,36 @@+import { type TopicOperation } from "@langfuse/shared/topics";++export function manualTopicsSelection({+ operation,+ summaryCounts,+ traceCount,+ smallSample,+}: {+ operation: TopicOperation;+ summaryCounts: number[];+ traceCount: number;+ smallSample: boolean;+}): { ready: boolean; disabledReason: string | null } {+ if (summaryCounts.length === 0)+ return {+ ready: false,+ disabledReason: "Select at least one active facet.",+ };+ if (operation === "process")+ return { ready: traceCount > 0, disabledReason: null };+ if (!summaryCounts.some((count) => count > 0))+ return {+ ready: false,+ disabledReason:+ "No summaries are available for the selected facets. Summarize traces first.",+ };+ const minimum = smallSample ? 10 : 100;+ if (!summaryCounts.some((count) => count >= minimum))+ return {+ ready: false,+ disabledReason: smallSample+ ? "Each selected facet has fewer than 10 summaries. Summarize more traces or widen the time range."+ : "Each selected facet has fewer than 100 summaries. Enable small sample mode, summarize more traces, or widen the time range.",+ };+ return { ready: true, disabledReason: null };+}diff --git a/web/src/features/topics/server/traceSelection.ts b/web/src/features/topics/server/traceSelection.tsindex 439466788f..f6ad40bca0 100644--- a/web/src/features/topics/server/traceSelection.ts+++ b/web/src/features/topics/server/traceSelection.ts@@ -1,4 +1,5 @@import { z } from "zod";+import { TOPICS_BACKFILL_TRACE_LIMIT } from "../backfill-limit";import { InvalidRequestError } from "@langfuse/shared";import {topicExecutionInputSchema,@@ -12,6 +13,10 @@ import { selectTopicTraceRows } from "@langfuse/shared/topics/server";export const topicTraceSelectionSchema =topicTraceSelectionCriteriaSchema.safeExtend({projectId: topicIdSchema,+ limit: topicTraceSelectionCriteriaSchema.shape.limit.refine(+ (limit) => limit === null || limit <= TOPICS_BACKFILL_TRACE_LIMIT,+ `Select at most ${TOPICS_BACKFILL_TRACE_LIMIT.toLocaleString("en-US")} traces.`,+ ),});export const topicTriggerInputSchema = z@@ -21,6 +26,15 @@ export const topicTriggerInputSchema = zselection: topicTraceSelectionSnapshotSchema,}),])+ .refine(+ (input) =>+ input.operation !== "process" ||+ ("selection" in input+ ? input.selection.limit === null ||+ input.selection.limit <= TOPICS_BACKFILL_TRACE_LIMIT+ : input.traceIds.length <= TOPICS_BACKFILL_TRACE_LIMIT),+ `Select at most ${TOPICS_BACKFILL_TRACE_LIMIT.toLocaleString("en-US")} traces.`,+ ).refine((input) =>input.operation !== "update" ||@@ -33,11 +47,15 @@ export async function previewTopicTraces(input: z.infer<typeof topicTraceSelectionSchema>,prisma: PrismaClient,) {- const rows = await selectTopicTraceRows(input, prisma, "preview");+ const selection = {+ ...input,+ limit: input.limit ?? TOPICS_BACKFILL_TRACE_LIMIT,+ };+ const rows = await selectTopicTraceRows(selection, prisma, "preview");const matchedTraceCount = Number(rows[0]?.matchedTraceCount ?? 0);return {matchedTraceCount,- selectedTraceCount: Math.min(matchedTraceCount, input.limit ?? Infinity),+ selectedTraceCount: Math.min(matchedTraceCount, selection.limit),traces: rows.map((row) => ({id: row.id,timestamp: new Date(Number(row.timestampMs)),@@ -54,7 +72,11 @@ export async function resolveTopicTraceSelection(if (!("selection" in input)) return input;const { selection, ...execution } = input;const rows = await selectTopicTraceRows(- { ...selection, projectId: input.projectId },+ {+ ...selection,+ limit: selection.limit ?? TOPICS_BACKFILL_TRACE_LIMIT,+ projectId: input.projectId,+ },prisma,"ids",);diff --git a/web/src/features/topics/time-range.ts b/web/src/features/topics/time-range.tsindex 2d4c705096..cf563fbcb4 100644--- a/web/src/features/topics/time-range.ts+++ b/web/src/features/topics/time-range.ts@@ -2,13 +2,6 @@ import { addDays, format } from "date-fns";import type { TopicTimeRange } from "@langfuse/shared/topics";const DAY = 86_400_000;-export const topicTimeRangePresets = [- { value: "1", label: "Last 24 hours" },- { value: "7", label: "Last 7 days" },- { value: "30", label: "Last 30 days" },- { value: "90", label: "Last 90 days" },- { value: "custom", label: "Custom range" },-];export function relativeTopicTimeRange(days: number, to = new Date()) {return { from: new Date(to.getTime() - days * DAY), to };diff --git a/web/src/features/topics/useRegenerateTopics.ts b/web/src/features/topics/useRegenerateTopics.tsnew file mode 100644index 0000000000..7cf12be1db--- /dev/null+++ b/web/src/features/topics/useRegenerateTopics.ts@@ -0,0 +1,165 @@+import { useRef, useState } from "react";+import { api, type RouterOutputs } from "@/src/utils/api";+import {+ type TopicExecutionStatus,+ type TopicFacet,+ type TopicFacetRef,+ type TopicRule,+ type TopicTimeRange,+} from "@langfuse/shared/topics";++/**+ * Data and the `topics.trigger` (`operation: "update"`) write behind the+ * Regenerate topics dialog. Facets follow the Topics rule at their latest+ * version, the one automatic processing summarizes with. Call it above the+ * dialog content so the request and its error outlive a close.+ */+export function useRegenerateTopics({+ projectId,+ facets,+ canWrite,+ timeRange,+ executions,+ executionsError,+}: {+ projectId: string;+ facets: TopicFacet[];+ canWrite: boolean;+ timeRange: TopicTimeRange | null;+ executions: TopicExecutionList | undefined;+ executionsError: string | null;+}) {+ const settings = api.topics.modelSettings.useQuery({ projectId });+ const rules = api.topics.rules.useQuery({ projectId });+ const configured = Boolean(+ settings.data?.summary &&+ settings.data.embedding &&+ settings.data.clustering,+ );+ const rule = rules.data?.length === 1 ? rules.data[0] : null;+ const prerequisiteReason = (() => {+ if (rules.error)+ return `Could not load active Topics facets: ${rules.error.message}`;+ if (!rules.isSuccess) return "Checking the active Topics facets…";+ if (executionsError)+ return `Could not check Topics runs: ${executionsError}`;+ if (executions === undefined)+ return "Checking for Topics runs in progress…";+ return null;+ })();+ const candidates = rules.isSuccess ? ruleFacets(facets, rule) : [];+ const runInProgress = Boolean(+ executions?.some((execution) => isBusy(execution.status)),+ );+ const pausedMessage = settings.data?.blockedAt+ ? (settings.data.blockMessage ?? "")+ : null;+ const disabledReason =+ prerequisiteReason ??+ regenerateDisabledReason({+ canWrite,+ settings: settings.isPending+ ? "loading"+ : {+ configured,+ pausedMessage,+ },+ runInProgress,+ hasTimeRange: timeRange !== null,+ facetCount: candidates.length,+ });+ // Survives reopening the dialog so a retried identical request reuses its+ // requestId and cannot start a second run.+ const request = useRef<{ key: string; id: string } | null>(null);+ const trigger = api.topics.trigger.useMutation();+ const [error, setError] = useState<string | null>(null);+ /** Resolves to the new execution id, or null after recording `error`. */+ async function submit(values: RegenerateRequest) {+ if (!timeRange) return null;+ setError(null);+ const input = {+ projectId,+ operation: "update" as const,+ timeRange,+ ...values,+ };+ const key = JSON.stringify(input);+ if (request.current?.key !== key)+ request.current = { key, id: crypto.randomUUID() };+ try {+ const result = await trigger.mutateAsync({+ ...input,+ requestId: request.current.id,+ });+ request.current = null;+ return result.id;+ } catch (cause) {+ setError(+ cause instanceof Error ? cause.message : "Could not regenerate topics.",+ );+ return null;+ }+ }+ function clearError() {+ if (!trigger.isPending) setError(null);+ }+ return {+ timeRange,+ facets: candidates,+ disabledReason,+ prerequisiteReason,+ isSubmitting: trigger.isPending,+ error,+ clearError,+ submit,+ };+}++function ruleFacets(facets: TopicFacet[], rule: TopicRule | null) {+ return facets.flatMap((facet) => {+ const version = facet.versions[0];+ if (!version || (rule && !rule.facetIds.includes(facet.id))) return [];+ return [{ facet, version }];+ });+}++function regenerateDisabledReason({+ canWrite,+ settings,+ runInProgress,+ hasTimeRange,+ facetCount,+}: {+ canWrite: boolean;+ settings: "loading" | { configured: boolean; pausedMessage: string | null };+ runInProgress: boolean;+ hasTimeRange: boolean;+ facetCount: number;+}): string | null {+ if (!canWrite)+ return "You need permission to manage Topics in this project to regenerate topics.";+ if (settings === "loading") return "Checking the Topics setup…";+ if (settings.pausedMessage !== null)+ return ["Topics is paused.", settings.pausedMessage]+ .filter(Boolean)+ .join(" ");+ if (!settings.configured)+ return "Choose the Topics models in Configure Topics first.";+ if (runInProgress)+ return "A Topics run is in progress. Regenerate when it finishes.";+ if (!hasTimeRange) return "Choose a valid summary time range.";+ if (facetCount === 0)+ return "Turn on at least one facet in Configure Topics.";+ return null;+}++const isBusy = (status: TopicExecutionStatus) =>+ status === "queued" || status === "running";++type TopicExecutionList = RouterOutputs["topics"]["executions"];+export type RegenerateRequest = {+ facets: TopicFacetRef[];+ exploratory: boolean;+ minimumTraceCount: number;+};+export type RegenerateTopics = ReturnType<typeof useRegenerateTopics>;