diff --git a/client/src/components/Chat/Input/ChatForm.tsx b/client/src/components/Chat/Input/ChatForm.tsx index b7794e52d6..1a3f38c740 100644 --- a/client/src/components/Chat/Input/ChatForm.tsx +++ b/client/src/components/Chat/Input/ChatForm.tsx @@ -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'; 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 4c56435f58..1212cfcae5 100644 --- a/client/src/components/Chat/Input/__tests__/ChatForm.queryParams.spec.tsx +++ b/client/src/components/Chat/Input/__tests__/ChatForm.queryParams.spec.tsx @@ -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'; diff --git a/client/src/components/Chat/Messages/Elapsed.tsx b/client/src/components/Chat/Messages/Elapsed.tsx index 6f87ccd0a6..d682a84c57 100644 --- a/client/src/components/Chat/Messages/Elapsed.tsx +++ b/client/src/components/Chat/Messages/Elapsed.tsx @@ -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'; diff --git a/client/src/components/Chat/Messages/__tests__/Elapsed.spec.tsx b/client/src/components/Chat/Messages/__tests__/Elapsed.spec.tsx index 855a59e558..57f94cbfee 100644 --- a/client/src/components/Chat/Messages/__tests__/Elapsed.spec.tsx +++ b/client/src/components/Chat/Messages/__tests__/Elapsed.spec.tsx @@ -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(); diff --git a/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx b/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx index 6b7962d272..e40ea39e64 100644 --- a/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx +++ b/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx @@ -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'; diff --git a/client/src/hooks/Chat/useChatFunctions.ts b/client/src/hooks/Chat/useChatFunctions.ts index 7a57f91205..ece1cb0a1d 100644 --- a/client/src/hooks/Chat/useChatFunctions.ts +++ b/client/src/hooks/Chat/useChatFunctions.ts @@ -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'; diff --git a/client/src/hooks/Chat/useChatHelpers.ts b/client/src/hooks/Chat/useChatHelpers.ts index 8ca896dc20..ac542cb523 100644 --- a/client/src/hooks/Chat/useChatHelpers.ts +++ b/client/src/hooks/Chat/useChatHelpers.ts @@ -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'; diff --git a/client/src/hooks/Config/__tests__/useClearStates.spec.tsx b/client/src/hooks/Config/__tests__/useClearStates.spec.tsx index 8b2d4b599c..a93899ed70 100644 --- a/client/src/hooks/Config/__tests__/useClearStates.spec.tsx +++ b/client/src/hooks/Config/__tests__/useClearStates.spec.tsx @@ -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'; diff --git a/client/src/hooks/Config/useClearStates.ts b/client/src/hooks/Config/useClearStates.ts index 0b8d8e355b..f7d9bbae86 100644 --- a/client/src/hooks/Config/useClearStates.ts +++ b/client/src/hooks/Config/useClearStates.ts @@ -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'; diff --git a/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts b/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts index 8f8213eb9c..c39a0ba283 100644 --- a/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts +++ b/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts @@ -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; } diff --git a/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx b/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx index bcfc84f1de..a600efb06c 100644 --- a/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx +++ b/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx @@ -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'; diff --git a/client/src/hooks/SSE/useEventHandlers.ts b/client/src/hooks/SSE/useEventHandlers.ts index 4565478cc8..b5d1aa6f42 100644 --- a/client/src/hooks/SSE/useEventHandlers.ts +++ b/client/src/hooks/SSE/useEventHandlers.ts @@ -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'; diff --git a/client/src/hooks/SSE/useResumableSSE.ts b/client/src/hooks/SSE/useResumableSSE.ts index 4d6b76a750..a4ffe6ff35 100644 --- a/client/src/hooks/SSE/useResumableSSE.ts +++ b/client/src/hooks/SSE/useResumableSSE.ts @@ -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'; diff --git a/client/src/hooks/SSE/useResumeOnLoad.ts b/client/src/hooks/SSE/useResumeOnLoad.ts index 660048b22b..fcfb3292ac 100644 --- a/client/src/hooks/SSE/useResumeOnLoad.ts +++ b/client/src/hooks/SSE/useResumeOnLoad.ts @@ -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'; diff --git a/client/src/hooks/SSE/useSSE.ts b/client/src/hooks/SSE/useSSE.ts index c6022d90e5..3a6f6b9d1a 100644 --- a/client/src/hooks/SSE/useSSE.ts +++ b/client/src/hooks/SSE/useSSE.ts @@ -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'; diff --git a/client/src/store/generation.ts b/client/src/store/generation.ts deleted file mode 100644 index f42f81f8c8..0000000000 --- a/client/src/store/generation.ts +++ /dev/null @@ -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(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/packages/chat/src/react.ts b/packages/chat/src/react.ts index 9a158b87a0..57001a5ebf 100644 --- a/packages/chat/src/react.ts +++ b/packages/chat/src/react.ts @@ -57,3 +57,8 @@ export { useIsSubmitting, } from './react/submission'; export type { PaneIndex } from './react/submission'; +export { + submissionStartFamily, + showStopButtonByIndex, + abortScrollFamily, +} from './react/generation'; diff --git a/client/src/store/__tests__/generation.spec.ts b/packages/chat/src/react/__tests__/generation.spec.ts similarity index 100% rename from client/src/store/__tests__/generation.spec.ts rename to packages/chat/src/react/__tests__/generation.spec.ts diff --git a/packages/chat/src/react/generation.ts b/packages/chat/src/react/generation.ts new file mode 100644 index 0000000000..01fe584a09 --- /dev/null +++ b/packages/chat/src/react/generation.ts @@ -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], 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 = + atomFamily((_index: string | number) => atom(null)); + +/** Whether the pane at this index shows its Stop control. */ +export const showStopButtonByIndex: (index: string | number) => PrimitiveAtom = atomFamily( + (_index: string | number) => atom(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(false); + return atom( + (get) => get(valueAtom), + (get, set, update: SetStateAction) => { + const newValue = typeof update === 'function' ? update(get(valueAtom)) : update; + set(valueAtom, newValue); + }, + ); + }, +);