@mastra/upstash 0.11.1-alpha.0 → 0.11.1-alpha.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/dist/index.cjs CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var agent = require('@mastra/core/agent');
4
+ var error = require('@mastra/core/error');
4
5
  var storage = require('@mastra/core/storage');
5
6
  var redis = require('@upstash/redis');
6
7
  var vector = require('@mastra/core/vector');
@@ -19,7 +20,8 @@ var UpstashStore = class extends storage.MastraStorage {
19
20
  }
20
21
  get supports() {
21
22
  return {
22
- selectByIncludeResourceScope: true
23
+ selectByIncludeResourceScope: true,
24
+ resourceWorkingMemory: true
23
25
  };
24
26
  }
25
27
  transformEvalRecord(record) {
@@ -198,8 +200,18 @@ var UpstashStore = class extends storage.MastraStorage {
198
200
  });
199
201
  }
200
202
  return filteredEvals.map((record) => this.transformEvalRecord(record));
201
- } catch (error) {
202
- console.error("Failed to get evals for the specified agent:", error);
203
+ } catch (error$1) {
204
+ const mastraError = new error.MastraError(
205
+ {
206
+ id: "STORAGE_UPSTASH_STORAGE_GET_EVALS_BY_AGENT_NAME_FAILED",
207
+ domain: error.ErrorDomain.STORAGE,
208
+ category: error.ErrorCategory.THIRD_PARTY,
209
+ details: { agentName }
210
+ },
211
+ error$1
212
+ );
213
+ this.logger?.trackException(mastraError);
214
+ this.logger.error(mastraError.toString());
203
215
  return [];
204
216
  }
205
217
  }
@@ -213,8 +225,19 @@ var UpstashStore = class extends storage.MastraStorage {
213
225
  end: args.toDate
214
226
  };
215
227
  }
216
- const { traces } = await this.getTracesPaginated(args);
217
- return traces;
228
+ try {
229
+ const { traces } = await this.getTracesPaginated(args);
230
+ return traces;
231
+ } catch (error$1) {
232
+ throw new error.MastraError(
233
+ {
234
+ id: "STORAGE_UPSTASH_STORAGE_GET_TRACES_FAILED",
235
+ domain: error.ErrorDomain.STORAGE,
236
+ category: error.ErrorCategory.THIRD_PARTY
237
+ },
238
+ error$1
239
+ );
240
+ }
218
241
  }
