@mastra/upstash 0.11.0 → 0.11.1-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/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');
@@ -198,8 +199,18 @@ var UpstashStore = class extends storage.MastraStorage {
198
199
  });
199
200
  }
200
201
  return filteredEvals.map((record) => this.transformEvalRecord(record));
201
- } catch (error) {
202
- console.error("Failed to get evals for the specified agent:", error);
202
+ } catch (error$1) {
203
+ const mastraError = new error.MastraError(
204
+ {
205
+ id: "STORAGE_UPSTASH_STORAGE_GET_EVALS_BY_AGENT_NAME_FAILED",
206
+ domain: error.ErrorDomain.STORAGE,
207
+ category: error.ErrorCategory.THIRD_PARTY,
208
+ details: { agentName }
209
+ },
210
+ error$1
211
+ );
212
+ this.logger?.trackException(mastraError);
213
+ this.logger.error(mastraError.toString());
203
214
  return [];
204
215
  }
205
216
  }
@@ -213,8 +224,19 @@ var UpstashStore = class extends storage.MastraStorage {
213
224
  end: args.toDate
214
225
  };
215
226
  }
216
- const { traces } = await this.getTracesPaginated(args);
217
- return traces;
227
+ try {
228
+ const { traces } = await this.getTracesPaginated(args);
229
+ return traces;
230
+ } catch (error$1) {
231
+ throw new error.MastraError(
232
+ {
233
+ id: "STORAGE_UPSTASH_STORAGE_GET_TRACES_FAILED",
234
+ domain: error.ErrorDomain.STORAGE,
235
+ category: error.ErrorCategory.THIRD_PARTY
236
+ },
237
+ error$1
238
+ );
239
+ }
218
240
  }
