From b94ccc6db557f705ba4f810ed56c55ce21f383dd Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Tue, 24 Dec 2024 12:32:19 -0800 Subject: [PATCH 1/2] fix: scroll to the bottom on history change --- extensions/vscode/e2e/tests/GUI.test.ts | 62 ++++++++++++++ gui/src/components/ChatScrollAnchor.tsx | 38 --------- gui/src/pages/gui/Chat.tsx | 105 +++++++++++++----------- 3 files changed, 118 insertions(+), 87 deletions(-) delete mode 100644 gui/src/components/ChatScrollAnchor.tsx diff --git a/extensions/vscode/e2e/tests/GUI.test.ts b/extensions/vscode/e2e/tests/GUI.test.ts index d4c005eae..b5e3cabc3 100644 --- a/extensions/vscode/e2e/tests/GUI.test.ts +++ b/extensions/vscode/e2e/tests/GUI.test.ts @@ -193,6 +193,68 @@ describe("GUI Test", () => { }); describe("Chat Paths", () => { + it("Send many messages → chat auto scrolls → go to history → open previous chat → it is scrolled to the bottom", async () => { + for (let i = 0; i <= 20; i++) { + const { userMessage, llmResponse } = + TestUtils.generateTestMessagePair(i); + await GUIActions.sendMessage({ + view, + message: userMessage, + inputFieldIndex: i, + }); + const response = await TestUtils.waitForSuccess(() => + GUISelectors.getThreadMessageByText(view, llmResponse), + ); + + const viewportHeight = await driver.executeScript( + "return window.innerHeight", + ); + + const isInViewport = await driver.executeScript( + ` + const rect = arguments[0].getBoundingClientRect(); + return ( + rect.top >= 0 && + rect.bottom <= ${viewportHeight} + ); + `, + response, + ); + + expect(isInViewport).to.eq(true); + } + + await view.switchBack(); + + await (await GUISelectors.getHistoryNavButton(view)).click(); + await GUIActions.switchToReactIframe(); + TestUtils.waitForSuccess(async () => { + await (await GUISelectors.getNthHistoryTableRow(view, 0)).click(); + }); + + const { llmResponse } = TestUtils.generateTestMessagePair(20); + const response = await TestUtils.waitForSuccess(() => + GUISelectors.getThreadMessageByText(view, llmResponse), + ); + + const viewportHeight = await driver.executeScript( + "return window.innerHeight", + ); + + const isInViewport = await driver.executeScript( + ` + const rect = arguments[0].getBoundingClientRect(); + return ( + rect.top >= 0 && + rect.bottom <= ${viewportHeight} + ); + `, + response, + ); + + expect(isInViewport).to.eq(true); + }).timeout(DEFAULT_TIMEOUT.XL * 1000); + it("Open chat and send message → press arrow up and arrow down to cycle through messages → submit another message → press arrow up and arrow down to cycle through messages", async () => { await GUIActions.sendMessage({ view, diff --git a/gui/src/components/ChatScrollAnchor.tsx b/gui/src/components/ChatScrollAnchor.tsx deleted file mode 100644 index 9b32e7283..000000000 --- a/gui/src/components/ChatScrollAnchor.tsx +++ /dev/null @@ -1,38 +0,0 @@ -import { useEffect } from "react"; -import { useInView } from "react-intersection-observer"; -import { vscBackground } from "."; - -interface ChatScrollAnchorProps { - trackVisibility: boolean; - isAtBottom: boolean; - scrollAreaRef: React.RefObject; -} - -export function ChatScrollAnchor({ - trackVisibility, - isAtBottom, - scrollAreaRef, -}: ChatScrollAnchorProps) { - const { ref, inView, entry } = useInView(); - - useEffect(() => { - if (isAtBottom && trackVisibility && !inView) { - if (!scrollAreaRef.current) return; - - const scrollAreaElement = scrollAreaRef.current; - - scrollAreaElement.scrollTop = - scrollAreaElement.scrollHeight - scrollAreaElement.clientHeight; - } - }, [inView, entry, isAtBottom, trackVisibility]); - - return ( -
- ); -} diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index b7748bf22..8e11e39e5 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -19,7 +19,6 @@ import { lightGray, vscBackground, } from "../../components"; -import { ChatScrollAnchor } from "../../components/ChatScrollAnchor"; import CodeToEditCard from "../../components/CodeToEditCard"; import FeedbackDialog from "../../components/dialogs/FeedbackDialog"; import { useFindWidget } from "../../components/find/FindWidget"; @@ -75,6 +74,7 @@ import { loadLastSession, saveCurrentSession, } from "../../redux/thunks/session"; +import { C } from "core/autocomplete/constants/AutocompleteLanguageInfo"; const StopButton = styled.div` background-color: ${vscBackground}; @@ -132,6 +132,58 @@ function fallbackRender({ error, resetErrorBoundary }: any) { ); } +const useAutoScroll = ( + ref: React.RefObject, + history: unknown[], +) => { + const [userHasScrolled, setUserHasScrolled] = useState(false); + + useEffect(() => { + if (history.length) { + setUserHasScrolled(false); + } + }, [history.length]); + + useEffect(() => { + if (!ref.current || history.length === 0) return; + + const handleScroll = () => { + const elem = ref.current; + if (!elem) return; + + const isAtBottom = + Math.abs(elem.scrollHeight - elem.scrollTop - elem.clientHeight) < 1; + + /** + * We stop auto scrolling if a user manually scrolled up. + * We resume auto scrolling if a user manually scrolled to the bottom. + */ + setUserHasScrolled(!isAtBottom); + }; + + const resizeObserver = new ResizeObserver(() => { + const elem = ref.current; + if (!elem || userHasScrolled) return; + elem.scrollTop = elem.scrollHeight; + }); + + ref.current.addEventListener("scroll", handleScroll); + + // Observe the container + resizeObserver.observe(ref.current); + + // Observe all immediate children + Array.from(ref.current.children).forEach((child) => { + resizeObserver.observe(child); + }); + + return () => { + resizeObserver.disconnect(); + ref.current?.removeEventListener("scroll", handleScroll); + }; + }, [ref, history.length, userHasScrolled]); +}; + export function Chat() { const posthog = usePostHog(); const dispatch = useAppDispatch(); @@ -147,7 +199,6 @@ export function Chat() { const [stepsOpen, setStepsOpen] = useState<(boolean | undefined)[]>([]); const mainTextInputRef = useRef(null); const stepsDivRef = useRef(null); - const [isAtBottom, setIsAtBottom] = useState(false); const history = useAppSelector((state) => state.session.history); const showChatScrollbar = useAppSelector( (state) => state.config.config.ui?.showChatScrollbar, @@ -168,34 +219,6 @@ export function Chat() { selectIsSingleRangeEditOrInsertion, ); const lastSessionId = useAppSelector((state) => state.session.lastSessionId); - const snapToBottom = useCallback(() => { - if (!stepsDivRef.current) return; - const elem = stepsDivRef.current; - elem.scrollTop = elem.scrollHeight - elem.clientHeight; - - setIsAtBottom(true); - }, [stepsDivRef, setIsAtBottom]); - - const smoothScrollToBottom = useCallback(async () => { - if (!stepsDivRef.current) return; - const elem = stepsDivRef.current; - elem.scrollTo({ - top: elem.scrollHeight - elem.clientHeight, - behavior: "smooth", - }); - - setIsAtBottom(true); - }, [stepsDivRef, setIsAtBottom]); - - useEffect(() => { - if (isStreaming) snapToBottom(); - }, [isStreaming, snapToBottom]); - - // useEffect(() => { - // setTimeout(() => { - // smoothScrollToBottom(); - // }, 400); - // }, [smoothScrollToBottom, state.sessionId]); useEffect(() => { // Cmd + Backspace to delete current step @@ -215,18 +238,6 @@ export function Chat() { }; }, [isStreaming]); - const handleScroll = () => { - // Temporary fix to account for additional height when code blocks are added - const OFFSET_HERUISTIC = 300; - if (!stepsDivRef.current) return; - - const { scrollTop, scrollHeight, clientHeight } = stepsDivRef.current; - const atBottom = - scrollHeight - clientHeight <= scrollTop + OFFSET_HERUISTIC; - - setIsAtBottom(atBottom); - }; - const { widget, highlights } = useFindWidget(stepsDivRef); const sendInput = useCallback( @@ -340,6 +351,8 @@ export function Chat() { const showScrollbar = showChatScrollbar || window.innerHeight > 5000; + useAutoScroll(stepsDivRef, history); + return ( <> {isInEditMode && ( @@ -356,14 +369,13 @@ export function Chat() { 0 ? "flex-1" : ""}`} - onScroll={handleScroll} > {highlights} {history.map((item, index: number) => (
))} -
From 3f991b41b6d3035889dd6cf402febac7903ab07d Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Tue, 24 Dec 2024 14:29:42 -0800 Subject: [PATCH 2/2] improv: clean up code --- gui/src/pages/gui/Chat.tsx | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 8e11e39e5..de8453445 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -70,11 +70,7 @@ import ConfigErrorIndicator from "./ConfigError"; import { ToolCallDiv } from "./ToolCallDiv"; import { ToolCallButtons } from "./ToolCallDiv/ToolCallButtonsDiv"; import ToolOutput from "./ToolCallDiv/ToolOutput"; -import { - loadLastSession, - saveCurrentSession, -} from "../../redux/thunks/session"; -import { C } from "core/autocomplete/constants/AutocompleteLanguageInfo"; +import { loadLastSession } from "../../redux/thunks/session"; const StopButton = styled.div` background-color: ${vscBackground};