@@ -484,20 +484,37 @@ function setSrStatus(message) {
484484 }
485485}
486486
487+ function isNearMessageBottom ( threshold = 80 ) {
488+ return messages . scrollHeight - messages . scrollTop - messages . clientHeight < threshold ;
489+ }
490+
487491function scrollToBottom ( force = false ) {
488- const distanceFromBottom =
489- messages . scrollHeight - messages . scrollTop - messages . clientHeight ;
490- if ( ! force && distanceFromBottom >= 80 ) {
492+ if ( ! force && ! isNearMessageBottom ( ) ) {
491493 return ;
492494 }
493495 messages . scrollTop = messages . scrollHeight ;
494496}
495497
496498function keepLatestTurnVisible ( force = false ) {
497499 const schedule = window . requestAnimationFrame || ( ( callback ) => setTimeout ( callback , 0 ) ) ;
498- schedule ( ( ) => scrollToBottom ( force ) ) ;
500+ const shouldForce = force || Boolean ( activeResponseState ?. keepLatestTurnPinned ) ;
501+ schedule ( ( ) => {
502+ scrollToBottom ( shouldForce ) ;
503+ if ( shouldForce ) {
504+ schedule ( ( ) => scrollToBottom ( true ) ) ;
505+ }
506+ } ) ;
499507}
500508
509+ messages . addEventListener ( "scroll" , ( ) => {
510+ if ( ! activeResponseState ?. keepLatestTurnPinned ) {
511+ return ;
512+ }
513+ if ( ! isNearMessageBottom ( 160 ) ) {
514+ activeResponseState . keepLatestTurnPinned = false ;
515+ }
516+ } ) ;
517+
501518function clearWelcome ( ) {
502519 const welcome = messages . querySelector ( ".welcome-panel" ) ;
503520 if ( welcome ) {
@@ -929,6 +946,7 @@ function schedulePendingAnswerRefresh(forSessionId, attempt = 0) {
929946
930947async function loadHistory ( ) {
931948 if ( ! sessionId ) {
949+ setSrStatus ( "" ) ;
932950 showWelcomeIfEmpty ( ) ;
933951 return ;
934952 }
@@ -1035,6 +1053,7 @@ function startNewChat() {
10351053 applyPrivateChatState ( false , { restoreModel : wasPrivate } ) ;
10361054 window . localStorage . removeItem ( sessionStorageKey ) ;
10371055 messages . textContent = "" ;
1056+ setSrStatus ( "" ) ;
10381057 showWelcomeIfEmpty ( ) ;
10391058 input . focus ( ) ;
10401059}
@@ -2895,6 +2914,7 @@ async function sendMessage() {
28952914 statusEl : null ,
28962915 currentStatus : initialStatus ,
28972916 stopped : false ,
2917+ keepLatestTurnPinned : true ,
28982918 } ;
28992919 const controller = new AbortController ( ) ;
29002920 activeResponseController = controller ;
@@ -2973,13 +2993,14 @@ async function sendMessage() {
29732993 }
29742994 finalizeActivityFeed ( state ) ;
29752995 if ( ! state . failed ) {
2996+ const shouldKeepLatestTurnVisible = isNearMessageBottom ( ) ;
29762997 if ( state . bodyEl ) {
29772998 revealAssistantBody ( pending , state ) ;
29782999 state . bodyEl . classList . remove ( "streaming-cursor" ) ;
29793000 await renderFinalMarkdown ( state . bodyEl , state . raw ) ;
29803001 }
29813002 pending . appendChild ( makeMetaRow ( pending , state . displayModel , state . routing ) ) ;
2982- scrollToBottom ( ) ;
3003+ keepLatestTurnVisible ( shouldKeepLatestTurnVisible ) ;
29833004 setSrStatus (
29843005 state . displayModel ? `Reply ready from ${ state . displayModel } ` : "Reply ready"
29853006 ) ;
0 commit comments