Defer streaming chat markdown rendering
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
20f37f367a
commit
0d73e66a1c
2 files changed
+50
-5
No files matched your search
@@ -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<typeof import("react")>()),
|
||||
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(
|
||||
<ChatMarkdown text="latest markdown" cwd={undefined} isStreaming />,
|
||||
);
|
||||
|
||||
expect(reactMock.useDeferredValue).toHaveBeenCalledWith("latest markdown");
|
||||
expect(markup).toContain("<strong>deferred markdown</strong>");
|
||||
expect(markup).not.toContain("latest markdown");
|
||||
});
|
||||
|
||||
it("renders the latest markdown immediately after streaming finishes", () => {
|
||||
reactMock.useDeferredValue.mockReturnValue("stale markdown");
|
||||
|
||||
const markup = renderToStaticMarkup(<ChatMarkdown text="final markdown" cwd={undefined} />);
|
||||
|
||||
expect(markup).toContain("final markdown");
|
||||
expect(markup).not.toContain("stale markdown");
|
||||
});
|
||||
});
|
||||
@@ -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<ReturnType<typeof resolveMarkdownFileLinkMeta>>
|
||||
>();
|
||||
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 (
|
||||
<li {...props} data-task-marker-offset={markerOffset ?? undefined}>
|
||||
{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}
|
||||
</ReactMarkdown>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user