@mastra/mongodb 1.18.7 → 1.18.8-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/dist/index.js CHANGED
@@ -9,7 +9,7 @@ import { MessageList } from "@mastra/core/agent";
9
9
  import { saveScorePayloadSchema } from "@mastra/core/evals";
10
10
  import { skillSnapshotFieldValuesEqual } from "@mastra/core/storage/domains/skills";
11
11
  //#region package.json
12
- var version = "1.18.7";
12
+ var version = "1.18.8-alpha.1";
13
13
  //#endregion
14
14
  //#region src/vector/filter.ts
15
15
  /**
@@ -3096,42 +3096,65 @@ var MongoDBDatasetsStorage = class extends DatasetsStorage {
3096
3096
  }
3097
3097
  async _doUpdateItem(args) {
3098
3098
  try {
3099
- const existing = await this.getItemById({ id: args.id });
3100
- if (!existing) throw new MastraError({
3101
- id: createStorageErrorId("MONGODB", "UPDATE_ITEM", "NOT_FOUND"),
3102
- domain: ErrorDomain.STORAGE,
3103
- category: ErrorCategory.USER,
3104
- details: { itemId: args.id }
3105
- });
3106
- if (existing.datasetId !== args.datasetId) throw new MastraError({
3107
- id: createStorageErrorId("MONGODB", "UPDATE_ITEM", "DATASET_MISMATCH"),
3108
- domain: ErrorDomain.STORAGE,
3109
- category: ErrorCategory.USER,
3110
- details: {
3111
- itemId: args.id,
3112
- expectedDatasetId: args.datasetId,
3113
- actualDatasetId: existing.datasetId
3114
- }
3115
- });
3116
- if (!(args.input !== void 0 || args.groundTruth !== void 0 || args.expectedTrajectory !== void 0 || args.toolMocks !== void 0 || args.unmockedToolPolicy !== void 0 || args.scorerIds !== void 0 || args.requestContext !== void 0 || args.metadata !== void 0 || args.source !== void 0)) return existing;
3099
+ if (!(args.input !== void 0 || args.groundTruth !== void 0 || args.expectedTrajectory !== void 0 || args.toolMocks !== void 0 || args.unmockedToolPolicy !== void 0 || args.scorerIds !== void 0 || args.requestContext !== void 0 || args.metadata !== void 0 || args.source !== void 0)) {
3100
+ const existing = await this.getItemById({ id: args.id });
3101
+ if (!existing) throw new MastraError({
3102
+ id: createStorageErrorId("MONGODB", "UPDATE_ITEM", "NOT_FOUND"),
3103
+ domain: ErrorDomain.STORAGE,
3104
+ category: ErrorCategory.USER,
3105
+ details: { itemId: args.id }
3106
+ });
3107
+ if (existing.datasetId !== args.datasetId) throw new MastraError({
3108
+ id: createStorageErrorId("MONGODB", "UPDATE_ITEM", "DATASET_MISMATCH"),
3109
+ domain: ErrorDomain.STORAGE,
3110
+ category: ErrorCategory.USER,
3111
+ details: {
3112
+ itemId: args.id,
3113
+ expectedDatasetId: args.datasetId,
3114
+ actualDatasetId: existing.datasetId
3115
+ }
3116
+ });
3117
+ return existing;
3118
+ }
3117
3119
  const now = /* @__PURE__ */ new Date();
3118
3120
  const versionId = randomUUID();
3119
- const mergedInput = args.input !== void 0 ? args.input : existing.input;
3120
- const mergedGroundTruth = args.groundTruth !== void 0 ? args.groundTruth : existing.groundTruth;
3121
- const mergedExpectedTrajectory = args.expectedTrajectory !== void 0 ? args.expectedTrajectory : existing.expectedTrajectory;
3122
- const mergedToolMocks = args.toolMocks !== void 0 ? args.toolMocks : existing.toolMocks;
3123
- const mergedUnmockedToolPolicy = args.unmockedToolPolicy !== void 0 ? args.unmockedToolPolicy : existing.unmockedToolPolicy;
3124
- const mergedScorerIds = args.scorerIds !== void 0 ? args.scorerIds ?? void 0 : existing.scorerIds;
3125
- const mergedRequestContext = args.requestContext !== void 0 ? args.requestContext : existing.requestContext;
3126
- const mergedMetadata = args.metadata !== void 0 ? args.metadata : existing.metadata;
3127
- const mergedSource = args.source !== void 0 ? args.source : existing.source;
3128
3121
  const datasetsCollection = await this.getCollection(TABLE_DATASETS);
