@mastra/cloudflare 0.10.5-alpha.0 → 0.10.5-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.
Files changed (3) hide show
  1. package/dist/index.cjs +316 -88
  2. package/dist/index.js +295 -67
  3. package/package.json +3 -3
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 Cloudflare = require('cloudflare');
6
7
 
@@ -63,10 +64,15 @@ var CloudflareStore = class extends storage.MastraStorage {
63
64
  });
64
65
  this.logger.info("Using Cloudflare KV REST API");
65
66
  }
66
- } catch (error) {
67
- const message = error instanceof Error ? error.message : String(error);
68
- this.logger.error("Failed to initialize CloudflareStore:", { message });
69
- throw error;
67
+ } catch (error$1) {
68
+ throw new error.MastraError(
69
+ {
70
+ id: "CLOUDFLARE_STORAGE_INIT_FAILED",
71
+ domain: error.ErrorDomain.STORAGE,
72
+ category: error.ErrorCategory.THIRD_PARTY
73
+ },
74
+ error$1
75
+ );
70
76
  }
71
77
  }
72
78
  getBinding(tableName) {
@@ -185,9 +191,18 @@ var CloudflareStore = class extends storage.MastraStorage {
185
191
  });
186
192
  return response.result;
187
193
  }
188
- } catch (error) {
189
- this.logger.error(`Failed to list keys for ${tableName}:`, error);
190
- throw new Error(`Failed to list keys: ${error.message}`);
194
+ } catch (error$1) {
195
+ throw new error.MastraError(
196
+ {
197
+ id: "CLOUDFLARE_STORAGE_LIST_NAMESPACE_KEYS_FAILED",
198
+ domain: error.ErrorDomain.STORAGE,
199
+ category: error.ErrorCategory.THIRD_PARTY,
200
+ details: {
201
+ tableName
202
+ }
203
+ },
204
+ error$1
205
+ );
191
206
  }
192
207
  }
193
208
  async createNamespaceById(title) {
@@ -630,9 +645,18 @@ var CloudflareStore = class extends storage.MastraStorage {
630
645
  createdAt: (/* @__PURE__ */ new Date()).toISOString()
631
646
  };
632
647
  await this.putKV({ tableName, key: schemaKey, value: schema, metadata });
633
- } catch (error) {
634
- this.logger.error(`Failed to store schema for ${tableName}:`, error);
635
- throw new Error(`Failed to store schema: ${error.message}`);
648
+ } catch (error$1) {
649
+ throw new error.MastraError(
650
+ {
651
+ id: "CLOUDFLARE_STORAGE_CREATE_TABLE_FAILED",
652
+ domain: error.ErrorDomain.STORAGE,
653
+ category: error.ErrorCategory.THIRD_PARTY,
654
+ details: {
655
+ tableName
656
+ }
657
+ },
658
+ error$1
659
+ );
636
660
  }
637
661
  }
