Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🔑 refactor: Read Stream Auth and Deployment Config Through ChatSettings (#17141)

Marco Beretta42858847b3db
diff --git a/client/src/hooks/Chat/__tests__/transport.spec.tsx b/client/src/hooks/Chat/__tests__/transport.spec.tsx
index fe17a6b41a..1404342e93 100644
--- a/client/src/hooks/Chat/__tests__/transport.spec.tsx
+++ b/client/src/hooks/Chat/__tests__/transport.spec.tsx
@@ -30,6 +30,7 @@ import type { MutableSnapshot } from 'recoil';
import type { StreamStatusResponse } from '~/data-provider';
import type { ChatContract } from '~/hooks/Chat/contract';
import type { Transport } from '~/hooks/Chat/contract';
+import { ChatSettingsContext, defaultChatSettings } from '~/Providers/ChatSettingsContext';
import { ChatTransportContext } from '~/Providers/ChatTransportContext';
import { ChatContext, useChatContext } from '~/Providers/ChatContext';
import { startupConfigKey } from '~/data-provider/Endpoints/queries';
@@ -89,6 +90,11 @@ function createFakeTransport(overrides: Partial<Transport> = {}) {
return { transport, streams, sends };
}
+const chatSettings = {
+ ...defaultChatSettings,
+ auth: { token: 'test-token', isAuthenticated: true },
+};
+
function createWrapper(
transport: Transport,
seed?: (state: MutableSnapshot) => void,
@@ -111,9 +117,11 @@ function createWrapper(
}}
>
<SteerHostTestProvider>
- <ChatTransportContext.Provider value={transport}>
- {children}
- </ChatTransportContext.Provider>
+ <ChatSettingsContext.Provider value={chatSettings}>
+ <ChatTransportContext.Provider value={transport}>
+ {children}
+ </ChatTransportContext.Provider>
+ </ChatSettingsContext.Provider>
</SteerHostTestProvider>
</RecoilRoot>
</QueryClientProvider>
diff --git a/client/src/hooks/Chat/contract.ts b/client/src/hooks/Chat/contract.ts
index 78cf507d80..e92a604934 100644
--- a/client/src/hooks/Chat/contract.ts
+++ b/client/src/hooks/Chat/contract.ts
@@ -193,12 +193,21 @@ export type ChatConfig = {
queuedSendLockTimeoutMs?: number;
queuedTurnReconciliationTimeoutMs?: number;
steerArmConfirmationTimeoutMs?: number;
+ /** Whether the deployment has answered with its config; until then the fields above are
+ * defaults, and a decision that depends on them (a rebuilt turn's retention) waits. A host
+ * that leaves it unset has no config to wait for. */
+ loaded?: boolean;
+ /** Whether the deployment tracks token balances, refreshed after each turn. */
+ balanceEnabled?: boolean;
};
/** 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;
+ /** Bearer token the chat's streams and run aborts authenticate with. */
+ token?: string;
+ isAuthenticated?: boolean;
};
/**
diff --git a/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts b/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts
index fb7545059d..6ccfe49f9b 100644
--- a/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts
+++ b/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts
@@ -190,9 +190,14 @@ jest.mock('~/store', () => ({
default: {},
}));
-jest.mock('~/hooks/AuthContext', () => ({
- useAuthContext: () => ({ token: 'test-token', isAuthenticated: true }),
-}));
+jest.mock('~/Providers/ChatSettingsContext', () => {
+ const actual = jest.requireActual('~/Providers/ChatSettingsContext');
+ const settings = {
+ ...actual.defaultChatSettings,
+ auth: { token: 'test-token', isAuthenticated: true },
+ };
+ return { ...actual, useChatSettings: () => settings };
+});
const mockFetchStreamStatus = jest.fn();
const mockGetConversationById = jest.fn();
diff --git a/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx b/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx
index e9c8be66e2..3679b3c4c1 100644
--- a/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx
+++ b/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx
@@ -1,7 +1,7 @@
import { RecoilRoot } from 'recoil';
import { renderHook, act, waitFor } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
-import { Constants, ContentTypes, QueryKeys } from 'librechat-data-provider';
+import { Constants, ContentTypes, QueryKeys, RetentionMode } from 'librechat-data-provider';
import { isSubmittingFamily, resetSubmitting, submissionByIndex } from '@librechat/chat/react';
import {
Provider as JotaiProvider,
@@ -28,6 +28,7 @@ import type { Agents, TMessage, TConversation, TSubmission } from 'librechat-dat
import type { QueuedMessage, PendingSteer, DetachedRun } from '@librechat/chat';
import type { MutableSnapshot } from 'recoil';
import type { ReactNode } from 'react';
+import { ChatSettingsContext, defaultChatSettings } from '~/Providers/ChatSettingsContext';
import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state';
import { pendingApprovalActionFamily } from '~/components/Chat/approval/state';
import { agentQueuedTurnsQueryKey } from '~/data-provider/SSE/queuedTurns';
@@ -39,7 +40,7 @@ const mockUseStreamStatus = jest.fn();
const mockUseActiveJobs = jest.fn();
const mockUseAgentQueuedTurns = jest.fn();
const mockExtendActiveJobsGrace = jest.fn();
-let mockStartupConfig: { interface?: { retentionMode?: string } } | undefined;
+let mockStartupConfig: { interface?: { retentionMode?: RetentionMode } } | undefined;
let mockStartupConfigSettled = true;
let mockFileMap: Record<string, { llmDeliveryPath?: 'provider' | 'text' | 'none' }> = {};
@@ -62,7 +63,6 @@ jest.mock('~/data-provider', () => ({
ACTIVE_JOBS_SUCCESSOR_GRACE_MS: jest.requireActual('~/data-provider/SSE/queries')
.ACTIVE_JOBS_SUCCESSOR_GRACE_MS,
extendActiveJobsGrace: () => mockExtendActiveJobsGrace(),
- useGetStartupConfig: () => ({ data: mockStartupConfig, isFetched: mockStartupConfigSettled }),
streamStatusQueryKey: (conversationId: string) => ['streamStatus', conversationId],
}));
@@ -216,7 +216,18 @@ function renderUseResumeOnLoad({
<SiblingIndexProbe />
<PendingSteersProbe />
<QueuedMessagesProbe />
- {children}
+ <ChatSettingsContext.Provider
+ value={{
+ ...defaultChatSettings,
+ config: {
+ ...defaultChatSettings.config,
+ retentionMode: mockStartupConfig?.interface?.retentionMode,
+ loaded: mockStartupConfigSettled,
+ },
+ }}
+ >
+ {children}
+ </ChatSettingsContext.Provider>
</SteerHostTestProvider>
</RecoilRoot>
</JotaiProvider>
@@ -466,7 +477,7 @@ describe('useResumeOnLoad', () => {
* `isTemporary: false`, but the resume converts it; the rebuilt submission must
* already treat it as hidden so it never bumps the chat in the history caches. */
it('treats a pre-policy run as temporary once retention is forced', async () => {
- mockStartupConfig = { interface: { retentionMode: 'ephemeral' } };
+ mockStartupConfig = { interface: { retentionMode: RetentionMode.EPHEMERAL } };
const observedSubmissions: Array<TSubmission | null> = [];
mockUseStreamStatus.mockReturnValue({
...ACTIVE_STATUS,
@@ -509,7 +520,7 @@ describe('useResumeOnLoad', () => {
});
expect(observedSubmissions.filter(Boolean)).toHaveLength(0);
- mockStartupConfig = { interface: { retentionMode: 'ephemeral' } };
+ mockStartupConfig = { interface: { retentionMode: RetentionMode.EPHEMERAL } };
mockStartupConfigSettled = true;
rerender();
await act(async () => {
diff --git a/client/src/hooks/SSE/abort.ts b/client/src/hooks/SSE/abort.ts
index 845cf915b4..df676d7eb5 100644
--- a/client/src/hooks/SSE/abort.ts
+++ b/client/src/hooks/SSE/abort.ts
@@ -13,14 +13,14 @@ import {
} from '~/utils';
import { createErrorMessage, resolveErrorTurn, resolveSettledErrorReadState } from './turns';
import { useChatTransport } from '~/Providers/ChatTransportContext';
-import { useAuthContext } from '~/hooks/AuthContext';
+import { useChatSettings } from '~/Providers/ChatSettingsContext';
/** Writes a failed or stopped turn: the error card, a cancelled run, and an abort that settles from what streamed. */
export default function useAbortHandlers(
ctx: EventHandlerContext,
finalHandler: (data: TFinalResData, submission: EventSubmission) => void,
) {
- const { token } = useAuthContext();
+ const { token } = useChatSettings().auth;
const transport = useChatTransport();
const {
setCompleted,
diff --git a/client/src/hooks/SSE/useResumableSSE.ts b/client/src/hooks/SSE/useResumableSSE.ts
index 8316994b7d..87623fd82d 100644
--- a/client/src/hooks/SSE/useResumableSSE.ts
+++ b/client/src/hooks/SSE/useResumableSSE.ts
@@ -19,9 +19,9 @@ import {
findActivityLabelMessageIndex,
findReasoningLabelMessageIndex,
} from '~/utils';
-import { useGetUserBalance, fetchStreamStatus, useGetStartupConfig } from '~/data-provider';
+import { useGetUserBalance, fetchStreamStatus } from '~/data-provider';
import { useChatTransport } from '~/Providers/ChatTransportContext';
-import { useAuthContext } from '~/hooks/AuthContext';
+import { useChatSettings } from '~/Providers/ChatSettingsContext';
import useResumableEvents from './events';
import useResumableSteers from './steers';
import useResumableCache from './cache';
@@ -67,16 +67,14 @@ export default function useResumableSSE(
) {
const [run] = useState(() => new ResumableRun());
const [streamId, setStreamId] = useState<string | null>(null);
- const { token, isAuthenticated } = useAuthContext();
+ const { auth, config } = useChatSettings();
+ const { token, isAuthenticated = false } = auth;
const transport = useChatTransport();
const { setMessages, getMessages, setConversation, setIsSubmitting, newConversation } =
chatHelpers;
- const { data: startupConfig } = useGetStartupConfig();
- const balanceQuery = useGetUserBalance({
- enabled: !!isAuthenticated && startupConfig?.balance?.enabled,
- });
- const balanceEnabled = startupConfig?.balance?.enabled ?? false;
+ const { balanceEnabled = false } = config;
+ const balanceQuery = useGetUserBalance({ enabled: isAuthenticated && balanceEnabled });
const refreshBalance = useCallback(() => {
balanceEnabled && balanceQuery.refetch();
}, [balanceEnabled, balanceQuery]);
diff --git a/client/src/hooks/SSE/useResumeOnLoad.ts b/client/src/hooks/SSE/useResumeOnLoad.ts
index c78dcc11c9..0cbabd3f4b 100644
--- a/client/src/hooks/SSE/useResumeOnLoad.ts
+++ b/client/src/hooks/SSE/useResumeOnLoad.ts
@@ -26,9 +26,10 @@ import {
import type { TMessage, Agents } from 'librechat-data-provider';
import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state';
import { pendingApprovalActionFamily } from '~/components/Chat/approval/state';
-import { useStreamStatus, useGetStartupConfig } from '~/data-provider';
+import { useChatSettings } from '~/Providers/ChatSettingsContext';
import { getBranchSiblingIndexesForTarget } from '~/utils';
import useResumeAnnouncements from './announcements';
+import { useStreamStatus } from '~/data-provider';
import { settleFinishedRun } from './finished';
import useOwedSuccessor from './successor';
import useSteerState from './steer';
@@ -65,8 +66,9 @@ export default function useResumeOnLoad(
const endpointType = currentConversation?.endpointType;
const actualEndpoint = endpointType ?? endpoint;
const resumableEnabled = !isAssistantsEndpoint(actualEndpoint);
- const { data: startupConfig, isFetched: startupConfigSettled } = useGetStartupConfig();
- const isRetentionForced = isForcedTemporaryRetention(startupConfig?.interface?.retentionMode);
+ const { config } = useChatSettings();
+ const startupConfigSettled = config.loaded !== false;
+ const isRetentionForced = isForcedTemporaryRetention(config.retentionMode);
// Track conversations we've already processed (either resumed or skipped)
const processedConvoRef = useRef<string | null>(null);
/**
diff --git a/client/src/hooks/SSE/useSSE.ts b/client/src/hooks/SSE/useSSE.ts
index 4e3e4f7efb..99538fdc64 100644
--- a/client/src/hooks/SSE/useSSE.ts
+++ b/client/src/hooks/SSE/useSSE.ts
@@ -20,10 +20,10 @@ import type { EventHandlerParams } from './useEventHandlers';
import type { TResData, TFinalResData } from '~/common';
import { clearComposerDrafts, applyPendingAction, findPendingActionMessageIndex } from '~/utils';
import { pendingApprovalActionFamily } from '~/components/Chat/approval/state';
-import { useGetStartupConfig, useGetUserBalance } from '~/data-provider';
import { useChatTransport } from '~/Providers/ChatTransportContext';
+import { useChatSettings } from '~/Providers/ChatSettingsContext';
import { reopenTermsOnRefusal } from '~/utils/terms';
-import { useAuthContext } from '~/hooks/AuthContext';
+import { useGetUserBalance } from '~/data-provider';
import useEventHandlers from './useEventHandlers';
import useUsageHandler from './useUsageHandler';
import useLocalize from '~/hooks/useLocalize';
@@ -49,7 +49,8 @@ export default function useSSE(
const jotaiStore = useStore();
const setActiveRunId = useSetAtom(activeRunFamily(runIndex));
- const { token, isAuthenticated } = useAuthContext();
+ const { auth, config } = useChatSettings();
+ const { token, isAuthenticated = false } = auth;
const transport = useChatTransport();
const [completed, setCompleted] = useState(new Set());
const setAbortScroll = useSetAtom(abortScrollFamily(runIndex));
@@ -86,10 +87,8 @@ export default function useSSE(
const queryClient = useQueryClient();
const localize = useLocalize();
- const { data: startupConfig } = useGetStartupConfig();
- const balanceQuery = useGetUserBalance({
- enabled: !!isAuthenticated && startupConfig?.balance?.enabled,
- });
+ const { balanceEnabled = false } = config;
+ const balanceQuery = useGetUserBalance({ enabled: isAuthenticated && balanceEnabled });
const {
bindResponse,
contextHandler,
@@ -144,7 +143,7 @@ export default function useSSE(
setIsSubmitting(false);
setShowStopButton(false);
}
- (startupConfig?.balance?.enabled ?? false) && balanceQuery.refetch();
+ balanceEnabled && balanceQuery.refetch();
console.log('final', data);
};
@@ -212,7 +211,7 @@ export default function useSSE(
const handleError = (data: ChatErrorData | null | undefined) => {
console.log('error in server stream.');
const termsRefused = reopenTermsOnRefusal(queryClient, data);
- (startupConfig?.balance?.enabled ?? false) && balanceQuery.refetch();
+ balanceEnabled && balanceQuery.refetch();
resetLive(currentSubmission);
if (data === undefined) {
setIsSubmitting(false);
diff --git a/client/src/routes/ChatSettings.tsx b/client/src/routes/ChatSettings.tsx
index 131c74a469..a291c13fff 100644
--- a/client/src/routes/ChatSettings.tsx
+++ b/client/src/routes/ChatSettings.tsx
@@ -38,8 +38,8 @@ export default function ChatSettingsProvider({ children }: { children: ReactNode
const [isTemporary, setIsTemporary] = useRecoilState<boolean>(store.isTemporary);
const getEphemeralAgent = useGetEphemeralAgent();
const addedConversation = useRecoilValue(store.conversationByKeySelector(1));
- const { data: startupConfig } = useGetStartupConfig();
- const { user } = useAuthContext();
+ const { data: startupConfig, isFetched: startupConfigLoaded } = useGetStartupConfig();
+ const { user, token, isAuthenticated } = useAuthContext();
const config = useMemo<ChatConfig>(() => {
const ui = startupConfig?.interface;
@@ -51,9 +51,14 @@ export default function ChatSettingsProvider({ children }: { children: ReactNode
queuedSendLockTimeoutMs: ui?.queuedSendLockTimeoutMs,
queuedTurnReconciliationTimeoutMs: ui?.queuedTurnReconciliationTimeoutMs,
steerArmConfirmationTimeoutMs: ui?.steerArmConfirmationTimeoutMs,
+ loaded: startupConfigLoaded,
+ balanceEnabled: startupConfig?.balance?.enabled ?? false,
};
- }, [startupConfig]);
- const auth = useMemo<ChatAuth>(() => ({ user }), [user]);
+ }, [startupConfig, startupConfigLoaded]);
+ const auth = useMemo<ChatAuth>(
+ () => ({ user, token, isAuthenticated }),
+ [user, token, isAuthenticated],
+ );
const getPaneConversations = usePaneConversations();
const settings = useMemo<ChatSettings>(
diff --git a/client/src/routes/__tests__/ChatSettings.spec.tsx b/client/src/routes/__tests__/ChatSettings.spec.tsx
index 4c5881b463..03302fdf11 100644
--- a/client/src/routes/__tests__/ChatSettings.spec.tsx
+++ b/client/src/routes/__tests__/ChatSettings.spec.tsx
@@ -13,11 +13,15 @@ let mockStartupConfig: Partial<TStartupConfig> | undefined;
let mockUser: TUser | undefined;
jest.mock('~/data-provider', () => ({
- useGetStartupConfig: () => ({ data: mockStartupConfig }),
+ useGetStartupConfig: () => ({ data: mockStartupConfig, isFetched: mockStartupConfig != null }),
}));
jest.mock('~/hooks/AuthContext', () => ({
- useAuthContext: () => ({ user: mockUser }),
+ useAuthContext: () => ({
+ user: mockUser,
+ token: mockUser ? 'token-1' : undefined,
+ isAuthenticated: mockUser != null,
+ }),
}));
const renderSettings = (initialize?: (snapshot: MutableSnapshot) => void) =>
@@ -93,6 +97,8 @@ describe('ChatSettingsProvider', () => {
expect(result.current.settings.config).toEqual({
feedbackEnabled: false,
canRenameRunningChat: false,
+ loaded: false,
+ balanceEnabled: false,
});
});
@@ -101,6 +107,7 @@ describe('ChatSettingsProvider', () => {
mockStartupConfig = {
conversationTitleOwnershipVersion: 1,
modelSpecs: { list: modelSpecs },
+ balance: { enabled: true },
interface: {
retentionMode: RetentionMode.EPHEMERAL,
runningChatRename: true,
@@ -120,6 +127,8 @@ describe('ChatSettingsProvider', () => {
queuedSendLockTimeoutMs: 1_000,
queuedTurnReconciliationTimeoutMs: 2_000,
steerArmConfirmationTimeoutMs: 3_000,
+ loaded: true,
+ balanceEnabled: true,
});
});
@@ -136,7 +145,11 @@ describe('ChatSettingsProvider', () => {
const { result } = renderSettings();
- expect(result.current.settings.auth.user).toBe(mockUser);
+ expect(result.current.settings.auth).toEqual({
+ user: mockUser,
+ token: 'token-1',
+ isAuthenticated: true,
+ });
});
it('falls back to the stock defaults without a host', () => {