3129
3122
  const itemsCollection = await this.getCollection(TABLE_DATASET_ITEMS);
3130
3123
  const versionsCollection = await this.getCollection(TABLE_DATASET_VERSIONS);
3131
- let newVersion = 0;
3132
- let parentOrganizationId = null;
3133
- let parentProjectId = null;
3124
+ let updated;
3134
3125
  await this.#connector.withTransaction(async (session) => {
3126
+ const row = await itemsCollection.findOne({
3127
+ id: args.id,
3128
+ validTo: null,
3129
+ isDeleted: false
3130
+ }, { session });
3131
+ const existing = row ? this.transformItemRow(row) : null;
3132
+ if (!existing) throw new MastraError({
3133
+ id: createStorageErrorId("MONGODB", "UPDATE_ITEM", "NOT_FOUND"),
3134
+ domain: ErrorDomain.STORAGE,
3135
+ category: ErrorCategory.USER,
3136
+ details: { itemId: args.id }
3137
+ });
3138
+ if (existing.datasetId !== args.datasetId) throw new MastraError({
3139
+ id: createStorageErrorId("MONGODB", "UPDATE_ITEM", "DATASET_MISMATCH"),
3140
+ domain: ErrorDomain.STORAGE,
3141
+ category: ErrorCategory.USER,
3142
+ details: {
3143
+ itemId: args.id,
3144
+ expectedDatasetId: args.datasetId,
3145
+ actualDatasetId: existing.datasetId
3146
+ }
3147
+ });
3148
+ if (existing.metadata?.__purged === true) throw new MastraError({
3149
+ id: "DATASET_ITEM_PURGED",
3150
+ domain: ErrorDomain.STORAGE,
3151
+ category: ErrorCategory.USER,
3152
+ details: {
3153
+ datasetId: args.datasetId,
3154
+ itemId: args.id
3155
+ },
3156
+ text: `Purged dataset item cannot be updated: ${args.id}`
3157
+ });
3135
3158
  const result = await datasetsCollection.findOneAndUpdate({ id: args.datasetId }, { $inc: { version: 1 } }, {
3136
3159
  session,
3137
3160
  returnDocument: "after"
@@ -3142,9 +3165,18 @@ var MongoDBDatasetsStorage = class extends DatasetsStorage {
3142
3165
  category: ErrorCategory.USER,
3143
3166
  details: { datasetId: args.datasetId }
3144
3167
  });
3145
- newVersion = result.version;
3146
- parentOrganizationId = result.organizationId ?? null;
3147
- parentProjectId = result.projectId ?? null;
3168
+ const mergedInput = args.input !== void 0 ? args.input : existing.input;
3169
+ const mergedGroundTruth = args.groundTruth !== void 0 ? args.groundTruth : existing.groundTruth;
3170
+ const mergedExpectedTrajectory = args.expectedTrajectory !== void 0 ? args.expectedTrajectory : existing.expectedTrajectory;
3171
+ const mergedToolMocks = args.toolMocks !== void 0 ? args.toolMocks : existing.toolMocks;
3172
+ const mergedUnmockedToolPolicy = args.unmockedToolPolicy !== void 0 ? args.unmockedToolPolicy : existing.unmockedToolPolicy;
3173
+ const mergedScorerIds = args.scorerIds !== void 0 ? args.scorerIds ?? void 0 : existing.scorerIds;
3174
+ const mergedRequestContext = args.requestContext !== void 0 ? args.requestContext : existing.requestContext;
3175
+ const mergedMetadata = args.metadata !== void 0 ? args.metadata : existing.metadata;
3176
+ const mergedSource = args.source !== void 0 ? args.source : existing.source;
3177
+ const newVersion = result.version;
3178
+ const parentOrganizationId = result.organizationId ?? null;
3179
+ const parentProjectId = result.projectId ?? null;
3148
3180
  await itemsCollection.updateOne({
3149
3181
  id: args.id,
3150
3182
  validTo: null,
@@ -3177,23 +3209,24 @@ var MongoDBDatasetsStorage = class extends DatasetsStorage {
3177
3209
  version: newVersion,
3178
3210
  createdAt: now
3179
3211
  }, { session });
3212
+ updated = {
3213
+ ...existing,
3214
+ datasetVersion: newVersion,
3215
+ organizationId: parentOrganizationId,
3216
+ projectId: parentProjectId,
3217
+ input: mergedInput,
3218
+ groundTruth: mergedGroundTruth,
3219
+ expectedTrajectory: mergedExpectedTrajectory,
3220
+ toolMocks: mergedToolMocks,
3221
+ unmockedToolPolicy: mergedUnmockedToolPolicy,
3222
+ scorerIds: mergedScorerIds,
3223
+ requestContext: mergedRequestContext,
3224
+ metadata: mergedMetadata,
3225
+ source: mergedSource,
3226
+ updatedAt: now
3227
+ };
3180
3228
  });
