@mastra/cloudflare 1.0.0-beta.1 → 1.0.0-beta.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/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @mastra/cloudflare
2
2
 
3
+ ## 1.0.0-beta.2
4
+
5
+ ### Patch Changes
6
+
7
+ - Fix message sorting in listMessages when using semantic recall (include parameter). Messages are now always sorted by createdAt instead of storage order, ensuring correct chronological ordering of conversation history. ([#10491](https://github.com/mastra-ai/mastra/pull/10491))
8
+
9
+ - Updated dependencies [[`21a15de`](https://github.com/mastra-ai/mastra/commit/21a15de369fe82aac26bb642ed7be73505475e8b), [`feb7ee4`](https://github.com/mastra-ai/mastra/commit/feb7ee4d09a75edb46c6669a3beaceec78811747), [`b0e2ea5`](https://github.com/mastra-ai/mastra/commit/b0e2ea5b52c40fae438b9e2f7baee6f0f89c5442), [`c456e01`](https://github.com/mastra-ai/mastra/commit/c456e0149e3c176afcefdbd9bb1d2c5917723725), [`ab035c2`](https://github.com/mastra-ai/mastra/commit/ab035c2ef6d8cc7bb25f06f1a38508bd9e6f126b), [`1a46a56`](https://github.com/mastra-ai/mastra/commit/1a46a566f45a3fcbadc1cf36bf86d351f264bfa3), [`3cf540b`](https://github.com/mastra-ai/mastra/commit/3cf540b9fbfea8f4fc8d3a2319a4e6c0b0cbfd52), [`1c6ce51`](https://github.com/mastra-ai/mastra/commit/1c6ce51f875915ab57fd36873623013699a2a65d), [`898a972`](https://github.com/mastra-ai/mastra/commit/898a9727d286c2510d6b702dfd367e6aaf5c6b0f), [`a97003a`](https://github.com/mastra-ai/mastra/commit/a97003aa1cf2f4022a41912324a1e77263b326b8), [`ccc141e`](https://github.com/mastra-ai/mastra/commit/ccc141ed27da0abc3a3fc28e9e5128152e8e37f4), [`fe3b897`](https://github.com/mastra-ai/mastra/commit/fe3b897c2ccbcd2b10e81b099438c7337feddf89), [`00123ba`](https://github.com/mastra-ai/mastra/commit/00123ba96dc9e5cd0b110420ebdba56d8f237b25), [`29c4309`](https://github.com/mastra-ai/mastra/commit/29c4309f818b24304c041bcb4a8f19b5f13f6b62), [`16785ce`](https://github.com/mastra-ai/mastra/commit/16785ced928f6f22638f4488cf8a125d99211799), [`de8239b`](https://github.com/mastra-ai/mastra/commit/de8239bdcb1d8c0cfa06da21f1569912a66bbc8a), [`b5e6cd7`](https://github.com/mastra-ai/mastra/commit/b5e6cd77fc8c8e64e0494c1d06cee3d84e795d1e), [`3759cb0`](https://github.com/mastra-ai/mastra/commit/3759cb064935b5f74c65ac2f52a1145f7352899d), [`651e772`](https://github.com/mastra-ai/mastra/commit/651e772eb1475fb13e126d3fcc01751297a88214), [`b61b93f`](https://github.com/mastra-ai/mastra/commit/b61b93f9e058b11dd2eec169853175d31dbdd567), [`bae33d9`](https://github.com/mastra-ai/mastra/commit/bae33d91a63fbb64d1e80519e1fc1acaed1e9013), [`c0b731f`](https://github.com/mastra-ai/mastra/commit/c0b731fb27d712dc8582e846df5c0332a6a0c5ba), [`43ca8f2`](https://github.com/mastra-ai/mastra/commit/43ca8f2c7334851cc7b4d3d2f037d8784bfbdd5f), [`2ca67cc`](https://github.com/mastra-ai/mastra/commit/2ca67cc3bb1f6a617353fdcab197d9efebe60d6f), [`9e67002`](https://github.com/mastra-ai/mastra/commit/9e67002b52c9be19936c420a489dbee9c5fd6a78), [`35edc49`](https://github.com/mastra-ai/mastra/commit/35edc49ac0556db609189641d6341e76771b81fc)]:
10
+ - @mastra/core@1.0.0-beta.5
11
+
3
12
  ## 1.0.0-beta.1
4
13
 
5
14
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -578,69 +578,32 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
578
578
  );
579
579
  }
580
580
  const { field, direction } = this.parseOrderBy(orderBy, "ASC");
581
- const messageIds = /* @__PURE__ */ new Set();
582
- const hasFilters = !!resourceId || !!filter?.dateRange;
583
- if (hasFilters || perPage === Number.MAX_SAFE_INTEGER) {
584
- try {
585
- const threadMessagesKey = this.getThreadMessagesKey(threadId);
586
- const allIds = await this.getFullOrder(threadMessagesKey);
587
- allIds.forEach((id) => messageIds.add(id));
588
- } catch {
589
- }
590
- } else {
591
- if (perPage > 0) {
592
- try {
593
- const threadMessagesKey = this.getThreadMessagesKey(threadId);
594
- const fullOrder = await this.getFullOrder(threadMessagesKey);
595
- const totalMessages = fullOrder.length;
596
- let start;
597
- let end;
598
- if (direction === "ASC") {
599
- start = offset;
600
- end = Math.min(offset + perPage - 1, totalMessages - 1);
601
- } else {
602
- start = Math.max(totalMessages - offset - perPage, 0);
603
- end = totalMessages - offset - 1;
604
- }
605
- const paginatedIds = await this.getRange(threadMessagesKey, start, end);
606
- paginatedIds.forEach((id) => messageIds.add(id));
607
- } catch {
608
- }
609
- }
610
- }
611
- if (include && include.length > 0) {
612
- await this.getIncludedMessagesWithContext(threadId, include, messageIds);
581
+ const threadMessageIds = /* @__PURE__ */ new Set();
582
+ try {
583
+ const threadMessagesKey = this.getThreadMessagesKey(threadId);
584
+ const allIds = await this.getFullOrder(threadMessagesKey);
585
+ allIds.forEach((id) => threadMessageIds.add(id));
586
+ } catch {
613
587
  }
614
- const messages = await this.fetchAndParseMessagesFromMultipleThreads(
615
- Array.from(messageIds),
616
- include,
617
- include && include.length > 0 ? void 0 : threadId
588
+ const threadMessages = await this.fetchAndParseMessagesFromMultipleThreads(
589
+ Array.from(threadMessageIds),
590
+ void 0,
591
+ threadId
618
592
  );
619
- let filteredMessages = messages;
593
+ let filteredThreadMessages = threadMessages;
620
594
  if (resourceId) {
621
- filteredMessages = filteredMessages.filter((msg) => msg.resourceId === resourceId);
595
+ filteredThreadMessages = filteredThreadMessages.filter((msg) => msg.resourceId === resourceId);
622
596
  }
623
597
  const dateRange = filter?.dateRange;
624
598
  if (dateRange) {
625
- filteredMessages = filteredMessages.filter((msg) => {
599
+ filteredThreadMessages = filteredThreadMessages.filter((msg) => {
626
600
  const messageDate = new Date(msg.createdAt);
627
601
  if (dateRange.start && messageDate < new Date(dateRange.start)) return false;
628
602
  if (dateRange.end && messageDate > new Date(dateRange.end)) return false;
629
603
  return true;
630
604
  });
631
605
  }
632
- let total;
633
- if (hasFilters) {
634
- total = filteredMessages.length;
635
- } else {
636
- try {
637
- const threadMessagesKey = this.getThreadMessagesKey(threadId);
638
- const fullOrder = await this.getFullOrder(threadMessagesKey);
639
- total = fullOrder.length;
640
- } catch {
641
- total = filteredMessages.length;
642
- }
643
- }
606
+ const total = filteredThreadMessages.length;
644
607
  if (perPage === 0 && (!include || include.length === 0)) {
645
608
  return {
646
609
  messages: [],
@@ -650,45 +613,58 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
650
613
  hasMore: offset < total
651
614
  };
652
615
  }
653
- if (hasFilters && perPage !== Number.MAX_SAFE_INTEGER && perPage > 0) {
654
- if (direction === "ASC") {
655
- filteredMessages = filteredMessages.slice(offset, offset + perPage);
656
- } else {
657
- const start = Math.max(filteredMessages.length - offset - perPage, 0);
658
- const end = filteredMessages.length - offset;
659
- filteredMessages = filteredMessages.slice(start, end);
616
+ filteredThreadMessages.sort((a, b) => {
617
+ const timeA = new Date(a.createdAt).getTime();
618
+ const timeB = new Date(b.createdAt).getTime();
619
+ const timeDiff = direction === "ASC" ? timeA - timeB : timeB - timeA;
620
+ if (timeDiff === 0) {
621
+ return a.id.localeCompare(b.id);
660
622
  }
623
+ return timeDiff;
624
+ });
625
+ let paginatedMessages;
626
+ if (perPage === 0) {
627
+ paginatedMessages = [];
628
+ } else if (perPage === Number.MAX_SAFE_INTEGER) {
629
+ paginatedMessages = filteredThreadMessages;
630
+ } else {
631
+ paginatedMessages = filteredThreadMessages.slice(offset, offset + perPage);
661
632
  }
662
- const paginatedCount = hasFilters && perPage !== Number.MAX_SAFE_INTEGER && perPage > 0 ? filteredMessages.length : filteredMessages.length;
663
- try {
664
- const threadMessagesKey = this.getThreadMessagesKey(threadId);
665
- const messageOrder = await this.getFullOrder(threadMessagesKey);
666
- const orderMap = new Map(messageOrder.map((id, index) => [id, index]));
667
- filteredMessages.sort((a, b) => {
668
- const indexA = orderMap.get(a.id);
669
- const indexB = orderMap.get(b.id);
670
- if (indexA !== void 0 && indexB !== void 0) {
671
- return direction === "ASC" ? indexA - indexB : indexB - indexA;
672
- }
673
- const timeA = new Date(a.createdAt).getTime();
674
- const timeB = new Date(b.createdAt).getTime();
675
- const timeDiff = direction === "ASC" ? timeA - timeB : timeB - timeA;
676
- if (timeDiff === 0) {
677
- return a.id.localeCompare(b.id);
678
- }
679
- return timeDiff;
680
- });
681
- } catch {
682
- filteredMessages.sort((a, b) => {
683
- const timeA = new Date(a.createdAt).getTime();
684
- const timeB = new Date(b.createdAt).getTime();
685
- const timeDiff = direction === "ASC" ? timeA - timeB : timeB - timeA;
686
- if (timeDiff === 0) {
687
- return a.id.localeCompare(b.id);
688
- }
689
- return timeDiff;
690
- });
633
+ let includedMessages = [];
634
+ if (include && include.length > 0) {
635
+ const includedMessageIds = /* @__PURE__ */ new Set();
636
+ await this.getIncludedMessagesWithContext(threadId, include, includedMessageIds);
637
+ const paginatedIds = new Set(paginatedMessages.map((m) => m.id));
638
+ const idsToFetch = Array.from(includedMessageIds).filter((id) => !paginatedIds.has(id));
639
+ if (idsToFetch.length > 0) {
640
+ includedMessages = await this.fetchAndParseMessagesFromMultipleThreads(idsToFetch, include, void 0);
641
+ }
642
+ }
643
+ const seenIds = /* @__PURE__ */ new Set();
644
+ const allMessages = [];
645
+ for (const msg of paginatedMessages) {
646
+ if (!seenIds.has(msg.id)) {
647
+ allMessages.push(msg);
648
+ seenIds.add(msg.id);
649
+ }
650
+ }
651
+ for (const msg of includedMessages) {
652
+ if (!seenIds.has(msg.id)) {
653
+ allMessages.push(msg);
654
+ seenIds.add(msg.id);
655
+ }
691
656
  }
657
+ allMessages.sort((a, b) => {
658
+ const timeA = new Date(a.createdAt).getTime();
659
+ const timeB = new Date(b.createdAt).getTime();
660
+ const timeDiff = direction === "ASC" ? timeA - timeB : timeB - timeA;
661
+ if (timeDiff === 0) {
662
+ return a.id.localeCompare(b.id);
663
+ }
664
+ return timeDiff;
665
+ });
666
+ let filteredMessages = allMessages;
667
+ const paginatedCount = paginatedMessages.length;
692
668
  if (total === 0 && filteredMessages.length === 0 && (!include || include.length === 0)) {
693
669
  return {
694
670
  messages: [],
@@ -721,14 +697,7 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
721
697
  });
722
698
  const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
723
699
  const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
724
- let hasMore;
725
- if (perPageInput === false || allThreadMessagesReturned) {
726
- hasMore = false;
727
- } else if (direction === "ASC") {
728
- hasMore = offset + paginatedCount < total;
729
- } else {
730
- hasMore = total - offset - perPage > 0;
731
- }
700
+ const hasMore = perPageInput !== false && !allThreadMessagesReturned && offset + paginatedCount < total;
732
701
  return {
733
702
  messages: finalMessages,
734
703
  total,