mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-21 21:50:49 +02:00

- Introduced new violation scores for TTS, STT, Fork, Import, and File Upload actions in the .env.example file. - Updated logViolation function to accept a score parameter, allowing for dynamic severity levels based on the action type. - Modified limiters for Fork, Import, Message, STT, TTS, Tool Call, and File Upload to utilize the new violation scores when logging violations.
44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
const rateLimit = require('express-rate-limit');
|
|
const { RedisStore } = require('rate-limit-redis');
|
|
const { ViolationTypes } = require('librechat-data-provider');
|
|
const ioredisClient = require('~/cache/ioredisClient');
|
|
const logViolation = require('~/cache/logViolation');
|
|
const { isEnabled } = require('~/server/utils');
|
|
const { logger } = require('~/config');
|
|
|
|
const { TOOL_CALL_VIOLATION_SCORE: score } = process.env;
|
|
|
|
const handler = async (req, res) => {
|
|
const type = ViolationTypes.TOOL_CALL_LIMIT;
|
|
const errorMessage = {
|
|
type,
|
|
max: 1,
|
|
limiter: 'user',
|
|
windowInMinutes: 1,
|
|
};
|
|
|
|
await logViolation(req, res, type, errorMessage, score);
|
|
res.status(429).json({ message: 'Too many tool call requests. Try again later' });
|
|
};
|
|
|
|
const limiterOptions = {
|
|
windowMs: 1000,
|
|
max: 1,
|
|
handler,
|
|
keyGenerator: function (req) {
|
|
return req.user?.id;
|
|
},
|
|
};
|
|
|
|
if (isEnabled(process.env.USE_REDIS) && ioredisClient) {
|
|
logger.debug('Using Redis for tool call rate limiter.');
|
|
const store = new RedisStore({
|
|
sendCommand: (...args) => ioredisClient.call(...args),
|
|
prefix: 'tool_call_limiter:',
|
|
});
|
|
limiterOptions.store = store;
|
|
}
|
|
|
|
const toolCallLimiter = rateLimit(limiterOptions);
|
|
|
|
module.exports = toolCallLimiter;
|