AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
perf(frontend/copilot): keep Streamdown props stable while streaming (#15678)
Abhimanyu Yadav76a92141111a
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CredentialMention/CredentialMentionMarkdown.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CredentialMention/CredentialMentionMarkdown.tsxindex 6609907e7c..91f40c0e68 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CredentialMention/CredentialMentionMarkdown.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CredentialMention/CredentialMentionMarkdown.tsx@@ -3,6 +3,10 @@ import {MessageResponse,type MessageResponseProps,} from "@/components/ai-elements/message";+import {+ createComponentsMerger,+ type StreamdownComponents,+} from "@/components/ai-elements/streamdown-components";import { CredentialMentionBadge } from "./CredentialMentionBadge";import {credentialMentionImageProvider,@@ -13,13 +17,10 @@ interface Props extends Omit<MessageResponseProps, "children"> {children: string;}-export function CredentialMentionMarkdown({- children,- components,- ...props-}: Props) {- const Image = components?.img;- function renderImage(imageProps: React.JSX.IntrinsicElements["img"]) {+const NO_COMPONENTS: StreamdownComponents = {};++function createImageRenderer(Image: StreamdownComponents["img"]) {+ return function renderImage(imageProps: React.JSX.IntrinsicElements["img"]) {const provider = credentialMentionImageProvider(imageProps.src);if (provider !== null)return (@@ -30,11 +31,25 @@ export function CredentialMentionMarkdown({);if (typeof Image === "string") return createElement(Image, imageProps);return Image ? <Image {...imageProps} /> : null;- }+ };+}++// A new `img` per render would make Streamdown re-parse every block and remount+// every image on each streamed chunk, so it is built once per `components`.+const withCredentialBadges = createComponentsMerger((components) => ({+ ...components,+ img: createImageRenderer(components.img),+}));++export function CredentialMentionMarkdown({+ children,+ components,+ ...props+}: Props) {return (<MessageResponse{...props}- components={{ ...components, img: renderImage }}+ components={withCredentialBadges(components ?? NO_COMPONENTS)}>{credentialMentionsToMarkdown(children)}</MessageResponse>diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CredentialMention/__tests__/CredentialMentionMarkdown.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CredentialMention/__tests__/CredentialMentionMarkdown.test.tsxindex 712a5f16a3..b16f1da63b 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CredentialMention/__tests__/CredentialMentionMarkdown.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CredentialMention/__tests__/CredentialMentionMarkdown.test.tsx@@ -1,5 +1,6 @@import { render, screen } from "@/tests/integrations/test-utils";import { describe, expect, it } from "vitest";+import type { ComponentProps } from "react";import { CredentialMentionMarkdown } from "../CredentialMentionMarkdown";describe("credential badges in messages", () => {@@ -37,4 +38,57 @@ describe("credential badges in messages", () => {true,);});++ it("keeps the same badge and image elements while the reply streams in", async () => {+ function FramedImage({ src, alt }: ComponentProps<"img">) {+ return <span data-testid="framed-image" data-src={src} title={alt} />;+ }+ const components = { img: FramedImage };+ const reply =+ "Use [Work Gmail](credential://google/work-secret-id) with ";+ const { container, rerender } = render(+ <CredentialMentionMarkdown components={components}>+ {reply}+ </CredentialMentionMarkdown>,+ );+ const framed = await screen.findByTestId("framed-image");+ const badge = container.querySelector('img[src*="google"]');+ expect(badge).not.toBeNull();++ rerender(+ <CredentialMentionMarkdown components={components}>+ {`${reply} and more text`}+ </CredentialMentionMarkdown>,+ );+ expect(await screen.findByText(/and more text/)).toBeTruthy();++ expect(screen.getByTestId("framed-image")).toBe(framed);+ expect(container.querySelector('img[src*="google"]')).toBe(badge);+ });++ it("uses an image component the caller replaced in place on the same map", async () => {+ function FirstImage({ src }: ComponentProps<"img">) {+ return <span data-testid="first-image" data-src={src} />;+ }+ function SecondImage({ src }: ComponentProps<"img">) {+ return <span data-testid="second-image" data-src={src} />;+ }+ const components: { img: typeof FirstImage } = { img: FirstImage };+ const reply = "See ";+ const { rerender } = render(+ <CredentialMentionMarkdown components={components}>+ {reply}+ </CredentialMentionMarkdown>,+ );+ expect(await screen.findByTestId("first-image")).toBeTruthy();++ components.img = SecondImage;+ rerender(+ <CredentialMentionMarkdown components={components}>+ {`${reply} and more text`}+ </CredentialMentionMarkdown>,+ );++ expect(await screen.findByTestId("second-image")).toBeTruthy();+ });});diff --git a/autogpt_platform/frontend/src/components/ai-elements/__tests__/message-streaming.test.tsx b/autogpt_platform/frontend/src/components/ai-elements/__tests__/message-streaming.test.tsxnew file mode 100644index 0000000000..542c67bd3b--- /dev/null+++ b/autogpt_platform/frontend/src/components/ai-elements/__tests__/message-streaming.test.tsx@@ -0,0 +1,137 @@+import { cleanup, render, waitFor } from "@testing-library/react";+import type { ComponentProps } from "react";+import type { StreamdownProps } from "streamdown";+import { afterEach, describe, expect, it, vi } from "vitest";+import { MessageResponse } from "../message";++const streamdownProps = vi.hoisted(() => [] as StreamdownProps[]);++vi.mock("streamdown", async (importOriginal) => {+ const actual = await importOriginal<typeof import("streamdown")>();+ function RecordingStreamdown(+ props: ComponentProps<typeof actual.Streamdown>,+ ) {+ streamdownProps.push(props);+ return <actual.Streamdown {...props} />;+ }+ return { ...actual, Streamdown: RecordingStreamdown };+});++const TABLE = "| Account | Status |\n| --- | --- |\n| Work | ok |";++function Strong({ children }: { children?: React.ReactNode }) {+ return <strong data-testid="custom-strong">{children}</strong>;+}++const CUSTOM_COMPONENTS = { strong: Strong };++describe("MessageResponse while a reply streams in", () => {+ afterEach(() => {+ cleanup();+ streamdownProps.length = 0;+ });++ it("hands Streamdown the same components, plugins and link safety on every flush", async () => {+ const { rerender, container } = render(+ <MessageResponse components={CUSTOM_COMPONENTS}>Hello</MessageResponse>,+ );+ rerender(+ <MessageResponse components={CUSTOM_COMPONENTS}>+ Hello **world**+ </MessageResponse>,+ );+ await waitFor(() =>+ expect(+ container.querySelector('[data-testid="custom-strong"]'),+ ).toBeTruthy(),+ );++ expect(streamdownProps.length).toBeGreaterThanOrEqual(2);+ const [first, ...rest] = streamdownProps;+ for (const props of rest) {+ expect(props.components).toBe(first.components);+ expect(props.plugins).toBe(first.plugins);+ expect(props.linkSafety).toBe(first.linkSafety);+ }+ });++ it("keeps the same props across separate messages without custom components", () => {+ render(<MessageResponse>First reply</MessageResponse>);+ render(<MessageResponse>Second reply</MessageResponse>);++ const [first, second] = streamdownProps;+ expect(second.components).toBe(first.components);+ expect(second.plugins).toBe(first.plugins);+ expect(second.linkSafety).toBe(first.linkSafety);+ });++ it("keeps the table and its scroll position while text streams in after it", async () => {+ const { rerender, container } = render(+ <MessageResponse>{TABLE}</MessageResponse>,+ );+ await waitFor(() => expect(container.querySelector("table")).toBeTruthy());+ const wrapper = container.querySelector("table")?.parentElement;+ expect(wrapper?.classList.contains("overflow-x-auto")).toBe(true);+ wrapper!.scrollLeft = 40;++ rerender(+ <MessageResponse>{`${TABLE}\n\nBoth accounts are connected.`}</MessageResponse>,+ );+ await waitFor(() =>+ expect(container.textContent).toContain("Both accounts are connected."),+ );++ expect(container.querySelector("table")?.parentElement).toBe(wrapper);+ expect(wrapper!.scrollLeft).toBe(40);+ });++ it("lets the caller's components override the default table wrapper", async () => {+ function PlainTable({ children }: { children?: React.ReactNode }) {+ return <table data-testid="plain-table">{children}</table>;+ }+ const { container } = render(+ <MessageResponse components={{ table: PlainTable }}>+ {TABLE}+ </MessageResponse>,+ );+ await waitFor(() =>+ expect(+ container.querySelector('[data-testid="plain-table"]'),+ ).toBeTruthy(),+ );+ expect(container.querySelector(".overflow-x-auto")).toBeNull();+ });++ it("picks up a component the caller replaced in place on the same map", async () => {+ function FirstTable({ children }: { children?: React.ReactNode }) {+ return <table data-testid="first-table">{children}</table>;+ }+ function SecondTable({ children }: { children?: React.ReactNode }) {+ return <table data-testid="second-table">{children}</table>;+ }+ const components: Record<string, typeof FirstTable> = {+ table: FirstTable,+ };+ const { container, rerender } = render(+ <MessageResponse components={components}>{TABLE}</MessageResponse>,+ );+ await waitFor(() =>+ expect(+ container.querySelector('[data-testid="first-table"]'),+ ).toBeTruthy(),+ );++ components.table = SecondTable;+ rerender(+ <MessageResponse components={components}>+ {`${TABLE}\n\nDone.`}+ </MessageResponse>,+ );++ await waitFor(() =>+ expect(+ container.querySelector('[data-testid="second-table"]'),+ ).toBeTruthy(),+ );+ });+});diff --git a/autogpt_platform/frontend/src/components/ai-elements/message.tsx b/autogpt_platform/frontend/src/components/ai-elements/message.tsxindex c5ded4db90..88957d904b 100644--- a/autogpt_platform/frontend/src/components/ai-elements/message.tsx+++ b/autogpt_platform/frontend/src/components/ai-elements/message.tsx@@ -23,6 +23,10 @@ import type { ComponentProps, HTMLAttributes, ReactElement } from "react";import { createContext, memo, useContext, useEffect, useState } from "react";import type { LinkSafetyModalProps } from "streamdown";import { Streamdown } from "streamdown";+import {+ createComponentsMerger,+ type StreamdownComponents,+} from "./streamdown-components";export type MessageProps = HTMLAttributes<HTMLDivElement> & {from: UIMessage["role"];@@ -367,6 +371,43 @@ const math: MathPlugin = {],};+// Streamdown re-parses every block of a message when the identity of a+// component in `components` changes, and React remounts any element whose+// component type changes, so everything handed to Streamdown is built once.+function MessageTable({ children, ...tableProps }: ComponentProps<"table">) {+ // Tables can be wider than the chat column (long URLs, many columns).+ // Wrap in an overflow-x-auto div so the page never grows past the+ // chat container — the table scrolls internally instead.+ return (+ <div className="my-4 max-w-full overflow-x-auto rounded-xl border border-zinc-200 bg-white shadow-message-table">+ <table {...tableProps}>{children}</table>+ </div>+ );+}++function renderExternalLinkModal(modalProps: LinkSafetyModalProps) {+ return <ExternalLinkModal {...modalProps} />;+}++const STREAMDOWN_PLUGINS = { code, mermaid, math, cjk };++const LINK_SAFETY = {+ enabled: true,+ onLinkCheck: isSameOriginLink,+ renderModal: renderExternalLinkModal,+};++const TABLE_COMPONENT: StreamdownComponents = { table: MessageTable };++const mergeTableComponent = createComponentsMerger((components) => ({+ ...TABLE_COMPONENT,+ ...components,+}));++function withTableComponent(components: StreamdownComponents | undefined) {+ return components ? mergeTableComponent(components) : TABLE_COMPONENT;+}+export const MessageResponse = memo(({ className, children, components, ...props }: MessageResponseProps) => (<Streamdown@@ -386,23 +427,9 @@ export const MessageResponse = memo("[&_tbody_tr:hover_td]:bg-zinc-50",className,)}- components={{- // Tables can be wider than the chat column (long URLs, many columns).- // Wrap in an overflow-x-auto div so the page never grows past the- // chat container — the table scrolls internally instead.- table: ({ children, ...tableProps }) => (- <div className="my-4 max-w-full overflow-x-auto rounded-xl border border-zinc-200 bg-white shadow-message-table">- <table {...tableProps}>{children}</table>- </div>- ),- ...components,- }}- plugins={{ code, mermaid, math, cjk }}- linkSafety={{- enabled: true,- onLinkCheck: isSameOriginLink,- renderModal: (modalProps) => <ExternalLinkModal {...modalProps} />,- }}+ components={withTableComponent(components)}+ plugins={STREAMDOWN_PLUGINS}+ linkSafety={LINK_SAFETY}{...props}>{typeof children === "string"diff --git a/autogpt_platform/frontend/src/components/ai-elements/streamdown-components.ts b/autogpt_platform/frontend/src/components/ai-elements/streamdown-components.tsnew file mode 100644index 0000000000..268a037dc2--- /dev/null+++ b/autogpt_platform/frontend/src/components/ai-elements/streamdown-components.ts@@ -0,0 +1,32 @@+import type { StreamdownProps } from "streamdown";++export type StreamdownComponents = NonNullable<StreamdownProps["components"]>;++function hasSameEntries(a: StreamdownComponents, b: StreamdownComponents) {+ const keys = Object.keys(a) as (keyof StreamdownComponents)[];+ return (+ keys.length === Object.keys(b).length && keys.every((k) => a[k] === b[k])+ );+}++// Streamdown re-parses every block of a message when the identity of a+// component in `components` changes, and React remounts any element whose+// component type changes. This builds `merge(components)` once per+// `components` object, and again only if one of its entries has changed+// since, so a caller that edits its map in place still gets the new renderer.+export function createComponentsMerger(+ merge: (components: StreamdownComponents) => StreamdownComponents,+) {+ const cache = new WeakMap<+ StreamdownComponents,+ { snapshot: StreamdownComponents; merged: StreamdownComponents }+ >();+ return function mergeOnce(components: StreamdownComponents) {+ const cached = cache.get(components);+ if (cached && hasSameEntries(cached.snapshot, components))+ return cached.merged;+ const merged = merge(components);+ cache.set(components, { snapshot: { ...components }, merged });+ return merged;+ };+}