@mastra/cloudflare 0.0.0-trigger-playground-ui-package-20250506151043 → 0.0.0-tsconfig-compile-20250703214351
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/_tsup-dts-rollup.d.cts +71 -24
- package/dist/_tsup-dts-rollup.d.ts +71 -24
- package/dist/index.cjs +432 -118
- package/dist/index.js +411 -97
- package/package.json +17 -13
package/dist/index.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { MessageList } from '@mastra/core/agent';
|
|
2
|
+
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
1
3
|
import { MastraStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_WORKFLOW_SNAPSHOT, TABLE_EVALS, TABLE_TRACES } from '@mastra/core/storage';
|
|
2
4
|
import Cloudflare from 'cloudflare';
|
|
3
5
|
|
|
@@ -57,9 +59,14 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
57
59
|
this.logger.info("Using Cloudflare KV REST API");
|
|
58
60
|
}
|
|
59
61
|
} catch (error) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
throw new MastraError(
|
|
63
|
+
{
|
|
64
|
+
id: "CLOUDFLARE_STORAGE_INIT_FAILED",
|
|
65
|
+
domain: ErrorDomain.STORAGE,
|
|
66
|
+
category: ErrorCategory.THIRD_PARTY
|
|
67
|
+
},
|
|
68
|
+
error
|
|
69
|
+
);
|
|
63
70
|
}
|
|
64
71
|
}
|
|
65
72
|
getBinding(tableName) {
|
|
@@ -80,7 +87,25 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
80
87
|
}))
|
|
81
88
|
};
|
|
82
89
|
}
|
|
83
|
-
|
|
90
|
+
let allNamespaces = [];
|
|
91
|
+
let currentPage = 1;
|
|
92
|
+
const perPage = 50;
|
|
93
|
+
let morePagesExist = true;
|
|
94
|
+
while (morePagesExist) {
|
|
95
|
+
const response = await this.client.kv.namespaces.list({
|
|
96
|
+
account_id: this.accountId,
|
|
97
|
+
page: currentPage,
|
|
98
|
+
per_page: perPage
|
|
99
|
+
});
|
|
100
|
+
if (response.result) {
|
|
101
|
+
allNamespaces = allNamespaces.concat(response.result);
|
|
102
|
+
}
|
|
103
|
+
morePagesExist = response.result ? response.result.length === perPage : false;
|
|
104
|
+
if (morePagesExist) {
|
|
105
|
+
currentPage++;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return { result: allNamespaces };
|
|
84
109
|
}
|
|
85
110
|
async getNamespaceValue(tableName, key) {
|
|
86
111
|
try {
|
|
@@ -161,8 +186,17 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
161
186
|
return response.result;
|
|
162
187
|
}
|
|
163
188
|
} catch (error) {
|
|
164
|
-
|
|
165
|
-
|
|
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
|
+
);
|
|
166
200
|
}
|
|
167
201
|
}
|
|
168
202
|
async createNamespaceById(title) {
|
|
@@ -213,18 +247,57 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
213
247
|
async getNamespaceId(tableName) {
|
|
214
248
|
const prefix = this.namespacePrefix ? `${this.namespacePrefix}_` : "";
|
|
215
249
|
try {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
return await this.getOrCreateNamespaceId(`${prefix}mastra_workflows`);
|
|
220
|
-
} else {
|
|
221
|
-
return await this.getOrCreateNamespaceId(`${prefix}mastra_evals`);
|
|
250
|
+
const legacyNamespaceId = await this.checkLegacyNamespace(tableName, prefix);
|
|
251
|
+
if (legacyNamespaceId) {
|
|
252
|
+
return legacyNamespaceId;
|
|
222
253
|
}
|
|
254
|
+
return await this.getOrCreateNamespaceId(`${prefix}${tableName}`);
|
|
223
255
|
} catch (error) {
|
|
224
256
|
this.logger.error("Error fetching namespace ID:", error);
|
|
225
257
|
throw new Error(`Failed to fetch namespace ID for table ${tableName}: ${error.message}`);
|
|
226
258
|
}
|
|
227
259
|
}
|
|
260
|
+
LEGACY_NAMESPACE_MAP = {
|
|
261
|
+
[TABLE_MESSAGES]: TABLE_THREADS,
|
|
262
|
+
[TABLE_WORKFLOW_SNAPSHOT]: "mastra_workflows",
|
|
263
|
+
[TABLE_TRACES]: TABLE_EVALS
|
|
264
|
+
};
|
|
265
|
+
/**
|
|
266
|
+
* There were a few legacy mappings for tables such as
|
|
267
|
+
* - messages -> threads
|
|
268
|
+
* - workflow_snapshot -> mastra_workflows
|
|
269
|
+
* - traces -> evals
|
|
270
|
+
* This has been updated to use dedicated namespaces for each table.
|
|
271
|
+
* In the case of data for a table existing in the legacy namespace, warn the user to migrate to the new namespace.
|
|
272
|
+
*
|
|
273
|
+
* @param tableName The table name to check for legacy data
|
|
274
|
+
* @param prefix The namespace prefix
|
|
275
|
+
* @returns The legacy namespace ID if data exists; otherwise, null
|
|
276
|
+
*/
|
|
277
|
+
async checkLegacyNamespace(tableName, prefix) {
|
|
278
|
+
const legacyNamespaceBase = this.LEGACY_NAMESPACE_MAP[tableName];
|
|
279
|
+
if (legacyNamespaceBase) {
|
|
280
|
+
const legacyNamespace = `${prefix}${legacyNamespaceBase}`;
|
|
281
|
+
const keyPrefix = this.namespacePrefix ? `${this.namespacePrefix}:` : "";
|
|
282
|
+
const prefixKey = `${keyPrefix}${tableName}:`;
|
|
283
|
+
const legacyId = await this.getNamespaceIdByName(legacyNamespace);
|
|
284
|
+
if (legacyId) {
|
|
285
|
+
const response = await this.client.kv.namespaces.keys.list(legacyId, {
|
|
286
|
+
account_id: this.accountId,
|
|
287
|
+
prefix: prefixKey
|
|
288
|
+
});
|
|
289
|
+
const keys = response.result;
|
|
290
|
+
const hasTableData = keys.length > 0;
|
|
291
|
+
if (hasTableData) {
|
|
292
|
+
this.logger.warn(
|
|
293
|
+
`Using legacy namespace "${legacyNamespace}" for ${tableName}. Consider migrating to a dedicated namespace "${prefix}${tableName}".`
|
|
294
|
+
);
|
|
295
|
+
return legacyId;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
228
301
|
/**
|
|
229
302
|
* Helper to safely serialize data for KV storage
|
|
230
303
|
*/
|
|
@@ -550,15 +623,6 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
550
623
|
throw error;
|
|
551
624
|
}
|
|
552
625
|
}
|
|
553
|
-
ensureDate(date) {
|
|
554
|
-
if (!date) return void 0;
|
|
555
|
-
return date instanceof Date ? date : new Date(date);
|
|
556
|
-
}
|
|
557
|
-
serializeDate(date) {
|
|
558
|
-
if (!date) return void 0;
|
|
559
|
-
const dateObj = this.ensureDate(date);
|
|
560
|
-
return dateObj?.toISOString();
|
|
561
|
-
}
|
|
562
626
|
ensureMetadata(metadata) {
|
|
563
627
|
if (!metadata) return {};
|
|
564
628
|
return typeof metadata === "string" ? JSON.parse(metadata) : metadata;
|
|
@@ -576,14 +640,45 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
576
640
|
};
|
|
577
641
|
await this.putKV({ tableName, key: schemaKey, value: schema, metadata });
|
|
578
642
|
} catch (error) {
|
|
579
|
-
|
|
580
|
-
|
|
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
|
+
);
|
|
581
654
|
}
|
|
582
655
|
}
|
|
656
|
+
/**
|
|
657
|
+
* No-op: This backend is schemaless and does not require schema changes.
|
|
658
|
+
* @param tableName Name of the table
|
|
659
|
+
* @param schema Schema of the table
|
|
660
|
+
* @param ifNotExists Array of column names to add if they don't exist
|
|
661
|
+
*/
|
|
662
|
+
async alterTable(_args) {
|
|
663
|
+
}
|
|
583
664
|
async clearTable({ tableName }) {
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
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
|
+
);
|
|
587
682
|
}
|
|
588
683
|
}
|
|
589
684
|
async insert({
|
|
@@ -601,9 +696,17 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
601
696
|
await this.validateRecord(processedRecord, tableName);
|
|
602
697
|
await this.putKV({ tableName, key, value: processedRecord });
|
|
603
698
|
} catch (error) {
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
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
|
+
);
|
|
607
710
|
}
|
|
608
711
|
}
|
|
609
712
|
async load({ tableName, keys }) {
|
|
@@ -619,9 +722,19 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
619
722
|
};
|
|
620
723
|
return processed;
|
|
621
724
|
} catch (error) {
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
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());
|
|
625
738
|
return null;
|
|
626
739
|
}
|
|
627
740
|
}
|
|
@@ -636,9 +749,19 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
636
749
|
metadata: this.ensureMetadata(thread.metadata)
|
|
637
750
|
};
|
|
638
751
|
} catch (error) {
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
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());
|
|
642
765
|
return null;
|
|
643
766
|
}
|
|
644
767
|
}
|
|
@@ -659,16 +782,38 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
659
782
|
metadata: this.ensureMetadata(thread.metadata)
|
|
660
783
|
};
|
|
661
784
|
} catch (error) {
|
|
662
|
-
const
|
|
663
|
-
|
|
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());
|
|
664
798
|
return null;
|
|
665
799
|
}
|
|
666
800
|
})
|
|
667
801
|
);
|
|
668
802
|
return threads.filter((thread) => thread !== null);
|
|
669
803
|
} catch (error) {
|
|
670
|
-
const
|
|
671
|
-
|
|
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());
|
|
672
817
|
return [];
|
|
673
818
|
}
|
|
674
819
|
}
|
|
@@ -677,9 +822,17 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
677
822
|
await this.insert({ tableName: TABLE_THREADS, record: thread });
|
|
678
823
|
return thread;
|
|
679
824
|
} catch (error) {
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
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
|
+
);
|
|
683
836
|
}
|
|
684
837
|
}
|
|
685
838
|
async updateThread({
|
|
@@ -704,9 +857,18 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
704
857
|
await this.insert({ tableName: TABLE_THREADS, record: updatedThread });
|
|
705
858
|
return updatedThread;
|
|
706
859
|
} catch (error) {
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
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
|
+
);
|
|
710
872
|
}
|
|
711
873
|
}
|
|
712
874
|
async deleteThread({ threadId }) {
|
|
@@ -726,9 +888,17 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
726
888
|
this.deleteKV(TABLE_THREADS, this.getKey(TABLE_THREADS, { id: threadId }))
|
|
727
889
|
]);
|
|
728
890
|
} catch (error) {
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
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
|
+
);
|
|
732
902
|
}
|
|
733
903
|
}
|
|
734
904
|
getMessageKey(threadId, messageId) {
|
|
@@ -749,7 +919,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
749
919
|
throw error;
|
|
750
920
|
}
|
|
751
921
|
}
|
|
752
|
-
async saveMessages(
|
|
922
|
+
async saveMessages(args) {
|
|
923
|
+
const { messages, format = "v1" } = args;
|
|
753
924
|
if (!Array.isArray(messages) || messages.length === 0) return [];
|
|
754
925
|
try {
|
|
755
926
|
const validatedMessages = messages.map((message, index) => {
|
|
@@ -765,15 +936,17 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
765
936
|
return {
|
|
766
937
|
...message,
|
|
767
938
|
createdAt: this.ensureDate(message.createdAt),
|
|
768
|
-
type: message.type || "
|
|
939
|
+
type: message.type || "v2",
|
|
769
940
|
_index: index
|
|
770
941
|
};
|
|
771
|
-
});
|
|
942
|
+
}).filter((m) => !!m);
|
|
772
943
|
const messagesByThread = validatedMessages.reduce((acc, message) => {
|
|
773
|
-
if (!acc.has(message.threadId)) {
|
|
944
|
+
if (message.threadId && !acc.has(message.threadId)) {
|
|
774
945
|
acc.set(message.threadId, []);
|
|
775
946
|
}
|
|
776
|
-
|
|
947
|
+
if (message.threadId) {
|
|
948
|
+
acc.get(message.threadId).push(message);
|
|
949
|
+
}
|
|
777
950
|
return acc;
|
|
778
951
|
}, /* @__PURE__ */ new Map());
|
|
779
952
|
await Promise.all(
|
|
@@ -785,7 +958,7 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
785
958
|
}
|
|
786
959
|
await Promise.all(
|
|
787
960
|
threadMessages.map(async (message) => {
|
|
788
|
-
const key =
|
|
961
|
+
const key = this.getMessageKey(threadId, message.id);
|
|
789
962
|
const { _index, ...cleanMessage } = message;
|
|
790
963
|
const serializedMessage = {
|
|
791
964
|
...cleanMessage,
|
|
@@ -798,27 +971,45 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
798
971
|
const entries = await this.updateSorting(threadMessages);
|
|
799
972
|
await this.updateSortedMessages(orderKey, entries);
|
|
800
973
|
} catch (error) {
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
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
|
+
);
|
|
804
985
|
}
|
|
805
986
|
})
|
|
806
987
|
);
|
|
807
|
-
|
|
988
|
+
const prepared = validatedMessages.map(
|
|
989
|
+
({ _index, ...message }) => ({ ...message, type: message.type !== "v2" ? message.type : void 0 })
|
|
990
|
+
);
|
|
991
|
+
const list = new MessageList().add(prepared, "memory");
|
|
992
|
+
if (format === `v2`) return list.get.all.v2();
|
|
993
|
+
return list.get.all.v1();
|
|
808
994
|
} catch (error) {
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
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
|
+
);
|
|
812
1003
|
}
|
|
813
1004
|
}
|
|
814
|
-
async getMessages({
|
|
1005
|
+
async getMessages({
|
|
1006
|
+
threadId,
|
|
1007
|
+
resourceId,
|
|
1008
|
+
selectBy,
|
|
1009
|
+
format
|
|
1010
|
+
}) {
|
|
815
1011
|
if (!threadId) throw new Error("threadId is required");
|
|
816
|
-
|
|
817
|
-
if (typeof selectBy?.last === "number") {
|
|
818
|
-
limit = Math.max(0, selectBy.last);
|
|
819
|
-
} else if (selectBy?.last === false) {
|
|
820
|
-
limit = 0;
|
|
821
|
-
}
|
|
1012
|
+
const limit = this.resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
|
|
822
1013
|
const messageIds = /* @__PURE__ */ new Set();
|
|
823
1014
|
if (limit === 0 && !selectBy?.include?.length) return [];
|
|
824
1015
|
try {
|
|
@@ -839,17 +1030,45 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
839
1030
|
return new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime();
|
|
840
1031
|
});
|
|
841
1032
|
} catch (error) {
|
|
842
|
-
const
|
|
843
|
-
|
|
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());
|
|
844
1047
|
messages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
845
1048
|
}
|
|
846
|
-
|
|
1049
|
+
const prepared = messages.map(({ _index, ...message }) => ({
|
|
847
1050
|
...message,
|
|
1051
|
+
type: message.type === `v2` ? void 0 : message.type,
|
|
848
1052
|
createdAt: this.ensureDate(message.createdAt)
|
|
849
1053
|
}));
|
|
1054
|
+
const list = new MessageList({ threadId, resourceId }).add(prepared, "memory");
|
|
1055
|
+
if (format === `v1`) return list.get.all.v1();
|
|
1056
|
+
return list.get.all.v2();
|
|
850
1057
|
} catch (error) {
|
|
851
|
-
const
|
|
852
|
-
|
|
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());
|
|
853
1072
|
return [];
|
|
854
1073
|
}
|
|
855
1074
|
}
|
|
@@ -860,7 +1079,7 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
860
1079
|
}
|
|
861
1080
|
}
|
|
862
1081
|
validateWorkflowState(state) {
|
|
863
|
-
if (!state?.runId || !state?.value || !state?.context?.
|
|
1082
|
+
if (!state?.runId || !state?.value || !state?.context?.input || !state?.activePaths) {
|
|
864
1083
|
throw new Error("Invalid workflow state structure");
|
|
865
1084
|
}
|
|
866
1085
|
}
|
|
@@ -876,18 +1095,17 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
876
1095
|
return normalizedSteps;
|
|
877
1096
|
}
|
|
878
1097
|
normalizeWorkflowState(data) {
|
|
879
|
-
const steps = data.context?.stepResults || data.context?.steps || {};
|
|
880
1098
|
return {
|
|
881
1099
|
runId: data.runId,
|
|
882
1100
|
value: data.value,
|
|
883
|
-
context:
|
|
884
|
-
|
|
885
|
-
triggerData: data.context?.triggerData || {},
|
|
886
|
-
attempts: data.context?.attempts || {}
|
|
887
|
-
},
|
|
1101
|
+
context: data.context,
|
|
1102
|
+
serializedStepGraph: data.serializedStepGraph,
|
|
888
1103
|
suspendedPaths: data.suspendedPaths || {},
|
|
889
1104
|
activePaths: data.activePaths || [],
|
|
890
|
-
timestamp: data.timestamp || Date.now()
|
|
1105
|
+
timestamp: data.timestamp || Date.now(),
|
|
1106
|
+
status: data.status,
|
|
1107
|
+
result: data.result,
|
|
1108
|
+
error: data.error
|
|
891
1109
|
};
|
|
892
1110
|
}
|
|
893
1111
|
async persistWorkflowSnapshot(params) {
|
|
@@ -908,9 +1126,20 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
908
1126
|
}
|
|
909
1127
|
});
|
|
910
1128
|
} catch (error) {
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
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
|
+
);
|
|
914
1143
|
}
|
|
915
1144
|
}
|
|
916
1145
|
async loadWorkflowSnapshot(params) {
|
|
@@ -924,9 +1153,22 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
924
1153
|
this.validateWorkflowState(state);
|
|
925
1154
|
return state;
|
|
926
1155
|
} catch (error) {
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
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());
|
|
930
1172
|
return null;
|
|
931
1173
|
}
|
|
932
1174
|
}
|
|
@@ -946,9 +1188,18 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
946
1188
|
})
|
|
947
1189
|
);
|
|
948
1190
|
} catch (error) {
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
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
|
+
);
|
|
952
1203
|
}
|
|
953
1204
|
}
|
|
954
1205
|
async getTraces({
|
|
@@ -1028,8 +1279,17 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
1028
1279
|
createdAt: record.createdAt
|
|
1029
1280
|
}));
|
|
1030
1281
|
} catch (error) {
|
|
1031
|
-
const
|
|
1032
|
-
|
|
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());
|
|
1033
1293
|
return [];
|
|
1034
1294
|
}
|
|
1035
1295
|
}
|
|
@@ -1044,7 +1304,12 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
1044
1304
|
return value;
|
|
1045
1305
|
}
|
|
1046
1306
|
getEvalsByAgentName(_agentName, _type) {
|
|
1047
|
-
throw new
|
|
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
|
+
});
|
|
1048
1313
|
}
|
|
1049
1314
|
parseWorkflowRun(row) {
|
|
1050
1315
|
let parsedSnapshot = row.snapshot;
|
|
@@ -1127,8 +1392,16 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
1127
1392
|
total: runs.length
|
|
1128
1393
|
};
|
|
1129
1394
|
} catch (error) {
|
|
1130
|
-
const
|
|
1131
|
-
|
|
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());
|
|
1132
1405
|
return { runs: [], total: 0 };
|
|
1133
1406
|
}
|
|
1134
1407
|
}
|
|
@@ -1151,13 +1424,54 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
1151
1424
|
this.validateWorkflowState(state);
|
|
1152
1425
|
return this.parseWorkflowRun({ ...data, snapshot: state });
|
|
1153
1426
|
} catch (error) {
|
|
1154
|
-
const
|
|
1155
|
-
|
|
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());
|
|
1156
1442
|
return null;
|
|
1157
1443
|
}
|
|
1158
1444
|
}
|
|
1445
|
+
async getTracesPaginated(_args) {
|
|
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
|
+
});
|
|
1452
|
+
}
|
|
1453
|
+
async getThreadsByResourceIdPaginated(_args) {
|
|
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
|
+
});
|
|
1460
|
+
}
|
|
1461
|
+
async getMessagesPaginated(_args) {
|
|
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
|
+
});
|
|
1468
|
+
}
|
|
1159
1469
|
async close() {
|
|
1160
1470
|
}
|
|
1471
|
+
async updateMessages(_args) {
|
|
1472
|
+
this.logger.error("updateMessages is not yet implemented in CloudflareStore");
|
|
1473
|
+
throw new Error("Method not implemented");
|
|
1474
|
+
}
|
|
1161
1475
|
};
|
|
1162
1476
|
|
|
1163
1477
|
export { CloudflareStore };
|