Merge pull request #5638 from continuedev/pe/tools-autoscroll

fix: prevent autoscrolling on new tool msgs
This commit is contained in:
Dallin Romney 2025-05-16 13:22:29 -07:00 • committed by GitHub
commit e3d70fac17
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 18 additions and 10 deletions

View file

@ -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() {
<div
key={item.message.id}
style={{
minHeight: index === history.length - 1 ? "25vh" : 0,
minHeight: index === history.length - 1 ? "200px" : 0,
}}
>
<ErrorBoundary

View file

@ -1,16 +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<HTMLDivElement>,
history: unknown[],
history: ChatHistoryItemWithMessageId[],
) => {
const [userHasScrolled, setUserHasScrolled] = useState(false);
const numUserMsgs = useMemo(() => getNumUserMsgs(history), [history.length]);
useEffect(() => {
if (history.length) {
setUserHasScrolled(false);
}
}, [history.length]);
setUserHasScrolled(false);
}, [numUserMsgs]);
useEffect(() => {
if (!ref.current || history.length === 0) return;

View file

@ -32,7 +32,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 };
};