LibreChat/api/server/services/Config/app.js
Danny Avila f05f50460a
🔭 feat: Admin Config Read APIs (Resolved and Effective Config, Scope Catalog, Env Status) (#17383)
* 🔭 feat: Admin Config Read APIs (Resolved and Effective Config, Scope Catalog, Env Status)

Admin clients could read one config document at a time, or the deployment's merged config,
but nothing showed what a profile or a user resolves to: a client had to re-implement the
merge (Base-first precedence, merge keys, tombstones, the interface permission filter,
base-only fields, runtime stops, secret rebinding, the env allowlist) to preview it, and had
no way to see why part of a profile is ignored. Endpoint, model and tool pickers came from the
end-user routes, called as the signed-in admin, so they showed the admin's own scope and keys
and needed a browser User-Agent.

The admin config API now has four read routes. GET /resolved/:principalType/:principalId and
GET /effective?userId= return librechat.yaml, the documents that merge over it in merge order,
each with the parts the merge does not apply and why, and the result, in librechat.yaml shape.
They come from resolveConfigLayers, which tracks the same fold the served merge runs and is
pinned to it by parity tests, and from the stored documents rather than the override cache.
GET /catalog lists the endpoints (with the reason one is not served), models (served,
configured and defaults), MCP servers and tools a scope serves, resolved for the scope rather
than the caller and never with a user's stored key; GET /env-status reports whether each env
name the config uses is set, never its value. Every read is filtered to the sections the caller
can read, redacted as reads redact, and scoped to the caller's tenant. The admin Langfuse routes
now say why a deployment offers no connection settings. Meta reports resolvedApi, catalogApi,
envStatus and limits.catalogModelsTtlMs.

* 🪞 fix: Report Duplicate Custom Endpoints from the Served Definition

When librechat.yaml defines a custom endpoint name twice, or both `Ollama` and `ollama`, the
catalog kept the first definition for a name's env names, reason and models, while
`loadCustomEndpointsConfig` serves the last servable one. An entry could pair the served config
of one definition with the env names of another.

`resolveServedCustomEndpoints` now holds the runtime rule (the last definition with a name, key,
base URL and models wins under its served name), and `loadCustomEndpointsConfig`,
`loadConfigModels` and the catalog all use it. A name with no servable definition is reported
from its last one.

* ⏱️ feat: Make the Admin Catalog Model-List Lifetime Configurable

`GET /api/admin/config/catalog` reused a scope's model lists for a fixed two minutes. The new
librechat.yaml field `endpoints.catalogModelsTtlMs` sets that lifetime (milliseconds, `0` to one
hour, two minutes by default); `0` resolves the lists on every read. It is a base-only path, so a
profile cannot change it and `GET /meta` reports one value, now read from the deployment's config
on each request as `limits.catalogModelsTtlMs`.
2026-10-24 09:23:22 +02:00

134 lines
4.5 KiB
JavaScript

const mongoose = require('mongoose');
const { CacheKeys } = require('librechat-data-provider');
const { AppService } = require('@librechat/data-schemas');
const {
createAppConfigService,
clearMcpConfigCache,
createCodeEnvironmentRegistry,
createConfigCacheInvalidation,
createRedisConfigInvalidationPubSub,
mergeAccessibleCodeEnvironments,
ioredisClient,
cacheConfig,
standardCache,
} = require('@librechat/api');
const { setCachedTools } = require('./getCachedTools');
const { loadAndFormatTools } = require('~/server/services/start/tools');
const loadCustomConfig = require('./loadCustomConfig');
const getLogStores = require('~/cache/getLogStores');
const paths = require('~/config/paths');
const db = require('~/models');
let codeEnvironmentRegistry;
function getCodeEnvironmentRegistry() {
if (codeEnvironmentRegistry == null) {
codeEnvironmentRegistry = createCodeEnvironmentRegistry(mongoose, {
configurationCache: cacheConfig.USE_REDIS
? standardCache('CODE_ENVIRONMENT_CONFIG')
: undefined,
});
}
return codeEnvironmentRegistry;
}
async function invalidateCodeEnvironmentConfigCache(tenantId) {
await getCodeEnvironmentRegistry().invalidateAccessibleConfigurations(tenantId);
}
const loadBaseConfig = async (mode) => {
/** @type {TCustomConfig} */
const config = (await loadCustomConfig(true, { mode })) ?? {};
/** @type {Record<string, FunctionTool>} */
const systemTools = loadAndFormatTools({
adminFilter: config.filteredTools,
adminIncluded: config.includedTools,
directory: paths.structuredTools,
});
return AppService({ config, paths, systemTools });
};
const appConfigService = createAppConfigService({
loadBaseConfig,
setCachedTools,
getCache: getLogStores,
cacheKeys: CacheKeys,
getApplicableConfigs: db.getApplicableConfigs,
getUserPrincipals: db.getUserPrincipals,
overrideEnvAllowlist: process.env.CONFIG_OVERRIDE_ENV_ALLOWLIST,
augmentConfig: ({ appConfig, baseConfig, principals, options }) => {
if (!options.userId) return appConfig;
return mergeAccessibleCodeEnvironments({
appConfig,
deploymentConfig: baseConfig,
actor: {
userId: options.userId,
role: options.role ?? null,
idOnTheSource: options.idOnTheSource ?? null,
principals,
},
registry: getCodeEnvironmentRegistry(),
});
},
});
const {
getAppConfig,
resolveConfigLayers,
clearAppConfigCache,
clearOverrideCache,
reloadBaseConfig,
overrideCacheTtl,
} = appConfigService;
// Config owns the reader; models never import this module to obtain it.
db.initializeMessageBudget(getAppConfig);
/**
* Admin config writes clear the per-principal override caches, the MCP config-source servers
* and the YAML base config. Base-only sections such as `filters` and `messageFilter` exist only
* in librechat.yaml, and an admin write is how a running server picks up an edit to them: the
* next read reloads the YAML (keeping the last good config if it fails) and republishes the
* tools derived from it. With Redis, the clear is broadcast so every instance reloads its YAML
* and drops its in-process merged config.
*/
const configCacheInvalidation = createConfigCacheInvalidation({
clears: [
{ label: 'clearAppConfigCache', clear: () => clearAppConfigCache() },
{ label: 'clearOverrideCache', clear: clearOverrideCache },
{ label: 'clearMcpConfigCache', clear: () => clearMcpConfigCache() },
],
load: reloadBaseConfig,
pubsub: createRedisConfigInvalidationPubSub({
useRedis: cacheConfig.USE_REDIS,
client: ioredisClient,
namespace: cacheConfig.REDIS_KEY_PREFIX,
}),
});
/**
* Invalidate the config caches an admin config mutation affects, on every instance.
* @param {string} [tenantId] - Optional tenant ID to scope override cache clearing.
*/
function invalidateConfigCaches(tenantId) {
return configCacheInvalidation.invalidate(tenantId);
}
/**
* The same clear for the admin reload route, then librechat.yaml loaded again here and, when it
* loaded, the broadcast; resolves with what each step reached.
* @param {string} [tenantId] - Optional tenant ID to scope override cache clearing.
* @returns {Promise<import('@librechat/api').ConfigReloadReport>}
*/
function reloadConfigCaches(tenantId) {
return configCacheInvalidation.reload(tenantId);
}
module.exports = {
getAppConfig,
resolveConfigLayers,
overrideCacheTtl,
invalidateConfigCaches,
reloadConfigCaches,
getCodeEnvironmentRegistry,
invalidateCodeEnvironmentConfigCache,
};