diff --git a/apps/web/src/chat-scroll.test.ts b/apps/web/src/chat-scroll.test.ts index 5311fb40ae..21b63777bd 100644 --- a/apps/web/src/chat-scroll.test.ts +++ b/apps/web/src/chat-scroll.test.ts @@ -1,11 +1,11 @@ import { describe, expect, it } from "vitest"; -import { AUTO_SCROLL_BOTTOM_THRESHOLD_PX, isScrollContainerNearBottom } from "./chat-scroll"; +import { AUTO_SCROLL_BOTTOM_EPSILON_PX, isScrollContainerAtBottom } from "./chat-scroll"; -describe("isScrollContainerNearBottom", () => { +describe("isScrollContainerAtBottom", () => { it("returns true when already at bottom", () => { expect( - isScrollContainerNearBottom({ + isScrollContainerAtBottom({ scrollTop: 600, clientHeight: 400, scrollHeight: 1_000, @@ -13,31 +13,31 @@ describe("isScrollContainerNearBottom", () => { ).toBe(true); }); - it("returns true when within the auto-scroll threshold", () => { + it("returns true when within the bottom epsilon", () => { expect( - isScrollContainerNearBottom({ - scrollTop: 540, + isScrollContainerAtBottom({ + scrollTop: 599, clientHeight: 400, scrollHeight: 1_000, }), ).toBe(true); }); - it("returns false when the user is meaningfully above the bottom", () => { + it("returns false when the user is above the epsilon", () => { expect( - isScrollContainerNearBottom({ - scrollTop: 520, + isScrollContainerAtBottom({ + scrollTop: 597, clientHeight: 400, scrollHeight: 1_000, }), ).toBe(false); }); - it("clamps negative thresholds to zero", () => { + it("clamps negative epsilons to zero", () => { expect( - isScrollContainerNearBottom( + isScrollContainerAtBottom( { - scrollTop: 539, + scrollTop: 599, clientHeight: 400, scrollHeight: 1_000, }, @@ -46,17 +46,27 @@ describe("isScrollContainerNearBottom", () => { ).toBe(false); }); - it("falls back to the default threshold for non-finite values", () => { + it("falls back to the default epsilon for non-finite values", () => { expect( - isScrollContainerNearBottom( + isScrollContainerAtBottom( { - scrollTop: 540, + scrollTop: 599, clientHeight: 400, scrollHeight: 1_000, }, Number.NaN, ), ).toBe(true); - expect(AUTO_SCROLL_BOTTOM_THRESHOLD_PX).toBe(64); + expect(AUTO_SCROLL_BOTTOM_EPSILON_PX).toBe(2); + }); + + it("treats overscrolled values as bottom", () => { + expect( + isScrollContainerAtBottom({ + scrollTop: 610, + clientHeight: 400, + scrollHeight: 1_000, + }), + ).toBe(true); }); }); diff --git a/apps/web/src/chat-scroll.ts b/apps/web/src/chat-scroll.ts index 35190ab1b9..b5997bbcd7 100644 --- a/apps/web/src/chat-scroll.ts +++ b/apps/web/src/chat-scroll.ts @@ -1,4 +1,4 @@ -export const AUTO_SCROLL_BOTTOM_THRESHOLD_PX = 64; +export const AUTO_SCROLL_BOTTOM_EPSILON_PX = 2; interface ScrollPosition { scrollTop: number; @@ -6,19 +6,19 @@ interface ScrollPosition { scrollHeight: number; } -export function isScrollContainerNearBottom( +export function isScrollContainerAtBottom( position: ScrollPosition, - thresholdPx = AUTO_SCROLL_BOTTOM_THRESHOLD_PX, + epsilonPx = AUTO_SCROLL_BOTTOM_EPSILON_PX, ): boolean { - const threshold = Number.isFinite(thresholdPx) - ? Math.max(0, thresholdPx) - : AUTO_SCROLL_BOTTOM_THRESHOLD_PX; + const epsilon = Number.isFinite(epsilonPx) + ? Math.max(0, epsilonPx) + : AUTO_SCROLL_BOTTOM_EPSILON_PX; const { scrollTop, clientHeight, scrollHeight } = position; if (![scrollTop, clientHeight, scrollHeight].every(Number.isFinite)) { return true; } - const distanceFromBottom = scrollHeight - clientHeight - scrollTop; - return distanceFromBottom <= threshold; + const distanceFromBottom = Math.max(0, scrollHeight - clientHeight - scrollTop); + return distanceFromBottom <= epsilon; } diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 9b5288a4fd..6d2a06561a 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -61,7 +61,7 @@ import { formatElapsed, formatTimestamp, } from "../session-logic"; -import { isScrollContainerNearBottom } from "../chat-scroll"; +import { isScrollContainerAtBottom } from "../chat-scroll"; import { useStore } from "../store"; import { truncateTitle } from "../truncateTitle"; import { @@ -1094,8 +1094,7 @@ export default function ChatView({ threadId }: ChatViewProps) { } }, [lastInvokedScriptByProjectId]); - // Auto-scroll on new messages - const messageCount = activeThread?.messages.length ?? 0; + // Auto-tail while the user stays at the bottom. const workLogCount = workLogEntries.length; const scrollMessagesToBottom = useCallback((behavior: ScrollBehavior = "auto") => { const scrollContainer = messagesScrollRef.current; @@ -1106,21 +1105,16 @@ export default function ChatView({ threadId }: ChatViewProps) { const onMessagesScroll = useCallback(() => { const scrollContainer = messagesScrollRef.current; if (!scrollContainer) return; - shouldAutoScrollRef.current = isScrollContainerNearBottom(scrollContainer); + shouldAutoScrollRef.current = isScrollContainerAtBottom(scrollContainer); }, []); useLayoutEffect(() => { if (!activeThread?.id) return; scrollMessagesToBottom(); }, [activeThread?.id, scrollMessagesToBottom]); - useEffect(() => { + useLayoutEffect(() => { if (!shouldAutoScrollRef.current) return; - scrollMessagesToBottom("smooth"); - }, [messageCount, scrollMessagesToBottom]); - useEffect(() => { - if (phase !== "running") return; - if (!shouldAutoScrollRef.current) return; - scrollMessagesToBottom("smooth"); - }, [phase, workLogCount, scrollMessagesToBottom]); + scrollMessagesToBottom(); + }, [activeThread?.messages, workLogCount, scrollMessagesToBottom]); useEffect(() => { setExpandedWorkGroups({});