🪢 refactor: Move Stop Button, Abort Scroll and Submission Start State to Jotai (#16968)

* refactor: Move Stop Button, Abort Scroll and Submission Start State to Jotai

* fix: Clear the Elapsed Baseline With the Other Per-Pane Run State
This commit is contained in:
Marco Beretta 2026-10-11 23:04:34 +02:00 • committed by GitHub
parent f8ffb4c4e3
commit e582fa3f90
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 364 additions and 127 deletions

View file

@ -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]);

View file

@ -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 }) => (
<QueryClientProvider client={queryClient}>
<RecoilRoot
initializeState={({ set }) => {
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);
}
}}
>
<MemoryRouter initialEntries={[`${pathname}?${query}`]}>
<NavigationBridge />
<AuthContextProvider authConfig={{ loginRedirect: '', test: true }}>
<ChatTransportContext.Provider value={transport}>
<DndProvider backend={HTML5Backend}>{children}</DndProvider>
</ChatTransportContext.Provider>
</AuthContextProvider>
</MemoryRouter>
</RecoilRoot>
<JotaiProvider store={jotaiStore}>
<RecoilRoot
initializeState={({ set }) => {
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);
}
}}
>
<MemoryRouter initialEntries={[`${pathname}?${query}`]}>
<NavigationBridge />
<AuthContextProvider authConfig={{ loginRedirect: '', test: true }}>
<ChatTransportContext.Provider value={transport}>
<DndProvider backend={HTML5Backend}>{children}</DndProvider>
</ChatTransportContext.Provider>
</AuthContextProvider>
</MemoryRouter>
</RecoilRoot>
</JotaiProvider>
</QueryClientProvider>
);
const view = render(

View file

@ -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 <ElapsedTimer start={submissionStart ?? undefined} />;
});
@ -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 (
<span className="flex items-center ps-1.5 text-text-secondary">
<span className="text-text-secondary flex items-center ps-1.5">
<span aria-hidden="true" className="shimmer tabular-nums" data-testid="stream-elapsed">
{localize(labels.key, labels.values)}
</span>

View file

@ -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(
<RecoilRoot initializeState={initializeState}>
<Elapsed index={0} />
<RecoilRoot>
<Provider store={storeWithStart(start)}>
<Elapsed index={0} />
</Provider>
</RecoilRoot>,
);
}
@ -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(
<RecoilRoot initializeState={({ set }) => set(store.submissionStartFamily(0), start)}>
<Elapsed index={0} />
<RecoilRoot>
<Provider store={jotaiStore}>
<Elapsed index={0} />
</Provider>
</RecoilRoot>,
);
expect(screen.getByTestId('stream-elapsed')).toHaveTextContent(/^30s$/);
view.rerender(
<RecoilRoot initializeState={({ set }) => set(store.submissionStartFamily(0), start)}>
{null}
<RecoilRoot>
<Provider store={jotaiStore}>{null}</Provider>
</RecoilRoot>,
);
expect(screen.queryByTestId('stream-elapsed')).toBeNull();
advance(5_000);
view.rerender(
<RecoilRoot initializeState={({ set }) => set(store.submissionStartFamily(0), start)}>
<Elapsed index={0} />
<RecoilRoot>
<Provider store={jotaiStore}>
<Elapsed index={0} />
</Provider>
</RecoilRoot>,
);
expect(screen.getByTestId('stream-elapsed')).toHaveTextContent(/^35s$/);

View file

@ -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([

View file

@ -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);

View file

@ -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

View file

@ -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 }) => (
<JotaiProvider store={jotaiStore}>
<RecoilRoot initializeState={({ set }) => set(store.conversationKeysAtom, [0, 1])}>
{children}
</RecoilRoot>
</JotaiProvider>
);
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)),
);
});
});

View file

@ -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));

View file

@ -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() }) }));

View file

@ -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),
},
}));

View file

@ -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;
};

View file

@ -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(

View file

@ -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<string | null>(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<AbortController | null>(null);

View file

@ -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(

View file

@ -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;

View file

@ -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);
});
});

View file

@ -33,23 +33,6 @@ const submissionByIndex = atomFamily<TSubmission | null, string | number>({
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<number | null, string | number>({
key: 'submissionStartByIndex',
default: null,
});
const submissionKeysSelector = selector<(string | number)[]>({
key: 'submissionKeysSelector',
get: ({ get }) => {
@ -219,27 +202,6 @@ const textByIndex = atomFamily<string, string | number>({
default: '',
});
const showStopButtonByIndex = atomFamily<boolean, string | number>({
key: 'showStopButtonByIndex',
default: false,
});
const abortScrollFamily = atomFamily<boolean, string | number>({
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,

View file

@ -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<number | null>(null),
);
export const showStopButtonByIndex = atomFamily((_index: string | number) => atom<boolean>(false));
export const abortScrollFamily = atomFamily((index: string | number) => {
const valueAtom = atom<boolean>(false);
return atom(
(get) => get(valueAtom),
(get, set, update: SetStateAction<boolean>) => {
const newValue = typeof update === 'function' ? update(get(valueAtom)) : update;
logger.log('message_scrolling', 'Setting abortScrollByIndex', { key: index, newValue });
set(valueAtom, newValue);
},
);
});

View file

@ -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);
});
});