638
662
  /**
@@ -644,9 +668,23 @@ var CloudflareStore = class extends storage.MastraStorage {
644
668
  async alterTable(_args) {
645
669
  }
646
670
  async clearTable({ tableName }) {
647
- const keys = await this.listKV(tableName);
648
- if (keys.length > 0) {
649
- await Promise.all(keys.map((keyObj) => this.deleteKV(tableName, keyObj.name)));
671
+ try {
672
+ const keys = await this.listKV(tableName);
673
+ if (keys.length > 0) {
674
+ await Promise.all(keys.map((keyObj) => this.deleteKV(tableName, keyObj.name)));
675
+ }
676
+ } catch (error$1) {
677
+ throw new error.MastraError(
678
+ {
679
+ id: "CLOUDFLARE_STORAGE_CLEAR_TABLE_FAILED",
680
+ domain: error.ErrorDomain.STORAGE,
681
+ category: error.ErrorCategory.THIRD_PARTY,
682
+ details: {
683
+ tableName
684
+ }
685
+ },
686
+ error$1
687
+ );
650
688
  }
651
689
  }
652
690
  async insert({
@@ -663,10 +701,18 @@ var CloudflareStore = class extends storage.MastraStorage {
663
701
  };
664
702
  await this.validateRecord(processedRecord, tableName);
665
703
  await this.putKV({ tableName, key, value: processedRecord });
666
- } catch (error) {
667
- const message = error instanceof Error ? error.message : String(error);
668
- this.logger.error(`Failed to insert record for ${tableName}:`, { message });
669
- throw error;
704
+ } catch (error$1) {
705
+ throw new error.MastraError(
706
+ {
707
+ id: "CLOUDFLARE_STORAGE_INSERT_FAILED",
708
+ domain: error.ErrorDomain.STORAGE,
709
+ category: error.ErrorCategory.THIRD_PARTY,
710
+ details: {
711
+ tableName
712
+ }
713
+ },
714
+ error$1
715
+ );
670
716
  }
671
717
  }
672
718
  async load({ tableName, keys }) {
@@ -681,10 +727,20 @@ var CloudflareStore = class extends storage.MastraStorage {
681
727
  metadata: this.ensureMetadata(data.metadata)
682
728
  };
683
729
  return processed;
684
- } catch (error) {
685
- this.logger.error(`Failed to load data for ${tableName}:`, {
686
- error: error instanceof Error ? error.message : String(error)
687
- });
730
+ } catch (error$1) {
731
+ const mastraError = new error.MastraError(
732
+ {
733
+ id: "CLOUDFLARE_STORAGE_LOAD_FAILED",
734
+ domain: error.ErrorDomain.STORAGE,
735
+ category: error.ErrorCategory.THIRD_PARTY,
736
+ details: {
737
+ tableName
738
+ }
739
+ },
740
+ error$1
741
+ );
742
+ this.logger?.trackException(mastraError);
743
+ this.logger?.error(mastraError.toString());
688
744
  return null;
689
745
  }
690
746
  }
@@ -698,10 +754,20 @@ var CloudflareStore = class extends storage.MastraStorage {
698
754
  updatedAt: this.ensureDate(thread.updatedAt),
699
755
  metadata: this.ensureMetadata(thread.metadata)
700
756
  };
701
- } catch (error) {
702
- this.logger.error(`Error processing thread ${threadId}:`, {
703
- error: error instanceof Error ? error.message : String(error)
704
- });
757
+ } catch (error$1) {
758
+ const mastraError = new error.MastraError(
759
+ {
760
+ id: "CLOUDFLARE_STORAGE_GET_THREAD_BY_ID_FAILED",
761
+ domain: error.ErrorDomain.STORAGE,
762
+ category: error.ErrorCategory.THIRD_PARTY,
763
+ details: {
764
+ threadId
765
+ }
766
+ },
767
+ error$1
768
+ );
769
+ this.logger?.trackException(mastraError);
770
+ this.logger?.error(mastraError.toString());
705
771
  return null;
706
772
  }
707
773
  }
@@ -721,17 +787,39 @@ var CloudflareStore = class extends storage.MastraStorage {
721
787
  updatedAt: this.ensureDate(thread.updatedAt),
722
788
  metadata: this.ensureMetadata(thread.metadata)
723
789
  };
724
- } catch (error) {
725
- const message = error instanceof Error ? error.message : String(error);
726
- this.logger.error(`Error processing thread from key ${keyObj.name}:`, { message });
790
+ } catch (error$1) {
791
+ const mastraError = new error.MastraError(
792
+ {
793
+ id: "CLOUDFLARE_STORAGE_GET_THREADS_BY_RESOURCE_ID_FAILED",
794
+ domain: error.ErrorDomain.STORAGE,
795
+ category: error.ErrorCategory.THIRD_PARTY,
796
+ details: {
797
+ resourceId
798
+ }
799
+ },
800
+ error$1
801
+ );
802
+ this.logger?.trackException(mastraError);
803
+ this.logger?.error(mastraError.toString());
727
804
  return null;
728
805
  }
729
806
  })
730
807
  );
731
808
  return threads.filter((thread) => thread !== null);
732
- } catch (error) {
733
- const message = error instanceof Error ? error.message : String(error);
734
- this.logger.error(`Error getting threads for resourceId ${resourceId}:`, { message });
809
+ } catch (error$1) {
810
+ const mastraError = new error.MastraError(
811
+ {
812
+ id: "CLOUDFLARE_STORAGE_GET_THREADS_BY_RESOURCE_ID_FAILED",
813
+ domain: error.ErrorDomain.STORAGE,
814
+ category: error.ErrorCategory.THIRD_PARTY,
815
+ details: {
816
+ resourceId
817
+ }
818
+ },
819
+ error$1
820
+ );
821
+ this.logger?.trackException(mastraError);
822
+ this.logger?.error(mastraError.toString());
735
823
  return [];
736
824
  }
737
825
  }
@@ -739,10 +827,18 @@ var CloudflareStore = class extends storage.MastraStorage {
739
827
  try {
740
828
  await this.insert({ tableName: storage.TABLE_THREADS, record: thread });
741
829
  return thread;
742
- } catch (error) {
743
- const message = error instanceof Error ? error.message : String(error);
744
- this.logger.error("Error saving thread:", { message });
745
- throw error;
830
+ } catch (error$1) {
831
+ throw new error.MastraError(
832
+ {
833
+ id: "CLOUDFLARE_STORAGE_SAVE_THREAD_FAILED",
834
+ domain: error.ErrorDomain.STORAGE,
835
+ category: error.ErrorCategory.THIRD_PARTY,
836
+ details: {
837
+ threadId: thread.id
838
+ }
839
+ },
840
+ error$1
841
+ );
746
842
  }
747
843
  }
748
844
  async updateThread({
@@ -766,10 +862,19 @@ var CloudflareStore = class extends storage.MastraStorage {
766
862
  };
767
863
  await this.insert({ tableName: storage.TABLE_THREADS, record: updatedThread });
768
864
  return updatedThread;
769
- } catch (error) {
770
- const message = error instanceof Error ? error.message : String(error);
771
- this.logger.error(`Error updating thread ${id}:`, { message });
772
- throw error;
865
+ } catch (error$1) {
866
+ throw new error.MastraError(
867
+ {
868
+ id: "CLOUDFLARE_STORAGE_UPDATE_THREAD_FAILED",
869
+ domain: error.ErrorDomain.STORAGE,
870
+ category: error.ErrorCategory.THIRD_PARTY,
871
+ details: {
872
+ threadId: id,
873
+ title
874
+ }
875
+ },
876
+ error$1
877
+ );
773
878
  }
774
879
  }
775
880
  async deleteThread({ threadId }) {
@@ -788,10 +893,18 @@ var CloudflareStore = class extends storage.MastraStorage {
788
893
  // Delete thread
789
894
  this.deleteKV(storage.TABLE_THREADS, this.getKey(storage.TABLE_THREADS, { id: threadId }))
790
895
  ]);
791
- } catch (error) {
792
- const message = error instanceof Error ? error.message : String(error);
793
- this.logger.error(`Error deleting thread ${threadId}:`, { message });
794
- throw error;
896
+ } catch (error$1) {
897
+ throw new error.MastraError(
898
+ {
899
+ id: "CLOUDFLARE_STORAGE_DELETE_THREAD_FAILED",
900
+ domain: error.ErrorDomain.STORAGE,
901
+ category: error.ErrorCategory.THIRD_PARTY,
902
+ details: {
903
+ threadId
904
+ }
905
+ },
906
+ error$1
907
+ );
795
908
  }
796
909
  }
797
910
  getMessageKey(threadId, messageId) {
@@ -863,10 +976,18 @@ var CloudflareStore = class extends storage.MastraStorage {
863
976
  const orderKey = this.getThreadMessagesKey(threadId);
864
977
  const entries = await this.updateSorting(threadMessages);
865
978
  await this.updateSortedMessages(orderKey, entries);
866
- } catch (error) {
867
- const errorMessage = error instanceof Error ? error.message : String(error);
868
- this.logger.error(`Error processing messages for thread ${threadId}: ${errorMessage}`);
869
- throw error;
979
+ } catch (error$1) {
980
+ throw new error.MastraError(
981
+ {
982
+ id: "CLOUDFLARE_STORAGE_SAVE_MESSAGES_FAILED",
983
+ domain: error.ErrorDomain.STORAGE,
984
+ category: error.ErrorCategory.THIRD_PARTY,
985
+ details: {
986
+ threadId
987
+ }
988
+ },
989
+ error$1
990
+ );
870
991
  }
871
992
  })
872
993
  );
@@ -876,10 +997,15 @@ var CloudflareStore = class extends storage.MastraStorage {
876
997
  const list = new agent.MessageList().add(prepared, "memory");
877
998
  if (format === `v2`) return list.get.all.v2();
878
999
  return list.get.all.v1();
879
- } catch (error) {
880
- const errorMessage = error instanceof Error ? error.message : String(error);
881
- this.logger.error(`Error saving messages: ${errorMessage}`);
882
- throw error;
1000
+ } catch (error$1) {
1001
+ throw new error.MastraError(
1002
+ {
1003
+ id: "CLOUDFLARE_STORAGE_SAVE_MESSAGES_FAILED",
1004
+ domain: error.ErrorDomain.STORAGE,
1005
+ category: error.ErrorCategory.THIRD_PARTY
1006
+ },
1007
+ error$1
1008
+ );
883
1009
  }
884
1010
  }
885
1011
  async getMessages({
@@ -889,12 +1015,7 @@ var CloudflareStore = class extends storage.MastraStorage {
889
1015
  format
890
1016
  }) {
891
1017
  if (!threadId) throw new Error("threadId is required");
892
- let limit = 40;
893
- if (typeof selectBy?.last === "number") {
894
- limit = Math.max(0, selectBy.last);
895
- } else if (selectBy?.last === false) {
896
- limit = 0;
897
- }
1018
+ const limit = this.resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
898
1019
  const messageIds = /* @__PURE__ */ new Set();
