@mastra/mongodb 1.17.0-alpha.0 → 1.17.0-alpha.2

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.17.0-alpha.0";
12
+ var version = "1.17.0-alpha.2";
13
13
  //#endregion
14
14
  //#region src/vector/filter.ts
15
15
  /**
@@ -5404,12 +5404,23 @@ var MemoryStorageMongoDB = class MemoryStorageMongoDB extends MemoryStorage {
5404
5404
  return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
5405
5405
  });
5406
5406
  }
5407
- async _getIncludedMessages({ include }) {
5407
+ /**
5408
+ * Fetches the messages named by `include` together with their surrounding context.
5409
+ *
5410
+ * @param include - Message ids to pin, each with an optional before/after window.
5411
+ * @param resourceId - When set, restricts both the pinned messages and their context
5412
+ * to that resource so an id from another resource returns nothing.
5413
+ */
5414
+ async _getIncludedMessages({ include, resourceId }) {
5408
5415
  if (!include || include.length === 0) return null;
5409
5416
  const collection = await this.getCollection(TABLE_MESSAGES);
5417
+ const resourceFilter = resourceId ? { resourceId } : {};
5410
5418
  const targetIds = include.map((inc) => inc.id).filter(Boolean);
5411
5419
  if (targetIds.length === 0) return null;
5412
- const targetDocs = await collection.find({ id: { $in: targetIds } }, { projection: {
5420
+ const targetDocs = await collection.find({
5421
+ id: { $in: targetIds },
5422
+ ...resourceFilter
5423
+ }, { projection: {
5413
5424
  id: 1,
5414
5425
  thread_id: 1,
5415
5426
  createdAt: 1
@@ -5426,7 +5437,8 @@ var MemoryStorageMongoDB = class MemoryStorageMongoDB extends MemoryStorage {
5426
5437
  if (!target) continue;
5427
5438
  const prevMessages = await collection.find({
5428
5439
  thread_id: target.threadId,
5429
- createdAt: { $lte: target.createdAt }
5440
+ createdAt: { $lte: target.createdAt },
5441
+ ...resourceFilter
5430
5442
  }).sort({
5431
5443
  createdAt: -1,
5432
5444
  id: -1
@@ -5435,7 +5447,8 @@ var MemoryStorageMongoDB = class MemoryStorageMongoDB extends MemoryStorage {
5435
5447
  if (withNextMessages > 0) {
5436
5448
  const nextMessages = await collection.find({
5437
5449
  thread_id: target.threadId,
5438
- createdAt: { $gt: target.createdAt }
5450
+ createdAt: { $gt: target.createdAt },
5451
+ ...resourceFilter
5439
5452
  }).sort({
5440
5453
  createdAt: 1,
5441
5454
  id: 1
@@ -5510,7 +5523,10 @@ var MemoryStorageMongoDB = class MemoryStorageMongoDB extends MemoryStorage {
5510
5523
  hasMore: false
5511
5524
  };
5512
5525
  if (perPage === 0 && include && include.length > 0) {
5513
- const includeMessages = await this._getIncludedMessages({ include });
5526
+ const includeMessages = await this._getIncludedMessages({
5527
+ include,
5528
+ resourceId
5529
+ });
5514
5530
  const list = new MessageList().add(includeMessages ?? [], "memory");
5515
5531
  return {
5516
5532
  messages: this._sortMessages(list.get.all.db(), field, direction),
@@ -5546,7 +5562,10 @@ var MemoryStorageMongoDB = class MemoryStorageMongoDB extends MemoryStorage {
5546
5562
  };
5547
5563
  const messageIds = new Set(messages.map((m) => m.id));
5548
5564
  if (include && include.length > 0) {
5549
- const includeMessages = await this._getIncludedMessages({ include });
5565
+ const includeMessages = await this._getIncludedMessages({
5566
+ include,
5567
+ resourceId
5568
+ });
5550
5569
  if (includeMessages) {
5551
5570
  for (const includeMsg of includeMessages) if (!messageIds.has(includeMsg.id)) {
5552
5571
  messages.push(includeMsg);
@@ -5627,7 +5646,10 @@ var MemoryStorageMongoDB = class MemoryStorageMongoDB extends MemoryStorage {
5627
5646
  hasMore: false
5628
5647
  };
5629
5648
  if (perPage === 0 && include && include.length > 0) {
5630
- const includeMessages = await this._getIncludedMessages({ include });
5649
+ const includeMessages = await this._getIncludedMessages({
5650
+ include,
5651
+ resourceId
5652
+ });
5631
5653
  if (!includeMessages || includeMessages.length === 0) return {
5632
5654
  messages: [],
5633
5655
  total: 0,
@@ -5670,7 +5692,10 @@ var MemoryStorageMongoDB = class MemoryStorageMongoDB extends MemoryStorage {
5670
5692
  };
5671
5693
  const messageIds = new Set(messages.map((m) => m.id));
5672
5694
  if (include && include.length > 0) {
5673
- const includeMessages = await this._getIncludedMessages({ include });
5695
+ const includeMessages = await this._getIncludedMessages({
5696
+ include,
5697
+ resourceId
5698
+ });
5674
5699
  if (includeMessages) {
5675
5700
  for (const includeMsg of includeMessages) if (!messageIds.has(includeMsg.id)) {
5676
5701
  messages.push(includeMsg);
@@ -5978,7 +6003,7 @@ var MemoryStorageMongoDB = class MemoryStorageMongoDB extends MemoryStorage {
5978
6003
  const now = /* @__PURE__ */ new Date();
5979
6004
  const updatedThread = {
5980
6005
  ...thread,
5981
- title,
6006
+ title: title ?? thread.title,
5982
6007
  metadata: {
5983
6008
  ...thread.metadata,
5984
6009
  ...metadata
@@ -5987,7 +6012,7 @@ var MemoryStorageMongoDB = class MemoryStorageMongoDB extends MemoryStorage {
5987
6012
  };
5988
6013
  try {
5989
6014
  await (await this.getCollection(TABLE_THREADS)).updateOne({ id }, { $set: {
5990
- title,
6015
+ title: updatedThread.title,
5991
6016
  metadata: updatedThread.metadata,
5992
6017
  updatedAt: now
5993
6018
  } });