3181
- return {
3182
- ...existing,
3183
- datasetVersion: newVersion,
3184
- organizationId: parentOrganizationId,
3185
- projectId: parentProjectId,
3186
- input: mergedInput,
3187
- groundTruth: mergedGroundTruth,
3188
- expectedTrajectory: mergedExpectedTrajectory,
3189
- toolMocks: mergedToolMocks,
3190
- unmockedToolPolicy: mergedUnmockedToolPolicy,
3191
- scorerIds: mergedScorerIds,
3192
- requestContext: mergedRequestContext,
3193
- metadata: mergedMetadata,
3194
- source: mergedSource,
3195
- updatedAt: now
3196
- };
3229
+ return updated;
3197
3230
  } catch (error) {
3198
3231
  if (error instanceof MastraError) throw error;
3199
3232
  throw new MastraError({
@@ -3205,24 +3238,29 @@ var MongoDBDatasetsStorage = class extends DatasetsStorage {
3205
3238
  }
3206
3239
  async _doDeleteItem({ id, datasetId }) {
3207
3240
  try {
3208
- const existing = await this.getItemById({ id });
3209
- if (!existing) return;
3210
- if (existing.datasetId !== datasetId) throw new MastraError({
3211
- id: createStorageErrorId("MONGODB", "DELETE_ITEM", "DATASET_MISMATCH"),
3212
- domain: ErrorDomain.STORAGE,
3213
- category: ErrorCategory.USER,
3214
- details: {
3215
- itemId: id,
3216
- expectedDatasetId: datasetId,
3217
- actualDatasetId: existing.datasetId
3218
- }
3219
- });
3220
3241
  const now = /* @__PURE__ */ new Date();
3221
3242
  const versionId = randomUUID();
3222
3243
  const datasetsCollection = await this.getCollection(TABLE_DATASETS);
3223
3244
  const itemsCollection = await this.getCollection(TABLE_DATASET_ITEMS);
3224
3245
  const versionsCollection = await this.getCollection(TABLE_DATASET_VERSIONS);
3225
3246
  await this.#connector.withTransaction(async (session) => {
3247
+ const row = await itemsCollection.findOne({
3248
+ id,
3249
+ validTo: null,
3250
+ isDeleted: false
3251
+ }, { session });
3252
+ const existing = row ? this.transformItemRow(row) : null;
3253
+ if (!existing) return;
3254
+ if (existing.datasetId !== datasetId) throw new MastraError({
3255
+ id: createStorageErrorId("MONGODB", "DELETE_ITEM", "DATASET_MISMATCH"),
3256
+ domain: ErrorDomain.STORAGE,
3257
+ category: ErrorCategory.USER,
3258
+ details: {
3259
+ itemId: id,
3260
+ expectedDatasetId: datasetId,
3261
+ actualDatasetId: existing.datasetId
3262
+ }
3263
+ });
3226
3264
  const result = await datasetsCollection.findOneAndUpdate({ id: datasetId }, { $inc: { version: 1 } }, {
3227
3265
  session,
3228
3266
  returnDocument: "after"
@@ -3444,19 +3482,19 @@ var MongoDBDatasetsStorage = class extends DatasetsStorage {
3444
3482
  details: { datasetId: input.datasetId }
3445
3483
  });
3446
3484
  const itemsCollection = await this.getCollection(TABLE_DATASET_ITEMS);
3447
- const currentItems = (await itemsCollection.find({
3448
- id: { $in: input.itemIds },
3449
- datasetId: input.datasetId,
3450
- validTo: null,
3451
- isDeleted: false
3452
- }).toArray()).map((row) => this.transformItemRow(row));
3453
- if (currentItems.length === 0) return;
3454
3485
  const now = /* @__PURE__ */ new Date();
3455
3486
  const versionId = randomUUID();
3456
3487
  const datasetsCollection = await this.getCollection(TABLE_DATASETS);
3457
3488
  const versionsCollection = await this.getCollection(TABLE_DATASET_VERSIONS);
3458
- const currentIds = currentItems.map((i) => i.id);
3459
3489
  await this.#connector.withTransaction(async (session) => {
3490
+ const currentItems = (await itemsCollection.find({
3491
+ id: { $in: input.itemIds },
3492
+ datasetId: input.datasetId,
3493
+ validTo: null,
3494
+ isDeleted: false
3495
+ }, { session }).toArray()).map((row) => this.transformItemRow(row));
3496
+ if (currentItems.length === 0) return;
3497
+ const currentIds = currentItems.map((i) => i.id);
3460
3498
  const result = await datasetsCollection.findOneAndUpdate({ id: input.datasetId }, { $inc: { version: 1 } }, {
3461
3499
  session,
3462
3500
  returnDocument: "after"
@@ -7937,6 +7975,8 @@ var MemoryStorageMongoDB = class MemoryStorageMongoDB extends MemoryStorage {
7937
7975
  };
7938
7976
  //#endregion
7939
7977
  //#region src/storage/domains/notifications/index.ts
7978
+ /** Ids per updateMany/find command — keeps the `$in` filter far below MongoDB's 16 MiB BSON limit. */
7979
+ const BULK_ID_BATCH_SIZE = 500;
7940
7980
  const statusTimestamp = (status, now) => {
7941
7981
  if (status === "delivered") return { deliveredAt: now };
7942
7982
  if (status === "seen") return { seenAt: now };
@@ -8267,6 +8307,31 @@ var NotificationsMongoDB = class NotificationsMongoDB extends NotificationsStora
8267
8307
  if (!updated) throw new Error(`Notification ${input.id} was not found for thread ${input.threadId}`);
8268
8308
  return updated;
8269
8309
  }
8310
+ async updateNotificationsStatus(input) {
8311
+ const ids = Array.from(new Set(input.ids));
8312
+ if (ids.length === 0) return [];
8313
+ const now = /* @__PURE__ */ new Date();
8314
+ const update = { $set: {
8315
+ status: input.status,
8316
+ ...statusTimestamp(input.status, now),
8317
+ updatedAt: now
8318
+ } };
8319
+ const collection = await this.getCollection();
8320
+ const updated = [];
8321
+ for (let offset = 0; offset < ids.length; offset += BULK_ID_BATCH_SIZE) {
8322
+ const filter = {
8323
+ threadId: input.threadId,
8324
+ id: { $in: ids.slice(offset, offset + BULK_ID_BATCH_SIZE) }
8325
+ };
8326
+ await collection.updateMany(filter, update);
8327
+ const rows = await collection.find({
8328
+ ...filter,
8329
+ status: input.status
8330
+ }).toArray();
8331
+ for (const row of rows) updated.push(rowToNotification(row));
8332
+ }
8333
+ return updated;
8334
+ }
8270
8335
  async findCoalescable(input) {
8271
8336
  if (!input.dedupeKey && !input.coalesceKey) return void 0;
8272
8337
  const row = await (await this.getCollection()).findOne({