@@ -960,9 +960,29 @@ function _Chat() {
960
960
( scrollRef . current . scrollTop + scrollRef . current . clientHeight ) ,
961
961
) <= 1
962
962
: false ;
963
+ const isAttachWithTop = useCallback ( ( ) => {
964
+ // if user is typing, should scroll
965
+ if ( ! scrollRef . current ) return false ;
966
+ // get lastMessageId
967
+ const lastMessageId = session . messages [ session . messages . length - 1 ] ?. id ;
968
+ if ( ! lastMessageId ) return false ;
969
+ const lastMessage = document . getElementById ( lastMessageId ) ;
970
+ if ( ! lastMessage ) return false ;
971
+
972
+ const topDistance =
973
+ lastMessage . getBoundingClientRect ( ) . top -
974
+ scrollRef . current . getBoundingClientRect ( ) . top ;
975
+ // leave some space for user question
976
+ return topDistance < 100 ;
977
+ } , [ session . messages , scrollRef ] ) ;
978
+
979
+ const isTyping = userInput !== "" ;
980
+
981
+ // if user is typing, should auto scroll to bottom
982
+ // if user is not typing, should auto scroll to bottom only if already at bottom
963
983
const { setAutoScroll, scrollDomToBottom } = useScrollToBottom (
964
984
scrollRef ,
965
- isScrolledToBottom ,
985
+ ( isScrolledToBottom || isAttachWithTop ( ) ) && ! isTyping ,
966
986
) ;
967
987
const [ hitBottom , setHitBottom ] = useState ( true ) ;
968
988
const isMobileScreen = useMobileScreen ( ) ;
@@ -1717,6 +1737,7 @@ function _Chat() {
1717
1737
? styles [ "chat-message-user" ]
1718
1738
: styles [ "chat-message" ]
1719
1739
}
1740
+ id = { message . id }
1720
1741
>
1721
1742
< div className = { styles [ "chat-message-container" ] } >
1722
1743
< div className = { styles [ "chat-message-header" ] } >
0 commit comments