899
1020
  if (limit === 0 && !selectBy?.include?.length) return [];
900
1021
  try {
@@ -914,9 +1035,21 @@ var CloudflareStore = class extends storage.MastraStorage {
914
1035
  if (indexA !== void 0 && indexB !== void 0) return orderMap.get(a.id) - orderMap.get(b.id);
915
1036
  return new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime();
916
1037
  });
917
- } catch (error) {
918
- const errorMessage = error instanceof Error ? error.message : String(error);
919
- this.logger.warn(`Error sorting messages, falling back to creation time: ${errorMessage}`);
1038
+ } catch (error$1) {
1039
+ const mastraError = new error.MastraError(
1040
+ {
1041
+ id: "CLOUDFLARE_STORAGE_SORT_MESSAGES_FAILED",
1042
+ domain: error.ErrorDomain.STORAGE,
1043
+ category: error.ErrorCategory.THIRD_PARTY,
1044
+ text: `Error sorting messages for thread ${threadId} falling back to creation time`,
1045
+ details: {
1046
+ threadId
1047
+ }
1048
+ },
1049
+ error$1
1050
+ );
1051
+ this.logger?.trackException(mastraError);
1052
+ this.logger?.error(mastraError.toString());
920
1053
  messages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
921
1054
  }
922
1055
  const prepared = messages.map(({ _index, ...message }) => ({
@@ -927,9 +1060,21 @@ var CloudflareStore = class extends storage.MastraStorage {
927
1060
  const list = new agent.MessageList({ threadId, resourceId }).add(prepared, "memory");
928
1061
  if (format === `v1`) return list.get.all.v1();
929
1062
  return list.get.all.v2();
930
- } catch (error) {
931
- const errorMessage = error instanceof Error ? error.message : String(error);
932
- this.logger.error(`Error retrieving messages for thread ${threadId}: ${errorMessage}`);
1063
+ } catch (error$1) {
1064
+ const mastraError = new error.MastraError(
1065
+ {
1066
+ id: "CLOUDFLARE_STORAGE_GET_MESSAGES_FAILED",
1067
+ domain: error.ErrorDomain.STORAGE,
1068
+ category: error.ErrorCategory.THIRD_PARTY,
1069
+ text: `Error retrieving messages for thread ${threadId}`,
1070
+ details: {
1071
+ threadId
1072
+ }
1073
+ },
1074
+ error$1
1075
+ );
1076
+ this.logger?.trackException(mastraError);
1077
+ this.logger?.error(mastraError.toString());
933
1078
  return [];
934
1079
  }
935
1080
  }
@@ -986,10 +1131,21 @@ var CloudflareStore = class extends storage.MastraStorage {
986
1131
  updatedAt: /* @__PURE__ */ new Date()
987
1132
  }
988
1133
  });
989
- } catch (error) {
990
- const message = error instanceof Error ? error.message : String(error);
991
- this.logger.error("Error persisting workflow snapshot:", { message });
992
- throw error;
1134
+ } catch (error$1) {
1135
+ throw new error.MastraError(
1136
+ {
1137
+ id: "CLOUDFLARE_STORAGE_PERSIST_WORKFLOW_SNAPSHOT_FAILED",
1138
+ domain: error.ErrorDomain.STORAGE,
1139
+ category: error.ErrorCategory.THIRD_PARTY,
1140
+ text: `Error persisting workflow snapshot for namespace ${params.namespace}, workflow ${params.workflowName}, run ${params.runId}`,
1141
+ details: {
1142
+ namespace: params.namespace,
1143
+ workflowName: params.workflowName,
1144
+ runId: params.runId
1145
+ }
1146
+ },
1147
+ error$1
1148
+ );
993
1149
  }
994
1150
  }
995
1151
  async loadWorkflowSnapshot(params) {
@@ -1002,10 +1158,23 @@ var CloudflareStore = class extends storage.MastraStorage {
1002
1158
  const state = this.normalizeWorkflowState(data.snapshot || data);
1003
1159
  this.validateWorkflowState(state);
1004
1160
  return state;
1005
- } catch (error) {
1006
- this.logger.error("Error loading workflow snapshot:", {
1007
- error: error instanceof Error ? error.message : String(error)
1008
- });
1161
+ } catch (error$1) {
1162
+ const mastraError = new error.MastraError(
1163
+ {
1164
+ id: "CLOUDFLARE_STORAGE_LOAD_WORKFLOW_SNAPSHOT_FAILED",
1165
+ domain: error.ErrorDomain.STORAGE,
1166
+ category: error.ErrorCategory.THIRD_PARTY,
1167
+ text: `Error loading workflow snapshot for namespace ${params.namespace}, workflow ${params.workflowName}, run ${params.runId}`,
1168
+ details: {
1169
+ namespace: params.namespace,
1170
+ workflowName: params.workflowName,
1171
+ runId: params.runId
1172
+ }
1173
+ },
1174
+ error$1
1175
+ );
1176
+ this.logger?.trackException(mastraError);
1177
+ this.logger?.error(mastraError.toString());
1009
1178
  return null;
1010
1179
  }
1011
1180
  }
