@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.es.js CHANGED
@@ -1773,6 +1773,9 @@ const conversationPreset = {
1773
1773
  thinkingBudget: {
1774
1774
  type: Number,
1775
1775
  },
1776
+ thinkingLevel: {
1777
+ type: String,
1778
+ },
1776
1779
  effort: {
1777
1780
  type: String,
1778
1781
  },
@@ -2510,6 +2513,7 @@ const transactionSchema = new Schema({
2510
2513
  inputTokens: { type: Number },
2511
2514
  writeTokens: { type: Number },
2512
2515
  readTokens: { type: Number },
2516
+ messageId: { type: String },
2513
2517
  }, {
2514
2518
  timestamps: true,
2515
2519
  });
@@ -2737,6 +2741,7 @@ groupSchema.index({ idOnTheSource: 1, source: 1 }, {
2737
2741
  });
2738
2742
  groupSchema.index({ memberIds: 1 });
2739
2743
 
2744
+ const CANCEL_RATE = 1.15;
2740
2745
  /**
2741
2746
  * Checks if the connected MongoDB deployment supports transactions
2742
2747
  * This requires a MongoDB replica set configuration
@@ -7010,6 +7015,77 @@ function createShareMethods(mongoose) {
7010
7015
  };
7011
7016
  }
7012
7017
 
7018
+ function createTransactionMethods(mongoose) {
7019
+ async function updateBalance({ user, incrementValue, setValues }) {
7020
+ var _a;
7021
+ const maxRetries = 10;
7022
+ let delay = 50;
7023
+ let lastError = null;
7024
+ const Balance = mongoose.models.Balance;
7025
+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
7026
+ try {
7027
+ const currentBalanceDoc = await Balance.findOne({ user }).lean();
7028
+ const currentCredits = (_a = currentBalanceDoc === null || currentBalanceDoc === void 0 ? void 0 : currentBalanceDoc.tokenCredits) !== null && _a !== void 0 ? _a : 0;
7029
+ const newCredits = Math.max(0, currentCredits + incrementValue);
7030
+ const updatePayload = {
7031
+ $set: {
7032
+ tokenCredits: newCredits,
7033
+ ...(setValues !== null && setValues !== void 0 ? setValues : {}),
7034
+ },
7035
+ };
7036
+ if (currentBalanceDoc) {
7037
+ const updatedBalance = await Balance.findOneAndUpdate({ user, tokenCredits: currentCredits }, updatePayload, { new: true }).lean();
7038
+ if (updatedBalance) {
7039
+ return updatedBalance;
7040
+ }
7041
+ lastError = new Error(`Concurrency conflict for user ${user} on attempt ${attempt}.`);
7042
+ }
7043
+ else {
7044
+ try {
7045
+ const updatedBalance = await Balance.findOneAndUpdate({ user }, updatePayload, {
7046
+ upsert: true,
7047
+ new: true,
7048
+ }).lean();
7049
+ if (updatedBalance) {
7050
+ return updatedBalance;
7051
+ }
7052
+ lastError = new Error(`Upsert race condition suspected for user ${user} on attempt ${attempt}.`);
7053
+ }
7054
+ catch (error) {
7055
+ if (error instanceof Error &&
7056
+ 'code' in error &&
7057
+ error.code === 11000) {
7058
+ lastError = error;
7059
+ }
7060
+ else {
7061
+ throw error;
7062
+ }
7063
+ }
7064
+ }
7065
+ }
7066
+ catch (error) {
7067
+ logger$1.error(`[updateBalance] Error during attempt ${attempt} for user ${user}:`, error);
7068
+ lastError = error instanceof Error ? error : new Error(String(error));
7069
+ }
7070
+ if (attempt < maxRetries) {
7071
+ const jitter = Math.random() * delay * 0.5;
7072
+ await new Promise((resolve) => setTimeout(resolve, delay + jitter));
7073
+ delay = Math.min(delay * 2, 2000);
7074
+ }
7075
+ }
7076
+ logger$1.error(`[updateBalance] Failed to update balance for user ${user} after ${maxRetries} attempts.`);
7077
+ throw (lastError !== null && lastError !== void 0 ? lastError : new Error(`Failed to update balance for user ${user} after maximum retries due to persistent conflicts.`));
7078
+ }
7079
+ /** Bypasses document middleware; all computed fields must be pre-calculated before calling. */
7080
+ async function bulkInsertTransactions(docs) {
7081
+ const Transaction = mongoose.models.Transaction;
7082
+ if (docs.length) {
7083
+ await Transaction.insertMany(docs);
7084
+ }
7085
+ }
7086
+ return { updateBalance, bulkInsertTransactions };
7087
+ }
7088
+
7013
7089
  /**
7014
7090
  * Creates all database methods for all collections
7015
7091
  * @param mongoose - Mongoose instance
@@ -7031,8 +7107,9 @@ function createMethods(mongoose) {
7031
7107
  ...createAclEntryMethods(mongoose),
7032
7108
  ...createShareMethods(mongoose),
7033
7109
  ...createPluginAuthMethods(mongoose),
7110
+ ...createTransactionMethods(mongoose),
7034
7111
  };
7035
7112
  }
7036
7113
 
7037
- export { AppService, DEFAULT_REFRESH_TOKEN_EXPIRY, DEFAULT_SESSION_EXPIRY, RoleBits, Action as actionSchema, agentApiKeySchema, agentCategorySchema, agentSchema, agentsConfigSetup, assistantSchema, balanceSchema, bannerSchema, categoriesSchema, conversationTag as conversationTagSchema, convoSchema, createMethods, createModels, decrypt, decryptV2, decryptV3, defaultVertexModels, encrypt, encryptV2, encryptV3, file as fileSchema, getRandomValues, getTransactionSupport, getWebSearchKeys, groupSchema, hashBackupCode, hashToken, keySchema, loadDefaultInterface, loadTurnstileConfig, loadWebSearchConfig, logger$1 as logger, logger as meiliLogger, MemoryEntrySchema as memorySchema, messageSchema, pluginAuthSchema, presetSchema, processModelSpecs, projectSchema, promptGroupSchema, promptSchema, roleSchema, sessionSchema, shareSchema, signPayload, supportsTransactions, tokenSchema, toolCallSchema, transactionSchema, userSchema, validateVertexConfig, vertexConfigSetup, webSearchAuth, webSearchKeys };
7114
+ export { AppService, CANCEL_RATE, DEFAULT_REFRESH_TOKEN_EXPIRY, DEFAULT_SESSION_EXPIRY, RoleBits, Action as actionSchema, agentApiKeySchema, agentCategorySchema, agentSchema, agentsConfigSetup, assistantSchema, balanceSchema, bannerSchema, categoriesSchema, conversationTag as conversationTagSchema, convoSchema, createMethods, createModels, decrypt, decryptV2, decryptV3, defaultVertexModels, encrypt, encryptV2, encryptV3, file as fileSchema, getRandomValues, getTransactionSupport, getWebSearchKeys, groupSchema, hashBackupCode, hashToken, keySchema, loadDefaultInterface, loadTurnstileConfig, loadWebSearchConfig, logger$1 as logger, logger as meiliLogger, MemoryEntrySchema as memorySchema, messageSchema, pluginAuthSchema, presetSchema, processModelSpecs, projectSchema, promptGroupSchema, promptSchema, roleSchema, sessionSchema, shareSchema, signPayload, supportsTransactions, tokenSchema, toolCallSchema, transactionSchema, userSchema, validateVertexConfig, vertexConfigSetup, webSearchAuth, webSearchKeys };
7038
7115
  //# sourceMappingURL=index.es.js.map