📦 refactor: Move the Generation Run Atoms Into the Chat Package (#17140)

This commit is contained in:
Marco Beretta 2026-10-15 21:34:17 +02:00 • committed by GitHub
parent 2a701417b7
commit 25ecf9e7cb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 82 additions and 60 deletions

View file

@ -3,6 +3,7 @@ import { useAtom } from 'jotai';
import { useRecoilValue } from 'recoil';
import { useWatch } from 'react-hook-form';
import { useNavigate } from 'react-router-dom';
import { showStopButtonByIndex } from '@librechat/chat/react';
import {
composerSurfaceClasses,
composerSurfaceShadow,
@ -61,7 +62,6 @@ 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';

View file

@ -12,6 +12,7 @@ import { Provider as JotaiProvider, createStore, getDefaultStore, useAtom } from
import {
activeGenerationCreatedAtByConvoId,
activeGenerationProtocolVersionByConvoId,
showStopButtonByIndex,
} from '@librechat/chat/react';
import type { TConversation } from 'librechat-data-provider';
import type { Transport } from '~/hooks/Chat/contract';
@ -19,7 +20,6 @@ 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';

View file

@ -1,7 +1,7 @@
import { memo, useEffect, useState } from 'react';
import { useAtomValue } from 'jotai';
import { useTranslation } from 'react-i18next';
import { submissionStartFamily } from '~/store/generation';
import { submissionStartFamily } from '@librechat/chat/react';
import { getElapsedDurationLabels } from '~/utils';
import { useLocalize } from '~/hooks';

View file

@ -2,8 +2,8 @@ import React from 'react';
import { RecoilRoot } from 'recoil';
import { Provider, createStore } from 'jotai';
import { render, screen, act } from '@testing-library/react';
import { submissionStartFamily } from '@librechat/chat/react';
import Elapsed, { shouldShowElapsed } from '~/components/Chat/Messages/Elapsed';
import { submissionStartFamily } from '~/store/generation';
function storeWithStart(start?: number) {
const jotaiStore = createStore();

View file

@ -1,6 +1,5 @@
import { renderHook, act } from '@testing-library/react';
import { Provider as JotaiProvider, createStore } from 'jotai';
import { revealedQueuedTurnFamily } from '@librechat/chat/react';
import {
Constants,
ContentTypes,
@ -8,6 +7,11 @@ import {
QueryKeys,
createPayload,
} from 'librechat-data-provider';
import {
revealedQueuedTurnFamily,
submissionStartFamily,
showStopButtonByIndex,
} from '@librechat/chat/react';
import type {
Agent,
CodeEnvironmentMode,
@ -26,7 +30,6 @@ import {
} from '~/components/Chat/Input/TokenUsage/store';
import { ChatSettingsContext, defaultChatSettings } from '~/Providers/ChatSettingsContext';
import { pendingReasoningOverrideFamily } from '~/components/Chat/Input/Composer/state';
import { submissionStartFamily, showStopButtonByIndex } from '~/store/generation';
import useChatFunctions from '../useChatFunctions';
import { isPasteSubmitted } from '~/utils';

View file

@ -4,13 +4,18 @@ import { useStore, useSetAtom } from 'jotai';
import { useNavigate } from 'react-router-dom';
import { useQueryClient } from '@tanstack/react-query';
import { hasPendingAssistantParent } from '@librechat/chat';
import { revealedQueuedTurnFamily, isSubmittingFamily } from '@librechat/chat/react';
import {
Constants,
QueryKeys,
EModelEndpoint,
isAssistantsEndpoint,
} from 'librechat-data-provider';
import {
isSubmittingFamily,
submissionStartFamily,
showStopButtonByIndex,
revealedQueuedTurnFamily,
} from '@librechat/chat/react';
import type {
TMessage,
TSubmission,
@ -32,7 +37,6 @@ import {
import { logger, requestChatFocus, markPasteSubmitted, isSubmittableMessage } from '~/utils';
import { activeUsageResponseIdFamily } from '~/components/Chat/Input/TokenUsage/store';
import useFocusRegeneratedResponse from '~/hooks/Chat/useFocusRegeneratedResponse';
import { submissionStartFamily, showStopButtonByIndex } from '~/store/generation';
import useGetConversation from '~/hooks/Conversations/useGetConversation';
import useCodeApprovalMode from '~/hooks/Agents/useCodeApprovalMode';
import useSetFilesToDelete from '~/hooks/Files/useSetFilesToDelete';

View file

@ -8,6 +8,7 @@ import {
activeGenerationProtocolVersionByConvoId,
isSubmittingFamily,
useIsSubmitting,
abortScrollFamily,
} from '@librechat/chat/react';
import type { TMessage } from 'librechat-data-provider';
import type { ChatContract } from './contract';
@ -18,7 +19,6 @@ import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/
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';

View file

@ -3,16 +3,16 @@ import { RecoilRoot } from 'recoil';
import { act, renderHook } from '@testing-library/react';
import { createStore, Provider as JotaiProvider } from 'jotai';
import { Constants, ReasoningEffort } from 'librechat-data-provider';
import {
abortScrollFamily,
showStopButtonByIndex,
submissionStartFamily,
} from '@librechat/chat/react';
import {
pendingQuotesByConvoId,
pendingManualSkillsByConvoId,
pendingReasoningOverrideFamily,
} from '~/components/Chat/Input/Composer/state';
import {
abortScrollFamily,
showStopButtonByIndex,
submissionStartFamily,
} from '~/store/generation';
import { filesDialogTriggerAtom, showFilesDialogAtom } from '~/store/filesDialog';
import useClearStates from '../useClearStates';
import store from '~/store';

View file

@ -2,16 +2,16 @@ import { useStore } from 'jotai';
import { useRecoilCallback } from 'recoil';
import { Constants } from 'librechat-data-provider';
import { isSubmittingFamily } from '@librechat/chat/react';
import {
abortScrollFamily,
showStopButtonByIndex,
submissionStartFamily,
} from '@librechat/chat/react';
import {
getReasoningStateKey,
clearPendingComposerContext,
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';

View file

@ -177,7 +177,7 @@ jest.mock('jotai', () => {
if (atom === queue.drainAfterAbortByIndex(0)) {
return mockSetDrainAfterAbort;
}
const generation = jest.requireActual('~/store/generation');
const generation = jest.requireActual('@librechat/chat/react');
if (atom === generation.abortScrollFamily(0)) {
return mockSetAbortScroll;
}

View file

@ -14,6 +14,7 @@ import {
revealedQueuedTurnFamily,
pendingSteersByConvoId,
activeGenerationCreatedAtByConvoId,
submissionStartFamily,
} from '@librechat/chat/react';
import type { Agents, TMessage, TConversation, TSubmission } from 'librechat-data-provider';
import type { MutableSnapshot } from 'recoil';
@ -30,7 +31,6 @@ import {
import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state';
import { pendingApprovalActionFamily } from '~/components/Chat/approval/state';
import { agentQueuedTurnsQueryKey } from '~/data-provider/SSE/queuedTurns';
import { submissionStartFamily } from '~/store/generation';
import useResumeOnLoad from '../useResumeOnLoad';
import store from '~/store';

View file

@ -3,6 +3,7 @@ import { v4 } from 'uuid';
import { useSetRecoilState } from 'recoil';
import { useStore, useSetAtom } from 'jotai';
import { useQueryClient } from '@tanstack/react-query';
import { submissionStartFamily } from '@librechat/chat/react';
import { useParams, useNavigate, useLocation } from 'react-router-dom';
import {
QueryKeys,
@ -71,7 +72,6 @@ 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';

View file

@ -3,8 +3,12 @@ import { v4 } from 'uuid';
import { useSetRecoilState } from 'recoil';
import { useStore, useSetAtom } from 'jotai';
import { useQueryClient } from '@tanstack/react-query';
import { liveAppliedSteerIdsAtom } from '@librechat/chat/react';
import { ABORT_SWEEP_STATUSES, selectLocalSteersForQueue } from '@librechat/chat';
import {
liveAppliedSteerIdsAtom,
abortScrollFamily,
showStopButtonByIndex,
} from '@librechat/chat/react';
import {
Constants,
QueryKeys,
@ -134,7 +138,6 @@ import {
blockRecovery,
} from '~/components/Chat/Steering/recovery';
import { pendingApprovalActionFamily } from '~/components/Chat/approval/state';
import { abortScrollFamily, showStopButtonByIndex } from '~/store/generation';
import { useChatTransport } from '~/Providers/ChatTransportContext';
import { reopenTermsOnRefusal } from '~/utils/terms';
import { useAuthContext } from '~/hooks/AuthContext';

View file

@ -12,6 +12,7 @@ import {
revealedQueuedTurnFamily,
activeGenerationCreatedAtByConvoId,
useIsSubmitting,
submissionStartFamily,
} from '@librechat/chat/react';
import {
hasSubmissionUserMessage,
@ -43,7 +44,6 @@ import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/
import { pendingApprovalActionFamily } from '~/components/Chat/approval/state';
import { dedupeSteersById, getBranchSiblingIndexesForTarget } from '~/utils';
import { agentQueuedTurnsQueryKey } from '~/data-provider/SSE/queuedTurns';
import { submissionStartFamily } from '~/store/generation';
import { resumeRequestsAtom } from '~/hooks/Chat/resume';
import useSteerState from './steer';
import store from '~/store';

View file

@ -3,6 +3,7 @@ import { v4 } from 'uuid';
import { useSetRecoilState } from 'recoil';
import { useStore, useSetAtom } from 'jotai';
import { useQueryClient } from '@tanstack/react-query';
import { abortScrollFamily, showStopButtonByIndex } from '@librechat/chat/react';
import { startedAsNewConversation, buildCreatedInitialResponse } from '@librechat/chat';
import { StepEvents, createPayload, removeNullishValues } from 'librechat-data-provider';
import type {
@ -20,7 +21,6 @@ import type { EventHandlerParams } from './useEventHandlers';
import type { TResData, TFinalResData } from '~/common';
import { clearComposerDrafts, applyPendingAction, findPendingActionMessageIndex } from '~/utils';
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 { reopenTermsOnRefusal } from '~/utils/terms';

View file

@ -1,34 +0,0 @@
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

@ -57,3 +57,8 @@ export {
useIsSubmitting,
} from './react/submission';
export type { PaneIndex } from './react/submission';
export {
submissionStartFamily,
showStopButtonByIndex,
abortScrollFamily,
} from './react/generation';

View file

@ -0,0 +1,41 @@
import { atom } from 'jotai';
import { atomFamily } from 'jotai/utils';
import type { PrimitiveAtom, WritableAtom } from 'jotai';
import type { SetStateAction } from 'react';
/** A pane flag a binding reads and sets like React state. */
type FlagAtom = WritableAtom<boolean, [SetStateAction<boolean>], void>;
/**
* 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: (index: string | number) => PrimitiveAtom<number | null> =
atomFamily((_index: string | number) => atom<number | null>(null));
/** Whether the pane at this index shows its Stop control. */
export const showStopButtonByIndex: (index: string | number) => PrimitiveAtom<boolean> = atomFamily(
(_index: string | number) => atom<boolean>(false),
);
/** Whether the pane at this index has stopped following the stream to the bottom. */
export const abortScrollFamily: (index: string | number) => FlagAtom = atomFamily(
(_index: string | number): FlagAtom => {
const valueAtom = atom<boolean>(false);
return atom(
(get) => get(valueAtom),
(get, set, update: SetStateAction<boolean>) => {
const newValue = typeof update === 'function' ? update(get(valueAtom)) : update;
set(valueAtom, newValue);
},
);
},
);