@mastra/mongodb 1.18.6-alpha.0 → 1.18.6

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.
@@ -3,7 +3,7 @@ name: mastra-mongodb
3
3
  description: Documentation for @mastra/mongodb. Use when working with @mastra/mongodb APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/mongodb"
6
- version: "1.18.6-alpha.0"
6
+ version: "1.18.6"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.18.6-alpha.0",
2
+ "version": "1.18.6",
3
3
  "package": "@mastra/mongodb",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -10,7 +10,7 @@ let _mastra_core_agent = require("@mastra/core/agent");
10
10
  let _mastra_core_evals = require("@mastra/core/evals");
11
11
  let _mastra_core_storage_domains_skills = require("@mastra/core/storage/domains/skills");
12
12
  //#region package.json
13
- var version = "1.18.6-alpha.0";
13
+ var version = "1.18.6";
14
14
  //#endregion
15
15
  //#region src/vector/filter.ts
16
16
  /**
@@ -1396,6 +1396,10 @@ var MongoDBConnector = class MongoDBConnector {
1396
1396
  if (this.#handler) return this.#handler.getCollection(collectionName);
1397
1397
  return (await this.getConnection()).collection(collectionName);
1398
1398
  }
1399
+ async collectionExists(collectionName) {
1400
+ if (this.#handler) return true;
1401
+ return (await this.getConnection()).listCollections({ name: collectionName }).hasNext();
1402
+ }
1399
1403
  /**
1400
1404
  * Returns true when the deployment supports multi-document transactions
1401
1405
  * (replica set or sharded cluster). Standalone servers and custom connector
@@ -2792,6 +2796,8 @@ var MongoDBDatasetsStorage = class extends _mastra_core_storage.DatasetsStorage
2792
2796
  };
2793
2797
  }
2794
2798
  transformItemRow(row) {
2799
+ const metadata = (typeof row.metadata === "string" ? (0, _mastra_core_storage.safelyParseJSON)(row.metadata) : row.metadata) ?? void 0;
2800
+ const emptyValue = metadata?.__purged === true ? null : void 0;
2795
2801
  return {
2796
2802
  id: row.id,
2797
2803
  datasetId: row.datasetId,
@@ -2802,12 +2808,12 @@ var MongoDBDatasetsStorage = class extends _mastra_core_storage.DatasetsStorage
2802
2808
  input: typeof row.input === "string" ? (0, _mastra_core_storage.safelyParseJSON)(row.input) : row.input,
2803
2809
  groundTruth: typeof row.groundTruth === "string" ? (0, _mastra_core_storage.safelyParseJSON)(row.groundTruth) : row.groundTruth,
2804
2810
  expectedTrajectory: typeof row.expectedTrajectory === "string" ? (0, _mastra_core_storage.safelyParseJSON)(row.expectedTrajectory) : row.expectedTrajectory,
2805
- toolMocks: (typeof row.toolMocks === "string" ? (0, _mastra_core_storage.safelyParseJSON)(row.toolMocks) : row.toolMocks) ?? void 0,
2806
- unmockedToolPolicy: row.unmockedToolPolicy ?? void 0,
2807
- scorerIds: (typeof row.scorerIds === "string" ? (0, _mastra_core_storage.safelyParseJSON)(row.scorerIds) : row.scorerIds) ?? void 0,
2808
- requestContext: typeof row.requestContext === "string" ? (0, _mastra_core_storage.safelyParseJSON)(row.requestContext) : row.requestContext,
2809
- metadata: typeof row.metadata === "string" ? (0, _mastra_core_storage.safelyParseJSON)(row.metadata) : row.metadata,
2810
- source: typeof row.source === "string" ? (0, _mastra_core_storage.safelyParseJSON)(row.source) : row.source,
2811
+ toolMocks: (typeof row.toolMocks === "string" ? (0, _mastra_core_storage.safelyParseJSON)(row.toolMocks) : row.toolMocks) ?? emptyValue,
2812
+ unmockedToolPolicy: row.unmockedToolPolicy ?? emptyValue,
2813
+ scorerIds: (typeof row.scorerIds === "string" ? (0, _mastra_core_storage.safelyParseJSON)(row.scorerIds) : row.scorerIds) ?? emptyValue,
2814
+ requestContext: (typeof row.requestContext === "string" ? (0, _mastra_core_storage.safelyParseJSON)(row.requestContext) : row.requestContext) ?? emptyValue,
2815
+ metadata,
2816
+ source: (typeof row.source === "string" ? (0, _mastra_core_storage.safelyParseJSON)(row.source) : row.source) ?? emptyValue,
2811
2817
  createdAt: (0, _mastra_core_storage.ensureDate)(row.createdAt),
2812
2818
  updatedAt: (0, _mastra_core_storage.ensureDate)(row.updatedAt)
2813
2819
  };
@@ -3273,6 +3279,79 @@ var MongoDBDatasetsStorage = class extends _mastra_core_storage.DatasetsStorage
3273
3279
  }, error);
3274
3280
  }
3275
3281
  }
3282
+ async _doPurgeItem({ id, datasetId }) {
3283
+ try {
3284
+ if (!await this.#connector.supportsTransactions()) throw new _mastra_core_error.MastraError({
3285
+ id: "MONGODB_DATASET_ITEM_PURGE_REQUIRES_TRANSACTIONS",
3286
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
3287
+ category: _mastra_core_error.ErrorCategory.USER,
3288
+ text: "Dataset item purge requires a MongoDB replica set or sharded deployment with transaction support.",
3289
+ details: {
3290
+ datasetId,
3291
+ itemId: id
3292
+ }
3293
+ });
3294
+ const itemsCollection = await this.getCollection(_mastra_core_storage.TABLE_DATASET_ITEMS);
3295
+ const experimentsCollection = await this.getCollection(_mastra_core_storage.TABLE_EXPERIMENTS);
3296
+ const experimentResultsCollection = await this.getCollection(_mastra_core_storage.TABLE_EXPERIMENT_RESULTS);
3297
+ const experimentCollectionsExist = await this.#connector.collectionExists(_mastra_core_storage.TABLE_EXPERIMENTS) && await this.#connector.collectionExists(_mastra_core_storage.TABLE_EXPERIMENT_RESULTS);
3298
+ const metadata = {
3299
+ __purged: true,
3300
+ purgedAt: (/* @__PURE__ */ new Date()).toISOString()
3301
+ };
3302
+ await this.#connector.withTransaction(async (session) => {
3303
+ if (!await itemsCollection.findOneAndUpdate({
3304
+ id,
3305
+ datasetId
3306
+ }, { $inc: { purgeBarrierRevision: 1 } }, {
3307
+ projection: { id: 1 },
3308
+ returnDocument: "after",
3309
+ session,
3310
+ sort: { datasetVersion: -1 }
3311
+ })) return;
3312
+ await itemsCollection.updateMany({
3313
+ id,
3314
+ datasetId
3315
+ }, { $set: {
3316
+ input: null,
3317
+ groundTruth: null,
3318
+ expectedTrajectory: null,
3319
+ toolMocks: null,
3320
+ unmockedToolPolicy: null,
3321
+ scorerIds: null,
3322
+ requestContext: null,
3323
+ metadata,
3324
+ source: null
3325
+ } }, { session });
3326
+ if (experimentCollectionsExist) {
3327
+ const experimentIds = await experimentsCollection.find({ datasetId }, {
3328
+ projection: { id: 1 },
3329
+ session
3330
+ }).map((experiment) => experiment.id).toArray();
3331
+ if (experimentIds.length > 0) await experimentResultsCollection.updateMany({
3332
+ itemId: id,
3333
+ experimentId: { $in: experimentIds }
3334
+ }, { $set: {
3335
+ input: null,
3336
+ output: null,
3337
+ groundTruth: null,
3338
+ error: null,
3339
+ toolMockReport: null,
3340
+ tags: null,
3341
+ comment: null,
3342
+ metadata
3343
+ } }, { session });
3344
+ }
3345
+ });
3346
+ } catch (error) {
3347
+ if (error instanceof _mastra_core_error.MastraError) throw error;
3348
+ throw new _mastra_core_error.MastraError({
3349
+ id: (0, _mastra_core_storage.createStorageErrorId)("MONGODB", "PURGE_ITEM", "FAILED"),
3350
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
3351
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY
3352
+ }, error);
3353
+ }
3354
+ }
3276
3355
  async _doBatchInsertItems(input) {
3277
3356
  try {
3278
3357
  if (input.items.length === 0) return [];
@@ -3704,7 +3783,7 @@ function transformExperimentResultRow(row) {
3704
3783
  itemDatasetVersion: row.itemDatasetVersion != null ? Number(row.itemDatasetVersion) : null,
3705
3784
  organizationId: row.organizationId ?? null,
3706
3785
  projectId: row.projectId ?? null,
3707
- input: parseJsonField(row.input),
3786
+ input: row.input === null ? null : parseJsonField(row.input),
3708
3787
  output: parseJsonField(row.output) ?? null,
3709
3788
  groundTruth: parseJsonField(row.groundTruth) ?? null,
3710
3789
  metadata: parseJsonField(row.metadata) ?? null,
@@ -3746,6 +3825,27 @@ var MongoDBExperimentsStorage = class MongoDBExperimentsStorage extends _mastra_
3746
3825
  async getCollection(name) {
3747
3826
  return this.#connector.getCollection(name);
3748
3827
  }
3828
+ async #withPurgeBarrier(experimentId, itemId, fn) {
3829
+ const experiments = await this.getCollection(_mastra_core_storage.TABLE_EXPERIMENTS);
3830
+ const items = await this.getCollection(_mastra_core_storage.TABLE_DATASET_ITEMS);
3831
+ return this.#connector.withTransaction(async (session) => {
3832
+ const datasetId = (await experiments.findOne({ id: experimentId }, { session }))?.datasetId ?? null;
3833
+ let purgeMetadata = null;
3834
+ if (datasetId) {
3835
+ const item = await items.findOneAndUpdate({
3836
+ id: itemId,
3837
+ datasetId
3838
+ }, { $inc: { purgeBarrierRevision: 1 } }, {
3839
+ projection: { metadata: 1 },
3840
+ returnDocument: "after",
3841
+ session,
3842
+ sort: { datasetVersion: -1 }
3843
+ });
3844
+ purgeMetadata = item?.metadata?.__purged === true ? item.metadata : null;
3845
+ }
3846
+ return fn(session, purgeMetadata);
3847
+ });
3848
+ }
3749
3849
  /**
3750
3850
  * Prune whole experiments older than the `experiments` policy's `maxAge`.
3751
3851
  *
@@ -4081,52 +4181,34 @@ var MongoDBExperimentsStorage = class MongoDBExperimentsStorage extends _mastra_
4081
4181
  async addExperimentResult(input) {
4082
4182
  const id = input.id ?? (0, crypto$1.randomUUID)();
4083
4183
  const now = /* @__PURE__ */ new Date();
