Skip to content

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

Pace Metabot reasoning only while the user can see it (#85229)

Paolo Rascunà16c946de8fe5
diff --git a/frontend/src/metabase/api/ai-streaming/process-stream.ts b/frontend/src/metabase/api/ai-streaming/process-stream.ts
index de4cc3bfb8d..b8a2e6fff41 100644
--- a/frontend/src/metabase/api/ai-streaming/process-stream.ts
+++ b/frontend/src/metabase/api/ai-streaming/process-stream.ts
@@ -53,6 +53,9 @@ export type AIStreamingConfig = {
onToolErrorPart?: (event: ToolOutputErrorEvent) => void;
onError?: (error: StreamedError) => void;
onMessageMetadata?: (metadata: MessageMetadata) => void;
+ // Returns true while the user can see the reasoning of this stream.
+ // The pacer paces reasoning only then. See `smoothStreamEvents`.
+ shouldPaceReasoning?: () => boolean;
};
export interface ProcessedChatResponse {
@@ -93,7 +96,9 @@ export async function processChatResponse(
const events = parseSSEStream(stream);
const paced = isReducedMotionPreferred()
? events
- : smoothStreamEvents(events);
+ : smoothStreamEvents(events, {
+ shouldPaceReasoning: config.shouldPaceReasoning,
+ });
try {
for await (const event of paced) {
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 ce310b36df6..6679b5eb78e 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
@@ -105,7 +105,7 @@ describe("processChatResponse", () => {
]);
});
- it("should fire reasoning callbacks and not append reasoning to result", async () => {
+ it("should fire one reasoning callback per server delta and not append reasoning to result", async () => {
const mockStream = createMockSSEStream([
{ type: "reasoning-start", id: "r1" },
{ type: "reasoning-delta", id: "r1", delta: "Think" },
@@ -116,9 +116,14 @@ describe("processChatResponse", () => {
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.onReasoningDelta).toHaveBeenCalledTimes(2);
+ expect(config.onReasoningDelta).toHaveBeenNthCalledWith(
+ 1,
+ expect.objectContaining({ delta: "Think" }),
+ );
+ expect(config.onReasoningDelta).toHaveBeenNthCalledWith(
+ 2,
+ expect.objectContaining({ delta: "ing" }),
);
expect(config.onReasoningEnd).toHaveBeenCalledTimes(1);
expect(config.onError).not.toHaveBeenCalled();
@@ -126,6 +131,28 @@ describe("processChatResponse", () => {
expect(result.toolCalls).toEqual([]);
});
+ it("paces reasoning callbacks when shouldPaceReasoning returns true", async () => {
+ const mockStream = createMockSSEStream([
+ { type: "reasoning-delta", id: "r1", delta: "thinking it over" },
+ ]);
+ const config = {
+ ...getMockedCallbacks(),
+ shouldPaceReasoning: () => true,
+ };
+
+ await processChatResponse(mockStream, config);
+
+ expect(config.onReasoningDelta).toHaveBeenCalledTimes(3);
+ expect(config.onReasoningDelta).toHaveBeenNthCalledWith(
+ 1,
+ expect.objectContaining({ delta: "thinking " }),
+ );
+ expect(config.onReasoningDelta).toHaveBeenNthCalledWith(
+ 3,
+ expect.objectContaining({ delta: "over" }),
+ );
+ });
+
it("should ignore unhandled event types", async () => {
const mockStream = createMockSSEStream([{ type: "start-step" }]);
const config = getMockedCallbacks();
diff --git a/frontend/src/metabase/api/ai-streaming/smooth-stream.ts b/frontend/src/metabase/api/ai-streaming/smooth-stream.ts
index a5270162479..968c08d8379 100644
--- a/frontend/src/metabase/api/ai-streaming/smooth-stream.ts
+++ b/frontend/src/metabase/api/ai-streaming/smooth-stream.ts
@@ -29,19 +29,35 @@ type SmoothableDelta = TextDeltaEvent | ReasoningDeltaEvent;
const isSmoothable = (event: SSEEvent): event is SmoothableDelta =>
event.type === "text-delta" || event.type === "reasoning-delta";
-export type SmoothTextOptions = { delayInMs?: number };
+export type SmoothTextOptions = {
+ delayInMs?: number;
+ // Returns true while the user can see the reasoning of this stream.
+ shouldPaceReasoning?: () => boolean;
+};
-// 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
+// Re-emits text 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. While the document is hidden,
// it emits the whole buffer at once and starts no new wait.
+// Reasoning deltas are paced only while `shouldPaceReasoning` returns true.
+// When it returns false, the buffered words go out at once and the reasoning
+// passes through. Paced reasoning delays every later event, such as the SQL edit,
+// by minutes, so it is paced only while the user can see it.
+// https://github.com/metabase/metabase/issues/84969
export async function* smoothStreamEvents(
events: AsyncIterable<SSEEvent>,
- { delayInMs = DEFAULT_SMOOTHING_DELAY_MS }: SmoothTextOptions = {},
+ {
+ delayInMs = DEFAULT_SMOOTHING_DELAY_MS,
+ shouldPaceReasoning = () => false,
+ }: SmoothTextOptions = {},
): AsyncGenerator<SSEEvent> {
let buffer = "";
let pending: SmoothableDelta | null = null;
+ const canPace = () =>
+ !isDocumentHidden() &&
+ (pending?.type === "text-delta" || shouldPaceReasoning());
+
function* flush() {
if (pending && buffer.length > 0) {
yield { ...pending, delta: buffer };
@@ -50,8 +66,12 @@ export async function* smoothStreamEvents(
}
for await (const event of events) {
- // any other event flushes the buffered tail first, to preserve ordering
- if (!isSmoothable(event)) {
+ // other events, and reasoning that is not paced, flush the buffered tail
+ // first, to keep the order, and then pass through
+ const isPassThrough =
+ !isSmoothable(event) ||
+ (event.type === "reasoning-delta" && !shouldPaceReasoning());
+ if (isPassThrough) {
yield* flush();
yield event;
continue;
@@ -66,17 +86,17 @@ export async function* smoothStreamEvents(
pending = event;
let word = takeWord(buffer);
- while (word !== null && !isDocumentHidden()) {
+ while (word !== null && canPace()) {
yield { ...pending, delta: word };
buffer = buffer.slice(word.length);
- if (isDocumentHidden()) {
+ if (!canPace()) {
break;
}
await wait(delayInMs);
word = takeWord(buffer);
}
- if (isDocumentHidden()) {
+ if (!canPace()) {
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 d27aeb7ef9f..f2f8093b74e 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 { smoothStreamEvents } from "./smooth-stream";
+import { type SmoothTextOptions, smoothStreamEvents } from "./smooth-stream";
import type { SSEEvent } from "./sse-types";
async function* asAsyncIterable(events: SSEEvent[]) {
@@ -7,22 +7,27 @@ async function* asAsyncIterable(events: SSEEvent[]) {
}
}
-const smooth = async (events: SSEEvent[]) => {
+const collect = async (events: AsyncIterable<SSEEvent>) => {
const out: SSEEvent[] = [];
- for await (const event of smoothStreamEvents(asAsyncIterable(events), {
- delayInMs: 0,
- })) {
+ for await (const event of events) {
out.push(event);
}
return out;
};
+const smooth = (events: SSEEvent[], options: SmoothTextOptions = {}) =>
+ collect(
+ smoothStreamEvents(asAsyncIterable(events), { delayInMs: 0, ...options }),
+ );
+
const deltasOfType = (events: SSEEvent[], type: string) =>
events.flatMap((event) =>
event.type === type && "delta" in event ? [event.delta] : [],
);
const textDeltas = (events: SSEEvent[]) => deltasOfType(events, "text-delta");
+const reasoningDeltas = (events: SSEEvent[]) =>
+ deltasOfType(events, "reasoning-delta");
describe("smoothStreamEvents", () => {
it("emits buffered text one word at a time", async () => {
@@ -60,30 +65,157 @@ describe("smoothStreamEvents", () => {
]);
});
- 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" },
- ]);
+ describe("reasoning deltas", () => {
+ beforeEach(() => {
+ jest.spyOn(global, "setTimeout");
+ });
- expect(deltasOfType(out, "reasoning-delta")).toEqual([
- "thinking ",
- "it ",
- "over",
- ]);
- expect(out.every((e) => e.type !== "text-delta")).toBe(true);
+ afterEach(() => {
+ jest.restoreAllMocks();
+ });
+
+ it("passes reasoning deltas through unchanged when reasoning is not visible", async () => {
+ const events: SSEEvent[] = [
+ { type: "reasoning-start", id: "r1" },
+ { type: "reasoning-delta", id: "r1", delta: "thinking it over" },
+ { type: "reasoning-end", id: "r1" },
+ ];
+
+ const out = await smooth(events);
+
+ expect(out).toEqual(events);
+ expect(setTimeout).not.toHaveBeenCalled();
+ });
+
+ it("passes reasoning through when shouldPaceReasoning returns false", async () => {
+ const events: SSEEvent[] = [
+ { type: "reasoning-delta", id: "r1", delta: "thinking it over" },
+ ];
+
+ const out = await smooth(events, { shouldPaceReasoning: () => false });
+
+ expect(out).toEqual(events);
+ expect(setTimeout).not.toHaveBeenCalled();
+ });
+
+ it("paces reasoning word by word while shouldPaceReasoning returns true", async () => {
+ const out = await smooth(
+ [{ type: "reasoning-delta", id: "r1", delta: "thinking it over" }],
+ { shouldPaceReasoning: () => true },
+ );
+
+ expect(reasoningDeltas(out)).toEqual(["thinking ", "it ", "over"]);
+ expect(setTimeout).toHaveBeenCalledTimes(2);
+ });
+
+ it("flushes the reasoning buffer at once when reasoning stops being visible", async () => {
+ let isVisible = true;
+ const out: SSEEvent[] = [];
+ const events = smoothStreamEvents(
+ asAsyncIterable([
+ {
+ type: "reasoning-delta",
+ id: "r1",
+ delta: "one two three four five",
+ },
+ ]),
+ { delayInMs: 0, shouldPaceReasoning: () => isVisible },
+ );
+
+ // A user collapses the reasoning step in the middle of a delta.
+ // The test closes the reasoning after the first emitted word.
+ for await (const event of events) {
+ out.push(event);
+ isVisible = false;
+ }
+
+ // The pacer sends the rest of the buffer as one delta.
+ expect(reasoningDeltas(out)).toEqual(["one ", "two three four five"]);
+ // No wait starts after the close.
+ expect(setTimeout).not.toHaveBeenCalled();
+ });
+
+ it("starts pacing reasoning at the next delta when it becomes visible", async () => {
+ let isVisible = false;
+ async function* showDuringStream(): AsyncGenerator<SSEEvent> {
+ yield { type: "reasoning-delta", id: "r1", delta: "a b " };
+ isVisible = true;
+ yield { type: "reasoning-delta", id: "r1", delta: "c d" };
+ }
+
+ const out = await collect(
+ smoothStreamEvents(showDuringStream(), {
+ delayInMs: 0,
+ shouldPaceReasoning: () => isVisible,
+ }),
+ );
+
+ expect(reasoningDeltas(out)).toEqual(["a b ", "c ", "d"]);
+ });
+
+ it("keeps the reasoning tail in order when visibility ends", async () => {
+ let isVisible = true;
+ async function* hideDuringStream(): AsyncGenerator<SSEEvent> {
+ yield { type: "reasoning-delta", id: "r1", delta: "Think" };
+ isVisible = false;
+ yield { type: "reasoning-delta", id: "r1", delta: "ing" };
+ }
+
+ const out = await collect(
+ smoothStreamEvents(hideDuringStream(), {
+ delayInMs: 0,
+ shouldPaceReasoning: () => isVisible,
+ }),
+ );
+
+ expect(reasoningDeltas(out)).toEqual(["Think", "ing"]);
+ });
+
+ it("paces text whatever shouldPaceReasoning returns", async () => {
+ const out = await smooth(
+ [{ type: "text-delta", id: "t1", delta: "hello there world" }],
+ { shouldPaceReasoning: () => false },
+ );
+
+ expect(textDeltas(out)).toEqual(["hello ", "there ", "world"]);
+ expect(setTimeout).toHaveBeenCalledTimes(2);
+ });
+
+ it("does not pace visible reasoning while the document is hidden", async () => {
+ jest.spyOn(document, "hidden", "get").mockReturnValue(true);
+ const events: SSEEvent[] = [
+ { type: "reasoning-delta", id: "r1", delta: "thinking it over" },
+ ];
+
+ const out = await smooth(events, { shouldPaceReasoning: () => true });
+
+ expect(out).toEqual(events);
+ expect(setTimeout).not.toHaveBeenCalled();
+ });
});
- it("flushes the reasoning buffer before switching to text", async () => {
- const out = await smooth([
+ it("emits reasoning before the text that follows it", async () => {
+ const events: SSEEvent[] = [
{ type: "reasoning-delta", id: "r1", delta: "reasoned" },
{ type: "text-delta", id: "t1", delta: "answer " },
{ type: "text-end", id: "t1" },
+ ];
+
+ expect(await smooth(events)).toEqual(events);
+ });
+
+ it("flushes the buffered text tail before a reasoning delta", async () => {
+ const out = await smooth([
+ { type: "text-delta", id: "t1", delta: "done " },
+ { type: "text-delta", id: "t1", delta: "tail" },
+ { type: "reasoning-delta", id: "r1", delta: "why" },
]);
- expect(deltasOfType(out, "reasoning-delta")).toEqual(["reasoned"]);
- expect(textDeltas(out)).toEqual(["answer "]);
+ expect(out).toEqual([
+ { type: "text-delta", id: "t1", delta: "done " },
+ { type: "text-delta", id: "t1", delta: "tail" },
+ { type: "reasoning-delta", id: "r1", delta: "why" },
+ ]);
});
});
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/MetabotChainOfThought.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/MetabotChainOfThought.tsx
index 55e85da881d..901d9e54916 100644
--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/MetabotChainOfThought.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/MetabotChainOfThought.tsx
@@ -25,11 +25,13 @@ import {
} from "./utils";
type MetabotChainOfThoughtProps = {
+ conversationId: string;
part: MetabotAgentChainOfThoughtMessage;
supportsReasoning?: boolean;
};
export const MetabotChainOfThought = ({
+ conversationId,
part,
supportsReasoning = true,
}: MetabotChainOfThoughtProps) => {
@@ -39,12 +41,13 @@ export const MetabotChainOfThought = ({
return (
<>
{!part.finished && <MetabotResponseLoader />}
- <ChainOfThought part={part} />
+ <ChainOfThought conversationId={conversationId} part={part} />
</>
);
};
const ChainOfThought = ({
+ conversationId,
part,
}: Omit<MetabotChainOfThoughtProps, "supportsReasoning">) => {
const isStreaming = !part.finished;
@@ -146,9 +149,11 @@ const ChainOfThought = ({
return (
<div key={`reasoning-${index}`} className={S.step}>
<ReasoningStep
+ conversationId={conversationId}
text={step.text}
label={active ? t`Thinking` : stepReasoningLabel(index)}
active={active}
+ shown={open}
/>
</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
index f2d36dff0f6..6f547abd234 100644
--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/MetabotChainOfThought.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/MetabotChainOfThought.unit.spec.tsx
@@ -4,12 +4,17 @@ import fetchMock from "fetch-mock";
import { setupEnterprisePlugins } from "__support__/enterprise";
import { mockSettings } from "__support__/settings";
import { createMockState } from "__support__/state";
-import { renderWithProviders, screen } from "__support__/ui";
+import { renderWithProviders, screen, waitFor } from "__support__/ui";
import type { MetabotAgentChainOfThoughtMessage } from "metabase/metabot/state";
+import { isReasoningVisible } from "metabase/metabot/utils/reasoning-visibility";
+import { checkNotNull } from "metabase/utils/types";
import { createMockDashboard } from "metabase-types/api/mocks";
import { MetabotChainOfThought } from "./MetabotChainOfThought";
+const TEST_CONVERSATION_ID = "c1";
+const OTHER_CONVERSATION_ID = "c2";
+
const chain = (
overrides: Partial<MetabotAgentChainOfThoughtMessage>,
): MetabotAgentChainOfThoughtMessage => ({
@@ -32,7 +37,10 @@ const setup = (
createMockDashboard({ id: 123, name: "Orders" }),
);
return renderWithProviders(
- <MetabotChainOfThought part={{ ...message, finished: !isStreaming }} />,
+ <MetabotChainOfThought
+ part={{ ...message, finished: !isStreaming }}
+ conversationId={TEST_CONVERSATION_ID}
+ />,
{ storeInitialState: createMockState({ settings }) },
);
};
@@ -41,6 +49,19 @@ const expandChain = async () => {
await userEvent.click(screen.getByTestId("metabot-chain-of-thought-header"));
};
+const toggleReasoningStep = async (label: RegExp) => {
+ const header = screen.getByTestId("metabot-chain-of-thought-header");
+ const stepToggle = await waitFor(() =>
+ checkNotNull(
+ screen
+ .getAllByRole("button", { name: label })
+ .find((button) => button !== header),
+ ),
+ );
+ await userEvent.click(stepToggle);
+ return stepToggle;
+};
+
afterEach(() => {
delete window.overrideIsWithinIframe;
});
@@ -646,4 +667,56 @@ describe("MetabotChainOfThought", () => {
await userEvent.click(screen.getByText("1 result"));
expect(screen.getByText("Sample DB", { exact: false })).toBeInTheDocument();
});
+
+ describe("reasoning visibility", () => {
+ it("marks reasoning visible only while the header and the active step are open", async () => {
+ const message = chain({
+ steps: [{ kind: "reasoning", text: "Weighing the join order" }],
+ });
+ const { rerender, unmount } = setup(message, true);
+ expect(isReasoningVisible(TEST_CONVERSATION_ID)).toBe(false);
+
+ await expandChain();
+ expect(isReasoningVisible(TEST_CONVERSATION_ID)).toBe(false);
+
+ await toggleReasoningStep(/Thinking/);
+ expect(isReasoningVisible(TEST_CONVERSATION_ID)).toBe(true);
+ expect(isReasoningVisible(OTHER_CONVERSATION_ID)).toBe(false);
+
+ await expandChain();
+ expect(isReasoningVisible(TEST_CONVERSATION_ID)).toBe(false);
+
+ await expandChain();
+ expect(isReasoningVisible(TEST_CONVERSATION_ID)).toBe(true);
+
+ rerender(
+ <MetabotChainOfThought
+ part={{ ...message, finished: true }}
+ conversationId={TEST_CONVERSATION_ID}
+ />,
+ );
+ expect(isReasoningVisible(TEST_CONVERSATION_ID)).toBe(false);
+
+ unmount();
+ expect(isReasoningVisible(TEST_CONVERSATION_ID)).toBe(false);
+ });
+
+ it("does not mark an inactive reasoning step visible", async () => {
+ setup(
+ chain({
+ steps: [
+ { kind: "reasoning", text: "First thought" },
+ { kind: "tool", id: "t1", name: "analyze_data", status: "ended" },
+ { kind: "reasoning", text: "Second thought" },
+ ],
+ }),
+ true,
+ );
+
+ await expandChain();
+ const firstStep = await toggleReasoningStep(/Thought briefly/);
+ expect(firstStep).toHaveAttribute("aria-expanded", "true");
+ expect(isReasoningVisible(TEST_CONVERSATION_ID)).toBe(false);
+ });
+ });
});
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/ReasoningStep.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/ReasoningStep.tsx
index 11e1b5a3379..4221979cfe2 100644
--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/ReasoningStep.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChainOfThought/ReasoningStep.tsx
@@ -3,20 +3,28 @@ import { useState } from "react";
import Animation from "metabase/css/core/animation.module.css";
import { AIMarkdown } from "metabase/metabot/components/AIMarkdown";
+import { useReasoningVisibility } from "metabase/metabot/utils/reasoning-visibility";
import { Collapse, Icon, Text, UnstyledButton } from "metabase/ui";
import S from "./MetabotChainOfThought.module.css";
export const ReasoningStep = ({
+ conversationId,
text,
label,
active,
+ shown,
}: {
+ conversationId: string;
text: string;
label: string;
active: boolean;
+ // Whether the chain of thought around this step is open.
+ shown: boolean;
}) => {
const [open, setOpen] = useState(false);
+ // The stream paces reasoning only while the user can read it.
+ useReasoningVisibility(conversationId, shown && open && active);
return (
<div className={S.toolStep}>
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx
index d3c185881c9..c0b09963e04 100644
--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx
@@ -255,7 +255,11 @@ const AgentPart = ({
<AgentToolCallPart part={p} onSelect={onToolCallSelect} />
))
.with({ type: "chain_of_thought" }, (p) => (
- <MetabotChainOfThought part={p} supportsReasoning={supportsReasoning} />
+ <MetabotChainOfThought
+ conversationId={conversationId}
+ part={p}
+ supportsReasoning={supportsReasoning}
+ />
))
.exhaustive();
diff --git a/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.unit.spec.tsx b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.unit.spec.tsx
index 46de1a5ff12..9de8ad563a0 100644
--- a/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.unit.spec.tsx
@@ -1,6 +1,7 @@
jest.unmock("@uiw/react-codemirror");
import userEvent from "@testing-library/user-event";
+import type { ReactElement } from "react";
import { setupEnterprisePlugins } from "__support__/enterprise";
import { createMockMetadataFromState } from "__support__/metadata";
@@ -15,6 +16,7 @@ import { createMockState } from "__support__/state";
import { createMockEntitiesState } from "__support__/store";
import { renderWithProviders, screen, waitFor, within } from "__support__/ui";
import type { SSEEvent } from "metabase/api/ai-streaming/sse-types";
+import { MetabotChainOfThought } from "metabase/metabot/components/MetabotChat/MetabotChainOfThought";
import { MetabotProvider } from "metabase/metabot/context";
import {
erroredResponse,
@@ -57,9 +59,43 @@ const CODE_EDIT_RESPONSE: SSEEvent[] = [
},
];
+// At 10 ms per paced word, this reasoning takes at least 15 s to replay.
+const LONG_REASONING_WORD_COUNT = 1500;
+// Keep it at most a third of the replay time above, and below the 30 s jest timeout.
+const SQL_ARRIVAL_TIMEOUT_MS = 5000;
+
+const LONG_REASONING_THEN_CODE_EDIT_RESPONSE: SSEEvent[] = [
+ { type: "reasoning-start", id: "r1" },
+ {
+ type: "reasoning-delta",
+ id: "r1",
+ delta: "word ".repeat(LONG_REASONING_WORD_COUNT),
+ },
+ { type: "reasoning-end", id: "r1" },
+ ...CODE_EDIT_RESPONSE,
+];
+
+const OTHER_CONVERSATION_REASONING = (
+ <MetabotChainOfThought
+ conversationId="other-conversation"
+ part={{
+ id: "chain-1",
+ role: "agent",
+ type: "chain_of_thought",
+ steps: [{ kind: "reasoning", text: "Weighing the join order" }],
+ finished: false,
+ }}
+ />
+);
+
function TestEditor({ question }: { question: Question }) {
- const { extensions, portalElement, togglePrompt, isPromptOpen } =
- useInlineSQLPrompt(question, "qb");
+ const {
+ extensions,
+ portalElement,
+ togglePrompt,
+ isPromptOpen,
+ proposedQuestion,
+ } = useInlineSQLPrompt(question, "qb");
const query = checkNotNull(question.legacyNativeQuery());
return (
@@ -84,11 +120,19 @@ function TestEditor({ question }: { question: Question }) {
</NativeQueryEditor.TopBar>
</NativeQueryEditor>
{portalElement}
+ <output data-testid="proposed-sql">
+ {proposedQuestion?.legacyNativeQuery()?.queryText()}
+ </output>
</>
);
}
-function setup({ isMetabotEnabled = true } = {}) {
+interface SetupOpts {
+ isMetabotEnabled?: boolean;
+ otherContent?: ReactElement;
+}
+
+function setup({ isMetabotEnabled = true, otherContent }: SetupOpts = {}) {
setupEnterprisePlugins();
setupDatabasesEndpoints([TEST_DB]);
setupUserMetabotPermissionsEndpoint();
@@ -113,11 +157,26 @@ function setup({ isMetabotEnabled = true } = {}) {
renderWithProviders(
<MetabotProvider>
<TestEditor question={question} />
+ {otherContent}
</MetabotProvider>,
{ storeInitialState: state },
);
}
+async function openOtherConversationReasoning() {
+ const header = await screen.findByTestId("metabot-chain-of-thought-header");
+ await userEvent.click(header);
+ const stepToggle = await waitFor(() =>
+ checkNotNull(
+ screen
+ .getAllByRole("button", { name: /Thinking/ })
+ .find((button) => button !== header),
+ ),
+ );
+ await userEvent.click(stepToggle);
+ expect(stepToggle).toHaveAttribute("aria-expanded", "true");
+}
+
async function startGenerating() {
expect(await screen.findByText("SELECT")).toBeInTheDocument();
await userEvent.click(await screen.findByLabelText("Ask Metabot"));
@@ -132,6 +191,19 @@ async function startGenerating() {
return prompt;
}
+async function expectProposedSqlSoon() {
+ await waitFor(
+ () => {
+ expect(
+ screen.queryByTestId("metabot-inline-sql-prompt"),
+ ).not.toBeInTheDocument();
+ expect(screen.queryByText("Working on it...")).not.toBeInTheDocument();
+ expect(screen.getByTestId("proposed-sql")).toHaveTextContent("SELECT 2");
+ },
+ { timeout: SQL_ARRIVAL_TIMEOUT_MS },
+ );
+}
+
describe("useInlineSQLPrompt", () => {
it("toggles the inline prompt open and closed", async () => {
setup();
@@ -184,6 +256,34 @@ describe("useInlineSQLPrompt", () => {
});
});
+ // https://github.com/metabase/metabase/issues/84969
+ it("shows the SQL soon after a stream with long reasoning", async () => {
+ const agent = mockAgentEndpoint({
+ events: LONG_REASONING_THEN_CODE_EDIT_RESPONSE,
+ waitForResponse: true,
+ });
+ setup();
+
+ await startGenerating();
+
+ agent.sendResponse();
+ await expectProposedSqlSoon();
+ });
+
+ it("keeps the SQL fast while another conversation shows open reasoning", async () => {
+ const agent = mockAgentEndpoint({
+ events: LONG_REASONING_THEN_CODE_EDIT_RESPONSE,
+ waitForResponse: true,
+ });
+ setup({ otherContent: OTHER_CONVERSATION_REASONING });
+
+ await openOtherConversationReasoning();
+ await startGenerating();
+
+ agent.sendResponse();
+ await expectProposedSqlSoon();
+ });
+
it("replaces the working status with the error when generation fails", async () => {
const agent = mockAgentEndpoint({
events: erroredResponse,
diff --git a/frontend/src/metabase/metabot/state/actions.ts b/frontend/src/metabase/metabot/state/actions.ts
index b63d3e3dc53..57f9cf2d5b8 100644
--- a/frontend/src/metabase/metabot/state/actions.ts
+++ b/frontend/src/metabase/metabot/state/actions.ts
@@ -40,6 +40,7 @@ import {
isHistoryEnabledProfile,
} from "../constants";
import { PLUGIN_METABOT_SLASH_COMMANDS } from "../plugins";
+import { isReasoningVisible } from "../utils/reasoning-visibility";
import { metabot } from "./reducer";
import {
@@ -524,6 +525,7 @@ export const sendAgentRequest = createAsyncThunk<
sourceId: conversationId,
},
{
+ shouldPaceReasoning: () => isReasoningVisible(conversationId),
onDataPart: function handleDataPart(part) {
const pushDataPart = (
message: Omit<MetabotAgentDataPartMessage, "id" | "role">,
diff --git a/frontend/src/metabase/metabot/tests/message.unit.spec.tsx b/frontend/src/metabase/metabot/tests/message.unit.spec.tsx
index ed5efe1b709..999341f7e33 100644
--- a/frontend/src/metabase/metabot/tests/message.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/tests/message.unit.spec.tsx
@@ -1,7 +1,8 @@
import userEvent from "@testing-library/user-event";
-import { screen } from "__support__/ui";
+import { screen, waitFor } from "__support__/ui";
import type { SSEEvent } from "metabase/api/ai-streaming/sse-types";
+import { checkNotNull } from "metabase/utils/types";
import {
assertConversation,
@@ -19,6 +20,63 @@ import {
whoIsYourFavoriteResponse,
} from "./utils";
+// At 10 ms per paced word, this reasoning takes at least 15 s to replay.
+const LONG_REASONING_WORD_COUNT = 1500;
+// Keep it at most a third of the replay time above, and below the 30 s jest timeout.
+const ANSWER_ARRIVAL_TIMEOUT_MS = 5000;
+// At 10 ms per paced word, this reasoning takes about 2 s to replay.
+const OPEN_REASONING_WORD_COUNT = 200;
+// Well above the replay time above, so the paced stream ends inside the test.
+const OPEN_ANSWER_ARRIVAL_TIMEOUT_MS = 8000;
+
+const ANSWER_TEXT = "Here you go.";
+
+// `setup` forces reduced motion, which skips the pacer. These tests need the
+// pacer, so they allow motion.
+const allowMotion = () => {
+ const reducedMotion = window.matchMedia;
+ window.matchMedia = (query: string) => ({
+ matches: false,
+ media: query,
+ onchange: null,
+ addEventListener: () => {},
+ removeEventListener: () => {},
+ addListener: () => {},
+ removeListener: () => {},
+ dispatchEvent: () => false,
+ });
+ return () => {
+ window.matchMedia = reducedMotion;
+ };
+};
+
+const openChainAndReasoningStep = async () => {
+ const header = await screen.findByTestId("metabot-chain-of-thought-header");
+ await userEvent.click(header);
+ const stepToggle = await waitFor(() =>
+ checkNotNull(
+ screen
+ .getAllByRole("button", { name: /Thinking/ })
+ .find((button) => button !== header),
+ ),
+ );
+ await userEvent.click(stepToggle);
+ expect(stepToggle).toHaveAttribute("aria-expanded", "true");
+};
+
+// Streamed markdown splits words into separate elements, so match the chat text.
+const expectAnswerWithin = (timeout: number) =>
+ waitFor(
+ () => {
+ expect(screen.getByTestId("metabot-chat")).toHaveTextContent(ANSWER_TEXT);
+ },
+ { timeout },
+ );
+
+const renderedWordCount = () =>
+ screen.getByTestId("metabot-chain-of-thought").textContent?.match(/\bword\b/g)
+ ?.length ?? 0;
+
describe("metabot > message", () => {
it("should properly send chat messages", async () => {
setup();
@@ -110,6 +168,74 @@ describe("metabot > message", () => {
["agent", "You, but don't tell anyone."],
]);
});
+
+ describe("reasoning pacing", () => {
+ let restoreMotion = () => {};
+
+ afterEach(() => {
+ restoreMotion();
+ });
+
+ // https://github.com/metabase/metabase/issues/84969
+ it("shows the answer soon after a stream with long collapsed reasoning", async () => {
+ setup();
+ restoreMotion = allowMotion();
+ mockAgentEndpoint({
+ events: [
+ { type: "reasoning-start", id: "r1" },
+ {
+ type: "reasoning-delta",
+ id: "r1",
+ delta: "word ".repeat(LONG_REASONING_WORD_COUNT),
+ },
+ { type: "reasoning-end", id: "r1" },
+ { type: "text-delta", id: "t1", delta: ANSWER_TEXT },
+ { type: "finish", finishReason: "stop" },
+ ],
+ });
+
+ await enterChatMessage("Question");
+
+ await expectAnswerWithin(ANSWER_ARRIVAL_TIMEOUT_MS);
+ });
+
+ it("paces reasoning while the user has it open", async () => {
+ setup();
+ restoreMotion = allowMotion();
+ const [pause] = createPauses(1);
+ mockAgentEndpoint({
+ stream: createMockSSEStream(
+ (async function* () {
+ yield { type: "reasoning-start", id: "r1" };
+ yield { type: "reasoning-delta", id: "r1", delta: "start " };
+ await pause.promise;
+ yield {
+ type: "reasoning-delta",
+ id: "r1",
+ delta: "word ".repeat(OPEN_REASONING_WORD_COUNT),
+ };
+ yield { type: "reasoning-end", id: "r1" };
+ yield { type: "text-delta", id: "t1", delta: ANSWER_TEXT };
+ yield { type: "finish", finishReason: "stop" };
+ })(),
+ ),
+ });
+
+ await enterChatMessage("Question");
+ await openChainAndReasoningStep();
+ pause.resolve();
+
+ // Wait for the first words, outside `act`. An `act` scope returns only when
+ // React has no more work, and a paced stream gives it work every 10 ms. On a
+ // slow machine it then returns only after the last word.
+ await waitFor(() => {
+ expect(renderedWordCount()).toBeGreaterThan(0);
+ });
+ expect(renderedWordCount()).toBeLessThan(OPEN_REASONING_WORD_COUNT);
+
+ await expectAnswerWithin(OPEN_ANSWER_ARRIVAL_TIMEOUT_MS);
+ });
+ });
});
const incompleteResponse = (
diff --git a/frontend/src/metabase/metabot/utils/reasoning-visibility.ts b/frontend/src/metabase/metabot/utils/reasoning-visibility.ts
new file mode 100644
index 00000000000..45ec791f4f5
--- /dev/null
+++ b/frontend/src/metabase/metabot/utils/reasoning-visibility.ts
@@ -0,0 +1,46 @@
+import { useEffect } from "react";
+
+// A count, not a set. Two views of one conversation can show its reasoning at
+// once. The cleanup of one view must not clear the other.
+const visibleStepCounts = new Map<string, number>();
+
+/**
+ * Returns true while at least one open, active reasoning step of the
+ * conversation is on the screen.
+ */
+export function isReasoningVisible(conversationId: string): boolean {
+ return (visibleStepCounts.get(conversationId) ?? 0) > 0;
+}
+
+function addVisibleStep(conversationId: string) {
+ visibleStepCounts.set(
+ conversationId,
+ (visibleStepCounts.get(conversationId) ?? 0) + 1,
+ );
+}
+
+function removeVisibleStep(conversationId: string) {
+ const count = (visibleStepCounts.get(conversationId) ?? 0) - 1;
+ if (count > 0) {
+ visibleStepCounts.set(conversationId, count);
+ } else {
+ visibleStepCounts.delete(conversationId);
+ }
+}
+
+/**
+ * Marks the reasoning of the conversation as visible while `visible` is true
+ * and the calling component is mounted.
+ */
+export function useReasoningVisibility(
+ conversationId: string,
+ visible: boolean,
+) {
+ useEffect(() => {
+ if (!visible) {
+ return;
+ }
+ addVisibleStep(conversationId);
+ return () => removeVisibleStep(conversationId);
+ }, [conversationId, visible]);
+}