@mastra/mongodb 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ import { MastraVector, validateUpsertInput, validateVectorValues } from '@mastra
4
4
  import { MongoClient } from 'mongodb';
5
5
  import { v4 } from 'uuid';
6
6
  import { BaseFilterTranslator } from '@mastra/core/vector/filter';
7
+ import { randomUUID } from 'crypto';
7
8
  import { MessageList } from '@mastra/core/agent';
8
9
  import { saveScorePayloadSchema } from '@mastra/core/evals';
9
10
 
@@ -11,7 +12,7 @@ import { saveScorePayloadSchema } from '@mastra/core/evals';
11
12
 
12
13
  // package.json
13
14
  var package_default = {
14
- version: "1.1.0"};
15
+ version: "1.2.0"};
15
16
  var MongoDBFilterTranslator = class extends BaseFilterTranslator {
16
17
  getSupportedOperators() {
17
18
  return {
@@ -1022,7 +1023,7 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends AgentsStorage {
1022
1023
  };
1023
1024
  await collection.insertOne(this.serializeAgent(newAgent));
1024
1025
  const { id: _id, authorId: _authorId, metadata: _metadata, ...snapshotConfig } = agent;
1025
- const versionId = crypto.randomUUID();
1026
+ const versionId = randomUUID();
1026
1027
  await this.createVersion({
1027
1028
  id: versionId,
1028
1029
  agentId: agent.id,
@@ -1031,9 +1032,6 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends AgentsStorage {
1031
1032
  changedFields: Object.keys(snapshotConfig),
1032
1033
  changeMessage: "Initial version"
1033
1034
  });
1034
- newAgent.activeVersionId = versionId;
1035
- newAgent.status = "published";
1036
- await collection.updateOne({ id: agent.id }, { $set: { activeVersionId: versionId, status: "published" } });
1037
1035
  return newAgent;
1038
1036
  } catch (error) {
1039
1037
  if (error instanceof MastraError) {
@@ -1066,14 +1064,49 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends AgentsStorage {
1066
1064
  const updateDoc = {
1067
1065
  updatedAt: /* @__PURE__ */ new Date()
1068
1066
  };
1069
- if (updates.authorId !== void 0) updateDoc.authorId = updates.authorId;
1070
- if (updates.activeVersionId !== void 0) {
1071
- updateDoc.activeVersionId = updates.activeVersionId;
1072
- updateDoc.status = "published";
1067
+ const metadataFields = {
1068
+ authorId: updates.authorId,
1069
+ activeVersionId: updates.activeVersionId,
1070
+ metadata: updates.metadata
1071
+ };
1072
+ const configFields = {};
1073
+ for (const field of SNAPSHOT_FIELDS) {
1074
+ if (updates[field] !== void 0) {
1075
+ configFields[field] = updates[field];
1076
+ }
1077
+ }
1078
+ if (Object.keys(configFields).length > 0) {
1079
+ const latestVersion = await this.getLatestVersion(id);
1080
+ const nextVersionNumber = latestVersion ? latestVersion.versionNumber + 1 : 1;
1081
+ if (!latestVersion) {
1082
+ throw new MastraError({
1083
+ id: createStorageErrorId("MONGODB", "UPDATE_AGENT", "NO_VERSION"),
1084
+ domain: ErrorDomain.STORAGE,
1085
+ category: ErrorCategory.USER,
1086
+ text: `Cannot update config fields for agent ${id} - no versions exist`,
1087
+ details: { id }
1088
+ });
1089
+ }
1090
+ const versionInput = {
1091
+ id: randomUUID(),
1092
+ agentId: id,
1093
+ versionNumber: nextVersionNumber,
1094
+ ...this.extractSnapshotFields(latestVersion),
1095
+ // Start from latest version
1096
+ ...configFields,
1097
+ // Apply updates
1098
+ changedFields: Object.keys(configFields),
1099
+ changeMessage: `Updated: ${Object.keys(configFields).join(", ")}`
1100
+ };
1101
+ await this.createVersion(versionInput);
1102
+ }
1103
+ if (metadataFields.authorId !== void 0) updateDoc.authorId = metadataFields.authorId;
1104
+ if (metadataFields.activeVersionId !== void 0) {
1105
+ updateDoc.activeVersionId = metadataFields.activeVersionId;
1073
1106
  }
1074
- if (updates.metadata !== void 0) {
1107
+ if (metadataFields.metadata !== void 0) {
1075
1108
  const existingMetadata = existingAgent.metadata || {};
1076
- updateDoc.metadata = { ...existingMetadata, ...updates.metadata };
1109
+ updateDoc.metadata = { ...existingMetadata, ...metadataFields.metadata };
1077
1110
  }
1078
1111
  await collection.updateOne({ id }, { $set: updateDoc });
1079
1112
  const updatedAgent = await collection.findOne({ id });
@@ -1175,6 +1208,123 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends AgentsStorage {
1175
1208
  );
1176
1209
  }
1177
1210
  }
1211
+ async listAgentsResolved(args) {
1212
+ try {
1213
+ const { page = 0, perPage: perPageInput, orderBy } = args || {};
1214
+ const { field, direction } = this.parseOrderBy(orderBy);
1215
+ if (page < 0) {
1216
+ throw new MastraError(
1217
+ {
1218
+ id: createStorageErrorId("MONGODB", "LIST_AGENTS_RESOLVED", "INVALID_PAGE"),
1219
+ domain: ErrorDomain.STORAGE,
1220
+ category: ErrorCategory.USER,
1221
+ details: { page }
1222
+ },
1223
+ new Error("page must be >= 0")
1224
+ );
1225
+ }
1226
+ const perPage = normalizePerPage(perPageInput, 100);
1227
+ const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1228
+ const agentsCollection = await this.getCollection(TABLE_AGENTS);
1229
+ const total = await agentsCollection.countDocuments({});
1230
+ if (total === 0 || perPage === 0) {
1231
+ return {
1232
+ agents: [],
1233
+ total,
1234
+ page,
1235
+ perPage: perPageForResponse,
1236
+ hasMore: false
1237
+ };
1238
+ }
1239
+ const sortOrder = direction === "ASC" ? 1 : -1;
1240
+ const pipeline = [
1241
+ // Sort agents
1242
+ { $sort: { [field]: sortOrder } },
1243
+ // Paginate
1244
+ { $skip: offset }
1245
+ ];
1246
+ if (perPageInput !== false) {
1247
+ pipeline.push({ $limit: perPage });
1248
+ }
1249
+ pipeline.push({
1250
+ $lookup: {
1251
+ from: TABLE_AGENT_VERSIONS,
1252
+ localField: "activeVersionId",
1253
+ foreignField: "id",
1254
+ as: "activeVersion"
1255
+ }
1256
+ });
1257
+ pipeline.push({
1258
+ $lookup: {
1259
+ from: TABLE_AGENT_VERSIONS,
1260
+ let: { agentId: "$id" },
1261
+ pipeline: [
1262
+ { $match: { $expr: { $eq: ["$agentId", "$$agentId"] } } },
1263
+ { $sort: { versionNumber: -1 } },
1264
+ { $limit: 1 }
1265
+ ],
1266
+ as: "latestVersion"
1267
+ }
1268
+ });
1269
+ pipeline.push({
1270
+ $addFields: {
1271
+ version: {
1272
+ $cond: {
1273
+ if: { $gt: [{ $size: "$activeVersion" }, 0] },
1274
+ then: { $arrayElemAt: ["$activeVersion", 0] },
1275
+ else: { $arrayElemAt: ["$latestVersion", 0] }
1276
+ }
1277
+ }
1278
+ }
1279
+ });
1280
+ pipeline.push({
1281
+ $project: {
1282
+ activeVersion: 0,
1283
+ latestVersion: 0
1284
+ }
1285
+ });
1286
+ const results = await agentsCollection.aggregate(pipeline).toArray();
1287
+ const resolvedAgents = results.map((doc) => {
1288
+ const agent = this.transformAgent(doc);
1289
+ if (doc.version) {
1290
+ const version = this.transformVersion(doc.version);
1291
+ const {
1292
+ id: _versionId,
1293
+ agentId: _agentId,
1294
+ versionNumber: _versionNumber,
1295
+ changedFields: _changedFields,
1296
+ changeMessage: _changeMessage,
1297
+ createdAt: _createdAt,
1298
+ ...snapshotConfig
1299
+ } = version;
1300
+ return {
1301
+ ...agent,
1302
+ ...snapshotConfig
1303
+ };
1304
+ }
1305
+ return agent;
1306
+ });
1307
+ return {
1308
+ agents: resolvedAgents,
1309
+ total,
1310
+ page,
1311
+ perPage: perPageForResponse,
1312
+ hasMore: perPageInput !== false && offset + perPage < total
1313
+ };
1314
+ } catch (error) {
1315
+ if (error instanceof MastraError) {
1316
+ throw error;
1317
+ }
1318
+ throw new MastraError(
1319
+ {
1320
+ id: createStorageErrorId("MONGODB", "LIST_AGENTS_RESOLVED", "FAILED"),
1321
+ domain: ErrorDomain.STORAGE,
1322
+ category: ErrorCategory.THIRD_PARTY
1323
+ },
1324
+ error
1325
+ );
1326
+ }
1327
+ }
1178
1328
  /**
1179
1329
  * Transforms a raw MongoDB document into a thin StorageAgentType record.
1180
1330
  * Only returns metadata-level fields (no config/snapshot fields).
@@ -1408,6 +1558,18 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends AgentsStorage {
1408
1558
  // ==========================================================================
1409
1559
  // Private Helper Methods
1410
1560
  // ==========================================================================
1561
+ /**
1562
+ * Extracts just the snapshot config fields from a version.
1563
+ */
1564
+ extractSnapshotFields(version) {
1565
+ const result = {};
1566
+ for (const field of SNAPSHOT_FIELDS) {
1567
+ if (version[field] !== void 0) {
1568
+ result[field] = version[field];
1569
+ }
1570
+ }
1571
+ return result;
1572
+ }
1411
1573
  /**
1412
1574
  * Transforms a raw MongoDB version document into an AgentVersion.
1413
1575
  * Config fields are returned directly (no nested snapshot object).
@@ -1435,12 +1597,14 @@ function formatDateForMongoDB(date) {
1435
1597
  }
1436
1598
 
1437
1599
  // src/storage/domains/memory/index.ts
1600
+ var OM_TABLE = "mastra_observational_memory";
1438
1601
  var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends MemoryStorage {
1602
+ supportsObservationalMemory = true;
1439
1603
  #connector;
1440
1604
  #skipDefaultIndexes;
1441
1605
  #indexes;
1442
1606
  /** Collections managed by this domain */
1443
- static MANAGED_COLLECTIONS = [TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES];
1607
+ static MANAGED_COLLECTIONS = [TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, OM_TABLE];
1444
1608
  constructor(config) {
1445
1609
  super();
1446
1610
  this.#connector = resolveMongoDBConfig(config);
@@ -1475,7 +1639,11 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends MemoryStorage {
1475
1639
  // Resources collection indexes
1476
1640
  { collection: TABLE_RESOURCES, keys: { id: 1 }, options: { unique: true } },
1477
1641
  { collection: TABLE_RESOURCES, keys: { createdAt: -1 } },
1478
- { collection: TABLE_RESOURCES, keys: { updatedAt: -1 } }
1642
+ { collection: TABLE_RESOURCES, keys: { updatedAt: -1 } },
1643
+ // Observational Memory collection indexes
1644
+ { collection: OM_TABLE, keys: { id: 1 }, options: { unique: true } },
1645
+ { collection: OM_TABLE, keys: { lookupKey: 1 } },
1646
+ { collection: OM_TABLE, keys: { lookupKey: 1, generationCount: -1 } }
1479
1647
  ];
1480
1648
  }
1481
1649
  /**
@@ -1710,6 +1878,118 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends MemoryStorage {
1710
1878
  };
1711
1879
  }
1712
1880
  }
1881
+ async listMessagesByResourceId(args) {
1882
+ const { resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
1883
+ if (!resourceId || typeof resourceId !== "string" || resourceId.trim().length === 0) {
1884
+ throw new MastraError(
1885
+ {
1886
+ id: createStorageErrorId("MONGODB", "LIST_MESSAGES", "INVALID_QUERY"),
1887
+ domain: ErrorDomain.STORAGE,
1888
+ category: ErrorCategory.USER,
1889
+ details: { resourceId: resourceId ?? "" }
1890
+ },
1891
+ new Error("resourceId is required")
1892
+ );
1893
+ }
1894
+ if (page < 0) {
1895
+ throw new MastraError(
1896
+ {
1897
+ id: createStorageErrorId("MONGODB", "LIST_MESSAGES", "INVALID_PAGE"),
1898
+ domain: ErrorDomain.STORAGE,
1899
+ category: ErrorCategory.USER,
1900
+ details: { page }
1901
+ },
1902
+ new Error("page must be >= 0")
1903
+ );
1904
+ }
1905
+ const perPage = normalizePerPage(perPageInput, 40);
1906
+ const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1907
+ try {
1908
+ const { field, direction } = this.parseOrderBy(orderBy, "ASC");
1909
+ const sortOrder = direction === "ASC" ? 1 : -1;
1910
+ const collection = await this.getCollection(TABLE_MESSAGES);
1911
+ const query = {};
1912
+ query.resourceId = resourceId;
1913
+ if (filter?.dateRange?.start) {
1914
+ const startOp = filter.dateRange.startExclusive ? "$gt" : "$gte";
1915
+ query.createdAt = { ...query.createdAt, [startOp]: formatDateForMongoDB(filter.dateRange.start) };
1916
+ }
1917
+ if (filter?.dateRange?.end) {
1918
+ const endOp = filter.dateRange.endExclusive ? "$lt" : "$lte";
1919
+ query.createdAt = { ...query.createdAt, [endOp]: formatDateForMongoDB(filter.dateRange.end) };
1920
+ }
1921
+ const total = await collection.countDocuments(query);
1922
+ const messages = [];
1923
+ if (perPage !== 0) {
1924
+ const sortObj = { [field]: sortOrder };
1925
+ let cursor = collection.find(query).sort(sortObj).skip(offset);
1926
+ if (perPageInput !== false) {
1927
+ cursor = cursor.limit(perPage);
1928
+ }
1929
+ const dataResult = await cursor.toArray();
1930
+ messages.push(...dataResult.map((row) => this.parseRow(row)));
1931
+ }
1932
+ if (total === 0 && messages.length === 0 && (!include || include.length === 0)) {
1933
+ return {
1934
+ messages: [],
1935
+ total: 0,
1936
+ page,
1937
+ perPage: perPageForResponse,
1938
+ hasMore: false
1939
+ };
1940
+ }
1941
+ const messageIds = new Set(messages.map((m) => m.id));
1942
+ if (include && include.length > 0) {
1943
+ const includeMessages = await this._getIncludedMessages({ include });
1944
+ if (includeMessages) {
1945
+ for (const includeMsg of includeMessages) {
1946
+ if (!messageIds.has(includeMsg.id)) {
1947
+ messages.push(includeMsg);
1948
+ messageIds.add(includeMsg.id);
1949
+ }
1950
+ }
1951
+ }
1952
+ }
1953
+ const list = new MessageList().add(messages, "memory");
1954
+ let finalMessages = list.get.all.db();
1955
+ finalMessages = finalMessages.sort((a, b) => {
1956
+ const isDateField = field === "createdAt" || field === "updatedAt";
1957
+ const aValue = isDateField ? new Date(a[field]).getTime() : a[field];
1958
+ const bValue = isDateField ? new Date(b[field]).getTime() : b[field];
1959
+ if (typeof aValue === "number" && typeof bValue === "number") {
1960
+ return direction === "ASC" ? aValue - bValue : bValue - aValue;
1961
+ }
1962
+ return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
1963
+ });
1964
+ const hasMore = perPageInput !== false && offset + perPage < total;
1965
+ return {
1966
+ messages: finalMessages,
1967
+ total,
1968
+ page,
1969
+ perPage: perPageForResponse,
1970
+ hasMore
1971
+ };
1972
+ } catch (error) {
1973
+ const mastraError = new MastraError(
1974
+ {
1975
+ id: createStorageErrorId("MONGODB", "LIST_MESSAGES_BY_RESOURCE_ID", "FAILED"),
1976
+ domain: ErrorDomain.STORAGE,
1977
+ category: ErrorCategory.THIRD_PARTY,
1978
+ details: { resourceId }
1979
+ },
1980
+ error
1981
+ );
1982
+ this.logger?.error?.(mastraError.toString());
1983
+ this.logger?.trackException?.(mastraError);
1984
+ return {
1985
+ messages: [],
1986
+ total: 0,
1987
+ page,
1988
+ perPage: perPageForResponse,
1989
+ hasMore: false
1990
+ };
1991
+ }
1992
+ }
1713
1993
  async saveMessages({ messages }) {
1714
1994
  if (messages.length === 0) return { messages: [] };
1715
1995
  try {
@@ -2160,6 +2440,355 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends MemoryStorage {
2160
2440
  );
2161
2441
  }
2162
2442
  }
2443
+ // ============================================
2444
+ // Observational Memory Methods
2445
+ // ============================================
2446
+ getOMKey(threadId, resourceId) {
2447
+ return threadId ? `thread:${threadId}` : `resource:${resourceId}`;
2448
+ }
2449
+ parseOMDocument(doc) {
2450
+ return {
2451
+ id: doc.id,
2452
+ scope: doc.scope,
2453
+ threadId: doc.threadId || null,
2454
+ resourceId: doc.resourceId,
2455
+ createdAt: doc.createdAt instanceof Date ? doc.createdAt : new Date(doc.createdAt),
2456
+ updatedAt: doc.updatedAt instanceof Date ? doc.updatedAt : new Date(doc.updatedAt),
2457
+ lastObservedAt: doc.lastObservedAt ? doc.lastObservedAt instanceof Date ? doc.lastObservedAt : new Date(doc.lastObservedAt) : void 0,
2458
+ originType: doc.originType || "initial",
2459
+ generationCount: Number(doc.generationCount || 0),
2460
+ activeObservations: doc.activeObservations || "",
2461
+ bufferedObservations: doc.activeObservationsPendingUpdate || void 0,
2462
+ totalTokensObserved: Number(doc.totalTokensObserved || 0),
2463
+ observationTokenCount: Number(doc.observationTokenCount || 0),
2464
+ pendingMessageTokens: Number(doc.pendingMessageTokens || 0),
2465
+ isReflecting: Boolean(doc.isReflecting),
2466
+ isObserving: Boolean(doc.isObserving),
2467
+ config: doc.config || {},
2468
+ metadata: doc.metadata || void 0,
2469
+ observedMessageIds: doc.observedMessageIds || void 0,
2470
+ observedTimezone: doc.observedTimezone || void 0
2471
+ };
2472
+ }
2473
+ async getObservationalMemory(threadId, resourceId) {
2474
+ try {
2475
+ const lookupKey = this.getOMKey(threadId, resourceId);
2476
+ const collection = await this.getCollection(OM_TABLE);
2477
+ const doc = await collection.findOne({ lookupKey }, { sort: { generationCount: -1 } });
2478
+ if (!doc) return null;
2479
+ return this.parseOMDocument(doc);
2480
+ } catch (error) {
2481
+ throw new MastraError(
2482
+ {
2483
+ id: createStorageErrorId("MONGODB", "GET_OBSERVATIONAL_MEMORY", "FAILED"),
2484
+ domain: ErrorDomain.STORAGE,
2485
+ category: ErrorCategory.THIRD_PARTY,
2486
+ details: { threadId, resourceId }
2487
+ },
2488
+ error
2489
+ );
2490
+ }
2491
+ }
2492
+ async getObservationalMemoryHistory(threadId, resourceId, limit = 10) {
2493
+ try {
2494
+ const lookupKey = this.getOMKey(threadId, resourceId);
2495
+ const collection = await this.getCollection(OM_TABLE);
2496
+ const docs = await collection.find({ lookupKey }).sort({ generationCount: -1 }).limit(limit).toArray();
2497
+ return docs.map((doc) => this.parseOMDocument(doc));
2498
+ } catch (error) {
2499
+ throw new MastraError(
2500
+ {
2501
+ id: createStorageErrorId("MONGODB", "GET_OBSERVATIONAL_MEMORY_HISTORY", "FAILED"),
2502
+ domain: ErrorDomain.STORAGE,
2503
+ category: ErrorCategory.THIRD_PARTY,
2504
+ details: { threadId, resourceId, limit }
2505
+ },
2506
+ error
2507
+ );
2508
+ }
2509
+ }
2510
+ async initializeObservationalMemory(input) {
2511
+ try {
2512
+ const id = randomUUID();
2513
+ const now = /* @__PURE__ */ new Date();
2514
+ const lookupKey = this.getOMKey(input.threadId, input.resourceId);
2515
+ const record = {
2516
+ id,
2517
+ scope: input.scope,
2518
+ threadId: input.threadId,
2519
+ resourceId: input.resourceId,
2520
+ createdAt: now,
2521
+ updatedAt: now,
2522
+ lastObservedAt: void 0,
2523
+ originType: "initial",
2524
+ generationCount: 0,
2525
+ activeObservations: "",
2526
+ totalTokensObserved: 0,
2527
+ observationTokenCount: 0,
2528
+ pendingMessageTokens: 0,
2529
+ isReflecting: false,
2530
+ isObserving: false,
2531
+ config: input.config,
2532
+ observedTimezone: input.observedTimezone
2533
+ };
2534
+ const collection = await this.getCollection(OM_TABLE);
2535
+ await collection.insertOne({
2536
+ id,
2537
+ lookupKey,
2538
+ scope: input.scope,
2539
+ resourceId: input.resourceId,
2540
+ threadId: input.threadId || null,
2541
+ activeObservations: "",
2542
+ activeObservationsPendingUpdate: null,
2543
+ originType: "initial",
2544
+ config: input.config,
2545
+ generationCount: 0,
2546
+ lastObservedAt: null,
2547
+ lastReflectionAt: null,
2548
+ pendingMessageTokens: 0,
2549
+ totalTokensObserved: 0,
2550
+ observationTokenCount: 0,
2551
+ isObserving: false,
2552
+ isReflecting: false,
2553
+ observedTimezone: input.observedTimezone || null,
2554
+ createdAt: now,
2555
+ updatedAt: now
2556
+ });
2557
+ return record;
2558
+ } catch (error) {
2559
+ throw new MastraError(
2560
+ {
2561
+ id: createStorageErrorId("MONGODB", "INITIALIZE_OBSERVATIONAL_MEMORY", "FAILED"),
2562
+ domain: ErrorDomain.STORAGE,
2563
+ category: ErrorCategory.THIRD_PARTY,
2564
+ details: { threadId: input.threadId, resourceId: input.resourceId }
2565
+ },
2566
+ error
2567
+ );
2568
+ }
2569
+ }
2570
+ async updateActiveObservations(input) {
2571
+ try {
2572
+ const now = /* @__PURE__ */ new Date();
2573
+ const collection = await this.getCollection(OM_TABLE);
2574
+ const safeTokenCount = Number.isFinite(input.tokenCount) && input.tokenCount >= 0 ? input.tokenCount : 0;
2575
+ const updateDoc = {
2576
+ activeObservations: input.observations,
2577
+ lastObservedAt: input.lastObservedAt,
2578
+ pendingMessageTokens: 0,
2579
+ observationTokenCount: safeTokenCount,
2580
+ observedMessageIds: input.observedMessageIds ?? null,
2581
+ updatedAt: now
2582
+ };
2583
+ const result = await collection.updateOne(
2584
+ { id: input.id },
2585
+ {
2586
+ $set: updateDoc,
2587
+ $inc: { totalTokensObserved: safeTokenCount }
2588
+ }
2589
+ );
2590
+ if (result.matchedCount === 0) {
2591
+ throw new MastraError({
2592
+ id: createStorageErrorId("MONGODB", "UPDATE_ACTIVE_OBSERVATIONS", "NOT_FOUND"),
2593
+ text: `Observational memory record not found: ${input.id}`,
2594
+ domain: ErrorDomain.STORAGE,
2595
+ category: ErrorCategory.THIRD_PARTY,
2596
+ details: { id: input.id }
2597
+ });
2598
+ }
2599
+ } catch (error) {
2600
+ if (error instanceof MastraError) {
2601
+ throw error;
2602
+ }
2603
+ throw new MastraError(
2604
+ {
2605
+ id: createStorageErrorId("MONGODB", "UPDATE_ACTIVE_OBSERVATIONS", "FAILED"),
2606
+ domain: ErrorDomain.STORAGE,
2607
+ category: ErrorCategory.THIRD_PARTY,
2608
+ details: { id: input.id }
2609
+ },
2610
+ error
2611
+ );
2612
+ }
2613
+ }
2614
+ async createReflectionGeneration(input) {
2615
+ try {
2616
+ const id = randomUUID();
2617
+ const now = /* @__PURE__ */ new Date();
2618
+ const lookupKey = this.getOMKey(input.currentRecord.threadId, input.currentRecord.resourceId);
2619
+ const record = {
2620
+ id,
2621
+ scope: input.currentRecord.scope,
2622
+ threadId: input.currentRecord.threadId,
2623
+ resourceId: input.currentRecord.resourceId,
2624
+ createdAt: now,
2625
+ updatedAt: now,
2626
+ lastObservedAt: input.currentRecord.lastObservedAt,
2627
+ originType: "reflection",
2628
+ generationCount: input.currentRecord.generationCount + 1,
2629
+ activeObservations: input.reflection,
2630
+ totalTokensObserved: input.currentRecord.totalTokensObserved,
2631
+ observationTokenCount: input.tokenCount,
2632
+ pendingMessageTokens: 0,
2633
+ isReflecting: false,
2634
+ isObserving: false,
2635
+ config: input.currentRecord.config,
2636
+ metadata: input.currentRecord.metadata,
2637
+ observedTimezone: input.currentRecord.observedTimezone
2638
+ };
2639
+ const collection = await this.getCollection(OM_TABLE);
2640
+ await collection.insertOne({
2641
+ id,
2642
+ lookupKey,
2643
+ scope: record.scope,
2644
+ resourceId: record.resourceId,
2645
+ threadId: record.threadId || null,
2646
+ activeObservations: input.reflection,
2647
+ activeObservationsPendingUpdate: null,
2648
+ originType: "reflection",
2649
+ config: record.config,
2650
+ generationCount: input.currentRecord.generationCount + 1,
2651
+ lastObservedAt: record.lastObservedAt || null,
2652
+ lastReflectionAt: now,
2653
+ pendingMessageTokens: record.pendingMessageTokens,
2654
+ totalTokensObserved: record.totalTokensObserved,
2655
+ observationTokenCount: record.observationTokenCount,
2656
+ isObserving: false,
2657
+ isReflecting: false,
2658
+ observedTimezone: record.observedTimezone || null,
2659
+ createdAt: now,
2660
+ updatedAt: now,
2661
+ metadata: record.metadata || null
2662
+ });
2663
+ return record;
2664
+ } catch (error) {
2665
+ throw new MastraError(
2666
+ {
2667
+ id: createStorageErrorId("MONGODB", "CREATE_REFLECTION_GENERATION", "FAILED"),
2668
+ domain: ErrorDomain.STORAGE,
2669
+ category: ErrorCategory.THIRD_PARTY,
2670
+ details: { currentRecordId: input.currentRecord.id }
2671
+ },
2672
+ error
2673
+ );
2674
+ }
2675
+ }
2676
+ async setReflectingFlag(id, isReflecting) {
2677
+ try {
2678
+ const collection = await this.getCollection(OM_TABLE);
2679
+ const result = await collection.updateOne({ id }, { $set: { isReflecting, updatedAt: /* @__PURE__ */ new Date() } });
2680
+ if (result.matchedCount === 0) {
2681
+ throw new MastraError({
2682
+ id: createStorageErrorId("MONGODB", "SET_REFLECTING_FLAG", "NOT_FOUND"),
2683
+ text: `Observational memory record not found: ${id}`,
2684
+ domain: ErrorDomain.STORAGE,
2685
+ category: ErrorCategory.THIRD_PARTY,
2686
+ details: { id, isReflecting }
2687
+ });
2688
+ }
2689
+ } catch (error) {
2690
+ if (error instanceof MastraError) {
2691
+ throw error;
2692
+ }
2693
+ throw new MastraError(
2694
+ {
2695
+ id: createStorageErrorId("MONGODB", "SET_REFLECTING_FLAG", "FAILED"),
2696
+ domain: ErrorDomain.STORAGE,
2697
+ category: ErrorCategory.THIRD_PARTY,
2698
+ details: { id, isReflecting }
2699
+ },
2700
+ error
2701
+ );
2702
+ }
2703
+ }
2704
+ async setObservingFlag(id, isObserving) {
2705
+ try {
2706
+ const collection = await this.getCollection(OM_TABLE);
2707
+ const result = await collection.updateOne({ id }, { $set: { isObserving, updatedAt: /* @__PURE__ */ new Date() } });
2708
+ if (result.matchedCount === 0) {
2709
+ throw new MastraError({
2710
+ id: createStorageErrorId("MONGODB", "SET_OBSERVING_FLAG", "NOT_FOUND"),
2711
+ text: `Observational memory record not found: ${id}`,
2712
+ domain: ErrorDomain.STORAGE,
2713
+ category: ErrorCategory.THIRD_PARTY,
2714
+ details: { id, isObserving }
2715
+ });
2716
+ }
2717
+ } catch (error) {
2718
+ if (error instanceof MastraError) {
2719
+ throw error;
2720
+ }
2721
+ throw new MastraError(
2722
+ {
2723
+ id: createStorageErrorId("MONGODB", "SET_OBSERVING_FLAG", "FAILED"),
2724
+ domain: ErrorDomain.STORAGE,
2725
+ category: ErrorCategory.THIRD_PARTY,
2726
+ details: { id, isObserving }
2727
+ },
2728
+ error
2729
+ );
2730
+ }
2731
+ }
2732
+ async clearObservationalMemory(threadId, resourceId) {
2733
+ try {
2734
+ const lookupKey = this.getOMKey(threadId, resourceId);
2735
+ const collection = await this.getCollection(OM_TABLE);
2736
+ await collection.deleteMany({ lookupKey });
2737
+ } catch (error) {
2738
+ throw new MastraError(
2739
+ {
2740
+ id: createStorageErrorId("MONGODB", "CLEAR_OBSERVATIONAL_MEMORY", "FAILED"),
2741
+ domain: ErrorDomain.STORAGE,
2742
+ category: ErrorCategory.THIRD_PARTY,
2743
+ details: { threadId, resourceId }
2744
+ },
2745
+ error
2746
+ );
2747
+ }
2748
+ }
2749
+ async addPendingMessageTokens(id, tokenCount) {
2750
+ if (typeof tokenCount !== "number" || !Number.isFinite(tokenCount) || tokenCount < 0) {
2751
+ throw new MastraError({
2752
+ id: createStorageErrorId("MONGODB", "ADD_PENDING_MESSAGE_TOKENS", "INVALID_INPUT"),
2753
+ text: `Invalid tokenCount: must be a finite non-negative number, got ${tokenCount}`,
2754
+ domain: ErrorDomain.STORAGE,
2755
+ category: ErrorCategory.USER,
2756
+ details: { id, tokenCount }
2757
+ });
2758
+ }
2759
+ try {
2760
+ const collection = await this.getCollection(OM_TABLE);
2761
+ const result = await collection.updateOne(
2762
+ { id },
2763
+ {
2764
+ $inc: { pendingMessageTokens: tokenCount },
2765
+ $set: { updatedAt: /* @__PURE__ */ new Date() }
2766
+ }
2767
+ );
2768
+ if (result.matchedCount === 0) {
2769
+ throw new MastraError({
2770
+ id: createStorageErrorId("MONGODB", "ADD_PENDING_MESSAGE_TOKENS", "NOT_FOUND"),
2771
+ text: `Observational memory record not found: ${id}`,
2772
+ domain: ErrorDomain.STORAGE,
2773
+ category: ErrorCategory.THIRD_PARTY,
2774
+ details: { id, tokenCount }
2775
+ });
2776
+ }
2777
+ } catch (error) {
2778
+ if (error instanceof MastraError) {
2779
+ throw error;
2780
+ }
2781
+ throw new MastraError(
2782
+ {
2783
+ id: createStorageErrorId("MONGODB", "ADD_PENDING_MESSAGE_TOKENS", "FAILED"),
2784
+ domain: ErrorDomain.STORAGE,
2785
+ category: ErrorCategory.THIRD_PARTY,
2786
+ details: { id, tokenCount }
2787
+ },
2788
+ error
2789
+ );
2790
+ }
2791
+ }
2163
2792
  };
2164
2793
  var ObservabilityMongoDB = class _ObservabilityMongoDB extends ObservabilityStorage {
2165
2794
  #connector;
@@ -3037,7 +3666,7 @@ var ScoresStorageMongoDB = class _ScoresStorageMongoDB extends ScoresStorage {
3037
3666
  }
3038
3667
  try {
3039
3668
  const now = /* @__PURE__ */ new Date();
3040
- const scoreId = crypto.randomUUID();
3669
+ const scoreId = randomUUID();
3041
3670
  const scorer = typeof validatedScore.scorer === "string" ? safelyParseJSON(validatedScore.scorer) : validatedScore.scorer;
3042
3671
  const preprocessStepResult = typeof validatedScore.preprocessStepResult === "string" ? safelyParseJSON(validatedScore.preprocessStepResult) : validatedScore.preprocessStepResult;
3043
3672
  const analyzeStepResult = typeof validatedScore.analyzeStepResult === "string" ? safelyParseJSON(validatedScore.analyzeStepResult) : validatedScore.analyzeStepResult;