Skip to content

Commit 535b687

Browse files
perf(web): streaming replies re-render only their last Markdown block
Every streamed update re-parsed the whole reply and re-rendered every element in it, so a long reply cost more with each delta. While a reply streams, split it at blank lines that close every open block and render each block through its own memoized react-markdown, so finished blocks keep their render. Text with syntax that spans blocks (definitions, footnotes, raw HTML, container directives, display math) still renders whole, and so does the finished reply. The renderer context also rebuilt its file-link maps from the text on every update; key them by the file links found so streamed text keeps them stable. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 739c65d commit 535b687

4 files changed

Lines changed: 194 additions & 43 deletions

File tree

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 87 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ import {
189189
import { resolveLinkTarget } from "../browser/browserLinkTarget";
190190
import { PullRequestLinkPreview } from "./pullRequest/PullRequestLinkPreview";
191191
import { MarkdownCodeBlock, SuspenseShikiCodeBlock } from "./chat/MarkdownCodeBlock";
192+
import { splitMarkdownBlocks } from "./chat/markdownStreamBlocks";
192193
import { MarkdownMermaidCodeBlock } from "./chat/MarkdownMermaidCodeBlock";
193194
import { MarkdownTable } from "./chat/MarkdownTable";
194195
import { MarkdownDetails } from "./chat/MarkdownDetails";
@@ -434,6 +435,44 @@ const markdownServerConfigFieldsAtom = Atom.family((environmentId: EnvironmentId
434435
}).pipe(Atom.withLabel("markdown-server-config-fields")),
435436
);
436437