219
242
  async getTracesPaginated(args) {
220
243
  const { name, scope, page = 0, perPage = 100, attributes, filters, dateRange } = args;
@@ -297,8 +320,21 @@ var UpstashStore = class extends storage.MastraStorage {
297
320
  perPage: resolvedPerPage,
298
321
  hasMore
299
322
  };
300
- } catch (error) {
301
- console.error("Failed to get traces:", error);
323
+ } catch (error$1) {
324
+ const mastraError = new error.MastraError(
325
+ {
326
+ id: "STORAGE_UPSTASH_STORAGE_GET_TRACES_PAGINATED_FAILED",
327
+ domain: error.ErrorDomain.STORAGE,
328
+ category: error.ErrorCategory.THIRD_PARTY,
329
+ details: {
330
+ name: args.name || "",
331
+ scope: args.scope || ""
332
+ }
333
+ },
334
+ error$1
335
+ );
336
+ this.logger?.trackException(mastraError);
337
+ this.logger.error(mastraError.toString());
302
338
  return {
303
339
  traces: [],
304
340
  total: 0,
@@ -312,7 +348,21 @@ var UpstashStore = class extends storage.MastraStorage {
312
348
  tableName,
313
349
  schema
314
350
  }) {
315
- await this.redis.set(`schema:${tableName}`, schema);
351
+ try {
352
+ await this.redis.set(`schema:${tableName}`, schema);
353
+ } catch (error$1) {
354
+ throw new error.MastraError(
355
+ {
356
+ id: "STORAGE_UPSTASH_STORAGE_CREATE_TABLE_FAILED",
357
+ domain: error.ErrorDomain.STORAGE,
358
+ category: error.ErrorCategory.THIRD_PARTY,
359
+ details: {
360
+ tableName
361
+ }
362
+ },
363
+ error$1
364
+ );
365
+ }
316
366
  }
317
367
  /**
318
368
  * No-op: This backend is schemaless and does not require schema changes.
@@ -324,43 +374,113 @@ var UpstashStore = class extends storage.MastraStorage {
324
374
  }
325
375
  async clearTable({ tableName }) {
326
376
  const pattern = `${tableName}:*`;
327
- await this.scanAndDelete(pattern);
377
+ try {
378
+ await this.scanAndDelete(pattern);
379
+ } catch (error$1) {
380
+ throw new error.MastraError(
381
+ {
382
+ id: "STORAGE_UPSTASH_STORAGE_CLEAR_TABLE_FAILED",
383
+ domain: error.ErrorDomain.STORAGE,
384
+ category: error.ErrorCategory.THIRD_PARTY,
385
+ details: {
386
+ tableName
387
+ }
388
+ },
389
+ error$1
390
+ );
391
+ }
328
392
  }
329
393
  async insert({ tableName, record }) {
330
394
  const { key, processedRecord } = this.processRecord(tableName, record);
331
- await this.redis.set(key, processedRecord);
395
+ try {
396
+ await this.redis.set(key, processedRecord);
397
+ } catch (error$1) {
398
+ throw new error.MastraError(
399
+ {
400
+ id: "STORAGE_UPSTASH_STORAGE_INSERT_FAILED",
401
+ domain: error.ErrorDomain.STORAGE,
402
+ category: error.ErrorCategory.THIRD_PARTY,
403
+ details: {
404
+ tableName
405
+ }
406
+ },
407
+ error$1
408
+ );
409
+ }
332
410
  }
333
411
  async batchInsert(input) {
334
412
  const { tableName, records } = input;
335
413
  if (!records.length) return;
336
414
  const batchSize = 1e3;
337
- for (let i = 0; i < records.length; i += batchSize) {
338
- const batch = records.slice(i, i + batchSize);
339
- const pipeline = this.redis.pipeline();
340
- for (const record of batch) {
341
- const { key, processedRecord } = this.processRecord(tableName, record);
342
- pipeline.set(key, processedRecord);
415
+ try {
416
+ for (let i = 0; i < records.length; i += batchSize) {
417
+ const batch = records.slice(i, i + batchSize);
418
+ const pipeline = this.redis.pipeline();
419
+ for (const record of batch) {
420
+ const { key, processedRecord } = this.processRecord(tableName, record);
421
+ pipeline.set(key, processedRecord);
422
+ }
423
+ await pipeline.exec();
343
424
  }
344
- await pipeline.exec();
425
+ } catch (error$1) {
426
+ throw new error.MastraError(
427
+ {
428
+ id: "STORAGE_UPSTASH_STORAGE_BATCH_INSERT_FAILED",
429
+ domain: error.ErrorDomain.STORAGE,
430
+ category: error.ErrorCategory.THIRD_PARTY,
431
+ details: {
432
+ tableName
433
+ }
434
+ },
435
+ error$1
436
+ );
345
437
  }
346
438
  }
347
439
  async load({ tableName, keys }) {
348
440
  const key = this.getKey(tableName, keys);
349
- const data = await this.redis.get(key);
350
- return data || null;
441
+ try {
442
+ const data = await this.redis.get(key);
443
+ return data || null;
444
+ } catch (error$1) {
445
+ throw new error.MastraError(
446
+ {
447
+ id: "STORAGE_UPSTASH_STORAGE_LOAD_FAILED",
448
+ domain: error.ErrorDomain.STORAGE,
449
+ category: error.ErrorCategory.THIRD_PARTY,
450
+ details: {
451
+ tableName
452
+ }
453
+ },
454
+ error$1
455
+ );
456
+ }
351
457
  }
352
458
  async getThreadById({ threadId }) {
353
- const thread = await this.load({
354
- tableName: storage.TABLE_THREADS,
355
- keys: { id: threadId }
356
- });
357
- if (!thread) return null;
358
- return {
359
- ...thread,
360
- createdAt: this.ensureDate(thread.createdAt),
361
- updatedAt: this.ensureDate(thread.updatedAt),
362
- metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata
363
- };
459
+ try {
460
+ const thread = await this.load({
461
+ tableName: storage.TABLE_THREADS,
462
+ keys: { id: threadId }
463
+ });
464
+ if (!thread) return null;
465
+ return {
466
+ ...thread,
467
+ createdAt: this.ensureDate(thread.createdAt),
468
+ updatedAt: this.ensureDate(thread.updatedAt),
469
+ metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata
470
+ };
471
+ } catch (error$1) {
472
+ throw new error.MastraError(
473
+ {
474
+ id: "STORAGE_UPSTASH_STORAGE_GET_THREAD_BY_ID_FAILED",
475
+ domain: error.ErrorDomain.STORAGE,
476
+ category: error.ErrorCategory.THIRD_PARTY,
477
+ details: {
478
+ threadId
479
+ }
480
+ },
481
+ error$1
482
+ );
483
+ }
364
484
  }
365
485
  /**
366
486
  * @deprecated use getThreadsByResourceIdPaginated instead
@@ -389,8 +509,20 @@ var UpstashStore = class extends storage.MastraStorage {
389
509
  }
390
510
  allThreads.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
391
511
  return allThreads;
392
- } catch (error) {
393
- console.error("Error in getThreadsByResourceId:", error);
512
+ } catch (error$1) {
513
+ const mastraError = new error.MastraError(
514
+ {
515
+ id: "STORAGE_UPSTASH_STORAGE_GET_THREADS_BY_RESOURCE_ID_FAILED",
516
+ domain: error.ErrorDomain.STORAGE,
517
+ category: error.ErrorCategory.THIRD_PARTY,
518
+ details: {
519
+ resourceId
520
+ }
521
+ },
522
+ error$1
523
+ );
524
+ this.logger?.trackException(mastraError);
525
+ this.logger.error(mastraError.toString());
394
526
  return [];
395
527
  }
396
528
  }
@@ -410,8 +542,22 @@ var UpstashStore = class extends storage.MastraStorage {
410
542
  perPage,
411
543
  hasMore
412
544
  };
413
- } catch (error) {
414
- console.error("Error in getThreadsByResourceIdPaginated:", error);
545
+ } catch (error$1) {
546
+ const mastraError = new error.MastraError(
547
+ {
548
+ id: "STORAGE_UPSTASH_STORAGE_GET_THREADS_BY_RESOURCE_ID_PAGINATED_FAILED",
549
+ domain: error.ErrorDomain.STORAGE,
550
+ category: error.ErrorCategory.THIRD_PARTY,
551
+ details: {
552
+ resourceId,
553
+ page,
554
+ perPage
555
+ }
556
+ },
557
+ error$1
558
+ );
559
+ this.logger?.trackException(mastraError);
560
+ this.logger.error(mastraError.toString());
415
561
  return {
416
562
  threads: [],
417
563
  total: 0,
@@ -422,11 +568,28 @@ var UpstashStore = class extends storage.MastraStorage {
422
568
  }
423
569
  }
424
570
  async saveThread({ thread }) {
425
- await this.insert({
426
- tableName: storage.TABLE_THREADS,
427
- record: thread
428
- });
429
- return thread;
571
+ try {
572
+ await this.insert({
573
+ tableName: storage.TABLE_THREADS,
574
+ record: thread
575
+ });
576
+ return thread;
577
+ } catch (error$1) {
578
+ const mastraError = new error.MastraError(
579
+ {
580
+ id: "STORAGE_UPSTASH_STORAGE_SAVE_THREAD_FAILED",
581
+ domain: error.ErrorDomain.STORAGE,
582
+ category: error.ErrorCategory.THIRD_PARTY,
583
+ details: {
584
+ threadId: thread.id
585
+ }
586
+ },
587
+ error$1
588
+ );
589
+ this.logger?.trackException(mastraError);
590
+ this.logger.error(mastraError.toString());
591
+ throw mastraError;
592
+ }
430
593
  }
431
594
  async updateThread({
432
595
  id,
@@ -435,7 +598,15 @@ var UpstashStore = class extends storage.MastraStorage {
435
598
  }) {
436
599
  const thread = await this.getThreadById({ threadId: id });
437
600
  if (!thread) {
438
- throw new Error(`Thread ${id} not found`);
601
+ throw new error.MastraError({
602
+ id: "STORAGE_UPSTASH_STORAGE_UPDATE_THREAD_FAILED",
603
+ domain: error.ErrorDomain.STORAGE,
604
+ category: error.ErrorCategory.USER,
605
+ text: `Thread ${id} not found`,
606
+ details: {
607
+ threadId: id
608
+ }
609
+ });
439
610
  }
440
611
  const updatedThread = {
441
612
  ...thread,
@@ -445,34 +616,73 @@ var UpstashStore = class extends storage.MastraStorage {
445
616
  ...metadata
446
617
  }
447
618
  };
448
- await this.saveThread({ thread: updatedThread });
449
- return updatedThread;
619
+ try {
620
+ await this.saveThread({ thread: updatedThread });
621
+ return updatedThread;
622
+ } catch (error$1) {
623
+ throw new error.MastraError(
624
+ {
625
+ id: "STORAGE_UPSTASH_STORAGE_UPDATE_THREAD_FAILED",
626
+ domain: error.ErrorDomain.STORAGE,
627
+ category: error.ErrorCategory.THIRD_PARTY,
628
+ details: {
629
+ threadId: id
630
+ }
631
+ },
632
+ error$1
633
+ );
634
+ }
450
635
  }
451
636
  async deleteThread({ threadId }) {
452
637
  const threadKey = this.getKey(storage.TABLE_THREADS, { id: threadId });
453
638
  const threadMessagesKey = this.getThreadMessagesKey(threadId);
454
- const messageIds = await this.redis.zrange(threadMessagesKey, 0, -1);
455
- const pipeline = this.redis.pipeline();
456
- pipeline.del(threadKey);
457
- pipeline.del(threadMessagesKey);
458
- for (let i = 0; i < messageIds.length; i++) {
459
- const messageId = messageIds[i];
460
- const messageKey = this.getMessageKey(threadId, messageId);
461
- pipeline.del(messageKey);
639
+ try {
640
+ const messageIds = await this.redis.zrange(threadMessagesKey, 0, -1);
641
+ const pipeline = this.redis.pipeline();
642
+ pipeline.del(threadKey);
643
+ pipeline.del(threadMessagesKey);
644
+ for (let i = 0; i < messageIds.length; i++) {
645
+ const messageId = messageIds[i];
646
+ const messageKey = this.getMessageKey(threadId, messageId);
647
+ pipeline.del(messageKey);
648
+ }
649
+ await pipeline.exec();
650
+ await this.scanAndDelete(this.getMessageKey(threadId, "*"));
651
+ } catch (error$1) {
652
+ throw new error.MastraError(
653
+ {
654
+ id: "STORAGE_UPSTASH_STORAGE_DELETE_THREAD_FAILED",
655
+ domain: error.ErrorDomain.STORAGE,
656
+ category: error.ErrorCategory.THIRD_PARTY,
657
+ details: {
658
+ threadId
659
+ }
660
+ },
661
+ error$1
662
+ );
462
663
  }
463
- await pipeline.exec();
464
- await this.scanAndDelete(this.getMessageKey(threadId, "*"));
465
664
  }
466
665
  async saveMessages(args) {
467
666
  const { messages, format = "v1" } = args;
468
667
  if (messages.length === 0) return [];
469
668
  const threadId = messages[0]?.threadId;
470
- if (!threadId) {
471
- throw new Error("Thread ID is required");
472
- }
473
- const thread = await this.getThreadById({ threadId });
474
- if (!thread) {
475
- throw new Error(`Thread ${threadId} not found`);
669
+ try {
670
+ if (!threadId) {
671
+ throw new Error("Thread ID is required");
672
+ }
673
+ const thread = await this.getThreadById({ threadId });
674
+ if (!thread) {
675
+ throw new Error(`Thread ${threadId} not found`);
676
+ }
677
+ } catch (error$1) {
678
+ throw new error.MastraError(
679
+ {
680
+ id: "STORAGE_UPSTASH_STORAGE_SAVE_MESSAGES_INVALID_ARGS",
681
+ domain: error.ErrorDomain.STORAGE,
682
+ category: error.ErrorCategory.USER
683
+ },
684
+ error$1
685
+ );
476
686
  }
477
687
  const messagesWithIndex = messages.map((message, index) => ({
478
688
  ...message,
@@ -480,32 +690,63 @@ var UpstashStore = class extends storage.MastraStorage {
480
690
  }));
481
691
  const threadKey = this.getKey(storage.TABLE_THREADS, { id: threadId });
482
692
  const existingThread = await this.redis.get(threadKey);
483
- const batchSize = 1e3;
484
- for (let i = 0; i < messagesWithIndex.length; i += batchSize) {
485
- const batch = messagesWithIndex.slice(i, i + batchSize);
486
- const pipeline = this.redis.pipeline();
487
- for (const message of batch) {
488
- const key = this.getMessageKey(message.threadId, message.id);
489
- const createdAtScore = new Date(message.createdAt).getTime();
490
- const score = message._index !== void 0 ? message._index : createdAtScore;
491
- pipeline.set(key, message);
492
- pipeline.zadd(this.getThreadMessagesKey(message.threadId), {
493
- score,
494
- member: message.id
495
- });
496
- }
497
- if (i === 0 && existingThread) {
498
- const updatedThread = {
499
- ...existingThread,
500
- updatedAt: /* @__PURE__ */ new Date()
501
- };
502
- pipeline.set(threadKey, this.processRecord(storage.TABLE_THREADS, updatedThread).processedRecord);
693
+ try {
694
+ const batchSize = 1e3;
695
+ for (let i = 0; i < messagesWithIndex.length; i += batchSize) {
696
+ const batch = messagesWithIndex.slice(i, i + batchSize);
697
+ const pipeline = this.redis.pipeline();
698
+ for (const message of batch) {
699
+ const key = this.getMessageKey(message.threadId, message.id);
700
+ const createdAtScore = new Date(message.createdAt).getTime();
701
+ const score = message._index !== void 0 ? message._index : createdAtScore;
702
+ const existingKeyPattern = this.getMessageKey("*", message.id);
703
+ const keys = await this.scanKeys(existingKeyPattern);
704
+ if (keys.length > 0) {
705
+ const pipeline2 = this.redis.pipeline();
706
+ keys.forEach((key2) => pipeline2.get(key2));
707
+ const results = await pipeline2.exec();
708
+ const existingMessages = results.filter(
709
+ (msg) => msg !== null
710
+ );
711
+ for (const existingMessage of existingMessages) {
712
+ const existingMessageKey = this.getMessageKey(existingMessage.threadId, existingMessage.id);
713
+ if (existingMessage && existingMessage.threadId !== message.threadId) {
714
+ pipeline.del(existingMessageKey);
715
+ pipeline.zrem(this.getThreadMessagesKey(existingMessage.threadId), existingMessage.id);
716
+ }
717
+ }
718
+ }
719
+ pipeline.set(key, message);
720
+ pipeline.zadd(this.getThreadMessagesKey(message.threadId), {
721
+ score,
722
+ member: message.id
723
+ });
724
+ }
725
+ if (i === 0 && existingThread) {
726
+ const updatedThread = {
727
+ ...existingThread,
728
+ updatedAt: /* @__PURE__ */ new Date()
729
+ };
730
+ pipeline.set(threadKey, this.processRecord(storage.TABLE_THREADS, updatedThread).processedRecord);
731
+ }
732
+ await pipeline.exec();
503
733
  }
504
- await pipeline.exec();
734
+ const list = new agent.MessageList().add(messages, "memory");
735
+ if (format === `v2`) return list.get.all.v2();
736
+ return list.get.all.v1();
737
+ } catch (error$1) {
738
+ throw new error.MastraError(
739
+ {
740
+ id: "STORAGE_UPSTASH_STORAGE_SAVE_MESSAGES_FAILED",
741
+ domain: error.ErrorDomain.STORAGE,
742
+ category: error.ErrorCategory.THIRD_PARTY,
743
+ details: {
744
+ threadId
745
+ }
746
+ },
747
+ error$1
748
+ );
505
749
  }
