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

* feat: add GOOGLE_MODELS env var * feat: add gemini vision support * refactor(GoogleClient): adjust clientOptions handling depending on model * fix(logger): fix redact logic and redact errors only * fix(GoogleClient): do not allow non-multiModal messages when gemini-pro-vision is selected * refactor(OpenAIClient): use `isVisionModel` client property to avoid calling validateVisionModel multiple times * refactor: better debug logging by correctly traversing, redacting sensitive info, and logging condensed versions of long values * refactor(GoogleClient): allow response errors to be thrown/caught above client handling so user receives meaningful error message debug orderedMessages, parentMessageId, and buildMessages result * refactor(AskController): use model from client.modelOptions.model when saving intermediate messages, which requires for the progress callback to be initialized after the client is initialized * feat(useSSE): revert to previous model if the model was auto-switched by backend due to message attachments * docs: update with google updates, notes about Gemini Pro Vision * fix: redis should not be initialized without USE_REDIS and increase max listeners to 20
45 lines
1.5 KiB
JavaScript
45 lines
1.5 KiB
JavaScript
const mongoose = require('mongoose');
|
|
const { isEnabled } = require('../server/utils/handleText');
|
|
const transactionSchema = require('./schema/transaction');
|
|
const { getMultiplier } = require('./tx');
|
|
const Balance = require('./Balance');
|
|
const cancelRate = 1.15;
|
|
|
|
// Method to calculate and set the tokenValue for a transaction
|
|
transactionSchema.methods.calculateTokenValue = function () {
|
|
if (!this.valueKey || !this.tokenType) {
|
|
this.tokenValue = this.rawAmount;
|
|
}
|
|
const { valueKey, tokenType, model } = this;
|
|
const multiplier = getMultiplier({ valueKey, tokenType, model });
|
|
this.rate = multiplier;
|
|
this.tokenValue = this.rawAmount * multiplier;
|
|
if (this.context && this.tokenType === 'completion' && this.context === 'incomplete') {
|
|
this.tokenValue = Math.ceil(this.tokenValue * cancelRate);
|
|
this.rate *= cancelRate;
|
|
}
|
|
};
|
|
|
|
// Static method to create a transaction and update the balance
|
|
transactionSchema.statics.create = async function (transactionData) {
|
|
const Transaction = this;
|
|
|
|
const transaction = new Transaction(transactionData);
|
|
transaction.calculateTokenValue();
|
|
|
|
// Save the transaction
|
|
await transaction.save();
|
|
|
|
if (!isEnabled(process.env.CHECK_BALANCE)) {
|
|
return;
|
|
}
|
|
|
|
// Adjust the user's balance
|
|
return await Balance.findOneAndUpdate(
|
|
{ user: transaction.user },
|
|
{ $inc: { tokenCredits: transaction.tokenValue } },
|
|
{ upsert: true, new: true },
|
|
).lean();
|
|
};
|
|
|
|
module.exports = mongoose.model('Transaction', transactionSchema);
|