From e582fa3f90ebd54b63e1eca3a7af2f7225efbd14 Mon Sep 17 00:00:00 2001 From: Marco Beretta <81851188+berry-13@users.noreply.github.com> Date: Sun, 11 Oct 2026 23:04:34 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=AA=A2=20refactor:=20Move=20Stop=20Button?= =?UTF-8?q?,=20Abort=20Scroll=20and=20Submission=20Start=20State=20to=20Jo?= =?UTF-8?q?tai=20(#16968)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: Move Stop Button, Abort Scroll and Submission Start State to Jotai * fix: Clear the Elapsed Baseline With the Other Per-Pane Run State --- client/src/components/Chat/Input/ChatForm.tsx | 6 +- .../__tests__/ChatForm.queryParams.spec.tsx | 62 ++++++---- .../src/components/Chat/Messages/Elapsed.tsx | 8 +- .../Chat/Messages/__tests__/Elapsed.spec.tsx | 42 +++++-- .../useChatFunctions.regenerate.spec.tsx | 38 ++++-- client/src/hooks/Chat/useChatFunctions.ts | 7 +- client/src/hooks/Chat/useChatHelpers.ts | 5 +- .../Config/__tests__/useClearStates.spec.tsx | 38 ++++++ client/src/hooks/Config/useClearStates.ts | 10 +- .../useEventHandlers.cancel.spec.tsx | 2 +- .../SSE/__tests__/useResumableSSE.spec.ts | 21 ++-- .../SSE/__tests__/useResumeOnLoad.spec.tsx | 9 +- client/src/hooks/SSE/useEventHandlers.ts | 5 +- client/src/hooks/SSE/useResumableSSE.ts | 5 +- client/src/hooks/SSE/useResumeOnLoad.ts | 5 +- client/src/hooks/SSE/useSSE.ts | 7 +- client/src/store/__tests__/generation.spec.ts | 30 +++++ client/src/store/families.ts | 41 ------- client/src/store/generation.ts | 34 +++++ .../mock/scenarios/run-indicators.spec.ts | 116 ++++++++++++++++++ 20 files changed, 364 insertions(+), 127 deletions(-) create mode 100644 client/src/store/__tests__/generation.spec.ts create mode 100644 client/src/store/generation.ts create mode 100644 e2e/specs/mock/scenarios/run-indicators.spec.ts diff --git a/client/src/components/Chat/Input/ChatForm.tsx b/client/src/components/Chat/Input/ChatForm.tsx index 08a3ff9387..fd13848655 100644 --- a/client/src/components/Chat/Input/ChatForm.tsx +++ b/client/src/components/Chat/Input/ChatForm.tsx @@ -1,7 +1,8 @@ import { memo, useRef, useMemo, useEffect, useState, useCallback } from 'react'; +import { useAtom } from 'jotai'; +import { useRecoilValue } from 'recoil'; import { useWatch } from 'react-hook-form'; import { useNavigate } from 'react-router-dom'; -import { useRecoilState, useRecoilValue } from 'recoil'; import { composerSurfaceClasses, composerSurfaceShadow, @@ -60,6 +61,7 @@ import useAskAnswerMode from '~/hooks/Input/useAskAnswerMode'; import AskUserQuestionPopover from './AskUserQuestionPopover'; import useComposerItems from '~/hooks/Input/useComposerItems'; import useAttachTarget from '~/hooks/Input/useAttachTarget'; +import { showStopButtonByIndex } from '~/store/generation'; import Hints, { composerHintId } from './Composer/Hints'; import PastedTextDialog from './Files/PastedTextDialog'; import DuringRunSendButton from './DuringRunSendButton'; @@ -210,7 +212,7 @@ const ChatForm = memo(function ChatForm({ const maximizeChatSpace = useRecoilValue(store.maximizeChatSpace); const { isTemporary } = useChatSettings(); - const [showStopButton, setShowStopButton] = useRecoilState(store.showStopButtonByIndex(index)); + const [showStopButton, setShowStopButton] = useAtom(showStopButtonByIndex(index)); const plusPopoverAtom = useMemo(() => store.showPlusPopoverFamily(index), [index]); const mentionPopoverAtom = useMemo(() => store.showMentionPopoverFamily(index), [index]); diff --git a/client/src/components/Chat/Input/__tests__/ChatForm.queryParams.spec.tsx b/client/src/components/Chat/Input/__tests__/ChatForm.queryParams.spec.tsx index 856d8e92ef..c7afc15f62 100644 --- a/client/src/components/Chat/Input/__tests__/ChatForm.queryParams.spec.tsx +++ b/client/src/components/Chat/Input/__tests__/ChatForm.queryParams.spec.tsx @@ -3,6 +3,7 @@ import { DndProvider } from 'react-dnd'; import { useForm } from 'react-hook-form'; import { RecoilRoot, useRecoilState } from 'recoil'; import { HTML5Backend } from 'react-dnd-html5-backend'; +import { Provider as JotaiProvider, createStore } from 'jotai'; import { QueryKeys, EModelEndpoint } from 'librechat-data-provider'; import { act, render, screen, fireEvent } from '@testing-library/react'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; @@ -13,6 +14,7 @@ import type { ChatFormValues } from '~/common'; import { ChatTransportContext, defaultChatTransport } from '~/Providers/ChatTransportContext'; import { getNewConversationDraftId, getDraft, setDraft } from '~/utils/drafts'; import { ChatContext, ChatFormProvider } from '~/Providers'; +import { showStopButtonByIndex } from '~/store/generation'; import { AuthContextProvider } from '~/hooks/AuthContext'; import { startupConfigKey } from '~/data-provider'; import ChatForm from '../ChatForm'; @@ -162,34 +164,42 @@ function mountComposer( location = useLocation(); return null; } + const jotaiStore = createStore(); + if (liveRun) { + jotaiStore.set(showStopButtonByIndex(0), true); + } const wrapper = ({ children }: { children: React.ReactNode }) => ( - { - if (liveRun) { - set(store.isSubmittingFamily(0), true); - set(store.showStopButtonByIndex(0), true); - set(store.activeGenerationCreatedAtByConvoId(conversation.conversationId ?? ''), 1000); - set( - store.activeGenerationProtocolVersionByConvoId(conversation.conversationId ?? ''), - 2, - ); - } - if (speechSettingsInitialized) { - set(store.engineSTT, 'external'); - set(store.autoTranscribeAudio, false); - } - }} - > - - - - - {children} - - - - + + { + if (liveRun) { + set(store.isSubmittingFamily(0), true); + set( + store.activeGenerationCreatedAtByConvoId(conversation.conversationId ?? ''), + 1000, + ); + set( + store.activeGenerationProtocolVersionByConvoId(conversation.conversationId ?? ''), + 2, + ); + } + if (speechSettingsInitialized) { + set(store.engineSTT, 'external'); + set(store.autoTranscribeAudio, false); + } + }} + > + + + + + {children} + + + + + ); const view = render( diff --git a/client/src/components/Chat/Messages/Elapsed.tsx b/client/src/components/Chat/Messages/Elapsed.tsx index 01e9af3a2c..6f87ccd0a6 100644 --- a/client/src/components/Chat/Messages/Elapsed.tsx +++ b/client/src/components/Chat/Messages/Elapsed.tsx @@ -1,9 +1,9 @@ import { memo, useEffect, useState } from 'react'; -import { useRecoilValue } from 'recoil'; +import { useAtomValue } from 'jotai'; import { useTranslation } from 'react-i18next'; +import { submissionStartFamily } from '~/store/generation'; import { getElapsedDurationLabels } from '~/utils'; import { useLocalize } from '~/hooks'; -import store from '~/store'; const elapsedSeconds = (start: number): number => Math.max(0, Math.floor((Date.now() - start) / 1000)); @@ -49,7 +49,7 @@ export const shouldShowElapsed = ({ * settled state for the animation to misrepresent. */ const Elapsed = memo(function Elapsed({ index }: { index: number }) { - const submissionStart = useRecoilValue(store.submissionStartFamily(index)); + const submissionStart = useAtomValue(submissionStartFamily(index)); return ; }); @@ -76,7 +76,7 @@ export const ElapsedTimer = memo(function ElapsedTimer({ start: startAt }: { sta * that replace the timer sit behind the same `p-1.5`. Inline-start, so * the alignment holds in RTL. */ return ( - + diff --git a/client/src/components/Chat/Messages/__tests__/Elapsed.spec.tsx b/client/src/components/Chat/Messages/__tests__/Elapsed.spec.tsx index e83054aa3e..855a59e558 100644 --- a/client/src/components/Chat/Messages/__tests__/Elapsed.spec.tsx +++ b/client/src/components/Chat/Messages/__tests__/Elapsed.spec.tsx @@ -1,13 +1,24 @@ import React from 'react'; -import { RecoilRoot, type MutableSnapshot } from 'recoil'; +import { RecoilRoot } from 'recoil'; +import { Provider, createStore } from 'jotai'; import { render, screen, act } from '@testing-library/react'; import Elapsed, { shouldShowElapsed } from '~/components/Chat/Messages/Elapsed'; -import store from '~/store'; +import { submissionStartFamily } from '~/store/generation'; -function renderElapsed(initializeState?: (snapshot: MutableSnapshot) => void) { +function storeWithStart(start?: number) { + const jotaiStore = createStore(); + if (start != null) { + jotaiStore.set(submissionStartFamily(0), start); + } + return jotaiStore; +} + +function renderElapsed(start?: number) { return render( - - + + + + , ); } @@ -29,7 +40,7 @@ describe('Elapsed', () => { it('renders seconds from the submission start anchor and rolls into minutes', () => { const start = Date.now() - 5_000; - renderElapsed(({ set }) => set(store.submissionStartFamily(0), start)); + renderElapsed(start); expect(screen.getByTestId('stream-elapsed')).toHaveTextContent(/^5s$/); expect(screen.getByTestId('stream-elapsed')).toHaveAttribute('aria-hidden', 'true'); @@ -66,7 +77,7 @@ describe('Elapsed', () => { it('clamps a future anchor to zero instead of going negative', () => { const start = Date.now() + 60_000; - renderElapsed(({ set }) => set(store.submissionStartFamily(0), start)); + renderElapsed(start); expect(screen.getByTestId('stream-elapsed')).toHaveTextContent(/^0s$/); @@ -76,25 +87,30 @@ describe('Elapsed', () => { it('continues from the anchored start across an unmount and remount', () => { const start = Date.now() - 30_000; + const jotaiStore = storeWithStart(start); const view = render( - set(store.submissionStartFamily(0), start)}> - + + + + , ); expect(screen.getByTestId('stream-elapsed')).toHaveTextContent(/^30s$/); view.rerender( - set(store.submissionStartFamily(0), start)}> - {null} + + {null} , ); expect(screen.queryByTestId('stream-elapsed')).toBeNull(); advance(5_000); view.rerender( - set(store.submissionStartFamily(0), start)}> - + + + + , ); expect(screen.getByTestId('stream-elapsed')).toHaveTextContent(/^35s$/); diff --git a/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx b/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx index 0a524a9a51..7286d26b9a 100644 --- a/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx +++ b/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx @@ -24,12 +24,12 @@ import { pendingUsageFamily, } from '~/components/Chat/Input/TokenUsage/store'; import { pendingReasoningOverrideFamily } from '~/components/Chat/Input/Composer/state'; +import { submissionStartFamily, showStopButtonByIndex } from '~/store/generation'; import { revealedQueuedTurnFamily } from '~/store/steer'; import useChatFunctions from '../useChatFunctions'; import { isPasteSubmitted } from '~/utils'; const mockNavigate = jest.fn(); -const mockSetShowStopButton = jest.fn(); const mockSetIsSubmitting = jest.fn(); const mockGetEphemeralAgent = jest.fn((): TEphemeralAgent | null => null); const mockSetFilesToDelete = jest.fn(); @@ -86,7 +86,7 @@ jest.mock('@tanstack/react-query', () => ({ jest.mock('recoil', () => ({ useRecoilValue: () => false, useSetRecoilState: (atom: unknown) => - String(atom).includes('isSubmitting') ? mockSetIsSubmitting : mockSetShowStopButton, + String(atom).includes('isSubmitting') ? mockSetIsSubmitting : jest.fn(), useRecoilCallback: (factory: any) => factory({ snapshot: { @@ -124,8 +124,6 @@ jest.mock('~/store', () => ({ __esModule: true, default: { isSubmittingFamily: () => 'isSubmitting', - submissionStartFamily: () => 'submissionStart', - showStopButtonByIndex: () => 'showStopButton', pendingManualSkillsByConvoId: () => 'pendingManualSkills', pendingQuotesByConvoId: () => 'pendingQuotes', messagesSiblingIdxFamily: () => 'messagesSiblingIdx', @@ -479,7 +477,12 @@ describe('useChatFunctions ask', () => { }); it('synchronously reports a refusal while another submit is in flight', () => { - const { result, setMessages, setSubmission } = renderAsk([], 'conversation-1', { + const { + result, + setMessages, + setSubmission, + reasoningStore: store, + } = renderAsk([], 'conversation-1', { isSubmitting: true, }); @@ -491,7 +494,20 @@ describe('useChatFunctions ask', () => { expect(askResult!).toBe(false); expect(setMessages).not.toHaveBeenCalled(); expect(setSubmission).not.toHaveBeenCalled(); - expect(mockSetShowStopButton).not.toHaveBeenCalled(); + expect(store.get(showStopButtonByIndex(0))).toBe(false); + expect(store.get(submissionStartFamily(0))).toBeNull(); + }); + + it('shows the stop button and stamps the elapsed baseline for an accepted submit', () => { + const { result, reasoningStore: store } = renderAsk([]); + const before = Date.now(); + + act(() => { + result.current.ask({ text: 'first turn', conversationId: 'conversation-1' }); + }); + + expect(store.get(showStopButtonByIndex(0))).toBe(true); + expect(store.get(submissionStartFamily(0))).toBeGreaterThanOrEqual(before); }); it('refuses a second submit fired in the same task, before isSubmitting commits', () => { @@ -529,7 +545,12 @@ describe('useChatFunctions ask', () => { }); it('reports a refusal when no endpoint is available', () => { - const { result, setMessages, setSubmission } = renderAsk([], 'conversation-1', { + const { + result, + setMessages, + setSubmission, + reasoningStore: store, + } = renderAsk([], 'conversation-1', { endpoint: null, }); @@ -541,7 +562,8 @@ describe('useChatFunctions ask', () => { expect(askResult!).toBe(false); expect(setMessages).not.toHaveBeenCalled(); expect(setSubmission).not.toHaveBeenCalled(); - expect(mockSetShowStopButton).not.toHaveBeenCalled(); + expect(store.get(showStopButtonByIndex(0))).toBe(false); + expect(store.get(submissionStartFamily(0))).toBeNull(); }); it.each([ diff --git a/client/src/hooks/Chat/useChatFunctions.ts b/client/src/hooks/Chat/useChatFunctions.ts index 8623958552..2e0dfed94c 100644 --- a/client/src/hooks/Chat/useChatFunctions.ts +++ b/client/src/hooks/Chat/useChatFunctions.ts @@ -1,7 +1,7 @@ import { useCallback, useEffect, useRef } from 'react'; import { v4 } from 'uuid'; -import { useStore } from 'jotai'; import { cloneDeep } from 'lodash'; +import { useStore, useSetAtom } from 'jotai'; import { useNavigate } from 'react-router-dom'; import { useQueryClient } from '@tanstack/react-query'; import { useSetRecoilState, useRecoilValue, useRecoilCallback } from 'recoil'; @@ -54,6 +54,7 @@ import { } from '~/hooks/Agents/codeDecision'; import { activeUsageResponseIdFamily } from '~/components/Chat/Input/TokenUsage/store'; import useFocusRegeneratedResponse from '~/hooks/Chat/useFocusRegeneratedResponse'; +import { submissionStartFamily, showStopButtonByIndex } from '~/store/generation'; import { usesReasoningCapabilities } from '~/hooks/Endpoint/useModelReasoning'; import useGetConversation from '~/hooks/Conversations/useGetConversation'; import useCodeApprovalMode from '~/hooks/Agents/useCodeApprovalMode'; @@ -255,8 +256,8 @@ export default function useChatFunctions({ const { isTemporary, config, auth } = useChatSettings(); const { getExpiry } = useUserKey(immutableConversation?.endpoint ?? ''); const setIsSubmitting = useSetRecoilState(store.isSubmittingFamily(index)); - const setSubmissionStart = useSetRecoilState(store.submissionStartFamily(index)); - const setShowStopButton = useSetRecoilState(store.showStopButtonByIndex(index)); + const setSubmissionStart = useSetAtom(submissionStartFamily(index)); + const setShowStopButton = useSetAtom(showStopButtonByIndex(index)); const focusRegeneratedResponse = useFocusRegeneratedResponse(); const jotaiStore = useStore(); const getConversation = useGetConversation(index); diff --git a/client/src/hooks/Chat/useChatHelpers.ts b/client/src/hooks/Chat/useChatHelpers.ts index 34c3d9ec21..234757d866 100644 --- a/client/src/hooks/Chat/useChatHelpers.ts +++ b/client/src/hooks/Chat/useChatHelpers.ts @@ -1,5 +1,5 @@ import { useCallback, useMemo, useRef, useState } from 'react'; -import { useSetAtom, useStore } from 'jotai'; +import { useAtom, useSetAtom, useStore } from 'jotai'; import { useQueryClient } from '@tanstack/react-query'; import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil'; import { Constants, QueryKeys, isAssistantsEndpoint } from 'librechat-data-provider'; @@ -12,6 +12,7 @@ import { drainAfterAbortByIndex, runEndByIndex } from '~/hooks/Chat/queue'; import { useChatSettings } from '~/Providers/ChatSettingsContext'; import useChatFunctions from '~/hooks/Chat/useChatFunctions'; import useSteerConvert from '~/hooks/Chat/useSteerConvert'; +import { abortScrollFamily } from '~/store/generation'; import { resolveAbortSteerTarget } from '~/utils'; import useNewConvo from '~/hooks/useNewConvo'; import { getMessageCacheIds } from './cache'; @@ -384,7 +385,7 @@ export default function useChatHelpers(index = 0, paramId?: string): ChatContrac const [preset, setPreset] = useRecoilState(store.presetByIndex(index)); const [showPopover, setShowPopover] = useRecoilState(store.showPopoverFamily(index)); - const [abortScroll, setAbortScroll] = useRecoilState(store.abortScrollFamily(index)); + const [abortScroll, setAbortScroll] = useAtom(abortScrollFamily(index)); const [optionSettings, setOptionSettings] = useRecoilState(store.optionSettingsFamily(index)); /** Read once per chat rather than per message row: message rows never unmount, so a diff --git a/client/src/hooks/Config/__tests__/useClearStates.spec.tsx b/client/src/hooks/Config/__tests__/useClearStates.spec.tsx index f0c031b2b4..a83f9d54a3 100644 --- a/client/src/hooks/Config/__tests__/useClearStates.spec.tsx +++ b/client/src/hooks/Config/__tests__/useClearStates.spec.tsx @@ -3,9 +3,15 @@ import { RecoilRoot } from 'recoil'; import { act, renderHook } from '@testing-library/react'; import { ReasoningEffort } from 'librechat-data-provider'; import { createStore, Provider as JotaiProvider } from 'jotai'; +import { + abortScrollFamily, + showStopButtonByIndex, + submissionStartFamily, +} from '~/store/generation'; import { pendingReasoningOverrideFamily } from '~/components/Chat/Input/Composer/state'; import { filesDialogTriggerAtom, showFilesDialogAtom } from '~/store/filesDialog'; import useClearStates from '../useClearStates'; +import store from '~/store'; describe('useClearStates', () => { /* Jotai's default store outlives the authenticated route, so a file manager @@ -53,4 +59,36 @@ describe('useClearStates', () => { jotaiStore.get(pendingReasoningOverrideFamily('unmounted-conversation')), ).toBeUndefined(); }); + + /* `skipFirst` keeps the landing pane's run state; every other pane's stop + button, scroll latch and elapsed baseline reset so the next session starts idle. */ + it.each([ + [false, [false, false]], + [true, [true, false]], + ])('resets per-pane run state (skipFirst %s)', async (skipFirst, expected) => { + const jotaiStore = createStore(); + for (const key of [0, 1]) { + jotaiStore.set(showStopButtonByIndex(key), true); + jotaiStore.set(abortScrollFamily(key), true); + jotaiStore.set(submissionStartFamily(key), 1000); + } + const wrapper = ({ children }: { children: React.ReactNode }) => ( + + set(store.conversationKeysAtom, [0, 1])}> + {children} + + + ); + + const { result } = renderHook(() => useClearStates(), { wrapper }); + await act(async () => { + await result.current(skipFirst); + }); + + expect([0, 1].map((key) => jotaiStore.get(showStopButtonByIndex(key)))).toEqual(expected); + expect([0, 1].map((key) => jotaiStore.get(abortScrollFamily(key)))).toEqual(expected); + expect([0, 1].map((key) => jotaiStore.get(submissionStartFamily(key)))).toEqual( + expected.map((kept) => (kept ? 1000 : null)), + ); + }); }); diff --git a/client/src/hooks/Config/useClearStates.ts b/client/src/hooks/Config/useClearStates.ts index 1dfd234152..8450f056a6 100644 --- a/client/src/hooks/Config/useClearStates.ts +++ b/client/src/hooks/Config/useClearStates.ts @@ -4,6 +4,11 @@ import { getReasoningStateKey, clearPendingReasoningOverrides, } from '~/components/Chat/Input/Composer/state'; +import { + abortScrollFamily, + showStopButtonByIndex, + submissionStartFamily, +} from '~/store/generation'; import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state'; import { showFilesDialogAtom, filesDialogTriggerAtom } from '~/store/filesDialog'; import { showSkillsPopoverFamily } from '~/components/Chat/Input/skillsState'; @@ -44,8 +49,9 @@ export default function useClearStates() { reset(store.filesByIndex(key)); reset(store.presetByIndex(key)); reset(store.textByIndex(key)); - reset(store.showStopButtonByIndex(key)); - reset(store.abortScrollFamily(key)); + jotaiStore.set(showStopButtonByIndex(key), false); + jotaiStore.set(abortScrollFamily(key), false); + jotaiStore.set(submissionStartFamily(key), null); reset(store.isSubmittingFamily(key)); reset(store.optionSettingsFamily(key)); reset(store.showPopoverFamily(key)); diff --git a/client/src/hooks/SSE/__tests__/useEventHandlers.cancel.spec.tsx b/client/src/hooks/SSE/__tests__/useEventHandlers.cancel.spec.tsx index 5c247c7256..ffa573bf45 100644 --- a/client/src/hooks/SSE/__tests__/useEventHandlers.cancel.spec.tsx +++ b/client/src/hooks/SSE/__tests__/useEventHandlers.cancel.spec.tsx @@ -23,7 +23,7 @@ jest.mock('react-router-dom', () => ({ })); jest.mock('~/store', () => ({ __esModule: true, - default: { abortScroll: 'abortScroll', submissionStartFamily: () => 'start' }, + default: { abortScroll: 'abortScroll' }, })); jest.mock('~/hooks/AuthContext', () => ({ useAuthContext: () => ({}) })); jest.mock('~/Providers', () => ({ useLiveAnnouncer: () => ({ announcePolite: jest.fn() }) })); diff --git a/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts b/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts index c172ccdd8f..d641490d05 100644 --- a/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts +++ b/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts @@ -104,9 +104,7 @@ const mockQueryClient = { }; const mockActiveRunAtom = { key: 'activeRun' }; -const mockAbortScrollAtom = { key: 'abortScroll' }; const mockSubmissionAtom = { key: 'submission' }; -const mockShowStopButtonAtom = { key: 'showStopButton' }; const mockPendingSteersAtom = { key: 'pendingSteers' }; /** The follow-up queue a restored submission lands back in. */ const queuedIn = (conversationId: string) => @@ -140,15 +138,9 @@ const mockUseSetRecoilStateMock = jest.fn((atom: unknown) => { if (atom === mockActiveRunAtom) { return mockSetActiveRun; } - if (atom === mockAbortScrollAtom) { - return mockSetAbortScroll; - } if (atom === mockSubmissionAtom) { return mockSetSubmission; } - if (atom === mockShowStopButtonAtom) { - return mockSetShowStopButton; - } return jest.fn(); }); function mockUseSetRecoilState(atom: unknown) { @@ -187,8 +179,8 @@ jest.mock('recoil', () => ({ useRecoilCallback: mockUseRecoilCallback, })); -/** The run-end and interrupt-drain signals are Jotai atoms; their pane setters are swapped for - * spies, the same way the Recoil setters above are. */ +/** The run-end, interrupt-drain, abort-scroll and stop-button state are Jotai atoms; their pane + * setters are swapped for spies, the same way the Recoil setters above are. */ jest.mock('jotai', () => { const actual = jest.requireActual('jotai'); return { @@ -201,6 +193,13 @@ jest.mock('jotai', () => { if (atom === queue.drainAfterAbortByIndex(0)) { return mockSetDrainAfterAbort; } + const generation = jest.requireActual('~/store/generation'); + if (atom === generation.abortScrollFamily(0)) { + return mockSetAbortScroll; + } + if (atom === generation.showStopButtonByIndex(0)) { + return mockSetShowStopButton; + } return actual.useSetAtom(atom); }, }; @@ -210,9 +209,7 @@ jest.mock('~/store', () => ({ __esModule: true, default: { activeRunFamily: jest.fn(() => mockActiveRunAtom), - abortScrollFamily: jest.fn(() => mockAbortScrollAtom), submissionByIndex: jest.fn(() => mockSubmissionAtom), - showStopButtonByIndex: jest.fn(() => mockShowStopButtonAtom), pendingSteersByConvoId: jest.fn(() => mockPendingSteersAtom), }, })); diff --git a/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx b/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx index de535a194e..99e4aa0944 100644 --- a/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx +++ b/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx @@ -12,6 +12,7 @@ import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/ import { queuedMessagesByConvoId, resetQueueFamilies } from '~/hooks/Chat/queue'; import { pendingApprovalActionFamily } from '~/components/Chat/approval/state'; import { agentQueuedTurnsQueryKey } from '~/data-provider/SSE/queuedTurns'; +import { submissionStartFamily } from '~/store/generation'; import { revealedQueuedTurnFamily } from '~/store/steer'; import useResumeOnLoad from '../useResumeOnLoad'; import store from '~/store'; @@ -126,6 +127,9 @@ function renderUseResumeOnLoad({ }) { const getMessages = jest.fn(getMessagesOverride ?? (() => messages)); const jotaiStore = createStore(); + if (submissionStart != null) { + jotaiStore.set(submissionStartFamily(0), submissionStart); + } const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } }, }); @@ -140,9 +144,6 @@ function renderUseResumeOnLoad({ store.activeGenerationCreatedAtByConvoId(conversationId), attachedGenerationCreatedAt, ); - if (submissionStart != null) { - snapshot.set(store.submissionStartFamily(0), submissionStart); - } if (pendingSteers) { snapshot.set(store.pendingSteersByConvoId(conversationId), pendingSteers); } @@ -159,7 +160,7 @@ function renderUseResumeOnLoad({ return null; }; const SubmissionStartProbe = () => { - const currentStart = useRecoilValue(store.submissionStartFamily(0)); + const currentStart = useAtomValue(submissionStartFamily(0)); onSubmissionStart?.(currentStart); return null; }; diff --git a/client/src/hooks/SSE/useEventHandlers.ts b/client/src/hooks/SSE/useEventHandlers.ts index 4384d6d84a..086507e22b 100644 --- a/client/src/hooks/SSE/useEventHandlers.ts +++ b/client/src/hooks/SSE/useEventHandlers.ts @@ -1,6 +1,6 @@ import { useCallback, useEffect, useRef } from 'react'; import { v4 } from 'uuid'; -import { useStore } from 'jotai'; +import { useStore, useSetAtom } from 'jotai'; import { useQueryClient } from '@tanstack/react-query'; import { useSetRecoilState, useRecoilCallback } from 'recoil'; import { useParams, useNavigate, useLocation } from 'react-router-dom'; @@ -65,6 +65,7 @@ import { shouldResetSubagentAtomsOnConversationChange } from './cleanup'; import useAttachmentHandler from '~/hooks/SSE/useAttachmentHandler'; import { useChatTransport } from '~/Providers/ChatTransportContext'; import useContentHandler from '~/hooks/SSE/useContentHandler'; +import { submissionStartFamily } from '~/store/generation'; import useStepHandler from '~/hooks/SSE/useStepHandler'; import { useApplyAgentTemplate } from '~/hooks/Agents'; import { useAuthContext } from '~/hooks/AuthContext'; @@ -593,7 +594,7 @@ export default function useEventHandlers({ * its generation, or a later externally-started run attached at this index * would inherit a stale baseline. Navigation teardown deliberately does not * clear it: a reattach to a still-live run keeps its original start. */ - const setSubmissionStart = useSetRecoilState(store.submissionStartFamily(runIndex)); + const setSubmissionStart = useSetAtom(submissionStartFamily(runIndex)); const { mutate: reconcileCodeDecision } = useReconcileConversationCodeEnvironmentMutation(setConversation); const reconcileFailedCodeDecision = useCallback( diff --git a/client/src/hooks/SSE/useResumableSSE.ts b/client/src/hooks/SSE/useResumableSSE.ts index cbdbd5d398..5a7a97a9ed 100644 --- a/client/src/hooks/SSE/useResumableSSE.ts +++ b/client/src/hooks/SSE/useResumableSSE.ts @@ -98,6 +98,7 @@ import useEventHandlers, { } from './useEventHandlers'; import { drainAfterAbortByIndex, queuedMessagesByConvoId, runEndByIndex } from '~/hooks/Chat/queue'; import { pendingApprovalActionFamily } from '~/components/Chat/approval/state'; +import { abortScrollFamily, showStopButtonByIndex } from '~/store/generation'; import { withSubmittedCodeDecision } from '~/hooks/Agents/codeDecision'; import { useChatTransport } from '~/Providers/ChatTransportContext'; import useSteerConvert from '~/hooks/Chat/useSteerConvert'; @@ -969,9 +970,9 @@ export default function useResumableSSE( ); const [_completed, setCompleted] = useState(new Set()); const [streamId, setStreamId] = useState(null); - const setAbortScroll = useSetRecoilState(store.abortScrollFamily(runIndex)); + const setAbortScroll = useSetAtom(abortScrollFamily(runIndex)); const setSubmission = useSetRecoilState(store.submissionByIndex(runIndex)); - const setShowStopButton = useSetRecoilState(store.showStopButtonByIndex(runIndex)); + const setShowStopButton = useSetAtom(showStopButtonByIndex(runIndex)); const setLiveAppliedSteerIds = useSetAtom(liveAppliedSteerIdsAtom); const streamRef = useRef(null); diff --git a/client/src/hooks/SSE/useResumeOnLoad.ts b/client/src/hooks/SSE/useResumeOnLoad.ts index b6ee8e6be3..01bb0c4c6a 100644 --- a/client/src/hooks/SSE/useResumeOnLoad.ts +++ b/client/src/hooks/SSE/useResumeOnLoad.ts @@ -1,5 +1,5 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; -import { useStore, useAtom } from 'jotai'; +import { useStore, useAtom, useSetAtom } from 'jotai'; import { useQueryClient } from '@tanstack/react-query'; import { useSetRecoilState, useRecoilValue, useRecoilCallback } from 'recoil'; import { @@ -42,6 +42,7 @@ import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/ import { pendingApprovalActionFamily } from '~/components/Chat/approval/state'; import { agentQueuedTurnsQueryKey } from '~/data-provider/SSE/queuedTurns'; import useSteerConvert from '~/hooks/Chat/useSteerConvert'; +import { submissionStartFamily } from '~/store/generation'; import { revealedQueuedTurnFamily } from '~/store/steer'; import { resumeRequestsAtom } from '~/hooks/Chat/resume'; import { useFileMapContext } from '~/Providers'; @@ -297,7 +298,7 @@ export default function useResumeOnLoad( const jotaiStore = useStore(); const queryClient = useQueryClient(); const setSubmission = useSetRecoilState(store.submissionByIndex(runIndex)); - const setSubmissionStart = useSetRecoilState(store.submissionStartFamily(runIndex)); + const setSubmissionStart = useSetAtom(submissionStartFamily(runIndex)); const currentSubmission = useRecoilValue(store.submissionByIndex(runIndex)); const isSubmitting = useRecoilValue(store.isSubmittingFamily(runIndex)); const attachedGenerationCreatedAt = useRecoilValue( diff --git a/client/src/hooks/SSE/useSSE.ts b/client/src/hooks/SSE/useSSE.ts index 0f7b5555cd..2fc0630505 100644 --- a/client/src/hooks/SSE/useSSE.ts +++ b/client/src/hooks/SSE/useSSE.ts @@ -1,7 +1,7 @@ import { useEffect, useState } from 'react'; import { v4 } from 'uuid'; -import { useStore } from 'jotai'; import { useSetRecoilState } from 'recoil'; +import { useStore, useSetAtom } from 'jotai'; import { StepEvents, createPayload, removeNullishValues } from 'librechat-data-provider'; import type { Agents, @@ -19,6 +19,7 @@ import type { TResData, TFinalResData } from '~/common'; import { clearComposerDrafts, applyPendingAction, findPendingActionMessageIndex } from '~/utils'; import { startedAsNewConversation, buildCreatedInitialResponse } from './useEventHandlers'; import { pendingApprovalActionFamily } from '~/components/Chat/approval/state'; +import { abortScrollFamily, showStopButtonByIndex } from '~/store/generation'; import { useGetStartupConfig, useGetUserBalance } from '~/data-provider'; import { useChatTransport } from '~/Providers/ChatTransportContext'; import { useAuthContext } from '~/hooks/AuthContext'; @@ -49,8 +50,8 @@ export default function useSSE( const { token, isAuthenticated } = useAuthContext(); const transport = useChatTransport(); const [completed, setCompleted] = useState(new Set()); - const setAbortScroll = useSetRecoilState(store.abortScrollFamily(runIndex)); - const setShowStopButton = useSetRecoilState(store.showStopButtonByIndex(runIndex)); + const setAbortScroll = useSetAtom(abortScrollFamily(runIndex)); + const setShowStopButton = useSetAtom(showStopButtonByIndex(runIndex)); const { setMessages, getMessages, setConversation, setIsSubmitting, newConversation } = chatHelpers; diff --git a/client/src/store/__tests__/generation.spec.ts b/client/src/store/__tests__/generation.spec.ts new file mode 100644 index 0000000000..25f9efdbf9 --- /dev/null +++ b/client/src/store/__tests__/generation.spec.ts @@ -0,0 +1,30 @@ +import { createStore } from 'jotai'; +import { abortScrollFamily, showStopButtonByIndex, submissionStartFamily } from '../generation'; + +describe('generation atoms', () => { + it('start idle for every pane', () => { + const store = createStore(); + + expect(store.get(showStopButtonByIndex(0))).toBe(false); + expect(store.get(abortScrollFamily(0))).toBe(false); + expect(store.get(submissionStartFamily(0))).toBeNull(); + }); + + it('keep each pane independent', () => { + const store = createStore(); + store.set(showStopButtonByIndex(0), true); + store.set(submissionStartFamily(0), 1000); + + expect(store.get(showStopButtonByIndex(1))).toBe(false); + expect(store.get(submissionStartFamily(1))).toBeNull(); + }); + + it('accepts value and updater writes on the abort-scroll latch', () => { + const store = createStore(); + store.set(abortScrollFamily(0), true); + expect(store.get(abortScrollFamily(0))).toBe(true); + + store.set(abortScrollFamily(0), (prev) => !prev); + expect(store.get(abortScrollFamily(0))).toBe(false); + }); +}); diff --git a/client/src/store/families.ts b/client/src/store/families.ts index 8a71753e48..ba5a86abb0 100644 --- a/client/src/store/families.ts +++ b/client/src/store/families.ts @@ -33,23 +33,6 @@ const submissionByIndex = atomFamily({ default: null, }); -/** - * Epoch ms baseline for the streaming elapsed indicator at this chat index. - * Stamped when this session submits a generation (every path through `ask`), - * cleared by the terminal handlers when that generation ends, and only FILLED - * — never overwritten — when resume-on-load attaches a run, preferring the - * server-recorded generation start so a reload reports real elapsed time. - * The reading therefore survives mid-stream remounts (new-conversation id - * hydration, navigating away from a still-live run and back) without a later, - * externally-started generation inheriting a stale baseline. Known residual: - * a run whose end this pane never observed (left mid-stream, finished - * elsewhere) leaves its stamp for the next attach at this index to inherit. - */ -const submissionStartFamily = atomFamily({ - key: 'submissionStartByIndex', - default: null, -}); - const submissionKeysSelector = selector<(string | number)[]>({ key: 'submissionKeysSelector', get: ({ get }) => { @@ -219,27 +202,6 @@ const textByIndex = atomFamily({ default: '', }); -const showStopButtonByIndex = atomFamily({ - key: 'showStopButtonByIndex', - default: false, -}); - -const abortScrollFamily = atomFamily({ - key: 'abortScrollByIndex', - default: false, - effects: [ - ({ onSet, node }) => { - onSet(async (newValue) => { - const key = Number(node.key.split(Constants.COMMON_DIVIDER)[1]); - logger.log('message_scrolling', 'Recoil Effect: Setting abortScrollByIndex', { - key, - newValue, - }); - }); - }, - ] as const, -}); - const isSubmittingFamily = atomFamily({ key: 'isSubmittingByIndex', default: false, @@ -522,10 +484,7 @@ export default { filesByIndex, presetByIndex, submissionByIndex, - submissionStartFamily, textByIndex, - showStopButtonByIndex, - abortScrollFamily, isSubmittingFamily, optionSettingsFamily, showPopoverFamily, diff --git a/client/src/store/generation.ts b/client/src/store/generation.ts new file mode 100644 index 0000000000..f42f81f8c8 --- /dev/null +++ b/client/src/store/generation.ts @@ -0,0 +1,34 @@ +import { atom } from 'jotai'; +import { atomFamily } from 'jotai/utils'; +import type { SetStateAction } from 'react'; +import { logger } from '~/utils'; + +/** + * Epoch ms baseline for the streaming elapsed indicator at this chat index. + * Stamped when this session submits a generation (every path through `ask`), + * cleared by the terminal handlers when that generation ends, and only FILLED + * (never overwritten) when resume-on-load attaches a run, preferring the + * server-recorded generation start so a reload reports real elapsed time. + * The reading therefore survives mid-stream remounts (new-conversation id + * hydration, navigating away from a still-live run and back) without a later, + * externally-started generation inheriting a stale baseline. Known residual: + * a run whose end this pane never observed (left mid-stream, finished + * elsewhere) leaves its stamp for the next attach at this index to inherit. + */ +export const submissionStartFamily = atomFamily((_index: string | number) => + atom(null), +); + +export const showStopButtonByIndex = atomFamily((_index: string | number) => atom(false)); + +export const abortScrollFamily = atomFamily((index: string | number) => { + const valueAtom = atom(false); + return atom( + (get) => get(valueAtom), + (get, set, update: SetStateAction) => { + const newValue = typeof update === 'function' ? update(get(valueAtom)) : update; + logger.log('message_scrolling', 'Setting abortScrollByIndex', { key: index, newValue }); + set(valueAtom, newValue); + }, + ); +}); diff --git a/e2e/specs/mock/scenarios/run-indicators.spec.ts b/e2e/specs/mock/scenarios/run-indicators.spec.ts new file mode 100644 index 0000000000..9472673b93 --- /dev/null +++ b/e2e/specs/mock/scenarios/run-indicators.spec.ts @@ -0,0 +1,116 @@ +import { expect, test } from '@playwright/test'; +import type { Page } 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 STREAM_ROUTE = /\/api\/agents\/chat\/stream\//; + +const uniqueLabel = (prefix: string) => + `${prefix}-${Date.now()}-${Math.floor(Math.random() * 1e4)}`; + +const assistantTurn = (page: Page) => + messagesView(page) + .locator('.message-render') + .filter({ has: page.locator('.agent-turn') }) + .last(); + +const stopButton = (page: Page) => page.getByRole('button', { name: 'Stop generating' }); + +/** Reads `12s` or `1m 3s` as whole seconds. */ +const toSeconds = (reading: string): number => { + const match = /^(?:(\d+)m )?(\d+)s$/.exec(reading.trim()); + if (!match) { + throw new Error(`Unexpected elapsed reading: ${reading}`); + } + return Number(match[1] ?? 0) * 60 + Number(match[2]); +}; + +test.describe('run indicators', () => { + test('a live reply shows the stop button and a ticking elapsed reading until it settles @scenario:live-run-shows-stop-and-elapsed-until-it-settles', async ({ + page, + }) => { + test.setTimeout(120_000); + const label = uniqueLabel('run-indicators'); + + await page.goto(NEW_CHAT_PATH, { timeout: 10_000 }); + await selectMockEndpoint(page, MOCK_ENDPOINTS[0]); + + const run = await sendMessage(page, `E2E_SLOW_REPLY:${label}`); + expect(run.ok()).toBeTruthy(); + await expect(messagesView(page).getByText('chunk-010')).toBeVisible({ timeout: 15_000 }); + + await expect(stopButton(page)).toBeVisible(); + const elapsed = assistantTurn(page).getByTestId('stream-elapsed'); + await expect(elapsed).toHaveText(/^\d+s$/); + const firstReading = (await elapsed.textContent()) ?? ''; + await expect(elapsed).not.toHaveText(firstReading, { timeout: 5_000 }); + + await expect(stopButton(page)).toBeHidden({ timeout: 60_000 }); + await expect(elapsed).toHaveCount(0); + }); + + test('the elapsed reading keeps counting from the run start across a reload, and stopping clears it @scenario:elapsed-reading-survives-a-reload-mid-run', async ({ + page, + }) => { + test.setTimeout(120_000); + const label = uniqueLabel('run-indicators-reload'); + + await page.goto(NEW_CHAT_PATH, { timeout: 10_000 }); + await selectMockEndpoint(page, MOCK_ENDPOINTS[0]); + + /** The long reply runs in an already persisted conversation, so the reload + * lands on it while the reply is still streaming. */ + 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 response = await sendMessage(page, `${LONG_REPLY_MARKER}:${label}`); + expect(response.ok()).toBeTruthy(); + const elapsed = assistantTurn(page).getByTestId('stream-elapsed'); + await expect(elapsed).toHaveText(/^[3-9]s$/, { timeout: 15_000 }); + const beforeReload = toSeconds((await elapsed.textContent()) ?? ''); + + const reattached = page.waitForResponse( + (candidate) => + candidate.request().method() === 'GET' && + STREAM_ROUTE.test(new URL(candidate.url()).pathname) && + candidate.url().includes('resume=true'), + { timeout: 30_000 }, + ); + await page.reload(); + expect((await reattached).status()).toBe(200); + + /** A reload that restarted the count would read below the pre-reload value. */ + const resumedElapsed = assistantTurn(page).getByTestId('stream-elapsed'); + await expect(resumedElapsed).toHaveText(/^\d+s$/, { timeout: 15_000 }); + expect(toSeconds((await resumedElapsed.textContent()) ?? '')).toBeGreaterThanOrEqual( + beforeReload, + ); + await expect(stopButton(page)).toBeVisible(); + + const [abort] = await Promise.all([ + page.waitForResponse( + (candidate) => + candidate.request().method() === 'POST' && + candidate.url().includes('/api/agents/chat/abort'), + { timeout: 30_000 }, + ), + stopButton(page).click(), + ]); + expect(abort.ok()).toBeTruthy(); + await expect(stopButton(page)).toBeHidden({ timeout: 30_000 }); + await expect(resumedElapsed).toHaveCount(0); + }); +});