438+
function fileLinkSourcesKey(
439+
text: string,
440+
cwd: string | undefined,
441+
imageBaseDir: string | undefined,
442+
): string {
443+
const hrefs = extractMarkdownLinkHrefs(renderCodexInlineDirectivesAsMarkdown(text))
444+
.filter((href) => !parseComposerContextHref(href))
445+
.map(normalizeMarkdownLinkHrefKey)
446+
.filter((href) => resolveMarkdownFileLinkMeta(href, cwd, imageBaseDir ?? cwd));
447+
const spans = extractInlineCodeSpans(text).filter((span) =>
448+
resolveInlineCodeFileLinkMeta(span, cwd, imageBaseDir ?? cwd),
449+
);
450+
return JSON.stringify([[...new Set(hrefs)], [...new Set(spans)]]);
451+
}
452+
453+
function resolveFileLinkSources(
454+
key: string,
455+
cwd: string | undefined,
456+
imageBaseDir: string | undefined,
457+
) {
458+
const [hrefs, spans] = JSON.parse(key) as [string[], string[]];
459+
const markdownFileLinkMetaByHref = new Map<string, MarkdownFileLinkMeta>();
460+
for (const href of hrefs) {
461+
const meta = resolveMarkdownFileLinkMeta(href, cwd, imageBaseDir ?? cwd);
462+
if (meta) markdownFileLinkMetaByHref.set(href, meta);
463+
}
464+
const inlineCodeFileLinkMetaByText = new Map<string, MarkdownFileLinkMeta>();
465+
for (const span of spans) {
466+
const meta = resolveInlineCodeFileLinkMeta(span, cwd, imageBaseDir ?? cwd);
467+
if (meta) inlineCodeFileLinkMetaByText.set(span, meta);
468+
}
469+
const fileLinkParentSuffixByPath = buildFileLinkParentSuffixByPath([
470+
...[...markdownFileLinkMetaByHref.values()].map((meta) => meta.filePath),
471+
...[...inlineCodeFileLinkMetaByText.values()].map((meta) => meta.filePath),
472+
]);
473+
return { markdownFileLinkMetaByHref, inlineCodeFileLinkMetaByText, fileLinkParentSuffixByPath };
474+
}
475+
437476
function useChatMarkdownState({
438477
text,
439478
inline = false,
@@ -605,40 +644,6 @@ function useChatMarkdownState({
605644
});
606645
};
607646
const diffThemeName = resolveDiffThemeName(resolvedTheme);
608-
const markdownFileLinkMetaByHref = (() => {
609-
const metaByHref = new Map<
610-
string,
611-
NonNullable<ReturnType<typeof resolveMarkdownFileLinkMeta>>
612-
>();
613-
for (const href of extractMarkdownLinkHrefs(renderCodexInlineDirectivesAsMarkdown(text))) {
614-
if (parseComposerContextHref(href)) continue;
615-
const normalizedHref = normalizeMarkdownLinkHrefKey(href);
616-
if (metaByHref.has(normalizedHref)) continue;
617-
const meta = resolveMarkdownFileLinkMeta(normalizedHref, cwd, imageBaseDir ?? cwd);
618-
if (meta) {
619-
metaByHref.set(normalizedHref, meta);
620-
}
621-
}
622-
return metaByHref;
623-
})();
624-
const inlineCodeFileLinkMetaByText = (() => {
625-
const metaByText = new Map<string, MarkdownFileLinkMeta>();
626-
for (const span of extractInlineCodeSpans(text)) {
627-
if (metaByText.has(span)) continue;
628-
const meta = resolveInlineCodeFileLinkMeta(span, cwd, imageBaseDir ?? cwd);
629-
if (meta) {
630-
metaByText.set(span, meta);
631-
}
632-
}
633-
return metaByText;
634-
})();
635-
const fileLinkParentSuffixByPath = (() => {
636-
const filePaths = [
637-
...[...markdownFileLinkMetaByHref.values()].map((meta) => meta.filePath),
638-
...[...inlineCodeFileLinkMetaByText.values()].map((meta) => meta.filePath),
639-
];
640-
return buildFileLinkParentSuffixByPath(filePaths);
641-
})();
642647
const markdownUrlTransform = (href: string) => {
643648
if (parseAssistantCitationHref(href)) return href;
644649
if (parseComposerContextHref(href)) return href;
@@ -666,6 +671,12 @@ function useChatMarkdownState({
666671
// Subscribe so hydrated settings apply before the anchor synchronously decides whether to intercept _blank.
667672
const linkTargetPreference = useClientSettings((settings) => settings.browserLinkTarget);
668673
const mathEnabled = useClientSettings((settings) => settings.mathRenderingEnabled);
674+
// Keyed by the file links found, not the text, so streamed text that adds none keeps the
675+
// renderer context, and every block reading it, unchanged.
676+
// A template literal marks the key as a string, so the compiler memoizes the maps on its value.
677+
const fileLinksKey = `${fileLinkSourcesKey(text, cwd, imageBaseDir)}`;
678+
const { markdownFileLinkMetaByHref, inlineCodeFileLinkMetaByText, fileLinkParentSuffixByPath } =
679+
resolveFileLinkSources(fileLinksKey, cwd, imageBaseDir);
669680
const resolveThreadPullRequest = (
670681
href: string,
671682
): (ThreadPullRequestKey & { readonly url: string }) | null => {
@@ -1622,6 +1633,32 @@ const CHAT_MARKDOWN_COMPONENTS = {
16221633
},
16231634
} satisfies Components;
16241635

1636+
const MarkdownSource = memo(function MarkdownSource({
1637+
text,
1638+
remarkPlugins,
1639+
rehypePlugins,
1640+
urlTransform,
1641+
}: {
1642+
text: string;
1643+
remarkPlugins: NonNullable<ReactMarkdownOptions["remarkPlugins"]>;
1644+
rehypePlugins: NonNullable<ReactMarkdownOptions["rehypePlugins"]>;
1645+
urlTransform: ReactMarkdownOptions["urlTransform"];
1646+
}) {
1647+
return (
1648+
<ChatMarkdownTextContext value={text}>
1649+
<ReactMarkdown
1650+
remarkPlugins={remarkPlugins}
1651+
rehypePlugins={rehypePlugins}
1652+
skipHtml={false}
1653+
components={CHAT_MARKDOWN_COMPONENTS}
1654+
urlTransform={urlTransform}
1655+
>
1656+
{text}
1657+
</ReactMarkdown>
1658+
</ChatMarkdownTextContext>
1659+
);
1660+
});
1661+
16251662
function ChatMarkdown({
16261663
text,
16271664
className,
@@ -1639,8 +1676,17 @@ function ChatMarkdown({
16391676
localMediaPreview,
16401677
closeLocalMediaPreview,
16411678
} = useChatMarkdownState({ text, inline, ...props });
1679+
// Streaming re-renders only the last block; offsets for task edits need the whole text.
1680+
const streamingBlocks =
1681+
props.isStreaming === true &&
1682+
!inline &&
1683+
extraRemarkPlugins.length === 0 &&
1684+
!props.onTaskListChange
1685+
? splitMarkdownBlocks(text)
1686+
: null;
16421687
const incrementalParsing =
16431688
props.isStreaming === true &&
1689+
streamingBlocks === null &&
16441690
extraRemarkPlugins.length === 0 &&
16451691
/(?:^|\n) {0,3}(?:`{3}|~{3})/.test(text);
16461692
const Root = inline ? "span" : "div";
@@ -1668,21 +1714,20 @@ function ChatMarkdown({
16681714
onCopy={handleCopy}
16691715
>
16701716
<ChatMarkdownRendererContext value={componentState}>
1671-
<ChatMarkdownTextContext value={text}>
1672-
<ReactMarkdown
1717+
{(streamingBlocks ?? [text]).map((source, index) => (
1718+
<MarkdownSource
1719+
// oxlint-disable-next-line react/no-array-index-key -- streamed blocks only change at the end
1720+
key={index}
1721+
text={source}
16731722
remarkPlugins={remarkPlugins}
16741723
rehypePlugins={
16751724
parseRawHtml
16761725
? CHAT_MARKDOWN_RENDER_REHYPE_PLUGINS
16771726
: CHAT_MARKDOWN_LITERAL_HTML_REHYPE_PLUGINS
16781727
}
1679-
skipHtml={false}
1680-
components={CHAT_MARKDOWN_COMPONENTS}
16811728
urlTransform={markdownUrlTransform}
1682-
>
1683-
{text}
1684-
</ReactMarkdown>
1685-
</ChatMarkdownTextContext>
1729+
/>
1730+
))}
16861731
</ChatMarkdownRendererContext>
16871732
{localMediaPreview ? (
16881733
<ExpandedImageDialog preview={localMediaPreview} onClose={closeLocalMediaPreview} />
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {
2+
CHAT_MARKDOWN_REHYPE_PLUGINS,
3+
CHAT_MARKDOWN_REMARK_PLUGINS,
4+
} from "@t3tools/shared/markdownPipeline";
5+
import { renderToStaticMarkup } from "react-dom/server";
6+
import ReactMarkdown from "react-markdown";
7+
import { describe, expect, it } from "vite-plus/test";
8+
9+
import { splitMarkdownBlocks } from "./markdownStreamBlocks";
10+
11+
// Whitespace between top-level blocks collapses in the rendered message.
12+
const html = (source: string) =>
13+
renderToStaticMarkup(
14+
<ReactMarkdown
15+
remarkPlugins={CHAT_MARKDOWN_REMARK_PLUGINS}
16+
rehypePlugins={CHAT_MARKDOWN_REHYPE_PLUGINS}
17+
>
18+
{source}
19+
</ReactMarkdown>,
20+
)
21+
.replace(/>\s+</g, "><")
22+
.trim();
23+
24+
describe("splitMarkdownBlocks", () => {
25+
it.each([
26+
"# Title\n\nFirst paragraph with `code` and **bold**.\n\nSecond [link](https://example.com).",
27+
"- one\n- two\n\n- loose three\n\n continued\n\nAfter the list.",
28+
"1. first\n\n2. second\n\nText\n\n3) restarted",
29+
"```ts\nconst a = 1;\n\n\nconst b = 2;\n```\n\nafter\n\n~~~\nx\n\n~~~~\n\ny\n~~~\n\nend",
30+
"> quote\n\n> another quote\n\n> [!NOTE]\n> alert",
31+
"| a | b |\n| - | - |\n| 1 | 2 |\n\nAfter the table.",
32+
"para\n\n indented code\n\nnext",
33+
"Setext\n===\n\nnext\n---\n\n- [ ] task\n- [x] done",
34+
])("renders each block as the whole text renders it, at every streamed prefix: %j", (text) => {
35+
for (let end = 0; end <= text.length; end++) {
36+
const source = text.slice(0, end);
37+
const blocks = splitMarkdownBlocks(source);
38+
expect(blocks?.join("")).toBe(source);
39+
expect(blocks!.map(html).join("")).toBe(html(source));
40+
}
41+
});
42+
43+
it("splits only between closed blocks", () => {
44+
expect(splitMarkdownBlocks("a\n\n```\nb\n\nc\n```\n\n- d\n\n- e\n\nf")).toEqual([
45+
"a\n\n",
46+
"```\nb\n\nc\n```\n\n- d\n\n- e\n\n",
47+
"f",
48+
]);
49+
});
50+
51+
it.each([
52+
"[ref]\n\n[ref]: /later",
53+
"a[^x]\n\n[^x]: note",
54+
"<details>\n\nbody\n\n</details>",
55+
":::note\n\ncontent\n\n:::",
56+
"$$\na\n\nb\n$$",
57+
"a\r\n\r\nb",
58+
])("keeps text with document-wide syntax whole: %j", (text) => {
59+
expect(splitMarkdownBlocks(text)).toBeNull();
60+
});
61+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Definitions, footnotes, raw HTML, container directives and display math can tie blocks together.
2+
const DOCUMENT_WIDE_SYNTAX = /^ {0,3}(?:\[[^\]\n]+\]:|<[A-Za-z!?/]|:::|\$\$)|\[\^/m;
3+
const FENCE_PATTERN = /^ {0,3}(`{3,}|~{3,})/;
4+
// After a blank line these still continue the previous block: a list item or indented content.
5+
const CONTINUATION_PATTERN = /^(?:[ \t]|(?:[-*+]|\d{1,9}[.)])(?:[ \t]|$))/;
6+
7+
/**
8+
* Cuts Markdown at blank lines that close every open block, so each piece renders exactly as it
9+
* would inside the whole text. Returns null when the text uses syntax that spans blocks.
10+
*/
11+
export function splitMarkdownBlocks(text: string): string[] | null {
12+
if (text.includes("\r") || DOCUMENT_WIDE_SYNTAX.test(text)) return null;
13+
const blocks: string[] = [];
14+
let blockStart = 0;
15+
let lineStart = 0;
16+
let fence: string | null = null;
17+
let afterBlankLine = false;
18+
for (const line of text.split("\n")) {
19+
const opener = FENCE_PATTERN.exec(line)?.[1];
20+
if (fence !== null) {
21+
if (
22+
opener &&
23+
opener[0] === fence[0] &&
24+
opener.length >= fence.length &&
25+
line.trim() === opener
26+
) {
27+
fence = null;
28+
}
29+
} else if (line.trim() === "") {
30+
afterBlankLine = true;
31+
} else {
32+
if (afterBlankLine && lineStart > blockStart && !CONTINUATION_PATTERN.test(line)) {
33+
blocks.push(text.slice(blockStart, lineStart));
34+
blockStart = lineStart;
35+
}
36+
afterBlankLine = false;
37+
fence = opener ?? null;
38+
}
39+
lineStart += line.length + 1;
40+
}
41+
blocks.push(text.slice(blockStart));
42+
return blocks;
43+
}

‎apps/web/test/appScenarios.renders.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ it("app-stream-assistant-turn", async () => {
182182
const counted = await measure(async () => {
183183
let text = "";
184184
for (let delta = 0; delta < STREAM_DELTAS; delta++) {
185-
text += `word${delta} `;
185+
text += `word${delta}\n\n`;
186186
await act(() =>
187187
emitThreadEvent(LIVE, {
188188
type: "turn-item.updated",
@@ -204,6 +204,8 @@ it("app-stream-assistant-turn", async () => {
204204
// Only the streaming row changes; history rows must not re-render with it.
205205
expect(counted.components.UserTimelineRow).toBeUndefined();
206206
expect(counted.components.AssistantTimelineRow).toBe(STREAM_DELTAS);
207+
// Each delta adds a paragraph; finished blocks keep their render.
208+
expect(counted.components.MarkdownParagraph).toBe(STREAM_DELTAS);
207209
await report("app-stream-assistant-turn", [counted], { commits: false });
208210
});
209211

0 commit comments

Comments
 (0)