From 652b08d363152f871415d68cc9fdaedda1dcd169 Mon Sep 17 00:00:00 2001 From: Chris Scott Date: Sat, 29 Nov 2025 11:58:47 -0500 Subject: [PATCH] Auto-scroll during streaming after user sends a message --- .../src/components/message/MessageThread.tsx | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/frontend/src/components/message/MessageThread.tsx b/frontend/src/components/message/MessageThread.tsx index 47e1cf47..7b0b05b7 100644 --- a/frontend/src/components/message/MessageThread.tsx +++ b/frontend/src/components/message/MessageThread.tsx @@ -36,10 +36,14 @@ export const MessageThread = memo(function MessageThread({ opcodeUrl, sessionID, const { data: messages, isLoading, error } = useMessages(opcodeUrl, sessionID, directory) const userScrolledUpRef = useRef(false) const hasInitialScrolledRef = useRef(false) + const lastMessageCountRef = useRef(0) + const isFollowingRef = useRef(true) useEffect(() => { hasInitialScrolledRef.current = false userScrolledUpRef.current = false + lastMessageCountRef.current = 0 + isFollowingRef.current = true }, [sessionID]) const scrollToBottom = useCallback(() => { @@ -57,6 +61,11 @@ export const MessageThread = memo(function MessageThread({ opcodeUrl, sessionID, const handleScroll = () => { const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight const isScrolledUp = distanceFromBottom > SCROLL_THRESHOLD + + if (isScrolledUp) { + isFollowingRef.current = false + } + if (userScrolledUpRef.current !== isScrolledUp) { userScrolledUpRef.current = isScrolledUp onScrollStateChange?.(isScrolledUp) @@ -70,9 +79,27 @@ export const MessageThread = memo(function MessageThread({ opcodeUrl, sessionID, useEffect(() => { if (!containerRef?.current || !messages) return - if (!hasInitialScrolledRef.current && messages.length > 0) { + const currentCount = messages.length + const prevCount = lastMessageCountRef.current + lastMessageCountRef.current = currentCount + + if (!hasInitialScrolledRef.current && currentCount > 0) { hasInitialScrolledRef.current = true scrollToBottom() + return + } + + if (currentCount > prevCount) { + const newMessage = messages[currentCount - 1] + if (newMessage?.info.role === 'user') { + isFollowingRef.current = true + scrollToBottom() + return + } + } + + if (isFollowingRef.current) { + scrollToBottom() } }, [messages, containerRef, scrollToBottom])