diff --git a/client/src/Providers/ChatSettingsContext.tsx b/client/src/Providers/ChatSettingsContext.tsx index aceb093287..e351e35cda 100644 --- a/client/src/Providers/ChatSettingsContext.tsx +++ b/client/src/Providers/ChatSettingsContext.tsx @@ -9,6 +9,8 @@ export const defaultChatSettings: ChatSettings = { saveDrafts: true, isTemporary: false, setIsTemporary: () => undefined, + config: { feedbackEnabled: false, canRenameRunningChat: false }, + auth: {}, }; export const ChatSettingsContext = createContext(defaultChatSettings); diff --git a/client/src/components/Chat/Menus/__tests__/BookmarkMenu.spec.tsx b/client/src/components/Chat/Menus/__tests__/BookmarkMenu.spec.tsx index af92a51c90..9f04d3d052 100644 --- a/client/src/components/Chat/Menus/__tests__/BookmarkMenu.spec.tsx +++ b/client/src/components/Chat/Menus/__tests__/BookmarkMenu.spec.tsx @@ -4,13 +4,11 @@ import { render, screen } from '@testing-library/react'; import { RetentionMode } from 'librechat-data-provider'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import type { TConversation } from 'librechat-data-provider'; +import { ChatSettingsContext, defaultChatSettings } from '~/Providers/ChatSettingsContext'; import BookmarkMenu from '../BookmarkMenu'; import store from '~/store'; -const mockUseGetStartupConfig = jest.fn(); - jest.mock('~/data-provider', () => ({ - useGetStartupConfig: (...args: unknown[]) => mockUseGetStartupConfig(...args), useConversationTagsQuery: () => ({ data: [] }), useTagConversationMutation: () => ({ mutate: jest.fn(), isLoading: false }), })); @@ -52,10 +50,6 @@ function renderMenu({ conversation: Partial; retentionMode?: RetentionMode; }) { - mockUseGetStartupConfig.mockReturnValue({ - data: retentionMode ? { interface: { retentionMode } } : { interface: {} }, - }); - return render( - + + + , ); diff --git a/client/src/components/Chat/__tests__/TemporaryChat.spec.tsx b/client/src/components/Chat/__tests__/TemporaryChat.spec.tsx index 5a063a75dc..2228183243 100644 --- a/client/src/components/Chat/__tests__/TemporaryChat.spec.tsx +++ b/client/src/components/Chat/__tests__/TemporaryChat.spec.tsx @@ -22,6 +22,10 @@ jest.mock('~/hooks', () => ({ useLocalize: () => (key: string) => (key === 'com_ui_temporary' ? 'Temporary Chat' : key), })); +jest.mock('~/hooks/AuthContext', () => ({ + useAuthContext: () => ({ user: undefined }), +})); + let mockRetentionMode: string | undefined; jest.mock('~/data-provider', () => ({ diff --git a/client/src/hooks/Chat/__tests__/contract.spec.tsx b/client/src/hooks/Chat/__tests__/contract.spec.tsx index 4ddc7bd13c..e855ee9309 100644 --- a/client/src/hooks/Chat/__tests__/contract.spec.tsx +++ b/client/src/hooks/Chat/__tests__/contract.spec.tsx @@ -1,14 +1,21 @@ import React from 'react'; import { RecoilRoot } from 'recoil'; import { renderHook } from '@testing-library/react'; +import { RetentionMode } from 'librechat-data-provider'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; -import type { TConversation } from 'librechat-data-provider'; +import type { TConversation, TModelSpec } from 'librechat-data-provider'; import type { MutableSnapshot } from 'recoil'; +import type { ChatSettings } from '../contract'; +import { ChatSettingsContext, defaultChatSettings } from '~/Providers/ChatSettingsContext'; +import useTemporaryChat from '../useTemporaryChat'; import useChatHelpers from '../useChatHelpers'; +import useTokenLimits from '../useTokenLimits'; import store from '~/store'; +/** No `useGetStartupConfig`: a hook that still read the deployment config directly would throw. */ jest.mock('~/data-provider', () => ({ - useGetStartupConfig: () => ({ data: undefined }), + useTokenConfigQuery: () => ({ data: undefined }), + useGetAgentByIdQuery: () => ({ data: undefined }), useAbortStreamMutation: () => ({ mutateAsync: jest.fn() }), supportsGenerationProtocolV2: () => false, })); @@ -33,17 +40,27 @@ jest.mock('~/hooks/Chat/useSteerConvert', () => ({ default: () => jest.fn(), })); +function createWrapper( + initializeState?: (snapshot: MutableSnapshot) => void, + settings: ChatSettings = defaultChatSettings, +) { + const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); + return function Wrapper({ children }: { children: React.ReactNode }) { + return ( + + + {children} + + + ); + }; +} + function renderChatHelpers( paramId?: string, initializeState?: (snapshot: MutableSnapshot) => void, ) { - const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); - const wrapper = ({ children }: { children: React.ReactNode }) => ( - - {children} - - ); - return renderHook(() => useChatHelpers(0, paramId), { wrapper }); + return renderHook(() => useChatHelpers(0, paramId), { wrapper: createWrapper(initializeState) }); } describe('useChatHelpers contract members', () => { @@ -68,3 +85,54 @@ describe('useChatHelpers contract members', () => { expect(result.current.messagesKey).toBe('convo-1'); }); }); + +describe('substitute host config', () => { + const modelSpecs = [ + { name: 'long', label: 'Long', preset: { endpoint: 'openAI', maxContextTokens: 4_096 } }, + ] as TModelSpec[]; + const hostSettings: ChatSettings = { + ...defaultChatSettings, + config: { + feedbackEnabled: true, + canRenameRunningChat: true, + retentionMode: RetentionMode.EPHEMERAL, + modelSpecs, + }, + }; + const wrapper = createWrapper(undefined, hostSettings); + + it('reaches the chat helpers', () => { + const { result } = renderHook(() => useChatHelpers(0), { wrapper }); + + expect(result.current.feedbackEnabled).toBe(true); + }); + + it('reaches temporary chat', () => { + const { result } = renderHook(() => useTemporaryChat(), { wrapper }); + + expect(result.current.isEnforced).toBe(true); + }); + + it('reaches the token limits', () => { + const conversation = { endpoint: 'openAI', spec: 'long' } as TConversation; + const { result } = renderHook(() => useTokenLimits(conversation), { wrapper }); + + expect(result.current.maxContextTokens).toBe(4_096); + }); + + it('leaves them on their defaults without a host', () => { + const conversation = { endpoint: 'openAI', spec: 'long' } as TConversation; + const { result } = renderHook( + () => ({ + helpers: useChatHelpers(0), + temporary: useTemporaryChat(), + limits: useTokenLimits(conversation), + }), + { wrapper: createWrapper() }, + ); + + expect(result.current.helpers.feedbackEnabled).toBe(false); + expect(result.current.temporary.isEnforced).toBe(false); + expect(result.current.limits.maxContextTokens).toBeUndefined(); + }); +}); diff --git a/client/src/hooks/Chat/__tests__/feedback.spec.tsx b/client/src/hooks/Chat/__tests__/feedback.spec.tsx index b9dc057133..0053c41564 100644 --- a/client/src/hooks/Chat/__tests__/feedback.spec.tsx +++ b/client/src/hooks/Chat/__tests__/feedback.spec.tsx @@ -3,6 +3,7 @@ import { RecoilRoot } from 'recoil'; import { renderHook } from '@testing-library/react'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import type { TStartupConfig } from 'librechat-data-provider'; +import ChatSettingsProvider from '~/routes/ChatSettings'; import useChatHelpers from '../useChatHelpers'; let mockStartupConfig: Partial | undefined; @@ -13,6 +14,10 @@ jest.mock('~/data-provider', () => ({ supportsGenerationProtocolV2: () => false, })); +jest.mock('~/hooks/AuthContext', () => ({ + useAuthContext: () => ({ user: undefined }), +})); + jest.mock('~/hooks/Messages/useLatestMessage', () => ({ useLatestMessage: () => null, useLatestMessageId: () => null, @@ -37,7 +42,9 @@ function renderChatHelpers() { const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); const wrapper = ({ children }: { children: React.ReactNode }) => ( - {children} + + {children} + ); return renderHook(() => useChatHelpers(0), { wrapper }); diff --git a/client/src/hooks/Chat/__tests__/useChatOptions.spec.tsx b/client/src/hooks/Chat/__tests__/useChatOptions.spec.tsx index e2d597a1c5..ea3f274509 100644 --- a/client/src/hooks/Chat/__tests__/useChatOptions.spec.tsx +++ b/client/src/hooks/Chat/__tests__/useChatOptions.spec.tsx @@ -2,7 +2,9 @@ import '@testing-library/jest-dom'; import { QueryKeys } from 'librechat-data-provider'; import { render, screen, renderHook, act } from '@testing-library/react'; import type { QueryClient } from '@tanstack/react-query'; +import type { ReactNode } from 'react'; import type { MenuItemProps } from '~/common'; +import { ChatSettingsContext, defaultChatSettings } from '~/Providers/ChatSettingsContext'; import useChatOptions from '../useChatOptions'; const mockState = { @@ -13,7 +15,7 @@ const mockState = { cached: undefined as Record | undefined, route: 'convo-1' as string | undefined, activeJobs: [] as string[], - startupConfig: undefined as Record | undefined, + canRenameRunningChat: false, projects: undefined as { _id: string; name: string }[] | undefined, projectsError: false, hasNextPage: false, @@ -59,7 +61,6 @@ jest.mock('~/Providers', () => ({ })); jest.mock('~/data-provider', () => ({ useGetConvoIdQuery: () => ({ data: mockState.cached }), - useGetStartupConfig: () => ({ data: mockState.startupConfig }), useActiveJobs: () => ({ data: { activeJobIds: mockState.activeJobs } }), usePinConversationMutation: () => ({ mutate: mockPin }), useArchiveConvoMutation: () => ({ mutate: mockArchive }), @@ -111,8 +112,29 @@ jest.mock('../useExportShare', () => ({ })); const setup = (readOnly = false, isMenuOpen = false) => - renderHook(() => - useChatOptions({ isSharedButtonEnabled: true, closeMenu: mockCloseMenu, readOnly, isMenuOpen }), + renderHook( + () => + useChatOptions({ + isSharedButtonEnabled: true, + closeMenu: mockCloseMenu, + readOnly, + isMenuOpen, + }), + { + wrapper: ({ children }: { children: ReactNode }) => ( + + {children} + + ), + }, ); const labels = (items: MenuItemProps[]) => items.filter((item) => item.show !== false && item.separate !== true).map((item) => item.label); @@ -132,7 +154,7 @@ describe('useChatOptions', () => { mockClient.current = new (jest.requireActual('@tanstack/react-query').QueryClient)(); mockState.route = 'convo-1'; mockState.activeJobs = []; - mockState.startupConfig = undefined; + mockState.canRenameRunningChat = false; mockState.projects = undefined; mockState.projectsError = false; mockState.hasNextPage = false; @@ -537,10 +559,7 @@ describe('useChatOptions', () => { it('keeps rename enabled while generating when the deployment supports title ownership', () => { mockState.activeJobs = ['convo-1']; - mockState.startupConfig = { - conversationTitleOwnershipVersion: 1, - interface: { runningChatRename: true }, - }; + mockState.canRenameRunningChat = true; const { result } = setup(); expect(find(result.current.items, 'com_ui_rename').disabled).toBe(false); diff --git a/client/src/hooks/Chat/__tests__/useSteerEscalate.spec.tsx b/client/src/hooks/Chat/__tests__/useSteerEscalate.spec.tsx index 8ba0fd4d9f..9196cee7fc 100644 --- a/client/src/hooks/Chat/__tests__/useSteerEscalate.spec.tsx +++ b/client/src/hooks/Chat/__tests__/useSteerEscalate.spec.tsx @@ -4,14 +4,14 @@ import { getDefaultStore, useAtomValue } from 'jotai'; import { act, renderHook, waitFor } from '@testing-library/react'; import type { MutableSnapshot } from 'recoil'; import type { PendingSteer } from '~/hooks/Chat/queue'; +import { ChatSettingsContext, defaultChatSettings } from '~/Providers/ChatSettingsContext'; import { escalatingSteerFamily } from '~/store/steer'; import useSteerEscalate from '../useSteerEscalate'; import store from '~/store'; const mockArmMutateAsync = jest.fn(); const mockShowToast = jest.fn(); -type StartupConfigStub = { data?: { interface?: { steerArmConfirmationTimeoutMs?: number } } }; -const mockGetStartupConfig = jest.fn(() => ({ data: undefined })); +let steerArmConfirmationTimeoutMs: number | undefined; jest.mock('@librechat/client', () => ({ useToastContext: () => ({ showToast: mockShowToast }), @@ -20,7 +20,6 @@ jest.mock('@librechat/client', () => ({ jest.mock('~/data-provider', () => ({ DEFAULT_STEER_ARM_CONFIRMATION_TIMEOUT_MS: 10_000, useArmSteerMutation: () => ({ mutateAsync: mockArmMutateAsync }), - useGetStartupConfig: () => mockGetStartupConfig(), supportsGenerationProtocolV2: (value: unknown) => value != null && typeof value === 'object' && @@ -64,7 +63,16 @@ function setup({ ); }; const wrapper = ({ children }: { children: React.ReactNode }) => ( - {children} + + + {children} + + ); return renderHook( @@ -81,7 +89,7 @@ describe('useSteerEscalate', () => { beforeEach(() => { jest.clearAllMocks(); act(() => getDefaultStore().set(escalatingSteerFamily(CONVO_ID), false)); - mockGetStartupConfig.mockReturnValue({ data: undefined }); + steerArmConfirmationTimeoutMs = undefined; }); afterEach(() => { @@ -260,9 +268,7 @@ describe('useSteerEscalate', () => { it('uses the configured arm confirmation timeout', async () => { jest.useFakeTimers(); - mockGetStartupConfig.mockReturnValue({ - data: { interface: { steerArmConfirmationTimeoutMs: 20_000 } }, - }); + steerArmConfirmationTimeoutMs = 20_000; mockArmMutateAsync.mockReturnValue(new Promise(() => undefined)); const { result } = setup(); diff --git a/client/src/hooks/Chat/__tests__/useSteering.spec.tsx b/client/src/hooks/Chat/__tests__/useSteering.spec.tsx index e3cb075c53..325a122a73 100644 --- a/client/src/hooks/Chat/__tests__/useSteering.spec.tsx +++ b/client/src/hooks/Chat/__tests__/useSteering.spec.tsx @@ -71,6 +71,10 @@ jest.mock('~/Providers', () => ({ useFileMapContext: () => mockFileMap, })); +jest.mock('~/hooks/AuthContext', () => ({ + useAuthContext: () => ({ user: undefined }), +})); + /** The reconciliation window is an operator lever; these specs exercise the * shipped default unless a case overrides it. */ let mockStartupConfig: { interface?: { queuedTurnReconciliationTimeoutMs?: number } } | undefined; diff --git a/client/src/hooks/Chat/contract.ts b/client/src/hooks/Chat/contract.ts index 878e66acaa..8b64ceac82 100644 --- a/client/src/hooks/Chat/contract.ts +++ b/client/src/hooks/Chat/contract.ts @@ -1,7 +1,10 @@ import type { TFile, + TUser, TPreset, TMessage, + TModelSpec, + RetentionMode, TConversation, ChatTransport, ChatTransportRequest, @@ -170,6 +173,30 @@ export type AddedChatContract = { /** Composer action while a run is in flight: fold the text into the run, or queue a new turn. */ export type DuringRunAction = 'steer' | 'interrupt' | 'queue'; +/** The deployment settings the chat hooks act on, resolved by the host from its startup config. + * An unset timeout falls back to the chat's own default. */ +export type ChatConfig = { + /** How long the deployment keeps conversations; forced temporary retention hides the paths + * that would keep one. */ + retentionMode?: RetentionMode; + /** Whether replies offer rating feedback; off until the deployment has answered. */ + feedbackEnabled: boolean; + /** Whether a running chat can be renamed, because the deployment protects a manual title from + * the pending generated one. */ + canRenameRunningChat: boolean; + /** Model specs the deployment offers, for presets and token limits a spec carries. */ + modelSpecs?: TModelSpec[]; + queuedSendLockTimeoutMs?: number; + queuedTurnReconciliationTimeoutMs?: number; + steerArmConfirmationTimeoutMs?: number; +}; + +/** The signed-in account, as far as the chat reads it. */ +export type ChatAuth = { + /** Fills user variables such as `{{current_user}}` in a conversation's prompt prefix. */ + user?: TUser; +}; + /** * App-global preferences the chat reads but does not own. The host supplies them, so the chat * hooks never reach into the app's state store for shell settings. A preference belongs here only @@ -188,6 +215,8 @@ export type ChatSettings = { isTemporary: boolean; /** Turns temporary chat on or off for the next conversation. */ setIsTemporary: Dispatch>; + config: ChatConfig; + auth: ChatAuth; }; /** The assistants abort route and the run it stops. */ diff --git a/client/src/hooks/Chat/useBookmarkItems.tsx b/client/src/hooks/Chat/useBookmarkItems.tsx index 56e509de25..2041be8fee 100644 --- a/client/src/hooks/Chat/useBookmarkItems.tsx +++ b/client/src/hooks/Chat/useBookmarkItems.tsx @@ -8,11 +8,8 @@ import { Constants, QueryKeys, isForcedTemporaryRetention } from 'librechat-data import type { TConversationTag } from 'librechat-data-provider'; import type { ReactNode } from 'react'; import type * as t from '~/common'; -import { - useConversationTagsQuery, - useGetStartupConfig, - useTagConversationMutation, -} from '~/data-provider'; +import { useConversationTagsQuery, useTagConversationMutation } from '~/data-provider'; +import { useChatSettings } from '~/Providers/ChatSettingsContext'; import { BookmarkEditDialog } from '~/components/Bookmarks'; import { useBookmarkSuccess, useLocalize } from '~/hooks'; import { isTemporaryConversation, logger } from '~/utils'; @@ -42,7 +39,7 @@ export default function useBookmarkItems({ const queryClient = useQueryClient(); const { showToast } = useToastContext(); - const { data: startupConfig } = useGetStartupConfig(); + const { config } = useChatSettings(); const conversation = useRecoilValue(store.conversationByIndex(0)) || undefined; const conversationId = conversation?.conversationId ?? ''; const updateConvoTags = useBookmarkSuccess(conversationId); @@ -53,8 +50,7 @@ export default function useBookmarkItems({ * to keep permanent-chat bookmarking hidden on a chat that is already effectively temporary. */ const isTemporary = - isTemporaryConversation(conversation) || - isForcedTemporaryRetention(startupConfig?.interface?.retentionMode); + isTemporaryConversation(conversation) || isForcedTemporaryRetention(config.retentionMode); const [isDialogOpen, setIsDialogOpen] = useState(false); const newBookmarkRef = useRef(null); diff --git a/client/src/hooks/Chat/useChatFunctions.ts b/client/src/hooks/Chat/useChatFunctions.ts index bfd192c065..8623958552 100644 --- a/client/src/hooks/Chat/useChatFunctions.ts +++ b/client/src/hooks/Chat/useChatFunctions.ts @@ -26,7 +26,6 @@ import type { TMessage, TSubmission, TConversation, - TStartupConfig, TEndpointOption, TEndpointsConfig, EndpointSchemaKey, @@ -65,9 +64,7 @@ import useCodeWorkspace from '~/hooks/Agents/useCodeWorkspace'; import useGetSender from '~/hooks/Conversations/useGetSender'; import { revealedQueuedTurnFamily } from '~/store/steer'; import store, { useGetEphemeralAgent } from '~/store'; -import { startupConfigKey } from '~/data-provider'; import useUserKey from '~/hooks/Input/useUserKey'; -import { useAuthContext } from '~/hooks'; /** A revalidating cache younger than this is locally authoritative (the run * that just streamed wrote it) and stays sendable; older ones wait for the @@ -251,12 +248,11 @@ export default function useChatFunctions({ const navigate = useNavigate(); const reasoningStore = useStore(); const getSender = useGetSender(); - const { user } = useAuthContext(); const queryClient = useQueryClient(); const setFilesToDelete = useSetFilesToDelete(); const getEphemeralAgent = useGetEphemeralAgent(); const agentsMap = useAgentsMapContext(); - const { isTemporary } = useChatSettings(); + const { isTemporary, config, auth } = useChatSettings(); const { getExpiry } = useUserKey(immutableConversation?.endpoint ?? ''); const setIsSubmitting = useSetRecoilState(store.isSubmittingFamily(index)); const setSubmissionStart = useSetRecoilState(store.submissionStartFamily(index)); @@ -477,7 +473,6 @@ export default function useChatFunctions({ const ephemeralAgent = getEphemeralAgent(conversationId ?? Constants.NEW_CONVO); const endpointsConfig = queryClient.getQueryData([QueryKeys.endpoints]); - const startupConfig = queryClient.getQueryData(startupConfigKey(true)); const endpointType = getEndpointField(endpointsConfig, endpoint, 'type'); const defaultParamsEndpoint = getDefaultParamsEndpoint(endpointsConfig, endpoint); /** @@ -605,7 +600,7 @@ export default function useChatFunctions({ if (conversation?.promptPrefix) { conversation.promptPrefix = replaceSpecialVars({ text: conversation.promptPrefix, - user, + user: auth.user, }); } @@ -869,7 +864,7 @@ export default function useChatFunctions({ conversation, addedConvo, endpointsConfig, - startupConfig?.modelSpecs?.list, + config.modelSpecs, ); } else { initialResponse.content = []; diff --git a/client/src/hooks/Chat/useChatHelpers.ts b/client/src/hooks/Chat/useChatHelpers.ts index 81d7245bee..4a01a93686 100644 --- a/client/src/hooks/Chat/useChatHelpers.ts +++ b/client/src/hooks/Chat/useChatHelpers.ts @@ -5,14 +5,11 @@ import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil'; import { Constants, QueryKeys, isAssistantsEndpoint } from 'librechat-data-provider'; import type { TMessage } from 'librechat-data-provider'; import type { ChatContract } from './contract'; -import { - useGetStartupConfig, - useAbortStreamMutation, - supportsGenerationProtocolV2, -} from '~/data-provider'; import { useLatestMessage, useLatestMessageId } from '~/hooks/Messages/useLatestMessage'; +import { useAbortStreamMutation, supportsGenerationProtocolV2 } from '~/data-provider'; import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state'; import { drainAfterAbortByIndex, runEndByIndex } from '~/hooks/Chat/queue'; +import { useChatSettings } from '~/Providers/ChatSettingsContext'; import useChatFunctions from '~/hooks/Chat/useChatFunctions'; import useSteerConvert from '~/hooks/Chat/useSteerConvert'; import { resolveAbortSteerTarget } from '~/utils'; @@ -388,11 +385,8 @@ export default function useChatHelpers(index = 0, paramId?: string): ChatContrac const [optionSettings, setOptionSettings] = useRecoilState(store.optionSettingsFamily(index)); /** Read once per chat rather than per message row: message rows never unmount, so a - * per-row config observer would accumulate for the length of the conversation. - * Stays disabled until the config resolves, so a `feedback: false` deployment never - * flashes controls whose writes the server rejects. */ - const { data: startupConfig } = useGetStartupConfig(); - const feedbackEnabled = startupConfig != null && startupConfig.interface?.feedback !== false; + * per-row settings read would accumulate for the length of the conversation. */ + const { feedbackEnabled } = useChatSettings().config; return useMemo( (): ChatContract => ({ diff --git a/client/src/hooks/Chat/useChatOptions.tsx b/client/src/hooks/Chat/useChatOptions.tsx index 40dba9cf8c..a37a2154d4 100644 --- a/client/src/hooks/Chat/useChatOptions.tsx +++ b/client/src/hooks/Chat/useChatOptions.tsx @@ -3,7 +3,6 @@ import { useRecoilValue } from 'recoil'; import { useToastContext } from '@librechat/client'; import { useQueryClient } from '@tanstack/react-query'; import { useNavigate, useParams } from 'react-router-dom'; -import { supportsConversationTitleOwnership } from 'librechat-data-provider'; import { Pen, Pin, Trash, Archive, CopyPlus, ArchiveRestore } from 'lucide-react'; import type { TConversation } from 'librechat-data-provider'; import type { ReactNode } from 'react'; @@ -11,7 +10,6 @@ import type * as t from '~/common'; import { useActiveJobs, useGetConvoIdQuery, - useGetStartupConfig, useArchiveConvoMutation, usePinConversationMutation, useDuplicateConversationMutation, @@ -19,6 +17,7 @@ import { import { findConvoInAllQueries, isTemporaryConversation, hasRealTitle } from '~/utils'; import DeleteButton from '~/components/Conversations/ConvoOptions/DeleteButton'; import { useLocalize, useNavigateToConvo, useNewConvo } from '~/hooks'; +import { useChatSettings } from '~/Providers/ChatSettingsContext'; import { useChatContext, useLiveAnnouncer } from '~/Providers'; import useProjectMenuItem from './useProjectMenuItem'; import { NotificationSeverity } from '~/common'; @@ -64,7 +63,7 @@ export default function useChatOptions({ const { newConversation } = useNewConvo(); const { setConversation } = useChatContext(); const { navigateToConvo } = useNavigateToConvo(0); - const { data: startupConfig } = useGetStartupConfig(); + const { config } = useChatSettings(); const { data: activeJobs } = useActiveJobs(); const { conversationId: routeConversationId } = useParams(); /** A request outlives the click: the route that matters is the one open when it resolves. */ @@ -91,7 +90,7 @@ export default function useChatOptions({ /** The sidebar's rule: a running chat can only be renamed where the deployment can protect * the manual title from the pending generated one. */ const canRename = - supportsConversationTitleOwnership(startupConfig) || + config.canRenameRunningChat || (!isGenerating && (current?.titleSetByUser === true || hasRealTitle(title))); const renameRef = useRef(null); diff --git a/client/src/hooks/Chat/useQueueDrain.ts b/client/src/hooks/Chat/useQueueDrain.ts index 64cb1de385..9c6c463f1a 100644 --- a/client/src/hooks/Chat/useQueueDrain.ts +++ b/client/src/hooks/Chat/useQueueDrain.ts @@ -19,8 +19,9 @@ import { canRestoreRecovery, } from '~/components/Chat/Steering/recovery'; import { acquireQueueSendLock, releaseQueueSendLock, hasQueuedIntent } from '~/utils/queueIntent'; -import { useGetStartupConfig, useMarkFilesUsageMutation } from '~/data-provider'; import { selectQueuedTurnReveal } from '~/hooks/Chat/useQueuedTurnReveal'; +import { useChatSettings } from '~/Providers/ChatSettingsContext'; +import { useMarkFilesUsageMutation } from '~/data-provider'; import { revealedQueuedTurnFamily } from '~/store/steer'; import { mergeQueuedMessages } from '~/utils/queue'; import { insertQueuedOrigin } from '~/utils/steer'; @@ -106,9 +107,8 @@ export default function useQueueDrain( const sendLockRef = useRef(null); const revealLockRef = useRef(false); const { mutate: markFilesUsage } = useMarkFilesUsageMutation(); - const { data: startupConfig } = useGetStartupConfig(); - const sendLockTimeoutMs = - startupConfig?.interface?.queuedSendLockTimeoutMs ?? DEFAULT_QUEUED_SEND_LOCK_TIMEOUT_MS; + const { config } = useChatSettings(); + const sendLockTimeoutMs = config.queuedSendLockTimeoutMs ?? DEFAULT_QUEUED_SEND_LOCK_TIMEOUT_MS; const ownQueue = useAtomValue( queuedMessagesByConvoId(activeConversationId ?? Constants.NEW_CONVO), ); diff --git a/client/src/hooks/Chat/useSteerEscalate.ts b/client/src/hooks/Chat/useSteerEscalate.ts index 95851d5eec..4576829d11 100644 --- a/client/src/hooks/Chat/useSteerEscalate.ts +++ b/client/src/hooks/Chat/useSteerEscalate.ts @@ -3,11 +3,8 @@ import { useSetAtom } from 'jotai'; import { useToastContext } from '@librechat/client'; import { useRecoilValue, useRecoilCallback } from 'recoil'; import { DEFAULT_STEER_ARM_CONFIRMATION_TIMEOUT_MS } from 'librechat-data-provider'; -import { - supportsGenerationProtocolV2, - useArmSteerMutation, - useGetStartupConfig, -} from '~/data-provider'; +import { supportsGenerationProtocolV2, useArmSteerMutation } from '~/data-provider'; +import { useChatSettings } from '~/Providers/ChatSettingsContext'; import { escalatingSteerFamily } from '~/store/steer'; import useLocalize from '~/hooks/useLocalize'; import store from '~/store'; @@ -50,10 +47,9 @@ export default function useSteerEscalate(conversationId: string) { const localize = useLocalize(); const { showToast } = useToastContext(); const { mutateAsync: armSteer } = useArmSteerMutation(); - const { data: startupConfig } = useGetStartupConfig(); + const { config } = useChatSettings(); const armConfirmTimeoutMs = - startupConfig?.interface?.steerArmConfirmationTimeoutMs ?? - DEFAULT_STEER_ARM_CONFIRMATION_TIMEOUT_MS; + config.steerArmConfirmationTimeoutMs ?? DEFAULT_STEER_ARM_CONFIRMATION_TIMEOUT_MS; const setEscalating = useSetAtom(escalatingSteerFamily(conversationId)); const activeGenerationCreatedAt = useRecoilValue( store.activeGenerationCreatedAtByConvoId(conversationId), diff --git a/client/src/hooks/Chat/useSteering.ts b/client/src/hooks/Chat/useSteering.ts index d1e909cf73..17280eb9b7 100644 --- a/client/src/hooks/Chat/useSteering.ts +++ b/client/src/hooks/Chat/useSteering.ts @@ -31,7 +31,6 @@ import type { ExtendedFile, FileSetter } from '~/common'; import type { PendingSteer } from '~/hooks/Chat/queue'; import type { DuringRunAction } from './contract'; import { - useGetStartupConfig, useCancelSteerMutation, useSteerMessageMutation, useMarkFilesUsageMutation, @@ -613,8 +612,11 @@ export default function useSteering({ const { mutate: markFilesUsage } = useMarkFilesUsageMutation(); const { mutate: enqueueAgentQueuedTurn } = useEnqueueAgentQueuedTurnMutation(); const { mutateAsync: cancelAgentQueuedTurn } = useCancelAgentQueuedTurnMutation(); - const { duringRunDefaultAction: defaultAction, setDuringRunDefaultAction: setDefaultAction } = - useChatSettings(); + const { + config, + duringRunDefaultAction: defaultAction, + setDuringRunDefaultAction: setDefaultAction, + } = useChatSettings(); const { selected: codeApprovalMode } = useCodeApprovalMode( conversation, @@ -633,16 +635,13 @@ export default function useSteering({ const queuedMessages = useAtomValue(queuedMessagesByConvoId(queueKey)); const pendingReasoningOverride = useAtomValue(pendingReasoningOverrideFamily(reasoningStateKey)); const setQueuedMessages = useSetAtom(queuedMessagesByConvoId(queueKey)); - const { data: startupConfig } = useGetStartupConfig(); /** Bound transport-outcome reconciliation while still guaranteeing several * list reads after the enqueue promise settles. Focus/remount remains a later * reconciliation path; a slow proxy or a delayed read replica needs the * operator to widen this rather than wait for one. */ const reconciliationWindowMs = - startupConfig?.interface?.queuedTurnReconciliationTimeoutMs ?? - DEFAULT_QUEUED_TURN_RECONCILIATION_TIMEOUT_MS; - const sendLockTimeoutMs = - startupConfig?.interface?.queuedSendLockTimeoutMs ?? DEFAULT_QUEUED_SEND_LOCK_TIMEOUT_MS; + config.queuedTurnReconciliationTimeoutMs ?? DEFAULT_QUEUED_TURN_RECONCILIATION_TIMEOUT_MS; + const sendLockTimeoutMs = config.queuedSendLockTimeoutMs ?? DEFAULT_QUEUED_SEND_LOCK_TIMEOUT_MS; const knownClientRequestIds = useMemo( () => Array.from( diff --git a/client/src/hooks/Chat/useTemporaryChat.ts b/client/src/hooks/Chat/useTemporaryChat.ts index 7837e18750..5ba678bfc3 100644 --- a/client/src/hooks/Chat/useTemporaryChat.ts +++ b/client/src/hooks/Chat/useTemporaryChat.ts @@ -2,7 +2,6 @@ import { useCallback } from 'react'; import { useRecoilValue } from 'recoil'; import { Constants, isForcedTemporaryRetention } from 'librechat-data-provider'; import { useChatSettings } from '~/Providers/ChatSettingsContext'; -import { useGetStartupConfig } from '~/data-provider'; import store from '~/store'; export type UseTemporaryChatResult = { @@ -17,11 +16,10 @@ export type UseTemporaryChatResult = { }; export default function useTemporaryChat(): UseTemporaryChatResult { - const { data: startupConfig } = useGetStartupConfig(); - const { isTemporary, setIsTemporary } = useChatSettings(); + const { isTemporary, setIsTemporary, config } = useChatSettings(); const conversation = useRecoilValue(store.conversationByIndex(0)); const isSubmitting = useRecoilValue(store.isSubmittingFamily(0)); - const isEnforced = isForcedTemporaryRetention(startupConfig?.interface?.retentionMode); + const isEnforced = isForcedTemporaryRetention(config.retentionMode); const toggle = useCallback(() => { if (isEnforced) { diff --git a/client/src/hooks/Chat/useTokenLimits.ts b/client/src/hooks/Chat/useTokenLimits.ts index b7a96614ca..b072b9db52 100644 --- a/client/src/hooks/Chat/useTokenLimits.ts +++ b/client/src/hooks/Chat/useTokenLimits.ts @@ -1,8 +1,8 @@ import { useMemo } from 'react'; import { isAgentsEndpoint, resolveModelCatalogKey } from 'librechat-data-provider'; import type { TConversation, TModelTokenomics } from 'librechat-data-provider'; -import { useGetStartupConfig, useTokenConfigQuery, useGetAgentByIdQuery } from '~/data-provider'; -import { getModelSpec } from '~/utils'; +import { useTokenConfigQuery, useGetAgentByIdQuery } from '~/data-provider'; +import { useChatSettings } from '~/Providers/ChatSettingsContext'; export interface TokenLimits { /** Statically resolved max context; live snapshots override this at run time */ @@ -23,7 +23,7 @@ function toNumber(value: unknown): number | undefined { * server-resolved token config lookup. */ export default function useTokenLimits(conversation: TConversation | null): TokenLimits { - const { data: startupConfig } = useGetStartupConfig(); + const { modelSpecs } = useChatSettings().config; const { data: tokenConfig } = useTokenConfigQuery(); const endpoint = conversation?.endpoint ?? ''; @@ -35,7 +35,7 @@ export default function useTokenLimits(conversation: TConversation | null): Toke const maxContextSetting = conversation?.maxContextTokens; return useMemo(() => { - const specPreset = getModelSpec({ specName: spec, startupConfig })?.preset; + const specPreset = spec ? modelSpecs?.find((s) => s.name === spec)?.preset : undefined; let lookupEndpoint = endpoint; let lookupModel = model ?? ''; @@ -56,5 +56,5 @@ export default function useTokenLimits(conversation: TConversation | null): Toke rates?.context; return { maxContextTokens, rates, endpoint: lookupEndpoint, model: lookupModel }; - }, [endpoint, model, spec, maxContextSetting, agent, startupConfig, tokenConfig]); + }, [endpoint, model, spec, maxContextSetting, agent, modelSpecs, tokenConfig]); } diff --git a/client/src/routes/ChatSettings.tsx b/client/src/routes/ChatSettings.tsx index 973236c03e..9e300a111d 100644 --- a/client/src/routes/ChatSettings.tsx +++ b/client/src/routes/ChatSettings.tsx @@ -1,20 +1,39 @@ import { useMemo } from 'react'; import { useAtom } from 'jotai'; +import { supportsConversationTitleOwnership } from 'librechat-data-provider'; import { useRecoilState, useRecoilValue, useResetRecoilState } from 'recoil'; import type { ReactNode } from 'react'; -import type { ChatSettings } from '~/hooks/Chat/contract'; +import type { ChatSettings, ChatConfig, ChatAuth } from '~/hooks/Chat/contract'; import { ChatTransportContext, defaultChatTransport } from '~/Providers/ChatTransportContext'; import { ChatSettingsContext } from '~/Providers/ChatSettingsContext'; import { duringRunActionAtom } from '~/store/duringRun'; +import { useGetStartupConfig } from '~/data-provider'; +import { useAuthContext } from '~/hooks/AuthContext'; import store from '~/store'; -/** Supplies the chat's app-global preferences from the app's own settings store, and the - * transport its turns run over. */ +/** 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 }) { const [duringRunDefaultAction, setDuringRunDefaultAction] = useAtom(duringRunActionAtom); const resetVisibleArtifacts = useResetRecoilState(store.visibleArtifacts); const saveDrafts = useRecoilValue(store.saveDrafts); const [isTemporary, setIsTemporary] = useRecoilState(store.isTemporary); + const { data: startupConfig } = useGetStartupConfig(); + const { user } = useAuthContext(); + + const config = useMemo(() => { + const ui = startupConfig?.interface; + return { + retentionMode: ui?.retentionMode, + feedbackEnabled: startupConfig != null && ui?.feedback !== false, + canRenameRunningChat: supportsConversationTitleOwnership(startupConfig), + modelSpecs: startupConfig?.modelSpecs?.list, + queuedSendLockTimeoutMs: ui?.queuedSendLockTimeoutMs, + queuedTurnReconciliationTimeoutMs: ui?.queuedTurnReconciliationTimeoutMs, + steerArmConfirmationTimeoutMs: ui?.steerArmConfirmationTimeoutMs, + }; + }, [startupConfig]); + const auth = useMemo(() => ({ user }), [user]); const settings = useMemo( () => ({ @@ -24,6 +43,8 @@ export default function ChatSettingsProvider({ children }: { children: ReactNode saveDrafts, isTemporary, setIsTemporary, + config, + auth, }), [ duringRunDefaultAction, @@ -32,6 +53,8 @@ export default function ChatSettingsProvider({ children }: { children: ReactNode saveDrafts, isTemporary, setIsTemporary, + config, + auth, ], ); diff --git a/client/src/routes/__tests__/ChatSettings.spec.tsx b/client/src/routes/__tests__/ChatSettings.spec.tsx index d7947af244..bc5b47b3ee 100644 --- a/client/src/routes/__tests__/ChatSettings.spec.tsx +++ b/client/src/routes/__tests__/ChatSettings.spec.tsx @@ -1,12 +1,25 @@ import React from 'react'; import { useAtomValue, getDefaultStore } from 'jotai'; +import { RetentionMode } from 'librechat-data-provider'; import { act, renderHook } from '@testing-library/react'; import { RecoilRoot, useRecoilValue, type MutableSnapshot } from 'recoil'; +import type { TStartupConfig, TUser } from 'librechat-data-provider'; import { defaultChatSettings, useChatSettings } from '~/Providers/ChatSettingsContext'; import { duringRunActionAtom } from '~/store/duringRun'; import ChatSettingsProvider from '../ChatSettings'; import store from '~/store'; +let mockStartupConfig: Partial | undefined; +let mockUser: TUser | undefined; + +jest.mock('~/data-provider', () => ({ + useGetStartupConfig: () => ({ data: mockStartupConfig }), +})); + +jest.mock('~/hooks/AuthContext', () => ({ + useAuthContext: () => ({ user: mockUser }), +})); + const renderSettings = (initialize?: (snapshot: MutableSnapshot) => void) => renderHook( () => ({ @@ -24,7 +37,11 @@ const renderSettings = (initialize?: (snapshot: MutableSnapshot) => void) => ); describe('ChatSettingsProvider', () => { - beforeEach(() => getDefaultStore().set(duringRunActionAtom, 'steer')); + beforeEach(() => { + getDefaultStore().set(duringRunActionAtom, 'steer'); + mockStartupConfig = undefined; + mockUser = undefined; + }); it('supplies the stored preferences to the chat', () => { const { result } = renderSettings(() => { getDefaultStore().set(duringRunActionAtom, 'interrupt'); @@ -54,6 +71,58 @@ describe('ChatSettingsProvider', () => { expect(result.current.visibleArtifacts).toBeNull(); }); + it('keeps feedback and running-chat rename off until the deployment answers', () => { + const { result } = renderSettings(); + + expect(result.current.settings.config).toEqual({ + feedbackEnabled: false, + canRenameRunningChat: false, + }); + }); + + it('supplies the deployment config the chat hooks act on', () => { + const modelSpecs = [{ name: 'spec', label: 'Spec', preset: { endpoint: 'openAI' } }]; + mockStartupConfig = { + conversationTitleOwnershipVersion: 1, + modelSpecs: { list: modelSpecs }, + interface: { + retentionMode: RetentionMode.EPHEMERAL, + runningChatRename: true, + queuedSendLockTimeoutMs: 1_000, + queuedTurnReconciliationTimeoutMs: 2_000, + steerArmConfirmationTimeoutMs: 3_000, + }, + } as Partial; + + const { result } = renderSettings(); + + expect(result.current.settings.config).toEqual({ + retentionMode: RetentionMode.EPHEMERAL, + feedbackEnabled: true, + canRenameRunningChat: true, + modelSpecs, + queuedSendLockTimeoutMs: 1_000, + queuedTurnReconciliationTimeoutMs: 2_000, + steerArmConfirmationTimeoutMs: 3_000, + }); + }); + + it('turns feedback off when the deployment does', () => { + mockStartupConfig = { interface: { feedback: false } } as Partial; + + const { result } = renderSettings(); + + expect(result.current.settings.config.feedbackEnabled).toBe(false); + }); + + it('supplies the signed-in account', () => { + mockUser = { id: 'user-1', name: 'Ada' } as TUser; + + const { result } = renderSettings(); + + expect(result.current.settings.auth.user).toBe(mockUser); + }); + it('falls back to the stock defaults without a host', () => { const { result } = renderHook(() => useChatSettings()); diff --git a/e2e/specs/mock/scenarios/chat-host-config.spec.ts b/e2e/specs/mock/scenarios/chat-host-config.spec.ts new file mode 100644 index 0000000000..f549ef3afe --- /dev/null +++ b/e2e/specs/mock/scenarios/chat-host-config.spec.ts @@ -0,0 +1,33 @@ +import { expect, test } from '@playwright/test'; +import { + MOCK_ENDPOINTS, + NEW_CHAT_PATH, + messagesView, + replyPrompt, + replyText, + selectMockEndpoint, + sendMessageAndWaitForCompletion, +} from '../helpers'; + +/** + * The deployment config the chat acts on reaches it through the host's chat settings rather than + * through the chat's own startup-config reads. These scenarios check what the chat does with it. + */ + +test.describe('chat host config', () => { + test('a reply offers rating feedback the deployment allows @scenario:reply-offers-feedback-from-host-config', async ({ + page, + }) => { + test.setTimeout(60000); + await page.goto(NEW_CHAT_PATH, { timeout: 10000 }); + await selectMockEndpoint(page, MOCK_ENDPOINTS[0]); + + const label = `host-feedback-${Date.now()}`; + await sendMessageAndWaitForCompletion(page, replyPrompt(label)); + await expect(messagesView(page).getByText(replyText(label))).toBeVisible({ timeout: 20000 }); + + const thumbsUp = messagesView(page).getByRole('button', { name: 'Love this' }).last(); + await expect(thumbsUp).toBeVisible(); + await expect(thumbsUp).toHaveAttribute('aria-pressed', 'false'); + }); +});