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
🧵 refactor: Read the Pane Conversation Through ChatSettings in Stream Resume (#17420)
Marco Berettabf45d5653a26
diff --git a/client/src/Providers/ChatSettingsContext.tsx b/client/src/Providers/ChatSettingsContext.tsxindex e299f1c3e5..6a33a29821 100644--- a/client/src/Providers/ChatSettingsContext.tsx+++ b/client/src/Providers/ChatSettingsContext.tsx@@ -14,6 +14,7 @@ export const defaultChatSettings: ChatSettings = {config: { feedbackEnabled: false, canRenameRunningChat: false },auth: {},getPaneConversations: () => [],+ usePaneConversation: () => null,};export const ChatSettingsContext = createContext<ChatSettings>(defaultChatSettings);diff --git a/client/src/hooks/Chat/__tests__/transport.spec.tsx b/client/src/hooks/Chat/__tests__/transport.spec.tsxindex 1404342e93..f611e612b0 100644--- a/client/src/hooks/Chat/__tests__/transport.spec.tsx+++ b/client/src/hooks/Chat/__tests__/transport.spec.tsx@@ -34,6 +34,7 @@ import { ChatSettingsContext, defaultChatSettings } from '~/Providers/ChatSettinimport { ChatTransportContext } from '~/Providers/ChatTransportContext';import { ChatContext, useChatContext } from '~/Providers/ChatContext';import { startupConfigKey } from '~/data-provider/Endpoints/queries';+import { usePaneConversation } from '~/routes/ChatSettings';import useResumableSSE from '~/hooks/SSE/useResumableSSE';import useResumeOnLoad from '~/hooks/SSE/useResumeOnLoad';import useChatHelpers from '~/hooks/Chat/useChatHelpers';@@ -92,6 +93,7 @@ function createFakeTransport(overrides: Partial<Transport> = {}) {const chatSettings = {...defaultChatSettings,+ usePaneConversation,auth: { token: 'test-token', isAuthenticated: true },};diff --git a/client/src/hooks/Chat/contract.ts b/client/src/hooks/Chat/contract.tsindex e92a604934..20b73dd2d9 100644--- a/client/src/hooks/Chat/contract.ts+++ b/client/src/hooks/Chat/contract.ts@@ -237,6 +237,9 @@ export type ChatSettings = {/** The conversation each mounted chat pane shows, read when called rather than at render:* steer recovery asks whether any pane holding a conversation is still running. */getPaneConversations: () => ReadonlyArray<PaneConversation>;+ /** A hook, stable across renders: the conversation a pane shows, subscribed so the reader+ * re-renders when the pane moves to another conversation. `null` before the pane has one. */+ usePaneConversation: (index: string | number) => TConversation | null;};/** One mounted chat pane and the conversation it shows. */diff --git a/client/src/hooks/Chat/useChatFunctions.ts b/client/src/hooks/Chat/useChatFunctions.tsindex 9731c4b7a2..eb71a65841 100644--- a/client/src/hooks/Chat/useChatFunctions.ts+++ b/client/src/hooks/Chat/useChatFunctions.ts@@ -28,7 +28,7 @@ import type {TConversation,TEndpointsConfig,} from 'librechat-data-provider';-import type { SetterOrUpdater } from 'recoil';+import type { Dispatch, SetStateAction } from 'react';import type { TAskFunction, ExtendedFile } from '~/common';import {logger,@@ -82,11 +82,11 @@ export default function useChatFunctions({getMessages: (conversationId?: string | null) => TMessage[] | undefined;setMessages: (messages: TMessage[]) => void;files?: Map<string, ExtendedFile>;- setFiles?: SetterOrUpdater<Map<string, ExtendedFile>>;- setSubmission: SetterOrUpdater<TSubmission | null>;+ setFiles?: Dispatch<SetStateAction<Map<string, ExtendedFile>>>;+ setSubmission: Dispatch<SetStateAction<TSubmission | null>>;/** Supplied by the host that owns the conversation atom, so a send records the decision it* established without this hook becoming a second writer of that state. */- setConversation: SetterOrUpdater<TConversation | null>;+ setConversation: Dispatch<SetStateAction<TConversation | null>>;}) {const navigate = useNavigate();const reasoningStore = useStore();diff --git a/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx b/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsxindex 3679b3c4c1..ea492edaf9 100644--- a/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx+++ b/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx@@ -32,6 +32,7 @@ import { ChatSettingsContext, defaultChatSettings } from '~/Providers/ChatSettinimport { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state';import { pendingApprovalActionFamily } from '~/components/Chat/approval/state';import { agentQueuedTurnsQueryKey } from '~/data-provider/SSE/queuedTurns';+import { usePaneConversation } from '~/routes/ChatSettings';import { SteerHostTestProvider } from 'test/steerHost';import useResumeOnLoad from '../useResumeOnLoad';import store from '~/store';@@ -219,6 +220,7 @@ function renderUseResumeOnLoad({<ChatSettingsContext.Providervalue={{...defaultChatSettings,+ usePaneConversation,config: {...defaultChatSettings.config,retentionMode: mockStartupConfig?.interface?.retentionMode,diff --git a/client/src/hooks/SSE/handlers.ts b/client/src/hooks/SSE/handlers.tsindex 6695fa51b2..19fa4833ef 100644--- a/client/src/hooks/SSE/handlers.ts+++ b/client/src/hooks/SSE/handlers.ts@@ -2,7 +2,6 @@ import type { TMessage, EventSubmission } from 'librechat-data-provider';import type { Location, NavigateFunction } from 'react-router-dom';import type { QueryClient } from '@tanstack/react-query';import type { MutableRefObject } from 'react';-import type { SetterOrUpdater } from 'recoil';import type useFocusRegeneratedResponse from '~/hooks/Chat/useFocusRegeneratedResponse';import type useAttachmentHandler from './useAttachmentHandler';import type { EventHandlerParams } from './useEventHandlers';@@ -23,7 +22,6 @@ export type EventHandlerContext = Required<| 'setShowStopButton'> & {queryClient: QueryClient;- setAbortScroll: SetterOrUpdater<boolean>;/** Clears the elapsed anchor on every terminal path. */setSubmissionStart: (start: number | null) => void;announcePolite: ReturnType<typeof useLiveAnnouncer>['announcePolite'];diff --git a/client/src/hooks/SSE/opening.ts b/client/src/hooks/SSE/opening.tsindex 46a5d4075e..9f9e1385fa 100644--- a/client/src/hooks/SSE/opening.ts+++ b/client/src/hooks/SSE/opening.ts@@ -70,7 +70,6 @@ export default function useTurnHandlers(ctx: EventHandlerContext) {runIndex,announcePolite,lastAnnouncementTimeRef,- setAbortScroll,applyAgentTemplate,focusRegeneratedResponse,location,@@ -289,12 +288,11 @@ export default function useTurnHandlers(ctx: EventHandlerContext) {});}- scrollToEnd(() => setAbortScroll(false));+ scrollToEnd();},[setMessages,queryClient,- setAbortScroll,isAddedRequest,announcePolite,setConversation,diff --git a/client/src/hooks/SSE/useEventHandlers.ts b/client/src/hooks/SSE/useEventHandlers.tsindex c44c75eaf0..b1ff0898e3 100644--- a/client/src/hooks/SSE/useEventHandlers.ts+++ b/client/src/hooks/SSE/useEventHandlers.ts@@ -1,5 +1,4 @@import { useCallback, useEffect, useRef } from 'react';-import { useSetRecoilState } from 'recoil';import { useStore, useSetAtom } from 'jotai';import { QueryKeys } from 'librechat-data-provider';import { buildRecoveryPreset } from '@librechat/chat';@@ -12,7 +11,6 @@ import {pendingReasoningOverrideFamily,} from '@librechat/chat/react';import type { TMessage, TConversation, EventSubmission } from 'librechat-data-provider';-import type { SetterOrUpdater } from 'recoil';import type { EventHandlerContext } from './handlers';import type { ConvoGenerator } from '~/common';import useFocusRegeneratedResponse from '~/hooks/Chat/useFocusRegeneratedResponse';@@ -27,7 +25,6 @@ import { useLiveAnnouncer } from '~/Providers';import useTurnHandlers from './opening';import useAbortHandlers from './abort';import useFinalHandler from './final';-import store from '~/store';/** Skill caches refreshed when a chat turn authors a skill via `create_file`/`edit_file`. */const SKILL_QUERY_KEYS = [@@ -45,10 +42,10 @@ export type EventHandlerParams = {setCompleted: React.Dispatch<React.SetStateAction<Set<unknown>>>;setMessages: (messages: TMessage[]) => void;getMessages: () => TMessage[] | undefined;- setIsSubmitting: SetterOrUpdater<boolean>;- setConversation?: SetterOrUpdater<TConversation | null>;+ setIsSubmitting: React.Dispatch<React.SetStateAction<boolean>>;+ setConversation?: React.Dispatch<React.SetStateAction<TConversation | null>>;newConversation?: ConvoGenerator;- setShowStopButton: SetterOrUpdater<boolean>;+ setShowStopButton: React.Dispatch<React.SetStateAction<boolean>>;};export default function useEventHandlers({@@ -66,7 +63,6 @@ export default function useEventHandlers({const reasoningStore = useStore();const { announcePolite } = useLiveAnnouncer();const applyAgentTemplate = useApplyAgentTemplate();- const setAbortScroll = useSetRecoilState(store.abortScroll);/** Cleared on every terminal path below: the elapsed anchor must not outlive* its generation, or a later externally-started run attached at this index* would inherit a stale baseline. Navigation teardown deliberately does not@@ -234,7 +230,6 @@ export default function useEventHandlers({setConversation,setIsSubmitting,setShowStopButton,- setAbortScroll,setSubmissionStart,announcePolite,lastAnnouncementTimeRef,diff --git a/client/src/hooks/SSE/useResumeOnLoad.ts b/client/src/hooks/SSE/useResumeOnLoad.tsindex 0cbabd3f4b..2adfe3ebca 100644--- a/client/src/hooks/SSE/useResumeOnLoad.ts+++ b/client/src/hooks/SSE/useResumeOnLoad.ts@@ -1,5 +1,4 @@import { useCallback, useEffect, useRef, useState } from 'react';-import { useRecoilValue } from 'recoil';import { useQueryClient } from '@tanstack/react-query';import { useStore, useSetAtom, useAtomValue } from 'jotai';import {@@ -33,7 +32,6 @@ import { useStreamStatus } from '~/data-provider';import { settleFinishedRun } from './finished';import useOwedSuccessor from './successor';import useSteerState from './steer';-import store from '~/store';/*** Hook to resume streaming if navigating to a conversation with active generation.@@ -61,12 +59,12 @@ export default function useResumeOnLoad(const attachedGenerationCreatedAt = useAtomValue(activeGenerationCreatedAtByConvoId(conversationId ?? ''),);- const currentConversation = useRecoilValue(store.conversationByIndex(runIndex));+ const { config, usePaneConversation } = useChatSettings();+ const currentConversation = usePaneConversation(runIndex);const endpoint = currentConversation?.endpoint;const endpointType = currentConversation?.endpointType;const actualEndpoint = endpointType ?? endpoint;const resumableEnabled = !isAssistantsEndpoint(actualEndpoint);- const { config } = useChatSettings();const startupConfigSettled = config.loaded !== false;const isRetentionForced = isForcedTemporaryRetention(config.retentionMode);// Track conversations we've already processed (either resumed or skipped)diff --git a/client/src/hooks/__tests__/useNewConvo.spec.ts b/client/src/hooks/__tests__/useNewConvo.spec.tsindex 1b9fd91e6f..a94079b8f9 100644--- a/client/src/hooks/__tests__/useNewConvo.spec.ts+++ b/client/src/hooks/__tests__/useNewConvo.spec.ts@@ -502,7 +502,6 @@ describe('useNewConvo reset cleanup', () => {setCompleted: jest.fn(),setIsSubmitting: jest.fn(),setShowStopButton: jest.fn(),- setAbortScroll: jest.fn(),setSubmissionStart: jest.fn(),announcePolite: jest.fn(),lastAnnouncementTimeRef: { current: 0 },diff --git a/client/src/routes/ChatSettings.tsx b/client/src/routes/ChatSettings.tsxindex a291c13fff..1b6880bd7e 100644--- a/client/src/routes/ChatSettings.tsx+++ b/client/src/routes/ChatSettings.tsx@@ -29,6 +29,11 @@ export function usePaneConversations(): ChatSettings['getPaneConversations'] {);}+/** Subscribes to the conversation a pane shows in the app's pane store. */+export function usePaneConversation(index: string | number) {+ return useRecoilValue(store.conversationByIndex(index));+}+/** Supplies the chat's app-global preferences from the app's own settings store, the deployment* config and signed-in account from the app's queries, and the transport its turns run over. */export default function ChatSettingsProvider({ children }: { children: ReactNode }) {@@ -74,6 +79,7 @@ export default function ChatSettingsProvider({ children }: { children: ReactNodeconfig,auth,getPaneConversations,+ usePaneConversation,}),[duringRunDefaultAction,diff --git a/client/src/store/settings.ts b/client/src/store/settings.tsindex c425fb96f5..8168e5272f 100644--- a/client/src/store/settings.ts+++ b/client/src/store/settings.ts@@ -25,7 +25,6 @@ const normalizeSavedSpeechEngine = (// Static atoms without localStorageconst staticAtoms = {- abortScroll: atom<boolean>({ key: 'abortScroll', default: false }),optionSettings: atom<TOptionSettings>({ key: 'optionSettings', default: {} }),currentSettingsView: atom<SettingsViews>({key: 'currentSettingsView',diff --git a/e2e/specs/mock/scenarios/pane-resume.spec.ts b/e2e/specs/mock/scenarios/pane-resume.spec.tsnew file mode 100644index 0000000000..e9580969f8--- /dev/null+++ b/e2e/specs/mock/scenarios/pane-resume.spec.ts@@ -0,0 +1,56 @@+import { expect, test } from '@playwright/test';+import {+ MOCK_ENDPOINTS,+ NEW_CHAT_PATH,+ messagesView,+ replyPrompt,+ replyText,+ selectMockEndpoint,+ sendMessage,+} from '../helpers';++/** The fake model's resume reply: 240 chunks 60ms apart, about 14 seconds. */+const LONG_REPLY_MARKER = 'E2E_RESUME_ICON_REPLY';+const LONG_REPLY_LAST_CHUNK = 'chunk-239';++/**+ * Whether a chat can reattach to its run depends on the conversation its pane shows, which the+ * host supplies through the chat settings. Leaving through the router keeps the app mounted, so+ * the pane moves to another conversation and back without a reload.+ */+test.describe('pane conversation on return', () => {+ test('returning to a running chat through the router shows the reply finish @scenario:a-chat-left-mid-reply-finishes-on-return', async ({+ page,+ }) => {+ test.setTimeout(120_000);+ const label = `pane-resume-${Date.now()}`;++ await page.goto(NEW_CHAT_PATH, { timeout: 10_000 });+ await selectMockEndpoint(page, MOCK_ENDPOINTS[0]);++ const setup = await sendMessage(page, replyPrompt(`${label}-setup`));+ expect(setup.ok()).toBeTruthy();+ await expect(messagesView(page).getByText(replyText(`${label}-setup`))).toBeVisible({+ timeout: 30_000,+ });+ await expect(page).toHaveURL(/\/c\/[0-9a-fA-F-]{36}$/, { timeout: 15_000 });+ const conversationPath = new URL(page.url()).pathname;++ const response = await sendMessage(page, `${LONG_REPLY_MARKER}:${label}`);+ expect(response.ok()).toBeTruthy();+ await expect(messagesView(page).getByText('chunk-010')).toBeVisible({ timeout: 15_000 });++ await page.evaluate((path) => {+ window.history.pushState({}, '', path);+ window.dispatchEvent(new PopStateEvent('popstate'));+ }, NEW_CHAT_PATH);+ await expect(page).toHaveURL(/\/c\/new$/);+ await expect(messagesView(page).getByText('chunk-010')).toHaveCount(0);++ await page.goBack();+ await expect(page).toHaveURL(new RegExp(`${conversationPath}(\\?.*)?$`));+ const assistantMessage = messagesView(page).locator('.message-render').last();+ await expect(assistantMessage).toContainText(LONG_REPLY_LAST_CHUNK, { timeout: 90_000 });+ await expect(page.getByRole('button', { name: 'Stop generating' })).toBeHidden();+ });+});