@mastra/upstash 1.3.1 → 1.4.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/CHANGELOG.md +38 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/index.cjs +31 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +31 -18
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +7 -0
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -436,6 +436,7 @@ var StoreMemoryUpstash = class extends MemoryStorage {
|
|
|
436
436
|
hasMore: perPageInput === false ? false : end < total
|
|
437
437
|
};
|
|
438
438
|
} catch (error) {
|
|
439
|
+
if (error instanceof MastraError && error.category === ErrorCategory.USER) throw error;
|
|
439
440
|
const mastraError = new MastraError({
|
|
440
441
|
id: createStorageErrorId("UPSTASH", "LIST_THREADS", "FAILED"),
|
|
441
442
|
domain: ErrorDomain.STORAGE,
|
|
@@ -449,13 +450,7 @@ var StoreMemoryUpstash = class extends MemoryStorage {
|
|
|
449
450
|
}, error);
|
|
450
451
|
this.logger?.trackException(mastraError);
|
|
451
452
|
this.logger.error(mastraError.toString());
|
|
452
|
-
|
|
453
|
-
threads: [],
|
|
454
|
-
total: 0,
|
|
455
|
-
page,
|
|
456
|
-
perPage: perPageForResponse,
|
|
457
|
-
hasMore: false
|
|
458
|
-
};
|
|
453
|
+
throw mastraError;
|
|
459
454
|
}
|
|
460
455
|
}
|
|
461
456
|
async saveThread({ thread }) {
|
|
@@ -662,16 +657,38 @@ var StoreMemoryUpstash = class extends MemoryStorage {
|
|
|
662
657
|
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
663
658
|
});
|
|
664
659
|
}
|
|
665
|
-
|
|
660
|
+
/**
|
|
661
|
+
* Fetches the messages named by `include` together with their surrounding context.
|
|
662
|
+
*
|
|
663
|
+
* @param include - Message ids to pin, each with an optional before/after window.
|
|
664
|
+
* @param resourceId - When set, drops any pinned or context message owned by another
|
|
665
|
+
* resource so an id from another resource returns nothing.
|
|
666
|
+
*/
|
|
667
|
+
async _getIncludedMessages(include, resourceId) {
|
|
666
668
|
if (!include?.length) return [];
|
|
667
669
|
const messageIds = /* @__PURE__ */ new Set();
|
|
668
670
|
const messageIdToThreadIds = {};
|
|
669
671
|
for (const item of include) {
|
|
670
672
|
const itemThreadId = await this._getThreadIdForMessage(item.id);
|
|
671
673
|
if (!itemThreadId) continue;
|
|
674
|
+
const itemThreadMessagesKey = getThreadMessagesKey(itemThreadId);
|
|
675
|
+
if (resourceId !== void 0) {
|
|
676
|
+
const threadMessageIds = await this.client.zrange(itemThreadMessagesKey, 0, -1);
|
|
677
|
+
const threadPipeline = this.client.pipeline();
|
|
678
|
+
threadMessageIds.forEach((id) => threadPipeline.get(getMessageKey(itemThreadId, id)));
|
|
679
|
+
const threadMessages = (await threadPipeline.exec()).filter((message) => message !== null).filter((message) => message.resourceId === resourceId);
|
|
680
|
+
const targetIndex = threadMessages.findIndex((message) => message.id === item.id);
|
|
681
|
+
if (targetIndex === -1) continue;
|
|
682
|
+
const start = Math.max(0, targetIndex - (item.withPreviousMessages ?? 0));
|
|
683
|
+
const end = Math.min(threadMessages.length, targetIndex + (item.withNextMessages ?? 0) + 1);
|
|
684
|
+
for (const message of threadMessages.slice(start, end)) {
|
|
685
|
+
messageIds.add(message.id);
|
|
686
|
+
messageIdToThreadIds[message.id] = itemThreadId;
|
|
687
|
+
}
|
|
688
|
+
continue;
|
|
689
|
+
}
|
|
672
690
|
messageIds.add(item.id);
|
|
673
691
|
messageIdToThreadIds[item.id] = itemThreadId;
|
|
674
|
-
const itemThreadMessagesKey = getThreadMessagesKey(itemThreadId);
|
|
675
692
|
const rank = await this.client.zrank(itemThreadMessagesKey, item.id);
|
|
676
693
|
if (rank === null) continue;
|
|
677
694
|
if (item.withPreviousMessages) {
|
|
@@ -692,7 +709,8 @@ var StoreMemoryUpstash = class extends MemoryStorage {
|
|
|
692
709
|
const tId = messageIdToThreadIds[id];
|
|
693
710
|
pipeline.get(getMessageKey(tId, id));
|
|
694
711
|
});
|
|
695
|
-
|
|
712
|
+
const includedMessages = (await pipeline.exec()).filter((result) => result !== null);
|
|
713
|
+
return resourceId ? includedMessages.filter((message) => message.resourceId === resourceId) : includedMessages;
|
|
696
714
|
}
|
|
697
715
|
parseStoredMessage(storedMessage) {
|
|
698
716
|
const defaultMessageContent = {
|
|
@@ -786,7 +804,7 @@ var StoreMemoryUpstash = class extends MemoryStorage {
|
|
|
786
804
|
hasMore: false
|
|
787
805
|
};
|
|
788
806
|
let includedMessages = [];
|
|
789
|
-
if (include && include.length > 0) includedMessages = (await this._getIncludedMessages(include)).map(this.parseStoredMessage);
|
|
807
|
+
if (include && include.length > 0) includedMessages = (await this._getIncludedMessages(include, resourceId)).map(this.parseStoredMessage);
|
|
790
808
|
if (perPage === 0 && include && include.length > 0) {
|
|
791
809
|
const list = new MessageList().add(includedMessages, "memory");
|
|
792
810
|
return {
|
|
@@ -847,6 +865,7 @@ var StoreMemoryUpstash = class extends MemoryStorage {
|
|
|
847
865
|
hasMore
|
|
848
866
|
};
|
|
849
867
|
} catch (error) {
|
|
868
|
+
if (error instanceof MastraError && error.category === ErrorCategory.USER) throw error;
|
|
850
869
|
const mastraError = new MastraError({
|
|
851
870
|
id: createStorageErrorId("UPSTASH", "LIST_MESSAGES", "FAILED"),
|
|
852
871
|
domain: ErrorDomain.STORAGE,
|
|
@@ -858,13 +877,7 @@ var StoreMemoryUpstash = class extends MemoryStorage {
|
|
|
858
877
|
}, error);
|
|
859
878
|
this.logger.error(mastraError.toString());
|
|
860
879
|
this.logger?.trackException(mastraError);
|
|
861
|
-
|
|
862
|
-
messages: [],
|
|
863
|
-
total: 0,
|
|
864
|
-
page,
|
|
865
|
-
perPage: perPageForResponse,
|
|
866
|
-
hasMore: false
|
|
867
|
-
};
|
|
880
|
+
throw mastraError;
|
|
868
881
|
}
|
|
869
882
|
}
|
|
870
883
|
async getResourceById({ resourceId }) {
|