Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Fix: Chat embedded in a website: automatically scrolls down when a question is answered. (#21940)

balibabu7901a9e71451
diff --git a/web/src/components/next-message-item/index.tsx b/web/src/components/next-message-item/index.tsx
index 5b802abc9e..c5b4c12b9c 100644
--- a/web/src/components/next-message-item/index.tsx
+++ b/web/src/components/next-message-item/index.tsx
@@ -136,7 +136,7 @@ function MessageItem({
);
const renderContent = useCallback(() => {
- if (!messageContent && !(item.data || (sendLoading && !isShare))) {
+ if (!messageContent && !(item.data || sendLoading)) {
return null;
}
@@ -155,7 +155,7 @@ function MessageItem({
{hasCustomChildren ? (
children
) : sendLoading && isEmpty(messageContent) ? (
- <>{!isShare && <LoadingDots className="text-text-secondary" />}</>
+ <LoadingDots className="text-text-secondary" />
) : (
<MarkdownContent
loading={loading}
@@ -171,7 +171,7 @@ function MessageItem({
children,
clickDocumentButton,
isAssistant,
- isShare,
+ isUser,
item.data,
loading,
messageContent,
diff --git a/web/src/hooks/__tests__/logic-hooks-add-newest-answer.test.tsx b/web/src/hooks/__tests__/logic-hooks-add-newest-answer.test.tsx
new file mode 100644
index 0000000000..af7591b483
--- /dev/null
+++ b/web/src/hooks/__tests__/logic-hooks-add-newest-answer.test.tsx
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { MessageType } from '@/constants/chat';
+import { IAnswer, IMessage } from '@/interfaces/database/chat';
+import { buildMessageUuidWithRole } from '@/utils/chat';
+import { act, renderHook } from '@testing-library/react';
+import { useSelectDerivedMessages } from '../logic-hooks';
+
+// Chatbot/embed SSE frames carry `id: null` (WriteChatbotFrame hardcodes it
+// for python wire parity). IAnswer types id as string, so cast through null.
+const embedAnswer = (text: string): IAnswer =>
+ ({ answer: text, id: null }) as unknown as IAnswer;
+
+describe('useSelectDerivedMessages addNewestAnswer', () => {
+ it('keeps the placeholder id when the stream frame id is null (embed chat)', () => {
+ const { result } = renderHook(() => useSelectDerivedMessages());
+
+ act(() => {
+ result.current.addNewestQuestion({
+ id: 'q1',
+ role: MessageType.User,
+ content: '你好',
+ } as IMessage);
+ });
+
+ // Two chunks of the same answer: the id must stay stable so the
+ // virtualizer item key does not churn per chunk.
+ act(() => {
+ result.current.addNewestAnswer(embedAnswer('chunk1'));
+ });
+ const firstChunkId = result.current.derivedMessages.at(-1)?.id;
+ act(() => {
+ result.current.addNewestAnswer(embedAnswer('chunk1chunk2'));
+ });
+
+ expect(firstChunkId).toBe('q1');
+ expect(result.current.derivedMessages.at(-1)?.id).toBe('q1');
+ expect(result.current.derivedMessages).toHaveLength(2);
+ });
+
+ it('never leaves assistant messages on the shared assistant_null key', () => {
+ const { result } = renderHook(() => useSelectDerivedMessages());
+
+ // Embed opening handshake: the prologue arrives as an answer with id null.
+ act(() => {
+ result.current.addNewestAnswer(embedAnswer('Hi!'));
+ });
+ act(() => {
+ result.current.addNewestQuestion({
+ id: 'q1',
+ role: MessageType.User,
+ content: '贾雨村',
+ } as IMessage);
+ });
+ act(() => {
+ result.current.addNewestAnswer(embedAnswer('《红楼梦》第一回…'));
+ });
+
+ const ids = result.current.derivedMessages.map((x) => x.id);
+ expect(ids.every((id) => typeof id === 'string' && id.length > 0)).toBe(
+ true,
+ );
+ const keys = result.current.derivedMessages.map((x) =>
+ buildMessageUuidWithRole(x),
+ );
+ expect(new Set(keys).size).toBe(keys.length);
+ });
+
+ it('prefers the backend message id when the frame carries one (in-app chat)', () => {
+ const { result } = renderHook(() => useSelectDerivedMessages());
+
+ act(() => {
+ result.current.addNewestQuestion({
+ id: 'q1',
+ role: MessageType.User,
+ content: 'hi',
+ } as IMessage);
+ });
+ act(() => {
+ result.current.addNewestAnswer({ answer: 'hello', id: 'msg-1' });
+ });
+
+ expect(result.current.derivedMessages.at(-1)?.id).toBe('msg-1');
+ });
+});
diff --git a/web/src/hooks/logic-hooks.ts b/web/src/hooks/logic-hooks.ts
index 0a204f218d..65650e2056 100644
--- a/web/src/hooks/logic-hooks.ts
+++ b/web/src/hooks/logic-hooks.ts
@@ -22,12 +22,7 @@ import { MessageType } from '@/constants/chat';
import { FormInstance } from '@/interfaces/antd-compat';
import { Pagination } from '@/interfaces/common';
import { ResponseType } from '@/interfaces/database/base';
-import {
- IAnswer,
- IClientConversation,
- IMessage,
- Message,
-} from '@/interfaces/database/chat';
+import { IAnswer, IMessage, Message } from '@/interfaces/database/chat';
import { IKnowledgeFile } from '@/interfaces/database/dataset';
import { changeLanguageAsync } from '@/locales/config';
import api from '@/utils/api';
@@ -519,13 +514,19 @@ export const useSelectDerivedMessages = (resetKey?: string) => {
role: MessageType.Assistant,
content: answer.answer,
reference: answer.reference,
- id: buildMessageUuid({
- id: answer.id,
- role: MessageType.Assistant,
- }),
prompt: answer.prompt,
audio_binary: answer.audio_binary,
...omit(answer, 'reference'),
+ // Chatbot/embed SSE frames carry `id: null`; assigned after the
+ // spread so the null cannot clobber a real id. Reusing the
+ // placeholder message's id keeps the virtualizer item key stable
+ // across stream chunks — otherwise every assistant message shares
+ // the `assistant_null` key and the virtualizer's per-key size
+ // cache inflates the other assistant rows with this row's height.
+ id: buildMessageUuid({
+ id: answer.id || pre?.at(-1)?.id,
+ role: MessageType.Assistant,
+ }),
},
];
});
@@ -672,43 +673,6 @@ export interface IRemoveMessageById {
removeMessageById(messageId: string): void;
}
-export const useRemoveMessagesAfterCurrentMessage = (
- setCurrentConversation: (
- callback: (state: IClientConversation) => IClientConversation,
- ) => void,
-) => {
- const removeMessagesAfterCurrentMessage = useCallback(
- (messageId: string) => {
- setCurrentConversation((pre) => {
- const index = pre.message?.findIndex((x) => x.id === messageId);
- if (index !== -1) {
- let nextMessages = pre.message?.slice(0, index + 2) ?? [];
- const latestMessage = nextMessages.at(-1);
- nextMessages = latestMessage
- ? [
- ...nextMessages.slice(0, -1),
- {
- ...latestMessage,
- content: '',
- reference: undefined,
- prompt: undefined,
- },
- ]
- : nextMessages;
- return {
- ...pre,
- message: nextMessages,
- };
- }
- return pre;
- });
- },
- [setCurrentConversation],
- );
-
- return { removeMessagesAfterCurrentMessage };
-};
-
export interface IRegenerateMessage {
regenerateMessage?: (message: Message) => void;
}
diff --git a/web/src/pages/agent/share/index.tsx b/web/src/pages/agent/share/index.tsx
index 45aecf517e..bbc2bc4b79 100644
--- a/web/src/pages/agent/share/index.tsx
+++ b/web/src/pages/agent/share/index.tsx
@@ -1,9 +1,12 @@
import { EmbedContainer } from '@/components/embed-container';
import { FileUploadProps } from '@/components/file-upload';
+import { LoadingDots } from '@/components/loading-dots';
import { NextMessageInput } from '@/components/message-input/next';
import MessageItem from '@/components/next-message-item';
import PdfSheet from '@/components/pdf-drawer';
import { useClickDrawer } from '@/components/pdf-drawer/hooks';
+import { RAGFlowAvatar } from '@/components/ragflow-avatar';
+import SvgIcon from '@/components/svg-icon';
import { useSyncThemeFromParams } from '@/components/theme-provider';
import { VirtualMessageList } from '@/components/virtual-message-list';
import { MessageType } from '@/constants/chat';
@@ -69,6 +72,10 @@ const ChatContainer = () => {
sendFormMessage,
});
const sendDisabled = useSendButtonDisabled(value);
+ // The assistant message is only created when the first SSE frame arrives,
+ // so fill the gap between sending and the first frame with a loading row.
+ const showSendLoading =
+ sendLoading && derivedMessages.at(-1)?.role !== MessageType.Assistant;
const showBeginParameterDialog = useCallback(() => {
if (inputsData && inputsData.inputs && !isEmpty(inputsData.inputs)) {
@@ -183,6 +190,28 @@ const ChatContainer = () => {
</MessageItem>
)}
/>
+ {showSendLoading && (
+ <div className="flex w-full gap-5 py-6">
+ {visibleAvatar &&
+ (inputsData.avatar || inputsData.title ? (
+ <RAGFlowAvatar
+ className="size-10"
+ avatar={inputsData.avatar}
+ name={inputsData.title}
+ isPerson
+ />
+ ) : (
+ <SvgIcon
+ name={'assistant'}
+ width={'100%'}
+ className="size-10 fill-current"
+ />
+ ))}
+ <div className="px-2.5 py-1.5">
+ <LoadingDots className="text-text-secondary" />
+ </div>
+ </div>
+ )}
</div>
{isTaskMode || (
<div className="flex w-full justify-center md:mb-8">
diff --git a/web/src/pages/next-chats/share/index.tsx b/web/src/pages/next-chats/share/index.tsx
index f5e07c3187..e01fa64be4 100644
--- a/web/src/pages/next-chats/share/index.tsx
+++ b/web/src/pages/next-chats/share/index.tsx
@@ -89,6 +89,8 @@ const ChatContainer = () => {
derivedMessages.length - 1 === i
}
index={i}
+ isLast={derivedMessages.length - 1 === i}
+ sendLoading={sendLoading}
clickDocumentButton={clickDocumentButton}
showLikeButton={false}
showLoudspeaker={false}