Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(conversation): stop replaying the fade-in over already-shown assistant text (#21603)

Arvin Xu0e641e99d4f6
diff --git a/src/features/Conversation/Messages/Assistant/components/MessageContent.tsx b/src/features/Conversation/Messages/Assistant/components/MessageContent.tsx
index 71cba84b6f6..241765d2b40 100644
--- a/src/features/Conversation/Messages/Assistant/components/MessageContent.tsx
+++ b/src/features/Conversation/Messages/Assistant/components/MessageContent.tsx
@@ -14,7 +14,11 @@ import { useMarkdown } from '../useMarkdown';
const MessageContent = memo<UIChatMessage>(
({ id, tools, content, chunksList, search, imageList, metadata, ...props }) => {
- const { drawer, markdownProps } = useMarkdown(id, !!tools?.length);
+ const { drawer, markdownProps } = useMarkdown(
+ id,
+ !!tools?.length,
+ !!content && content !== LOADING_FLAT,
+ );
// Use ConversationStore instead of ChatStore
const generating = useConversationStore(messageStateSelectors.isMessageGenerating(id));
const isCreating = useConversationStore(messageStateSelectors.isMessageCreating(id));
diff --git a/src/features/Conversation/Messages/Assistant/useMarkdown.tsx b/src/features/Conversation/Messages/Assistant/useMarkdown.tsx
index 1537cbd4b9b..652576c0e3f 100644
--- a/src/features/Conversation/Messages/Assistant/useMarkdown.tsx
+++ b/src/features/Conversation/Messages/Assistant/useMarkdown.tsx
@@ -5,13 +5,14 @@ import isEqual from 'fast-deep-equal';
import { dataSelectors, messageStateSelectors, useConversationStore } from '../../store';
import { useChatMarkdown } from '../useChatMarkdown';
-export const useMarkdown = (id: string, disableStreaming = false) => {
+export const useMarkdown = (id: string, disableStreaming = false, hasContent = false) => {
const item = useConversationStore(dataSelectors.getDbMessageById(id), isEqual)!;
const isGenerating = useConversationStore(messageStateSelectors.isMessageGenerating(id));
return useChatMarkdown({
citations: item?.search?.citations,
enableStream: !disableStreaming,
+ hasContent,
id,
isGenerating,
});
diff --git a/src/features/Conversation/Messages/AssistantGroup/components/MessageContent.test.tsx b/src/features/Conversation/Messages/AssistantGroup/components/MessageContent.test.tsx
index 01a20e36da4..ce5b521b723 100644
--- a/src/features/Conversation/Messages/AssistantGroup/components/MessageContent.test.tsx
+++ b/src/features/Conversation/Messages/AssistantGroup/components/MessageContent.test.tsx
@@ -45,7 +45,7 @@ vi.mock('../../../store', () => ({
useConversationStore: (selector: (state: unknown) => unknown) => selector({}),
}));
-type UseMarkdownArgs = [id: string, disableStreaming?: boolean];
+type UseMarkdownArgs = [id: string, disableStreaming?: boolean, hasContent?: boolean];
const useMarkdownMock = vi.fn((..._args: UseMarkdownArgs) => ({}));
@@ -80,7 +80,7 @@ describe('MessageContent', () => {
it('disables markdown streaming when the block already has tools below the text', () => {
render(<MessageContent hasToolsOverride contentOverride="lead sentence" id="block-1" />);
- expect(useMarkdownMock).toHaveBeenCalledWith('block-1', true);
+ expect(useMarkdownMock).toHaveBeenCalledWith('block-1', true, true);
});
it('keeps markdown streaming enabled when the block has no tools and is not disabled', () => {
@@ -88,7 +88,7 @@ describe('MessageContent', () => {
<MessageContent contentOverride="lead sentence" hasToolsOverride={false} id="block-1" />,
);
- expect(useMarkdownMock).toHaveBeenCalledWith('block-1', false);
+ expect(useMarkdownMock).toHaveBeenCalledWith('block-1', false, true);
});
it('disables markdown streaming when disableStreaming is set even without tools', () => {
@@ -101,7 +101,7 @@ describe('MessageContent', () => {
/>,
);
- expect(useMarkdownMock).toHaveBeenCalledWith('block-1', true);
+ expect(useMarkdownMock).toHaveBeenCalledWith('block-1', true, true);
});
it('renders multimodal image parts instead of the raw serialized JSON', () => {
diff --git a/src/features/Conversation/Messages/AssistantGroup/components/MessageContent.tsx b/src/features/Conversation/Messages/AssistantGroup/components/MessageContent.tsx
index 6f1a39aec10..719f7cf9ab7 100644
--- a/src/features/Conversation/Messages/AssistantGroup/components/MessageContent.tsx
+++ b/src/features/Conversation/Messages/AssistantGroup/components/MessageContent.tsx
@@ -58,7 +58,11 @@ const MessageContent = memo<MessageContentProps>(
// Once a tool call exists below this block's text, the text is already
// finalized β€” skip the streaming/fade-in animation so settled content above
// a tool doesn't keep re-animating.
- const { drawer, markdownProps } = useMarkdown(id, disableStreaming || hasTools);
+ const { drawer, markdownProps } = useMarkdown(
+ id,
+ disableStreaming || hasTools,
+ !!content && content !== LOADING_FLAT,
+ );
if (!content && !hasTools) return <ContentLoading id={id} startTime={createdAt} />;
diff --git a/src/features/Conversation/Messages/AssistantGroup/useMarkdown.tsx b/src/features/Conversation/Messages/AssistantGroup/useMarkdown.tsx
index edcd10fd2b7..abf6cae8050 100644
--- a/src/features/Conversation/Messages/AssistantGroup/useMarkdown.tsx
+++ b/src/features/Conversation/Messages/AssistantGroup/useMarkdown.tsx
@@ -1,13 +1,14 @@
import { messageStateSelectors, useConversationStore } from '../../store';
import { useChatMarkdown } from '../useChatMarkdown';
-export const useMarkdown = (id: string, disableStreaming = false) => {
+export const useMarkdown = (id: string, disableStreaming = false, hasContent = false) => {
const isGenerating = useConversationStore(
messageStateSelectors.isAssistantGroupItemGenerating(id),
);
return useChatMarkdown({
enableStream: !disableStreaming,
+ hasContent,
id,
isGenerating,
});
diff --git a/src/features/Conversation/Messages/useChatMarkdown.test.tsx b/src/features/Conversation/Messages/useChatMarkdown.test.tsx
index 2f71a95fba8..481df082ce5 100644
--- a/src/features/Conversation/Messages/useChatMarkdown.test.tsx
+++ b/src/features/Conversation/Messages/useChatMarkdown.test.tsx
@@ -68,4 +68,59 @@ describe('useChatMarkdown (assistant / grouped message pipeline)', () => {
expect(result.current.markdownProps.animated).toBe(false);
expect(result.current.markdownProps.enableStream).toBe(false);
});
+
+ it('animates a message that has never been on screen', () => {
+ mockTransitionMode = 'fadeIn';
+
+ const { result } = renderHook(() =>
+ useChatMarkdown({ hasContent: true, id: 'fresh-1', isGenerating: true }),
+ );
+
+ expect(result.current.markdownProps.animated).toBe(true);
+ });
+
+ it('does not re-enter streaming after the text settled statically', () => {
+ mockTransitionMode = 'fadeIn';
+
+ // A transient block below makes this one non-last, then disappears again.
+ const { result, rerender } = renderHook(
+ ({ enableStream }) =>
+ useChatMarkdown({ enableStream, hasContent: true, id: 'settle-1', isGenerating: true }),
+ { initialProps: { enableStream: false } },
+ );
+ rerender({ enableStream: true });
+
+ expect(result.current.markdownProps.animated).toBe(false);
+ expect(result.current.markdownProps.enableStream).toBe(false);
+ });
+
+ it('does not replay the fade when a message whose text was shown remounts', () => {
+ mockTransitionMode = 'fadeIn';
+
+ const first = renderHook(() =>
+ useChatMarkdown({ hasContent: true, id: 'remount-1', isGenerating: true }),
+ );
+ expect(first.result.current.markdownProps.animated).toBe(true);
+ first.unmount();
+
+ const second = renderHook(() =>
+ useChatMarkdown({ hasContent: true, id: 'remount-1', isGenerating: true }),
+ );
+
+ expect(second.result.current.markdownProps.animated).toBe(false);
+ });
+
+ it('still animates after a remount when no text had been shown yet', () => {
+ mockTransitionMode = 'fadeIn';
+
+ renderHook(() =>
+ useChatMarkdown({ hasContent: false, id: 'empty-1', isGenerating: true }),
+ ).unmount();
+
+ const { result } = renderHook(() =>
+ useChatMarkdown({ hasContent: true, id: 'empty-1', isGenerating: true }),
+ );
+
+ expect(result.current.markdownProps.animated).toBe(true);
+ });
});
diff --git a/src/features/Conversation/Messages/useChatMarkdown.tsx b/src/features/Conversation/Messages/useChatMarkdown.tsx
index 100893083a0..2026988223a 100644
--- a/src/features/Conversation/Messages/useChatMarkdown.tsx
+++ b/src/features/Conversation/Messages/useChatMarkdown.tsx
@@ -1,7 +1,7 @@
'use client';
import { type MarkdownProps } from '@lobehub/ui';
-import { type ReactNode, useMemo, useState } from 'react';
+import { type ReactNode, useEffect, useMemo, useState } from 'react';
import { HtmlPreviewDrawer } from '@/components/HtmlPreview';
import { useUserStore } from '@/store/user';
@@ -23,9 +23,19 @@ const remarkPlugins = assistantMarkdownElements
.map((element: MarkdownElement) => element.remarkPlugin)
.filter(Boolean);
+/**
+ * Message ids whose text has already been on screen. The stream renderer fades
+ * every character in from zero on mount, so a remount β€” the group re-partitions,
+ * or a stream patch and a snapshot refresh disagree about the block's shape β€”
+ * must not replay that fade over text the user has already read.
+ */
+const shownTextIds = new Set<string>();
+
interface UseChatMarkdownOptions {
citations?: MarkdownProps['citations'];
enableStream?: boolean;
+ /** Whether the message has text to show yet; loading placeholders do not count. */
+ hasContent?: boolean;
id: string;
isGenerating: boolean;
}
@@ -34,12 +44,22 @@ export const useChatMarkdown = ({
id,
isGenerating,
citations,
- enableStream = true,
+ enableStream: enableStreamOption = true,
+ hasContent = false,
}: UseChatMarkdownOptions): {
drawer: ReactNode;
markdownProps: Partial<MarkdownProps>;
} => {
const { transitionMode } = useUserStore(userGeneralSettingsSelectors.config);
+ // Once text has settled (rendered statically, or shown by an earlier mount),
+ // it never re-enters streaming: switching back remounts the stream renderer.
+ const [settled, setSettled] = useState(() => shownTextIds.has(id));
+ if (!settled && hasContent && !enableStreamOption) setSettled(true);
+ useEffect(() => {
+ if (hasContent) shownTextIds.add(id);
+ }, [hasContent, id]);
+
+ const enableStream = enableStreamOption && !settled;
const animated = enableStream && transitionMode === 'fadeIn' && isGenerating;
const [drawerContent, setDrawerContent] = useState<string | null>(null);