@mastra/dynamodb 1.2.2 → 1.3.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/dist/index.js CHANGED
@@ -1745,7 +1745,10 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1745
1745
  hasMore: false
1746
1746
  };
1747
1747
  if (perPage === 0 && include && include.length > 0) {
1748
- const includeMessages = await this._getIncludedMessages({ include });
1748
+ const includeMessages = await this._getIncludedMessages({
1749
+ include,
1750
+ resourceId
1751
+ });
1749
1752
  const list = new MessageList().add(includeMessages, "memory");
1750
1753
  return {
1751
1754
  messages: this._sortMessages(list.get.all.db(), field, direction),
@@ -1834,7 +1837,10 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1834
1837
  const messageIds = new Set(paginatedMessages.map((m) => m.id));
1835
1838
  let includeMessages = [];
1836
1839
  if (include && include.length > 0) {
1837
- includeMessages = await this._getIncludedMessages({ include });
1840
+ includeMessages = await this._getIncludedMessages({
1841
+ include,
1842
+ resourceId
1843
+ });
1838
1844
  for (const includeMsg of includeMessages) if (!messageIds.has(includeMsg.id)) {
1839
1845
  paginatedMessages.push(includeMsg);
1840
1846
  messageIds.add(includeMsg.id);
@@ -1852,6 +1858,7 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1852
1858
  hasMore
1853
1859
  };
1854
1860
  } catch (error) {
1861
+ if (error instanceof MastraError && error.category === ErrorCategory.USER) throw error;
1855
1862
  const mastraError = new MastraError({
1856
1863
  id: createStorageErrorId("DYNAMODB", "LIST_MESSAGES", "FAILED"),
1857
1864
  domain: ErrorDomain.STORAGE,
@@ -1863,13 +1870,7 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1863
1870
  }, error);
1864
1871
  this.logger?.error?.(mastraError.toString());
1865
1872
  this.logger?.trackException?.(mastraError);
1866
- return {
1867
- messages: [],
1868
- total: 0,
1869
- page,
1870
- perPage: perPageForResponse,
1871
- hasMore: false
1872
- };
1873
+ throw mastraError;
1873
1874
  }
1874
1875
  }
1875
1876
  async saveMessages(args) {
@@ -1989,7 +1990,8 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
1989
1990
  hasMore: offset + perPage < total
1990
1991
  };
1991
1992
  } catch (error) {
1992
- throw new MastraError({
1993
+ if (error instanceof MastraError && error.category === ErrorCategory.USER) throw error;
1994
+ const mastraError = new MastraError({
1993
1995
  id: createStorageErrorId("DYNAMODB", "LIST_THREADS", "FAILED"),
1994
1996
  domain: ErrorDomain.STORAGE,
1995
1997
  category: ErrorCategory.THIRD_PARTY,
@@ -2000,6 +2002,9 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
2000
2002
  perPage: perPageForResponse
2001
2003
  }
2002
2004
  }, error);
2005
+ this.logger?.error?.(mastraError.toString());
2006
+ this.logger?.trackException?.(mastraError);
2007
+ throw mastraError;
2003
2008
  }
2004
2009
  }
2005
2010
  _sortMessages(messages, field, direction) {
@@ -2010,7 +2015,14 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
2010
2015
  return direction === "ASC" ? aValue - bValue : bValue - aValue;
2011
2016
  });
2012
2017
  }
2013
- async _getIncludedMessages({ include }) {
2018
+ /**
2019
+ * Fetches the messages named by `include` together with their surrounding context.
2020
+ *
2021
+ * @param include - Message ids to pin, each with an optional before/after window.
2022
+ * @param resourceId - When set, restricts both the pinned messages and their context
2023
+ * to that resource so an id from another resource returns nothing.
2024
+ */
2025
+ async _getIncludedMessages({ include, resourceId }) {
2014
2026
  if (!include?.length) return [];
2015
2027
  const targetResults = await Promise.all(include.map((inc) => this.service.entities.message.get({
2016
2028
  entity: "message",
@@ -2023,7 +2035,7 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
2023
2035
  data: null
2024
2036
  }))));
2025
2037
  const targetMap = /* @__PURE__ */ new Map();
2026
- for (const { id, data } of targetResults) if (data) {
2038
+ for (const { id, data } of targetResults) if (data && (!resourceId || data.resourceId === resourceId)) {
2027
2039
  const createdAt = typeof data.createdAt === "string" ? data.createdAt : new Date(data.createdAt).toISOString();
2028
2040
  targetMap.set(id, {
2029
2041
  threadId: data.threadId,
@@ -2031,7 +2043,7 @@ var MemoryStorageDynamoDB = class extends MemoryStorage {
2031
2043
  });
2032
2044
  }
2033
2045
  if (targetMap.size === 0) return [];
2034
- const parseQueryMessages = (data) => data.map((item) => this.parseMessageData(item)).filter((msg) => "content" in msg && typeof msg.content === "object");
2046
+ const parseQueryMessages = (data) => data.map((item) => this.parseMessageData(item)).filter((msg) => "content" in msg && typeof msg.content === "object").filter((msg) => !resourceId || msg.resourceId === resourceId);
2035
2047
  const includeMessages = [];
2036
2048
  for (const includeItem of include) {
2037
2049
  const { id, withPreviousMessages = 0, withNextMessages = 0 } = includeItem;