Skip to content

Commit 6ca128e

Browse files
committed
perf(mobile): convert only the changed block of a streaming assistant message
A streamed reply re-ran the whole Markdown-to-native conversion on every update. While a message is streaming, split its text at blank lines that close every open block, keep the previous update's converted blocks, and convert only the blocks that changed. Finished text, every other consumer and text with syntax that ties blocks together keep the whole-text conversion.
1 parent 9e85538 commit 6ca128e

6 files changed

Lines changed: 597 additions & 33 deletions

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,7 @@ const AssistantMarkdownContent = memo(function AssistantMarkdownContent(props: {
11911191
readonly onUseArtifactTemplate?: ((template: CodexArtifactTemplate) => void) | undefined;
11921192
readonly renderImage: MarkdownImageRenderer;
11931193
readonly skills?: ReadonlyArray<SelectableMarkdownSkill> | undefined;
1194+
readonly streaming?: boolean | undefined;
11941195
}) {
11951196
const liveMarkdown = useLiveThreadLinkLabels(props.markdown, props.environmentId);
11961197
const threadIcons = useThreadChipIcons(props.environmentId, liveMarkdown);
@@ -1208,6 +1209,7 @@ const AssistantMarkdownContent = memo(function AssistantMarkdownContent(props: {
12081209
onUseArtifactTemplate={props.onUseArtifactTemplate}
12091210
renderImage={props.renderImage}
12101211
skills={props.skills}
1212+
streaming={props.streaming}
12111213
/>
12121214
);
12131215
});
@@ -1220,6 +1222,7 @@ function AssistantMarkdownBlocks(props: {
12201222
readonly onUseArtifactTemplate?: ((template: CodexArtifactTemplate) => void) | undefined;
12211223
readonly renderImage: MarkdownImageRenderer;
12221224
readonly skills?: ReadonlyArray<SelectableMarkdownSkill> | undefined;
1225+
readonly streaming?: boolean | undefined;
12231226
}) {
12241227
const blocks = useMemo(
12251228
() =>
@@ -1262,6 +1265,7 @@ function AssistantMarkdownBlocks(props: {
12621265
textStyle={props.markdownStyles.nativeTextStyle}
12631266
{...props.linkHandlers}
12641267
renderImage={props.renderImage}
1268+
streaming={props.streaming}
12651269
/>
12661270
);
12671271
});
@@ -2266,6 +2270,7 @@ function renderFeedEntry(
22662270
onUseArtifactTemplate={props.onUseArtifactTemplate}
22672271
renderImage={props.renderMarkdownImage}
22682272
skills={props.skills}
2273+
streaming={message.streaming}
22692274
/>
22702275
</MarkdownImageAvailableWidthContext>
22712276
) : null}

‎apps/mobile/src/lib/markdownDocument.ts‎

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -287,25 +287,35 @@ export function markdownWithHtml(markdown: string): string {
287287
// Start only at the beginning of a backtick run to avoid retrying every suffix.
288288
const INLINE_CODE_SPAN = /(?<!`)`+([^`\r\n]+)`+/g;
289289

290-
export function markdownWithInlineFileLinks(markdown: string, fileLink: RegExp): string {
291-
return transformNativeMarkdown(markdown, fileLink, false);
290+
/**
291+
* File links are rewritten only when some inline-code span of the document looks like a path.
292+
* That is a decision about the whole document, so a caller converting it in pieces passes it on.
293+
*/
294+
export function markdownMayLinkFiles(markdown: string, fileLink: RegExp): boolean {
295+
for (const [, code] of markdown.matchAll(INLINE_CODE_SPAN)) {
296+
if (fileLink.test(code!.trim())) return true;
297+
}
298+
return false;
299+
}
300+
301+
export function markdownWithInlineFileLinks(
302+
markdown: string,
303+
fileLink: RegExp,
304+
linkFiles?: boolean,
305+
): string {
306+
return transformNativeMarkdown(markdown, fileLink, false, linkFiles);
292307
}
293308

294309
function transformNativeMarkdown(
295310
markdown: string,
296311
fileLink: RegExp | undefined,
297312
tableBreaks: boolean,
313+
linkFilesOverride?: boolean,
298314
): string {
299315
const breakTables = tableBreaks && mayHaveTableUnderText(markdown);
300-
let linkFiles = false;
301-
if (fileLink) {
302-
for (const [, code] of markdown.matchAll(INLINE_CODE_SPAN)) {
303-
if (fileLink.test(code!.trim())) {
304-
linkFiles = true;
305-
break;
306-
}
307-
}
308-
}
316+
const linkFiles = fileLink
317+
? (linkFilesOverride ?? markdownMayLinkFiles(markdown, fileLink))
318+
: false;
309319
if (!linkFiles && !breakTables) return markdown;
310320
const edits: MarkdownEdit[] = [];
311321
const fileEdits: MarkdownEdit[] = [];
@@ -441,7 +451,16 @@ export function parseMarkdownDocument(markdown: string): Root {
441451
}
442452

443453
export function markdownForNativeRendering(markdown: string, fileLink: RegExp): string {
444-
return transformNativeMarkdown(markdownForNativeParsing(markdown), fileLink, true);
454+
return markdownRenderParsed(markdownForNativeParsing(markdown), fileLink);
455+
}
456+
457+
/** What `markdownForNativeRendering` does after `markdownForNativeParsing`. */
458+
export function markdownRenderParsed(
459+
parsed: string,
460+
fileLink: RegExp,
461+
linkFiles?: boolean,
462+
): string {
463+
return transformNativeMarkdown(parsed, fileLink, true, linkFiles);
445464
}
446465

447466
/** Native media slots only cover standalone images; nested file embeds must become links. */

0 commit comments

Comments
 (0)