@librechat/data-schemas 0.0.7 → 0.0.8
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 +1470 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.es.js +1467 -6
- package/dist/index.es.js.map +1 -1
- package/dist/types/config/meiliLogger.d.ts +4 -0
- package/dist/types/config/parsers.d.ts +32 -0
- package/dist/types/config/winston.d.ts +4 -0
- package/dist/types/crypto/index.d.ts +3 -0
- package/dist/types/index.d.ts +7 -46
- package/dist/types/methods/index.d.ts +67 -0
- package/dist/types/methods/role.d.ts +35 -0
- package/dist/types/methods/session.d.ts +48 -0
- package/dist/types/methods/token.d.ts +34 -0
- package/dist/types/methods/user.d.ts +38 -0
- package/dist/types/methods/user.test.d.ts +1 -0
- package/dist/types/models/action.d.ts +30 -0
- package/dist/types/models/agent.d.ts +30 -0
- package/dist/types/models/assistant.d.ts +30 -0
- package/dist/types/models/balance.d.ts +30 -0
- package/dist/types/models/banner.d.ts +30 -0
- package/dist/types/models/conversationTag.d.ts +30 -0
- package/dist/types/models/convo.d.ts +30 -0
- package/dist/types/models/file.d.ts +30 -0
- package/dist/types/models/index.d.ts +53 -0
- package/dist/types/models/key.d.ts +30 -0
- package/dist/types/models/message.d.ts +30 -0
- package/dist/types/models/pluginAuth.d.ts +30 -0
- package/dist/types/models/plugins/mongoMeili.d.ts +52 -0
- package/dist/types/models/preset.d.ts +30 -0
- package/dist/types/models/project.d.ts +30 -0
- package/dist/types/models/prompt.d.ts +30 -0
- package/dist/types/models/promptGroup.d.ts +30 -0
- package/dist/types/models/role.d.ts +30 -0
- package/dist/types/models/session.d.ts +30 -0
- package/dist/types/models/sharedLink.d.ts +30 -0
- package/dist/types/models/token.d.ts +30 -0
- package/dist/types/models/toolCall.d.ts +30 -0
- package/dist/types/models/transaction.d.ts +30 -0
- package/dist/types/models/user.d.ts +30 -0
- package/dist/types/schema/action.d.ts +2 -27
- package/dist/types/schema/agent.d.ts +4 -31
- package/dist/types/schema/assistant.d.ts +4 -16
- package/dist/types/schema/balance.d.ts +4 -12
- package/dist/types/schema/convo.d.ts +2 -49
- package/dist/types/schema/file.d.ts +2 -26
- package/dist/types/schema/index.d.ts +23 -0
- package/dist/types/schema/message.d.ts +2 -36
- package/dist/types/schema/role.d.ts +2 -5
- package/dist/types/schema/session.d.ts +2 -6
- package/dist/types/schema/token.d.ts +2 -11
- package/dist/types/schema/user.d.ts +5 -36
- package/dist/types/types/action.d.ts +52 -0
- package/dist/types/types/agent.d.ts +55 -0
- package/dist/types/types/assistant.d.ts +39 -0
- package/dist/types/types/balance.d.ts +35 -0
- package/dist/types/types/banner.d.ts +34 -0
- package/dist/types/types/convo.d.ts +74 -0
- package/dist/types/types/file.d.ts +51 -0
- package/dist/types/types/index.d.ts +12 -0
- package/dist/types/types/message.d.ts +67 -0
- package/dist/types/types/role.d.ts +30 -0
- package/dist/types/types/session.d.ts +61 -0
- package/dist/types/types/token.d.ts +62 -0
- package/dist/types/types/user.d.ts +91 -0
- package/package.json +12 -5
package/dist/index.es.js
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
|
+
import jwt from 'jsonwebtoken';
|
|
2
|
+
import { webcrypto } from 'node:crypto';
|
|
1
3
|
import mongoose, { Schema } from 'mongoose';
|
|
2
|
-
import { FileSources, Constants, PermissionTypes, Permissions, SystemRoles } from 'librechat-data-provider';
|
|
4
|
+
import { FileSources, Constants, PermissionTypes, Permissions, SystemRoles, roleDefaults } from 'librechat-data-provider';
|
|
5
|
+
import _ from 'lodash';
|
|
6
|
+
import { MeiliSearch } from 'meilisearch';
|
|
7
|
+
import path from 'path';
|
|
8
|
+
import winston from 'winston';
|
|
9
|
+
import 'winston-daily-rotate-file';
|
|
10
|
+
import { klona } from 'klona';
|
|
11
|
+
import traverse from 'traverse';
|
|
12
|
+
|
|
13
|
+
async function signPayload({ payload, secret, expirationTime, }) {
|
|
14
|
+
return jwt.sign(payload, secret, { expiresIn: expirationTime });
|
|
15
|
+
}
|
|
16
|
+
async function hashToken(str) {
|
|
17
|
+
const data = new TextEncoder().encode(str);
|
|
18
|
+
const hashBuffer = await webcrypto.subtle.digest('SHA-256', data);
|
|
19
|
+
return Buffer.from(hashBuffer).toString('hex');
|
|
20
|
+
}
|
|
3
21
|
|
|
4
22
|
// Define the Auth sub-schema with type-safety.
|
|
5
23
|
const AuthSchema = new Schema({
|
|
@@ -131,6 +149,10 @@ const agentSchema = new Schema({
|
|
|
131
149
|
ref: 'Project',
|
|
132
150
|
index: true,
|
|
133
151
|
},
|
|
152
|
+
versions: {
|
|
153
|
+
type: [Schema.Types.Mixed],
|
|
154
|
+
default: [],
|
|
155
|
+
},
|
|
134
156
|
}, {
|
|
135
157
|
timestamps: true,
|
|
136
158
|
});
|
|
@@ -425,9 +447,9 @@ const convoSchema = new Schema({
|
|
|
425
447
|
type: String,
|
|
426
448
|
index: true,
|
|
427
449
|
},
|
|
428
|
-
messages: [{ type:
|
|
450
|
+
messages: [{ type: Schema.Types.ObjectId, ref: 'Message' }],
|
|
429
451
|
agentOptions: {
|
|
430
|
-
type:
|
|
452
|
+
type: Schema.Types.Mixed,
|
|
431
453
|
},
|
|
432
454
|
...conversationPreset,
|
|
433
455
|
agent_id: {
|
|
@@ -617,6 +639,25 @@ const messageSchema = new Schema({
|
|
|
617
639
|
finish_reason: {
|
|
618
640
|
type: String,
|
|
619
641
|
},
|
|
642
|
+
feedback: {
|
|
643
|
+
type: {
|
|
644
|
+
rating: {
|
|
645
|
+
type: String,
|
|
646
|
+
enum: ['thumbsUp', 'thumbsDown'],
|
|
647
|
+
required: true,
|
|
648
|
+
},
|
|
649
|
+
tag: {
|
|
650
|
+
type: mongoose.Schema.Types.Mixed,
|
|
651
|
+
required: false,
|
|
652
|
+
},
|
|
653
|
+
text: {
|
|
654
|
+
type: String,
|
|
655
|
+
required: false,
|
|
656
|
+
},
|
|
657
|
+
},
|
|
658
|
+
default: undefined,
|
|
659
|
+
required: false,
|
|
660
|
+
},
|
|
620
661
|
_meiliIndex: {
|
|
621
662
|
type: Boolean,
|
|
622
663
|
required: false,
|
|
@@ -863,6 +904,9 @@ const rolePermissionsSchema = new Schema({
|
|
|
863
904
|
[PermissionTypes.RUN_CODE]: {
|
|
864
905
|
[Permissions.USE]: { type: Boolean, default: true },
|
|
865
906
|
},
|
|
907
|
+
[PermissionTypes.WEB_SEARCH]: {
|
|
908
|
+
[Permissions.USE]: { type: Boolean, default: true },
|
|
909
|
+
},
|
|
866
910
|
}, { _id: false });
|
|
867
911
|
const roleSchema = new Schema({
|
|
868
912
|
name: { type: String, required: true, unique: true, index: true },
|
|
@@ -883,6 +927,7 @@ const roleSchema = new Schema({
|
|
|
883
927
|
[PermissionTypes.MULTI_CONVO]: { [Permissions.USE]: true },
|
|
884
928
|
[PermissionTypes.TEMPORARY_CHAT]: { [Permissions.USE]: true },
|
|
885
929
|
[PermissionTypes.RUN_CODE]: { [Permissions.USE]: true },
|
|
930
|
+
[PermissionTypes.WEB_SEARCH]: { [Permissions.USE]: true },
|
|
886
931
|
}),
|
|
887
932
|
},
|
|
888
933
|
});
|
|
@@ -1046,7 +1091,7 @@ const BackupCodeSchema = new Schema({
|
|
|
1046
1091
|
used: { type: Boolean, default: false },
|
|
1047
1092
|
usedAt: { type: Date, default: null },
|
|
1048
1093
|
}, { _id: false });
|
|
1049
|
-
const
|
|
1094
|
+
const userSchema = new Schema({
|
|
1050
1095
|
name: {
|
|
1051
1096
|
type: String,
|
|
1052
1097
|
},
|
|
@@ -1057,7 +1102,7 @@ const User = new Schema({
|
|
|
1057
1102
|
},
|
|
1058
1103
|
email: {
|
|
1059
1104
|
type: String,
|
|
1060
|
-
required: [true,
|
|
1105
|
+
required: [true, "can't be blank"],
|
|
1061
1106
|
lowercase: true,
|
|
1062
1107
|
unique: true,
|
|
1063
1108
|
match: [/\S+@\S+\.\S+/, 'is invalid'],
|
|
@@ -1102,6 +1147,11 @@ const User = new Schema({
|
|
|
1102
1147
|
unique: true,
|
|
1103
1148
|
sparse: true,
|
|
1104
1149
|
},
|
|
1150
|
+
samlId: {
|
|
1151
|
+
type: String,
|
|
1152
|
+
unique: true,
|
|
1153
|
+
sparse: true,
|
|
1154
|
+
},
|
|
1105
1155
|
ldapId: {
|
|
1106
1156
|
type: String,
|
|
1107
1157
|
unique: true,
|
|
@@ -1148,5 +1198,1416 @@ const User = new Schema({
|
|
|
1148
1198
|
},
|
|
1149
1199
|
}, { timestamps: true });
|
|
1150
1200
|
|
|
1151
|
-
|
|
1201
|
+
/**
|
|
1202
|
+
* Creates or returns the User model using the provided mongoose instance and schema
|
|
1203
|
+
*/
|
|
1204
|
+
function createUserModel(mongoose) {
|
|
1205
|
+
return mongoose.models.User || mongoose.model('User', userSchema);
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
/**
|
|
1209
|
+
* Creates or returns the Token model using the provided mongoose instance and schema
|
|
1210
|
+
*/
|
|
1211
|
+
function createTokenModel(mongoose) {
|
|
1212
|
+
return mongoose.models.Token || mongoose.model('Token', tokenSchema);
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
/**
|
|
1216
|
+
* Creates or returns the Session model using the provided mongoose instance and schema
|
|
1217
|
+
*/
|
|
1218
|
+
function createSessionModel(mongoose) {
|
|
1219
|
+
return mongoose.models.Session || mongoose.model('Session', sessionSchema);
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
/**
|
|
1223
|
+
* Creates or returns the Balance model using the provided mongoose instance and schema
|
|
1224
|
+
*/
|
|
1225
|
+
function createBalanceModel(mongoose) {
|
|
1226
|
+
return mongoose.models.Balance || mongoose.model('Balance', balanceSchema);
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
const logDir$1 = path.join(__dirname, '..', 'logs');
|
|
1230
|
+
const { NODE_ENV: NODE_ENV$1, DEBUG_LOGGING: DEBUG_LOGGING$1 = 'false' } = process.env;
|
|
1231
|
+
const useDebugLogging$1 = (typeof DEBUG_LOGGING$1 === 'string' && DEBUG_LOGGING$1.toLowerCase() === 'true') ||
|
|
1232
|
+
DEBUG_LOGGING$1 === 'true';
|
|
1233
|
+
const levels$1 = {
|
|
1234
|
+
error: 0,
|
|
1235
|
+
warn: 1,
|
|
1236
|
+
info: 2,
|
|
1237
|
+
http: 3,
|
|
1238
|
+
verbose: 4,
|
|
1239
|
+
debug: 5,
|
|
1240
|
+
activity: 6,
|
|
1241
|
+
silly: 7,
|
|
1242
|
+
};
|
|
1243
|
+
winston.addColors({
|
|
1244
|
+
info: 'green',
|
|
1245
|
+
warn: 'italic yellow',
|
|
1246
|
+
error: 'red',
|
|
1247
|
+
debug: 'blue',
|
|
1248
|
+
});
|
|
1249
|
+
const level$1 = () => {
|
|
1250
|
+
const env = NODE_ENV$1 || 'development';
|
|
1251
|
+
const isDevelopment = env === 'development';
|
|
1252
|
+
return isDevelopment ? 'debug' : 'warn';
|
|
1253
|
+
};
|
|
1254
|
+
const fileFormat$1 = winston.format.combine(winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), winston.format.errors({ stack: true }), winston.format.splat());
|
|
1255
|
+
const logLevel = useDebugLogging$1 ? 'debug' : 'error';
|
|
1256
|
+
const transports$1 = [
|
|
1257
|
+
new winston.transports.DailyRotateFile({
|
|
1258
|
+
level: logLevel,
|
|
1259
|
+
filename: `${logDir$1}/meiliSync-%DATE%.log`,
|
|
1260
|
+
datePattern: 'YYYY-MM-DD',
|
|
1261
|
+
zippedArchive: true,
|
|
1262
|
+
maxSize: '20m',
|
|
1263
|
+
maxFiles: '14d',
|
|
1264
|
+
format: fileFormat$1,
|
|
1265
|
+
}),
|
|
1266
|
+
];
|
|
1267
|
+
const consoleFormat$1 = winston.format.combine(winston.format.colorize({ all: true }), winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), winston.format.printf((info) => `${info.timestamp} ${info.level}: ${info.message}`));
|
|
1268
|
+
transports$1.push(new winston.transports.Console({
|
|
1269
|
+
level: 'info',
|
|
1270
|
+
format: consoleFormat$1,
|
|
1271
|
+
}));
|
|
1272
|
+
const logger$1 = winston.createLogger({
|
|
1273
|
+
level: level$1(),
|
|
1274
|
+
levels: levels$1,
|
|
1275
|
+
transports: transports$1,
|
|
1276
|
+
});
|
|
1277
|
+
|
|
1278
|
+
// Environment flags
|
|
1279
|
+
/**
|
|
1280
|
+
* Flag to indicate if search is enabled based on environment variables.
|
|
1281
|
+
*/
|
|
1282
|
+
const searchEnabled = process.env.SEARCH != null && process.env.SEARCH.toLowerCase() === 'true';
|
|
1283
|
+
/**
|
|
1284
|
+
* Flag to indicate if MeiliSearch is enabled based on required environment variables.
|
|
1285
|
+
*/
|
|
1286
|
+
const meiliEnabled = process.env.MEILI_HOST != null && process.env.MEILI_MASTER_KEY != null && searchEnabled;
|
|
1287
|
+
/**
|
|
1288
|
+
* Local implementation of parseTextParts to avoid dependency on librechat-data-provider
|
|
1289
|
+
* Extracts text content from an array of content items
|
|
1290
|
+
*/
|
|
1291
|
+
const parseTextParts = (content) => {
|
|
1292
|
+
if (!Array.isArray(content)) {
|
|
1293
|
+
return '';
|
|
1294
|
+
}
|
|
1295
|
+
return content
|
|
1296
|
+
.filter((item) => item.type === 'text' && typeof item.text === 'string')
|
|
1297
|
+
.map((item) => item.text)
|
|
1298
|
+
.join(' ')
|
|
1299
|
+
.trim();
|
|
1300
|
+
};
|
|
1301
|
+
/**
|
|
1302
|
+
* Local implementation to handle Bing convoId conversion
|
|
1303
|
+
*/
|
|
1304
|
+
const cleanUpPrimaryKeyValue = (value) => {
|
|
1305
|
+
return value.replace(/--/g, '|');
|
|
1306
|
+
};
|
|
1307
|
+
/**
|
|
1308
|
+
* Validates the required options for configuring the mongoMeili plugin.
|
|
1309
|
+
*/
|
|
1310
|
+
const validateOptions = (options) => {
|
|
1311
|
+
const requiredKeys = ['host', 'apiKey', 'indexName'];
|
|
1312
|
+
requiredKeys.forEach((key) => {
|
|
1313
|
+
if (!options[key]) {
|
|
1314
|
+
throw new Error(`Missing mongoMeili Option: ${key}`);
|
|
1315
|
+
}
|
|
1316
|
+
});
|
|
1317
|
+
};
|
|
1318
|
+
/**
|
|
1319
|
+
* Factory function to create a MeiliMongooseModel class which extends a Mongoose model.
|
|
1320
|
+
* This class contains static and instance methods to synchronize and manage the MeiliSearch index
|
|
1321
|
+
* corresponding to the MongoDB collection.
|
|
1322
|
+
*
|
|
1323
|
+
* @param config - Configuration object.
|
|
1324
|
+
* @param config.index - The MeiliSearch index object.
|
|
1325
|
+
* @param config.attributesToIndex - List of attributes to index.
|
|
1326
|
+
* @returns A class definition that will be loaded into the Mongoose schema.
|
|
1327
|
+
*/
|
|
1328
|
+
const createMeiliMongooseModel = ({ index, attributesToIndex, }) => {
|
|
1329
|
+
const primaryKey = attributesToIndex[0];
|
|
1330
|
+
class MeiliMongooseModel {
|
|
1331
|
+
/**
|
|
1332
|
+
* Synchronizes the data between the MongoDB collection and the MeiliSearch index.
|
|
1333
|
+
*
|
|
1334
|
+
* The synchronization process involves:
|
|
1335
|
+
* 1. Fetching all documents from the MongoDB collection and MeiliSearch index.
|
|
1336
|
+
* 2. Comparing documents from both sources.
|
|
1337
|
+
* 3. Deleting documents from MeiliSearch that no longer exist in MongoDB.
|
|
1338
|
+
* 4. Adding documents to MeiliSearch that exist in MongoDB but not in the index.
|
|
1339
|
+
* 5. Updating documents in MeiliSearch if key fields (such as `text` or `title`) differ.
|
|
1340
|
+
* 6. Updating the `_meiliIndex` field in MongoDB to indicate the indexing status.
|
|
1341
|
+
*
|
|
1342
|
+
* Note: The function processes documents in batches because MeiliSearch's
|
|
1343
|
+
* `index.getDocuments` requires an exact limit and `index.addDocuments` does not handle
|
|
1344
|
+
* partial failures in a batch.
|
|
1345
|
+
*
|
|
1346
|
+
* @returns {Promise<void>} Resolves when the synchronization is complete.
|
|
1347
|
+
*/
|
|
1348
|
+
static async syncWithMeili() {
|
|
1349
|
+
try {
|
|
1350
|
+
let moreDocuments = true;
|
|
1351
|
+
const mongoDocuments = await this.find().lean();
|
|
1352
|
+
const format = (doc) => _.omitBy(_.pick(doc, attributesToIndex), (v, k) => k.startsWith('$'));
|
|
1353
|
+
const mongoMap = new Map(mongoDocuments.map((doc) => {
|
|
1354
|
+
const typedDoc = doc;
|
|
1355
|
+
return [typedDoc[primaryKey], format(typedDoc)];
|
|
1356
|
+
}));
|
|
1357
|
+
const indexMap = new Map();
|
|
1358
|
+
let offset = 0;
|
|
1359
|
+
const batchSize = 1000;
|
|
1360
|
+
while (moreDocuments) {
|
|
1361
|
+
const batch = await index.getDocuments({ limit: batchSize, offset });
|
|
1362
|
+
if (batch.results.length === 0) {
|
|
1363
|
+
moreDocuments = false;
|
|
1364
|
+
}
|
|
1365
|
+
for (const doc of batch.results) {
|
|
1366
|
+
indexMap.set(doc[primaryKey], format(doc));
|
|
1367
|
+
}
|
|
1368
|
+
offset += batchSize;
|
|
1369
|
+
}
|
|
1370
|
+
logger$1.debug('[syncWithMeili]', { indexMap: indexMap.size, mongoMap: mongoMap.size });
|
|
1371
|
+
const updateOps = [];
|
|
1372
|
+
// Process documents present in the MeiliSearch index
|
|
1373
|
+
for (const [id, doc] of indexMap) {
|
|
1374
|
+
const update = {};
|
|
1375
|
+
update[primaryKey] = id;
|
|
1376
|
+
if (mongoMap.has(id)) {
|
|
1377
|
+
const mongoDoc = mongoMap.get(id);
|
|
1378
|
+
if ((doc.text && doc.text !== (mongoDoc === null || mongoDoc === void 0 ? void 0 : mongoDoc.text)) ||
|
|
1379
|
+
(doc.title && doc.title !== (mongoDoc === null || mongoDoc === void 0 ? void 0 : mongoDoc.title))) {
|
|
1380
|
+
logger$1.debug(`[syncWithMeili] ${id} had document discrepancy in ${doc.text ? 'text' : 'title'} field`);
|
|
1381
|
+
updateOps.push({
|
|
1382
|
+
updateOne: { filter: update, update: { $set: { _meiliIndex: true } } },
|
|
1383
|
+
});
|
|
1384
|
+
await index.addDocuments([doc]);
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
else {
|
|
1388
|
+
await index.deleteDocument(id);
|
|
1389
|
+
updateOps.push({
|
|
1390
|
+
updateOne: { filter: update, update: { $set: { _meiliIndex: false } } },
|
|
1391
|
+
});
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
// Process documents present in MongoDB
|
|
1395
|
+
for (const [id, doc] of mongoMap) {
|
|
1396
|
+
const update = {};
|
|
1397
|
+
update[primaryKey] = id;
|
|
1398
|
+
if (!indexMap.has(id)) {
|
|
1399
|
+
await index.addDocuments([doc]);
|
|
1400
|
+
updateOps.push({
|
|
1401
|
+
updateOne: { filter: update, update: { $set: { _meiliIndex: true } } },
|
|
1402
|
+
});
|
|
1403
|
+
}
|
|
1404
|
+
else if (doc._meiliIndex === false) {
|
|
1405
|
+
updateOps.push({
|
|
1406
|
+
updateOne: { filter: update, update: { $set: { _meiliIndex: true } } },
|
|
1407
|
+
});
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
if (updateOps.length > 0) {
|
|
1411
|
+
await this.collection.bulkWrite(updateOps);
|
|
1412
|
+
logger$1.debug(`[syncWithMeili] Finished indexing ${primaryKey === 'messageId' ? 'messages' : 'conversations'}`);
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
catch (error) {
|
|
1416
|
+
logger$1.error('[syncWithMeili] Error adding document to Meili', error);
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
/**
|
|
1420
|
+
* Updates settings for the MeiliSearch index
|
|
1421
|
+
*/
|
|
1422
|
+
static async setMeiliIndexSettings(settings) {
|
|
1423
|
+
return await index.updateSettings(settings);
|
|
1424
|
+
}
|
|
1425
|
+
/**
|
|
1426
|
+
* Searches the MeiliSearch index and optionally populates results
|
|
1427
|
+
*/
|
|
1428
|
+
static async meiliSearch(q, params, populate) {
|
|
1429
|
+
const data = await index.search(q, params);
|
|
1430
|
+
if (populate) {
|
|
1431
|
+
const query = {};
|
|
1432
|
+
query[primaryKey] = _.map(data.hits, (hit) => cleanUpPrimaryKeyValue(hit[primaryKey]));
|
|
1433
|
+
const projection = Object.keys(this.schema.obj).reduce((results, key) => {
|
|
1434
|
+
if (!key.startsWith('$')) {
|
|
1435
|
+
results[key] = 1;
|
|
1436
|
+
}
|
|
1437
|
+
return results;
|
|
1438
|
+
}, { _id: 1, __v: 1 });
|
|
1439
|
+
const hitsFromMongoose = await this.find(query, projection).lean();
|
|
1440
|
+
const populatedHits = data.hits.map((hit) => {
|
|
1441
|
+
hit[primaryKey];
|
|
1442
|
+
const originalHit = _.find(hitsFromMongoose, (item) => {
|
|
1443
|
+
const typedItem = item;
|
|
1444
|
+
return typedItem[primaryKey] === hit[primaryKey];
|
|
1445
|
+
});
|
|
1446
|
+
return {
|
|
1447
|
+
...(originalHit && typeof originalHit === 'object' ? originalHit : {}),
|
|
1448
|
+
...hit,
|
|
1449
|
+
};
|
|
1450
|
+
});
|
|
1451
|
+
data.hits = populatedHits;
|
|
1452
|
+
}
|
|
1453
|
+
return data;
|
|
1454
|
+
}
|
|
1455
|
+
/**
|
|
1456
|
+
* Preprocesses the current document for indexing
|
|
1457
|
+
*/
|
|
1458
|
+
preprocessObjectForIndex() {
|
|
1459
|
+
const object = _.omitBy(_.pick(this.toJSON(), attributesToIndex), (v, k) => k.startsWith('$'));
|
|
1460
|
+
if (object.conversationId &&
|
|
1461
|
+
typeof object.conversationId === 'string' &&
|
|
1462
|
+
object.conversationId.includes('|')) {
|
|
1463
|
+
object.conversationId = object.conversationId.replace(/\|/g, '--');
|
|
1464
|
+
}
|
|
1465
|
+
if (object.content && Array.isArray(object.content)) {
|
|
1466
|
+
object.text = parseTextParts(object.content);
|
|
1467
|
+
delete object.content;
|
|
1468
|
+
}
|
|
1469
|
+
return object;
|
|
1470
|
+
}
|
|
1471
|
+
/**
|
|
1472
|
+
* Adds the current document to the MeiliSearch index
|
|
1473
|
+
*/
|
|
1474
|
+
async addObjectToMeili() {
|
|
1475
|
+
const object = this.preprocessObjectForIndex();
|
|
1476
|
+
try {
|
|
1477
|
+
await index.addDocuments([object]);
|
|
1478
|
+
}
|
|
1479
|
+
catch (error) {
|
|
1480
|
+
logger$1.error('[addObjectToMeili] Error adding document to Meili', error);
|
|
1481
|
+
}
|
|
1482
|
+
await this.collection.updateMany({ _id: this._id }, { $set: { _meiliIndex: true } });
|
|
1483
|
+
}
|
|
1484
|
+
/**
|
|
1485
|
+
* Updates the current document in the MeiliSearch index
|
|
1486
|
+
*/
|
|
1487
|
+
async updateObjectToMeili() {
|
|
1488
|
+
const object = _.omitBy(_.pick(this.toJSON(), attributesToIndex), (v, k) => k.startsWith('$'));
|
|
1489
|
+
await index.updateDocuments([object]);
|
|
1490
|
+
}
|
|
1491
|
+
/**
|
|
1492
|
+
* Deletes the current document from the MeiliSearch index.
|
|
1493
|
+
*
|
|
1494
|
+
* @returns {Promise<void>}
|
|
1495
|
+
*/
|
|
1496
|
+
async deleteObjectFromMeili() {
|
|
1497
|
+
await index.deleteDocument(this._id);
|
|
1498
|
+
}
|
|
1499
|
+
/**
|
|
1500
|
+
* Post-save hook to synchronize the document with MeiliSearch.
|
|
1501
|
+
*
|
|
1502
|
+
* If the document is already indexed (i.e. `_meiliIndex` is true), it updates it;
|
|
1503
|
+
* otherwise, it adds the document to the index.
|
|
1504
|
+
*/
|
|
1505
|
+
postSaveHook() {
|
|
1506
|
+
if (this._meiliIndex) {
|
|
1507
|
+
this.updateObjectToMeili();
|
|
1508
|
+
}
|
|
1509
|
+
else {
|
|
1510
|
+
this.addObjectToMeili();
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
/**
|
|
1514
|
+
* Post-update hook to update the document in MeiliSearch.
|
|
1515
|
+
*
|
|
1516
|
+
* This hook is triggered after a document update, ensuring that changes are
|
|
1517
|
+
* propagated to the MeiliSearch index if the document is indexed.
|
|
1518
|
+
*/
|
|
1519
|
+
postUpdateHook() {
|
|
1520
|
+
if (this._meiliIndex) {
|
|
1521
|
+
this.updateObjectToMeili();
|
|
1522
|
+
}
|
|
1523
|
+
}
|
|
1524
|
+
/**
|
|
1525
|
+
* Post-remove hook to delete the document from MeiliSearch.
|
|
1526
|
+
*
|
|
1527
|
+
* This hook is triggered after a document is removed, ensuring that the document
|
|
1528
|
+
* is also removed from the MeiliSearch index if it was previously indexed.
|
|
1529
|
+
*/
|
|
1530
|
+
postRemoveHook() {
|
|
1531
|
+
if (this._meiliIndex) {
|
|
1532
|
+
this.deleteObjectFromMeili();
|
|
1533
|
+
}
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
return MeiliMongooseModel;
|
|
1537
|
+
};
|
|
1538
|
+
/**
|
|
1539
|
+
* Mongoose plugin to synchronize MongoDB collections with a MeiliSearch index.
|
|
1540
|
+
*
|
|
1541
|
+
* This plugin:
|
|
1542
|
+
* - Validates the provided options.
|
|
1543
|
+
* - Adds a `_meiliIndex` field to the schema to track indexing status.
|
|
1544
|
+
* - Sets up a MeiliSearch client and creates an index if it doesn't already exist.
|
|
1545
|
+
* - Loads class methods for syncing, searching, and managing documents in MeiliSearch.
|
|
1546
|
+
* - Registers Mongoose hooks (post-save, post-update, post-remove, etc.) to maintain index consistency.
|
|
1547
|
+
*
|
|
1548
|
+
* @param schema - The Mongoose schema to which the plugin is applied.
|
|
1549
|
+
* @param options - Configuration options.
|
|
1550
|
+
* @param options.host - The MeiliSearch host.
|
|
1551
|
+
* @param options.apiKey - The MeiliSearch API key.
|
|
1552
|
+
* @param options.indexName - The name of the MeiliSearch index.
|
|
1553
|
+
* @param options.primaryKey - The primary key field for indexing.
|
|
1554
|
+
*/
|
|
1555
|
+
function mongoMeili(schema, options) {
|
|
1556
|
+
const mongoose = options.mongoose;
|
|
1557
|
+
validateOptions(options);
|
|
1558
|
+
// Add _meiliIndex field to the schema to track if a document has been indexed in MeiliSearch.
|
|
1559
|
+
schema.add({
|
|
1560
|
+
_meiliIndex: {
|
|
1561
|
+
type: Boolean,
|
|
1562
|
+
required: false,
|
|
1563
|
+
select: false,
|
|
1564
|
+
default: false,
|
|
1565
|
+
},
|
|
1566
|
+
});
|
|
1567
|
+
const { host, apiKey, indexName, primaryKey } = options;
|
|
1568
|
+
const client = new MeiliSearch({ host, apiKey });
|
|
1569
|
+
client.createIndex(indexName, { primaryKey });
|
|
1570
|
+
const index = client.index(indexName);
|
|
1571
|
+
// Collect attributes from the schema that should be indexed
|
|
1572
|
+
const attributesToIndex = [
|
|
1573
|
+
...Object.entries(schema.obj).reduce((results, [key, value]) => {
|
|
1574
|
+
const schemaValue = value;
|
|
1575
|
+
return schemaValue.meiliIndex ? [...results, key] : results;
|
|
1576
|
+
}, []),
|
|
1577
|
+
];
|
|
1578
|
+
schema.loadClass(createMeiliMongooseModel({ index, attributesToIndex }));
|
|
1579
|
+
// Register Mongoose hooks
|
|
1580
|
+
schema.post('save', function (doc) {
|
|
1581
|
+
var _a;
|
|
1582
|
+
(_a = doc.postSaveHook) === null || _a === void 0 ? void 0 : _a.call(doc);
|
|
1583
|
+
});
|
|
1584
|
+
schema.post('updateOne', function (doc) {
|
|
1585
|
+
var _a;
|
|
1586
|
+
(_a = doc.postUpdateHook) === null || _a === void 0 ? void 0 : _a.call(doc);
|
|
1587
|
+
});
|
|
1588
|
+
schema.post('deleteOne', function (doc) {
|
|
1589
|
+
var _a;
|
|
1590
|
+
(_a = doc.postRemoveHook) === null || _a === void 0 ? void 0 : _a.call(doc);
|
|
1591
|
+
});
|
|
1592
|
+
// Pre-deleteMany hook: remove corresponding documents from MeiliSearch when multiple documents are deleted.
|
|
1593
|
+
schema.pre('deleteMany', async function (next) {
|
|
1594
|
+
if (!meiliEnabled) {
|
|
1595
|
+
return next();
|
|
1596
|
+
}
|
|
1597
|
+
try {
|
|
1598
|
+
const conditions = this.getQuery();
|
|
1599
|
+
if (Object.prototype.hasOwnProperty.call(schema.obj, 'messages')) {
|
|
1600
|
+
const convoIndex = client.index('convos');
|
|
1601
|
+
const deletedConvos = await mongoose
|
|
1602
|
+
.model('Conversation')
|
|
1603
|
+
.find(conditions)
|
|
1604
|
+
.lean();
|
|
1605
|
+
const promises = deletedConvos.map((convo) => convoIndex.deleteDocument(convo.conversationId));
|
|
1606
|
+
await Promise.all(promises);
|
|
1607
|
+
}
|
|
1608
|
+
if (Object.prototype.hasOwnProperty.call(schema.obj, 'messageId')) {
|
|
1609
|
+
const messageIndex = client.index('messages');
|
|
1610
|
+
const deletedMessages = await mongoose
|
|
1611
|
+
.model('Message')
|
|
1612
|
+
.find(conditions)
|
|
1613
|
+
.lean();
|
|
1614
|
+
const promises = deletedMessages.map((message) => messageIndex.deleteDocument(message.messageId));
|
|
1615
|
+
await Promise.all(promises);
|
|
1616
|
+
}
|
|
1617
|
+
return next();
|
|
1618
|
+
}
|
|
1619
|
+
catch (error) {
|
|
1620
|
+
if (meiliEnabled) {
|
|
1621
|
+
logger$1.error('[MeiliMongooseModel.deleteMany] There was an issue deleting conversation indexes upon deletion. Next startup may be slow due to syncing.', error);
|
|
1622
|
+
}
|
|
1623
|
+
return next();
|
|
1624
|
+
}
|
|
1625
|
+
});
|
|
1626
|
+
// Post-findOneAndUpdate hook
|
|
1627
|
+
schema.post('findOneAndUpdate', async function (doc) {
|
|
1628
|
+
var _a;
|
|
1629
|
+
if (!meiliEnabled) {
|
|
1630
|
+
return;
|
|
1631
|
+
}
|
|
1632
|
+
if (doc.unfinished) {
|
|
1633
|
+
return;
|
|
1634
|
+
}
|
|
1635
|
+
let meiliDoc;
|
|
1636
|
+
if (doc.messages) {
|
|
1637
|
+
try {
|
|
1638
|
+
meiliDoc = await client.index('convos').getDocument(doc.conversationId);
|
|
1639
|
+
}
|
|
1640
|
+
catch (error) {
|
|
1641
|
+
logger$1.debug('[MeiliMongooseModel.findOneAndUpdate] Convo not found in MeiliSearch and will index ' +
|
|
1642
|
+
doc.conversationId, error);
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
if (meiliDoc && meiliDoc.title === doc.title) {
|
|
1646
|
+
return;
|
|
1647
|
+
}
|
|
1648
|
+
(_a = doc.postSaveHook) === null || _a === void 0 ? void 0 : _a.call(doc);
|
|
1649
|
+
});
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
/**
|
|
1653
|
+
* Creates or returns the Conversation model using the provided mongoose instance and schema
|
|
1654
|
+
*/
|
|
1655
|
+
function createConversationModel(mongoose) {
|
|
1656
|
+
if (process.env.MEILI_HOST && process.env.MEILI_MASTER_KEY) {
|
|
1657
|
+
convoSchema.plugin(mongoMeili, {
|
|
1658
|
+
mongoose,
|
|
1659
|
+
host: process.env.MEILI_HOST,
|
|
1660
|
+
apiKey: process.env.MEILI_MASTER_KEY,
|
|
1661
|
+
/** Note: Will get created automatically if it doesn't exist already */
|
|
1662
|
+
indexName: 'convos',
|
|
1663
|
+
primaryKey: 'conversationId',
|
|
1664
|
+
});
|
|
1665
|
+
}
|
|
1666
|
+
return (mongoose.models.Conversation || mongoose.model('Conversation', convoSchema));
|
|
1667
|
+
}
|
|
1668
|
+
|
|
1669
|
+
/**
|
|
1670
|
+
* Creates or returns the Message model using the provided mongoose instance and schema
|
|
1671
|
+
*/
|
|
1672
|
+
function createMessageModel(mongoose) {
|
|
1673
|
+
if (process.env.MEILI_HOST && process.env.MEILI_MASTER_KEY) {
|
|
1674
|
+
messageSchema.plugin(mongoMeili, {
|
|
1675
|
+
mongoose,
|
|
1676
|
+
host: process.env.MEILI_HOST,
|
|
1677
|
+
apiKey: process.env.MEILI_MASTER_KEY,
|
|
1678
|
+
indexName: 'messages',
|
|
1679
|
+
primaryKey: 'messageId',
|
|
1680
|
+
});
|
|
1681
|
+
}
|
|
1682
|
+
return mongoose.models.Message || mongoose.model('Message', messageSchema);
|
|
1683
|
+
}
|
|
1684
|
+
|
|
1685
|
+
/**
|
|
1686
|
+
* Creates or returns the Agent model using the provided mongoose instance and schema
|
|
1687
|
+
*/
|
|
1688
|
+
function createAgentModel(mongoose) {
|
|
1689
|
+
return mongoose.models.Agent || mongoose.model('Agent', agentSchema);
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
/**
|
|
1693
|
+
* Creates or returns the Role model using the provided mongoose instance and schema
|
|
1694
|
+
*/
|
|
1695
|
+
function createRoleModel(mongoose) {
|
|
1696
|
+
return mongoose.models.Role || mongoose.model('Role', roleSchema);
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
/**
|
|
1700
|
+
* Creates or returns the Action model using the provided mongoose instance and schema
|
|
1701
|
+
*/
|
|
1702
|
+
function createActionModel(mongoose) {
|
|
1703
|
+
return mongoose.models.Action || mongoose.model('Action', Action);
|
|
1704
|
+
}
|
|
1705
|
+
|
|
1706
|
+
/**
|
|
1707
|
+
* Creates or returns the Assistant model using the provided mongoose instance and schema
|
|
1708
|
+
*/
|
|
1709
|
+
function createAssistantModel(mongoose) {
|
|
1710
|
+
return mongoose.models.Assistant || mongoose.model('Assistant', assistantSchema);
|
|
1711
|
+
}
|
|
1712
|
+
|
|
1713
|
+
/**
|
|
1714
|
+
* Creates or returns the File model using the provided mongoose instance and schema
|
|
1715
|
+
*/
|
|
1716
|
+
function createFileModel(mongoose) {
|
|
1717
|
+
return mongoose.models.File || mongoose.model('File', file);
|
|
1718
|
+
}
|
|
1719
|
+
|
|
1720
|
+
/**
|
|
1721
|
+
* Creates or returns the Banner model using the provided mongoose instance and schema
|
|
1722
|
+
*/
|
|
1723
|
+
function createBannerModel(mongoose) {
|
|
1724
|
+
return mongoose.models.Banner || mongoose.model('Banner', bannerSchema);
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
/**
|
|
1728
|
+
* Creates or returns the Project model using the provided mongoose instance and schema
|
|
1729
|
+
*/
|
|
1730
|
+
function createProjectModel(mongoose) {
|
|
1731
|
+
return mongoose.models.Project || mongoose.model('Project', projectSchema);
|
|
1732
|
+
}
|
|
1733
|
+
|
|
1734
|
+
/**
|
|
1735
|
+
* Creates or returns the Key model using the provided mongoose instance and schema
|
|
1736
|
+
*/
|
|
1737
|
+
function createKeyModel(mongoose) {
|
|
1738
|
+
return mongoose.models.Key || mongoose.model('Key', keySchema);
|
|
1739
|
+
}
|
|
1740
|
+
|
|
1741
|
+
/**
|
|
1742
|
+
* Creates or returns the PluginAuth model using the provided mongoose instance and schema
|
|
1743
|
+
*/
|
|
1744
|
+
function createPluginAuthModel(mongoose) {
|
|
1745
|
+
return mongoose.models.PluginAuth || mongoose.model('PluginAuth', pluginAuthSchema);
|
|
1746
|
+
}
|
|
1747
|
+
|
|
1748
|
+
/**
|
|
1749
|
+
* Creates or returns the Transaction model using the provided mongoose instance and schema
|
|
1750
|
+
*/
|
|
1751
|
+
function createTransactionModel(mongoose) {
|
|
1752
|
+
return (mongoose.models.Transaction || mongoose.model('Transaction', transactionSchema));
|
|
1753
|
+
}
|
|
1754
|
+
|
|
1755
|
+
/**
|
|
1756
|
+
* Creates or returns the Preset model using the provided mongoose instance and schema
|
|
1757
|
+
*/
|
|
1758
|
+
function createPresetModel(mongoose) {
|
|
1759
|
+
return mongoose.models.Preset || mongoose.model('Preset', presetSchema);
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
/**
|
|
1763
|
+
* Creates or returns the Prompt model using the provided mongoose instance and schema
|
|
1764
|
+
*/
|
|
1765
|
+
function createPromptModel(mongoose) {
|
|
1766
|
+
return mongoose.models.Prompt || mongoose.model('Prompt', promptSchema);
|
|
1767
|
+
}
|
|
1768
|
+
|
|
1769
|
+
/**
|
|
1770
|
+
* Creates or returns the PromptGroup model using the provided mongoose instance and schema
|
|
1771
|
+
*/
|
|
1772
|
+
function createPromptGroupModel(mongoose) {
|
|
1773
|
+
return (mongoose.models.PromptGroup ||
|
|
1774
|
+
mongoose.model('PromptGroup', promptGroupSchema));
|
|
1775
|
+
}
|
|
1776
|
+
|
|
1777
|
+
/**
|
|
1778
|
+
* Creates or returns the ConversationTag model using the provided mongoose instance and schema
|
|
1779
|
+
*/
|
|
1780
|
+
function createConversationTagModel(mongoose) {
|
|
1781
|
+
return (mongoose.models.ConversationTag ||
|
|
1782
|
+
mongoose.model('ConversationTag', conversationTag));
|
|
1783
|
+
}
|
|
1784
|
+
|
|
1785
|
+
/**
|
|
1786
|
+
* Creates or returns the SharedLink model using the provided mongoose instance and schema
|
|
1787
|
+
*/
|
|
1788
|
+
function createSharedLinkModel(mongoose) {
|
|
1789
|
+
return mongoose.models.SharedLink || mongoose.model('SharedLink', shareSchema);
|
|
1790
|
+
}
|
|
1791
|
+
|
|
1792
|
+
/**
|
|
1793
|
+
* Creates or returns the ToolCall model using the provided mongoose instance and schema
|
|
1794
|
+
*/
|
|
1795
|
+
function createToolCallModel(mongoose) {
|
|
1796
|
+
return mongoose.models.ToolCall || mongoose.model('ToolCall', toolCallSchema);
|
|
1797
|
+
}
|
|
1798
|
+
|
|
1799
|
+
/**
|
|
1800
|
+
* Creates all database models for all collections
|
|
1801
|
+
*/
|
|
1802
|
+
function createModels(mongoose) {
|
|
1803
|
+
return {
|
|
1804
|
+
User: createUserModel(mongoose),
|
|
1805
|
+
Token: createTokenModel(mongoose),
|
|
1806
|
+
Session: createSessionModel(mongoose),
|
|
1807
|
+
Balance: createBalanceModel(mongoose),
|
|
1808
|
+
Conversation: createConversationModel(mongoose),
|
|
1809
|
+
Message: createMessageModel(mongoose),
|
|
1810
|
+
Agent: createAgentModel(mongoose),
|
|
1811
|
+
Role: createRoleModel(mongoose),
|
|
1812
|
+
Action: createActionModel(mongoose),
|
|
1813
|
+
Assistant: createAssistantModel(mongoose),
|
|
1814
|
+
File: createFileModel(mongoose),
|
|
1815
|
+
Banner: createBannerModel(mongoose),
|
|
1816
|
+
Project: createProjectModel(mongoose),
|
|
1817
|
+
Key: createKeyModel(mongoose),
|
|
1818
|
+
PluginAuth: createPluginAuthModel(mongoose),
|
|
1819
|
+
Transaction: createTransactionModel(mongoose),
|
|
1820
|
+
Preset: createPresetModel(mongoose),
|
|
1821
|
+
Prompt: createPromptModel(mongoose),
|
|
1822
|
+
PromptGroup: createPromptGroupModel(mongoose),
|
|
1823
|
+
ConversationTag: createConversationTagModel(mongoose),
|
|
1824
|
+
SharedLink: createSharedLinkModel(mongoose),
|
|
1825
|
+
ToolCall: createToolCallModel(mongoose),
|
|
1826
|
+
};
|
|
1827
|
+
}
|
|
1828
|
+
|
|
1829
|
+
/** Factory function that takes mongoose instance and returns the methods */
|
|
1830
|
+
function createUserMethods(mongoose) {
|
|
1831
|
+
/**
|
|
1832
|
+
* Search for a single user based on partial data and return matching user document as plain object.
|
|
1833
|
+
*/
|
|
1834
|
+
async function findUser(searchCriteria, fieldsToSelect) {
|
|
1835
|
+
const User = mongoose.models.User;
|
|
1836
|
+
const query = User.findOne(searchCriteria);
|
|
1837
|
+
if (fieldsToSelect) {
|
|
1838
|
+
query.select(fieldsToSelect);
|
|
1839
|
+
}
|
|
1840
|
+
return (await query.lean());
|
|
1841
|
+
}
|
|
1842
|
+
/**
|
|
1843
|
+
* Count the number of user documents in the collection based on the provided filter.
|
|
1844
|
+
*/
|
|
1845
|
+
async function countUsers(filter = {}) {
|
|
1846
|
+
const User = mongoose.models.User;
|
|
1847
|
+
return await User.countDocuments(filter);
|
|
1848
|
+
}
|
|
1849
|
+
/**
|
|
1850
|
+
* Creates a new user, optionally with a TTL of 1 week.
|
|
1851
|
+
*/
|
|
1852
|
+
async function createUser(data, balanceConfig, disableTTL = true, returnUser = false) {
|
|
1853
|
+
const User = mongoose.models.User;
|
|
1854
|
+
const Balance = mongoose.models.Balance;
|
|
1855
|
+
const userData = {
|
|
1856
|
+
...data,
|
|
1857
|
+
expiresAt: disableTTL ? undefined : new Date(Date.now() + 604800 * 1000), // 1 week in milliseconds
|
|
1858
|
+
};
|
|
1859
|
+
if (disableTTL) {
|
|
1860
|
+
delete userData.expiresAt;
|
|
1861
|
+
}
|
|
1862
|
+
const user = await User.create(userData);
|
|
1863
|
+
// If balance is enabled, create or update a balance record for the user
|
|
1864
|
+
if ((balanceConfig === null || balanceConfig === void 0 ? void 0 : balanceConfig.enabled) && (balanceConfig === null || balanceConfig === void 0 ? void 0 : balanceConfig.startBalance)) {
|
|
1865
|
+
const update = {
|
|
1866
|
+
$inc: { tokenCredits: balanceConfig.startBalance },
|
|
1867
|
+
};
|
|
1868
|
+
if (balanceConfig.autoRefillEnabled &&
|
|
1869
|
+
balanceConfig.refillIntervalValue != null &&
|
|
1870
|
+
balanceConfig.refillIntervalUnit != null &&
|
|
1871
|
+
balanceConfig.refillAmount != null) {
|
|
1872
|
+
update.$set = {
|
|
1873
|
+
autoRefillEnabled: true,
|
|
1874
|
+
refillIntervalValue: balanceConfig.refillIntervalValue,
|
|
1875
|
+
refillIntervalUnit: balanceConfig.refillIntervalUnit,
|
|
1876
|
+
refillAmount: balanceConfig.refillAmount,
|
|
1877
|
+
};
|
|
1878
|
+
}
|
|
1879
|
+
await Balance.findOneAndUpdate({ user: user._id }, update, {
|
|
1880
|
+
upsert: true,
|
|
1881
|
+
new: true,
|
|
1882
|
+
}).lean();
|
|
1883
|
+
}
|
|
1884
|
+
if (returnUser) {
|
|
1885
|
+
return user.toObject();
|
|
1886
|
+
}
|
|
1887
|
+
return user._id;
|
|
1888
|
+
}
|
|
1889
|
+
/**
|
|
1890
|
+
* Update a user with new data without overwriting existing properties.
|
|
1891
|
+
*/
|
|
1892
|
+
async function updateUser(userId, updateData) {
|
|
1893
|
+
const User = mongoose.models.User;
|
|
1894
|
+
const updateOperation = {
|
|
1895
|
+
$set: updateData,
|
|
1896
|
+
$unset: { expiresAt: '' }, // Remove the expiresAt field to prevent TTL
|
|
1897
|
+
};
|
|
1898
|
+
return (await User.findByIdAndUpdate(userId, updateOperation, {
|
|
1899
|
+
new: true,
|
|
1900
|
+
runValidators: true,
|
|
1901
|
+
}).lean());
|
|
1902
|
+
}
|
|
1903
|
+
/**
|
|
1904
|
+
* Retrieve a user by ID and convert the found user document to a plain object.
|
|
1905
|
+
*/
|
|
1906
|
+
async function getUserById(userId, fieldsToSelect) {
|
|
1907
|
+
const User = mongoose.models.User;
|
|
1908
|
+
const query = User.findById(userId);
|
|
1909
|
+
if (fieldsToSelect) {
|
|
1910
|
+
query.select(fieldsToSelect);
|
|
1911
|
+
}
|
|
1912
|
+
return (await query.lean());
|
|
1913
|
+
}
|
|
1914
|
+
/**
|
|
1915
|
+
* Delete a user by their unique ID.
|
|
1916
|
+
*/
|
|
1917
|
+
async function deleteUserById(userId) {
|
|
1918
|
+
try {
|
|
1919
|
+
const User = mongoose.models.User;
|
|
1920
|
+
const result = await User.deleteOne({ _id: userId });
|
|
1921
|
+
if (result.deletedCount === 0) {
|
|
1922
|
+
return { deletedCount: 0, message: 'No user found with that ID.' };
|
|
1923
|
+
}
|
|
1924
|
+
return { deletedCount: result.deletedCount, message: 'User was deleted successfully.' };
|
|
1925
|
+
}
|
|
1926
|
+
catch (error) {
|
|
1927
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
1928
|
+
throw new Error('Error deleting user: ' + errorMessage);
|
|
1929
|
+
}
|
|
1930
|
+
}
|
|
1931
|
+
/**
|
|
1932
|
+
* Generates a JWT token for a given user.
|
|
1933
|
+
*/
|
|
1934
|
+
async function generateToken(user) {
|
|
1935
|
+
if (!user) {
|
|
1936
|
+
throw new Error('No user provided');
|
|
1937
|
+
}
|
|
1938
|
+
let expires = 1000 * 60 * 15;
|
|
1939
|
+
if (process.env.SESSION_EXPIRY !== undefined && process.env.SESSION_EXPIRY !== '') {
|
|
1940
|
+
try {
|
|
1941
|
+
const evaluated = eval(process.env.SESSION_EXPIRY);
|
|
1942
|
+
if (evaluated) {
|
|
1943
|
+
expires = evaluated;
|
|
1944
|
+
}
|
|
1945
|
+
}
|
|
1946
|
+
catch (error) {
|
|
1947
|
+
console.warn('Invalid SESSION_EXPIRY expression, using default:', error);
|
|
1948
|
+
}
|
|
1949
|
+
}
|
|
1950
|
+
return await signPayload({
|
|
1951
|
+
payload: {
|
|
1952
|
+
id: user._id,
|
|
1953
|
+
username: user.username,
|
|
1954
|
+
provider: user.provider,
|
|
1955
|
+
email: user.email,
|
|
1956
|
+
},
|
|
1957
|
+
secret: process.env.JWT_SECRET,
|
|
1958
|
+
expirationTime: expires / 1000,
|
|
1959
|
+
});
|
|
1960
|
+
}
|
|
1961
|
+
// Return all methods
|
|
1962
|
+
return {
|
|
1963
|
+
findUser,
|
|
1964
|
+
countUsers,
|
|
1965
|
+
createUser,
|
|
1966
|
+
updateUser,
|
|
1967
|
+
getUserById,
|
|
1968
|
+
deleteUserById,
|
|
1969
|
+
generateToken,
|
|
1970
|
+
};
|
|
1971
|
+
}
|
|
1972
|
+
|
|
1973
|
+
const SPLAT_SYMBOL = Symbol.for('splat');
|
|
1974
|
+
const MESSAGE_SYMBOL = Symbol.for('message');
|
|
1975
|
+
const CONSOLE_JSON_STRING_LENGTH = parseInt(process.env.CONSOLE_JSON_STRING_LENGTH || '', 10) || 255;
|
|
1976
|
+
const sensitiveKeys = [
|
|
1977
|
+
/^(sk-)[^\s]+/, // OpenAI API key pattern
|
|
1978
|
+
/(Bearer )[^\s]+/, // Header: Bearer token pattern
|
|
1979
|
+
/(api-key:? )[^\s]+/, // Header: API key pattern
|
|
1980
|
+
/(key=)[^\s]+/, // URL query param: sensitive key pattern (Google)
|
|
1981
|
+
];
|
|
1982
|
+
/**
|
|
1983
|
+
* Determines if a given value string is sensitive and returns matching regex patterns.
|
|
1984
|
+
*
|
|
1985
|
+
* @param valueStr - The value string to check.
|
|
1986
|
+
* @returns An array of regex patterns that match the value string.
|
|
1987
|
+
*/
|
|
1988
|
+
function getMatchingSensitivePatterns(valueStr) {
|
|
1989
|
+
if (valueStr) {
|
|
1990
|
+
// Filter and return all regex patterns that match the value string
|
|
1991
|
+
return sensitiveKeys.filter((regex) => regex.test(valueStr));
|
|
1992
|
+
}
|
|
1993
|
+
return [];
|
|
1994
|
+
}
|
|
1995
|
+
/**
|
|
1996
|
+
* Redacts sensitive information from a console message and trims it to a specified length if provided.
|
|
1997
|
+
* @param str - The console message to be redacted.
|
|
1998
|
+
* @param trimLength - The optional length at which to trim the redacted message.
|
|
1999
|
+
* @returns The redacted and optionally trimmed console message.
|
|
2000
|
+
*/
|
|
2001
|
+
function redactMessage(str, trimLength) {
|
|
2002
|
+
if (!str) {
|
|
2003
|
+
return '';
|
|
2004
|
+
}
|
|
2005
|
+
const patterns = getMatchingSensitivePatterns(str);
|
|
2006
|
+
patterns.forEach((pattern) => {
|
|
2007
|
+
str = str.replace(pattern, '$1[REDACTED]');
|
|
2008
|
+
});
|
|
2009
|
+
return str;
|
|
2010
|
+
}
|
|
2011
|
+
/**
|
|
2012
|
+
* Redacts sensitive information from log messages if the log level is 'error'.
|
|
2013
|
+
* Note: Intentionally mutates the object.
|
|
2014
|
+
* @param info - The log information object.
|
|
2015
|
+
* @returns The modified log information object.
|
|
2016
|
+
*/
|
|
2017
|
+
const redactFormat = winston.format((info) => {
|
|
2018
|
+
if (info.level === 'error') {
|
|
2019
|
+
// Type guard to ensure message is a string
|
|
2020
|
+
if (typeof info.message === 'string') {
|
|
2021
|
+
info.message = redactMessage(info.message);
|
|
2022
|
+
}
|
|
2023
|
+
// Handle MESSAGE_SYMBOL with type safety
|
|
2024
|
+
const symbolValue = info[MESSAGE_SYMBOL];
|
|
2025
|
+
if (typeof symbolValue === 'string') {
|
|
2026
|
+
info[MESSAGE_SYMBOL] = redactMessage(symbolValue);
|
|
2027
|
+
}
|
|
2028
|
+
}
|
|
2029
|
+
return info;
|
|
2030
|
+
});
|
|
2031
|
+
/**
|
|
2032
|
+
* Truncates long strings, especially base64 image data, within log messages.
|
|
2033
|
+
*
|
|
2034
|
+
* @param value - The value to be inspected and potentially truncated.
|
|
2035
|
+
* @param length - The length at which to truncate the value. Default: 100.
|
|
2036
|
+
* @returns The truncated or original value.
|
|
2037
|
+
*/
|
|
2038
|
+
const truncateLongStrings = (value, length = 100) => {
|
|
2039
|
+
if (typeof value === 'string') {
|
|
2040
|
+
return value.length > length ? value.substring(0, length) + '... [truncated]' : value;
|
|
2041
|
+
}
|
|
2042
|
+
return value;
|
|
2043
|
+
};
|
|
2044
|
+
/**
|
|
2045
|
+
* An array mapping function that truncates long strings (objects converted to JSON strings).
|
|
2046
|
+
* @param item - The item to be condensed.
|
|
2047
|
+
* @returns The condensed item.
|
|
2048
|
+
*/
|
|
2049
|
+
const condenseArray = (item) => {
|
|
2050
|
+
if (typeof item === 'string') {
|
|
2051
|
+
return truncateLongStrings(JSON.stringify(item));
|
|
2052
|
+
}
|
|
2053
|
+
else if (typeof item === 'object') {
|
|
2054
|
+
return truncateLongStrings(JSON.stringify(item));
|
|
2055
|
+
}
|
|
2056
|
+
return item;
|
|
2057
|
+
};
|
|
2058
|
+
/**
|
|
2059
|
+
* Formats log messages for debugging purposes.
|
|
2060
|
+
* - Truncates long strings within log messages.
|
|
2061
|
+
* - Condenses arrays by truncating long strings and objects as strings within array items.
|
|
2062
|
+
* - Redacts sensitive information from log messages if the log level is 'error'.
|
|
2063
|
+
* - Converts log information object to a formatted string.
|
|
2064
|
+
*
|
|
2065
|
+
* @param options - The options for formatting log messages.
|
|
2066
|
+
* @returns The formatted log message.
|
|
2067
|
+
*/
|
|
2068
|
+
const debugTraverse = winston.format.printf(({ level, message, timestamp, ...metadata }) => {
|
|
2069
|
+
if (!message) {
|
|
2070
|
+
return `${timestamp} ${level}`;
|
|
2071
|
+
}
|
|
2072
|
+
// Type-safe version of the CJS logic: !message?.trim || typeof message !== 'string'
|
|
2073
|
+
if (typeof message !== 'string' || !message.trim) {
|
|
2074
|
+
return `${timestamp} ${level}: ${JSON.stringify(message)}`;
|
|
2075
|
+
}
|
|
2076
|
+
let msg = `${timestamp} ${level}: ${truncateLongStrings(message.trim(), 150)}`;
|
|
2077
|
+
try {
|
|
2078
|
+
if (level !== 'debug') {
|
|
2079
|
+
return msg;
|
|
2080
|
+
}
|
|
2081
|
+
if (!metadata) {
|
|
2082
|
+
return msg;
|
|
2083
|
+
}
|
|
2084
|
+
// Type-safe access to SPLAT_SYMBOL using bracket notation
|
|
2085
|
+
const metadataRecord = metadata;
|
|
2086
|
+
const splatArray = metadataRecord[SPLAT_SYMBOL];
|
|
2087
|
+
const debugValue = Array.isArray(splatArray) ? splatArray[0] : undefined;
|
|
2088
|
+
if (!debugValue) {
|
|
2089
|
+
return msg;
|
|
2090
|
+
}
|
|
2091
|
+
if (debugValue && Array.isArray(debugValue)) {
|
|
2092
|
+
msg += `\n${JSON.stringify(debugValue.map(condenseArray))}`;
|
|
2093
|
+
return msg;
|
|
2094
|
+
}
|
|
2095
|
+
if (typeof debugValue !== 'object') {
|
|
2096
|
+
return (msg += ` ${debugValue}`);
|
|
2097
|
+
}
|
|
2098
|
+
msg += '\n{';
|
|
2099
|
+
const copy = klona(metadata);
|
|
2100
|
+
traverse(copy).forEach(function (value) {
|
|
2101
|
+
var _a;
|
|
2102
|
+
if (typeof (this === null || this === void 0 ? void 0 : this.key) === 'symbol') {
|
|
2103
|
+
return;
|
|
2104
|
+
}
|
|
2105
|
+
let _parentKey = '';
|
|
2106
|
+
const parent = this.parent;
|
|
2107
|
+
if (typeof (parent === null || parent === void 0 ? void 0 : parent.key) !== 'symbol' && (parent === null || parent === void 0 ? void 0 : parent.key)) {
|
|
2108
|
+
_parentKey = parent.key;
|
|
2109
|
+
}
|
|
2110
|
+
const parentKey = `${parent && parent.notRoot ? _parentKey + '.' : ''}`;
|
|
2111
|
+
const tabs = `${parent && parent.notRoot ? ' ' : ' '}`;
|
|
2112
|
+
const currentKey = (_a = this === null || this === void 0 ? void 0 : this.key) !== null && _a !== void 0 ? _a : 'unknown';
|
|
2113
|
+
if (this.isLeaf && typeof value === 'string') {
|
|
2114
|
+
const truncatedText = truncateLongStrings(value);
|
|
2115
|
+
msg += `\n${tabs}${parentKey}${currentKey}: ${JSON.stringify(truncatedText)},`;
|
|
2116
|
+
}
|
|
2117
|
+
else if (this.notLeaf && Array.isArray(value) && value.length > 0) {
|
|
2118
|
+
const currentMessage = `\n${tabs}// ${value.length} ${currentKey.replace(/s$/, '')}(s)`;
|
|
2119
|
+
this.update(currentMessage, true);
|
|
2120
|
+
msg += currentMessage;
|
|
2121
|
+
const stringifiedArray = value.map(condenseArray);
|
|
2122
|
+
msg += `\n${tabs}${parentKey}${currentKey}: [${stringifiedArray}],`;
|
|
2123
|
+
}
|
|
2124
|
+
else if (this.isLeaf && typeof value === 'function') {
|
|
2125
|
+
msg += `\n${tabs}${parentKey}${currentKey}: function,`;
|
|
2126
|
+
}
|
|
2127
|
+
else if (this.isLeaf) {
|
|
2128
|
+
msg += `\n${tabs}${parentKey}${currentKey}: ${value},`;
|
|
2129
|
+
}
|
|
2130
|
+
});
|
|
2131
|
+
msg += '\n}';
|
|
2132
|
+
return msg;
|
|
2133
|
+
}
|
|
2134
|
+
catch (e) {
|
|
2135
|
+
const errorMessage = e instanceof Error ? e.message : 'Unknown error';
|
|
2136
|
+
return (msg += `\n[LOGGER PARSING ERROR] ${errorMessage}`);
|
|
2137
|
+
}
|
|
2138
|
+
});
|
|
2139
|
+
/**
|
|
2140
|
+
* Truncates long string values in JSON log objects.
|
|
2141
|
+
* Prevents outputting extremely long values (e.g., base64, blobs).
|
|
2142
|
+
*/
|
|
2143
|
+
const jsonTruncateFormat = winston.format((info) => {
|
|
2144
|
+
const truncateLongStrings = (str, maxLength) => str.length > maxLength ? str.substring(0, maxLength) + '...' : str;
|
|
2145
|
+
const seen = new WeakSet();
|
|
2146
|
+
const truncateObject = (obj) => {
|
|
2147
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
2148
|
+
return obj;
|
|
2149
|
+
}
|
|
2150
|
+
// Handle circular references - now with proper object type
|
|
2151
|
+
if (seen.has(obj)) {
|
|
2152
|
+
return '[Circular]';
|
|
2153
|
+
}
|
|
2154
|
+
seen.add(obj);
|
|
2155
|
+
if (Array.isArray(obj)) {
|
|
2156
|
+
return obj.map((item) => truncateObject(item));
|
|
2157
|
+
}
|
|
2158
|
+
// We know this is an object at this point
|
|
2159
|
+
const objectRecord = obj;
|
|
2160
|
+
const newObj = {};
|
|
2161
|
+
Object.entries(objectRecord).forEach(([key, value]) => {
|
|
2162
|
+
if (typeof value === 'string') {
|
|
2163
|
+
newObj[key] = truncateLongStrings(value, CONSOLE_JSON_STRING_LENGTH);
|
|
2164
|
+
}
|
|
2165
|
+
else {
|
|
2166
|
+
newObj[key] = truncateObject(value);
|
|
2167
|
+
}
|
|
2168
|
+
});
|
|
2169
|
+
return newObj;
|
|
2170
|
+
};
|
|
2171
|
+
return truncateObject(info);
|
|
2172
|
+
});
|
|
2173
|
+
|
|
2174
|
+
// Define log directory
|
|
2175
|
+
const logDir = path.join(__dirname, '..', 'logs');
|
|
2176
|
+
// Type-safe environment variables
|
|
2177
|
+
const { NODE_ENV, DEBUG_LOGGING, CONSOLE_JSON, DEBUG_CONSOLE } = process.env;
|
|
2178
|
+
const useConsoleJson = typeof CONSOLE_JSON === 'string' && CONSOLE_JSON.toLowerCase() === 'true';
|
|
2179
|
+
const useDebugConsole = typeof DEBUG_CONSOLE === 'string' && DEBUG_CONSOLE.toLowerCase() === 'true';
|
|
2180
|
+
const useDebugLogging = typeof DEBUG_LOGGING === 'string' && DEBUG_LOGGING.toLowerCase() === 'true';
|
|
2181
|
+
// Define custom log levels
|
|
2182
|
+
const levels = {
|
|
2183
|
+
error: 0,
|
|
2184
|
+
warn: 1,
|
|
2185
|
+
info: 2,
|
|
2186
|
+
http: 3,
|
|
2187
|
+
verbose: 4,
|
|
2188
|
+
debug: 5,
|
|
2189
|
+
activity: 6,
|
|
2190
|
+
silly: 7,
|
|
2191
|
+
};
|
|
2192
|
+
winston.addColors({
|
|
2193
|
+
info: 'green',
|
|
2194
|
+
warn: 'italic yellow',
|
|
2195
|
+
error: 'red',
|
|
2196
|
+
debug: 'blue',
|
|
2197
|
+
});
|
|
2198
|
+
const level = () => {
|
|
2199
|
+
const env = NODE_ENV || 'development';
|
|
2200
|
+
return env === 'development' ? 'debug' : 'warn';
|
|
2201
|
+
};
|
|
2202
|
+
const fileFormat = winston.format.combine(redactFormat(), winston.format.timestamp({ format: () => new Date().toISOString() }), winston.format.errors({ stack: true }), winston.format.splat());
|
|
2203
|
+
const transports = [
|
|
2204
|
+
new winston.transports.DailyRotateFile({
|
|
2205
|
+
level: 'error',
|
|
2206
|
+
filename: `${logDir}/error-%DATE%.log`,
|
|
2207
|
+
datePattern: 'YYYY-MM-DD',
|
|
2208
|
+
zippedArchive: true,
|
|
2209
|
+
maxSize: '20m',
|
|
2210
|
+
maxFiles: '14d',
|
|
2211
|
+
format: fileFormat,
|
|
2212
|
+
}),
|
|
2213
|
+
];
|
|
2214
|
+
if (useDebugLogging) {
|
|
2215
|
+
transports.push(new winston.transports.DailyRotateFile({
|
|
2216
|
+
level: 'debug',
|
|
2217
|
+
filename: `${logDir}/debug-%DATE%.log`,
|
|
2218
|
+
datePattern: 'YYYY-MM-DD',
|
|
2219
|
+
zippedArchive: true,
|
|
2220
|
+
maxSize: '20m',
|
|
2221
|
+
maxFiles: '14d',
|
|
2222
|
+
format: winston.format.combine(fileFormat, debugTraverse),
|
|
2223
|
+
}));
|
|
2224
|
+
}
|
|
2225
|
+
const consoleFormat = winston.format.combine(redactFormat(), winston.format.colorize({ all: true }), winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), winston.format.printf((info) => {
|
|
2226
|
+
const message = `${info.timestamp} ${info.level}: ${info.message}`;
|
|
2227
|
+
return info.level.includes('error') ? redactMessage(message) : message;
|
|
2228
|
+
}));
|
|
2229
|
+
let consoleLogLevel = 'info';
|
|
2230
|
+
if (useDebugConsole) {
|
|
2231
|
+
consoleLogLevel = 'debug';
|
|
2232
|
+
}
|
|
2233
|
+
// Add console transport
|
|
2234
|
+
if (useDebugConsole) {
|
|
2235
|
+
transports.push(new winston.transports.Console({
|
|
2236
|
+
level: consoleLogLevel,
|
|
2237
|
+
format: useConsoleJson
|
|
2238
|
+
? winston.format.combine(fileFormat, jsonTruncateFormat(), winston.format.json())
|
|
2239
|
+
: winston.format.combine(fileFormat, debugTraverse),
|
|
2240
|
+
}));
|
|
2241
|
+
}
|
|
2242
|
+
else if (useConsoleJson) {
|
|
2243
|
+
transports.push(new winston.transports.Console({
|
|
2244
|
+
level: consoleLogLevel,
|
|
2245
|
+
format: winston.format.combine(fileFormat, jsonTruncateFormat(), winston.format.json()),
|
|
2246
|
+
}));
|
|
2247
|
+
}
|
|
2248
|
+
else {
|
|
2249
|
+
transports.push(new winston.transports.Console({
|
|
2250
|
+
level: consoleLogLevel,
|
|
2251
|
+
format: consoleFormat,
|
|
2252
|
+
}));
|
|
2253
|
+
}
|
|
2254
|
+
// Create logger
|
|
2255
|
+
const logger = winston.createLogger({
|
|
2256
|
+
level: level(),
|
|
2257
|
+
levels,
|
|
2258
|
+
transports,
|
|
2259
|
+
});
|
|
2260
|
+
|
|
2261
|
+
var _a, _b;
|
|
2262
|
+
class SessionError extends Error {
|
|
2263
|
+
constructor(message, code = 'SESSION_ERROR') {
|
|
2264
|
+
super(message);
|
|
2265
|
+
this.name = 'SessionError';
|
|
2266
|
+
this.code = code;
|
|
2267
|
+
}
|
|
2268
|
+
}
|
|
2269
|
+
const { REFRESH_TOKEN_EXPIRY } = (_a = process.env) !== null && _a !== void 0 ? _a : {};
|
|
2270
|
+
const expires = (_b = eval(REFRESH_TOKEN_EXPIRY !== null && REFRESH_TOKEN_EXPIRY !== void 0 ? REFRESH_TOKEN_EXPIRY : '0')) !== null && _b !== void 0 ? _b : 1000 * 60 * 60 * 24 * 7; // 7 days default
|
|
2271
|
+
// Factory function that takes mongoose instance and returns the methods
|
|
2272
|
+
function createSessionMethods(mongoose) {
|
|
2273
|
+
const Session = mongoose.models.Session;
|
|
2274
|
+
/**
|
|
2275
|
+
* Creates a new session for a user
|
|
2276
|
+
*/
|
|
2277
|
+
async function createSession(userId, options = {}) {
|
|
2278
|
+
if (!userId) {
|
|
2279
|
+
throw new SessionError('User ID is required', 'INVALID_USER_ID');
|
|
2280
|
+
}
|
|
2281
|
+
try {
|
|
2282
|
+
const session = new Session({
|
|
2283
|
+
user: userId,
|
|
2284
|
+
expiration: options.expiration || new Date(Date.now() + expires),
|
|
2285
|
+
});
|
|
2286
|
+
const refreshToken = await generateRefreshToken(session);
|
|
2287
|
+
return { session, refreshToken };
|
|
2288
|
+
}
|
|
2289
|
+
catch (error) {
|
|
2290
|
+
logger.error('[createSession] Error creating session:', error);
|
|
2291
|
+
throw new SessionError('Failed to create session', 'CREATE_SESSION_FAILED');
|
|
2292
|
+
}
|
|
2293
|
+
}
|
|
2294
|
+
/**
|
|
2295
|
+
* Finds a session by various parameters
|
|
2296
|
+
*/
|
|
2297
|
+
async function findSession(params, options = { lean: true }) {
|
|
2298
|
+
try {
|
|
2299
|
+
const query = {};
|
|
2300
|
+
if (!params.refreshToken && !params.userId && !params.sessionId) {
|
|
2301
|
+
throw new SessionError('At least one search parameter is required', 'INVALID_SEARCH_PARAMS');
|
|
2302
|
+
}
|
|
2303
|
+
if (params.refreshToken) {
|
|
2304
|
+
const tokenHash = await hashToken(params.refreshToken);
|
|
2305
|
+
query.refreshTokenHash = tokenHash;
|
|
2306
|
+
}
|
|
2307
|
+
if (params.userId) {
|
|
2308
|
+
query.user = params.userId;
|
|
2309
|
+
}
|
|
2310
|
+
if (params.sessionId) {
|
|
2311
|
+
const sessionId = typeof params.sessionId === 'object' &&
|
|
2312
|
+
params.sessionId !== null &&
|
|
2313
|
+
'sessionId' in params.sessionId
|
|
2314
|
+
? params.sessionId.sessionId
|
|
2315
|
+
: params.sessionId;
|
|
2316
|
+
if (!mongoose.Types.ObjectId.isValid(sessionId)) {
|
|
2317
|
+
throw new SessionError('Invalid session ID format', 'INVALID_SESSION_ID');
|
|
2318
|
+
}
|
|
2319
|
+
query._id = sessionId;
|
|
2320
|
+
}
|
|
2321
|
+
// Add expiration check to only return valid sessions
|
|
2322
|
+
query.expiration = { $gt: new Date() };
|
|
2323
|
+
const sessionQuery = Session.findOne(query);
|
|
2324
|
+
if (options.lean) {
|
|
2325
|
+
return (await sessionQuery.lean());
|
|
2326
|
+
}
|
|
2327
|
+
return await sessionQuery.exec();
|
|
2328
|
+
}
|
|
2329
|
+
catch (error) {
|
|
2330
|
+
logger.error('[findSession] Error finding session:', error);
|
|
2331
|
+
throw new SessionError('Failed to find session', 'FIND_SESSION_FAILED');
|
|
2332
|
+
}
|
|
2333
|
+
}
|
|
2334
|
+
/**
|
|
2335
|
+
* Updates session expiration
|
|
2336
|
+
*/
|
|
2337
|
+
async function updateExpiration(session, newExpiration) {
|
|
2338
|
+
try {
|
|
2339
|
+
const sessionDoc = typeof session === 'string' ? await Session.findById(session) : session;
|
|
2340
|
+
if (!sessionDoc) {
|
|
2341
|
+
throw new SessionError('Session not found', 'SESSION_NOT_FOUND');
|
|
2342
|
+
}
|
|
2343
|
+
sessionDoc.expiration = newExpiration || new Date(Date.now() + expires);
|
|
2344
|
+
return await sessionDoc.save();
|
|
2345
|
+
}
|
|
2346
|
+
catch (error) {
|
|
2347
|
+
logger.error('[updateExpiration] Error updating session:', error);
|
|
2348
|
+
throw new SessionError('Failed to update session expiration', 'UPDATE_EXPIRATION_FAILED');
|
|
2349
|
+
}
|
|
2350
|
+
}
|
|
2351
|
+
/**
|
|
2352
|
+
* Deletes a session by refresh token or session ID
|
|
2353
|
+
*/
|
|
2354
|
+
async function deleteSession(params) {
|
|
2355
|
+
try {
|
|
2356
|
+
if (!params.refreshToken && !params.sessionId) {
|
|
2357
|
+
throw new SessionError('Either refreshToken or sessionId is required', 'INVALID_DELETE_PARAMS');
|
|
2358
|
+
}
|
|
2359
|
+
const query = {};
|
|
2360
|
+
if (params.refreshToken) {
|
|
2361
|
+
query.refreshTokenHash = await hashToken(params.refreshToken);
|
|
2362
|
+
}
|
|
2363
|
+
if (params.sessionId) {
|
|
2364
|
+
query._id = params.sessionId;
|
|
2365
|
+
}
|
|
2366
|
+
const result = await Session.deleteOne(query);
|
|
2367
|
+
if (result.deletedCount === 0) {
|
|
2368
|
+
logger.warn('[deleteSession] No session found to delete');
|
|
2369
|
+
}
|
|
2370
|
+
return result;
|
|
2371
|
+
}
|
|
2372
|
+
catch (error) {
|
|
2373
|
+
logger.error('[deleteSession] Error deleting session:', error);
|
|
2374
|
+
throw new SessionError('Failed to delete session', 'DELETE_SESSION_FAILED');
|
|
2375
|
+
}
|
|
2376
|
+
}
|
|
2377
|
+
/**
|
|
2378
|
+
* Deletes all sessions for a user
|
|
2379
|
+
*/
|
|
2380
|
+
async function deleteAllUserSessions(userId, options = {}) {
|
|
2381
|
+
try {
|
|
2382
|
+
if (!userId) {
|
|
2383
|
+
throw new SessionError('User ID is required', 'INVALID_USER_ID');
|
|
2384
|
+
}
|
|
2385
|
+
const userIdString = typeof userId === 'object' && userId !== null ? userId.userId : userId;
|
|
2386
|
+
if (!mongoose.Types.ObjectId.isValid(userIdString)) {
|
|
2387
|
+
throw new SessionError('Invalid user ID format', 'INVALID_USER_ID_FORMAT');
|
|
2388
|
+
}
|
|
2389
|
+
const query = { user: userIdString };
|
|
2390
|
+
if (options.excludeCurrentSession && options.currentSessionId) {
|
|
2391
|
+
query._id = { $ne: options.currentSessionId };
|
|
2392
|
+
}
|
|
2393
|
+
const result = await Session.deleteMany(query);
|
|
2394
|
+
if (result.deletedCount && result.deletedCount > 0) {
|
|
2395
|
+
logger.debug(`[deleteAllUserSessions] Deleted ${result.deletedCount} sessions for user ${userIdString}.`);
|
|
2396
|
+
}
|
|
2397
|
+
return result;
|
|
2398
|
+
}
|
|
2399
|
+
catch (error) {
|
|
2400
|
+
logger.error('[deleteAllUserSessions] Error deleting user sessions:', error);
|
|
2401
|
+
throw new SessionError('Failed to delete user sessions', 'DELETE_ALL_SESSIONS_FAILED');
|
|
2402
|
+
}
|
|
2403
|
+
}
|
|
2404
|
+
/**
|
|
2405
|
+
* Generates a refresh token for a session
|
|
2406
|
+
*/
|
|
2407
|
+
async function generateRefreshToken(session) {
|
|
2408
|
+
if (!session || !session.user) {
|
|
2409
|
+
throw new SessionError('Invalid session object', 'INVALID_SESSION');
|
|
2410
|
+
}
|
|
2411
|
+
try {
|
|
2412
|
+
const expiresIn = session.expiration ? session.expiration.getTime() : Date.now() + expires;
|
|
2413
|
+
if (!session.expiration) {
|
|
2414
|
+
session.expiration = new Date(expiresIn);
|
|
2415
|
+
}
|
|
2416
|
+
const refreshToken = await signPayload({
|
|
2417
|
+
payload: {
|
|
2418
|
+
id: session.user,
|
|
2419
|
+
sessionId: session._id,
|
|
2420
|
+
},
|
|
2421
|
+
secret: process.env.JWT_REFRESH_SECRET,
|
|
2422
|
+
expirationTime: Math.floor((expiresIn - Date.now()) / 1000),
|
|
2423
|
+
});
|
|
2424
|
+
session.refreshTokenHash = await hashToken(refreshToken);
|
|
2425
|
+
await session.save();
|
|
2426
|
+
return refreshToken;
|
|
2427
|
+
}
|
|
2428
|
+
catch (error) {
|
|
2429
|
+
logger.error('[generateRefreshToken] Error generating refresh token:', error);
|
|
2430
|
+
throw new SessionError('Failed to generate refresh token', 'GENERATE_TOKEN_FAILED');
|
|
2431
|
+
}
|
|
2432
|
+
}
|
|
2433
|
+
/**
|
|
2434
|
+
* Counts active sessions for a user
|
|
2435
|
+
*/
|
|
2436
|
+
async function countActiveSessions(userId) {
|
|
2437
|
+
try {
|
|
2438
|
+
if (!userId) {
|
|
2439
|
+
throw new SessionError('User ID is required', 'INVALID_USER_ID');
|
|
2440
|
+
}
|
|
2441
|
+
return await Session.countDocuments({
|
|
2442
|
+
user: userId,
|
|
2443
|
+
expiration: { $gt: new Date() },
|
|
2444
|
+
});
|
|
2445
|
+
}
|
|
2446
|
+
catch (error) {
|
|
2447
|
+
logger.error('[countActiveSessions] Error counting active sessions:', error);
|
|
2448
|
+
throw new SessionError('Failed to count active sessions', 'COUNT_SESSIONS_FAILED');
|
|
2449
|
+
}
|
|
2450
|
+
}
|
|
2451
|
+
return {
|
|
2452
|
+
findSession,
|
|
2453
|
+
SessionError,
|
|
2454
|
+
deleteSession,
|
|
2455
|
+
createSession,
|
|
2456
|
+
updateExpiration,
|
|
2457
|
+
countActiveSessions,
|
|
2458
|
+
generateRefreshToken,
|
|
2459
|
+
deleteAllUserSessions,
|
|
2460
|
+
};
|
|
2461
|
+
}
|
|
2462
|
+
|
|
2463
|
+
// Factory function that takes mongoose instance and returns the methods
|
|
2464
|
+
function createTokenMethods(mongoose) {
|
|
2465
|
+
/**
|
|
2466
|
+
* Creates a new Token instance.
|
|
2467
|
+
*/
|
|
2468
|
+
async function createToken(tokenData) {
|
|
2469
|
+
try {
|
|
2470
|
+
const Token = mongoose.models.Token;
|
|
2471
|
+
const currentTime = new Date();
|
|
2472
|
+
const expiresAt = new Date(currentTime.getTime() + tokenData.expiresIn * 1000);
|
|
2473
|
+
const newTokenData = {
|
|
2474
|
+
...tokenData,
|
|
2475
|
+
createdAt: currentTime,
|
|
2476
|
+
expiresAt,
|
|
2477
|
+
};
|
|
2478
|
+
return await Token.create(newTokenData);
|
|
2479
|
+
}
|
|
2480
|
+
catch (error) {
|
|
2481
|
+
logger.debug('An error occurred while creating token:', error);
|
|
2482
|
+
throw error;
|
|
2483
|
+
}
|
|
2484
|
+
}
|
|
2485
|
+
/**
|
|
2486
|
+
* Updates a Token document that matches the provided query.
|
|
2487
|
+
*/
|
|
2488
|
+
async function updateToken(query, updateData) {
|
|
2489
|
+
try {
|
|
2490
|
+
const Token = mongoose.models.Token;
|
|
2491
|
+
return await Token.findOneAndUpdate(query, updateData, { new: true });
|
|
2492
|
+
}
|
|
2493
|
+
catch (error) {
|
|
2494
|
+
logger.debug('An error occurred while updating token:', error);
|
|
2495
|
+
throw error;
|
|
2496
|
+
}
|
|
2497
|
+
}
|
|
2498
|
+
/**
|
|
2499
|
+
* Deletes all Token documents that match the provided token, user ID, or email.
|
|
2500
|
+
*/
|
|
2501
|
+
async function deleteTokens(query) {
|
|
2502
|
+
try {
|
|
2503
|
+
const Token = mongoose.models.Token;
|
|
2504
|
+
return await Token.deleteMany({
|
|
2505
|
+
$or: [
|
|
2506
|
+
{ userId: query.userId },
|
|
2507
|
+
{ token: query.token },
|
|
2508
|
+
{ email: query.email },
|
|
2509
|
+
{ identifier: query.identifier },
|
|
2510
|
+
],
|
|
2511
|
+
});
|
|
2512
|
+
}
|
|
2513
|
+
catch (error) {
|
|
2514
|
+
logger.debug('An error occurred while deleting tokens:', error);
|
|
2515
|
+
throw error;
|
|
2516
|
+
}
|
|
2517
|
+
}
|
|
2518
|
+
/**
|
|
2519
|
+
* Finds a Token document that matches the provided query.
|
|
2520
|
+
*/
|
|
2521
|
+
async function findToken(query) {
|
|
2522
|
+
try {
|
|
2523
|
+
const Token = mongoose.models.Token;
|
|
2524
|
+
const conditions = [];
|
|
2525
|
+
if (query.userId) {
|
|
2526
|
+
conditions.push({ userId: query.userId });
|
|
2527
|
+
}
|
|
2528
|
+
if (query.token) {
|
|
2529
|
+
conditions.push({ token: query.token });
|
|
2530
|
+
}
|
|
2531
|
+
if (query.email) {
|
|
2532
|
+
conditions.push({ email: query.email });
|
|
2533
|
+
}
|
|
2534
|
+
if (query.identifier) {
|
|
2535
|
+
conditions.push({ identifier: query.identifier });
|
|
2536
|
+
}
|
|
2537
|
+
const token = await Token.findOne({
|
|
2538
|
+
$and: conditions,
|
|
2539
|
+
}).lean();
|
|
2540
|
+
return token;
|
|
2541
|
+
}
|
|
2542
|
+
catch (error) {
|
|
2543
|
+
logger.debug('An error occurred while finding token:', error);
|
|
2544
|
+
throw error;
|
|
2545
|
+
}
|
|
2546
|
+
}
|
|
2547
|
+
// Return all methods
|
|
2548
|
+
return {
|
|
2549
|
+
findToken,
|
|
2550
|
+
createToken,
|
|
2551
|
+
updateToken,
|
|
2552
|
+
deleteTokens,
|
|
2553
|
+
};
|
|
2554
|
+
}
|
|
2555
|
+
|
|
2556
|
+
// Factory function that takes mongoose instance and returns the methods
|
|
2557
|
+
function createRoleMethods(mongoose) {
|
|
2558
|
+
/**
|
|
2559
|
+
* Initialize default roles in the system.
|
|
2560
|
+
* Creates the default roles (ADMIN, USER) if they don't exist in the database.
|
|
2561
|
+
* Updates existing roles with new permission types if they're missing.
|
|
2562
|
+
*/
|
|
2563
|
+
async function initializeRoles() {
|
|
2564
|
+
const Role = mongoose.models.Role;
|
|
2565
|
+
for (const roleName of [SystemRoles.ADMIN, SystemRoles.USER]) {
|
|
2566
|
+
let role = await Role.findOne({ name: roleName });
|
|
2567
|
+
const defaultPerms = roleDefaults[roleName].permissions;
|
|
2568
|
+
if (!role) {
|
|
2569
|
+
// Create new role if it doesn't exist.
|
|
2570
|
+
role = new Role(roleDefaults[roleName]);
|
|
2571
|
+
}
|
|
2572
|
+
else {
|
|
2573
|
+
// Ensure role.permissions is defined.
|
|
2574
|
+
role.permissions = role.permissions || {};
|
|
2575
|
+
// For each permission type in defaults, add it if missing.
|
|
2576
|
+
for (const permType of Object.keys(defaultPerms)) {
|
|
2577
|
+
if (role.permissions[permType] == null) {
|
|
2578
|
+
role.permissions[permType] = defaultPerms[permType];
|
|
2579
|
+
}
|
|
2580
|
+
}
|
|
2581
|
+
}
|
|
2582
|
+
await role.save();
|
|
2583
|
+
}
|
|
2584
|
+
}
|
|
2585
|
+
/**
|
|
2586
|
+
* List all roles in the system (for testing purposes)
|
|
2587
|
+
* Returns an array of all roles with their names and permissions
|
|
2588
|
+
*/
|
|
2589
|
+
async function listRoles() {
|
|
2590
|
+
const Role = mongoose.models.Role;
|
|
2591
|
+
return await Role.find({}).select('name permissions').lean();
|
|
2592
|
+
}
|
|
2593
|
+
// Return all methods you want to expose
|
|
2594
|
+
return {
|
|
2595
|
+
listRoles,
|
|
2596
|
+
initializeRoles,
|
|
2597
|
+
};
|
|
2598
|
+
}
|
|
2599
|
+
|
|
2600
|
+
/**
|
|
2601
|
+
* Creates all database methods for all collections
|
|
2602
|
+
*/
|
|
2603
|
+
function createMethods(mongoose) {
|
|
2604
|
+
return {
|
|
2605
|
+
...createUserMethods(mongoose),
|
|
2606
|
+
...createSessionMethods(mongoose),
|
|
2607
|
+
...createTokenMethods(mongoose),
|
|
2608
|
+
...createRoleMethods(mongoose),
|
|
2609
|
+
};
|
|
2610
|
+
}
|
|
2611
|
+
|
|
2612
|
+
export { Action as actionSchema, agentSchema, assistantSchema, balanceSchema, bannerSchema, categoriesSchema, conversationTag as conversationTagSchema, convoSchema, createMethods, createModels, file as fileSchema, hashToken, keySchema, logger, logger$1 as meiliLogger, messageSchema, pluginAuthSchema, presetSchema, projectSchema, promptGroupSchema, promptSchema, roleSchema, sessionSchema, shareSchema, signPayload, tokenSchema, toolCallSchema, transactionSchema, userSchema };
|
|
1152
2613
|
//# sourceMappingURL=index.es.js.map
|