@devries-cameron20 / t3code Mirror · read-only / commits / d8c926c395
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 12:00 UTC

perf(web): streaming replies re-render only their last Markdown block (#27417)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 30, 2026 at 02:10 UTC d8c926c395155be72b09be594cb498adda3dfed2
4 files changed +194 -43
apps/web/src/components/ChatMarkdown.tsx
+87 -42
@@ -189,6 +189,7 @@ import {
189 import { resolveLinkTarget } from "../browser/browserLinkTarget";
190 import { PullRequestLinkPreview } from "./pullRequest/PullRequestLinkPreview";
191 import { MarkdownCodeBlock, SuspenseShikiCodeBlock } from "./chat/MarkdownCodeBlock";
192 +import { splitMarkdownBlocks } from "./chat/markdownStreamBlocks";
193 import { MarkdownMermaidCodeBlock } from "./chat/MarkdownMermaidCodeBlock";
194 import { MarkdownTable } from "./chat/MarkdownTable";
195 import { MarkdownDetails } from "./chat/MarkdownDetails";
@@ -434,6 +435,44 @@ const markdownServerConfigFieldsAtom = Atom.family((environmentId: EnvironmentId
435 }).pipe(Atom.withLabel("markdown-server-config-fields")),
436 );
437
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 +
476 function useChatMarkdownState({
477 text,
478 inline = false,
@@ -605,40 +644,6 @@ function useChatMarkdownState({
644 });
645 };
646 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 - })();
647 const markdownUrlTransform = (href: string) => {
648 if (parseAssistantCitationHref(href)) return href;
649 if (parseComposerContextHref(href)) return href;
@@ -666,6 +671,12 @@ function useChatMarkdownState({
671 // Subscribe so hydrated settings apply before the anchor synchronously decides whether to intercept _blank.
672 const linkTargetPreference = useClientSettings((settings) => settings.browserLinkTarget);
673 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);
680 const resolveThreadPullRequest = (
681 href: string,
682 ): (ThreadPullRequestKey & { readonly url: string }) | null => {
@@ -1622,6 +1633,32 @@ const CHAT_MARKDOWN_COMPONENTS = {
1633 },
1634 } satisfies Components;
1635
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 +
1662 function ChatMarkdown({
1663 text,
1664 className,
@@ -1639,8 +1676,17 @@ function ChatMarkdown({
1676 localMediaPreview,
1677 closeLocalMediaPreview,
1678 } = 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;
1687 const incrementalParsing =
1688 props.isStreaming === true &&
1689 + streamingBlocks === null &&
1690 extraRemarkPlugins.length === 0 &&
1691 /(?:^|\n) {0,3}(?:`{3}|~{3})/.test(text);
1692 const Root = inline ? "span" : "div";
@@ -1668,21 +1714,20 @@ function ChatMarkdown({
1714 onCopy={handleCopy}
1715 >
1716 <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}
1722 remarkPlugins={remarkPlugins}
1723 rehypePlugins={
1724 parseRawHtml
1725 ? CHAT_MARKDOWN_RENDER_REHYPE_PLUGINS
1726 : CHAT_MARKDOWN_LITERAL_HTML_REHYPE_PLUGINS
1727 }
1679 - skipHtml={false}
1680 - components={CHAT_MARKDOWN_COMPONENTS}
1728 urlTransform={markdownUrlTransform}
1682 - >
1683 - {text}
1684 - </ReactMarkdown>
1685 - </ChatMarkdownTextContext>
1729 + />
1730 + ))}
1731 </ChatMarkdownRendererContext>
1732 {localMediaPreview ? (
1733 <ExpandedImageDialog preview={localMediaPreview} onClose={closeLocalMediaPreview} />
apps/web/src/components/chat/markdownStreamBlocks.test.tsx new
+61
@@ -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 +});
apps/web/src/components/chat/markdownStreamBlocks.ts new
+43
@@ -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
+3 -1
@@ -182,7 +182,7 @@ it("app-stream-assistant-turn", async () => {
182 const counted = await measure(async () => {
183 let text = "";
184 for (let delta = 0; delta < STREAM_DELTAS; delta++) {
185 - text += `word${delta} `;
185 + text += `word${delta}\n\n`;
186 await act(() =>
187 emitThreadEvent(LIVE, {
188 type: "turn-item.updated",
@@ -204,6 +204,8 @@ it("app-stream-assistant-turn", async () => {
204 // Only the streaming row changes; history rows must not re-render with it.
205 expect(counted.components.UserTimelineRow).toBeUndefined();
206 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);
209 await report("app-stream-assistant-turn", [counted], { commits: false });
210 });
211