219
241
  async getTracesPaginated(args) {
220
242
  const { name, scope, page = 0, perPage = 100, attributes, filters, dateRange } = args;
@@ -297,8 +319,21 @@ var UpstashStore = class extends storage.MastraStorage {
297
319
  perPage: resolvedPerPage,
298
320
  hasMore
299
321
  };
300
- } catch (error) {
301
- console.error("Failed to get traces:", error);
322
+ } catch (error$1) {
323
+ const mastraError = new error.MastraError(
324
+ {
325
+ id: "STORAGE_UPSTASH_STORAGE_GET_TRACES_PAGINATED_FAILED",
326
+ domain: error.ErrorDomain.STORAGE,
327
+ category: error.ErrorCategory.THIRD_PARTY,
328
+ details: {
329
+ name: args.name || "",
330
+ scope: args.scope || ""
331
+ }
332
+ },
333
+ error$1
334
+ );
335
+ this.logger?.trackException(mastraError);
336
+ this.logger.error(mastraError.toString());
302
337
  return {
303
338
  traces: [],
304
339
  total: 0,
@@ -312,7 +347,21 @@ var UpstashStore = class extends storage.MastraStorage {
312
347
  tableName,
313
348
  schema
314
349
  }) {
315
- await this.redis.set(`schema:${tableName}`, schema);
350
+ try {
351
+ await this.redis.set(`schema:${tableName}`, schema);
352
+ } catch (error$1) {
353
+ throw new error.MastraError(
354
+ {
355
+ id: "STORAGE_UPSTASH_STORAGE_CREATE_TABLE_FAILED",
356
+ domain: error.ErrorDomain.STORAGE,
357
+ category: error.ErrorCategory.THIRD_PARTY,
358
+ details: {
359
+ tableName
360
+ }
361
+ },
362
+ error$1
363
+ );
364
+ }
316
365
  }
317
366
  /**
318
367
  * No-op: This backend is schemaless and does not require schema changes.
@@ -324,43 +373,113 @@ var UpstashStore = class extends storage.MastraStorage {
324
373
  }
325
374
  async clearTable({ tableName }) {
326
375
  const pattern = `${tableName}:*`;
327
- await this.scanAndDelete(pattern);
376
+ try {
377
+ await this.scanAndDelete(pattern);
378
+ } catch (error$1) {
379
+ throw new error.MastraError(
380
+ {
381
+ id: "STORAGE_UPSTASH_STORAGE_CLEAR_TABLE_FAILED",
382
+ domain: error.ErrorDomain.STORAGE,
383
+ category: error.ErrorCategory.THIRD_PARTY,
384
+ details: {
385
+ tableName
386
+ }
387
+ },
388
+ error$1
389
+ );
390
+ }
328
391
  }
329
392
  async insert({ tableName, record }) {
330
393
  const { key, processedRecord } = this.processRecord(tableName, record);
331
- await this.redis.set(key, processedRecord);
394
+ try {
395
+ await this.redis.set(key, processedRecord);
396
+ } catch (error$1) {
397
+ throw new error.MastraError(
398
+ {
399
+ id: "STORAGE_UPSTASH_STORAGE_INSERT_FAILED",
400
+ domain: error.ErrorDomain.STORAGE,
401
+ category: error.ErrorCategory.THIRD_PARTY,
402
+ details: {
403
+ tableName
404
+ }
405
+ },
406
+ error$1
407
+ );
408
+ }
332
409
  }
333
410
  async batchInsert(input) {
334
411
  const { tableName, records } = input;
335
412
  if (!records.length) return;
336
413
  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);
414
+ try {
415
+ for (let i = 0; i < records.length; i += batchSize) {
416
+ const batch = records.slice(i, i + batchSize);
417
+ const pipeline = this.redis.pipeline();
418
+ for (const record of batch) {
419
+ const { key, processedRecord } = this.processRecord(tableName, record);
420
+ pipeline.set(key, processedRecord);
421
+ }
422
+ await pipeline.exec();
343
423
  }
344
- await pipeline.exec();
424
+ } catch (error$1) {
425
+ throw new error.MastraError(
426
+ {
427
+ id: "STORAGE_UPSTASH_STORAGE_BATCH_INSERT_FAILED",
428
+ domain: error.ErrorDomain.STORAGE,
429
+ category: error.ErrorCategory.THIRD_PARTY,
430
+ details: {
431
+ tableName
432
+ }
433
+ },
434
+ error$1
435
+ );
345
436
  }
346
437
  }
347
438
  async load({ tableName, keys }) {
348
439
  const key = this.getKey(tableName, keys);
349
- const data = await this.redis.get(key);
350
- return data || null;
440
+ try {
441
+ const data = await this.redis.get(key);
442
+ return data || null;
443
+ } catch (error$1) {
444
+ throw new error.MastraError(
445
+ {
446
+ id: "STORAGE_UPSTASH_STORAGE_LOAD_FAILED",
447
+ domain: error.ErrorDomain.STORAGE,
448
+ category: error.ErrorCategory.THIRD_PARTY,
449
+ details: {
450
+ tableName
451
+ }
452
+ },
453
+ error$1
454
+ );
455
+ }
351
456
  }
352
457
  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
- };
458
+ try {
459
+ const thread = await this.load({
460
+ tableName: storage.TABLE_THREADS,
461
+ keys: { id: threadId }
462
+ });
463
+ if (!thread) return null;
464
+ return {
465
+ ...thread,
466
+ createdAt: this.ensureDate(thread.createdAt),
467
+ updatedAt: this.ensureDate(thread.updatedAt),
468
+ metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata
469
+ };
470
+ } catch (error$1) {
471
+ throw new error.MastraError(
472
+ {
473
+ id: "STORAGE_UPSTASH_STORAGE_GET_THREAD_BY_ID_FAILED",
474
+ domain: error.ErrorDomain.STORAGE,
475
+ category: error.ErrorCategory.THIRD_PARTY,
476
+ details: {
477
+ threadId
478
+ }
479
+ },
480
+ error$1
481
+ );
482
+ }
364
483
  }
365
484
  /**
366
485
  * @deprecated use getThreadsByResourceIdPaginated instead
@@ -389,8 +508,20 @@ var UpstashStore = class extends storage.MastraStorage {
389
508
  }
390
509
  allThreads.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
391
510
  return allThreads;
392
- } catch (error) {
393
- console.error("Error in getThreadsByResourceId:", error);
511
+ } catch (error$1) {
512
+ const mastraError = new error.MastraError(
513
+ {
514
+ id: "STORAGE_UPSTASH_STORAGE_GET_THREADS_BY_RESOURCE_ID_FAILED",
515
+ domain: error.ErrorDomain.STORAGE,
516
+ category: error.ErrorCategory.THIRD_PARTY,
517
+ details: {
518
+ resourceId
519
+ }
520
+ },
521
+ error$1
522
+ );
523
+ this.logger?.trackException(mastraError);
524
+ this.logger.error(mastraError.toString());
394
525
  return [];
395
526
  }
396
527
  }
@@ -410,8 +541,22 @@ var UpstashStore = class extends storage.MastraStorage {
410
541
  perPage,
411
542
  hasMore
412
543
  };
413
- } catch (error) {
414
- console.error("Error in getThreadsByResourceIdPaginated:", error);
544
+ } catch (error$1) {
545
+ const mastraError = new error.MastraError(
546
+ {
547
+ id: "STORAGE_UPSTASH_STORAGE_GET_THREADS_BY_RESOURCE_ID_PAGINATED_FAILED",
548
+ domain: error.ErrorDomain.STORAGE,
549
+ category: error.ErrorCategory.THIRD_PARTY,
550
+ details: {
551
+ resourceId,
552
+ page,
553
+ perPage
554
+ }
555
+ },
556
+ error$1
557
+ );
558
+ this.logger?.trackException(mastraError);
559
+ this.logger.error(mastraError.toString());
415
560
  return {
416
561
  threads: [],
417
562
  total: 0,
@@ -422,11 +567,28 @@ var UpstashStore = class extends storage.MastraStorage {
422
567
  }
423
568
  }
424
569
  async saveThread({ thread }) {
425
- await this.insert({
426
- tableName: storage.TABLE_THREADS,
427
- record: thread
428
- });
429
- return thread;
570
+ try {
571
+ await this.insert({
572
+ tableName: storage.TABLE_THREADS,
573
+ record: thread
574
+ });
575
+ return thread;
576
+ } catch (error$1) {
577
+ const mastraError = new error.MastraError(
578
+ {
579
+ id: "STORAGE_UPSTASH_STORAGE_SAVE_THREAD_FAILED",
580
+ domain: error.ErrorDomain.STORAGE,
581
+ category: error.ErrorCategory.THIRD_PARTY,
582
+ details: {
583
+ threadId: thread.id
584
+ }
585
+ },
586
+ error$1
587
+ );
588
+ this.logger?.trackException(mastraError);
589
+ this.logger.error(mastraError.toString());
590
+ throw mastraError;
591
+ }
430
592
  }
431
593
  async updateThread({
432
594
  id,
@@ -435,7 +597,15 @@ var UpstashStore = class extends storage.MastraStorage {
435
597
  }) {
436
598
  const thread = await this.getThreadById({ threadId: id });
437
599
  if (!thread) {
438
- throw new Error(`Thread ${id} not found`);
600
+ throw new error.MastraError({
601
+ id: "STORAGE_UPSTASH_STORAGE_UPDATE_THREAD_FAILED",
602
+ domain: error.ErrorDomain.STORAGE,
603
+ category: error.ErrorCategory.USER,
604
+ text: `Thread ${id} not found`,
605
+ details: {
606
+ threadId: id
607
+ }
608
+ });
439
609
  }
440
610
  const updatedThread = {
441
611
  ...thread,
@@ -445,34 +615,73 @@ var UpstashStore = class extends storage.MastraStorage {
445
615
  ...metadata
446
616
  }
447
617
  };
448
- await this.saveThread({ thread: updatedThread });
449
- return updatedThread;
618
+ try {
619
+ await this.saveThread({ thread: updatedThread });
620
+ return updatedThread;
621
+ } catch (error$1) {
622
+ throw new error.MastraError(
623
+ {
624
+ id: "STORAGE_UPSTASH_STORAGE_UPDATE_THREAD_FAILED",
625
+ domain: error.ErrorDomain.STORAGE,
626
+ category: error.ErrorCategory.THIRD_PARTY,
627
+ details: {
628
+ threadId: id
629
+ }
630
+ },
631
+ error$1
632
+ );
633
+ }
450
634
  }
451
635
  async deleteThread({ threadId }) {
452
636
  const threadKey = this.getKey(storage.TABLE_THREADS, { id: threadId });
453
637
  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);
638
+ try {
639
+ const messageIds = await this.redis.zrange(threadMessagesKey, 0, -1);
640
+ const pipeline = this.redis.pipeline();
641
+ pipeline.del(threadKey);
642
+ pipeline.del(threadMessagesKey);
643
+ for (let i = 0; i < messageIds.length; i++) {
644
+ const messageId = messageIds[i];
645
+ const messageKey = this.getMessageKey(threadId, messageId);
646
+ pipeline.del(messageKey);
647
+ }
648
+ await pipeline.exec();
649
+ await this.scanAndDelete(this.getMessageKey(threadId, "*"));
650
+ } catch (error$1) {
651
+ throw new error.MastraError(
652
+ {
653
+ id: "STORAGE_UPSTASH_STORAGE_DELETE_THREAD_FAILED",
654
+ domain: error.ErrorDomain.STORAGE,
655
+ category: error.ErrorCategory.THIRD_PARTY,
656
+ details: {
657
+ threadId
658
+ }
659
+ },
660
+ error$1
661
+ );
462
662
  }
463
- await pipeline.exec();
464
- await this.scanAndDelete(this.getMessageKey(threadId, "*"));
465
663
  }
466
664
  async saveMessages(args) {
467
665
  const { messages, format = "v1" } = args;
468
666
  if (messages.length === 0) return [];
469
667
  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`);
668
+ try {
669
+ if (!threadId) {
670
+ throw new Error("Thread ID is required");
671
+ }
672
+ const thread = await this.getThreadById({ threadId });
673
+ if (!thread) {
674
+ throw new Error(`Thread ${threadId} not found`);
675
+ }
676
+ } catch (error$1) {
677
+ throw new error.MastraError(
678
+ {
679
+ id: "STORAGE_UPSTASH_STORAGE_SAVE_MESSAGES_INVALID_ARGS",
680
+ domain: error.ErrorDomain.STORAGE,
681
+ category: error.ErrorCategory.USER
682
+ },
683
+ error$1
684
+ );
476
685
  }
477
686
  const messagesWithIndex = messages.map((message, index) => ({
478
687
  ...message,
@@ -480,32 +689,46 @@ var UpstashStore = class extends storage.MastraStorage {
480
689
  }));
481
690
  const threadKey = this.getKey(storage.TABLE_THREADS, { id: threadId });
482
691
  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);
692
+ try {
693
+ const batchSize = 1e3;
694
+ for (let i = 0; i < messagesWithIndex.length; i += batchSize) {
695
+ const batch = messagesWithIndex.slice(i, i + batchSize);
696
+ const pipeline = this.redis.pipeline();
697
+ for (const message of batch) {
698
+ const key = this.getMessageKey(message.threadId, message.id);
699
+ const createdAtScore = new Date(message.createdAt).getTime();
700
+ const score = message._index !== void 0 ? message._index : createdAtScore;
701
+ pipeline.set(key, message);
702
+ pipeline.zadd(this.getThreadMessagesKey(message.threadId), {
703
+ score,
704
+ member: message.id
705
+ });
706
+ }
707
+ if (i === 0 && existingThread) {
708
+ const updatedThread = {
709
+ ...existingThread,
710
+ updatedAt: /* @__PURE__ */ new Date()
711
+ };
712
+ pipeline.set(threadKey, this.processRecord(storage.TABLE_THREADS, updatedThread).processedRecord);
713
+ }
714
+ await pipeline.exec();
503
715
  }
