@mastra/lance 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
@@ -868,7 +868,7 @@ var StoreMemoryLance = class extends MemoryStorage {
868
868
  hasMore: false
869
869
  };
870
870
  if (perPage === 0 && include && include.length > 0) {
871
- const includedMessages = await this._getIncludedMessages(table, include);
871
+ const includedMessages = await this._getIncludedMessages(table, include, resourceId);
872
872
  const list = new MessageList().add(includedMessages, "memory");
873
873
  return {
874
874
  messages: this._sortMessages(list.get.all.db(), field, direction),
@@ -901,7 +901,7 @@ var StoreMemoryLance = class extends MemoryStorage {
901
901
  const primaryPageCount = messages.length;
902
902
  const messageIds = new Set(messages.map((m) => m.id));
903
903
  if (include && include.length > 0) {
904
- const includedMessages = await this._getIncludedMessages(table, include);
904
+ const includedMessages = await this._getIncludedMessages(table, include, resourceId);
905
905
  for (const includeMsg of includedMessages) if (!messageIds.has(includeMsg.id)) {
906
906
  messages.push(includeMsg);
907
907
  messageIds.add(includeMsg.id);
@@ -920,6 +920,7 @@ var StoreMemoryLance = class extends MemoryStorage {
920
920
  hasMore
921
921
  };
922
922
  } catch (error) {
923
+ if (error instanceof MastraError && error.category === ErrorCategory.USER) throw error;
923
924
  const mastraError = new MastraError({
924
925
  id: createStorageErrorId("LANCE", "LIST_MESSAGES", "FAILED"),
925
926
  domain: ErrorDomain.STORAGE,
@@ -931,13 +932,7 @@ var StoreMemoryLance = class extends MemoryStorage {
931
932
  }, error);
932
933
  this.logger?.error?.(mastraError.toString());
933
934
  this.logger?.trackException?.(mastraError);
934
- return {
935
- messages: [],
936
- total: 0,
937
- page,
938
- perPage: perPageForResponse,
939
- hasMore: false
940
- };
935
+ throw mastraError;
941
936
  }
942
937
  }
943
938
  async saveMessages(args) {
@@ -1045,6 +1040,7 @@ var StoreMemoryLance = class extends MemoryStorage {
1045
1040
  hasMore: offset + perPage < total
1046
1041
  };
1047
1042
  } catch (error) {
1043
+ if (error instanceof MastraError && error.category === ErrorCategory.USER) throw error;
1048
1044
  throw new MastraError({
1049
1045
  id: createStorageErrorId("LANCE", "LIST_THREADS", "FAILED"),
1050
1046
  domain: ErrorDomain.STORAGE,
@@ -1063,16 +1059,25 @@ var StoreMemoryLance = class extends MemoryStorage {
1063
1059
  return direction === "ASC" ? aValue - bValue : bValue - aValue;
1064
1060
  });
1065
1061
  }
1066
- async _getIncludedMessages(table, include) {
1062
+ /**
1063
+ * Fetches the messages named by `include` together with their surrounding context.
1064
+ *
1065
+ * @param table - Open handle to the messages table.
1066
+ * @param include - Message ids to pin, each with an optional before/after window.
1067
+ * @param resourceId - When set, restricts both the pinned messages and their context
1068
+ * to that resource so an id from another resource returns nothing.
1069
+ */
1070
+ async _getIncludedMessages(table, include, resourceId) {
1067
1071
  if (include.length === 0) return [];
1072
+ const resourceCondition = resourceId ? ` AND resourceId = '${this.escapeSql(resourceId)}'` : "";
1068
1073
  const targetIds = include.map((item) => item.id);
1069
1074
  const idCondition = targetIds.length === 1 ? `id = '${this.escapeSql(targetIds[0])}'` : `id IN (${targetIds.map((id) => `'${this.escapeSql(id)}'`).join(", ")})`;
1070
- const targetRecords = await table.query().where(idCondition).toArray();
1075
+ const targetRecords = await table.query().where(`${idCondition}${resourceCondition}`).toArray();
1071
1076
  if (!include.some((item) => item.withPreviousMessages || item.withNextMessages)) return targetRecords.map((row) => this.normalizeMessage(row));
1072
1077
  const threadIdsToFetch = [...new Set(targetRecords.map((r) => r.thread_id))];
1073
1078
  const threadCache = /* @__PURE__ */ new Map();
1074
1079
  for (const tid of threadIdsToFetch) {
1075
- const threadRecords = await table.query().where(`thread_id = '${this.escapeSql(tid)}'`).toArray();
1080
+ const threadRecords = await table.query().where(`thread_id = '${this.escapeSql(tid)}'${resourceCondition}`).toArray();
1076
1081
  threadRecords.sort((a, b) => a.createdAt - b.createdAt);
1077
1082
  threadCache.set(tid, threadRecords);
1078
1083
  }