@@ -1024,10 +1193,19 @@ var CloudflareStore = class extends storage.MastraStorage {
1024
1193
  await this.putKV({ tableName: input.tableName, key, value: processedRecord });
1025
1194
  })
1026
1195
  );
1027
- } catch (error) {
1028
- const message = error instanceof Error ? error.message : String(error);
1029
- this.logger.error("Error in batch insert:", { message });
1030
- throw error;
1196
+ } catch (error$1) {
1197
+ throw new error.MastraError(
1198
+ {
1199
+ id: "CLOUDFLARE_STORAGE_BATCH_INSERT_FAILED",
1200
+ domain: error.ErrorDomain.STORAGE,
1201
+ category: error.ErrorCategory.THIRD_PARTY,
1202
+ text: `Error in batch insert for table ${input.tableName}`,
1203
+ details: {
1204
+ tableName: input.tableName
1205
+ }
1206
+ },
1207
+ error$1
1208
+ );
1031
1209
  }
1032
1210
  }
1033
1211
  async getTraces({
@@ -1106,9 +1284,18 @@ var CloudflareStore = class extends storage.MastraStorage {
1106
1284
  other: this.parseJSON(record.other) || {},
1107
1285
  createdAt: record.createdAt
1108
1286
  }));
1109
- } catch (error) {
1110
- const message = error instanceof Error ? error.message : String(error);
1111
- this.logger.error("Failed to get traces:", { message });
1287
+ } catch (error$1) {
1288
+ const mastraError = new error.MastraError(
1289
+ {
1290
+ id: "CLOUDFLARE_STORAGE_GET_TRACES_FAILED",
1291
+ domain: error.ErrorDomain.STORAGE,
1292
+ category: error.ErrorCategory.THIRD_PARTY,
1293
+ text: `Failed to get traces`
1294
+ },
1295
+ error$1
1296
+ );
1297
+ this.logger?.trackException(mastraError);
1298
+ this.logger?.error(mastraError.toString());
1112
1299
  return [];
1113
1300
  }
1114
1301
  }
@@ -1123,7 +1310,12 @@ var CloudflareStore = class extends storage.MastraStorage {
1123
1310
  return value;
1124
1311
  }
1125
1312
  getEvalsByAgentName(_agentName, _type) {
1126
- throw new Error("Method not implemented.");
1313
+ throw new error.MastraError({
1314
+ id: "CLOUDFLARE_STORAGE_GET_EVALS_BY_AGENT_NAME_FAILED",
1315
+ domain: error.ErrorDomain.STORAGE,
1316
+ category: error.ErrorCategory.THIRD_PARTY,
1317
+ text: `Failed to get evals by agent name`
1318
+ });
1127
1319
  }
1128
1320
  parseWorkflowRun(row) {
1129
1321
  let parsedSnapshot = row.snapshot;
@@ -1205,9 +1397,17 @@ var CloudflareStore = class extends storage.MastraStorage {
1205
1397
  runs: pagedRuns,
1206
1398
  total: runs.length
1207
1399
  };
1208
- } catch (error) {
1209
- const message = error instanceof Error ? error.message : String(error);
1210
- this.logger.error("Error in getWorkflowRuns:", { message });
1400
+ } catch (error$1) {
1401
+ const mastraError = new error.MastraError(
1402
+ {
1403
+ id: "CLOUDFLARE_STORAGE_GET_WORKFLOW_RUNS_FAILED",
1404
+ domain: error.ErrorDomain.STORAGE,
1405
+ category: error.ErrorCategory.THIRD_PARTY
1406
+ },
1407
+ error$1
1408
+ );
1409
+ this.logger?.trackException(mastraError);
1410
+ this.logger?.error(mastraError.toString());
1211
1411
  return { runs: [], total: 0 };
1212
1412
  }
1213
1413
  }
@@ -1229,20 +1429,48 @@ var CloudflareStore = class extends storage.MastraStorage {
1229
1429
  const state = this.normalizeWorkflowState(data.snapshot || data);
1230
1430
  this.validateWorkflowState(state);
1231
1431
  return this.parseWorkflowRun({ ...data, snapshot: state });
1232
- } catch (error) {
1233
- const message = error instanceof Error ? error.message : String(error);
1234
- this.logger.error("Error in getWorkflowRunById:", { message });
1432
+ } catch (error$1) {
1433
+ const mastraError = new error.MastraError(
1434
+ {
1435
+ id: "CLOUDFLARE_STORAGE_GET_WORKFLOW_RUN_BY_ID_FAILED",
1436
+ domain: error.ErrorDomain.STORAGE,
1437
+ category: error.ErrorCategory.THIRD_PARTY,
1438
+ details: {
1439
+ namespace,
1440
+ workflowName,
1441
+ runId
1442
+ }
1443
+ },
1444
+ error$1
1445
+ );
1446
+ this.logger?.trackException(mastraError);
1447
+ this.logger?.error(mastraError.toString());
1235
1448
  return null;
1236
1449
  }
1237
1450
  }
1238
1451
  async getTracesPaginated(_args) {
1239
- throw new Error("Method not implemented.");
1452
+ throw new error.MastraError({
1453
+ id: "CLOUDFLARE_STORAGE_GET_TRACES_PAGINATED_FAILED",
1454
+ domain: error.ErrorDomain.STORAGE,
1455
+ category: error.ErrorCategory.THIRD_PARTY,
1456
+ text: "Method not implemented."
1457
+ });
1240
1458
  }
