diff --git a/frontend/src/components/message/PromptInput.tsx b/frontend/src/components/message/PromptInput.tsx index bb978fed..e4320f38 100644 --- a/frontend/src/components/message/PromptInput.tsx +++ b/frontend/src/components/message/PromptInput.tsx @@ -230,6 +230,7 @@ export const PromptInput = memo(forwardRef( setIsVoiceAutoSendPending(false) if (isStreamingResponse) { + onScrollToBottom() const parts = parsePromptToParts(prompt, attachedFiles, imageAttachments) const agentUsed = selectedAgent || currentMode sendPrompt.mutate({ @@ -294,6 +295,8 @@ export const PromptInput = memo(forwardRef( variant: currentVariant }) + onScrollToBottom() + setStoredAgent(sessionID, agentUsed) if (model) { setStoredModel({ providerID: model.providerID, modelID: model.modelID }) @@ -1210,32 +1213,39 @@ return (
{showMobileScrollButton ? ( - + <> + + {isSessionActive && ( +
+ +
+ )} + ) : ( - - )} - {isSessionActive ? ( + <> + + {isSessionActive ? (
) : ( - !hideSecondaryButtons && !showMobileScrollButton && ( + !hideSecondaryButtons && ( ) - )} - + )} + + )}
{!isMobile && ( diff --git a/frontend/src/hooks/useAutoScroll.test.tsx b/frontend/src/hooks/useAutoScroll.test.tsx index 3ed17c31..6cf4bc92 100644 --- a/frontend/src/hooks/useAutoScroll.test.tsx +++ b/frontend/src/hooks/useAutoScroll.test.tsx @@ -307,6 +307,75 @@ describe('useAutoScroll', () => { expect(currentScrollTop).not.toBe(containerHarness.div.scrollHeight - containerHarness.div.clientHeight) }) + it('disengages when mobile touch scrolls upward without pointer events', () => { + const messages = [createMessage('1', 'user')] + const { renderResult, containerHarness, onScrollStateChange } = setupHook(messages) + + act(() => { + containerHarness.setScrollTop(100) + containerHarness.div.dispatchEvent(new TouchEvent('touchstart', { + touches: [{ clientY: 200 } as Touch], + bubbles: true, + })) + containerHarness.div.dispatchEvent(new TouchEvent('touchmove', { + touches: [{ clientY: 260 } as Touch], + bubbles: true, + })) + }) + + expect(onScrollStateChange).toHaveBeenCalledWith(true) + + containerHarness.setScrollHeight(containerHarness.div.scrollHeight + 200) + + act(() => { + renderResult.rerender({ + containerRef: { current: containerHarness.div }, + messages, + sessionId: 'session-1', + contentVersion: messages.length + 1, + onScrollStateChange, + }) + vi.runOnlyPendingTimers() + }) + + expect(containerHarness.getScrollTop()).toBe(100) + }) + + it('shows scroll button when streaming growth carries disengaged user past threshold', () => { + const messages = [createMessage('1', 'user')] + const { renderResult, containerHarness, onScrollStateChange } = setupHook(messages) + const nearBottomPosition = containerHarness.div.scrollHeight - containerHarness.div.clientHeight - 60 + + act(() => { + containerHarness.setScrollTop(nearBottomPosition) + containerHarness.div.dispatchEvent(new TouchEvent('touchstart', { + touches: [{ clientY: 200 } as Touch], + bubbles: true, + })) + containerHarness.div.dispatchEvent(new TouchEvent('touchmove', { + touches: [{ clientY: 260 } as Touch], + bubbles: true, + })) + }) + + expect(onScrollStateChange).not.toHaveBeenCalledWith(true) + + containerHarness.setScrollHeight(containerHarness.div.scrollHeight + 200) + + act(() => { + renderResult.rerender({ + containerRef: { current: containerHarness.div }, + messages, + sessionId: 'session-1', + contentVersion: messages.length + 1, + onScrollStateChange, + }) + }) + + expect(onScrollStateChange).toHaveBeenCalledWith(true) + expect(containerHarness.getScrollTop()).toBe(nearBottomPosition) + }) + it('re-engages when user scrolls back to within threshold', () => { const messages = [createMessage('1', 'user')] const { containerHarness, onScrollStateChange } = setupHook(messages) diff --git a/frontend/src/hooks/useAutoScroll.ts b/frontend/src/hooks/useAutoScroll.ts index 24315ed6..202bb91c 100644 --- a/frontend/src/hooks/useAutoScroll.ts +++ b/frontend/src/hooks/useAutoScroll.ts @@ -31,6 +31,8 @@ export function useAutoScroll({ const userDisengagedRef = useRef(false) const pointerStartYRef = useRef(null) const pointerActiveRef = useRef(false) + const touchStartYRef = useRef(null) + const touchActiveRef = useRef(false) const onScrollStateChangeRef = useRef(onScrollStateChange) const scrollRequestIdRef = useRef(0) const isScrollButtonVisibleRef = useRef(false) @@ -68,6 +70,8 @@ export function useAutoScroll({ hasInitialScrolledRef.current = false userScrolledAtRef.current = 0 userDisengagedRef.current = false + touchStartYRef.current = null + touchActiveRef.current = false }, [sessionId]) useEffect(() => { @@ -87,6 +91,16 @@ export function useAutoScroll({ userDisengagedRef.current = true } + const updateScrollButtonVisibility = () => { + const container = containerRef?.current + if (!container || !userDisengagedRef.current || isScrollButtonVisibleRef.current) return + const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight + if (distanceFromBottom > SHOW_SCROLL_BUTTON_THRESHOLD_PX) { + isScrollButtonVisibleRef.current = true + onScrollStateChangeRef.current?.(true) + } + } + const handlePointerDown = (e: PointerEvent) => { pointerStartYRef.current = e.clientY pointerActiveRef.current = true @@ -105,6 +119,27 @@ export function useAutoScroll({ pointerActiveRef.current = false } + const handleTouchStart = (e: TouchEvent) => { + const touch = e.touches[0] + if (!touch) return + touchStartYRef.current = touch.clientY + touchActiveRef.current = true + scrollRequestIdRef.current += 1 + } + + const handleTouchMove = (e: TouchEvent) => { + const touch = e.touches[0] + if (!touch || touchStartYRef.current === null) return + if (touch.clientY > touchStartYRef.current + 4) { + markDisengaged() + } + } + + const handleTouchEnd = () => { + touchStartYRef.current = null + touchActiveRef.current = false + } + const handleWheel = (e: WheelEvent) => { if (e.deltaY < 0) { markDisengaged() @@ -129,16 +164,12 @@ export function useAutoScroll({ isScrollButtonVisibleRef.current = false onScrollStateChangeRef.current?.(false) } - } else if (userDisengagedRef.current && !isScrollButtonVisibleRef.current && scrollHeight - scrollTop - clientHeight > SHOW_SCROLL_BUTTON_THRESHOLD_PX) { - isScrollButtonVisibleRef.current = true - onScrollStateChangeRef.current?.(true) - } else if (pointerActiveRef.current && !userDisengagedRef.current) { + } else if (userDisengagedRef.current && !isScrollButtonVisibleRef.current) { + updateScrollButtonVisibility() + } else if ((pointerActiveRef.current || touchActiveRef.current) && !userDisengagedRef.current) { userScrolledAtRef.current = Date.now() userDisengagedRef.current = true - if (scrollHeight - scrollTop - clientHeight > SHOW_SCROLL_BUTTON_THRESHOLD_PX) { - isScrollButtonVisibleRef.current = true - onScrollStateChangeRef.current?.(true) - } + updateScrollButtonVisibility() } } @@ -146,6 +177,10 @@ export function useAutoScroll({ container.addEventListener('pointermove', handlePointerMove, { passive: true }) container.addEventListener('pointerup', handlePointerUp, { passive: true }) container.addEventListener('pointercancel', handlePointerUp, { passive: true }) + container.addEventListener('touchstart', handleTouchStart, { passive: true }) + container.addEventListener('touchmove', handleTouchMove, { passive: true }) + container.addEventListener('touchend', handleTouchEnd, { passive: true }) + container.addEventListener('touchcancel', handleTouchEnd, { passive: true }) container.addEventListener('wheel', handleWheel, { passive: true }) container.addEventListener('keydown', handleKeyDown) container.addEventListener('scroll', handleScroll, { passive: true }) @@ -155,6 +190,10 @@ export function useAutoScroll({ container.removeEventListener('pointermove', handlePointerMove) container.removeEventListener('pointerup', handlePointerUp) container.removeEventListener('pointercancel', handlePointerUp) + container.removeEventListener('touchstart', handleTouchStart) + container.removeEventListener('touchmove', handleTouchMove) + container.removeEventListener('touchend', handleTouchEnd) + container.removeEventListener('touchcancel', handleTouchEnd) container.removeEventListener('wheel', handleWheel) container.removeEventListener('keydown', handleKeyDown) container.removeEventListener('scroll', handleScroll) @@ -185,7 +224,16 @@ export function useAutoScroll({ const timeSinceUserScroll = Date.now() - userScrolledAtRef.current const recentlyScrolled = timeSinceUserScroll < SCROLL_LOCK_MS - if (recentlyScrolled || userDisengagedRef.current || pointerActiveRef.current) { + if (userDisengagedRef.current) { + const distanceFromBottom = containerRef.current.scrollHeight - containerRef.current.scrollTop - containerRef.current.clientHeight + if (!isScrollButtonVisibleRef.current && distanceFromBottom > SHOW_SCROLL_BUTTON_THRESHOLD_PX) { + isScrollButtonVisibleRef.current = true + onScrollStateChangeRef.current?.(true) + } + return + } + + if (recentlyScrolled || pointerActiveRef.current || touchActiveRef.current) { return } diff --git a/frontend/src/pages/SessionDetail.tsx b/frontend/src/pages/SessionDetail.tsx index 454ad63d..acaae0af 100644 --- a/frontend/src/pages/SessionDetail.tsx +++ b/frontend/src/pages/SessionDetail.tsx @@ -61,6 +61,22 @@ const compareMessageIds = (id1: string, id2: string): number => { const PENDING_ACTION_SYNC_INTERVAL_MS = 30000 const PROMPT_OVERLAY_CLEARANCE_PX = 16 + +const getMessagesContentVersion = (messages?: MessageWithParts[]): number => { + if (!messages) return 0 + return messages.reduce((sum, message) => { + return sum + message.parts.reduce((partSum, part) => { + if ('text' in part && typeof part.text === 'string') { + return partSum + part.text.length + } + if (part.type === 'tool') { + return partSum + JSON.stringify(part.state).length + } + return partSum + 1 + }, 0) + }, messages.length) +} + export function SessionDetail() { const { id, sessionId } = useParams<{ id: string; sessionId: string }>(); const navigate = useNavigate(); @@ -152,7 +168,7 @@ export function SessionDetail() { containerRef: messageContainerRef, messages: messages?.map(m => m.info), sessionId, - contentVersion: messages?.reduce((sum, m) => sum + m.parts.length, 0) ?? 0, + contentVersion: getMessagesContentVersion(messages), onScrollStateChange: setShowScrollButton }); const abortSession = useAbortSession(opcodeUrl, repoDirectory, sessionId);