Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

[Metabot] Reasoning UI (#78417)

Sloan Sparger776e1a0c61f5
diff --git a/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotChatHistory.tsx b/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotChatHistory.tsx
index c1f9f2b59e8..afcbb72148a 100644
--- a/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotChatHistory.tsx
+++ b/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotChatHistory.tsx
@@ -14,6 +14,9 @@ const isQuestionNavigationMessage = (message: MetabotChatMessage) =>
message.part.type === "data-generated_entity" &&
message.part.data.type === "card";
+const isHiddenInEmbedding = (message: MetabotChatMessage) =>
+ message.type === "chain_of_thought";
+
const AGENT_ID = "omnibot";
export function MetabotChatHistory() {
@@ -23,7 +26,12 @@ export function MetabotChatHistory() {
const scrollContainerRef = useRef<HTMLDivElement>(null);
const chatMessages = useMemo(
- () => messages.filter((message) => !isQuestionNavigationMessage(message)),
+ () =>
+ messages.filter(
+ (message) =>
+ !isQuestionNavigationMessage(message) &&
+ !isHiddenInEmbedding(message),
+ ),
[messages],
);
diff --git a/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotChatHistory.unit.spec.tsx b/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotChatHistory.unit.spec.tsx
index f871910b182..a93eea7b6a8 100644
--- a/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotChatHistory.unit.spec.tsx
+++ b/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotChatHistory.unit.spec.tsx
@@ -53,6 +53,24 @@ describe("MetabotChatHistory", () => {
).not.toBeInTheDocument();
});
+ it("should not render chain_of_thought messages (app-only reasoning UI)", () => {
+ setup({
+ ui: <MetabotChatHistory />,
+ metabotInitialState: makeVisibleState([
+ {
+ id: "1",
+ role: "agent",
+ type: "chain_of_thought",
+ steps: [{ kind: "reasoning", text: "Let me think about this" }],
+ },
+ ]),
+ });
+
+ expect(
+ screen.queryByTestId("metabot-chain-of-thought"),
+ ).not.toBeInTheDocument();
+ });
+
it("should render non-chart messages normally", async () => {
setup({
ui: <MetabotChatHistory />,
diff --git a/frontend/src/metabase/api/ai-streaming/process-stream.ts b/frontend/src/metabase/api/ai-streaming/process-stream.ts
index 76d6236a81b..de4cc3bfb8d 100644
--- a/frontend/src/metabase/api/ai-streaming/process-stream.ts
+++ b/frontend/src/metabase/api/ai-streaming/process-stream.ts
@@ -10,11 +10,14 @@ import {
toolOutputAvailableSchema,
toolOutputErrorSchema,
} from "./schemas";
-import { smoothTextEvents } from "./smooth-stream";
+import { smoothStreamEvents } from "./smooth-stream";
import { parseSSEStream } from "./sse-stream";
import type {
FinishReason,
MessageMetadata,
+ ReasoningDeltaEvent,
+ ReasoningEndEvent,
+ ReasoningStartEvent,
StartEvent,
ToolInputAvailableEvent,
ToolInputStartEvent,
@@ -39,6 +42,9 @@ export type StreamedError = { errorText: string };
export type AIStreamingConfig = {
onStart?: (event: StartEvent) => void;
onTextPart?: (delta: string) => void;
+ onReasoningStart?: (event: ReasoningStartEvent) => void;
+ onReasoningDelta?: (event: ReasoningDeltaEvent) => void;
+ onReasoningEnd?: (event: ReasoningEndEvent) => void;
// callback is only called if this version of the client is aware of the received data part type
onDataPart?: (part: KnownDataPart) => void;
onToolInputStart?: (event: ToolInputStartEvent) => void;
@@ -85,13 +91,18 @@ export async function processChatResponse(
};
const events = parseSSEStream(stream);
- const paced = isReducedMotionPreferred() ? events : smoothTextEvents(events);
+ const paced = isReducedMotionPreferred()
+ ? events
+ : smoothStreamEvents(events);
try {
for await (const event of paced) {
match(event)
.with({ type: "start" }, (e) => config.onStart?.(e))
.with({ type: "text-delta" }, (e) => config.onTextPart?.(e.delta))
+ .with({ type: "reasoning-start" }, (e) => config.onReasoningStart?.(e))
+ .with({ type: "reasoning-delta" }, (e) => config.onReasoningDelta?.(e))
+ .with({ type: "reasoning-end" }, (e) => config.onReasoningEnd?.(e))
.with({ type: "tool-input-start" }, (e) => config.onToolInputStart?.(e))
.with({ type: "tool-input-available" }, (e) => {
toolInputAvailableSchema.validateSync(e, { strict: true });
@@ -157,7 +168,6 @@ export async function processChatResponse(
// text: text-start, text-end
// tool: tool-input-delta, tool-input-error, tool-approval-request,
// tool-output-denied
- // reasoning: reasoning-start, reasoning-delta, reasoning-end
// sources: source-url, source-document
// files: file
});
diff --git a/frontend/src/metabase/api/ai-streaming/process-stream.unit.spec.ts b/frontend/src/metabase/api/ai-streaming/process-stream.unit.spec.ts
index 3bf1c9a993f..0b0beeb6721 100644
--- a/frontend/src/metabase/api/ai-streaming/process-stream.unit.spec.ts
+++ b/frontend/src/metabase/api/ai-streaming/process-stream.unit.spec.ts
@@ -6,6 +6,9 @@ import { createMockReadableStream, createMockSSEStream } from "./test-utils";
const getMockedCallbacks = () => ({
onStart: jest.fn(),
onTextPart: jest.fn(),
+ onReasoningStart: jest.fn(),
+ onReasoningDelta: jest.fn(),
+ onReasoningEnd: jest.fn(),
onDataPart: jest.fn(),
onToolInputStart: jest.fn(),
onToolInputAvailable: jest.fn(),
@@ -98,12 +101,30 @@ describe("processChatResponse", () => {
]);
});
- it("should ignore unhandled event types", async () => {
+ it("should fire reasoning callbacks and not append reasoning to result", async () => {
const mockStream = createMockSSEStream([
- // Unjustified type cast. FIXME
- { type: "reasoning-start", id: "r1" } as unknown as SSEEvent,
+ { type: "reasoning-start", id: "r1" },
+ { type: "reasoning-delta", id: "r1", delta: "Think" },
+ { type: "reasoning-delta", id: "r1", delta: "ing" },
+ { type: "reasoning-end", id: "r1" },
]);
const config = getMockedCallbacks();
+
+ const result = await processChatResponse(mockStream, config);
+ expect(config.onReasoningStart).toHaveBeenCalledTimes(1);
+ expect(config.onReasoningDelta).toHaveBeenCalledTimes(1);
+ expect(config.onReasoningDelta).toHaveBeenCalledWith(
+ expect.objectContaining({ delta: "Thinking" }),
+ );
+ expect(config.onReasoningEnd).toHaveBeenCalledTimes(1);
+ expect(config.onError).not.toHaveBeenCalled();
+ expect(result.data).toEqual([]);
+ expect(result.toolCalls).toEqual([]);
+ });
+
+ it("should ignore unhandled event types", async () => {
+ const mockStream = createMockSSEStream([{ type: "start-step" }]);
+ const config = getMockedCallbacks();
await expect(processChatResponse(mockStream, config)).resolves.toBeTruthy();
expect(config.onError).not.toHaveBeenCalled();
});
@@ -146,6 +167,27 @@ describe("processChatResponse", () => {
]);
});
+ it("forwards a streamed title on tool-input-available", async () => {
+ const config = getMockedCallbacks();
+ await processChatResponse(
+ createMockSSEStream([
+ {
+ type: "tool-input-available",
+ toolCallId: "x",
+ toolName: "read_resource",
+ input: {},
+ title: "Inspecting [Orders](metabase://dashboard/5)",
+ },
+ ]),
+ config,
+ );
+ expect(config.onToolInputAvailable).toHaveBeenCalledWith(
+ expect.objectContaining({
+ title: "Inspecting [Orders](metabase://dashboard/5)",
+ }),
+ );
+ });
+
it("should call onToolInputStart for tool-input-start events", async () => {
const config = getMockedCallbacks();
await processChatResponse(
diff --git a/frontend/src/metabase/api/ai-streaming/schemas.ts b/frontend/src/metabase/api/ai-streaming/schemas.ts
index a6f3867607e..0fbf0d3f9e6 100644
--- a/frontend/src/metabase/api/ai-streaming/schemas.ts
+++ b/frontend/src/metabase/api/ai-streaming/schemas.ts
@@ -12,6 +12,7 @@ export const toolInputAvailableSchema = Yup.object({
toolCallId: Yup.string().required(),
toolName: Yup.string().required(),
input: Yup.mixed().defined(),
+ title: Yup.string(),
});
export const toolOutputAvailableSchema = Yup.object({
@@ -43,9 +44,30 @@ export const knownDataPartTypes = [
"data-entity_saved",
"data-adhoc_viz",
"data-static_viz",
+ "data-search_results",
+ "data-tool_title",
"data-conversation-title",
] as const satisfies readonly KnownDataPart["type"][];
+export type SearchResultItem = {
+ id: number;
+ type: string;
+ name: string;
+ display_name?: string;
+ display?: CardDisplayType;
+ moderated_status?: string;
+ database_id?: number;
+ database_schema?: string;
+ database_name?: string;
+ collection?: { id?: number; name: string };
+};
+
+export type SearchResultsData = {
+ tool_call_id: string;
+ total_count: number;
+ results: SearchResultItem[];
+};
+
export type AdhocVizValue = {
query: unknown;
link: string;
@@ -89,6 +111,13 @@ export type EntitySavedValue = {
chart_id: string;
card_id: number;
destination: SavedEntityDestination;
+ tool_call_id?: string;
+ title?: string;
+};
+
+export type ToolTitleData = {
+ tool_call_id: string;
+ title: string;
};
export type KnownDataPart =
@@ -101,6 +130,8 @@ export type KnownDataPart =
| { type: "data-entity_saved"; data: EntitySavedValue }
| { type: "data-adhoc_viz"; data: AdhocVizValue }
| { type: "data-static_viz"; data: StaticVizValue }
+ | { type: "data-search_results"; data: SearchResultsData }
+ | { type: "data-tool_title"; data: ToolTitleData }
| { type: "data-conversation-title"; data: string };
export const isKnownDataPart = (part: {
diff --git a/frontend/src/metabase/api/ai-streaming/smooth-stream.ts b/frontend/src/metabase/api/ai-streaming/smooth-stream.ts
index 3a50823c888..32d4adcf156 100644
--- a/frontend/src/metabase/api/ai-streaming/smooth-stream.ts
+++ b/frontend/src/metabase/api/ai-streaming/smooth-stream.ts
@@ -1,6 +1,10 @@
-import type { SSEEvent, TextDeltaEvent } from "./sse-types";
+import type {
+ ReasoningDeltaEvent,
+ SSEEvent,
+ TextDeltaEvent,
+} from "./sse-types";
-// Whitespace-terminated, so a word only emits once it's complete.
+// whitespace-terminated, so a word only emits once it's complete
const WORD_PATTERN = /\S+\s+/m;
const DEFAULT_SMOOTHING_DELAY_MS = 10;
@@ -13,18 +17,22 @@ const takeWord = (buffer: string) => {
return match ? buffer.slice(0, match.index + match[0].length) : null;
};
+type SmoothableDelta = TextDeltaEvent | ReasoningDeltaEvent;
+
+const isSmoothable = (event: SSEEvent): event is SmoothableDelta =>
+ event.type === "text-delta" || event.type === "reasoning-delta";
+
export type SmoothTextOptions = { delayInMs?: number };
-// Re-emits text deltas one word at a time on a fixed cadence, so text renders at
-// a steady pace however unevenly the server chunks it. Awaiting between words
-// backpressures the stream, which is what paces it. Non-text events flush the
-// buffered tail first to preserve ordering.
-export async function* smoothTextEvents(
+// Re-emits text/reasoning deltas one word at a time on a fixed cadence, so they
+// render steadily however unevenly the server chunks them. Awaiting between words
+// backpressures the stream, which is what paces it.
+export async function* smoothStreamEvents(
events: AsyncIterable<SSEEvent>,
{ delayInMs = DEFAULT_SMOOTHING_DELAY_MS }: SmoothTextOptions = {},
): AsyncGenerator<SSEEvent> {
let buffer = "";
- let pending: TextDeltaEvent | null = null;
+ let pending: SmoothableDelta | null = null;
function* flush() {
if (pending && buffer.length > 0) {
@@ -34,14 +42,15 @@ export async function* smoothTextEvents(
}
for await (const event of events) {
- if (event.type !== "text-delta") {
+ // any other event flushes the buffered tail first, to preserve ordering
+ if (!isSmoothable(event)) {
yield* flush();
yield event;
continue;
}
- // A different text block can't share this buffer.
- if (pending && pending.id !== event.id) {
+ // a different block — or switching between text and reasoning — can't share this buffer
+ if (pending && (pending.type !== event.type || pending.id !== event.id)) {
yield* flush();
}
diff --git a/frontend/src/metabase/api/ai-streaming/smooth-stream.unit.spec.ts b/frontend/src/metabase/api/ai-streaming/smooth-stream.unit.spec.ts
index ed9a20ca031..aaec038f964 100644
--- a/frontend/src/metabase/api/ai-streaming/smooth-stream.unit.spec.ts
+++ b/frontend/src/metabase/api/ai-streaming/smooth-stream.unit.spec.ts
@@ -1,4 +1,4 @@
-import { smoothTextEvents } from "./smooth-stream";
+import { smoothStreamEvents } from "./smooth-stream";
import type { SSEEvent } from "./sse-types";
async function* asAsyncIterable(events: SSEEvent[]) {
@@ -9,7 +9,7 @@ async function* asAsyncIterable(events: SSEEvent[]) {
const smooth = async (events: SSEEvent[]) => {
const out: SSEEvent[] = [];
- for await (const event of smoothTextEvents(asAsyncIterable(events), {
+ for await (const event of smoothStreamEvents(asAsyncIterable(events), {
delayInMs: 0,
})) {
out.push(event);
@@ -17,10 +17,14 @@ const smooth = async (events: SSEEvent[]) => {
return out;
};
-const textDeltas = (events: SSEEvent[]) =>
- events.flatMap((event) => (event.type === "text-delta" ? [event.delta] : []));
+const deltasOfType = (events: SSEEvent[], type: string) =>
+ events.flatMap((event) =>
+ event.type === type && "delta" in event ? [event.delta] : [],
+ );
-describe("smoothTextEvents", () => {
+const textDeltas = (events: SSEEvent[]) => deltasOfType(events, "text-delta");
+
+describe("smoothStreamEvents", () => {
it("emits buffered text one word at a time", async () => {
const out = await smooth([
{ type: "text-start", id: "t1" },
@@ -55,4 +59,30 @@ describe("smoothTextEvents", () => {
{ type: "text-end", id: "t2" },
]);
});
+
+ it("word-paces reasoning deltas too, preserving their type and id", async () => {
+ const out = await smooth([
+ { type: "reasoning-start", id: "r1" },
+ { type: "reasoning-delta", id: "r1", delta: "thinking it over" },
+ { type: "reasoning-end", id: "r1" },
+ ]);
+
+ expect(deltasOfType(out, "reasoning-delta")).toEqual([
+ "thinking ",
+ "it ",
+ "over",
+ ]);
+ expect(out.every((e) => e.type !== "text-delta")).toBe(true);
+ });
+
+ it("flushes the reasoning buffer before switching to text", async () => {
+ const out = await smooth([
+ { type: "reasoning-delta", id: "r1", delta: "reasoned" },
+ { type: "text-delta", id: "t1", delta: "answer " },
+ { type: "text-end", id: "t1" },
+ ]);
+
+ expect(deltasOfType(out, "reasoning-delta")).toEqual(["reasoned"]);
+ expect(textDeltas(out)).toEqual(["answer "]);
+ });
});
diff --git a/frontend/src/metabase/api/ai-streaming/sse-types.ts b/frontend/src/metabase/api/ai-streaming/sse-types.ts
index c4b3575663f..055f82b5fbb 100644
--- a/frontend/src/metabase/api/ai-streaming/sse-types.ts
+++ b/frontend/src/metabase/api/ai-streaming/sse-types.ts
@@ -67,6 +67,24 @@ export type TextEndEvent = {
providerMetadata?: ProviderMetadata;
};
+// reasoning events
+export type ReasoningStartEvent = {
+ type: "reasoning-start";
+ id: string;
+ providerMetadata?: ProviderMetadata;
+};
+export type ReasoningDeltaEvent = {
+ type: "reasoning-delta";
+ id: string;
+ delta: string;
+ providerMetadata?: ProviderMetadata;
+};
+export type ReasoningEndEvent = {
+ type: "reasoning-end";
+ id: string;
+ providerMetadata?: ProviderMetadata;
+};
+
// tool events
export type ToolInputStartEvent = {
type: "tool-input-start";
@@ -130,6 +148,9 @@ export type SSEEvent =
| TextStartEvent
| TextDeltaEvent
| TextEndEvent
+ | ReasoningStartEvent
+ | ReasoningDeltaEvent
+ | ReasoningEndEvent
| ToolInputStartEvent
| ToolInputDeltaEvent
| ToolInputAvailableEvent
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/MetabotChainOfThought.module.css b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/MetabotChainOfThought.module.css
new file mode 100644
index 00000000000..7c158e0eaa8
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/MetabotChainOfThought.module.css
@@ -0,0 +1,229 @@
+.root {
+ width: 100%;
+}
+
+.trigger {
+ display: flex;
+ align-items: center;
+ gap: 0.375rem;
+ width: 100%;
+ color: var(--mb-color-text-tertiary);
+}
+
+.headerLabel {
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ min-width: 0;
+ font-size: 0.875rem;
+}
+
+.chevron {
+ flex-shrink: 0;
+ margin-left: 0.125rem;
+ transition: transform 150ms ease;
+}
+
+.chevronOpen {
+ transform: rotate(90deg);
+}
+
+/* background-clip:text can't reach an SVG, so the caret fakes the shimmer with a
+ color with %'s timed to shimmer animation */
+.chevronShimmer {
+ animation: chevron-pulse 1500ms linear infinite;
+}
+
+@keyframes chevron-pulse {
+ 0%,
+ 47% {
+ color: var(--mb-color-text-tertiary);
+ }
+
+ 57% {
+ color: var(--mb-color-border-neutral);
+ }
+
+ 100% {
+ color: var(--mb-color-text-tertiary);
+ }
+}
+
+@media (prefers-reduced-motion: reduce) {
+ .chevronShimmer {
+ animation: none;
+ }
+}
+
+.shimmer {
+ /* both colors stay in the text family (a lighter highlight over the resting
+ tone) so the sweep reads as a sheen rather than a spotlight */
+ --base-color: var(--mb-color-text-tertiary);
+ --highlight-color: var(--mb-color-border-neutral);
+
+ color: transparent !important;
+ background-clip: text;
+ -webkit-background-clip: text;
+ background-size: 200% 100%;
+ background-image: linear-gradient(
+ 135deg,
+ var(--base-color) 15%,
+ var(--highlight-color) 50%,
+ var(--base-color) 85%
+ );
+ background-position: 110% 0;
+ animation: rotate-background 1500ms linear infinite;
+}
+
+/* 200% of travel = one full tile, so the loop is pixel-identical at the seam */
+@keyframes rotate-background {
+ 0% {
+ background-position: 110% 0;
+ }
+
+ 100% {
+ background-position: -90% 0;
+ }
+}
+
+@media (prefers-reduced-motion: reduce) {
+ .shimmer {
+ animation: none;
+ color: var(--base-color) !important;
+ background-image: none;
+ }
+}
+
+.step {
+ display: flex;
+ flex-direction: column;
+ color: var(--mb-color-text-secondary);
+ font-size: 0.875rem;
+ line-height: 1.57;
+}
+
+.step strong,
+.step b {
+ font-weight: inherit;
+}
+
+.toolStep {
+ display: flex;
+ flex-direction: column;
+}
+
+.toolRow {
+ display: flex;
+ align-items: center;
+ gap: 0.5rem;
+ width: 100%;
+ line-height: 1.57;
+ color: var(--mb-color-text-tertiary);
+}
+
+.toolRowStatic {
+ cursor: default;
+}
+
+.toolRow a,
+.headerLabel a {
+ color: var(--mb-color-text-tertiary);
+ font-weight: 500;
+ text-decoration: none;
+}
+
+.toolRow a span,
+.headerLabel a span {
+ border-bottom-color: var(--mb-color-border-subtle);
+}
+
+.toolRow a svg,
+.headerLabel a svg {
+ width: 0.8rem;
+ height: 0.8rem;
+}
+
+.toolRow a:hover,
+.headerLabel a:hover {
+ color: var(--mb-color-brand);
+ text-decoration: none;
+}
+
+.toolRow a:hover span,
+.headerLabel a:hover span {
+ border-bottom-color: var(--mb-color-brand);
+}
+
+/* carets stay out of the way until the step is hovered/focused */
+.toolRow .chevron {
+ opacity: 0;
+ transition:
+ opacity 150ms ease,
+ transform 150ms ease;
+}
+
+.toolStep:hover .chevron,
+.toolRow:focus-visible .chevron {
+ opacity: 1;
+}
+
+.reasoningBody {
+ margin-top: 0.25rem;
+ color: var(--mb-color-text-secondary);
+}
+
+.resultCount {
+ margin-left: 0.375rem;
+ font-variant-numeric: tabular-nums;
+ opacity: 0.6;
+}
+
+.resultsList {
+ display: flex;
+ flex-direction: column;
+ gap: 0.1875rem;
+ margin-top: 0.375rem;
+ max-height: 7rem;
+ overflow-y: auto;
+}
+
+.resultRow {
+ display: flex;
+ align-items: center;
+ gap: 0.375rem;
+ min-width: 0;
+ color: var(--mb-color-text-secondary);
+ text-decoration: none;
+}
+
+.resultRow:hover {
+ color: var(--mb-color-brand);
+}
+
+.resultIcon {
+ flex-shrink: 0;
+ color: inherit;
+}
+
+.resultName {
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ min-width: 0;
+ font-size: 0.8125rem;
+}
+
+.resultContext {
+ flex-shrink: 0;
+ max-width: 45%;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ font-size: 0.75rem;
+ color: var(--mb-color-text-tertiary);
+}
+
+.contextSeparator {
+ margin: 0 0.25rem;
+ color: var(--mb-color-text-tertiary);
+}
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/MetabotChainOfThought.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/MetabotChainOfThought.tsx
new file mode 100644
index 00000000000..a494cd82b9c
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/MetabotChainOfThought.tsx
@@ -0,0 +1,152 @@
+import cx from "classnames";
+import { match } from "ts-pattern";
+import { t } from "ttag";
+
+import Animation from "metabase/css/core/animation.module.css";
+import { AIMarkdown } from "metabase/metabot/components/AIMarkdown";
+import type { MetabotAgentChainOfThoughtMessage } from "metabase/metabot/state";
+import { Collapse, Icon, Stack, Text, UnstyledButton } from "metabase/ui";
+
+import S from "./MetabotChainOfThought.module.css";
+import { ReasoningStep } from "./ReasoningStep";
+import { ResourceGroupStep, ToolStep } from "./ToolStep";
+import { useAutoCollapseOnSettle, useMeteredLabel, useNow } from "./hooks";
+import {
+ buildDisplayItems,
+ isRenderableStep,
+ isToolStep,
+ latestPreviewLabel,
+ reasoningLabel,
+ settledHeader,
+ soleReasoningStep,
+} from "./utils";
+
+export const MetabotChainOfThought = ({
+ message,
+ isStreaming,
+}: {
+ message: MetabotAgentChainOfThoughtMessage;
+ isStreaming: boolean;
+}) => {
+ const { open, toggle } = useAutoCollapseOnSettle(isStreaming);
+ const now = useNow(isStreaming);
+ const preview = useMeteredLabel(latestPreviewLabel(message.steps));
+
+ const renderableItems = buildDisplayItems(message.steps);
+
+ if (!isStreaming && renderableItems.length === 0) {
+ return null;
+ }
+
+ const durationMs =
+ message.startedAtMs != null && message.endedAtMs != null
+ ? message.endedAtMs - message.startedAtMs
+ : undefined;
+
+ const thinkingOnly = !message.steps.some(
+ (step) => isToolStep(step) && isRenderableStep(step),
+ );
+
+ const activeIndex = message.steps.reduce(
+ (acc, step, i) => (isRenderableStep(step) ? i : acc),
+ -1,
+ );
+ const isActive = (index: number) => isStreaming && index === activeIndex;
+
+ const headerContent = isStreaming
+ ? preview
+ : settledHeader(durationMs, thinkingOnly);
+
+ const stepReasoningLabel = (index: number): string => {
+ const start = message.steps[index].startedAtMs;
+ const end =
+ message.steps[index + 1]?.startedAtMs ??
+ message.endedAtMs ??
+ (isStreaming ? now : undefined);
+ return reasoningLabel(
+ start != null && end != null ? end - start : undefined,
+ );
+ };
+
+ const soleReasoning = isStreaming ? null : soleReasoningStep(renderableItems);
+
+ return (
+ <div className={S.root} data-testid="metabot-chain-of-thought">
+ <UnstyledButton
+ className={S.trigger}
+ aria-expanded={open}
+ onClick={toggle}
+ data-testid="metabot-chain-of-thought-header"
+ >
+ <Text
+ component="span"
+ className={cx(S.headerLabel, isStreaming && S.shimmer)}
+ c="currentColor"
+ >
+ {headerContent}
+ </Text>
+ <Icon
+ name="chevronright"
+ size={10}
+ className={cx(
+ S.chevron,
+ Animation.fadeIn,
+ isStreaming && S.chevronShimmer,
+ open && S.chevronOpen,
+ )}
+ />
+ </UnstyledButton>
+ <Collapse in={open}>
+ <Stack gap="0.5rem" mt="0.5rem">
+ {soleReasoning ? (
+ <div className={S.step}>
+ <div className={cx(S.reasoningBody, Animation.fadeIn)}>
+ <AIMarkdown>{soleReasoning.text}</AIMarkdown>
+ </div>
+ </div>
+ ) : (
+ renderableItems.map((item) =>
+ match(item)
+ .with({ kind: "resourceGroup" }, ({ steps, index }) => {
+ const active =
+ isStreaming &&
+ activeIndex >= index &&
+ activeIndex < index + steps.length;
+ return (
+ <div
+ key={`resource-${index}`}
+ className={cx(S.step, Animation.fadeIn)}
+ >
+ <ResourceGroupStep count={steps.length} done={!active} />
+ </div>
+ );
+ })
+ .with({ kind: "tool" }, ({ step, index }) => (
+ <div key={step.id} className={cx(S.step, Animation.fadeIn)}>
+ <ToolStep
+ step={step}
+ done={!isActive(index)}
+ animate={isStreaming}
+ />
+ </div>
+ ))
+ .with({ kind: "reasoning" }, ({ step, index }) => {
+ const active = isActive(index);
+ return (
+ <div key={`reasoning-${index}`} className={S.step}>
+ <ReasoningStep
+ text={step.text}
+ label={active ? t`Thinking` : stepReasoningLabel(index)}
+ active={active}
+ />
+ </div>
+ );
+ })
+ .exhaustive(),
+ )
+ )}
+ </Stack>
+ </Collapse>
+ </div>
+ );
+};
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/MetabotChainOfThought.unit.spec.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/MetabotChainOfThought.unit.spec.tsx
new file mode 100644
index 00000000000..832fb7df8cd
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/MetabotChainOfThought.unit.spec.tsx
@@ -0,0 +1,493 @@
+import userEvent from "@testing-library/user-event";
+import fetchMock from "fetch-mock";
+
+import { setupEnterprisePlugins } from "__support__/enterprise";
+import { mockSettings } from "__support__/settings";
+import { renderWithProviders, screen } from "__support__/ui";
+import type { MetabotAgentChainOfThoughtMessage } from "metabase/metabot/state";
+import { createMockState } from "metabase/redux/store/mocks";
+import { createMockDashboard } from "metabase-types/api/mocks";
+
+import { MetabotChainOfThought } from "./MetabotChainOfThought";
+
+const chain = (
+ overrides: Partial<MetabotAgentChainOfThoughtMessage>,
+): MetabotAgentChainOfThoughtMessage => ({
+ id: "chain-1",
+ role: "agent",
+ type: "chain_of_thought",
+ steps: [],
+ ...overrides,
+});
+
+const setup = (
+ message: MetabotAgentChainOfThoughtMessage,
+ isStreaming: boolean,
+) => {
+ setupEnterprisePlugins();
+ const settings = mockSettings({ "site-url": "http://localhost:3000" });
+ fetchMock.get(
+ "path:/api/dashboard/123",
+ createMockDashboard({ id: 123, name: "Orders" }),
+ );
+ return renderWithProviders(
+ <MetabotChainOfThought message={message} isStreaming={isStreaming} />,
+ { storeInitialState: createMockState({ settings }) },
+ );
+};
+
+const expandChain = async () => {
+ await userEvent.click(screen.getByTestId("metabot-chain-of-thought-header"));
+};
+
+describe("MetabotChainOfThought", () => {
+ it("shows Thinking for the empty shell while the turn is live", () => {
+ setup(chain({ steps: [] }), true);
+ expect(screen.getByText("Thinking")).toBeInTheDocument();
+ });
+
+ it("always offers the header toggle, even with no renderable steps", () => {
+ setup(chain({ steps: [{ kind: "reasoning", text: "" }] }), true);
+ expect(screen.getByText("Thinking")).toBeInTheDocument();
+ expect(screen.getByRole("button")).toBeInTheDocument();
+ });
+
+ it("renders nothing for a settled chain with no steps", () => {
+ setup(chain({ steps: [] }), false);
+ expect(
+ screen.queryByTestId("metabot-chain-of-thought"),
+ ).not.toBeInTheDocument();
+ });
+
+ it("renders nothing for a settled chain whose steps are all non-renderable", () => {
+ setup(
+ chain({
+ steps: [
+ { kind: "reasoning", text: "" },
+ { kind: "tool", id: "t1", name: "load_skill", status: "ended" },
+ ],
+ }),
+ false,
+ );
+ expect(
+ screen.queryByTestId("metabot-chain-of-thought"),
+ ).not.toBeInTheDocument();
+ });
+
+ it("never surfaces reasoning text in the header, only Thinking", () => {
+ setup(
+ chain({
+ steps: [
+ { kind: "reasoning", text: "Exploring the schema\nmore detail here" },
+ ],
+ }),
+ true,
+ );
+ const header = screen.getByRole("button", { name: /Thinking/ });
+ expect(header).toHaveTextContent("Thinking");
+ expect(header).not.toHaveTextContent("Exploring the schema");
+ });
+
+ it("shows the tool label as the headline when there is no reasoning", () => {
+ setup(
+ chain({
+ steps: [
+ { kind: "tool", id: "t1", name: "analyze_data", status: "started" },
+ ],
+ }),
+ true,
+ );
+ expect(screen.getAllByText("Analyzing the data").length).toBeGreaterThan(0);
+ });
+
+ it("shows Worked for N seconds once a tool-using turn settles past the threshold", () => {
+ setup(
+ chain({
+ steps: [
+ { kind: "tool", id: "t1", name: "analyze_data", status: "ended" },
+ ],
+ startedAtMs: 1000,
+ endedAtMs: 6000,
+ }),
+ false,
+ );
+ expect(screen.getByText("Worked for 5 seconds")).toBeInTheDocument();
+ });
+
+ it("rolls a sub-5s tool-using turn up to Worked briefly", () => {
+ setup(
+ chain({
+ steps: [
+ { kind: "tool", id: "t1", name: "analyze_data", status: "ended" },
+ ],
+ startedAtMs: 1000,
+ endedAtMs: 3400,
+ }),
+ false,
+ );
+ expect(screen.getByText("Worked briefly")).toBeInTheDocument();
+ expect(screen.queryByText(/Worked for/)).not.toBeInTheDocument();
+ });
+
+ it("swaps the verb to Thought for a thinking-only turn past the threshold", () => {
+ setup(
+ chain({
+ steps: [{ kind: "reasoning", text: "Weighing the join order" }],
+ startedAtMs: 1000,
+ endedAtMs: 9000,
+ }),
+ false,
+ );
+ expect(screen.getAllByText("Thought for 8 seconds").length).toBeGreaterThan(
+ 0,
+ );
+ expect(screen.queryByText(/Worked/)).not.toBeInTheDocument();
+ });
+
+ it("rolls a sub-5s thinking-only turn up to Thought briefly", () => {
+ setup(
+ chain({
+ steps: [{ kind: "reasoning", text: "quick" }],
+ startedAtMs: 1000,
+ endedAtMs: 2000,
+ }),
+ false,
+ );
+ expect(screen.getAllByText("Thought briefly").length).toBeGreaterThan(0);
+ expect(screen.queryByText(/Worked/)).not.toBeInTheDocument();
+ });
+
+ it("labels still-streaming reasoning Thinking and keeps it collapsed", () => {
+ setup(
+ chain({
+ steps: [{ kind: "reasoning", text: "Weighing the join order" }],
+ }),
+ true,
+ );
+ expect(screen.getByRole("button", { name: /Thinking/ })).toHaveAttribute(
+ "aria-expanded",
+ "false",
+ );
+ expect(screen.queryByText("Thought briefly")).not.toBeInTheDocument();
+ });
+
+ it("tucks a reasoning step under its own Thought briefly collapse when it isn't alone", async () => {
+ setup(
+ chain({
+ steps: [
+ { kind: "reasoning", text: "Weighing the join order" },
+ { kind: "tool", id: "t1", name: "analyze_data", status: "ended" },
+ ],
+ startedAtMs: 1000,
+ endedAtMs: 2000,
+ }),
+ false,
+ );
+ await expandChain();
+ // the header rolls up the whole turn ("Worked briefly"); the reasoning keeps
+ // its own "Thought briefly" collapse row — click it to reveal the body
+ expect(screen.getByText("Worked briefly")).toBeInTheDocument();
+ await userEvent.click(screen.getByText("Thought briefly"));
+ expect(screen.getByText("Weighing the join order")).toBeInTheDocument();
+ });
+
+ it("shows a lone thinking event inline, with no redundant collapse row", async () => {
+ setup(
+ chain({
+ steps: [{ kind: "reasoning", text: "Weighing the join order" }],
+ startedAtMs: 1000,
+ endedAtMs: 2000,
+ }),
+ false,
+ );
+ // the header already rolls it up as "Thought briefly"
+ expect(screen.getByText("Thought briefly")).toBeInTheDocument();
+ await expandChain();
+ // expanding shows the reasoning directly — the only "Thought briefly" is the
+ // header, not a second toggle repeating it
+ expect(screen.getByText("Weighing the join order")).toBeInTheDocument();
+ expect(screen.getAllByText("Thought briefly")).toHaveLength(1);
+ });
+
+ it("wraps the streamed search object in the verb + tense", async () => {
+ setup(
+ chain({
+ steps: [
+ {
+ kind: "tool",
+ id: "t1",
+ name: "search",
+ // backend streams just the object (the query); the FE owns the verb
+ title: "sales data",
+ status: "ended",
+ },
+ ],
+ startedAtMs: 1000,
+ endedAtMs: 2000,
+ }),
+ false,
+ );
+ await userEvent.click(screen.getByRole("button"));
+ expect(screen.getByText("Searched for sales data")).toBeInTheDocument();
+ });
+
+ it("renders a metabase:// link title as a clickable entity link with an icon", async () => {
+ setup(
+ chain({
+ steps: [
+ {
+ kind: "tool",
+ id: "t1",
+ name: "read_resource",
+ title: "[Orders](metabase://dashboard/123)",
+ status: "ended",
+ },
+ ],
+ startedAtMs: 1000,
+ endedAtMs: 2000,
+ }),
+ false,
+ );
+ await userEvent.click(screen.getByRole("button"));
+ // the row reads past tense with the entity inline ("Read Orders")
+ expect(await screen.findByText("Read")).toHaveTextContent("Read Orders");
+ expect(await screen.findByText("Orders")).toBeInTheDocument();
+ expect(
+ await screen.findByRole("img", { name: /dashboard icon/ }),
+ ).toBeInTheDocument();
+ });
+
+ it("labels a settled save_entity step with a clickable Saved link", async () => {
+ setup(
+ chain({
+ steps: [
+ {
+ kind: "tool",
+ id: "t1",
+ name: "save_entity",
+ // the link title only arrives once the saved card exists
+ title: "[Sales by Month](metabase://dashboard/123)",
+ status: "ended",
+ },
+ ],
+ startedAtMs: 1000,
+ endedAtMs: 2000,
+ }),
+ false,
+ );
+ await expandChain();
+ expect(await screen.findByText("Saved")).toHaveTextContent(
+ "Saved Sales by Month",
+ );
+ expect(await screen.findByText("Sales by Month")).toBeInTheDocument();
+ });
+
+ it("labels a running save_entity step with the generic Saving verb", () => {
+ setup(
+ chain({
+ steps: [
+ { kind: "tool", id: "t1", name: "save_entity", status: "started" },
+ ],
+ }),
+ true,
+ );
+ // no link title yet (the card doesn't exist mid-save) -> the generic verb
+ expect(screen.getAllByText("Saving").length).toBeGreaterThan(0);
+ });
+
+ it("keeps the top-level preview generic but names the entity in the active row", async () => {
+ setup(
+ chain({
+ steps: [
+ {
+ kind: "tool",
+ id: "t1",
+ name: "read_resource",
+ title: "[Orders](metabase://dashboard/123)",
+ status: "started",
+ },
+ ],
+ }),
+ true,
+ );
+ // top level stays generic + plural, never the entity
+ expect(screen.getByText("Reading resources")).toBeInTheDocument();
+ // expand: the running row names the entity, in the present tense
+ await userEvent.click(
+ screen.getByRole("button", { name: /Reading resources/ }),
+ );
+ // the running row reads present tense with the entity inline ("Reading Orders")
+ expect(await screen.findByText("Reading")).toHaveTextContent(
+ "Reading Orders",
+ );
+ expect(await screen.findByText("Orders")).toBeInTheDocument();
+ });
+
+ it("labels a search step with its query and a muted result count", async () => {
+ setup(
+ chain({
+ steps: [
+ {
+ kind: "tool",
+ id: "t1",
+ name: "search",
+ title: "revenue",
+ status: "ended",
+ searchResults: {
+ totalCount: 12,
+ results: [
+ {
+ id: 5,
+ type: "dashboard",
+ name: "Revenue Dashboard",
+ collection: { id: 3, name: "Finance" },
+ },
+ {
+ id: 9,
+ type: "table",
+ name: "orders",
+ display_name: "Orders",
+ database_id: 1,
+ database_name: "Sample DB",
+ database_schema: "PUBLIC",
+ },
+ ],
+ },
+ },
+ ],
+ startedAtMs: 1000,
+ endedAtMs: 2000,
+ }),
+ false,
+ );
+ // expand the chain, then expand the search step's results collapse
+ await expandChain();
+ expect(screen.getByText(/Searched for revenue/)).toBeInTheDocument();
+ expect(screen.getByText("12 results")).toBeInTheDocument();
+ expect(screen.queryByText("Explored 12 entities")).not.toBeInTheDocument();
+ await userEvent.click(screen.getByText("12 results"));
+ expect(screen.getByText("Revenue Dashboard")).toBeInTheDocument();
+ // collection context for a saved item
+ expect(screen.getByText("Finance")).toBeInTheDocument();
+ // tables prefer display_name, and show their database › schema
+ expect(screen.getByText("Orders")).toBeInTheDocument();
+ expect(screen.queryByText("orders")).not.toBeInTheDocument();
+ expect(screen.getByText("Sample DB", { exact: false })).toBeInTheDocument();
+ expect(screen.getByText("PUBLIC", { exact: false })).toBeInTheDocument();
+ });
+
+ it("labels an empty search with No results", async () => {
+ setup(
+ chain({
+ steps: [
+ {
+ kind: "tool",
+ id: "t1",
+ name: "search",
+ title: "revenue",
+ status: "ended",
+ searchResults: { totalCount: 0, results: [] },
+ },
+ ],
+ startedAtMs: 1000,
+ endedAtMs: 2000,
+ }),
+ false,
+ );
+ await expandChain();
+ expect(screen.getByText(/Searched for revenue/)).toBeInTheDocument();
+ expect(screen.getByText("No results")).toBeInTheDocument();
+ });
+
+ it("gives read_resource a real label and hides load_skill entirely", async () => {
+ setup(
+ chain({
+ steps: [
+ { kind: "tool", id: "t1", name: "read_resource", status: "ended" },
+ { kind: "tool", id: "t2", name: "load_skill", status: "ended" },
+ ],
+ startedAtMs: 1000,
+ endedAtMs: 3000,
+ }),
+ false,
+ );
+ await userEvent.click(screen.getByRole("button"));
+ expect(screen.getByText("Read resource")).toBeInTheDocument();
+ expect(screen.queryByText("Loading skill")).not.toBeInTheDocument();
+ expect(screen.queryByText("Working")).not.toBeInTheDocument();
+ });
+
+ it("hides deliberately unlabeled tools instead of showing Working", async () => {
+ setup(
+ chain({
+ steps: [
+ {
+ kind: "tool",
+ id: "t1",
+ name: "list_available_fields",
+ status: "ended",
+ },
+ { kind: "tool", id: "t2", name: "search", status: "ended" },
+ ],
+ startedAtMs: 1000,
+ endedAtMs: 2000,
+ }),
+ false,
+ );
+ await userEvent.click(screen.getByRole("button"));
+ // no query object streamed, and settled -> the past-tense generic verb
+ expect(screen.getByText("Searched")).toBeInTheDocument();
+ expect(screen.queryByText("Working")).not.toBeInTheDocument();
+ });
+
+ it("flips a tool label to past tense once the step has ended", async () => {
+ setup(
+ chain({
+ steps: [
+ { kind: "tool", id: "t1", name: "analyze_data", status: "ended" },
+ ],
+ startedAtMs: 1000,
+ endedAtMs: 2000,
+ }),
+ false,
+ );
+ await expandChain();
+ expect(screen.getByText("Analyzed the data")).toBeInTheDocument();
+ expect(screen.queryByText("Analyzing the data")).not.toBeInTheDocument();
+ });
+
+ it("shows the exact seconds on a reasoning row that ran long", async () => {
+ setup(
+ chain({
+ steps: [
+ { kind: "reasoning", text: "Deliberating", startedAtMs: 1000 },
+ { kind: "tool", id: "t1", name: "analyze_data", status: "ended" },
+ ],
+ startedAtMs: 1000,
+ endedAtMs: 8000,
+ }),
+ false,
+ );
+ await expandChain();
+ expect(screen.getByText("Thought for 7 seconds")).toBeInTheDocument();
+ expect(screen.queryByText("Thought briefly")).not.toBeInTheDocument();
+ });
+
+ it("aggregates a burst of resource reads into one row", async () => {
+ setup(
+ chain({
+ steps: [
+ { kind: "tool", id: "r1", name: "read_resource", status: "ended" },
+ { kind: "tool", id: "r2", name: "read_resource", status: "ended" },
+ { kind: "tool", id: "r3", name: "read_resource", status: "ended" },
+ ],
+ startedAtMs: 1000,
+ endedAtMs: 2000,
+ }),
+ false,
+ );
+ await expandChain();
+ expect(screen.getByText("Read 3 resources")).toBeInTheDocument();
+ expect(screen.queryByText("Reading resource")).not.toBeInTheDocument();
+ });
+});
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/ReasoningStep.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/ReasoningStep.tsx
new file mode 100644
index 00000000000..3463d651313
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/ReasoningStep.tsx
@@ -0,0 +1,44 @@
+import cx from "classnames";
+import { useState } from "react";
+
+import Animation from "metabase/css/core/animation.module.css";
+import { AIMarkdown } from "metabase/metabot/components/AIMarkdown";
+import { Collapse, Icon, Text, UnstyledButton } from "metabase/ui";
+
+import S from "./MetabotChainOfThought.module.css";
+
+export const ReasoningStep = ({
+ text,
+ label,
+ active,
+}: {
+ text: string;
+ label: string;
+ active: boolean;
+}) => {
+ const [open, setOpen] = useState(false);
+
+ return (
+ <div className={S.toolStep}>
+ <UnstyledButton
+ className={S.toolRow}
+ aria-expanded={open}
+ onClick={() => setOpen((prev) => !prev)}
+ >
+ <Text component="span" c="inherit" lh="inherit">
+ {label}
+ </Text>
+ <Icon
+ name="chevronright"
+ size={10}
+ className={cx(S.chevron, open && S.chevronOpen)}
+ />
+ </UnstyledButton>
+ <Collapse in={open}>
+ <div className={cx(S.reasoningBody, Animation.fadeIn)}>
+ <AIMarkdown isStreaming={active}>{text}</AIMarkdown>
+ </div>
+ </Collapse>
+ </div>
+ );
+};
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/SearchResults.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/SearchResults.tsx
new file mode 100644
index 00000000000..0e843723ad2
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/SearchResults.tsx
@@ -0,0 +1,109 @@
+import cx from "classnames";
+import { Fragment } from "react";
+
+import type { SearchResultItem } from "metabase/api/ai-streaming/schemas";
+import { EntityIcon } from "metabase/common/components/EntityIcon";
+import { Link } from "metabase/common/components/Link";
+import {
+ type IconData,
+ type IconModel,
+ modelIconMap,
+} from "metabase/common/utils/icon";
+import Animation from "metabase/css/core/animation.module.css";
+import { useGetIcon } from "metabase/hooks/use-icon";
+import type { MetabotChainStep } from "metabase/metabot/state";
+import { Text } from "metabase/ui";
+import { modelToUrl } from "metabase/urls";
+
+import S from "./MetabotChainOfThought.module.css";
+import { resultContextParts, toSearchModel } from "./utils";
+
+const isIconModel = (model: string): model is IconModel =>
+ model in modelIconMap;
+
+const ResultContext = ({ result }: { result: SearchResultItem }) => {
+ const parts = resultContextParts(result);
+ if (parts.length === 0) {
+ return null;
+ }
+ return (
+ <Text component="span" className={S.resultContext} lh="inherit">
+ {parts.map((part, i) => (
+ <Fragment key={i}>
+ {i > 0 && (
+ <span className={S.contextSeparator} aria-hidden>
+ /
+ </span>
+ )}
+ {part}
+ </Fragment>
+ ))}
+ </Text>
+ );
+};
+
+const ResultIcon = ({ result }: { result: SearchResultItem }) => {
+ const getIcon = useGetIcon();
+ const model = toSearchModel(result.type);
+ const iconData: IconData = isIconModel(model)
+ ? getIcon({
+ model,
+ display: result.display,
+ moderated_status: result.moderated_status,
+ })
+ : { name: "document" };
+ return <EntityIcon {...iconData} size={13} className={S.resultIcon} />;
+};
+
+const SearchResultRow = ({
+ result,
+ index,
+ animate,
+}: {
+ result: SearchResultItem;
+ index: number;
+ animate: boolean;
+}) => (
+ <Link
+ to={modelToUrl({
+ id: result.id,
+ model: toSearchModel(result.type),
+ name: result.name,
+ database_id: result.database_id,
+ })}
+ className={cx(S.resultRow, animate && Animation.fadeIn)}
+ style={
+ animate ? { animationDelay: `${Math.min(index * 45, 360)}ms` } : undefined
+ }
+ >
+ <ResultIcon result={result} />
+ <Text component="span" className={S.resultName} c="inherit" lh="inherit">
+ {result.display_name ?? result.name}
+ </Text>
+ <ResultContext result={result} />
+ </Link>
+);
+
+export const SearchResultsList = ({
+ step,
+ animate,
+}: {
+ step: MetabotChainStep & { kind: "tool" };
+ animate: boolean;
+}) => {
+ if (!step.searchResults || step.searchResults.results.length === 0) {
+ return null;
+ }
+ return (
+ <div className={S.resultsList}>
+ {step.searchResults.results.map((result, i) => (
+ <SearchResultRow
+ key={`${result.type}-${result.id}`}
+ result={result}
+ index={i}
+ animate={animate}
+ />
+ ))}
+ </div>
+ );
+};
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/ToolStep.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/ToolStep.tsx
new file mode 100644
index 00000000000..8f3549d836b
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/ToolStep.tsx
@@ -0,0 +1,110 @@
+import cx from "classnames";
+import { useState } from "react";
+import { t } from "ttag";
+
+import { TOOL_MESSAGES } from "metabase/metabot/constants";
+import { Collapse, Icon, Text, UnstyledButton } from "metabase/ui";
+
+import S from "./MetabotChainOfThought.module.css";
+import { SearchResultsList } from "./SearchResults";
+import { SEARCH_TOOL_NAME } from "./constants";
+import {
+ type ToolChainStep,
+ activeToolLabel,
+ doneToolLabel,
+ renderTitle,
+ searchResultCount,
+ titledToolLabel,
+} from "./utils";
+
+const toolLabelContent = (step: ToolChainStep, done: boolean) => {
+ if (step.name === SEARCH_TOOL_NAME) {
+ const label =
+ titledToolLabel(step, done) ?? (done ? t`Searched` : t`Searching`);
+ return (
+ <>
+ {renderTitle(label)}
+ {step.searchResults && (
+ <span className={S.resultCount}>
+ {searchResultCount(step.searchResults)}
+ </span>
+ )}
+ </>
+ );
+ }
+ const specific = titledToolLabel(step, done);
+ if (specific) {
+ return renderTitle(specific);
+ }
+ return done ? doneToolLabel(step.name) : activeToolLabel(step.name);
+};
+
+const ToolStepLabel = ({
+ step,
+ done,
+ className,
+}: {
+ step: ToolChainStep;
+ done: boolean;
+ className?: string;
+}) => (
+ <Text component="span" className={className} c="inherit" lh="inherit">
+ {toolLabelContent(step, done)}
+ </Text>
+);
+
+export const ResourceGroupStep = ({
+ count,
+ done,
+}: {
+ count: number;
+ done: boolean;
+}) => (
+ <div className={S.toolStep}>
+ <div className={cx(S.toolRow, S.toolRowStatic)}>
+ <Text component="span" c="inherit">
+ {done
+ ? TOOL_MESSAGES.read_resource.done(count)
+ : TOOL_MESSAGES.read_resource.active(count)}
+ </Text>
+ </div>
+ </div>
+);
+
+export const ToolStep = ({
+ step,
+ done,
+ animate,
+}: {
+ step: ToolChainStep;
+ done: boolean;
+ animate: boolean;
+}) => {
+ const [open, setOpen] = useState(false);
+ const hasResults = !!step.searchResults?.results.length;
+
+ return (
+ <div className={S.toolStep}>
+ <UnstyledButton
+ className={cx(S.toolRow, !hasResults && S.toolRowStatic)}
+ component={hasResults ? "button" : "div"}
+ aria-expanded={hasResults ? open : undefined}
+ onClick={hasResults ? () => setOpen((prev) => !prev) : undefined}
+ >
+ <ToolStepLabel step={step} done={done} />
+ {hasResults && (
+ <Icon
+ name="chevronright"
+ size={10}
+ className={cx(S.chevron, open && S.chevronOpen)}
+ />
+ )}
+ </UnstyledButton>
+ {hasResults && (
+ <Collapse in={open}>
+ <SearchResultsList step={step} animate={animate} />
+ </Collapse>
+ )}
+ </div>
+ );
+};
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/constants.ts b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/constants.ts
new file mode 100644
index 00000000000..0597b8a40b3
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/constants.ts
@@ -0,0 +1,7 @@
+export const SEARCH_TOOL_NAME = "search";
+export const SAVE_ENTITY_TOOL_NAME = "save_entity";
+export const RESOURCE_TOOL_NAME = "read_resource";
+
+export const REASONING_EXACT_THRESHOLD_MS = 5000;
+export const PREVIEW_MIN_MS = 600;
+export const NOW_TICK_MS = 150;
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/hooks.ts b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/hooks.ts
new file mode 100644
index 00000000000..81ea7d55da8
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/hooks.ts
@@ -0,0 +1,62 @@
+import { useDisclosure } from "@mantine/hooks";
+import { useEffect, useRef, useState } from "react";
+import { useInterval } from "react-use";
+
+import { NOW_TICK_MS, PREVIEW_MIN_MS } from "./constants";
+
+export const useNow = (active: boolean) => {
+ const [now, setNow] = useState(() => Date.now());
+ useInterval(() => setNow(Date.now()), active ? NOW_TICK_MS : null);
+ return now;
+};
+
+export const useAutoCollapseOnSettle = (isStreaming: boolean) => {
+ const [open, { toggle, close }] = useDisclosure(false);
+ const hasSettledRef = useRef(false);
+
+ useEffect(() => {
+ if (!isStreaming && !hasSettledRef.current) {
+ hasSettledRef.current = true;
+ close();
+ }
+ }, [isStreaming, close]);
+
+ return { open, toggle };
+};
+
+export const useMeteredLabel = (target: string): string => {
+ const [shown, setShown] = useState(target);
+ const queueRef = useRef<string[]>([]);
+ const lastRef = useRef(target);
+ const lockedRef = useRef(false);
+
+ useEffect(() => {
+ if (target === lastRef.current) {
+ return;
+ }
+ lastRef.current = target;
+ if (lockedRef.current) {
+ queueRef.current.push(target);
+ } else {
+ lockedRef.current = true;
+ setShown(target);
+ }
+ }, [target]);
+
+ useEffect(() => {
+ if (!lockedRef.current) {
+ return;
+ }
+ const id = setTimeout(() => {
+ const next = queueRef.current.shift();
+ if (next === undefined) {
+ lockedRef.current = false;
+ } else {
+ setShown(next);
+ }
+ }, PREVIEW_MIN_MS);
+ return () => clearTimeout(id);
+ }, [shown]);
+
+ return shown;
+};
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/index.ts b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/index.ts
new file mode 100644
index 00000000000..19f06c11f75
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/index.ts
@@ -0,0 +1 @@
+export { MetabotChainOfThought } from "./MetabotChainOfThought";
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/utils.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/utils.tsx
new file mode 100644
index 00000000000..9778b54e2a0
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/utils.tsx
@@ -0,0 +1,241 @@
+import { Fragment } from "react";
+import { match } from "ts-pattern";
+import { msgid, ngettext, t } from "ttag";
+
+import type { SearchResultItem } from "metabase/api/ai-streaming/schemas";
+import { MarkdownSmartLink } from "metabase/metabot/components/AIMarkdown/components/MarkdownSmartLink";
+import { getToolMessage } from "metabase/metabot/constants";
+import type { MetabotChainStep } from "metabase/metabot/state";
+import {
+ METABSE_PROTOCOL_MD_LINK,
+ type MetabaseProtocolEntityModel,
+ parseMetabaseProtocolMarkdownLink,
+} from "metabase/metabot/utils/links";
+import { isNotNull } from "metabase/utils/types";
+
+import {
+ REASONING_EXACT_THRESHOLD_MS,
+ RESOURCE_TOOL_NAME,
+ SAVE_ENTITY_TOOL_NAME,
+ SEARCH_TOOL_NAME,
+} from "./constants";
+
+export type ToolChainStep = MetabotChainStep & { kind: "tool" };
+export type ReasoningChainStep = MetabotChainStep & { kind: "reasoning" };
+
+const SEARCH_MODEL_BY_METABOT_TYPE: Record<string, string> = {
+ question: "card",
+ model: "dataset",
+};
+export const toSearchModel = (type: string) =>
+ SEARCH_MODEL_BY_METABOT_TYPE[type] ?? type;
+
+export const resultContextParts = (result: SearchResultItem): string[] => {
+ if (result.collection?.name) {
+ return [result.collection.name];
+ }
+ return [result.database_name, result.database_schema].filter(
+ (part): part is string => Boolean(part),
+ );
+};
+
+export const activeToolLabel = (name: string) =>
+ getToolMessage(name)?.active() ?? t`Thinking`;
+export const doneToolLabel = (name: string) =>
+ getToolMessage(name)?.done() ?? activeToolLabel(name);
+export const isHiddenTool = (name: string) => {
+ const message = getToolMessage(name);
+ return !!message && message.active() === undefined;
+};
+
+export type TitleSegment =
+ | { type: "text"; text: string }
+ | {
+ type: "link";
+ id: number;
+ name: string;
+ model: MetabaseProtocolEntityModel;
+ };
+
+export const splitTitle = (title: string): TitleSegment[] => {
+ const re = new RegExp(METABSE_PROTOCOL_MD_LINK.source, "g");
+ const segments: TitleSegment[] = [];
+ let lastIndex = 0;
+ for (const match of title.matchAll(re)) {
+ const start = match.index;
+ if (start > lastIndex) {
+ segments.push({ type: "text", text: title.slice(lastIndex, start) });
+ }
+ const parsed = parseMetabaseProtocolMarkdownLink(match[0]);
+ segments.push(
+ parsed
+ ? { type: "link", ...parsed }
+ : { type: "text", text: match.groups?.name ?? match[0] },
+ );
+ lastIndex = start + match[0].length;
+ }
+ if (lastIndex < title.length) {
+ segments.push({ type: "text", text: title.slice(lastIndex) });
+ }
+ return segments;
+};
+
+export const renderTitle = (title: string) =>
+ splitTitle(title).map((seg, i) =>
+ seg.type === "link" ? (
+ <MarkdownSmartLink
+ key={i}
+ id={seg.id}
+ name={seg.name}
+ model={seg.model}
+ />
+ ) : (
+ <Fragment key={i}>{seg.text}</Fragment>
+ ),
+ );
+
+export const titledToolLabel = (
+ step: ToolChainStep,
+ done: boolean,
+): string | null => {
+ const { title } = step;
+ if (!title) {
+ return null;
+ }
+ return match(step.name)
+ .with(SEARCH_TOOL_NAME, () =>
+ done ? t`Searched for ${title}` : t`Searching for ${title}`,
+ )
+ .with(RESOURCE_TOOL_NAME, () =>
+ done ? t`Read ${title}` : t`Reading ${title}`,
+ )
+ .with(SAVE_ENTITY_TOOL_NAME, () =>
+ done ? t`Saved ${title}` : t`Saving ${title}`,
+ )
+ .otherwise(() => title);
+};
+
+export const searchResultCount = ({ totalCount }: { totalCount: number }) =>
+ totalCount === 0
+ ? t`No results`
+ : ngettext(
+ msgid`${totalCount} result`,
+ `${totalCount} results`,
+ totalCount,
+ );
+
+const previewLabel = (step: MetabotChainStep): string | undefined =>
+ match(step)
+ .with({ kind: "reasoning" }, ({ text }) =>
+ text.trim() ? t`Thinking` : undefined,
+ )
+ .with(
+ { kind: "tool", name: RESOURCE_TOOL_NAME },
+ () => t`Reading resources`,
+ )
+ .with({ kind: "tool" }, ({ name }) =>
+ isHiddenTool(name) ? undefined : activeToolLabel(name),
+ )
+ .exhaustive();
+
+export const latestPreviewLabel = (steps: MetabotChainStep[]): string =>
+ steps.map(previewLabel).filter(isNotNull).at(-1) ?? t`Thinking`;
+
+export const isToolStep = (step: MetabotChainStep): step is ToolChainStep =>
+ step.kind === "tool";
+
+export const isRenderableStep = (step: MetabotChainStep) =>
+ isToolStep(step)
+ ? !!step.title || !!step.searchResults || !isHiddenTool(step.name)
+ : !!step.text;
+
+export const isResourceStep = (step: MetabotChainStep): step is ToolChainStep =>
+ isToolStep(step) && step.name === RESOURCE_TOOL_NAME;
+
+export type DisplayItem =
+ | { kind: "reasoning"; step: ReasoningChainStep; index: number }
+ | { kind: "tool"; step: ToolChainStep; index: number }
+ | { kind: "resourceGroup"; steps: ToolChainStep[]; index: number };
+
+const groupConsecutiveResources = (
+ steps: MetabotChainStep[],
+): MetabotChainStep[][] =>
+ steps.reduce<MetabotChainStep[][]>((groups, step) => {
+ const last = groups.at(-1);
+ if (last && isResourceStep(step) && isResourceStep(last[0])) {
+ return [...groups.slice(0, -1), [...last, step]];
+ }
+ return [...groups, [step]];
+ }, []);
+
+const toDisplayItem = (
+ group: MetabotChainStep[],
+ index: number,
+): DisplayItem => {
+ const [first] = group;
+ if (first.kind === "reasoning") {
+ return { kind: "reasoning", step: first, index };
+ }
+ const resources = group.filter(isResourceStep);
+ return resources.length > 1
+ ? { kind: "resourceGroup", steps: resources, index }
+ : { kind: "tool", step: first, index };
+};
+
+const isRenderableItem = (item: DisplayItem): boolean =>
+ item.kind === "resourceGroup" || isRenderableStep(item.step);
+
+export const buildDisplayItems = (steps: MetabotChainStep[]): DisplayItem[] => {
+ const groups = groupConsecutiveResources(steps);
+ return groups
+ .map((group, groupIndex) => {
+ const stepIndex = groups.slice(0, groupIndex).flat().length;
+ return toDisplayItem(group, stepIndex);
+ })
+ .filter(isRenderableItem);
+};
+
+export const soleReasoningStep = (
+ items: DisplayItem[],
+): ReasoningChainStep | null => {
+ const [only] = items;
+ return items.length === 1 && only.kind === "reasoning" ? only.step : null;
+};
+
+const exactSeconds = (durationMs: number | undefined): number | null =>
+ durationMs != null && durationMs >= REASONING_EXACT_THRESHOLD_MS
+ ? Math.round(durationMs / 1000)
+ : null;
+
+const thoughtFor = (seconds: number) =>
+ ngettext(
+ msgid`Thought for ${seconds} second`,
+ `Thought for ${seconds} seconds`,
+ seconds,
+ );
+
+const workedFor = (seconds: number) =>
+ ngettext(
+ msgid`Worked for ${seconds} second`,
+ `Worked for ${seconds} seconds`,
+ seconds,
+ );
+
+export const reasoningLabel = (durationMs: number | undefined): string => {
+ const seconds = exactSeconds(durationMs);
+ return seconds == null ? t`Thought briefly` : thoughtFor(seconds);
+};
+
+export const settledHeader = (
+ durationMs: number | undefined,
+ thinkingOnly: boolean,
+): string => {
+ if (durationMs == null) {
+ return thinkingOnly ? t`Thought about it` : t`Worked on it`;
+ }
+ if (thinkingOnly) {
+ return reasoningLabel(durationMs);
+ }
+ const seconds = exactSeconds(durationMs);
+ return seconds == null ? t`Worked briefly` : workedFor(seconds);
+};
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChat.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChat.tsx
index 3c2bf214804..f44a3415654 100644
--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChat.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChat.tsx
@@ -21,7 +21,6 @@ import type { MetabotConfig } from "../Metabot";
import Styles from "./MetabotChat.module.css";
import { MetabotChatEditor } from "./MetabotChatEditor";
import { Messages } from "./MetabotChatMessage";
-import { MetabotThinking } from "./MetabotThinking";
import { useScrollManager } from "./hooks";
const defaultConfig: MetabotConfig = {
@@ -180,10 +179,6 @@ export const MetabotChat = ({
debug={metabot.debugMode}
conversationId={metabot.conversationId}
/>
- {/* loading */}
- {metabot.isDoingScience && (
- <MetabotThinking toolCalls={metabot.activeToolCalls} />
- )}
{/* filler - height gets set via ref mutation */}
<div ref={fillerRef} data-testid="metabot-message-filler" />
{/* long convo warning */}
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx
index b8d6a3eef7c..83a36967988 100644
--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx
@@ -9,15 +9,16 @@ import { useSubmitMetabotFeedbackMutation } from "metabase/api/metabot";
import { useToast } from "metabase/common/hooks";
import { MetabotManagedProviderLimitActions } from "metabase/metabot/components/MetabotManagedProviderLimit";
import { useMetabotName } from "metabase/metabot/hooks";
-import type {
- MetabotAgentChatMessage,
- MetabotAgentDataPartMessage,
- MetabotAgentTextChatMessage,
- MetabotAgentTurnError,
- MetabotAgentTurnErroredMessage,
- MetabotChatMessage,
- MetabotDataPart,
- MetabotUserChatMessage,
+import {
+ type MetabotAgentChatMessage,
+ type MetabotAgentDataPartMessage,
+ type MetabotAgentTextChatMessage,
+ type MetabotAgentTurnError,
+ type MetabotAgentTurnErroredMessage,
+ type MetabotChatMessage,
+ type MetabotDataPart,
+ type MetabotUserChatMessage,
+ isChainOfThoughtMessage,
} from "metabase/metabot/state";
import {
ActionIcon,
@@ -37,6 +38,7 @@ import { AIMarkdown } from "../AIMarkdown/AIMarkdown";
import { AgentDataPartMessage } from "./MetabotAgentDataPartMessage";
import { AgentToolCallMessage } from "./MetabotAgentToolCallMessage";
+import { MetabotChainOfThought } from "./MetabotChainOfThought";
import Styles from "./MetabotChat.module.css";
import { MetabotFeedbackModal } from "./MetabotFeedbackModal";
@@ -68,11 +70,15 @@ const isUserVisibleMessage = (message: MetabotChatMessage): boolean =>
isUserVisibleDataPartMessage(message),
)
.with({ type: "tool_call" }, () => false)
+ .with({ type: "chain_of_thought" }, () => true)
.with({ type: "turn_aborted" }, () => true)
.with({ type: "turn_errored" }, () => true)
.with({ type: "turn_in_progress" }, () => false)
.exhaustive();
+const isConversationContent = (message: MetabotChatMessage) =>
+ !isChainOfThoughtMessage(message) && message.type !== "tool_call";
+
interface BaseMessageProps extends Omit<FlexProps, "onCopy"> {
message: MetabotChatMessage;
hideActions: boolean;
@@ -233,6 +239,9 @@ export const AgentMessage = ({
.with({ type: "tool_call" }, (m) => (
<AgentToolCallMessage message={m} />
))
+ .with({ type: "chain_of_thought" }, (m) => (
+ <MetabotChainOfThought message={m} isStreaming={isStreaming} />
+ ))
.with({ type: "turn_aborted" }, (m) => (
<AbortedTurnAlert messageId={m.id} debug={debug} onRetry={onRetry} />
))
@@ -536,6 +545,9 @@ export const Messages = ({
<>
{visibleMessages.map((message, index) => {
const next = visibleMessages[index + 1];
+ const nextContent = visibleMessages
+ .slice(index + 1)
+ .find(isConversationContent);
const isLastUserMessage = index > lastUserIndex;
return message.role === "agent" ? (
@@ -557,10 +569,18 @@ export const Messages = ({
? feedbackState.submitted[message.externalId]
: undefined
}
- hideActions={next?.role === "agent" || (isDoingScience && !next)}
+ hideActions={
+ isChainOfThoughtMessage(message) ||
+ nextContent?.role === "agent" ||
+ (isDoingScience && !nextContent)
+ }
extraActions={getExtraActions?.(message.id)}
onInternalLinkClick={onInternalLinkClick}
- isStreaming={isDoingScience && !next}
+ isStreaming={
+ isChainOfThoughtMessage(message)
+ ? isDoingScience && !nextContent
+ : isDoingScience && !next
+ }
/>
) : (
<UserMessage
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotThinking.module.css b/frontend/src/metabase/metabot/components/MetabotChat/MetabotThinking.module.css
deleted file mode 100644
index e1c9db5eee7..00000000000
--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotThinking.module.css
+++ /dev/null
@@ -1,37 +0,0 @@
-.toolCalls {
- padding-left: 0.75rem;
- border-left: 1px solid var(--mb-color-border-neutral);
- color: var(--mb-color-text-primary);
-}
-
-.toolCallStarted {
- --spread: 160px;
- --base-gradient-color: var(--mb-color-background_page-primary);
- --base-color: var(--mb-color-text-primary);
- --bg: linear-gradient(
- 90deg,
- #0000 calc(50% - var(--spread)),
- var(--base-gradient-color),
- #0000 calc(50% + var(--spread))
- );
-
- color: transparent;
- background-clip: text;
- background-size:
- 250% 100%,
- auto;
- background-image:
- var(--bg), linear-gradient(var(--base-color), var(--base-color));
- background-position: 50%;
- animation: rotate-background 1500ms ease infinite;
-}
-
-@keyframes rotate-background {
- 0% {
- background-position: 120%;
- }
-
- 100% {
- background-position: -20%;
- }
-}
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotThinking.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotThinking.tsx
deleted file mode 100644
index 65c9bed3628..00000000000
--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotThinking.tsx
+++ /dev/null
@@ -1,70 +0,0 @@
-import cx from "classnames";
-import { useMemo } from "react";
-
-import type { MetabotToolCall } from "metabase/metabot/state";
-import { Loader, Stack, Text, Transition } from "metabase/ui";
-
-import S from "./MetabotThinking.module.css";
-
-const fadeDown = {
- in: { opacity: 1, transform: "translateY(0)" },
- out: { opacity: 0, transform: "translateY(-100%)" },
- transitionProperty: "opacity, transform",
-};
-
-const ThoughtProcess = ({ toolCalls }: { toolCalls: MetabotToolCall[] }) => {
- if (!toolCalls.length) {
- return null;
- }
-
- // transition must be mounted before tool call is provided
- // so render a couple more than needed
- const slots = new Array(toolCalls.length + 2).fill(null);
-
- return (
- <Stack gap={0} mb="sm" className={S.toolCalls}>
- {slots.map((_, i) => {
- const tc: MetabotToolCall | undefined = toolCalls[i];
- return (
- <Transition
- key={i}
- mounted={!!tc}
- transition={fadeDown}
- duration={150}
- >
- {(styles) => (
- <Text
- style={styles}
- className={cx(tc?.status === "started" && S.toolCallStarted)}
- >
- {tc?.message}
- </Text>
- )}
- </Transition>
- );
- })}
- </Stack>
- );
-};
-
-export const MetabotThinking = ({
- toolCalls,
-}: {
- toolCalls: MetabotToolCall[];
-}) => {
- const toolCallsWithMsgs = useMemo(() => {
- return toolCalls.filter((tc) => !!tc.message);
- }, [toolCalls]);
-
- return (
- <Stack gap="xs" mt="md">
- <ThoughtProcess toolCalls={toolCallsWithMsgs} />
- <Loader
- color="core-brand"
- type="dots"
- size="lg"
- data-testid="metabot-response-loader"
- />
- </Stack>
- );
-};
diff --git a/frontend/src/metabase/metabot/components/MetabotConversationPage/MetabotConversationPage.unit.spec.tsx b/frontend/src/metabase/metabot/components/MetabotConversationPage/MetabotConversationPage.unit.spec.tsx
index 4d4ec03c3df..99ac35dffbb 100644
--- a/frontend/src/metabase/metabot/components/MetabotConversationPage/MetabotConversationPage.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotConversationPage/MetabotConversationPage.unit.spec.tsx
@@ -215,9 +215,7 @@ describe("MetabotConversationPage", () => {
});
expect(await screen.findByText("Loaded question")).toBeInTheDocument();
- expect(
- await screen.findByTestId("metabot-response-loader"),
- ).toBeInTheDocument();
+ expect(await screen.findByText("Thinking")).toBeInTheDocument();
expect(screen.getByTestId("metabot-stop-response")).toBeInTheDocument();
expect(
screen.queryByTestId("metabot-send-message"),
@@ -237,18 +235,14 @@ describe("MetabotConversationPage", () => {
metabotInitialState: createAskState(),
});
- expect(
- await screen.findByTestId("metabot-response-loader"),
- ).toBeInTheDocument();
+ expect(await screen.findByText("Thinking")).toBeInTheDocument();
act(() => {
jest.advanceTimersByTime(IN_PROGRESS_POLL_MS + 100);
});
expect(await screen.findByText("Here is the answer")).toBeInTheDocument();
- expect(
- screen.queryByTestId("metabot-response-loader"),
- ).not.toBeInTheDocument();
+ expect(screen.queryByText("Thinking")).not.toBeInTheDocument();
expect(screen.getByTestId("metabot-send-message")).toBeInTheDocument();
});
});
diff --git a/frontend/src/metabase/metabot/constants.ts b/frontend/src/metabase/metabot/constants.ts
index dffc353b9c0..a05ee0d511f 100644
--- a/frontend/src/metabase/metabot/constants.ts
+++ b/frontend/src/metabase/metabot/constants.ts
@@ -1,4 +1,4 @@
-import { t } from "ttag";
+import { msgid, ngettext, t } from "ttag";
import { isEmbedding } from "metabase/embedding/config";
@@ -110,53 +110,83 @@ export const METABOT_ERR_MSG = {
},
};
-export const TOOL_CALL_MESSAGES: Record<string, string | undefined> = {
- get construct_notebook_query() {
- return t`Creating a query`;
- },
- get analyze_data() {
- return t`Analyzing the data`;
- },
- get analyze_chart() {
- return t`Inspecting the visualization`;
- },
- get save_entity() {
- return t`Saving`;
- },
- get list_available_fields() {
- return undefined;
- },
- get search_data_sources() {
- return t`Checking available data sources`;
- },
- get search() {
- return t`Searching`;
- },
- get search_metabase_documentation() {
- return t`Consulting the docs`;
- },
- get write_transform_python() {
- return t`Writing Python`;
- },
- get write_transform_sql() {
- return t`Writing SQL`;
- },
- get todo_write() {
- return t`Planning`;
- },
- get todo_read() {
- return t`Planning`;
- },
- get search_transforms() {
- return t`Searching transforms`;
- },
- get get_transform_details() {
- return t`Getting transform details`;
- },
- get get_field_values() {
- return t`Retrieving table metadata`;
- },
- get search_tables() {
- return t`Searching database tables`;
- },
+export type ToolMessage = {
+ active: (count?: number) => string | undefined;
+ done: (count?: number) => string | undefined;
};
+
+export const TOOL_MESSAGES = {
+ analyze_chart: {
+ active: () => t`Inspecting the visualization`,
+ done: () => t`Inspected the visualization`,
+ },
+ analyze_data: {
+ active: () => t`Analyzing the data`,
+ done: () => t`Analyzed the data`,
+ },
+ construct_notebook_query: {
+ active: () => t`Creating a query`,
+ done: () => t`Created a query`,
+ },
+ get_field_values: {
+ active: () => t`Retrieving table metadata`,
+ done: () => t`Retrieved table metadata`,
+ },
+ get_transform_details: {
+ active: () => t`Getting transform details`,
+ done: () => t`Got transform details`,
+ },
+ list_available_fields: { active: () => undefined, done: () => undefined },
+ load_skill: { active: () => undefined, done: () => undefined },
+ read_resource: {
+ active: (count) =>
+ count == null
+ ? t`Reading resource`
+ : ngettext(
+ msgid`Reading ${count} resource`,
+ `Reading ${count} resources`,
+ count,
+ ),
+ done: (count) =>
+ count == null
+ ? t`Read resource`
+ : ngettext(
+ msgid`Read ${count} resource`,
+ `Read ${count} resources`,
+ count,
+ ),
+ },
+ save_entity: { active: () => t`Saving`, done: () => t`Saved` },
+ search: { active: () => t`Searching`, done: () => t`Searched` },
+ search_data_sources: {
+ active: () => t`Checking available data sources`,
+ done: () => t`Checked available data sources`,
+ },
+ search_metabase_documentation: {
+ active: () => t`Consulting the docs`,
+ done: () => t`Consulted the docs`,
+ },
+ search_tables: {
+ active: () => t`Searching database tables`,
+ done: () => t`Searched database tables`,
+ },
+ search_transforms: {
+ active: () => t`Searching transforms`,
+ done: () => t`Searched transforms`,
+ },
+ todo_read: { active: () => t`Planning`, done: () => t`Planned` },
+ todo_write: { active: () => t`Planning`, done: () => t`Planned` },
+ write_transform_python: {
+ active: () => t`Writing Python`,
+ done: () => t`Wrote Python`,
+ },
+ write_transform_sql: {
+ active: () => t`Writing SQL`,
+ done: () => t`Wrote SQL`,
+ },
+} satisfies Record<string, ToolMessage>;
+
+// widened view for lookups by dynamic (server-supplied) tool names
+const toolMessagesByName: Record<string, ToolMessage | undefined> =
+ TOOL_MESSAGES;
+export const getToolMessage = (name: string) => toolMessagesByName[name];
diff --git a/frontend/src/metabase/metabot/state/actions.ts b/frontend/src/metabase/metabot/state/actions.ts
index c44631c3305..9d8304acfdc 100644
--- a/frontend/src/metabase/metabot/state/actions.ts
+++ b/frontend/src/metabase/metabot/state/actions.ts
@@ -78,9 +78,13 @@ export const {
setConversationTitle,
setNavigateToPath,
setProfileOverride,
+ reasoningStart,
+ reasoningDelta,
toolCallStart,
toolCallArgs,
toolCallEnd,
+ toolCallTitled,
+ toolCallSearchResults,
setMetabotReqIdOverride,
setDebugMode,
addSuggestedTransform,
@@ -605,14 +609,40 @@ export const sendAgentRequest = createAsyncThunk<
cardId: part.data.card_id,
}),
);
+ const { tool_call_id, title } = part.data;
+ if (tool_call_id && title) {
+ dispatchToConvo(
+ toolCallTitled({
+ agentId,
+ toolCallId: tool_call_id,
+ title,
+ }),
+ );
+ }
pushDataPart({ type: "data_part", part });
})
+ .with({ type: "data-tool_title" }, (part) => {
+ const { tool_call_id, title } = part.data;
+ dispatchToConvo(
+ toolCallTitled({ agentId, toolCallId: tool_call_id, title }),
+ );
+ })
.with(
{ type: "data-navigate_to" },
{ type: "data-adhoc_viz" },
{ type: "data-static_viz" },
() => {},
)
+ .with({ type: "data-search_results" }, (part) => {
+ dispatchToConvo(
+ toolCallSearchResults({
+ agentId,
+ toolCallId: part.data.tool_call_id,
+ totalCount: part.data.total_count,
+ results: part.data.results,
+ }),
+ );
+ })
.exhaustive();
},
onStart: function handleStart(event) {
@@ -625,14 +655,26 @@ export const sendAgentRequest = createAsyncThunk<
);
},
onTextPart: function handleTextPart(delta) {
- dispatchToConvo(addAgentTextDelta({ agentId, text: delta }));
+ dispatchToConvo(
+ addAgentTextDelta({ agentId, text: delta, nowMs: Date.now() }),
+ );
+ },
+ onReasoningStart: function handleReasoningStart() {
+ dispatchToConvo(reasoningStart({ agentId, nowMs: Date.now() }));
+ },
+ onReasoningDelta: function handleReasoningDelta(event) {
+ dispatchToConvo(
+ reasoningDelta({ agentId, text: event.delta, nowMs: Date.now() }),
+ );
},
onToolInputStart: function handleToolInputStart(event) {
dispatchToConvo(
toolCallStart({
toolCallId: event.toolCallId,
toolName: event.toolName,
+ title: event.title,
agentId,
+ nowMs: Date.now(),
}),
);
},
@@ -641,8 +683,10 @@ export const sendAgentRequest = createAsyncThunk<
toolCallArgs({
toolCallId: event.toolCallId,
toolName: event.toolName,
+ title: event.title,
args: JSON.stringify(event.input),
agentId,
+ nowMs: Date.now(),
}),
);
},
@@ -655,6 +699,7 @@ export const sendAgentRequest = createAsyncThunk<
? event.output
: JSON.stringify(event.output),
agentId,
+ nowMs: Date.now(),
}),
);
},
@@ -665,6 +710,7 @@ export const sendAgentRequest = createAsyncThunk<
result: event.errorText,
isError: true,
agentId,
+ nowMs: Date.now(),
}),
);
},
diff --git a/frontend/src/metabase/metabot/state/index.ts b/frontend/src/metabase/metabot/state/index.ts
index 20cbb5ac183..20cff117ad8 100644
--- a/frontend/src/metabase/metabot/state/index.ts
+++ b/frontend/src/metabase/metabot/state/index.ts
@@ -2,3 +2,4 @@ export * from "./actions";
export * from "./reducer";
export * from "./selectors";
export * from "./types";
+export * from "./utils";
diff --git a/frontend/src/metabase/metabot/state/reducer-utils.ts b/frontend/src/metabase/metabot/state/reducer-utils.ts
index 2dbdb90207f..e4c5fa4551b 100644
--- a/frontend/src/metabase/metabot/state/reducer-utils.ts
+++ b/frontend/src/metabase/metabot/state/reducer-utils.ts
@@ -5,16 +5,18 @@ import { match } from "ts-pattern";
import {
METABOT_PROFILE_OVERRIDES,
- TOOL_CALL_MESSAGES,
+ getToolMessage,
} from "metabase/metabot/constants";
import { uuid } from "metabase/utils/uuid";
import type {
+ MetabotAgentChainOfThoughtMessage,
MetabotAgentId,
MetabotAgentTurnDisplayError,
MetabotAgentTurnError,
MetabotConverstationState,
MetabotDebugToolCallMessage,
+ MetabotSearchResults,
MetabotState,
} from "./types";
import { createMessageId } from "./utils";
@@ -51,11 +53,184 @@ export const pushNewToolCall = (
convo.activeToolCalls.push({
id: toolCallId,
name: toolName,
- message: TOOL_CALL_MESSAGES[toolName],
+ message: getToolMessage(toolName)?.active(),
status: "started",
});
};
+const activeChain = (convo: WritableDraft<MetabotConverstationState>) => {
+ const chain = convo.activeChainId
+ ? convo.messages.find((m) => m.id === convo.activeChainId)
+ : undefined;
+ return chain?.type === "chain_of_thought" ? chain : undefined;
+};
+
+const stampChainSpan = (
+ chain: WritableDraft<MetabotAgentChainOfThoughtMessage>,
+ nowMs?: number,
+) => {
+ if (nowMs == null) {
+ return;
+ }
+ chain.startedAtMs ??= nowMs;
+ chain.endedAtMs = nowMs;
+};
+
+const ensureChain = (
+ convo: WritableDraft<MetabotConverstationState>,
+ nowMs?: number,
+): WritableDraft<MetabotAgentChainOfThoughtMessage> => {
+ const existing = activeChain(convo);
+ if (existing) {
+ stampChainSpan(existing, nowMs);
+ return existing;
+ }
+ const chain: WritableDraft<MetabotAgentChainOfThoughtMessage> = {
+ id: createMessageId(),
+ role: "agent",
+ type: "chain_of_thought",
+ steps: [],
+ startedAtMs: nowMs,
+ endedAtMs: nowMs,
+ };
+ convo.messages.push(chain);
+ convo.activeChainId = chain.id;
+ return chain;
+};
+
+export const openChain = (convo: WritableDraft<MetabotConverstationState>) => {
+ ensureChain(convo);
+};
+
+const dropChain = (
+ convo: WritableDraft<MetabotConverstationState>,
+ id: string,
+) => {
+ convo.messages = convo.messages.filter((m) => m.id !== id);
+};
+
+export const startChainReasoning = (
+ convo: WritableDraft<MetabotConverstationState>,
+ nowMs?: number,
+) => {
+ ensureChain(convo, nowMs).steps.push({
+ kind: "reasoning",
+ text: "",
+ startedAtMs: nowMs,
+ });
+};
+
+export const appendChainReasoning = (
+ convo: WritableDraft<MetabotConverstationState>,
+ text: string,
+ nowMs?: number,
+) => {
+ const chain = ensureChain(convo, nowMs);
+ const last = chain.steps.at(-1);
+ if (last?.kind === "reasoning") {
+ last.text += text;
+ } else {
+ chain.steps.push({ kind: "reasoning", text, startedAtMs: nowMs });
+ }
+};
+
+export const addChainTool = (
+ convo: WritableDraft<MetabotConverstationState>,
+ {
+ id,
+ name,
+ title,
+ nowMs,
+ }: { id: string; name: string; title?: string; nowMs?: number },
+) => {
+ const existing = findChainToolStep(convo, id);
+ if (!existing) {
+ ensureChain(convo, nowMs).steps.push({
+ kind: "tool",
+ id,
+ name,
+ title,
+ status: "started",
+ startedAtMs: nowMs,
+ });
+ return;
+ }
+ if (title) {
+ existing.step.title = title;
+ }
+ if (existing.chain.id === convo.activeChainId) {
+ stampChainSpan(existing.chain, nowMs);
+ }
+};
+
+const findChainToolStep = (
+ convo: WritableDraft<MetabotConverstationState>,
+ toolCallId: string,
+) => {
+ for (const message of convo.messages) {
+ if (message.type === "chain_of_thought") {
+ const step = message.steps.find(
+ (s) => s.kind === "tool" && s.id === toolCallId,
+ );
+ if (step?.kind === "tool") {
+ return { chain: message, step };
+ }
+ }
+ }
+ return undefined;
+};
+
+export const setChainToolSearchResults = (
+ convo: WritableDraft<MetabotConverstationState>,
+ toolCallId: string,
+ searchResults: MetabotSearchResults,
+) => {
+ const found = findChainToolStep(convo, toolCallId);
+ if (found) {
+ found.step.searchResults = searchResults;
+ }
+};
+
+export const setChainToolTitle = (
+ convo: WritableDraft<MetabotConverstationState>,
+ toolCallId: string,
+ title: string,
+) => {
+ const found = findChainToolStep(convo, toolCallId);
+ if (found) {
+ found.step.title = title;
+ }
+};
+
+export const endChainTool = (
+ convo: WritableDraft<MetabotConverstationState>,
+ id: string,
+ nowMs?: number,
+) => {
+ const found = findChainToolStep(convo, id);
+ if (!found) {
+ return;
+ }
+ found.step.status = "ended";
+ const chainStillActive = found.chain.id === convo.activeChainId;
+ if (chainStillActive && nowMs != null) {
+ found.chain.endedAtMs = nowMs;
+ }
+};
+
+export const closeChain = (
+ convo: WritableDraft<MetabotConverstationState>,
+ nowMs?: number,
+) => {
+ const chain = activeChain(convo);
+ if (chain && chain.steps.length === 0) {
+ dropChain(convo, chain.id);
+ } else if (chain && nowMs != null) {
+ chain.endedAtMs = nowMs;
+ }
+ convo.activeChainId = undefined;
+};
+
export const getRequestConversation = (
state: WritableDraft<MetabotState>,
action: {
@@ -114,6 +289,7 @@ export const createConversation = (
visible: false,
state: {},
activeToolCalls: [],
+ activeChainId: undefined,
profileOverride: undefined,
pendingMessageExternalId: undefined,
...overrides,
diff --git a/frontend/src/metabase/metabot/state/reducer-utils.unit.spec.ts b/frontend/src/metabase/metabot/state/reducer-utils.unit.spec.ts
new file mode 100644
index 00000000000..a795af01ac9
--- /dev/null
+++ b/frontend/src/metabase/metabot/state/reducer-utils.unit.spec.ts
@@ -0,0 +1,91 @@
+import { produce } from "immer";
+
+import {
+ addChainTool,
+ appendChainReasoning,
+ closeChain,
+ createConversation,
+ endChainTool,
+ openChain,
+ startChainReasoning,
+} from "./reducer-utils";
+import type { MetabotConverstationState } from "./types";
+
+const chainOf = (convo: MetabotConverstationState) =>
+ convo.messages.find((m) => m.type === "chain_of_thought");
+
+describe("chain of thought duration timing", () => {
+ it("stamps and advances endedAtMs on each step so the span lives in redux", () => {
+ const opened = produce(createConversation("omnibot"), (d) => openChain(d));
+ const started = produce(opened, (d) => startChainReasoning(d, 1000));
+ expect(chainOf(started)).toMatchObject({
+ startedAtMs: 1000,
+ endedAtMs: 1000,
+ });
+
+ const advanced = produce(started, (d) =>
+ appendChainReasoning(d, "hi", 4000),
+ );
+ expect(chainOf(advanced)?.endedAtMs).toBe(4000);
+
+ const closed = produce(advanced, (d) => closeChain(d, 5000));
+ expect(chainOf(closed)?.endedAtMs).toBe(5000);
+ });
+
+ it("keeps the last step's end time when the answer closes the chain without a clock", () => {
+ const convo = produce(createConversation("omnibot"), (d) => {
+ openChain(d);
+ startChainReasoning(d, 1000);
+ appendChainReasoning(d, "x", 3000);
+ closeChain(d); // e.g. a data-part answer arrives with no nowMs
+ });
+ expect(chainOf(convo)?.endedAtMs).toBe(3000);
+ });
+
+ it("drops a shell that never gathered a step at turn teardown", () => {
+ const convo = produce(createConversation("omnibot"), (d) => {
+ openChain(d);
+ closeChain(d);
+ });
+ expect(chainOf(convo)).toBeUndefined();
+ expect(convo.activeChainId).toBeUndefined();
+ });
+
+ it("advances endedAtMs when a tool ends, so a turn ending on a tool counts its runtime", () => {
+ const convo = produce(createConversation("omnibot"), (d) => {
+ addChainTool(d, { id: "t1", name: "analyze_data", nowMs: 1000 });
+ endChainTool(d, "t1", 9000);
+ });
+ expect(chainOf(convo)).toMatchObject({
+ startedAtMs: 1000,
+ endedAtMs: 9000,
+ });
+ });
+
+ it("leaves a settled chain's span alone when its tool ends late", () => {
+ const convo = produce(createConversation("omnibot"), (d) => {
+ addChainTool(d, { id: "t1", name: "analyze_data", nowMs: 1000 });
+ closeChain(d, 2000); // answer text settled the chain
+ endChainTool(d, "t1", 9000);
+ });
+ expect(chainOf(convo)?.endedAtMs).toBe(2000);
+ });
+});
+
+describe("chain tool step dedupe", () => {
+ it("updates the existing step across a closed chain instead of re-adding it", () => {
+ const convo = produce(createConversation("omnibot"), (d) => {
+ // tool-input-start, then answer text closes the chain, then
+ // tool-input-available arrives late with the title
+ addChainTool(d, { id: "t1", name: "search", nowMs: 1000 });
+ closeChain(d, 2000);
+ addChainTool(d, { id: "t1", name: "search", title: "revenue" });
+ });
+ const chains = convo.messages.filter((m) => m.type === "chain_of_thought");
+ expect(chains).toHaveLength(1);
+ expect(chains[0].steps).toEqual([
+ expect.objectContaining({ kind: "tool", id: "t1", title: "revenue" }),
+ ]);
+ expect(convo.activeChainId).toBeUndefined();
+ });
+});
diff --git a/frontend/src/metabase/metabot/state/reducer.ts b/frontend/src/metabase/metabot/state/reducer.ts
index 04858b50e57..1a76805c661 100644
--- a/frontend/src/metabase/metabot/state/reducer.ts
+++ b/frontend/src/metabase/metabot/state/reducer.ts
@@ -2,6 +2,7 @@ import { type PayloadAction, createSlice, nanoid } from "@reduxjs/toolkit";
import { castDraft } from "immer";
import _ from "underscore";
+import type { SearchResultItem } from "metabase/api/ai-streaming/schemas";
import { logout } from "metabase/redux/auth";
import { uuid } from "metabase/utils/uuid";
import type {
@@ -16,15 +17,23 @@ import type { MetabotProfileId } from "../constants";
import { sendAgentRequest } from "./actions";
import {
type ConvoPayloadAction,
+ addChainTool,
appendAgentTurnAborted,
appendAgentTurnErrored,
+ appendChainReasoning,
+ closeChain,
convoReducer,
createConversation,
+ endChainTool,
findLastToolCallMessage,
getMetabotInitialState,
getRequestConversation,
+ openChain,
pushNewToolCall,
resetReactionState,
+ setChainToolSearchResults,
+ setChainToolTitle,
+ startChainReasoning,
} from "./reducer-utils";
import type {
MetabotAgentChatMessage,
@@ -112,6 +121,7 @@ export const metabot = createSlice({
>,
) => {
convo.activeToolCalls = [];
+ closeChain(convo);
const externalId = convo.pendingMessageExternalId;
// Unjustified type cast. FIXME
convo.messages.push({
@@ -125,8 +135,18 @@ export const metabot = createSlice({
} as any);
},
),
+ reasoningStart: convoReducer(
+ (convo, action: ConvoPayloadAction<{ nowMs?: number }>) => {
+ startChainReasoning(convo, action.payload.nowMs);
+ },
+ ),
+ reasoningDelta: convoReducer(
+ (convo, action: ConvoPayloadAction<{ text: string; nowMs?: number }>) => {
+ appendChainReasoning(convo, action.payload.text, action.payload.nowMs);
+ },
+ ),
addAgentTextDelta: convoReducer(
- (convo, action: ConvoPayloadAction<{ text: string }>) => {
+ (convo, action: ConvoPayloadAction<{ text: string; nowMs?: number }>) => {
const hasToolCalls = convo.activeToolCalls.length > 0;
const lastMessage = _.last(convo.messages);
const canAppend =
@@ -137,6 +157,7 @@ export const metabot = createSlice({
if (canAppend) {
lastMessage.message = lastMessage.message + action.payload.text;
} else {
+ closeChain(convo, action.payload.nowMs);
const externalId = convo.pendingMessageExternalId;
convo.messages.push({
id: createMessageId(),
@@ -176,10 +197,13 @@ export const metabot = createSlice({
action: ConvoPayloadAction<{
toolCallId: string;
toolName: string;
+ title?: string;
args?: string;
+ nowMs?: number;
}>,
) => {
- const { toolCallId, toolName, args } = action.payload;
+ const { toolCallId, toolName, title, args, nowMs } = action.payload;
+ addChainTool(convo, { id: toolCallId, name: toolName, title, nowMs });
// idempotent: both tool-input-start and tool-input-available are
// able to signal the start of a tool call
if (convo.activeToolCalls.some((tc) => tc.id === toolCallId)) {
@@ -194,10 +218,13 @@ export const metabot = createSlice({
action: ConvoPayloadAction<{
toolCallId: string;
toolName: string;
+ title?: string;
args: string;
+ nowMs?: number;
}>,
) => {
- const { toolCallId, toolName, args } = action.payload;
+ const { toolCallId, toolName, title, args, nowMs } = action.payload;
+ addChainTool(convo, { id: toolCallId, name: toolName, title, nowMs });
const existingMsg = findLastToolCallMessage(convo, toolCallId);
if (existingMsg) {
// if toolCallStart was called (tool-input-start event is optional)
@@ -215,11 +242,13 @@ export const metabot = createSlice({
toolCallId: string;
result?: string;
isError?: boolean;
+ nowMs?: number;
}>,
) => {
convo.activeToolCalls = convo.activeToolCalls.map((tc) =>
tc.id === action.payload.toolCallId ? { ...tc, status: "ended" } : tc,
);
+ endChainTool(convo, action.payload.toolCallId, action.payload.nowMs);
// Update the message in messages array with result for debug history
const message = findLastToolCallMessage(
@@ -235,6 +264,28 @@ export const metabot = createSlice({
}
},
),
+ toolCallSearchResults: convoReducer(
+ (
+ convo,
+ action: ConvoPayloadAction<{
+ toolCallId: string;
+ totalCount: number;
+ results: SearchResultItem[];
+ }>,
+ ) => {
+ const { toolCallId, totalCount, results } = action.payload;
+ setChainToolSearchResults(convo, toolCallId, { totalCount, results });
+ },
+ ),
+ toolCallTitled: convoReducer(
+ (
+ convo,
+ action: ConvoPayloadAction<{ toolCallId: string; title: string }>,
+ ) => {
+ const { toolCallId, title } = action.payload;
+ setChainToolTitle(convo, toolCallId, title);
+ },
+ ),
// only the last turn is rewindable (retry), so a single pre-turn snapshot
// is enough to roll `state` back; the server reconstructs it independently
rewindStateToMessageId: convoReducer(
@@ -369,10 +420,14 @@ export const metabot = createSlice({
convo.messages = castDraft(messages ?? []);
convo.state = snapshotState ?? {};
convo.activeToolCalls = activeToolCalls ?? [];
+ convo.activeChainId = undefined;
convo.conversationId = conversationId ?? uuid();
convo.loadId = nanoid();
convo.title = title;
convo.isProcessing = hasInProgressMessage(messages ?? []);
+ if (convo.isProcessing) {
+ openChain(convo); // resuming mid-response
+ }
convo.stateBeforeTurn = undefined;
convo.pendingMessageExternalId = undefined;
@@ -390,6 +445,8 @@ export const metabot = createSlice({
if (convo) {
convo.isProcessing = true;
convo.stateBeforeTurn = convo.state;
+ convo.activeChainId = undefined;
+ openChain(convo);
convo.pendingMessageExternalId = action.meta.arg.assistant_message_id;
}
})
@@ -400,6 +457,7 @@ export const metabot = createSlice({
convo.state = { ...action.payload.state };
}
convo.activeToolCalls = [];
+ closeChain(convo);
convo.isProcessing = false;
convo.experimental.developerMessage = "";
convo.pendingMessageExternalId = undefined;
@@ -443,6 +501,7 @@ export const metabot = createSlice({
convo.pendingMessageExternalId = undefined;
convo.activeToolCalls = [];
+ closeChain(convo);
convo.isProcessing = false;
}
});
diff --git a/frontend/src/metabase/metabot/state/types.ts b/frontend/src/metabase/metabot/state/types.ts
index e251ce92455..a78d50611b3 100644
--- a/frontend/src/metabase/metabot/state/types.ts
+++ b/frontend/src/metabase/metabot/state/types.ts
@@ -1,4 +1,7 @@
-import type { KnownDataPart } from "metabase/api/ai-streaming/schemas";
+import type {
+ KnownDataPart,
+ SearchResultItem,
+} from "metabase/api/ai-streaming/schemas";
import type { MetabotProfileId } from "metabase/metabot/constants";
import type {
MetabotCodeEdit,
@@ -10,9 +13,17 @@ import type {
export type MetabotDataPart = Exclude<
KnownDataPart,
- { type: "data-state" } | { type: "data-conversation-title" }
+ | { type: "data-state" }
+ | { type: "data-conversation-title" }
+ | { type: "data-search_results" }
+ | { type: "data-tool_title" }
>;
+export type MetabotSearchResults = {
+ totalCount: number;
+ results: SearchResultItem[];
+};
+
export type MetabotDataPartMetadata = {
codeEditBuffer?: MetabotCodeEditorBufferContext;
editorTransform?: MetabotTransformInfo;
@@ -89,10 +100,20 @@ export type MetabotAgentTurnInProgressMessage = {
externalId?: string;
};
+export type MetabotAgentChainOfThoughtMessage = {
+ id: string;
+ role: "agent";
+ type: "chain_of_thought";
+ steps: MetabotChainStep[];
+ startedAtMs?: number;
+ endedAtMs?: number;
+};
+
export type MetabotAgentChatMessage =
| MetabotAgentTextChatMessage
| MetabotAgentDataPartMessage
| MetabotDebugToolCallMessage
+ | MetabotAgentChainOfThoughtMessage
| MetabotAgentTurnAbortedMessage
| MetabotAgentTurnErroredMessage
| MetabotAgentTurnInProgressMessage;
@@ -113,6 +134,18 @@ export type MetabotToolCall = {
status: "started" | "ended";
};
+export type MetabotChainStep =
+ | { kind: "reasoning"; text: string; startedAtMs?: number }
+ | {
+ kind: "tool";
+ id: string;
+ name: string;
+ title?: string;
+ searchResults?: MetabotSearchResults;
+ status: "started" | "ended";
+ startedAtMs?: number;
+ };
+
export type MetabotReactionsState = {
navigateToPath: string | null;
suggestedCodeEdits: Partial<
@@ -131,6 +164,7 @@ export interface MetabotConverstationState {
state: MetabotStateContext;
stateBeforeTurn?: MetabotStateContext;
activeToolCalls: MetabotToolCall[];
+ activeChainId: string | undefined;
profileOverride: MetabotProfileId | undefined;
pendingMessageExternalId: string | undefined;
experimental: {
diff --git a/frontend/src/metabase/metabot/state/utils.ts b/frontend/src/metabase/metabot/state/utils.ts
index 3972731b850..26a910133da 100644
--- a/frontend/src/metabase/metabot/state/utils.ts
+++ b/frontend/src/metabase/metabot/state/utils.ts
@@ -1,11 +1,20 @@
import { nanoid } from "@reduxjs/toolkit";
-import type { MetabotChatMessage, SlashCommand } from "./types";
+import type {
+ MetabotAgentChainOfThoughtMessage,
+ MetabotChatMessage,
+ SlashCommand,
+} from "./types";
export const createMessageId = () => {
return `msg_${nanoid()}`;
};
+export const isChainOfThoughtMessage = (
+ message: MetabotChatMessage,
+): message is MetabotAgentChainOfThoughtMessage =>
+ message.type === "chain_of_thought";
+
export const hasInProgressMessage = (messages: MetabotChatMessage[]): boolean =>
messages.some((message) => message.type === "turn_in_progress");
diff --git a/frontend/src/metabase/metabot/tests/message.unit.spec.tsx b/frontend/src/metabase/metabot/tests/message.unit.spec.tsx
index a671db315f6..7f00e00f36d 100644
--- a/frontend/src/metabase/metabot/tests/message.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/tests/message.unit.spec.tsx
@@ -7,7 +7,6 @@ import {
enterChatMessage,
input,
mockAgentEndpoint,
- responseLoader,
sendMessageButton,
setup,
whoIsYourFavoriteResponse,
@@ -26,7 +25,9 @@ describe("metabot > message", () => {
expect(await input()).toHaveTextContent("Who is your favorite?");
await enterChatMessage("Who is your favorite?");
- expect(await responseLoader()).toBeInTheDocument();
+ expect(
+ await screen.findByTestId("metabot-chain-of-thought"),
+ ).toBeInTheDocument();
sendResponse();
@@ -50,6 +51,28 @@ describe("metabot > message", () => {
).toBeInTheDocument();
});
+ it("keeps the reply's actions when reasoning trails the final text", async () => {
+ setup();
+ mockAgentEndpoint({
+ stream: createMockSSEStream(
+ (async function* () {
+ yield { type: "text-delta", id: "t1", delta: "Here you go." };
+ yield { type: "reasoning-start", id: "r1" };
+ yield { type: "reasoning-delta", id: "r1", delta: "double-checking" };
+ yield { type: "reasoning-end", id: "r1" };
+ yield { type: "finish", finishReason: "stop" };
+ })(),
+ ),
+ });
+
+ await enterChatMessage("Question");
+
+ expect(await screen.findByText("Here you go.")).toBeInTheDocument();
+ expect(
+ await screen.findAllByTestId("metabot-chat-message-copy"),
+ ).toHaveLength(2);
+ });
+
it("should properly handle partial messages", async () => {
setup();
diff --git a/frontend/src/metabase/metabot/tests/reducer.unit.spec.ts b/frontend/src/metabase/metabot/tests/reducer.unit.spec.ts
index b0b1b322669..632687a1daf 100644
--- a/frontend/src/metabase/metabot/tests/reducer.unit.spec.ts
+++ b/frontend/src/metabase/metabot/tests/reducer.unit.spec.ts
@@ -323,6 +323,12 @@ describe("metabot reducer", () => {
describe("tool calls", () => {
const agentId = "omnibot" as const;
+ const getToolCallMessages = (store: ReturnType<typeof createTestStore>) =>
+ store
+ .getState()
+ .metabot.conversations.omnibot?.messages.filter(
+ (m) => m.type === "tool_call",
+ );
it("toolCallStart is idempotent for the same toolCallId", () => {
const store = createTestStore();
@@ -342,7 +348,7 @@ describe("metabot reducer", () => {
);
const convo = store.getState().metabot.conversations.omnibot;
- expect(convo?.messages).toHaveLength(1);
+ expect(getToolCallMessages(store)).toHaveLength(1);
expect(convo?.activeToolCalls).toHaveLength(1);
});
@@ -365,7 +371,7 @@ describe("metabot reducer", () => {
);
const convo = store.getState().metabot.conversations.omnibot;
- expect(convo?.messages).toEqual([
+ expect(getToolCallMessages(store)).toEqual([
expect.objectContaining({
id: "x",
type: "tool_call",
@@ -388,7 +394,7 @@ describe("metabot reducer", () => {
);
const convo = store.getState().metabot.conversations.omnibot;
- expect(convo?.messages).toEqual([
+ expect(getToolCallMessages(store)).toEqual([
expect.objectContaining({
id: "x",
type: "tool_call",
@@ -418,7 +424,7 @@ describe("metabot reducer", () => {
);
const convo = store.getState().metabot.conversations.omnibot;
- expect(convo?.messages).toEqual([
+ expect(getToolCallMessages(store)).toEqual([
expect.objectContaining({
id: "x",
type: "tool_call",
@@ -432,4 +438,177 @@ describe("metabot reducer", () => {
]);
});
});
+
+ describe("chain of thought", () => {
+ const agentId = "omnibot" as const;
+ const getConvo = (store: ReturnType<typeof createTestStore>) =>
+ store.getState().metabot.conversations.omnibot;
+ const getChain = (store: ReturnType<typeof createTestStore>) =>
+ getConvo(store)?.messages.find((m) => m.type === "chain_of_thought");
+
+ it("accumulates reasoning deltas into one chain step", () => {
+ const store = createTestStore();
+ store.dispatch(metabotActions.reasoningStart({ agentId }));
+ store.dispatch(metabotActions.reasoningDelta({ agentId, text: "Think" }));
+ store.dispatch(metabotActions.reasoningDelta({ agentId, text: "ing" }));
+
+ const chain = getChain(store);
+ expect(chain?.type === "chain_of_thought" && chain.steps).toEqual([
+ { kind: "reasoning", text: "Thinking" },
+ ]);
+ expect(getConvo(store)?.activeChainId).toBe(chain?.id);
+ });
+
+ it("interleaves tool calls between reasoning blocks in order", () => {
+ const store = createTestStore();
+ store.dispatch(metabotActions.reasoningStart({ agentId }));
+ store.dispatch(metabotActions.reasoningDelta({ agentId, text: "look" }));
+ store.dispatch(
+ metabotActions.toolCallStart({
+ agentId,
+ toolCallId: "t1",
+ toolName: "search",
+ }),
+ );
+ store.dispatch(metabotActions.reasoningStart({ agentId }));
+ store.dispatch(metabotActions.reasoningDelta({ agentId, text: "now" }));
+
+ const chain = getChain(store);
+ expect(chain?.type === "chain_of_thought" && chain.steps).toEqual([
+ { kind: "reasoning", text: "look" },
+ { kind: "tool", id: "t1", name: "search", status: "started" },
+ { kind: "reasoning", text: "now" },
+ ]);
+ });
+
+ it("marks a tool step ended when its result arrives", () => {
+ const store = createTestStore();
+ store.dispatch(
+ metabotActions.toolCallStart({
+ agentId,
+ toolCallId: "t1",
+ toolName: "search",
+ }),
+ );
+ store.dispatch(
+ metabotActions.toolCallEnd({ agentId, toolCallId: "t1", result: "ok" }),
+ );
+
+ const chain = getChain(store);
+ expect(chain?.type === "chain_of_thought" && chain.steps).toEqual([
+ { kind: "tool", id: "t1", name: "search", status: "ended" },
+ ]);
+ });
+
+ it("persists the chain but closes it when the answer text starts", () => {
+ const store = createTestStore();
+ store.dispatch(metabotActions.reasoningStart({ agentId }));
+ store.dispatch(metabotActions.reasoningDelta({ agentId, text: "hmm" }));
+ store.dispatch(metabotActions.addAgentTextDelta({ agentId, text: "hi" }));
+
+ // the chain message stays in history, and its id is released
+ expect(getChain(store)).toBeDefined();
+ expect(getConvo(store)?.activeChainId).toBeUndefined();
+
+ // later reasoning starts a fresh chain after the answer text
+ store.dispatch(metabotActions.reasoningStart({ agentId }));
+ const chains = getConvo(store)?.messages.filter(
+ (m) => m.type === "chain_of_thought",
+ );
+ expect(chains).toHaveLength(2);
+ });
+
+ it("attaches search results to their tool step, even after the chain closed", () => {
+ const store = createTestStore();
+ store.dispatch(
+ metabotActions.toolCallStart({
+ agentId,
+ toolCallId: "t1",
+ toolName: "search",
+ }),
+ );
+ store.dispatch(metabotActions.addAgentTextDelta({ agentId, text: "hi" }));
+ store.dispatch(
+ metabotActions.toolCallSearchResults({
+ agentId,
+ toolCallId: "t1",
+ totalCount: 1,
+ results: [{ id: 1, type: "table", name: "orders" }],
+ }),
+ );
+
+ const chain = getChain(store);
+ expect(
+ chain?.type === "chain_of_thought" && chain.steps[0],
+ ).toMatchObject({ kind: "tool", searchResults: { totalCount: 1 } });
+ });
+
+ it("stamps a save_entity step title from its saved-entity data part", () => {
+ const store = createTestStore();
+ store.dispatch(
+ metabotActions.toolCallStart({
+ agentId,
+ toolCallId: "t1",
+ toolName: "save_entity",
+ }),
+ );
+ store.dispatch(metabotActions.addAgentTextDelta({ agentId, text: "hi" }));
+ // the saved card's link only exists once the tool finishes
+ store.dispatch(
+ metabotActions.toolCallTitled({
+ agentId,
+ toolCallId: "t1",
+ title: "[Sales by Month](metabase://question/5)",
+ }),
+ );
+
+ const chain = getChain(store);
+ expect(
+ chain?.type === "chain_of_thought" && chain.steps[0],
+ ).toMatchObject({
+ kind: "tool",
+ name: "save_entity",
+ title: "[Sales by Month](metabase://question/5)",
+ });
+ });
+
+ it("backfills a title arriving on tool-input-available", () => {
+ const store = createTestStore();
+ store.dispatch(
+ metabotActions.toolCallStart({
+ agentId,
+ toolCallId: "t1",
+ toolName: "search",
+ }),
+ );
+ store.dispatch(
+ metabotActions.toolCallArgs({
+ agentId,
+ toolCallId: "t1",
+ toolName: "search",
+ title: "Searching revenue",
+ args: "{}",
+ }),
+ );
+
+ const chain = getChain(store);
+ expect(chain?.type === "chain_of_thought" && chain.steps).toEqual([
+ expect.objectContaining({ kind: "tool", title: "Searching revenue" }),
+ ]);
+ });
+
+ it("releases the active chain id when a snapshot replaces the conversation", () => {
+ const store = createTestStore();
+ store.dispatch(metabotActions.reasoningStart({ agentId }));
+ store.dispatch(
+ metabotActions.setConversationSnapshot({
+ agentId,
+ conversationId: "snap-1",
+ messages: [],
+ }),
+ );
+
+ expect(getConvo(store)?.activeChainId).toBeUndefined();
+ });
+ });
});
diff --git a/frontend/src/metabase/metabot/tests/tool-calls.unit.spec.tsx b/frontend/src/metabase/metabot/tests/tool-calls.unit.spec.tsx
index 8fd615e2d25..ea98a37fd0a 100644
--- a/frontend/src/metabase/metabot/tests/tool-calls.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/tests/tool-calls.unit.spec.tsx
@@ -16,7 +16,7 @@ import {
} from "./utils";
describe("metabot > tool calls", () => {
- it("should show list each tool being called as it comes in and cleared when finished", async () => {
+ it("should stream each tool call into the chain of thought, settling when finished", async () => {
setup();
const [pause1, pause2] = createPauses(2);
@@ -46,31 +46,31 @@ describe("metabot > tool calls", () => {
await enterChatMessage("Analyze this query");
- expect(await screen.findByText("Analyzing the data")).toBeInTheDocument();
+ expect(
+ (await screen.findAllByText("Analyzing the data")).length,
+ ).toBeGreaterThan(0);
expect(
screen.queryByText("Inspecting the visualization"),
).not.toBeInTheDocument();
pause1.resolve();
- expect(await screen.findByText("Analyzing the data")).toBeInTheDocument();
expect(
- await screen.findByText("Inspecting the visualization"),
- ).toBeInTheDocument();
+ (await screen.findAllByText("Inspecting the visualization")).length,
+ ).toBeGreaterThan(0);
pause2.resolve();
+ expect(
+ await screen.findByText(/Worked (briefly|for|on)/),
+ ).toBeInTheDocument();
await waitFor(() => {
- expect(screen.queryByText("Analyzing the data")).not.toBeInTheDocument();
- });
- await waitFor(() => {
- expect(
- screen.queryByText("Inspecting the visualization"),
- ).not.toBeInTheDocument();
+ expect(screen.getByText("Analyzed the data")).not.toBeVisible();
});
+ expect(screen.getByText("Inspected the visualization")).not.toBeVisible();
});
- it("should clear out list of tool calls when new text comes in", async () => {
+ it("should settle the chain when answer text arrives and start a fresh one for later tools", async () => {
setup();
const [pause1, pause2, pause3] = createPauses(3);
@@ -102,30 +102,23 @@ describe("metabot > tool calls", () => {
await enterChatMessage("Analyze this query");
- expect(await screen.findByText("Analyzing the data")).toBeInTheDocument();
+ expect(
+ (await screen.findAllByText("Analyzing the data")).length,
+ ).toBeGreaterThan(0);
expect(
screen.queryByText("Inspecting the visualization"),
).not.toBeInTheDocument();
pause1.resolve();
-
- await waitFor(() => {
- expect(screen.queryByText("Analyzing the data")).not.toBeInTheDocument();
- });
- expect(screen.getByText("Hey.")).toBeInTheDocument();
- await waitFor(() => {
- expect(
- screen.queryByText("Inspecting the visualization"),
- ).not.toBeInTheDocument();
- });
-
pause2.resolve();
expect(await screen.findByText("Hey.")).toBeInTheDocument();
+ await waitFor(() => {
+ expect(screen.getByText("Analyzed the data")).not.toBeVisible();
+ });
expect(
- await screen.findByText("Inspecting the visualization"),
- ).toBeInTheDocument();
- expect(screen.queryByText("Analyzing the data")).not.toBeInTheDocument();
+ (await screen.findAllByText("Inspecting the visualization")).length,
+ ).toBeGreaterThan(0);
pause3.resolve();
@@ -158,11 +151,13 @@ describe("metabot > tool calls", () => {
});
await enterChatMessage("Request");
- await assertConversation([
- ["user", "Request"],
- ["agent", "Response 1"],
- ["agent", "Response 2"],
- ]);
+ await waitFor(async () => {
+ await assertConversation([
+ ["user", "Request"],
+ ["agent", "Response 1"],
+ ["agent", "Response 2"],
+ ]);
+ });
});
it("should be able to stop a response via stop button", async () => {
diff --git a/frontend/src/metabase/metabot/tests/utils.tsx b/frontend/src/metabase/metabot/tests/utils.tsx
index 18e851e4548..4096f0a4663 100644
--- a/frontend/src/metabase/metabot/tests/utils.tsx
+++ b/frontend/src/metabase/metabot/tests/utils.tsx
@@ -76,9 +76,19 @@ export const conversationTitle = () =>
screen.findByTestId("metabot-conversation-title");
export const queryConversationTitle = () =>
screen.queryByTestId("metabot-conversation-title");
-export const chatMessages = () =>
- screen.findAllByTestId("metabot-chat-message");
-export const lastChatMessage = async () => (await chatMessages()).at(-1);
+export const chatMessages = async ({
+ includeChainOfThought = false,
+}: { includeChainOfThought?: boolean } = {}) => {
+ const messages = await screen.findAllByTestId("metabot-chat-message");
+ return includeChainOfThought
+ ? messages
+ : messages.filter(
+ (el) => !within(el).queryByTestId("metabot-chain-of-thought"),
+ );
+};
+export const lastChatMessage = async (options?: {
+ includeChainOfThought?: boolean;
+}) => (await chatMessages(options)).at(-1);
export const input = async () => {
const chatInput = await screen.findByTestId("metabot-chat-input");
return chatInput.querySelector('[contenteditable="true"]')!;
diff --git a/frontend/src/metabase/metabot/utils/slack-mrkdwn.ts b/frontend/src/metabase/metabot/utils/slack-mrkdwn.ts
index 36d74cc0a87..d9f13372143 100644
--- a/frontend/src/metabase/metabot/utils/slack-mrkdwn.ts
+++ b/frontend/src/metabase/metabot/utils/slack-mrkdwn.ts
@@ -35,6 +35,7 @@ export function convertSlackChatMessage(
}))
.with({ role: "agent", type: "data_part" }, (m) => m)
.with({ role: "agent", type: "tool_call" }, (m) => m)
+ .with({ role: "agent", type: "chain_of_thought" }, (m) => m)
.with({ role: "agent", type: "turn_aborted" }, (m) => m)
.with({ role: "agent", type: "turn_errored" }, (m) => m)
.with({ role: "agent", type: "turn_in_progress" }, (m) => m)
diff --git a/frontend/src/metabase/rich_text_editing/tiptap/extensions/MetabotEmbed/MetabotEmbed.module.css b/frontend/src/metabase/rich_text_editing/tiptap/extensions/MetabotEmbed/MetabotEmbed.module.css
index e0aaaef781a..9c7dc2c63e2 100644
--- a/frontend/src/metabase/rich_text_editing/tiptap/extensions/MetabotEmbed/MetabotEmbed.module.css
+++ b/frontend/src/metabase/rich_text_editing/tiptap/extensions/MetabotEmbed/MetabotEmbed.module.css
@@ -32,3 +32,35 @@
.iconShrink {
flex-shrink: 0;
}
+
+.toolCallStarted {
+ --spread: 160px;
+ --base-gradient-color: var(--mb-color-background_page-primary);
+ --base-color: var(--mb-color-text-primary);
+ --bg: linear-gradient(
+ 90deg,
+ #0000 calc(50% - var(--spread)),
+ var(--base-gradient-color),
+ #0000 calc(50% + var(--spread))
+ );
+
+ color: transparent;
+ background-clip: text;
+ background-size:
+ 250% 100%,
+ auto;
+ background-image:
+ var(--bg), linear-gradient(var(--base-color), var(--base-color));
+ background-position: 50%;
+ animation: rotate-background 1500ms ease infinite;
+}
+
+@keyframes rotate-background {
+ 0% {
+ background-position: 120%;
+ }
+
+ 100% {
+ background-position: -20%;
+ }
+}
diff --git a/frontend/src/metabase/rich_text_editing/tiptap/extensions/MetabotEmbed/MetabotEmbed.tsx b/frontend/src/metabase/rich_text_editing/tiptap/extensions/MetabotEmbed/MetabotEmbed.tsx
index 1c4204123cc..f03e10a457a 100644
--- a/frontend/src/metabase/rich_text_editing/tiptap/extensions/MetabotEmbed/MetabotEmbed.tsx
+++ b/frontend/src/metabase/rich_text_editing/tiptap/extensions/MetabotEmbed/MetabotEmbed.tsx
@@ -13,7 +13,6 @@ import { t } from "ttag";
import { useLazyMetabotGenerateContentQuery } from "metabase/api";
import CS from "metabase/css/core/index.css";
-import MetabotThinkingStyles from "metabase/metabot/components/MetabotChat/MetabotThinking.module.css";
import { MetabotIcon } from "metabase/metabot/components/MetabotIcon";
import {
useMetabotName,
@@ -318,10 +317,7 @@ export const MetabotComponent = memo(
<Flex px="md" pb="md" pt="sm" gap="sm" contentEditable={false}>
<Flex flex={1} my="auto">
{isLoading ? (
- <Text
- flex={1}
- className={MetabotThinkingStyles.toolCallStarted}
- >
+ <Text flex={1} className={S.toolCallStarted}>
{t`Working on it...`}
</Text>
) : errorText ? (
diff --git a/resources/metabot/prompts/shared/prompt_snippets/communication.selmer b/resources/metabot/prompts/shared/prompt_snippets/communication.selmer
index 3398047b98d..1532b9ed0fd 100644
--- a/resources/metabot/prompts/shared/prompt_snippets/communication.selmer
+++ b/resources/metabot/prompts/shared/prompt_snippets/communication.selmer
@@ -3,5 +3,3 @@
Link every entity you reference. The format is `[Descriptive name](metabase://<type>/<id>)` where `<type>` is one of `chart`, `metric`, `model`, `table`, `dashboard`, or `question`. Use human-readable names ("Regional Sales Analysis", not "Chart 123"). When you build a chart, always include its link in your final message.
Keep final messages brief: the chart link, one or two sentences on how you translated the question (which data source, the filter or grouping used), and any disclosures. The user is looking at the visualization — they don't need a narrated tour of the query.
-
-You don't need to be silent between tool calls — short thinking-out-loud is fine and sometimes helpful. But keep the final message to the user focused on results and decisions, not on your process.
diff --git a/resources/metabot/prompts/system/embedding-next.selmer b/resources/metabot/prompts/system/embedding-next.selmer
index 1d99151adfa..2cb288bbd59 100644
--- a/resources/metabot/prompts/system/embedding-next.selmer
+++ b/resources/metabot/prompts/system/embedding-next.selmer
@@ -20,6 +20,8 @@ You are judged on whether the produced query matches the user's intent against t
{% include "metabot/prompts/shared/prompt_snippets/communication.selmer" %}
+You don't need to be silent between tool calls — short thinking-out-loud is fine and sometimes helpful. But keep the final message to the user focused on results and decisions, not on your process.
+
{% include "metabot/prompts/shared/prompt_snippets/field-references.selmer" %}
{% include "metabot/prompts/shared/prompt_snippets/personality.selmer" %}
diff --git a/test/metabase/metabot/agent/prompt_gating_test.clj b/test/metabase/metabot/agent/prompt_gating_test.clj
index 76c194b347c..1ca2a9e62ba 100644
--- a/test/metabase/metabot/agent/prompt_gating_test.clj
+++ b/test/metabase/metabot/agent/prompt_gating_test.clj
@@ -278,3 +278,15 @@
metabot-chat-system-prompt ""]
(let [rendered (render-template "internal.selmer" all-yes-perms)]
(is (not (re-find #"NLQ only instruction" rendered)))))))
+
+(deftest ^:parallel communication-guidance-gates-on-reasoning-ui-test
+ (testing "templates for surfaces with a reasoning timeline skip the narration guidance"
+ (doseq [template ["internal.selmer"
+ "natural-language-querying-only.selmer"
+ "natural-language-querying-fallback.selmer"]]
+ (let [rendered (render-template template all-yes-perms)]
+ (is (not (re-find #"silent between tool calls" rendered))
+ template))))
+ (testing "the embedding SDK template (loader only) keeps the narration guidance"
+ (let [rendered (render-template "embedding-next.selmer" all-yes-perms)]
+ (is (re-find #"silent between tool calls" rendered)))))