diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx index b18eb8c04..411db4fc9 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx @@ -1,14 +1,13 @@ import { ScrollArea } from "@follow/components/ui/scroll-area/ScrollArea.js" import { tracker } from "@follow/tracker" import { clsx, cn, nextFrame } from "@follow/utils" -import { springScrollTo } from "@follow/utils/scroller" import type { BizUIMessage } from "@folo-services/ai-tools" import { ErrorBoundary } from "@sentry/react" import type { EditorState, LexicalEditor } from "lexical" import { AnimatePresence } from "motion/react" import { nanoid } from "nanoid" import type { FC } from "react" -import { Suspense, useCallback, useEffect, useState } from "react" +import { Suspense, useEffect, useState } from "react" import { useEventCallback } from "usehooks-ts" import { @@ -34,7 +33,7 @@ import { ChatInput } from "./ChatInput" import { CollapsibleError } from "./CollapsibleError" import { WelcomeScreen } from "./WelcomeScreen" -const SCROLL_BOTTOM_THRESHOLD = window.innerHeight / 2 +const SCROLL_BOTTOM_THRESHOLD = 100 const ChatInterfaceContent = () => { const hasMessages = useHasMessages() @@ -97,13 +96,6 @@ const ChatInterfaceContent = () => { } }, [scrollAreaRef]) - const scrollToBottom = useCallback(() => { - const scrollElement = scrollAreaRef - if (!scrollElement) return - - springScrollTo(scrollElement.scrollHeight, scrollElement) - }, [scrollAreaRef]) - const blockActions = useBlockActions() const handleSendMessage = useEventCallback( (message: string | EditorState, editor: LexicalEditor | null) => { @@ -175,6 +167,7 @@ const ChatInterfaceContent = () => { ) : ( { ) : (
- {status === "submitted" && } + + {(status === "submitted" || status === "streaming") && }
)}
@@ -195,24 +189,19 @@ const ChatInterfaceContent = () => { {shouldShowScrollToBottom && ( -
-
- -
+
+
)} diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIChatMessage.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIChatMessage.tsx index 14b9cb827..db789ba44 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIChatMessage.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIChatMessage.tsx @@ -247,8 +247,17 @@ export const AIChatMessage: React.FC = React.memo(({ message export const AIChatWaitingIndicator: React.FC = () => { return ( - - - + +
+ + Thinking… +
+
) } diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useAutoScroll.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useAutoScroll.tsx index 14435fb54..9f3b0ef5f 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useAutoScroll.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useAutoScroll.tsx @@ -1,26 +1,22 @@ import { springScrollTo } from "@follow/utils/scroller" -import { debounce } from "es-toolkit/compat" +import { debounce, throttle } from "es-toolkit/compat" import { useCallback, useEffect, useRef, useState } from "react" const BOTTOM_THRESHOLD = 50 export const useAutoScroll = (viewport: HTMLElement | null, enabled: boolean) => { const [isAtBottom, setIsAtBottom] = useState(true) - const [isUserInteracting, setIsUserInteracting] = useState(false) const scrollAnimationRef = useRef<{ stop: () => void } | null>(null) - const interactionTimeoutRef = useRef(null) const isAutoScrollingRef = useRef(false) - - const isUserInteractingRef = useRef(isUserInteracting) - useEffect(() => { - isUserInteractingRef.current = isUserInteracting - }, [isUserInteracting]) + const isAutoScrollCancelledRef = useRef(false) const isAtBottomRef = useRef(isAtBottom) useEffect(() => { isAtBottomRef.current = isAtBottom }, [isAtBottom]) + // cancellation is tracked purely via ref to avoid re-renders while user interacts + const scrollToBottom = useCallback( (force = false) => { if (!viewport) return @@ -29,7 +25,7 @@ export const useAutoScroll = (viewport: HTMLElement | null, enabled: boolean) => scrollAnimationRef.current.stop() } - if (force || (!isUserInteractingRef.current && isAtBottomRef.current)) { + if (force || (isAtBottomRef.current && !isAutoScrollCancelledRef.current)) { isAutoScrollingRef.current = true const targetScrollTop = viewport.scrollHeight - viewport.clientHeight const animation = springScrollTo(targetScrollTop, viewport) @@ -52,55 +48,43 @@ export const useAutoScroll = (viewport: HTMLElement | null, enabled: boolean) => useEffect(() => { if (!viewport) return + if (!enabled) return - if (!enabled) { - return - } - - const handleScroll = () => { + const handleScroll = throttle(() => { if (isAutoScrollingRef.current) { return } + }, 100) - const { scrollTop, scrollHeight, clientHeight } = viewport - const atBottom = scrollHeight - scrollTop - clientHeight <= BOTTOM_THRESHOLD - - if (!isUserInteractingRef.current) { - setIsAtBottom(atBottom) - } - } - - const handleWheel = () => { + const cancelAutoScroll = () => { isAutoScrollingRef.current = false - setIsUserInteracting(true) if (scrollAnimationRef.current) { scrollAnimationRef.current.stop() } - if (interactionTimeoutRef.current) { - window.clearTimeout(interactionTimeoutRef.current) - } - - interactionTimeoutRef.current = window.setTimeout(() => { - setIsUserInteracting(false) - handleScroll() // Re-check position after interaction ends - }, 150) + isAutoScrollCancelledRef.current = true + handleScroll() } + const handleWheel = throttle(cancelAutoScroll, 100) + const handleTouchStart = handleWheel + const handleTouchMove = handleWheel + viewport.addEventListener("scroll", handleScroll, { passive: true }) viewport.addEventListener("wheel", handleWheel, { passive: true }) + viewport.addEventListener("touchstart", handleTouchStart, { passive: true }) + viewport.addEventListener("touchmove", handleTouchMove, { passive: true }) return () => { viewport.removeEventListener("scroll", handleScroll) viewport.removeEventListener("wheel", handleWheel) - if (interactionTimeoutRef.current) { - window.clearTimeout(interactionTimeoutRef.current) - } + viewport.removeEventListener("touchstart", handleTouchStart) + viewport.removeEventListener("touchmove", handleTouchMove) if (scrollAnimationRef.current) { scrollAnimationRef.current.stop() } } - }, [viewport]) + }, [viewport, enabled]) useEffect(() => { if (!viewport || !enabled) return @@ -110,7 +94,7 @@ export const useAutoScroll = (viewport: HTMLElement | null, enabled: boolean) => const resizeObserver = new ResizeObserver( debounce(() => { - if (!isUserInteractingRef.current && isAtBottomRef.current) { + if (isAtBottomRef.current && !isAutoScrollCancelledRef.current) { requestAnimationFrame(() => { scrollToBottom() }) @@ -126,7 +110,7 @@ export const useAutoScroll = (viewport: HTMLElement | null, enabled: boolean) => }, [viewport, enabled, scrollToBottom]) const resetScrollState = useCallback(() => { - setIsUserInteracting(false) + isAutoScrollCancelledRef.current = false setIsAtBottom(true) scrollToBottom(true) }, [scrollToBottom]) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/services/index.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/services/index.ts index d6e8fdeae..c7eb2414a 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/services/index.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/services/index.ts @@ -154,23 +154,25 @@ class AIPersistServiceStatic { await db .insert(aiChatMessagesTable) .values( - messages.map((message) => { - const convertedParts = message.parts as any[] + messages + .filter((message) => message.parts.length > 0) + .map((message) => { + const convertedParts = message.parts as any[] - return { - id: message.id, - chatId, - role: message.role, - contentFormat: "plaintext" as const, - createdAt: new Date(), - status: "completed" as const, - finishedAt: message.metadata?.finishTime - ? new Date(message.metadata.finishTime) - : undefined, - messageParts: convertedParts, - metadata: message.metadata, - } as typeof aiChatMessagesTable.$inferInsert - }), + return { + id: message.id, + chatId, + role: message.role, + contentFormat: "plaintext" as const, + createdAt: new Date(), + status: "completed" as const, + finishedAt: message.metadata?.finishTime + ? new Date(message.metadata.finishTime) + : undefined, + messageParts: convertedParts, + metadata: message.metadata, + } as typeof aiChatMessagesTable.$inferInsert + }), ) .onConflictDoUpdate({ target: [aiChatMessagesTable.id],