mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-02-15 15:08:10 +01:00
* 🔧 refactor: Simplify payload parsing and enhance getSaveOptions logic - Removed unused bedrockInputSchema from payloadParser, streamlining the function. - Updated payloadParser to handle optional chaining for model parameters. - Enhanced getSaveOptions to ensure runOptions defaults to an empty object if parsing fails, improving robustness. - Adjusted the assignment of maxContextTokens to use the instance variable for consistency. * 🔧 fix: Update maxContextTokens assignment logic in initializeAgent function - Enhanced the maxContextTokens assignment to allow for user-defined values, ensuring it defaults to a calculated value only when not provided or invalid. This change improves flexibility in agent initialization. * 🧪 test: Add unit tests for initializeAgent function - Introduced comprehensive unit tests for the initializeAgent function, focusing on maxContextTokens behavior. - Tests cover scenarios for user-defined values, fallback calculations, and edge cases such as zero and negative values, enhancing overall test coverage and reliability of agent initialization logic. * refactor: default params Endpoint Configuration Handling - Integrated `getEndpointsConfig` to fetch endpoint configurations, allowing for dynamic handling of `defaultParamsEndpoint`. - Updated `buildEndpointOption` to pass `defaultParamsEndpoint` to `parseCompactConvo`, ensuring correct parameter handling based on endpoint type. - Added comprehensive unit tests for `buildDefaultConvo` and `cleanupPreset` to validate behavior with `defaultParamsEndpoint`, covering various scenarios and edge cases. - Refactored related hooks and utility functions to support the new configuration structure, improving overall flexibility and maintainability. * refactor: Centralize defaultParamsEndpoint retrieval - Introduced `getDefaultParamsEndpoint` function to streamline the retrieval of `defaultParamsEndpoint` across various hooks and middleware. - Updated multiple files to utilize the new function, enhancing code consistency and maintainability. - Removed redundant logic for fetching `defaultParamsEndpoint`, simplifying the codebase.
117 lines
3.9 KiB
JavaScript
117 lines
3.9 KiB
JavaScript
const { handleError } = require('@librechat/api');
|
|
const { logger } = require('@librechat/data-schemas');
|
|
const {
|
|
EndpointURLs,
|
|
EModelEndpoint,
|
|
isAgentsEndpoint,
|
|
parseCompactConvo,
|
|
getDefaultParamsEndpoint,
|
|
} = require('librechat-data-provider');
|
|
const azureAssistants = require('~/server/services/Endpoints/azureAssistants');
|
|
const assistants = require('~/server/services/Endpoints/assistants');
|
|
const { getEndpointsConfig } = require('~/server/services/Config');
|
|
const agents = require('~/server/services/Endpoints/agents');
|
|
const { updateFilesUsage } = require('~/models');
|
|
|
|
const buildFunction = {
|
|
[EModelEndpoint.agents]: agents.buildOptions,
|
|
[EModelEndpoint.assistants]: assistants.buildOptions,
|
|
[EModelEndpoint.azureAssistants]: azureAssistants.buildOptions,
|
|
};
|
|
|
|
async function buildEndpointOption(req, res, next) {
|
|
const { endpoint, endpointType } = req.body;
|
|
|
|
let endpointsConfig;
|
|
try {
|
|
endpointsConfig = await getEndpointsConfig(req);
|
|
} catch (error) {
|
|
logger.error('Error fetching endpoints config in buildEndpointOption', error);
|
|
}
|
|
|
|
const defaultParamsEndpoint = getDefaultParamsEndpoint(endpointsConfig, endpoint);
|
|
|
|
let parsedBody;
|
|
try {
|
|
parsedBody = parseCompactConvo({
|
|
endpoint,
|
|
endpointType,
|
|
conversation: req.body,
|
|
defaultParamsEndpoint,
|
|
});
|
|
} catch (error) {
|
|
logger.error(`Error parsing compact conversation for endpoint ${endpoint}`, error);
|
|
logger.debug({
|
|
'Error parsing compact conversation': { endpoint, endpointType, conversation: req.body },
|
|
});
|
|
return handleError(res, { text: 'Error parsing conversation' });
|
|
}
|
|
|
|
const appConfig = req.config;
|
|
if (appConfig.modelSpecs?.list && appConfig.modelSpecs?.enforce) {
|
|
/** @type {{ list: TModelSpec[] }}*/
|
|
const { list } = appConfig.modelSpecs;
|
|
const { spec } = parsedBody;
|
|
|
|
if (!spec) {
|
|
return handleError(res, { text: 'No model spec selected' });
|
|
}
|
|
|
|
const currentModelSpec = list.find((s) => s.name === spec);
|
|
if (!currentModelSpec) {
|
|
return handleError(res, { text: 'Invalid model spec' });
|
|
}
|
|
|
|
if (endpoint !== currentModelSpec.preset.endpoint) {
|
|
return handleError(res, { text: 'Model spec mismatch' });
|
|
}
|
|
|
|
try {
|
|
currentModelSpec.preset.spec = spec;
|
|
parsedBody = parseCompactConvo({
|
|
endpoint,
|
|
endpointType,
|
|
conversation: currentModelSpec.preset,
|
|
defaultParamsEndpoint,
|
|
});
|
|
if (currentModelSpec.iconURL != null && currentModelSpec.iconURL !== '') {
|
|
parsedBody.iconURL = currentModelSpec.iconURL;
|
|
}
|
|
} catch (error) {
|
|
logger.error(`Error parsing model spec for endpoint ${endpoint}`, error);
|
|
return handleError(res, { text: 'Error parsing model spec' });
|
|
}
|
|
} else if (parsedBody.spec && appConfig.modelSpecs?.list) {
|
|
// Non-enforced mode: if spec is selected, derive iconURL from model spec
|
|
const modelSpec = appConfig.modelSpecs.list.find((s) => s.name === parsedBody.spec);
|
|
if (modelSpec?.iconURL) {
|
|
parsedBody.iconURL = modelSpec.iconURL;
|
|
}
|
|
}
|
|
|
|
try {
|
|
const isAgents =
|
|
isAgentsEndpoint(endpoint) || req.baseUrl.startsWith(EndpointURLs[EModelEndpoint.agents]);
|
|
const builder = isAgents
|
|
? (...args) => buildFunction[EModelEndpoint.agents](req, ...args)
|
|
: buildFunction[endpointType ?? endpoint];
|
|
|
|
// TODO: use object params
|
|
req.body = req.body || {}; // Express 5: ensure req.body exists
|
|
req.body.endpointOption = await builder(endpoint, parsedBody, endpointType);
|
|
|
|
if (req.body.files && !isAgents) {
|
|
req.body.endpointOption.attachments = updateFilesUsage(req.body.files);
|
|
}
|
|
|
|
next();
|
|
} catch (error) {
|
|
logger.error(
|
|
`Error building endpoint option for endpoint ${endpoint} with type ${endpointType}`,
|
|
error,
|
|
);
|
|
return handleError(res, { text: 'Error building endpoint option' });
|
|
}
|
|
}
|
|
|
|
module.exports = buildEndpointOption;
|