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

🪶 fix: Time Out Client Refresh and Trim Lodash Imports (#17467)

Marco Beretta7d0e79786f6a
diff --git a/api/server/routes/__tests__/config.spec.js b/api/server/routes/__tests__/config.spec.js
index 2a295e73a0..a6feec5d20 100644
--- a/api/server/routes/__tests__/config.spec.js
+++ b/api/server/routes/__tests__/config.spec.js
@@ -176,6 +176,17 @@ describe('GET /api/config', () => {
expect(response.body.turnstile).toEqual({ siteKey: 'yaml-site-key' });
});
+ it('publishes configured client timeouts before login and omits invalid ones', async () => {
+ mockGetTenantId.mockReturnValue(undefined);
+ mockGetAppConfig.mockResolvedValue({ config: { clientLimits: { refreshMs: 90000 } } });
+ const configured = await request(createApp(null)).get('/api/config');
+ expect(configured.body.clientLimits).toEqual({ refreshMs: 90000 });
+
+ mockGetAppConfig.mockResolvedValue({ config: { clientLimits: { refreshMs: -1 } } });
+ const invalid = await request(createApp(null)).get('/api/config');
+ expect(invalid.body).not.toHaveProperty('clientLimits');
+ });
+
it('should never serve a signed-out visitor a policy link that would run script', async () => {
mockGetAppConfig.mockResolvedValue({
...baseAppConfig,
diff --git a/api/server/routes/config.js b/api/server/routes/config.js
index a7d32cd15d..3827b06599 100644
--- a/api/server/routes/config.js
+++ b/api/server/routes/config.js
@@ -24,6 +24,7 @@ const {
resolveCodeWorkspaceInheritanceCapability,
resolveCodeEnvironmentTransitionVersion,
loadConversationListLimits,
+ getClientLimits,
resolvePullRequestCapabilities,
resolvePreLoginAppConfig,
withSafePolicyLinks,
@@ -242,6 +243,7 @@ router.get('/', async function (req, res) {
socialLogins: baseConfig?.registration?.socialLogins ?? defaultSocialLogins,
turnstile: baseConfig?.turnstileConfig,
rum: getRumConfig(baseConfig?.config?.telemetry),
+ clientLimits: getClientLimits(baseConfig?.config?.clientLimits),
};
const interfaceConfig = baseConfig?.interfaceConfig;
@@ -359,6 +361,7 @@ router.get('/', async function (req, res) {
...(codeEnvironmentTransitionVersion != null ? { codeEnvironmentTransitionVersion } : {}),
...(cloudFront ? { cloudFront } : {}),
rum: getRumConfig(appConfig?.config?.telemetry),
+ clientLimits: getClientLimits(appConfig?.config?.clientLimits),
fileUploadSseEnabled: isEnabled(process.env.FILE_UPLOAD_SSE_ENABLED),
endpointsDropParamsMap: endpointsDropParamsMap,
};
diff --git a/client/src/components/Chat/Input/StreamAudio.tsx b/client/src/components/Chat/Input/StreamAudio.tsx
index 4288474dc9..25fea1729f 100644
--- a/client/src/components/Chat/Input/StreamAudio.tsx
+++ b/client/src/components/Chat/Input/StreamAudio.tsx
@@ -1,8 +1,8 @@
import { useEffect, useCallback } from 'react';
import { useParams } from 'react-router-dom';
import { useQueryClient } from '@tanstack/react-query';
-import { QueryKeys, getSpeechText } from 'librechat-data-provider';
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
+import { QueryKeys, getSpeechText, getClientLimit } from 'librechat-data-provider';
import type { TMessage } from 'librechat-data-provider';
import {
useCustomAudioRef,
@@ -90,11 +90,23 @@ export default function StreamAudio({ index = 0 }) {
}
logger.log('Fetching audio...', navigator.userAgent);
- const response = await fetch('/api/files/speech/tts', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
- body: JSON.stringify({ messageId: latestMessage?.messageId, runId: activeRunId, voice }),
- });
+ const controller = new AbortController();
+ const abortTimer = setTimeout(() => controller.abort(), getClientLimit('ttsMs'));
+ let response: Response;
+ try {
+ response = await fetch('/api/files/speech/tts', {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
+ body: JSON.stringify({
+ messageId: latestMessage?.messageId,
+ runId: activeRunId,
+ voice,
+ }),
+ signal: controller.signal,
+ });
+ } finally {
+ clearTimeout(abortTimer);
+ }
if (!response.ok) {
throw new Error('Failed to fetch audio');
diff --git a/client/src/components/Lia/Stage.tsx b/client/src/components/Lia/Stage.tsx
index 641142f9a5..d124c46d19 100644
--- a/client/src/components/Lia/Stage.tsx
+++ b/client/src/components/Lia/Stage.tsx
@@ -276,6 +276,24 @@ export default function Stage({
});
document.body.appendChild(mirror);
+ /* Caret measurement forces layout; coalesce keystrokes and focus changes into one read per frame. */
+ let caretTarget: HTMLTextAreaElement | null = null;
+ let caretFrame = 0;
+ const updateCaret = (target: HTMLTextAreaElement) => {
+ caretTarget = target;
+ if (caretFrame) {
+ return;
+ }
+ caretFrame = requestAnimationFrame(() => {
+ caretFrame = 0;
+ const next = caretTarget;
+ caretTarget = null;
+ if (next) {
+ engine.caret = caretPoint(next, mirror, root.getBoundingClientRect());
+ }
+ });
+ };
+
let keyTimes: number[] = [];
let longShown = false;
const onInput = (e: Event) => {
@@ -285,7 +303,7 @@ export default function Stage({
}
const now = performance.now();
engine.noteTyping();
- engine.caret = caretPoint(target, mirror, root.getBoundingClientRect());
+ updateCaret(target);
/* Deleting back under the threshold re-arms the reaction for the next long message. */
if (target.value.length <= 280) {
longShown = false;
@@ -328,7 +346,7 @@ export default function Stage({
};
const onFocusIn = (e: FocusEvent) => {
if (e.target instanceof HTMLTextAreaElement) {
- engine.caret = caretPoint(e.target, mirror, root.getBoundingClientRect());
+ updateCaret(e.target);
engine.glance('curious', 900, 'caret');
engine.noteActivity(true);
}
@@ -543,6 +561,7 @@ export default function Stage({
band?.removeEventListener('keydown', onKeyDown);
window.removeEventListener('pointermove', onPointerMove);
cancelAnimationFrame(pointerFrame);
+ cancelAnimationFrame(caretFrame);
window.removeEventListener('dragenter', onDragEnter);
window.removeEventListener('dragleave', onDragLeave);
window.removeEventListener('drop', onDrop);
diff --git a/client/src/components/Projects/ProjectResources.spec.tsx b/client/src/components/Projects/ProjectResources.spec.tsx
index 9a380fe9c8..341ea4ba8c 100644
--- a/client/src/components/Projects/ProjectResources.spec.tsx
+++ b/client/src/components/Projects/ProjectResources.spec.tsx
@@ -237,7 +237,8 @@ describe('ProjectResources', () => {
},
});
fireEvent.change(input, { target: { files: [new File(['d'], 'another-excess.txt')] } });
- expect(mockUploadMutateAsync).toHaveBeenCalledTimes(1);
+ /** Uploads run concurrently, so the second file starts while the first is still in flight. */
+ expect(mockUploadMutateAsync).toHaveBeenCalledTimes(2);
expect(mockShowToast).toHaveBeenCalledTimes(2);
expect(mockShowToast).toHaveBeenLastCalledWith(
expect.objectContaining({ message: 'Could not add 1 selected file(s)' }),
@@ -245,10 +246,9 @@ describe('ProjectResources', () => {
finishFirstUpload(uploadedFile);
await waitFor(() => expect(mockAddMutateAsync).toHaveBeenCalledTimes(2));
expect(mockUploadMutateAsync).toHaveBeenCalledTimes(2);
- expect(mockAddMutateAsync.mock.calls.map(([payload]) => payload.file_id)).toEqual([
- uploadedFile.file_id,
- 'second-canonical-id',
- ]);
+ expect(mockAddMutateAsync.mock.calls.map(([payload]) => payload.file_id).sort()).toEqual(
+ [uploadedFile.file_id, 'second-canonical-id'].sort(),
+ );
});
it('retries a failed association without uploading another binary', async () => {
diff --git a/client/src/components/Projects/ProjectResources.tsx b/client/src/components/Projects/ProjectResources.tsx
index 6f3ce2c965..fdbe69bf74 100644
--- a/client/src/components/Projects/ProjectResources.tsx
+++ b/client/src/components/Projects/ProjectResources.tsx
@@ -29,6 +29,7 @@ import {
defaultAgentCapabilities,
EToolResources,
FileContext,
+ getClientLimit,
MAX_CHAT_PROJECT_FILES,
PermissionTypes,
Permissions,
@@ -293,10 +294,20 @@ export default function ProjectResources({ project }: ProjectResourcesProps) {
}
};
+ /** runUpload reports its own failures per item, so workers never reject. */
const processUploads = async (items: UploadState[]) => {
- for (const item of items) {
- await runUpload(item);
- }
+ let next = 0;
+ const worker = async () => {
+ while (next < items.length) {
+ const item = items[next++];
+ await runUpload(item);
+ }
+ };
+ await Promise.all(
+ Array.from({ length: Math.min(getClientLimit('uploadConcurrency'), items.length) }, () =>
+ worker(),
+ ),
+ );
};
const handleUploadChange = (event: React.ChangeEvent<HTMLInputElement>) => {
diff --git a/client/src/data-provider/Files/sharepoint.ts b/client/src/data-provider/Files/sharepoint.ts
index cccd859741..b55fe0efb4 100644
--- a/client/src/data-provider/Files/sharepoint.ts
+++ b/client/src/data-provider/Files/sharepoint.ts
@@ -1,4 +1,5 @@
import { useMutation } from '@tanstack/react-query';
+import { getClientLimit } from 'librechat-data-provider';
import type { UseMutationResult } from '@tanstack/react-query';
export interface SharePointFile {
@@ -48,17 +49,26 @@ export const useSharePointBatchDownload = (): UseMutationResult<
file.downloadUrl ||
`https://graph.microsoft.com/v1.0/drives/${file.driveId}/items/${file.itemId}/content`;
- const response = await fetch(downloadUrl, {
- headers: {
- Authorization: `Bearer ${accessToken}`,
- },
- });
-
- if (!response.ok) {
- throw new Error(`${response.status} ${response.statusText}`);
+ const controller = new AbortController();
+ const abortTimer = setTimeout(() => controller.abort(), getClientLimit('sharepointMs'));
+ let response: Response;
+ let blob: Blob;
+ try {
+ response = await fetch(downloadUrl, {
+ headers: {
+ Authorization: `Bearer ${accessToken}`,
+ },
+ signal: controller.signal,
+ });
+
+ if (!response.ok) {
+ throw new Error(`${response.status} ${response.statusText}`);
+ }
+
+ blob = await response.blob();
+ } finally {
+ clearTimeout(abortTimer);
}
-
- const blob = await response.blob();
const contentType =
response.headers.get('content-type') || getMimeTypeFromFileName(file.name);
diff --git a/client/src/data-provider/SSE/__tests__/queries.test.ts b/client/src/data-provider/SSE/__tests__/queries.test.ts
index b408a9d571..6a28fb6fe8 100644
--- a/client/src/data-provider/SSE/__tests__/queries.test.ts
+++ b/client/src/data-provider/SSE/__tests__/queries.test.ts
@@ -41,6 +41,9 @@ import {
fetchStreamStatus,
genTitleQueryKey,
queueTitleGeneration,
+ markTitleGenerationProcessed,
+ isTitleGenerationProcessed,
+ clearTitleGenerationState,
useActiveJobs,
resetActiveJobsGrace,
extendActiveJobsGrace,
@@ -280,3 +283,12 @@ describe('title fetch retry policy — failure cap', () => {
}
});
});
+
+describe('clearTitleGenerationState', () => {
+ it('forgets processed conversations so the next session starts clean', () => {
+ markTitleGenerationProcessed('convo-clear');
+ expect(isTitleGenerationProcessed('convo-clear')).toBe(true);
+ clearTitleGenerationState();
+ expect(isTitleGenerationProcessed('convo-clear')).toBe(false);
+ });
+});
diff --git a/client/src/data-provider/SSE/queries.ts b/client/src/data-provider/SSE/queries.ts
index 87d860f1b8..3d866a5e94 100644
--- a/client/src/data-provider/SSE/queries.ts
+++ b/client/src/data-provider/SSE/queries.ts
@@ -64,6 +64,14 @@ export function queueTitleGeneration(conversationId: string) {
}
}
+/** Drops all module-level title state; called when a session ends so the next account starts clean. */
+export function clearTitleGenerationState() {
+ titleQueue.clear();
+ processedTitles.clear();
+ deferredTitles.clear();
+ queueListeners.forEach((listener) => listener());
+}
+
export function isTitleGenerationProcessed(conversationId: string): boolean {
return processedTitles.has(conversationId);
}
diff --git a/client/src/data-provider/connection.ts b/client/src/data-provider/connection.ts
index d5cff45652..135af9afdf 100644
--- a/client/src/data-provider/connection.ts
+++ b/client/src/data-provider/connection.ts
@@ -67,6 +67,7 @@ export const useHealthCheck = (isAuthenticated = false) => {
}, 500);
return () => {
+ isInitialized.current = false;
clearTimeout(initTimer);
if (intervalRef.current) {
clearInterval(intervalRef.current);
diff --git a/client/src/hooks/AuthContext.tsx b/client/src/hooks/AuthContext.tsx
index 53c6be209e..965d979ac8 100644
--- a/client/src/hooks/AuthContext.tsx
+++ b/client/src/hooks/AuthContext.tsx
@@ -7,7 +7,7 @@ import {
useCallback,
createContext,
} from 'react';
-import { debounce } from 'lodash';
+import debounce from 'lodash/debounce';
import { getDefaultStore } from 'jotai';
import { useNavigate } from 'react-router-dom';
import { useRecoilState, useSetRecoilState } from 'recoil';
@@ -41,6 +41,7 @@ import {
useGetUserQuery,
endPullRequestSession,
useLoginUserMutation,
+ clearTitleGenerationState,
setPullRequestSessionOwner,
useLogoutUserMutation,
useRefreshTokenMutation,
@@ -69,6 +70,7 @@ const endSessionClientState = (): void => {
clearRetainedFileDeletions();
clearComposerDraftStorage();
endPullRequestSession();
+ clearTitleGenerationState();
void clearTTSCache();
};
/** `document.prerendering` is not yet part of TypeScript's DOM types. */
diff --git a/client/src/hooks/Chat/ask/turn.ts b/client/src/hooks/Chat/ask/turn.ts
index bfd966d7bc..efb6944195 100644
--- a/client/src/hooks/Chat/ask/turn.ts
+++ b/client/src/hooks/Chat/ask/turn.ts
@@ -1,5 +1,5 @@
import { v4 } from 'uuid';
-import { cloneDeep } from 'lodash';
+import cloneDeep from 'lodash/cloneDeep';
import {
Constants,
ContentTypes,
diff --git a/client/src/hooks/Chat/useChatFunctions.ts b/client/src/hooks/Chat/useChatFunctions.ts
index eb71a65841..f9d0baeab3 100644
--- a/client/src/hooks/Chat/useChatFunctions.ts
+++ b/client/src/hooks/Chat/useChatFunctions.ts
@@ -1,5 +1,5 @@
import { useCallback, useEffect, useRef } from 'react';
-import { cloneDeep } from 'lodash';
+import cloneDeep from 'lodash/cloneDeep';
import { useStore, useSetAtom } from 'jotai';
import { useNavigate } from 'react-router-dom';
import { useQueryClient } from '@tanstack/react-query';
diff --git a/client/src/hooks/Files/useSharePointPicker.ts b/client/src/hooks/Files/useSharePointPicker.ts
index 21cbd520de..81ed638c1e 100644
--- a/client/src/hooks/Files/useSharePointPicker.ts
+++ b/client/src/hooks/Files/useSharePointPicker.ts
@@ -1,4 +1,4 @@
-import { useRef, useCallback } from 'react';
+import { useRef, useEffect, useCallback } from 'react';
import { useRecoilState } from 'recoil';
import { useToastContext } from '@librechat/client';
import type { SPPickerConfig } from '~/components/SidePanel/Agents/config';
@@ -332,6 +332,7 @@ export default function useSharePointPicker({
activeEventListenerRef.current = null;
}
if (portRef.current) {
+ portRef.current.removeEventListener('message', portMessageHandler);
portRef.current.close();
portRef.current = null;
}
@@ -339,7 +340,17 @@ export default function useSharePointPicker({
containerNode.innerHTML = '';
}
channelIdRef.current = '';
- }, [containerNode]);
+ }, [containerNode, portMessageHandler]);
+
+ /** The window and port listeners must not outlive the component, even if the owner never closes the picker. */
+ const cleanupRef = useRef(cleanup);
+ cleanupRef.current = cleanup;
+ useEffect(
+ () => () => {
+ cleanupRef.current();
+ },
+ [],
+ );
const handleDialogClose = useCallback(() => {
cleanup();
diff --git a/client/src/hooks/__tests__/AuthContext.spec.tsx b/client/src/hooks/__tests__/AuthContext.spec.tsx
index 80d9d9c093..c3bf34c560 100644
--- a/client/src/hooks/__tests__/AuthContext.spec.tsx
+++ b/client/src/hooks/__tests__/AuthContext.spec.tsx
@@ -56,6 +56,7 @@ let mockCapturedLogoutOptions: {
};
const mockEndPullRequestSession = jest.fn();
+const mockClearTitleGenerationState = jest.fn();
const mockRefreshMutate = jest.fn();
const mockLoginMutate = jest.fn();
const mockLogoutMutate = jest.fn();
@@ -88,6 +89,7 @@ jest.mock('~/data-provider', () => ({
})),
useGetRole: jest.fn(() => ({ data: null })),
endPullRequestSession: jest.fn(() => mockEndPullRequestSession()),
+ clearTitleGenerationState: jest.fn(() => mockClearTitleGenerationState()),
setPullRequestSessionOwner: jest.fn(),
useListRoles: jest.fn(() => ({ data: undefined })),
}));
@@ -525,6 +527,17 @@ describe('AuthContextProvider — logout onSuccess/onError handling', () => {
expect(mockEndPullRequestSession).toHaveBeenCalled();
});
+ it('clears module-level title generation state at the logout session boundary', () => {
+ mockClearTitleGenerationState.mockClear();
+ renderProvider();
+
+ act(() => {
+ mockCapturedLogoutOptions.onSuccess({ message: 'Logout successful' });
+ });
+
+ expect(mockClearTitleGenerationState).toHaveBeenCalled();
+ });
+
it('does not end the sidebar pull request queue while a session is being established', () => {
mockEndPullRequestSession.mockClear();
renderProvider();
diff --git a/client/src/hooks/useWakeLock.ts b/client/src/hooks/useWakeLock.ts
index a071947867..5d54f68722 100644
--- a/client/src/hooks/useWakeLock.ts
+++ b/client/src/hooks/useWakeLock.ts
@@ -118,6 +118,11 @@ export const useWakeLock = (shouldHold: boolean) => {
try {
const sentinel = await wakeLock.request('screen');
+ if (cancelled || wakeLockRef.current) {
+ /** The effect ended (or another request won) while this one was in flight. */
+ await sentinel.release().catch(() => undefined);
+ return;
+ }
wakeLockRef.current = sentinel;
if (DEBUG_WAKE_LOCK) {
diff --git a/client/src/utils/conversation.ts b/client/src/utils/conversation.ts
index 1f578e3884..1e02a5b40e 100644
--- a/client/src/utils/conversation.ts
+++ b/client/src/utils/conversation.ts
@@ -1,4 +1,4 @@
-import { pick } from 'lodash';
+import pick from 'lodash/pick';
import type { TConversation } from 'librechat-data-provider';
export const isTemporaryConversation = (conversation?: Partial<TConversation> | null): boolean =>
diff --git a/e2e/specs/mock/scenarios/client-refresh-timeout.spec.ts b/e2e/specs/mock/scenarios/client-refresh-timeout.spec.ts
new file mode 100644
index 0000000000..37a83a0b71
--- /dev/null
+++ b/e2e/specs/mock/scenarios/client-refresh-timeout.spec.ts
@@ -0,0 +1,35 @@
+import { expect, test } from '@playwright/test';
+import { NEW_CHAT_PATH } from '../helpers';
+
+/**
+ * A token refresh that never answers must not freeze the app: the deadline published in the
+ * startup config (`clientLimits.refreshMs`, kept from the previous load) expires and the visitor lands on the sign-in page.
+ */
+test('a hung token refresh sends the visitor to login after the configured deadline @scenario:client-refresh-timeout', async ({
+ page,
+}) => {
+ await page.route(
+ (url) => url.pathname === '/api/config',
+ async (route) => {
+ const response = await route.fetch();
+ const body = await response.json();
+ await route.fulfill({ response, json: { ...body, clientLimits: { refreshMs: 1500 } } });
+ },
+ );
+ let hangRefresh = false;
+ await page.route(
+ (url) => url.pathname === '/api/auth/refresh',
+ (route) => (hangRefresh ? new Promise<void>(() => undefined) : route.continue()),
+ );
+
+ /** The first load publishes the deadline; the next load's refresh starts with it already known. */
+ await page.goto(NEW_CHAT_PATH, { timeout: 15000 });
+ await page.waitForFunction(() => localStorage.getItem('librechat:clientLimits') != null, null, {
+ timeout: 15000,
+ });
+ hangRefresh = true;
+ await page.goto(NEW_CHAT_PATH, { timeout: 15000, waitUntil: 'commit' });
+
+ await expect(page).toHaveURL(/\/login/, { timeout: 15000 });
+ await expect(page.getByTestId('login-button')).toBeVisible();
+});
diff --git a/librechat.example.yaml b/librechat.example.yaml
index 85a878b3fe..801d1d96d6 100644
--- a/librechat.example.yaml
+++ b/librechat.example.yaml
@@ -16,6 +16,16 @@ cache: true
# sessionReplay:
# enabled: false # Explicitly opt in to masked DOM/input recordings.
+# Client limits: request deadlines in milliseconds (1 to 3600000) and upload concurrency. Every field is optional and defaults
+# to the value shown, which is the behavior when the block is absent. Raise a deadline when your
+# infrastructure legitimately needs longer.
+# clientLimits:
+# refreshMs: 30000 # token refresh; on expiry the user is sent to login
+# ttsMs: 60000 # time for the text-to-speech endpoint to start responding
+# sharepointMs: 300000 # download of one SharePoint file
+# cloudfrontMs: 15000 # CloudFront signed-cookie refresh
+# uploadConcurrency: 3 # project files uploaded at once (1 to 10)
+
# Chat Projects: the ceilings a single project may reach. Every field is optional and
# defaults to the value shown, which is the behavior when the block is absent.
projects:
diff --git a/packages/api/src/auth/openidRoleSync.ts b/packages/api/src/auth/openidRoleSync.ts
index d41fa45bd4..373078979b 100644
--- a/packages/api/src/auth/openidRoleSync.ts
+++ b/packages/api/src/auth/openidRoleSync.ts
@@ -1,4 +1,4 @@
-import { get } from 'lodash';
+import get from 'lodash/get';
import { SystemRoles } from 'librechat-data-provider';
import type { RecordAuditEntry } from '~/admin/audit';
import { recordSystemAuditEntry } from '~/admin/audit';
diff --git a/packages/api/src/clientlimits/client.spec.ts b/packages/api/src/clientlimits/client.spec.ts
new file mode 100644
index 0000000000..2d1c0756c8
--- /dev/null
+++ b/packages/api/src/clientlimits/client.spec.ts
@@ -0,0 +1,20 @@
+import { getClientLimits } from './client';
+
+describe('getClientLimits', () => {
+ it('returns undefined when unset or empty', () => {
+ expect(getClientLimits(undefined)).toBeUndefined();
+ expect(getClientLimits({})).toBeUndefined();
+ });
+
+ it('publishes configured values', () => {
+ expect(getClientLimits({ refreshMs: 90000, ttsMs: 120000 })).toEqual({
+ refreshMs: 90000,
+ ttsMs: 120000,
+ });
+ });
+
+ it('drops invalid configuration so client defaults apply', () => {
+ expect(getClientLimits({ refreshMs: -1 })).toBeUndefined();
+ expect(getClientLimits({ unknown: 5 })).toBeUndefined();
+ });
+});
diff --git a/packages/api/src/clientlimits/client.ts b/packages/api/src/clientlimits/client.ts
new file mode 100644
index 0000000000..2c815af11d
--- /dev/null
+++ b/packages/api/src/clientlimits/client.ts
@@ -0,0 +1,10 @@
+import { clientLimitsSchema } from 'librechat-data-provider';
+import type { TClientLimits } from 'librechat-data-provider';
+
+/** Publishes the operator's client request deadlines; invalid input is dropped so defaults apply. */
+export function getClientLimits(raw?: unknown): TClientLimits | undefined {
+ if (raw == null) return undefined;
+ const parsed = clientLimitsSchema.safeParse(raw);
+ if (!parsed.success || Object.keys(parsed.data).length === 0) return undefined;
+ return parsed.data;
+}
diff --git a/packages/api/src/index.ts b/packages/api/src/index.ts
index a8cbd98930..504c9d821f 100644
--- a/packages/api/src/index.ts
+++ b/packages/api/src/index.ts
@@ -142,6 +142,7 @@ export * from './rum/proxy';
export * from './rum/policy';
export * from './rum/config';
export * from './rum/limiter';
+export * from './clientlimits/client';
/* OpenAPI */
export { createOpenApiRouter } from './openapi/router';
export type { OpenApiRouterDeps } from './openapi/router';
diff --git a/packages/client/src/utils/cloudfront.spec.tsx b/packages/client/src/utils/cloudfront.spec.tsx
index 6b4e0affc9..73d88535fd 100644
--- a/packages/client/src/utils/cloudfront.spec.tsx
+++ b/packages/client/src/utils/cloudfront.spec.tsx
@@ -5,6 +5,7 @@ const mockGetTokenHeader = jest.fn(() => 'Bearer test-token');
jest.mock('librechat-data-provider', () => ({
apiBaseUrl: () => mockApiBaseUrl(),
+ getClientLimit: () => 15000,
}));
import {
diff --git a/packages/client/src/utils/cloudfront.ts b/packages/client/src/utils/cloudfront.ts
index 1dc07cff03..5d9bc6bd4a 100644
--- a/packages/client/src/utils/cloudfront.ts
+++ b/packages/client/src/utils/cloudfront.ts
@@ -1,4 +1,4 @@
-import { apiBaseUrl } from 'librechat-data-provider';
+import { apiBaseUrl, getClientLimit } from 'librechat-data-provider';
import type { TStartupConfig } from 'librechat-data-provider';
type CloudFrontCookieRefreshConfig = NonNullable<
@@ -108,19 +108,26 @@ async function postCloudFrontCookieRefresh(endpoint: string): Promise<boolean> {
headers.Authorization = authorization;
}
- const response = await fetch(endpoint, {
- method: 'POST',
- credentials: 'include',
- headers,
- body: '{}',
- });
+ const controller = new AbortController();
+ const abortTimer = setTimeout(() => controller.abort(), getClientLimit('cloudfrontMs'));
+ try {
+ const response = await fetch(endpoint, {
+ method: 'POST',
+ credentials: 'include',
+ headers,
+ body: '{}',
+ signal: controller.signal,
+ });
- if (!response.ok) {
- return false;
- }
+ if (!response.ok) {
+ return false;
+ }
- const payload = (await response.json()) as CloudFrontCookieRefreshResponse;
- return payload.ok === true;
+ const payload = (await response.json()) as CloudFrontCookieRefreshResponse;
+ return payload.ok === true;
+ } finally {
+ clearTimeout(abortTimer);
+ }
}
export function refreshCloudFrontCookiesOnce(): Promise<boolean> {
diff --git a/packages/data-provider/src/clientLimits.spec.ts b/packages/data-provider/src/clientLimits.spec.ts
new file mode 100644
index 0000000000..fb87d0b76d
--- /dev/null
+++ b/packages/data-provider/src/clientLimits.spec.ts
@@ -0,0 +1,42 @@
+/**
+ * @jest-environment jsdom
+ */
+import { DEFAULT_CLIENT_LIMITS, getClientLimit, setClientLimits } from './clientLimits';
+import { configSchema } from './config';
+
+describe('client timeouts', () => {
+ afterEach(() => {
+ setClientLimits(undefined);
+ localStorage.clear();
+ });
+
+ it('uses the defaults until the startup config sets values', () => {
+ expect(getClientLimit('refreshMs')).toBe(DEFAULT_CLIENT_LIMITS.refreshMs);
+ setClientLimits({ refreshMs: 90_000 });
+ expect(getClientLimit('refreshMs')).toBe(90_000);
+ expect(getClientLimit('ttsMs')).toBe(DEFAULT_CLIENT_LIMITS.ttsMs);
+ });
+
+ it('defaults upload concurrency to three and accepts an override', () => {
+ expect(getClientLimit('uploadConcurrency')).toBe(3);
+ setClientLimits({ uploadConcurrency: 5 });
+ expect(getClientLimit('uploadConcurrency')).toBe(5);
+ });
+
+ it('falls back to defaults for invalid values', () => {
+ setClientLimits({ refreshMs: -5 });
+ expect(getClientLimit('refreshMs')).toBe(30_000);
+ });
+
+ it('keeps the published values for the next page load', () => {
+ setClientLimits({ refreshMs: 45_000 });
+ expect(JSON.parse(localStorage.getItem('librechat:clientLimits') ?? '{}')).toEqual({
+ refreshMs: 45_000,
+ });
+ });
+
+ it('is accepted by configSchema', () => {
+ expect(configSchema.shape.clientLimits.safeParse({ cloudfrontMs: 20_000 }).success).toBe(true);
+ expect(configSchema.shape.clientLimits.safeParse({ cloudfrontMs: 0 }).success).toBe(false);
+ });
+});
diff --git a/packages/data-provider/src/clientLimits.ts b/packages/data-provider/src/clientLimits.ts
new file mode 100644
index 0000000000..50dfa82a6f
--- /dev/null
+++ b/packages/data-provider/src/clientLimits.ts
@@ -0,0 +1,59 @@
+import { z } from 'zod';
+
+const timeoutMs = z.number().int().positive().max(3_600_000).optional();
+
+/** Operator-tunable client limits: request deadlines in milliseconds and upload concurrency. */
+export const clientLimitsSchema = z
+ .object({
+ refreshMs: timeoutMs,
+ ttsMs: timeoutMs,
+ sharepointMs: timeoutMs,
+ cloudfrontMs: timeoutMs,
+ uploadConcurrency: z.number().int().min(1).max(10).optional(),
+ })
+ .strict();
+
+export type TClientLimits = z.infer<typeof clientLimitsSchema>;
+
+export const DEFAULT_CLIENT_LIMITS: Required<TClientLimits> = {
+ refreshMs: 30_000,
+ ttsMs: 60_000,
+ sharepointMs: 5 * 60_000,
+ cloudfrontMs: 15_000,
+ uploadConcurrency: 3,
+};
+
+const STORAGE_KEY = 'librechat:clientLimits';
+
+const parseLimits = (raw: unknown): TClientLimits => {
+ const parsed = clientLimitsSchema.safeParse(raw ?? {});
+ return parsed.success ? parsed.data : {};
+};
+
+/** The first token refresh starts before the startup config arrives, so the last published
+ * values are kept from the previous visit; a first visit uses the defaults. */
+const readStored = (): TClientLimits => {
+ if (typeof window === 'undefined') return {};
+ try {
+ return parseLimits(JSON.parse(localStorage.getItem(STORAGE_KEY) ?? 'null'));
+ } catch {
+ return {};
+ }
+};
+
+let resolved: TClientLimits = readStored();
+
+/** Applies the limits published in the startup config; invalid values keep the defaults. */
+export function setClientLimits(limits?: TClientLimits | null): void {
+ resolved = parseLimits(limits);
+ if (typeof window === 'undefined') return;
+ try {
+ localStorage.setItem(STORAGE_KEY, JSON.stringify(resolved));
+ } catch {
+ /* storage unavailable: the values apply for this page load only */
+ }
+}
+
+export function getClientLimit(key: keyof TClientLimits): number {
+ return resolved[key] ?? DEFAULT_CLIENT_LIMITS[key];
+}
diff --git a/packages/data-provider/src/config.ts b/packages/data-provider/src/config.ts
index 6288605599..3b0545b281 100644
--- a/packages/data-provider/src/config.ts
+++ b/packages/data-provider/src/config.ts
@@ -64,6 +64,8 @@ import { ComponentTypes, SettingTypes, OptionTypes } from './generate';
import { STATEFUL_CODE_ENVIRONMENTS } from './stateful-code';
import { specsConfigSchema, TSpecsConfig } from './models';
import { fileConfigSchema } from './file-config';
+import { clientLimitsSchema } from './clientLimits';
+import type { TClientLimits } from './clientLimits';
import {
PULL_REQUEST_PROVIDERS,
PULL_REQUEST_BATCH_VERSION,
@@ -3714,6 +3716,7 @@ export type TStartupConfig = {
titleGenerationTiming?: 'immediate' | 'final';
analyticsGtmId?: string;
rum?: TRumConfig;
+ clientLimits?: TClientLimits;
bundlerURL?: string;
staticBundlerURL?: string;
/** Whether the deployment has a configured RAG service. */
@@ -4393,6 +4396,7 @@ export type TToolCallPreviewsConfig = z.infer<typeof toolCallPreviewsConfigSchem
export const configSchema = z.object({
version: z.string(),
telemetry: telemetrySchema.optional(),
+ clientLimits: clientLimitsSchema.optional(),
permissions: z
.object({
maxWriteAttempts: permissionWriteAttemptsSchema,
diff --git a/packages/data-provider/src/data-service.ts b/packages/data-provider/src/data-service.ts
index 253b1b0e75..3d97a725d2 100644
--- a/packages/data-provider/src/data-service.ts
+++ b/packages/data-provider/src/data-service.ts
@@ -15,6 +15,7 @@ import type * as t from './types';
import { codeSetupAttemptSchema, codeSetupApprovalRequestSchema } from './codeSetup';
import { TOOL_CALL_PREVIEWS_PARAM, TOOL_CALL_PREVIEWS_VERSION } from './previews';
import * as permissions from './accessPermissions';
+import { setClientLimits } from './clientLimits';
import * as endpoints from './api-endpoints';
import { uploadEventStream } from './upload';
import * as mcp from './types/mcpServers';
@@ -445,7 +446,12 @@ export const getStartupConfig = (
mcpCustomUserVars?: Record<string, { title: string; description: string }>;
}
> => {
- return request.get(endpoints.config(options?.context));
+ return request
+ .get<config.TStartupConfig>(endpoints.config(options?.context))
+ .then((startupConfig) => {
+ setClientLimits(startupConfig?.clientLimits);
+ return startupConfig;
+ });
};
export const getAIEndpoints = (): Promise<t.TEndpointsConfig> => {
diff --git a/packages/data-provider/src/index.ts b/packages/data-provider/src/index.ts
index 99b88b5358..001156490d 100644
--- a/packages/data-provider/src/index.ts
+++ b/packages/data-provider/src/index.ts
@@ -3,6 +3,7 @@ export * from './azure';
export * from './bedrock';
export * from './balance';
export * from './config';
+export * from './clientLimits';
export * from './footer';
export * from './theme';
export * from './themes';
diff --git a/packages/data-provider/src/request.ts b/packages/data-provider/src/request.ts
index 21af6ed95e..2295de4523 100644
--- a/packages/data-provider/src/request.ts
+++ b/packages/data-provider/src/request.ts
@@ -5,6 +5,7 @@ import type * as t from './types';
import { TWO_FACTOR_ENROLLMENT_REQUIRED_CODE } from './config';
import { persistTwoFactorSetupToken } from './twoFactor';
import { setTokenHeader } from './headers-helpers';
+import { getClientLimit } from './clientLimits';
import * as endpoints from './api-endpoints';
async function _get<T>(url: string, options?: AxiosRequestConfig): Promise<T> {
@@ -85,8 +86,9 @@ type AuthRecoveryWindow = Window & {
__librechatAuthRecovery?: AuthRecoveryState;
};
+/** A hung refresh gates every API call through the deduped recovery promise, so it must reject. */
const refreshToken = (retry?: boolean): Promise<t.TRefreshTokenResponse | undefined> =>
- _post(endpoints.refreshToken(retry));
+ _post(endpoints.refreshToken(retry), undefined, { timeout: getClientLimit('refreshMs') });
const SHARE_PAGE_PATH_REGEX = /^\/share\/[^/]+\/?$/;
const SHARED_MESSAGES_PATH_REGEX = /^\/api\/share\/[^/]+$/;