504
- await pipeline.exec();
716
+ const list = new agent.MessageList().add(messages, "memory");
717
+ if (format === `v2`) return list.get.all.v2();
718
+ return list.get.all.v1();
719
+ } catch (error$1) {
720
+ throw new error.MastraError(
721
+ {
722
+ id: "STORAGE_UPSTASH_STORAGE_SAVE_MESSAGES_FAILED",
723
+ domain: error.ErrorDomain.STORAGE,
724
+ category: error.ErrorCategory.THIRD_PARTY,
725
+ details: {
726
+ threadId
727
+ }
728
+ },
729
+ error$1
730
+ );
505
731
  }
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
732
  }
510
733
  async _getIncludedMessages(threadId, selectBy) {
511
734
  const messageIds = /* @__PURE__ */ new Set();
@@ -550,63 +773,72 @@ var UpstashStore = class extends storage.MastraStorage {
550
773
  format
551
774
  }) {
552
775
  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;
776
+ try {
777
+ const allMessageIds = await this.redis.zrange(threadMessagesKey, 0, -1);
778
+ const limit = this.resolveMessageLimit({ last: selectBy?.last, defaultLimit: Number.MAX_SAFE_INTEGER });
779
+ const messageIds = /* @__PURE__ */ new Set();
780
+ const messageIdToThreadIds = {};
781
+ if (limit === 0 && !selectBy?.include) {
782
+ return [];
783
+ }
784
+ if (limit === Number.MAX_SAFE_INTEGER) {
785
+ const allIds = await this.redis.zrange(threadMessagesKey, 0, -1);
786
+ allIds.forEach((id) => {
787
+ messageIds.add(id);
788
+ messageIdToThreadIds[id] = threadId;
789
+ });
790
+ } else if (limit > 0) {
791
+ const latestIds = await this.redis.zrange(threadMessagesKey, -limit, -1);
792
+ latestIds.forEach((id) => {
793
+ messageIds.add(id);
794
+ messageIdToThreadIds[id] = threadId;
795
+ });
796
+ }
797
+ const includedMessages = await this._getIncludedMessages(threadId, selectBy);
798
+ const messages = [
799
+ ...includedMessages,
800
+ ...(await Promise.all(
801
+ Array.from(messageIds).map(async (id) => {
802
+ const tId = messageIdToThreadIds[id] || threadId;
803
+ const byThreadId = await this.redis.get(
804
+ this.getMessageKey(tId, id)
805
+ );
806
+ if (byThreadId) return byThreadId;
807
+ return null;
808
+ })
809
+ )).filter((msg) => msg !== null)
810
+ ];
811
+ messages.sort((a, b) => allMessageIds.indexOf(a.id) - allMessageIds.indexOf(b.id));
812
+ const seen = /* @__PURE__ */ new Set();
813
+ const dedupedMessages = messages.filter((row) => {
814
+ if (seen.has(row.id)) return false;
815
+ seen.add(row.id);
816
+ return true;
572
817
  });
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;
818
+ const prepared = dedupedMessages.filter((message) => message !== null && message !== void 0).map((message) => {
819
+ const { _index, ...messageWithoutIndex } = message;
820
+ return messageWithoutIndex;
578
821
  });
822
+ if (format === "v2") {
823
+ return prepared.map((msg) => ({
824
+ ...msg,
825
+ content: msg.content || { format: 2, parts: [{ type: "text", text: "" }] }
826
+ }));
827
+ }
828
+ return prepared;
829
+ } catch (error$1) {
830
+ throw new error.MastraError(
831
+ {
832
+ id: "STORAGE_UPSTASH_STORAGE_GET_MESSAGES_FAILED",
833
+ domain: error.ErrorDomain.STORAGE,
834
+ category: error.ErrorCategory.THIRD_PARTY,
835
+ details: {
836
+ threadId
837
+ }
838
+ },
839
+ error$1
840
+ );
579
841
  }
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
842
  }
