2025-07-11 13:29:51 -04:00
|
|
|
const { handleError } = require('@librechat/api');
|
2024-03-06 00:04:52 -05:00
|
|
|
const { ViolationTypes } = require('librechat-data-provider');
|
|
|
|
const { getModelsConfig } = require('~/server/controllers/ModelController');
|
|
|
|
const { logViolation } = require('~/cache');
|
2024-02-19 22:47:39 -05:00
|
|
|
/**
|
|
|
|
* Validates the model of the request.
|
|
|
|
*
|
|
|
|
* @async
|
2025-08-26 12:10:18 -04:00
|
|
|
* @param {ServerRequest} req - The Express request object.
|
2024-02-19 22:47:39 -05:00
|
|
|
* @param {Express.Response} res - The Express response object.
|
|
|
|
* @param {Function} next - The Express next function.
|
|
|
|
*/
|
|
|
|
const validateModel = async (req, res, next) => {
|
|
|
|
const { model, endpoint } = req.body;
|
|
|
|
if (!model) {
|
|
|
|
return handleError(res, { text: 'Model not provided' });
|
|
|
|
}
|
|
|
|
|
2024-03-06 00:04:52 -05:00
|
|
|
const modelsConfig = await getModelsConfig(req);
|
2024-02-20 12:57:58 -05:00
|
|
|
|
2024-02-19 22:47:39 -05:00
|
|
|
if (!modelsConfig) {
|
|
|
|
return handleError(res, { text: 'Models not loaded' });
|
|
|
|
}
|
|
|
|
|
|
|
|
const availableModels = modelsConfig[endpoint];
|
|
|
|
if (!availableModels) {
|
|
|
|
return handleError(res, { text: 'Endpoint models not loaded' });
|
|
|
|
}
|
|
|
|
|
|
|
|
let validModel = !!availableModels.find((availableModel) => availableModel === model);
|
|
|
|
|
|
|
|
if (validModel) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2025-08-11 14:26:28 -04:00
|
|
|
const { ILLEGAL_MODEL_REQ_SCORE: score = 1 } = process.env ?? {};
|
2024-02-19 22:47:39 -05:00
|
|
|
|
|
|
|
const type = ViolationTypes.ILLEGAL_MODEL_REQUEST;
|
|
|
|
const errorMessage = {
|
|
|
|
type,
|
|
|
|
};
|
|
|
|
|
|
|
|
await logViolation(req, res, type, errorMessage, score);
|
|
|
|
return handleError(res, { text: 'Illegal model request' });
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = validateModel;
|