506
- const list = new agent.MessageList().add(messages, "memory");
507
- if (format === `v2`) return list.get.all.v2();
508
- return list.get.all.v1();
509
750
  }
510
751
  async _getIncludedMessages(threadId, selectBy) {
511
752
  const messageIds = /* @__PURE__ */ new Set();
@@ -550,63 +791,72 @@ var UpstashStore = class extends storage.MastraStorage {
550
791
  format
551
792
  }) {
552
793
  const threadMessagesKey = this.getThreadMessagesKey(threadId);
553
- const allMessageIds = await this.redis.zrange(threadMessagesKey, 0, -1);
554
- let limit;
555
- if (typeof selectBy?.last === "number") {
556
- limit = Math.max(0, selectBy.last);
557
- } else if (selectBy?.last === false) {
558
- limit = 0;
559
- } else {
560
- limit = Number.MAX_SAFE_INTEGER;
561
- }
562
- const messageIds = /* @__PURE__ */ new Set();
563
- const messageIdToThreadIds = {};
564
- if (limit === 0 && !selectBy?.include) {
565
- return [];
566
- }
567
- if (limit === Number.MAX_SAFE_INTEGER) {
568
- const allIds = await this.redis.zrange(threadMessagesKey, 0, -1);
569
- allIds.forEach((id) => {
570
- messageIds.add(id);
571
- messageIdToThreadIds[id] = threadId;
794
+ try {
795
+ const allMessageIds = await this.redis.zrange(threadMessagesKey, 0, -1);
796
+ const limit = this.resolveMessageLimit({ last: selectBy?.last, defaultLimit: Number.MAX_SAFE_INTEGER });
797
+ const messageIds = /* @__PURE__ */ new Set();
798
+ const messageIdToThreadIds = {};
799
+ if (limit === 0 && !selectBy?.include) {
800
+ return [];
801
+ }
802
+ if (limit === Number.MAX_SAFE_INTEGER) {
803
+ const allIds = await this.redis.zrange(threadMessagesKey, 0, -1);
804
+ allIds.forEach((id) => {
805
+ messageIds.add(id);
806
+ messageIdToThreadIds[id] = threadId;
807
+ });
808
+ } else if (limit > 0) {
809
+ const latestIds = await this.redis.zrange(threadMessagesKey, -limit, -1);
810
+ latestIds.forEach((id) => {
811
+ messageIds.add(id);
812
+ messageIdToThreadIds[id] = threadId;
813
+ });
814
+ }
815
+ const includedMessages = await this._getIncludedMessages(threadId, selectBy);
816
+ const messages = [
817
+ ...includedMessages,
818
+ ...(await Promise.all(
819
+ Array.from(messageIds).map(async (id) => {
820
+ const tId = messageIdToThreadIds[id] || threadId;
821
+ const byThreadId = await this.redis.get(
822
+ this.getMessageKey(tId, id)
823
+ );
824
+ if (byThreadId) return byThreadId;
825
+ return null;
826
+ })
827
+ )).filter((msg) => msg !== null)
828
+ ];
829
+ messages.sort((a, b) => allMessageIds.indexOf(a.id) - allMessageIds.indexOf(b.id));
830
+ const seen = /* @__PURE__ */ new Set();
831
+ const dedupedMessages = messages.filter((row) => {
832
+ if (seen.has(row.id)) return false;
833
+ seen.add(row.id);
834
+ return true;
572
835
  });
573
- } else if (limit > 0) {
574
- const latestIds = await this.redis.zrange(threadMessagesKey, -limit, -1);
575
- latestIds.forEach((id) => {
576
- messageIds.add(id);
577
- messageIdToThreadIds[id] = threadId;
836
+ const prepared = dedupedMessages.filter((message) => message !== null && message !== void 0).map((message) => {
837
+ const { _index, ...messageWithoutIndex } = message;
838
+ return messageWithoutIndex;
578
839
  });
840
+ if (format === "v2") {
841
+ return prepared.map((msg) => ({
842
+ ...msg,
843
+ content: msg.content || { format: 2, parts: [{ type: "text", text: "" }] }
844
+ }));
845
+ }
846
+ return prepared;
847
+ } catch (error$1) {
848
+ throw new error.MastraError(
849
+ {
850
+ id: "STORAGE_UPSTASH_STORAGE_GET_MESSAGES_FAILED",
851
+ domain: error.ErrorDomain.STORAGE,
852
+ category: error.ErrorCategory.THIRD_PARTY,
853
+ details: {
854
+ threadId
855
+ }
856
+ },
857
+ error$1
858
+ );
579
859
  }
580
- const includedMessages = await this._getIncludedMessages(threadId, selectBy);
581
- const messages = [
582
- ...includedMessages,
583
- ...(await Promise.all(
584
- Array.from(messageIds).map(async (id) => {
585
- const tId = messageIdToThreadIds[id] || threadId;
586
- const byThreadId = await this.redis.get(this.getMessageKey(tId, id));
587
- if (byThreadId) return byThreadId;
588
- return null;
589
- })
590
- )).filter((msg) => msg !== null)
591
- ];
592
- messages.sort((a, b) => allMessageIds.indexOf(a.id) - allMessageIds.indexOf(b.id));
593
- const seen = /* @__PURE__ */ new Set();
594
- const dedupedMessages = messages.filter((row) => {
595
- if (seen.has(row.id)) return false;
596
- seen.add(row.id);
597
- return true;
598
- });
599
- const prepared = dedupedMessages.filter((message) => message !== null && message !== void 0).map((message) => {
600
- const { _index, ...messageWithoutIndex } = message;
601
- return messageWithoutIndex;
602
- });
603
- if (format === "v2") {
604
- return prepared.map((msg) => ({
605
- ...msg,
606
- content: msg.content || { format: 2, parts: [{ type: "text", text: "" }] }
607
- }));
608
- }
609
- return prepared;
610
860
  }
611
861
  async getMessagesPaginated(args) {
612
862
  const { threadId, selectBy, format } = args;
@@ -615,9 +865,9 @@ var UpstashStore = class extends storage.MastraStorage {
615
865
  const toDate = dateRange?.end;
616
866
  const threadMessagesKey = this.getThreadMessagesKey(threadId);
617
867
  const messages = [];
618
- const includedMessages = await this._getIncludedMessages(threadId, selectBy);
619
- messages.push(...includedMessages);
620
868
  try {
869
+ const includedMessages = await this._getIncludedMessages(threadId, selectBy);
870
+ messages.push(...includedMessages);
621
871
  const allMessageIds = await this.redis.zrange(threadMessagesKey, 0, -1);
622
872
  if (allMessageIds.length === 0) {
623
873
  return {
@@ -654,8 +904,20 @@ var UpstashStore = class extends storage.MastraStorage {
654
904
  perPage,
655
905
  hasMore
656
906
  };
657
- } catch (error) {
658
- console.error("Failed to get paginated messages:", error);
907
+ } catch (error$1) {
908
+ const mastraError = new error.MastraError(
909
+ {
910
+ id: "STORAGE_UPSTASH_STORAGE_GET_MESSAGES_PAGINATED_FAILED",
911
+ domain: error.ErrorDomain.STORAGE,
912
+ category: error.ErrorCategory.THIRD_PARTY,
913
+ details: {
914
+ threadId
915
+ }
916
+ },
917
+ error$1
918
+ );
919
+ this.logger.error(mastraError.toString());
920
+ this.logger?.trackException(mastraError);
659
921
  return {
660
922
  messages: [],
661
923
  total: 0,
@@ -667,17 +929,33 @@ var UpstashStore = class extends storage.MastraStorage {
667
929
  }
668
930
  async persistWorkflowSnapshot(params) {
669
931
  const { namespace = "workflows", workflowName, runId, snapshot } = params;
670
- await this.insert({
671
- tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
672
- record: {
673
- namespace,
674
- workflow_name: workflowName,
675
- run_id: runId,
676
- snapshot,
677
- createdAt: /* @__PURE__ */ new Date(),
678
- updatedAt: /* @__PURE__ */ new Date()
679
- }
680
- });
932
+ try {
933
+ await this.insert({
934
+ tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
935
+ record: {
936
+ namespace,
937
+ workflow_name: workflowName,
938
+ run_id: runId,
939
+ snapshot,
940
+ createdAt: /* @__PURE__ */ new Date(),
941
+ updatedAt: /* @__PURE__ */ new Date()
942
+ }
943
+ });
944
+ } catch (error$1) {
945
+ throw new error.MastraError(
946
+ {
947
+ id: "STORAGE_UPSTASH_STORAGE_PERSIST_WORKFLOW_SNAPSHOT_FAILED",
948
+ domain: error.ErrorDomain.STORAGE,
949
+ category: error.ErrorCategory.THIRD_PARTY,
950
+ details: {
951
+ namespace,
952
+ workflowName,
953
+ runId
954
+ }
955
+ },
956
+ error$1
957
+ );
958
+ }
681
959
  }
682
960
  async loadWorkflowSnapshot(params) {
683
961
  const { namespace = "workflows", workflowName, runId } = params;
@@ -686,9 +964,25 @@ var UpstashStore = class extends storage.MastraStorage {
686
964
  workflow_name: workflowName,
687
965
  run_id: runId
688
966
  });
689
- const data = await this.redis.get(key);
690
- if (!data) return null;
691
- return data.snapshot;
967
+ try {
968
+ const data = await this.redis.get(key);
969
+ if (!data) return null;
970
+ return data.snapshot;
971
+ } catch (error$1) {
972
+ throw new error.MastraError(
973
+ {
974
+ id: "STORAGE_UPSTASH_STORAGE_LOAD_WORKFLOW_SNAPSHOT_FAILED",
975
+ domain: error.ErrorDomain.STORAGE,
976
+ category: error.ErrorCategory.THIRD_PARTY,
977
+ details: {
978
+ namespace,
979
+ workflowName,
980
+ runId
981
+ }
982
+ },
983
+ error$1
984
+ );
985
+ }
692
986
  }
693
987
  /**
694
988
  * Get all evaluations with pagination and total count
@@ -775,9 +1069,22 @@ var UpstashStore = class extends storage.MastraStorage {
775
1069
  perPage,
776
1070
  hasMore
777
1071
  };
778
- } catch (error) {
1072
+ } catch (error$1) {
779
1073
  const { page = 0, perPage = 100 } = options || {};
780
- console.error("Failed to get evals:", error);
1074
+ const mastraError = new error.MastraError(
1075
+ {
1076
+ id: "STORAGE_UPSTASH_STORAGE_GET_EVALS_FAILED",
1077
+ domain: error.ErrorDomain.STORAGE,
1078
+ category: error.ErrorCategory.THIRD_PARTY,
1079
+ details: {
1080
+ page,
1081
+ perPage
1082
+ }
1083
+ },
1084
+ error$1
1085
+ );
1086
+ this.logger.error(mastraError.toString());
1087
+ this.logger?.trackException(mastraError);
781
1088
  return {
782
1089
  evals: [],
783
1090
  total: 0,
@@ -829,9 +1136,20 @@ var UpstashStore = class extends storage.MastraStorage {
829
1136
  runs = runs.slice(offset, offset + limit);
830
1137
  }
831
1138
  return { runs, total };
832
- } catch (error) {
833
- console.error("Error getting workflow runs:", error);
834
- throw error;
1139
+ } catch (error$1) {
1140
+ throw new error.MastraError(
1141
+ {
1142
+ id: "STORAGE_UPSTASH_STORAGE_GET_WORKFLOW_RUNS_FAILED",
1143
+ domain: error.ErrorDomain.STORAGE,
1144
+ category: error.ErrorCategory.THIRD_PARTY,
1145
+ details: {
1146
+ namespace,
1147
+ workflowName: workflowName || "",
1148
+ resourceId: resourceId || ""
1149
+ }
1150
+ },
1151
+ error$1
1152
+ );
835
1153
  }
836
1154
  }
837
1155
  async getWorkflowRunById({
@@ -851,9 +1169,20 @@ var UpstashStore = class extends storage.MastraStorage {
851
1169
  const data = workflows.find((w) => w?.run_id === runId && w?.workflow_name === workflowName);
852
1170
  if (!data) return null;
853
1171
  return this.parseWorkflowRun(data);
854
- } catch (error) {
855
- console.error("Error getting workflow run by ID:", error);
856
- throw error;
1172
+ } catch (error$1) {
1173
+ throw new error.MastraError(
1174
+ {
1175
+ id: "STORAGE_UPSTASH_STORAGE_GET_WORKFLOW_RUN_BY_ID_FAILED",
1176
+ domain: error.ErrorDomain.STORAGE,
1177
+ category: error.ErrorCategory.THIRD_PARTY,
1178
+ details: {
1179
+ namespace,
1180
+ runId,
1181
+ workflowName: workflowName || ""
1182
+ }
1183
+ },
1184
+ error$1
1185
+ );
857
1186
  }
858
1187
  }
859
1188
  async close() {
@@ -862,6 +1191,75 @@ var UpstashStore = class extends storage.MastraStorage {
862
1191
  this.logger.error("updateMessages is not yet implemented in UpstashStore");
863
1192
  throw new Error("Method not implemented");
864
1193
  }
1194
+ async getResourceById({ resourceId }) {
1195
+ try {
1196
+ const key = `${storage.TABLE_RESOURCES}:${resourceId}`;
1197
+ const data = await this.redis.get(key);
1198
+ if (!data) {
1199
+ return null;
1200
+ }
1201
+ return {
1202
+ ...data,
1203
+ createdAt: new Date(data.createdAt),
1204
+ updatedAt: new Date(data.updatedAt),
1205
+ // Ensure workingMemory is always returned as a string, regardless of automatic parsing
1206
+ workingMemory: typeof data.workingMemory === "object" ? JSON.stringify(data.workingMemory) : data.workingMemory,
1207
+ metadata: typeof data.metadata === "string" ? JSON.parse(data.metadata) : data.metadata
1208
+ };
1209
+ } catch (error) {
1210
+ this.logger.error("Error getting resource by ID:", error);
1211
+ throw error;
1212
+ }
1213
+ }
1214
+ async saveResource({ resource }) {
1215
+ try {
1216
+ const key = `${storage.TABLE_RESOURCES}:${resource.id}`;
1217
+ const serializedResource = {
1218
+ ...resource,
1219
+ metadata: JSON.stringify(resource.metadata),
1220
+ createdAt: resource.createdAt.toISOString(),
1221
+ updatedAt: resource.updatedAt.toISOString()
1222
+ };
1223
+ await this.redis.set(key, serializedResource);
1224
+ return resource;
1225
+ } catch (error) {
1226
+ this.logger.error("Error saving resource:", error);
1227
+ throw error;
1228
+ }
1229
+ }
1230
+ async updateResource({
1231
+ resourceId,
1232
+ workingMemory,
1233
+ metadata
1234
+ }) {
1235
+ try {
1236
+ const existingResource = await this.getResourceById({ resourceId });
1237
+ if (!existingResource) {
1238
+ const newResource = {
1239
+ id: resourceId,
1240
+ workingMemory,
1241
+ metadata: metadata || {},
1242
+ createdAt: /* @__PURE__ */ new Date(),
1243
+ updatedAt: /* @__PURE__ */ new Date()
1244
+ };
1245
+ return this.saveResource({ resource: newResource });
1246
+ }
1247
+ const updatedResource = {
1248
+ ...existingResource,
1249
+ workingMemory: workingMemory !== void 0 ? workingMemory : existingResource.workingMemory,
1250
+ metadata: {
1251
+ ...existingResource.metadata,
1252
+ ...metadata
1253
+ },
1254
+ updatedAt: /* @__PURE__ */ new Date()
1255
+ };
1256
+ await this.saveResource({ resource: updatedResource });
1257
+ return updatedResource;
1258
+ } catch (error) {
1259
+ this.logger.error("Error updating resource:", error);
1260
+ throw error;
1261
+ }
1262
+ }
865
1263
  };
866
1264
  var UpstashFilterTranslator = class extends filter.BaseFilterTranslator {
867
1265
  getSupportedOperators() {
@@ -1083,14 +1481,26 @@ var UpstashVector = class extends vector.MastraVector {
1083
1481
  vector,
1084
1482
  metadata: metadata?.[index]
1085
1483
  }));
1086
- await this.client.upsert(points, {
1087
- namespace
1088
- });
1089
- return generatedIds;
1484
+ try {
1485
+ await this.client.upsert(points, {
1486
+ namespace
1487
+ });
1488
+ return generatedIds;
1489
+ } catch (error$1) {
1490
+ throw new error.MastraError(
1491
+ {
1492
+ id: "STORAGE_UPSTASH_VECTOR_UPSERT_FAILED",
1493
+ domain: error.ErrorDomain.STORAGE,
1494
+ category: error.ErrorCategory.THIRD_PARTY,
1495
+ details: { namespace, vectorCount: vectors.length }
1496
+ },
1497
+ error$1
1498
+ );
1499
+ }
1090
1500
  }
1091
1501
  /**
1092
1502
  * Transforms a Mastra vector filter into an Upstash-compatible filter string.
1093
- * @param {VectorFilter} [filter] - The filter to transform.
1503
+ * @param {UpstashVectorFilter} [filter] - The filter to transform.
1094
1504
  * @returns {string | undefined} The transformed filter string, or undefined if no filter is provided.
1095
1505
  */
1096
1506
  transformFilter(filter) {
@@ -1117,29 +1527,52 @@ var UpstashVector = class extends vector.MastraVector {
1117
1527
  filter,
1118
1528
  includeVector = false
1119
1529
  }) {
1120
- const ns = this.client.namespace(namespace);
1121
- const filterString = this.transformFilter(filter);
1122
- const results = await ns.query({
1123
- topK,
1124
- vector: queryVector,
1125
- includeVectors: includeVector,
1126
- includeMetadata: true,
1127
- ...filterString ? { filter: filterString } : {}
1128
- });
1129
- return (results || []).map((result) => ({
1130
- id: `${result.id}`,
1131
- score: result.score,
1132
- metadata: result.metadata,
1133
- ...includeVector && { vector: result.vector || [] }
1134
- }));
1530
+ try {
1531
+ const ns = this.client.namespace(namespace);
1532
+ const filterString = this.transformFilter(filter);
1533
+ const results = await ns.query({
1534
+ topK,
1535
+ vector: queryVector,
1536
+ includeVectors: includeVector,
1537
+ includeMetadata: true,
1538
+ ...filterString ? { filter: filterString } : {}
1539
+ });
1540
+ return (results || []).map((result) => ({
1541
+ id: `${result.id}`,
1542
+ score: result.score,
1543
+ metadata: result.metadata,
1544
+ ...includeVector && { vector: result.vector || [] }
1545
+ }));
1546
+ } catch (error$1) {
1547
+ throw new error.MastraError(
1548
+ {
1549
+ id: "STORAGE_UPSTASH_VECTOR_QUERY_FAILED",
1550
+ domain: error.ErrorDomain.STORAGE,
1551
+ category: error.ErrorCategory.THIRD_PARTY,
1552
+ details: { namespace, topK }
1553
+ },
1554
+ error$1
1555
+ );
1556
+ }
1135
1557
  }
1136
1558
  /**
1137
1559
  * Lists all namespaces in the Upstash vector index, which correspond to indexes.
1138
1560
  * @returns {Promise<string[]>} A promise that resolves to a list of index names.
1139
1561
  */
1140
1562
  async listIndexes() {
1141
- const indexes = await this.client.listNamespaces();
1142
- return indexes.filter(Boolean);
1563
+ try {
1564
+ const indexes = await this.client.listNamespaces();
1565
+ return indexes.filter(Boolean);
1566
+ } catch (error$1) {
1567
+ throw new error.MastraError(
1568
+ {
1569
+ id: "STORAGE_UPSTASH_VECTOR_LIST_INDEXES_FAILED",
1570
+ domain: error.ErrorDomain.STORAGE,
1571
+ category: error.ErrorCategory.THIRD_PARTY
1572
+ },
1573
+ error$1
1574
+ );
1575
+ }
1143
1576
  }
1144
1577
  /**
1145
1578
  * Retrieves statistics about a vector index.
@@ -1148,12 +1581,24 @@ var UpstashVector = class extends vector.MastraVector {
1148
1581
  * @returns A promise that resolves to the index statistics including dimension, count and metric
1149
1582
  */
1150
1583
  async describeIndex({ indexName: namespace }) {
1151
- const info = await this.client.info();
1152
- return {
1153
- dimension: info.dimension,
1154
- count: info.namespaces?.[namespace]?.vectorCount || 0,
1155
- metric: info?.similarityFunction?.toLowerCase()
1156
- };
1584
+ try {
1585
+ const info = await this.client.info();
1586
+ return {
1587
+ dimension: info.dimension,
1588
+ count: info.namespaces?.[namespace]?.vectorCount || 0,
1589
+ metric: info?.similarityFunction?.toLowerCase()
1590
+ };
1591
+ } catch (error$1) {
1592
+ throw new error.MastraError(
1593
+ {
1594
+ id: "STORAGE_UPSTASH_VECTOR_DESCRIBE_INDEX_FAILED",
1595
+ domain: error.ErrorDomain.STORAGE,
1596
+ category: error.ErrorCategory.THIRD_PARTY,
1597
+ details: { namespace }
1598
+ },
1599
+ error$1
1600
+ );
1601
+ }
1157
1602
  }
1158
1603
  /**
1159
1604
  * Deletes an index (namespace).
@@ -1163,8 +1608,16 @@ var UpstashVector = class extends vector.MastraVector {
1163
1608
  async deleteIndex({ indexName: namespace }) {
1164
1609
  try {
1165
1610
  await this.client.deleteNamespace(namespace);
1166
- } catch (error) {
1167
- this.logger.error("Failed to delete namespace:", error);
1611
+ } catch (error$1) {
1612
+ throw new error.MastraError(
1613
+ {
1614
+ id: "STORAGE_UPSTASH_VECTOR_DELETE_INDEX_FAILED",
1615
+ domain: error.ErrorDomain.STORAGE,
1616
+ category: error.ErrorCategory.THIRD_PARTY,
1617
+ details: { namespace }
1618
+ },
1619
+ error$1
1620
+ );
1168
1621
  }
1169
1622
  }
1170
1623
  /**
@@ -1185,6 +1638,18 @@ var UpstashVector = class extends vector.MastraVector {
1185
1638
  if (!update.vector && update.metadata) {
1186
1639
  throw new Error("Both vector and metadata must be provided for an update");
1187
1640
  }
1641
+ } catch (error$1) {
1642
+ throw new error.MastraError(
1643
+ {
1644
+ id: "STORAGE_UPSTASH_VECTOR_UPDATE_VECTOR_FAILED",
1645
+ domain: error.ErrorDomain.STORAGE,
1646
+ category: error.ErrorCategory.THIRD_PARTY,
1647
+ details: { namespace, id }
1648
+ },
1649
+ error$1
1650
+ );
1651
+ }
1652
+ try {
1188
1653
  const updatePayload = { id };
1189
1654
  if (update.vector) {
1190
1655
  updatePayload.vector = update.vector;
@@ -1200,8 +1665,16 @@ var UpstashVector = class extends vector.MastraVector {
1200
1665
  await this.client.upsert(points, {
1201
1666
  namespace
1202
1667
  });
1203
- } catch (error) {
1204
- throw new Error(`Failed to update vector by id: ${id} for index name: ${namespace}: ${error.message}`);
1668
+ } catch (error$1) {
1669
+ throw new error.MastraError(
1670
+ {
1671
+ id: "STORAGE_UPSTASH_VECTOR_UPDATE_VECTOR_FAILED",
1672
+ domain: error.ErrorDomain.STORAGE,
1673
+ category: error.ErrorCategory.THIRD_PARTY,
1674
+ details: { namespace, id }
1675
+ },
1676
+ error$1
1677
+ );
1205
1678
  }
1206
1679
  }
1207
1680
  /**
@@ -1216,8 +1689,17 @@ var UpstashVector = class extends vector.MastraVector {
1216
1689
  await this.client.delete(id, {
1217
1690
  namespace
1218
1691
  });
1219
- } catch (error) {
1220
- this.logger.error(`Failed to delete vector by id: ${id} for namespace: ${namespace}:`, error);
1692
+ } catch (error$1) {
1693
+ const mastraError = new error.MastraError(
1694
+ {
1695
+ id: "STORAGE_UPSTASH_VECTOR_DELETE_VECTOR_FAILED",
1696
+ domain: error.ErrorDomain.STORAGE,
1697
+ category: error.ErrorCategory.THIRD_PARTY,
1698
+ details: { namespace, id }
1699
+ },
1700
+ error$1
1701
+ );
1702
+ this.logger?.error(mastraError.toString());
1221
1703
  }
1222
1704
  }
1223
1705
  };