🪝 refactor: Move the Chat Session Provider Into @librechat/chat (#17580)

* refactor: Move the Chat Session Provider Into @librechat/chat

* refactor: Run the Adaptive Stream From the Package Session
This commit is contained in:
Marco Beretta 2026-10-29 17:30:22 +01:00 • committed by GitHub
parent 36a438ad19
commit a5cd61ef6e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 385 additions and 173 deletions

View file

@ -1,17 +1,18 @@
import { useMemo } from 'react';
import { useAtomValue, useSetAtom } from 'jotai';
import { QueryKeys } from 'librechat-data-provider';
import { Constants, QueryKeys } from 'librechat-data-provider';
import { hashQueryKey, useQueryClient } from '@tanstack/react-query';
import type { ChatHost, ChatMessageCache, ChatMessagesQuery } from '@librechat/chat/react';
import type { QueryClient, QueryCacheNotifyEvent } from '@tanstack/react-query';
import type { ChatHost, ChatMessageCache } from '@librechat/chat/react';
import type { TMessage } from 'librechat-data-provider';
import { useLatestMessage, useLatestMessageId } from '~/hooks/Messages/useLatestMessage';
import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state';
import { useAbortStreamMutation, useGetMessagesByConvoId } from '~/data-provider';
import { useChatSettings } from '~/Providers/ChatSettingsContext';
import { messageAttachmentsMapAtom } from '~/hooks/SSE/atoms';
import { getMappingOptions } from '~/hooks/Chat/outcomes';
import { useAbortStreamMutation } from '~/data-provider';
import useTurnServices from '~/hooks/Chat/services';
import { useResumeOnLoad } from '~/hooks/SSE';
import useNewConvo from '~/hooks/useNewConvo';
/** Cache events that add, drop or rewrite a query's data. */
@ -72,6 +73,28 @@ function useMessageCache(): ChatMessageCache {
return useMemo(() => createMessageCache(queryClient), [queryClient]);
}
/** The conversation's messages query. Refetches stale caches on mount: navigation invalidates
* (not removes) messages, so a warm conversation renders instantly from cache and reconciles in
* the background instead of unmounting into a spinner. */
function useMessages(
conversationId: string | undefined,
{ isStreaming }: { isStreaming: boolean },
): ChatMessagesQuery {
const {
data = null,
isLoading,
isFetching,
} = useGetMessagesByConvoId(
conversationId ?? '',
{
enabled: !!conversationId && conversationId !== Constants.SEARCH,
refetchOnMount: true,
},
{ isStreaming },
);
return { messages: data, isLoading, isFetching };
}
/** The app's services behind `useChatHelpers`: its pane store, message query cache, turn
* services, thread selection, transport and settings. */
export const appChatHost: ChatHost = {
@ -88,6 +111,10 @@ export const appChatHost: ChatHost = {
useLiveAttachments: () => useAtomValue(messageAttachmentsMapAtom),
useFeedbackEnabled: () => useChatSettings().config.feedbackEnabled,
getMappingOptions,
useMessages,
useSessionEffects: ({ index, conversationId, chat, isSettled }) => {
useResumeOnLoad(conversationId, chat.getMessages, index, isSettled);
},
};
export { ChatHostProvider, useChatHost } from '@librechat/chat/react';

View file

@ -0,0 +1,96 @@
import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import { renderHook, waitFor } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { Constants, QueryKeys, dataService } from 'librechat-data-provider';
import type { ChatContract } from '@librechat/chat/react';
import type { TMessage } from 'librechat-data-provider';
import { appChatHost } from '../ChatHostContext';
const mockUseResumeOnLoad = jest.fn();
/** Resume owns the transport; the host only hands it the session's values. */
jest.mock('~/hooks/SSE', () => ({
useResumeOnLoad: (...args: unknown[]) => mockUseResumeOnLoad(...args),
}));
jest.mock('librechat-data-provider', () => {
const actual = jest.requireActual('librechat-data-provider');
return {
...actual,
dataService: { ...actual.dataService, getMessagesByConvoId: jest.fn() },
};
});
const getMessages = dataService.getMessagesByConvoId as jest.MockedFunction<
typeof dataService.getMessagesByConvoId
>;
const messages = [{ messageId: 'm-1', conversationId: 'convo-1' }] as TMessage[];
function renderMessages(queryClient: QueryClient, conversationId: string | undefined) {
const wrapper = ({ children }: { children: React.ReactNode }) => (
<MemoryRouter>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
</MemoryRouter>
);
return renderHook(() => appChatHost.useMessages(conversationId, { isStreaming: false }), {
wrapper,
});
}
describe('appChatHost.useMessages', () => {
beforeEach(() => {
getMessages.mockReset();
getMessages.mockResolvedValue(messages);
});
it('loads the conversation messages and reports when they settle', async () => {
const { result } = renderMessages(new QueryClient(), 'convo-1');
expect(result.current).toEqual({ messages: null, isLoading: true, isFetching: true });
await waitFor(() =>
expect(result.current).toEqual({ messages, isLoading: false, isFetching: false }),
);
expect(getMessages).toHaveBeenCalledWith('convo-1', { toolPreviews: true });
});
it('serves a warm cache at once while it revalidates in the background', async () => {
const queryClient = new QueryClient();
queryClient.setQueryData([QueryKeys.messages, 'convo-1'], messages);
queryClient.invalidateQueries({ queryKey: [QueryKeys.messages, 'convo-1'] });
const { result } = renderMessages(queryClient, 'convo-1');
expect(result.current).toEqual({ messages, isLoading: false, isFetching: true });
await waitFor(() => expect(result.current.isFetching).toBe(false));
});
it.each([
['no conversation', undefined],
['search', Constants.SEARCH as string],
])('loads nothing for %s', (_label, conversationId) => {
const { result } = renderMessages(new QueryClient(), conversationId);
expect(result.current.messages).toBeNull();
expect(result.current.isFetching).toBe(false);
expect(getMessages).not.toHaveBeenCalled();
});
});
describe('appChatHost.useSessionEffects', () => {
it('runs resume on load against the session contract', () => {
const chat = { getMessages: jest.fn() } as unknown as ChatContract;
renderHook(() =>
appChatHost.useSessionEffects({
index: 1,
conversationId: 'convo-1',
chat,
isSettled: true,
}),
);
expect(mockUseResumeOnLoad).toHaveBeenCalledWith('convo-1', chat.getMessages, 1, true);
});
});

View file

@ -8,8 +8,9 @@ import { Constants, buildTree } from 'librechat-data-provider';
import { QueuedTurnPortalProvider } from '@librechat/chat/components';
import {
submissionByIndex,
ComposerRestoreProvider,
composerLiftFamily,
ChatSessionProvider,
ComposerRestoreProvider,
} from '@librechat/chat/react';
import type { TChatProject, TMessage } from 'librechat-data-provider';
import type { ChatFormValues } from '~/common';
@ -21,7 +22,6 @@ import ApprovalProvider from './Messages/Content/ApprovalContext';
import ConversationStarters from './Input/ConversationStarters';
import { pendingApprovalActionFamily } from './approval/state';
import ProjectBadge from '~/components/Projects/ProjectBadge';
import { ChatSessionProvider } from '~/hooks/Chat/provider';
import { OwnerTextProvider } from './Messages/PrivateText';
import { AskAnswerHostProvider } from './ask/state';
import MessagesView from './Messages/MessagesView';

View file

@ -21,21 +21,29 @@ jest.mock('~/hooks/AuthContext', () => ({
}));
jest.mock('~/data-provider', () => ({
useGetMessagesByConvoId: () => ({ data: null, isLoading: false, isFetching: false }),
useProjectQuery: () => ({ data: undefined }),
}));
/**
* Heading selection only needs route params + conversation state. Stub the chat
* helper surface so the suite can inject matching vs stale IDs without standing
* up SSE, message trees, or full ChatRoute synchronization.
* Heading selection only needs route params + conversation state. Stub the session so the
* suite can inject matching vs stale IDs without standing up the chat host, SSE, message trees,
* or full ChatRoute synchronization.
*/
jest.mock('@librechat/chat/react', () => ({
...jest.requireActual('@librechat/chat/react'),
useChatHelpers: () => ({ conversation: mockConversation() }),
useQueueDrain: jest.fn(),
useQueuedTurnReveal: jest.fn(),
}));
jest.mock('@librechat/chat/react', () => {
const actual = jest.requireActual('@librechat/chat/react');
return {
...actual,
ChatSessionProvider: ({
children,
}: {
children: (session: { messages: null; isLoading: boolean }) => React.ReactNode;
}) => (
<actual.ChatContext.Provider value={{ conversation: mockConversation() }}>
{children({ messages: null, isLoading: false })}
</actual.ChatContext.Provider>
),
};
});
jest.mock('~/hooks', () => ({
useLocalize: () => (key: string) => (key === 'com_ui_new_chat' ? 'New chat' : key),
@ -43,11 +51,6 @@ jest.mock('~/hooks', () => ({
useScrollbarGutterSeed: jest.fn(),
}));
jest.mock('~/hooks/SSE', () => ({
useAdaptiveSSE: jest.fn(),
useResumeOnLoad: jest.fn(),
}));
jest.mock('../Presentation', () => ({
__esModule: true,
default: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,

View file

@ -27,16 +27,21 @@ jest.mock('librechat-data-provider', () => ({
buildTree: ({ messages }: { messages: unknown[] }) => messages,
}));
jest.mock('@librechat/chat/react', () => ({
...jest.requireActual('@librechat/chat/react'),
useChatHelpers: () => ({
conversation: mockConversation,
getMessages: jest.fn(),
ask: jest.fn(),
}),
useQueueDrain: jest.fn(),
useQueuedTurnReveal: jest.fn(),
}));
jest.mock('@librechat/chat/react', () => {
const actual = jest.requireActual('@librechat/chat/react');
return {
...actual,
ChatSessionProvider: ({
children,
}: {
children: (session: { messages: unknown[]; isLoading: boolean }) => React.ReactNode;
}) => (
<actual.ChatContext.Provider value={{ conversation: mockConversation }}>
{children({ messages: [{ messageId: 'message-1' }], isLoading: false })}
</actual.ChatContext.Provider>
),
};
});
jest.mock('~/hooks', () => ({
useAddedResponse: () => ({}),
@ -48,11 +53,6 @@ jest.mock('~/hooks/Chat/outcomes', () => ({
getMappingOptions: () => ({}),
}));
jest.mock('~/hooks/SSE', () => ({
useAdaptiveSSE: jest.fn(),
useResumeOnLoad: jest.fn(),
}));
jest.mock('~/Providers', () => {
const Passthrough = ({ children }: { children: React.ReactNode }) => <>{children}</>;
const Context = { Provider: Passthrough };
@ -67,11 +67,6 @@ jest.mock('~/Providers', () => {
jest.mock('~/data-provider', () => ({
useProjectQuery: () => ({ data: undefined }),
useGetMessagesByConvoId: () => ({
data: [{ messageId: 'message-1' }],
isLoading: false,
isFetching: false,
}),
}));
jest.mock('../Input/ConversationStarters', () => () => null);

View file

@ -1,97 +0,0 @@
import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import { renderHook } from '@testing-library/react';
import { useChatActions } from '@librechat/chat/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import type { TConversation } from 'librechat-data-provider';
import type { ChatContract } from '../contract';
import { ChatSessionProvider } from '../provider';
const mockUseChatHelpers = jest.fn();
const mockUseAdaptiveSSE = jest.fn();
const mockUseResumeOnLoad = jest.fn();
const mockUseQueueDrain = jest.fn();
const mockRevealQueuedTurn = jest.fn();
jest.mock('~/hooks/SSE', () => ({
useAdaptiveSSE: (...args: unknown[]) => mockUseAdaptiveSSE(...args),
useResumeOnLoad: (...args: unknown[]) => mockUseResumeOnLoad(...args),
}));
jest.mock('@librechat/chat/react', () => ({
...jest.requireActual('@librechat/chat/react'),
useChatHelpers: (index?: number, paramId?: string) => mockUseChatHelpers(index, paramId),
useQueuedTurnReveal: () => mockRevealQueuedTurn,
useQueueDrain: (...args: unknown[]) => mockUseQueueDrain(...args),
}));
const noop = () => undefined;
const contract: ChatContract = {
index: 0,
conversation: { conversationId: 'convo-1' } as TConversation,
setConversation: noop,
newConversation: noop,
preset: null,
setPreset: noop,
optionSettings: {},
setOptionSettings: noop,
getMessages: () => [],
messagesKey: 'convo-1',
setMessages: noop,
setSiblingIdx: noop,
latestMessageId: undefined,
latestMessageDepth: undefined,
subscribeMessages: () => () => undefined,
getMessagesRevision: () => 0,
useLiveAttachments: () => ({}),
getMappingOptions: () => ({}),
ask: jest.fn(),
regenerate: jest.fn(),
isSubmitting: false,
setIsSubmitting: noop,
handleRegenerate: noop,
handleContinue: noop,
stopGenerating: jest.fn(() => Promise.resolve()),
handleStopGenerating: noop,
abortScroll: false,
setAbortScroll: noop,
files: new Map(),
setFiles: noop,
filesLoading: false,
setFilesLoading: noop,
feedbackEnabled: false,
};
describe('ChatSessionProvider', () => {
beforeEach(() => {
jest.clearAllMocks();
mockUseChatHelpers.mockReturnValue(contract);
});
it('runs the stream, resume and queue drain against the contract it serves', () => {
const queryClient = new QueryClient();
const { result } = renderHook(() => useChatActions(), {
wrapper: ({ children }) => (
<MemoryRouter>
<QueryClientProvider client={queryClient}>
<ChatSessionProvider index={1} conversationId="convo-1">
{() => children}
</ChatSessionProvider>
</QueryClientProvider>
</MemoryRouter>
),
});
expect(mockUseChatHelpers).toHaveBeenCalledWith(1, 'convo-1');
expect(result.current.id).toBe('convo-1');
expect(mockUseAdaptiveSSE).toHaveBeenCalledWith(null, contract, false, 1);
expect(mockUseResumeOnLoad).toHaveBeenCalledWith('convo-1', contract.getMessages, 1, false);
expect(mockUseQueueDrain).toHaveBeenCalledWith(
1,
'convo-1',
contract.ask,
mockRevealQueuedTurn,
);
});
});

View file

@ -0,0 +1,160 @@
/**
* @jest-environment jsdom
*/
import { getDefaultStore } from 'jotai';
import { render } from '@testing-library/react';
import type { TConversation, TMessage, TSubmission } from 'librechat-data-provider';
import type { ChatHost, ChatMessagesQuery } from '../host';
import type { ChatContract } from '../contract';
import { submissionByIndex, clearSubmissions, isSubmittingFamily } from '../../submission';
import { ChatSessionProvider } from '../session';
import { useChatContext } from '../context';
import { ChatHostProvider } from '../host';
const mockUseChatHelpers = jest.fn();
const mockUseQueueDrain = jest.fn();
const mockUseAdaptiveSSE = jest.fn();
const mockRevealQueuedTurn = jest.fn();
/** The contract and the queue have specs of their own; here they only witness the order and
* arguments the session hands them. */
jest.mock('../helpers', () => ({
__esModule: true,
default: (index?: number, conversationId?: string) => mockUseChatHelpers(index, conversationId),
}));
jest.mock('../../steering/useQueuedTurnReveal', () => ({
__esModule: true,
default: () => mockRevealQueuedTurn,
}));
jest.mock('../../stream/adaptive', () => ({
__esModule: true,
default: (...args: unknown[]) => mockUseAdaptiveSSE(...args),
}));
jest.mock('../../steering/useQueueDrain', () => ({
__esModule: true,
default: (...args: unknown[]) => mockUseQueueDrain(...args),
}));
const contract = {
index: 1,
conversation: { conversationId: 'convo-1' } as TConversation,
getMessages: () => [],
ask: jest.fn(),
} as unknown as ChatContract;
function createHost(query: ChatMessagesQuery) {
const useMessages = jest.fn((): ChatMessagesQuery => query);
const useSessionEffects = jest.fn();
const host = { useMessages, useSessionEffects } as unknown as ChatHost;
return { host, useMessages, useSessionEffects };
}
function renderSession(host: ChatHost, pane = jest.fn(() => <Probe />)) {
render(
<ChatHostProvider host={host}>
<ChatSessionProvider index={1} conversationId="convo-1">
{pane}
</ChatSessionProvider>
</ChatHostProvider>,
);
return pane;
}
let served: ChatContract | null = null;
function Probe() {
served = useChatContext();
return null;
}
const messages = [{ messageId: 'm-1' }] as TMessage[];
describe('ChatSessionProvider', () => {
beforeEach(() => {
jest.clearAllMocks();
served = null;
mockUseChatHelpers.mockReturnValue(contract);
});
afterEach(() => {
clearSubmissions(getDefaultStore());
});
it('loads the messages through the host and hands them to the pane', () => {
const { host, useMessages } = createHost({ messages, isLoading: false, isFetching: false });
const pane = renderSession(host);
expect(useMessages).toHaveBeenCalledWith('convo-1', { isStreaming: false });
expect(pane).toHaveBeenLastCalledWith({ messages, isLoading: false });
});
it('asks the host for a streaming-aware query while the pane submits', () => {
getDefaultStore().set(isSubmittingFamily(1), true);
const { host, useMessages } = createHost({ messages, isLoading: false, isFetching: false });
renderSession(host);
expect(useMessages).toHaveBeenLastCalledWith('convo-1', { isStreaming: true });
getDefaultStore().set(isSubmittingFamily(1), false);
});
it('serves the contract and runs the stream, resume and queue drain against it', () => {
const submission = { conversation: {} } as TSubmission;
getDefaultStore().set(submissionByIndex(1), submission);
const { host, useSessionEffects } = createHost({
messages,
isLoading: false,
isFetching: false,
});
renderSession(host);
expect(mockUseChatHelpers).toHaveBeenCalledWith(1, 'convo-1');
expect(served).toBe(contract);
expect(mockUseAdaptiveSSE).toHaveBeenLastCalledWith(submission, contract, false, 1);
expect(useSessionEffects).toHaveBeenLastCalledWith({
index: 1,
conversationId: 'convo-1',
chat: contract,
isSettled: true,
});
expect(mockUseQueueDrain).toHaveBeenCalledWith(
1,
'convo-1',
contract.ask,
mockRevealQueuedTurn,
);
});
it.each([
['loading', { isLoading: true, isFetching: true }],
['revalidating a warm cache', { isLoading: false, isFetching: true }],
])('holds resume back while %s', (_label, state) => {
const { host, useSessionEffects } = createHost({ messages: null, ...state });
renderSession(host);
expect(useSessionEffects).toHaveBeenLastCalledWith(
expect.objectContaining({ isSettled: false }),
);
});
it('runs the stream and resume after the contract and before the drain', () => {
const order: string[] = [];
mockUseChatHelpers.mockImplementation(() => {
order.push('contract');
return contract;
});
mockUseQueueDrain.mockImplementation(() => order.push('drain'));
mockUseAdaptiveSSE.mockImplementation(() => order.push('stream'));
const { host, useMessages, useSessionEffects } = createHost({
messages,
isLoading: false,
isFetching: false,
});
useMessages.mockImplementation(() => {
order.push('messages');
return { messages, isLoading: false, isFetching: false };
});
useSessionEffects.mockImplementation(() => order.push('resume'));
renderSession(host);
expect(order.slice(0, 5)).toEqual(['messages', 'contract', 'stream', 'resume', 'drain']);
});
});

View file

@ -59,6 +59,8 @@ const createHost = (conversation: TConversation | null): ChatHost => ({
useLiveAttachments: () => noLiveAttachments,
useFeedbackEnabled: () => false,
getMappingOptions: () => ({}),
useMessages: () => ({ messages: null, isLoading: false, isFetching: false }),
useSessionEffects: noop,
});
const INDEX = 0;

View file

@ -140,6 +140,24 @@ export type ChatMessageCache = {
refetchStreamStatus: (conversationId: string) => void;
};
/** A conversation's messages as the app's data layer loads them. */
export type ChatMessagesQuery = {
messages: TMessage[] | null;
/** No data yet for this conversation. */
isLoading: boolean;
/** A load or background revalidation is in flight. */
isFetching: boolean;
};
/** What a pane's session hands its effects each render. */
export type ChatSessionEffectsParams = {
index: number;
conversationId?: string;
chat: ChatContract;
/** The messages finished loading and no revalidation is in flight. */
isSettled: boolean;
};
/**
* What `useChatHelpers` needs from the app that hosts it. Members named `use*` are hooks, called
* unconditionally by the chat hook, so the host can back each one with its own router, query
@ -167,6 +185,19 @@ export interface ChatHost {
useFeedbackEnabled(): boolean;
/** The app's tool outcome rules for viewing one message; see {@link ChatContract}. */
getMappingOptions(live: TAttachment[] | undefined): UIMappingOptions;
/** Loads a conversation's messages; `isStreaming` lets the query hold off refetching under a
* live stream. An absent id loads nothing. */
useMessages(
conversationId: string | undefined,
options: { isStreaming: boolean },
): ChatMessagesQuery;
/**
* Runs the pane's resume on load.
*
* @deprecated On arrival: a bridge to the app's `useResumeOnLoad` until it moves into this
* package (#17565), after which the session calls it itself and this member is removed.
*/
useSessionEffects(params: ChatSessionEffectsParams): void;
}
const ChatHostContext = createContext<ChatHost | null>(null);

View file

@ -1,5 +1,7 @@
export { useChat } from './chat';
export { ChatProvider } from './provider';
export { ChatSessionProvider } from './session';
export type { ChatSessionMessages } from './session';
export { default as useChatHelpers } from './helpers';
export { useAbortCleanup } from './abort';
export { getMessageCacheIds, getMessagesConversationId } from './cache';
@ -11,10 +13,12 @@ export type {
TurnServices,
ChatTurnsParams,
ChatMessageCache,
ChatMessagesQuery,
TurnCodeDecision,
AbortStreamParams,
AbortStreamResponse,
ChatPaneConversation,
ChatSessionEffectsParams,
} from './host';
export { useChatActions } from './actions';
export { getChatStatus, isEmptyContentPart } from './status';

View file

@ -1,17 +1,13 @@
import { useAtomValue } from 'jotai';
import { Constants } from 'librechat-data-provider';
import {
ChatContext,
useQueueDrain,
useChatHelpers,
useIsSubmitting,
submissionByIndex,
useQueuedTurnReveal,
} from '@librechat/chat/react';
import type { TMessage } from 'librechat-data-provider';
import type { ReactNode } from 'react';
import { useAdaptiveSSE, useResumeOnLoad } from '~/hooks/SSE';
import { useGetMessagesByConvoId } from '~/data-provider';
import { submissionByIndex, useIsSubmitting } from '../submission';
import useQueuedTurnReveal from '../steering/useQueuedTurnReveal';
import useQueueDrain from '../steering/useQueueDrain';
import useAdaptiveSSE from '../stream/adaptive';
import { ChatContext } from './context';
import useChatHelpers from './helpers';
import { useChatHost } from './host';
/** The pane's messages query, as `ChatSessionProvider` hands it to the pane it renders. */
export type ChatSessionMessages = {
@ -21,51 +17,46 @@ export type ChatSessionMessages = {
/**
* `ChatProvider` for the pane that owns the conversation's stream: it also loads the
* conversation's messages and runs the SSE connection, resume on load and the queued-turn drain
* conversation's messages and runs the stream, resume on load and the queued-turn drain
* against the contract it serves.
*
* Every session hook runs in this one component, in the order the pane has always run them:
* the messages query subscribes (and starts its mount refetch) before the contract and the
* session effects commit, and the queue drain commits after `useChatHelpers`, so `ask` has
* released its in-flight guard and sees the refetch before the drain can call it. The pane is
* a render function so it re-renders with every session render, as it did when it ran these
* hooks itself.
* a render function so it re-renders with every session render.
*/
export function ChatSessionProvider({
index = 0,
conversationId,
children,
}: {
/** The pane: `0` is the root pane, `1` the added (multi-convo) pane. */
index?: number;
/** The route's conversation id, which can run ahead of the pane's conversation. */
conversationId?: string;
children: (session: ChatSessionMessages) => ReactNode;
}) {
}): ReactNode {
const host = useChatHost();
const isSubmitting = useIsSubmitting(index);
const {
data: messages = null,
isLoading,
isFetching,
} = useGetMessagesByConvoId(
conversationId ?? '',
{
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
* reconciles in the background instead of unmounting into a spinner. */
refetchOnMount: true,
},
{ isStreaming: isSubmitting },
);
const { messages, isLoading, isFetching } = host.useMessages(conversationId, {
isStreaming: isSubmitting,
});
const chat = useChatHelpers(index, conversationId);
const rootSubmission = useAtomValue(submissionByIndex(index));
const submission = useAtomValue(submissionByIndex(index));
useAdaptiveSSE(rootSubmission, chat, false, index);
useAdaptiveSSE(submission, chat, false, index);
/** Auto-resume waits for messages to load AND the warm-cache background revalidation to
* settle: a stale invalidated cache mounts with isLoading false while the refetch is in
* flight, and resume must not build from (or race) it. */
useResumeOnLoad(conversationId, chat.getMessages, index, !isLoading && !isFetching);
/** Resume waits for messages to load AND the warm-cache background revalidation to settle:
* a stale invalidated cache mounts with isLoading false while the refetch is in flight, and
* resume must not build from (or race) it. */
host.useSessionEffects({
index,
conversationId,
chat,
isSettled: !isLoading && !isFetching,
});
/** Shows a server-owned queued follow-up as the next user turn as soon as its predecessor
* completes, ahead of the receipt and active-job polls. */