From 678a75f8f7b97b0f75407c6e1b1dd78c415d22eb Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Mon, 12 May 2025 17:15:27 -0700 Subject: [PATCH 1/3] fix: prevent autoscrolling on new tool msgs --- gui/src/pages/gui/Chat.tsx | 6 +++--- gui/src/pages/gui/useAutoScroll.ts | 12 ++++++++++-- gui/src/redux/slices/sessionSlice.ts | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 2514909bc..5afb75998 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -134,6 +134,8 @@ export function Chat() { return isJetBrains(); }, []); + useAutoScroll(stepsDivRef, history); + useEffect(() => { // Cmd + Backspace to delete current step const listener = (e: any) => { @@ -284,8 +286,6 @@ export function Chat() { const showScrollbar = showChatScrollbar ?? window.innerHeight > 5000; - useAutoScroll(stepsDivRef, history); - return ( <> {widget} @@ -301,7 +301,7 @@ export function Chat() {
, - history: unknown[], + history: ChatHistoryItemWithMessageId[], ) => { const [userHasScrolled, setUserHasScrolled] = useState(false); + const [numUserMsgs, setNumUserMsgs] = useState(0); useEffect(() => { - if (history.length) { + const newNumUserMsgs = history.filter( + (msg) => msg.message.role === "user", + ).length; + + if (newNumUserMsgs > numUserMsgs) { setUserHasScrolled(false); } + + setNumUserMsgs(newNumUserMsgs); }, [history.length]); useEffect(() => { diff --git a/gui/src/redux/slices/sessionSlice.ts b/gui/src/redux/slices/sessionSlice.ts index e81545d8b..4cfcf8c88 100644 --- a/gui/src/redux/slices/sessionSlice.ts +++ b/gui/src/redux/slices/sessionSlice.ts @@ -31,7 +31,7 @@ import { findCurrentToolCall, findToolCall } from "../util"; // We need this to handle reorderings (e.g. a mid-array deletion) of the messages array. // The proper fix is adding a UUID to all chat messages, but this is the temp workaround. -type ChatHistoryItemWithMessageId = ChatHistoryItem & { +export type ChatHistoryItemWithMessageId = ChatHistoryItem & { message: ChatMessage & { id: string }; }; From 83f452a750e1381cc8b0c17853c498b702c7c79c Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Mon, 12 May 2025 17:22:18 -0700 Subject: [PATCH 2/3] Update useAutoScroll.ts --- gui/src/pages/gui/useAutoScroll.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/gui/src/pages/gui/useAutoScroll.ts b/gui/src/pages/gui/useAutoScroll.ts index 4a166612b..cfd67ba52 100644 --- a/gui/src/pages/gui/useAutoScroll.ts +++ b/gui/src/pages/gui/useAutoScroll.ts @@ -9,15 +9,18 @@ export const useAutoScroll = ( const [numUserMsgs, setNumUserMsgs] = useState(0); useEffect(() => { + /** + * Only reset scroll state when a new user message is added to the chat. + * We don't want to auto-scroll on new tool response messages. + */ const newNumUserMsgs = history.filter( (msg) => msg.message.role === "user", ).length; if (newNumUserMsgs > numUserMsgs) { setUserHasScrolled(false); + setNumUserMsgs(newNumUserMsgs); } - - setNumUserMsgs(newNumUserMsgs); }, [history.length]); useEffect(() => { From b1c74e43a7b85a618c946efd543fde1ce86163c5 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Fri, 16 May 2025 11:19:31 -0700 Subject: [PATCH 3/3] Update useAutoScroll.ts --- gui/src/pages/gui/useAutoScroll.ts | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/gui/src/pages/gui/useAutoScroll.ts b/gui/src/pages/gui/useAutoScroll.ts index cfd67ba52..236ca9f76 100644 --- a/gui/src/pages/gui/useAutoScroll.ts +++ b/gui/src/pages/gui/useAutoScroll.ts @@ -1,27 +1,24 @@ -import { useEffect, useState } from "react"; +import { useEffect, useMemo, useState } from "react"; import { ChatHistoryItemWithMessageId } from "../../redux/slices/sessionSlice"; +/** + * Only reset scroll state when a new user message is added to the chat. + * We don't want to auto-scroll on new tool response messages. + */ +function getNumUserMsgs(history: ChatHistoryItemWithMessageId[]) { + return history.filter((msg) => msg.message.role === "user").length; +} + export const useAutoScroll = ( ref: React.RefObject, history: ChatHistoryItemWithMessageId[], ) => { const [userHasScrolled, setUserHasScrolled] = useState(false); - const [numUserMsgs, setNumUserMsgs] = useState(0); + const numUserMsgs = useMemo(() => getNumUserMsgs(history), [history.length]); useEffect(() => { - /** - * Only reset scroll state when a new user message is added to the chat. - * We don't want to auto-scroll on new tool response messages. - */ - const newNumUserMsgs = history.filter( - (msg) => msg.message.role === "user", - ).length; - - if (newNumUserMsgs > numUserMsgs) { - setUserHasScrolled(false); - setNumUserMsgs(newNumUserMsgs); - } - }, [history.length]); + setUserHasScrolled(false); + }, [numUserMsgs]); useEffect(() => { if (!ref.current || history.length === 0) return;