From b341f93805843d8acbf7e3ff69c8cf4ccc81265c Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Wed, 30 Sep 2026 10:19:49 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=8E=EF=B8=8F=20perf:=20Re-render=20Onl?= =?UTF-8?q?y=20the=20Tail=20Rows=20When=20a=20Message=20Is=20Sent=20(#1657?= =?UTF-8?q?2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every send moved the thread's tail (isSubmitting, latestMessageId, latestMessageDepth, and a handleContinue rebound per turn), and the memoized row context listed all of them as dependencies, so each move handed every row a new context object and re-rendered the whole thread: three commits of ~12,000 component renders on a 246-message thread, one of them inside the Enter keypress. - Keep the row context object stable; the tail values are ref-backed getters for call-time reads, and rows receive them as props that the row comparator (isSameTailRelation) only reacts to when the row enters or leaves the tail. - Fork reads the split target through a getter at click time. - HoverButtons subscribes to a boolean isSubmitting context and a stable operations context instead of the combined messages-view context; Fork and Feedback are memoized so the per-send toolbar pass stops there. - Make handleContinue referentially stable. - Cache the Intl formatters behind getMessageTimestamp per locale and clock. - Add a submit-lag benchmark (e2e/benchmarks-tree) that reports long tasks, Enter-to-row latency, a CPU profile and per-commit render reasons. --- client/src/Providers/MessagesViewContext.tsx | 73 +-- client/src/common/types.ts | 7 +- .../src/components/Chat/Messages/Feedback.tsx | 11 +- client/src/components/Chat/Messages/Fork.tsx | 16 +- .../components/Chat/Messages/HoverButtons.tsx | 14 +- .../src/components/Chat/Messages/Message.tsx | 11 +- .../components/Chat/Messages/MessageParts.tsx | 1 - .../Chat/Messages/__tests__/Fork.spec.tsx | 61 +++ .../Messages/__tests__/HoverButtons.spec.tsx | 118 ++++- .../Chat/Messages/ui/MessageRender.tsx | 17 +- .../src/components/Messages/ContentRender.tsx | 17 +- .../components/Messages/MessageContent.tsx | 11 +- .../__tests__/ContentRender.tail.spec.tsx | 240 +++++++++ .../Share/ShareMessagesProvider.tsx | 19 +- client/src/hooks/Chat/useChatHelpers.ts | 8 +- .../__tests__/useMemoizedChatContext.spec.tsx | 52 +- .../hooks/Messages/useMemoizedChatContext.ts | 48 +- .../src/hooks/Messages/useMessageActions.tsx | 10 +- client/src/utils/__tests__/messages.test.ts | 86 +++ client/src/utils/messages.ts | 72 ++- e2e/benchmarks-tree/submit-lag.perf.spec.ts | 493 ++++++++++++++++++ 21 files changed, 1248 insertions(+), 137 deletions(-) create mode 100644 client/src/components/Chat/Messages/__tests__/Fork.spec.tsx create mode 100644 client/src/components/Messages/__tests__/ContentRender.tail.spec.tsx create mode 100644 e2e/benchmarks-tree/submit-lag.perf.spec.ts diff --git a/client/src/Providers/MessagesViewContext.tsx b/client/src/Providers/MessagesViewContext.tsx index 1d9172c95b..afe54b5fe4 100644 --- a/client/src/Providers/MessagesViewContext.tsx +++ b/client/src/Providers/MessagesViewContext.tsx @@ -24,11 +24,29 @@ interface MessagesViewContextValue { setMessages: ReturnType['setMessages']; } +type MessagesOperations = Pick< + MessagesViewContextValue, + 'ask' | 'regenerate' | 'handleContinue' | 'getMessages' | 'setMessages' +>; + const MessagesViewContext = createContext(undefined); -// Export the context so it can be provided by other providers (e.g., ShareMessagesProvider) -export { MessagesViewContext }; -export type { MessagesViewContextValue }; +/** + * The view's operations alone. They are referentially stable, so components that + * only call them (every message row's hover actions) are not re-rendered by the + * submission and tail changes the combined context carries on each send. + */ +const MessagesOperationsContext = createContext(undefined); + +/** + * Whether the view is generating, alone. A boolean context re-renders its consumers + * only when a send starts or settles, which is when every row's rerun controls flip. + */ +const MessagesSubmittingContext = createContext(false); + +// Export the contexts so they can be provided by other providers (e.g., ShareMessagesProvider) +export { MessagesViewContext, MessagesOperationsContext, MessagesSubmittingContext }; +export type { MessagesViewContextValue, MessagesOperations }; export function MessagesViewProvider({ children }: { children: React.ReactNode }) { const chatContext = useChatContext(); @@ -101,10 +119,19 @@ export function MessagesViewProvider({ children }: { children: React.ReactNode } ); return ( - {children} + + + {children} + + ); } +/** Whether the view is generating; false outside a live messages view. */ +export function useMessagesIsSubmitting(): boolean { + return useContext(MessagesSubmittingContext); +} + export function useMessagesViewContext() { const context = useContext(MessagesViewContext); if (!context) { @@ -129,20 +156,15 @@ export function useMessagesSubmission() { } /** Hook for components that only need message operations */ -export function useMessagesOperations() { - const { ask, regenerate, handleContinue, getMessages, setMessages } = useMessagesViewContext(); - return useMemo( - () => ({ ask, regenerate, handleContinue, getMessages, setMessages }), - [ask, regenerate, handleContinue, getMessages, setMessages], - ); +export function useMessagesOperations(): MessagesOperations { + const context = useContext(MessagesOperationsContext); + if (!context) { + throw new Error('useMessagesOperations must be used within MessagesViewProvider'); + } + return context; } -type OptionalMessagesOps = Pick< - MessagesViewContextValue, - 'ask' | 'regenerate' | 'handleContinue' | 'getMessages' | 'setMessages' ->; - -const NOOP_OPS: OptionalMessagesOps = { +const NOOP_OPS: MessagesOperations = { ask: () => {}, regenerate: () => {}, handleContinue: () => {}, @@ -156,23 +178,8 @@ const NOOP_OPS: OptionalMessagesOps = { * be silently discarded rather than crashing. Callers must use optional chaining on * `getMessages()` results, as it returns `undefined` outside the provider. */ -export function useOptionalMessagesOperations(): OptionalMessagesOps { - const context = useContext(MessagesViewContext); - const ask = context?.ask; - const regenerate = context?.regenerate; - const handleContinue = context?.handleContinue; - const getMessages = context?.getMessages; - const setMessages = context?.setMessages; - return useMemo( - () => ({ - ask: ask ?? NOOP_OPS.ask, - regenerate: regenerate ?? NOOP_OPS.regenerate, - handleContinue: handleContinue ?? NOOP_OPS.handleContinue, - getMessages: getMessages ?? NOOP_OPS.getMessages, - setMessages: setMessages ?? NOOP_OPS.setMessages, - }), - [ask, regenerate, handleContinue, getMessages, setMessages], - ); +export function useOptionalMessagesOperations(): MessagesOperations { + return useContext(MessagesOperationsContext) ?? NOOP_OPS; } /** diff --git a/client/src/common/types.ts b/client/src/common/types.ts index 6d1ec5ce2a..15fefa73c5 100644 --- a/client/src/common/types.ts +++ b/client/src/common/types.ts @@ -399,8 +399,11 @@ export type TMessageChatContext = { index: number; regenerate: (message: t.TMessage, options?: { addedConvo?: t.TConversation | null }) => void; conversation: t.TConversation | null; - latestMessageId: string | undefined; - latestMessageDepth: number | undefined; + /** Getter backed by a ref, for reads at call time. Rendering reads the row's + * `latestMessageId` prop, which its comparator gates on the row's relation. */ + readonly latestMessageId: string | undefined; + /** Getter backed by a ref, for reads at call time (see `latestMessageId`). */ + readonly latestMessageDepth: number | undefined; handleContinue: (e: React.MouseEvent) => void; /** Resolved once per chat from `interface.feedback`; false until the config loads */ feedbackEnabled: boolean; diff --git a/client/src/components/Chat/Messages/Feedback.tsx b/client/src/components/Chat/Messages/Feedback.tsx index af6b063797..5db87e64bf 100644 --- a/client/src/components/Chat/Messages/Feedback.tsx +++ b/client/src/components/Chat/Messages/Feedback.tsx @@ -1,4 +1,4 @@ -import React, { useState, useCallback, useMemo, useEffect } from 'react'; +import React, { memo, useState, useCallback, useMemo, useEffect } from 'react'; import * as Ariakit from '@ariakit/react'; import { TFeedback, TFeedbackTag, getTagsForRating } from 'librechat-data-provider'; import { @@ -222,11 +222,7 @@ function FeedbackButtons({ const buttonClasses = (isActive: boolean, isLast: boolean) => hoverButtonClasses({ isActive, isLast }); -export default function Feedback({ - isLast = false, - handleFeedback, - feedback: initialFeedback, -}: FeedbackProps) { +function Feedback({ isLast = false, handleFeedback, feedback: initialFeedback }: FeedbackProps) { const localize = useLocalize(); const [openDialog, setOpenDialog] = useState(false); const [feedback, setFeedback] = useState(initialFeedback); @@ -342,3 +338,6 @@ export default function Feedback({ ); } + +/** Memoized: a send re-renders every row's toolbar, and nothing here depends on it. */ +export default memo(Feedback); diff --git a/client/src/components/Chat/Messages/Fork.tsx b/client/src/components/Chat/Messages/Fork.tsx index 341c7187fc..a5c4bebc4b 100644 --- a/client/src/components/Chat/Messages/Fork.tsx +++ b/client/src/components/Chat/Messages/Fork.tsx @@ -1,4 +1,4 @@ -import React, { useState, useRef } from 'react'; +import React, { memo, useState, useRef } from 'react'; import { useRecoilState } from 'recoil'; import * as Ariakit from '@ariakit/react'; import { VisuallyHidden } from '@ariakit/react'; @@ -199,17 +199,18 @@ const CheckboxOption: React.FC = ({ ); }; -export default function Fork({ +function Fork({ messageId, conversationId: _convoId, forkingSupported = false, - latestMessageId, + getLatestMessageId, isLast = false, }: { messageId: string; conversationId: string | null; forkingSupported?: boolean; - latestMessageId?: string; + /** Read when a fork starts: the split target must be the tail at click time. */ + getLatestMessageId?: () => string | undefined; isLast?: boolean; }) { const localize = useLocalize(); @@ -277,7 +278,7 @@ export default function Fork({ conversationId, option, splitAtTarget, - latestMessageId, + latestMessageId: getLatestMessageId?.(), }); }; @@ -337,7 +338,7 @@ export default function Fork({ splitAtTarget, conversationId, option: forkSetting, - latestMessageId, + latestMessageId: getLatestMessageId?.(), }); } else { popoverStore.toggle(); @@ -440,3 +441,6 @@ export default function Fork({ ); } + +/** Memoized: a send re-renders every row's toolbar, and nothing here depends on it. */ +export default memo(Fork); diff --git a/client/src/components/Chat/Messages/HoverButtons.tsx b/client/src/components/Chat/Messages/HoverButtons.tsx index 15c59e3438..0dc465de05 100644 --- a/client/src/components/Chat/Messages/HoverButtons.tsx +++ b/client/src/components/Chat/Messages/HoverButtons.tsx @@ -12,8 +12,8 @@ import { RegenerateIcon, } from '@librechat/client'; import type { TConversation, TMessage, TFeedback } from 'librechat-data-provider'; +import { useMessagesIsSubmitting, useOptionalMessagesOperations } from '~/Providers'; import { useGenerationsByLatest, useLocalize } from '~/hooks'; -import { useOptionalMessagesOperations } from '~/Providers'; import { hasEditablePart } from './Content/editableParts'; import { revealedQueuedTurnFamily } from '~/store/steer'; import { Fork } from '~/components/Conversations'; @@ -29,11 +29,14 @@ type THoverButtons = { copyToClipboard: (setIsCopied: React.Dispatch>) => void; getCanCopy: () => boolean; conversation: TConversation | null; - isSubmitting: boolean; message: TMessage; regenerate: () => void; handleContinue: (e: React.MouseEvent) => void; + /** The tail as of the row's last render, which the row re-renders on whenever + * this message enters or leaves it; compared against, never sent anywhere. */ latestMessageId?: string; + /** The tail at call time, for actions that send it (forking at a split target). */ + getLatestMessageId?: () => string | undefined; isLast: boolean; index: number; handleFeedback?: ({ feedback }: { feedback: TFeedback | undefined }) => void; @@ -129,15 +132,18 @@ const HoverButtons = ({ copyToClipboard, getCanCopy, conversation, - isSubmitting, message, regenerate, handleContinue, latestMessageId, + getLatestMessageId, isLast, handleFeedback, }: THoverButtons) => { const localize = useLocalize(); + /** Subscribed here rather than passed down: a send toggles the rerun controls on + * every row, and only this toolbar has to re-render for it. */ + const isSubmitting = useMessagesIsSubmitting(); const [isCopied, setIsCopied] = useState(false); const [TextToSpeech] = useRecoilState(store.textToSpeech); const { getMessages } = useOptionalMessagesOperations(); @@ -282,7 +288,7 @@ const HoverButtons = ({ messageId={message.messageId} conversationId={conversation.conversationId} forkingSupported={forkingSupported} - latestMessageId={latestMessageId} + getLatestMessageId={getLatestMessageId ?? (() => latestMessageId)} isLast={isLast} /> )} diff --git a/client/src/components/Chat/Messages/Message.tsx b/client/src/components/Chat/Messages/Message.tsx index 4ab2e8f963..9accb6ba2a 100644 --- a/client/src/components/Chat/Messages/Message.tsx +++ b/client/src/components/Chat/Messages/Message.tsx @@ -27,7 +27,8 @@ function Message(props: TMessageProps) { message: props.message, }); const { message } = props; - const { chatContext, effectiveIsSubmitting } = useMemoizedChatContext(message, isSubmitting); + const { chatContext, effectiveIsSubmitting, latestMessageId, latestMessageDepth } = + useMemoizedChatContext(message, isSubmitting); if (!message || typeof message !== 'object') { return null; @@ -36,7 +37,13 @@ function Message(props: TMessageProps) { return (
- +
); diff --git a/client/src/components/Chat/Messages/MessageParts.tsx b/client/src/components/Chat/Messages/MessageParts.tsx index efd1f8bc35..01fcb3534a 100644 --- a/client/src/components/Chat/Messages/MessageParts.tsx +++ b/client/src/components/Chat/Messages/MessageParts.tsx @@ -162,7 +162,6 @@ function MessageParts(props: TMessageProps) { isEditing={edit} message={message} enterEdit={enterEdit} - isSubmitting={isSubmitting} conversation={conversation ?? null} regenerate={() => regenerateMessage()} copyToClipboard={copyToClipboard} diff --git a/client/src/components/Chat/Messages/__tests__/Fork.spec.tsx b/client/src/components/Chat/Messages/__tests__/Fork.spec.tsx new file mode 100644 index 0000000000..8d59786b76 --- /dev/null +++ b/client/src/components/Chat/Messages/__tests__/Fork.spec.tsx @@ -0,0 +1,61 @@ +import React from 'react'; +import { RecoilRoot } from 'recoil'; +import { ForkOptions } from 'librechat-data-provider'; +import { fireEvent, render, screen } from '@testing-library/react'; +import store from '~/store'; +import Fork from '../Fork'; + +const mockMutate = jest.fn(); + +jest.mock('~/data-provider', () => ({ + useForkConvoMutation: () => ({ mutate: mockMutate }), +})); + +jest.mock('~/hooks', () => ({ + useLocalize: () => (key: string) => key, + useNavigateToConvo: () => ({ navigateToConvo: jest.fn() }), +})); + +jest.mock('@librechat/client', () => ({ + ...jest.requireActual('@librechat/client'), + useToastContext: () => ({ showToast: jest.fn() }), +})); + +describe('Fork', () => { + beforeEach(() => { + mockMutate.mockClear(); + }); + + /** The row holding this button does not re-render when the tail moves, so the + * split target is read when the fork starts rather than captured at render. */ + it('sends the tail current at click time as the split target', () => { + let latestMessageId = 'assistant-2'; + render( + { + set(store.rememberDefaultFork, true); + set(store.forkSetting, ForkOptions.TARGET_LEVEL); + set(store.splitAtTarget, true); + }} + > + latestMessageId} + /> + , + ); + + latestMessageId = 'assistant-3'; + fireEvent.click(screen.getByRole('button', { name: 'com_ui_fork_open_menu' })); + + expect(mockMutate).toHaveBeenCalledWith( + expect.objectContaining({ + messageId: 'user-1', + splitAtTarget: true, + latestMessageId: 'assistant-3', + }), + ); + }); +}); diff --git a/client/src/components/Chat/Messages/__tests__/HoverButtons.spec.tsx b/client/src/components/Chat/Messages/__tests__/HoverButtons.spec.tsx index 6ea8f3d11a..55cd394c0f 100644 --- a/client/src/components/Chat/Messages/__tests__/HoverButtons.spec.tsx +++ b/client/src/components/Chat/Messages/__tests__/HoverButtons.spec.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { MemoryRouter } from 'react-router-dom'; -import { render, screen } from '@testing-library/react'; import { RecoilRoot, type MutableSnapshot } from 'recoil'; +import { act, render, screen } from '@testing-library/react'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import { Constants, @@ -11,8 +11,9 @@ import { type TMessage, } from 'librechat-data-provider'; import { - MessagesViewContext, - type MessagesViewContextValue, + MessagesOperationsContext, + MessagesSubmittingContext, + type MessagesOperations, } from '~/Providers/MessagesViewContext'; import { hasCopyableText } from '~/hooks/Messages/useCopyToClipboard'; import HoverButtons from '~/components/Chat/Messages/HoverButtons'; @@ -62,27 +63,28 @@ function renderHoverButtons({ const { container } = render( - thread } as unknown as MessagesViewContextValue} + thread } as unknown as MessagesOperations} > - - - - + + + + + + , ); @@ -533,6 +535,76 @@ describe('HoverButtons edit affordance', () => { }); }); +/** + * The row above these controls no longer re-renders when a send starts or settles, + * so the rerun gate has to follow the pane's submission state on its own. + */ +describe('HoverButtons submission state', () => { + const assistantMessage = { + ...userMessage, + messageId: 'assistant-earlier', + parentMessageId: userMessage.messageId, + isCreatedByUser: false, + text: 'An earlier answer', + } as TMessage; + + let setSubmitting: (value: boolean) => void = () => undefined; + /** Holds the pane's submission state; the toolbar element itself never changes. */ + function SubmittingHost({ children }: { children: React.ReactNode }) { + const [submitting, setState] = React.useState(false); + setSubmitting = setState; + return ( + + {children} + + ); + } + + it('withholds and restores rerun on a settled row as a send starts and ends', () => { + const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); + const buttons = ( + true} + latestMessageId="assistant-latest" + /> + ); + render( + + set(store.textToSpeech, false)}> + [userMessage, assistantMessage], + } as unknown as MessagesOperations + } + > + + {buttons} + + + + , + ); + + expect(screen.getByTestId('regenerate-generation-button')).toBeInTheDocument(); + + act(() => setSubmitting(true)); + expect(screen.queryByTestId('regenerate-generation-button')).toBeNull(); + + act(() => setSubmitting(false)); + expect(screen.getByTestId('regenerate-generation-button')).toBeInTheDocument(); + }); +}); + describe('HoverButtons feedback affordance', () => { const assistantMessage = { ...userMessage, diff --git a/client/src/components/Chat/Messages/ui/MessageRender.tsx b/client/src/components/Chat/Messages/ui/MessageRender.tsx index 37a3a9f8d0..35f98a2a5b 100644 --- a/client/src/components/Chat/Messages/ui/MessageRender.tsx +++ b/client/src/components/Chat/Messages/ui/MessageRender.tsx @@ -3,10 +3,11 @@ import { useRecoilValue } from 'recoil'; import type { TMessage } from 'librechat-data-provider'; import type { TMessageProps, TMessageIcon, TMessageChatContext } from '~/common'; import { - areMessageFieldsEqual, cn, - getHeaderPrefixForScreenReader, + isSameTailRelation, getMessageAriaLabel, + areMessageFieldsEqual, + getHeaderPrefixForScreenReader, } from '~/utils'; import { revealOnRowHoverClasses, messageFooterClasses } from '~/components/Chat/Messages/styles'; import { parseWakeupText } from '~/components/Chat/Messages/Content/Parts/wakeup'; @@ -33,6 +34,9 @@ type MessageRenderProps = { isSubmitting?: boolean; /** Stable context object from wrapper — avoids ChatContext subscription inside memo */ chatContext: TMessageChatContext; + /** The thread's tail; the comparator re-renders only when this row's relation to it changes */ + latestMessageId?: string; + latestMessageDepth?: number; } & Pick< TMessageProps, 'currentEditId' | 'setCurrentEditId' | 'siblingIdx' | 'setSiblingIdx' | 'siblingCount' @@ -66,7 +70,7 @@ function areMessageRenderPropsEqual(prev: MessageRenderProps, next: MessageRende return false; } - return areMessageFieldsEqual(prev.message, next.message); + return areMessageFieldsEqual(prev.message, next.message) && isSameTailRelation(prev, next); } const MessageRender = memo(function MessageRender({ @@ -78,6 +82,8 @@ const MessageRender = memo(function MessageRender({ setCurrentEditId, isSubmitting = false, chatContext, + latestMessageId, + latestMessageDepth, }: MessageRenderProps) { const localize = useLocalize(); const { @@ -91,11 +97,9 @@ const MessageRender = memo(function MessageRender({ messageLabel, handleFeedback, handleContinue, - latestMessageId, copyToClipboard, getCanCopy, regenerateMessage, - latestMessageDepth, hasConfiguredSender, } = useMessageActions({ message: msg, @@ -106,6 +110,7 @@ const MessageRender = memo(function MessageRender({ const maximizeChatSpace = useRecoilValue(store.maximizeChatSpace); const handleRegenerateMessage = useCallback(() => regenerateMessage(), [regenerateMessage]); + const getLatestMessageId = useCallback(() => chatContext.latestMessageId, [chatContext]); const hasNoChildren = !(msg?.children?.length ?? 0); const isLast = useMemo( () => hasNoChildren && (msg?.depth === latestMessageDepth || msg?.depth === -1), @@ -206,13 +211,13 @@ const MessageRender = memo(function MessageRender({ isEditing={edit} message={msg} enterEdit={enterEdit} - isSubmitting={chatContext.isSubmitting} conversation={conversation ?? null} regenerate={handleRegenerateMessage} copyToClipboard={copyToClipboard} getCanCopy={getCanCopy} handleContinue={handleContinue} latestMessageId={latestMessageId} + getLatestMessageId={getLatestMessageId} handleFeedback={handleFeedback} isLast={isLast} /> diff --git a/client/src/components/Messages/ContentRender.tsx b/client/src/components/Messages/ContentRender.tsx index 8207d59440..e02312457a 100644 --- a/client/src/components/Messages/ContentRender.tsx +++ b/client/src/components/Messages/ContentRender.tsx @@ -5,10 +5,11 @@ import { Constants } from 'librechat-data-provider'; import type { TMessage, TMessageContentParts } from 'librechat-data-provider'; import type { TMessageProps, TMessageIcon, TMessageChatContext } from '~/common'; import { - areMessageFieldsEqual, cn, - getHeaderPrefixForScreenReader, + isSameTailRelation, getMessageAriaLabel, + areMessageFieldsEqual, + getHeaderPrefixForScreenReader, } from '~/utils'; import { revealOnRowHoverClasses, messageFooterClasses } from '~/components/Chat/Messages/styles'; import { useLocalize, useAttachments, useMessageActions, useContentMetadata } from '~/hooks'; @@ -43,6 +44,9 @@ type ContentRenderProps = { isSubmitting?: boolean; /** Stable context object from wrapper — avoids ChatContext subscription inside memo */ chatContext: TMessageChatContext; + /** The thread's tail; the comparator re-renders only when this row's relation to it changes */ + latestMessageId?: string; + latestMessageDepth?: number; } & Pick< TMessageProps, 'currentEditId' | 'setCurrentEditId' | 'siblingIdx' | 'setSiblingIdx' | 'siblingCount' @@ -75,7 +79,7 @@ function areContentRenderPropsEqual(prev: ContentRenderProps, next: ContentRende return false; } - return areMessageFieldsEqual(prev.message, next.message); + return areMessageFieldsEqual(prev.message, next.message) && isSameTailRelation(prev, next); } const ContentRender = memo(function ContentRender({ @@ -87,6 +91,8 @@ const ContentRender = memo(function ContentRender({ setCurrentEditId, isSubmitting = false, chatContext, + latestMessageId, + latestMessageDepth, }: ContentRenderProps) { const localize = useLocalize(); const { attachments, searchResults } = useAttachments({ @@ -103,11 +109,9 @@ const ContentRender = memo(function ContentRender({ messageLabel, handleContinue, handleFeedback, - latestMessageId, copyToClipboard, getCanCopy, regenerateMessage, - latestMessageDepth, hasConfiguredSender, } = useMessageActions({ message: msg, @@ -121,6 +125,7 @@ const ContentRender = memo(function ContentRender({ const showThinking = useAtomValue(showThinkingAtom); const handleRegenerateMessage = useCallback(() => regenerateMessage(), [regenerateMessage]); + const getLatestMessageId = useCallback(() => chatContext.latestMessageId, [chatContext]); const isLast = useMemo( () => !(msg?.children?.length ?? 0) && (msg?.depth === latestMessageDepth || msg?.depth === -1), [msg?.children, msg?.depth, latestMessageDepth], @@ -206,13 +211,13 @@ const ContentRender = memo(function ContentRender({ message={msg} isEditing={edit} enterEdit={enterEdit} - isSubmitting={chatContext.isSubmitting} conversation={conversation ?? null} regenerate={handleRegenerateMessage} copyToClipboard={copyToClipboard} getCanCopy={getCanCopy} handleContinue={handleContinue} latestMessageId={latestMessageId} + getLatestMessageId={getLatestMessageId} handleFeedback={handleFeedback} isLast={isLast} /> diff --git a/client/src/components/Messages/MessageContent.tsx b/client/src/components/Messages/MessageContent.tsx index 4c1b5d5e05..cdb0a5b889 100644 --- a/client/src/components/Messages/MessageContent.tsx +++ b/client/src/components/Messages/MessageContent.tsx @@ -27,7 +27,8 @@ function MessageContent(props: TMessageProps) { message: props.message, }); const { message } = props; - const { chatContext, effectiveIsSubmitting } = useMemoizedChatContext(message, isSubmitting); + const { chatContext, effectiveIsSubmitting, latestMessageId, latestMessageDepth } = + useMemoizedChatContext(message, isSubmitting); if (!message || typeof message !== 'object') { return null; @@ -36,7 +37,13 @@ function MessageContent(props: TMessageProps) { return (
- +
); diff --git a/client/src/components/Messages/__tests__/ContentRender.tail.spec.tsx b/client/src/components/Messages/__tests__/ContentRender.tail.spec.tsx new file mode 100644 index 0000000000..dcf006b7ed --- /dev/null +++ b/client/src/components/Messages/__tests__/ContentRender.tail.spec.tsx @@ -0,0 +1,240 @@ +import React, { useMemo, useState } from 'react'; +import { RecoilRoot } from 'recoil'; +import { act, render } from '@testing-library/react'; +import type { TConversation, TMessage } from 'librechat-data-provider'; +import { ChatContext, MessagesViewProvider, useChatContext } from '~/Providers'; +import MessageContent from '../MessageContent'; + +const mockRowRenders = new Map(); + +jest.mock('~/components/Chat/Messages/ui/MessageRow', () => ({ + __esModule: true, + default: ({ + id, + children, + footer, + }: { + id?: string; + children: React.ReactNode; + footer: React.ReactNode; + }) => { + mockRowRenders.set(id ?? '', (mockRowRenders.get(id ?? '') ?? 0) + 1); + return ( +
+ {children} + {footer} +
+ ); + }, +})); + +jest.mock('~/components/Chat/Messages/Content/ContentParts', () => ({ + __esModule: true, + default: () => null, +})); + +jest.mock('~/components/Chat/Messages/HoverButtons', () => ({ + __esModule: true, + default: () => null, +})); + +jest.mock('~/components/Chat/Messages/MessageIcon', () => ({ + __esModule: true, + default: () => null, +})); + +jest.mock('~/components/Chat/Messages/Elapsed', () => ({ + __esModule: true, + default: () => null, + shouldShowElapsed: () => false, +})); + +jest.mock('~/hooks', () => ({ + useMessageProcess: jest.requireActual('~/hooks/Messages/useMessageProcess').default, + useMemoizedChatContext: jest.requireActual('~/hooks/Messages/useMemoizedChatContext').default, + useLocalize: () => (key: string) => key, + useAttachments: () => ({ attachments: undefined, searchResults: undefined }), + useContentMetadata: () => ({ hasParallelContent: false }), + useMessageActions: ({ chatContext }: { chatContext: { conversation: TConversation } }) => ({ + edit: false, + index: 0, + agent: undefined, + assistant: undefined, + conversation: chatContext.conversation, + messageLabel: 'Assistant', + enterEdit: mockNoop, + handleContinue: mockNoop, + handleFeedback: undefined, + copyToClipboard: mockNoop, + getCanCopy: () => true, + regenerateMessage: mockNoop, + hasConfiguredSender: false, + }), +})); + +function mockNoop() {} + +const conversation = { + conversationId: 'convo-tail', + endpoint: 'openAI', + model: 'gpt-4', +} as TConversation; + +function row(messageId: string, parentMessageId: string, depth: number, isCreatedByUser: boolean) { + return { + messageId, + parentMessageId, + depth, + isCreatedByUser, + conversationId: conversation.conversationId, + sender: isCreatedByUser ? 'User' : 'Assistant', + text: messageId, + content: [{ type: 'text', text: messageId }], + createdAt: '2026-09-30T12:00:00.000Z', + children: [], + } as unknown as TMessage; +} + +/** Links each row to the next, as the thread index does for the visible path. */ +function thread(rows: TMessage[]): TMessage[] { + return rows.map((message, index) => ({ + ...message, + children: rows[index + 1] ? [rows[index + 1]] : [], + })); +} + +const settled = [ + row('user-1', '00000000-0000-0000-0000-000000000000', 0, true), + row('assistant-1', 'user-1', 1, false), + row('user-2', 'assistant-1', 2, true), + row('assistant-2', 'user-2', 3, false), +]; + +type ThreadState = { + messages: TMessage[]; + latestMessageId: string; + isSubmitting: boolean; +}; + +let setThreadState: React.Dispatch> = () => undefined; +const setCurrentEditId = () => undefined; +const setSiblingIdx = () => undefined; +const stableHelpers = { + ask: jest.fn(), + regenerate: jest.fn(), + handleContinue: jest.fn(), + setAbortScroll: jest.fn(), + getMessages: jest.fn(), + setMessages: jest.fn(), +}; + +function Thread() { + const [state, setState] = useState({ + messages: thread(settled), + latestMessageId: 'assistant-2', + isSubmitting: false, + }); + setThreadState = setState; + const latestMessageDepth = state.messages.find( + (message) => message.messageId === state.latestMessageId, + )?.depth; + + const chatHelpers = useMemo( + () => + ({ + ...stableHelpers, + index: 0, + conversation, + abortScroll: false, + feedbackEnabled: true, + isSubmitting: state.isSubmitting, + latestMessageId: state.latestMessageId, + latestMessageDepth, + }) as unknown as ReturnType, + [state.isSubmitting, state.latestMessageId, latestMessageDepth], + ); + + return ( + + + {state.messages.map((message) => ( + + ))} + + + ); +} + +function rendersOf(...ids: string[]) { + return ids.map((id) => mockRowRenders.get(id) ?? 0); +} + +describe('message rows across a send', () => { + beforeEach(() => { + mockRowRenders.clear(); + render( + + + , + ); + mockRowRenders.clear(); + }); + + it('re-renders only the rows entering or leaving the tail when a message is sent', () => { + act(() => { + setThreadState(() => ({ + messages: thread([ + ...settled, + row('user-3', 'assistant-2', 4, true), + row('assistant-3_', 'user-3', 5, false), + ]), + latestMessageId: 'assistant-3_', + isSubmitting: true, + })); + }); + + expect(rendersOf('user-1', 'assistant-1', 'user-2')).toEqual([0, 0, 0]); + expect(rendersOf('assistant-2', 'user-3', 'assistant-3_')).toEqual([1, 1, 1]); + }); + + it('leaves settled rows alone when the streaming id hydrates and the run settles', () => { + const sent = [ + ...settled, + row('user-3', 'assistant-2', 4, true), + row('assistant-3_', 'user-3', 5, false), + ]; + act(() => { + setThreadState({ + messages: thread(sent), + latestMessageId: 'assistant-3_', + isSubmitting: true, + }); + }); + mockRowRenders.clear(); + + const hydrated = [...sent.slice(0, 5), row('assistant-3', 'user-3', 5, false)]; + act(() => { + setThreadState({ + messages: thread(hydrated), + latestMessageId: 'assistant-3', + isSubmitting: true, + }); + }); + act(() => { + setThreadState((current) => ({ ...current, isSubmitting: false })); + }); + + expect(rendersOf('user-1', 'assistant-1', 'user-2', 'assistant-2', 'user-3')).toEqual([ + 0, 0, 0, 0, 0, + ]); + expect(mockRowRenders.get('assistant-3')).toBeGreaterThan(0); + }); +}); diff --git a/client/src/components/Share/ShareMessagesProvider.tsx b/client/src/components/Share/ShareMessagesProvider.tsx index cfaeff338c..7a958a6f59 100644 --- a/client/src/components/Share/ShareMessagesProvider.tsx +++ b/client/src/components/Share/ShareMessagesProvider.tsx @@ -1,7 +1,7 @@ import React, { useMemo } from 'react'; import type { TMessage } from 'librechat-data-provider'; -import type { MessagesViewContextValue } from '~/Providers/MessagesViewContext'; -import { MessagesViewContext } from '~/Providers/MessagesViewContext'; +import type { MessagesViewContextValue, MessagesOperations } from '~/Providers/MessagesViewContext'; +import { MessagesViewContext, MessagesOperationsContext } from '~/Providers/MessagesViewContext'; interface ShareMessagesProviderProps { messages: TMessage[]; @@ -37,7 +37,20 @@ export function ShareMessagesProvider({ messages, children }: ShareMessagesProvi [messages], ); + const operations = useMemo( + () => ({ + ask: contextValue.ask, + regenerate: contextValue.regenerate, + handleContinue: contextValue.handleContinue, + getMessages: contextValue.getMessages, + setMessages: contextValue.setMessages, + }), + [contextValue], + ); + return ( - {children} + + {children} + ); } diff --git a/client/src/hooks/Chat/useChatHelpers.ts b/client/src/hooks/Chat/useChatHelpers.ts index 46eb08bc29..450a79b189 100644 --- a/client/src/hooks/Chat/useChatHelpers.ts +++ b/client/src/hooks/Chat/useChatHelpers.ts @@ -109,6 +109,10 @@ export default function useChatHelpers(index = 0, paramId?: string) { const setSiblingIdx = useSetAtom( siblingIdxFamily(siblingKey(latestMessage?.parentMessageId ?? null)), ); + /** The setter is rebound whenever the tail's parent changes (every turn); the + * ref keeps `handleContinue` referentially stable so rows do not re-render. */ + const setSiblingIdxRef = useRef(setSiblingIdx); + setSiblingIdxRef.current = setSiblingIdx; const setMessages = useCallback( (messages: TMessage[]) => { @@ -374,9 +378,9 @@ export default function useChatHelpers(index = 0, paramId?: string) { (e: React.MouseEvent) => { e.preventDefault(); continueGeneration(); - setSiblingIdx(0); + setSiblingIdxRef.current(0); }, - [continueGeneration, setSiblingIdx], + [continueGeneration], ); const [preset, setPreset] = useRecoilState(store.presetByIndex(index)); diff --git a/client/src/hooks/Messages/__tests__/useMemoizedChatContext.spec.tsx b/client/src/hooks/Messages/__tests__/useMemoizedChatContext.spec.tsx index 75b91929d5..50d410db5c 100644 --- a/client/src/hooks/Messages/__tests__/useMemoizedChatContext.spec.tsx +++ b/client/src/hooks/Messages/__tests__/useMemoizedChatContext.spec.tsx @@ -1,7 +1,7 @@ import { renderHook } from '@testing-library/react'; import type { TConversation, TMessage } from 'librechat-data-provider'; -import { useChatContext } from '~/Providers'; import useMemoizedChatContext from '../useMemoizedChatContext'; +import { useChatContext } from '~/Providers'; jest.mock('~/Providers', () => ({ useChatContext: jest.fn(), @@ -27,15 +27,19 @@ const message = (overrides: Partial = {}) => ...overrides, }) as TMessage; -function mockChatContext(latestMessageId: string | undefined) { +const stableHelpers = { + ask: jest.fn(), + regenerate: jest.fn(), + handleContinue: jest.fn(), +}; + +function mockChatContext(latestMessageId: string | undefined, latestMessageDepth = -1) { mockUseChatContext.mockReturnValue({ - ask: jest.fn(), + ...stableHelpers, index: 0, - regenerate: jest.fn(), conversation, latestMessageId, - latestMessageDepth: -1, - handleContinue: jest.fn(), + latestMessageDepth, isSubmitting: true, } as unknown as ReturnType); } @@ -61,4 +65,40 @@ describe('useMemoizedChatContext', () => { expect(result.current.effectiveIsSubmitting).toBe(false); expect(result.current.chatContext.isSubmitting).toBe(true); }); + + /** Each send and each server id hydration moves the tail. A context object keyed + * on it re-rendered every memoized row in the thread; rows now read the tail + * from props their comparator gates, and the object keeps its identity. */ + it('keeps the context object stable while the tail and submission state move', () => { + mockChatContext('assistant-2', 3); + const row = message({ messageId: 'assistant-1', depth: 1 }); + const { result, rerender } = renderHook( + ({ submitting }: { submitting: boolean }) => useMemoizedChatContext(row, submitting), + { initialProps: { submitting: false } }, + ); + const initial = result.current.chatContext; + + mockChatContext('assistant-3_', 5); + rerender({ submitting: true }); + + expect(result.current.chatContext).toBe(initial); + expect(result.current.latestMessageId).toBe('assistant-3_'); + expect(result.current.latestMessageDepth).toBe(5); + }); + + it('reads the current tail and submission state through the stable object', () => { + mockChatContext('assistant-2', 3); + const { result, rerender } = renderHook( + ({ submitting }: { submitting: boolean }) => useMemoizedChatContext(message(), submitting), + { initialProps: { submitting: false } }, + ); + const { chatContext } = result.current; + + mockChatContext('assistant-3', 5); + rerender({ submitting: true }); + + expect(chatContext.latestMessageId).toBe('assistant-3'); + expect(chatContext.latestMessageDepth).toBe(5); + expect(chatContext.isSubmitting).toBe(true); + }); }); diff --git a/client/src/hooks/Messages/useMemoizedChatContext.ts b/client/src/hooks/Messages/useMemoizedChatContext.ts index 94430e37ae..6bff196fa9 100644 --- a/client/src/hooks/Messages/useMemoizedChatContext.ts +++ b/client/src/hooks/Messages/useMemoizedChatContext.ts @@ -3,24 +3,39 @@ import type { TMessage } from 'librechat-data-provider'; import type { TMessageChatContext } from '~/common/types'; import { useChatContext } from '~/Providers'; +type LiveChatValues = { + isSubmitting: boolean; + latestMessageId: string | undefined; + latestMessageDepth: number | undefined; +}; + /** * Creates a stable `TMessageChatContext` object for memo'd message components. * * Subscribes to `useChatContext()` internally (intended to be called from non-memo'd * wrapper components like `Message` and `MessageContent`), then produces: - * - A `chatContext` object that stays referentially stable during streaming - * (uses a getter for `isSubmitting` backed by a ref) + * - A `chatContext` object that stays referentially stable across submissions and + * new turns: `isSubmitting`, `latestMessageId` and `latestMessageDepth` are + * getters backed by a ref, for reads at call time (click handlers) * - A stable `conversation` reference that only updates when rendering-relevant fields change * - An `effectiveIsSubmitting` value (false for non-latest messages) + * - The current `latestMessageId` / `latestMessageDepth`, which the wrapper passes + * to the row as props so the row's comparator can ignore changes that leave + * this row's relation to the tail unchanged (see `isSameTailRelation`) + * + * Every submission and every server id hydration moves the tail, so keying the + * context object on those values re-rendered every row of the thread several + * times per send, when only the rows entering or leaving the tail change. */ export default function useMemoizedChatContext( message: TMessage | null | undefined, isSubmitting: boolean, ) { const chatCtx = useChatContext(); + const { latestMessageId, latestMessageDepth } = chatCtx; - const isSubmittingRef = useRef(isSubmitting); - isSubmittingRef.current = isSubmitting; + const liveRef = useRef({ isSubmitting, latestMessageId, latestMessageDepth }); + liveRef.current = { isSubmitting, latestMessageId, latestMessageDepth }; /** * Stabilize conversation: only update when rendering-relevant fields change, @@ -39,44 +54,37 @@ export default function useMemoizedChatContext( ], ); - /** - * `isSubmitting` is included in deps so that chatContext gets a new reference - * when streaming starts/ends (2x per session). This ensures HoverButtons - * re-renders to update regenerate/edit button visibility via useGenerationsByLatest. - * The getter pattern is still valuable: callbacks reading chatContext.isSubmitting - * at call-time always get the current value even between these re-renders. - */ const chatContext: TMessageChatContext = useMemo( () => ({ ask: chatCtx.ask, index: chatCtx.index, regenerate: chatCtx.regenerate, conversation: stableConversation, - latestMessageId: chatCtx.latestMessageId, - latestMessageDepth: chatCtx.latestMessageDepth, handleContinue: chatCtx.handleContinue, feedbackEnabled: chatCtx.feedbackEnabled, get isSubmitting() { - return isSubmittingRef.current; + return liveRef.current.isSubmitting; + }, + get latestMessageId() { + return liveRef.current.latestMessageId; + }, + get latestMessageDepth() { + return liveRef.current.latestMessageDepth; }, }), - // eslint-disable-next-line react-hooks/exhaustive-deps [ chatCtx.ask, chatCtx.index, chatCtx.regenerate, stableConversation, - chatCtx.latestMessageId, - chatCtx.latestMessageDepth, chatCtx.handleContinue, chatCtx.feedbackEnabled, - isSubmitting, // intentional: forces new reference on streaming start/end so HoverButtons re-renders ], ); const messageId = message?.messageId ?? null; - const isLatestMessage = messageId === chatCtx.latestMessageId; + const isLatestMessage = messageId === latestMessageId; const effectiveIsSubmitting = isLatestMessage ? isSubmitting : false; - return { chatContext, effectiveIsSubmitting }; + return { chatContext, effectiveIsSubmitting, latestMessageId, latestMessageDepth }; } diff --git a/client/src/hooks/Messages/useMessageActions.tsx b/client/src/hooks/Messages/useMessageActions.tsx index 6f5a56b5b7..9ac1f616d8 100644 --- a/client/src/hooks/Messages/useMessageActions.tsx +++ b/client/src/hooks/Messages/useMessageActions.tsx @@ -45,13 +45,11 @@ export default function useMessageActions(props: TMessageActions) { index, regenerate, conversation, - latestMessageId, - latestMessageDepth, handleContinue, feedbackEnabled, - // NOTE: isSubmitting is intentionally NOT destructured here. - // chatContext.isSubmitting is a getter backed by a ref — destructuring - // would capture a one-time snapshot. Always access via chatContext.isSubmitting. + // NOTE: isSubmitting, latestMessageId and latestMessageDepth are intentionally + // NOT destructured here. They are getters backed by a ref — destructuring + // would capture a one-time snapshot. Rows render them from their own props. } = chatContext; const getAddedConvo = useGetAddedConvo(); @@ -205,8 +203,6 @@ export default function useMessageActions(props: TMessageActions) { handleFeedback: feedbackEnabled ? handleFeedback : undefined, handleContinue, copyToClipboard, - latestMessageId, regenerateMessage, - latestMessageDepth, }; } diff --git a/client/src/utils/__tests__/messages.test.ts b/client/src/utils/__tests__/messages.test.ts index 492d942e33..5f42bd0cc7 100644 --- a/client/src/utils/__tests__/messages.test.ts +++ b/client/src/utils/__tests__/messages.test.ts @@ -13,6 +13,7 @@ import { getHeaderPrefixForScreenReader, areMessageFieldsEqual, areMessageRowPropsEqual, + isSameTailRelation, isSubmittableMessage, createDualMessageContent, } from '../messages'; @@ -287,6 +288,55 @@ describe('getMessageTimestamp', () => { expect(() => getMessageTimestamp(iso, 'not a locale!!')).not.toThrow(); expect(getMessageTimestamp(iso, 'not a locale!!')).not.toBeNull(); }); + + /** Every message row formats its timestamp on each render; constructing the + * Intl formatters per call made them the costliest function of a send. */ + it('reuses its formatters across calls for the same locale and clock', () => { + const { DateTimeFormat, RelativeTimeFormat } = Intl; + const replaceIntl = (key: 'DateTimeFormat' | 'RelativeTimeFormat', value: unknown) => + Object.defineProperty(Intl, key, { value, configurable: true, writable: true }); + const constructed = { absolute: 0, relative: 0 }; + replaceIntl( + 'DateTimeFormat', + class extends DateTimeFormat { + constructor(...args: ConstructorParameters) { + super(...args); + constructed.absolute += 1; + } + }, + ); + replaceIntl( + 'RelativeTimeFormat', + class extends RelativeTimeFormat { + constructor(...args: ConstructorParameters) { + super(...args); + constructed.relative += 1; + } + }, + ); + try { + const iso = new Date(NOW - 5 * 60 * 1000).toISOString(); + const first = getMessageTimestamp(iso, 'fr-CA', true); + const second = getMessageTimestamp(iso, 'fr-CA', true); + getMessageTimestamp(iso, 'fr-CA', true); + + expect(second).toEqual(first); + expect(constructed).toEqual({ absolute: 1, relative: 1 }); + + getMessageTimestamp(iso, 'fr-CA', false); + expect(constructed).toEqual({ absolute: 2, relative: 1 }); + } finally { + replaceIntl('DateTimeFormat', DateTimeFormat); + replaceIntl('RelativeTimeFormat', RelativeTimeFormat); + } + }); + + it('keeps a formatter per clock format', () => { + const iso = new Date(2026, 5, 12, 15, 5).toISOString(); + + expect(getMessageTimestamp(iso, 'en-US', true)?.absolute).toMatch(/3:05\sPM/); + expect(getMessageTimestamp(iso, 'en-US', false)?.absolute).toMatch(/15:05/); + }); }); const noop = () => {}; @@ -411,6 +461,42 @@ describe('areMessageRowPropsEqual', () => { }); }); +describe('isSameTailRelation', () => { + const tailProps = ( + messageId: string, + depth: number, + latestMessageId: string | undefined, + latestMessageDepth: number | undefined, + ) => ({ + message: makeMessage({ messageId, depth }), + latestMessageId, + latestMessageDepth, + }); + + it('ignores a tail move that neither reaches nor leaves the row', () => { + expect(isSameTailRelation(tailProps('a', 1, 'b', 3), tailProps('a', 1, 'c_', 5))).toBe(true); + expect(isSameTailRelation(tailProps('a', 1, 'c_', 5), tailProps('a', 1, 'c', 5))).toBe(true); + }); + + it('re-renders the row the tail leaves', () => { + expect(isSameTailRelation(tailProps('b', 3, 'b', 3), tailProps('b', 3, 'c_', 5))).toBe(false); + }); + + it('re-renders the row the tail reaches', () => { + expect(isSameTailRelation(tailProps('c', 5, 'c_', 5), tailProps('c', 5, 'c', 5))).toBe(false); + }); + + it('re-renders when the tail depth reaches or leaves the row depth', () => { + expect(isSameTailRelation(tailProps('x', 3, 'b', 3), tailProps('x', 3, 'c', 5))).toBe(false); + }); + + it('re-renders when the tail becomes known or unknown', () => { + expect(isSameTailRelation(tailProps('a', 1, 'b', 3), tailProps('a', 1, undefined, 3))).toBe( + false, + ); + }); +}); + describe('isSubmittableMessage', () => { it('accepts non-whitespace text without files', () => { expect(isSubmittableMessage('Hello')).toBe(true); diff --git a/client/src/utils/messages.ts b/client/src/utils/messages.ts index 02e05d5f74..1547e986db 100644 --- a/client/src/utils/messages.ts +++ b/client/src/utils/messages.ts @@ -667,21 +667,59 @@ const RELATIVE_TIME_DIVISIONS: { amount: number; unit: Intl.RelativeTimeFormatUn { amount: Number.POSITIVE_INFINITY, unit: 'year' }, ]; +/** + * Intl formatters are expensive to construct and every message row formats its + * timestamp on each render, so they are built once per locale (and clock format). + */ +const resolvedLocales = new Map(); +const relativeFormatters = new Map(); +const absoluteFormatters = new Map(); + /** Returns the locale only when it is a syntactically valid BCP-47 tag, else undefined. */ const resolveLocale = (locale?: string): string | undefined => { if (!locale) { return undefined; } + if (resolvedLocales.has(locale)) { + return resolvedLocales.get(locale); + } + let resolved: string | undefined; try { Intl.DateTimeFormat.supportedLocalesOf(locale); - return locale; + resolved = locale; } catch { - return undefined; + resolved = undefined; } + resolvedLocales.set(locale, resolved); + return resolved; +}; + +const getRelativeFormatter = (locale?: string): Intl.RelativeTimeFormat => { + const key = locale ?? ''; + let formatter = relativeFormatters.get(key); + if (!formatter) { + formatter = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' }); + relativeFormatters.set(key, formatter); + } + return formatter; +}; + +const getAbsoluteFormatter = (locale?: string, hour12?: boolean): Intl.DateTimeFormat => { + const key = `${locale ?? ''}|${String(hour12)}`; + let formatter = absoluteFormatters.get(key); + if (!formatter) { + formatter = new Intl.DateTimeFormat(locale, { + dateStyle: 'medium', + timeStyle: 'short', + hour12, + }); + absoluteFormatters.set(key, formatter); + } + return formatter; }; const formatRelativeTime = (from: Date, to: Date, locale?: string): string => { - const formatter = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' }); + const formatter = getRelativeFormatter(locale); let duration = (from.getTime() - to.getTime()) / 1000; for (const division of RELATIVE_TIME_DIVISIONS) { if (Math.abs(duration) < division.amount) { @@ -713,11 +751,7 @@ export const getMessageTimestamp = ( return { iso: date.toISOString(), relative: formatRelativeTime(date, now, safeLocale), - absolute: new Intl.DateTimeFormat(safeLocale, { - dateStyle: 'medium', - timeStyle: 'short', - hour12, - }).format(date), + absolute: getAbsoluteFormatter(safeLocale, hour12).format(date), isRecent: Math.abs(now.getTime() - date.getTime()) < RECENT_THRESHOLD_MS, }; }; @@ -874,6 +908,28 @@ export function areMessageFieldsEqual( ); } +type TailRelationProps = { + message?: TMessage | null; + latestMessageId?: string; + latestMessageDepth?: number; +}; + +/** + * True when moving the thread's tail leaves a row's rendering unchanged. Rows read + * `latestMessageId` and `latestMessageDepth` only by comparing them to their own id + * and depth, and for whether a tail is known at all, so a submission or a server id + * hydration re-renders just the rows entering or leaving the tail. + */ +export function isSameTailRelation(prev: TailRelationProps, next: TailRelationProps): boolean { + return ( + (prev.latestMessageId == null) === (next.latestMessageId == null) && + (prev.latestMessageId === prev.message?.messageId) === + (next.latestMessageId === next.message?.messageId) && + (prev.latestMessageDepth === prev.message?.depth) === + (next.latestMessageDepth === next.message?.depth) + ); +} + /** * Comparator for the memoized message-row wrappers (Message / MessageContent / * MessageParts): identity-compare the scalar props, field-compare the message. diff --git a/e2e/benchmarks-tree/submit-lag.perf.spec.ts b/e2e/benchmarks-tree/submit-lag.perf.spec.ts new file mode 100644 index 0000000000..65523a9dae --- /dev/null +++ b/e2e/benchmarks-tree/submit-lag.perf.spec.ts @@ -0,0 +1,493 @@ +import fs from 'node:fs'; +import { randomUUID } from 'node:crypto'; +import { expect, test } from '@playwright/test'; +import type { Page } from '@playwright/test'; +import { deleteConversations, deleteMessagesByConversation, withMongo } from '../specs/mock/db'; +import { MOCK_ENDPOINTS, messagesView } from '../specs/mock/helpers'; +import { buildTreeMessages, TURNS, turnHeading } from './payload'; +import { getE2EUser } from '../setup/user'; + +/** + * Submit-lag benchmark: what the page does between the Enter press on a long + * thread and the new turn painting, and while the reply streams in. + * + * Dev build (component names, per-commit render reasons): + * npx playwright test -c e2e/playwright.config.tree-perf.ts submit-lag + * Built client (production timings; names are minified): + * npx playwright test -c e2e/playwright.config.tree-perf-prod.ts submit-lag + * + * `TREE_PERF_TURNS` sizes the synthetic thread. `SUBMIT_LAG_SOURCE=` + * seeds a real exported conversation (`{ messages }`) instead, re-owned by the e2e + * user and pointed at the mock endpoint. `SUBMIT_LAG_FLAT=false` measures the + * recursive renderer. The numbers are printed; nothing is asserted. + */ + +const userEmail = getE2EUser().email; +const ENDPOINT = MOCK_ENDPOINTS[0]; +const SOURCE = process.env.SUBMIT_LAG_SOURCE; +const FLAT = process.env.SUBMIT_LAG_FLAT !== 'false'; +const CONVO = { id: randomUUID(), title: 'Submit lag bench' }; +const PROFILE_MS = Number(process.env.SUBMIT_LAG_PROFILE_MS) || 1500; + +test.use({ trace: 'off', video: 'off', screenshot: 'off' }); + +type RawMessage = Record & { messageId: string; parentMessageId: string }; + +/** Storage bookkeeping an exported document carries that the seed must not copy. */ +const STORAGE_KEYS = new Set(['_id', '_meiliIndex', '_meiliCleanupVersion', 'expiredAt']); + +function sourceMessages(): { messages: RawMessage[]; lastText: string } { + if (!SOURCE) { + const messages = buildTreeMessages('L') as unknown as RawMessage[]; + return { messages, lastText: turnHeading('L', TURNS) }; + } + const exported = JSON.parse(fs.readFileSync(SOURCE, 'utf8')) as { messages: RawMessage[] }; + return { messages: exported.messages, lastText: '' }; +} + +async function seed(): Promise<{ count: number; lastText: string }> { + const { messages, lastText } = sourceMessages(); + await withMongo(async (db) => { + const user = await db.collection('users').findOne({ email: userEmail }); + if (!user) { + throw new Error(`seed: user "${userEmail}" not found`); + } + const userId = user._id.toString(); + const now = new Date(); + await db.collection('conversations').insertOne({ + conversationId: CONVO.id, + title: CONVO.title, + user: userId, + endpoint: ENDPOINT.label, + endpointType: 'custom', + model: ENDPOINT.model, + isArchived: false, + createdAt: now, + updatedAt: now, + __v: 0, + }); + const start = Date.now() - messages.length * 2000; + const docs = messages.map((message, index) => { + const fields = Object.entries(message).filter(([key]) => !STORAGE_KEYS.has(key)); + return { + ...Object.fromEntries(fields), + conversationId: CONVO.id, + user: userId, + endpoint: ENDPOINT.label, + model: ENDPOINT.model, + error: false, + unfinished: false, + createdAt: new Date(start + index * 1000), + updatedAt: new Date(start + index * 1000), + __v: 0, + }; + }); + await db.collection('messages').insertMany(docs); + }); + return { count: messages.length, lastText }; +} + +/** + * Page-side probe. A devtools-hook stub counts, per commit, which function + * components actually rendered (DevTools' own `PerformedWork` + child-pointer + * rule) and their self render time; the walk only runs while `tally` is on so + * the timing pass carries no instrumentation cost. + */ +const PROBE = `(() => { + const lag = { + tally: false, + t0: null, + marker: null, + userRowAt: null, + commits: [], + longTasks: [], + events: [], + reset(marker) { + this.t0 = null; + this.marker = marker; + this.userRowAt = null; + this.commits = []; + this.longTasks = []; + this.events = []; + }, + }; + window.__LAG__ = lag; + window.addEventListener('keydown', (event) => { + if (event.key === 'Enter' && lag.marker && lag.t0 == null) { + lag.t0 = performance.now(); + } + }, true); + new MutationObserver((records) => { + if (!lag.marker || lag.userRowAt != null || lag.t0 == null) return; + for (const record of records) { + for (const node of record.addedNodes) { + if (node.nodeType === 1 && !node.closest('form') && (node.textContent || '').includes(lag.marker)) { + lag.userRowAt = performance.now(); + return; + } + } + } + }).observe(document, { childList: true, subtree: true }); + try { + new PerformanceObserver((list) => { + for (const entry of list.getEntries()) lag.longTasks.push({ start: entry.startTime, duration: entry.duration }); + }).observe({ type: 'longtask', buffered: true }); + new PerformanceObserver((list) => { + for (const entry of list.getEntries()) { + lag.events.push({ name: entry.name, start: entry.startTime, duration: entry.duration, + processing: entry.processingEnd - entry.processingStart, delay: entry.processingStart - entry.startTime }); + } + }).observe({ type: 'event', durationThreshold: 16, buffered: true }); + } catch (_) {} + const COMPONENT_TAGS = new Set([0, 1, 11, 14, 15]); + const nameOf = (fiber) => { + let type = fiber.type; + for (let depth = 0; depth < 4 && type; depth += 1) { + if (typeof type === 'function') return type.displayName || type.name || 'anonymous'; + if (typeof type === 'object') { + if (type.displayName) return type.displayName; + type = type.type || type.render; + continue; + } + return String(type); + } + return 'anonymous'; + }; + const record = (stats, fiber, mounted) => { + const name = nameOf(fiber); + let slot = stats[name]; + if (!slot) { slot = { renders: 0, mounts: 0, time: 0 }; stats[name] = slot; } + if (mounted) slot.mounts += 1; else slot.renders += 1; + slot.time += fiber.selfBaseDuration || 0; + }; + const WATCH = new Set(['MessageContent', 'ContentRender', 'HoverButtons', 'MessageRow', 'Fork', + 'MessageTimestamp', 'ChatView', 'ChatForm', 'MessagesViewContent', 'List', 'Row', 'Message', + 'MessageRender', 'ContentParts', 'SiblingSwitch', 'SidebarChatProvider', 'MessageNav']); + const isPlain = (value) => value != null && typeof value === 'object' && !Array.isArray(value) && !value.$$typeof; + const reasonsOf = (fiber) => { + const prev = fiber.alternate; + const reasons = []; + const a = prev.memoizedProps || {}; + const b = fiber.memoizedProps || {}; + for (const key of new Set([...Object.keys(a), ...Object.keys(b)])) { + if (key === 'children' || a[key] === b[key]) continue; + if (isPlain(a[key]) && isPlain(b[key])) { + const inner = []; + for (const sub of new Set([...Object.keys(a[key]), ...Object.keys(b[key])])) { + let av; let bv; + try { av = a[key][sub]; bv = b[key][sub]; } catch (_) { continue; } + if (av !== bv) inner.push(sub); + } + reasons.push('prop:' + key + (inner.length ? '{' + inner.slice(0, 6).join(',') + '}' : '{same-fields}')); + } else { + reasons.push('prop:' + key); + } + } + let dep = fiber.dependencies && fiber.dependencies.firstContext; + let prevDep = prev.dependencies && prev.dependencies.firstContext; + while (dep) { + if (!prevDep || dep.memoizedValue !== prevDep.memoizedValue) { + const ctx = dep.context; + const name = ctx.displayName || (ctx.Provider && ctx.Provider.displayName) || 'Context'; + let inner = ''; + if (prevDep && isPlain(dep.memoizedValue) && isPlain(prevDep.memoizedValue)) { + const changed = []; + for (const sub of Object.keys(dep.memoizedValue)) { + if (dep.memoizedValue[sub] !== prevDep.memoizedValue[sub]) changed.push(sub); + } + inner = '{' + changed.slice(0, 8).join(',') + '}'; + } + reasons.push('ctx:' + name + inner); + } + dep = dep.next; + prevDep = prevDep && prevDep.next; + } + let hook = fiber.memoizedState; + let prevHook = prev.memoizedState; + let index = 0; + while (hook && prevHook && fiber.tag !== 1) { + if (hook.queue && hook.memoizedState !== prevHook.memoizedState) { + const repr = (value) => { + if (Array.isArray(value)) return 'arr' + value.length + (value.length ? ':' + repr(value[0]) : ''); + if (value && typeof value === 'object') return 'obj{' + Object.keys(value).slice(0, 3).join(',') + '}'; + return String(value).slice(0, 24); + }; + reasons.push('state#' + index + '(' + repr(prevHook.memoizedState) + '>' + repr(hook.memoizedState) + ')'); + } + hook = hook.next; prevHook = prevHook.next; index += 1; + } + if (!reasons.length) reasons.push('parent'); + return reasons; + }; + const walk = (rootFiber) => { + const stats = Object.create(null); + const why = Object.create(null); + const stack = [[rootFiber, false]]; + while (stack.length) { + const [fiber, mounting] = stack.pop(); + const prev = fiber.alternate; + const mounted = mounting || prev == null; + if (COMPONENT_TAGS.has(fiber.tag) && (mounted || (fiber.flags & 1) === 1)) { + record(stats, fiber, mounted); + const name = nameOf(fiber); + if (!mounted && WATCH.has(name)) { + const bucket = (why[name] ??= Object.create(null)); + for (const reason of reasonsOf(fiber)) bucket[reason] = (bucket[reason] || 0) + 1; + } + } + if (mounted || fiber.child !== prev.child) { + for (let child = fiber.child; child; child = child.sibling) stack.push([child, mounted]); + } + } + return { stats, why }; + }; + let nextId = 1; + window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = { + renderers: new Map(), + supportsFiber: true, + inject(renderer) { const id = nextId++; this.renderers.set(id, renderer); return id; }, + onScheduleFiberRoot() {}, + onCommitFiberUnmount() {}, + onPostCommitFiberRoot() {}, + checkDCE() {}, + onCommitFiberRoot(_id, root) { + if (!lag.tally || lag.t0 == null) return; + const at = performance.now(); + const { stats, why } = walk(root.current); + lag.commits.push({ at, walkMs: performance.now() - at, actual: root.current.actualDuration || 0, stats, why }); + }, + }; +})();`; + +type Slot = { renders: number; mounts: number; time: number }; +type Commit = { + at: number; + walkMs: number; + actual: number; + stats: Record; + why: Record>; +}; +type Probe = { + t0: number | null; + userRowAt: number | null; + commits: Commit[]; + longTasks: Array<{ start: number; duration: number }>; + events: Array<{ + name: string; + start: number; + duration: number; + processing: number; + delay: number; + }>; +}; + +declare global { + interface Window { + __LAG__: Probe & { tally: boolean; reset(marker: string): void }; + } +} + +type ProfileNode = { + id: number; + callFrame: { functionName: string; url: string; lineNumber: number }; + children?: number[]; +}; + +/** Self and inclusive CPU time per function from a CDP profile. */ +function summarizeProfile(profile: { + nodes: ProfileNode[]; + samples: number[]; + timeDeltas: number[]; + startTime: number; +}) { + const parent = new Map(); + const byId = new Map(); + for (const node of profile.nodes) { + byId.set(node.id, node); + for (const child of node.children ?? []) { + parent.set(child, node.id); + } + } + const keyOf = (node: ProfileNode) => { + const file = node.callFrame.url.split('/').pop()?.split('?')[0] ?? ''; + return `${node.callFrame.functionName || '(anon)'} ${file}:${node.callFrame.lineNumber + 1}`; + }; + const self = new Map(); + const inclusive = new Map(); + for (let i = 0; i < profile.samples.length; i++) { + const dt = (profile.timeDeltas[i + 1] ?? 0) / 1000; + const leaf = byId.get(profile.samples[i]); + if (!leaf) { + continue; + } + self.set(keyOf(leaf), (self.get(keyOf(leaf)) ?? 0) + dt); + const seen = new Set(); + for (let id: number | undefined = leaf.id; id != null; id = parent.get(id)) { + const node = byId.get(id); + if (!node) break; + const key = keyOf(node); + if (seen.has(key)) continue; + seen.add(key); + inclusive.set(key, (inclusive.get(key) ?? 0) + dt); + } + } + const top = (map: Map, n: number) => + [...map.entries()] + .filter(([key]) => !/^\((program|idle|root|garbage collector)\)/.test(key)) + .sort((a, b) => b[1] - a[1]) + .slice(0, n); + return { self: top(self, 30), inclusive: top(inclusive, 60) }; +} + +async function submit(page: Page, marker: string, beforeEnter?: () => Promise) { + const input = page.getByRole('textbox', { name: 'Message input' }); + await input.click(); + await input.fill(`E2E_SLOW_REPLY:${marker}`); + await page.evaluate((m) => window.__LAG__.reset(m), marker); + await page.waitForTimeout(300); + await beforeEnter?.(); + await input.press('Enter'); +} + +async function waitForTurnEnd(page: Page) { + await expect(page.getByRole('button', { name: 'Stop generating' })).toBeVisible({ + timeout: 30_000, + }); + await expect(page.getByRole('button', { name: 'Stop generating' })).toBeHidden({ + timeout: 120_000, + }); + await page.waitForTimeout(1500); +} + +function reportTimeline(label: string, probe: Probe) { + const t0 = probe.t0 ?? 0; + console.log(`\n=== ${label} ===`); + console.log( + `enter -> user row in DOM: ${probe.userRowAt ? (probe.userRowAt - t0).toFixed(0) : 'n/a'}ms`, + ); + const events = probe.events.filter((e) => e.start >= t0 - 50); + for (const e of events.slice(0, 8)) { + console.log( + ` event ${e.name.padEnd(10)} +${(e.start - t0).toFixed(0)}ms duration=${e.duration.toFixed(0)} delay=${e.delay.toFixed(0)} processing=${e.processing.toFixed(0)}`, + ); + } + const tasks = probe.longTasks.filter((task) => task.start >= t0 - 50); + const total = tasks.reduce((sum, task) => sum + task.duration, 0); + console.log(`long tasks after enter: n=${tasks.length} total=${total.toFixed(0)}ms`); + for (const task of tasks.slice(0, 40)) { + console.log(` +${(task.start - t0).toFixed(0).padStart(6)}ms ${task.duration.toFixed(0)}ms`); + } +} + +function reportCommits(label: string, probe: Probe, limit: number) { + const t0 = probe.t0 ?? 0; + console.log(`\n=== ${label}: ${probe.commits.length} commits ===`); + const totals: Record = Object.create(null); + for (const [index, commit] of probe.commits.entries()) { + let renders = 0; + let mounts = 0; + let time = 0; + for (const [name, slot] of Object.entries(commit.stats)) { + renders += slot.renders; + mounts += slot.mounts; + time += slot.time; + const total = (totals[name] ??= { renders: 0, mounts: 0, time: 0 }); + total.renders += slot.renders; + total.mounts += slot.mounts; + total.time += slot.time; + } + if (index < limit) { + const top = Object.entries(commit.stats) + .sort((a, b) => b[1].time - a[1].time) + .slice(0, 6) + .map( + ([name, slot]) => `${name}(${slot.renders}r/${slot.mounts}m ${slot.time.toFixed(1)}ms)`, + ) + .join(' '); + console.log( + ` #${index} +${(commit.at - t0).toFixed(0)}ms renders=${renders} mounts=${mounts} self=${time.toFixed(0)}ms actual=${commit.actual.toFixed(0)}ms :: ${top}`, + ); + if (renders > 300) { + for (const [name, reasons] of Object.entries(commit.why)) { + const line = Object.entries(reasons) + .sort((a, b) => b[1] - a[1]) + .slice(0, 6) + .map(([reason, n]) => `${reason}x${n}`) + .join(' '); + console.log(` why ${name.padEnd(20)} ${line}`); + } + } + } + } + console.log(' totals by self time:'); + for (const [name, slot] of Object.entries(totals) + .sort((a, b) => b[1].time - a[1].time) + .slice(0, 40)) { + console.log( + ` ${name.padEnd(36)} renders=${String(slot.renders).padStart(6)} mounts=${String(slot.mounts).padStart(6)} self=${slot.time.toFixed(1)}ms`, + ); + } +} + +test('submit on a long thread', async ({ page }) => { + test.setTimeout(10 * 60 * 1000); + const { count, lastText } = await seed(); + console.log(`seeded ${count} messages (${SOURCE ?? 'synthetic'}), flat=${FLAT}`); + await page.addInitScript((flat: boolean) => { + localStorage.setItem('textToSpeech', 'false'); + localStorage.setItem('LC_FLAT_THREAD', flat ? 'true' : 'false'); + }, FLAT); + await page.addInitScript({ content: PROBE }); + await page.goto(`/c/${CONVO.id}`, { timeout: 180_000 }); + if (lastText) { + await expect( + messagesView(page).getByRole('heading', { name: lastText, exact: true }).first(), + ).toBeAttached({ timeout: 120_000 }); + } + await expect(page.getByRole('textbox', { name: 'Message input' })).toBeVisible({ + timeout: 60_000, + }); + await page.waitForTimeout(4000); + const nodes = await page.evaluate(() => document.getElementsByTagName('*').length); + console.log(`DOM elements after load: ${nodes}`); + + const session = await page.context().newCDPSession(page); + await session.send('Profiler.enable'); + await session.send('Profiler.setSamplingInterval', { interval: 100 }); + await submit(page, `lag-timing-${Date.now()}`, async () => { + await session.send('Profiler.start'); + }); + await page.waitForTimeout(PROFILE_MS); + const { profile } = await session.send('Profiler.stop'); + const timing = await page.evaluate(() => window.__LAG__); + reportTimeline('timing pass (no fiber walk)', timing); + const summary = summarizeProfile(profile as unknown as Parameters[0]); + console.log(`\n--- CPU self time (first ${PROFILE_MS}ms after Enter) ---`); + for (const [key, ms] of summary.self) console.log(` ${ms.toFixed(1).padStart(8)}ms ${key}`); + console.log(`--- CPU inclusive time (first ${PROFILE_MS}ms after Enter) ---`); + for (const [key, ms] of summary.inclusive) + console.log(` ${ms.toFixed(1).padStart(8)}ms ${key}`); + await waitForTurnEnd(page); + + await page.evaluate(() => { + window.__LAG__.tally = true; + }); + await submit(page, `lag-tally-${Date.now()}`); + await expect(page.getByRole('button', { name: 'Stop generating' })).toBeVisible({ + timeout: 30_000, + }); + await page.waitForTimeout(800); + const early = await page.evaluate(() => { + const snapshot = window.__LAG__; + return { ...snapshot, commits: snapshot.commits.slice() }; + }); + reportCommits('submit commits (first 800ms after Stop visible)', early, 40); + await waitForTurnEnd(page); + const full = await page.evaluate(() => window.__LAG__); + reportCommits('whole turn (submit + stream + finalize)', full, 0); +}); + +test.afterAll(async () => { + await deleteMessagesByConversation([CONVO.id]); + await deleteConversations([CONVO.id]); +});