@librechat/data-schemas 0.0.36 → 0.0.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +78 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.es.js +78 -1
- package/dist/index.es.js.map +1 -1
- package/dist/types/methods/index.d.ts +3 -2
- package/dist/types/methods/transaction.d.ts +46 -0
- package/dist/types/schema/defaults.d.ts +3 -0
- package/dist/types/schema/transaction.d.ts +1 -0
- package/dist/types/types/index.d.ts +1 -0
- package/dist/types/types/transaction.d.ts +17 -0
- package/dist/types/utils/transactions.d.ts +1 -0
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1775,6 +1775,9 @@ const conversationPreset = {
|
|
|
1775
1775
|
thinkingBudget: {
|
|
1776
1776
|
type: Number,
|
|
1777
1777
|
},
|
|
1778
|
+
thinkingLevel: {
|
|
1779
|
+
type: String,
|
|
1780
|
+
},
|
|
1778
1781
|
effort: {
|
|
1779
1782
|
type: String,
|
|
1780
1783
|
},
|
|
@@ -2512,6 +2515,7 @@ const transactionSchema = new mongoose.Schema({
|
|
|
2512
2515
|
inputTokens: { type: Number },
|
|
2513
2516
|
writeTokens: { type: Number },
|
|
2514
2517
|
readTokens: { type: Number },
|
|
2518
|
+
messageId: { type: String },
|
|
2515
2519
|
}, {
|
|
2516
2520
|
timestamps: true,
|
|
2517
2521
|
});
|
|
@@ -2739,6 +2743,7 @@ groupSchema.index({ idOnTheSource: 1, source: 1 }, {
|
|
|
2739
2743
|
});
|
|
2740
2744
|
groupSchema.index({ memberIds: 1 });
|
|
2741
2745
|
|
|
2746
|
+
const CANCEL_RATE = 1.15;
|
|
2742
2747
|
/**
|
|
2743
2748
|
* Checks if the connected MongoDB deployment supports transactions
|
|
2744
2749
|
* This requires a MongoDB replica set configuration
|
|
@@ -7012,6 +7017,77 @@ function createShareMethods(mongoose) {
|
|
|
7012
7017
|
};
|
|
7013
7018
|
}
|
|
7014
7019
|
|
|
7020
|
+
function createTransactionMethods(mongoose) {
|
|
7021
|
+
async function updateBalance({ user, incrementValue, setValues }) {
|
|
7022
|
+
var _a;
|
|
7023
|
+
const maxRetries = 10;
|
|
7024
|
+
let delay = 50;
|
|
7025
|
+
let lastError = null;
|
|
7026
|
+
const Balance = mongoose.models.Balance;
|
|
7027
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
7028
|
+
try {
|
|
7029
|
+
const currentBalanceDoc = await Balance.findOne({ user }).lean();
|
|
7030
|
+
const currentCredits = (_a = currentBalanceDoc === null || currentBalanceDoc === void 0 ? void 0 : currentBalanceDoc.tokenCredits) !== null && _a !== void 0 ? _a : 0;
|
|
7031
|
+
const newCredits = Math.max(0, currentCredits + incrementValue);
|
|
7032
|
+
const updatePayload = {
|
|
7033
|
+
$set: {
|
|
7034
|
+
tokenCredits: newCredits,
|
|
7035
|
+
...(setValues !== null && setValues !== void 0 ? setValues : {}),
|
|
7036
|
+
},
|
|
7037
|
+
};
|
|
7038
|
+
if (currentBalanceDoc) {
|
|
7039
|
+
const updatedBalance = await Balance.findOneAndUpdate({ user, tokenCredits: currentCredits }, updatePayload, { new: true }).lean();
|
|
7040
|
+
if (updatedBalance) {
|
|
7041
|
+
return updatedBalance;
|
|
7042
|
+
}
|
|
7043
|
+
lastError = new Error(`Concurrency conflict for user ${user} on attempt ${attempt}.`);
|
|
7044
|
+
}
|
|
7045
|
+
else {
|
|
7046
|
+
try {
|
|
7047
|
+
const updatedBalance = await Balance.findOneAndUpdate({ user }, updatePayload, {
|
|
7048
|
+
upsert: true,
|
|
7049
|
+
new: true,
|
|
7050
|
+
}).lean();
|
|
7051
|
+
if (updatedBalance) {
|
|
7052
|
+
return updatedBalance;
|
|
7053
|
+
}
|
|
7054
|
+
lastError = new Error(`Upsert race condition suspected for user ${user} on attempt ${attempt}.`);
|
|
7055
|
+
}
|
|
7056
|
+
catch (error) {
|
|
7057
|
+
if (error instanceof Error &&
|
|
7058
|
+
'code' in error &&
|
|
7059
|
+
error.code === 11000) {
|
|
7060
|
+
lastError = error;
|
|
7061
|
+
}
|
|
7062
|
+
else {
|
|
7063
|
+
throw error;
|
|
7064
|
+
}
|
|
7065
|
+
}
|
|
7066
|
+
}
|
|
7067
|
+
}
|
|
7068
|
+
catch (error) {
|
|
7069
|
+
logger$1.error(`[updateBalance] Error during attempt ${attempt} for user ${user}:`, error);
|
|
7070
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
7071
|
+
}
|
|
7072
|
+
if (attempt < maxRetries) {
|
|
7073
|
+
const jitter = Math.random() * delay * 0.5;
|
|
7074
|
+
await new Promise((resolve) => setTimeout(resolve, delay + jitter));
|
|
7075
|
+
delay = Math.min(delay * 2, 2000);
|
|
7076
|
+
}
|
|
7077
|
+
}
|
|
7078
|
+
logger$1.error(`[updateBalance] Failed to update balance for user ${user} after ${maxRetries} attempts.`);
|
|
7079
|
+
throw (lastError !== null && lastError !== void 0 ? lastError : new Error(`Failed to update balance for user ${user} after maximum retries due to persistent conflicts.`));
|
|
7080
|
+
}
|
|
7081
|
+
/** Bypasses document middleware; all computed fields must be pre-calculated before calling. */
|
|
7082
|
+
async function bulkInsertTransactions(docs) {
|
|
7083
|
+
const Transaction = mongoose.models.Transaction;
|
|
7084
|
+
if (docs.length) {
|
|
7085
|
+
await Transaction.insertMany(docs);
|
|
7086
|
+
}
|
|
7087
|
+
}
|
|
7088
|
+
return { updateBalance, bulkInsertTransactions };
|
|
7089
|
+
}
|
|
7090
|
+
|
|
7015
7091
|
/**
|
|
7016
7092
|
* Creates all database methods for all collections
|
|
7017
7093
|
* @param mongoose - Mongoose instance
|
|
@@ -7033,10 +7109,12 @@ function createMethods(mongoose) {
|
|
|
7033
7109
|
...createAclEntryMethods(mongoose),
|
|
7034
7110
|
...createShareMethods(mongoose),
|
|
7035
7111
|
...createPluginAuthMethods(mongoose),
|
|
7112
|
+
...createTransactionMethods(mongoose),
|
|
7036
7113
|
};
|
|
7037
7114
|
}
|
|
7038
7115
|
|
|
7039
7116
|
exports.AppService = AppService;
|
|
7117
|
+
exports.CANCEL_RATE = CANCEL_RATE;
|
|
7040
7118
|
exports.DEFAULT_REFRESH_TOKEN_EXPIRY = DEFAULT_REFRESH_TOKEN_EXPIRY;
|
|
7041
7119
|
exports.DEFAULT_SESSION_EXPIRY = DEFAULT_SESSION_EXPIRY;
|
|
7042
7120
|
exports.actionSchema = Action;
|