LibreChat/api/server/routes/config.js
Marco Beretta caeb786e97
🔀 feat: Show the Conversation Pull Request in the Sidebar List (#16815)
* feat: Show the Conversation Pull Request in the Sidebar List

Add a batch lookup route and a sidebar row mark: the state icon with the CI dot, opening the same card beside the row on hover. The row keeps the conversation's own title.

* fix: Address Review Findings on the Sidebar Pull Request Batch and Row Mark

Cap the whole batch with a configurable deadline, make the row mark a keyboard-reachable button that opens the card on focus, refresh rows when the window regains focus through the shared batch, and show a retryable failure state instead of treating a failed first lookup as no pull request.

* fix: Send Sidebar Pull Request Batches One at a Time

Queue every batch request behind the one before it, so an oversized list or an overlapping flush cannot multiply the server's configured lookup concurrency, and bound how long one request may hold the queue so a request that never answers cannot stop the rest.

* fix: Keep the Batch Request Timeout Above the Server Deadline

Default the client's request timeout to just past the longest batchTimeoutSeconds a deployment may configure, so a queued request never starts while the previous request's server-side lookups are still running.

* fix: Keep Batch Lookups Inside the Concurrency Limit and Per-Entry Failures Per Entry

Share one lookup limiter across every batch request so lookups that outlive a request's deadline keep their slot, answer a missing token only for the conversations that needed it, and point the row at its pull request description only while that text is in the page.

* fix: Bound Batch Reads, Scope Concurrency Per Credential, and Gate the Sidebar on the Batch Version

Start the batch deadline when the request arrives and bound the config and lane reads, count lookup slots per credential, advertise a batch route version and ask only a server that has it, take no row space for an absent mark, show a failed refresh over a cached empty answer, keep a malformed response from stalling the request queue, and repair the lane recording tests that still used a mock removed on dev.

* fix: Move Capability Resolution Into the Package and Harden the Batch Route and Queue

Resolve the pull request startup capabilities in packages/api so the route stays wiring only, bound the config and lane reads by the configured deadline, merge identical lanes before taking limiter slots, fall back to the single route when a replica has no batch route, and dispose the batch queue when the signed-in user changes.

* fix: Abort the Batch Fallback With Its Request So the Queue Never Overlaps It

Give the batch fetcher the request's abort signal and pass it to every single-route call, abort a request that times out or is disposed, and stop the fallback workers from starting more calls once it has.

* fix: Forward the Abort Signal to the Batch POST

Pass the request's abort signal through the batch fetcher, the data-provider call and Axios, so a batch request that times out or is disposed is cancelled instead of continuing beside the next one, and settle a request promptly when it is aborted.

* fix: Close the Pull Request Lookup Follow-Ups and Cancel Abandoned Sidebar Rows
2026-10-07 12:36:57 +02:00

395 lines
14 KiB
JavaScript

const express = require('express');
const {
isEnabled,
resolveEmailChangeSettings,
checkEmailConfig,
isLangfuseConnectionAvailable,
isLangfuseFanoutEnabled,
getBalanceConfig,
getCloudFrontConfig,
getAppConfigOptionsFromUser,
resolveBuildInfo,
resolveTitleTiming,
getConversationTitleCapabilities,
sanitizeModelSpecs,
excludeHiddenModelSpecs,
isFileSnapshotEnabled,
getEndpointsDropParamsMap,
resolveCodeEnvironmentDecisionVersion,
isPasskeyEnabled,
buildPreLoginInterface,
resolveMaxPasskeysPerUser,
resolveCodeEnvironmentMoveCapabilities,
resolveCodeWorkspaceInheritanceCapability,
resolveCodeEnvironmentTransitionVersion,
loadConversationListLimits,
resolvePullRequestCapabilities,
} = require('@librechat/api');
const {
DEFAULT_MCP_APP_CSP_LIMITS,
EModelEndpoint,
defaultSocialLogins,
resolveMCPAppsPolicy,
} = require('librechat-data-provider');
const { logger, getTenantId, SystemCapabilities } = require('@librechat/data-schemas');
const { hasCapability, hasConfigCapability } = require('~/server/middleware/roles/capabilities');
const { getLdapConfig } = require('~/server/services/Config/ldap');
const { getRumConfig } = require('~/server/services/Config/rum');
const { getAppConfig } = require('~/server/services/Config/app');
const router = express.Router();
const emailLoginEnabled =
process.env.ALLOW_EMAIL_LOGIN === undefined || isEnabled(process.env.ALLOW_EMAIL_LOGIN);
const passwordResetEnabled = isEnabled(process.env.ALLOW_PASSWORD_RESET);
const sharedLinksEnabled =
process.env.ALLOW_SHARED_LINKS === undefined || isEnabled(process.env.ALLOW_SHARED_LINKS);
const publicSharedLinksEnabled =
sharedLinksEnabled && isEnabled(process.env.ALLOW_SHARED_LINKS_PUBLIC);
const sharePointFilePickerEnabled = isEnabled(process.env.ENABLE_SHAREPOINT_FILEPICKER);
const openidReuseTokens = isEnabled(process.env.OPENID_REUSE_TOKENS);
/**
* Resolve build metadata eagerly at module load so the first `/api/config`
* request does not pay the cost of `execFileSync('git', ...)` on the hot path.
* The resolver caches its result after the first call.
*/
resolveBuildInfo();
function isBirthday() {
const today = new Date();
return today.getMonth() === 1 && today.getDate() === 11;
}
/**
* Pre-login fields rendered by the unauthenticated login, registration, password-reset,
* and email-verification pages. Any field added here is readable by anonymous callers
* of `GET /api/config`, so keep this set strictly to what those pages need.
*
* See client consumers under `client/src/components/Auth/` and `client/src/routes/Layouts/Startup.tsx`.
*/
function buildPreLoginPayload() {
const isOpenIdEnabled =
!!process.env.OPENID_CLIENT_ID &&
(isEnabled(process.env.OPENID_USE_PKCE) || !!process.env.OPENID_CLIENT_SECRET?.trim()) &&
!!process.env.OPENID_ISSUER &&
!!process.env.OPENID_SESSION_SECRET;
const isSamlEnabled =
!!process.env.SAML_ENTRY_POINT &&
!!process.env.SAML_ISSUER &&
!!process.env.SAML_CERT &&
!!process.env.SAML_SESSION_SECRET;
const ldap = getLdapConfig();
/** @type {Partial<TStartupConfig>} */
const payload = {
appTitle: process.env.APP_TITLE || 'LibreChat',
discordLoginEnabled: !!process.env.DISCORD_CLIENT_ID && !!process.env.DISCORD_CLIENT_SECRET,
facebookLoginEnabled: !!process.env.FACEBOOK_CLIENT_ID && !!process.env.FACEBOOK_CLIENT_SECRET,
githubLoginEnabled: !!process.env.GITHUB_CLIENT_ID && !!process.env.GITHUB_CLIENT_SECRET,
googleLoginEnabled: !!process.env.GOOGLE_CLIENT_ID && !!process.env.GOOGLE_CLIENT_SECRET,
appleLoginEnabled:
!!process.env.APPLE_CLIENT_ID &&
!!process.env.APPLE_TEAM_ID &&
!!process.env.APPLE_KEY_ID &&
!!process.env.APPLE_PRIVATE_KEY_PATH,
passkeyLoginEnabled: isPasskeyEnabled(),
openidLoginEnabled: isOpenIdEnabled,
openidLabel: process.env.OPENID_BUTTON_LABEL || 'Continue with OpenID',
openidImageUrl: process.env.OPENID_IMAGE_URL,
openidAutoRedirect: isEnabled(process.env.OPENID_AUTO_REDIRECT),
samlLoginEnabled: !isOpenIdEnabled && isSamlEnabled,
samlLabel: process.env.SAML_BUTTON_LABEL,
samlImageUrl: process.env.SAML_IMAGE_URL,
serverDomain: process.env.DOMAIN_SERVER || 'http://localhost:3080',
emailLoginEnabled,
registrationEnabled: !ldap?.enabled && isEnabled(process.env.ALLOW_REGISTRATION),
socialLoginEnabled: isEnabled(process.env.ALLOW_SOCIAL_LOGIN),
emailEnabled: checkEmailConfig(),
passwordResetEnabled,
twoFactorAuthenticationRequired: isEnabled(process.env.ENFORCE_TWO_FACTOR_AUTHENTICATION),
};
const minPasswordLength = parseInt(process.env.MIN_PASSWORD_LENGTH, 10);
if (minPasswordLength && !isNaN(minPasswordLength)) {
payload.minPasswordLength = minPasswordLength;
}
if (ldap) {
payload.ldap = ldap;
}
return payload;
}
/**
* Fields shared by authenticated chat and share-view config. Anonymous share
* views receive these through `/api/share/:shareId/config` after share access
* checks, not through the generic startup config endpoint.
*/
function buildPublicSharePayload() {
/** @type {Partial<TStartupConfig>} */
const payload = {
analyticsGtmId: process.env.ANALYTICS_GTM_ID,
};
if (typeof process.env.CUSTOM_FOOTER === 'string') {
payload.customFooter = process.env.CUSTOM_FOOTER;
}
return payload;
}
/**
* Post-login fields appended only when `req.user` is present. These describe the
* authenticated UX (account-settings links, share-link feature flags, birthday icon,
* openid token-reuse marker) and are not needed on the pre-login screens, so they
* are not exposed to unauthenticated callers.
*/
function buildPostLoginPayload(appConfig) {
/** @type {Partial<TStartupConfig>} */
const payload = {
showBirthdayIcon:
isBirthday() ||
isEnabled(process.env.SHOW_BIRTHDAY_ICON) ||
process.env.SHOW_BIRTHDAY_ICON === '',
helpAndFaqURL: process.env.HELP_AND_FAQ_URL || 'https://librechat.ai',
sharedLinksEnabled,
publicSharedLinksEnabled,
openidReuseTokens,
ragEnabled: Boolean(process.env.RAG_API_URL?.trim()),
/** Read inline (not module-level) for per-request evaluation and test isolation */
allowAccountDeletion:
process.env.ALLOW_ACCOUNT_DELETION === undefined ||
isEnabled(process.env.ALLOW_ACCOUNT_DELETION),
allowEmailChange: resolveEmailChangeSettings(appConfig?.emailChange).enabled,
maxPasskeysPerUser: resolveMaxPasskeysPerUser(appConfig?.passkeys),
};
return payload;
}
function buildBuildInfoPayload(interfaceConfig) {
if (interfaceConfig?.buildInfo === false) {
return undefined;
}
const info = resolveBuildInfo();
if (!info.commit && !info.branch && !info.buildDate) {
return undefined;
}
return {
commit: info.commit,
commitShort: info.commitShort,
branch: info.branch,
buildDate: info.buildDate,
};
}
function buildWebSearchConfig(appConfig) {
const ws = appConfig?.webSearch;
if (!ws) {
return undefined;
}
const { searchProvider, scraperProvider, rerankerType } = ws;
if (!searchProvider && !scraperProvider && !rerankerType) {
return undefined;
}
return {
...(searchProvider && { searchProvider }),
...(scraperProvider && { scraperProvider }),
...(rerankerType && { rerankerType }),
};
}
function buildCloudFrontStartupConfig() {
const config = getCloudFrontConfig();
if (
config?.imageSigning !== 'cookies' ||
!config.domain ||
!config.cookieDomain ||
!config.privateKey ||
!config.keyPairId
) {
return undefined;
}
return {
cookieRefresh: {
endpoint: '/api/auth/cloudfront/refresh',
domain: config.domain,
},
};
}
router.get('/', async function (req, res) {
try {
const preLoginPayload = buildPreLoginPayload();
const publicSharePayload = buildPublicSharePayload();
const rum = getRumConfig();
if (!req.user) {
const tenantId = getTenantId();
const baseConfig = await getAppConfig(tenantId ? { tenantId } : { baseOnly: true });
/** @type {Partial<TStartupConfig>} */
const payload = {
...preLoginPayload,
socialLogins: baseConfig?.registration?.socialLogins ?? defaultSocialLogins,
turnstile: baseConfig?.turnstileConfig,
...(rum ? { rum } : {}),
};
const interfaceConfig = baseConfig?.interfaceConfig;
const preLoginInterface = buildPreLoginInterface(interfaceConfig);
if (preLoginInterface) {
payload.interface = preLoginInterface;
}
const unauthBuildInfo = buildBuildInfoPayload(interfaceConfig);
if (unauthBuildInfo) {
payload.buildInfo = unauthBuildInfo;
}
return res.status(200).send(payload);
}
const [appConfig, conversationListLimits] = await Promise.all([
getAppConfig({
...getAppConfigOptionsFromUser(req.user),
failClosed: true,
}),
loadConversationListLimits(getAppConfig),
]);
const codeEnvironmentDecisionVersion = resolveCodeEnvironmentDecisionVersion(
process.env.CODE_ENVIRONMENT_DECISION_VERSION,
);
const codeEnvironmentMoveCapabilities = resolveCodeEnvironmentMoveCapabilities(appConfig);
const codeEnvironmentTransitionVersion = resolveCodeEnvironmentTransitionVersion(appConfig);
const endpointsDropParamsMap = getEndpointsDropParamsMap(appConfig?.endpoints);
const balanceConfig = getBalanceConfig(appConfig);
const cloudFront = buildCloudFrontStartupConfig();
const langfuseFanoutEnabled = isLangfuseFanoutEnabled();
const langfuseConnectionAvailable = isLangfuseConnectionAvailable();
let langfuseConnectionAccess = false;
if (langfuseConnectionAvailable) {
try {
const userId = req.user.id ?? req.user._id?.toString();
if (userId) {
const capabilityUser = {
id: userId,
role: req.user.role ?? '',
tenantId: req.user.tenantId,
idOnTheSource: req.user.idOnTheSource ?? null,
};
const hasAdminAccess = await hasCapability(
capabilityUser,
SystemCapabilities.ACCESS_ADMIN,
);
if (hasAdminAccess) {
langfuseConnectionAccess = await hasConfigCapability(capabilityUser, 'langfuse');
}
}
} catch (err) {
logger.warn(`[config] Langfuse capability check failed: ${err.message}`);
}
}
/** @type {TStartupConfig} */
const payload = {
...preLoginPayload,
...publicSharePayload,
...buildPostLoginPayload(appConfig),
...getConversationTitleCapabilities(appConfig?.interfaceConfig),
conversationListLimits,
sharedLinksSnapshotFilesEnabled: sharedLinksEnabled && isFileSnapshotEnabled(appConfig),
socialLogins: appConfig?.registration?.socialLogins ?? defaultSocialLogins,
projects: appConfig?.projects,
interface: appConfig?.interfaceConfig,
titleGenerationTiming: resolveTitleTiming({
appConfig,
endpoint: EModelEndpoint.agents,
}),
turnstile: appConfig?.turnstileConfig,
modelSpecs: sanitizeModelSpecs(excludeHiddenModelSpecs(appConfig?.modelSpecs)),
balance: balanceConfig,
bundlerURL: process.env.SANDPACK_BUNDLER_URL,
staticBundlerURL: process.env.SANDPACK_STATIC_BUNDLER_URL,
sharePointFilePickerEnabled,
sharePointBaseUrl: process.env.SHAREPOINT_BASE_URL,
sharePointPickerGraphScope: process.env.SHAREPOINT_PICKER_GRAPH_SCOPE,
sharePointPickerSharePointScope: process.env.SHAREPOINT_PICKER_SHAREPOINT_SCOPE,
conversationImportMaxFileSize: process.env.CONVERSATION_IMPORT_MAX_FILE_SIZE_BYTES
? parseInt(process.env.CONVERSATION_IMPORT_MAX_FILE_SIZE_BYTES, 10)
: 0,
langfuseFanoutEnabled,
langfuseConnectionAccess,
insightsEnabled: isEnabled(process.env.ENABLE_INSIGHTS),
/** Lets the client skip the pull request lookup entirely when the feature is off. */
...resolvePullRequestCapabilities(appConfig),
compactionEnabled: appConfig?.summarization?.enabled !== false,
...(codeEnvironmentDecisionVersion != null ? { codeEnvironmentDecisionVersion } : {}),
mcpApps: resolveMCPAppsPolicy(
appConfig?.mcpSettings?.apps,
appConfig?.mcpAppSandbox ?? DEFAULT_MCP_APP_CSP_LIMITS,
appConfig?.mcpAppSandbox?.maxPersistedAppBytes,
appConfig?.mcpAppSandbox?.maxAdmissionRequestsPerMinute,
appConfig?.mcpAppSandbox?.url,
appConfig?.mcpAppSandbox?.maxActiveViews,
appConfig?.mcpAppSandbox?.maxActionPreviewChars,
appConfig?.mcpAppSandbox?.operationLimits,
),
...codeEnvironmentMoveCapabilities,
...resolveCodeWorkspaceInheritanceCapability(process.env.CODE_ENVIRONMENT_DECISION_VERSION),
...(codeEnvironmentTransitionVersion != null ? { codeEnvironmentTransitionVersion } : {}),
...(cloudFront ? { cloudFront } : {}),
...(rum ? { rum } : {}),
fileUploadSseEnabled: isEnabled(process.env.FILE_UPLOAD_SSE_ENABLED),
endpointsDropParamsMap: endpointsDropParamsMap,
};
const webSearch = buildWebSearchConfig(appConfig);
if (webSearch) {
payload.webSearch = webSearch;
}
const buildInfo = buildBuildInfoPayload(appConfig?.interfaceConfig);
if (buildInfo) {
payload.buildInfo = buildInfo;
}
const adminPanelURL = process.env.ADMIN_PANEL_URL;
if (adminPanelURL || !payload.allowAccountDeletion) {
try {
const userId = req.user.id ?? req.user._id?.toString();
if (userId) {
const hasAdminAccess = await hasCapability(
{ id: userId, role: req.user.role ?? '', tenantId: req.user.tenantId },
SystemCapabilities.ACCESS_ADMIN,
);
if (hasAdminAccess && adminPanelURL) {
payload.adminPanelURL = adminPanelURL;
}
if (hasAdminAccess && !payload.allowAccountDeletion) {
payload.allowAccountDeletion = true;
}
}
} catch (err) {
logger.warn(`[config] ACCESS_ADMIN capability check failed: ${err.message}`);
}
}
return res.status(200).send(payload);
} catch (err) {
logger.error('Error in startup config', err);
return res.status(500).send({ error: err.message });
}
});
module.exports = router;