This is an automated email from the ASF dual-hosted git repository.

lizhimins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new bd288864e fix(ai): keep unread counts stable when live messages 
persist (#6280)
bd288864e is described below

commit bd288864ef190acc39a0b0c3fad78500c7e772c6
Author: bai mengxuan <[email protected]>
AuthorDate: Fri Nov 27 12:28:09 2026 +0800

    fix(ai): keep unread counts stable when live messages persist (#6280)
---
 web/src/pages/ai/__tests__/AiPage.test.tsx         | 66 ++++++++++++++++++++++
 web/src/pages/ai/components/ChatThread.tsx         |  7 ++-
 .../ai/components/__tests__/ChatThread.test.tsx    | 43 ++++++++++++++
 3 files changed, 115 insertions(+), 1 deletion(-)

diff --git a/web/src/pages/ai/__tests__/AiPage.test.tsx 
b/web/src/pages/ai/__tests__/AiPage.test.tsx
index 20890d1c2..0bd830861 100644
--- a/web/src/pages/ai/__tests__/AiPage.test.tsx
+++ b/web/src/pages/ai/__tests__/AiPage.test.tsx
@@ -355,6 +355,72 @@ describe('AiPage', () => {
     
expect(screen.getByTestId('ai-send-stop-button')).toHaveAttribute('data-state', 
'send');
   });
 
+  it('doesNotCountPersistedCopiesOfVisibleLiveMessagesAsUnreadTest', async () 
=> {
+    const existing = [
+      { ...item(1, { type: 'user', text: 'earlier question' }, 40), turn: 1 },
+      { ...item(2, { type: 'text', text: 'earlier answer' }, 40), turn: 1 },
+      { ...item(3, { type: 'run_status', status: 'COMPLETED' }, 40), turn: 1 },
+    ];
+    
vi.mocked(getConversationTimeline).mockResolvedValue(timelinePage(existing));
+    let finishRun!: () => void;
+    const active = new Promise<void>((resolve) => {
+      finishRun = resolve;
+    });
+    vi.mocked(openRunStream).mockImplementation(async (_cid, _request, 
handlers) => {
+      handlers.onEvent({
+        type: 'run_started',
+        runId: 41,
+        conversationId: 7,
+        title: 'current question',
+        turn: 2,
+      });
+      handlers.onEvent({ type: 'text_delta', content: 'current answer' });
+      await active;
+      handlers.onEvent({ type: 'run_finished', runId: 41, status: 'COMPLETED', 
durationMs: 12 });
+    });
+    renderRouted('/ai/c/7');
+    expect(await screen.findByText('earlier answer')).toBeInTheDocument();
+    const input = await typeAndWaitForReady('current question');
+    fireEvent.keyDown(input, { key: 'Enter' });
+    expect(await screen.findByText('current answer')).toBeInTheDocument();
+    expect(screen.getByText('current question')).toBeInTheDocument();
+
+    // Read earlier history after both live messages have already been seen.
+    const scroller = screen.getByTestId('ai-thread-scroll');
+    Object.defineProperties(scroller, {
+      scrollHeight: { configurable: true, value: 1000 },
+      clientHeight: { configurable: true, value: 200 },
+      scrollTop: { configurable: true, writable: true, value: 200 },
+    });
+    fireEvent.scroll(scroller);
+    expect(screen.getByTestId('ai-thread-jump-to-latest')).toBeInTheDocument();
+    expect(screen.queryByTestId('ai-thread-unread')).not.toBeInTheDocument();
+
+    let finishHistory!: (page: AiTimelineVO) => void;
+    vi.mocked(getConversationTimeline).mockReturnValueOnce(
+      new Promise((resolve) => {
+        finishHistory = resolve;
+      }),
+    );
+    await act(async () => finishRun());
+    await waitFor(() => 
expect(getConversationTimeline).toHaveBeenCalledTimes(2));
+    expect(screen.queryByTestId('ai-thread-unread')).not.toBeInTheDocument();
+    await act(async () =>
+      finishHistory(
+        timelinePage([
+          ...existing,
+          { ...item(4, { type: 'user', text: 'current question' }), turn: 2 },
+          { ...item(5, { type: 'text', text: 'current answer' }), turn: 2 },
+          { ...item(6, { type: 'run_status', status: 'COMPLETED' }), turn: 2 },
+        ]),
+      ),
+    );
+    await waitFor(() => expect(screen.getAllByText('current 
answer')).toHaveLength(1));
+    expect(screen.getAllByText('current question')).toHaveLength(1);
+    
expect(screen.getByTestId('ai-send-stop-button')).toHaveAttribute('data-state', 
'send');
+    expect(screen.queryByTestId('ai-thread-unread')).not.toBeInTheDocument();
+  });
+
   it('attachesToTheStillRunningRunAfterNavigatingAwayAndBackTest', async () => 
{
     const user = userEvent.setup();
     vi.mocked(openRunStream).mockImplementation(
diff --git a/web/src/pages/ai/components/ChatThread.tsx 
b/web/src/pages/ai/components/ChatThread.tsx
index fbc649424..a45575078 100644
--- a/web/src/pages/ai/components/ChatThread.tsx
+++ b/web/src/pages/ai/components/ChatThread.tsx
@@ -134,7 +134,12 @@ const ChatThread = ({
   const [highlighted, setHighlighted] = useState<number | null>(null);
   const prevBubbleCountRef = useRef(bubbles.length);
 
-  const bubbleCount = bubbles.length + (liveBlocks && liveBlocks.length > 0 ? 
1 : 0);
+  // Match BubbleList's optimistic question and waiting/streaming answer so
+  // persisting the same messages does not look like a new arrival.
+  const bubbleCount =
+    bubbles.length +
+    (pendingUserText?.trim() ? 1 : 0) +
+    (liveBlocks && (streaming || liveBlocks.length > 0) ? 1 : 0);
 
   const followToBottom = useCallback((behavior: ScrollBehavior = 'smooth') => {
     sentinelRef.current?.scrollIntoView({ behavior });
diff --git a/web/src/pages/ai/components/__tests__/ChatThread.test.tsx 
b/web/src/pages/ai/components/__tests__/ChatThread.test.tsx
index 071579825..075287aa9 100644
--- a/web/src/pages/ai/components/__tests__/ChatThread.test.tsx
+++ b/web/src/pages/ai/components/__tests__/ChatThread.test.tsx
@@ -274,6 +274,49 @@ describe('ChatThread', () => {
     expect(scrollIntoView).not.toHaveBeenCalled();
   });
 
+  it('countsPendingQuestionAndWaitingAnswerOnceThroughPersistenceTest', () => {
+    const existing = [userBubble('earlier question'), assistantBubble('earlier 
answer')];
+    const { rerender } = renderThread({ bubbles: existing });
+    placeReader(600);
+    fireEvent.scroll(screen.getByTestId('ai-thread-scroll'));
+    scrollIntoView.mockClear();
+
+    rerender(
+      <LangProvider>
+        <ChatThread bubbles={existing} pendingUserText="new question" 
liveBlocks={[]} streaming />
+      </LangProvider>,
+    );
+    expect(screen.getByText('new question')).toBeInTheDocument();
+    expect(screen.getAllByTestId('ai-bubble-user-avatar')).toHaveLength(2);
+    expect(screen.getAllByTestId('ai-bubble-default-avatar')).toHaveLength(2);
+    expect(screen.getByTestId('ai-thread-unread')).toHaveTextContent('2');
+
+    rerender(
+      <LangProvider>
+        <ChatThread
+          bubbles={existing}
+          pendingUserText="new question"
+          liveBlocks={appendText([], 'new answer')}
+          streaming
+        />
+      </LangProvider>,
+    );
+    expect(screen.getByText('new answer')).toBeInTheDocument();
+    expect(screen.getByTestId('ai-thread-unread')).toHaveTextContent('2');
+
+    rerender(
+      <LangProvider>
+        <ChatThread
+          bubbles={[...existing, userBubble('new question'), 
assistantBubble('new answer')]}
+        />
+      </LangProvider>,
+    );
+    expect(screen.getAllByText('new question')).toHaveLength(1);
+    expect(screen.getAllByText('new answer')).toHaveLength(1);
+    expect(screen.getByTestId('ai-thread-unread')).toHaveTextContent('2');
+    expect(scrollIntoView).not.toHaveBeenCalled();
+  });
+
   it('jumpsBackToTheLatestAndClearsTheBadgeTest', async () => {
     const user = userEvent.setup();
     renderThread({ bubbles: [userBubble('检查集群状态')] });

Reply via email to