4084
- const doc = {
4085
- id,
4086
- experimentId: input.experimentId,
4087
- itemId: input.itemId,
4088
- itemDatasetVersion: input.itemDatasetVersion ?? null,
4089
- organizationId: input.organizationId ?? null,
4090
- projectId: input.projectId ?? null,
4091
- input: input.input,
4092
- output: input.output ?? null,
4093
- groundTruth: input.groundTruth ?? null,
4094
- metadata: input.metadata ?? null,
4095
- error: input.error ?? null,
4096
- startedAt: input.startedAt,
4097
- completedAt: input.completedAt,
4098
- retryCount: input.retryCount,
4099
- attempt: input.attempt ?? 0,
4100
- traceId: input.traceId ?? null,
4101
- status: input.status ?? null,
4102
- tags: input.tags ?? null,
4103
- toolMockReport: input.toolMockReport ?? null,
4104
- createdAt: now
4105
- };
4106
4184
  try {
4107
- await (await this.getCollection(_mastra_core_storage.TABLE_EXPERIMENT_RESULTS)).insertOne(doc);
4108
- return {
4109
- id,
4110
- experimentId: input.experimentId,
4111
- itemId: input.itemId,
4112
- itemDatasetVersion: input.itemDatasetVersion ?? null,
4113
- organizationId: input.organizationId ?? null,
4114
- projectId: input.projectId ?? null,
4115
- input: input.input,
4116
- output: input.output ?? null,
4117
- groundTruth: input.groundTruth ?? null,
4118
- metadata: input.metadata ?? null,
4119
- error: input.error ?? null,
4120
- startedAt: input.startedAt,
4121
- completedAt: input.completedAt,
4122
- retryCount: input.retryCount,
4123
- attempt: input.attempt ?? 0,
4124
- traceId: input.traceId ?? null,
4125
- status: input.status ?? null,
4126
- tags: input.tags ?? null,
4127
- toolMockReport: input.toolMockReport ?? null,
4128
- createdAt: now
4129
- };
4185
+ return await this.#withPurgeBarrier(input.experimentId, input.itemId, async (session, marker) => {
4186
+ const collection = await this.getCollection(_mastra_core_storage.TABLE_EXPERIMENT_RESULTS);
4187
+ const doc = {
4188
+ id,
4189
+ experimentId: input.experimentId,
4190
+ itemId: input.itemId,
4191
+ itemDatasetVersion: input.itemDatasetVersion ?? null,
4192
+ organizationId: input.organizationId ?? null,
4193
+ projectId: input.projectId ?? null,
4194
+ input: marker ? null : input.input,
4195
+ output: marker ? null : input.output ?? null,
4196
+ groundTruth: marker ? null : input.groundTruth ?? null,
4197
+ metadata: marker ?? input.metadata ?? null,
4198
+ error: marker ? null : input.error ?? null,
4199
+ startedAt: input.startedAt,
4200
+ completedAt: input.completedAt,
4201
+ retryCount: input.retryCount,
4202
+ attempt: input.attempt ?? 0,
4203
+ traceId: input.traceId ?? null,
4204
+ status: input.status ?? null,
4205
+ tags: marker ? null : input.tags ?? null,
4206
+ toolMockReport: marker ? null : input.toolMockReport ?? null,
4207
+ createdAt: now
4208
+ };
4209
+ await collection.insertOne(doc, { session });
4210
+ return transformExperimentResultRow(doc);
4211
+ });
4130
4212
  } catch (error) {
4131
4213
  throw new _mastra_core_error.MastraError({
4132
4214
  id: (0, _mastra_core_storage.createStorageErrorId)("MONGODB", "ADD_EXPERIMENT_RESULT", "FAILED"),
@@ -4139,37 +4221,41 @@ var MongoDBExperimentsStorage = class MongoDBExperimentsStorage extends _mastra_
4139
4221
  async upsertExperimentResult(input) {
4140
4222
  const attempt = input.attempt ?? 0;
4141
4223
  try {
4142
- return transformExperimentResultRow(await (await this.getCollection(_mastra_core_storage.TABLE_EXPERIMENT_RESULTS)).findOneAndUpdate({
4143
- experimentId: input.experimentId,
4144
- itemId: input.itemId,
4145
- $or: [{ attempt }, ...attempt === 0 ? [{ attempt: null }, { attempt: { $exists: false } }] : []]
4146
- }, {
4147
- $set: {
4148
- itemDatasetVersion: input.itemDatasetVersion ?? null,
4149
- organizationId: input.organizationId ?? null,
4150
- projectId: input.projectId ?? null,
4151
- input: input.input,
4152
- output: input.output ?? null,
4153
- groundTruth: input.groundTruth ?? null,
4154
- metadata: input.metadata ?? null,
4155
- error: input.error ?? null,
4156
- startedAt: input.startedAt,
4157
- completedAt: input.completedAt,
4158
- retryCount: input.retryCount,
4159
- attempt,
4160
- traceId: input.traceId ?? null,
4161
- status: input.status ?? null,
4162
- tags: input.tags ?? null,
4163
- toolMockReport: input.toolMockReport ?? null
4164
- },
4165
- $setOnInsert: {
4166
- id: (0, crypto$1.randomUUID)(),
4167
- createdAt: /* @__PURE__ */ new Date()
4168
- }
4169
- }, {
4170
- upsert: true,
4171
- returnDocument: "after"
4172
- }));
4224
+ return await this.#withPurgeBarrier(input.experimentId, input.itemId, async (session, marker) => {
4225
+ return transformExperimentResultRow(await (await this.getCollection(_mastra_core_storage.TABLE_EXPERIMENT_RESULTS)).findOneAndUpdate({
4226
+ experimentId: input.experimentId,
4227
+ itemId: input.itemId,
4228
+ $or: [{ attempt }, ...attempt === 0 ? [{ attempt: null }, { attempt: { $exists: false } }] : []]
4229
+ }, {
4230
+ $set: {
4231
+ itemDatasetVersion: input.itemDatasetVersion ?? null,
4232
+ organizationId: input.organizationId ?? null,
4233
+ projectId: input.projectId ?? null,
4234
+ input: marker ? null : input.input,
4235
+ output: marker ? null : input.output ?? null,
4236
+ groundTruth: marker ? null : input.groundTruth ?? null,
4237
+ metadata: marker ?? input.metadata ?? null,
4238
+ error: marker ? null : input.error ?? null,
4239
+ startedAt: input.startedAt,
4240
+ completedAt: input.completedAt,
4241
+ retryCount: input.retryCount,
4242
+ attempt,
4243
+ traceId: input.traceId ?? null,
4244
+ status: input.status ?? null,
4245
+ tags: marker ? null : input.tags ?? null,
4246
+ toolMockReport: marker ? null : input.toolMockReport ?? null,
4247
+ ...marker ? { comment: null } : {}
4248
+ },
4249
+ $setOnInsert: {
4250
+ id: (0, crypto$1.randomUUID)(),
4251
+ createdAt: /* @__PURE__ */ new Date()
4252
+ }
4253
+ }, {
4254
+ upsert: true,
4255
+ returnDocument: "after",
4256
+ session
4257
+ }));
4258
+ });
4173
4259
  } catch (error) {
4174
4260
  throw new _mastra_core_error.MastraError({
4175
4261
  id: (0, _mastra_core_storage.createStorageErrorId)("MONGODB", "UPSERT_EXPERIMENT_RESULT", "FAILED"),
@@ -4198,14 +4284,30 @@ var MongoDBExperimentsStorage = class MongoDBExperimentsStorage extends _mastra_
4198
4284
  const collection = await this.getCollection(_mastra_core_storage.TABLE_EXPERIMENT_RESULTS);
4199
4285
  const filter = { id: input.id };
4200
4286
  if (input.experimentId) filter.experimentId = input.experimentId;
4201
- const result = await collection.findOneAndUpdate(filter, { $set: updateFields }, { returnDocument: "after" });
4202
- if (!result) throw new _mastra_core_error.MastraError({
4287
+ const existing = await collection.findOne(filter);
4288
+ if (!existing) throw new _mastra_core_error.MastraError({
4203
4289
  id: (0, _mastra_core_storage.createStorageErrorId)("MONGODB", "UPDATE_EXPERIMENT_RESULT", "NOT_FOUND"),
4204
4290
  domain: _mastra_core_error.ErrorDomain.STORAGE,
4205
4291
  category: _mastra_core_error.ErrorCategory.USER,
4206
4292
  details: { resultId: input.id }
4207
4293
  });
4208
- return transformExperimentResultRow(result);
4294
+ return await this.#withPurgeBarrier(existing.experimentId, existing.itemId, async (session, marker) => {
4295
+ if (marker) {
4296
+ updateFields.tags = null;
4297
+ updateFields.comment = null;
4298
+ }
4299
+ const result = await collection.findOneAndUpdate(filter, { $set: updateFields }, {
4300
+ returnDocument: "after",
4301
+ session
4302
+ });
4303
+ if (!result) throw new _mastra_core_error.MastraError({
4304
+ id: (0, _mastra_core_storage.createStorageErrorId)("MONGODB", "UPDATE_EXPERIMENT_RESULT", "NOT_FOUND"),
4305
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
4306
+ category: _mastra_core_error.ErrorCategory.USER,
4307
+ details: { resultId: input.id }
4308
+ });
4309
+ return transformExperimentResultRow(result);
4310
+ });
4209
4311
  } catch (error) {
4210
4312
  if (error instanceof _mastra_core_error.MastraError) throw error;
4211
4313
  throw new _mastra_core_error.MastraError({