diff --git a/apps/web/src/components/ChatMarkdown.test.tsx b/apps/web/src/components/ChatMarkdown.test.tsx new file mode 100644 index 0000000000..78ea343603 --- /dev/null +++ b/apps/web/src/components/ChatMarkdown.test.tsx @@ -0,0 +1,40 @@ +import { renderToStaticMarkup } from "react-dom/server"; +import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; + +const reactMock = vi.hoisted(() => ({ + useDeferredValue: vi.fn<(text: string) => string>(), +})); + +vi.mock("react", async (importOriginal) => ({ + ...(await importOriginal()), + useDeferredValue: reactMock.useDeferredValue, +})); + +import ChatMarkdown from "./ChatMarkdown"; + +describe("ChatMarkdown streaming rendering", () => { + beforeEach(() => { + reactMock.useDeferredValue.mockReset(); + }); + + it("renders the scheduled markdown text instead of the latest streaming source", () => { + reactMock.useDeferredValue.mockReturnValue("**deferred markdown**"); + + const markup = renderToStaticMarkup( + , + ); + + expect(reactMock.useDeferredValue).toHaveBeenCalledWith("latest markdown"); + expect(markup).toContain("deferred markdown"); + expect(markup).not.toContain("latest markdown"); + }); + + it("renders the latest markdown immediately after streaming finishes", () => { + reactMock.useDeferredValue.mockReturnValue("stale markdown"); + + const markup = renderToStaticMarkup(); + + expect(markup).toContain("final markdown"); + expect(markup).not.toContain("stale markdown"); + }); +}); diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index 1d8920e481..8b0c6c94a7 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -25,6 +25,7 @@ import React, { isValidElement, use, useCallback, + useDeferredValue, memo, useEffect, useMemo, @@ -1172,12 +1173,14 @@ function ChatMarkdown({ serverConfig?.availableEditors ?? [], ); const diffThemeName = resolveDiffThemeName(resolvedTheme); + const deferredText = useDeferredValue(text); + const markdownText = isStreaming ? deferredText : text; const markdownFileLinkMetaByHref = useMemo(() => { const metaByHref = new Map< string, NonNullable> >(); - for (const href of extractMarkdownLinkHrefs(text)) { + for (const href of extractMarkdownLinkHrefs(markdownText)) { const normalizedHref = normalizeMarkdownLinkHrefKey(href); if (metaByHref.has(normalizedHref)) continue; const meta = resolveMarkdownFileLinkMeta(normalizedHref, cwd); @@ -1186,7 +1189,7 @@ function ChatMarkdown({ } } return metaByHref; - }, [cwd, text]); + }, [cwd, markdownText]); const fileLinkParentSuffixByPath = useMemo(() => { const filePaths = [...markdownFileLinkMetaByHref.values()].map((meta) => meta.filePath); return buildFileLinkParentSuffixByPath(filePaths); @@ -1253,7 +1256,9 @@ function ChatMarkdown({ li({ node, children, ...props }) { const listItemStart = node?.position?.start.offset; const markerOffset = - typeof listItemStart === "number" ? findTaskListMarkerOffset(text, listItemStart) : null; + typeof listItemStart === "number" + ? findTaskListMarkerOffset(markdownText, listItemStart) + : null; return (
  • {renderSkillInlineMarkdownChildren(children, skills)} @@ -1439,7 +1444,7 @@ function ChatMarkdown({ openMarkdownFileInPreview, resolvedTheme, skills, - text, + markdownText, threadRef, ], ); @@ -1462,7 +1467,7 @@ function ChatMarkdown({ components={markdownComponents} urlTransform={markdownUrlTransform} > - {text} + {markdownText} );