diff --git a/client/src/Providers/ChatHostContext.tsx b/client/src/Providers/ChatHostContext.tsx index 310e953507..c819209d46 100644 --- a/client/src/Providers/ChatHostContext.tsx +++ b/client/src/Providers/ChatHostContext.tsx @@ -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. */ diff --git a/client/src/Providers/ComposerPreferencesContext.tsx b/client/src/Providers/ComposerPreferencesContext.tsx index 9de4a56b56..3a499134ae 100644 --- a/client/src/Providers/ComposerPreferencesContext.tsx +++ b/client/src/Providers/ComposerPreferencesContext.tsx @@ -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( diff --git a/client/src/components/Chat/ChatView.tsx b/client/src/components/Chat/ChatView.tsx index 5fa8059499..4b8a5cc82a 100644 --- a/client/src/components/Chat/ChatView.tsx +++ b/client/src/components/Chat/ChatView.tsx @@ -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(); diff --git a/client/src/components/Chat/Input/ChatForm.tsx b/client/src/components/Chat/Input/ChatForm.tsx index f5a0eaf9f2..7de4ac2dad 100644 --- a/client/src/components/Chat/Input/ChatForm.tsx +++ b/client/src/components/Chat/Input/ChatForm.tsx @@ -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(null); const textAreaRef = useRef(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 && ( - <> - - - - - - - + + + + )} + {(routed || retargets) && ( + + + )} @@ -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} /> ); } diff --git a/client/src/components/Chat/Input/Mention.tsx b/client/src/components/Chat/Input/Mention.tsx index 4d7eaf55f1..65024bccbc 100644 --- a/client/src/components/Chat/Input/Mention.tsx +++ b/client/src/components/Chat/Input/Mention.tsx @@ -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) { + 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 ( ({ ...contract, retargets: true }), [contract]); + const form = useRef(null); const focused = useRef(false); useEffect(() => { @@ -175,7 +181,7 @@ export default function SideChatSurface({ }, [focusWhenReady, ready, conversation?.endpoint]); return ( - + diff --git a/client/src/hooks/Input/__tests__/useSelectMention.spec.ts b/client/src/hooks/Input/__tests__/useSelectMention.spec.ts new file mode 100644 index 0000000000..46c387087f --- /dev/null +++ b/client/src/hooks/Input/__tests__/useSelectMention.spec.ts @@ -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 }) => + 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'); + }); +}); diff --git a/client/src/hooks/Input/useSelectMention.ts b/client/src/hooks/Input/useSelectMention.ts index 62bd9fe13f..87d7b48d77 100644 --- a/client/src/hooks/Input/useSelectMention.ts +++ b/client/src/hooks/Input/useSelectMention.ts @@ -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; diff --git a/client/src/hooks/__tests__/useNewConvo.spec.ts b/client/src/hooks/__tests__/useNewConvo.spec.ts index 40805aebad..85e1fc7fce 100644 --- a/client/src/hooks/__tests__/useNewConvo.spec.ts +++ b/client/src/hooks/__tests__/useNewConvo.spec.ts @@ -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(); diff --git a/client/src/hooks/useNewConvo.ts b/client/src/hooks/useNewConvo.ts index 4ee8884156..c96160a5a3 100644 --- a/client/src/hooks/useNewConvo.ts +++ b/client/src/hooks/useNewConvo.ts @@ -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, diff --git a/client/src/utils/endpoints.spec.ts b/client/src/utils/endpoints.spec.ts index 08858704a4..dffd99a28c 100644 --- a/client/src/utils/endpoints.spec.ts +++ b/client/src/utils/endpoints.spec.ts @@ -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); + }); +}); diff --git a/client/src/utils/endpoints.ts b/client/src/utils/endpoints.ts index 11a27e82f6..e35e3d1058 100644 --- a/client/src/utils/endpoints.ts +++ b/client/src/utils/endpoints.ts @@ -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): 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; diff --git a/e2e/specs/mock/scenarios/side-chat-composer.spec.ts b/e2e/specs/mock/scenarios/side-chat-composer.spec.ts index 56fa4e80f6..1a36f69c9b 100644 --- a/e2e/specs/mock/scenarios/side-chat-composer.spec.ts +++ b/e2e/specs/mock/scenarios/side-chat-composer.spec.ts @@ -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(); +}); diff --git a/packages/chat/src/react/chat/contract.ts b/packages/chat/src/react/chat/contract.ts index 7a7791bd5d..b168f79133 100644 --- a/packages/chat/src/react/chat/contract.ts +++ b/packages/chat/src/react/chat/contract.ts @@ -43,6 +43,12 @@ export type ChatConversationContract = { setConversation: Dispatch>; /** 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. */