mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-20 06:51:59 +00:00
🎴 feat: Switch Side-Chat Models With @ Mentions (#18547)
* 🎯 feat: Let @ Pick the Model of a Conversation Open Beside the Chat * 🎚️ fix: Hand the Modular Chat Preference to the Mention List Instead of Reading the Store
This commit is contained in:
parent
d41ff8e8c8
commit
c1c5afeb27
14 changed files with 474 additions and 66 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import { useMemo } from 'react';
|
||||
import { SIDE_CHAT_PANE } from '@librechat/chat/react';
|
||||
import { Constants, QueryKeys } from 'librechat-data-provider';
|
||||
import { hashQueryKey, useQueryClient } from '@tanstack/react-query';
|
||||
import type { ChatHost, ChatMessageCache, ChatMessagesQuery } from '@librechat/chat/react';
|
||||
|
|
@ -112,7 +113,9 @@ function useMessages(
|
|||
* services, thread selection, transport and settings. */
|
||||
export const appChatHost: ChatHost = {
|
||||
usePaneConversation: (index) => useChatSettings().usePaneConversationState(index),
|
||||
useNewConversation: (index) => useNewConvo(index).newConversation,
|
||||
/** A conversation beside the chat has no route: its pane changes it in place or not at all. */
|
||||
useNewConversation: (index) =>
|
||||
useNewConvo(index, { routed: index !== SIDE_CHAT_PANE }).newConversation,
|
||||
useTurnServices,
|
||||
/** `mutateAsync` is bound once per observer; the mutation result object is fresh every
|
||||
* render and would defeat the contract's memo. */
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@ export type ComposerPreferences = {
|
|||
autoSendText: number;
|
||||
/** Whether the shell has loaded the speech settings yet; dictation stays off until it has. */
|
||||
speechSettingsInitialized: boolean;
|
||||
/** Whether a conversation may change endpoint and keep its messages, or starts anew instead. */
|
||||
modularChat: boolean;
|
||||
};
|
||||
|
||||
/** The preferences' own defaults, for a composer drawn outside a chat view. */
|
||||
|
|
@ -19,6 +21,7 @@ export const defaultComposerPreferences: ComposerPreferences = {
|
|||
enterToSend: true,
|
||||
autoSendText: -1,
|
||||
speechSettingsInitialized: false,
|
||||
modularChat: true,
|
||||
};
|
||||
|
||||
export const ComposerPreferencesContext = createContext<ComposerPreferences>(
|
||||
|
|
|
|||
|
|
@ -82,6 +82,7 @@ function ChatViewPane({
|
|||
const { saveDrafts } = useChatSettings();
|
||||
const enterToSend = useRecoilValue(store.enterToSend);
|
||||
const autoSendText = useRecoilValue(store.autoSendText);
|
||||
const modularChat = useRecoilValue(store.modularChat);
|
||||
const speechSettingsInitialized = useRecoilValue(store.speechSettingsInitialized);
|
||||
const centerFormOnLanding = useRecoilValue(store.centerFormOnLanding);
|
||||
const pendingAction = useAtomValue(
|
||||
|
|
@ -111,8 +112,8 @@ function ChatViewPane({
|
|||
defaultValues: { text: '' },
|
||||
});
|
||||
const composerPreferences = useMemo(
|
||||
() => ({ enterToSend, autoSendText, speechSettingsInitialized }),
|
||||
[enterToSend, autoSendText, speechSettingsInitialized],
|
||||
() => ({ enterToSend, autoSendText, speechSettingsInitialized, modularChat }),
|
||||
[enterToSend, autoSendText, speechSettingsInitialized, modularChat],
|
||||
);
|
||||
|
||||
const fileMap = useFileMapContext();
|
||||
|
|
|
|||
|
|
@ -142,6 +142,8 @@ interface ChatFormProps {
|
|||
stopsUnnamed: boolean;
|
||||
/** From ChatContext: the chat takes the composer's during-run actions though not main chat. */
|
||||
steers: boolean;
|
||||
/** From ChatContext: the chat changes its conversation's model in place though not main chat. */
|
||||
retargets: boolean;
|
||||
}
|
||||
|
||||
const ChatForm = memo(function ChatForm({
|
||||
|
|
@ -166,6 +168,7 @@ const ChatForm = memo(function ChatForm({
|
|||
stopGenerating,
|
||||
stopsUnnamed,
|
||||
steers,
|
||||
retargets,
|
||||
}: ChatFormProps) {
|
||||
const submitButtonRef = useRef<HTMLButtonElement>(null);
|
||||
const textAreaRef = useRef<HTMLTextAreaElement>(null);
|
||||
|
|
@ -509,11 +512,18 @@ const ChatForm = memo(function ChatForm({
|
|||
const idleActions = useMemo(
|
||||
() => ({
|
||||
prompts: hasPromptsAccess && slashCommandEnabled,
|
||||
/* A mention picks the model for main chat's conversation, so only its composer offers it. */
|
||||
mentions: atCommandEnabled && routed,
|
||||
/* A mention picks the conversation's model: main chat's, or one that is changed in place. */
|
||||
mentions: atCommandEnabled && (routed || retargets),
|
||||
attach: attachTarget.canAttach,
|
||||
}),
|
||||
[hasPromptsAccess, slashCommandEnabled, atCommandEnabled, attachTarget.canAttach, routed],
|
||||
[
|
||||
routed,
|
||||
retargets,
|
||||
hasPromptsAccess,
|
||||
atCommandEnabled,
|
||||
slashCommandEnabled,
|
||||
attachTarget.canAttach,
|
||||
],
|
||||
);
|
||||
const { submitText: submitAnswerText } = answerMode;
|
||||
const dictationAnswerModeActive = answerMode.composerAnswers;
|
||||
|
|
@ -740,27 +750,28 @@ const ChatForm = memo(function ChatForm({
|
|||
popovers={
|
||||
<>
|
||||
{routed && (
|
||||
<>
|
||||
<Suspense fallback={null}>
|
||||
<Mention
|
||||
index={index}
|
||||
popoverAtom={plusPopoverAtom}
|
||||
newConversation={generateConversation}
|
||||
textAreaRef={textAreaRef}
|
||||
commandChar="+"
|
||||
placeholder="com_ui_add_model_preset"
|
||||
includeAssistants={false}
|
||||
/>
|
||||
</Suspense>
|
||||
<Suspense fallback={null}>
|
||||
<Mention
|
||||
index={index}
|
||||
popoverAtom={mentionPopoverAtom}
|
||||
newConversation={newConversation}
|
||||
textAreaRef={textAreaRef}
|
||||
/>
|
||||
</Suspense>
|
||||
</>
|
||||
<Suspense fallback={null}>
|
||||
<Mention
|
||||
index={index}
|
||||
popoverAtom={plusPopoverAtom}
|
||||
newConversation={generateConversation}
|
||||
textAreaRef={textAreaRef}
|
||||
commandChar="+"
|
||||
placeholder="com_ui_add_model_preset"
|
||||
includeAssistants={false}
|
||||
/>
|
||||
</Suspense>
|
||||
)}
|
||||
{(routed || retargets) && (
|
||||
<Suspense fallback={null}>
|
||||
<Mention
|
||||
index={index}
|
||||
popoverAtom={mentionPopoverAtom}
|
||||
newConversation={newConversation}
|
||||
textAreaRef={textAreaRef}
|
||||
inPlace={!routed}
|
||||
/>
|
||||
</Suspense>
|
||||
)}
|
||||
<Suspense fallback={null}>
|
||||
<PromptsCommand index={index} textAreaRef={textAreaRef} submitPrompt={submitPrompt} />
|
||||
|
|
@ -979,6 +990,7 @@ function ChatFormWrapper({
|
|||
stopGenerating,
|
||||
stopsUnnamed = false,
|
||||
steers = false,
|
||||
retargets = false,
|
||||
} = useChatContext();
|
||||
|
||||
/**
|
||||
|
|
@ -1061,6 +1073,7 @@ function ChatFormWrapper({
|
|||
stopGenerating={stableStop}
|
||||
stopsUnnamed={stopsUnnamed}
|
||||
steers={steers}
|
||||
retargets={retargets}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
import { memo } from 'react';
|
||||
import { memo, useMemo } from 'react';
|
||||
import { useRecoilValue, useSetRecoilState } from 'recoil';
|
||||
import { MentionPopover } from '@librechat/chat/components';
|
||||
import type { RecoilState } from 'recoil';
|
||||
import type { ConvoGenerator } from '~/common';
|
||||
import useSelectMention, { mentionEndpoint } from '~/hooks/Input/useSelectMention';
|
||||
import { useComposerPreferences } from '~/Providers/ComposerPreferencesContext';
|
||||
import { useGetConversation, useLocalize, TranslationKeys } from '~/hooks';
|
||||
import useSelectMention from '~/hooks/Input/useSelectMention';
|
||||
import { getConvoSwitchLogic, switchesInPlace } from '~/utils';
|
||||
import { useAssistantsMapContext } from '~/Providers';
|
||||
import useMentions from '~/hooks/Input/useMentions';
|
||||
|
||||
|
|
@ -16,18 +18,27 @@ type MentionProps = {
|
|||
commandChar?: string;
|
||||
placeholder?: TranslationKeys;
|
||||
includeAssistants?: boolean;
|
||||
/**
|
||||
* Offers only the choices that change the pane's conversation in place, for a pane that
|
||||
* cannot start another one: a conversation open beside the chat.
|
||||
*/
|
||||
inPlace?: boolean;
|
||||
};
|
||||
|
||||
function MentionContent({
|
||||
index,
|
||||
popoverAtom,
|
||||
newConversation,
|
||||
textAreaRef,
|
||||
commandChar = '@',
|
||||
placeholder = 'com_ui_mention',
|
||||
includeAssistants = true,
|
||||
}: Omit<MentionProps, 'index'>) {
|
||||
inPlace = false,
|
||||
}: MentionProps) {
|
||||
const localize = useLocalize();
|
||||
const getConversation = useGetConversation(0);
|
||||
const getConversation = useGetConversation(index);
|
||||
/** The reader's preference as the chat view handed it down, for the list and the pick alike. */
|
||||
const { modularChat } = useComposerPreferences();
|
||||
const assistantsMap = useAssistantsMapContext();
|
||||
const setShowPopover = useSetRecoilState(popoverAtom);
|
||||
const {
|
||||
|
|
@ -44,17 +55,36 @@ function MentionContent({
|
|||
presets,
|
||||
modelSpecs,
|
||||
assistantsMap,
|
||||
modularChat,
|
||||
endpointsConfig,
|
||||
getConversation,
|
||||
newConversation,
|
||||
});
|
||||
|
||||
/** Decided by the rule the selection itself follows, so what is offered is what is done. */
|
||||
const offered = useMemo(() => {
|
||||
if (!inPlace) {
|
||||
return options;
|
||||
}
|
||||
const conversation = getConversation();
|
||||
return options.filter((option) => {
|
||||
const newEndpoint = mentionEndpoint(option, { presets, modelSpecs });
|
||||
return (
|
||||
newEndpoint != null &&
|
||||
newEndpoint !== '' &&
|
||||
switchesInPlace(
|
||||
getConvoSwitchLogic({ newEndpoint, modularChat, conversation, endpointsConfig }),
|
||||
)
|
||||
);
|
||||
});
|
||||
}, [inPlace, options, presets, modelSpecs, modularChat, endpointsConfig, getConversation]);
|
||||
|
||||
return (
|
||||
<MentionPopover
|
||||
textAreaRef={textAreaRef}
|
||||
commandChar={commandChar}
|
||||
placeholder={localize(placeholder)}
|
||||
options={options}
|
||||
options={offered}
|
||||
isLoading={isLoading}
|
||||
agentsList={agentsList}
|
||||
assistantListMap={assistantListMap}
|
||||
|
|
@ -66,7 +96,6 @@ function MentionContent({
|
|||
}
|
||||
|
||||
const MentionPopoverContainer = memo(function MentionPopoverContainer({
|
||||
index: _index,
|
||||
popoverAtom,
|
||||
...rest
|
||||
}: MentionProps) {
|
||||
|
|
|
|||
|
|
@ -160,6 +160,12 @@ export default function SideChatSurface({
|
|||
};
|
||||
}, [conversationId, index]);
|
||||
|
||||
/**
|
||||
* The app's `newConversation` for this pane changes the conversation in place and never
|
||||
* leaves it, so the composer may offer the choices that do that.
|
||||
*/
|
||||
const chat = useMemo(() => ({ ...contract, retargets: true }), [contract]);
|
||||
|
||||
const form = useRef<HTMLDivElement>(null);
|
||||
const focused = useRef(false);
|
||||
useEffect(() => {
|
||||
|
|
@ -175,7 +181,7 @@ export default function SideChatSurface({
|
|||
}, [focusWhenReady, ready, conversation?.endpoint]);
|
||||
|
||||
return (
|
||||
<ChatContext.Provider value={contract}>
|
||||
<ChatContext.Provider value={chat}>
|
||||
<TargetContext.Provider value={readiness}>
|
||||
<ChatFormProvider {...methods}>
|
||||
<ComposerRestoreProvider>
|
||||
|
|
|
|||
93
client/src/hooks/Input/__tests__/useSelectMention.spec.ts
Normal file
93
client/src/hooks/Input/__tests__/useSelectMention.spec.ts
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
import { renderHook } from '@testing-library/react';
|
||||
import { EModelEndpoint } from 'librechat-data-provider';
|
||||
import type { TPreset, TModelSpec, TConversation, TEndpointsConfig } from 'librechat-data-provider';
|
||||
import type { MentionOption } from '~/common';
|
||||
import useSelectMention, { mentionEndpoint } from '../useSelectMention';
|
||||
|
||||
let mockStoredModularChat = true;
|
||||
jest.mock('recoil', () => ({
|
||||
useRecoilValue: (key: string) => (key === 'modularChat' ? mockStoredModularChat : []),
|
||||
}));
|
||||
jest.mock('react-router-dom', () => ({ useSearchParams: () => [new URLSearchParams()] }));
|
||||
jest.mock('~/hooks', () => ({
|
||||
useDefaultConvo:
|
||||
() =>
|
||||
({ conversation }: { conversation: Partial<TConversation> }) =>
|
||||
conversation,
|
||||
}));
|
||||
jest.mock('~/store', () => ({
|
||||
__esModule: true,
|
||||
default: { modularChat: 'modularChat', availableTools: 'availableTools' },
|
||||
}));
|
||||
|
||||
const option = (type: string, value: string): MentionOption =>
|
||||
({ type, value, label: value }) as MentionOption;
|
||||
|
||||
describe('mentionEndpoint', () => {
|
||||
const presets = [{ presetId: 'p-1', endpoint: EModelEndpoint.google }] as TPreset[];
|
||||
const modelSpecs = [
|
||||
{ name: 'fast', preset: { endpoint: EModelEndpoint.anthropic } },
|
||||
] as TModelSpec[];
|
||||
const from = (type: string, value: string) =>
|
||||
mentionEndpoint(option(type, value), { presets, modelSpecs });
|
||||
|
||||
it('reads an endpoint and one of its models from the option itself', () => {
|
||||
expect(from('endpoint', EModelEndpoint.openAI)).toBe(EModelEndpoint.openAI);
|
||||
expect(from('model', 'Mistral')).toBe('Mistral');
|
||||
});
|
||||
|
||||
it('reads a preset and a model spec from what they are set to', () => {
|
||||
expect(from('preset', 'p-1')).toBe(EModelEndpoint.google);
|
||||
expect(from('modelSpec', 'fast')).toBe(EModelEndpoint.anthropic);
|
||||
});
|
||||
|
||||
it('names nothing for a preset or a model spec that is not there', () => {
|
||||
expect(from('preset', 'gone')).toBeUndefined();
|
||||
expect(from('modelSpec', 'gone')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('reads an agent and an assistant from the endpoint the option is listed under', () => {
|
||||
expect(from(EModelEndpoint.agents, 'agent_1')).toBe(EModelEndpoint.agents);
|
||||
expect(from(EModelEndpoint.assistants, 'asst_1')).toBe(EModelEndpoint.assistants);
|
||||
});
|
||||
});
|
||||
|
||||
describe('useSelectMention', () => {
|
||||
const open = { conversationId: 'c-1', endpoint: EModelEndpoint.openAI } as TConversation;
|
||||
const endpointsConfig = {
|
||||
[EModelEndpoint.openAI]: { order: 0 },
|
||||
[EModelEndpoint.google]: { order: 1 },
|
||||
} as TEndpointsConfig;
|
||||
/** Picks another endpoint, and says which conversation the pick was handed to. */
|
||||
const pick = (modularChat?: boolean): string | null | undefined => {
|
||||
const newConversation = jest.fn();
|
||||
const { result } = renderHook(() =>
|
||||
useSelectMention({
|
||||
modularChat,
|
||||
modelSpecs: [],
|
||||
endpointsConfig,
|
||||
newConversation,
|
||||
getConversation: () => open,
|
||||
}),
|
||||
);
|
||||
result.current.onSelectMention?.(option('endpoint', EModelEndpoint.google));
|
||||
return newConversation.mock.calls[0][0].template.conversationId;
|
||||
};
|
||||
afterEach(() => {
|
||||
mockStoredModularChat = true;
|
||||
});
|
||||
|
||||
it('follows the preference it is handed over the one the app has stored', () => {
|
||||
mockStoredModularChat = true;
|
||||
expect(pick(false)).toBe('new');
|
||||
mockStoredModularChat = false;
|
||||
expect(pick(true)).toBe('c-1');
|
||||
});
|
||||
|
||||
it('reads the app’s own preference when it is handed none', () => {
|
||||
mockStoredModularChat = true;
|
||||
expect(pick()).toBe('c-1');
|
||||
mockStoredModularChat = false;
|
||||
expect(pick()).toBe('new');
|
||||
});
|
||||
});
|
||||
|
|
@ -15,14 +15,36 @@ import {
|
|||
removeUnavailableTools,
|
||||
getModelSpecIconURL,
|
||||
getConvoSwitchLogic,
|
||||
switchesInPlace,
|
||||
logger,
|
||||
} from '~/utils';
|
||||
import { useDefaultConvo } from '~/hooks';
|
||||
import store from '~/store';
|
||||
|
||||
/**
|
||||
* The endpoint a mention would switch the conversation to, as {@link useSelectMention} reads
|
||||
* it from the option; nothing for a preset or a model spec that is no longer there.
|
||||
*/
|
||||
export function mentionEndpoint(
|
||||
option: MentionOption,
|
||||
{ presets, modelSpecs }: { presets?: TPreset[]; modelSpecs: TModelSpec[] },
|
||||
): string | null | undefined {
|
||||
if (option.type === 'preset') {
|
||||
return presets?.find((preset) => preset.presetId === option.value)?.endpoint;
|
||||
}
|
||||
if (option.type === 'modelSpec') {
|
||||
return modelSpecs.find((spec) => spec.name === option.value)?.preset.endpoint;
|
||||
}
|
||||
if (option.type === 'model' || option.type === 'endpoint') {
|
||||
return option.value;
|
||||
}
|
||||
return option.type;
|
||||
}
|
||||
|
||||
export default function useSelectMention({
|
||||
presets,
|
||||
modelSpecs,
|
||||
modularChat: given,
|
||||
assistantsMap,
|
||||
returnHandlers,
|
||||
endpointsConfig,
|
||||
|
|
@ -31,6 +53,11 @@ export default function useSelectMention({
|
|||
}: {
|
||||
presets?: TPreset[];
|
||||
modelSpecs: TModelSpec[];
|
||||
/**
|
||||
* The reader's Modular Chat preference, from a caller that was handed it. A caller that was
|
||||
* not leaves it out, and the app's own setting is read.
|
||||
*/
|
||||
modularChat?: boolean;
|
||||
returnHandlers?: boolean;
|
||||
assistantsMap?: TAssistantsMap;
|
||||
newConversation: ConvoGenerator;
|
||||
|
|
@ -38,7 +65,8 @@ export default function useSelectMention({
|
|||
getConversation: () => TConversation | null;
|
||||
}) {
|
||||
const getDefaultConversation = useDefaultConvo();
|
||||
const modularChat = useRecoilValue(store.modularChat);
|
||||
const stored = useRecoilValue(store.modularChat);
|
||||
const modularChat = given ?? stored;
|
||||
const availableTools = useRecoilValue(store.availableTools);
|
||||
const [searchParams] = useSearchParams();
|
||||
/**
|
||||
|
|
@ -69,19 +97,13 @@ export default function useSelectMention({
|
|||
return;
|
||||
}
|
||||
|
||||
const {
|
||||
template,
|
||||
shouldSwitch,
|
||||
isNewModular,
|
||||
newEndpointType,
|
||||
isCurrentModular,
|
||||
isExistingConversation,
|
||||
} = getConvoSwitchLogic({
|
||||
const logic = getConvoSwitchLogic({
|
||||
newEndpoint,
|
||||
modularChat,
|
||||
conversation,
|
||||
endpointsConfig,
|
||||
});
|
||||
const { template, shouldSwitch, isNewModular, newEndpointType, isCurrentModular } = logic;
|
||||
|
||||
if (newEndpointType) {
|
||||
preset.endpointType = newEndpointType;
|
||||
|
|
@ -96,7 +118,7 @@ export default function useSelectMention({
|
|||
}
|
||||
|
||||
const isModular = isCurrentModular && isNewModular && shouldSwitch;
|
||||
if (isExistingConversation && isModular) {
|
||||
if (switchesInPlace(logic)) {
|
||||
template.endpointType = newEndpointType as EModelEndpoint | undefined;
|
||||
|
||||
const currentConvo = getDefaultConversation({
|
||||
|
|
@ -153,19 +175,13 @@ export default function useSelectMention({
|
|||
|
||||
const conversation = getConversation();
|
||||
|
||||
const {
|
||||
shouldSwitch,
|
||||
isNewModular,
|
||||
isCurrentModular,
|
||||
isExistingConversation,
|
||||
newEndpointType,
|
||||
template,
|
||||
} = getConvoSwitchLogic({
|
||||
const logic = getConvoSwitchLogic({
|
||||
newEndpoint,
|
||||
modularChat,
|
||||
conversation,
|
||||
endpointsConfig,
|
||||
});
|
||||
const { isNewModular, newEndpointType, template } = logic;
|
||||
|
||||
const model = kwargs.model ?? '';
|
||||
if (model) {
|
||||
|
|
@ -185,7 +201,7 @@ export default function useSelectMention({
|
|||
template.spec = null;
|
||||
template.iconURL = null;
|
||||
template.modelLabel = null;
|
||||
if (isExistingConversation && isCurrentModular && isNewModular && shouldSwitch) {
|
||||
if (switchesInPlace(logic)) {
|
||||
template.endpointType = newEndpointType;
|
||||
|
||||
const currentConvo = getDefaultConversation({
|
||||
|
|
@ -245,26 +261,20 @@ export default function useSelectMention({
|
|||
const newPreset = removeUnavailableTools(_newPreset, availableTools);
|
||||
const newEndpoint = newPreset.endpoint ?? '';
|
||||
|
||||
const {
|
||||
template,
|
||||
shouldSwitch,
|
||||
isNewModular,
|
||||
newEndpointType,
|
||||
isCurrentModular,
|
||||
isExistingConversation,
|
||||
} = getConvoSwitchLogic({
|
||||
const logic = getConvoSwitchLogic({
|
||||
newEndpoint,
|
||||
modularChat,
|
||||
conversation,
|
||||
endpointsConfig,
|
||||
});
|
||||
const { template, shouldSwitch, isNewModular, newEndpointType, isCurrentModular } = logic;
|
||||
|
||||
newPreset.spec = null;
|
||||
newPreset.iconURL = newPreset.iconURL ?? null;
|
||||
newPreset.modelLabel = newPreset.modelLabel ?? null;
|
||||
const isModular = isCurrentModular && isNewModular && shouldSwitch;
|
||||
const disableParams = newPreset.defaultPreset === true;
|
||||
if (isExistingConversation && isModular) {
|
||||
if (switchesInPlace(logic)) {
|
||||
template.endpointType = newEndpointType as EModelEndpoint | undefined;
|
||||
template.spec = null;
|
||||
template.iconURL = null;
|
||||
|
|
|
|||
|
|
@ -1873,6 +1873,85 @@ describe('passive route navigation', () => {
|
|||
);
|
||||
});
|
||||
|
||||
describe('a pane the route does not address', () => {
|
||||
const held = 'c1b2c3d4-0000-4000-8000-000000000001';
|
||||
const change = {
|
||||
template: { conversationId: held, endpoint: EModelEndpoint.anthropic, model: 'chosen' },
|
||||
preset: { endpoint: EModelEndpoint.anthropic, model: 'chosen' },
|
||||
keepAddedConvos: true,
|
||||
buildDefault: false,
|
||||
disableFocus: true,
|
||||
};
|
||||
beforeEach(() => {
|
||||
mockRunSwitch = true;
|
||||
mockNavigate.mockClear();
|
||||
mockSetConversation.mockClear();
|
||||
mockPreviousConversation = { conversationId: held, endpoint: EModelEndpoint.openAI };
|
||||
});
|
||||
afterEach(() => {
|
||||
mockRunSwitch = false;
|
||||
mockPreviousConversation = null;
|
||||
});
|
||||
|
||||
it('changes the conversation it holds, and leaves the route where it is', async () => {
|
||||
const { result } = renderHook(() => useNewConvo(3, { routed: false }), {
|
||||
wrapper: QueryWrapper,
|
||||
});
|
||||
await act(async () => result.current.newConversation(change));
|
||||
|
||||
expect(mockSetConversation).toHaveBeenCalledTimes(1);
|
||||
expect(mockSetConversation).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
conversationId: held,
|
||||
endpoint: EModelEndpoint.anthropic,
|
||||
model: 'chosen',
|
||||
}),
|
||||
);
|
||||
expect(mockNavigate).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('takes nothing that would put another conversation in its place', async () => {
|
||||
const { result } = renderHook(() => useNewConvo(3, { routed: false }), {
|
||||
wrapper: QueryWrapper,
|
||||
});
|
||||
await act(async () => {
|
||||
result.current.newConversation();
|
||||
result.current.newConversation({ template: { endpoint: EModelEndpoint.anthropic } });
|
||||
result.current.newConversation({
|
||||
template: { ...change.template, conversationId: String(Constants.NEW_CONVO) },
|
||||
});
|
||||
result.current.newConversation({
|
||||
template: { ...change.template, conversationId: 'another-conversation' },
|
||||
});
|
||||
});
|
||||
|
||||
expect(mockSetConversation).not.toHaveBeenCalled();
|
||||
expect(mockNavigate).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('holds nothing to change before it has a saved conversation', async () => {
|
||||
mockPreviousConversation = { conversationId: 'new', endpoint: EModelEndpoint.openAI };
|
||||
const { result } = renderHook(() => useNewConvo(3, { routed: false }), {
|
||||
wrapper: QueryWrapper,
|
||||
});
|
||||
await act(async () =>
|
||||
result.current.newConversation({
|
||||
template: { ...change.template, conversationId: String(Constants.NEW_CONVO) },
|
||||
}),
|
||||
);
|
||||
|
||||
expect(mockSetConversation).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('is the route’s to move for the pane the route does address', async () => {
|
||||
const { result } = renderHook(() => useNewConvo(0), { wrapper: QueryWrapper });
|
||||
await act(async () => result.current.newConversation(change));
|
||||
|
||||
expect(mockSetConversation).toHaveBeenCalledTimes(1);
|
||||
expect(mockNavigate).toHaveBeenCalledWith(`/c/${held}`, { replace: true });
|
||||
});
|
||||
});
|
||||
|
||||
describe('remembered New Chat identity', () => {
|
||||
beforeEach(() => {
|
||||
localStorage.clear();
|
||||
|
|
|
|||
|
|
@ -118,7 +118,13 @@ function restoreRememberedTarget(
|
|||
return { ...conversation, endpoint, assistant_id: assistantId, model };
|
||||
}
|
||||
|
||||
const useNewConvo = (index = 0) => {
|
||||
/**
|
||||
* `routed` is false for a pane the page's route does not address, such as a conversation open
|
||||
* beside the chat. Such a pane holds one conversation: it takes a change to that conversation
|
||||
* (its model, endpoint or agent) and nothing that would put another in its place, and nothing
|
||||
* it does moves the route or the page's focus.
|
||||
*/
|
||||
const useNewConvo = (index = 0, { routed = true }: { routed?: boolean } = {}) => {
|
||||
const navigate = useNavigate();
|
||||
const [searchParams] = useSearchParams();
|
||||
const { data: startupConfig } = useGetStartupConfig();
|
||||
|
|
@ -294,7 +300,7 @@ const useNewConvo = (index = 0) => {
|
|||
setConversation(conversation);
|
||||
}
|
||||
setSubmission({} as TSubmission);
|
||||
if (isCancelled) {
|
||||
if (isCancelled || !routed) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -338,7 +344,7 @@ const useNewConvo = (index = 0) => {
|
|||
}
|
||||
navigate(path, { replace: true });
|
||||
},
|
||||
[index, endpointsConfig, defaultPreset, queryClient, hasAgentAccess, searchParams],
|
||||
[index, routed, endpointsConfig, defaultPreset, queryClient, hasAgentAccess, searchParams],
|
||||
);
|
||||
|
||||
const newConversation = useCallback(
|
||||
|
|
@ -353,6 +359,12 @@ const useNewConvo = (index = 0) => {
|
|||
disableParams,
|
||||
}: NewConversationOptions = {}) {
|
||||
const nextConversationId = _template.conversationId ?? '';
|
||||
if (!routed) {
|
||||
const held = getConversation()?.conversationId ?? '';
|
||||
if (held === '' || held === Constants.NEW_CONVO || nextConversationId !== held) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
const keepsExistingDraft =
|
||||
modelsData != null ||
|
||||
keepComposerState ||
|
||||
|
|
@ -541,7 +553,7 @@ const useNewConvo = (index = 0) => {
|
|||
* call already opened, such as agent metadata arriving late. The user did
|
||||
* not ask for anything there, so it must not cancel a conversation they
|
||||
* clicked while it was in flight. */
|
||||
if (!keepComposerState) {
|
||||
if (!keepComposerState && routed) {
|
||||
supersedeNavigation();
|
||||
}
|
||||
|
||||
|
|
@ -559,6 +571,7 @@ const useNewConvo = (index = 0) => {
|
|||
[
|
||||
index,
|
||||
files,
|
||||
routed,
|
||||
setFiles,
|
||||
agentsMap,
|
||||
saveDrafts,
|
||||
|
|
|
|||
|
|
@ -1,11 +1,14 @@
|
|||
import { EModelEndpoint, getEndpointField } from 'librechat-data-provider';
|
||||
import type { TEndpointsConfig, TConfig, TModelSpec } from 'librechat-data-provider';
|
||||
import type { TConversation } from 'librechat-data-provider';
|
||||
import {
|
||||
getAvailableEndpoints,
|
||||
getConvoSwitchLogic,
|
||||
getEndpointsFilter,
|
||||
getSpecAgentAvatarURL,
|
||||
mapEndpoints,
|
||||
normalizeModelSpecs,
|
||||
switchesInPlace,
|
||||
} from './endpoints';
|
||||
|
||||
const mockEndpointsConfig: TEndpointsConfig = {
|
||||
|
|
@ -148,3 +151,43 @@ describe('getSpecAgentAvatarURL', () => {
|
|||
expect(getSpecAgentAvatarURL(agentSpec(undefined), agentsMap)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('switchesInPlace', () => {
|
||||
const open = (endpoint: string, conversationId = 'c-1') =>
|
||||
({ conversationId, endpoint }) as TConversation;
|
||||
const decide = (
|
||||
conversation: TConversation | null,
|
||||
newEndpoint: string,
|
||||
modularChat = true,
|
||||
): boolean =>
|
||||
switchesInPlace(
|
||||
getConvoSwitchLogic({
|
||||
newEndpoint,
|
||||
modularChat,
|
||||
conversation,
|
||||
endpointsConfig: mockEndpointsConfig,
|
||||
}),
|
||||
);
|
||||
|
||||
it('changes an open conversation between endpoints that share a conversation', () => {
|
||||
expect(decide(open(EModelEndpoint.openAI), EModelEndpoint.google)).toBe(true);
|
||||
expect(decide(open(EModelEndpoint.openAI), 'Mistral')).toBe(true);
|
||||
expect(decide(open('Mistral'), EModelEndpoint.agents)).toBe(true);
|
||||
});
|
||||
|
||||
it('changes only within the same endpoint when modular chat is off', () => {
|
||||
expect(decide(open(EModelEndpoint.openAI), EModelEndpoint.openAI, false)).toBe(true);
|
||||
expect(decide(open(EModelEndpoint.openAI), EModelEndpoint.google, false)).toBe(false);
|
||||
});
|
||||
|
||||
it('starts another conversation for an assistant, unless it is the same assistants endpoint', () => {
|
||||
expect(decide(open(EModelEndpoint.openAI), EModelEndpoint.assistants)).toBe(false);
|
||||
expect(decide(open(EModelEndpoint.assistants), EModelEndpoint.openAI)).toBe(false);
|
||||
expect(decide(open(EModelEndpoint.assistants), EModelEndpoint.assistants)).toBe(true);
|
||||
});
|
||||
|
||||
it('has nothing to change before a conversation is saved', () => {
|
||||
expect(decide(null, EModelEndpoint.openAI)).toBe(false);
|
||||
expect(decide(open(EModelEndpoint.openAI, 'new'), EModelEndpoint.openAI)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -265,6 +265,18 @@ function hasSelectableEntitySelection({
|
|||
return endpointsConfig[endpoint] != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the switch {@link getConvoSwitchLogic} describes changes the conversation that is
|
||||
* open, keeping its messages, instead of starting another one.
|
||||
*/
|
||||
export const switchesInPlace = ({
|
||||
shouldSwitch,
|
||||
isNewModular,
|
||||
isCurrentModular,
|
||||
isExistingConversation,
|
||||
}: Omit<InitiatedTemplateResult, 'template' | 'newEndpointType'>): boolean =>
|
||||
isExistingConversation && isCurrentModular && isNewModular && shouldSwitch;
|
||||
|
||||
/** Get the conditional logic for switching conversations */
|
||||
export function getConvoSwitchLogic(params: ConversationInitParams): InitiatedTemplateResult {
|
||||
const { conversation, newEndpoint, endpointsConfig, modularChat = false } = params;
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import {
|
|||
MOCK_ENDPOINTS,
|
||||
conversationRow,
|
||||
selectMockEndpoint,
|
||||
modelSelectorTrigger,
|
||||
sendMessageAndWaitForCompletion,
|
||||
} from '../helpers';
|
||||
import { openChatHistory } from './sidebar';
|
||||
|
|
@ -471,3 +472,99 @@ test('the gauge beside the chat counts a reply as it is written @scenario:side-c
|
|||
await expect(stop).toBeVisible();
|
||||
expect(late).toBeGreaterThan(early);
|
||||
});
|
||||
|
||||
/** Opens the side composer's `@` list, and gives its field and the rows it offers. */
|
||||
async function openSideMention(page: Page, sideChat: Locator, field: Locator) {
|
||||
await field.click();
|
||||
await field.press('@');
|
||||
const combobox = sideChat.getByRole('combobox', { name: /Mention/ });
|
||||
await expect(combobox).toBeFocused();
|
||||
const listbox = page.locator(`[id="${await combobox.getAttribute('aria-controls')}"]`);
|
||||
await expect(listbox.getByRole('option').first()).toBeVisible();
|
||||
return { combobox, options: listbox.getByRole('option') };
|
||||
}
|
||||
|
||||
test('a model picked with @ in a side chat changes that conversation, and nothing of the chat on screen @scenario:side-chat-mention-switches-the-model-in-place', async ({
|
||||
page,
|
||||
}) => {
|
||||
test.setTimeout(150_000);
|
||||
const { earlierUrl, sideChat, main, field } = await openBeside(page, 'mention');
|
||||
const mainUrl = page.url();
|
||||
const conversationId = new URL(earlierUrl).pathname.split('/').pop() ?? '';
|
||||
const label = uniqueLabel('mentioned');
|
||||
const other = MOCK_ENDPOINTS[1];
|
||||
await expect(sideChat).toContainText('@ for models');
|
||||
|
||||
const { combobox, options } = await openSideMention(page, sideChat, field);
|
||||
await combobox.fill(other.label);
|
||||
await expect(options).toHaveCount(1);
|
||||
await options.first().click();
|
||||
|
||||
/** The list closes on the side composer, which now writes to the model picked. */
|
||||
await expect(page.getByRole('combobox', { name: /Mention/ })).toHaveCount(0);
|
||||
await expect(field).toBeFocused();
|
||||
await expect(field).toHaveValue('');
|
||||
await expect(field).toHaveAttribute('placeholder', new RegExp(other.label));
|
||||
/** The chat on screen is where it was, on the model it had. */
|
||||
expect(page.url()).toBe(mainUrl);
|
||||
await expect(modelSelectorTrigger(page)).toContainText(MOCK_ENDPOINTS[0].label);
|
||||
await expect(main.getByRole('textbox', { name: 'Message input' })).toHaveAttribute(
|
||||
'placeholder',
|
||||
new RegExp(MOCK_ENDPOINTS[0].label),
|
||||
);
|
||||
|
||||
/** The next turn goes to the same conversation, on that model. */
|
||||
const started = page.waitForRequest((request) => isStart(request.url(), request.method()));
|
||||
await field.click();
|
||||
await page.keyboard.type(replyPrompt(label));
|
||||
await page.keyboard.press('Enter');
|
||||
const request = await started;
|
||||
expect(decodeURIComponent(new URL(request.url()).pathname.split('/').pop() ?? '')).toBe(
|
||||
other.label,
|
||||
);
|
||||
expect(request.postDataJSON()).toMatchObject({ conversationId, model: other.model });
|
||||
const turns = sideChat.locator('.message-render');
|
||||
await expect(turns).toHaveCount(4, { timeout: 30_000 });
|
||||
await expect(turns.nth(3)).toContainText(replyText(label), { timeout: 30_000 });
|
||||
await expect(turns.nth(3)).toContainText(other.label);
|
||||
await expect(messagesView(page).locator('.message-render')).toHaveCount(2);
|
||||
expect(page.url()).toBe(mainUrl);
|
||||
|
||||
/** It is the conversation's model from then on, wherever it is opened. */
|
||||
await page.goto(earlierUrl, { timeout: 30_000 });
|
||||
await expect(messagesView(page).locator('.message-render')).toHaveCount(4, { timeout: 30_000 });
|
||||
await expect(modelSelectorTrigger(page)).toContainText(other.label);
|
||||
});
|
||||
|
||||
test('@ in a side chat offers only what changes its conversation in place @scenario:side-chat-mention-offers-only-in-place-choices', async ({
|
||||
page,
|
||||
}) => {
|
||||
test.setTimeout(150_000);
|
||||
/** With modular chat off, another endpoint starts another conversation, which a side chat cannot. */
|
||||
await page.addInitScript(() => {
|
||||
localStorage.setItem('modularChat', JSON.stringify(false));
|
||||
});
|
||||
const { sideChat, field } = await openBeside(page, 'in-place');
|
||||
|
||||
const { combobox, options } = await openSideMention(page, sideChat, field);
|
||||
await combobox.fill('Mock Provider');
|
||||
await expect(options.filter({ hasText: MOCK_ENDPOINTS[0].label }).first()).toBeVisible();
|
||||
await expect(options.filter({ hasText: MOCK_ENDPOINTS[1].label })).toHaveCount(0);
|
||||
await combobox.press('Escape');
|
||||
await expect(field).toBeFocused();
|
||||
|
||||
/** Main chat's own list still offers it: there it starts a new conversation. */
|
||||
const mainField = page
|
||||
.locator('main form')
|
||||
.first()
|
||||
.getByRole('textbox', { name: 'Message input' });
|
||||
await mainField.click();
|
||||
await mainField.press('@');
|
||||
const mainCombobox = page.getByRole('combobox', { name: /Mention/ });
|
||||
await expect(mainCombobox).toBeFocused();
|
||||
await mainCombobox.fill('Mock Provider');
|
||||
const mainOptions = page
|
||||
.locator(`[id="${await mainCombobox.getAttribute('aria-controls')}"]`)
|
||||
.getByRole('option');
|
||||
await expect(mainOptions.filter({ hasText: MOCK_ENDPOINTS[1].label }).first()).toBeVisible();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -43,6 +43,12 @@ export type ChatConversationContract = {
|
|||
setConversation: Dispatch<SetStateAction<TConversation | null>>;
|
||||
/** Starts a fresh conversation in this pane from a template and/or preset. */
|
||||
newConversation: (options?: NewConversationOptions) => void;
|
||||
/**
|
||||
* Whether the composer may offer a change of model, endpoint or agent though the pane is not
|
||||
* main chat's: `newConversation` then changes the conversation the pane holds, in place, and
|
||||
* takes nothing that would start another. Main chat's pane offers every choice without it.
|
||||
*/
|
||||
retargets?: boolean;
|
||||
/** The preset applied to this pane, if any. */
|
||||
preset: TPreset | null;
|
||||
/** Replaces the preset applied to this pane. */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue