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

* feat: use openrouter data for model token cost/context * chore: add ttl for tokenConfig and refetch models if cache expired
44 lines
1 KiB
JavaScript
44 lines
1 KiB
JavaScript
const mongoose = require('mongoose');
|
|
const balanceSchema = require('./schema/balance');
|
|
const { getMultiplier } = require('./tx');
|
|
const { logger } = require('~/config');
|
|
|
|
balanceSchema.statics.check = async function ({
|
|
user,
|
|
model,
|
|
endpoint,
|
|
valueKey,
|
|
tokenType,
|
|
amount,
|
|
endpointTokenConfig,
|
|
}) {
|
|
const multiplier = getMultiplier({ valueKey, tokenType, model, endpoint, endpointTokenConfig });
|
|
const tokenCost = amount * multiplier;
|
|
const { tokenCredits: balance } = (await this.findOne({ user }, 'tokenCredits').lean()) ?? {};
|
|
|
|
logger.debug('[Balance.check]', {
|
|
user,
|
|
model,
|
|
endpoint,
|
|
valueKey,
|
|
tokenType,
|
|
amount,
|
|
balance,
|
|
multiplier,
|
|
endpointTokenConfig: !!endpointTokenConfig,
|
|
});
|
|
|
|
if (!balance) {
|
|
return {
|
|
canSpend: false,
|
|
balance: 0,
|
|
tokenCost,
|
|
};
|
|
}
|
|
|
|
logger.debug('[Balance.check]', { tokenCost });
|
|
|
|
return { canSpend: balance >= tokenCost, balance, tokenCost };
|
|
};
|
|
|
|
module.exports = mongoose.model('Balance', balanceSchema);
|