@mastra/cloudflare 1.2.2 → 1.2.3-alpha.0

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
@@ -1028,6 +1028,22 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
1028
1028
  * @param include - Array of message IDs to include, optionally with context windows
1029
1029
  * @param messageIds - Set to accumulate the message IDs that should be fetched
1030
1030
  */
1031
+ _sortMessages(messages, field, direction) {
1032
+ return messages.sort((a, b) => {
1033
+ const isDateField = field === "createdAt" || field === "updatedAt";
1034
+ const aVal = isDateField ? new Date(a[field]).getTime() : a[field];
1035
+ const bVal = isDateField ? new Date(b[field]).getTime() : b[field];
1036
+ if (aVal == null && bVal == null) return a.id.localeCompare(b.id);
1037
+ if (aVal == null) return 1;
1038
+ if (bVal == null) return -1;
1039
+ if (typeof aVal === "number" && typeof bVal === "number") {
1040
+ const cmp2 = direction === "ASC" ? aVal - bVal : bVal - aVal;
1041
+ return cmp2 !== 0 ? cmp2 : a.id.localeCompare(b.id);
1042
+ }
1043
+ const cmp = direction === "ASC" ? String(aVal).localeCompare(String(bVal)) : String(bVal).localeCompare(String(aVal));
1044
+ return cmp !== 0 ? cmp : a.id.localeCompare(b.id);
1045
+ });
1046
+ }
1031
1047
  async getIncludedMessagesWithContext(include, messageIds) {
1032
1048
  await Promise.all(
1033
1049
  include.map(async (item) => {
@@ -1186,6 +1202,26 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
1186
1202
  );
1187
1203
  }
1188
1204
  const { field, direction } = this.parseOrderBy(orderBy, "ASC");
1205
+ if (perPage === 0 && include && include.length > 0) {
1206
+ const includedMessageIds = /* @__PURE__ */ new Set();
1207
+ await this.getIncludedMessagesWithContext(include, includedMessageIds);
1208
+ const includedMessages2 = await this.fetchAndParseMessagesFromMultipleThreads(
1209
+ Array.from(includedMessageIds),
1210
+ include,
1211
+ void 0
1212
+ );
1213
+ const list2 = new MessageList().add(includedMessages2, "memory");
1214
+ return {
1215
+ messages: this._sortMessages(list2.get.all.db(), field, direction),
1216
+ total: 0,
1217
+ page,
1218
+ perPage: perPageForResponse,
1219
+ hasMore: false
1220
+ };
1221
+ }
1222
+ if (perPage === 0 && (!include || include.length === 0)) {
1223
+ return { messages: [], total: 0, page, perPage: perPageForResponse, hasMore: false };
1224
+ }
1189
1225
  const threadMessageIds = /* @__PURE__ */ new Set();
1190
1226
  for (const tid of threadIds) {
1191
1227
  try {
@@ -1210,15 +1246,6 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
1210
1246
  filter?.dateRange
1211
1247
  );
1212
1248
  const total = filteredThreadMessages.length;
1213
- if (perPage === 0 && (!include || include.length === 0)) {
1214
- return {
1215
- messages: [],
1216
- total,
1217
- page,
1218
- perPage: perPageForResponse,
1219
- hasMore: offset < total
1220
- };
1221
- }
1222
1249
  filteredThreadMessages.sort((a, b) => {
1223
1250
  const timeA = new Date(a.createdAt).getTime();
1224
1251
  const timeB = new Date(b.createdAt).getTime();
@@ -1290,21 +1317,7 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
1290
1317
  prepared,
1291
1318
  "memory"
1292
1319
  );
1293
- let finalMessages = list.get.all.db();
1294
- finalMessages = finalMessages.sort((a, b) => {
1295
- const isDateField = field === "createdAt" || field === "updatedAt";
1296
- const aVal = isDateField ? new Date(a[field]).getTime() : a[field];
1297
- const bVal = isDateField ? new Date(b[field]).getTime() : b[field];
1298
- if (aVal == null && bVal == null) return a.id.localeCompare(b.id);
1299
- if (aVal == null) return 1;
1300
- if (bVal == null) return -1;
1301
- if (typeof aVal === "number" && typeof bVal === "number") {
1302
- const cmp2 = direction === "ASC" ? aVal - bVal : bVal - aVal;
1303
- return cmp2 !== 0 ? cmp2 : a.id.localeCompare(b.id);
1304
- }
1305
- const cmp = direction === "ASC" ? String(aVal).localeCompare(String(bVal)) : String(bVal).localeCompare(String(aVal));
1306
- return cmp !== 0 ? cmp : a.id.localeCompare(b.id);
1307
- });
1320
+ const finalMessages = this._sortMessages(list.get.all.db(), field, direction);
1308
1321
  const threadIdSet = new Set(threadIds);
1309
1322
  const returnedThreadMessageIds = new Set(
1310
1323
  finalMessages.filter((m) => m.threadId && threadIdSet.has(m.threadId)).map((m) => m.id)