From fba51dc8564a8d291bafe2f38aac4f92b1bd6d94 Mon Sep 17 00:00:00 2001 From: "Bingxi Zhao (Frank)" <13051345080@163.com> Date: Sat, 25 Apr 2026 17:47:56 +0800 Subject: [PATCH] fix: auto-update, auto-scroll to the bottom --- web/app/(utility)/knowledge/page.tsx | 2 -- .../(workspace)/agents/[botId]/chat/page.tsx | 28 +++++++++++++------ web/hooks/useChatAutoScroll.ts | 6 +++- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/web/app/(utility)/knowledge/page.tsx b/web/app/(utility)/knowledge/page.tsx index 205283185..4fe699397 100644 --- a/web/app/(utility)/knowledge/page.tsx +++ b/web/app/(utility)/knowledge/page.tsx @@ -1644,7 +1644,6 @@ function KnowledgePageContent() { ref={createFileRef} type="file" multiple - accept={uploadPolicy.accept || undefined} className="hidden" onChange={(event) => { setNewKbFiles((prev) => @@ -1819,7 +1818,6 @@ function KnowledgePageContent() { ref={uploadFileRef} type="file" multiple - accept={uploadPolicy.accept || undefined} className="hidden" onChange={(event) => { setUploadFiles((prev) => diff --git a/web/app/(workspace)/agents/[botId]/chat/page.tsx b/web/app/(workspace)/agents/[botId]/chat/page.tsx index f36d7b870..a6ba159d9 100644 --- a/web/app/(workspace)/agents/[botId]/chat/page.tsx +++ b/web/app/(workspace)/agents/[botId]/chat/page.tsx @@ -37,14 +37,17 @@ export default function BotChatPage() { const scrollRef = useRef(null); const inputRef = useRef(null); - const scrollToBottom = useCallback(() => { - requestAnimationFrame(() => { - scrollRef.current?.scrollTo({ - top: scrollRef.current.scrollHeight, - behavior: "smooth", + const scrollToBottom = useCallback( + (behavior: ScrollBehavior = "smooth") => { + requestAnimationFrame(() => { + scrollRef.current?.scrollTo({ + top: scrollRef.current.scrollHeight, + behavior, + }); }); - }); - }, []); + }, + [], + ); useEffect(() => { if (!botId) { @@ -64,10 +67,17 @@ export default function BotChatPage() { role: m.role as "user" | "assistant", content: m.content, })); - if (restored.length) setMessages(restored); + if (restored.length) { + setMessages(restored); + // Markdown/KaTeX inside AssistantResponse can grow the container after + // the first paint — re-snap a few times so we land at the bottom. + requestAnimationFrame(() => scrollToBottom("instant")); + window.setTimeout(() => scrollToBottom("instant"), 80); + window.setTimeout(() => scrollToBottom("instant"), 250); + } }) .catch(() => {}); - }, [botId]); + }, [botId, scrollToBottom]); useEffect(() => { if (!botId) { diff --git a/web/hooks/useChatAutoScroll.ts b/web/hooks/useChatAutoScroll.ts index 4b09f618c..5b5c01008 100644 --- a/web/hooks/useChatAutoScroll.ts +++ b/web/hooks/useChatAutoScroll.ts @@ -90,8 +90,12 @@ export function useChatAutoScroll({ // After streaming ends, dynamically-loaded components (e.g. MathAnimatorViewer // via next/dynamic) may render and grow the content height. Detect that and // scroll to bottom so the user can see the full result. + // hasMessages is in deps so the observer attaches once the messages + // container mounts on session reopen — without it, the container ref is null + // on initial mount and the observer is never set up. useEffect(() => { if (isStreaming) return; + if (!hasMessages) return; const container = containerRef.current; if (!container) return; @@ -118,7 +122,7 @@ export function useChatAutoScroll({ mo.disconnect(); if (rafId) cancelAnimationFrame(rafId); }; - }, [isStreaming, scrollToBottom]); + }, [hasMessages, isStreaming, scrollToBottom]); const handleScroll = useCallback(() => { const container = containerRef.current;