mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
* feat: add filterFilesByEndpointConfig to filter disabled file processing by provider * chore: explicit define of endpointFileConfig for better debugging * refactor: move `normalizeEndpointName` to data-provider as used app-wide * chore: remove overrideEndpoint from useFileHandling * refactor: improve endpoint file config selection * refactor: update filterFilesByEndpointConfig to accept structured parameters and improve endpoint file config handling * refactor: replace defaultFileConfig with getEndpointFileConfig for improved file configuration handling across components * test: add comprehensive unit tests for getEndpointFileConfig to validate endpoint configuration handling * refactor: streamline agent endpoint assignment and improve file filtering logic * feat: add error handling for disabled file uploads in endpoint configuration * refactor: update encodeAndFormat functions to accept structured parameters for provider and endpoint * refactor: streamline requestFiles handling in initializeAgent function * fix: getEndpointFileConfig partial config merging scenarios * refactor: enhance mergeWithDefault function to support document-supported providers with comprehensive MIME types * refactor: user-configured default file config in getEndpointFileConfig * fix: prevent file handling when endpoint is disabled and file is dragged to chat * refactor: move `getEndpointField` to `data-provider` and update usage across components and hooks * fix: prioritize endpointType based on agent.endpoint in file filtering logic * fix: prioritize agent.endpoint in file filtering logic and remove unnecessary endpointType defaulting
117 lines
3.7 KiB
JavaScript
117 lines
3.7 KiB
JavaScript
const { isUserProvided } = require('@librechat/api');
|
|
const {
|
|
EModelEndpoint,
|
|
extractEnvVariable,
|
|
normalizeEndpointName,
|
|
} = require('librechat-data-provider');
|
|
const { fetchModels } = require('~/server/services/ModelService');
|
|
const { getAppConfig } = require('./app');
|
|
|
|
/**
|
|
* Load config endpoints from the cached configuration object
|
|
* @function loadConfigModels
|
|
* @param {ServerRequest} req - The Express request object.
|
|
*/
|
|
async function loadConfigModels(req) {
|
|
const appConfig = await getAppConfig({ role: req.user?.role });
|
|
if (!appConfig) {
|
|
return {};
|
|
}
|
|
const modelsConfig = {};
|
|
const azureConfig = appConfig.endpoints?.[EModelEndpoint.azureOpenAI];
|
|
const { modelNames } = azureConfig ?? {};
|
|
|
|
if (modelNames && azureConfig) {
|
|
modelsConfig[EModelEndpoint.azureOpenAI] = modelNames;
|
|
}
|
|
|
|
if (modelNames && azureConfig && azureConfig.plugins) {
|
|
modelsConfig[EModelEndpoint.gptPlugins] = modelNames;
|
|
}
|
|
|
|
if (azureConfig?.assistants && azureConfig.assistantModels) {
|
|
modelsConfig[EModelEndpoint.azureAssistants] = azureConfig.assistantModels;
|
|
}
|
|
|
|
if (!Array.isArray(appConfig.endpoints?.[EModelEndpoint.custom])) {
|
|
return modelsConfig;
|
|
}
|
|
|
|
const customEndpoints = appConfig.endpoints[EModelEndpoint.custom].filter(
|
|
(endpoint) =>
|
|
endpoint.baseURL &&
|
|
endpoint.apiKey &&
|
|
endpoint.name &&
|
|
endpoint.models &&
|
|
(endpoint.models.fetch || endpoint.models.default),
|
|
);
|
|
|
|
/**
|
|
* @type {Record<string, Promise<string[]>>}
|
|
* Map for promises keyed by unique combination of baseURL and apiKey */
|
|
const fetchPromisesMap = {};
|
|
/**
|
|
* @type {Record<string, string[]>}
|
|
* Map to associate unique keys with endpoint names; note: one key may can correspond to multiple endpoints */
|
|
const uniqueKeyToEndpointsMap = {};
|
|
/**
|
|
* @type {Record<string, Partial<TEndpoint>>}
|
|
* Map to associate endpoint names to their configurations */
|
|
const endpointsMap = {};
|
|
|
|
for (let i = 0; i < customEndpoints.length; i++) {
|
|
const endpoint = customEndpoints[i];
|
|
const { models, name: configName, baseURL, apiKey, headers: endpointHeaders } = endpoint;
|
|
const name = normalizeEndpointName(configName);
|
|
endpointsMap[name] = endpoint;
|
|
|
|
const API_KEY = extractEnvVariable(apiKey);
|
|
const BASE_URL = extractEnvVariable(baseURL);
|
|
|
|
const uniqueKey = `${BASE_URL}__${API_KEY}`;
|
|
|
|
modelsConfig[name] = [];
|
|
|
|
if (models.fetch && !isUserProvided(API_KEY) && !isUserProvided(BASE_URL)) {
|
|
fetchPromisesMap[uniqueKey] =
|
|
fetchPromisesMap[uniqueKey] ||
|
|
fetchModels({
|
|
name,
|
|
apiKey: API_KEY,
|
|
baseURL: BASE_URL,
|
|
user: req.user.id,
|
|
userObject: req.user,
|
|
headers: endpointHeaders,
|
|
direct: endpoint.directEndpoint,
|
|
userIdQuery: models.userIdQuery,
|
|
});
|
|
uniqueKeyToEndpointsMap[uniqueKey] = uniqueKeyToEndpointsMap[uniqueKey] || [];
|
|
uniqueKeyToEndpointsMap[uniqueKey].push(name);
|
|
continue;
|
|
}
|
|
|
|
if (Array.isArray(models.default)) {
|
|
modelsConfig[name] = models.default.map((model) =>
|
|
typeof model === 'string' ? model : model.name,
|
|
);
|
|
}
|
|
}
|
|
|
|
const fetchedData = await Promise.all(Object.values(fetchPromisesMap));
|
|
const uniqueKeys = Object.keys(fetchPromisesMap);
|
|
|
|
for (let i = 0; i < fetchedData.length; i++) {
|
|
const currentKey = uniqueKeys[i];
|
|
const modelData = fetchedData[i];
|
|
const associatedNames = uniqueKeyToEndpointsMap[currentKey];
|
|
|
|
for (const name of associatedNames) {
|
|
const endpoint = endpointsMap[name];
|
|
modelsConfig[name] = !modelData?.length ? (endpoint.models.default ?? []) : modelData;
|
|
}
|
|
}
|
|
|
|
return modelsConfig;
|
|
}
|
|
|
|
module.exports = loadConfigModels;
|