From 4bd04b6f57cfcc141b6ded127c02401916de59b6 Mon Sep 17 00:00:00 2001 From: Marco Beretta <81851188+berry-13@users.noreply.github.com> Date: Thu, 15 Oct 2026 16:31:28 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=AA=9F=20refactor:=20Read=20Mounted=20Pan?= =?UTF-8?q?e=20Conversations=20through=20ChatSettings=20in=20Steer=20Recov?= =?UTF-8?q?ery=20(#17137)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/Providers/ChatSettingsContext.tsx | 1 + .../Chat/__tests__/useSteerRecovery.spec.tsx | 42 ++++++++++++++----- client/src/hooks/Chat/contract.ts | 10 +++++ client/src/hooks/Chat/useSteerRecovery.ts | 12 +++--- client/src/routes/ChatSettings.tsx | 25 ++++++++++- .../routes/__tests__/ChatSettings.spec.tsx | 16 +++++++ 6 files changed, 88 insertions(+), 18 deletions(-) diff --git a/client/src/Providers/ChatSettingsContext.tsx b/client/src/Providers/ChatSettingsContext.tsx index e351e35cda..e096431a1b 100644 --- a/client/src/Providers/ChatSettingsContext.tsx +++ b/client/src/Providers/ChatSettingsContext.tsx @@ -11,6 +11,7 @@ export const defaultChatSettings: ChatSettings = { setIsTemporary: () => undefined, config: { feedbackEnabled: false, canRenameRunningChat: false }, auth: {}, + getPaneConversations: () => [], }; export const ChatSettingsContext = createContext(defaultChatSettings); diff --git a/client/src/hooks/Chat/__tests__/useSteerRecovery.spec.tsx b/client/src/hooks/Chat/__tests__/useSteerRecovery.spec.tsx index 9e7dc72b1d..5e74d2eb48 100644 --- a/client/src/hooks/Chat/__tests__/useSteerRecovery.spec.tsx +++ b/client/src/hooks/Chat/__tests__/useSteerRecovery.spec.tsx @@ -15,7 +15,9 @@ import { ComposerRestoreProvider, useComposerRestoreHost, } from '~/Providers/ComposerRestoreContext'; +import { ChatSettingsContext, defaultChatSettings } from '~/Providers/ChatSettingsContext'; import { queuedMessagesByConvoId, resetQueueFamilies } from '~/hooks/Chat/queue'; +import { usePaneConversations } from '~/routes/ChatSettings'; import useSteerRecovery from '../useSteerRecovery'; import store from '~/store'; @@ -53,6 +55,16 @@ function RewakePublisher({ rewake, children }: { rewake: RewakeDrain; children: return <>{children}; } +/** The app's own pane reader, behind the settings context the hook reads it from. */ +function PaneSettings({ children }: { children: React.ReactNode }) { + const getPaneConversations = usePaneConversations(); + const settings = React.useMemo( + () => ({ ...defaultChatSettings, getPaneConversations }), + [getPaneConversations], + ); + return {children}; +} + function setup(initialize?: (snapshot: MutableSnapshot) => void, rewake?: RewakeDrain) { const wrapper = ({ children }: { children: React.ReactNode }) => ( void, rewake?: Rewake initialize?.(snapshot); }} > - - {rewake ? {children} : children} - + + + {rewake ? {children} : children} + + ); return renderHook( @@ -279,9 +293,11 @@ describe('useSteerRecovery', () => { ]); }} > - - - + + + + + , ); @@ -381,7 +397,9 @@ describe('useSteerRecovery', () => { ]); }} > - + + + , ); @@ -540,8 +558,10 @@ describe('useSteerRecovery', () => { ]); }} > - - {live && } + + + {live && } + ); @@ -607,7 +627,9 @@ describe('useSteerRecovery', () => { ]); }} > - + + + , ); diff --git a/client/src/hooks/Chat/contract.ts b/client/src/hooks/Chat/contract.ts index 313d89c07b..983a92ef8f 100644 --- a/client/src/hooks/Chat/contract.ts +++ b/client/src/hooks/Chat/contract.ts @@ -223,6 +223,16 @@ export type ChatSettings = { setIsTemporary: Dispatch>; config: ChatConfig; auth: ChatAuth; + /** The conversation each mounted chat pane shows, read when called rather than at render: + * steer recovery asks whether any pane holding a conversation is still running. */ + getPaneConversations: () => ReadonlyArray; +}; + +/** One mounted chat pane and the conversation it shows. */ +export type PaneConversation = { + /** The pane key, as the app keys its panes (`0` for the main chat). */ + index: string | number; + conversationId: string | null; }; /** The assistants abort route and the run it stops. */ diff --git a/client/src/hooks/Chat/useSteerRecovery.ts b/client/src/hooks/Chat/useSteerRecovery.ts index 3ec1005d72..ba63bb2289 100644 --- a/client/src/hooks/Chat/useSteerRecovery.ts +++ b/client/src/hooks/Chat/useSteerRecovery.ts @@ -12,6 +12,7 @@ import { import { useSteerMessageMutation, useCancelSteerMutation } from '~/data-provider'; import { useComposerRestoreHost } from '~/Providers/ComposerRestoreContext'; import { carriedSteerContext, isLegacyDeliveryUncertain } from '~/utils'; +import { useChatSettings } from '~/Providers/ChatSettingsContext'; import { useSteerRehome } from '~/hooks/Chat/useSteerCancel'; import useSteerConvert from '~/hooks/Chat/useSteerConvert'; import store from '~/store'; @@ -28,6 +29,7 @@ export default function useSteerRecovery(conversationId: string) { const rehomeSteer = useSteerRehome(conversationId); const { rewakeDrain } = useComposerRestoreHost(); const jotaiStore = useStore(); + const { getPaneConversations } = useChatSettings(); /** Whether the run this steer belongs to has finished, read live at ack time. * Unmounting is not the same signal: `PendingSteers` also unmounts when the @@ -36,17 +38,15 @@ export default function useSteerRecovery(conversationId: string) { * the server was already making. */ const isRunOver = useCallback( (snapshot: Snapshot) => { - const keys = snapshot.getLoadable(store.conversationKeysAtom).getValue(); let held = false; - for (const key of keys) { - const convo = snapshot.getLoadable(store.conversationByIndex(key)).getValue(); - if (convo?.conversationId !== conversationId) { + for (const pane of getPaneConversations()) { + if (pane.conversationId !== conversationId) { continue; } /* Comparison mode can hold one conversation in several panes, and only the pane that owns the run is submitting: any live pane means the run is still going, whichever slot comes first. */ - if (snapshot.getLoadable(store.isSubmittingFamily(key)).getValue() === true) { + if (snapshot.getLoadable(store.isSubmittingFamily(pane.index)).getValue() === true) { return false; } held = true; @@ -56,7 +56,7 @@ export default function useSteerRecovery(conversationId: string) { same mistake as reading the unmount as the end, one step further out. */ return held; }, - [conversationId], + [conversationId, getPaneConversations], ); const markStatus = useCallback( diff --git a/client/src/routes/ChatSettings.tsx b/client/src/routes/ChatSettings.tsx index 9e300a111d..5e0f9318fc 100644 --- a/client/src/routes/ChatSettings.tsx +++ b/client/src/routes/ChatSettings.tsx @@ -1,9 +1,9 @@ import { useMemo } from 'react'; import { useAtom } from 'jotai'; import { supportsConversationTitleOwnership } from 'librechat-data-provider'; -import { useRecoilState, useRecoilValue, useResetRecoilState } from 'recoil'; +import { useRecoilState, useRecoilValue, useRecoilCallback, useResetRecoilState } from 'recoil'; import type { ReactNode } from 'react'; -import type { ChatSettings, ChatConfig, ChatAuth } from '~/hooks/Chat/contract'; +import type { ChatAuth, ChatConfig, ChatSettings, PaneConversation } from '~/hooks/Chat/contract'; import { ChatTransportContext, defaultChatTransport } from '~/Providers/ChatTransportContext'; import { ChatSettingsContext } from '~/Providers/ChatSettingsContext'; import { duringRunActionAtom } from '~/store/duringRun'; @@ -11,6 +11,24 @@ import { useGetStartupConfig } from '~/data-provider'; import { useAuthContext } from '~/hooks/AuthContext'; import store from '~/store'; +/** Reads the conversation each mounted pane shows from the app's pane store, at call time. */ +export function usePaneConversations(): ChatSettings['getPaneConversations'] { + return useRecoilCallback( + ({ snapshot }) => + (): PaneConversation[] => + snapshot + .getLoadable(store.conversationKeysAtom) + .getValue() + .map((index) => ({ + index, + conversationId: + snapshot.getLoadable(store.conversationByIndex(index)).getValue()?.conversationId ?? + null, + })), + [], + ); +} + /** Supplies the chat's app-global preferences from the app's own settings store, the deployment * config and signed-in account from the app's queries, and the transport its turns run over. */ export default function ChatSettingsProvider({ children }: { children: ReactNode }) { @@ -34,6 +52,7 @@ export default function ChatSettingsProvider({ children }: { children: ReactNode }; }, [startupConfig]); const auth = useMemo(() => ({ user }), [user]); + const getPaneConversations = usePaneConversations(); const settings = useMemo( () => ({ @@ -45,6 +64,7 @@ export default function ChatSettingsProvider({ children }: { children: ReactNode setIsTemporary, config, auth, + getPaneConversations, }), [ duringRunDefaultAction, @@ -55,6 +75,7 @@ export default function ChatSettingsProvider({ children }: { children: ReactNode setIsTemporary, config, auth, + getPaneConversations, ], ); diff --git a/client/src/routes/__tests__/ChatSettings.spec.tsx b/client/src/routes/__tests__/ChatSettings.spec.tsx index bc5b47b3ee..4c5881b463 100644 --- a/client/src/routes/__tests__/ChatSettings.spec.tsx +++ b/client/src/routes/__tests__/ChatSettings.spec.tsx @@ -71,6 +71,22 @@ describe('ChatSettingsProvider', () => { expect(result.current.visibleArtifacts).toBeNull(); }); + it('reports the conversation each mounted pane shows when asked', () => { + const { result } = renderSettings(({ set }) => { + set(store.conversationKeysAtom, [0, 1]); + set(store.conversationByIndex(0), { conversationId: 'convo-a' } as never); + }); + + expect(result.current.settings.getPaneConversations()).toEqual([ + { index: 0, conversationId: 'convo-a' }, + { index: 1, conversationId: null }, + ]); + }); + + it('knows no panes when no host supplies settings', () => { + expect(defaultChatSettings.getPaneConversations()).toEqual([]); + }); + it('keeps feedback and running-chat rename off until the deployment answers', () => { const { result } = renderSettings();