@@ -181,6 +181,7 @@ import {
181181} from "../browser/openFileInPreview" ;
182182import { resolveLinkTarget } from "../browser/browserLinkTarget" ;
183183import { PullRequestLinkPreview } from "./pullRequest/PullRequestLinkPreview" ;
184+ import { streamingMarkdownRenderDelay } from "./ChatMarkdown.logic" ;
184185
185186interface ChatMarkdownProps {
186187 text : string ;
@@ -324,6 +325,33 @@ const highlightedCodeCache = new LRUCache<string>(
324325 MAX_HIGHLIGHT_CACHE_MEMORY_BYTES ,
325326) ;
326327
328+ function useStreamingMarkdownText ( text : string , isStreaming : boolean ) : string {
329+ const [ renderedText , setRenderedText ] = useState ( text ) ;
330+ const lastRenderedAtRef = useRef ( Date . now ( ) ) ;
331+
332+ useEffect ( ( ) => {
333+ if ( ! isStreaming ) {
334+ lastRenderedAtRef . current = Date . now ( ) ;
335+ setRenderedText ( ( current ) => ( current === text ? current : text ) ) ;
336+ return ;
337+ }
338+
339+ const delay = streamingMarkdownRenderDelay ( {
340+ lastRenderedAt : lastRenderedAtRef . current ,
341+ now : Date . now ( ) ,
342+ } ) ;
343+ const timeout = setTimeout ( ( ) => {
344+ lastRenderedAtRef . current = Date . now ( ) ;
345+ setRenderedText ( text ) ;
346+ } , delay ) ;
347+ return ( ) => {
348+ clearTimeout ( timeout ) ;
349+ } ;
350+ } , [ isStreaming , text ] ) ;
351+
352+ return isStreaming ? renderedText : text ;
353+ }
354+
327355function findTaskListMarkerOffset ( markdown : string , listItemStart : number ) : number | null {
328356 const firstLineEnd = markdown . indexOf ( "\n" , listItemStart ) ;
329357 const firstLine = markdown . slice (
@@ -2168,6 +2196,7 @@ function useChatMarkdownState({
21682196 imageBaseDir,
21692197 onImageExpand,
21702198} : ChatMarkdownProps ) {
2199+ const renderedText = useStreamingMarkdownText ( text , isStreaming ) ;
21712200 const { resolvedTheme } = useTheme ( ) ;
21722201 const [ localMediaPreview , setLocalMediaPreview ] = useState < ExpandedImagePreview | null > ( null ) ;
21732202 const expandMedia = onImageExpand ?? setLocalMediaPreview ;
@@ -2588,7 +2617,7 @@ function useChatMarkdownState({
25882617 resolvedTheme,
25892618 serverConfig,
25902619 skills,
2591- text,
2620+ text : renderedText ,
25922621 threadRef,
25932622 updateThreadPullRequestLink,
25942623 } ) ,
@@ -2614,7 +2643,7 @@ function useChatMarkdownState({
26142643 resolvedTheme ,
26152644 serverConfig ,
26162645 skills ,
2617- text ,
2646+ renderedText ,
26182647 threadRef ,
26192648 updateThreadPullRequestLink ,
26202649 ] ,
@@ -2624,6 +2653,7 @@ function useChatMarkdownState({
26242653 handleCopy,
26252654 markdownUrlTransform,
26262655 localMediaPreview,
2656+ renderedText,
26272657 setLocalMediaPreview,
26282658 } ;
26292659}
@@ -3110,6 +3140,7 @@ function ChatMarkdown({
31103140 handleCopy,
31113141 markdownUrlTransform,
31123142 localMediaPreview,
3143+ renderedText,
31133144 setLocalMediaPreview,
31143145 } = useChatMarkdownState ( { text, ...props } ) ;
31153146 const remarkPlugins = useMemo (
@@ -3139,7 +3170,7 @@ function ChatMarkdown({
31393170 components = { CHAT_MARKDOWN_COMPONENTS }
31403171 urlTransform = { markdownUrlTransform }
31413172 >
3142- { text }
3173+ { renderedText }
31433174 </ ReactMarkdown >
31443175 </ ChatMarkdownRendererContext >
31453176 { localMediaPreview ? (
0 commit comments