@@ -234,6 +234,7 @@ export type ChatPageHost = ChatHost &
234234 chatInputHistoryIndex : number ;
235235 chatDraftBeforeHistory : string | null ;
236236 chatScrollCommitCleanup : ( ( ) => void ) | null ;
237+ chatStreamRenderFrame : number | null ;
237238 chatScrollFrame : number | null ;
238239 chatScrollGuardFrame : number | null ;
239240 chatScrollTimeout : number | null ;
@@ -454,6 +455,7 @@ export function resetChatStateForRouteSession(
454455 previousComposerScope ?: StoredChatOutboxScope ;
455456 } = { } ,
456457) : ChatComposerRouteResetResult {
458+ cancelChatStreamRenderFrame ( state ) ;
457459 const previousSessionKey = state . sessionKey ;
458460 const previousComposerScopeKey = storedChatOutboxScopeKey (
459461 options . previousComposerScope ?? resolveStoredChatOutboxScope ( state , previousSessionKey ) ,
@@ -1263,6 +1265,7 @@ export function createPageState(
12631265 chatInputHistoryIndex : - 1 ,
12641266 chatDraftBeforeHistory : null ,
12651267 chatScrollCommitCleanup : null ,
1268+ chatStreamRenderFrame : null ,
12661269 chatScrollFrame : null ,
12671270 chatScrollGuardFrame : null ,
12681271 chatScrollTimeout : null ,
@@ -1387,23 +1390,23 @@ export function handlePageGatewayEvent(state: ChatPageHost, event: GatewayEventF
13871390 removeDeliveredQueuedChatSendForRun ( state , payload ?. runId ) ;
13881391 void resumeStoredChatOutboxes ( state ) ;
13891392 }
1390- requestPageUpdate ( state ) ;
1393+ requestChatPageUpdate ( state , payload ?. state === "delta" ? "animation-frame" : "immediate" ) ;
13911394 return ;
13921395 }
13931396 if ( event . event === "chat.side_result" ) {
13941397 if ( handleChatSideResultGatewayEvent ( state as unknown as ChatState , event . payload ) ) {
1395- requestPageUpdate ( state ) ;
1398+ requestChatPageUpdate ( state ) ;
13961399 }
13971400 return ;
13981401 }
13991402 if ( event . event === "agent" || event . event === "session.tool" ) {
14001403 handleAgentEvent ( state as never , event . payload as never ) ;
1401- requestPageUpdate ( state ) ;
1404+ requestChatPageUpdate ( state ) ;
14021405 return ;
14031406 }
14041407 if ( event . event === "session.operation" ) {
14051408 handleSessionOperationEvent ( state as never , event . payload as never ) ;
1406- requestPageUpdate ( state ) ;
1409+ requestChatPageUpdate ( state ) ;
14071410 return ;
14081411 }
14091412 if ( event . event === "chat.send_timing" ) {
@@ -1413,13 +1416,13 @@ export function handlePageGatewayEvent(state: ChatPageHost, event: GatewayEventF
14131416 if ( event . event === "session.message" ) {
14141417 handleSessionMessageEvent ( state , event . payload ) ;
14151418 void resumeStoredChatOutboxes ( state ) ;
1416- requestPageUpdate ( state ) ;
1419+ requestChatPageUpdate ( state ) ;
14171420 return ;
14181421 }
14191422 if ( event . event === "sessions.changed" ) {
14201423 handleSessionsChangedEvent ( state , event . payload ) ;
14211424 void resumeStoredChatOutboxes ( state ) ;
1422- requestPageUpdate ( state ) ;
1425+ requestChatPageUpdate ( state ) ;
14231426 return ;
14241427 }
14251428 if ( event . event === "task" ) {
@@ -1518,8 +1521,42 @@ function preserveDeliveredQueuedUserTurn(state: ChatPageHost, item: ChatQueueIte
15181521 }
15191522}
15201523
1521- function requestPageUpdate ( state : ChatPageHost ) {
1522- state . requestUpdate ?.( ) ;
1524+ type ChatPageUpdateMode = "immediate" | "animation-frame" ;
1525+
1526+ function cancelChatStreamRenderFrame ( state : Pick < ChatPageHost , "chatStreamRenderFrame" > ) : void {
1527+ const frame = state . chatStreamRenderFrame ;
1528+ if ( frame == null ) {
1529+ return ;
1530+ }
1531+ state . chatStreamRenderFrame = null ;
1532+ if ( typeof globalThis . cancelAnimationFrame === "function" ) {
1533+ globalThis . cancelAnimationFrame ( frame ) ;
1534+ }
1535+ }
1536+
1537+ export function requestChatPageUpdate (
1538+ state : Pick < ChatPageHost , "chatStreamRenderFrame" | "requestUpdate" > ,
1539+ mode : ChatPageUpdateMode = "immediate" ,
1540+ ) : void {
1541+ if ( mode === "immediate" || typeof globalThis . requestAnimationFrame !== "function" ) {
1542+ cancelChatStreamRenderFrame ( state ) ;
1543+ state . requestUpdate ?.( ) ;
1544+ return ;
1545+ }
1546+ if ( state . chatStreamRenderFrame != null ) {
1547+ return ;
1548+ }
1549+ // Deltas still mutate the canonical stream immediately. One frame owns the
1550+ // paint; terminal/non-stream events cancel it so stale partial UI cannot win.
1551+ let frame = 0 ;
1552+ frame = globalThis . requestAnimationFrame ( ( ) => {
1553+ if ( state . chatStreamRenderFrame !== frame ) {
1554+ return ;
1555+ }
1556+ state . chatStreamRenderFrame = null ;
1557+ state . requestUpdate ?.( ) ;
1558+ } ) ;
1559+ state . chatStreamRenderFrame = frame ;
15231560}
15241561
15251562type ChatRenderLifecycleScope = {
@@ -1578,6 +1615,7 @@ export class ChatStateController<TState extends ChatPageHost> implements Reactiv
15781615 attach ( state : TState ) {
15791616 if ( this . stateValue && this . stateValue !== state ) {
15801617 this . composerPersistence . stop ( ) ;
1618+ cancelChatStreamRenderFrame ( this . stateValue ) ;
15811619 cancelChatScroll ( this . stateValue ) ;
15821620 }
15831621 this . stateValue = state ;
@@ -1859,6 +1897,7 @@ export class ChatStateController<TState extends ChatPageHost> implements Reactiv
18591897 }
18601898 const state = this . stateValue ;
18611899 if ( state ) {
1900+ cancelChatStreamRenderFrame ( state ) ;
18621901 cancelChatScroll ( state ) ;
18631902 clearSessionWorkspaceTimers ( state ) ;
18641903 }
0 commit comments