1241
1459
  async getThreadsByResourceIdPaginated(_args) {
1242
- throw new Error("Method not implemented.");
1460
+ throw new error.MastraError({
1461
+ id: "CLOUDFLARE_STORAGE_GET_THREADS_BY_RESOURCE_ID_PAGINATED_FAILED",
1462
+ domain: error.ErrorDomain.STORAGE,
1463
+ category: error.ErrorCategory.THIRD_PARTY,
1464
+ text: "Method not implemented."
1465
+ });
1243
1466
  }
1244
1467
  async getMessagesPaginated(_args) {
1245
- throw new Error("Method not implemented.");
1468
+ throw new error.MastraError({
1469
+ id: "CLOUDFLARE_STORAGE_GET_MESSAGES_PAGINATED_FAILED",
1470
+ domain: error.ErrorDomain.STORAGE,
1471
+ category: error.ErrorCategory.THIRD_PARTY,
1472
+ text: "Method not implemented."
1473
+ });
1246
1474
  }
1247
1475
  async close() {
1248
1476
  }
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { MessageList } from '@mastra/core/agent';
2
+ import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
2
3
  import { MastraStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_WORKFLOW_SNAPSHOT, TABLE_EVALS, TABLE_TRACES } from '@mastra/core/storage';
3
4
  import Cloudflare from 'cloudflare';
4
5
 
@@ -58,9 +59,14 @@ var CloudflareStore = class extends MastraStorage {
58
59
  this.logger.info("Using Cloudflare KV REST API");
59
60
  }
60
61
  } catch (error) {
61
- const message = error instanceof Error ? error.message : String(error);
62
- this.logger.error("Failed to initialize CloudflareStore:", { message });
63
- throw error;
62
+ throw new MastraError(
63
+ {
64
+ id: "CLOUDFLARE_STORAGE_INIT_FAILED",
65
+ domain: ErrorDomain.STORAGE,
66
+ category: ErrorCategory.THIRD_PARTY
67
+ },
68
+ error
69
+ );
64
70
  }
65
71
  }
66
72
  getBinding(tableName) {
@@ -180,8 +186,17 @@ var CloudflareStore = class extends MastraStorage {
180
186
  return response.result;
181
187
  }
182
188
  } catch (error) {
183
- this.logger.error(`Failed to list keys for ${tableName}:`, error);
184
- throw new Error(`Failed to list keys: ${error.message}`);
189
+ throw new MastraError(
190
+ {
191
+ id: "CLOUDFLARE_STORAGE_LIST_NAMESPACE_KEYS_FAILED",
192
+ domain: ErrorDomain.STORAGE,
193
+ category: ErrorCategory.THIRD_PARTY,
194
+ details: {
195
+ tableName
196
+ }
197
+ },
198
+ error
199
+ );
185
200
  }
186
201
  }
187
202
  async createNamespaceById(title) {
@@ -625,8 +640,17 @@ var CloudflareStore = class extends MastraStorage {
625
640
  };
626
641
  await this.putKV({ tableName, key: schemaKey, value: schema, metadata });
627
642
  } catch (error) {
628
- this.logger.error(`Failed to store schema for ${tableName}:`, error);
629
- throw new Error(`Failed to store schema: ${error.message}`);
643
+ throw new MastraError(
644
+ {
645
+ id: "CLOUDFLARE_STORAGE_CREATE_TABLE_FAILED",
646
+ domain: ErrorDomain.STORAGE,
647
+ category: ErrorCategory.THIRD_PARTY,
648
+ details: {
649
+ tableName
650
+ }
651
+ },
652
+ error
653
+ );
630
654
  }
631
655
  }