611
843
  async getMessagesPaginated(args) {
612
844
  const { threadId, selectBy, format } = args;
@@ -615,9 +847,9 @@ var UpstashStore = class extends storage.MastraStorage {
615
847
  const toDate = dateRange?.end;
616
848
  const threadMessagesKey = this.getThreadMessagesKey(threadId);
617
849
  const messages = [];
618
- const includedMessages = await this._getIncludedMessages(threadId, selectBy);
619
- messages.push(...includedMessages);
620
850
  try {
851
+ const includedMessages = await this._getIncludedMessages(threadId, selectBy);
852
+ messages.push(...includedMessages);
621
853
  const allMessageIds = await this.redis.zrange(threadMessagesKey, 0, -1);
622
854
  if (allMessageIds.length === 0) {
623
855
  return {
@@ -654,8 +886,20 @@ var UpstashStore = class extends storage.MastraStorage {
654
886
  perPage,
655
887
  hasMore
656
888
  };
657
- } catch (error) {
658
- console.error("Failed to get paginated messages:", error);
889
+ } catch (error$1) {
890
+ const mastraError = new error.MastraError(
891
+ {
892
+ id: "STORAGE_UPSTASH_STORAGE_GET_MESSAGES_PAGINATED_FAILED",
893
+ domain: error.ErrorDomain.STORAGE,
894
+ category: error.ErrorCategory.THIRD_PARTY,
895
+ details: {
896
+ threadId
897
+ }
898
+ },
899
+ error$1
900
+ );
901
+ this.logger.error(mastraError.toString());
902
+ this.logger?.trackException(mastraError);
659
903
  return {
660
904
  messages: [],
661
905
  total: 0,
@@ -667,17 +911,33 @@ var UpstashStore = class extends storage.MastraStorage {
667
911
  }
668
912
  async persistWorkflowSnapshot(params) {
669
913
  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
- });
914
+ try {
915
+ await this.insert({
916
+ tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
917
+ record: {
918
+ namespace,
919
+ workflow_name: workflowName,
920
+ run_id: runId,
921
+ snapshot,
922
+ createdAt: /* @__PURE__ */ new Date(),
923
+ updatedAt: /* @__PURE__ */ new Date()
924
+ }
925
+ });
926
+ } catch (error$1) {
927
+ throw new error.MastraError(
928
+ {
929
+ id: "STORAGE_UPSTASH_STORAGE_PERSIST_WORKFLOW_SNAPSHOT_FAILED",
930
+ domain: error.ErrorDomain.STORAGE,
931
+ category: error.ErrorCategory.THIRD_PARTY,
932
+ details: {
933
+ namespace,
934
+ workflowName,
935
+ runId
936
+ }
937
+ },
938
+ error$1
939
+ );
940
+ }
681
941
  }
682
942
  async loadWorkflowSnapshot(params) {
683
943
  const { namespace = "workflows", workflowName, runId } = params;
@@ -686,9 +946,25 @@ var UpstashStore = class extends storage.MastraStorage {
686
946
  workflow_name: workflowName,
687
947
  run_id: runId
688
948
  });
689
- const data = await this.redis.get(key);
690
- if (!data) return null;
691
- return data.snapshot;
949
+ try {
950
+ const data = await this.redis.get(key);
951
+ if (!data) return null;
952
+ return data.snapshot;
953
+ } catch (error$1) {
954
+ throw new error.MastraError(
955
+ {
956
+ id: "STORAGE_UPSTASH_STORAGE_LOAD_WORKFLOW_SNAPSHOT_FAILED",
957
+ domain: error.ErrorDomain.STORAGE,
958
+ category: error.ErrorCategory.THIRD_PARTY,
959
+ details: {
960
+ namespace,
961
+ workflowName,
962
+ runId
963
+ }
964
+ },
965
+ error$1
966
+ );
967
+ }
692
968
  }
693
969
  /**
694
970
  * Get all evaluations with pagination and total count
@@ -775,9 +1051,22 @@ var UpstashStore = class extends storage.MastraStorage {
775
1051
  perPage,
776
1052
  hasMore
777
1053
  };
778
- } catch (error) {
1054
+ } catch (error$1) {
779
1055
  const { page = 0, perPage = 100 } = options || {};
780
- console.error("Failed to get evals:", error);
1056
+ const mastraError = new error.MastraError(
1057
+ {
1058
+ id: "STORAGE_UPSTASH_STORAGE_GET_EVALS_FAILED",
1059
+ domain: error.ErrorDomain.STORAGE,
1060
+ category: error.ErrorCategory.THIRD_PARTY,
1061
+ details: {
1062
+ page,
1063
+ perPage
1064
+ }
1065
+ },
1066
+ error$1
1067
+ );
1068
+ this.logger.error(mastraError.toString());
1069
+ this.logger?.trackException(mastraError);
781
1070
  return {
782
1071
  evals: [],
783
1072
  total: 0,
@@ -829,9 +1118,20 @@ var UpstashStore = class extends storage.MastraStorage {
829
1118
  runs = runs.slice(offset, offset + limit);
830
1119
  }
831
1120
  return { runs, total };
832
- } catch (error) {
833
- console.error("Error getting workflow runs:", error);
834
- throw error;
1121
+ } catch (error$1) {
1122
+ throw new error.MastraError(
1123
+ {
1124
+ id: "STORAGE_UPSTASH_STORAGE_GET_WORKFLOW_RUNS_FAILED",
1125
+ domain: error.ErrorDomain.STORAGE,
1126
+ category: error.ErrorCategory.THIRD_PARTY,
1127
+ details: {
1128
+ namespace,
1129
+ workflowName: workflowName || "",
1130
+ resourceId: resourceId || ""
1131
+ }
1132
+ },
1133
+ error$1
1134
+ );
835
1135
  }
836
1136
  }
837
1137
  async getWorkflowRunById({
@@ -851,13 +1151,28 @@ var UpstashStore = class extends storage.MastraStorage {
851
1151
  const data = workflows.find((w) => w?.run_id === runId && w?.workflow_name === workflowName);
852
1152
  if (!data) return null;
853
1153
  return this.parseWorkflowRun(data);
854
- } catch (error) {
855
- console.error("Error getting workflow run by ID:", error);
856
- throw error;
1154
+ } catch (error$1) {
1155
+ throw new error.MastraError(
1156
+ {
1157
+ id: "STORAGE_UPSTASH_STORAGE_GET_WORKFLOW_RUN_BY_ID_FAILED",
1158
+ domain: error.ErrorDomain.STORAGE,
1159
+ category: error.ErrorCategory.THIRD_PARTY,
1160
+ details: {
1161
+ namespace,
1162
+ runId,
1163
+ workflowName: workflowName || ""
1164
+ }
1165
+ },
1166
+ error$1
1167
+ );
857
1168
  }
858
1169
  }
859
1170
  async close() {
860
1171
  }
1172
+ async updateMessages(_args) {
1173
+ this.logger.error("updateMessages is not yet implemented in UpstashStore");
1174
+ throw new Error("Method not implemented");
1175
+ }
861
1176
  };
862
1177
  var UpstashFilterTranslator = class extends filter.BaseFilterTranslator {
863
1178
  getSupportedOperators() {
@@ -1079,10 +1394,22 @@ var UpstashVector = class extends vector.MastraVector {
1079
1394
  vector,
1080
1395
  metadata: metadata?.[index]
1081
1396
  }));
1082
- await this.client.upsert(points, {
1083
- namespace
1084
- });
1085
- return generatedIds;
1397
+ try {
1398
+ await this.client.upsert(points, {
1399
+ namespace
1400
+ });
1401
+ return generatedIds;
1402
+ } catch (error$1) {
1403
+ throw new error.MastraError(
1404
+ {
1405
+ id: "STORAGE_UPSTASH_VECTOR_UPSERT_FAILED",
1406
+ domain: error.ErrorDomain.STORAGE,
1407
+ category: error.ErrorCategory.THIRD_PARTY,
1408
+ details: { namespace, vectorCount: vectors.length }
1409
+ },
1410
+ error$1
1411
+ );
1412
+ }
1086
1413
  }
1087
1414
  /**
1088
1415
  * Transforms a Mastra vector filter into an Upstash-compatible filter string.
@@ -1113,29 +1440,52 @@ var UpstashVector = class extends vector.MastraVector {
1113
1440
  filter,
1114
1441
  includeVector = false
1115
1442
  }) {
1116
- const ns = this.client.namespace(namespace);
1117
- const filterString = this.transformFilter(filter);
1118
- const results = await ns.query({
1119
- topK,
1120
- vector: queryVector,
1121
- includeVectors: includeVector,
1122
- includeMetadata: true,
1123
- ...filterString ? { filter: filterString } : {}
1124
- });
1125
- return (results || []).map((result) => ({
1126
- id: `${result.id}`,
1127
- score: result.score,
1128
- metadata: result.metadata,
1129
- ...includeVector && { vector: result.vector || [] }
1130
- }));
1443
+ try {
1444
+ const ns = this.client.namespace(namespace);
1445
+ const filterString = this.transformFilter(filter);
1446
+ const results = await ns.query({
1447
+ topK,
1448
+ vector: queryVector,
1449
+ includeVectors: includeVector,
1450
+ includeMetadata: true,
1451
+ ...filterString ? { filter: filterString } : {}
1452
+ });
1453
+ return (results || []).map((result) => ({
1454
+ id: `${result.id}`,
1455
+ score: result.score,
1456
+ metadata: result.metadata,
1457
+ ...includeVector && { vector: result.vector || [] }
1458
+ }));
1459
+ } catch (error$1) {
1460
+ throw new error.MastraError(
1461
+ {
1462
+ id: "STORAGE_UPSTASH_VECTOR_QUERY_FAILED",
1463
+ domain: error.ErrorDomain.STORAGE,
1464
+ category: error.ErrorCategory.THIRD_PARTY,
1465
+ details: { namespace, topK }
1466
+ },
1467
+ error$1
1468
+ );
1469
+ }
1131
1470
  }
1132
1471
  /**
1133
1472
  * Lists all namespaces in the Upstash vector index, which correspond to indexes.
1134
1473
  * @returns {Promise<string[]>} A promise that resolves to a list of index names.
1135
1474
  */
1136
1475
  async listIndexes() {
1137
- const indexes = await this.client.listNamespaces();
1138
- return indexes.filter(Boolean);
1476
+ try {
1477
+ const indexes = await this.client.listNamespaces();
1478
+ return indexes.filter(Boolean);
1479
+ } catch (error$1) {
1480
+ throw new error.MastraError(
1481
+ {
1482
+ id: "STORAGE_UPSTASH_VECTOR_LIST_INDEXES_FAILED",
1483
+ domain: error.ErrorDomain.STORAGE,
1484
+ category: error.ErrorCategory.THIRD_PARTY
1485
+ },
1486
+ error$1
1487
+ );
1488
+ }
1139
1489
  }
1140
1490
  /**
1141
1491
  * Retrieves statistics about a vector index.
@@ -1144,12 +1494,24 @@ var UpstashVector = class extends vector.MastraVector {
1144
1494
  * @returns A promise that resolves to the index statistics including dimension, count and metric
1145
1495
  */
1146
1496
  async describeIndex({ indexName: namespace }) {
1147
- const info = await this.client.info();
1148
- return {
1149
- dimension: info.dimension,
1150
- count: info.namespaces?.[namespace]?.vectorCount || 0,
1151
- metric: info?.similarityFunction?.toLowerCase()
1152
- };
1497
+ try {
1498
+ const info = await this.client.info();
1499
+ return {
1500
+ dimension: info.dimension,
1501
+ count: info.namespaces?.[namespace]?.vectorCount || 0,
1502
+ metric: info?.similarityFunction?.toLowerCase()
1503
+ };
1504
+ } catch (error$1) {
1505
+ throw new error.MastraError(
1506
+ {
1507
+ id: "STORAGE_UPSTASH_VECTOR_DESCRIBE_INDEX_FAILED",
1508
+ domain: error.ErrorDomain.STORAGE,
1509
+ category: error.ErrorCategory.THIRD_PARTY,
1510
+ details: { namespace }
1511
+ },
1512
+ error$1
1513
+ );
1514
+ }
1153
1515
  }
1154
1516
  /**
1155
1517
  * Deletes an index (namespace).
@@ -1159,8 +1521,16 @@ var UpstashVector = class extends vector.MastraVector {
1159
1521
  async deleteIndex({ indexName: namespace }) {
1160
1522
  try {
1161
1523
  await this.client.deleteNamespace(namespace);
1162
- } catch (error) {
1163
- this.logger.error("Failed to delete namespace:", error);
1524
+ } catch (error$1) {
1525
+ throw new error.MastraError(
1526
+ {
1527
+ id: "STORAGE_UPSTASH_VECTOR_DELETE_INDEX_FAILED",
1528
+ domain: error.ErrorDomain.STORAGE,
1529
+ category: error.ErrorCategory.THIRD_PARTY,
1530
+ details: { namespace }
1531
+ },
1532
+ error$1
1533
+ );
1164
1534
  }
1165
1535
  }
1166
1536
  /**
@@ -1181,6 +1551,18 @@ var UpstashVector = class extends vector.MastraVector {
1181
1551
  if (!update.vector && update.metadata) {
1182
1552
  throw new Error("Both vector and metadata must be provided for an update");
1183
1553
  }
1554
+ } catch (error$1) {
1555
+ throw new error.MastraError(
1556
+ {
1557
+ id: "STORAGE_UPSTASH_VECTOR_UPDATE_VECTOR_FAILED",
1558
+ domain: error.ErrorDomain.STORAGE,
1559
+ category: error.ErrorCategory.THIRD_PARTY,
1560
+ details: { namespace, id }
1561
+ },
1562
+ error$1
1563
+ );
1564
+ }
1565
+ try {
1184
1566
  const updatePayload = { id };
1185
1567
  if (update.vector) {
1186
1568
  updatePayload.vector = update.vector;
@@ -1196,8 +1578,16 @@ var UpstashVector = class extends vector.MastraVector {
1196
1578
  await this.client.upsert(points, {
1197
1579
  namespace
1198
1580
  });
1199
- } catch (error) {
1200
- throw new Error(`Failed to update vector by id: ${id} for index name: ${namespace}: ${error.message}`);
1581
+ } catch (error$1) {
1582
+ throw new error.MastraError(
1583
+ {
1584
+ id: "STORAGE_UPSTASH_VECTOR_UPDATE_VECTOR_FAILED",
1585
+ domain: error.ErrorDomain.STORAGE,
1586
+ category: error.ErrorCategory.THIRD_PARTY,
1587
+ details: { namespace, id }
1588
+ },
1589
+ error$1
1590
+ );
1201
1591
  }
1202
1592
  }
1203
1593
  /**
@@ -1212,8 +1602,17 @@ var UpstashVector = class extends vector.MastraVector {
1212
1602
  await this.client.delete(id, {
1213
1603
  namespace
1214
1604
  });
1215
- } catch (error) {
1216
- this.logger.error(`Failed to delete vector by id: ${id} for namespace: ${namespace}:`, error);
1605
+ } catch (error$1) {
1606
+ const mastraError = new error.MastraError(
1607
+ {
1608
+ id: "STORAGE_UPSTASH_VECTOR_DELETE_VECTOR_FAILED",
1609
+ domain: error.ErrorDomain.STORAGE,
1610
+ category: error.ErrorCategory.THIRD_PARTY,
1611
+ details: { namespace, id }
1612
+ },
1613
+ error$1
1614
+ );
1615
+ this.logger?.error(mastraError.toString());
1217
1616
  }
1218
1617
  }
1219
1618
  };