🪢 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
refactor(topics): open Topics dialogs through DialogController (#18771)
Lysander Kiesel655fb94de800
diff --git a/web/src/features/topics/AutomaticProcessingDialog.stories.tsx b/web/src/features/topics/AutomaticProcessingDialog.stories.tsxindex f55aff3068..443cf11a2a 100644--- a/web/src/features/topics/AutomaticProcessingDialog.stories.tsx+++ b/web/src/features/topics/AutomaticProcessingDialog.stories.tsx@@ -1,7 +1,7 @@-import * as DialogPrimitive from "@radix-ui/react-dialog";import { expect, fn, userEvent, within } from "storybook/test";import preview from "../../../.storybook/preview";+import { DialogController } from "@/src/components/design-system/DialogController/DialogController";import { AutomaticProcessingDialog } from "./AutomaticProcessingDialog";const models = {@@ -15,9 +15,12 @@ const meta = preview.meta({parameters: { layout: "fullscreen" },decorators: [(Story) => (- <DialogPrimitive.Root open>- <Story />- </DialogPrimitive.Root>+ <DialogController<null>+ initialState={() => null}+ renderDialog={() => <Story />}+ >+ {() => null}+ </DialogController>),],args: {diff --git a/web/src/features/topics/AutomaticProcessingDialog.tsx b/web/src/features/topics/AutomaticProcessingDialog.tsxindex fee8564fbf..b85829d841 100644--- a/web/src/features/topics/AutomaticProcessingDialog.tsx+++ b/web/src/features/topics/AutomaticProcessingDialog.tsx@@ -25,7 +25,7 @@ type AutomaticProcessingDialogProps = {const COST_NOTE ="Each sampled trace makes LLM calls on your connections, so cost depends on your trace volume, the selected models, and the sampling rate.";-/** Dialog content; render it inside a Radix `Dialog.Root`. */+/** Dialog content; render it from `AutomaticProcessingDialogController`. */export function AutomaticProcessingDialog({error,initialEnabled,diff --git a/web/src/features/topics/AutomaticProcessingDialogController.tsx b/web/src/features/topics/AutomaticProcessingDialogController.tsxnew file mode 100644index 0000000000..8a1112368c--- /dev/null+++ b/web/src/features/topics/AutomaticProcessingDialogController.tsx@@ -0,0 +1,95 @@+import { useState, type ReactNode } from "react";+import { DialogController } from "@/src/components/design-system/DialogController/DialogController";+import { type TopicRule } from "@langfuse/shared/topics";+import { type RouterOutputs } from "@/src/utils/api";+import { AutomaticProcessingDialog } from "./AutomaticProcessingDialog";+import { automaticProcessingState } from "./useAutomaticProcessing";++type StoredSettings = RouterOutputs["topics"]["modelSettings"];++type AutomaticProcessingSettings = {+ rule: TopicRule | null;+ stored: StoredSettings | undefined;+ traceIdleMs: number | null;+ /** The write shared with "Keep off", so it outlives a close. */+ save: (input: { enabled: boolean; sampling: number }) => Promise<unknown>;+ saving: boolean;+};++type AutomaticProcessingDialogControllerProps = AutomaticProcessingSettings & {+ children: (control: {+ openDialog: (initialEnabled: boolean) => void;+ }) => ReactNode;+};++/**+ * Owns the automatic processing dialog. Wrap the shared parent and pass+ * `openDialog` to every opener, e.g. the header control and the paused notice.+ */+export function AutomaticProcessingDialogController({+ children,+ ...settings+}: AutomaticProcessingDialogControllerProps) {+ return (+ <DialogController<boolean>+ renderDialog={({ state, closeDialog }) => (+ <AutomaticProcessingDialogContent+ {...settings}+ initialEnabled={state}+ closeDialog={closeDialog}+ />+ )}+ >+ {({ openDialog }) => children({ openDialog })}+ </DialogController>+ );+}++function AutomaticProcessingDialogContent({+ closeDialog,+ initialEnabled,+ rule,+ save,+ saving,+ stored,+ traceIdleMs,+}: AutomaticProcessingSettings & {+ closeDialog: () => void;+ initialEnabled: boolean;+}) {+ const [error, setError] = useState<string | null>(null);+ const state = automaticProcessingState(stored, rule);+ const paused =+ stored?.blockMessage && state.status === "paused"+ ? { shortLabel: state.shortLabel, message: stored.blockMessage }+ : null;+ const submit = (input: { enabled: boolean; sampling: number }) => {+ setError(null);+ save(input)+ .then(closeDialog)+ .catch((cause: unknown) =>+ setError(+ cause instanceof Error+ ? cause.message+ : "Could not save automatic processing.",+ ),+ );+ };++ return (+ <AutomaticProcessingDialog+ initialEnabled={initialEnabled}+ initialSampling={rule?.sampling ?? 1}+ traceIdleMs={traceIdleMs ?? 600_000}+ models={{+ summary: stored?.summary?.model ?? null,+ embedding: stored?.embedding?.model ?? null,+ clustering: stored?.clustering?.model ?? null,+ }}+ paused={paused}+ saving={saving}+ error={error}+ onSave={submit}+ />+ );+}diff --git a/web/src/features/topics/ConfigureTopicsDialog.stories.tsx b/web/src/features/topics/ConfigureTopicsDialog.stories.tsxindex 77dd11c400..4e89151811 100644--- a/web/src/features/topics/ConfigureTopicsDialog.stories.tsx+++ b/web/src/features/topics/ConfigureTopicsDialog.stories.tsx@@ -1,6 +1,7 @@import { expect, fn, userEvent, within } from "storybook/test";import preview from "../../../.storybook/preview";+import { DialogController } from "@/src/components/design-system/DialogController/DialogController";import {ConfigureTopicsDialog,type ConfigureTopicsDraft,@@ -70,8 +71,19 @@ const filledDraft = {const meta = preview.meta({component: ConfigureTopicsDialog,parameters: { layout: "fullscreen" },+ decorators: [+ (Story) => (+ <DialogController<null>+ initialState={() => null}+ renderDialog={() => <Story />}+ >+ {() => null}+ </DialogController>+ ),+ ],args: {providerGroups,+ closeDialog: fn(),onConfigureProviders: fn(),onSave: fn(),},@@ -80,18 +92,14 @@ const meta = preview.meta({export const NeedsSetup = meta.story({args: {draft: emptyDraft,- defaultOpen: true,notice: "none",- triggerVariant: "primary",},});export const Configured = meta.story({args: {draft: filledDraft,- defaultOpen: true,notice: "none",- triggerVariant: "secondary",},});@@ -99,9 +107,7 @@ export const SavesTheSheet = meta.story({name: "(Test) Save sends facets, filter, and idle time",args: {draft: filledDraft,- defaultOpen: true,notice: "none",- triggerVariant: "secondary",},play: async ({ args }) => {const body = within(document.body);@@ -129,13 +135,11 @@ export const SavesTheSheet = meta.story({export const Paused = meta.story({args: {draft: { ...filledDraft, automaticProcessing: false },- defaultOpen: true,notice: "paused",shortLabel: "LLM connection missing",pausedAgo: "3 days ago",message:'The LLM connection "Amazon Bedrock" was deleted. Choose another connection in Configure Topics, then turn automatic processing back on.',- triggerVariant: "primary",},});@@ -143,9 +147,7 @@ export const SavesTurnedOffWithoutModelTests = meta.story({name: "(Test) Save with automatic processing off ignores failing unchanged models",args: {draft: { ...filledDraft, automaticProcessing: false },- defaultOpen: true,notice: "none",- triggerVariant: "secondary",slotChecks: {summary: { status: "ok" },embedding: { status: "ok" },@@ -165,9 +167,7 @@ export const BlocksSaveWhileRunningWithFailingModel = meta.story({name: "(Test) Save with automatic processing on needs every model to pass",args: {draft: filledDraft,- defaultOpen: true,notice: "none",- triggerVariant: "secondary",slotChecks: {summary: { status: "ok" },embedding: { status: "ok" },diff --git a/web/src/features/topics/ConfigureTopicsDialog.tsx b/web/src/features/topics/ConfigureTopicsDialog.tsxindex d36262aec7..d917ddb32b 100644--- a/web/src/features/topics/ConfigureTopicsDialog.tsx+++ b/web/src/features/topics/ConfigureTopicsDialog.tsx@@ -1,7 +1,5 @@import { useId, useMemo, useState } from "react";-import * as DialogPrimitive from "@radix-ui/react-dialog";import { AlertTriangle, ExternalLinkIcon } from "lucide-react";-import { Button } from "@/src/components/design-system/Button/Button";import { Dialog } from "@/src/components/design-system/Dialog/Dialog";import DocPopup from "@/src/components/layouts/doc-popup";import { Input } from "@/src/components/design-system/Input/Input";@@ -90,10 +88,7 @@ type ConfigureTopicsDialogProps = {providerGroups: Array<[string, string[]]>;embeddingProviderGroups?: Array<[string, string[]]>;draft: ConfigureTopicsDraft;- defaultOpen: boolean;- /** Controls the dialog from outside, e.g. an empty state that opens it. */- open?: boolean;- onOpenChange?: (open: boolean) => void;+ closeDialog: () => void;onConfigureProviders: () => void;onSave: (draft: ConfigureTopicsSaveDraft) => void | Promise<void>;onSlotCommit?: (@@ -110,7 +105,6 @@ type ConfigureTopicsDialogProps = {projectId?: string;/** Storybook keeps the sample filter. The project page chooses traces when a run starts. */sampleScope?: boolean;- triggerVariant: "primary" | "secondary";} & (| { notice: "none" }| {@@ -140,9 +134,10 @@ const SLOT_COPY = {},} as const;+/** Dialog content; render it from a `DialogController`. */export function ConfigureTopicsDialog(props: ConfigureTopicsDialogProps) {const {- defaultOpen,+ closeDialog,draft,canWrite = true,onConfigureProviders,@@ -155,7 +150,6 @@ export function ConfigureTopicsDialog(props: ConfigureTopicsDialogProps) {saveError = null,saving = false,slotChecks,- triggerVariant,} = props;const paused =props.notice === "paused"@@ -165,9 +159,6 @@ export function ConfigureTopicsDialog(props: ConfigureTopicsDialogProps) {message: props.message,}: null;- const [localOpen, setLocalOpen] = useState(defaultOpen);- const open = props.open ?? localOpen;- const setOpen = props.onOpenChange ?? setLocalOpen;const formId = useId();const [summary, setSummary] = useState(draft.summary);const [embedding, setEmbedding] = useState(draft.embedding);@@ -227,170 +218,160 @@ export function ConfigureTopicsDialog(props: ConfigureTopicsDialogProps) {};return (- <div className="flex flex-col items-start gap-4">- <Button- text="Configure Topics"- variant={triggerVariant}- size="sm"- onClick={() => setOpen(true)}- />- <DialogPrimitive.Root open={open} onOpenChange={setOpen}>- <Dialog- title="Configure Topics"- size="lg"- actions={[- {- label: "Save",- type: "submit",- form: formId,- loading: saving,- disabled: !canSave,- },- ]}- >- <form- id={formId}- className="flex min-h-0 flex-1 flex-col"- onSubmit={(event) => {- event.preventDefault();- Promise.resolve(- onSave({- summary,- embedding,- embeddingDimensions: dimensions,- clustering,- facets,- filters,- idleSeconds,- }),- )- .then(() => setOpen(false))- .catch(() => undefined);- }}+ <Dialog+ title="Configure Topics"+ size="lg"+ actions={[+ {+ label: "Save",+ type: "submit",+ form: formId,+ loading: saving,+ disabled: !canSave,+ },+ ]}+ >+ <form+ id={formId}+ className="flex min-h-0 flex-1 flex-col"+ onSubmit={(event) => {+ event.preventDefault();+ Promise.resolve(+ onSave({+ summary,+ embedding,+ embeddingDimensions: dimensions,+ clustering,+ facets,+ filters,+ idleSeconds,+ }),+ )+ .then(closeDialog)+ .catch(() => undefined);+ }}+ >+ <div className="flex min-h-0 flex-1 flex-col gap-4 overflow-y-auto px-4 pb-4">+ <p className="text-muted-foreground text-sm">+ Models, what to look for, and which traces to run.+ </p>+ {paused ? (+ <PausedNotice+ shortLabel={paused.shortLabel}+ pausedAgo={paused.pausedAgo}+ message={paused.message}+ onConfigureProviders={onConfigureProviders}+ />+ ) : null}+ <Stepper+ number={1}+ title="Models"+ description="The connections and models used for summaries, embeddings, and clustering."+ compactBottomSpacing+ >+ <ModelSlot+ name="summary"+ model={summary}+ locked={false}+ disabled={!canWrite}+ check={slotChecks?.summary}+ connectionMissing={paused !== null}+ providerGroups={providerGroups}+ onConfigureProviders={onConfigureProviders}+ onSelect={(model) => {+ setSummary(model);+ commitSlot("summary", model);+ }}+ />+ <ModelSlot+ name="embedding"+ model={embedding}+ locked={draft.embeddingLocked}+ disabled={!canWrite}+ check={slotChecks?.embedding}+ dimensions={dimensions}+ onDimensionsChange={(value) => {+ setDimensions(value);+ if (embedding) commitSlot("embedding", embedding, value);+ }}+ providerGroups={embeddingProviderGroups}+ onConfigureProviders={onConfigureProviders}+ onSelect={(model) => {+ setEmbedding(model);+ commitSlot("embedding", model);+ }}+ />+ <ModelSlot+ name="clustering"+ model={clustering}+ locked={false}+ disabled={!canWrite}+ check={slotChecks?.clustering}+ providerGroups={providerGroups}+ onConfigureProviders={onConfigureProviders}+ onSelect={(model) => {+ setClustering(model);+ commitSlot("clustering", model);+ }}+ />+ </Stepper>+ <Stepper+ number={2}+ title="Facets"+ description={+ <>+ Choose which questions you want answered. Five can be on.+ <DocPopup description="A facet is one question Topics answers for every trace, such as what the user asked for, how the run ended, or which issues came up. Answers to the same facet are clustered into topics." />+ </>+ }+ compactBottomSpacing+ >+ <FacetPicker+ facets={facets}+ browsing={browsing}+ browsed={browsed}+ atCap={atFacetCap}+ canWrite={canWrite}+ onBrowse={setBrowsing}+ onEnabledChange={setFacetEnabled}+ />+ </Stepper>+ <Stepper+ number={3}+ title="Run settings"+ description="Choose which traces to include and set the idle time."+ compactBottomSpacing>- <div className="flex min-h-0 flex-1 flex-col gap-4 overflow-y-auto px-4 pb-4">+ <div className="flex items-center gap-2">+ <div className="w-24">+ <Input+ aria-label="Seconds to wait after the last observation"+ type="number"+ inputMode="numeric"+ min={60}+ value={idleSeconds}+ onChange={(event) => setIdleSeconds(event.target.value)}+ />+ </div><p className="text-muted-foreground text-sm">- Models, what to look for, and which traces to run.+ seconds to wait after the last observation before considering+ the trace done and summarizing it.</p>- {paused ? (- <PausedNotice- shortLabel={paused.shortLabel}- pausedAgo={paused.pausedAgo}- message={paused.message}- onConfigureProviders={onConfigureProviders}- />- ) : null}- <Stepper- number={1}- title="Models"- description="The connections and models used for summaries, embeddings, and clustering."- compactBottomSpacing- >- <ModelSlot- name="summary"- model={summary}- locked={false}- disabled={!canWrite}- check={slotChecks?.summary}- connectionMissing={paused !== null}- providerGroups={providerGroups}- onConfigureProviders={onConfigureProviders}- onSelect={(model) => {- setSummary(model);- commitSlot("summary", model);- }}- />- <ModelSlot- name="embedding"- model={embedding}- locked={draft.embeddingLocked}- disabled={!canWrite}- check={slotChecks?.embedding}- dimensions={dimensions}- onDimensionsChange={(value) => {- setDimensions(value);- if (embedding) commitSlot("embedding", embedding, value);- }}- providerGroups={embeddingProviderGroups}- onConfigureProviders={onConfigureProviders}- onSelect={(model) => {- setEmbedding(model);- commitSlot("embedding", model);- }}- />- <ModelSlot- name="clustering"- model={clustering}- locked={false}- disabled={!canWrite}- check={slotChecks?.clustering}- providerGroups={providerGroups}- onConfigureProviders={onConfigureProviders}- onSelect={(model) => {- setClustering(model);- commitSlot("clustering", model);- }}- />- </Stepper>- <Stepper- number={2}- title="Facets"- description={- <>- Choose which questions you want answered. Five can be on.- <DocPopup description="A facet is one question Topics answers for every trace, such as what the user asked for, how the run ended, or which issues came up. Answers to the same facet are clustered into topics." />- </>- }- compactBottomSpacing- >- <FacetPicker- facets={facets}- browsing={browsing}- browsed={browsed}- atCap={atFacetCap}- canWrite={canWrite}- onBrowse={setBrowsing}- onEnabledChange={setFacetEnabled}- />- </Stepper>- <Stepper- number={3}- title="Run settings"- description="Choose which traces to include and set the idle time."- compactBottomSpacing- >- <div className="flex items-center gap-2">- <div className="w-24">- <Input- aria-label="Seconds to wait after the last observation"- type="number"- inputMode="numeric"- min={60}- value={idleSeconds}- onChange={(event) => setIdleSeconds(event.target.value)}- />- </div>- <p className="text-muted-foreground text-sm">- seconds to wait after the last observation before- considering the trace done and summarizing it.- </p>- </div>- {sampleScope ? (- <RuleScope- projectId={projectId}- filters={filters}- onFiltersChange={setFilters}- />- ) : null}- </Stepper></div>- {saveError ? (- <p className="text-destructive px-4 pb-2 text-sm">{saveError}</p>+ {sampleScope ? (+ <RuleScope+ projectId={projectId}+ filters={filters}+ onFiltersChange={setFilters}+ />) : null}- </form>- </Dialog>- </DialogPrimitive.Root>- </div>+ </Stepper>+ </div>+ {saveError ? (+ <p className="text-destructive px-4 pb-2 text-sm">{saveError}</p>+ ) : null}+ </form>+ </Dialog>);}diff --git a/web/src/features/topics/ConfigureTopicsDialogController.tsx b/web/src/features/topics/ConfigureTopicsDialogController.tsxnew file mode 100644index 0000000000..c570e60d42--- /dev/null+++ b/web/src/features/topics/ConfigureTopicsDialogController.tsx@@ -0,0 +1,421 @@+import { useRef, useState, type ReactNode } from "react";+import { formatDistanceToNow } from "date-fns";+import { DialogController } from "@/src/components/design-system/DialogController/DialogController";+import { env } from "@/src/env.mjs";+import { type JudgeModel } from "@/src/features/evals/v2/judgeModel";+import { api, type RouterOutputs } from "@/src/utils/api";+import {+ getEvaluatorBlockMetadata,+ LLMAdapter,+ supportedModels,+} from "@langfuse/shared";+import {+ DEFAULT_TOPIC_FACETS,+ topicsModelSettingsSchema,+ type TopicFacet,+ type TopicsModelSlotName,+} from "@langfuse/shared/topics";+import {+ ConfigureTopicsDialog,+ type ConfigureTopicsSaveDraft,+ type TopicSlotCheck,+} from "./ConfigureTopicsDialog";+import { type ConfigureTopicsSettingsInput } from "./TopicModelSettings";+import { topicsSetupPayload } from "./topics-setup";++type Connection = RouterOutputs["llmApiKey"]["effective"]["data"][number];+type SlotValue = { llmApiKeyId: string; model: string } | null;++type ConfigureTopicsDialogControllerProps = {+ projectId: string;+ settings: ConfigureTopicsSettingsInput | null;+ children: (control: { openDialog: () => void }) => ReactNode;+};++/**+ * Owns the Configure Topics dialog. Wrap the shared parent and pass+ * `openDialog` to every opener, e.g. the header button and the setup screen.+ * Openers stay disabled until `settings` has loaded. The save request outlives+ * a close, so a reopened dialog cannot start an overlapping save.+ */+export function ConfigureTopicsDialogController({+ projectId,+ settings,+ children,+}: ConfigureTopicsDialogControllerProps) {+ const save = useSaveTopicsSetup(projectId);+ return (+ <DialogController+ renderDialog={({ closeDialog }) =>+ settings ? (+ <ConfigureTopicsSettings+ {...settings}+ save={save}+ closeDialog={closeDialog}+ />+ ) : null+ }+ >+ {({ openDialog }) => {+ function openConfigureDialog() {+ openDialog();+ }+ return children({ openDialog: openConfigureDialog });+ }}+ </DialogController>+ );+}++function ConfigureTopicsSettings({+ projectId,+ canWrite,+ stored,+ traceIdleMs,+ connections,+ facets,+ rule,+ save,+ closeDialog,+}: ConfigureTopicsSettingsInput & {+ save: ReturnType<typeof useSaveTopicsSetup>;+ closeDialog: () => void;+}) {+ const requests = useRef({ summary: 0, embedding: 0, clustering: 0 });+ const [checks, setChecks] = useState<+ Record<TopicsModelSlotName, TopicSlotCheck>+ >(() => ({+ summary: stored.summary ? { status: "ok" } : { status: "idle" },+ embedding: stored.embedding ? { status: "ok" } : { status: "idle" },+ clustering: stored.clustering ? { status: "ok" } : { status: "idle" },+ }));+ const [saveError, setSaveError] = useState<string | null>(null);+ const test = api.topics.testModelSettings.useMutation();+ const providerGroups = connections.map<[string, string[]]>((connection) => [+ connection.provider,+ modelsFor(connection, "chat"),+ ]);+ const embeddingGroups = connections.map<[string, string[]]>((connection) => [+ connection.provider,+ modelsFor(+ connection,+ "embedding",+ stored.embedding?.llmApiKeyId === connection.id+ ? stored.embedding.model+ : null,+ ),+ ]);+ const draft = {+ summary: judgeModel(stored.summary, connections),+ embedding: judgeModel(stored.embedding, connections),+ embeddingDimensions: String(stored.embeddingDimensions),+ clustering: judgeModel(stored.clustering, connections),+ facets: facetDrafts(facets, rule ? new Set(rule.facetIds) : null),+ filters: [],+ idleSeconds: String((traceIdleMs ?? 600_000) / 1000),+ embeddingLocked: false,+ automaticProcessing: stored.enabled ?? false,+ };+ const paused = stored.blockMessage+ ? {+ shortLabel: stored.blockReason+ ? getEvaluatorBlockMetadata(stored.blockReason).shortLabel+ : "Paused",+ pausedAgo: stored.blockedAt+ ? formatDistanceToNow(new Date(stored.blockedAt), { addSuffix: true })+ : "just now",+ message: stored.blockMessage,+ }+ : null;++ const commitSlot = (+ slot: TopicsModelSlotName,+ model: JudgeModel,+ dimensions: string,+ ) => {+ const parsedDimensions =+ topicsModelSettingsSchema.shape.embeddingDimensions.safeParse(+ Number(dimensions),+ );+ if (slot === "embedding" && !parsedDimensions.success) {+ setChecks((current) => ({+ ...current,+ embedding: {+ status: "error",+ message: parsedDimensions.error.issues[0]?.message,+ },+ }));+ return;+ }+ const preferred = {+ summary: stored.summary?.llmApiKeyId,+ embedding: stored.embedding?.llmApiKeyId,+ clustering: stored.clustering?.llmApiKeyId,+ }[slot];+ const llmApiKeyId = connectionId(+ model,+ connections,+ preferred,+ slot === "embedding" ? "embedding" : "chat",+ slot === "embedding" ? stored.embedding?.model : null,+ );+ if (!llmApiKeyId) {+ setChecks((current) => ({+ ...current,+ [slot]: {+ status: "error",+ message: "Choose a model from a connection in this project.",+ },+ }));+ return;+ }+ const request = ++requests.current[slot];+ setChecks((current) => ({ ...current, [slot]: { status: "testing" } }));+ const value = { llmApiKeyId, model: model.model };+ test+ .mutateAsync({+ projectId,+ summary: slot === "summary" ? value : null,+ embedding: slot === "embedding" ? value : null,+ embeddingDimensions: parsedDimensions.success+ ? parsedDimensions.data+ : stored.embeddingDimensions,+ clustering: slot === "clustering" ? value : null,+ })+ .then((errors) => {+ if (requests.current[slot] !== request) return;+ const message = errors[slot];+ setChecks((current) => ({+ ...current,+ [slot]: message ? { status: "error", message } : { status: "ok" },+ }));+ })+ .catch((error: unknown) => {+ if (requests.current[slot] !== request) return;+ setChecks((current) => ({+ ...current,+ [slot]: {+ status: "error",+ message:+ error instanceof Error ? error.message : "The test call failed.",+ },+ }));+ });+ };++ const onSave = async (value: ConfigureTopicsSaveDraft) => {+ setSaveError(null);+ const parsedDimensions =+ topicsModelSettingsSchema.shape.embeddingDimensions.safeParse(+ Number(value.embeddingDimensions),+ );+ if (!parsedDimensions.success)+ throw new Error(+ parsedDimensions.error.issues[0]?.message ??+ "Check the embedding dimensions.",+ );+ const summary = slotValue(+ value.summary,+ connections,+ stored.summary?.llmApiKeyId,+ "chat",+ );+ const embedding = slotValue(+ value.embedding,+ connections,+ stored.embedding?.llmApiKeyId,+ "embedding",+ stored.embedding?.model,+ );+ const clustering = slotValue(+ value.clustering,+ connections,+ stored.clustering?.llmApiKeyId,+ "chat",+ );+ if (stored.enabled && (!summary || !embedding || !clustering))+ throw new Error("Choose a model from a connection in this project.");+ try {+ await save.mutateAsync({+ projectId,+ ...topicsSetupPayload(value, {+ summary,+ embedding,+ embeddingDimensions: parsedDimensions.data,+ clustering,+ }),+ ruleId: rule?.id,+ });+ } catch (error) {+ const message =+ error instanceof Error ? error.message : "Could not save Topics.";+ setSaveError(message);+ throw error;+ }+ };++ return (+ <ConfigureTopicsDialog+ projectId={projectId}+ providerGroups={providerGroups}+ embeddingProviderGroups={embeddingGroups}+ draft={draft}+ closeDialog={closeDialog}+ canWrite={canWrite}+ saving={save.isPending}+ saveError={saveError}+ slotChecks={checks}+ onSlotCommit={commitSlot}+ onConfigureProviders={() => {+ window.open(+ `${env.NEXT_PUBLIC_BASE_PATH ?? ""}/project/${projectId}/settings/llm-connections`,+ "_blank",+ "noopener,noreferrer",+ );+ }}+ onSave={onSave}+ {...(paused+ ? { notice: "paused" as const, ...paused }+ : { notice: "none" as const })}+ />+ );+}++function useSaveTopicsSetup(projectId: string) {+ const utils = api.useUtils();+ return api.topics.saveSetup.useMutation({+ onSuccess: () => {+ utils.topics.modelSettings.invalidate({ projectId });+ utils.topics.facets.invalidate({ projectId });+ utils.topics.rules.invalidate({ projectId });+ utils.topics.tracingReadinessSettings.invalidate({ projectId });+ },+ });+}++const OPENAI_EMBEDDING_MODELS = [+ "text-embedding-3-small",+ "text-embedding-3-large",+ "text-embedding-ada-002",+];++const GOOGLE_EMBEDDING_MODELS = [+ "textembedding-gecko",+ "textembedding-gecko-multilingual",+];++function embeddingModelsFor(adapter: string) {+ if (adapter === LLMAdapter.OpenAI || adapter === LLMAdapter.Azure)+ return OPENAI_EMBEDDING_MODELS;+ if (adapter === LLMAdapter.GoogleAIStudio || adapter === LLMAdapter.VertexAI)+ return GOOGLE_EMBEDDING_MODELS;+ return [];+}++function modelsFor(+ connection: Connection,+ kind: "chat" | "embedding",+ savedModel?: string | null,+) {+ const chat = connection.withDefaultModels+ ? (supportedModels[connection.adapter as keyof typeof supportedModels] ??+ [])+ : [];+ const embedding =+ kind === "embedding" && connection.withDefaultModels+ ? embeddingModelsFor(connection.adapter)+ : [];+ return Array.from(+ new Set(+ [savedModel, ...embedding, ...connection.customModels, ...chat].filter(+ (model): model is string => Boolean(model),+ ),+ ),+ );+}++function judgeModel(+ slot: SlotValue,+ connections: Connection[],+): JudgeModel | null {+ if (!slot) return null;+ const connection = connections.find((item) => item.id === slot.llmApiKeyId);+ return {+ provider: connection?.provider ?? "Missing connection",+ model: slot.model,+ };+}++function connectionId(+ model: JudgeModel,+ connections: Connection[],+ preferredId: string | undefined,+ kind: "chat" | "embedding",+ savedModel?: string | null,+) {+ const matches = connections.filter(+ (connection) =>+ connection.provider === model.provider &&+ modelsFor(+ connection,+ kind,+ connection.id === preferredId ? savedModel : null,+ ).includes(model.model),+ );+ return (+ matches.find((connection) => connection.id === preferredId)?.id ??+ matches[0]?.id ??+ null+ );+}++function slotValue(+ model: JudgeModel | null,+ connections: Connection[],+ preferredId: string | undefined,+ kind: "chat" | "embedding",+ savedModel?: string | null,+) {+ if (!model) return null;+ const llmApiKeyId = connectionId(+ model,+ connections,+ preferredId,+ kind,+ savedModel,+ );+ return llmApiKeyId ? { llmApiKeyId, model: model.model } : null;+}++function facetDrafts(+ facets: TopicFacet[],+ enabledIds: ReadonlySet<string> | null,+) {+ const byName = new Map(facets.map((facet) => [facet.name, facet]));+ const builtIn = DEFAULT_TOPIC_FACETS.map((preset) => {+ const existing = byName.get(preset.name);+ return {+ id: existing?.id ?? preset.name,+ name: preset.name,+ question: existing?.versions[0]?.prompt ?? preset.prompt,+ builtIn: true,+ enabled: enabledIds+ ? Boolean(existing && enabledIds.has(existing.id))+ : true,+ };+ });+ const custom = facets.flatMap((facet) => {+ if (facet.isBuiltIn) return [];+ const question = facet.versions[0]?.prompt;+ if (!question) return [];+ return [+ {+ id: facet.id,+ name: facet.name,+ question,+ builtIn: false,+ enabled: enabledIds?.has(facet.id) ?? false,+ },+ ];+ });+ return [...builtIn, ...custom];+}diff --git a/web/src/features/topics/TopicModelSettings.tsx b/web/src/features/topics/TopicModelSettings.tsxindex ffec9e953e..ac8c5e80a0 100644--- a/web/src/features/topics/TopicModelSettings.tsx+++ b/web/src/features/topics/TopicModelSettings.tsx@@ -1,36 +1,34 @@-import { useRef, useState } from "react";-import { formatDistanceToNow } from "date-fns";-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";-import { env } from "@/src/env.mjs";-import { type JudgeModel } from "@/src/features/evals/v2/judgeModel";import { api, type RouterOutputs } from "@/src/utils/api";-import {- getEvaluatorBlockMetadata,- LLMAdapter,- supportedModels,-} from "@langfuse/shared";-import {- DEFAULT_TOPIC_FACETS,- topicsModelSettingsSchema,- type TopicFacet,- type TopicRule,- type TopicsModelSlotName,-} from "@langfuse/shared/topics";-import {- ConfigureTopicsDialog,- type ConfigureTopicsSaveDraft,- type TopicSlotCheck,-} from "./ConfigureTopicsDialog";-import { topicsSetupPayload } from "./topics-setup";+import { type TopicFacet, type TopicRule } from "@langfuse/shared/topics";import { useAutomaticProcessing } from "./useAutomaticProcessing";type StoredSettings = RouterOutputs["topics"]["modelSettings"];type Connection = RouterOutputs["llmApiKey"]["effective"]["data"][number];-type SlotValue = { llmApiKeyId: string; model: string } | null;-/** Topics setup: one dialog for models, facets, and which traces to include. */+export type ConfigureTopicsSettingsInput = {+ projectId: string;+ canWrite: boolean;+ stored: StoredSettings;+ traceIdleMs: number | null;+ connections: Connection[];+ facets: TopicFacet[];+ rule: TopicRule | null;+};++export type TopicSetupNoticeState = {+ unavailable: boolean;+ status:+ | { kind: "paused"; message: string }+ | { kind: "setup" }+ | { kind: "models" }+ | null;+};++/**+ * Topics setup state. The page wraps itself in `ConfigureTopicsDialogController`+ * and `AutomaticProcessingDialogController` and wires their `openDialog` into+ * the header buttons and `TopicSetupNotice`.+ */export function useTopicModelSettings({projectId,canWrite,@@ -54,524 +52,57 @@ export function useTopicModelSettings({settings.data.clustering,);const rule = rules.data?.length === 1 ? rules.data[0] : null;- const dataKey = `${settings.dataUpdatedAt}-${rules.dataUpdatedAt}-${readinessSettings.dataUpdatedAt}`;- // Pins the key while Configure is open so a refetch cannot reset unsaved edits.- const [openKey, setOpenKey] = useState<string | null>(null);- const setConfigureOpen = (open: boolean) => setOpenKey(open ? dataKey : null);const automaticProcessing = useAutomaticProcessing({- canWrite,projectId,rule,stored: settings.data,- traceIdleMs: readinessSettings.data?.traceIdleMs ?? null,});- const unavailable = settings.data && !settings.data.processingAvailable && (- <Alert variant="info">- <Alert.Title>- Topics processing is not enabled for this project- </Alert.Title>- <Alert.Description>- <p>- You can configure Topics, but processing traces and automatic- processing stay unavailable until Topics is enabled for this project.- </p>- </Alert.Description>- </Alert>- );- let notice = null;+ let status: TopicSetupNoticeState["status"] = null;if (settings.data?.blockMessage)- notice = (- <Alert variant="warning">- <Alert.Title>Automatic Topics processing is paused</Alert.Title>- <Alert.Description>- <p className="break-words">{settings.data.blockMessage}</p>- <div className="mt-2 flex flex-wrap gap-2">- <Button- text="Turn back on"- variant="secondary"- size="sm"- disabled={!canWrite}- onClick={automaticProcessing.openEnable}- />- <Button- text="Keep off"- variant="secondary"- size="sm"- disabled={!canWrite}- onClick={automaticProcessing.clearPause}- />- </div>- </Alert.Description>- </Alert>- );+ status = { kind: "paused", message: settings.data.blockMessage };else if (settings.data && !configured && facetsReady && !facets.length)- notice = (- <div className="mx-auto w-full max-w-xl pt-12">- <SplashScreen- title="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",- description:- "Pick the models Topics runs on and the facets to analyze, such as intent, outcome, and issues.",- },- {- title: "Analyze traces",- description:- "Topics summarizes new traces automatically, and past traces whenever you process them.",- },- {- title: "Gather insights",- description:- "Similar summaries are clustered into topics, on demand or continuously, so patterns across your traces show up here.",- },- ]}- primaryAction={{- label: "Configure Topics",- component: (- <Button- text="Configure Topics"- disabled={!canWrite}- onClick={() => setConfigureOpen(true)}- />- ),- }}- />- </div>- );- else if (settings.data && !configured)- notice = (- <Alert variant="info">- <Alert.Title>Choose the models Topics runs on</Alert.Title>- <Alert.Description>- <p>- Topics uses your LLM connections for three jobs: facet summaries,- embeddings, and topic clustering. Open Configure Topics to set all- three.- </p>- </Alert.Description>- </Alert>- );+ status = { kind: "setup" };+ else if (settings.data && !configured) status = { kind: "models" };- const ready = Boolean(+ const configureSettings: ConfigureTopicsSettingsInput | null =settings.data &&readinessSettings.data &&connections.data &&rules.isSuccess &&- facetsReady,- );- const action = ready ? (- <ConfigureTopicsSettings- key={openKey ?? dataKey}- projectId={projectId}- canWrite={canWrite}- stored={settings.data!}- traceIdleMs={readinessSettings.data!.traceIdleMs}- connections={connections.data!.data}- facets={facets}- rule={rule}- open={openKey !== null}- onOpenChange={setConfigureOpen}- />- ) : (- <ConfigureTopicsDialog- projectId={projectId}- providerGroups={[]}- draft={{- summary: null,- embedding: null,- embeddingDimensions: "1024",- clustering: null,- facets: [],- filters: [],- idleSeconds: "600",- embeddingLocked: false,- automaticProcessing: false,- }}- defaultOpen={false}- onConfigureProviders={() => undefined}- onSave={() => undefined}- canWrite={false}- triggerVariant="secondary"- notice="none"- />- );+ facetsReady+ ? {+ projectId,+ canWrite,+ stored: settings.data,+ traceIdleMs: readinessSettings.data.traceIdleMs,+ connections: connections.data.data,+ facets,+ rule,+ }+ : null;return {- action,- notice: (- <>- {unavailable}- {notice}- </>- ),- automaticProcessing: automaticProcessing.control,- };-}--function ConfigureTopicsSettings({- projectId,- canWrite,- stored,- traceIdleMs,- connections,- facets,- rule,- open,- onOpenChange,-}: {- projectId: string;- canWrite: boolean;- stored: StoredSettings;- traceIdleMs: number | null;- connections: Connection[];- facets: TopicFacet[];- rule: TopicRule | null;- open: boolean;- onOpenChange: (open: boolean) => void;-}) {- const utils = api.useUtils();- const requests = useRef({ summary: 0, embedding: 0, clustering: 0 });- const [checks, setChecks] = useState<- Record<TopicsModelSlotName, TopicSlotCheck>- >(() => ({- summary: stored.summary ? { status: "ok" } : { status: "idle" },- embedding: stored.embedding ? { status: "ok" } : { status: "idle" },- clustering: stored.clustering ? { status: "ok" } : { status: "idle" },- }));- const [saveError, setSaveError] = useState<string | null>(null);- const test = api.topics.testModelSettings.useMutation();- const save = api.topics.saveSetup.useMutation({- onSuccess: () => {- utils.topics.modelSettings.invalidate({ projectId });- utils.topics.facets.invalidate({ projectId });- utils.topics.rules.invalidate({ projectId });- utils.topics.tracingReadinessSettings.invalidate({ projectId });+ configure: {+ settings: configureSettings,+ triggerVariant:+ configureSettings && !configured+ ? ("primary" as const)+ : ("secondary" as const),},- });- const providerGroups = connections.map<[string, string[]]>((connection) => [- connection.provider,- modelsFor(connection, "chat"),- ]);- const embeddingGroups = connections.map<[string, string[]]>((connection) => [- connection.provider,- modelsFor(- connection,- "embedding",- stored.embedding?.llmApiKeyId === connection.id- ? stored.embedding.model- : null,- ),- ]);- const draft = {- summary: judgeModel(stored.summary, connections),- embedding: judgeModel(stored.embedding, connections),- embeddingDimensions: String(stored.embeddingDimensions),- clustering: judgeModel(stored.clustering, connections),- facets: facetDrafts(facets, rule ? new Set(rule.facetIds) : null),- filters: [],- idleSeconds: String((traceIdleMs ?? 600_000) / 1000),- embeddingLocked: false,- automaticProcessing: stored.enabled ?? false,- };- const paused = stored.blockMessage- ? {- shortLabel: stored.blockReason- ? getEvaluatorBlockMetadata(stored.blockReason).shortLabel- : "Paused",- pausedAgo: stored.blockedAt- ? formatDistanceToNow(new Date(stored.blockedAt), { addSuffix: true })- : "just now",- message: stored.blockMessage,- }- : null;-- const commitSlot = (- slot: TopicsModelSlotName,- model: JudgeModel,- dimensions: string,- ) => {- const parsedDimensions =- topicsModelSettingsSchema.shape.embeddingDimensions.safeParse(- Number(dimensions),- );- if (slot === "embedding" && !parsedDimensions.success) {- setChecks((current) => ({- ...current,- embedding: {- status: "error",- message: parsedDimensions.error.issues[0]?.message,- },- }));- return;- }- const preferred = {- summary: stored.summary?.llmApiKeyId,- embedding: stored.embedding?.llmApiKeyId,- clustering: stored.clustering?.llmApiKeyId,- }[slot];- const llmApiKeyId = connectionId(- model,- connections,- preferred,- slot === "embedding" ? "embedding" : "chat",- slot === "embedding" ? stored.embedding?.model : null,- );- if (!llmApiKeyId) {- setChecks((current) => ({- ...current,- [slot]: {- status: "error",- message: "Choose a model from a connection in this project.",- },- }));- return;- }- const request = ++requests.current[slot];- setChecks((current) => ({ ...current, [slot]: { status: "testing" } }));- const value = { llmApiKeyId, model: model.model };- test- .mutateAsync({- projectId,- summary: slot === "summary" ? value : null,- embedding: slot === "embedding" ? value : null,- embeddingDimensions: parsedDimensions.success- ? parsedDimensions.data- : stored.embeddingDimensions,- clustering: slot === "clustering" ? value : null,- })- .then((errors) => {- if (requests.current[slot] !== request) return;- const message = errors[slot];- setChecks((current) => ({- ...current,- [slot]: message ? { status: "error", message } : { status: "ok" },- }));- })- .catch((error: unknown) => {- if (requests.current[slot] !== request) return;- setChecks((current) => ({- ...current,- [slot]: {- status: "error",- message:- error instanceof Error ? error.message : "The test call failed.",- },- }));- });- };-- const onSave = async (value: ConfigureTopicsSaveDraft) => {- setSaveError(null);- const parsedDimensions =- topicsModelSettingsSchema.shape.embeddingDimensions.safeParse(- Number(value.embeddingDimensions),- );- if (!parsedDimensions.success)- throw new Error(- parsedDimensions.error.issues[0]?.message ??- "Check the embedding dimensions.",- );- const summary = slotValue(- value.summary,- connections,- stored.summary?.llmApiKeyId,- "chat",- );- const embedding = slotValue(- value.embedding,- connections,- stored.embedding?.llmApiKeyId,- "embedding",- stored.embedding?.model,- );- const clustering = slotValue(- value.clustering,- connections,- stored.clustering?.llmApiKeyId,- "chat",- );- if (stored.enabled && (!summary || !embedding || !clustering))- throw new Error("Choose a model from a connection in this project.");- try {- await save.mutateAsync({- projectId,- ...topicsSetupPayload(value, {- summary,- embedding,- embeddingDimensions: parsedDimensions.data,- clustering,- }),- ruleId: rule?.id,- });- } catch (error) {- const message =- error instanceof Error ? error.message : "Could not save Topics.";- setSaveError(message);- throw error;- }- };-- return (- <ConfigureTopicsDialog- projectId={projectId}- providerGroups={providerGroups}- embeddingProviderGroups={embeddingGroups}- draft={draft}- defaultOpen={false}- open={open}- onOpenChange={onOpenChange}- canWrite={canWrite}- saving={save.isPending}- saveError={saveError}- slotChecks={checks}- onSlotCommit={commitSlot}- onConfigureProviders={() => {- window.open(- `${env.NEXT_PUBLIC_BASE_PATH ?? ""}/project/${projectId}/settings/llm-connections`,- "_blank",- "noopener,noreferrer",- );- }}- onSave={onSave}- triggerVariant={configured(stored) ? "secondary" : "primary"}- {...(paused- ? { notice: "paused" as const, ...paused }- : { notice: "none" as const })}- />- );-}--function configured(stored: StoredSettings) {- return Boolean(stored.summary && stored.embedding && stored.clustering);-}--const OPENAI_EMBEDDING_MODELS = [- "text-embedding-3-small",- "text-embedding-3-large",- "text-embedding-ada-002",-];--const GOOGLE_EMBEDDING_MODELS = [- "textembedding-gecko",- "textembedding-gecko-multilingual",-];--function embeddingModelsFor(adapter: string) {- if (adapter === LLMAdapter.OpenAI || adapter === LLMAdapter.Azure)- return OPENAI_EMBEDDING_MODELS;- if (adapter === LLMAdapter.GoogleAIStudio || adapter === LLMAdapter.VertexAI)- return GOOGLE_EMBEDDING_MODELS;- return [];-}--function modelsFor(- connection: Connection,- kind: "chat" | "embedding",- savedModel?: string | null,-) {- const chat = connection.withDefaultModels- ? (supportedModels[connection.adapter as keyof typeof supportedModels] ??- [])- : [];- const embedding =- kind === "embedding" && connection.withDefaultModels- ? embeddingModelsFor(connection.adapter)- : [];- return Array.from(- new Set(- [savedModel, ...embedding, ...connection.customModels, ...chat].filter(- (model): model is string => Boolean(model),- ),- ),- );-}--function judgeModel(- slot: SlotValue,- connections: Connection[],-): JudgeModel | null {- if (!slot) return null;- const connection = connections.find((item) => item.id === slot.llmApiKeyId);- return {- provider: connection?.provider ?? "Missing connection",- model: slot.model,- };-}--function connectionId(- model: JudgeModel,- connections: Connection[],- preferredId: string | undefined,- kind: "chat" | "embedding",- savedModel?: string | null,-) {- const matches = connections.filter(- (connection) =>- connection.provider === model.provider &&- modelsFor(- connection,- kind,- connection.id === preferredId ? savedModel : null,- ).includes(model.model),- );- return (- matches.find((connection) => connection.id === preferredId)?.id ??- matches[0]?.id ??- null- );-}--function slotValue(- model: JudgeModel | null,- connections: Connection[],- preferredId: string | undefined,- kind: "chat" | "embedding",- savedModel?: string | null,-) {- if (!model) return null;- const llmApiKeyId = connectionId(- model,- connections,- preferredId,- kind,- savedModel,- );- return llmApiKeyId ? { llmApiKeyId, model: model.model } : null;-}--function facetDrafts(- facets: TopicFacet[],- enabledIds: ReadonlySet<string> | null,-) {- const byName = new Map(facets.map((facet) => [facet.name, facet]));- const builtIn = DEFAULT_TOPIC_FACETS.map((preset) => {- const existing = byName.get(preset.name);- return {- id: existing?.id ?? preset.name,- name: preset.name,- question: existing?.versions[0]?.prompt ?? preset.prompt,- builtIn: true,- enabled: enabledIds- ? Boolean(existing && enabledIds.has(existing.id))- : true,- };- });- const custom = facets.flatMap((facet) => {- if (facet.isBuiltIn) return [];- const question = facet.versions[0]?.prompt;- if (!question) return [];- return [- {- id: facet.id,- name: facet.name,- question,- builtIn: false,- enabled: enabledIds?.has(facet.id) ?? false,+ automaticProcessing: {+ ...automaticProcessing,+ dialog: {+ rule,+ stored: settings.data,+ traceIdleMs: readinessSettings.data?.traceIdleMs ?? null,+ save: automaticProcessing.save,+ saving: automaticProcessing.saving,},- ];- });- return [...builtIn, ...custom];+ },+ notice: {+ unavailable: Boolean(settings.data && !settings.data.processingAvailable),+ status,+ } satisfies TopicSetupNoticeState,+ };}diff --git a/web/src/features/topics/TopicSetupNotice.tsx b/web/src/features/topics/TopicSetupNotice.tsxnew file mode 100644index 0000000000..4e0c2d8687--- /dev/null+++ b/web/src/features/topics/TopicSetupNotice.tsx@@ -0,0 +1,114 @@+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";+import { type TopicSetupNoticeState } from "./TopicModelSettings";++type TopicSetupNoticeProps = {+ notice: TopicSetupNoticeState;+ canWrite: boolean;+ canConfigure: boolean;+ onConfigure: () => void;+ onKeepOff: () => void;+ onTurnBackOn: () => void;+};++/** Setup, paused, and availability notices above the Topics workspace. */+export function TopicSetupNotice({+ notice,+ canWrite,+ canConfigure,+ onConfigure,+ onKeepOff,+ onTurnBackOn,+}: TopicSetupNoticeProps) {+ const { status } = notice;+ return (+ <>+ {notice.unavailable ? (+ <Alert variant="info">+ <Alert.Title>+ Topics processing is not enabled for this project+ </Alert.Title>+ <Alert.Description>+ <p>+ You can configure Topics, but processing traces and automatic+ processing stay unavailable until Topics is enabled for this+ project.+ </p>+ </Alert.Description>+ </Alert>+ ) : null}+ {status?.kind === "paused" ? (+ <Alert variant="warning">+ <Alert.Title>Automatic Topics processing is paused</Alert.Title>+ <Alert.Description>+ <p className="break-words">{status.message}</p>+ <div className="mt-2 flex flex-wrap gap-2">+ <Button+ text="Turn back on"+ variant="secondary"+ size="sm"+ disabled={!canWrite}+ onClick={onTurnBackOn}+ />+ <Button+ text="Keep off"+ variant="secondary"+ size="sm"+ disabled={!canWrite}+ onClick={onKeepOff}+ />+ </div>+ </Alert.Description>+ </Alert>+ ) : null}+ {status?.kind === "setup" ? (+ <div className="mx-auto w-full max-w-xl pt-12">+ <SplashScreen+ title="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",+ description:+ "Pick the models Topics runs on and the facets to analyze, such as intent, outcome, and issues.",+ },+ {+ title: "Analyze traces",+ description:+ "Topics summarizes new traces automatically, and past traces whenever you process them.",+ },+ {+ title: "Gather insights",+ description:+ "Similar summaries are clustered into topics, on demand or continuously, so patterns across your traces show up here.",+ },+ ]}+ primaryAction={{+ label: "Configure Topics",+ component: (+ <Button+ text="Configure Topics"+ disabled={!canWrite || !canConfigure}+ onClick={onConfigure}+ />+ ),+ }}+ />+ </div>+ ) : null}+ {status?.kind === "models" ? (+ <Alert variant="info">+ <Alert.Title>Choose the models Topics runs on</Alert.Title>+ <Alert.Description>+ <p>+ Topics uses your LLM connections for three jobs: facet summaries,+ embeddings, and topic clustering. Open Configure Topics to set all+ three.+ </p>+ </Alert.Description>+ </Alert>+ ) : null}+ </>+ );+}diff --git a/web/src/features/topics/TopicsPage.clienttest.tsx b/web/src/features/topics/TopicsPage.clienttest.tsxindex d4486cd42d..f86512247f 100644--- a/web/src/features/topics/TopicsPage.clienttest.tsx+++ b/web/src/features/topics/TopicsPage.clienttest.tsx@@ -71,7 +71,31 @@ vi.mock("./TopicPipelineForm", () => ({},}));vi.mock("./TopicModelSettings", () => ({- useTopicModelSettings: () => ({ action: null, notice: null }),+ useTopicModelSettings: () => ({+ configure: { settings: null, triggerVariant: "secondary" },+ automaticProcessing: {+ state: { status: "unconfigured" },+ clearPause: vi.fn(),+ saving: false,+ dialog: {},+ },+ notice: { unavailable: false, status: null },+ }),+}));+vi.mock("./TopicSetupNotice", () => ({ TopicSetupNotice: () => null }));+vi.mock("./ConfigureTopicsDialogController", () => ({+ ConfigureTopicsDialogController: ({+ children,+ }: {+ children: (control: { openDialog: () => void }) => ReactNode;+ }) => children({ openDialog: vi.fn() }),+}));+vi.mock("./AutomaticProcessingDialogController", () => ({+ AutomaticProcessingDialogController: ({+ children,+ }: {+ children: (control: { openDialog: () => void }) => ReactNode;+ }) => children({ openDialog: vi.fn() }),}));vi.mock("./CurrentTopics", () => ({useCurrentTopics: (input: {diff --git a/web/src/features/topics/TopicsPage.tsx b/web/src/features/topics/TopicsPage.tsxindex 7647c840e6..7662a8b75f 100644--- a/web/src/features/topics/TopicsPage.tsx+++ b/web/src/features/topics/TopicsPage.tsx@@ -40,6 +40,10 @@ import {} from "@langfuse/shared/topics";import { useTopicPipelineForm } from "./TopicPipelineForm";import { useTopicModelSettings } from "./TopicModelSettings";+import { AutomaticProcessingControl } from "./AutomaticProcessingControl";+import { AutomaticProcessingDialogController } from "./AutomaticProcessingDialogController";+import { ConfigureTopicsDialogController } from "./ConfigureTopicsDialogController";+import { TopicSetupNotice } from "./TopicSetupNotice";import { CurrentTopics, useCurrentTopics } from "./CurrentTopics";import { TopicsFilters } from "./TopicsFilters";import { TopicsActionsMenu } from "./TopicsActionsMenu";@@ -121,13 +125,63 @@ function TopicsWorkspace({ projectId }: { projectId: string }) {);}-function TopicsWorkspaceView({- projectId,- facets,-}: {+type TopicsWorkspaceViewProps = {projectId: string;facets: UseQueryResult<TopicFacet[], { message: string }>;-}) {+};++/**+ * Wraps the workspace in the dialog controllers whose dialogs open from more+ * than one place, and hands their `openDialog` to the content.+ */+function TopicsWorkspaceView({ projectId, facets }: TopicsWorkspaceViewProps) {+ const canWrite = useHasProjectAccess({ projectId, scope: "topics:CUD" });+ const modelSettings = useTopicModelSettings({+ projectId,+ canWrite,+ facets: facets.data ?? [],+ facetsReady: facets.isSuccess,+ });+ return (+ <ConfigureTopicsDialogController+ projectId={projectId}+ settings={modelSettings.configure.settings}+ >+ {({ openDialog: openConfigure }) => (+ <AutomaticProcessingDialogController+ {...modelSettings.automaticProcessing.dialog}+ >+ {({ openDialog: openAutomaticProcessing }) => (+ <TopicsWorkspaceContent+ projectId={projectId}+ facets={facets}+ canWrite={canWrite}+ modelSettings={modelSettings}+ openConfigure={openConfigure}+ openAutomaticProcessing={openAutomaticProcessing}+ />+ )}+ </AutomaticProcessingDialogController>+ )}+ </ConfigureTopicsDialogController>+ );+}++type TopicsWorkspaceContentProps = TopicsWorkspaceViewProps & {+ canWrite: boolean;+ modelSettings: ReturnType<typeof useTopicModelSettings>;+ openConfigure: () => void;+ openAutomaticProcessing: (initialEnabled: boolean) => void;+};++function TopicsWorkspaceContent({+ projectId,+ facets,+ canWrite,+ modelSettings,+ openConfigure,+ openAutomaticProcessing,+}: TopicsWorkspaceContentProps) {const [historyOpen, setHistoryOpen] = useState(false);const { timeRange, setTimeRange } = useGlobalDateRange({allowedRanges: sharedTimeRangePresets,@@ -143,7 +197,6 @@ function TopicsWorkspaceView({});const router = useRouter();const utils = api.useUtils();- const canWrite = useHasProjectAccess({ projectId, scope: "topics:CUD" });const executions = api.topics.executions.useQuery({ projectId },{@@ -253,16 +306,27 @@ function TopicsWorkspaceView({onTriggered: openExecution,timeRange: validTimeRange ? currentTimeRange : null,});- const modelSettings = useTopicModelSettings({- projectId,- canWrite,- facets: facets.data ?? [],- facetsReady: facets.isSuccess,- });+ const automaticProcessing = modelSettings.automaticProcessing;+ function openAutomaticProcessingFromHeader() {+ openAutomaticProcessing(automaticProcessing.state.status === "on");+ }+ function turnAutomaticProcessingBackOn() {+ openAutomaticProcessing(true);+ }const actions = (<div className="ph-no-capture flex items-center justify-end gap-1">- {modelSettings.automaticProcessing}- {modelSettings.action}+ <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}<DropdownMenuariaLabel="Topics actions"@@ -334,7 +398,14 @@ function TopicsWorkspaceView({{!validTimeRange && (<ErrorMessage message="Select a time range of at most 90 days." />)}- {modelSettings.notice}+ <TopicSetupNotice+ notice={modelSettings.notice}+ canWrite={canWrite}+ canConfigure={modelSettings.configure.settings !== null}+ onConfigure={openConfigure}+ onKeepOff={automaticProcessing.clearPause}+ onTurnBackOn={turnAutomaticProcessingBackOn}+ />{configuration}<Sheetopen={historyOpen || executionId !== null}diff --git a/web/src/features/topics/useAutomaticProcessing.tsx b/web/src/features/topics/useAutomaticProcessing.tsxindex f7c21548e6..26ece6faad 100644--- a/web/src/features/topics/useAutomaticProcessing.tsx+++ b/web/src/features/topics/useAutomaticProcessing.tsx@@ -1,108 +1,45 @@-import { useState } from "react";-import * as DialogPrimitive from "@radix-ui/react-dialog";import { getEvaluatorBlockMetadata } from "@langfuse/shared";import { type TopicRule } from "@langfuse/shared/topics";import { api, type RouterOutputs } from "@/src/utils/api";-import {- AutomaticProcessingControl,- type AutomaticProcessingState,-} from "./AutomaticProcessingControl";-import { AutomaticProcessingDialog } from "./AutomaticProcessingDialog";+import { type AutomaticProcessingState } from "./AutomaticProcessingControl";type StoredSettings = RouterOutputs["topics"]["modelSettings"];/**- * The header control and dialog for automatic processing. `openEnable` lets- * the paused notice open the same dialog.+ * Automatic processing status and its single write. The paused notice's+ * "Keep off" and the dialog's Save share it, so writes never overlap.*/export function useAutomaticProcessing({- canWrite,projectId,rule,stored,- traceIdleMs,}: {- canWrite: boolean;projectId: string;rule: TopicRule | null;stored: StoredSettings | undefined;- traceIdleMs: number | null;}) {+ const mutation = useSetAutomaticProcessing(projectId);+ return {+ state: automaticProcessingState(stored, rule),+ clearPause: () => mutation.mutate({ projectId, enabled: false }),+ save: (input: { enabled: boolean; sampling: number }) =>+ mutation.mutateAsync({ projectId, ...input }),+ saving: mutation.isPending,+ };+}++function useSetAutomaticProcessing(projectId: string) {const utils = api.useUtils();- // The switch's starting position while the dialog is open.- const [dialog, setDialog] = useState<{ enabled: boolean } | null>(null);- const [error, setError] = useState<string | null>(null);- const mutation = api.topics.setAutomaticProcessing.useMutation({+ return api.topics.setAutomaticProcessing.useMutation({onSuccess: () =>Promise.all([utils.topics.modelSettings.invalidate({ projectId }),utils.topics.rules.invalidate({ projectId }),]),});- const state = automaticProcessingState(stored, rule);- const paused =- stored?.blockMessage && state.status === "paused"- ? { shortLabel: state.shortLabel, message: stored.blockMessage }- : null;- const open = (enabled: boolean) => {- setError(null);- setDialog({ enabled });- };- const save = (input: { enabled: boolean; sampling: number }) => {- setError(null);- mutation- .mutateAsync({ projectId, ...input })- .then(() => setDialog(null))- .catch((cause: unknown) =>- setError(- cause instanceof Error- ? cause.message- : "Could not save automatic processing.",- ),- );- };-- const control = (- <>- <AutomaticProcessingControl- state={state}- canWrite={canWrite && !mutation.isPending}- onOpen={() => open(state.status === "on")}- />- <DialogPrimitive.Root- open={dialog !== null}- onOpenChange={(next) => {- if (!next) setDialog(null);- }}- >- {dialog ? (- <AutomaticProcessingDialog- initialEnabled={dialog.enabled}- initialSampling={rule?.sampling ?? 1}- traceIdleMs={traceIdleMs ?? 600_000}- models={{- summary: stored?.summary?.model ?? null,- embedding: stored?.embedding?.model ?? null,- clustering: stored?.clustering?.model ?? null,- }}- paused={paused}- saving={mutation.isPending}- error={error}- onSave={save}- />- ) : null}- </DialogPrimitive.Root>- </>- );-- return {- control,- openEnable: () => open(true),- clearPause: () => mutation.mutate({ projectId, enabled: false }),- };}-function automaticProcessingState(+export function automaticProcessingState(stored: StoredSettings | undefined,rule: TopicRule | null,): AutomaticProcessingState {