Skip to content

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

[Metabot] Animated markdown streaming (#78046)

Sloan Sparger428c6be07075
diff --git a/bun.lock b/bun.lock
index c1c0ceec52f..81634b98daa 100644
--- a/bun.lock
+++ b/bun.lock
@@ -118,6 +118,7 @@
"leaflet-draw": "^0.4.9",
"lodash.debounce": "^4.0.8",
"lodash.orderby": "^4.6.0",
+ "marked": "18.0.6",
"merge-refs": "^1.3.0",
"mustache": "^2.3.2",
"normalizr": "^3.0.2",
@@ -153,6 +154,7 @@
"redux-undo": "^1.1.0",
"rehype-external-links": "^3.0.0",
"remark-gfm": "^4.0.0",
+ "remend": "1.3.0",
"reselect": "^5.1.0",
"resize-observer-polyfill": "^1.5.1",
"server-text-width": "^1.0.2",
@@ -4019,7 +4021,7 @@
"markdown-table": ["[email protected]", "", {}, "sha512-wiYz4+JrLyb/DqW2hkFJxP7Vd7JuTDm77fvbM8VfEQdmSMqcImWeeRbHwZjBjIFki/VaMK2BhFi7oUUZeM5bqw=="],
- "marked": ["[email protected]", "", { "bin": { "marked": "bin/marked.js" } }, "sha512-PRsaiG84bK+AMvxziE/lCFss8juXjNaWzVbN5tXAm4XjeaS9NAHhop+PjQxz2A9h8Q4M/xGmzP8vqNwy6JeK0A=="],
+ "marked": ["[email protected]", "", { "bin": { "marked": "bin/marked.js" } }, "sha512-MrV5puXBfuiy6wl6DLaq3BtIJQAJToAd5zt/ZKhRfGRAuFPALE7/4Y7jnxRQoEgK/pBgurGqLyAuRgZ2xOjr6w=="],
"marky": ["[email protected]", "", {}, "sha512-q9JtQJKjpsVxCRVgQ+WapguSbKC3SQ5HEzFGPAJMStgh3QjCawp00UKv3MTTAArTmGmmPUvllHZoNbZ3gs0I+Q=="],
@@ -4877,6 +4879,8 @@
"remedial": ["[email protected]", "", {}, "sha512-/62tYiOe6DzS5BqVsNpH/nkGlX45C/Sp6V+NtiN6JQNS1Viay7cWkazmRkrQrdFj2eshDe96SIQNIoMxqhzBOg=="],
+ "remend": ["[email protected]", "", {}, "sha512-iIhggPkhW3hFImKtB10w0dz4EZbs28mV/dmbcYVonWEJ6UGHHpP+bFZnTh6GNWJONg5m+U56JrL+8IxZRdgWjw=="],
+
"remove-trailing-spaces": ["[email protected]", "", {}, "sha512-O3vsMYfWighyFbTd8hk8VaSj9UAGENxAtX+//ugIst2RMk5e03h6RoIS+0ylsFxY1gvmPuAY/PO4It+gPEeySA=="],
"renderkid": ["[email protected]", "", { "dependencies": { "css-select": "^4.1.3", "dom-converter": "^0.2.0", "htmlparser2": "^6.1.0", "lodash": "^4.17.21", "strip-ansi": "^6.0.1" } }, "sha512-q/7VIQA8lmM1hF+jn+sFSPWGlMkSAeNYcPLmDQx2zzuiDfaLrOmumR8iaUKlenFgh0XRPIUeSPlH3A+AW3Z5pg=="],
@@ -7067,6 +7071,8 @@
"redoc/classnames": ["[email protected]", "", {}, "sha512-saHYOzhIQs6wy2sVxTM6bUDsQO4F50V9RQ22qBpEdCW+I+/Wmke2HOl6lS6dTpdxVhb88/I6+Hs+438c3lfUow=="],
+ "redoc/marked": ["[email protected]", "", { "bin": { "marked": "bin/marked.js" } }, "sha512-PRsaiG84bK+AMvxziE/lCFss8juXjNaWzVbN5tXAm4XjeaS9NAHhop+PjQxz2A9h8Q4M/xGmzP8vqNwy6JeK0A=="],
+
"reduce-css-calc/balanced-match": ["[email protected]", "", {}, "sha512-STw03mQKnGUYtoNjmowo4F2cRmIIxYEGiMsjjwla/u5P1lxadj/05WkNaFjNiKTgJkj8KiXbgAiRTmcQRwQNtg=="],
"reduce-function-call/balanced-match": ["[email protected]", "", {}, "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="],
diff --git a/frontend/src/metabase/api/ai-streaming/process-stream.ts b/frontend/src/metabase/api/ai-streaming/process-stream.ts
index 10fabcc5bf1..76d6236a81b 100644
--- a/frontend/src/metabase/api/ai-streaming/process-stream.ts
+++ b/frontend/src/metabase/api/ai-streaming/process-stream.ts
@@ -1,5 +1,7 @@
import { P, isMatching, match } from "ts-pattern";
+import { isReducedMotionPreferred } from "metabase/utils/dom";
+
import {
type KnownDataPart,
dataEventSchema,
@@ -8,6 +10,7 @@ import {
toolOutputAvailableSchema,
toolOutputErrorSchema,
} from "./schemas";
+import { smoothTextEvents } from "./smooth-stream";
import { parseSSEStream } from "./sse-stream";
import type {
FinishReason,
@@ -81,8 +84,11 @@ export async function processChatResponse(
data: [],
};
+ const events = parseSSEStream(stream);
+ const paced = isReducedMotionPreferred() ? events : smoothTextEvents(events);
+
try {
- for await (const event of parseSSEStream(stream)) {
+ for await (const event of paced) {
match(event)
.with({ type: "start" }, (e) => config.onStart?.(e))
.with({ type: "text-delta" }, (e) => config.onTextPart?.(e.delta))
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 08f08399e9e..3bf1c9a993f 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
@@ -186,18 +186,24 @@ describe("processChatResponse", () => {
).rejects.toBeTruthy();
});
- it("should forward each text delta to onTextPart", async () => {
+ it("should smooth text deltas word by word", async () => {
const config = getMockedCallbacks();
- const mockStream = createMockSSEStream([
- { type: "text-start", id: "t1" },
- { type: "text-delta", id: "t1", delta: "You, but " },
- { type: "text-delta", id: "t1", delta: "don't tell anyone." },
- { type: "text-end", id: "t1" },
- ]);
+ await processChatResponse(
+ createMockSSEStream([
+ { type: "text-start", id: "t1" },
+ { type: "text-delta", id: "t1", delta: "You, but don't tell anyone." },
+ { type: "text-end", id: "t1" },
+ ]),
+ config,
+ );
- await processChatResponse(mockStream, config);
- expect(config.onTextPart).toHaveBeenNthCalledWith(1, "You, but ");
- expect(config.onTextPart).toHaveBeenNthCalledWith(2, "don't tell anyone.");
+ expect(config.onTextPart.mock.calls.map(([delta]) => delta)).toEqual([
+ "You, ",
+ "but ",
+ "don't ",
+ "tell ",
+ "anyone.",
+ ]);
});
it("should resolve with partial response for aborted requests", async () => {
diff --git a/frontend/src/metabase/api/ai-streaming/smooth-stream.ts b/frontend/src/metabase/api/ai-streaming/smooth-stream.ts
new file mode 100644
index 00000000000..3a50823c888
--- /dev/null
+++ b/frontend/src/metabase/api/ai-streaming/smooth-stream.ts
@@ -0,0 +1,61 @@
+import type { SSEEvent, TextDeltaEvent } from "./sse-types";
+
+// Whitespace-terminated, so a word only emits once it's complete.
+const WORD_PATTERN = /\S+\s+/m;
+
+const DEFAULT_SMOOTHING_DELAY_MS = 10;
+
+const wait = (ms: number) =>
+ new Promise<void>((resolve) => setTimeout(resolve, ms));
+
+const takeWord = (buffer: string) => {
+ const match = WORD_PATTERN.exec(buffer);
+ return match ? buffer.slice(0, match.index + match[0].length) : null;
+};
+
+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(
+ events: AsyncIterable<SSEEvent>,
+ { delayInMs = DEFAULT_SMOOTHING_DELAY_MS }: SmoothTextOptions = {},
+): AsyncGenerator<SSEEvent> {
+ let buffer = "";
+ let pending: TextDeltaEvent | null = null;
+
+ function* flush() {
+ if (pending && buffer.length > 0) {
+ yield { ...pending, delta: buffer };
+ buffer = "";
+ }
+ }
+
+ for await (const event of events) {
+ if (event.type !== "text-delta") {
+ yield* flush();
+ yield event;
+ continue;
+ }
+
+ // A different text block can't share this buffer.
+ if (pending && pending.id !== event.id) {
+ yield* flush();
+ }
+
+ buffer += event.delta;
+ pending = event;
+
+ let word = takeWord(buffer);
+ while (word !== null) {
+ yield { ...pending, delta: word };
+ buffer = buffer.slice(word.length);
+ await wait(delayInMs);
+ word = takeWord(buffer);
+ }
+ }
+
+ 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
new file mode 100644
index 00000000000..ed9a20ca031
--- /dev/null
+++ b/frontend/src/metabase/api/ai-streaming/smooth-stream.unit.spec.ts
@@ -0,0 +1,58 @@
+import { smoothTextEvents } from "./smooth-stream";
+import type { SSEEvent } from "./sse-types";
+
+async function* asAsyncIterable(events: SSEEvent[]) {
+ for (const event of events) {
+ yield event;
+ }
+}
+
+const smooth = async (events: SSEEvent[]) => {
+ const out: SSEEvent[] = [];
+ for await (const event of smoothTextEvents(asAsyncIterable(events), {
+ delayInMs: 0,
+ })) {
+ out.push(event);
+ }
+ return out;
+};
+
+const textDeltas = (events: SSEEvent[]) =>
+ events.flatMap((event) => (event.type === "text-delta" ? [event.delta] : []));
+
+describe("smoothTextEvents", () => {
+ it("emits buffered text one word at a time", async () => {
+ const out = await smooth([
+ { type: "text-start", id: "t1" },
+ { type: "text-delta", id: "t1", delta: "hello there world" },
+ { type: "text-end", id: "t1" },
+ ]);
+
+ expect(textDeltas(out)).toEqual(["hello ", "there ", "world"]);
+ });
+
+ it("flushes the buffered tail before a non-text event", async () => {
+ const out = await smooth([
+ { type: "text-delta", id: "t1", delta: "done " },
+ { type: "text-delta", id: "t1", delta: "tail" },
+ { type: "data-state", data: {} },
+ ]);
+
+ expect(textDeltas(out)).toEqual(["done ", "tail"]);
+ expect(out.at(-1)).toEqual({ type: "data-state", data: {} });
+ });
+
+ it("flushes when a delta from a new text id arrives", async () => {
+ const out = await smooth([
+ { type: "text-delta", id: "t1", delta: "first" },
+ { type: "text-delta", id: "t2", delta: "second " },
+ { type: "text-end", id: "t2" },
+ ]);
+
+ expect(out).toEqual([
+ { type: "text-delta", id: "t1", delta: "first" },
+ { type: "text-delta", id: "t2", delta: "second " },
+ { type: "text-end", id: "t2" },
+ ]);
+ });
+});
diff --git a/frontend/src/metabase/common/components/Markdown/Markdown.module.css b/frontend/src/metabase/common/components/Markdown/Markdown.module.css
new file mode 100644
index 00000000000..28e0ae33b0d
--- /dev/null
+++ b/frontend/src/metabase/common/components/Markdown/Markdown.module.css
@@ -0,0 +1,53 @@
+.markdownRoot {
+ color: var(--markdown-color, inherit);
+}
+
+.lineClamp {
+ display: -webkit-box;
+ -webkit-line-clamp: var(--markdown-line-clamp);
+ -webkit-box-orient: vertical;
+ overflow: hidden;
+ overflow-wrap: break-word;
+ white-space: pre-line;
+}
+
+.markdownRoot p {
+ margin: 0;
+ line-height: 1.57em;
+}
+
+.markdownRoot p:not(:last-child) {
+ margin-bottom: var(--mantine-spacing-sm);
+}
+
+.markdownRoot a {
+ cursor: pointer;
+ text-decoration: none;
+ color: var(--mb-color-core-brand);
+}
+
+.markdownRoot a:hover {
+ text-decoration: underline;
+}
+
+.markdownRoot[data-unstyle-links] a {
+ color: var(--mb-color-text-primary-inverse);
+}
+
+.markdownRoot[data-unstyle-links] a:hover {
+ text-decoration: none;
+}
+
+.markdownRoot img {
+ max-width: 100%;
+ height: auto;
+}
+
+.markdownRoot hr {
+ border: none;
+ border-bottom: 1px solid var(--mb-color-border-neutral);
+}
+
+.markdownRoot[data-dark] hr {
+ border-bottom-color: var(--mb-color-background_page-tertiary-inverse);
+}
diff --git a/frontend/src/metabase/common/components/Markdown/Markdown.styled.tsx b/frontend/src/metabase/common/components/Markdown/Markdown.styled.tsx
deleted file mode 100644
index 9e882769918..00000000000
--- a/frontend/src/metabase/common/components/Markdown/Markdown.styled.tsx
+++ /dev/null
@@ -1,62 +0,0 @@
-// eslint-disable-next-line no-restricted-imports
-import { css } from "@emotion/react";
-// eslint-disable-next-line no-restricted-imports
-import styled from "@emotion/styled";
-import type { FC, ReactElement } from "react";
-import ReactMarkdown from "react-markdown";
-
-import { color } from "metabase/ui/utils/colors";
-
-import type { MarkdownProps } from "./Markdown";
-
-export const MarkdownRoot = styled(getComponent(ReactMarkdown))<MarkdownProps>`
- color: ${(props) => (props.c ? color(props.c) : "inherit")};
- ${(props) =>
- props.lineClamp &&
- css`
- display: -webkit-box;
- -webkit-line-clamp: ${props.lineClamp};
- -webkit-box-orient: vertical;
- overflow: hidden;
- overflow-wrap: break-word;
- white-space: pre-line;
- `}
-
- p {
- margin: 0;
- line-height: 1.57em;
- }
-
- p:not(:last-child) {
- margin-bottom: 0.5rem;
- }
-
- a {
- cursor: pointer;
- text-decoration: none;
- color: ${(props) =>
- props.unstyleLinks ? color("text-primary-inverse") : color("core-brand")};
- }
-
- a:hover {
- text-decoration: ${(props) => (props.unstyleLinks ? "none" : "underline")};
- }
-
- img {
- max-width: 100%;
- height: auto;
- }
-
- hr {
- border: none;
- border-bottom: 1px solid
- ${(props) =>
- props.dark
- ? "var(--mb-color-background_page-tertiary-inverse)"
- : "var(--mb-color-border-neutral)"};
- }
-`;
-
-function getComponent<P>(component: (props: P) => ReactElement): FC<P> {
- return component;
-}
diff --git a/frontend/src/metabase/common/components/Markdown/Markdown.tsx b/frontend/src/metabase/common/components/Markdown/Markdown.tsx
index a647a2f6a92..b102bab7f51 100644
--- a/frontend/src/metabase/common/components/Markdown/Markdown.tsx
+++ b/frontend/src/metabase/common/components/Markdown/Markdown.tsx
@@ -1,13 +1,18 @@
-import type { AnchorHTMLAttributes, ComponentPropsWithRef } from "react";
+import cx from "classnames";
+import type {
+ AnchorHTMLAttributes,
+ CSSProperties,
+ ComponentPropsWithRef,
+} from "react";
import { useMemo } from "react";
-import type ReactMarkdown from "react-markdown";
-import { defaultUrlTransform } from "react-markdown";
+import ReactMarkdown, { defaultUrlTransform } from "react-markdown";
import remarkGfm from "remark-gfm";
import type { ColorName } from "metabase/ui/colors/types";
+import { color } from "metabase/ui/utils/colors";
import { DATA_IMAGE_URI_PATTERN } from "metabase/visualizations/lib/utils";
-import { MarkdownRoot } from "./Markdown.styled";
+import S from "./Markdown.module.css";
const REMARK_PLUGINS = [remarkGfm];
@@ -15,6 +20,11 @@ const MarkdownLink = (props: AnchorHTMLAttributes<HTMLAnchorElement>) => (
<a {...props} target="_blank" rel="noopener noreferrer" />
);
+type MarkdownCssVariables = CSSProperties & {
+ "--markdown-color"?: string;
+ "--markdown-line-clamp"?: number;
+};
+
function urlTransform(url: string): string {
if (url.startsWith("metabase://")) {
return url;
@@ -45,6 +55,7 @@ export const Markdown = ({
disallowHeading = false,
unstyleLinks = false,
c,
+ lineClamp,
components,
...rest
}: MarkdownProps): JSX.Element => {
@@ -60,19 +71,27 @@ export const Markdown = ({
[components],
);
+ const style: MarkdownCssVariables = {
+ "--markdown-color": c ? color(c) : undefined,
+ "--markdown-line-clamp": lineClamp,
+ };
+
return (
- <MarkdownRoot
- className={className}
- dark={dark}
- remarkPlugins={REMARK_PLUGINS}
- urlTransform={urlTransform}
- unstyleLinks={unstyleLinks}
- c={c}
- components={customizedComponents}
- {...additionalOptions}
- {...rest}
+ <div
+ className={cx(S.markdownRoot, { [S.lineClamp]: lineClamp }, className)}
+ style={style}
+ data-dark={dark || undefined}
+ data-unstyle-links={unstyleLinks || undefined}
>
- {children}
- </MarkdownRoot>
+ <ReactMarkdown
+ remarkPlugins={REMARK_PLUGINS}
+ urlTransform={urlTransform}
+ components={customizedComponents}
+ {...additionalOptions}
+ {...rest}
+ >
+ {children}
+ </ReactMarkdown>
+ </div>
);
};
diff --git a/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.module.css b/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.module.css
index 31a45e6cbbd..2c36171c41a 100644
--- a/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.module.css
+++ b/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.module.css
@@ -1,9 +1,42 @@
+.aiMarkdownRoot {
+ min-width: 0;
+}
+
+/* Blocks are separate elements, so the intra-block paragraph rule can't space them. */
+.aiMarkdownRoot > :not(:last-child) {
+ margin-bottom: var(--mantine-spacing-sm);
+}
+
.aiMarkdown {
line-height: 1.5;
min-width: 0;
max-width: 100%;
}
+.aiMarkdown > :last-child {
+ margin-bottom: 0;
+}
+
+.streamedWord {
+ animation: streamed-word-fade 150ms ease;
+}
+
+@keyframes streamed-word-fade {
+ from {
+ opacity: 0;
+ }
+
+ to {
+ opacity: 1;
+ }
+}
+
+@media (prefers-reduced-motion: reduce) {
+ .streamedWord {
+ animation: none;
+ }
+}
+
.tableWrapper {
border: 1px solid var(--mb-color-border-neutral);
border-radius: 8px;
@@ -105,10 +138,6 @@
padding: 0;
}
-.aiMarkdown p {
- margin-bottom: 1rem;
-}
-
.aiMarkdown h1,
.aiMarkdown h2,
.aiMarkdown h3,
diff --git a/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.tsx b/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.tsx
index 4ec557604e5..28f2e103e21 100644
--- a/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.tsx
+++ b/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.tsx
@@ -11,25 +11,22 @@ import {
} from "react";
import { t } from "ttag";
-import {
- Markdown,
- type MarkdownProps,
-} from "metabase/common/components/Markdown";
import { parseMetabaseProtocolLink } from "metabase/metabot/utils/links";
import { ActionIcon, CopyButton, Icon, Tooltip } from "metabase/ui";
import S from "./AIMarkdown.module.css";
+import { StreamingMarkdown } from "./StreamingMarkdown";
import { InternalLink } from "./components/InternalLink";
import { MarkdownSmartLink } from "./components/MarkdownSmartLink";
-type AIMarkdownProps = MarkdownProps & {
+type AIMarkdownProps = {
+ children: string;
+ className?: string;
+ isStreaming?: boolean;
onInternalLinkClick?: (link: string) => void;
singleNewlinesAreParagraphs?: boolean;
};
-// SEC-505: block all images; to support them we'll need detection for valid/allowed image sources
-const DISALLOWED_ELEMENTS = ["img"];
-
const splitMessageLinesAsParagraphs = (message: string) =>
message.replaceAll(/\r?\n|\r/g, "\n\n");
@@ -134,32 +131,27 @@ export const AIMarkdown = memo(
className,
onInternalLinkClick,
children,
+ isStreaming = false,
singleNewlinesAreParagraphs = false,
- ...props
}: AIMarkdownProps) => {
const components = useMemo(
() => getComponents({ onInternalLinkClick }),
[onInternalLinkClick],
);
- const normalizedChildren = useMemo(
- () =>
- singleNewlinesAreParagraphs
- ? splitMessageLinesAsParagraphs(children)
- : children,
- [children, singleNewlinesAreParagraphs],
- );
+ const source = singleNewlinesAreParagraphs
+ ? splitMessageLinesAsParagraphs(children)
+ : children;
return (
- <Markdown
- className={cx(S.aiMarkdown, className)}
- components={components}
- disallowedElements={DISALLOWED_ELEMENTS}
- unwrapDisallowed
- {...props}
- >
- {normalizedChildren}
- </Markdown>
+ <div className={cx(S.aiMarkdownRoot, className)}>
+ <StreamingMarkdown
+ blockClassName={S.aiMarkdown}
+ components={components}
+ isStreaming={isStreaming}
+ source={source}
+ />
+ </div>
);
},
);
diff --git a/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.unit.spec.tsx b/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.unit.spec.tsx
index f906199fc7f..e60aaf0444d 100644
--- a/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.unit.spec.tsx
@@ -1,4 +1,4 @@
-import { screen } from "@testing-library/react";
+import { screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import fetchMock from "fetch-mock";
import { assocIn } from "icepick";
@@ -13,8 +13,15 @@ import { createMockStructuredDatasetQuery } from "metabase-types/api/mocks/query
import { AIMarkdown } from "./AIMarkdown";
+type SetupProps = {
+ children: string;
+ className?: string;
+ isStreaming?: boolean;
+ singleNewlinesAreParagraphs?: boolean;
+};
+
const setup = (
- props: { children: string },
+ props: SetupProps,
{ conversationCharts }: { conversationCharts?: Record<string, unknown> } = {},
) => {
setupEnterprisePlugins();
@@ -36,6 +43,25 @@ const setup = (
});
};
+// Animation spans and paragraph structure have no accessible role to query by.
+/* eslint-disable testing-library/no-node-access */
+const animatedWords = (container: HTMLElement) =>
+ Array.from(container.querySelectorAll("[data-streamed-word]")).map(
+ (span) => span.textContent,
+ );
+
+const renderedText = (container: HTMLElement) =>
+ Array.from(container.querySelectorAll("p"))
+ .map((p) => p.textContent)
+ .join(" ");
+
+const hasElement = (container: HTMLElement, selector: string) =>
+ container.querySelector(selector) !== null;
+
+const countElements = (container: HTMLElement, selector: string) =>
+ container.querySelectorAll(selector).length;
+/* eslint-enable testing-library/no-node-access */
+
describe("AIMarkdown", () => {
beforeEach(() => {
jest.mocked(navigator.clipboard.writeText).mockClear();
@@ -97,9 +123,20 @@ describe("AIMarkdown", () => {
expect(screen.getByRole("cell", { name: "$42" })).toBeInTheDocument();
});
- it("should not render images", async () => {
+ it("should not render images (SEC-505)", async () => {
+ setup({
+ children: `start ![remote](https://example.com/cat.png) ![data](data:image/png;base64,iVBORw0KGgo=) end`,
+ });
+
+ expect(await screen.findByText(/start/)).toBeInTheDocument();
+ expect(screen.getByText(/end/)).toBeInTheDocument();
+ expect(screen.queryByRole("img")).not.toBeInTheDocument();
+ });
+
+ it("should not render images while streaming (SEC-505)", async () => {
setup({
children: `start ![remote](https://example.com/cat.png) ![data](data:image/png;base64,iVBORw0KGgo=) end`,
+ isStreaming: true,
});
expect(await screen.findByText(/start/)).toBeInTheDocument();
@@ -126,4 +163,103 @@ FROM orders
expect(writeText.mock.calls[0][0]).toContain("SELECT *\nFROM orders");
expect(writeText.mock.calls[0][0]).not.toContain("```");
});
+
+ it("should render a single className root even when split into blocks", async () => {
+ const { container } = setup({
+ children: "para one\n\npara two\n\npara three",
+ className: "message-bubble",
+ isStreaming: true,
+ });
+
+ await waitFor(() =>
+ expect(renderedText(container)).toContain("para three"),
+ );
+ expect(countElements(container, ".message-bubble")).toBe(1);
+ });
+
+ describe("streaming", () => {
+ it("should not animate when not streaming", () => {
+ const { container } = setup({ children: "hello world" });
+ expect(animatedWords(container)).toHaveLength(0);
+ });
+
+ it("should not animate words already visible when streaming began", () => {
+ const { container } = setup({
+ children: "hello world",
+ isStreaming: true,
+ });
+ expect(animatedWords(container)).toHaveLength(0);
+ });
+
+ it("should animate only words appended after streaming began", async () => {
+ const { container, rerender } = setup({
+ children: "hello",
+ isStreaming: true,
+ });
+
+ rerender(<AIMarkdown isStreaming>{"hello brave new world"}</AIMarkdown>);
+
+ await waitFor(() =>
+ expect(animatedWords(container)).toEqual(["brave", "new", "world"]),
+ );
+ });
+
+ it("should render the exact source once streaming ends", async () => {
+ const { container, rerender } = setup({
+ children: "done **bold**",
+ isStreaming: true,
+ });
+
+ rerender(<AIMarkdown isStreaming={false}>{"done **bold**"}</AIMarkdown>);
+
+ await waitFor(() => expect(renderedText(container)).toBe("done bold"));
+ expect(renderedText(container)).not.toContain("**");
+ });
+
+ it("should not leak repair artifacts from incomplete markdown", async () => {
+ const { container } = setup({
+ children: "streaming **bol",
+ isStreaming: true,
+ });
+
+ await waitFor(() =>
+ expect(renderedText(container)).toContain("streaming"),
+ );
+ expect(renderedText(container)).not.toContain("**");
+ expect(hasElement(container, "strong")).toBe(true);
+ });
+
+ it("should not leak an emphasis marker inside a streaming list item", async () => {
+ // Trailing space makes the list block end in a newline — the case remend mishandles.
+ const { container } = setup({
+ children: "- word then *emphasis ",
+ isStreaming: true,
+ });
+
+ await waitFor(() => expect(hasElement(container, "li")).toBe(true));
+ expect(container).not.toHaveTextContent(/\*/);
+ expect(hasElement(container, "em")).toBe(true);
+ });
+
+ it("should not animate words inside links, code, or tables", async () => {
+ const { container, rerender } = setup({
+ children: "intro",
+ isStreaming: true,
+ });
+
+ rerender(
+ <AIMarkdown isStreaming>
+ {
+ "intro\n\n[a link](https://example.com)\n\n`inline`\n\n```\ncode here\n```"
+ }
+ </AIMarkdown>,
+ );
+
+ await waitFor(() => expect(hasElement(container, "a")).toBe(true));
+ const animated = animatedWords(container).join(" ");
+ expect(animated).not.toContain("link");
+ expect(animated).not.toContain("inline");
+ expect(animated).not.toContain("code");
+ });
+ });
});
diff --git a/frontend/src/metabase/metabot/components/AIMarkdown/MarkdownBlock.tsx b/frontend/src/metabase/metabot/components/AIMarkdown/MarkdownBlock.tsx
new file mode 100644
index 00000000000..462483fda01
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/AIMarkdown/MarkdownBlock.tsx
@@ -0,0 +1,35 @@
+import { memo } from "react";
+
+import {
+ Markdown,
+ type MarkdownProps,
+} from "metabase/common/components/Markdown";
+
+// SEC-505: block all images; to support them we'll need detection for valid/allowed image sources
+const DISALLOWED_ELEMENTS = ["img"];
+
+type MarkdownBlockProps = {
+ source: string;
+ className?: string;
+ components: Record<string, any>;
+ rehypePlugins?: MarkdownProps["rehypePlugins"];
+};
+
+export const MarkdownBlock = memo(function MarkdownBlock({
+ source,
+ className,
+ components,
+ rehypePlugins,
+}: MarkdownBlockProps) {
+ return (
+ <Markdown
+ className={className}
+ components={components}
+ rehypePlugins={rehypePlugins}
+ disallowedElements={DISALLOWED_ELEMENTS}
+ unwrapDisallowed
+ >
+ {source}
+ </Markdown>
+ );
+});
diff --git a/frontend/src/metabase/metabot/components/AIMarkdown/StreamingMarkdown.tsx b/frontend/src/metabase/metabot/components/AIMarkdown/StreamingMarkdown.tsx
new file mode 100644
index 00000000000..efde5ff69a4
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/AIMarkdown/StreamingMarkdown.tsx
@@ -0,0 +1,74 @@
+import { useMemo, useRef } from "react";
+import remend from "remend";
+
+import type { MarkdownProps } from "metabase/common/components/Markdown";
+import { isReducedMotionPreferred } from "metabase/utils/dom";
+
+import { MarkdownBlock } from "./MarkdownBlock";
+import { createRehypeStreamWords } from "./rehypeStreamWords";
+import { splitMarkdownBlocks } from "./splitMarkdownBlocks";
+import { useCoalescedSource } from "./useCoalescedSource";
+
+type StreamingMarkdownProps = {
+ source: string;
+ isStreaming: boolean;
+ blockClassName: string;
+ components: Record<string, any>;
+};
+
+const repair = (source: string) => {
+ try {
+ // remend appends an emphasis closer after a block's trailing newline
+ // (`*a\n` → `*a\n*`), which won't parse; trim so it lands against the content.
+ return remend(source.trimEnd(), { linkMode: "text-only" });
+ } catch {
+ return source;
+ }
+};
+
+// Repairs a possibly incomplete markdown stream to valid markdown and animates its rendering
+export const StreamingMarkdown = ({
+ source,
+ isStreaming,
+ blockClassName,
+ components,
+}: StreamingMarkdownProps) => {
+ const animate = isStreaming && !isReducedMotionPreferred();
+ const coalesced = useCoalescedSource(source, animate);
+ const blocks = useMemo(() => splitMarkdownBlocks(coalesced), [coalesced]);
+
+ // reuse each block's plugin so its animation boundary stays consistent across renders
+ const pluginsByBlock = useRef(
+ new Map<number, MarkdownProps["rehypePlugins"]>(),
+ );
+
+ const getPlugins = (index: number, isLast: boolean) => {
+ const cached = pluginsByBlock.current.get(index);
+ if (cached) {
+ return cached;
+ }
+ if (!animate || !isLast) {
+ return undefined;
+ }
+ const created = [createRehypeStreamWords({ animateFromChar: null })];
+ pluginsByBlock.current.set(index, created);
+ return created;
+ };
+
+ return (
+ <>
+ {blocks.map((block, index) => {
+ const isLast = index === blocks.length - 1;
+ return (
+ <MarkdownBlock
+ key={index}
+ className={blockClassName}
+ components={components}
+ rehypePlugins={getPlugins(index, isLast)}
+ source={isStreaming && isLast ? repair(block) : block}
+ />
+ );
+ })}
+ </>
+ );
+};
diff --git a/frontend/src/metabase/metabot/components/AIMarkdown/rehypeStreamWords.ts b/frontend/src/metabase/metabot/components/AIMarkdown/rehypeStreamWords.ts
new file mode 100644
index 00000000000..2e7dcb83fbc
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/AIMarkdown/rehypeStreamWords.ts
@@ -0,0 +1,122 @@
+import type {
+ Element,
+ ElementContent,
+ Parent,
+ Root,
+ RootContent,
+ Text,
+} from "hast";
+
+import S from "./AIMarkdown.module.css";
+
+// Links are excluded because AIMarkdown's `a` override reads node.children[0].value
+// to build smart links; code/tables are excluded to keep copy text and layout intact.
+const SKIPPED_TAGS = new Set([
+ "a",
+ "pre",
+ "code",
+ "table",
+ "thead",
+ "tbody",
+ "tfoot",
+ "tr",
+ "th",
+ "td",
+ "svg",
+ "math",
+ "script",
+ "style",
+]);
+
+export type StreamWordsState = { animateFromChar: number | null };
+
+const countChars = (node: Root | RootContent): number => {
+ if (node.type === "text") {
+ return node.value.length;
+ }
+ return "children" in node
+ ? node.children.reduce((total, child) => total + countChars(child), 0)
+ : 0;
+};
+
+const wrapWord = (word: string): Element => ({
+ type: "element",
+ tagName: "span",
+ properties: { className: [S.streamedWord], "data-streamed-word": "" },
+ children: [{ type: "text", value: word }],
+});
+
+/**
+ * Wraps newly streamed words in fade-in spans. The boundary is frozen on first
+ * parse so spans only ever get appended — React reuses same-index nodes, so a
+ * moving boundary would swap text into an existing span and never re-fade it.
+ */
+export const createRehypeStreamWords =
+ (state: StreamWordsState) => () => (tree: Root) => {
+ if (state.animateFromChar === null) {
+ state.animateFromChar = countChars(tree);
+ return;
+ }
+
+ const animateFrom = state.animateFromChar;
+ let offset = 0;
+
+ const splitText = (node: Text): ElementContent[] | null => {
+ const start = offset;
+ offset += node.value.length;
+
+ if (offset <= animateFrom) {
+ return null;
+ }
+
+ const parts = node.value.split(/(\s+)/).filter(Boolean);
+ const out: ElementContent[] = [];
+ let cursor = start;
+ let plain = "";
+
+ for (const part of parts) {
+ const partStart = cursor;
+ cursor += part.length;
+ const isWhitespace = /^\s+$/.test(part);
+
+ // A word that started before the boundary was already visible; re-wrapping re-fades it.
+ if (isWhitespace || partStart < animateFrom) {
+ plain += part;
+ continue;
+ }
+ if (plain) {
+ out.push({ type: "text", value: plain });
+ plain = "";
+ }
+ out.push(wrapWord(part));
+ }
+ if (plain) {
+ out.push({ type: "text", value: plain });
+ }
+
+ const unchanged = out.length === 1 && out[0].type === "text";
+ return unchanged ? null : out;
+ };
+
+ const walk = (parent: Parent) => {
+ for (let i = 0; i < parent.children.length; i++) {
+ const child = parent.children[i];
+
+ if (child.type === "element") {
+ if (SKIPPED_TAGS.has(child.tagName)) {
+ offset += countChars(child);
+ continue;
+ }
+ walk(child);
+ } else if (child.type === "text") {
+ const replacement = splitText(child);
+ if (replacement) {
+ parent.children.splice(i, 1, ...replacement);
+ i += replacement.length - 1;
+ }
+ }
+ }
+ };
+
+ walk(tree);
+ };
diff --git a/frontend/src/metabase/metabot/components/AIMarkdown/rehypeStreamWords.unit.spec.ts b/frontend/src/metabase/metabot/components/AIMarkdown/rehypeStreamWords.unit.spec.ts
new file mode 100644
index 00000000000..9d22f4d38cb
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/AIMarkdown/rehypeStreamWords.unit.spec.ts
@@ -0,0 +1,104 @@
+import type { Element, Root } from "hast";
+
+import {
+ type StreamWordsState,
+ createRehypeStreamWords,
+} from "./rehypeStreamWords";
+
+const text = (value: string) => ({ type: "text" as const, value });
+
+const element = (tagName: string, children: Element["children"]): Element => ({
+ type: "element",
+ tagName,
+ properties: {},
+ children,
+});
+
+const root = (children: Root["children"]): Root => ({ type: "root", children });
+
+const run = (state: StreamWordsState, tree: Root) => {
+ createRehypeStreamWords(state)()(tree);
+ return tree;
+};
+
+const spans = (node: Root | Element): Element[] =>
+ node.children.flatMap((child) =>
+ child.type === "element"
+ ? [...(child.tagName === "span" ? [child] : []), ...spans(child)]
+ : [],
+ );
+
+const spanWords = (node: Root | Element) =>
+ spans(node).map((span) =>
+ span.children[0].type === "text" ? span.children[0].value : "",
+ );
+
+const freshState = (): StreamWordsState => ({ animateFromChar: null });
+
+describe("createRehypeStreamWords", () => {
+ it("should wrap nothing on the first parse and freeze the boundary", () => {
+ const state = freshState();
+ const tree = run(state, root([element("p", [text("Hello world")])]));
+
+ expect(spans(tree)).toHaveLength(0);
+ expect(state.animateFromChar).toBe("Hello world".length);
+ });
+
+ it("should be idempotent on the first parse for StrictMode double renders", () => {
+ const state = freshState();
+ run(state, root([element("p", [text("Hello world")])]));
+ const frozen = state.animateFromChar;
+
+ run(state, root([element("p", [text("Hello world")])]));
+
+ expect(state.animateFromChar).toBe(frozen);
+ });
+
+ it("should wrap only words appended after the boundary", () => {
+ const state: StreamWordsState = { animateFromChar: 6 };
+ const tree = run(state, root([element("p", [text("Hello world again")])]));
+
+ expect(spanWords(tree)).toEqual(["world", "again"]);
+ });
+
+ it("should leave a word that started before the boundary unwrapped", () => {
+ const state: StreamWordsState = { animateFromChar: 3 };
+ const tree = run(state, root([element("p", [text("Hello world")])]));
+
+ expect(spanWords(tree)).toEqual(["world"]);
+ });
+
+ it("should preserve whitespace outside of spans", () => {
+ const state: StreamWordsState = { animateFromChar: 0 };
+ const paragraph = element("p", [text("a b")]);
+ run(state, root([paragraph]));
+
+ const values = paragraph.children.map((child) =>
+ child.type === "text" ? child.value : "span",
+ );
+ expect(values).toEqual(["span", " ", "span"]);
+ });
+
+ it.each(["a", "pre", "code", "table", "svg"])(
+ "should not wrap words inside %s",
+ (tagName) => {
+ const state: StreamWordsState = { animateFromChar: 0 };
+ const tree = run(
+ state,
+ root([element(tagName, [text("untouched words here")])]),
+ );
+
+ expect(spans(tree)).toHaveLength(0);
+ },
+ );
+
+ it("should still count skipped subtrees so later offsets stay aligned", () => {
+ const state: StreamWordsState = { animateFromChar: 6 };
+ const tree = run(
+ state,
+ root([element("code", [text("abcdef")]), element("p", [text("after")])]),
+ );
+
+ expect(spanWords(tree)).toEqual(["after"]);
+ });
+});
diff --git a/frontend/src/metabase/metabot/components/AIMarkdown/splitMarkdownBlocks.ts b/frontend/src/metabase/metabot/components/AIMarkdown/splitMarkdownBlocks.ts
new file mode 100644
index 00000000000..acbf9300381
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/AIMarkdown/splitMarkdownBlocks.ts
@@ -0,0 +1,35 @@
+import { Lexer } from "marked";
+
+const FOOTNOTE_PATTERN = /\[\^[^\]\s]+\]/;
+
+// Splits markdown into top-level block sources for memoized rendering.
+export const splitMarkdownBlocks = (source: string): string[] => {
+ if (source === "") {
+ return [];
+ }
+
+ if (FOOTNOTE_PATTERN.test(source)) {
+ return [source];
+ }
+
+ try {
+ const tokens = new Lexer({ gfm: true }).lex(source);
+
+ // Reference definitions are document-scoped, so blocks can't resolve them alone.
+ if (tokens.links && Object.keys(tokens.links).length > 0) {
+ return [source];
+ }
+
+ return tokens.reduce<string[]>((acc, token) => {
+ const isBlankLine = token.type === "space";
+ if (isBlankLine && acc.length > 0) {
+ acc[acc.length - 1] += token.raw;
+ } else if (!isBlankLine) {
+ acc.push(token.raw);
+ }
+ return acc;
+ }, []);
+ } catch {
+ return [source];
+ }
+};
diff --git a/frontend/src/metabase/metabot/components/AIMarkdown/splitMarkdownBlocks.unit.spec.ts b/frontend/src/metabase/metabot/components/AIMarkdown/splitMarkdownBlocks.unit.spec.ts
new file mode 100644
index 00000000000..c125cb8def3
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/AIMarkdown/splitMarkdownBlocks.unit.spec.ts
@@ -0,0 +1,50 @@
+import { splitMarkdownBlocks } from "./splitMarkdownBlocks";
+
+describe("splitMarkdownBlocks", () => {
+ it("should return no blocks for an empty source", () => {
+ expect(splitMarkdownBlocks("")).toEqual([]);
+ });
+
+ it("should split top-level blocks without losing any source", () => {
+ const source =
+ "# Title\n\npara one\n\n- a\n- b\n\n| h |\n| - |\n| 1 |\n\n```sql\nSELECT 1\n```\n\nlast";
+ const blocks = splitMarkdownBlocks(source);
+
+ expect(blocks.length).toBeGreaterThan(1);
+ expect(blocks.join("")).toBe(source);
+ });
+
+ it("should not emit empty blocks for blank lines", () => {
+ const blocks = splitMarkdownBlocks("a\n\n\n\nb");
+ expect(blocks.every((block) => block.trim() !== "")).toBe(true);
+ });
+
+ it("should keep completed blocks stable as the source grows", () => {
+ const full = "para one\n\npara two\n\npara three";
+ const finalBlocks = splitMarkdownBlocks(full);
+
+ for (let length = 1; length <= full.length; length++) {
+ const blocks = splitMarkdownBlocks(full.slice(0, length));
+ const completed = blocks.slice(0, -1);
+
+ completed.forEach((block, index) => {
+ expect(block).toBe(finalBlocks[index]);
+ });
+ }
+ });
+
+ it("should degrade to a single block for link reference definitions", () => {
+ const source = "See [x] here.\n\n[x]: https://example.com\n";
+ expect(splitMarkdownBlocks(source)).toEqual([source]);
+ });
+
+ it("should degrade to a single block for footnotes", () => {
+ const source = "Text[^1]\n\n[^1]: note\n";
+ expect(splitMarkdownBlocks(source)).toEqual([source]);
+ });
+
+ it("should treat an unterminated fence as one block", () => {
+ const blocks = splitMarkdownBlocks("intro\n\n```sql\nSELECT 1");
+ expect(blocks[blocks.length - 1]).toContain("```sql");
+ });
+});
diff --git a/frontend/src/metabase/metabot/components/AIMarkdown/useCoalescedSource.ts b/frontend/src/metabase/metabot/components/AIMarkdown/useCoalescedSource.ts
new file mode 100644
index 00000000000..a09b3b9ece8
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/AIMarkdown/useCoalescedSource.ts
@@ -0,0 +1,48 @@
+import { useEffect, useRef, useState } from "react";
+
+/**
+ * Throttles a growing streamed string to one update per animation frame.
+ * Returns the exact source during the same render that streaming ends.
+ */
+export const useCoalescedSource = (source: string, isStreaming: boolean) => {
+ const [frameValue, setFrameValue] = useState(source);
+ const [prevIsStreaming, setPrevIsStreaming] = useState(isStreaming);
+ const latest = useRef(source);
+ const frameId = useRef<number | null>(null);
+
+ if (prevIsStreaming !== isStreaming) {
+ setPrevIsStreaming(isStreaming);
+ setFrameValue(source);
+ }
+
+ useEffect(() => {
+ latest.current = source;
+
+ if (!isStreaming || source === frameValue || frameId.current !== null) {
+ return;
+ }
+
+ frameId.current = requestAnimationFrame(() => {
+ frameId.current = null;
+ setFrameValue(latest.current);
+ });
+ }, [source, isStreaming, frameValue]);
+
+ useEffect(() => {
+ if (!isStreaming && frameId.current !== null) {
+ cancelAnimationFrame(frameId.current);
+ frameId.current = null;
+ }
+ }, [isStreaming]);
+
+ useEffect(
+ () => () => {
+ if (frameId.current !== null) {
+ cancelAnimationFrame(frameId.current);
+ }
+ },
+ [],
+ );
+
+ return isStreaming ? frameValue : source;
+};
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx
index b4b8ab815dd..b8d6a3eef7c 100644
--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx
@@ -184,6 +184,7 @@ interface AgentMessageProps extends Omit<BaseMessageProps, "message"> {
submittedFeedback: "positive" | "negative" | undefined;
onInternalLinkClick?: (link: string) => void;
extraActions?: ReactNode;
+ isStreaming?: boolean;
}
export const AgentMessage = ({
@@ -200,6 +201,7 @@ export const AgentMessage = ({
onInternalLinkClick,
hideActions,
extraActions,
+ isStreaming = false,
...props
}: AgentMessageProps) => {
const messageId = "externalId" in message ? (message.externalId ?? "") : "";
@@ -215,6 +217,7 @@ export const AgentMessage = ({
<AIMarkdown
className={Styles.message}
onInternalLinkClick={onInternalLinkClick}
+ isStreaming={isStreaming}
>
{m.message}
</AIMarkdown>
@@ -557,6 +560,7 @@ export const Messages = ({
hideActions={next?.role === "agent" || (isDoingScience && !next)}
extraActions={getExtraActions?.(message.id)}
onInternalLinkClick={onInternalLinkClick}
+ isStreaming={isDoingScience && !next}
/>
) : (
<UserMessage
diff --git a/frontend/src/metabase/metabot/tests/utils.tsx b/frontend/src/metabase/metabot/tests/utils.tsx
index 1fe1e961e61..18e851e4548 100644
--- a/frontend/src/metabase/metabot/tests/utils.tsx
+++ b/frontend/src/metabase/metabot/tests/utils.tsx
@@ -53,6 +53,21 @@ import { getMetabotInitialState } from "../state/reducer-utils";
export { createMockReadableStream, createMockSSEStream, createPauses };
+const mockReducedMotion = () => {
+ window.matchMedia = (query: string) =>
+ // jsdom has no matchMedia; this stub only needs the fields our code reads.
+ ({
+ matches: query.includes("prefers-reduced-motion"),
+ media: query,
+ onchange: null,
+ addEventListener: () => {},
+ removeEventListener: () => {},
+ addListener: () => {},
+ removeListener: () => {},
+ dispatchEvent: () => false,
+ }) as unknown as MediaQueryList;
+};
+
export const mockAgentEndpoint = (params: MockStreamedEndpointParams) =>
mockStreamedEndpoint("/api/metabot/agent-streaming", params);
@@ -131,7 +146,10 @@ export const assertConversation = async (
screen.queryByTestId("metabot-chat-message"),
).not.toBeInTheDocument();
});
- } else {
+ return;
+ }
+
+ await waitFor(async () => {
const realMessages = await chatMessages();
expect(realMessages.length).toBe(expectedMessages.length);
expectedMessages.forEach(([expectedRole, expectedMessage], index) => {
@@ -139,7 +157,7 @@ export const assertConversation = async (
expect(realMessage).toHaveAttribute("data-message-role", expectedRole);
expect(realMessage).toHaveTextContent(expectedMessage);
});
- }
+ });
};
export const lastReqBody = async (
@@ -225,6 +243,8 @@ export function setup(
initialRoute?: string;
} | void,
) {
+ mockReducedMotion(); // induce reduced motion to avoid waiting for streaming to finish
+
const settings = mockSettings({
"llm-metabot-configured?": options?.isConfigured ?? true,
});
diff --git a/jest.config.js b/jest.config.js
index 3df9509f7cc..53922c2c120 100644
--- a/jest.config.js
+++ b/jest.config.js
@@ -42,6 +42,8 @@ const baseConfig = {
// Force jose to use Node.js runtime instead of browser runtime in jsdom environment.
// The browser runtime expects CryptoKey to be globally available, which jsdom doesn't provide.
"^jose$": "<rootDir>/node_modules/jose/dist/node/cjs/index.js",
+ // remend only declares an `import` export condition, so jest's CJS resolver can't find it.
+ "^remend$": "<rootDir>/node_modules/remend/dist/index.js",
"^build-configs/(.*)$": "<rootDir>/frontend/build/$1",
"\\.(css|less)$": "<rootDir>/frontend/test/__mocks__/styleMock.js",
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
diff --git a/jest.esm-packages.js b/jest.esm-packages.js
index 54f2ac7cac9..a0c8efc6cfd 100644
--- a/jest.esm-packages.js
+++ b/jest.esm-packages.js
@@ -39,6 +39,7 @@ const esmPackages = [
"jose",
"longest-streak",
"markdown-table",
+ "marked",
"mdast.*",
"micromark.*",
"parse-entities",
@@ -46,6 +47,7 @@ const esmPackages = [
"react-markdown",
"rehype-external-links",
"remark.*",
+ "remend",
"space-separated-tokens",
"stringify-entities",
"trim-lines",
diff --git a/package.json b/package.json
index ec0432dcb0a..c7acbe5fc89 100644
--- a/package.json
+++ b/package.json
@@ -127,6 +127,7 @@
"leaflet-draw": "^0.4.9",
"lodash.debounce": "^4.0.8",
"lodash.orderby": "^4.6.0",
+ "marked": "18.0.6",
"merge-refs": "^1.3.0",
"mustache": "^2.3.2",
"normalizr": "^3.0.2",
@@ -162,6 +163,7 @@
"redux-undo": "^1.1.0",
"rehype-external-links": "^3.0.0",
"remark-gfm": "^4.0.0",
+ "remend": "1.3.0",
"reselect": "^5.1.0",
"resize-observer-polyfill": "^1.5.1",
"server-text-width": "^1.0.2",