mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 08:12:00 +02:00

* chore: add assistants to supportsBalanceCheck * feat(Transaction): getTransactions and refactor export of model * refactor: use enum: ViolationTypes.TOKEN_BALANCE * feat(assistants): check balance * refactor(assistants): only add promptBuffer if new convo (for title), and remove endpoint definition * refactor(assistants): Count tokens up to the current context window * fix(Switcher): make Select list explicitly controlled * feat(assistants): use assistant's default model when no model is specified instead of the last selected assistant, prevent assistant_id from being recorded in non-assistant endpoints * chore(assistants/chat): import order * chore: bump librechat-data-provider due to changes
45 lines
1.7 KiB
JavaScript
45 lines
1.7 KiB
JavaScript
const { ViolationTypes } = require('librechat-data-provider');
|
|
const { logViolation } = require('~/cache');
|
|
const Balance = require('./Balance');
|
|
/**
|
|
* Checks the balance for a user and determines if they can spend a certain amount.
|
|
* If the user cannot spend the amount, it logs a violation and denies the request.
|
|
*
|
|
* @async
|
|
* @function
|
|
* @param {Object} params - The function parameters.
|
|
* @param {Express.Request} params.req - The Express request object.
|
|
* @param {Express.Response} params.res - The Express response object.
|
|
* @param {Object} params.txData - The transaction data.
|
|
* @param {string} params.txData.user - The user ID or identifier.
|
|
* @param {('prompt' | 'completion')} params.txData.tokenType - The type of token.
|
|
* @param {number} params.txData.amount - The amount of tokens.
|
|
* @param {string} params.txData.model - The model name or identifier.
|
|
* @param {string} [params.txData.endpointTokenConfig] - The token configuration for the endpoint.
|
|
* @returns {Promise<boolean>} Returns true if the user can spend the amount, otherwise denies the request.
|
|
* @throws {Error} Throws an error if there's an issue with the balance check.
|
|
*/
|
|
const checkBalance = async ({ req, res, txData }) => {
|
|
const { canSpend, balance, tokenCost } = await Balance.check(txData);
|
|
|
|
if (canSpend) {
|
|
return true;
|
|
}
|
|
|
|
const type = ViolationTypes.TOKEN_BALANCE;
|
|
const errorMessage = {
|
|
type,
|
|
balance,
|
|
tokenCost,
|
|
promptTokens: txData.amount,
|
|
};
|
|
|
|
if (txData.generations && txData.generations.length > 0) {
|
|
errorMessage.generations = txData.generations;
|
|
}
|
|
|
|
await logViolation(req, res, type, errorMessage, 0);
|
|
throw new Error(JSON.stringify(errorMessage));
|
|
};
|
|
|
|
module.exports = checkBalance;
|