@mastra/mongodb 1.0.0 → 1.1.0-alpha.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
@@ -1,5 +1,5 @@
1
1
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
2
- import { createVectorErrorId, AgentsStorage, TABLE_AGENTS, createStorageErrorId, normalizePerPage, calculatePagination, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, safelyParseJSON, ObservabilityStorage, TABLE_SPANS, listTracesArgsSchema, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, MastraCompositeStore, TraceStatus, transformScoreRow as transformScoreRow$1 } from '@mastra/core/storage';
2
+ import { createVectorErrorId, AgentsStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, createStorageErrorId, normalizePerPage, calculatePagination, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, safelyParseJSON, ObservabilityStorage, TABLE_SPANS, listTracesArgsSchema, toTraceSpans, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, MastraCompositeStore, TraceStatus, transformScoreRow as transformScoreRow$1 } from '@mastra/core/storage';
3
3
  import { MastraVector, validateUpsertInput, validateVectorValues } from '@mastra/core/vector';
4
4
  import { MongoClient } from 'mongodb';
5
5
  import { v4 } from 'uuid';
@@ -11,7 +11,7 @@ import { saveScorePayloadSchema } from '@mastra/core/evals';
11
11
 
12
12
  // package.json
13
13
  var package_default = {
14
- version: "1.0.0"};
14
+ version: "1.1.0-alpha.0"};
15
15
  var MongoDBFilterTranslator = class extends BaseFilterTranslator {
16
16
  getSupportedOperators() {
17
17
  return {
@@ -897,7 +897,7 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends AgentsStorage {
897
897
  #skipDefaultIndexes;
898
898
  #indexes;
899
899
  /** Collections managed by this domain */
900
- static MANAGED_COLLECTIONS = [TABLE_AGENTS];
900
+ static MANAGED_COLLECTIONS = [TABLE_AGENTS, TABLE_AGENT_VERSIONS];
901
901
  constructor(config) {
902
902
  super();
903
903
  this.#connector = resolveMongoDBConfig(config);
@@ -917,7 +917,10 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends AgentsStorage {
917
917
  return [
918
918
  { collection: TABLE_AGENTS, keys: { id: 1 }, options: { unique: true } },
919
919
  { collection: TABLE_AGENTS, keys: { createdAt: -1 } },
920
- { collection: TABLE_AGENTS, keys: { updatedAt: -1 } }
920
+ { collection: TABLE_AGENTS, keys: { updatedAt: -1 } },
921
+ { collection: TABLE_AGENT_VERSIONS, keys: { id: 1 }, options: { unique: true } },
922
+ { collection: TABLE_AGENT_VERSIONS, keys: { agentId: 1, versionNumber: -1 }, options: { unique: true } },
923
+ { collection: TABLE_AGENT_VERSIONS, keys: { agentId: 1, createdAt: -1 } }
921
924
  ];
922
925
  }
923
926
  async createDefaultIndexes() {
@@ -954,8 +957,10 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends AgentsStorage {
954
957
  await this.createCustomIndexes();
955
958
  }
956
959
  async dangerouslyClearAll() {
957
- const collection = await this.getCollection(TABLE_AGENTS);
958
- await collection.deleteMany({});
960
+ const versionsCollection = await this.getCollection(TABLE_AGENT_VERSIONS);
961
+ await versionsCollection.deleteMany({});
962
+ const agentsCollection = await this.getCollection(TABLE_AGENTS);
963
+ await agentsCollection.deleteMany({});
959
964
  }
960
965
  async getAgentById({ id }) {
961
966
  try {
@@ -1041,6 +1046,9 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends AgentsStorage {
1041
1046
  if (updates.outputProcessors !== void 0) updateDoc.outputProcessors = updates.outputProcessors;
1042
1047
  if (updates.memory !== void 0) updateDoc.memory = updates.memory;
1043
1048
  if (updates.scorers !== void 0) updateDoc.scorers = updates.scorers;
1049
+ if (updates.integrationTools !== void 0) updateDoc.integrationTools = updates.integrationTools;
1050
+ if (updates.ownerId !== void 0) updateDoc.ownerId = updates.ownerId;
1051
+ if (updates.activeVersionId !== void 0) updateDoc.activeVersionId = updates.activeVersionId;
1044
1052
  if (updates.metadata !== void 0) {
1045
1053
  const existingMetadata = existingAgent.metadata || {};
1046
1054
  updateDoc.metadata = { ...existingMetadata, ...updates.metadata };
@@ -1074,6 +1082,7 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends AgentsStorage {
1074
1082
  }
1075
1083
  async deleteAgent({ id }) {
1076
1084
  try {
1085
+ await this.deleteVersionsByAgentId(id);
1077
1086
  const collection = await this.getCollection(TABLE_AGENTS);
1078
1087
  await collection.deleteOne({ id });
1079
1088
  } catch (error) {
@@ -1157,6 +1166,212 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends AgentsStorage {
1157
1166
  ...agent
1158
1167
  };
1159
1168
  }
1169
+ // ==========================================================================
1170
+ // Agent Version Methods
1171
+ // ==========================================================================
1172
+ async createVersion(input) {
1173
+ try {
1174
+ const collection = await this.getCollection(TABLE_AGENT_VERSIONS);
1175
+ const now = /* @__PURE__ */ new Date();
1176
+ const versionDoc = {
1177
+ id: input.id,
1178
+ agentId: input.agentId,
1179
+ versionNumber: input.versionNumber,
1180
+ name: input.name ?? void 0,
1181
+ snapshot: input.snapshot,
1182
+ changedFields: input.changedFields ?? void 0,
1183
+ changeMessage: input.changeMessage ?? void 0,
1184
+ createdAt: now
1185
+ };
1186
+ await collection.insertOne(versionDoc);
1187
+ return {
1188
+ ...input,
1189
+ createdAt: now
1190
+ };
1191
+ } catch (error) {
1192
+ throw new MastraError(
1193
+ {
1194
+ id: createStorageErrorId("MONGODB", "CREATE_VERSION", "FAILED"),
1195
+ domain: ErrorDomain.STORAGE,
1196
+ category: ErrorCategory.THIRD_PARTY,
1197
+ details: { versionId: input.id, agentId: input.agentId }
1198
+ },
1199
+ error
1200
+ );
1201
+ }
1202
+ }
1203
+ async getVersion(id) {
1204
+ try {
1205
+ const collection = await this.getCollection(TABLE_AGENT_VERSIONS);
1206
+ const result = await collection.findOne({ id });
1207
+ if (!result) {
1208
+ return null;
1209
+ }
1210
+ return this.transformVersion(result);
1211
+ } catch (error) {
1212
+ throw new MastraError(
1213
+ {
1214
+ id: createStorageErrorId("MONGODB", "GET_VERSION", "FAILED"),
1215
+ domain: ErrorDomain.STORAGE,
1216
+ category: ErrorCategory.THIRD_PARTY,
1217
+ details: { versionId: id }
1218
+ },
1219
+ error
1220
+ );
1221
+ }
1222
+ }
1223
+ async getVersionByNumber(agentId, versionNumber) {
1224
+ try {
1225
+ const collection = await this.getCollection(TABLE_AGENT_VERSIONS);
1226
+ const result = await collection.findOne({ agentId, versionNumber });
1227
+ if (!result) {
1228
+ return null;
1229
+ }
1230
+ return this.transformVersion(result);
1231
+ } catch (error) {
1232
+ throw new MastraError(
1233
+ {
1234
+ id: createStorageErrorId("MONGODB", "GET_VERSION_BY_NUMBER", "FAILED"),
1235
+ domain: ErrorDomain.STORAGE,
1236
+ category: ErrorCategory.THIRD_PARTY,
1237
+ details: { agentId, versionNumber }
1238
+ },
1239
+ error
1240
+ );
1241
+ }
1242
+ }
1243
+ async getLatestVersion(agentId) {
1244
+ try {
1245
+ const collection = await this.getCollection(TABLE_AGENT_VERSIONS);
1246
+ const result = await collection.find({ agentId }).sort({ versionNumber: -1 }).limit(1).toArray();
1247
+ if (!result || result.length === 0) {
1248
+ return null;
1249
+ }
1250
+ return this.transformVersion(result[0]);
1251
+ } catch (error) {
1252
+ throw new MastraError(
1253
+ {
1254
+ id: createStorageErrorId("MONGODB", "GET_LATEST_VERSION", "FAILED"),
1255
+ domain: ErrorDomain.STORAGE,
1256
+ category: ErrorCategory.THIRD_PARTY,
1257
+ details: { agentId }
1258
+ },
1259
+ error
1260
+ );
1261
+ }
1262
+ }
1263
+ async listVersions(input) {
1264
+ const { agentId, page = 0, perPage: perPageInput, orderBy } = input;
1265
+ if (page < 0) {
1266
+ throw new MastraError(
1267
+ {
1268
+ id: createStorageErrorId("MONGODB", "LIST_VERSIONS", "INVALID_PAGE"),
1269
+ domain: ErrorDomain.STORAGE,
1270
+ category: ErrorCategory.USER,
1271
+ details: { page }
1272
+ },
1273
+ new Error("page must be >= 0")
1274
+ );
1275
+ }
1276
+ const perPage = normalizePerPage(perPageInput, 20);
1277
+ const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1278
+ try {
1279
+ const { field, direction } = this.parseVersionOrderBy(orderBy);
1280
+ const collection = await this.getCollection(TABLE_AGENT_VERSIONS);
1281
+ const total = await collection.countDocuments({ agentId });
1282
+ if (total === 0 || perPage === 0) {
1283
+ return {
1284
+ versions: [],
1285
+ total,
1286
+ page,
1287
+ perPage: perPageForResponse,
1288
+ hasMore: false
1289
+ };
1290
+ }
1291
+ const sortOrder = direction === "ASC" ? 1 : -1;
1292
+ let cursor = collection.find({ agentId }).sort({ [field]: sortOrder }).skip(offset);
1293
+ if (perPageInput !== false) {
1294
+ cursor = cursor.limit(perPage);
1295
+ }
1296
+ const results = await cursor.toArray();
1297
+ const versions = results.map((doc) => this.transformVersion(doc));
1298
+ return {
1299
+ versions,
1300
+ total,
1301
+ page,
1302
+ perPage: perPageForResponse,
1303
+ hasMore: perPageInput !== false && offset + perPage < total
1304
+ };
1305
+ } catch (error) {
1306
+ throw new MastraError(
1307
+ {
1308
+ id: createStorageErrorId("MONGODB", "LIST_VERSIONS", "FAILED"),
1309
+ domain: ErrorDomain.STORAGE,
1310
+ category: ErrorCategory.THIRD_PARTY,
1311
+ details: { agentId }
1312
+ },
1313
+ error
1314
+ );
1315
+ }
1316
+ }
1317
+ async deleteVersion(id) {
1318
+ try {
1319
+ const collection = await this.getCollection(TABLE_AGENT_VERSIONS);
1320
+ await collection.deleteOne({ id });
1321
+ } catch (error) {
1322
+ throw new MastraError(
1323
+ {
1324
+ id: createStorageErrorId("MONGODB", "DELETE_VERSION", "FAILED"),
1325
+ domain: ErrorDomain.STORAGE,
1326
+ category: ErrorCategory.THIRD_PARTY,
1327
+ details: { versionId: id }
1328
+ },
1329
+ error
1330
+ );
1331
+ }
1332
+ }
1333
+ async deleteVersionsByAgentId(agentId) {
1334
+ try {
1335
+ const collection = await this.getCollection(TABLE_AGENT_VERSIONS);
1336
+ await collection.deleteMany({ agentId });
1337
+ } catch (error) {
1338
+ throw new MastraError(
1339
+ {
1340
+ id: createStorageErrorId("MONGODB", "DELETE_VERSIONS_BY_AGENT_ID", "FAILED"),
1341
+ domain: ErrorDomain.STORAGE,
1342
+ category: ErrorCategory.THIRD_PARTY,
1343
+ details: { agentId }
1344
+ },
1345
+ error
1346
+ );
1347
+ }
1348
+ }
1349
+ async countVersions(agentId) {
1350
+ try {
1351
+ const collection = await this.getCollection(TABLE_AGENT_VERSIONS);
1352
+ return await collection.countDocuments({ agentId });
1353
+ } catch (error) {
1354
+ throw new MastraError(
1355
+ {
1356
+ id: createStorageErrorId("MONGODB", "COUNT_VERSIONS", "FAILED"),
1357
+ domain: ErrorDomain.STORAGE,
1358
+ category: ErrorCategory.THIRD_PARTY,
1359
+ details: { agentId }
1360
+ },
1361
+ error
1362
+ );
1363
+ }
1364
+ }
1365
+ // ==========================================================================
1366
+ // Private Helper Methods
1367
+ // ==========================================================================
1368
+ transformVersion(doc) {
1369
+ const { _id, ...version } = doc;
1370
+ return {
1371
+ ...version,
1372
+ createdAt: version.createdAt instanceof Date ? version.createdAt : new Date(version.createdAt)
1373
+ };
1374
+ }
1160
1375
  };
1161
1376
  function formatDateForMongoDB(date) {
1162
1377
  return typeof date === "string" ? new Date(date) : date;
@@ -2490,7 +2705,7 @@ Note: This migration may take some time for large collections.
2490
2705
  perPage,
2491
2706
  hasMore: (page + 1) * perPage < count2
2492
2707
  },
2493
- spans: spans2.map((span) => this.transformSpanFromMongo(span))
2708
+ spans: toTraceSpans(spans2.map((span) => this.transformSpanFromMongo(span)))
2494
2709
  };
2495
2710
  }
2496
2711
  const count = await collection.countDocuments(mongoFilter);
@@ -2533,7 +2748,7 @@ Note: This migration may take some time for large collections.
2533
2748
  perPage,
2534
2749
  hasMore: (page + 1) * perPage < count
2535
2750
  },
2536
- spans: spans.map((span) => this.transformSpanFromMongo(span))
2751
+ spans: toTraceSpans(spans.map((span) => this.transformSpanFromMongo(span)))
2537
2752
  };
2538
2753
  } catch (error) {
2539
2754
  throw new MastraError(