@mastra/upstash 1.0.3 → 1.0.4

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
@@ -502,6 +502,22 @@ var StoreMemoryUpstash = class extends MemoryStorage {
502
502
  }
503
503
  return messageData.threadId || null;
504
504
  }
505
+ _sortMessages(messages, field, direction) {
506
+ return messages.sort((a, b) => {
507
+ const getVal = (msg) => {
508
+ if (field === "createdAt") {
509
+ return new Date(msg.createdAt).getTime();
510
+ }
511
+ const value = msg[field];
512
+ if (typeof value === "number") return value;
513
+ if (value instanceof Date) return value.getTime();
514
+ return 0;
515
+ };
516
+ const aValue = getVal(a);
517
+ const bValue = getVal(b);
518
+ return direction === "ASC" ? aValue - bValue : bValue - aValue;
519
+ });
520
+ }
505
521
  async _getIncludedMessages(include) {
506
522
  if (!include?.length) return [];
507
523
  const messageIds = /* @__PURE__ */ new Set();
@@ -638,11 +654,25 @@ var StoreMemoryUpstash = class extends MemoryStorage {
638
654
  new Error("page must be >= 0")
639
655
  );
640
656
  }
657
+ const { field, direction } = this.parseOrderBy(orderBy, "ASC");
658
+ if (perPage === 0 && (!include || include.length === 0)) {
659
+ return { messages: [], total: 0, page, perPage: perPageForResponse, hasMore: false };
660
+ }
641
661
  let includedMessages = [];
642
662
  if (include && include.length > 0) {
643
663
  const included = await this._getIncludedMessages(include);
644
664
  includedMessages = included.map(this.parseStoredMessage);
645
665
  }
666
+ if (perPage === 0 && include && include.length > 0) {
667
+ const list2 = new MessageList().add(includedMessages, "memory");
668
+ return {
669
+ messages: this._sortMessages(list2.get.all.db(), field, direction),
670
+ total: 0,
671
+ page,
672
+ perPage: perPageForResponse,
673
+ hasMore: false
674
+ };
675
+ }
646
676
  const allMessageIdsWithThreads = [];
647
677
  for (const tid of threadIds) {
648
678
  const threadMessagesKey = getThreadMessagesKey(tid);
@@ -672,25 +702,7 @@ var StoreMemoryUpstash = class extends MemoryStorage {
672
702
  (msg) => new Date(msg.createdAt),
673
703
  filter?.dateRange
674
704
  );
675
- const { field, direction } = this.parseOrderBy(orderBy, "ASC");
676
- const getFieldValue = (msg) => {
677
- if (field === "createdAt") {
678
- return new Date(msg.createdAt).getTime();
679
- }
680
- const value = msg[field];
681
- if (typeof value === "number") {
682
- return value;
683
- }
684
- if (value instanceof Date) {
685
- return value.getTime();
686
- }
687
- return 0;
688
- };
689
- messagesData.sort((a, b) => {
690
- const aValue = getFieldValue(a);
691
- const bValue = getFieldValue(b);
692
- return direction === "ASC" ? aValue - bValue : bValue - aValue;
693
- });
705
+ messagesData = this._sortMessages(messagesData, field, direction);
694
706
  const total = messagesData.length;
695
707
  const start = offset;
696
708
  const end = perPageInput === false ? total : start + perPage;
@@ -711,11 +723,7 @@ var StoreMemoryUpstash = class extends MemoryStorage {
711
723
  }
712
724
  const list = new MessageList().add(allMessages, "memory");
713
725
  let finalMessages = list.get.all.db();
714
- finalMessages = finalMessages.sort((a, b) => {
715
- const aValue = getFieldValue(a);
716
- const bValue = getFieldValue(b);
717
- return direction === "ASC" ? aValue - bValue : bValue - aValue;
718
- });
726
+ finalMessages = this._sortMessages(finalMessages, field, direction);
719
727
  const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
720
728
  const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
721
729
  const hasMore = perPageInput !== false && !allThreadMessagesReturned && end < total;