@mastra/mongodb 1.2.0 → 1.3.0-alpha.1
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/CHANGELOG.md +96 -0
- package/dist/docs/SKILL.md +17 -23
- package/dist/docs/{SOURCE_MAP.json → assets/SOURCE_MAP.json} +1 -1
- package/dist/docs/{memory/01-working-memory.md → references/docs-memory-working-memory.md} +16 -27
- package/dist/docs/{rag/02-retrieval.md → references/docs-rag-retrieval.md} +26 -53
- package/dist/docs/{rag/01-vector-databases.md → references/docs-rag-vector-databases.md} +198 -202
- package/dist/docs/{storage/01-reference.md → references/reference-storage-mongodb.md} +34 -15
- package/dist/docs/{vectors/01-reference.md → references/reference-vectors-mongodb.md} +108 -14
- package/dist/index.cjs +1705 -236
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1705 -238
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/agents/index.d.ts +7 -12
- package/dist/storage/domains/agents/index.d.ts.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +8 -2
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/prompt-blocks/index.d.ts +35 -0
- package/dist/storage/domains/prompt-blocks/index.d.ts.map +1 -0
- package/dist/storage/domains/scorer-definitions/index.d.ts +35 -0
- package/dist/storage/domains/scorer-definitions/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +3 -1
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +3 -4
- package/dist/docs/README.md +0 -34
package/dist/index.cjs
CHANGED
|
@@ -14,7 +14,7 @@ var evals = require('@mastra/core/evals');
|
|
|
14
14
|
|
|
15
15
|
// package.json
|
|
16
16
|
var package_default = {
|
|
17
|
-
version: "1.
|
|
17
|
+
version: "1.3.0-alpha.1"};
|
|
18
18
|
var MongoDBFilterTranslator = class extends filter.BaseFilterTranslator {
|
|
19
19
|
getSupportedOperators() {
|
|
20
20
|
return {
|
|
@@ -973,6 +973,42 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends storage.AgentsSto
|
|
|
973
973
|
async init() {
|
|
974
974
|
await this.createDefaultIndexes();
|
|
975
975
|
await this.createCustomIndexes();
|
|
976
|
+
await this.#migrateToolsToJsonbFormat();
|
|
977
|
+
}
|
|
978
|
+
/**
|
|
979
|
+
* Migrates the tools field from string[] format to object format { "tool-key": { "description": "..." } }.
|
|
980
|
+
* This handles the transition from the old format where tools were stored as an array of string keys
|
|
981
|
+
* to the new format where tools can have per-agent description overrides.
|
|
982
|
+
*/
|
|
983
|
+
async #migrateToolsToJsonbFormat() {
|
|
984
|
+
try {
|
|
985
|
+
const versionsCollection = await this.getCollection(storage.TABLE_AGENT_VERSIONS);
|
|
986
|
+
const cursor = versionsCollection.find({ tools: { $type: "array" } });
|
|
987
|
+
const updates = [];
|
|
988
|
+
for await (const doc of cursor) {
|
|
989
|
+
if (Array.isArray(doc.tools)) {
|
|
990
|
+
const toolsObject = {};
|
|
991
|
+
for (const toolKey of doc.tools) {
|
|
992
|
+
if (typeof toolKey === "string") {
|
|
993
|
+
toolsObject[toolKey] = {};
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
updates.push({ id: doc.id, tools: toolsObject });
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
if (updates.length > 0) {
|
|
1000
|
+
const bulkOps = updates.map((update) => ({
|
|
1001
|
+
updateOne: {
|
|
1002
|
+
filter: { id: update.id },
|
|
1003
|
+
update: { $set: { tools: update.tools } }
|
|
1004
|
+
}
|
|
1005
|
+
}));
|
|
1006
|
+
await versionsCollection.bulkWrite(bulkOps);
|
|
1007
|
+
this.logger?.info?.(`Migrated ${updates.length} agent version tools from array to object format`);
|
|
1008
|
+
}
|
|
1009
|
+
} catch (error) {
|
|
1010
|
+
this.logger?.warn?.("Failed to migrate tools to object format:", error);
|
|
1011
|
+
}
|
|
976
1012
|
}
|
|
977
1013
|
async dangerouslyClearAll() {
|
|
978
1014
|
const versionsCollection = await this.getCollection(storage.TABLE_AGENT_VERSIONS);
|
|
@@ -980,7 +1016,7 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends storage.AgentsSto
|
|
|
980
1016
|
const agentsCollection = await this.getCollection(storage.TABLE_AGENTS);
|
|
981
1017
|
await agentsCollection.deleteMany({});
|
|
982
1018
|
}
|
|
983
|
-
async
|
|
1019
|
+
async getById(id) {
|
|
984
1020
|
try {
|
|
985
1021
|
const collection = await this.getCollection(storage.TABLE_AGENTS);
|
|
986
1022
|
const result = await collection.findOne({ id });
|
|
@@ -1000,7 +1036,8 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends storage.AgentsSto
|
|
|
1000
1036
|
);
|
|
1001
1037
|
}
|
|
1002
1038
|
}
|
|
1003
|
-
async
|
|
1039
|
+
async create(input) {
|
|
1040
|
+
const { agent } = input;
|
|
1004
1041
|
try {
|
|
1005
1042
|
const collection = await this.getCollection(storage.TABLE_AGENTS);
|
|
1006
1043
|
const existing = await collection.findOne({ id: agent.id });
|
|
@@ -1050,7 +1087,8 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends storage.AgentsSto
|
|
|
1050
1087
|
);
|
|
1051
1088
|
}
|
|
1052
1089
|
}
|
|
1053
|
-
async
|
|
1090
|
+
async update(input) {
|
|
1091
|
+
const { id, ...updates } = input;
|
|
1054
1092
|
try {
|
|
1055
1093
|
const collection = await this.getCollection(storage.TABLE_AGENTS);
|
|
1056
1094
|
const existingAgent = await collection.findOne({ id });
|
|
@@ -1089,14 +1127,17 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends storage.AgentsSto
|
|
|
1089
1127
|
details: { id }
|
|
1090
1128
|
});
|
|
1091
1129
|
}
|
|
1130
|
+
const sanitizedConfigFields = Object.fromEntries(
|
|
1131
|
+
Object.entries(configFields).map(([key, value]) => [key, value === null ? void 0 : value])
|
|
1132
|
+
);
|
|
1092
1133
|
const versionInput = {
|
|
1093
1134
|
id: crypto.randomUUID(),
|
|
1094
1135
|
agentId: id,
|
|
1095
1136
|
versionNumber: nextVersionNumber,
|
|
1096
1137
|
...this.extractSnapshotFields(latestVersion),
|
|
1097
1138
|
// Start from latest version
|
|
1098
|
-
...
|
|
1099
|
-
// Apply updates
|
|
1139
|
+
...sanitizedConfigFields,
|
|
1140
|
+
// Apply updates (null values converted to undefined)
|
|
1100
1141
|
changedFields: Object.keys(configFields),
|
|
1101
1142
|
changeMessage: `Updated: ${Object.keys(configFields).join(", ")}`
|
|
1102
1143
|
};
|
|
@@ -1137,9 +1178,9 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends storage.AgentsSto
|
|
|
1137
1178
|
);
|
|
1138
1179
|
}
|
|
1139
1180
|
}
|
|
1140
|
-
async
|
|
1181
|
+
async delete(id) {
|
|
1141
1182
|
try {
|
|
1142
|
-
await this.
|
|
1183
|
+
await this.deleteVersionsByParentId(id);
|
|
1143
1184
|
const collection = await this.getCollection(storage.TABLE_AGENTS);
|
|
1144
1185
|
await collection.deleteOne({ id });
|
|
1145
1186
|
} catch (error$1) {
|
|
@@ -1154,7 +1195,7 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends storage.AgentsSto
|
|
|
1154
1195
|
);
|
|
1155
1196
|
}
|
|
1156
1197
|
}
|
|
1157
|
-
async
|
|
1198
|
+
async list(args) {
|
|
1158
1199
|
try {
|
|
1159
1200
|
const { page = 0, perPage: perPageInput, orderBy } = args || {};
|
|
1160
1201
|
const { field, direction } = this.parseOrderBy(orderBy);
|
|
@@ -1210,123 +1251,6 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends storage.AgentsSto
|
|
|
1210
1251
|
);
|
|
1211
1252
|
}
|
|
1212
1253
|
}
|
|
1213
|
-
async listAgentsResolved(args) {
|
|
1214
|
-
try {
|
|
1215
|
-
const { page = 0, perPage: perPageInput, orderBy } = args || {};
|
|
1216
|
-
const { field, direction } = this.parseOrderBy(orderBy);
|
|
1217
|
-
if (page < 0) {
|
|
1218
|
-
throw new error.MastraError(
|
|
1219
|
-
{
|
|
1220
|
-
id: storage.createStorageErrorId("MONGODB", "LIST_AGENTS_RESOLVED", "INVALID_PAGE"),
|
|
1221
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1222
|
-
category: error.ErrorCategory.USER,
|
|
1223
|
-
details: { page }
|
|
1224
|
-
},
|
|
1225
|
-
new Error("page must be >= 0")
|
|
1226
|
-
);
|
|
1227
|
-
}
|
|
1228
|
-
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1229
|
-
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1230
|
-
const agentsCollection = await this.getCollection(storage.TABLE_AGENTS);
|
|
1231
|
-
const total = await agentsCollection.countDocuments({});
|
|
1232
|
-
if (total === 0 || perPage === 0) {
|
|
1233
|
-
return {
|
|
1234
|
-
agents: [],
|
|
1235
|
-
total,
|
|
1236
|
-
page,
|
|
1237
|
-
perPage: perPageForResponse,
|
|
1238
|
-
hasMore: false
|
|
1239
|
-
};
|
|
1240
|
-
}
|
|
1241
|
-
const sortOrder = direction === "ASC" ? 1 : -1;
|
|
1242
|
-
const pipeline = [
|
|
1243
|
-
// Sort agents
|
|
1244
|
-
{ $sort: { [field]: sortOrder } },
|
|
1245
|
-
// Paginate
|
|
1246
|
-
{ $skip: offset }
|
|
1247
|
-
];
|
|
1248
|
-
if (perPageInput !== false) {
|
|
1249
|
-
pipeline.push({ $limit: perPage });
|
|
1250
|
-
}
|
|
1251
|
-
pipeline.push({
|
|
1252
|
-
$lookup: {
|
|
1253
|
-
from: storage.TABLE_AGENT_VERSIONS,
|
|
1254
|
-
localField: "activeVersionId",
|
|
1255
|
-
foreignField: "id",
|
|
1256
|
-
as: "activeVersion"
|
|
1257
|
-
}
|
|
1258
|
-
});
|
|
1259
|
-
pipeline.push({
|
|
1260
|
-
$lookup: {
|
|
1261
|
-
from: storage.TABLE_AGENT_VERSIONS,
|
|
1262
|
-
let: { agentId: "$id" },
|
|
1263
|
-
pipeline: [
|
|
1264
|
-
{ $match: { $expr: { $eq: ["$agentId", "$$agentId"] } } },
|
|
1265
|
-
{ $sort: { versionNumber: -1 } },
|
|
1266
|
-
{ $limit: 1 }
|
|
1267
|
-
],
|
|
1268
|
-
as: "latestVersion"
|
|
1269
|
-
}
|
|
1270
|
-
});
|
|
1271
|
-
pipeline.push({
|
|
1272
|
-
$addFields: {
|
|
1273
|
-
version: {
|
|
1274
|
-
$cond: {
|
|
1275
|
-
if: { $gt: [{ $size: "$activeVersion" }, 0] },
|
|
1276
|
-
then: { $arrayElemAt: ["$activeVersion", 0] },
|
|
1277
|
-
else: { $arrayElemAt: ["$latestVersion", 0] }
|
|
1278
|
-
}
|
|
1279
|
-
}
|
|
1280
|
-
}
|
|
1281
|
-
});
|
|
1282
|
-
pipeline.push({
|
|
1283
|
-
$project: {
|
|
1284
|
-
activeVersion: 0,
|
|
1285
|
-
latestVersion: 0
|
|
1286
|
-
}
|
|
1287
|
-
});
|
|
1288
|
-
const results = await agentsCollection.aggregate(pipeline).toArray();
|
|
1289
|
-
const resolvedAgents = results.map((doc) => {
|
|
1290
|
-
const agent = this.transformAgent(doc);
|
|
1291
|
-
if (doc.version) {
|
|
1292
|
-
const version = this.transformVersion(doc.version);
|
|
1293
|
-
const {
|
|
1294
|
-
id: _versionId,
|
|
1295
|
-
agentId: _agentId,
|
|
1296
|
-
versionNumber: _versionNumber,
|
|
1297
|
-
changedFields: _changedFields,
|
|
1298
|
-
changeMessage: _changeMessage,
|
|
1299
|
-
createdAt: _createdAt,
|
|
1300
|
-
...snapshotConfig
|
|
1301
|
-
} = version;
|
|
1302
|
-
return {
|
|
1303
|
-
...agent,
|
|
1304
|
-
...snapshotConfig
|
|
1305
|
-
};
|
|
1306
|
-
}
|
|
1307
|
-
return agent;
|
|
1308
|
-
});
|
|
1309
|
-
return {
|
|
1310
|
-
agents: resolvedAgents,
|
|
1311
|
-
total,
|
|
1312
|
-
page,
|
|
1313
|
-
perPage: perPageForResponse,
|
|
1314
|
-
hasMore: perPageInput !== false && offset + perPage < total
|
|
1315
|
-
};
|
|
1316
|
-
} catch (error$1) {
|
|
1317
|
-
if (error$1 instanceof error.MastraError) {
|
|
1318
|
-
throw error$1;
|
|
1319
|
-
}
|
|
1320
|
-
throw new error.MastraError(
|
|
1321
|
-
{
|
|
1322
|
-
id: storage.createStorageErrorId("MONGODB", "LIST_AGENTS_RESOLVED", "FAILED"),
|
|
1323
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1324
|
-
category: error.ErrorCategory.THIRD_PARTY
|
|
1325
|
-
},
|
|
1326
|
-
error$1
|
|
1327
|
-
);
|
|
1328
|
-
}
|
|
1329
|
-
}
|
|
1330
1254
|
/**
|
|
1331
1255
|
* Transforms a raw MongoDB document into a thin StorageAgentType record.
|
|
1332
1256
|
* Only returns metadata-level fields (no config/snapshot fields).
|
|
@@ -1525,7 +1449,7 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends storage.AgentsSto
|
|
|
1525
1449
|
);
|
|
1526
1450
|
}
|
|
1527
1451
|
}
|
|
1528
|
-
async
|
|
1452
|
+
async deleteVersionsByParentId(agentId) {
|
|
1529
1453
|
try {
|
|
1530
1454
|
const collection = await this.getCollection(storage.TABLE_AGENT_VERSIONS);
|
|
1531
1455
|
await collection.deleteMany({ agentId });
|
|
@@ -2460,12 +2384,26 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends storage.MemorySto
|
|
|
2460
2384
|
originType: doc.originType || "initial",
|
|
2461
2385
|
generationCount: Number(doc.generationCount || 0),
|
|
2462
2386
|
activeObservations: doc.activeObservations || "",
|
|
2387
|
+
// Handle new chunk-based structure
|
|
2388
|
+
bufferedObservationChunks: doc.bufferedObservationChunks || void 0,
|
|
2389
|
+
// Deprecated fields (for backward compatibility)
|
|
2463
2390
|
bufferedObservations: doc.activeObservationsPendingUpdate || void 0,
|
|
2391
|
+
bufferedObservationTokens: doc.bufferedObservationTokens ? Number(doc.bufferedObservationTokens) : void 0,
|
|
2392
|
+
bufferedMessageIds: void 0,
|
|
2393
|
+
// Use bufferedObservationChunks instead
|
|
2394
|
+
bufferedReflection: doc.bufferedReflection || void 0,
|
|
2395
|
+
bufferedReflectionTokens: doc.bufferedReflectionTokens ? Number(doc.bufferedReflectionTokens) : void 0,
|
|
2396
|
+
bufferedReflectionInputTokens: doc.bufferedReflectionInputTokens ? Number(doc.bufferedReflectionInputTokens) : void 0,
|
|
2397
|
+
reflectedObservationLineCount: doc.reflectedObservationLineCount ? Number(doc.reflectedObservationLineCount) : void 0,
|
|
2464
2398
|
totalTokensObserved: Number(doc.totalTokensObserved || 0),
|
|
2465
2399
|
observationTokenCount: Number(doc.observationTokenCount || 0),
|
|
2466
2400
|
pendingMessageTokens: Number(doc.pendingMessageTokens || 0),
|
|
2467
2401
|
isReflecting: Boolean(doc.isReflecting),
|
|
2468
2402
|
isObserving: Boolean(doc.isObserving),
|
|
2403
|
+
isBufferingObservation: Boolean(doc.isBufferingObservation),
|
|
2404
|
+
isBufferingReflection: Boolean(doc.isBufferingReflection),
|
|
2405
|
+
lastBufferedAtTokens: typeof doc.lastBufferedAtTokens === "number" ? doc.lastBufferedAtTokens : parseInt(String(doc.lastBufferedAtTokens ?? "0"), 10) || 0,
|
|
2406
|
+
lastBufferedAtTime: doc.lastBufferedAtTime ? new Date(doc.lastBufferedAtTime) : null,
|
|
2469
2407
|
config: doc.config || {},
|
|
2470
2408
|
metadata: doc.metadata || void 0,
|
|
2471
2409
|
observedMessageIds: doc.observedMessageIds || void 0,
|
|
@@ -2530,6 +2468,10 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends storage.MemorySto
|
|
|
2530
2468
|
pendingMessageTokens: 0,
|
|
2531
2469
|
isReflecting: false,
|
|
2532
2470
|
isObserving: false,
|
|
2471
|
+
isBufferingObservation: false,
|
|
2472
|
+
isBufferingReflection: false,
|
|
2473
|
+
lastBufferedAtTokens: 0,
|
|
2474
|
+
lastBufferedAtTime: null,
|
|
2533
2475
|
config: input.config,
|
|
2534
2476
|
observedTimezone: input.observedTimezone
|
|
2535
2477
|
};
|
|
@@ -2552,6 +2494,10 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends storage.MemorySto
|
|
|
2552
2494
|
observationTokenCount: 0,
|
|
2553
2495
|
isObserving: false,
|
|
2554
2496
|
isReflecting: false,
|
|
2497
|
+
isBufferingObservation: false,
|
|
2498
|
+
isBufferingReflection: false,
|
|
2499
|
+
lastBufferedAtTokens: 0,
|
|
2500
|
+
lastBufferedAtTime: null,
|
|
2555
2501
|
observedTimezone: input.observedTimezone || null,
|
|
2556
2502
|
createdAt: now,
|
|
2557
2503
|
updatedAt: now
|
|
@@ -2634,6 +2580,10 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends storage.MemorySto
|
|
|
2634
2580
|
pendingMessageTokens: 0,
|
|
2635
2581
|
isReflecting: false,
|
|
2636
2582
|
isObserving: false,
|
|
2583
|
+
isBufferingObservation: false,
|
|
2584
|
+
isBufferingReflection: false,
|
|
2585
|
+
lastBufferedAtTokens: 0,
|
|
2586
|
+
lastBufferedAtTime: null,
|
|
2637
2587
|
config: input.currentRecord.config,
|
|
2638
2588
|
metadata: input.currentRecord.metadata,
|
|
2639
2589
|
observedTimezone: input.currentRecord.observedTimezone
|
|
@@ -2657,6 +2607,10 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends storage.MemorySto
|
|
|
2657
2607
|
observationTokenCount: record.observationTokenCount,
|
|
2658
2608
|
isObserving: false,
|
|
2659
2609
|
isReflecting: false,
|
|
2610
|
+
isBufferingObservation: false,
|
|
2611
|
+
isBufferingReflection: false,
|
|
2612
|
+
lastBufferedAtTokens: 0,
|
|
2613
|
+
lastBufferedAtTime: null,
|
|
2660
2614
|
observedTimezone: record.observedTimezone || null,
|
|
2661
2615
|
createdAt: now,
|
|
2662
2616
|
updatedAt: now,
|
|
@@ -2731,6 +2685,72 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends storage.MemorySto
|
|
|
2731
2685
|
);
|
|
2732
2686
|
}
|
|
2733
2687
|
}
|
|
2688
|
+
async setBufferingObservationFlag(id, isBuffering, lastBufferedAtTokens) {
|
|
2689
|
+
try {
|
|
2690
|
+
const collection = await this.getCollection(OM_TABLE);
|
|
2691
|
+
const updateDoc = {
|
|
2692
|
+
isBufferingObservation: isBuffering,
|
|
2693
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
2694
|
+
};
|
|
2695
|
+
if (lastBufferedAtTokens !== void 0) {
|
|
2696
|
+
updateDoc.lastBufferedAtTokens = lastBufferedAtTokens;
|
|
2697
|
+
}
|
|
2698
|
+
const result = await collection.updateOne({ id }, { $set: updateDoc });
|
|
2699
|
+
if (result.matchedCount === 0) {
|
|
2700
|
+
throw new error.MastraError({
|
|
2701
|
+
id: storage.createStorageErrorId("MONGODB", "SET_BUFFERING_OBSERVATION_FLAG", "NOT_FOUND"),
|
|
2702
|
+
text: `Observational memory record not found: ${id}`,
|
|
2703
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2704
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2705
|
+
details: { id, isBuffering, lastBufferedAtTokens: lastBufferedAtTokens ?? null }
|
|
2706
|
+
});
|
|
2707
|
+
}
|
|
2708
|
+
} catch (error$1) {
|
|
2709
|
+
if (error$1 instanceof error.MastraError) {
|
|
2710
|
+
throw error$1;
|
|
2711
|
+
}
|
|
2712
|
+
throw new error.MastraError(
|
|
2713
|
+
{
|
|
2714
|
+
id: storage.createStorageErrorId("MONGODB", "SET_BUFFERING_OBSERVATION_FLAG", "FAILED"),
|
|
2715
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2716
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2717
|
+
details: { id, isBuffering, lastBufferedAtTokens: lastBufferedAtTokens ?? null }
|
|
2718
|
+
},
|
|
2719
|
+
error$1
|
|
2720
|
+
);
|
|
2721
|
+
}
|
|
2722
|
+
}
|
|
2723
|
+
async setBufferingReflectionFlag(id, isBuffering) {
|
|
2724
|
+
try {
|
|
2725
|
+
const collection = await this.getCollection(OM_TABLE);
|
|
2726
|
+
const result = await collection.updateOne(
|
|
2727
|
+
{ id },
|
|
2728
|
+
{ $set: { isBufferingReflection: isBuffering, updatedAt: /* @__PURE__ */ new Date() } }
|
|
2729
|
+
);
|
|
2730
|
+
if (result.matchedCount === 0) {
|
|
2731
|
+
throw new error.MastraError({
|
|
2732
|
+
id: storage.createStorageErrorId("MONGODB", "SET_BUFFERING_REFLECTION_FLAG", "NOT_FOUND"),
|
|
2733
|
+
text: `Observational memory record not found: ${id}`,
|
|
2734
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2735
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2736
|
+
details: { id, isBuffering }
|
|
2737
|
+
});
|
|
2738
|
+
}
|
|
2739
|
+
} catch (error$1) {
|
|
2740
|
+
if (error$1 instanceof error.MastraError) {
|
|
2741
|
+
throw error$1;
|
|
2742
|
+
}
|
|
2743
|
+
throw new error.MastraError(
|
|
2744
|
+
{
|
|
2745
|
+
id: storage.createStorageErrorId("MONGODB", "SET_BUFFERING_REFLECTION_FLAG", "FAILED"),
|
|
2746
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2747
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2748
|
+
details: { id, isBuffering }
|
|
2749
|
+
},
|
|
2750
|
+
error$1
|
|
2751
|
+
);
|
|
2752
|
+
}
|
|
2753
|
+
}
|
|
2734
2754
|
async clearObservationalMemory(threadId, resourceId) {
|
|
2735
2755
|
try {
|
|
2736
2756
|
const lookupKey = this.getOMKey(threadId, resourceId);
|
|
@@ -2748,10 +2768,10 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends storage.MemorySto
|
|
|
2748
2768
|
);
|
|
2749
2769
|
}
|
|
2750
2770
|
}
|
|
2751
|
-
async
|
|
2771
|
+
async setPendingMessageTokens(id, tokenCount) {
|
|
2752
2772
|
if (typeof tokenCount !== "number" || !Number.isFinite(tokenCount) || tokenCount < 0) {
|
|
2753
2773
|
throw new error.MastraError({
|
|
2754
|
-
id: storage.createStorageErrorId("MONGODB", "
|
|
2774
|
+
id: storage.createStorageErrorId("MONGODB", "SET_PENDING_MESSAGE_TOKENS", "INVALID_INPUT"),
|
|
2755
2775
|
text: `Invalid tokenCount: must be a finite non-negative number, got ${tokenCount}`,
|
|
2756
2776
|
domain: error.ErrorDomain.STORAGE,
|
|
2757
2777
|
category: error.ErrorCategory.USER,
|
|
@@ -2763,13 +2783,12 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends storage.MemorySto
|
|
|
2763
2783
|
const result = await collection.updateOne(
|
|
2764
2784
|
{ id },
|
|
2765
2785
|
{
|
|
2766
|
-
$
|
|
2767
|
-
$set: { updatedAt: /* @__PURE__ */ new Date() }
|
|
2786
|
+
$set: { pendingMessageTokens: tokenCount, updatedAt: /* @__PURE__ */ new Date() }
|
|
2768
2787
|
}
|
|
2769
2788
|
);
|
|
2770
2789
|
if (result.matchedCount === 0) {
|
|
2771
2790
|
throw new error.MastraError({
|
|
2772
|
-
id: storage.createStorageErrorId("MONGODB", "
|
|
2791
|
+
id: storage.createStorageErrorId("MONGODB", "SET_PENDING_MESSAGE_TOKENS", "NOT_FOUND"),
|
|
2773
2792
|
text: `Observational memory record not found: ${id}`,
|
|
2774
2793
|
domain: error.ErrorDomain.STORAGE,
|
|
2775
2794
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
@@ -2782,7 +2801,7 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends storage.MemorySto
|
|
|
2782
2801
|
}
|
|
2783
2802
|
throw new error.MastraError(
|
|
2784
2803
|
{
|
|
2785
|
-
id: storage.createStorageErrorId("MONGODB", "
|
|
2804
|
+
id: storage.createStorageErrorId("MONGODB", "SET_PENDING_MESSAGE_TOKENS", "FAILED"),
|
|
2786
2805
|
domain: error.ErrorDomain.STORAGE,
|
|
2787
2806
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
2788
2807
|
details: { id, tokenCount }
|
|
@@ -2791,116 +2810,405 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends storage.MemorySto
|
|
|
2791
2810
|
);
|
|
2792
2811
|
}
|
|
2793
2812
|
}
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
*/
|
|
2816
|
-
getDefaultIndexDefinitions() {
|
|
2817
|
-
return [
|
|
2818
|
-
{ collection: storage.TABLE_SPANS, keys: { spanId: 1, traceId: 1 }, options: { unique: true } },
|
|
2819
|
-
{ collection: storage.TABLE_SPANS, keys: { traceId: 1 } },
|
|
2820
|
-
{ collection: storage.TABLE_SPANS, keys: { parentSpanId: 1 } },
|
|
2821
|
-
{ collection: storage.TABLE_SPANS, keys: { startedAt: -1 } },
|
|
2822
|
-
{ collection: storage.TABLE_SPANS, keys: { spanType: 1 } },
|
|
2823
|
-
{ collection: storage.TABLE_SPANS, keys: { name: 1 } }
|
|
2824
|
-
];
|
|
2825
|
-
}
|
|
2826
|
-
async createDefaultIndexes() {
|
|
2827
|
-
if (this.#skipDefaultIndexes) {
|
|
2828
|
-
return;
|
|
2829
|
-
}
|
|
2830
|
-
for (const indexDef of this.getDefaultIndexDefinitions()) {
|
|
2831
|
-
try {
|
|
2832
|
-
const collection = await this.getCollection(indexDef.collection);
|
|
2833
|
-
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
2834
|
-
} catch (error) {
|
|
2835
|
-
this.logger?.warn?.(`Failed to create index on ${indexDef.collection}:`, error);
|
|
2836
|
-
}
|
|
2837
|
-
}
|
|
2838
|
-
}
|
|
2839
|
-
/**
|
|
2840
|
-
* Creates custom user-defined indexes for this domain's collections.
|
|
2841
|
-
*/
|
|
2842
|
-
async createCustomIndexes() {
|
|
2843
|
-
if (!this.#indexes || this.#indexes.length === 0) {
|
|
2844
|
-
return;
|
|
2845
|
-
}
|
|
2846
|
-
for (const indexDef of this.#indexes) {
|
|
2847
|
-
try {
|
|
2848
|
-
const collection = await this.getCollection(indexDef.collection);
|
|
2849
|
-
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
2850
|
-
} catch (error) {
|
|
2851
|
-
this.logger?.warn?.(`Failed to create custom index on ${indexDef.collection}:`, error);
|
|
2813
|
+
// ============================================
|
|
2814
|
+
// Async Buffering Methods
|
|
2815
|
+
// ============================================
|
|
2816
|
+
async updateBufferedObservations(input) {
|
|
2817
|
+
try {
|
|
2818
|
+
const collection = await this.getCollection(OM_TABLE);
|
|
2819
|
+
const newChunk = {
|
|
2820
|
+
id: `ombuf-${crypto.randomUUID()}`,
|
|
2821
|
+
cycleId: input.chunk.cycleId,
|
|
2822
|
+
observations: input.chunk.observations,
|
|
2823
|
+
tokenCount: input.chunk.tokenCount,
|
|
2824
|
+
messageIds: input.chunk.messageIds,
|
|
2825
|
+
messageTokens: input.chunk.messageTokens,
|
|
2826
|
+
lastObservedAt: input.chunk.lastObservedAt,
|
|
2827
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
2828
|
+
suggestedContinuation: input.chunk.suggestedContinuation,
|
|
2829
|
+
currentTask: input.chunk.currentTask
|
|
2830
|
+
};
|
|
2831
|
+
const $set = { updatedAt: /* @__PURE__ */ new Date() };
|
|
2832
|
+
if (input.lastBufferedAtTime) {
|
|
2833
|
+
$set.lastBufferedAtTime = input.lastBufferedAtTime;
|
|
2852
2834
|
}
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
===========================================================================
|
|
2862
|
-
MIGRATION REQUIRED: Duplicate spans detected in ${storage.TABLE_SPANS} collection
|
|
2863
|
-
===========================================================================
|
|
2864
|
-
|
|
2865
|
-
Found ${duplicateInfo.duplicateCount} duplicate (traceId, spanId) combinations.
|
|
2866
|
-
|
|
2867
|
-
The spans collection requires a unique index on (traceId, spanId), but your
|
|
2868
|
-
database contains duplicate entries that must be resolved first.
|
|
2869
|
-
|
|
2870
|
-
To fix this, run the manual migration command:
|
|
2871
|
-
|
|
2872
|
-
npx mastra migrate
|
|
2873
|
-
|
|
2874
|
-
This command will:
|
|
2875
|
-
1. Remove duplicate spans (keeping the most complete/recent version)
|
|
2876
|
-
2. Add the required unique index
|
|
2877
|
-
|
|
2878
|
-
Note: This migration may take some time for large collections.
|
|
2879
|
-
===========================================================================
|
|
2880
|
-
`;
|
|
2835
|
+
const result = await collection.updateOne(
|
|
2836
|
+
{ id: input.id },
|
|
2837
|
+
{
|
|
2838
|
+
$push: { bufferedObservationChunks: newChunk },
|
|
2839
|
+
$set
|
|
2840
|
+
}
|
|
2841
|
+
);
|
|
2842
|
+
if (result.matchedCount === 0) {
|
|
2881
2843
|
throw new error.MastraError({
|
|
2882
|
-
id: storage.createStorageErrorId("MONGODB", "
|
|
2844
|
+
id: storage.createStorageErrorId("MONGODB", "UPDATE_BUFFERED_OBSERVATIONS", "NOT_FOUND"),
|
|
2845
|
+
text: `Observational memory record not found: ${input.id}`,
|
|
2883
2846
|
domain: error.ErrorDomain.STORAGE,
|
|
2884
|
-
category: error.ErrorCategory.
|
|
2885
|
-
|
|
2847
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2848
|
+
details: { id: input.id }
|
|
2886
2849
|
});
|
|
2887
2850
|
}
|
|
2851
|
+
} catch (error$1) {
|
|
2852
|
+
if (error$1 instanceof error.MastraError) {
|
|
2853
|
+
throw error$1;
|
|
2854
|
+
}
|
|
2855
|
+
throw new error.MastraError(
|
|
2856
|
+
{
|
|
2857
|
+
id: storage.createStorageErrorId("MONGODB", "UPDATE_BUFFERED_OBSERVATIONS", "FAILED"),
|
|
2858
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2859
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2860
|
+
details: { id: input.id }
|
|
2861
|
+
},
|
|
2862
|
+
error$1
|
|
2863
|
+
);
|
|
2888
2864
|
}
|
|
2889
|
-
await this.createDefaultIndexes();
|
|
2890
|
-
await this.createCustomIndexes();
|
|
2891
2865
|
}
|
|
2892
|
-
|
|
2893
|
-
* Checks if the unique index on (spanId, traceId) already exists on the spans collection.
|
|
2894
|
-
* Used to skip deduplication when the index already exists (migration already complete).
|
|
2895
|
-
*/
|
|
2896
|
-
async spansUniqueIndexExists() {
|
|
2866
|
+
async swapBufferedToActive(input) {
|
|
2897
2867
|
try {
|
|
2898
|
-
const collection = await this.getCollection(
|
|
2899
|
-
const
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
2868
|
+
const collection = await this.getCollection(OM_TABLE);
|
|
2869
|
+
const doc = await collection.findOne({ id: input.id });
|
|
2870
|
+
if (!doc) {
|
|
2871
|
+
throw new error.MastraError({
|
|
2872
|
+
id: storage.createStorageErrorId("MONGODB", "SWAP_BUFFERED_TO_ACTIVE", "NOT_FOUND"),
|
|
2873
|
+
text: `Observational memory record not found: ${input.id}`,
|
|
2874
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2875
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2876
|
+
details: { id: input.id }
|
|
2877
|
+
});
|
|
2878
|
+
}
|
|
2879
|
+
const chunks = Array.isArray(doc.bufferedObservationChunks) ? doc.bufferedObservationChunks : [];
|
|
2880
|
+
if (chunks.length === 0) {
|
|
2881
|
+
return {
|
|
2882
|
+
chunksActivated: 0,
|
|
2883
|
+
messageTokensActivated: 0,
|
|
2884
|
+
observationTokensActivated: 0,
|
|
2885
|
+
messagesActivated: 0,
|
|
2886
|
+
activatedCycleIds: [],
|
|
2887
|
+
activatedMessageIds: []
|
|
2888
|
+
};
|
|
2889
|
+
}
|
|
2890
|
+
const retentionFloor = input.messageTokensThreshold * (1 - input.activationRatio);
|
|
2891
|
+
const targetMessageTokens = Math.max(0, input.currentPendingTokens - retentionFloor);
|
|
2892
|
+
let cumulativeMessageTokens = 0;
|
|
2893
|
+
let bestBoundary = 0;
|
|
2894
|
+
let bestBoundaryMessageTokens = 0;
|
|
2895
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
2896
|
+
cumulativeMessageTokens += chunks[i].messageTokens ?? 0;
|
|
2897
|
+
const boundary = i + 1;
|
|
2898
|
+
const isUnder = cumulativeMessageTokens <= targetMessageTokens;
|
|
2899
|
+
const bestIsUnder = bestBoundaryMessageTokens <= targetMessageTokens;
|
|
2900
|
+
if (bestBoundary === 0) {
|
|
2901
|
+
bestBoundary = boundary;
|
|
2902
|
+
bestBoundaryMessageTokens = cumulativeMessageTokens;
|
|
2903
|
+
} else if (isUnder && !bestIsUnder) {
|
|
2904
|
+
bestBoundary = boundary;
|
|
2905
|
+
bestBoundaryMessageTokens = cumulativeMessageTokens;
|
|
2906
|
+
} else if (isUnder && bestIsUnder) {
|
|
2907
|
+
if (cumulativeMessageTokens > bestBoundaryMessageTokens) {
|
|
2908
|
+
bestBoundary = boundary;
|
|
2909
|
+
bestBoundaryMessageTokens = cumulativeMessageTokens;
|
|
2910
|
+
}
|
|
2911
|
+
} else if (!isUnder && !bestIsUnder) {
|
|
2912
|
+
if (cumulativeMessageTokens < bestBoundaryMessageTokens) {
|
|
2913
|
+
bestBoundary = boundary;
|
|
2914
|
+
bestBoundaryMessageTokens = cumulativeMessageTokens;
|
|
2915
|
+
}
|
|
2916
|
+
}
|
|
2917
|
+
}
|
|
2918
|
+
const chunksToActivate = bestBoundary === 0 ? 1 : bestBoundary;
|
|
2919
|
+
const activatedChunks = chunks.slice(0, chunksToActivate);
|
|
2920
|
+
const remainingChunks = chunks.slice(chunksToActivate);
|
|
2921
|
+
const activatedContent = activatedChunks.map((c) => c.observations).join("\n\n");
|
|
2922
|
+
const activatedTokens = activatedChunks.reduce((sum, c) => sum + c.tokenCount, 0);
|
|
2923
|
+
const activatedMessageTokens = activatedChunks.reduce((sum, c) => sum + (c.messageTokens ?? 0), 0);
|
|
2924
|
+
const activatedMessageCount = activatedChunks.reduce((sum, c) => sum + c.messageIds.length, 0);
|
|
2925
|
+
const activatedCycleIds = activatedChunks.map((c) => c.cycleId).filter((id) => !!id);
|
|
2926
|
+
const activatedMessageIds = activatedChunks.flatMap((c) => c.messageIds ?? []);
|
|
2927
|
+
const latestChunk = activatedChunks[activatedChunks.length - 1];
|
|
2928
|
+
const lastObservedAt = input.lastObservedAt ?? (latestChunk?.lastObservedAt ? new Date(latestChunk.lastObservedAt) : /* @__PURE__ */ new Date());
|
|
2929
|
+
const existingActive = doc.activeObservations || "";
|
|
2930
|
+
const existingTokenCount = Number(doc.observationTokenCount || 0);
|
|
2931
|
+
const newActive = existingActive ? `${existingActive}
|
|
2932
|
+
|
|
2933
|
+
${activatedContent}` : activatedContent;
|
|
2934
|
+
const newTokenCount = existingTokenCount + activatedTokens;
|
|
2935
|
+
const existingPending = Number(doc.pendingMessageTokens || 0);
|
|
2936
|
+
const newPending = Math.max(0, existingPending - activatedMessageTokens);
|
|
2937
|
+
await collection.updateOne(
|
|
2938
|
+
{ id: input.id },
|
|
2939
|
+
{
|
|
2940
|
+
$set: {
|
|
2941
|
+
activeObservations: newActive,
|
|
2942
|
+
observationTokenCount: newTokenCount,
|
|
2943
|
+
pendingMessageTokens: newPending,
|
|
2944
|
+
bufferedObservationChunks: remainingChunks.length > 0 ? remainingChunks : null,
|
|
2945
|
+
lastObservedAt,
|
|
2946
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
2947
|
+
}
|
|
2948
|
+
}
|
|
2949
|
+
);
|
|
2950
|
+
return {
|
|
2951
|
+
chunksActivated: activatedChunks.length,
|
|
2952
|
+
messageTokensActivated: activatedMessageTokens,
|
|
2953
|
+
observationTokensActivated: activatedTokens,
|
|
2954
|
+
messagesActivated: activatedMessageCount,
|
|
2955
|
+
activatedCycleIds,
|
|
2956
|
+
activatedMessageIds,
|
|
2957
|
+
observations: activatedContent,
|
|
2958
|
+
perChunk: activatedChunks.map((c) => ({
|
|
2959
|
+
cycleId: c.cycleId ?? "",
|
|
2960
|
+
messageTokens: c.messageTokens ?? 0,
|
|
2961
|
+
observationTokens: c.tokenCount,
|
|
2962
|
+
messageCount: c.messageIds.length,
|
|
2963
|
+
observations: c.observations
|
|
2964
|
+
}))
|
|
2965
|
+
};
|
|
2966
|
+
} catch (error$1) {
|
|
2967
|
+
if (error$1 instanceof error.MastraError) {
|
|
2968
|
+
throw error$1;
|
|
2969
|
+
}
|
|
2970
|
+
throw new error.MastraError(
|
|
2971
|
+
{
|
|
2972
|
+
id: storage.createStorageErrorId("MONGODB", "SWAP_BUFFERED_TO_ACTIVE", "FAILED"),
|
|
2973
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2974
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2975
|
+
details: { id: input.id }
|
|
2976
|
+
},
|
|
2977
|
+
error$1
|
|
2978
|
+
);
|
|
2979
|
+
}
|
|
2980
|
+
}
|
|
2981
|
+
async updateBufferedReflection(input) {
|
|
2982
|
+
try {
|
|
2983
|
+
const collection = await this.getCollection(OM_TABLE);
|
|
2984
|
+
const doc = await collection.findOne({ id: input.id });
|
|
2985
|
+
if (!doc) {
|
|
2986
|
+
throw new error.MastraError({
|
|
2987
|
+
id: storage.createStorageErrorId("MONGODB", "UPDATE_BUFFERED_REFLECTION", "NOT_FOUND"),
|
|
2988
|
+
text: `Observational memory record not found: ${input.id}`,
|
|
2989
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2990
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2991
|
+
details: { id: input.id }
|
|
2992
|
+
});
|
|
2993
|
+
}
|
|
2994
|
+
const existingContent = doc.bufferedReflection || "";
|
|
2995
|
+
const existingTokens = Number(doc.bufferedReflectionTokens || 0);
|
|
2996
|
+
const existingInputTokens = Number(doc.bufferedReflectionInputTokens || 0);
|
|
2997
|
+
const newContent = existingContent ? `${existingContent}
|
|
2998
|
+
|
|
2999
|
+
${input.reflection}` : input.reflection;
|
|
3000
|
+
const newTokens = existingTokens + input.tokenCount;
|
|
3001
|
+
const newInputTokens = existingInputTokens + input.inputTokenCount;
|
|
3002
|
+
const result = await collection.updateOne(
|
|
3003
|
+
{ id: input.id },
|
|
3004
|
+
{
|
|
3005
|
+
$set: {
|
|
3006
|
+
bufferedReflection: newContent,
|
|
3007
|
+
bufferedReflectionTokens: newTokens,
|
|
3008
|
+
bufferedReflectionInputTokens: newInputTokens,
|
|
3009
|
+
reflectedObservationLineCount: input.reflectedObservationLineCount,
|
|
3010
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
3011
|
+
}
|
|
3012
|
+
}
|
|
3013
|
+
);
|
|
3014
|
+
if (result.matchedCount === 0) {
|
|
3015
|
+
throw new error.MastraError({
|
|
3016
|
+
id: storage.createStorageErrorId("MONGODB", "UPDATE_BUFFERED_REFLECTION", "NOT_FOUND"),
|
|
3017
|
+
text: `Observational memory record not found: ${input.id}`,
|
|
3018
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3019
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
3020
|
+
details: { id: input.id }
|
|
3021
|
+
});
|
|
3022
|
+
}
|
|
3023
|
+
} catch (error$1) {
|
|
3024
|
+
if (error$1 instanceof error.MastraError) {
|
|
3025
|
+
throw error$1;
|
|
3026
|
+
}
|
|
3027
|
+
throw new error.MastraError(
|
|
3028
|
+
{
|
|
3029
|
+
id: storage.createStorageErrorId("MONGODB", "UPDATE_BUFFERED_REFLECTION", "FAILED"),
|
|
3030
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3031
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
3032
|
+
details: { id: input.id }
|
|
3033
|
+
},
|
|
3034
|
+
error$1
|
|
3035
|
+
);
|
|
3036
|
+
}
|
|
3037
|
+
}
|
|
3038
|
+
async swapBufferedReflectionToActive(input) {
|
|
3039
|
+
try {
|
|
3040
|
+
const collection = await this.getCollection(OM_TABLE);
|
|
3041
|
+
const doc = await collection.findOne({ id: input.currentRecord.id });
|
|
3042
|
+
if (!doc) {
|
|
3043
|
+
throw new error.MastraError({
|
|
3044
|
+
id: storage.createStorageErrorId("MONGODB", "SWAP_BUFFERED_REFLECTION_TO_ACTIVE", "NOT_FOUND"),
|
|
3045
|
+
text: `Observational memory record not found: ${input.currentRecord.id}`,
|
|
3046
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3047
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
3048
|
+
details: { id: input.currentRecord.id }
|
|
3049
|
+
});
|
|
3050
|
+
}
|
|
3051
|
+
const bufferedReflection = doc.bufferedReflection || "";
|
|
3052
|
+
const reflectedLineCount = Number(doc.reflectedObservationLineCount || 0);
|
|
3053
|
+
if (!bufferedReflection) {
|
|
3054
|
+
throw new error.MastraError({
|
|
3055
|
+
id: storage.createStorageErrorId("MONGODB", "SWAP_BUFFERED_REFLECTION_TO_ACTIVE", "NO_CONTENT"),
|
|
3056
|
+
text: "No buffered reflection to swap",
|
|
3057
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3058
|
+
category: error.ErrorCategory.USER,
|
|
3059
|
+
details: { id: input.currentRecord.id }
|
|
3060
|
+
});
|
|
3061
|
+
}
|
|
3062
|
+
const currentObservations = doc.activeObservations || "";
|
|
3063
|
+
const allLines = currentObservations.split("\n");
|
|
3064
|
+
const unreflectedLines = allLines.slice(reflectedLineCount);
|
|
3065
|
+
const unreflectedContent = unreflectedLines.join("\n").trim();
|
|
3066
|
+
const newObservations = unreflectedContent ? `${bufferedReflection}
|
|
3067
|
+
|
|
3068
|
+
${unreflectedContent}` : bufferedReflection;
|
|
3069
|
+
const newRecord = await this.createReflectionGeneration({
|
|
3070
|
+
currentRecord: input.currentRecord,
|
|
3071
|
+
reflection: newObservations,
|
|
3072
|
+
tokenCount: input.tokenCount
|
|
3073
|
+
});
|
|
3074
|
+
await collection.updateOne(
|
|
3075
|
+
{ id: input.currentRecord.id },
|
|
3076
|
+
{
|
|
3077
|
+
$set: {
|
|
3078
|
+
bufferedReflection: null,
|
|
3079
|
+
bufferedReflectionTokens: null,
|
|
3080
|
+
bufferedReflectionInputTokens: null,
|
|
3081
|
+
reflectedObservationLineCount: null,
|
|
3082
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
3083
|
+
}
|
|
3084
|
+
}
|
|
3085
|
+
);
|
|
3086
|
+
return newRecord;
|
|
3087
|
+
} catch (error$1) {
|
|
3088
|
+
if (error$1 instanceof error.MastraError) {
|
|
3089
|
+
throw error$1;
|
|
3090
|
+
}
|
|
3091
|
+
throw new error.MastraError(
|
|
3092
|
+
{
|
|
3093
|
+
id: storage.createStorageErrorId("MONGODB", "SWAP_BUFFERED_REFLECTION_TO_ACTIVE", "FAILED"),
|
|
3094
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3095
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
3096
|
+
details: { id: input.currentRecord.id }
|
|
3097
|
+
},
|
|
3098
|
+
error$1
|
|
3099
|
+
);
|
|
3100
|
+
}
|
|
3101
|
+
}
|
|
3102
|
+
};
|
|
3103
|
+
var ObservabilityMongoDB = class _ObservabilityMongoDB extends storage.ObservabilityStorage {
|
|
3104
|
+
#connector;
|
|
3105
|
+
#skipDefaultIndexes;
|
|
3106
|
+
#indexes;
|
|
3107
|
+
/** Collections managed by this domain */
|
|
3108
|
+
static MANAGED_COLLECTIONS = [storage.TABLE_SPANS];
|
|
3109
|
+
constructor(config) {
|
|
3110
|
+
super();
|
|
3111
|
+
this.#connector = resolveMongoDBConfig(config);
|
|
3112
|
+
this.#skipDefaultIndexes = config.skipDefaultIndexes;
|
|
3113
|
+
this.#indexes = config.indexes?.filter(
|
|
3114
|
+
(idx) => _ObservabilityMongoDB.MANAGED_COLLECTIONS.includes(idx.collection)
|
|
3115
|
+
);
|
|
3116
|
+
}
|
|
3117
|
+
async getCollection(name) {
|
|
3118
|
+
return this.#connector.getCollection(name);
|
|
3119
|
+
}
|
|
3120
|
+
/**
|
|
3121
|
+
* Returns default index definitions for the observability domain collections.
|
|
3122
|
+
* These indexes optimize common query patterns for span and trace lookups.
|
|
3123
|
+
*/
|
|
3124
|
+
getDefaultIndexDefinitions() {
|
|
3125
|
+
return [
|
|
3126
|
+
{ collection: storage.TABLE_SPANS, keys: { spanId: 1, traceId: 1 }, options: { unique: true } },
|
|
3127
|
+
{ collection: storage.TABLE_SPANS, keys: { traceId: 1 } },
|
|
3128
|
+
{ collection: storage.TABLE_SPANS, keys: { parentSpanId: 1 } },
|
|
3129
|
+
{ collection: storage.TABLE_SPANS, keys: { startedAt: -1 } },
|
|
3130
|
+
{ collection: storage.TABLE_SPANS, keys: { spanType: 1 } },
|
|
3131
|
+
{ collection: storage.TABLE_SPANS, keys: { name: 1 } }
|
|
3132
|
+
];
|
|
3133
|
+
}
|
|
3134
|
+
async createDefaultIndexes() {
|
|
3135
|
+
if (this.#skipDefaultIndexes) {
|
|
3136
|
+
return;
|
|
3137
|
+
}
|
|
3138
|
+
for (const indexDef of this.getDefaultIndexDefinitions()) {
|
|
3139
|
+
try {
|
|
3140
|
+
const collection = await this.getCollection(indexDef.collection);
|
|
3141
|
+
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
3142
|
+
} catch (error) {
|
|
3143
|
+
this.logger?.warn?.(`Failed to create index on ${indexDef.collection}:`, error);
|
|
3144
|
+
}
|
|
3145
|
+
}
|
|
3146
|
+
}
|
|
3147
|
+
/**
|
|
3148
|
+
* Creates custom user-defined indexes for this domain's collections.
|
|
3149
|
+
*/
|
|
3150
|
+
async createCustomIndexes() {
|
|
3151
|
+
if (!this.#indexes || this.#indexes.length === 0) {
|
|
3152
|
+
return;
|
|
3153
|
+
}
|
|
3154
|
+
for (const indexDef of this.#indexes) {
|
|
3155
|
+
try {
|
|
3156
|
+
const collection = await this.getCollection(indexDef.collection);
|
|
3157
|
+
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
3158
|
+
} catch (error) {
|
|
3159
|
+
this.logger?.warn?.(`Failed to create custom index on ${indexDef.collection}:`, error);
|
|
3160
|
+
}
|
|
3161
|
+
}
|
|
3162
|
+
}
|
|
3163
|
+
async init() {
|
|
3164
|
+
const uniqueIndexExists = await this.spansUniqueIndexExists();
|
|
3165
|
+
if (!uniqueIndexExists) {
|
|
3166
|
+
const duplicateInfo = await this.checkForDuplicateSpans();
|
|
3167
|
+
if (duplicateInfo.hasDuplicates) {
|
|
3168
|
+
const errorMessage = `
|
|
3169
|
+
===========================================================================
|
|
3170
|
+
MIGRATION REQUIRED: Duplicate spans detected in ${storage.TABLE_SPANS} collection
|
|
3171
|
+
===========================================================================
|
|
3172
|
+
|
|
3173
|
+
Found ${duplicateInfo.duplicateCount} duplicate (traceId, spanId) combinations.
|
|
3174
|
+
|
|
3175
|
+
The spans collection requires a unique index on (traceId, spanId), but your
|
|
3176
|
+
database contains duplicate entries that must be resolved first.
|
|
3177
|
+
|
|
3178
|
+
To fix this, run the manual migration command:
|
|
3179
|
+
|
|
3180
|
+
npx mastra migrate
|
|
3181
|
+
|
|
3182
|
+
This command will:
|
|
3183
|
+
1. Remove duplicate spans (keeping the most complete/recent version)
|
|
3184
|
+
2. Add the required unique index
|
|
3185
|
+
|
|
3186
|
+
Note: This migration may take some time for large collections.
|
|
3187
|
+
===========================================================================
|
|
3188
|
+
`;
|
|
3189
|
+
throw new error.MastraError({
|
|
3190
|
+
id: storage.createStorageErrorId("MONGODB", "MIGRATION_REQUIRED", "DUPLICATE_SPANS"),
|
|
3191
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3192
|
+
category: error.ErrorCategory.USER,
|
|
3193
|
+
text: errorMessage
|
|
3194
|
+
});
|
|
3195
|
+
}
|
|
3196
|
+
}
|
|
3197
|
+
await this.createDefaultIndexes();
|
|
3198
|
+
await this.createCustomIndexes();
|
|
3199
|
+
}
|
|
3200
|
+
/**
|
|
3201
|
+
* Checks if the unique index on (spanId, traceId) already exists on the spans collection.
|
|
3202
|
+
* Used to skip deduplication when the index already exists (migration already complete).
|
|
3203
|
+
*/
|
|
3204
|
+
async spansUniqueIndexExists() {
|
|
3205
|
+
try {
|
|
3206
|
+
const collection = await this.getCollection(storage.TABLE_SPANS);
|
|
3207
|
+
const indexes = await collection.indexes();
|
|
3208
|
+
return indexes.some((idx) => idx.unique === true && idx.key?.spanId === 1 && idx.key?.traceId === 1);
|
|
3209
|
+
} catch {
|
|
3210
|
+
return false;
|
|
3211
|
+
}
|
|
2904
3212
|
}
|
|
2905
3213
|
/**
|
|
2906
3214
|
* Checks for duplicate (traceId, spanId) combinations in the spans collection.
|
|
@@ -3551,6 +3859,1161 @@ Note: This migration may take some time for large collections.
|
|
|
3551
3859
|
return span;
|
|
3552
3860
|
}
|
|
3553
3861
|
};
|
|
3862
|
+
var SNAPSHOT_FIELDS2 = ["name", "description", "content", "rules"];
|
|
3863
|
+
var MongoDBPromptBlocksStorage = class _MongoDBPromptBlocksStorage extends storage.PromptBlocksStorage {
|
|
3864
|
+
#connector;
|
|
3865
|
+
#skipDefaultIndexes;
|
|
3866
|
+
#indexes;
|
|
3867
|
+
static MANAGED_COLLECTIONS = [storage.TABLE_PROMPT_BLOCKS, storage.TABLE_PROMPT_BLOCK_VERSIONS];
|
|
3868
|
+
constructor(config) {
|
|
3869
|
+
super();
|
|
3870
|
+
this.#connector = resolveMongoDBConfig(config);
|
|
3871
|
+
this.#skipDefaultIndexes = config.skipDefaultIndexes;
|
|
3872
|
+
this.#indexes = config.indexes?.filter(
|
|
3873
|
+
(idx) => _MongoDBPromptBlocksStorage.MANAGED_COLLECTIONS.includes(idx.collection)
|
|
3874
|
+
);
|
|
3875
|
+
}
|
|
3876
|
+
async getCollection(name) {
|
|
3877
|
+
return this.#connector.getCollection(name);
|
|
3878
|
+
}
|
|
3879
|
+
getDefaultIndexDefinitions() {
|
|
3880
|
+
return [
|
|
3881
|
+
{ collection: storage.TABLE_PROMPT_BLOCKS, keys: { id: 1 }, options: { unique: true } },
|
|
3882
|
+
{ collection: storage.TABLE_PROMPT_BLOCKS, keys: { createdAt: -1 } },
|
|
3883
|
+
{ collection: storage.TABLE_PROMPT_BLOCKS, keys: { updatedAt: -1 } },
|
|
3884
|
+
{ collection: storage.TABLE_PROMPT_BLOCKS, keys: { authorId: 1 } },
|
|
3885
|
+
{ collection: storage.TABLE_PROMPT_BLOCK_VERSIONS, keys: { id: 1 }, options: { unique: true } },
|
|
3886
|
+
{
|
|
3887
|
+
collection: storage.TABLE_PROMPT_BLOCK_VERSIONS,
|
|
3888
|
+
keys: { blockId: 1, versionNumber: -1 },
|
|
3889
|
+
options: { unique: true }
|
|
3890
|
+
},
|
|
3891
|
+
{ collection: storage.TABLE_PROMPT_BLOCK_VERSIONS, keys: { blockId: 1, createdAt: -1 } }
|
|
3892
|
+
];
|
|
3893
|
+
}
|
|
3894
|
+
async createDefaultIndexes() {
|
|
3895
|
+
if (this.#skipDefaultIndexes) {
|
|
3896
|
+
return;
|
|
3897
|
+
}
|
|
3898
|
+
for (const indexDef of this.getDefaultIndexDefinitions()) {
|
|
3899
|
+
try {
|
|
3900
|
+
const collection = await this.getCollection(indexDef.collection);
|
|
3901
|
+
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
3902
|
+
} catch (error) {
|
|
3903
|
+
this.logger?.warn?.(`Failed to create index on ${indexDef.collection}:`, error);
|
|
3904
|
+
}
|
|
3905
|
+
}
|
|
3906
|
+
}
|
|
3907
|
+
async createCustomIndexes() {
|
|
3908
|
+
if (!this.#indexes || this.#indexes.length === 0) {
|
|
3909
|
+
return;
|
|
3910
|
+
}
|
|
3911
|
+
for (const indexDef of this.#indexes) {
|
|
3912
|
+
try {
|
|
3913
|
+
const collection = await this.getCollection(indexDef.collection);
|
|
3914
|
+
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
3915
|
+
} catch (error) {
|
|
3916
|
+
this.logger?.warn?.(`Failed to create custom index on ${indexDef.collection}:`, error);
|
|
3917
|
+
}
|
|
3918
|
+
}
|
|
3919
|
+
}
|
|
3920
|
+
async init() {
|
|
3921
|
+
await this.createDefaultIndexes();
|
|
3922
|
+
await this.createCustomIndexes();
|
|
3923
|
+
}
|
|
3924
|
+
async dangerouslyClearAll() {
|
|
3925
|
+
const versionsCollection = await this.getCollection(storage.TABLE_PROMPT_BLOCK_VERSIONS);
|
|
3926
|
+
await versionsCollection.deleteMany({});
|
|
3927
|
+
const blocksCollection = await this.getCollection(storage.TABLE_PROMPT_BLOCKS);
|
|
3928
|
+
await blocksCollection.deleteMany({});
|
|
3929
|
+
}
|
|
3930
|
+
// ==========================================================================
|
|
3931
|
+
// Prompt Block CRUD
|
|
3932
|
+
// ==========================================================================
|
|
3933
|
+
async getById(id) {
|
|
3934
|
+
try {
|
|
3935
|
+
const collection = await this.getCollection(storage.TABLE_PROMPT_BLOCKS);
|
|
3936
|
+
const result = await collection.findOne({ id });
|
|
3937
|
+
if (!result) {
|
|
3938
|
+
return null;
|
|
3939
|
+
}
|
|
3940
|
+
return this.transformBlock(result);
|
|
3941
|
+
} catch (error$1) {
|
|
3942
|
+
throw new error.MastraError(
|
|
3943
|
+
{
|
|
3944
|
+
id: storage.createStorageErrorId("MONGODB", "GET_PROMPT_BLOCK_BY_ID", "FAILED"),
|
|
3945
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3946
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
3947
|
+
details: { id }
|
|
3948
|
+
},
|
|
3949
|
+
error$1
|
|
3950
|
+
);
|
|
3951
|
+
}
|
|
3952
|
+
}
|
|
3953
|
+
async create(input) {
|
|
3954
|
+
const { promptBlock } = input;
|
|
3955
|
+
try {
|
|
3956
|
+
const collection = await this.getCollection(storage.TABLE_PROMPT_BLOCKS);
|
|
3957
|
+
const existing = await collection.findOne({ id: promptBlock.id });
|
|
3958
|
+
if (existing) {
|
|
3959
|
+
throw new error.MastraError({
|
|
3960
|
+
id: storage.createStorageErrorId("MONGODB", "CREATE_PROMPT_BLOCK", "ALREADY_EXISTS"),
|
|
3961
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3962
|
+
category: error.ErrorCategory.USER,
|
|
3963
|
+
details: { id: promptBlock.id },
|
|
3964
|
+
text: `Prompt block with id ${promptBlock.id} already exists`
|
|
3965
|
+
});
|
|
3966
|
+
}
|
|
3967
|
+
const now = /* @__PURE__ */ new Date();
|
|
3968
|
+
const newBlock = {
|
|
3969
|
+
id: promptBlock.id,
|
|
3970
|
+
status: "draft",
|
|
3971
|
+
activeVersionId: void 0,
|
|
3972
|
+
authorId: promptBlock.authorId,
|
|
3973
|
+
metadata: promptBlock.metadata,
|
|
3974
|
+
createdAt: now,
|
|
3975
|
+
updatedAt: now
|
|
3976
|
+
};
|
|
3977
|
+
await collection.insertOne(this.serializeBlock(newBlock));
|
|
3978
|
+
const snapshotConfig = {};
|
|
3979
|
+
for (const field of SNAPSHOT_FIELDS2) {
|
|
3980
|
+
if (promptBlock[field] !== void 0) {
|
|
3981
|
+
snapshotConfig[field] = promptBlock[field];
|
|
3982
|
+
}
|
|
3983
|
+
}
|
|
3984
|
+
const versionId = crypto.randomUUID();
|
|
3985
|
+
await this.createVersion({
|
|
3986
|
+
id: versionId,
|
|
3987
|
+
blockId: promptBlock.id,
|
|
3988
|
+
versionNumber: 1,
|
|
3989
|
+
...snapshotConfig,
|
|
3990
|
+
changedFields: Object.keys(snapshotConfig),
|
|
3991
|
+
changeMessage: "Initial version"
|
|
3992
|
+
});
|
|
3993
|
+
return newBlock;
|
|
3994
|
+
} catch (error$1) {
|
|
3995
|
+
if (error$1 instanceof error.MastraError) {
|
|
3996
|
+
throw error$1;
|
|
3997
|
+
}
|
|
3998
|
+
throw new error.MastraError(
|
|
3999
|
+
{
|
|
4000
|
+
id: storage.createStorageErrorId("MONGODB", "CREATE_PROMPT_BLOCK", "FAILED"),
|
|
4001
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4002
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4003
|
+
details: { id: promptBlock.id }
|
|
4004
|
+
},
|
|
4005
|
+
error$1
|
|
4006
|
+
);
|
|
4007
|
+
}
|
|
4008
|
+
}
|
|
4009
|
+
async update(input) {
|
|
4010
|
+
const { id, ...updates } = input;
|
|
4011
|
+
try {
|
|
4012
|
+
const collection = await this.getCollection(storage.TABLE_PROMPT_BLOCKS);
|
|
4013
|
+
const existingBlock = await collection.findOne({ id });
|
|
4014
|
+
if (!existingBlock) {
|
|
4015
|
+
throw new error.MastraError({
|
|
4016
|
+
id: storage.createStorageErrorId("MONGODB", "UPDATE_PROMPT_BLOCK", "NOT_FOUND"),
|
|
4017
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4018
|
+
category: error.ErrorCategory.USER,
|
|
4019
|
+
details: { id },
|
|
4020
|
+
text: `Prompt block with id ${id} not found`
|
|
4021
|
+
});
|
|
4022
|
+
}
|
|
4023
|
+
const updateDoc = {
|
|
4024
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
4025
|
+
};
|
|
4026
|
+
const metadataFields = {
|
|
4027
|
+
authorId: updates.authorId,
|
|
4028
|
+
activeVersionId: updates.activeVersionId,
|
|
4029
|
+
metadata: updates.metadata,
|
|
4030
|
+
status: updates.status
|
|
4031
|
+
};
|
|
4032
|
+
const configFields = {};
|
|
4033
|
+
for (const field of SNAPSHOT_FIELDS2) {
|
|
4034
|
+
if (updates[field] !== void 0) {
|
|
4035
|
+
configFields[field] = updates[field];
|
|
4036
|
+
}
|
|
4037
|
+
}
|
|
4038
|
+
if (Object.keys(configFields).length > 0) {
|
|
4039
|
+
const latestVersion = await this.getLatestVersion(id);
|
|
4040
|
+
if (!latestVersion) {
|
|
4041
|
+
throw new error.MastraError({
|
|
4042
|
+
id: storage.createStorageErrorId("MONGODB", "UPDATE_PROMPT_BLOCK", "NO_VERSION"),
|
|
4043
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4044
|
+
category: error.ErrorCategory.USER,
|
|
4045
|
+
text: `Cannot update config fields for prompt block ${id} - no versions exist`,
|
|
4046
|
+
details: { id }
|
|
4047
|
+
});
|
|
4048
|
+
}
|
|
4049
|
+
const existingSnapshot = this.extractSnapshotFields(latestVersion);
|
|
4050
|
+
await this.createVersion({
|
|
4051
|
+
id: crypto.randomUUID(),
|
|
4052
|
+
blockId: id,
|
|
4053
|
+
versionNumber: latestVersion.versionNumber + 1,
|
|
4054
|
+
...existingSnapshot,
|
|
4055
|
+
...configFields,
|
|
4056
|
+
changedFields: Object.keys(configFields),
|
|
4057
|
+
changeMessage: `Updated: ${Object.keys(configFields).join(", ")}`
|
|
4058
|
+
});
|
|
4059
|
+
}
|
|
4060
|
+
if (metadataFields.authorId !== void 0) updateDoc.authorId = metadataFields.authorId;
|
|
4061
|
+
if (metadataFields.activeVersionId !== void 0) {
|
|
4062
|
+
updateDoc.activeVersionId = metadataFields.activeVersionId;
|
|
4063
|
+
if (metadataFields.status === void 0) {
|
|
4064
|
+
updateDoc.status = "published";
|
|
4065
|
+
}
|
|
4066
|
+
}
|
|
4067
|
+
if (metadataFields.status !== void 0) {
|
|
4068
|
+
updateDoc.status = metadataFields.status;
|
|
4069
|
+
}
|
|
4070
|
+
if (metadataFields.metadata !== void 0) {
|
|
4071
|
+
const existingMetadata = existingBlock.metadata || {};
|
|
4072
|
+
updateDoc.metadata = { ...existingMetadata, ...metadataFields.metadata };
|
|
4073
|
+
}
|
|
4074
|
+
await collection.updateOne({ id }, { $set: updateDoc });
|
|
4075
|
+
const updatedBlock = await collection.findOne({ id });
|
|
4076
|
+
if (!updatedBlock) {
|
|
4077
|
+
throw new error.MastraError({
|
|
4078
|
+
id: storage.createStorageErrorId("MONGODB", "UPDATE_PROMPT_BLOCK", "NOT_FOUND_AFTER_UPDATE"),
|
|
4079
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4080
|
+
category: error.ErrorCategory.SYSTEM,
|
|
4081
|
+
text: `Prompt block with id ${id} was deleted during update`,
|
|
4082
|
+
details: { id }
|
|
4083
|
+
});
|
|
4084
|
+
}
|
|
4085
|
+
return this.transformBlock(updatedBlock);
|
|
4086
|
+
} catch (error$1) {
|
|
4087
|
+
if (error$1 instanceof error.MastraError) {
|
|
4088
|
+
throw error$1;
|
|
4089
|
+
}
|
|
4090
|
+
throw new error.MastraError(
|
|
4091
|
+
{
|
|
4092
|
+
id: storage.createStorageErrorId("MONGODB", "UPDATE_PROMPT_BLOCK", "FAILED"),
|
|
4093
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4094
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4095
|
+
details: { id }
|
|
4096
|
+
},
|
|
4097
|
+
error$1
|
|
4098
|
+
);
|
|
4099
|
+
}
|
|
4100
|
+
}
|
|
4101
|
+
async delete(id) {
|
|
4102
|
+
try {
|
|
4103
|
+
await this.deleteVersionsByParentId(id);
|
|
4104
|
+
const collection = await this.getCollection(storage.TABLE_PROMPT_BLOCKS);
|
|
4105
|
+
await collection.deleteOne({ id });
|
|
4106
|
+
} catch (error$1) {
|
|
4107
|
+
throw new error.MastraError(
|
|
4108
|
+
{
|
|
4109
|
+
id: storage.createStorageErrorId("MONGODB", "DELETE_PROMPT_BLOCK", "FAILED"),
|
|
4110
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4111
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4112
|
+
details: { id }
|
|
4113
|
+
},
|
|
4114
|
+
error$1
|
|
4115
|
+
);
|
|
4116
|
+
}
|
|
4117
|
+
}
|
|
4118
|
+
async list(args) {
|
|
4119
|
+
try {
|
|
4120
|
+
const { page = 0, perPage: perPageInput, orderBy, authorId, metadata } = args || {};
|
|
4121
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
4122
|
+
if (page < 0) {
|
|
4123
|
+
throw new error.MastraError(
|
|
4124
|
+
{
|
|
4125
|
+
id: storage.createStorageErrorId("MONGODB", "LIST_PROMPT_BLOCKS", "INVALID_PAGE"),
|
|
4126
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4127
|
+
category: error.ErrorCategory.USER,
|
|
4128
|
+
details: { page }
|
|
4129
|
+
},
|
|
4130
|
+
new Error("page must be >= 0")
|
|
4131
|
+
);
|
|
4132
|
+
}
|
|
4133
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
4134
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
4135
|
+
const collection = await this.getCollection(storage.TABLE_PROMPT_BLOCKS);
|
|
4136
|
+
const filter = {};
|
|
4137
|
+
if (authorId) {
|
|
4138
|
+
filter.authorId = authorId;
|
|
4139
|
+
}
|
|
4140
|
+
if (metadata) {
|
|
4141
|
+
for (const [key, value] of Object.entries(metadata)) {
|
|
4142
|
+
filter[`metadata.${key}`] = value;
|
|
4143
|
+
}
|
|
4144
|
+
}
|
|
4145
|
+
const total = await collection.countDocuments(filter);
|
|
4146
|
+
if (total === 0 || perPage === 0) {
|
|
4147
|
+
return {
|
|
4148
|
+
promptBlocks: [],
|
|
4149
|
+
total,
|
|
4150
|
+
page,
|
|
4151
|
+
perPage: perPageForResponse,
|
|
4152
|
+
hasMore: false
|
|
4153
|
+
};
|
|
4154
|
+
}
|
|
4155
|
+
const sortOrder = direction === "ASC" ? 1 : -1;
|
|
4156
|
+
let cursor = collection.find(filter).sort({ [field]: sortOrder }).skip(offset);
|
|
4157
|
+
if (perPageInput !== false) {
|
|
4158
|
+
cursor = cursor.limit(perPage);
|
|
4159
|
+
}
|
|
4160
|
+
const results = await cursor.toArray();
|
|
4161
|
+
const promptBlocks = results.map((doc) => this.transformBlock(doc));
|
|
4162
|
+
return {
|
|
4163
|
+
promptBlocks,
|
|
4164
|
+
total,
|
|
4165
|
+
page,
|
|
4166
|
+
perPage: perPageForResponse,
|
|
4167
|
+
hasMore: perPageInput !== false && offset + perPage < total
|
|
4168
|
+
};
|
|
4169
|
+
} catch (error$1) {
|
|
4170
|
+
if (error$1 instanceof error.MastraError) {
|
|
4171
|
+
throw error$1;
|
|
4172
|
+
}
|
|
4173
|
+
throw new error.MastraError(
|
|
4174
|
+
{
|
|
4175
|
+
id: storage.createStorageErrorId("MONGODB", "LIST_PROMPT_BLOCKS", "FAILED"),
|
|
4176
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4177
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
4178
|
+
},
|
|
4179
|
+
error$1
|
|
4180
|
+
);
|
|
4181
|
+
}
|
|
4182
|
+
}
|
|
4183
|
+
// ==========================================================================
|
|
4184
|
+
// Prompt Block Version Methods
|
|
4185
|
+
// ==========================================================================
|
|
4186
|
+
async createVersion(input) {
|
|
4187
|
+
try {
|
|
4188
|
+
const collection = await this.getCollection(storage.TABLE_PROMPT_BLOCK_VERSIONS);
|
|
4189
|
+
const now = /* @__PURE__ */ new Date();
|
|
4190
|
+
const versionDoc = {
|
|
4191
|
+
id: input.id,
|
|
4192
|
+
blockId: input.blockId,
|
|
4193
|
+
versionNumber: input.versionNumber,
|
|
4194
|
+
changedFields: input.changedFields ?? void 0,
|
|
4195
|
+
changeMessage: input.changeMessage ?? void 0,
|
|
4196
|
+
createdAt: now
|
|
4197
|
+
};
|
|
4198
|
+
for (const field of SNAPSHOT_FIELDS2) {
|
|
4199
|
+
if (input[field] !== void 0) {
|
|
4200
|
+
versionDoc[field] = input[field];
|
|
4201
|
+
}
|
|
4202
|
+
}
|
|
4203
|
+
await collection.insertOne(versionDoc);
|
|
4204
|
+
return {
|
|
4205
|
+
...input,
|
|
4206
|
+
createdAt: now
|
|
4207
|
+
};
|
|
4208
|
+
} catch (error$1) {
|
|
4209
|
+
throw new error.MastraError(
|
|
4210
|
+
{
|
|
4211
|
+
id: storage.createStorageErrorId("MONGODB", "CREATE_PROMPT_BLOCK_VERSION", "FAILED"),
|
|
4212
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4213
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4214
|
+
details: { versionId: input.id, blockId: input.blockId }
|
|
4215
|
+
},
|
|
4216
|
+
error$1
|
|
4217
|
+
);
|
|
4218
|
+
}
|
|
4219
|
+
}
|
|
4220
|
+
async getVersion(id) {
|
|
4221
|
+
try {
|
|
4222
|
+
const collection = await this.getCollection(storage.TABLE_PROMPT_BLOCK_VERSIONS);
|
|
4223
|
+
const result = await collection.findOne({ id });
|
|
4224
|
+
if (!result) {
|
|
4225
|
+
return null;
|
|
4226
|
+
}
|
|
4227
|
+
return this.transformVersion(result);
|
|
4228
|
+
} catch (error$1) {
|
|
4229
|
+
throw new error.MastraError(
|
|
4230
|
+
{
|
|
4231
|
+
id: storage.createStorageErrorId("MONGODB", "GET_PROMPT_BLOCK_VERSION", "FAILED"),
|
|
4232
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4233
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4234
|
+
details: { versionId: id }
|
|
4235
|
+
},
|
|
4236
|
+
error$1
|
|
4237
|
+
);
|
|
4238
|
+
}
|
|
4239
|
+
}
|
|
4240
|
+
async getVersionByNumber(blockId, versionNumber) {
|
|
4241
|
+
try {
|
|
4242
|
+
const collection = await this.getCollection(storage.TABLE_PROMPT_BLOCK_VERSIONS);
|
|
4243
|
+
const result = await collection.findOne({ blockId, versionNumber });
|
|
4244
|
+
if (!result) {
|
|
4245
|
+
return null;
|
|
4246
|
+
}
|
|
4247
|
+
return this.transformVersion(result);
|
|
4248
|
+
} catch (error$1) {
|
|
4249
|
+
throw new error.MastraError(
|
|
4250
|
+
{
|
|
4251
|
+
id: storage.createStorageErrorId("MONGODB", "GET_PROMPT_BLOCK_VERSION_BY_NUMBER", "FAILED"),
|
|
4252
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4253
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4254
|
+
details: { blockId, versionNumber }
|
|
4255
|
+
},
|
|
4256
|
+
error$1
|
|
4257
|
+
);
|
|
4258
|
+
}
|
|
4259
|
+
}
|
|
4260
|
+
async getLatestVersion(blockId) {
|
|
4261
|
+
try {
|
|
4262
|
+
const collection = await this.getCollection(storage.TABLE_PROMPT_BLOCK_VERSIONS);
|
|
4263
|
+
const result = await collection.find({ blockId }).sort({ versionNumber: -1 }).limit(1).toArray();
|
|
4264
|
+
if (!result || result.length === 0) {
|
|
4265
|
+
return null;
|
|
4266
|
+
}
|
|
4267
|
+
return this.transformVersion(result[0]);
|
|
4268
|
+
} catch (error$1) {
|
|
4269
|
+
throw new error.MastraError(
|
|
4270
|
+
{
|
|
4271
|
+
id: storage.createStorageErrorId("MONGODB", "GET_LATEST_PROMPT_BLOCK_VERSION", "FAILED"),
|
|
4272
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4273
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4274
|
+
details: { blockId }
|
|
4275
|
+
},
|
|
4276
|
+
error$1
|
|
4277
|
+
);
|
|
4278
|
+
}
|
|
4279
|
+
}
|
|
4280
|
+
async listVersions(input) {
|
|
4281
|
+
const { blockId, page = 0, perPage: perPageInput, orderBy } = input;
|
|
4282
|
+
if (page < 0) {
|
|
4283
|
+
throw new error.MastraError(
|
|
4284
|
+
{
|
|
4285
|
+
id: storage.createStorageErrorId("MONGODB", "LIST_PROMPT_BLOCK_VERSIONS", "INVALID_PAGE"),
|
|
4286
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4287
|
+
category: error.ErrorCategory.USER,
|
|
4288
|
+
details: { page }
|
|
4289
|
+
},
|
|
4290
|
+
new Error("page must be >= 0")
|
|
4291
|
+
);
|
|
4292
|
+
}
|
|
4293
|
+
const perPage = storage.normalizePerPage(perPageInput, 20);
|
|
4294
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
4295
|
+
try {
|
|
4296
|
+
const { field, direction } = this.parseVersionOrderBy(orderBy);
|
|
4297
|
+
const collection = await this.getCollection(storage.TABLE_PROMPT_BLOCK_VERSIONS);
|
|
4298
|
+
const total = await collection.countDocuments({ blockId });
|
|
4299
|
+
if (total === 0 || perPage === 0) {
|
|
4300
|
+
return {
|
|
4301
|
+
versions: [],
|
|
4302
|
+
total,
|
|
4303
|
+
page,
|
|
4304
|
+
perPage: perPageForResponse,
|
|
4305
|
+
hasMore: false
|
|
4306
|
+
};
|
|
4307
|
+
}
|
|
4308
|
+
const sortOrder = direction === "ASC" ? 1 : -1;
|
|
4309
|
+
let cursor = collection.find({ blockId }).sort({ [field]: sortOrder }).skip(offset);
|
|
4310
|
+
if (perPageInput !== false) {
|
|
4311
|
+
cursor = cursor.limit(perPage);
|
|
4312
|
+
}
|
|
4313
|
+
const results = await cursor.toArray();
|
|
4314
|
+
const versions = results.map((doc) => this.transformVersion(doc));
|
|
4315
|
+
return {
|
|
4316
|
+
versions,
|
|
4317
|
+
total,
|
|
4318
|
+
page,
|
|
4319
|
+
perPage: perPageForResponse,
|
|
4320
|
+
hasMore: perPageInput !== false && offset + perPage < total
|
|
4321
|
+
};
|
|
4322
|
+
} catch (error$1) {
|
|
4323
|
+
throw new error.MastraError(
|
|
4324
|
+
{
|
|
4325
|
+
id: storage.createStorageErrorId("MONGODB", "LIST_PROMPT_BLOCK_VERSIONS", "FAILED"),
|
|
4326
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4327
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4328
|
+
details: { blockId }
|
|
4329
|
+
},
|
|
4330
|
+
error$1
|
|
4331
|
+
);
|
|
4332
|
+
}
|
|
4333
|
+
}
|
|
4334
|
+
async deleteVersion(id) {
|
|
4335
|
+
try {
|
|
4336
|
+
const collection = await this.getCollection(storage.TABLE_PROMPT_BLOCK_VERSIONS);
|
|
4337
|
+
await collection.deleteOne({ id });
|
|
4338
|
+
} catch (error$1) {
|
|
4339
|
+
throw new error.MastraError(
|
|
4340
|
+
{
|
|
4341
|
+
id: storage.createStorageErrorId("MONGODB", "DELETE_PROMPT_BLOCK_VERSION", "FAILED"),
|
|
4342
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4343
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4344
|
+
details: { versionId: id }
|
|
4345
|
+
},
|
|
4346
|
+
error$1
|
|
4347
|
+
);
|
|
4348
|
+
}
|
|
4349
|
+
}
|
|
4350
|
+
async deleteVersionsByParentId(blockId) {
|
|
4351
|
+
try {
|
|
4352
|
+
const collection = await this.getCollection(storage.TABLE_PROMPT_BLOCK_VERSIONS);
|
|
4353
|
+
await collection.deleteMany({ blockId });
|
|
4354
|
+
} catch (error$1) {
|
|
4355
|
+
throw new error.MastraError(
|
|
4356
|
+
{
|
|
4357
|
+
id: storage.createStorageErrorId("MONGODB", "DELETE_VERSIONS_BY_BLOCK_ID", "FAILED"),
|
|
4358
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4359
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4360
|
+
details: { blockId }
|
|
4361
|
+
},
|
|
4362
|
+
error$1
|
|
4363
|
+
);
|
|
4364
|
+
}
|
|
4365
|
+
}
|
|
4366
|
+
async countVersions(blockId) {
|
|
4367
|
+
try {
|
|
4368
|
+
const collection = await this.getCollection(storage.TABLE_PROMPT_BLOCK_VERSIONS);
|
|
4369
|
+
return await collection.countDocuments({ blockId });
|
|
4370
|
+
} catch (error$1) {
|
|
4371
|
+
throw new error.MastraError(
|
|
4372
|
+
{
|
|
4373
|
+
id: storage.createStorageErrorId("MONGODB", "COUNT_PROMPT_BLOCK_VERSIONS", "FAILED"),
|
|
4374
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4375
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4376
|
+
details: { blockId }
|
|
4377
|
+
},
|
|
4378
|
+
error$1
|
|
4379
|
+
);
|
|
4380
|
+
}
|
|
4381
|
+
}
|
|
4382
|
+
// ==========================================================================
|
|
4383
|
+
// Private Helper Methods
|
|
4384
|
+
// ==========================================================================
|
|
4385
|
+
transformBlock(doc) {
|
|
4386
|
+
const { _id, ...rest } = doc;
|
|
4387
|
+
return {
|
|
4388
|
+
id: rest.id,
|
|
4389
|
+
status: rest.status,
|
|
4390
|
+
activeVersionId: rest.activeVersionId,
|
|
4391
|
+
authorId: rest.authorId,
|
|
4392
|
+
metadata: rest.metadata,
|
|
4393
|
+
createdAt: rest.createdAt instanceof Date ? rest.createdAt : new Date(rest.createdAt),
|
|
4394
|
+
updatedAt: rest.updatedAt instanceof Date ? rest.updatedAt : new Date(rest.updatedAt)
|
|
4395
|
+
};
|
|
4396
|
+
}
|
|
4397
|
+
serializeBlock(block) {
|
|
4398
|
+
return {
|
|
4399
|
+
id: block.id,
|
|
4400
|
+
status: block.status,
|
|
4401
|
+
activeVersionId: block.activeVersionId,
|
|
4402
|
+
authorId: block.authorId,
|
|
4403
|
+
metadata: block.metadata,
|
|
4404
|
+
createdAt: block.createdAt,
|
|
4405
|
+
updatedAt: block.updatedAt
|
|
4406
|
+
};
|
|
4407
|
+
}
|
|
4408
|
+
transformVersion(doc) {
|
|
4409
|
+
const { _id, ...version } = doc;
|
|
4410
|
+
const result = {
|
|
4411
|
+
id: version.id,
|
|
4412
|
+
blockId: version.blockId,
|
|
4413
|
+
versionNumber: version.versionNumber,
|
|
4414
|
+
changedFields: version.changedFields,
|
|
4415
|
+
changeMessage: version.changeMessage,
|
|
4416
|
+
createdAt: version.createdAt instanceof Date ? version.createdAt : new Date(version.createdAt)
|
|
4417
|
+
};
|
|
4418
|
+
for (const field of SNAPSHOT_FIELDS2) {
|
|
4419
|
+
if (version[field] !== void 0) {
|
|
4420
|
+
result[field] = version[field];
|
|
4421
|
+
}
|
|
4422
|
+
}
|
|
4423
|
+
return result;
|
|
4424
|
+
}
|
|
4425
|
+
extractSnapshotFields(version) {
|
|
4426
|
+
const result = {};
|
|
4427
|
+
for (const field of SNAPSHOT_FIELDS2) {
|
|
4428
|
+
if (version[field] !== void 0) {
|
|
4429
|
+
result[field] = version[field];
|
|
4430
|
+
}
|
|
4431
|
+
}
|
|
4432
|
+
return result;
|
|
4433
|
+
}
|
|
4434
|
+
};
|
|
4435
|
+
var SNAPSHOT_FIELDS3 = [
|
|
4436
|
+
"name",
|
|
4437
|
+
"description",
|
|
4438
|
+
"type",
|
|
4439
|
+
"model",
|
|
4440
|
+
"instructions",
|
|
4441
|
+
"scoreRange",
|
|
4442
|
+
"presetConfig",
|
|
4443
|
+
"defaultSampling"
|
|
4444
|
+
];
|
|
4445
|
+
var MongoDBScorerDefinitionsStorage = class _MongoDBScorerDefinitionsStorage extends storage.ScorerDefinitionsStorage {
|
|
4446
|
+
#connector;
|
|
4447
|
+
#skipDefaultIndexes;
|
|
4448
|
+
#indexes;
|
|
4449
|
+
static MANAGED_COLLECTIONS = [storage.TABLE_SCORER_DEFINITIONS, storage.TABLE_SCORER_DEFINITION_VERSIONS];
|
|
4450
|
+
constructor(config) {
|
|
4451
|
+
super();
|
|
4452
|
+
this.#connector = resolveMongoDBConfig(config);
|
|
4453
|
+
this.#skipDefaultIndexes = config.skipDefaultIndexes;
|
|
4454
|
+
this.#indexes = config.indexes?.filter(
|
|
4455
|
+
(idx) => _MongoDBScorerDefinitionsStorage.MANAGED_COLLECTIONS.includes(idx.collection)
|
|
4456
|
+
);
|
|
4457
|
+
}
|
|
4458
|
+
async getCollection(name) {
|
|
4459
|
+
return this.#connector.getCollection(name);
|
|
4460
|
+
}
|
|
4461
|
+
getDefaultIndexDefinitions() {
|
|
4462
|
+
return [
|
|
4463
|
+
{ collection: storage.TABLE_SCORER_DEFINITIONS, keys: { id: 1 }, options: { unique: true } },
|
|
4464
|
+
{ collection: storage.TABLE_SCORER_DEFINITIONS, keys: { createdAt: -1 } },
|
|
4465
|
+
{ collection: storage.TABLE_SCORER_DEFINITIONS, keys: { updatedAt: -1 } },
|
|
4466
|
+
{ collection: storage.TABLE_SCORER_DEFINITIONS, keys: { authorId: 1 } },
|
|
4467
|
+
{ collection: storage.TABLE_SCORER_DEFINITION_VERSIONS, keys: { id: 1 }, options: { unique: true } },
|
|
4468
|
+
{
|
|
4469
|
+
collection: storage.TABLE_SCORER_DEFINITION_VERSIONS,
|
|
4470
|
+
keys: { scorerDefinitionId: 1, versionNumber: -1 },
|
|
4471
|
+
options: { unique: true }
|
|
4472
|
+
},
|
|
4473
|
+
{ collection: storage.TABLE_SCORER_DEFINITION_VERSIONS, keys: { scorerDefinitionId: 1, createdAt: -1 } }
|
|
4474
|
+
];
|
|
4475
|
+
}
|
|
4476
|
+
async createDefaultIndexes() {
|
|
4477
|
+
if (this.#skipDefaultIndexes) {
|
|
4478
|
+
return;
|
|
4479
|
+
}
|
|
4480
|
+
for (const indexDef of this.getDefaultIndexDefinitions()) {
|
|
4481
|
+
try {
|
|
4482
|
+
const collection = await this.getCollection(indexDef.collection);
|
|
4483
|
+
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
4484
|
+
} catch (error) {
|
|
4485
|
+
this.logger?.warn?.(`Failed to create index on ${indexDef.collection}:`, error);
|
|
4486
|
+
}
|
|
4487
|
+
}
|
|
4488
|
+
}
|
|
4489
|
+
async createCustomIndexes() {
|
|
4490
|
+
if (!this.#indexes || this.#indexes.length === 0) {
|
|
4491
|
+
return;
|
|
4492
|
+
}
|
|
4493
|
+
for (const indexDef of this.#indexes) {
|
|
4494
|
+
try {
|
|
4495
|
+
const collection = await this.getCollection(indexDef.collection);
|
|
4496
|
+
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
4497
|
+
} catch (error) {
|
|
4498
|
+
this.logger?.warn?.(`Failed to create custom index on ${indexDef.collection}:`, error);
|
|
4499
|
+
}
|
|
4500
|
+
}
|
|
4501
|
+
}
|
|
4502
|
+
async init() {
|
|
4503
|
+
await this.createDefaultIndexes();
|
|
4504
|
+
await this.createCustomIndexes();
|
|
4505
|
+
}
|
|
4506
|
+
async dangerouslyClearAll() {
|
|
4507
|
+
const versionsCollection = await this.getCollection(storage.TABLE_SCORER_DEFINITION_VERSIONS);
|
|
4508
|
+
await versionsCollection.deleteMany({});
|
|
4509
|
+
const scorerDefinitionsCollection = await this.getCollection(storage.TABLE_SCORER_DEFINITIONS);
|
|
4510
|
+
await scorerDefinitionsCollection.deleteMany({});
|
|
4511
|
+
}
|
|
4512
|
+
// ==========================================================================
|
|
4513
|
+
// Scorer Definition CRUD
|
|
4514
|
+
// ==========================================================================
|
|
4515
|
+
async getById(id) {
|
|
4516
|
+
try {
|
|
4517
|
+
const collection = await this.getCollection(storage.TABLE_SCORER_DEFINITIONS);
|
|
4518
|
+
const result = await collection.findOne({ id });
|
|
4519
|
+
if (!result) {
|
|
4520
|
+
return null;
|
|
4521
|
+
}
|
|
4522
|
+
return this.transformScorerDefinition(result);
|
|
4523
|
+
} catch (error$1) {
|
|
4524
|
+
throw new error.MastraError(
|
|
4525
|
+
{
|
|
4526
|
+
id: storage.createStorageErrorId("MONGODB", "GET_SCORER_DEFINITION_BY_ID", "FAILED"),
|
|
4527
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4528
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4529
|
+
details: { id }
|
|
4530
|
+
},
|
|
4531
|
+
error$1
|
|
4532
|
+
);
|
|
4533
|
+
}
|
|
4534
|
+
}
|
|
4535
|
+
async create(input) {
|
|
4536
|
+
const { scorerDefinition } = input;
|
|
4537
|
+
try {
|
|
4538
|
+
const collection = await this.getCollection(storage.TABLE_SCORER_DEFINITIONS);
|
|
4539
|
+
const existing = await collection.findOne({ id: scorerDefinition.id });
|
|
4540
|
+
if (existing) {
|
|
4541
|
+
throw new error.MastraError({
|
|
4542
|
+
id: storage.createStorageErrorId("MONGODB", "CREATE_SCORER_DEFINITION", "ALREADY_EXISTS"),
|
|
4543
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4544
|
+
category: error.ErrorCategory.USER,
|
|
4545
|
+
details: { id: scorerDefinition.id },
|
|
4546
|
+
text: `Scorer definition with id ${scorerDefinition.id} already exists`
|
|
4547
|
+
});
|
|
4548
|
+
}
|
|
4549
|
+
const now = /* @__PURE__ */ new Date();
|
|
4550
|
+
const newScorerDefinition = {
|
|
4551
|
+
id: scorerDefinition.id,
|
|
4552
|
+
status: "draft",
|
|
4553
|
+
activeVersionId: void 0,
|
|
4554
|
+
authorId: scorerDefinition.authorId,
|
|
4555
|
+
metadata: scorerDefinition.metadata,
|
|
4556
|
+
createdAt: now,
|
|
4557
|
+
updatedAt: now
|
|
4558
|
+
};
|
|
4559
|
+
await collection.insertOne(this.serializeScorerDefinition(newScorerDefinition));
|
|
4560
|
+
const snapshotConfig = {};
|
|
4561
|
+
for (const field of SNAPSHOT_FIELDS3) {
|
|
4562
|
+
if (scorerDefinition[field] !== void 0) {
|
|
4563
|
+
snapshotConfig[field] = scorerDefinition[field];
|
|
4564
|
+
}
|
|
4565
|
+
}
|
|
4566
|
+
const versionId = crypto.randomUUID();
|
|
4567
|
+
await this.createVersion({
|
|
4568
|
+
id: versionId,
|
|
4569
|
+
scorerDefinitionId: scorerDefinition.id,
|
|
4570
|
+
versionNumber: 1,
|
|
4571
|
+
...snapshotConfig,
|
|
4572
|
+
changedFields: Object.keys(snapshotConfig),
|
|
4573
|
+
changeMessage: "Initial version"
|
|
4574
|
+
});
|
|
4575
|
+
return newScorerDefinition;
|
|
4576
|
+
} catch (error$1) {
|
|
4577
|
+
if (error$1 instanceof error.MastraError) {
|
|
4578
|
+
throw error$1;
|
|
4579
|
+
}
|
|
4580
|
+
throw new error.MastraError(
|
|
4581
|
+
{
|
|
4582
|
+
id: storage.createStorageErrorId("MONGODB", "CREATE_SCORER_DEFINITION", "FAILED"),
|
|
4583
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4584
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4585
|
+
details: { id: scorerDefinition.id }
|
|
4586
|
+
},
|
|
4587
|
+
error$1
|
|
4588
|
+
);
|
|
4589
|
+
}
|
|
4590
|
+
}
|
|
4591
|
+
async update(input) {
|
|
4592
|
+
const { id, ...updates } = input;
|
|
4593
|
+
try {
|
|
4594
|
+
const collection = await this.getCollection(storage.TABLE_SCORER_DEFINITIONS);
|
|
4595
|
+
const existingScorerDefinition = await collection.findOne({ id });
|
|
4596
|
+
if (!existingScorerDefinition) {
|
|
4597
|
+
throw new error.MastraError({
|
|
4598
|
+
id: storage.createStorageErrorId("MONGODB", "UPDATE_SCORER_DEFINITION", "NOT_FOUND"),
|
|
4599
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4600
|
+
category: error.ErrorCategory.USER,
|
|
4601
|
+
details: { id },
|
|
4602
|
+
text: `Scorer definition with id ${id} not found`
|
|
4603
|
+
});
|
|
4604
|
+
}
|
|
4605
|
+
const updateDoc = {
|
|
4606
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
4607
|
+
};
|
|
4608
|
+
const metadataFields = {
|
|
4609
|
+
authorId: updates.authorId,
|
|
4610
|
+
activeVersionId: updates.activeVersionId,
|
|
4611
|
+
metadata: updates.metadata,
|
|
4612
|
+
status: updates.status
|
|
4613
|
+
};
|
|
4614
|
+
const configFields = {};
|
|
4615
|
+
for (const field of SNAPSHOT_FIELDS3) {
|
|
4616
|
+
if (updates[field] !== void 0) {
|
|
4617
|
+
configFields[field] = updates[field];
|
|
4618
|
+
}
|
|
4619
|
+
}
|
|
4620
|
+
if (Object.keys(configFields).length > 0) {
|
|
4621
|
+
const latestVersion = await this.getLatestVersion(id);
|
|
4622
|
+
if (!latestVersion) {
|
|
4623
|
+
throw new error.MastraError({
|
|
4624
|
+
id: storage.createStorageErrorId("MONGODB", "UPDATE_SCORER_DEFINITION", "NO_VERSION"),
|
|
4625
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4626
|
+
category: error.ErrorCategory.USER,
|
|
4627
|
+
text: `Cannot update config fields for scorer definition ${id} - no versions exist`,
|
|
4628
|
+
details: { id }
|
|
4629
|
+
});
|
|
4630
|
+
}
|
|
4631
|
+
const existingSnapshot = this.extractSnapshotFields(latestVersion);
|
|
4632
|
+
await this.createVersion({
|
|
4633
|
+
id: crypto.randomUUID(),
|
|
4634
|
+
scorerDefinitionId: id,
|
|
4635
|
+
versionNumber: latestVersion.versionNumber + 1,
|
|
4636
|
+
...existingSnapshot,
|
|
4637
|
+
...configFields,
|
|
4638
|
+
changedFields: Object.keys(configFields),
|
|
4639
|
+
changeMessage: `Updated: ${Object.keys(configFields).join(", ")}`
|
|
4640
|
+
});
|
|
4641
|
+
}
|
|
4642
|
+
if (metadataFields.authorId !== void 0) updateDoc.authorId = metadataFields.authorId;
|
|
4643
|
+
if (metadataFields.activeVersionId !== void 0) {
|
|
4644
|
+
updateDoc.activeVersionId = metadataFields.activeVersionId;
|
|
4645
|
+
if (metadataFields.status === void 0) {
|
|
4646
|
+
updateDoc.status = "published";
|
|
4647
|
+
}
|
|
4648
|
+
}
|
|
4649
|
+
if (metadataFields.status !== void 0) {
|
|
4650
|
+
updateDoc.status = metadataFields.status;
|
|
4651
|
+
}
|
|
4652
|
+
if (metadataFields.metadata !== void 0) {
|
|
4653
|
+
const existingMetadata = existingScorerDefinition.metadata || {};
|
|
4654
|
+
updateDoc.metadata = { ...existingMetadata, ...metadataFields.metadata };
|
|
4655
|
+
}
|
|
4656
|
+
await collection.updateOne({ id }, { $set: updateDoc });
|
|
4657
|
+
const updatedScorerDefinition = await collection.findOne({ id });
|
|
4658
|
+
if (!updatedScorerDefinition) {
|
|
4659
|
+
throw new error.MastraError({
|
|
4660
|
+
id: storage.createStorageErrorId("MONGODB", "UPDATE_SCORER_DEFINITION", "NOT_FOUND_AFTER_UPDATE"),
|
|
4661
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4662
|
+
category: error.ErrorCategory.SYSTEM,
|
|
4663
|
+
text: `Scorer definition with id ${id} was deleted during update`,
|
|
4664
|
+
details: { id }
|
|
4665
|
+
});
|
|
4666
|
+
}
|
|
4667
|
+
return this.transformScorerDefinition(updatedScorerDefinition);
|
|
4668
|
+
} catch (error$1) {
|
|
4669
|
+
if (error$1 instanceof error.MastraError) {
|
|
4670
|
+
throw error$1;
|
|
4671
|
+
}
|
|
4672
|
+
throw new error.MastraError(
|
|
4673
|
+
{
|
|
4674
|
+
id: storage.createStorageErrorId("MONGODB", "UPDATE_SCORER_DEFINITION", "FAILED"),
|
|
4675
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4676
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4677
|
+
details: { id }
|
|
4678
|
+
},
|
|
4679
|
+
error$1
|
|
4680
|
+
);
|
|
4681
|
+
}
|
|
4682
|
+
}
|
|
4683
|
+
async delete(id) {
|
|
4684
|
+
try {
|
|
4685
|
+
await this.deleteVersionsByParentId(id);
|
|
4686
|
+
const collection = await this.getCollection(storage.TABLE_SCORER_DEFINITIONS);
|
|
4687
|
+
await collection.deleteOne({ id });
|
|
4688
|
+
} catch (error$1) {
|
|
4689
|
+
throw new error.MastraError(
|
|
4690
|
+
{
|
|
4691
|
+
id: storage.createStorageErrorId("MONGODB", "DELETE_SCORER_DEFINITION", "FAILED"),
|
|
4692
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4693
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4694
|
+
details: { id }
|
|
4695
|
+
},
|
|
4696
|
+
error$1
|
|
4697
|
+
);
|
|
4698
|
+
}
|
|
4699
|
+
}
|
|
4700
|
+
async list(args) {
|
|
4701
|
+
try {
|
|
4702
|
+
const { page = 0, perPage: perPageInput, orderBy, authorId, metadata } = args || {};
|
|
4703
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
4704
|
+
if (page < 0) {
|
|
4705
|
+
throw new error.MastraError(
|
|
4706
|
+
{
|
|
4707
|
+
id: storage.createStorageErrorId("MONGODB", "LIST_SCORER_DEFINITIONS", "INVALID_PAGE"),
|
|
4708
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4709
|
+
category: error.ErrorCategory.USER,
|
|
4710
|
+
details: { page }
|
|
4711
|
+
},
|
|
4712
|
+
new Error("page must be >= 0")
|
|
4713
|
+
);
|
|
4714
|
+
}
|
|
4715
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
4716
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
4717
|
+
const collection = await this.getCollection(storage.TABLE_SCORER_DEFINITIONS);
|
|
4718
|
+
const filter = {};
|
|
4719
|
+
if (authorId) {
|
|
4720
|
+
filter.authorId = authorId;
|
|
4721
|
+
}
|
|
4722
|
+
if (metadata) {
|
|
4723
|
+
for (const [key, value] of Object.entries(metadata)) {
|
|
4724
|
+
filter[`metadata.${key}`] = value;
|
|
4725
|
+
}
|
|
4726
|
+
}
|
|
4727
|
+
const total = await collection.countDocuments(filter);
|
|
4728
|
+
if (total === 0 || perPage === 0) {
|
|
4729
|
+
return {
|
|
4730
|
+
scorerDefinitions: [],
|
|
4731
|
+
total,
|
|
4732
|
+
page,
|
|
4733
|
+
perPage: perPageForResponse,
|
|
4734
|
+
hasMore: false
|
|
4735
|
+
};
|
|
4736
|
+
}
|
|
4737
|
+
const sortOrder = direction === "ASC" ? 1 : -1;
|
|
4738
|
+
let cursor = collection.find(filter).sort({ [field]: sortOrder }).skip(offset);
|
|
4739
|
+
if (perPageInput !== false) {
|
|
4740
|
+
cursor = cursor.limit(perPage);
|
|
4741
|
+
}
|
|
4742
|
+
const results = await cursor.toArray();
|
|
4743
|
+
const scorerDefinitions = results.map((doc) => this.transformScorerDefinition(doc));
|
|
4744
|
+
return {
|
|
4745
|
+
scorerDefinitions,
|
|
4746
|
+
total,
|
|
4747
|
+
page,
|
|
4748
|
+
perPage: perPageForResponse,
|
|
4749
|
+
hasMore: perPageInput !== false && offset + perPage < total
|
|
4750
|
+
};
|
|
4751
|
+
} catch (error$1) {
|
|
4752
|
+
if (error$1 instanceof error.MastraError) {
|
|
4753
|
+
throw error$1;
|
|
4754
|
+
}
|
|
4755
|
+
throw new error.MastraError(
|
|
4756
|
+
{
|
|
4757
|
+
id: storage.createStorageErrorId("MONGODB", "LIST_SCORER_DEFINITIONS", "FAILED"),
|
|
4758
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4759
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
4760
|
+
},
|
|
4761
|
+
error$1
|
|
4762
|
+
);
|
|
4763
|
+
}
|
|
4764
|
+
}
|
|
4765
|
+
// ==========================================================================
|
|
4766
|
+
// Scorer Definition Version Methods
|
|
4767
|
+
// ==========================================================================
|
|
4768
|
+
async createVersion(input) {
|
|
4769
|
+
try {
|
|
4770
|
+
const collection = await this.getCollection(storage.TABLE_SCORER_DEFINITION_VERSIONS);
|
|
4771
|
+
const now = /* @__PURE__ */ new Date();
|
|
4772
|
+
const versionDoc = {
|
|
4773
|
+
id: input.id,
|
|
4774
|
+
scorerDefinitionId: input.scorerDefinitionId,
|
|
4775
|
+
versionNumber: input.versionNumber,
|
|
4776
|
+
changedFields: input.changedFields ?? void 0,
|
|
4777
|
+
changeMessage: input.changeMessage ?? void 0,
|
|
4778
|
+
createdAt: now
|
|
4779
|
+
};
|
|
4780
|
+
for (const field of SNAPSHOT_FIELDS3) {
|
|
4781
|
+
if (input[field] !== void 0) {
|
|
4782
|
+
versionDoc[field] = input[field];
|
|
4783
|
+
}
|
|
4784
|
+
}
|
|
4785
|
+
await collection.insertOne(versionDoc);
|
|
4786
|
+
return {
|
|
4787
|
+
...input,
|
|
4788
|
+
createdAt: now
|
|
4789
|
+
};
|
|
4790
|
+
} catch (error$1) {
|
|
4791
|
+
throw new error.MastraError(
|
|
4792
|
+
{
|
|
4793
|
+
id: storage.createStorageErrorId("MONGODB", "CREATE_SCORER_DEFINITION_VERSION", "FAILED"),
|
|
4794
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4795
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4796
|
+
details: { versionId: input.id, scorerDefinitionId: input.scorerDefinitionId }
|
|
4797
|
+
},
|
|
4798
|
+
error$1
|
|
4799
|
+
);
|
|
4800
|
+
}
|
|
4801
|
+
}
|
|
4802
|
+
async getVersion(id) {
|
|
4803
|
+
try {
|
|
4804
|
+
const collection = await this.getCollection(storage.TABLE_SCORER_DEFINITION_VERSIONS);
|
|
4805
|
+
const result = await collection.findOne({ id });
|
|
4806
|
+
if (!result) {
|
|
4807
|
+
return null;
|
|
4808
|
+
}
|
|
4809
|
+
return this.transformVersion(result);
|
|
4810
|
+
} catch (error$1) {
|
|
4811
|
+
throw new error.MastraError(
|
|
4812
|
+
{
|
|
4813
|
+
id: storage.createStorageErrorId("MONGODB", "GET_SCORER_DEFINITION_VERSION", "FAILED"),
|
|
4814
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4815
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4816
|
+
details: { versionId: id }
|
|
4817
|
+
},
|
|
4818
|
+
error$1
|
|
4819
|
+
);
|
|
4820
|
+
}
|
|
4821
|
+
}
|
|
4822
|
+
async getVersionByNumber(scorerDefinitionId, versionNumber) {
|
|
4823
|
+
try {
|
|
4824
|
+
const collection = await this.getCollection(storage.TABLE_SCORER_DEFINITION_VERSIONS);
|
|
4825
|
+
const result = await collection.findOne({ scorerDefinitionId, versionNumber });
|
|
4826
|
+
if (!result) {
|
|
4827
|
+
return null;
|
|
4828
|
+
}
|
|
4829
|
+
return this.transformVersion(result);
|
|
4830
|
+
} catch (error$1) {
|
|
4831
|
+
throw new error.MastraError(
|
|
4832
|
+
{
|
|
4833
|
+
id: storage.createStorageErrorId("MONGODB", "GET_SCORER_DEFINITION_VERSION_BY_NUMBER", "FAILED"),
|
|
4834
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4835
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4836
|
+
details: { scorerDefinitionId, versionNumber }
|
|
4837
|
+
},
|
|
4838
|
+
error$1
|
|
4839
|
+
);
|
|
4840
|
+
}
|
|
4841
|
+
}
|
|
4842
|
+
async getLatestVersion(scorerDefinitionId) {
|
|
4843
|
+
try {
|
|
4844
|
+
const collection = await this.getCollection(storage.TABLE_SCORER_DEFINITION_VERSIONS);
|
|
4845
|
+
const result = await collection.find({ scorerDefinitionId }).sort({ versionNumber: -1 }).limit(1).toArray();
|
|
4846
|
+
if (!result || result.length === 0) {
|
|
4847
|
+
return null;
|
|
4848
|
+
}
|
|
4849
|
+
return this.transformVersion(result[0]);
|
|
4850
|
+
} catch (error$1) {
|
|
4851
|
+
throw new error.MastraError(
|
|
4852
|
+
{
|
|
4853
|
+
id: storage.createStorageErrorId("MONGODB", "GET_LATEST_SCORER_DEFINITION_VERSION", "FAILED"),
|
|
4854
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4855
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4856
|
+
details: { scorerDefinitionId }
|
|
4857
|
+
},
|
|
4858
|
+
error$1
|
|
4859
|
+
);
|
|
4860
|
+
}
|
|
4861
|
+
}
|
|
4862
|
+
async listVersions(input) {
|
|
4863
|
+
const { scorerDefinitionId, page = 0, perPage: perPageInput, orderBy } = input;
|
|
4864
|
+
if (page < 0) {
|
|
4865
|
+
throw new error.MastraError(
|
|
4866
|
+
{
|
|
4867
|
+
id: storage.createStorageErrorId("MONGODB", "LIST_SCORER_DEFINITION_VERSIONS", "INVALID_PAGE"),
|
|
4868
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4869
|
+
category: error.ErrorCategory.USER,
|
|
4870
|
+
details: { page }
|
|
4871
|
+
},
|
|
4872
|
+
new Error("page must be >= 0")
|
|
4873
|
+
);
|
|
4874
|
+
}
|
|
4875
|
+
const perPage = storage.normalizePerPage(perPageInput, 20);
|
|
4876
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
4877
|
+
try {
|
|
4878
|
+
const { field, direction } = this.parseVersionOrderBy(orderBy);
|
|
4879
|
+
const collection = await this.getCollection(storage.TABLE_SCORER_DEFINITION_VERSIONS);
|
|
4880
|
+
const total = await collection.countDocuments({ scorerDefinitionId });
|
|
4881
|
+
if (total === 0 || perPage === 0) {
|
|
4882
|
+
return {
|
|
4883
|
+
versions: [],
|
|
4884
|
+
total,
|
|
4885
|
+
page,
|
|
4886
|
+
perPage: perPageForResponse,
|
|
4887
|
+
hasMore: false
|
|
4888
|
+
};
|
|
4889
|
+
}
|
|
4890
|
+
const sortOrder = direction === "ASC" ? 1 : -1;
|
|
4891
|
+
let cursor = collection.find({ scorerDefinitionId }).sort({ [field]: sortOrder }).skip(offset);
|
|
4892
|
+
if (perPageInput !== false) {
|
|
4893
|
+
cursor = cursor.limit(perPage);
|
|
4894
|
+
}
|
|
4895
|
+
const results = await cursor.toArray();
|
|
4896
|
+
const versions = results.map((doc) => this.transformVersion(doc));
|
|
4897
|
+
return {
|
|
4898
|
+
versions,
|
|
4899
|
+
total,
|
|
4900
|
+
page,
|
|
4901
|
+
perPage: perPageForResponse,
|
|
4902
|
+
hasMore: perPageInput !== false && offset + perPage < total
|
|
4903
|
+
};
|
|
4904
|
+
} catch (error$1) {
|
|
4905
|
+
throw new error.MastraError(
|
|
4906
|
+
{
|
|
4907
|
+
id: storage.createStorageErrorId("MONGODB", "LIST_SCORER_DEFINITION_VERSIONS", "FAILED"),
|
|
4908
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4909
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4910
|
+
details: { scorerDefinitionId }
|
|
4911
|
+
},
|
|
4912
|
+
error$1
|
|
4913
|
+
);
|
|
4914
|
+
}
|
|
4915
|
+
}
|
|
4916
|
+
async deleteVersion(id) {
|
|
4917
|
+
try {
|
|
4918
|
+
const collection = await this.getCollection(storage.TABLE_SCORER_DEFINITION_VERSIONS);
|
|
4919
|
+
await collection.deleteOne({ id });
|
|
4920
|
+
} catch (error$1) {
|
|
4921
|
+
throw new error.MastraError(
|
|
4922
|
+
{
|
|
4923
|
+
id: storage.createStorageErrorId("MONGODB", "DELETE_SCORER_DEFINITION_VERSION", "FAILED"),
|
|
4924
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4925
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4926
|
+
details: { versionId: id }
|
|
4927
|
+
},
|
|
4928
|
+
error$1
|
|
4929
|
+
);
|
|
4930
|
+
}
|
|
4931
|
+
}
|
|
4932
|
+
async deleteVersionsByParentId(scorerDefinitionId) {
|
|
4933
|
+
try {
|
|
4934
|
+
const collection = await this.getCollection(storage.TABLE_SCORER_DEFINITION_VERSIONS);
|
|
4935
|
+
await collection.deleteMany({ scorerDefinitionId });
|
|
4936
|
+
} catch (error$1) {
|
|
4937
|
+
throw new error.MastraError(
|
|
4938
|
+
{
|
|
4939
|
+
id: storage.createStorageErrorId("MONGODB", "DELETE_VERSIONS_BY_SCORER_DEFINITION_ID", "FAILED"),
|
|
4940
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4941
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4942
|
+
details: { scorerDefinitionId }
|
|
4943
|
+
},
|
|
4944
|
+
error$1
|
|
4945
|
+
);
|
|
4946
|
+
}
|
|
4947
|
+
}
|
|
4948
|
+
async countVersions(scorerDefinitionId) {
|
|
4949
|
+
try {
|
|
4950
|
+
const collection = await this.getCollection(storage.TABLE_SCORER_DEFINITION_VERSIONS);
|
|
4951
|
+
return await collection.countDocuments({ scorerDefinitionId });
|
|
4952
|
+
} catch (error$1) {
|
|
4953
|
+
throw new error.MastraError(
|
|
4954
|
+
{
|
|
4955
|
+
id: storage.createStorageErrorId("MONGODB", "COUNT_SCORER_DEFINITION_VERSIONS", "FAILED"),
|
|
4956
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4957
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4958
|
+
details: { scorerDefinitionId }
|
|
4959
|
+
},
|
|
4960
|
+
error$1
|
|
4961
|
+
);
|
|
4962
|
+
}
|
|
4963
|
+
}
|
|
4964
|
+
// ==========================================================================
|
|
4965
|
+
// Private Helper Methods
|
|
4966
|
+
// ==========================================================================
|
|
4967
|
+
transformScorerDefinition(doc) {
|
|
4968
|
+
const { _id, ...rest } = doc;
|
|
4969
|
+
return {
|
|
4970
|
+
id: rest.id,
|
|
4971
|
+
status: rest.status,
|
|
4972
|
+
activeVersionId: rest.activeVersionId,
|
|
4973
|
+
authorId: rest.authorId,
|
|
4974
|
+
metadata: rest.metadata,
|
|
4975
|
+
createdAt: rest.createdAt instanceof Date ? rest.createdAt : new Date(rest.createdAt),
|
|
4976
|
+
updatedAt: rest.updatedAt instanceof Date ? rest.updatedAt : new Date(rest.updatedAt)
|
|
4977
|
+
};
|
|
4978
|
+
}
|
|
4979
|
+
serializeScorerDefinition(scorerDefinition) {
|
|
4980
|
+
return {
|
|
4981
|
+
id: scorerDefinition.id,
|
|
4982
|
+
status: scorerDefinition.status,
|
|
4983
|
+
activeVersionId: scorerDefinition.activeVersionId,
|
|
4984
|
+
authorId: scorerDefinition.authorId,
|
|
4985
|
+
metadata: scorerDefinition.metadata,
|
|
4986
|
+
createdAt: scorerDefinition.createdAt,
|
|
4987
|
+
updatedAt: scorerDefinition.updatedAt
|
|
4988
|
+
};
|
|
4989
|
+
}
|
|
4990
|
+
transformVersion(doc) {
|
|
4991
|
+
const { _id, ...version } = doc;
|
|
4992
|
+
const result = {
|
|
4993
|
+
id: version.id,
|
|
4994
|
+
scorerDefinitionId: version.scorerDefinitionId,
|
|
4995
|
+
versionNumber: version.versionNumber,
|
|
4996
|
+
changedFields: version.changedFields,
|
|
4997
|
+
changeMessage: version.changeMessage,
|
|
4998
|
+
createdAt: version.createdAt instanceof Date ? version.createdAt : new Date(version.createdAt)
|
|
4999
|
+
};
|
|
5000
|
+
for (const field of SNAPSHOT_FIELDS3) {
|
|
5001
|
+
if (version[field] !== void 0) {
|
|
5002
|
+
result[field] = version[field];
|
|
5003
|
+
}
|
|
5004
|
+
}
|
|
5005
|
+
return result;
|
|
5006
|
+
}
|
|
5007
|
+
extractSnapshotFields(version) {
|
|
5008
|
+
const result = {};
|
|
5009
|
+
for (const field of SNAPSHOT_FIELDS3) {
|
|
5010
|
+
if (version[field] !== void 0) {
|
|
5011
|
+
result[field] = version[field];
|
|
5012
|
+
}
|
|
5013
|
+
}
|
|
5014
|
+
return result;
|
|
5015
|
+
}
|
|
5016
|
+
};
|
|
3554
5017
|
function transformScoreRow(row) {
|
|
3555
5018
|
return storage.transformScoreRow(row, {
|
|
3556
5019
|
convertTimestamps: true
|
|
@@ -4215,12 +5678,16 @@ var MongoDBStore = class extends storage.MastraCompositeStore {
|
|
|
4215
5678
|
const workflows = new WorkflowsStorageMongoDB(domainConfig);
|
|
4216
5679
|
const observability = new ObservabilityMongoDB(domainConfig);
|
|
4217
5680
|
const agents = new MongoDBAgentsStorage(domainConfig);
|
|
5681
|
+
const promptBlocks = new MongoDBPromptBlocksStorage(domainConfig);
|
|
5682
|
+
const scorerDefinitions = new MongoDBScorerDefinitionsStorage(domainConfig);
|
|
4218
5683
|
this.stores = {
|
|
4219
5684
|
memory,
|
|
4220
5685
|
scores,
|
|
4221
5686
|
workflows,
|
|
4222
5687
|
observability,
|
|
4223
|
-
agents
|
|
5688
|
+
agents,
|
|
5689
|
+
promptBlocks,
|
|
5690
|
+
scorerDefinitions
|
|
4224
5691
|
};
|
|
4225
5692
|
}
|
|
4226
5693
|
/**
|
|
@@ -4342,6 +5809,8 @@ Example Complex Query:
|
|
|
4342
5809
|
exports.MONGODB_PROMPT = MONGODB_PROMPT;
|
|
4343
5810
|
exports.MemoryStorageMongoDB = MemoryStorageMongoDB;
|
|
4344
5811
|
exports.MongoDBAgentsStorage = MongoDBAgentsStorage;
|
|
5812
|
+
exports.MongoDBPromptBlocksStorage = MongoDBPromptBlocksStorage;
|
|
5813
|
+
exports.MongoDBScorerDefinitionsStorage = MongoDBScorerDefinitionsStorage;
|
|
4345
5814
|
exports.MongoDBStore = MongoDBStore;
|
|
4346
5815
|
exports.MongoDBVector = MongoDBVector;
|
|
4347
5816
|
exports.ObservabilityMongoDB = ObservabilityMongoDB;
|