632
656
  /**
@@ -638,9 +662,23 @@ var CloudflareStore = class extends MastraStorage {
638
662
  async alterTable(_args) {
639
663
  }
640
664
  async clearTable({ tableName }) {
641
- const keys = await this.listKV(tableName);
642
- if (keys.length > 0) {
643
- await Promise.all(keys.map((keyObj) => this.deleteKV(tableName, keyObj.name)));
665
+ try {
666
+ const keys = await this.listKV(tableName);
667
+ if (keys.length > 0) {
668
+ await Promise.all(keys.map((keyObj) => this.deleteKV(tableName, keyObj.name)));
669
+ }
670
+ } catch (error) {
671
+ throw new MastraError(
672
+ {
673
+ id: "CLOUDFLARE_STORAGE_CLEAR_TABLE_FAILED",
674
+ domain: ErrorDomain.STORAGE,
675
+ category: ErrorCategory.THIRD_PARTY,
676
+ details: {
677
+ tableName
678
+ }
679
+ },
680
+ error
681
+ );
644
682
  }
645
683
  }
646
684
  async insert({
@@ -658,9 +696,17 @@ var CloudflareStore = class extends MastraStorage {
658
696
  await this.validateRecord(processedRecord, tableName);
659
697
  await this.putKV({ tableName, key, value: processedRecord });
660
698
  } catch (error) {
661
- const message = error instanceof Error ? error.message : String(error);
662
- this.logger.error(`Failed to insert record for ${tableName}:`, { message });
663
- throw error;
699
+ throw new MastraError(
700
+ {
701
+ id: "CLOUDFLARE_STORAGE_INSERT_FAILED",
702
+ domain: ErrorDomain.STORAGE,
703
+ category: ErrorCategory.THIRD_PARTY,
704
+ details: {
705
+ tableName
706
+ }
707
+ },
708
+ error
709
+ );
664
710
  }
665
711
  }
666
712
  async load({ tableName, keys }) {
@@ -676,9 +722,19 @@ var CloudflareStore = class extends MastraStorage {
676
722
  };
677
723
  return processed;
678
724
  } catch (error) {
679
- this.logger.error(`Failed to load data for ${tableName}:`, {
680
- error: error instanceof Error ? error.message : String(error)
681
- });
725
+ const mastraError = new MastraError(
726
+ {
727
+ id: "CLOUDFLARE_STORAGE_LOAD_FAILED",
728
+ domain: ErrorDomain.STORAGE,
729
+ category: ErrorCategory.THIRD_PARTY,
730
+ details: {
731
+ tableName
732
+ }
733
+ },
734
+ error
735
+ );
736
+ this.logger?.trackException(mastraError);
737
+ this.logger?.error(mastraError.toString());
682
738
  return null;
683
739
  }
684
740
  }
@@ -693,9 +749,19 @@ var CloudflareStore = class extends MastraStorage {
693
749
  metadata: this.ensureMetadata(thread.metadata)
694
750
  };
695
751
  } catch (error) {
696
- this.logger.error(`Error processing thread ${threadId}:`, {
697
- error: error instanceof Error ? error.message : String(error)
698
- });
752
+ const mastraError = new MastraError(
753
+ {
754
+ id: "CLOUDFLARE_STORAGE_GET_THREAD_BY_ID_FAILED",
755
+ domain: ErrorDomain.STORAGE,
756
+ category: ErrorCategory.THIRD_PARTY,
757
+ details: {
758
+ threadId
759
+ }
760
+ },
761
+ error
762
+ );
763
+ this.logger?.trackException(mastraError);
764
+ this.logger?.error(mastraError.toString());
699
765
  return null;
700
766
  }
701
767
  }
@@ -716,16 +782,38 @@ var CloudflareStore = class extends MastraStorage {
716
782
  metadata: this.ensureMetadata(thread.metadata)
717
783
  };
718
784
  } catch (error) {
719
- const message = error instanceof Error ? error.message : String(error);
720
- this.logger.error(`Error processing thread from key ${keyObj.name}:`, { message });
785
+ const mastraError = new MastraError(
786
+ {
787
+ id: "CLOUDFLARE_STORAGE_GET_THREADS_BY_RESOURCE_ID_FAILED",
788
+ domain: ErrorDomain.STORAGE,
789
+ category: ErrorCategory.THIRD_PARTY,
790
+ details: {
791
+ resourceId
792
+ }
793
+ },
794
+ error
795
+ );
796
+ this.logger?.trackException(mastraError);
797
+ this.logger?.error(mastraError.toString());
721
798
  return null;
722
799
  }
723
800
  })
724
801
  );
725
802
  return threads.filter((thread) => thread !== null);
726
803
  } catch (error) {
727
- const message = error instanceof Error ? error.message : String(error);
728
- this.logger.error(`Error getting threads for resourceId ${resourceId}:`, { message });
804
+ const mastraError = new MastraError(
805
+ {
806
+ id: "CLOUDFLARE_STORAGE_GET_THREADS_BY_RESOURCE_ID_FAILED",
807
+ domain: ErrorDomain.STORAGE,
808
+ category: ErrorCategory.THIRD_PARTY,
809
+ details: {
810
+ resourceId
811
+ }
812
+ },
813
+ error
814
+ );
815
+ this.logger?.trackException(mastraError);
816
+ this.logger?.error(mastraError.toString());
729
817
  return [];
730
818
  }
731
819
  }
@@ -734,9 +822,17 @@ var CloudflareStore = class extends MastraStorage {
734
822
  await this.insert({ tableName: TABLE_THREADS, record: thread });
735
823
  return thread;
736
824
  } catch (error) {
737
- const message = error instanceof Error ? error.message : String(error);
738
- this.logger.error("Error saving thread:", { message });
739
- throw error;
825
+ throw new MastraError(
826
+ {
827
+ id: "CLOUDFLARE_STORAGE_SAVE_THREAD_FAILED",
828
+ domain: ErrorDomain.STORAGE,
829
+ category: ErrorCategory.THIRD_PARTY,
830
+ details: {
831
+ threadId: thread.id
832
+ }
833
+ },
834
+ error
835
+ );
740
836
  }
741
837
  }
742
838
  async updateThread({
@@ -761,9 +857,18 @@ var CloudflareStore = class extends MastraStorage {
761
857
  await this.insert({ tableName: TABLE_THREADS, record: updatedThread });
762
858
  return updatedThread;
763
859
  } catch (error) {
764
- const message = error instanceof Error ? error.message : String(error);
765
- this.logger.error(`Error updating thread ${id}:`, { message });
766
- throw error;
860
+ throw new MastraError(
861
+ {
862
+ id: "CLOUDFLARE_STORAGE_UPDATE_THREAD_FAILED",
863
+ domain: ErrorDomain.STORAGE,
864
+ category: ErrorCategory.THIRD_PARTY,
865
+ details: {
866
+ threadId: id,
867
+ title
868
+ }
869
+ },
870
+ error
871
+ );
767
872
  }
768
873
  }
769
874
  async deleteThread({ threadId }) {
@@ -783,9 +888,17 @@ var CloudflareStore = class extends MastraStorage {
783
888
  this.deleteKV(TABLE_THREADS, this.getKey(TABLE_THREADS, { id: threadId }))
784
889
  ]);
785
890
  } catch (error) {
786
- const message = error instanceof Error ? error.message : String(error);
787
- this.logger.error(`Error deleting thread ${threadId}:`, { message });
788
- throw error;
891
+ throw new MastraError(
892
+ {
893
+ id: "CLOUDFLARE_STORAGE_DELETE_THREAD_FAILED",
894
+ domain: ErrorDomain.STORAGE,
895
+ category: ErrorCategory.THIRD_PARTY,
896
+ details: {
897
+ threadId
898
+ }
899
+ },
900
+ error
901
+ );
789
902
  }
790
903
  }
791
904
  getMessageKey(threadId, messageId) {
@@ -858,9 +971,17 @@ var CloudflareStore = class extends MastraStorage {
858
971
  const entries = await this.updateSorting(threadMessages);
859
972
  await this.updateSortedMessages(orderKey, entries);
860
973
  } catch (error) {
861
- const errorMessage = error instanceof Error ? error.message : String(error);
862
- this.logger.error(`Error processing messages for thread ${threadId}: ${errorMessage}`);
863
- throw error;
974
+ throw new MastraError(
975
+ {
976
+ id: "CLOUDFLARE_STORAGE_SAVE_MESSAGES_FAILED",
977
+ domain: ErrorDomain.STORAGE,
978
+ category: ErrorCategory.THIRD_PARTY,
979
+ details: {
980
+ threadId
981
+ }
982
+ },
983
+ error
984
+ );
864
985
  }
865
986
  })
866
987
  );
@@ -871,9 +992,14 @@ var CloudflareStore = class extends MastraStorage {
871
992
  if (format === `v2`) return list.get.all.v2();
872
993
  return list.get.all.v1();
873
994
  } catch (error) {
874
- const errorMessage = error instanceof Error ? error.message : String(error);
875
- this.logger.error(`Error saving messages: ${errorMessage}`);
876
- throw error;
995
+ throw new MastraError(
996
+ {
997
+ id: "CLOUDFLARE_STORAGE_SAVE_MESSAGES_FAILED",
998
+ domain: ErrorDomain.STORAGE,
999
+ category: ErrorCategory.THIRD_PARTY
1000
+ },
1001
+ error
1002
+ );
877
1003
  }
878
1004
  }
879
1005
  async getMessages({
@@ -883,12 +1009,7 @@ var CloudflareStore = class extends MastraStorage {
883
1009
  format
884
1010
  }) {
885
1011
  if (!threadId) throw new Error("threadId is required");
886
- let limit = 40;
887
- if (typeof selectBy?.last === "number") {
888
- limit = Math.max(0, selectBy.last);
889
- } else if (selectBy?.last === false) {
890
- limit = 0;
891
- }
1012
+ const limit = this.resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
892
1013
  const messageIds = /* @__PURE__ */ new Set();
