Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🧶 feat: Flatten Active Message Threads Behind a Flag (#15633)
Danny Avila422f3a6f8d23
diff --git a/.gitignore b/.gitignoreindex 2920b9b838..3af5dc3ca2 100644--- a/.gitignore+++ b/.gitignore@@ -94,6 +94,7 @@ archivesrc/style - official.css/e2e/specs/.test-results//e2e/benchmarks/.test-results/+/e2e/benchmarks-tree/.test-results//e2e/.generated//e2e/playwright-report//e2e/playwright-report-bombadil/diff --git a/client/src/components/Chat/ChatView.tsx b/client/src/components/Chat/ChatView.tsxindex c6316d2ae1..c0bceec1b8 100644--- a/client/src/components/Chat/ChatView.tsx+++ b/client/src/components/Chat/ChatView.tsx@@ -1,10 +1,10 @@-import { memo, useCallback } from 'react';+import { memo, useMemo } from 'react';import { useRecoilValue } from 'recoil';import { useForm } from 'react-hook-form';import { Spinner } from '@librechat/client';import { useParams } from 'react-router-dom';import { Constants, buildTree } from 'librechat-data-provider';-import type { TChatProject, TMessage } from 'librechat-data-provider';+import type { TChatProject } from 'librechat-data-provider';import type { ChatFormValues } from '~/common';import {useAddedResponse,@@ -50,19 +50,12 @@ function ChatView({ index = 0, project }: { index?: number; project?: TChatProjeconst fileMap = useFileMapContext();const {- data: messagesTree = null,+ data: messages = null,isLoading,isFetching,} = useGetMessagesByConvoId(conversationId ?? '',{- select: useCallback(- (data: TMessage[]) => {- const dataTree = buildTree({ messages: data, fileMap });- return dataTree?.length === 0 ? null : (dataTree ?? null);- },- [fileMap],- ),enabled: !!conversationId && conversationId !== Constants.SEARCH,/** Refetch stale caches on mount: navigation invalidates (not removes)* messages now, so a warm conversation renders instantly from cache and@@ -71,6 +64,10 @@ function ChatView({ index = 0, project }: { index?: number; project?: TChatProje},{ isStreaming: isSubmitting },);+ const messagesTree = useMemo(() => {+ const dataTree = buildTree({ messages, fileMap });+ return dataTree?.length === 0 ? null : (dataTree ?? null);+ }, [messages, fileMap]);const chatHelpers = useChatHelpers(index, conversationId);const addedChatHelpers = useAddedResponse();@@ -104,7 +101,7 @@ function ChatView({ index = 0, project }: { index?: number; project?: TChatProje} else if ((isLoading || isNavigating) && !isLandingPage) {content = <LoadingSpinner />;} else if (!isLandingPage) {- content = <MessagesView messagesTree={messagesTree} />;+ content = <MessagesView messagesTree={messagesTree} messages={messages} />;} else {content = <Landing centerFormOnLanding={centerFormOnLanding} />;}diff --git a/client/src/components/Chat/Messages/MessagesView.tsx b/client/src/components/Chat/Messages/MessagesView.tsxindex 8166a5d9ce..8b6269f754 100644--- a/client/src/components/Chat/Messages/MessagesView.tsx+++ b/client/src/components/Chat/Messages/MessagesView.tsx@@ -5,11 +5,13 @@ import { Constants } from 'librechat-data-provider';import { CSSTransition } from 'react-transition-group';import type { TMessage } from 'librechat-data-provider';import { useScreenshot, useMessageScrolling, useScrollbarGutter, useLocalize } from '~/hooks';+import { MessagesViewProvider, useChatContext, useFileMapContext } from '~/Providers';import { RowMountProvider, useProgressiveRowMount } from '~/hooks/Messages';-import { MessagesViewProvider, useChatContext } from '~/Providers';import ScrollToBottom from '~/components/Messages/ScrollToBottom';+import useThreadRows from '~/hooks/Messages/useThreadRows';import { steerOverlayHeightFamily } from '~/store/steer';import { autoScrollAtom } from '~/store/autoScroll';+import { FLAT_THREAD, ThreadList } from './Thread';import { fontSizeAtom } from '~/store/fontSize';import MultiMessage from './MultiMessage';import MessageNav from './MessageNav';@@ -94,8 +96,10 @@ const ScrollButton = memo(function ScrollButton({function MessagesViewContent({messagesTree: _messagesTree,+ messages,}: {messagesTree?: TMessage[] | null;+ messages?: TMessage[] | null;}) {const localize = useLocalize();const fontSize = useAtomValue(fontSizeAtom);@@ -115,6 +119,8 @@ function MessagesViewContent({useScrollbarGutter(scrollableRef);const { conversationId } = conversation ?? {};+ const fileMap = useFileMapContext();+ const threadRows = useThreadRows(FLAT_THREAD ? messages : null, conversationId, fileMap);const { index, latestMessageDepth } = useChatContext();const isSubmitting = useRecoilValue(store.isSubmittingFamily(index));@@ -179,12 +185,20 @@ function MessagesViewContent({<><div ref={screenshotTargetRef} data-testid="screenshot-target"><RowMountProvider mountWindow={mountWindow}>- <MultiMessage- messagesTree={_messagesTree}- messageId={conversationId ?? null}- setCurrentEditId={setCurrentEditId}- currentEditId={currentEditId ?? null}- />+ {FLAT_THREAD && threadRows ? (+ <ThreadList+ rows={threadRows}+ setCurrentEditId={setCurrentEditId}+ currentEditId={currentEditId ?? null}+ />+ ) : (+ <MultiMessage+ messagesTree={_messagesTree}+ messageId={conversationId ?? null}+ setCurrentEditId={setCurrentEditId}+ currentEditId={currentEditId ?? null}+ />+ )}</RowMountProvider></div></>@@ -212,10 +226,16 @@ function MessagesViewContent({);}-export default function MessagesView({ messagesTree }: { messagesTree?: TMessage[] | null }) {+export default function MessagesView({+ messagesTree,+ messages,+}: {+ messagesTree?: TMessage[] | null;+ messages?: TMessage[] | null;+}) {return (<MessagesViewProvider>- <MessagesViewContent messagesTree={messagesTree} />+ <MessagesViewContent messagesTree={messagesTree} messages={messages} /></MessagesViewProvider>);}diff --git a/client/src/components/Chat/Messages/MultiMessage.tsx b/client/src/components/Chat/Messages/MultiMessage.tsxindex 5a2d8aa67f..cbf9b2e3f8 100644--- a/client/src/components/Chat/Messages/MultiMessage.tsx+++ b/client/src/components/Chat/Messages/MultiMessage.tsx@@ -1,16 +1,16 @@import { memo, useEffect, useRef, useCallback } from 'react';-import { useRecoilState } from 'recoil';+import { useAtom } from 'jotai';import { isAssistantsEndpoint } from 'librechat-data-provider';import type { TMessage } from 'librechat-data-provider';import type { ReactElement } from 'react';import type { TMessageProps } from '~/common';import EventSubagentActivityGroup from '~/components/Chat/Subagents/EventSubagentActivityGroup';import MessageContent from '~/components/Messages/MessageContent';+import { siblingIdxFamily, siblingKey } from './Thread/state';import { useRowMountWindow } from '~/hooks/Messages';import { hasParallelLanes } from '~/utils/lanes';import MessageParts from './MessageParts';import Message from './Message';-import store from '~/store';/** First-run sentinel for `parentRef`: `messageId` itself may legitimately be* null/undefined at the root level, so those can't mark "not yet bound". */@@ -23,7 +23,7 @@ function MultiMessage({currentEditId,setCurrentEditId,}: TMessageProps) {- const [siblingIdx, setSiblingIdx] = useRecoilState(store.messagesSiblingIdxFamily(messageId));+ const [siblingIdx, setSiblingIdx] = useAtom(siblingIdxFamily(siblingKey(messageId)));const mountWindow = useRowMountWindow();const setSiblingIdxRev = useCallback(diff --git a/client/src/components/Chat/Messages/Thread/List.tsx b/client/src/components/Chat/Messages/Thread/List.tsxnew file mode 100644index 0000000000..c3f3032992--- /dev/null+++ b/client/src/components/Chat/Messages/Thread/List.tsx@@ -0,0 +1,37 @@+import { memo } from 'react';+import type { ThreadRow } from '~/utils/thread';+import type { TMessageProps } from '~/common';+import { useRowMountWindow } from '~/hooks/Messages';+import Row from './Row';++type ListProps = {+ rows: ThreadRow[];+ currentEditId: TMessageProps['currentEditId'];+ setCurrentEditId: TMessageProps['setCurrentEditId'];+};++/**+ * Flat thread renderer. Rows are keyed by position, exactly the reconciliation+ * the recursive renderer relied on: message ids change across the streaming+ * lifecycle, while a row's position does not, so the streaming row keeps its+ * component instance and a sibling switch reuses the instances below it.+ */+function List({ rows, currentEditId, setCurrentEditId }: ListProps) {+ const mountWindow = useRowMountWindow();+ return (+ <>+ {rows.map((row) =>+ mountWindow == null || (row.depth >= mountWindow.start && row.depth <= mountWindow.end) ? (+ <Row+ key={row.depth}+ row={row}+ currentEditId={currentEditId}+ setCurrentEditId={setCurrentEditId}+ />+ ) : null,+ )}+ </>+ );+}++export default memo(List);diff --git a/client/src/components/Chat/Messages/Thread/Row.tsx b/client/src/components/Chat/Messages/Thread/Row.tsxnew file mode 100644index 0000000000..7edb9568b1--- /dev/null+++ b/client/src/components/Chat/Messages/Thread/Row.tsx@@ -0,0 +1,76 @@+import { memo, useCallback } from 'react';+import { useSetAtom } from 'jotai';+import { isAssistantsEndpoint } from 'librechat-data-provider';+import type { ThreadRow } from '~/utils/thread';+import type { TMessageProps } from '~/common';+import EventSubagentActivityGroup from '~/components/Chat/Subagents/EventSubagentActivityGroup';+import MessageContent from '~/components/Messages/MessageContent';+import { hasParallelLanes } from '~/utils/lanes';+import MessageParts from '../MessageParts';+import { siblingIdxFamily } from './state';+import Message from '../Message';++type RowProps = {+ row: ThreadRow;+ currentEditId: TMessageProps['currentEditId'];+ setCurrentEditId: TMessageProps['setCurrentEditId'];+};++/** One visible row of the flat thread: the message plus its activity group. */+function Row({ row, currentEditId, setCurrentEditId }: RowProps) {+ const { message, siblingIdx, siblingCount, parentKey } = row;+ const setSiblingIdx = useSetAtom(siblingIdxFamily(parentKey));+ const setSiblingIdxRev = useCallback(+ (value: number) => {+ setSiblingIdx(siblingCount - value - 1);+ },+ [siblingCount, setSiblingIdx],+ );++ const sharedProps = {+ message,+ currentEditId,+ setCurrentEditId,+ siblingIdx: siblingCount - siblingIdx - 1,+ siblingCount,+ setSiblingIdx: setSiblingIdxRev,+ };++ let content: JSX.Element;+ if (isAssistantsEndpoint(message.endpoint) && message.content) {+ content = <MessageParts {...sharedProps} />;+ } else if (message.content) {+ content = <MessageContent {...sharedProps} />;+ } else {+ content = <Message {...sharedProps} />;+ }++ let activityParentMessageIds: string[] = [];+ if (message.isCreatedByUser) {+ if (row.childCount === 0) activityParentMessageIds = [message.messageId];+ } else {+ activityParentMessageIds = [message.messageId, message.parentMessageId].filter(+ (id): id is string => typeof id === 'string' && id.length > 0,+ );+ }+ const isEditingActivityAnchor =+ typeof currentEditId === 'string' && activityParentMessageIds.includes(currentEditId);+ const hasParallelContent = !message.isCreatedByUser && hasParallelLanes(message.content);++ return (+ <>+ {content}+ {!isEditingActivityAnchor && activityParentMessageIds.length > 0 ? (+ <div className="w-full border-0 bg-transparent">+ <EventSubagentActivityGroup+ conversationId={message.conversationId ?? ''}+ parentMessageIds={activityParentMessageIds}+ hasParallelContent={hasParallelContent}+ />+ </div>+ ) : null}+ </>+ );+}++export default memo(Row);diff --git a/client/src/components/Chat/Messages/Thread/flag.ts b/client/src/components/Chat/Messages/Thread/flag.tsnew file mode 100644index 0000000000..bce002d6ff--- /dev/null+++ b/client/src/components/Chat/Messages/Thread/flag.ts@@ -0,0 +1,15 @@+/**+ * Prototype switch for the flat thread renderer, read once per page load.+ * `LC_FLAT_THREAD` in localStorage wins; `VITE_FLAT_THREAD` sets the default.+ */+function readFlag(): boolean {+ const fallback = import.meta.env.VITE_FLAT_THREAD === 'true';+ try {+ const stored = localStorage.getItem('LC_FLAT_THREAD');+ return stored == null ? fallback : stored === 'true';+ } catch {+ return fallback;+ }+}++export const FLAT_THREAD = readFlag();diff --git a/client/src/components/Chat/Messages/Thread/index.ts b/client/src/components/Chat/Messages/Thread/index.tsnew file mode 100644index 0000000000..ade2061285--- /dev/null+++ b/client/src/components/Chat/Messages/Thread/index.ts@@ -0,0 +1,2 @@+export { default as ThreadList } from './List';+export { FLAT_THREAD } from './flag';diff --git a/client/src/components/Chat/Messages/Thread/state.ts b/client/src/components/Chat/Messages/Thread/state.tsnew file mode 100644index 0000000000..155d7c7673--- /dev/null+++ b/client/src/components/Chat/Messages/Thread/state.ts@@ -0,0 +1,69 @@+import { useCallback, useMemo, useRef, useSyncExternalStore } from 'react';+import { atom, useStore } from 'jotai';+import { atomFamily } from 'jotai/utils';+import type { Atom } from 'jotai';++/**+ * Sibling selection of the message thread: per parent, the REVERSED index of+ * the child on display (0 = newest). Feature-owned state, shared by the+ * recursive and flat renderers, the share view, and the writers that focus a+ * regenerated response, restore a resumed branch, or clear a conversation.+ */++/** Recoil keyed `null` and `undefined` parents distinctly from any id; one sentinel keeps that. */+export const NULL_SIBLING_KEY = '__null_parent__';++export const siblingKey = (parentId: string | null | undefined): string =>+ parentId ?? NULL_SIBLING_KEY;++export const siblingIdxFamily = atomFamily((_key: string) => atom(0));++/** Read-only atom over the selections of the given parents, for one subscription per consumer. */+export function siblingIndexesAtom(keys: readonly string[]): Atom<Record<string, number>> {+ return atom((get) => {+ const indexes: Record<string, number> = {};+ for (const key of keys) {+ indexes[key] = get(siblingIdxFamily(key));+ }+ return indexes;+ });+}++function sameKeys(a: readonly string[], b: readonly string[]): boolean {+ if (a === b) {+ return true;+ }+ if (a.length !== b.length) {+ return false;+ }+ for (let i = 0; i < a.length; i += 1) {+ if (a[i] !== b[i]) {+ return false;+ }+ }+ return true;+}++/**+ * Subscribes to the selections of `parentKeys` through one derived atom that+ * is only replaced when the KEYS change by content: callers derive the key+ * list from the messages cache on every write, and a fresh atom per write+ * would hand every consumer a new value object and a render per delta.+ * Read through `useSyncExternalStore` so a consumer renders exactly when the+ * selection object changes, with no mount-time re-render.+ */+export function useSiblingIndexes(parentKeys: readonly string[]): Record<string, number> {+ const jotaiStore = useStore();+ const stableRef = useRef(parentKeys);+ if (!sameKeys(stableRef.current, parentKeys)) {+ stableRef.current = parentKeys;+ }+ const stableKeys = stableRef.current;+ const indexesAtom = useMemo(() => siblingIndexesAtom(stableKeys), [stableKeys]);+ const subscribe = useCallback(+ (onChange: () => void) => jotaiStore.sub(indexesAtom, onChange),+ [jotaiStore, indexesAtom],+ );+ const read = useCallback(() => jotaiStore.get(indexesAtom), [jotaiStore, indexesAtom]);+ return useSyncExternalStore(subscribe, read, read);+}diff --git a/client/src/components/Chat/Messages/__tests__/MultiMessage.spec.tsx b/client/src/components/Chat/Messages/__tests__/MultiMessage.spec.tsxindex d5bf5ce960..92203969af 100644--- a/client/src/components/Chat/Messages/__tests__/MultiMessage.spec.tsx+++ b/client/src/components/Chat/Messages/__tests__/MultiMessage.spec.tsx@@ -1,5 +1,6 @@import React from 'react';import { RecoilRoot } from 'recoil';+import { Provider as JotaiProvider } from 'jotai';import { fireEvent, render, screen } from '@testing-library/react';import type { TMessage } from 'librechat-data-provider';import MultiMessage from '../MultiMessage';@@ -63,14 +64,16 @@ const msg = (messageId: string): TMessage =>const tree = (ids: string[]) => ids.map((id) => msg(id));const treeElement = (ids: string[]) => (- <RecoilRoot>- <MultiMessage- messageId="parent-1"- messagesTree={tree(ids)}- currentEditId={null}- setCurrentEditId={jest.fn()}- />- </RecoilRoot>+ <JotaiProvider>+ <RecoilRoot>+ <MultiMessage+ messageId="parent-1"+ messagesTree={tree(ids)}+ currentEditId={null}+ setCurrentEditId={jest.fn()}+ />+ </RecoilRoot>+ </JotaiProvider>);const displayed = () => screen.getAllByTestId('row')[0].textContent;diff --git a/client/src/components/Share/MultiMessage.tsx b/client/src/components/Share/MultiMessage.tsxindex 24ec2a5dd3..dc70b7151f 100644--- a/client/src/components/Share/MultiMessage.tsx+++ b/client/src/components/Share/MultiMessage.tsx@@ -1,10 +1,10 @@import { useEffect } from 'react';-import { useRecoilState } from 'recoil';+import { useAtom } from 'jotai';import type { TMessage } from 'librechat-data-provider';import type { TMessageProps } from '~/common';+import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state';import Message from './Message';-import store from '~/store';export default function MultiMessage({// messageId is used recursively here@@ -13,7 +13,7 @@ export default function MultiMessage({currentEditId,setCurrentEditId,}: TMessageProps) {- const [siblingIdx, setSiblingIdx] = useRecoilState(store.messagesSiblingIdxFamily(messageId));+ const [siblingIdx, setSiblingIdx] = useAtom(siblingIdxFamily(siblingKey(messageId)));const setSiblingIdxRev = (value: number) => {setSiblingIdx((messagesTree?.length ?? 0) - value - 1);diff --git a/client/src/components/Share/ShareView.tsx b/client/src/components/Share/ShareView.tsxindex 13228a35a0..5a8cf35ac3 100644--- a/client/src/components/Share/ShareView.tsx+++ b/client/src/components/Share/ShareView.tsx@@ -1,8 +1,9 @@import { memo, useState, useCallback, useContext } from 'react';import Cookies from 'js-cookie';+import { useStore } from 'jotai';import { buildTree } from 'librechat-data-provider';+import { useRecoilState, useRecoilValue } from 'recoil';import { useParams, useNavigate } from 'react-router-dom';-import { useRecoilState, useRecoilValue, useRecoilCallback } from 'recoil';import { useGetSharedMessages } from 'librechat-data-provider/react-query';import { CalendarDays, ExternalLink, RefreshCw, Settings, MessageSquarePlus } from 'lucide-react';import {@@ -21,6 +22,7 @@ import {} from '@librechat/client';import SharedSubagentActivityDialog from '~/components/Chat/Subagents/SharedSubagentActivityDialog';import { cn, DEFAULT_APP_TITLE, getResponseStatus, selectActiveBranchTail } from '~/utils';+import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state';import { useLocalize, useDocumentTitle, useAuthContext } from '~/hooks';import { ThemeSelector, LangSelector } from '~/components/Appearance';import { ShareMessagesProvider } from './ShareMessagesProvider';@@ -43,6 +45,7 @@ function SharedView() {const { isAuthReady } = useAuthContext();const { theme, setTheme } = useContext(ThemeContext);const { shareId } = useParams();+ const jotaiStore = useStore();const { data: config } = useGetSharedStartupConfig(shareId, { enabled: isAuthReady });const { data, isLoading, isFetching, refetch } = useGetSharedMessages(shareId ?? '', {enabled: isAuthReady,@@ -89,26 +92,20 @@ function SharedView() {* the root on SHARED_CONVO_KEY. An index is sent (not id or createdAt) because* shared ids are re-anonymized per request and createdAt can collide, while* the payload order is stable across requests. */- const getActiveTargetIndex = useRecoilCallback(- ({ snapshot }) =>- () => {- const messages = data?.messages;- if (messages == null || messages.length === 0) {- return undefined;- }- const getSiblingIndex = (parentMessageId: string | null | undefined) =>- snapshot- .getLoadable(store.messagesSiblingIdxFamily(parentMessageId ?? SHARED_CONVO_KEY))- .getValue() ?? 0;- const tail = selectActiveBranchTail(messages, SHARED_CONVO_KEY, getSiblingIndex);- if (tail == null) {- return undefined;- }- const index = messages.findIndex((message) => message.messageId === tail.messageId);- return index >= 0 ? index : undefined;- },- [data?.messages],- );+ const getActiveTargetIndex = useCallback(() => {+ const messages = data?.messages;+ if (messages == null || messages.length === 0) {+ return undefined;+ }+ const getSiblingIndex = (parentMessageId: string | null | undefined) =>+ jotaiStore.get(siblingIdxFamily(siblingKey(parentMessageId ?? SHARED_CONVO_KEY)));+ const tail = selectActiveBranchTail(messages, SHARED_CONVO_KEY, getSiblingIndex);+ if (tail == null) {+ return undefined;+ }+ const index = messages.findIndex((message) => message.messageId === tail.messageId);+ return index >= 0 ? index : undefined;+ }, [data?.messages, jotaiStore]);const { mutate: forkSharedConvo } = forkShare;const handleContinue = useCallback(() => {diff --git a/client/src/hooks/Chat/useChatHelpers.ts b/client/src/hooks/Chat/useChatHelpers.tsindex e83f9eaa0f..e99ede8eb0 100644--- a/client/src/hooks/Chat/useChatHelpers.ts+++ b/client/src/hooks/Chat/useChatHelpers.ts@@ -1,4 +1,5 @@import { useCallback, useMemo, useRef, useState } from 'react';+import { useSetAtom } from 'jotai';import { useQueryClient } from '@tanstack/react-query';import { Constants, QueryKeys, isAssistantsEndpoint } from 'librechat-data-provider';import { useRecoilState, useRecoilValue, useSetRecoilState, useRecoilCallback } from 'recoil';@@ -9,6 +10,7 @@ import {supportsGenerationProtocolV2,} from '~/data-provider';import { useLatestMessage, useLatestMessageId } from '~/hooks/Messages/useLatestMessage';+import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state';import useChatFunctions from '~/hooks/Chat/useChatFunctions';import useSteerConvert from '~/hooks/Chat/useSteerConvert';import { resolveAbortSteerTarget } from '~/utils';@@ -104,8 +106,8 @@ export default function useChatHelpers(index = 0, paramId?: string) {const latestMessageRef = useRef(latestMessage);latestMessageRef.current = latestMessage;- const setSiblingIdx = useSetRecoilState(- store.messagesSiblingIdxFamily(latestMessage?.parentMessageId ?? null),+ const setSiblingIdx = useSetAtom(+ siblingIdxFamily(siblingKey(latestMessage?.parentMessageId ?? null)),);const setMessages = useCallback(diff --git a/client/src/hooks/Chat/useFocusRegeneratedResponse.ts b/client/src/hooks/Chat/useFocusRegeneratedResponse.tsindex 5d5ad3abc0..2c2ab63f8e 100644--- a/client/src/hooks/Chat/useFocusRegeneratedResponse.ts+++ b/client/src/hooks/Chat/useFocusRegeneratedResponse.ts@@ -1,5 +1,6 @@-import { useRecoilCallback } from 'recoil';-import store from '~/store';+import { useCallback } from 'react';+import { useStore } from 'jotai';+import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state';/*** Focus a regenerated response's fork on its newest sibling. A regeneration@@ -12,14 +13,14 @@ import store from '~/store';* event so there is no flash on the kept sibling in between.*/export default function useFocusRegeneratedResponse() {- return useRecoilCallback(- ({ set }) =>- (parentMessageId?: string | null) => {- if (parentMessageId == null) {- return;- }- set(store.messagesSiblingIdxFamily(parentMessageId), 0);- },- [],+ const jotaiStore = useStore();+ return useCallback(+ (parentMessageId?: string | null) => {+ if (parentMessageId == null) {+ return;+ }+ jotaiStore.set(siblingIdxFamily(siblingKey(parentMessageId)), 0);+ },+ [jotaiStore],);}diff --git a/client/src/hooks/Config/useClearStates.ts b/client/src/hooks/Config/useClearStates.tsindex e19e8073a4..0dde3e2630 100644--- a/client/src/hooks/Config/useClearStates.ts+++ b/client/src/hooks/Config/useClearStates.ts@@ -1,5 +1,6 @@import { useStore } from 'jotai';import { useRecoilCallback } from 'recoil';+import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state';import { showSkillsPopoverFamily } from '~/components/Chat/Input/skillsState';import { clearLocalStorage } from '~/utils/localStorage';import store from '~/store';@@ -53,7 +54,7 @@ export default function useClearStates() {reset(store.globalAudioPlayingFamily(key));reset(store.activeRunFamily(key));reset(store.audioRunFamily(key));- reset(store.messagesSiblingIdxFamily(key.toString()));+ jotaiStore.set(siblingIdxFamily(siblingKey(key.toString())), 0);}clearLocalStorage(skipFirst);diff --git a/client/src/hooks/Messages/__tests__/useLatestMessage.spec.tsx b/client/src/hooks/Messages/__tests__/useLatestMessage.spec.tsxindex 89f769c07d..f1a260da0a 100644--- a/client/src/hooks/Messages/__tests__/useLatestMessage.spec.tsx+++ b/client/src/hooks/Messages/__tests__/useLatestMessage.spec.tsx@@ -1,6 +1,7 @@import React from 'react';import { act, renderHook } from '@testing-library/react';import { RecoilRoot, type MutableSnapshot } from 'recoil';+import { Provider as JotaiProvider, createStore } from 'jotai';import { MemoryRouter, Route, Routes } from 'react-router-dom';import { QueryClient, QueryClientProvider } from '@tanstack/react-query';import { QueryKeys, type TConversation, type TMessage } from 'librechat-data-provider';@@ -11,6 +12,7 @@ import {useGetLatestMessage,} from '~/hooks/Messages/useLatestMessage';import { getBranchSiblingIndexesForTarget, getMessageBranchSiblingParentIds } from '~/utils';+import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state';import store from '~/store';function createQueryClient() {@@ -69,17 +71,20 @@ function createWrapper(queryClient = createQueryClient(),initializeState?: (snapshot: MutableSnapshot) => void,initialEntry = '/c/conversation-1',+ jotaiStore = createStore(),) {return function Wrapper({ children }: { children: React.ReactNode }) {return (<QueryClientProvider client={queryClient}>- <RecoilRoot initializeState={initializeState}>- <MemoryRouter initialEntries={[initialEntry]}>- <Routes>- <Route path="/c/:conversationId?" element={children} />- </Routes>- </MemoryRouter>- </RecoilRoot>+ <JotaiProvider store={jotaiStore}>+ <RecoilRoot initializeState={initializeState}>+ <MemoryRouter initialEntries={[initialEntry]}>+ <Routes>+ <Route path="/c/:conversationId?" element={children} />+ </Routes>+ </MemoryRouter>+ </RecoilRoot>+ </JotaiProvider></QueryClientProvider>);};@@ -166,11 +171,17 @@ describe('useLatestMessage', () => {],);+ const jotaiStore = createStore();+ jotaiStore.set(siblingIdxFamily(siblingKey(userMessage.messageId)), 1);const { result } = renderHook(() => useLatestMessage(0), {- wrapper: createWrapper(queryClient, ({ set }) => {- set(store.conversationByIndex(0), conversation);- set(store.messagesSiblingIdxFamily(userMessage.messageId), 1);- }),+ wrapper: createWrapper(+ queryClient,+ ({ set }) => {+ set(store.conversationByIndex(0), conversation);+ },+ undefined,+ jotaiStore,+ ),});expect(result.current).toEqual(@@ -194,11 +205,17 @@ describe('useLatestMessage', () => {],);+ const jotaiStore = createStore();+ jotaiStore.set(siblingIdxFamily(siblingKey(userMessage.messageId)), 99);const { result } = renderHook(() => useLatestMessage(0), {- wrapper: createWrapper(queryClient, ({ set }) => {- set(store.conversationByIndex(0), conversation);- set(store.messagesSiblingIdxFamily(userMessage.messageId), 99);- }),+ wrapper: createWrapper(+ queryClient,+ ({ set }) => {+ set(store.conversationByIndex(0), conversation);+ },+ undefined,+ jotaiStore,+ ),});expect(result.current).toEqual(@@ -382,11 +399,17 @@ describe('useGetLatestMessage', () => {],);+ const jotaiStore = createStore();+ jotaiStore.set(siblingIdxFamily(siblingKey(userMessage.messageId)), 1);const { result } = renderHook(() => useGetLatestMessage(0), {- wrapper: createWrapper(queryClient, ({ set }) => {- set(store.conversationByIndex(0), conversation);- set(store.messagesSiblingIdxFamily(userMessage.messageId), 1);- }),+ wrapper: createWrapper(+ queryClient,+ ({ set }) => {+ set(store.conversationByIndex(0), conversation);+ },+ undefined,+ jotaiStore,+ ),});expect(result.current()).toEqual(diff --git a/client/src/hooks/Messages/useBuildMessageTree.ts b/client/src/hooks/Messages/useBuildMessageTree.tsindex 6ac9e57043..23c43fd5df 100644--- a/client/src/hooks/Messages/useBuildMessageTree.ts+++ b/client/src/hooks/Messages/useBuildMessageTree.ts@@ -1,13 +1,14 @@-import { useRecoilCallback } from 'recoil';+import { useCallback } from 'react';+import { useStore } from 'jotai';import type { TMessage } from 'librechat-data-provider';-import store from '~/store';+import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state';export default function useBuildMessageTree() {- const getSiblingIdx = useRecoilCallback(- ({ snapshot }) =>- async (messageId: string | null | undefined) =>- await snapshot.getPromise(store.messagesSiblingIdxFamily(messageId)),- [],+ const jotaiStore = useStore();+ const getSiblingIdx = useCallback(+ async (messageId: string | null | undefined) =>+ jotaiStore.get(siblingIdxFamily(siblingKey(messageId))),+ [jotaiStore],);// return an object or an array based on branches and recursive optiondiff --git a/client/src/hooks/Messages/useLatestMessage.ts b/client/src/hooks/Messages/useLatestMessage.tsindex 7766f40e0d..1fb8483fb9 100644--- a/client/src/hooks/Messages/useLatestMessage.ts+++ b/client/src/hooks/Messages/useLatestMessage.ts@@ -1,35 +1,20 @@-import { useCallback } from 'react';+import { useCallback, useMemo } from 'react';+import { useStore } from 'jotai';+import { useRecoilValue } from 'recoil';import { useParams } from 'react-router-dom';import { Constants, QueryKeys } from 'librechat-data-provider';import { useQuery, useQueryClient } from '@tanstack/react-query';-import { selectorFamily, useRecoilValue, useRecoilCallback } from 'recoil';import type { TMessage } from 'librechat-data-provider';+import {+ siblingIdxFamily,+ siblingKey,+ useSiblingIndexes,+} from '~/components/Chat/Messages/Thread/state';import { getMessageBranchSiblingParentIds, selectActiveBranchTail } from '~/utils';import store from '~/store';-const NULL_PARENT_KEY = '__null_parent__';const EMPTY_PARENT_IDS: (string | null)[] = [];--const getParentLookupKey = (parentMessageId: string | null | undefined) =>- parentMessageId ?? NULL_PARENT_KEY;--const siblingIndexesByParentSelector = selectorFamily<- Record<string, number>,- readonly (string | null)[]->({- key: 'latestMessageSiblingIndexesByParent',- get:- (parentIds) =>- ({ get }) => {- const indexes: Record<string, number> = {};-- for (const parentId of parentIds) {- indexes[getParentLookupKey(parentId)] = get(store.messagesSiblingIdxFamily(parentId));- }-- return indexes;- },-});+const getParentLookupKey = siblingKey;function useMessagesCacheSelect<TData>(messagesQueryId: string | null | undefined,@@ -85,7 +70,8 @@ function useLatestMessageSiblingIndexes([rootSiblingKey],);const parentIds = useMessagesCacheSelect(messagesQueryId, selectParentIds) ?? EMPTY_PARENT_IDS;- return useRecoilValue(siblingIndexesByParentSelector(parentIds));+ const parentKeys = useMemo(() => parentIds.map(siblingKey), [parentIds]);+ return useSiblingIndexes(parentKeys);}export function useLatestMessage(@@ -175,21 +161,18 @@ export function useGetLatestMessage(messagesQueryIdOverride?: string | null,): () => TMessage | null {const queryClient = useQueryClient();+ const jotaiStore = useStore();const conversationId = useRecoilValue(store.conversationIdByIndex(index));const messagesQueryId = useLatestMessagesQueryId(index, conversationId, messagesQueryIdOverride);- return useRecoilCallback(- ({ snapshot }) =>- (): TMessage | null => {- if (!messagesQueryId) {- return null;- }- const messages =- queryClient.getQueryData<TMessage[]>([QueryKeys.messages, messagesQueryId]) ?? [];- return selectActiveBranchTail(messages, conversationId, (parentId) =>- snapshot.getLoadable(store.messagesSiblingIdxFamily(parentId)).getValue(),- );- },- [queryClient, conversationId, messagesQueryId],- );+ return useCallback((): TMessage | null => {+ if (!messagesQueryId) {+ return null;+ }+ const messages =+ queryClient.getQueryData<TMessage[]>([QueryKeys.messages, messagesQueryId]) ?? [];+ return selectActiveBranchTail(messages, conversationId, (parentId) =>+ jotaiStore.get(siblingIdxFamily(siblingKey(parentId))),+ );+ }, [queryClient, jotaiStore, conversationId, messagesQueryId]);}diff --git a/client/src/hooks/Messages/useThreadRows.ts b/client/src/hooks/Messages/useThreadRows.tsnew file mode 100644index 0000000000..beae49b4ed--- /dev/null+++ b/client/src/hooks/Messages/useThreadRows.ts@@ -0,0 +1,88 @@+import { useEffect, useMemo, useRef } from 'react';+import { useStore } from 'jotai';+import type { TFile, TMessage } from 'librechat-data-provider';+import type { ThreadIndex, ThreadRow } from '~/utils/thread';+import {+ ROOT_KEY,+ buildThreadIndex,+ changedParentKeys,+ reconcileSiblingIdx,+ resolveThreadRows,+} from '~/utils/thread';+import {+ siblingIdxFamily,+ siblingKey,+ useSiblingIndexes,+} from '~/components/Chat/Messages/Thread/state';++const EMPTY_KEYS: string[] = [];++/**+ * The visible thread as a flat list of rows, derived from the messages cache+ * plus the sibling selection of the branch points only. A streaming delta+ * changes one message object, so every other row is reused by reference and+ * the memoized row components bail without a field diff.+ *+ * Selection reconciliation (follow an appended sibling, keep the viewed one+ * through churn) runs once per index change over the parents whose children+ * list actually changed, instead of once per rendered level.+ */+export default function useThreadRows(+ messages: TMessage[] | null | undefined,+ rootKey: string | null | undefined,+ fileMap?: Record<string, TFile>,+): ThreadRow[] | null {+ const jotaiStore = useStore();+ const rootSiblingKey = siblingKey(rootKey);+ const index = useMemo(+ () => (messages ? buildThreadIndex(messages, fileMap) : null),+ [messages, fileMap],+ );+ const branchKeys = useMemo(+ () =>+ index+ ? index.branchParentKeys.map((key) => (key === ROOT_KEY ? rootSiblingKey : key))+ : EMPTY_KEYS,+ [index, rootSiblingKey],+ );+ const siblingIndexes = useSiblingIndexes(branchKeys);++ const previousRowsRef = useRef<ThreadRow[] | null>(null);+ const rows = useMemo(() => {+ if (!index) {+ return null;+ }+ const next = resolveThreadRows(+ index,+ rootSiblingKey,+ (key) => siblingIndexes[key] ?? 0,+ previousRowsRef.current,+ );+ previousRowsRef.current = next;+ return next;+ }, [index, rootSiblingKey, siblingIndexes]);++ const previousIndexRef = useRef<{ rootKey: string; index: ThreadIndex } | null>(null);+ useEffect(() => {+ if (!index) {+ previousIndexRef.current = null;+ return;+ }+ const previous = previousIndexRef.current;+ previousIndexRef.current = { rootKey: rootSiblingKey, index };+ const previousIndex = previous && previous.rootKey === rootSiblingKey ? previous.index : null;+ for (const key of changedParentKeys(previousIndex, index)) {+ const atom = siblingIdxFamily(key === ROOT_KEY ? rootSiblingKey : key);+ const nextIdx = reconcileSiblingIdx(+ previousIndex?.children.get(key),+ index.children.get(key) ?? [],+ jotaiStore.get(atom),+ );+ if (nextIdx != null) {+ jotaiStore.set(atom, nextIdx);+ }+ }+ }, [index, rootSiblingKey, jotaiStore]);++ return rows;+}diff --git a/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx b/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsxindex e3a5d039e8..7261870e77 100644--- a/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx+++ b/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx@@ -1,11 +1,13 @@import { renderHook, act } from '@testing-library/react';import { RecoilRoot, useRecoilValue, useSetRecoilState } from 'recoil';import { QueryClient, QueryClientProvider } from '@tanstack/react-query';+import { Provider as JotaiProvider, createStore, useAtomValue } from 'jotai';import { Constants, ContentTypes, QueryKeys } from 'librechat-data-provider';import type { TMessage, TConversation, TSubmission } from 'librechat-data-provider';import type { MutableSnapshot } from 'recoil';import type { ReactNode } from 'react';import type { PendingSteer, QueuedMessage } from '~/store/families';+import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state';import useResumeOnLoad from '../useResumeOnLoad';import store from '~/store';@@ -110,6 +112,7 @@ function renderUseResumeOnLoad({onSubmissionStart?: (submissionStart: number | null) => void;}) {const getMessages = jest.fn(getMessagesOverride ?? (() => messages));+ const jotaiStore = createStore();const queryClient = new QueryClient({defaultOptions: { queries: { retry: false } },});@@ -158,7 +161,7 @@ function renderUseResumeOnLoad({return null;};const SiblingIndexProbe = () => {- const siblingIndex = useRecoilValue(store.messagesSiblingIdxFamily(siblingIndexParentId));+ const siblingIndex = useAtomValue(siblingIdxFamily(siblingKey(siblingIndexParentId)));if (siblingIndexParentId) {onSiblingIndex?.(siblingIndex);}@@ -167,14 +170,16 @@ function renderUseResumeOnLoad({const wrapper = ({ children }: { children: ReactNode }) => (<QueryClientProvider client={queryClient}>- <RecoilRoot initializeState={initializeState}>- <SubmissionProbe />- <SubmissionStartProbe />- <SiblingIndexProbe />- <PendingSteersProbe />- <QueuedMessagesProbe />- {children}- </RecoilRoot>+ <JotaiProvider store={jotaiStore}>+ <RecoilRoot initializeState={initializeState}>+ <SubmissionProbe />+ <SubmissionStartProbe />+ <SiblingIndexProbe />+ <PendingSteersProbe />+ <QueuedMessagesProbe />+ {children}+ </RecoilRoot>+ </JotaiProvider></QueryClientProvider>);diff --git a/client/src/hooks/SSE/useResumeOnLoad.ts b/client/src/hooks/SSE/useResumeOnLoad.tsindex 06b7e6c2e7..6d091daa14 100644--- a/client/src/hooks/SSE/useResumeOnLoad.ts+++ b/client/src/hooks/SSE/useResumeOnLoad.ts@@ -1,4 +1,5 @@-import { useEffect, useMemo, useRef, useState } from 'react';+import { useCallback, useEffect, useMemo, useRef, useState } from 'react';+import { useStore } from 'jotai';import { useQueryClient } from '@tanstack/react-query';import { useSetRecoilState, useRecoilValue, useRecoilCallback } from 'recoil';import {@@ -33,6 +34,7 @@ import {getGenerationProtocolVersion,supportsGenerationProtocolV2,} from '~/data-provider/SSE/protocol';+import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state';import useSteerConvert from '~/hooks/Chat/useSteerConvert';import store from '~/store';@@ -254,6 +256,7 @@ export default function useResumeOnLoad(runIndex = 0,messagesLoaded = true,) {+ const jotaiStore = useStore();const queryClient = useQueryClient();const setSubmission = useSetRecoilState(store.submissionByIndex(runIndex));const setSubmissionStart = useSetRecoilState(store.submissionStartFamily(runIndex));@@ -293,21 +296,20 @@ export default function useResumeOnLoad(* enter a resume→404→resume loop. A genuinely newer handoff has a different* createdAt key and remains eligible. */const consumedHandoffGenerationRef = useRef<string | null>(null);- const restoreResumeBranch = useRecoilCallback(- ({ set }) =>- (resumeState: Agents.ResumeState, messages: TMessage[], activeConversationId: string) => {- const targetMessageId = getResumeBranchTargetMessageId(resumeState, messages);- const branchIndexes = getBranchSiblingIndexesForTarget(- messages,- targetMessageId,- activeConversationId,- );+ const restoreResumeBranch = useCallback(+ (resumeState: Agents.ResumeState, messages: TMessage[], activeConversationId: string) => {+ const targetMessageId = getResumeBranchTargetMessageId(resumeState, messages);+ const branchIndexes = getBranchSiblingIndexesForTarget(+ messages,+ targetMessageId,+ activeConversationId,+ );- for (const { parentMessageId, siblingIdx } of branchIndexes) {- set(store.messagesSiblingIdxFamily(parentMessageId), siblingIdx);- }- },- [],+ for (const { parentMessageId, siblingIdx } of branchIndexes) {+ jotaiStore.set(siblingIdxFamily(siblingKey(parentMessageId)), siblingIdx);+ }+ },+ [jotaiStore],);/** Restore pending-steer chips for steers the server still has queueddiff --git a/client/src/store/families.ts b/client/src/store/families.tsindex cadd2af9b1..3bfb45ec9d 100644--- a/client/src/store/families.ts+++ b/client/src/store/families.ts@@ -676,11 +676,6 @@ const audioRunFamily = atomFamily<string | null, string | number | null>({default: null,});-const messagesSiblingIdxFamily = atomFamily<number, string | null | undefined>({- key: 'messagesSiblingIdx',- default: 0,-});-/** Setter-only access to the conversation atom: registers the key like* `useCreateConversationAtom` but never subscribes to the value, so callers* that only write (navigation, per-row actions) don't re-render on every@@ -796,7 +791,6 @@ export default {isSubmittingFamily,optionSettingsFamily,showPopoverFamily,- messagesSiblingIdxFamily,anySubmittingSelector,allConversationsSelector,conversationIdByIndex,diff --git a/client/src/utils/thread.spec.ts b/client/src/utils/thread.spec.tsnew file mode 100644index 0000000000..0ccbe701d3--- /dev/null+++ b/client/src/utils/thread.spec.ts@@ -0,0 +1,156 @@+import type { TFile, TMessage } from 'librechat-data-provider';+import {+ ROOT_KEY,+ buildThreadIndex,+ changedParentKeys,+ reconcileSiblingIdx,+ resolveThreadRows,+} from './thread';++const ROOT = '00000000-0000-0000-0000-000000000000';++function message(messageId: string, parentMessageId: string, text = messageId): TMessage {+ return {+ messageId,+ parentMessageId,+ conversationId: 'c',+ text,+ isCreatedByUser: messageId.startsWith('u'),+ } as TMessage;+}++/** u1 -> a1 -> u2 -> (a2-old, a2) ; a2 -> u3 */+function fixture(): TMessage[] {+ return [+ message('u1', ROOT),+ message('a1', 'u1'),+ message('u2', 'a1'),+ message('a2-old', 'u2'),+ message('a2', 'u2'),+ message('u3', 'a2'),+ ];+}++describe('buildThreadIndex', () => {+ it('indexes children in array order and lists branch points', () => {+ const index = buildThreadIndex(fixture());+ expect(index.children.get(ROOT_KEY)).toEqual(['u1']);+ expect(index.children.get('u2')).toEqual(['a2-old', 'a2']);+ expect(index.branchParentKeys).toEqual(['u2']);+ });++ it('is memoized per array identity', () => {+ const messages = fixture();+ expect(buildThreadIndex(messages)).toBe(buildThreadIndex(messages));+ expect(buildThreadIndex(messages)).not.toBe(buildThreadIndex(messages.slice()));+ });++ it('links a child that precedes its parent and roots an orphan', () => {+ const index = buildThreadIndex([+ message('a1', 'u1'),+ message('u1', ROOT),+ message('x', 'gone'),+ ]);+ expect(index.children.get('u1')).toEqual(['a1']);+ expect(index.children.get(ROOT_KEY)).toEqual(['u1', 'x']);+ });++ it('breaks a parent cycle by resurfacing it as a root', () => {+ const index = buildThreadIndex([message('p', 'q'), message('q', 'p')]);+ expect(index.children.get(ROOT_KEY)).toEqual(['p']);+ expect(index.children.get('p')).toEqual(['q']);+ expect(index.children.get('q')).toEqual([]);+ const rows = resolveThreadRows(index, 'c', () => 0, null);+ expect(rows.map((row) => row.source.messageId)).toEqual(['p', 'q']);+ });+});++describe('resolveThreadRows', () => {+ it('follows the newest sibling by default and reports tree fields', () => {+ const rows = resolveThreadRows(buildThreadIndex(fixture()), 'c', () => 0, null);+ expect(rows.map((row) => row.source.messageId)).toEqual(['u1', 'a1', 'u2', 'a2', 'u3']);+ expect(rows[3]).toMatchObject({ siblingIdx: 0, siblingCount: 2, depth: 3, childCount: 1 });+ expect(rows[3].message.depth).toBe(3);+ expect(rows[3].message.children?.length).toBe(1);+ expect(rows[2].parentKey).toBe('a1');+ expect(rows[0].parentKey).toBe('c');+ });++ it('switches a branch through the selection lookup', () => {+ const rows = resolveThreadRows(+ buildThreadIndex(fixture()),+ 'c',+ (key) => (key === 'u2' ? 1 : 0),+ null,+ );+ expect(rows.map((row) => row.source.messageId)).toEqual(['u1', 'a1', 'u2', 'a2-old']);+ });++ it('reuses every untouched row across a streaming write and keeps the array when nothing changed', () => {+ const first = fixture();+ const index = buildThreadIndex(first);+ const rows = resolveThreadRows(index, 'c', () => 0, null);+ const streamed = first.map((m) => (m.messageId === 'u3' ? { ...m, text: 'u3 more' } : m));+ const next = resolveThreadRows(buildThreadIndex(streamed), 'c', () => 0, rows);+ expect(next).not.toBe(rows);+ expect(next.slice(0, 4)).toEqual(rows.slice(0, 4));+ expect(next[0]).toBe(rows[0]);+ expect(next[4]).not.toBe(rows[4]);+ expect(next[4].message.text).toBe('u3 more');+ const again = resolveThreadRows(buildThreadIndex(streamed.slice()), 'c', () => 0, next);+ expect(again).toBe(next);+ });+});++describe('resolveThreadRows with a file map', () => {+ it('hydrates files on the row while reusing the raw cache identity', () => {+ const withFile = { ...message('u1', ROOT), files: [{ file_id: 'f1' }] } as TMessage;+ const fileMap: Record<string, TFile> = {+ f1: { file_id: 'f1', filename: 'hydrated.txt' } as TFile,+ };+ const first = [withFile, message('a1', 'u1')];+ const rows = resolveThreadRows(buildThreadIndex(first, fileMap), 'c', () => 0, null);+ expect(rows[0].source).toBe(withFile);+ expect(rows[0].message.files?.[0]).toMatchObject({ filename: 'hydrated.txt' });+ const streamed = first.map((m) => (m.messageId === 'a1' ? { ...m, text: 'more' } : m));+ const next = resolveThreadRows(buildThreadIndex(streamed, fileMap), 'c', () => 0, rows);+ expect(next[0]).toBe(rows[0]);+ const remapped = resolveThreadRows(+ buildThreadIndex(streamed, { ...fileMap }),+ 'c',+ () => 0,+ next,+ );+ expect(remapped[0]).not.toBe(next[0]);+ });+});++describe('reconcileSiblingIdx', () => {+ it('follows an appended newest sibling', () => {+ expect(reconcileSiblingIdx(['a', 'b'], ['a', 'b', 'c'], 1)).toBe(0);+ expect(reconcileSiblingIdx(['a', 'b'], ['a', 'b', 'c'], 0)).toBeNull();+ });++ it('keeps the viewed sibling through a re-key or reorder', () => {+ expect(reconcileSiblingIdx(['a', 'b', 'c'], ['a', 'b', 'c2'], 1)).toBeNull();+ expect(reconcileSiblingIdx(['a', 'b', 'c'], ['b', 'a', 'c'], 2)).toBe(1);+ });++ it('falls back to the newest when the viewed sibling vanished', () => {+ expect(reconcileSiblingIdx(['a', 'b', 'c'], ['b', 'c'], 2)).toBe(0);+ });++ it('clamps a stale index on a parent it has not seen', () => {+ expect(reconcileSiblingIdx(undefined, ['a'], 3)).toBe(0);+ expect(reconcileSiblingIdx(undefined, ['a', 'b'], 1)).toBeNull();+ });+});++describe('changedParentKeys', () => {+ it('reports only parents whose children list changed', () => {+ const before = buildThreadIndex(fixture());+ const after = buildThreadIndex([...fixture(), message('a3', 'u3')]);+ expect(changedParentKeys(before, after)).toEqual(['u3']);+ expect(changedParentKeys(null, before).length).toBe(before.children.size);+ });+});diff --git a/client/src/utils/thread.ts b/client/src/utils/thread.tsnew file mode 100644index 0000000000..1a53d2c33c--- /dev/null+++ b/client/src/utils/thread.ts@@ -0,0 +1,333 @@+import type { TFile, TMessage } from 'librechat-data-provider';++/** Parent key under which parentless (and self-parented) messages are filed. */+export const ROOT_KEY = '';++/**+ * Keyed view of the flat messages array. Nodes reference the cached message+ * objects themselves (no per-message copies), so an untouched message keeps+ * its identity across streaming writes and memoized rows can bail on a+ * reference check instead of a field diff.+ */+export type ThreadIndex = {+ /** Raw cache objects: the identity rows are reused by. */+ messages: Map<string, TMessage>;+ /** parent key -> child ids in creation (array) order */+ children: Map<string, string[]>;+ /** Parent keys with more than one child: the only levels whose selection matters. */+ branchParentKeys: string[];+ /** Applied to `files` when a row is built, so a hydrated copy never replaces the raw identity. */+ fileMap?: Record<string, TFile>;+};++/** One visible row of the thread: the active path from root to leaf. */+export type ThreadRow = {+ /** Cache identity of the message, for reuse checks. */+ source: TMessage;+ /** `source` with files hydrated and widened with the tree fields the row components read. */+ message: TMessage;+ /** The file map `message` was hydrated with; a new map rebuilds the row. */+ fileMap?: Record<string, TFile>;+ /** Sibling-selection key: parent message id, or the root key for top-level rows. */+ parentKey: string;+ depth: number;+ /** Reversed sibling index (0 = newest), matching the selection atoms. */+ siblingIdx: number;+ siblingCount: number;+ childCount: number;+};++type IndexCacheEntry = {+ bare?: ThreadIndex;+ fileMap?: Record<string, TFile>;+ hydrated?: ThreadIndex;+};++const indexCache = new WeakMap<(TMessage | undefined)[], IndexCacheEntry>();++const NO_CHILDREN: TMessage[] = [];++function hydrateFiles(message: TMessage, fileMap?: Record<string, TFile>): TMessage {+ if (!message.files || !fileMap) {+ return message;+ }+ return {+ ...message,+ files: message.files.map((file) => fileMap[file.file_id ?? ''] ?? file),+ };+}++function parentKeyOf(message: TMessage): string {+ const parentId = message.parentMessageId;+ if (parentId == null || parentId === message.messageId) {+ return ROOT_KEY;+ }+ return parentId;+}++/**+ * Order-robust like `buildTree`: linking happens after every message is+ * indexed, a message whose parent is absent becomes a root, and nodes on a+ * corrupt parent cycle (unreachable from any root) resurface as roots.+ */+function indexMessages(+ messages: (TMessage | undefined)[],+ fileMap?: Record<string, TFile>,+): ThreadIndex {+ const byId = new Map<string, TMessage>();+ const ordered: TMessage[] = [];+ for (const message of messages) {+ if (!message) {+ continue;+ }+ byId.set(message.messageId, message);+ ordered.push(message);+ }++ const children = new Map<string, string[]>();+ const link = (parentKey: string, id: string) => {+ const list = children.get(parentKey);+ if (list) {+ list.push(id);+ return;+ }+ children.set(parentKey, [id]);+ };+ for (const message of ordered) {+ const parentKey = parentKeyOf(message);+ link(parentKey !== ROOT_KEY && byId.has(parentKey) ? parentKey : ROOT_KEY, message.messageId);+ }++ /** Every node has one parent, so a roots-down walk reaches each node once;+ * an already-visited child is a cycle back-edge and is severed so path+ * resolution terminates. */+ const visited = new Set<string>();+ const walk = (startIds: string[]) => {+ const stack = [...startIds];+ while (stack.length > 0) {+ const id = stack.pop() as string;+ if (visited.has(id)) {+ continue;+ }+ visited.add(id);+ const kids = children.get(id);+ if (!kids) {+ continue;+ }+ const reachable = kids.filter((kid) => !visited.has(kid));+ if (reachable.length !== kids.length) {+ children.set(id, reachable);+ }+ for (let i = reachable.length - 1; i >= 0; i -= 1) {+ stack.push(reachable[i]);+ }+ }+ };+ walk(children.get(ROOT_KEY) ?? []);+ if (visited.size !== ordered.length) {+ for (const message of ordered) {+ if (!visited.has(message.messageId)) {+ link(ROOT_KEY, message.messageId);+ walk([message.messageId]);+ }+ }+ }++ const branchParentKeys: string[] = [];+ for (const [parentKey, ids] of children) {+ if (ids.length > 1) {+ branchParentKeys.push(parentKey);+ }+ }+ return { messages: byId, children, branchParentKeys, fileMap };+}++/** Memoized per messages-array identity, with one extra slot for the latest fileMap. */+export function buildThreadIndex(+ messages: (TMessage | undefined)[],+ fileMap?: Record<string, TFile>,+): ThreadIndex {+ const cached = indexCache.get(messages);+ if (cached) {+ if (fileMap == null && cached.bare) {+ return cached.bare;+ }+ if (fileMap != null && cached.fileMap === fileMap && cached.hydrated) {+ return cached.hydrated;+ }+ }+ const index = indexMessages(messages, fileMap);+ const entry = cached ?? {};+ if (fileMap == null) {+ entry.bare = index;+ } else {+ entry.fileMap = fileMap;+ entry.hydrated = index;+ }+ indexCache.set(messages, entry);+ return index;+}++export type SiblingIndexLookup = (parentKey: string) => number;++function clampSiblingIdx(siblingIdx: number, siblingCount: number): number {+ return siblingIdx >= 0 && siblingIdx < siblingCount ? siblingIdx : 0;+}++function rowsEqual(+ previous: ThreadRow,+ index: ThreadIndex,+ source: TMessage,+ depth: number,+ siblingIdx: number,+ siblingCount: number,+ childCount: number,+): boolean {+ return (+ previous.source === source &&+ previous.fileMap === index.fileMap &&+ previous.depth === depth &&+ previous.siblingIdx === siblingIdx &&+ previous.siblingCount === siblingCount &&+ previous.childCount === childCount+ );+}++/**+ * Walks the active branch (root -> leaf) following the per-level sibling+ * selection, O(depth). Rows whose inputs are unchanged are reused from+ * `previousRows` so memoized row components bail by reference; when every+ * row is reused, `previousRows` itself is returned.+ *+ * `rootKey` is the selection key of the top level (the conversation id), the+ * same key the sibling atoms have always used for root siblings.+ */+export function resolveThreadRows(+ index: ThreadIndex,+ rootKey: string,+ getSiblingIdx: SiblingIndexLookup,+ previousRows: ThreadRow[] | null,+): ThreadRow[] {+ const previousById = new Map<string, ThreadRow>();+ if (previousRows) {+ for (const row of previousRows) {+ previousById.set(row.source.messageId, row);+ }+ }++ const rows: ThreadRow[] = [];+ let reusedAll = previousRows != null;+ let siblings = index.children.get(ROOT_KEY) ?? [];+ let parentKey = rootKey;+ let atomKey = rootKey;+ let depth = 0;++ while (siblings.length > 0) {+ const siblingCount = siblings.length;+ const siblingIdx = clampSiblingIdx(getSiblingIdx(atomKey), siblingCount);+ const id = siblings[siblingCount - siblingIdx - 1];+ const source = index.messages.get(id);+ if (!source) {+ break;+ }+ const childIds = index.children.get(id) ?? [];+ const childCount = childIds.length;+ const previous = previousById.get(id);+ if (+ previous &&+ rowsEqual(previous, index, source, depth, siblingIdx, siblingCount, childCount)+ ) {+ rows.push(previous);+ } else {+ reusedAll = false;+ const children =+ childCount === 0+ ? NO_CHILDREN+ : childIds+ .map((childId) => index.messages.get(childId))+ .filter((child): child is TMessage => child != null);+ rows.push({+ source,+ message: { ...hydrateFiles(source, index.fileMap), depth, children },+ fileMap: index.fileMap,+ parentKey,+ depth,+ siblingIdx,+ siblingCount,+ childCount,+ });+ }+ parentKey = id;+ atomKey = id;+ siblings = childIds;+ depth += 1;+ }++ if (reusedAll && previousRows && previousRows.length === rows.length) {+ return previousRows;+ }+ return rows;+}++/**+ * Sibling-selection reconciliation for one level, ported from the recursive+ * `MultiMessage` effect: an APPENDED newest child is followed (send, regenerate+ * and edit-resubmit all append); any other change to the children list keeps+ * the message the user was viewing, recomputing its reversed index, and falls+ * back to the newest only when it no longer exists. Returns the next reversed+ * index, or `null` when the current one stands.+ */+export function reconcileSiblingIdx(+ previousIds: string[] | undefined,+ nextIds: string[],+ currentIdx: number,+): number | null {+ const length = nextIds.length;+ if (length === 0) {+ return null;+ }+ if (!previousIds) {+ return currentIdx >= length ? 0 : null;+ }+ const newestId = nextIds[length - 1];+ const previousNewestId = previousIds[previousIds.length - 1];+ const appendedNewest =+ previousNewestId == null ||+ (newestId !== previousNewestId &&+ !previousIds.includes(newestId) &&+ nextIds.includes(previousNewestId));+ let nextIdx = currentIdx;+ if (appendedNewest) {+ nextIdx = 0;+ } else if (currentIdx > 0) {+ const viewedId = previousIds[previousIds.length - currentIdx - 1];+ const viewedIndex = viewedId == null ? -1 : nextIds.indexOf(viewedId);+ nextIdx = viewedIndex >= 0 ? length - viewedIndex - 1 : 0;+ } else if (currentIdx >= length) {+ nextIdx = 0;+ }+ return nextIdx === currentIdx ? null : nextIdx;+}++function sameIds(a: string[] | undefined, b: string[]): boolean {+ if (!a || a.length !== b.length) {+ return false;+ }+ for (let i = 0; i < a.length; i += 1) {+ if (a[i] !== b[i]) {+ return false;+ }+ }+ return true;+}++/** Index keys whose children list changed between two indexes. */+export function changedParentKeys(previous: ThreadIndex | null, next: ThreadIndex): string[] {+ const changed: string[] = [];+ for (const [key, ids] of next.children) {+ if (!sameIds(previous?.children.get(key), ids)) {+ changed.push(key);+ }+ }+ return changed;+}diff --git a/client/src/vite-env.d.ts b/client/src/vite-env.d.tsindex a7feb04700..9ff196fed7 100644--- a/client/src/vite-env.d.ts+++ b/client/src/vite-env.d.ts@@ -3,6 +3,7 @@interface ImportMetaEnv {readonly VITE_ENABLE_LOGGER: string;readonly VITE_LOGGER_FILTER: string;+ readonly VITE_FLAT_THREAD: string;// Add other env variables here}diff --git a/e2e/benchmarks-tree/flatenv.ts b/e2e/benchmarks-tree/flatenv.tsnew file mode 100644index 0000000000..dcbd247b3b--- /dev/null+++ b/e2e/benchmarks-tree/flatenv.ts@@ -0,0 +1,2 @@+/** Side-effect module: defaults the flat thread renderer ON for the parity config. */+process.env.VITE_FLAT_THREAD = process.env.VITE_FLAT_THREAD ?? 'true';diff --git a/e2e/benchmarks-tree/payload.ts b/e2e/benchmarks-tree/payload.tsnew file mode 100644index 0000000000..0f266a38fc--- /dev/null+++ b/e2e/benchmarks-tree/payload.ts@@ -0,0 +1,99 @@+import type { SeedMessage } from '../specs/mock/db';++/**+ * Deterministic transcript for the message-tree render benchmark: a long+ * linear spine with two regenerate branches, one shallow (short alternate+ * continuation) and one at the leaf, so both a deep path swap and a+ * leaf-only sibling switch can be measured against the same thread.+ */+/** Turns per seeded thread; `TREE_PERF_TURNS` overrides it for scaling runs. */+export const TURNS = Number(process.env.TREE_PERF_TURNS) || 120;+export const ROWS = TURNS * 2;+export const SHALLOW_BRANCH_TURN = 3;+export const SHALLOW_BRANCH_CONTINUATION_TURNS = 2;+export const ROOT_PARENT = '00000000-0000-0000-0000-000000000000';++const SENTENCE =+ 'The rollout plan sequences every dependent service behind a single feature flag so rollbacks stay one toggle away. ';++export function marker(label: string): string {+ return `TREEBENCH-${label}`;+}++export function turnHeading(label: string, turn: number): string {+ return `${marker(label)} section ${turn}`;+}++export function altHeading(label: string, turn: number): string {+ return `${marker(label)} alternate ${turn}`;+}++function assistantBody(heading: string, turn: number): string {+ let body = `## ${heading}\n\n${SENTENCE}${SENTENCE}\n\n`;+ body += `- ${marker('point')} one for turn ${turn}\n`;+ body += `- ${marker('point')} two for turn ${turn}\n\n`;+ if (turn % 3 === 0) {+ body +=+ '```ts\nexport function rollout(stage: number): boolean {\n return stage > 0;\n}\n```\n\n';+ }+ if (turn % 4 === 0) {+ body += '| Service | Requests |\n| --- | --- |\n| gateway | 120000 |\n| worker | 135500 |\n\n';+ }+ return body;+}++function userRow(label: string, id: string, parentMessageId: string, turn: number): SeedMessage {+ return {+ messageId: id,+ parentMessageId,+ text: `${marker(label)} prompt ${turn}: walk me through the rollout for stage ${turn}.`,+ isCreatedByUser: true,+ sender: 'User',+ };+}++function assistantRow(id: string, parentMessageId: string, heading: string, turn: number) {+ return {+ messageId: id,+ parentMessageId,+ text: assistantBody(heading, turn),+ isCreatedByUser: false,+ sender: 'Assistant',+ };+}++/**+ * Seed order doubles as creation order, so an alternate sibling is emitted+ * BEFORE the spine row it competes with: the newest sibling is the default+ * selection, and the spine must stay the default visible path.+ */+export function buildTreeMessages(label: string): SeedMessage[] {+ const messages: SeedMessage[] = [];+ let parentMessageId = ROOT_PARENT;+ for (let turn = 1; turn <= TURNS; turn += 1) {+ const userId = `${label}-user-${turn}`;+ messages.push(userRow(label, userId, parentMessageId, turn));+ const assistantId = `${label}-assistant-${turn}`;+ if (turn === SHALLOW_BRANCH_TURN) {+ const altId = `${label}-assistant-${turn}-alt`;+ messages.push(assistantRow(altId, userId, altHeading(label, turn), turn));+ let altParent = altId;+ for (let extra = 1; extra <= SHALLOW_BRANCH_CONTINUATION_TURNS; extra += 1) {+ const altTurn = 1000 + extra;+ const altUser = `${label}-alt-user-${extra}`;+ messages.push(userRow(label, altUser, altParent, altTurn));+ const altAssistant = `${label}-alt-assistant-${extra}`;+ messages.push(assistantRow(altAssistant, altUser, altHeading(label, altTurn), altTurn));+ altParent = altAssistant;+ }+ }+ if (turn === TURNS) {+ messages.push(+ assistantRow(`${label}-assistant-${turn}-alt`, userId, altHeading(label, turn), turn),+ );+ }+ messages.push(assistantRow(assistantId, userId, turnHeading(label, turn), turn));+ parentMessageId = assistantId;+ }+ return messages;+}diff --git a/e2e/benchmarks-tree/tree-render.perf.spec.ts b/e2e/benchmarks-tree/tree-render.perf.spec.tsnew file mode 100644index 0000000000..701227156f--- /dev/null+++ b/e2e/benchmarks-tree/tree-render.perf.spec.ts@@ -0,0 +1,383 @@+import { randomUUID } from 'node:crypto';+import { expect, test } from '@playwright/test';+import type { Page } from '@playwright/test';+import type { BrowserPhase } from '../perf/browser';+import type { PerfSnapshot } from '../perf/scan';+import { createBrowserProbe, installBrowserPerf } from '../perf/browser';+import { deleteConversations, deleteMessagesByConversation, withMongo } from '../specs/mock/db';+import { MOCK_ENDPOINTS, messagesView, sendMessage } from '../specs/mock/helpers';+import { getE2EUser } from '../setup/user';+import {+ attachSnapshot,+ buildTallySetup,+ installReactScan,+ longTaskStats,+ resetPerf,+ snapshotPerf,+ topComponents,+ totals,+} from '../perf/scan';+import {+ ROWS,+ SHALLOW_BRANCH_CONTINUATION_TURNS,+ SHALLOW_BRANCH_TURN,+ TURNS,+ altHeading,+ buildTreeMessages,+ turnHeading,+} from './payload';++/**+ * Message-tree render benchmark (react-scan).+ *+ * Measures what the tree-shaped render path costs on a long thread: renders+ * and render time per streamed delta, and per sibling switch at the leaf and+ * near the root. The numbers are printed and attached; the assertions only+ * guard the harness itself.+ */++const userEmail = getE2EUser().email;+/** `TREE_PERF_SCAN=0` drops react-scan so long-task totals carry no instrumentation overhead. */+const WITH_SCAN = process.env.TREE_PERF_SCAN !== '0';+const VARIANTS = [+ { name: 'tree (recursive MultiMessage)', flat: false, label: 'T' },+ { name: 'flat (index + path list)', flat: true, label: 'F' },+] as const;+type Variant = (typeof VARIANTS)[number];+const CONVOS = new Map(+ VARIANTS.map((variant) => [+ variant.label,+ { id: randomUUID(), label: variant.label, title: `Tree render bench ${variant.label}` },+ ]),+);+const ENDPOINT = MOCK_ENDPOINTS[0];+const KEY_COMPONENTS = [+ 'MultiMessage',+ 'MessageRender',+ 'ContentRender',+ 'Message',+ 'MessageContent',+ 'MarkdownBlock',+ 'SiblingSwitch',+ 'MessagesViewContent',+];++async function seedTree(CONVO: { id: string; label: string; title: string }): Promise<void> {+ await withMongo(async (db) => {+ const user = await db.collection('users').findOne({ email: userEmail });+ if (!user) {+ throw new Error(`E2E 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() - ROWS * 2000;+ const docs = buildTreeMessages(CONVO.label).map((message, index) => ({+ ...message,+ 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);+ });+}++function heading(page: Page, text: string) {+ return messagesView(page).getByRole('heading', { name: text, exact: true }).first();+}++type ResourceSample = {+ heapMB: number;+ heapTotalMB: number;+ nodes: number;+ listeners: number;+ rendererCpuS: number;+};++/**+ * Process-level cost per phase: JS heap after a forced GC (retained memory,+ * not allocation churn), DOM node and listener counts, and the renderer+ * process CPU time consumed during the phase (all threads, so compositor and+ * GC work count, which the main-thread task total above leaves out).+ */+async function createResourceProbe(page: Page) {+ const session = await page.context().newCDPSession(page);+ await session.send('HeapProfiler.enable');+ await session.send('Performance.enable');+ const browser = page.context().browser();+ const browserSession = browser ? await browser.newBrowserCDPSession() : null;+ const rendererCpu = async () => {+ if (!browserSession) {+ return 0;+ }+ const { processInfo } = await browserSession.send('SystemInfo.getProcessInfo');+ return processInfo+ .filter((info) => info.type === 'renderer')+ .reduce((sum, info) => sum + info.cpuTime, 0);+ };+ let cpuStart = 0;+ return {+ async start() {+ cpuStart = await rendererCpu();+ },+ async sample(): Promise<ResourceSample> {+ const cpuEnd = await rendererCpu();+ await session.send('HeapProfiler.collectGarbage');+ const { metrics } = await session.send('Performance.getMetrics');+ const metric = (name: string) => metrics.find((entry) => entry.name === name)?.value ?? 0;+ return {+ heapMB: metric('JSHeapUsedSize') / 1048576,+ heapTotalMB: metric('JSHeapTotalSize') / 1048576,+ nodes: metric('Nodes'),+ listeners: metric('JSEventListeners'),+ rendererCpuS: cpuEnd - cpuStart,+ };+ },+ };+}++function report(+ name: string,+ snapshot: PerfSnapshot,+ browser: BrowserPhase,+ extra: Record<string, number> = {},+ resources?: ResourceSample,+) {+ const sum = totals(snapshot);+ const tasks = longTaskStats(snapshot);+ console.log(`\n=== ${name} (${Math.ceil(snapshot.elapsedMs)}ms wall) ===`);+ console.log(+ `total renders=${sum.renders} render-time=${sum.time.toFixed(0)}ms ` ++ `longtask-total=${tasks.total.toFixed(0)}ms worst-longtask=${tasks.worst.toFixed(0)}ms`,+ );+ console.log(+ `cdp: task=${browser.taskMs.toFixed(0)}ms script=${browser.scriptMs.toFixed(0)}ms ` ++ `layout=${browser.layoutMs.toFixed(0)}ms style=${browser.styleMs.toFixed(0)}ms ` ++ `busy=${browser.busyPercent.toFixed(1)}% layouts=${browser.layoutCount} ` ++ `heap=${(browser.heapEndBytes / 1048576).toFixed(0)}MB nodes=${browser.nodesEnd}`,+ );+ if (resources) {+ console.log(+ `resources: rendererCPU=${resources.rendererCpuS.toFixed(2)}s ` ++ `heapAfterGC=${resources.heapMB.toFixed(1)}MB heapTotal=${resources.heapTotalMB.toFixed(0)}MB ` ++ `nodes=${resources.nodes} listeners=${resources.listeners}`,+ );+ }+ for (const [key, value] of Object.entries(extra)) {+ console.log(`${key}=${value}`);+ }+ console.log('key components:');+ for (const component of KEY_COMPONENTS) {+ const slot = snapshot.renders[component];+ console.log(+ ` ${component.padEnd(20)} renders=${String(slot?.count ?? 0).padStart(6)} time=${(slot?.time ?? 0).toFixed(1)}ms`,+ );+ }+ console.log('top components:');+ for (const line of topComponents(snapshot, 12)) {+ console.log(` ${line}`);+ }+}++/** Rows the deep switch drops: the spine below the branch turn, less the alternate continuation. */+const DEEP_SWITCH_ROWS = ROWS - SHALLOW_BRANCH_TURN * 2 - SHALLOW_BRANCH_CONTINUATION_TURNS * 2;++async function clickSibling(page: Page, name: string, position: 'first' | 'last') {+ const buttons = page.getByRole('button', { name, exact: true });+ const button = position === 'first' ? buttons.first() : buttons.last();+ await button.dispatchEvent('click');+}++test.describe('message tree render perf (react-scan)', () => {+ test.beforeAll(async () => {+ for (const convo of CONVOS.values()) {+ await seedTree(convo);+ }+ });++ test.afterAll(async () => {+ const ids = Array.from(CONVOS.values()).map((convo) => convo.id);+ await deleteMessagesByConversation(ids);+ await deleteConversations(ids);+ });++ for (const variant of VARIANTS) {+ test(`${variant.name}: streaming and sibling switches on a long thread`, async ({+ page,+ }, testInfo) => {+ await runVariant(page, testInfo, variant);+ });+ }+});++async function runVariant(+ page: Page,+ testInfo: Parameters<Parameters<typeof test>[1]>[1],+ variant: Variant,+) {+ const CONVO = CONVOS.get(variant.label);+ if (!CONVO) {+ throw new Error('variant conversation missing');+ }+ {+ test.setTimeout(8 * 60 * 1000);+ if (WITH_SCAN) {+ await installReactScan(page, 'MultiMessage');+ } else {+ await page.addInitScript({ content: buildTallySetup('MultiMessage') });+ }+ await installBrowserPerf(page);+ const probe = await createBrowserProbe(page);+ const resourceProbe = await createResourceProbe(page);+ /** TTS mounts a src-less <audio> per row whose error event logs a React+ * fiber dump through vite's console forwarding, which floods the+ * terminal and stalls the page under measurement. */+ await page.addInitScript((flat: boolean) => {+ localStorage.setItem('textToSpeech', 'false');+ localStorage.setItem('LC_FLAT_THREAD', flat ? 'true' : 'false');+ const original = console.error.bind(console);+ console.error = (...args: unknown[]) => {+ if (typeof args[0] === 'string' && args[0].startsWith('Error fetching audio')) {+ return;+ }+ original(...args);+ };+ }, variant.flat);+ console.log(`\n##### variant: ${variant.name} (react-scan ${WITH_SCAN ? 'on' : 'off'})`);++ /** The probe's page global comes from an init script, so it can only start+ * once a document exists: the load phase's CDP totals begin at the load+ * event, after the first commit. */+ await page.goto(`/c/${CONVO.id}`, { timeout: 180_000 });+ await probe.start();+ await resourceProbe.start();+ await expect(heading(page, turnHeading(CONVO.label, 1))).toBeAttached({ timeout: 120_000 });+ await expect(heading(page, turnHeading(CONVO.label, TURNS))).toBeAttached({+ timeout: 120_000,+ });+ await page.waitForTimeout(1000);+ const load = await snapshotPerf(page);+ report(+ 'load (all rows mounted)',+ load,+ await probe.finish(),+ { rows: ROWS },+ await resourceProbe.sample(),+ );+ await attachSnapshot(testInfo, 'load.json', load, { rows: ROWS });++ await resetPerf(page);+ await probe.start();+ await resourceProbe.start();+ await sendMessage(page, 'E2E_SLOW_REPLY:tree');+ 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(500);+ const stream = await snapshotPerf(page);+ const streamBrowser = await probe.finish();+ const streamResources = await resourceProbe.sample();+ const flushes = stream.renders['ContentRender']?.count ?? 0;+ const multi = stream.renders['MultiMessage']?.count ?? 0;+ report(+ 'stream (160 chunks @35ms into a ' + ROWS + '-row thread)',+ stream,+ streamBrowser,+ {+ 'flushes(ContentRender renders)': flushes,+ 'MultiMessage renders per flush': flushes > 0 ? Math.round((multi / flushes) * 10) / 10 : 0,+ },+ streamResources,+ );+ await attachSnapshot(testInfo, 'stream.json', stream, { rows: ROWS + 2, flushes });+ if (WITH_SCAN) {+ expect(flushes).toBeGreaterThan(5);+ }++ await resetPerf(page);+ await probe.start();+ await resourceProbe.start();+ await clickSibling(page, 'Previous sibling message', 'last');+ await expect(heading(page, altHeading(CONVO.label, TURNS))).toBeVisible({ timeout: 30_000 });+ await page.waitForTimeout(500);+ const leafPrev = await snapshotPerf(page);+ report(+ 'switch leaf -> alternate (drops 2 rows)',+ leafPrev,+ await probe.finish(),+ {},+ await resourceProbe.sample(),+ );+ await attachSnapshot(testInfo, 'switch-leaf-prev.json', leafPrev, {});++ await resetPerf(page);+ await probe.start();+ await resourceProbe.start();+ await clickSibling(page, 'Next sibling message', 'last');+ await expect(heading(page, turnHeading(CONVO.label, TURNS))).toBeVisible({ timeout: 30_000 });+ await page.waitForTimeout(500);+ const leafNext = await snapshotPerf(page);+ report(+ 'switch leaf -> spine (restores 2 rows)',+ leafNext,+ await probe.finish(),+ {},+ await resourceProbe.sample(),+ );+ await attachSnapshot(testInfo, 'switch-leaf-next.json', leafNext, {});++ await resetPerf(page);+ await probe.start();+ await resourceProbe.start();+ await clickSibling(page, 'Previous sibling message', 'first');+ await expect(heading(page, altHeading(CONVO.label, 3))).toBeVisible({ timeout: 30_000 });+ await page.waitForTimeout(500);+ const shallowPrev = await snapshotPerf(page);+ report(+ `switch turn ${SHALLOW_BRANCH_TURN} -> alternate (drops ~${DEEP_SWITCH_ROWS} rows)`,+ shallowPrev,+ await probe.finish(),+ {},+ await resourceProbe.sample(),+ );+ await attachSnapshot(testInfo, 'switch-shallow-prev.json', shallowPrev, {});++ await resetPerf(page);+ await probe.start();+ await resourceProbe.start();+ await clickSibling(page, 'Next sibling message', 'first');+ await expect(heading(page, turnHeading(CONVO.label, TURNS))).toBeAttached({+ timeout: 60_000,+ });+ await page.waitForTimeout(1000);+ const shallowNext = await snapshotPerf(page);+ report(+ `switch turn ${SHALLOW_BRANCH_TURN} -> spine (remounts ~${DEEP_SWITCH_ROWS} rows)`,+ shallowNext,+ await probe.finish(),+ {},+ await resourceProbe.sample(),+ );+ await attachSnapshot(testInfo, 'switch-shallow-next.json', shallowNext, {});+ }+}diff --git a/e2e/playwright.config.tree-parity.ts b/e2e/playwright.config.tree-parity.tsnew file mode 100644index 0000000000..19bbc78f3e--- /dev/null+++ b/e2e/playwright.config.tree-parity.ts@@ -0,0 +1,20 @@+import { defineConfig } from '@playwright/test';+/** The perf config snapshots `process.env` into each server's env at import+ * time, so the flag has to be in place before that import is evaluated. */+import './benchmarks-tree/flatenv';+import treePerfConfig from './playwright.config.tree-perf';++/**+ * Runs the branch-sensitive mock specs against the vite dev server with the+ * flat thread renderer defaulted ON, so the prototype is checked against the+ * same scenarios the recursive renderer passes.+ */++export default defineConfig({+ ...treePerfConfig,+ testDir: 'specs/mock',+ testMatch: process.env.TREE_PARITY_MATCH+ ? new RegExp(process.env.TREE_PARITY_MATCH)+ : /(message-tree|thread-fold|chat|hover-actions)\.spec\.ts$/,+ outputDir: 'specs/.test-results',+});diff --git a/e2e/playwright.config.tree-perf.ts b/e2e/playwright.config.tree-perf.tsnew file mode 100644index 0000000000..858c945557--- /dev/null+++ b/e2e/playwright.config.tree-perf.ts@@ -0,0 +1,41 @@+import { defineConfig } from '@playwright/test';+import path from 'node:path';+import mockConfig from './playwright.config.mock';+import { getE2EServerAddress } from './setup/env';++/**+ * Message-tree render benchmark config. Runs against the vite dev server so+ * react-scan sees component names (the production minifier strips them).+ */+const rootPath = path.resolve(__dirname, '..');+const { host: backendHost, port: backendPort } = getE2EServerAddress();+const devHost = backendHost.includes(':') ? `[${backendHost}]` : backendHost;+const DEV_PORT = process.env.E2E_DEV_PORT || '3090';+const DEV_SERVER_URL = `http://${devHost}:${DEV_PORT}`;++const mockServers = [mockConfig.webServer ?? []].flat();++export default defineConfig({+ ...mockConfig,+ testDir: 'benchmarks-tree',+ outputDir: 'benchmarks-tree/.test-results',+ timeout: 10 * 60 * 1000,+ retries: 0,+ reporter: [['line']],+ use: {+ ...mockConfig.use,+ baseURL: DEV_SERVER_URL,+ },+ webServer: [+ ...mockServers,+ {+ command: 'npm run frontend:dev',+ cwd: rootPath,+ env: { ...process.env, PORT: DEV_PORT, HOST: backendHost, BACKEND_PORT: backendPort },+ url: DEV_SERVER_URL,+ stdout: 'pipe',+ timeout: 180_000,+ reuseExistingServer: false,+ },+ ],+});