@mastra/redis 1.4.0-alpha.0 → 1.4.0-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/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @mastra/redis
2
2
 
3
+ ## 1.4.0-alpha.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Fixed resource-scoped message includes across storage adapters so included context cannot cross resource boundaries. ([#20984](https://github.com/mastra-ai/mastra/pull/20984))
8
+
9
+ - Updated dependencies [[`6445eba`](https://github.com/mastra-ai/mastra/commit/6445eba6020abac681aba1cc9289f446cb400cbe), [`df31eb0`](https://github.com/mastra-ai/mastra/commit/df31eb0c7087d782a0d9346e467f9a4af4b0eef6), [`fcd0667`](https://github.com/mastra-ai/mastra/commit/fcd0667a4e378be35c9a1b1eb19cce78fbfd7282), [`bab06b1`](https://github.com/mastra-ai/mastra/commit/bab06b18923873a584bdfc71a6b4ec7fb4727fb7)]:
10
+ - @mastra/core@1.58.0-alpha.5
11
+
3
12
  ## 1.4.0-alpha.0
4
13
 
5
14
  ### Minor Changes
@@ -3,7 +3,7 @@ name: mastra-redis
3
3
  description: Documentation for @mastra/redis. Use when working with @mastra/redis APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/redis"
6
- version: "1.4.0-alpha.0"
6
+ version: "1.4.0-alpha.1"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.4.0-alpha.0",
2
+ "version": "1.4.0-alpha.1",
3
3
  "package": "@mastra/redis",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -432,16 +432,36 @@ var StoreMemoryRedis = class extends _mastra_core_storage.MemoryStorage {
432
432
  if (message.threadId) await this.client.set(getMessageIndexKey(messageId), message.threadId);
433
433
  return message.threadId || null;
434
434
  }
435
- async getIncludedMessages(include) {
435
+ /**
436
+ * Fetches the messages named by `include` together with their surrounding context.
437
+ *
438
+ * @param include - Message ids to pin, each with an optional before/after window.
439
+ * @param resourceId - When set, drops any pinned or context message owned by another
440
+ * resource so an id from another resource returns nothing.
441
+ */
442
+ async getIncludedMessages(include, resourceId) {
436
443
  if (!include?.length) return [];
437
444
  const messageIds = /* @__PURE__ */ new Set();
438
445
  const messageIdToThreadIds = {};
439
446
  for (const item of include) {
440
447
  const itemThreadId = await this.getThreadIdForMessage(item.id);
441
448
  if (!itemThreadId) continue;
449
+ const itemThreadMessagesKey = getThreadMessagesKey(itemThreadId);
450
+ if (resourceId !== void 0) {
451
+ const threadMessageIds = await this.client.zRange(itemThreadMessagesKey, 0, -1);
452
+ const threadMessages = (await this.client.mGet(threadMessageIds.map((id) => getMessageKey(itemThreadId, id)))).filter((data) => data !== null).map((data) => JSON.parse(data)).filter((message) => message.resourceId === resourceId);
453
+ const targetIndex = threadMessages.findIndex((message) => message.id === item.id);
454
+ if (targetIndex === -1) continue;
455
+ const start = Math.max(0, targetIndex - (item.withPreviousMessages ?? 0));
456
+ const end = Math.min(threadMessages.length, targetIndex + (item.withNextMessages ?? 0) + 1);
457
+ for (const message of threadMessages.slice(start, end)) {
458
+ messageIds.add(message.id);
459
+ messageIdToThreadIds[message.id] = itemThreadId;
460
+ }
461
+ continue;
462
+ }
442
463
  messageIds.add(item.id);
443
464
  messageIdToThreadIds[item.id] = itemThreadId;
444
- const itemThreadMessagesKey = getThreadMessagesKey(itemThreadId);
445
465
  const rank = await this.client.zRank(itemThreadMessagesKey, item.id);
446
466
  if (rank === null) continue;
447
467
  if (item.withPreviousMessages) {
@@ -458,7 +478,8 @@ var StoreMemoryRedis = class extends _mastra_core_storage.MemoryStorage {
458
478
  }
459
479
  if (messageIds.size === 0) return [];
460
480
  const keysToFetch = Array.from(messageIds).map((id) => getMessageKey(messageIdToThreadIds[id], id));
461
- return (await this.client.mGet(keysToFetch)).filter((data) => data !== null).map((data) => JSON.parse(data));
481
+ const includedMessages = (await this.client.mGet(keysToFetch)).filter((data) => data !== null).map((data) => JSON.parse(data));
482
+ return resourceId ? includedMessages.filter((message) => message.resourceId === resourceId) : includedMessages;
462
483
  }
463
484
  parseStoredMessage(storedMessage) {
464
485
  const defaultMessageContent = {
@@ -562,7 +583,7 @@ var StoreMemoryRedis = class extends _mastra_core_storage.MemoryStorage {
562
583
  hasMore: false
563
584
  };
564
585
  let includedMessages = [];
565
- if (include && include.length > 0) includedMessages = (await this.getIncludedMessages(include)).map(this.parseStoredMessage);
586
+ if (include && include.length > 0) includedMessages = (await this.getIncludedMessages(include, resourceId)).map(this.parseStoredMessage);
566
587
  if (perPage === 0 && include && include.length > 0) return {
567
588
  messages: new _mastra_core_agent.MessageList().add(includedMessages, "memory").get.all.db().sort((a, b) => {
568
589
  const aValue = getFieldValue(a);