893
1014
  if (limit === 0 && !selectBy?.include?.length) return [];
894
1015
  try {
@@ -909,8 +1030,20 @@ var CloudflareStore = class extends MastraStorage {
909
1030
  return new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime();
910
1031
  });
911
1032
  } catch (error) {
912
- const errorMessage = error instanceof Error ? error.message : String(error);
913
- this.logger.warn(`Error sorting messages, falling back to creation time: ${errorMessage}`);
1033
+ const mastraError = new MastraError(
1034
+ {
1035
+ id: "CLOUDFLARE_STORAGE_SORT_MESSAGES_FAILED",
1036
+ domain: ErrorDomain.STORAGE,
1037
+ category: ErrorCategory.THIRD_PARTY,
1038
+ text: `Error sorting messages for thread ${threadId} falling back to creation time`,
1039
+ details: {
1040
+ threadId
1041
+ }
1042
+ },
1043
+ error
1044
+ );
1045
+ this.logger?.trackException(mastraError);
1046
+ this.logger?.error(mastraError.toString());
914
1047
  messages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
915
1048
  }
916
1049
  const prepared = messages.map(({ _index, ...message }) => ({
@@ -922,8 +1055,20 @@ var CloudflareStore = class extends MastraStorage {
922
1055
  if (format === `v1`) return list.get.all.v1();
923
1056
  return list.get.all.v2();
924
1057
  } catch (error) {
925
- const errorMessage = error instanceof Error ? error.message : String(error);
926
- this.logger.error(`Error retrieving messages for thread ${threadId}: ${errorMessage}`);
1058
+ const mastraError = new MastraError(
1059
+ {
1060
+ id: "CLOUDFLARE_STORAGE_GET_MESSAGES_FAILED",
1061
+ domain: ErrorDomain.STORAGE,
1062
+ category: ErrorCategory.THIRD_PARTY,
1063
+ text: `Error retrieving messages for thread ${threadId}`,
1064
+ details: {
1065
+ threadId
1066
+ }
1067
+ },
1068
+ error
1069
+ );
1070
+ this.logger?.trackException(mastraError);
1071
+ this.logger?.error(mastraError.toString());
927
1072
  return [];
928
1073
  }
929
1074
  }
@@ -981,9 +1126,20 @@ var CloudflareStore = class extends MastraStorage {
981
1126
  }
982
1127
  });
983
1128
  } catch (error) {
984
- const message = error instanceof Error ? error.message : String(error);
985
- this.logger.error("Error persisting workflow snapshot:", { message });
986
- throw error;
1129
+ throw new MastraError(
1130
+ {
1131
+ id: "CLOUDFLARE_STORAGE_PERSIST_WORKFLOW_SNAPSHOT_FAILED",
1132
+ domain: ErrorDomain.STORAGE,
1133
+ category: ErrorCategory.THIRD_PARTY,
1134
+ text: `Error persisting workflow snapshot for namespace ${params.namespace}, workflow ${params.workflowName}, run ${params.runId}`,
1135
+ details: {
1136
+ namespace: params.namespace,
1137
+ workflowName: params.workflowName,
1138
+ runId: params.runId
1139
+ }
1140
+ },
1141
+ error
1142
+ );
987
1143
  }
988
1144
  }
989
1145
  async loadWorkflowSnapshot(params) {
@@ -997,9 +1153,22 @@ var CloudflareStore = class extends MastraStorage {
997
1153
  this.validateWorkflowState(state);
998
1154
  return state;
999
1155
  } catch (error) {
1000
- this.logger.error("Error loading workflow snapshot:", {
1001
- error: error instanceof Error ? error.message : String(error)
1002
- });
1156
+ const mastraError = new MastraError(
1157
+ {
1158
+ id: "CLOUDFLARE_STORAGE_LOAD_WORKFLOW_SNAPSHOT_FAILED",
1159
+ domain: ErrorDomain.STORAGE,
1160
+ category: ErrorCategory.THIRD_PARTY,
1161
+ text: `Error loading workflow snapshot for namespace ${params.namespace}, workflow ${params.workflowName}, run ${params.runId}`,
1162
+ details: {
1163
+ namespace: params.namespace,
1164
+ workflowName: params.workflowName,
1165
+ runId: params.runId
1166
+ }
1167
+ },
1168
+ error
1169
+ );
1170
+ this.logger?.trackException(mastraError);
1171
+ this.logger?.error(mastraError.toString());
1003
1172
  return null;
1004
1173
  }
1005
1174
  }
@@ -1019,9 +1188,18 @@ var CloudflareStore = class extends MastraStorage {
1019
1188
  })
1020
1189
  );
1021
1190
  } catch (error) {
1022
- const message = error instanceof Error ? error.message : String(error);
1023
- this.logger.error("Error in batch insert:", { message });
1024
- throw error;
1191
+ throw new MastraError(
1192
+ {
1193
+ id: "CLOUDFLARE_STORAGE_BATCH_INSERT_FAILED",
1194
+ domain: ErrorDomain.STORAGE,
1195
+ category: ErrorCategory.THIRD_PARTY,
1196
+ text: `Error in batch insert for table ${input.tableName}`,
1197
+ details: {
1198
+ tableName: input.tableName
1199
+ }
1200
+ },
1201
+ error
1202
+ );
1025
1203
  }
1026
1204
  }
1027
1205
  async getTraces({
@@ -1101,8 +1279,17 @@ var CloudflareStore = class extends MastraStorage {
1101
1279
  createdAt: record.createdAt
1102
1280
  }));
1103
1281
  } catch (error) {
1104
- const message = error instanceof Error ? error.message : String(error);
1105
- this.logger.error("Failed to get traces:", { message });
1282
+ const mastraError = new MastraError(
1283
+ {
1284
+ id: "CLOUDFLARE_STORAGE_GET_TRACES_FAILED",
1285
+ domain: ErrorDomain.STORAGE,
1286
+ category: ErrorCategory.THIRD_PARTY,
1287
+ text: `Failed to get traces`
1288
+ },
1289
+ error
1290
+ );
1291
+ this.logger?.trackException(mastraError);
1292
+ this.logger?.error(mastraError.toString());
1106
1293
  return [];
1107
1294
  }
1108
1295
  }
@@ -1117,7 +1304,12 @@ var CloudflareStore = class extends MastraStorage {
1117
1304
  return value;
1118
1305
  }
1119
1306
  getEvalsByAgentName(_agentName, _type) {
1120
- throw new Error("Method not implemented.");
1307
+ throw new MastraError({
1308
+ id: "CLOUDFLARE_STORAGE_GET_EVALS_BY_AGENT_NAME_FAILED",
1309
+ domain: ErrorDomain.STORAGE,
1310
+ category: ErrorCategory.THIRD_PARTY,
1311
+ text: `Failed to get evals by agent name`
1312
+ });
1121
1313
  }
1122
1314
  parseWorkflowRun(row) {
1123
1315
  let parsedSnapshot = row.snapshot;
@@ -1200,8 +1392,16 @@ var CloudflareStore = class extends MastraStorage {
1200
1392
  total: runs.length
1201
1393
  };
1202
1394
  } catch (error) {
1203
- const message = error instanceof Error ? error.message : String(error);
1204
- this.logger.error("Error in getWorkflowRuns:", { message });
1395
+ const mastraError = new MastraError(
1396
+ {
1397
+ id: "CLOUDFLARE_STORAGE_GET_WORKFLOW_RUNS_FAILED",
1398
+ domain: ErrorDomain.STORAGE,
1399
+ category: ErrorCategory.THIRD_PARTY
1400
+ },
1401
+ error
1402
+ );
1403
+ this.logger?.trackException(mastraError);
1404
+ this.logger?.error(mastraError.toString());
1205
1405
  return { runs: [], total: 0 };
1206
1406
  }
1207
1407
  }
@@ -1224,19 +1424,47 @@ var CloudflareStore = class extends MastraStorage {
1224
1424
  this.validateWorkflowState(state);
1225
1425
  return this.parseWorkflowRun({ ...data, snapshot: state });
1226
1426
  } catch (error) {
1227
- const message = error instanceof Error ? error.message : String(error);
1228
- this.logger.error("Error in getWorkflowRunById:", { message });
1427
+ const mastraError = new MastraError(
1428
+ {
1429
+ id: "CLOUDFLARE_STORAGE_GET_WORKFLOW_RUN_BY_ID_FAILED",
1430
+ domain: ErrorDomain.STORAGE,
1431
+ category: ErrorCategory.THIRD_PARTY,
1432
+ details: {
1433
+ namespace,
1434
+ workflowName,
1435
+ runId
1436
+ }
1437
+ },
1438
+ error
1439
+ );
1440
+ this.logger?.trackException(mastraError);
1441
+ this.logger?.error(mastraError.toString());
1229
1442
  return null;
1230
1443
  }
1231
1444
  }
1232
1445
  async getTracesPaginated(_args) {
1233
- throw new Error("Method not implemented.");
1446
+ throw new MastraError({
1447
+ id: "CLOUDFLARE_STORAGE_GET_TRACES_PAGINATED_FAILED",
1448
+ domain: ErrorDomain.STORAGE,
1449
+ category: ErrorCategory.THIRD_PARTY,
1450
+ text: "Method not implemented."
1451
+ });
1234
1452
  }
1235
1453
  async getThreadsByResourceIdPaginated(_args) {
1236
- throw new Error("Method not implemented.");
1454
+ throw new MastraError({
1455
+ id: "CLOUDFLARE_STORAGE_GET_THREADS_BY_RESOURCE_ID_PAGINATED_FAILED",
1456
+ domain: ErrorDomain.STORAGE,
1457
+ category: ErrorCategory.THIRD_PARTY,
1458
+ text: "Method not implemented."
1459
+ });
1237
1460
  }
1238
1461
  async getMessagesPaginated(_args) {
1239
- throw new Error("Method not implemented.");
1462
+ throw new MastraError({
1463
+ id: "CLOUDFLARE_STORAGE_GET_MESSAGES_PAGINATED_FAILED",
1464
+ domain: ErrorDomain.STORAGE,
1465
+ category: ErrorCategory.THIRD_PARTY,
1466
+ text: "Method not implemented."
1467
+ });
1240
1468
  }
1241
1469
  async close() {
1242
1470
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/cloudflare",
3
- "version": "0.10.5-alpha.0",
3
+ "version": "0.10.5-alpha.1",
4
4
  "description": "Cloudflare provider for Mastra - includes db storage capabilities",
5
5
  "type": "module",
6
6
  "files": [
@@ -36,8 +36,8 @@
36
36
  "typescript": "^5.8.3",
37
37
  "vitest": "^3.2.3",
38
38
  "@internal/lint": "0.0.13",
39
- "@internal/storage-test-utils": "0.0.9",
40
- "@mastra/core": "0.10.7-alpha.0"
39
+ "@mastra/core": "0.10.7-alpha.1",
40
+ "@internal/storage-test-utils": "0.0.9"
41
41
  },
42
42
  "peerDependencies": {
43
43
  "@mastra/core": ">=0.10.4-0 <0.11.0"