mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 14:08:21 +00:00
🪟 refactor: Read Mounted Pane Conversations through ChatSettings in Steer Recovery (#17137)
This commit is contained in:
parent
c577e384ed
commit
4bd04b6f57
6 changed files with 88 additions and 18 deletions
|
|
@ -11,6 +11,7 @@ export const defaultChatSettings: ChatSettings = {
|
|||
setIsTemporary: () => undefined,
|
||||
config: { feedbackEnabled: false, canRenameRunningChat: false },
|
||||
auth: {},
|
||||
getPaneConversations: () => [],
|
||||
};
|
||||
|
||||
export const ChatSettingsContext = createContext<ChatSettings>(defaultChatSettings);
|
||||
|
|
|
|||
|
|
@ -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 <ChatSettingsContext.Provider value={settings}>{children}</ChatSettingsContext.Provider>;
|
||||
}
|
||||
|
||||
function setup(initialize?: (snapshot: MutableSnapshot) => void, rewake?: RewakeDrain) {
|
||||
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
<RecoilRoot
|
||||
|
|
@ -61,9 +73,11 @@ function setup(initialize?: (snapshot: MutableSnapshot) => void, rewake?: Rewake
|
|||
initialize?.(snapshot);
|
||||
}}
|
||||
>
|
||||
<ComposerRestoreProvider>
|
||||
{rewake ? <RewakePublisher rewake={rewake}>{children}</RewakePublisher> : children}
|
||||
</ComposerRestoreProvider>
|
||||
<PaneSettings>
|
||||
<ComposerRestoreProvider>
|
||||
{rewake ? <RewakePublisher rewake={rewake}>{children}</RewakePublisher> : children}
|
||||
</ComposerRestoreProvider>
|
||||
</PaneSettings>
|
||||
</RecoilRoot>
|
||||
);
|
||||
return renderHook(
|
||||
|
|
@ -279,9 +293,11 @@ describe('useSteerRecovery', () => {
|
|||
]);
|
||||
}}
|
||||
>
|
||||
<RewakePublisher rewake={rewake}>
|
||||
<Tree />
|
||||
</RewakePublisher>
|
||||
<PaneSettings>
|
||||
<RewakePublisher rewake={rewake}>
|
||||
<Tree />
|
||||
</RewakePublisher>
|
||||
</PaneSettings>
|
||||
</RecoilRoot>
|
||||
</ComposerRestoreProvider>,
|
||||
);
|
||||
|
|
@ -381,7 +397,9 @@ describe('useSteerRecovery', () => {
|
|||
]);
|
||||
}}
|
||||
>
|
||||
<Tree />
|
||||
<PaneSettings>
|
||||
<Tree />
|
||||
</PaneSettings>
|
||||
</RecoilRoot>
|
||||
</ComposerRestoreProvider>,
|
||||
);
|
||||
|
|
@ -540,8 +558,10 @@ describe('useSteerRecovery', () => {
|
|||
]);
|
||||
}}
|
||||
>
|
||||
<Observer />
|
||||
{live && <Recovery />}
|
||||
<PaneSettings>
|
||||
<Observer />
|
||||
{live && <Recovery />}
|
||||
</PaneSettings>
|
||||
</RecoilRoot>
|
||||
);
|
||||
|
||||
|
|
@ -607,7 +627,9 @@ describe('useSteerRecovery', () => {
|
|||
]);
|
||||
}}
|
||||
>
|
||||
<Tree />
|
||||
<PaneSettings>
|
||||
<Tree />
|
||||
</PaneSettings>
|
||||
</RecoilRoot>,
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -223,6 +223,16 @@ export type ChatSettings = {
|
|||
setIsTemporary: Dispatch<SetStateAction<boolean>>;
|
||||
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<PaneConversation>;
|
||||
};
|
||||
|
||||
/** 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. */
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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<ChatAuth>(() => ({ user }), [user]);
|
||||
const getPaneConversations = usePaneConversations();
|
||||
|
||||
const settings = useMemo<ChatSettings>(
|
||||
() => ({
|
||||
|
|
@ -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,
|
||||
],
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue