@mastra/cloudflare 1.2.3 → 1.3.0

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 (62) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/dist/chunk-E4MARS3A.cjs +2135 -0
  3. package/dist/chunk-E4MARS3A.cjs.map +1 -0
  4. package/dist/chunk-NSFHQATX.js +2128 -0
  5. package/dist/chunk-NSFHQATX.js.map +1 -0
  6. package/dist/chunk-O57GFJSB.js +2265 -0
  7. package/dist/chunk-O57GFJSB.js.map +1 -0
  8. package/dist/chunk-ZBYNKKG6.cjs +2275 -0
  9. package/dist/chunk-ZBYNKKG6.cjs.map +1 -0
  10. package/dist/do/index.cjs +32 -0
  11. package/dist/do/index.cjs.map +1 -0
  12. package/dist/do/index.d.ts +91 -0
  13. package/dist/do/index.d.ts.map +1 -0
  14. package/dist/do/index.js +3 -0
  15. package/dist/do/index.js.map +1 -0
  16. package/dist/do/storage/db/index.d.ts +76 -0
  17. package/dist/do/storage/db/index.d.ts.map +1 -0
  18. package/dist/do/storage/domains/memory/index.d.ts +60 -0
  19. package/dist/do/storage/domains/memory/index.d.ts.map +1 -0
  20. package/dist/do/storage/domains/scores/index.d.ts +38 -0
  21. package/dist/do/storage/domains/scores/index.d.ts.map +1 -0
  22. package/dist/do/storage/domains/utils.d.ts +3 -0
  23. package/dist/do/storage/domains/utils.d.ts.map +1 -0
  24. package/dist/do/storage/domains/workflows/index.d.ts +46 -0
  25. package/dist/do/storage/domains/workflows/index.d.ts.map +1 -0
  26. package/dist/do/storage/sql-builder.d.ts +128 -0
  27. package/dist/do/storage/sql-builder.d.ts.map +1 -0
  28. package/dist/docs/SKILL.md +2 -2
  29. package/dist/docs/assets/SOURCE_MAP.json +56 -2
  30. package/dist/docs/references/reference-storage-cloudflare.md +79 -8
  31. package/dist/index.cjs +49 -2269
  32. package/dist/index.cjs.map +1 -1
  33. package/dist/index.d.ts +4 -1
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +2 -2262
  36. package/dist/index.js.map +1 -1
  37. package/dist/kv/index.cjs +28 -0
  38. package/dist/kv/index.cjs.map +1 -0
  39. package/dist/{storage → kv}/index.d.ts +11 -7
  40. package/dist/kv/index.d.ts.map +1 -0
  41. package/dist/kv/index.js +3 -0
  42. package/dist/kv/index.js.map +1 -0
  43. package/dist/kv/storage/db/index.d.ts.map +1 -0
  44. package/dist/kv/storage/domains/memory/index.d.ts.map +1 -0
  45. package/dist/kv/storage/domains/scores/index.d.ts.map +1 -0
  46. package/dist/kv/storage/domains/workflows/index.d.ts.map +1 -0
  47. package/dist/kv/storage/test-utils.d.ts.map +1 -0
  48. package/dist/kv/storage/types.d.ts.map +1 -0
  49. package/package.json +26 -5
  50. package/dist/storage/db/index.d.ts.map +0 -1
  51. package/dist/storage/domains/memory/index.d.ts.map +0 -1
  52. package/dist/storage/domains/scores/index.d.ts.map +0 -1
  53. package/dist/storage/domains/workflows/index.d.ts.map +0 -1
  54. package/dist/storage/index.d.ts.map +0 -1
  55. package/dist/storage/test-utils.d.ts.map +0 -1
  56. package/dist/storage/types.d.ts.map +0 -1
  57. /package/dist/{storage → kv/storage}/db/index.d.ts +0 -0
  58. /package/dist/{storage → kv/storage}/domains/memory/index.d.ts +0 -0
  59. /package/dist/{storage → kv/storage}/domains/scores/index.d.ts +0 -0
  60. /package/dist/{storage → kv/storage}/domains/workflows/index.d.ts +0 -0
  61. /package/dist/{storage → kv/storage}/test-utils.d.ts +0 -0
  62. /package/dist/{storage → kv/storage}/types.d.ts +0 -0
@@ -0,0 +1,2275 @@
1
+ 'use strict';
2
+
3
+ var error = require('@mastra/core/error');
4
+ var storage = require('@mastra/core/storage');
5
+ var Cloudflare = require('cloudflare');
6
+ var agent = require('@mastra/core/agent');
7
+ var base = require('@mastra/core/base');
8
+ var evals = require('@mastra/core/evals');
9
+
10
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
11
+
12
+ var Cloudflare__default = /*#__PURE__*/_interopDefault(Cloudflare);
13
+
14
+ // src/kv/index.ts
15
+ function resolveCloudflareConfig(config) {
16
+ if ("client" in config) {
17
+ return {
18
+ client: config.client,
19
+ accountId: config.accountId,
20
+ namespacePrefix: config.namespacePrefix
21
+ };
22
+ }
23
+ if ("bindings" in config) {
24
+ return {
25
+ bindings: config.bindings,
26
+ namespacePrefix: config.keyPrefix
27
+ };
28
+ }
29
+ return {
30
+ client: new Cloudflare__default.default({ apiToken: config.apiToken }),
31
+ accountId: config.accountId,
32
+ namespacePrefix: config.namespacePrefix
33
+ };
34
+ }
35
+ var CloudflareKVDB = class extends base.MastraBase {
36
+ bindings;
37
+ client;
38
+ accountId;
39
+ namespacePrefix;
40
+ constructor(config) {
41
+ super({
42
+ component: "STORAGE",
43
+ name: "CLOUDFLARE_KV_DB"
44
+ });
45
+ this.bindings = config.bindings;
46
+ this.namespacePrefix = config.namespacePrefix || "";
47
+ this.client = config.client;
48
+ this.accountId = config.accountId;
49
+ }
50
+ getBinding(tableName) {
51
+ if (!this.bindings) {
52
+ throw new Error(`Cannot use Workers API binding for ${tableName}: Store initialized with REST API configuration`);
53
+ }
54
+ const binding = this.bindings[tableName];
55
+ if (!binding) throw new Error(`No binding found for namespace ${tableName}`);
56
+ return binding;
57
+ }
58
+ getKey(tableName, record) {
59
+ const prefix = this.namespacePrefix ? `${this.namespacePrefix}:` : "";
60
+ switch (tableName) {
61
+ case storage.TABLE_THREADS:
62
+ if (!record.id) throw new Error("Thread ID is required");
63
+ return `${prefix}${tableName}:${record.id}`;
64
+ case storage.TABLE_MESSAGES:
65
+ if (!record.threadId || !record.id) throw new Error("Thread ID and Message ID are required");
66
+ return `${prefix}${tableName}:${record.threadId}:${record.id}`;
67
+ case storage.TABLE_WORKFLOW_SNAPSHOT:
68
+ if (!record.workflow_name || !record.run_id) {
69
+ throw new Error("Workflow name, and run ID are required");
70
+ }
71
+ let key = `${prefix}${tableName}:${record.workflow_name}:${record.run_id}`;
72
+ if (record.resourceId) {
73
+ key = `${key}:${record.resourceId}`;
74
+ }
75
+ return key;
76
+ case storage.TABLE_TRACES:
77
+ if (!record.id) throw new Error("Trace ID is required");
78
+ return `${prefix}${tableName}:${record.id}`;
79
+ case storage.TABLE_SCORERS:
80
+ if (!record.id) throw new Error("Score ID is required");
81
+ return `${prefix}${tableName}:${record.id}`;
82
+ default:
83
+ throw new Error(`Unsupported table: ${tableName}`);
84
+ }
85
+ }
86
+ getSchemaKey(tableName) {
87
+ const prefix = this.namespacePrefix ? `${this.namespacePrefix}:` : "";
88
+ return `${prefix}schema:${tableName}`;
89
+ }
90
+ /**
91
+ * Helper to safely parse data from KV storage
92
+ */
93
+ safeParse(text) {
94
+ if (!text) return null;
95
+ try {
96
+ const data = JSON.parse(text);
97
+ if (data && typeof data === "object" && "value" in data) {
98
+ if (typeof data.value === "string") {
99
+ try {
100
+ return JSON.parse(data.value);
101
+ } catch {
102
+ return data.value;
103
+ }
104
+ }
105
+ return null;
106
+ }
107
+ return data;
108
+ } catch (error) {
109
+ const message = error instanceof Error ? error.message : String(error);
110
+ this.logger.error("Failed to parse text:", { message, text });
111
+ return null;
112
+ }
113
+ }
114
+ async createNamespaceById(title) {
115
+ if (this.bindings) {
116
+ return {
117
+ id: title,
118
+ title,
119
+ supports_url_encoding: true
120
+ };
121
+ }
122
+ return await this.client.kv.namespaces.create({
123
+ account_id: this.accountId,
124
+ title
125
+ });
126
+ }
127
+ async createNamespace(namespaceName) {
128
+ try {
129
+ const response = await this.createNamespaceById(namespaceName);
130
+ return response.id;
131
+ } catch (error) {
132
+ if (error.message && error.message.includes("already exists")) {
133
+ const namespaces = await this.listNamespaces();
134
+ const namespace = namespaces.result.find((ns) => ns.title === namespaceName);
135
+ if (namespace) return namespace.id;
136
+ }
137
+ this.logger.error("Error creating namespace:", error);
138
+ throw new Error(`Failed to create namespace ${namespaceName}: ${error.message}`);
139
+ }
140
+ }
141
+ async listNamespaces() {
142
+ if (this.bindings) {
143
+ return {
144
+ result: Object.keys(this.bindings).map((name) => ({
145
+ id: name,
146
+ title: name,
147
+ supports_url_encoding: true
148
+ }))
149
+ };
150
+ }
151
+ let allNamespaces = [];
152
+ let currentPage = 1;
153
+ const perPage = 50;
154
+ let morePagesExist = true;
155
+ while (morePagesExist) {
156
+ const response = await this.client.kv.namespaces.list({
157
+ account_id: this.accountId,
158
+ page: currentPage,
159
+ per_page: perPage
160
+ });
161
+ if (response.result) {
162
+ allNamespaces = allNamespaces.concat(response.result);
163
+ }
164
+ morePagesExist = response.result ? response.result.length === perPage : false;
165
+ if (morePagesExist) {
166
+ currentPage++;
167
+ }
168
+ }
169
+ return { result: allNamespaces };
170
+ }
171
+ async getNamespaceIdByName(namespaceName) {
172
+ try {
173
+ const response = await this.listNamespaces();
174
+ const namespace = response.result.find((ns) => ns.title === namespaceName);
175
+ return namespace ? namespace.id : null;
176
+ } catch (error) {
177
+ this.logger.error(`Failed to get namespace ID for ${namespaceName}:`, error);
178
+ return null;
179
+ }
180
+ }
181
+ async getOrCreateNamespaceId(namespaceName) {
182
+ let namespaceId = await this.getNamespaceIdByName(namespaceName);
183
+ if (!namespaceId) {
184
+ namespaceId = await this.createNamespace(namespaceName);
185
+ }
186
+ return namespaceId;
187
+ }
188
+ async getNamespaceId(tableName) {
189
+ const prefix = this.namespacePrefix ? `${this.namespacePrefix}_` : "";
190
+ try {
191
+ return await this.getOrCreateNamespaceId(`${prefix}${tableName}`);
192
+ } catch (error) {
193
+ this.logger.error("Error fetching namespace ID:", error);
194
+ throw new Error(`Failed to fetch namespace ID for table ${tableName}: ${error.message}`);
195
+ }
196
+ }
197
+ async getNamespaceValue(tableName, key) {
198
+ try {
199
+ if (this.bindings) {
200
+ const binding = this.getBinding(tableName);
201
+ const result = await binding.getWithMetadata(key, "text");
202
+ if (!result) return null;
203
+ return JSON.stringify(result);
204
+ } else {
205
+ const namespaceId = await this.getNamespaceId(tableName);
206
+ const response = await this.client.kv.namespaces.values.get(namespaceId, key, {
207
+ account_id: this.accountId
208
+ });
209
+ return await response.text();
210
+ }
211
+ } catch (error) {
212
+ if (error.message && error.message.includes("key not found")) {
213
+ return null;
214
+ }
215
+ const message = error instanceof Error ? error.message : String(error);
216
+ this.logger.error(`Failed to get value for ${tableName} ${key}:`, { message });
217
+ throw error;
218
+ }
219
+ }
220
+ async getKV(tableName, key) {
221
+ try {
222
+ const text = await this.getNamespaceValue(tableName, key);
223
+ return this.safeParse(text);
224
+ } catch (error) {
225
+ this.logger.error(`Failed to get KV value for ${tableName}:${key}:`, error);
226
+ throw new Error(`Failed to get KV value: ${error.message}`);
227
+ }
228
+ }
229
+ async getTableSchema(tableName) {
230
+ try {
231
+ const schemaKey = this.getSchemaKey(tableName);
232
+ return await this.getKV(tableName, schemaKey);
233
+ } catch (error) {
234
+ const message = error instanceof Error ? error.message : String(error);
235
+ this.logger.error(`Failed to get schema for ${tableName}:`, { message });
236
+ return null;
237
+ }
238
+ }
239
+ validateColumnValue(value, column) {
240
+ if (value === void 0 || value === null) {
241
+ return column.nullable ?? false;
242
+ }
243
+ switch (column.type) {
244
+ case "text":
245
+ case "uuid":
246
+ return typeof value === "string";
247
+ case "integer":
248
+ case "bigint":
249
+ return typeof value === "number";
250
+ case "timestamp":
251
+ return value instanceof Date || typeof value === "string" && !isNaN(Date.parse(value));
252
+ case "jsonb":
253
+ if (typeof value !== "object") return false;
254
+ try {
255
+ JSON.stringify(value);
256
+ return true;
257
+ } catch {
258
+ return false;
259
+ }
260
+ default:
261
+ return false;
262
+ }
263
+ }
264
+ async validateAgainstSchema(record, schema) {
265
+ try {
266
+ if (!schema || typeof schema !== "object" || schema.value === null) {
267
+ throw new Error("Invalid schema format");
268
+ }
269
+ for (const [columnName, column] of Object.entries(schema)) {
270
+ const value = record[columnName];
271
+ if (column.primaryKey && (value === void 0 || value === null)) {
272
+ throw new Error(`Missing primary key value for column ${columnName}`);
273
+ }
274
+ if (!this.validateColumnValue(value, column)) {
275
+ const valueType = value === null ? "null" : typeof value;
276
+ throw new Error(`Invalid value for column ${columnName}: expected ${column.type}, got ${valueType}`);
277
+ }
278
+ }
279
+ } catch (error) {
280
+ const message = error instanceof Error ? error.message : String(error);
281
+ this.logger.error(`Error validating record against schema:`, { message, record, schema });
282
+ throw error;
283
+ }
284
+ }
285
+ async validateRecord(record, tableName) {
286
+ try {
287
+ if (!record || typeof record !== "object") {
288
+ throw new Error("Record must be an object");
289
+ }
290
+ const recordTyped = record;
291
+ const schema = await this.getTableSchema(tableName);
292
+ if (schema) {
293
+ await this.validateAgainstSchema(recordTyped, schema);
294
+ return;
295
+ }
296
+ switch (tableName) {
297
+ case storage.TABLE_THREADS:
298
+ if (!("id" in recordTyped) || !("resourceId" in recordTyped) || !("title" in recordTyped)) {
299
+ throw new Error("Thread record missing required fields");
300
+ }
301
+ break;
302
+ case storage.TABLE_MESSAGES:
303
+ if (!("id" in recordTyped) || !("threadId" in recordTyped) || !("content" in recordTyped) || !("role" in recordTyped)) {
304
+ throw new Error("Message record missing required fields");
305
+ }
306
+ break;
307
+ case storage.TABLE_WORKFLOW_SNAPSHOT:
308
+ if (!("workflow_name" in recordTyped) || !("run_id" in recordTyped)) {
309
+ throw new Error("Workflow record missing required fields");
310
+ }
311
+ break;
312
+ case storage.TABLE_TRACES:
313
+ if (!("id" in recordTyped)) {
314
+ throw new Error("Trace record missing required fields");
315
+ }
316
+ break;
317
+ case storage.TABLE_SCORERS:
318
+ if (!("id" in recordTyped) || !("scorerId" in recordTyped)) {
319
+ throw new Error("Score record missing required fields");
320
+ }
321
+ break;
322
+ default:
323
+ throw new Error(`Unknown table type: ${tableName}`);
324
+ }
325
+ } catch (error) {
326
+ const message = error instanceof Error ? error.message : String(error);
327
+ this.logger.error(`Failed to validate record for ${tableName}:`, { message, record });
328
+ throw error;
329
+ }
330
+ }
331
+ async insert({ tableName, record }) {
332
+ try {
333
+ const key = this.getKey(tableName, record);
334
+ const processedRecord = { ...record };
335
+ await this.validateRecord(processedRecord, tableName);
336
+ await this.putKV({ tableName, key, value: processedRecord });
337
+ } catch (error$1) {
338
+ throw new error.MastraError(
339
+ {
340
+ id: storage.createStorageErrorId("CLOUDFLARE", "INSERT", "FAILED"),
341
+ domain: error.ErrorDomain.STORAGE,
342
+ category: error.ErrorCategory.THIRD_PARTY,
343
+ details: {
344
+ tableName
345
+ }
346
+ },
347
+ error$1
348
+ );
349
+ }
350
+ }
351
+ async load({ tableName, keys }) {
352
+ try {
353
+ const key = this.getKey(tableName, keys);
354
+ const data = await this.getKV(tableName, key);
355
+ if (!data) return null;
356
+ return data;
357
+ } catch (error$1) {
358
+ const mastraError = new error.MastraError(
359
+ {
360
+ id: storage.createStorageErrorId("CLOUDFLARE", "LOAD", "FAILED"),
361
+ domain: error.ErrorDomain.STORAGE,
362
+ category: error.ErrorCategory.THIRD_PARTY,
363
+ details: {
364
+ tableName
365
+ }
366
+ },
367
+ error$1
368
+ );
369
+ this.logger?.trackException(mastraError);
370
+ this.logger?.error(mastraError.toString());
371
+ return null;
372
+ }
373
+ }
374
+ async batchInsert(input) {
375
+ if (!input.records || input.records.length === 0) return;
376
+ try {
377
+ await Promise.all(
378
+ input.records.map(async (record) => {
379
+ const key = this.getKey(input.tableName, record);
380
+ await this.putKV({ tableName: input.tableName, key, value: record });
381
+ })
382
+ );
383
+ } catch (error$1) {
384
+ throw new error.MastraError(
385
+ {
386
+ id: storage.createStorageErrorId("CLOUDFLARE", "BATCH_INSERT", "FAILED"),
387
+ domain: error.ErrorDomain.STORAGE,
388
+ category: error.ErrorCategory.THIRD_PARTY,
389
+ text: `Error in batch insert for table ${input.tableName}`,
390
+ details: {
391
+ tableName: input.tableName
392
+ }
393
+ },
394
+ error$1
395
+ );
396
+ }
397
+ }
398
+ safeSerialize(data) {
399
+ return typeof data === "string" ? data : JSON.stringify(data);
400
+ }
401
+ async putNamespaceValue({
402
+ tableName,
403
+ key,
404
+ value,
405
+ metadata
406
+ }) {
407
+ try {
408
+ const serializedValue = this.safeSerialize(value);
409
+ const serializedMetadata = metadata ? this.safeSerialize(metadata) : "";
410
+ if (this.bindings) {
411
+ const binding = this.getBinding(tableName);
412
+ await binding.put(key, serializedValue, { metadata: serializedMetadata });
413
+ } else {
414
+ const namespaceId = await this.getNamespaceId(tableName);
415
+ await this.client.kv.namespaces.values.update(namespaceId, key, {
416
+ account_id: this.accountId,
417
+ value: serializedValue,
418
+ metadata: serializedMetadata
419
+ });
420
+ }
421
+ } catch (error) {
422
+ const message = error instanceof Error ? error.message : String(error);
423
+ this.logger.error(`Failed to put value for ${tableName} ${key}:`, { message });
424
+ throw error;
425
+ }
426
+ }
427
+ async putKV({
428
+ tableName,
429
+ key,
430
+ value,
431
+ metadata
432
+ }) {
433
+ try {
434
+ await this.putNamespaceValue({ tableName, key, value, metadata });
435
+ } catch (error) {
436
+ this.logger.error(`Failed to put KV value for ${tableName}:${key}:`, error);
437
+ throw new Error(`Failed to put KV value: ${error.message}`);
438
+ }
439
+ }
440
+ async createTable({
441
+ tableName,
442
+ schema
443
+ }) {
444
+ try {
445
+ const schemaKey = this.getSchemaKey(tableName);
446
+ const metadata = {
447
+ type: "table_schema",
448
+ tableName,
449
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
450
+ };
451
+ await this.putKV({ tableName, key: schemaKey, value: schema, metadata });
452
+ } catch (error$1) {
453
+ throw new error.MastraError(
454
+ {
455
+ id: storage.createStorageErrorId("CLOUDFLARE", "CREATE_TABLE", "FAILED"),
456
+ domain: error.ErrorDomain.STORAGE,
457
+ category: error.ErrorCategory.THIRD_PARTY,
458
+ details: {
459
+ tableName
460
+ }
461
+ },
462
+ error$1
463
+ );
464
+ }
465
+ }
466
+ async clearTable({ tableName }) {
467
+ try {
468
+ const keys = await this.listKV(tableName);
469
+ if (keys.length > 0) {
470
+ await Promise.all(keys.map((keyObj) => this.deleteKV(tableName, keyObj.name)));
471
+ }
472
+ } catch (error$1) {
473
+ throw new error.MastraError(
474
+ {
475
+ id: storage.createStorageErrorId("CLOUDFLARE", "CLEAR_TABLE", "FAILED"),
476
+ domain: error.ErrorDomain.STORAGE,
477
+ category: error.ErrorCategory.THIRD_PARTY,
478
+ details: {
479
+ tableName
480
+ }
481
+ },
482
+ error$1
483
+ );
484
+ }
485
+ }
486
+ async dropTable({ tableName }) {
487
+ try {
488
+ const keys = await this.listKV(tableName);
489
+ if (keys.length > 0) {
490
+ await Promise.all(keys.map((keyObj) => this.deleteKV(tableName, keyObj.name)));
491
+ }
492
+ } catch (error$1) {
493
+ throw new error.MastraError(
494
+ {
495
+ id: storage.createStorageErrorId("CLOUDFLARE", "DROP_TABLE", "FAILED"),
496
+ domain: error.ErrorDomain.STORAGE,
497
+ category: error.ErrorCategory.THIRD_PARTY,
498
+ details: {
499
+ tableName
500
+ }
501
+ },
502
+ error$1
503
+ );
504
+ }
505
+ }
506
+ async listNamespaceKeys(tableName, options) {
507
+ try {
508
+ if (this.bindings) {
509
+ const binding = this.getBinding(tableName);
510
+ const response = await binding.list({
511
+ limit: options?.limit || 1e3,
512
+ prefix: options?.prefix
513
+ });
514
+ return response.keys;
515
+ } else {
516
+ const namespaceId = await this.getNamespaceId(tableName);
517
+ const response = await this.client.kv.namespaces.keys.list(namespaceId, {
518
+ account_id: this.accountId,
519
+ limit: options?.limit || 1e3,
520
+ prefix: options?.prefix
521
+ });
522
+ return response.result;
523
+ }
524
+ } catch (error$1) {
525
+ throw new error.MastraError(
526
+ {
527
+ id: storage.createStorageErrorId("CLOUDFLARE", "LIST_NAMESPACE_KEYS", "FAILED"),
528
+ domain: error.ErrorDomain.STORAGE,
529
+ category: error.ErrorCategory.THIRD_PARTY,
530
+ details: {
531
+ tableName
532
+ }
533
+ },
534
+ error$1
535
+ );
536
+ }
537
+ }
538
+ async deleteNamespaceValue(tableName, key) {
539
+ if (this.bindings) {
540
+ const binding = this.getBinding(tableName);
541
+ await binding.delete(key);
542
+ } else {
543
+ const namespaceId = await this.getNamespaceId(tableName);
544
+ await this.client.kv.namespaces.values.delete(namespaceId, key, {
545
+ account_id: this.accountId
546
+ });
547
+ }
548
+ }
549
+ async deleteKV(tableName, key) {
550
+ try {
551
+ await this.deleteNamespaceValue(tableName, key);
552
+ } catch (error) {
553
+ this.logger.error(`Failed to delete KV value for ${tableName}:${key}:`, error);
554
+ throw new Error(`Failed to delete KV value: ${error.message}`);
555
+ }
556
+ }
557
+ async listKV(tableName, options) {
558
+ try {
559
+ return await this.listNamespaceKeys(tableName, options);
560
+ } catch (error) {
561
+ this.logger.error(`Failed to list KV for ${tableName}:`, error);
562
+ throw new Error(`Failed to list KV: ${error.message}`);
563
+ }
564
+ }
565
+ };
566
+
567
+ // src/kv/storage/domains/memory/index.ts
568
+ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
569
+ #db;
570
+ constructor(config) {
571
+ super();
572
+ this.#db = new CloudflareKVDB(resolveCloudflareConfig(config));
573
+ }
574
+ async init() {
575
+ }
576
+ async dangerouslyClearAll() {
577
+ await this.#db.clearTable({ tableName: storage.TABLE_MESSAGES });
578
+ await this.#db.clearTable({ tableName: storage.TABLE_THREADS });
579
+ await this.#db.clearTable({ tableName: storage.TABLE_RESOURCES });
580
+ }
581
+ ensureMetadata(metadata) {
582
+ if (!metadata) return void 0;
583
+ return typeof metadata === "string" ? JSON.parse(metadata) : metadata;
584
+ }
585
+ /**
586
+ * Summarizes message content without exposing raw data (for logging).
587
+ * Returns type, length, and keys only to prevent PII leakage.
588
+ */
589
+ summarizeMessageContent(content) {
590
+ if (!content) return { type: "undefined" };
591
+ if (typeof content === "string") return { type: "string", length: content.length };
592
+ if (Array.isArray(content)) return { type: "array", length: content.length };
593
+ if (typeof content === "object") return { type: "object", keys: Object.keys(content) };
594
+ return { type: typeof content };
595
+ }
596
+ async getThreadById({ threadId }) {
597
+ const thread = await this.#db.load({ tableName: storage.TABLE_THREADS, keys: { id: threadId } });
598
+ if (!thread) return null;
599
+ try {
600
+ return {
601
+ ...thread,
602
+ createdAt: storage.ensureDate(thread.createdAt),
603
+ updatedAt: storage.ensureDate(thread.updatedAt),
604
+ metadata: this.ensureMetadata(thread.metadata)
605
+ };
606
+ } catch (error$1) {
607
+ const mastraError = new error.MastraError(
608
+ {
609
+ id: storage.createStorageErrorId("CLOUDFLARE", "GET_THREAD_BY_ID", "FAILED"),
610
+ domain: error.ErrorDomain.STORAGE,
611
+ category: error.ErrorCategory.THIRD_PARTY,
612
+ details: {
613
+ threadId
614
+ }
615
+ },
616
+ error$1
617
+ );
618
+ this.logger?.trackException(mastraError);
619
+ this.logger?.error(mastraError.toString());
620
+ return null;
621
+ }
622
+ }
623
+ async listThreads(args) {
624
+ const { page = 0, perPage: perPageInput, orderBy, filter } = args;
625
+ try {
626
+ this.validatePaginationInput(page, perPageInput ?? 100);
627
+ } catch (error$1) {
628
+ throw new error.MastraError(
629
+ {
630
+ id: storage.createStorageErrorId("CLOUDFLARE", "LIST_THREADS", "INVALID_PAGE"),
631
+ domain: error.ErrorDomain.STORAGE,
632
+ category: error.ErrorCategory.USER,
633
+ details: { page, ...perPageInput !== void 0 && { perPage: perPageInput } }
634
+ },
635
+ error$1 instanceof Error ? error$1 : new Error("Invalid pagination parameters")
636
+ );
637
+ }
638
+ const perPage = storage.normalizePerPage(perPageInput, 100);
639
+ try {
640
+ const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
641
+ const { field, direction } = this.parseOrderBy(orderBy);
642
+ const prefix = this.#db.namespacePrefix ? `${this.#db.namespacePrefix}:` : "";
643
+ const keyObjs = await this.#db.listKV(storage.TABLE_THREADS, { prefix: `${prefix}${storage.TABLE_THREADS}` });
644
+ const threads = [];
645
+ for (const { name: key } of keyObjs) {
646
+ const data = await this.#db.getKV(storage.TABLE_THREADS, key);
647
+ if (!data) continue;
648
+ if (filter?.resourceId && data.resourceId !== filter.resourceId) {
649
+ continue;
650
+ }
651
+ if (filter?.metadata && Object.keys(filter.metadata).length > 0) {
652
+ const metadata = this.ensureMetadata(data.metadata);
653
+ if (!metadata) continue;
654
+ const matches = Object.entries(filter.metadata).every(([key2, value]) => metadata[key2] === value);
655
+ if (!matches) continue;
656
+ }
657
+ threads.push(data);
658
+ }
659
+ threads.sort((a, b) => {
660
+ const aTime = new Date(a[field] || 0).getTime();
661
+ const bTime = new Date(b[field] || 0).getTime();
662
+ return direction === "ASC" ? aTime - bTime : bTime - aTime;
663
+ });
664
+ const end = perPageInput === false ? threads.length : offset + perPage;
665
+ const paginatedThreads = threads.slice(offset, end);
666
+ return {
667
+ page,
668
+ perPage: perPageForResponse,
669
+ total: threads.length,
670
+ hasMore: perPageInput === false ? false : offset + perPage < threads.length,
671
+ threads: paginatedThreads
672
+ };
673
+ } catch (error$1) {
674
+ throw new error.MastraError(
675
+ {
676
+ id: storage.createStorageErrorId("CLOUDFLARE", "LIST_THREADS", "FAILED"),
677
+ domain: error.ErrorDomain.STORAGE,
678
+ category: error.ErrorCategory.THIRD_PARTY,
679
+ text: "Failed to list threads with filters"
680
+ },
681
+ error$1
682
+ );
683
+ }
684
+ }
685
+ async saveThread({ thread }) {
686
+ try {
687
+ await this.#db.insert({ tableName: storage.TABLE_THREADS, record: thread });
688
+ return thread;
689
+ } catch (error$1) {
690
+ throw new error.MastraError(
691
+ {
692
+ id: storage.createStorageErrorId("CLOUDFLARE", "SAVE_THREAD", "FAILED"),
693
+ domain: error.ErrorDomain.STORAGE,
694
+ category: error.ErrorCategory.THIRD_PARTY,
695
+ details: {
696
+ threadId: thread.id
697
+ }
698
+ },
699
+ error$1
700
+ );
701
+ }
702
+ }
703
+ async updateThread({
704
+ id,
705
+ title,
706
+ metadata
707
+ }) {
708
+ try {
709
+ const thread = await this.getThreadById({ threadId: id });
710
+ if (!thread) {
711
+ throw new Error(`Thread ${id} not found`);
712
+ }
713
+ const updatedThread = {
714
+ ...thread,
715
+ title,
716
+ metadata: this.ensureMetadata({
717
+ ...thread.metadata ?? {},
718
+ ...metadata
719
+ }),
720
+ updatedAt: /* @__PURE__ */ new Date()
721
+ };
722
+ await this.#db.insert({ tableName: storage.TABLE_THREADS, record: updatedThread });
723
+ return updatedThread;
724
+ } catch (error$1) {
725
+ throw new error.MastraError(
726
+ {
727
+ id: storage.createStorageErrorId("CLOUDFLARE", "UPDATE_THREAD", "FAILED"),
728
+ domain: error.ErrorDomain.STORAGE,
729
+ category: error.ErrorCategory.THIRD_PARTY,
730
+ details: {
731
+ threadId: id,
732
+ title
733
+ }
734
+ },
735
+ error$1
736
+ );
737
+ }
738
+ }
739
+ getMessageKey(threadId, messageId) {
740
+ try {
741
+ return this.#db.getKey(storage.TABLE_MESSAGES, { threadId, id: messageId });
742
+ } catch (error) {
743
+ const message = error instanceof Error ? error.message : String(error);
744
+ this.logger?.error(`Error getting message key for thread ${threadId} and message ${messageId}:`, { message });
745
+ throw error;
746
+ }
747
+ }
748
+ getThreadMessagesKey(threadId) {
749
+ try {
750
+ return this.#db.getKey(storage.TABLE_MESSAGES, { threadId, id: "messages" });
751
+ } catch (error) {
752
+ const message = error instanceof Error ? error.message : String(error);
753
+ this.logger?.error(`Error getting thread messages key for thread ${threadId}:`, { message });
754
+ throw error;
755
+ }
756
+ }
757
+ async deleteThread({ threadId }) {
758
+ try {
759
+ const thread = await this.getThreadById({ threadId });
760
+ if (!thread) {
761
+ throw new Error(`Thread ${threadId} not found`);
762
+ }
763
+ const messageKeys = await this.#db.listKV(storage.TABLE_MESSAGES);
764
+ const threadMessageKeys = messageKeys.filter((key) => key.name.includes(`${storage.TABLE_MESSAGES}:${threadId}:`));
765
+ await Promise.all([
766
+ // Delete message order
767
+ this.#db.deleteKV(storage.TABLE_MESSAGES, this.getThreadMessagesKey(threadId)),
768
+ // Delete all messages
769
+ ...threadMessageKeys.map((key) => this.#db.deleteKV(storage.TABLE_MESSAGES, key.name)),
770
+ // Delete thread
771
+ this.#db.deleteKV(storage.TABLE_THREADS, this.#db.getKey(storage.TABLE_THREADS, { id: threadId }))
772
+ ]);
773
+ } catch (error$1) {
774
+ throw new error.MastraError(
775
+ {
776
+ id: storage.createStorageErrorId("CLOUDFLARE", "DELETE_THREAD", "FAILED"),
777
+ domain: error.ErrorDomain.STORAGE,
778
+ category: error.ErrorCategory.THIRD_PARTY,
779
+ details: {
780
+ threadId
781
+ }
782
+ },
783
+ error$1
784
+ );
785
+ }
786
+ }
787
+ /**
788
+ * Searches all threads in the KV store to find a message by its ID.
789
+ *
790
+ * **Performance Warning**: This method sequentially scans all threads to locate
791
+ * the message. For stores with many threads, this can result in significant
792
+ * latency and API calls. When possible, callers should provide the `threadId`
793
+ * directly to avoid this full scan.
794
+ *
795
+ * @param messageId - The globally unique message ID to search for
796
+ * @returns The message with its threadId if found, null otherwise
797
+ */
798
+ async findMessageInAnyThread(messageId) {
799
+ try {
800
+ const prefix = this.#db.namespacePrefix ? `${this.#db.namespacePrefix}:` : "";
801
+ const threadKeys = await this.#db.listKV(storage.TABLE_THREADS, { prefix: `${prefix}${storage.TABLE_THREADS}` });
802
+ for (const { name: threadKey } of threadKeys) {
803
+ const threadId = threadKey.split(":").pop();
804
+ if (!threadId || threadId === "messages") continue;
805
+ const messageKey = this.getMessageKey(threadId, messageId);
806
+ const message = await this.#db.getKV(storage.TABLE_MESSAGES, messageKey);
807
+ if (message) {
808
+ return { ...message, threadId };
809
+ }
810
+ }
811
+ return null;
812
+ } catch (error) {
813
+ this.logger?.error(`Error finding message ${messageId} in any thread:`, error);
814
+ return null;
815
+ }
816
+ }
817
+ /**
818
+ * Queue for serializing sorted order updates.
819
+ * Updates the sorted order for a given key. This operation is eventually consistent.
820
+ */
821
+ updateQueue = /* @__PURE__ */ new Map();
822
+ async updateSorting(threadMessages) {
823
+ return threadMessages.map((msg) => ({
824
+ message: msg,
825
+ // Use _index if available, otherwise timestamp, matching Upstash
826
+ score: msg._index !== void 0 ? msg._index : msg.createdAt.getTime()
827
+ })).sort((a, b) => a.score - b.score).map((item) => ({
828
+ id: item.message.id,
829
+ score: item.score
830
+ }));
831
+ }
832
+ /**
833
+ * Updates the sorted order for a given key. This operation is eventually consistent.
834
+ * Note: Operations on the same orderKey are serialized using a queue to prevent
835
+ * concurrent updates from conflicting with each other.
836
+ */
837
+ async updateSortedMessages(orderKey, newEntries) {
838
+ const currentPromise = this.updateQueue.get(orderKey) || Promise.resolve();
839
+ const nextPromise = currentPromise.then(async () => {
840
+ try {
841
+ const currentOrder = await this.getSortedMessages(orderKey);
842
+ const orderMap = new Map(currentOrder.map((entry) => [entry.id, entry]));
843
+ for (const entry of newEntries) {
844
+ orderMap.set(entry.id, entry);
845
+ }
846
+ const updatedOrder = Array.from(orderMap.values()).sort((a, b) => a.score - b.score);
847
+ await this.#db.putKV({
848
+ tableName: storage.TABLE_MESSAGES,
849
+ key: orderKey,
850
+ value: JSON.stringify(updatedOrder)
851
+ });
852
+ } catch (error) {
853
+ const message = error instanceof Error ? error.message : String(error);
854
+ this.logger?.error(`Error updating sorted order for key ${orderKey}:`, { message });
855
+ throw error;
856
+ } finally {
857
+ if (this.updateQueue.get(orderKey) === nextPromise) {
858
+ this.updateQueue.delete(orderKey);
859
+ }
860
+ }
861
+ });
862
+ this.updateQueue.set(orderKey, nextPromise);
863
+ return nextPromise;
864
+ }
865
+ async getSortedMessages(orderKey) {
866
+ const raw = await this.#db.getKV(storage.TABLE_MESSAGES, orderKey);
867
+ if (!raw) return [];
868
+ try {
869
+ const arr = JSON.parse(typeof raw === "string" ? raw : JSON.stringify(raw));
870
+ return Array.isArray(arr) ? arr : [];
871
+ } catch (e) {
872
+ this.logger?.error(`Error parsing order data for key ${orderKey}:`, { e });
873
+ return [];
874
+ }
875
+ }
876
+ async migrateMessage(messageId, fromThreadId, toThreadId) {
877
+ try {
878
+ const oldMessageKey = this.getMessageKey(fromThreadId, messageId);
879
+ const message = await this.#db.getKV(storage.TABLE_MESSAGES, oldMessageKey);
880
+ if (!message) return;
881
+ const updatedMessage = {
882
+ ...message,
883
+ threadId: toThreadId
884
+ };
885
+ const newMessageKey = this.getMessageKey(toThreadId, messageId);
886
+ await this.#db.putKV({ tableName: storage.TABLE_MESSAGES, key: newMessageKey, value: updatedMessage });
887
+ const oldOrderKey = this.getThreadMessagesKey(fromThreadId);
888
+ const oldEntries = await this.getSortedMessages(oldOrderKey);
889
+ const filteredEntries = oldEntries.filter((entry) => entry.id !== messageId);
890
+ await this.updateSortedMessages(oldOrderKey, filteredEntries);
891
+ const newOrderKey = this.getThreadMessagesKey(toThreadId);
892
+ const newEntries = await this.getSortedMessages(newOrderKey);
893
+ const newEntry = { id: messageId, score: Date.now() };
894
+ newEntries.push(newEntry);
895
+ await this.updateSortedMessages(newOrderKey, newEntries);
896
+ await this.#db.deleteKV(storage.TABLE_MESSAGES, oldMessageKey);
897
+ } catch (error) {
898
+ this.logger?.error(`Error migrating message ${messageId} from ${fromThreadId} to ${toThreadId}:`, error);
899
+ throw error;
900
+ }
901
+ }
902
+ async saveMessages(args) {
903
+ const { messages } = args;
904
+ if (!Array.isArray(messages) || messages.length === 0) return { messages: [] };
905
+ try {
906
+ const validatedMessages = messages.map((message, index) => {
907
+ const errors = [];
908
+ if (!message.id) errors.push("id is required");
909
+ if (!message.threadId) errors.push("threadId is required");
910
+ if (!message.content) errors.push("content is required");
911
+ if (!message.role) errors.push("role is required");
912
+ if (!message.createdAt) errors.push("createdAt is required");
913
+ if (message.resourceId === null || message.resourceId === void 0) errors.push("resourceId is required");
914
+ if (errors.length > 0) {
915
+ throw new Error(`Invalid message at index ${index}: ${errors.join(", ")}`);
916
+ }
917
+ return {
918
+ ...message,
919
+ createdAt: storage.ensureDate(message.createdAt),
920
+ type: message.type || "v2",
921
+ _index: index
922
+ };
923
+ }).filter((m) => !!m);
924
+ const messageMigrationTasks = [];
925
+ for (const message of validatedMessages) {
926
+ const existingMessage = await this.findMessageInAnyThread(message.id);
927
+ this.logger?.debug(
928
+ `Checking message ${message.id}: existing=${existingMessage?.threadId}, new=${message.threadId}`
929
+ );
930
+ if (existingMessage && existingMessage.threadId && existingMessage.threadId !== message.threadId) {
931
+ this.logger?.debug(`Migrating message ${message.id} from ${existingMessage.threadId} to ${message.threadId}`);
932
+ messageMigrationTasks.push(this.migrateMessage(message.id, existingMessage.threadId, message.threadId));
933
+ }
934
+ }
935
+ await Promise.all(messageMigrationTasks);
936
+ const messagesByThread = validatedMessages.reduce((acc, message) => {
937
+ if (message.threadId && !acc.has(message.threadId)) {
938
+ acc.set(message.threadId, []);
939
+ }
940
+ if (message.threadId) {
941
+ acc.get(message.threadId).push(message);
942
+ }
943
+ return acc;
944
+ }, /* @__PURE__ */ new Map());
945
+ await Promise.all(
946
+ Array.from(messagesByThread.entries()).map(async ([threadId, threadMessages]) => {
947
+ try {
948
+ const thread = await this.getThreadById({ threadId });
949
+ if (!thread) {
950
+ throw new Error(`Thread ${threadId} not found`);
951
+ }
952
+ await Promise.all(
953
+ threadMessages.map(async (message) => {
954
+ const key = this.getMessageKey(threadId, message.id);
955
+ const { _index, ...cleanMessage } = message;
956
+ const serializedMessage = {
957
+ ...cleanMessage,
958
+ createdAt: storage.serializeDate(cleanMessage.createdAt)
959
+ };
960
+ this.logger?.debug(`Saving message ${message.id}`, {
961
+ contentSummary: this.summarizeMessageContent(serializedMessage.content)
962
+ });
963
+ await this.#db.putKV({ tableName: storage.TABLE_MESSAGES, key, value: serializedMessage });
964
+ })
965
+ );
966
+ const orderKey = this.getThreadMessagesKey(threadId);
967
+ const entries = await this.updateSorting(threadMessages);
968
+ await this.updateSortedMessages(orderKey, entries);
969
+ const updatedThread = {
970
+ ...thread,
971
+ updatedAt: /* @__PURE__ */ new Date()
972
+ };
973
+ await this.#db.putKV({
974
+ tableName: storage.TABLE_THREADS,
975
+ key: this.#db.getKey(storage.TABLE_THREADS, { id: threadId }),
976
+ value: updatedThread
977
+ });
978
+ } catch (error$1) {
979
+ throw new error.MastraError(
980
+ {
981
+ id: storage.createStorageErrorId("CLOUDFLARE", "SAVE_MESSAGES", "FAILED"),
982
+ domain: error.ErrorDomain.STORAGE,
983
+ category: error.ErrorCategory.THIRD_PARTY,
984
+ details: {
985
+ threadId
986
+ }
987
+ },
988
+ error$1
989
+ );
990
+ }
991
+ })
992
+ );
993
+ const prepared = validatedMessages.map(
994
+ ({ _index, ...message }) => ({ ...message, type: message.type !== "v2" ? message.type : void 0 })
995
+ );
996
+ const list = new agent.MessageList().add(prepared, "memory");
997
+ return { messages: list.get.all.db() };
998
+ } catch (error$1) {
999
+ throw new error.MastraError(
1000
+ {
1001
+ id: storage.createStorageErrorId("CLOUDFLARE", "SAVE_MESSAGES", "FAILED"),
1002
+ domain: error.ErrorDomain.STORAGE,
1003
+ category: error.ErrorCategory.THIRD_PARTY
1004
+ },
1005
+ error$1
1006
+ );
1007
+ }
1008
+ }
1009
+ async getRank(orderKey, id) {
1010
+ const order = await this.getSortedMessages(orderKey);
1011
+ const index = order.findIndex((item) => item.id === id);
1012
+ return index >= 0 ? index : null;
1013
+ }
1014
+ async getRange(orderKey, start, end) {
1015
+ const order = await this.getSortedMessages(orderKey);
1016
+ const actualStart = start < 0 ? Math.max(0, order.length + start) : start;
1017
+ const actualEnd = end < 0 ? order.length + end : Math.min(end, order.length - 1);
1018
+ const sliced = order.slice(actualStart, actualEnd + 1);
1019
+ return sliced.map((item) => item.id);
1020
+ }
1021
+ async getLastN(orderKey, n) {
1022
+ return this.getRange(orderKey, -n, -1);
1023
+ }
1024
+ async getFullOrder(orderKey) {
1025
+ return this.getRange(orderKey, 0, -1);
1026
+ }
1027
+ /**
1028
+ * Retrieves messages specified in the include array along with their surrounding context.
1029
+ *
1030
+ * **Performance Note**: When `threadId` is not provided in an include entry, this method
1031
+ * must call `findMessageInAnyThread` which sequentially scans all threads in the KV store.
1032
+ * For optimal performance, callers should provide `threadId` in include entries when known.
1033
+ *
1034
+ * @param include - Array of message IDs to include, optionally with context windows
1035
+ * @param messageIds - Set to accumulate the message IDs that should be fetched
1036
+ */
1037
+ _sortMessages(messages, field, direction) {
1038
+ return messages.sort((a, b) => {
1039
+ const isDateField = field === "createdAt" || field === "updatedAt";
1040
+ const aVal = isDateField ? new Date(a[field]).getTime() : a[field];
1041
+ const bVal = isDateField ? new Date(b[field]).getTime() : b[field];
1042
+ if (aVal == null && bVal == null) return a.id.localeCompare(b.id);
1043
+ if (aVal == null) return 1;
1044
+ if (bVal == null) return -1;
1045
+ if (typeof aVal === "number" && typeof bVal === "number") {
1046
+ const cmp2 = direction === "ASC" ? aVal - bVal : bVal - aVal;
1047
+ return cmp2 !== 0 ? cmp2 : a.id.localeCompare(b.id);
1048
+ }
1049
+ const cmp = direction === "ASC" ? String(aVal).localeCompare(String(bVal)) : String(bVal).localeCompare(String(aVal));
1050
+ return cmp !== 0 ? cmp : a.id.localeCompare(b.id);
1051
+ });
1052
+ }
1053
+ async getIncludedMessagesWithContext(include, messageIds) {
1054
+ await Promise.all(
1055
+ include.map(async (item) => {
1056
+ let targetThreadId = item.threadId;
1057
+ if (!targetThreadId) {
1058
+ const foundMessage = await this.findMessageInAnyThread(item.id);
1059
+ if (!foundMessage) return;
1060
+ targetThreadId = foundMessage.threadId;
1061
+ }
1062
+ if (!targetThreadId) return;
1063
+ const threadMessagesKey = this.getThreadMessagesKey(targetThreadId);
1064
+ messageIds.add(item.id);
1065
+ if (!item.withPreviousMessages && !item.withNextMessages) return;
1066
+ const rank = await this.getRank(threadMessagesKey, item.id);
1067
+ if (rank === null) return;
1068
+ if (item.withPreviousMessages) {
1069
+ const prevIds = await this.getRange(
1070
+ threadMessagesKey,
1071
+ Math.max(0, rank - item.withPreviousMessages),
1072
+ rank - 1
1073
+ );
1074
+ prevIds.forEach((id) => messageIds.add(id));
1075
+ }
1076
+ if (item.withNextMessages) {
1077
+ const nextIds = await this.getRange(threadMessagesKey, rank + 1, rank + item.withNextMessages);
1078
+ nextIds.forEach((id) => messageIds.add(id));
1079
+ }
1080
+ })
1081
+ );
1082
+ }
1083
+ async getRecentMessages(threadId, limit, messageIds) {
1084
+ if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
1085
+ if (limit <= 0) return;
1086
+ try {
1087
+ const threadMessagesKey = this.getThreadMessagesKey(threadId);
1088
+ const latestIds = await this.getLastN(threadMessagesKey, limit);
1089
+ latestIds.forEach((id) => messageIds.add(id));
1090
+ } catch {
1091
+ this.logger?.debug(`No message order found for thread ${threadId}, skipping latest messages`);
1092
+ }
1093
+ }
1094
+ /**
1095
+ * Fetches and parses messages from one or more threads.
1096
+ *
1097
+ * **Performance Note**: When neither `include` entries with `threadId` nor `targetThreadId`
1098
+ * are provided, this method falls back to `findMessageInAnyThread` which scans all threads.
1099
+ * For optimal performance, provide `threadId` in include entries or specify `targetThreadId`.
1100
+ */
1101
+ async fetchAndParseMessagesFromMultipleThreads(messageIds, include, targetThreadId) {
1102
+ const messageIdToThreadId = /* @__PURE__ */ new Map();
1103
+ if (include) {
1104
+ for (const item of include) {
1105
+ if (item.threadId) {
1106
+ messageIdToThreadId.set(item.id, item.threadId);
1107
+ }
1108
+ }
1109
+ }
1110
+ const messages = await Promise.all(
1111
+ messageIds.map(async (id) => {
1112
+ try {
1113
+ let threadId = messageIdToThreadId.get(id);
1114
+ if (!threadId) {
1115
+ if (targetThreadId) {
1116
+ threadId = targetThreadId;
1117
+ } else {
1118
+ const foundMessage = await this.findMessageInAnyThread(id);
1119
+ if (foundMessage) {
1120
+ threadId = foundMessage.threadId;
1121
+ }
1122
+ }
1123
+ }
1124
+ if (!threadId) return null;
1125
+ const key = this.getMessageKey(threadId, id);
1126
+ const data = await this.#db.getKV(storage.TABLE_MESSAGES, key);
1127
+ if (!data) return null;
1128
+ const parsed = typeof data === "string" ? JSON.parse(data) : data;
1129
+ this.logger?.debug(`Retrieved message ${id} from thread ${threadId}`, {
1130
+ contentSummary: this.summarizeMessageContent(parsed.content)
1131
+ });
1132
+ return parsed;
1133
+ } catch (error) {
1134
+ const message = error instanceof Error ? error.message : String(error);
1135
+ this.logger?.error(`Error retrieving message ${id}:`, { message });
1136
+ return null;
1137
+ }
1138
+ })
1139
+ );
1140
+ return messages.filter((msg) => msg !== null);
1141
+ }
1142
+ /**
1143
+ * Retrieves messages by their IDs.
1144
+ *
1145
+ * **Performance Warning**: This method calls `findMessageInAnyThread` for each message ID,
1146
+ * which scans all threads in the KV store. For large numbers of messages or threads,
1147
+ * this can result in significant latency. Consider using `listMessages` with specific
1148
+ * thread IDs when the thread context is known.
1149
+ */
1150
+ async listMessagesById({ messageIds }) {
1151
+ if (messageIds.length === 0) return { messages: [] };
1152
+ try {
1153
+ const messages = (await Promise.all(messageIds.map((id) => this.findMessageInAnyThread(id)))).filter(
1154
+ (result) => !!result
1155
+ );
1156
+ const prepared = messages.map(({ _index, ...message }) => ({
1157
+ ...message,
1158
+ ...message.type !== `v2` && { type: message.type },
1159
+ createdAt: storage.ensureDate(message.createdAt)
1160
+ }));
1161
+ const list = new agent.MessageList().add(prepared, "memory");
1162
+ return { messages: list.get.all.db() };
1163
+ } catch (error$1) {
1164
+ const mastraError = new error.MastraError(
1165
+ {
1166
+ id: storage.createStorageErrorId("CLOUDFLARE", "LIST_MESSAGES_BY_ID", "FAILED"),
1167
+ domain: error.ErrorDomain.STORAGE,
1168
+ category: error.ErrorCategory.THIRD_PARTY,
1169
+ text: `Error retrieving messages by ID`,
1170
+ details: {
1171
+ messageIds: JSON.stringify(messageIds)
1172
+ }
1173
+ },
1174
+ error$1
1175
+ );
1176
+ this.logger?.trackException(mastraError);
1177
+ this.logger?.error(mastraError.toString());
1178
+ return { messages: [] };
1179
+ }
1180
+ }
1181
+ async listMessages(args) {
1182
+ const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
1183
+ const threadIds = Array.isArray(threadId) ? threadId : [threadId];
1184
+ const isValidThreadId = (id) => typeof id === "string" && id.trim().length > 0;
1185
+ if (threadIds.length === 0 || threadIds.some((id) => !isValidThreadId(id))) {
1186
+ throw new error.MastraError(
1187
+ {
1188
+ id: storage.createStorageErrorId("CLOUDFLARE", "LIST_MESSAGES", "INVALID_THREAD_ID"),
1189
+ domain: error.ErrorDomain.STORAGE,
1190
+ category: error.ErrorCategory.THIRD_PARTY,
1191
+ details: { threadId: Array.isArray(threadId) ? JSON.stringify(threadId) : String(threadId) }
1192
+ },
1193
+ new Error("threadId must be a non-empty string or array of non-empty strings")
1194
+ );
1195
+ }
1196
+ const perPage = storage.normalizePerPage(perPageInput, 40);
1197
+ const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
1198
+ try {
1199
+ if (page < 0) {
1200
+ throw new error.MastraError(
1201
+ {
1202
+ id: storage.createStorageErrorId("CLOUDFLARE", "LIST_MESSAGES", "INVALID_PAGE"),
1203
+ domain: error.ErrorDomain.STORAGE,
1204
+ category: error.ErrorCategory.USER,
1205
+ details: { page }
1206
+ },
1207
+ new Error("page must be >= 0")
1208
+ );
1209
+ }
1210
+ const { field, direction } = this.parseOrderBy(orderBy, "ASC");
1211
+ if (perPage === 0 && include && include.length > 0) {
1212
+ const includedMessageIds = /* @__PURE__ */ new Set();
1213
+ await this.getIncludedMessagesWithContext(include, includedMessageIds);
1214
+ const includedMessages2 = await this.fetchAndParseMessagesFromMultipleThreads(
1215
+ Array.from(includedMessageIds),
1216
+ include,
1217
+ void 0
1218
+ );
1219
+ const list2 = new agent.MessageList().add(includedMessages2, "memory");
1220
+ return {
1221
+ messages: this._sortMessages(list2.get.all.db(), field, direction),
1222
+ total: 0,
1223
+ page,
1224
+ perPage: perPageForResponse,
1225
+ hasMore: false
1226
+ };
1227
+ }
1228
+ if (perPage === 0 && (!include || include.length === 0)) {
1229
+ return { messages: [], total: 0, page, perPage: perPageForResponse, hasMore: false };
1230
+ }
1231
+ const threadMessageIds = /* @__PURE__ */ new Set();
1232
+ for (const tid of threadIds) {
1233
+ try {
1234
+ const threadMessagesKey = this.getThreadMessagesKey(tid);
1235
+ const allIds = await this.getFullOrder(threadMessagesKey);
1236
+ allIds.forEach((id) => threadMessageIds.add(id));
1237
+ } catch {
1238
+ }
1239
+ }
1240
+ const threadMessages = await this.fetchAndParseMessagesFromMultipleThreads(
1241
+ Array.from(threadMessageIds),
1242
+ void 0,
1243
+ threadIds.length === 1 ? threadIds[0] : void 0
1244
+ );
1245
+ let filteredThreadMessages = threadMessages;
1246
+ if (resourceId) {
1247
+ filteredThreadMessages = filteredThreadMessages.filter((msg) => msg.resourceId === resourceId);
1248
+ }
1249
+ filteredThreadMessages = storage.filterByDateRange(
1250
+ filteredThreadMessages,
1251
+ (msg) => new Date(msg.createdAt),
1252
+ filter?.dateRange
1253
+ );
1254
+ const total = filteredThreadMessages.length;
1255
+ filteredThreadMessages.sort((a, b) => {
1256
+ const timeA = new Date(a.createdAt).getTime();
1257
+ const timeB = new Date(b.createdAt).getTime();
1258
+ const timeDiff = direction === "ASC" ? timeA - timeB : timeB - timeA;
1259
+ if (timeDiff === 0) {
1260
+ return a.id.localeCompare(b.id);
1261
+ }
1262
+ return timeDiff;
1263
+ });
1264
+ let paginatedMessages;
1265
+ if (perPage === 0) {
1266
+ paginatedMessages = [];
1267
+ } else if (perPage === Number.MAX_SAFE_INTEGER) {
1268
+ paginatedMessages = filteredThreadMessages;
1269
+ } else {
1270
+ paginatedMessages = filteredThreadMessages.slice(offset, offset + perPage);
1271
+ }
1272
+ let includedMessages = [];
1273
+ if (include && include.length > 0) {
1274
+ const includedMessageIds = /* @__PURE__ */ new Set();
1275
+ await this.getIncludedMessagesWithContext(include, includedMessageIds);
1276
+ const paginatedIds = new Set(paginatedMessages.map((m) => m.id));
1277
+ const idsToFetch = Array.from(includedMessageIds).filter((id) => !paginatedIds.has(id));
1278
+ if (idsToFetch.length > 0) {
1279
+ includedMessages = await this.fetchAndParseMessagesFromMultipleThreads(idsToFetch, include, void 0);
1280
+ }
1281
+ }
1282
+ const seenIds = /* @__PURE__ */ new Set();
1283
+ const allMessages = [];
1284
+ for (const msg of paginatedMessages) {
1285
+ if (!seenIds.has(msg.id)) {
1286
+ allMessages.push(msg);
1287
+ seenIds.add(msg.id);
1288
+ }
1289
+ }
1290
+ for (const msg of includedMessages) {
1291
+ if (!seenIds.has(msg.id)) {
1292
+ allMessages.push(msg);
1293
+ seenIds.add(msg.id);
1294
+ }
1295
+ }
1296
+ allMessages.sort((a, b) => {
1297
+ const timeA = new Date(a.createdAt).getTime();
1298
+ const timeB = new Date(b.createdAt).getTime();
1299
+ const timeDiff = direction === "ASC" ? timeA - timeB : timeB - timeA;
1300
+ if (timeDiff === 0) {
1301
+ return a.id.localeCompare(b.id);
1302
+ }
1303
+ return timeDiff;
1304
+ });
1305
+ let filteredMessages = allMessages;
1306
+ const paginatedCount = paginatedMessages.length;
1307
+ if (total === 0 && filteredMessages.length === 0 && (!include || include.length === 0)) {
1308
+ return {
1309
+ messages: [],
1310
+ total: 0,
1311
+ page,
1312
+ perPage: perPageForResponse,
1313
+ hasMore: false
1314
+ };
1315
+ }
1316
+ const prepared = filteredMessages.map(({ _index, ...message }) => ({
1317
+ ...message,
1318
+ type: message.type !== "v2" ? message.type : void 0,
1319
+ createdAt: storage.ensureDate(message.createdAt)
1320
+ }));
1321
+ const primaryThreadId = Array.isArray(threadId) ? threadId[0] : threadId;
1322
+ const list = new agent.MessageList({ threadId: primaryThreadId, resourceId }).add(
1323
+ prepared,
1324
+ "memory"
1325
+ );
1326
+ const finalMessages = this._sortMessages(list.get.all.db(), field, direction);
1327
+ const threadIdSet = new Set(threadIds);
1328
+ const returnedThreadMessageIds = new Set(
1329
+ finalMessages.filter((m) => m.threadId && threadIdSet.has(m.threadId)).map((m) => m.id)
1330
+ );
1331
+ const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
1332
+ const hasMore = perPageInput !== false && !allThreadMessagesReturned && offset + paginatedCount < total;
1333
+ return {
1334
+ messages: finalMessages,
1335
+ total,
1336
+ page,
1337
+ perPage: perPageForResponse,
1338
+ hasMore
1339
+ };
1340
+ } catch (error$1) {
1341
+ const mastraError = new error.MastraError(
1342
+ {
1343
+ id: storage.createStorageErrorId("CLOUDFLARE", "LIST_MESSAGES", "FAILED"),
1344
+ domain: error.ErrorDomain.STORAGE,
1345
+ category: error.ErrorCategory.THIRD_PARTY,
1346
+ text: `Failed to list messages for thread ${Array.isArray(threadId) ? threadId.join(",") : threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
1347
+ details: {
1348
+ threadId: Array.isArray(threadId) ? threadId.join(",") : threadId,
1349
+ resourceId: resourceId ?? ""
1350
+ }
1351
+ },
1352
+ error$1
1353
+ );
1354
+ this.logger?.error?.(mastraError.toString());
1355
+ this.logger?.trackException?.(mastraError);
1356
+ return {
1357
+ messages: [],
1358
+ total: 0,
1359
+ page,
1360
+ perPage: perPageForResponse,
1361
+ hasMore: false
1362
+ };
1363
+ }
1364
+ }
1365
+ async updateMessages(args) {
1366
+ try {
1367
+ const { messages } = args;
1368
+ const updatedMessages = [];
1369
+ for (const messageUpdate of messages) {
1370
+ const { id, content, ...otherFields } = messageUpdate;
1371
+ const prefix = this.#db.namespacePrefix ? `${this.#db.namespacePrefix}:` : "";
1372
+ const keyObjs = await this.#db.listKV(storage.TABLE_MESSAGES, { prefix: `${prefix}${storage.TABLE_MESSAGES}` });
1373
+ let existingMessage = null;
1374
+ let messageKey = "";
1375
+ for (const { name: key } of keyObjs) {
1376
+ const data = await this.#db.getKV(storage.TABLE_MESSAGES, key);
1377
+ if (data && data.id === id) {
1378
+ existingMessage = data;
1379
+ messageKey = key;
1380
+ break;
1381
+ }
1382
+ }
1383
+ if (!existingMessage) {
1384
+ continue;
1385
+ }
1386
+ const updatedMessage = {
1387
+ ...existingMessage,
1388
+ ...otherFields,
1389
+ id
1390
+ };
1391
+ if (content) {
1392
+ if (content.metadata !== void 0) {
1393
+ updatedMessage.content = {
1394
+ ...updatedMessage.content,
1395
+ metadata: {
1396
+ ...updatedMessage.content?.metadata,
1397
+ ...content.metadata
1398
+ }
1399
+ };
1400
+ }
1401
+ if (content.content !== void 0) {
1402
+ updatedMessage.content = {
1403
+ ...updatedMessage.content,
1404
+ content: content.content
1405
+ };
1406
+ }
1407
+ }
1408
+ if ("threadId" in messageUpdate && messageUpdate.threadId && messageUpdate.threadId !== existingMessage.threadId) {
1409
+ await this.#db.deleteKV(storage.TABLE_MESSAGES, messageKey);
1410
+ updatedMessage.threadId = messageUpdate.threadId;
1411
+ const newMessageKey = this.getMessageKey(messageUpdate.threadId, id);
1412
+ await this.#db.putKV({
1413
+ tableName: storage.TABLE_MESSAGES,
1414
+ key: newMessageKey,
1415
+ value: updatedMessage
1416
+ });
1417
+ if (existingMessage.threadId) {
1418
+ const sourceOrderKey = this.getThreadMessagesKey(existingMessage.threadId);
1419
+ const sourceEntries = await this.getSortedMessages(sourceOrderKey);
1420
+ const filteredEntries = sourceEntries.filter((entry) => entry.id !== id);
1421
+ await this.updateSortedMessages(sourceOrderKey, filteredEntries);
1422
+ }
1423
+ const destOrderKey = this.getThreadMessagesKey(messageUpdate.threadId);
1424
+ const destEntries = await this.getSortedMessages(destOrderKey);
1425
+ const newEntry = { id, score: Date.now() };
1426
+ destEntries.push(newEntry);
1427
+ await this.updateSortedMessages(destOrderKey, destEntries);
1428
+ } else {
1429
+ await this.#db.putKV({
1430
+ tableName: storage.TABLE_MESSAGES,
1431
+ key: messageKey,
1432
+ value: updatedMessage
1433
+ });
1434
+ }
1435
+ const threadsToUpdate = /* @__PURE__ */ new Set();
1436
+ if (updatedMessage.threadId) {
1437
+ threadsToUpdate.add(updatedMessage.threadId);
1438
+ }
1439
+ if ("threadId" in messageUpdate && messageUpdate.threadId && messageUpdate.threadId !== existingMessage.threadId) {
1440
+ if (existingMessage.threadId) {
1441
+ threadsToUpdate.add(existingMessage.threadId);
1442
+ }
1443
+ threadsToUpdate.add(messageUpdate.threadId);
1444
+ }
1445
+ for (const threadId of threadsToUpdate) {
1446
+ const thread = await this.getThreadById({ threadId });
1447
+ if (thread) {
1448
+ const updatedThread = {
1449
+ ...thread,
1450
+ updatedAt: /* @__PURE__ */ new Date()
1451
+ };
1452
+ await this.#db.putKV({
1453
+ tableName: storage.TABLE_THREADS,
1454
+ key: this.#db.getKey(storage.TABLE_THREADS, { id: threadId }),
1455
+ value: updatedThread
1456
+ });
1457
+ }
1458
+ }
1459
+ updatedMessages.push(updatedMessage);
1460
+ }
1461
+ return updatedMessages;
1462
+ } catch (error$1) {
1463
+ throw new error.MastraError(
1464
+ {
1465
+ id: storage.createStorageErrorId("CLOUDFLARE", "UPDATE_MESSAGES", "FAILED"),
1466
+ domain: error.ErrorDomain.STORAGE,
1467
+ category: error.ErrorCategory.THIRD_PARTY,
1468
+ text: "Failed to update messages"
1469
+ },
1470
+ error$1
1471
+ );
1472
+ }
1473
+ }
1474
+ async getResourceById({ resourceId }) {
1475
+ try {
1476
+ const data = await this.#db.getKV(storage.TABLE_RESOURCES, resourceId);
1477
+ if (!data) return null;
1478
+ const resource = typeof data === "string" ? JSON.parse(data) : data;
1479
+ return {
1480
+ ...resource,
1481
+ createdAt: storage.ensureDate(resource.createdAt),
1482
+ updatedAt: storage.ensureDate(resource.updatedAt),
1483
+ metadata: this.ensureMetadata(resource.metadata)
1484
+ };
1485
+ } catch (error$1) {
1486
+ const mastraError = new error.MastraError(
1487
+ {
1488
+ id: storage.createStorageErrorId("CLOUDFLARE", "GET_RESOURCE_BY_ID", "FAILED"),
1489
+ domain: error.ErrorDomain.STORAGE,
1490
+ category: error.ErrorCategory.THIRD_PARTY,
1491
+ details: {
1492
+ resourceId
1493
+ }
1494
+ },
1495
+ error$1
1496
+ );
1497
+ this.logger?.trackException(mastraError);
1498
+ this.logger?.error(mastraError.toString());
1499
+ return null;
1500
+ }
1501
+ }
1502
+ async saveResource({ resource }) {
1503
+ try {
1504
+ const resourceToSave = {
1505
+ ...resource,
1506
+ metadata: resource.metadata ? JSON.stringify(resource.metadata) : null
1507
+ };
1508
+ await this.#db.putKV({
1509
+ tableName: storage.TABLE_RESOURCES,
1510
+ key: resource.id,
1511
+ value: resourceToSave
1512
+ });
1513
+ return resource;
1514
+ } catch (error$1) {
1515
+ throw new error.MastraError(
1516
+ {
1517
+ id: storage.createStorageErrorId("CLOUDFLARE", "SAVE_RESOURCE", "FAILED"),
1518
+ domain: error.ErrorDomain.STORAGE,
1519
+ category: error.ErrorCategory.THIRD_PARTY,
1520
+ details: {
1521
+ resourceId: resource.id
1522
+ }
1523
+ },
1524
+ error$1
1525
+ );
1526
+ }
1527
+ }
1528
+ async updateResource({
1529
+ resourceId,
1530
+ workingMemory,
1531
+ metadata
1532
+ }) {
1533
+ const existingResource = await this.getResourceById({ resourceId });
1534
+ if (!existingResource) {
1535
+ const newResource = {
1536
+ id: resourceId,
1537
+ workingMemory,
1538
+ metadata: metadata || {},
1539
+ createdAt: /* @__PURE__ */ new Date(),
1540
+ updatedAt: /* @__PURE__ */ new Date()
1541
+ };
1542
+ return this.saveResource({ resource: newResource });
1543
+ }
1544
+ const updatedAt = /* @__PURE__ */ new Date();
1545
+ const updatedResource = {
1546
+ ...existingResource,
1547
+ workingMemory: workingMemory !== void 0 ? workingMemory : existingResource.workingMemory,
1548
+ metadata: {
1549
+ ...existingResource.metadata,
1550
+ ...metadata
1551
+ },
1552
+ updatedAt
1553
+ };
1554
+ return this.saveResource({ resource: updatedResource });
1555
+ }
1556
+ async deleteMessages(messageIds) {
1557
+ if (messageIds.length === 0) return;
1558
+ try {
1559
+ const threadIds = /* @__PURE__ */ new Set();
1560
+ for (const messageId of messageIds) {
1561
+ const message = await this.findMessageInAnyThread(messageId);
1562
+ if (message?.threadId) {
1563
+ threadIds.add(message.threadId);
1564
+ const messageKey = this.getMessageKey(message.threadId, messageId);
1565
+ await this.#db.deleteKV(storage.TABLE_MESSAGES, messageKey);
1566
+ const orderKey = this.getThreadMessagesKey(message.threadId);
1567
+ const entries = await this.getSortedMessages(orderKey);
1568
+ const filteredEntries = entries.filter((entry) => entry.id !== messageId);
1569
+ if (filteredEntries.length > 0) {
1570
+ await this.#db.putKV({
1571
+ tableName: storage.TABLE_MESSAGES,
1572
+ key: orderKey,
1573
+ value: JSON.stringify(filteredEntries)
1574
+ });
1575
+ } else {
1576
+ await this.#db.deleteKV(storage.TABLE_MESSAGES, orderKey);
1577
+ }
1578
+ }
1579
+ }
1580
+ for (const threadId of threadIds) {
1581
+ const thread = await this.getThreadById({ threadId });
1582
+ if (thread) {
1583
+ const updatedThread = {
1584
+ ...thread,
1585
+ updatedAt: /* @__PURE__ */ new Date()
1586
+ };
1587
+ await this.#db.putKV({
1588
+ tableName: storage.TABLE_THREADS,
1589
+ key: this.#db.getKey(storage.TABLE_THREADS, { id: threadId }),
1590
+ value: updatedThread
1591
+ });
1592
+ }
1593
+ }
1594
+ } catch (error$1) {
1595
+ throw new error.MastraError(
1596
+ {
1597
+ id: storage.createStorageErrorId("CLOUDFLARE", "DELETE_MESSAGES", "FAILED"),
1598
+ domain: error.ErrorDomain.STORAGE,
1599
+ category: error.ErrorCategory.THIRD_PARTY,
1600
+ details: { messageIds: JSON.stringify(messageIds) }
1601
+ },
1602
+ error$1
1603
+ );
1604
+ }
1605
+ }
1606
+ };
1607
+ function transformScoreRow(row) {
1608
+ return storage.transformScoreRow(row);
1609
+ }
1610
+ var ScoresStorageCloudflare = class extends storage.ScoresStorage {
1611
+ #db;
1612
+ constructor(config) {
1613
+ super();
1614
+ this.#db = new CloudflareKVDB(resolveCloudflareConfig(config));
1615
+ }
1616
+ async init() {
1617
+ }
1618
+ async dangerouslyClearAll() {
1619
+ await this.#db.clearTable({ tableName: storage.TABLE_SCORERS });
1620
+ }
1621
+ async getScoreById({ id }) {
1622
+ try {
1623
+ const score = await this.#db.getKV(storage.TABLE_SCORERS, id);
1624
+ if (!score) {
1625
+ return null;
1626
+ }
1627
+ return transformScoreRow(score);
1628
+ } catch (error$1) {
1629
+ const mastraError = new error.MastraError(
1630
+ {
1631
+ id: storage.createStorageErrorId("CLOUDFLARE", "GET_SCORE_BY_ID", "FAILED"),
1632
+ domain: error.ErrorDomain.STORAGE,
1633
+ category: error.ErrorCategory.THIRD_PARTY,
1634
+ text: `Failed to get score by id: ${id}`
1635
+ },
1636
+ error$1
1637
+ );
1638
+ this.logger?.trackException(mastraError);
1639
+ this.logger?.error(mastraError.toString());
1640
+ return null;
1641
+ }
1642
+ }
1643
+ async saveScore(score) {
1644
+ let parsedScore;
1645
+ try {
1646
+ parsedScore = evals.saveScorePayloadSchema.parse(score);
1647
+ } catch (error$1) {
1648
+ throw new error.MastraError(
1649
+ {
1650
+ id: storage.createStorageErrorId("CLOUDFLARE", "SAVE_SCORE", "VALIDATION_FAILED"),
1651
+ domain: error.ErrorDomain.STORAGE,
1652
+ category: error.ErrorCategory.USER,
1653
+ details: {
1654
+ scorer: typeof score.scorer?.id === "string" ? score.scorer.id : String(score.scorer?.id ?? "unknown"),
1655
+ entityId: score.entityId ?? "unknown",
1656
+ entityType: score.entityType ?? "unknown",
1657
+ traceId: score.traceId ?? "",
1658
+ spanId: score.spanId ?? ""
1659
+ }
1660
+ },
1661
+ error$1
1662
+ );
1663
+ }
1664
+ const id = crypto.randomUUID();
1665
+ try {
1666
+ const serializedRecord = {};
1667
+ for (const [key, value] of Object.entries(parsedScore)) {
1668
+ if (value !== null && value !== void 0) {
1669
+ if (typeof value === "object") {
1670
+ serializedRecord[key] = JSON.stringify(value);
1671
+ } else {
1672
+ serializedRecord[key] = value;
1673
+ }
1674
+ } else {
1675
+ serializedRecord[key] = null;
1676
+ }
1677
+ }
1678
+ const now = /* @__PURE__ */ new Date();
1679
+ serializedRecord.id = id;
1680
+ serializedRecord.createdAt = now.toISOString();
1681
+ serializedRecord.updatedAt = now.toISOString();
1682
+ await this.#db.putKV({
1683
+ tableName: storage.TABLE_SCORERS,
1684
+ key: id,
1685
+ value: serializedRecord
1686
+ });
1687
+ return { score: { ...parsedScore, id, createdAt: now, updatedAt: now } };
1688
+ } catch (error$1) {
1689
+ const mastraError = new error.MastraError(
1690
+ {
1691
+ id: storage.createStorageErrorId("CLOUDFLARE", "SAVE_SCORE", "FAILED"),
1692
+ domain: error.ErrorDomain.STORAGE,
1693
+ category: error.ErrorCategory.THIRD_PARTY,
1694
+ details: { id }
1695
+ },
1696
+ error$1
1697
+ );
1698
+ this.logger?.trackException(mastraError);
1699
+ this.logger?.error(mastraError.toString());
1700
+ throw mastraError;
1701
+ }
1702
+ }
1703
+ async listScoresByScorerId({
1704
+ scorerId,
1705
+ entityId,
1706
+ entityType,
1707
+ source,
1708
+ pagination
1709
+ }) {
1710
+ try {
1711
+ const keys = await this.#db.listKV(storage.TABLE_SCORERS);
1712
+ const scores = [];
1713
+ for (const { name: key } of keys) {
1714
+ const score = await this.#db.getKV(storage.TABLE_SCORERS, key);
1715
+ if (entityId && score.entityId !== entityId) {
1716
+ continue;
1717
+ }
1718
+ if (entityType && score.entityType !== entityType) {
1719
+ continue;
1720
+ }
1721
+ if (source && score.source !== source) {
1722
+ continue;
1723
+ }
1724
+ if (score && score.scorerId === scorerId) {
1725
+ scores.push(transformScoreRow(score));
1726
+ }
1727
+ }
1728
+ scores.sort((a, b) => {
1729
+ const dateA = new Date(a.createdAt || 0).getTime();
1730
+ const dateB = new Date(b.createdAt || 0).getTime();
1731
+ return dateB - dateA;
1732
+ });
1733
+ const { page, perPage: perPageInput } = pagination;
1734
+ const perPage = storage.normalizePerPage(perPageInput, 100);
1735
+ const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
1736
+ const total = scores.length;
1737
+ const end = perPageInput === false ? scores.length : start + perPage;
1738
+ const pagedScores = scores.slice(start, end);
1739
+ return {
1740
+ pagination: {
1741
+ total,
1742
+ page,
1743
+ perPage: perPageForResponse,
1744
+ hasMore: end < total
1745
+ },
1746
+ scores: pagedScores
1747
+ };
1748
+ } catch (error$1) {
1749
+ const mastraError = new error.MastraError(
1750
+ {
1751
+ id: storage.createStorageErrorId("CLOUDFLARE", "GET_SCORES_BY_SCORER_ID", "FAILED"),
1752
+ domain: error.ErrorDomain.STORAGE,
1753
+ category: error.ErrorCategory.THIRD_PARTY,
1754
+ text: `Failed to get scores by scorer id: ${scorerId}`
1755
+ },
1756
+ error$1
1757
+ );
1758
+ this.logger?.trackException(mastraError);
1759
+ this.logger?.error(mastraError.toString());
1760
+ return { pagination: { total: 0, page: 0, perPage: 100, hasMore: false }, scores: [] };
1761
+ }
1762
+ }
1763
+ async listScoresByRunId({
1764
+ runId,
1765
+ pagination
1766
+ }) {
1767
+ try {
1768
+ const keys = await this.#db.listKV(storage.TABLE_SCORERS);
1769
+ const scores = [];
1770
+ for (const { name: key } of keys) {
1771
+ const score = await this.#db.getKV(storage.TABLE_SCORERS, key);
1772
+ if (score && score.runId === runId) {
1773
+ scores.push(transformScoreRow(score));
1774
+ }
1775
+ }
1776
+ scores.sort((a, b) => {
1777
+ const dateA = new Date(a.createdAt || 0).getTime();
1778
+ const dateB = new Date(b.createdAt || 0).getTime();
1779
+ return dateB - dateA;
1780
+ });
1781
+ const { page, perPage: perPageInput } = pagination;
1782
+ const perPage = storage.normalizePerPage(perPageInput, 100);
1783
+ const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
1784
+ const total = scores.length;
1785
+ const end = perPageInput === false ? scores.length : start + perPage;
1786
+ const pagedScores = scores.slice(start, end);
1787
+ return {
1788
+ pagination: {
1789
+ total,
1790
+ page,
1791
+ perPage: perPageForResponse,
1792
+ hasMore: end < total
1793
+ },
1794
+ scores: pagedScores
1795
+ };
1796
+ } catch (error$1) {
1797
+ const mastraError = new error.MastraError(
1798
+ {
1799
+ id: storage.createStorageErrorId("CLOUDFLARE", "GET_SCORES_BY_RUN_ID", "FAILED"),
1800
+ domain: error.ErrorDomain.STORAGE,
1801
+ category: error.ErrorCategory.THIRD_PARTY,
1802
+ text: `Failed to get scores by run id: ${runId}`
1803
+ },
1804
+ error$1
1805
+ );
1806
+ this.logger?.trackException(mastraError);
1807
+ this.logger?.error(mastraError.toString());
1808
+ return { pagination: { total: 0, page: 0, perPage: 100, hasMore: false }, scores: [] };
1809
+ }
1810
+ }
1811
+ async listScoresByEntityId({
1812
+ entityId,
1813
+ entityType,
1814
+ pagination
1815
+ }) {
1816
+ try {
1817
+ const keys = await this.#db.listKV(storage.TABLE_SCORERS);
1818
+ const scores = [];
1819
+ for (const { name: key } of keys) {
1820
+ const score = await this.#db.getKV(storage.TABLE_SCORERS, key);
1821
+ if (score && score.entityId === entityId && score.entityType === entityType) {
1822
+ scores.push(transformScoreRow(score));
1823
+ }
1824
+ }
1825
+ scores.sort((a, b) => {
1826
+ const dateA = new Date(a.createdAt || 0).getTime();
1827
+ const dateB = new Date(b.createdAt || 0).getTime();
1828
+ return dateB - dateA;
1829
+ });
1830
+ const { page, perPage: perPageInput } = pagination;
1831
+ const perPage = storage.normalizePerPage(perPageInput, 100);
1832
+ const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
1833
+ const total = scores.length;
1834
+ const end = perPageInput === false ? scores.length : start + perPage;
1835
+ const pagedScores = scores.slice(start, end);
1836
+ return {
1837
+ pagination: {
1838
+ total,
1839
+ page,
1840
+ perPage: perPageForResponse,
1841
+ hasMore: end < total
1842
+ },
1843
+ scores: pagedScores
1844
+ };
1845
+ } catch (error$1) {
1846
+ const mastraError = new error.MastraError(
1847
+ {
1848
+ id: storage.createStorageErrorId("CLOUDFLARE", "GET_SCORES_BY_ENTITY_ID", "FAILED"),
1849
+ domain: error.ErrorDomain.STORAGE,
1850
+ category: error.ErrorCategory.THIRD_PARTY,
1851
+ text: `Failed to get scores by entity id: ${entityId}, type: ${entityType}`
1852
+ },
1853
+ error$1
1854
+ );
1855
+ this.logger?.trackException(mastraError);
1856
+ this.logger?.error(mastraError.toString());
1857
+ return { pagination: { total: 0, page: 0, perPage: 100, hasMore: false }, scores: [] };
1858
+ }
1859
+ }
1860
+ async listScoresBySpan({
1861
+ traceId,
1862
+ spanId,
1863
+ pagination
1864
+ }) {
1865
+ try {
1866
+ const keys = await this.#db.listKV(storage.TABLE_SCORERS);
1867
+ const scores = [];
1868
+ for (const { name: key } of keys) {
1869
+ const score = await this.#db.getKV(storage.TABLE_SCORERS, key);
1870
+ if (score && score.traceId === traceId && score.spanId === spanId) {
1871
+ scores.push(transformScoreRow(score));
1872
+ }
1873
+ }
1874
+ scores.sort((a, b) => {
1875
+ const dateA = new Date(a.createdAt || 0).getTime();
1876
+ const dateB = new Date(b.createdAt || 0).getTime();
1877
+ return dateB - dateA;
1878
+ });
1879
+ const { page, perPage: perPageInput } = pagination;
1880
+ const perPage = storage.normalizePerPage(perPageInput, 100);
1881
+ const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
1882
+ const total = scores.length;
1883
+ const end = perPageInput === false ? scores.length : start + perPage;
1884
+ const pagedScores = scores.slice(start, end);
1885
+ return {
1886
+ pagination: {
1887
+ total,
1888
+ page,
1889
+ perPage: perPageForResponse,
1890
+ hasMore: end < total
1891
+ },
1892
+ scores: pagedScores
1893
+ };
1894
+ } catch (error$1) {
1895
+ const mastraError = new error.MastraError(
1896
+ {
1897
+ id: storage.createStorageErrorId("CLOUDFLARE", "GET_SCORES_BY_SPAN", "FAILED"),
1898
+ domain: error.ErrorDomain.STORAGE,
1899
+ category: error.ErrorCategory.THIRD_PARTY,
1900
+ text: `Failed to get scores by span: traceId=${traceId}, spanId=${spanId}`
1901
+ },
1902
+ error$1
1903
+ );
1904
+ this.logger?.trackException(mastraError);
1905
+ this.logger?.error(mastraError.toString());
1906
+ return { pagination: { total: 0, page: 0, perPage: 100, hasMore: false }, scores: [] };
1907
+ }
1908
+ }
1909
+ };
1910
+ var WorkflowsStorageCloudflare = class extends storage.WorkflowsStorage {
1911
+ #db;
1912
+ constructor(config) {
1913
+ super();
1914
+ this.#db = new CloudflareKVDB(resolveCloudflareConfig(config));
1915
+ }
1916
+ supportsConcurrentUpdates() {
1917
+ return false;
1918
+ }
1919
+ async init() {
1920
+ }
1921
+ async dangerouslyClearAll() {
1922
+ await this.#db.clearTable({ tableName: storage.TABLE_WORKFLOW_SNAPSHOT });
1923
+ }
1924
+ validateWorkflowParams(params) {
1925
+ const { workflowName, runId } = params;
1926
+ if (!workflowName || !runId) {
1927
+ throw new Error("Invalid workflow snapshot parameters");
1928
+ }
1929
+ }
1930
+ async updateWorkflowResults(_args) {
1931
+ throw new Error(
1932
+ "updateWorkflowResults is not implemented for Cloudflare KV storage. Cloudflare KV is eventually-consistent and does not support atomic read-modify-write operations needed for concurrent workflow updates."
1933
+ );
1934
+ }
1935
+ async updateWorkflowState(_args) {
1936
+ throw new Error(
1937
+ "updateWorkflowState is not implemented for Cloudflare KV storage. Cloudflare KV is eventually-consistent and does not support atomic read-modify-write operations needed for concurrent workflow updates."
1938
+ );
1939
+ }
1940
+ async persistWorkflowSnapshot(params) {
1941
+ try {
1942
+ const { workflowName, runId, resourceId, snapshot, createdAt, updatedAt } = params;
1943
+ const now = /* @__PURE__ */ new Date();
1944
+ const existingKey = this.#db.getKey(storage.TABLE_WORKFLOW_SNAPSHOT, { workflow_name: workflowName, run_id: runId });
1945
+ const existing = await this.#db.getKV(storage.TABLE_WORKFLOW_SNAPSHOT, existingKey);
1946
+ await this.#db.putKV({
1947
+ tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
1948
+ key: existingKey,
1949
+ value: {
1950
+ workflow_name: workflowName,
1951
+ run_id: runId,
1952
+ resourceId,
1953
+ snapshot: JSON.stringify(snapshot),
1954
+ createdAt: existing?.createdAt ?? createdAt ?? now,
1955
+ updatedAt: updatedAt ?? now
1956
+ }
1957
+ });
1958
+ } catch (error$1) {
1959
+ throw new error.MastraError(
1960
+ {
1961
+ id: storage.createStorageErrorId("CLOUDFLARE", "PERSIST_WORKFLOW_SNAPSHOT", "FAILED"),
1962
+ domain: error.ErrorDomain.STORAGE,
1963
+ category: error.ErrorCategory.THIRD_PARTY,
1964
+ text: `Error persisting workflow snapshot for workflow ${params.workflowName}, run ${params.runId}`,
1965
+ details: {
1966
+ workflowName: params.workflowName,
1967
+ runId: params.runId
1968
+ }
1969
+ },
1970
+ error$1
1971
+ );
1972
+ }
1973
+ }
1974
+ async loadWorkflowSnapshot(params) {
1975
+ try {
1976
+ this.validateWorkflowParams(params);
1977
+ const { workflowName, runId } = params;
1978
+ const key = this.#db.getKey(storage.TABLE_WORKFLOW_SNAPSHOT, { workflow_name: workflowName, run_id: runId });
1979
+ const data = await this.#db.getKV(storage.TABLE_WORKFLOW_SNAPSHOT, key);
1980
+ if (!data) return null;
1981
+ const snapshotData = typeof data.snapshot === "string" ? JSON.parse(data.snapshot) : data.snapshot;
1982
+ return snapshotData;
1983
+ } catch (error$1) {
1984
+ const mastraError = new error.MastraError(
1985
+ {
1986
+ id: storage.createStorageErrorId("CLOUDFLARE", "LOAD_WORKFLOW_SNAPSHOT", "FAILED"),
1987
+ domain: error.ErrorDomain.STORAGE,
1988
+ category: error.ErrorCategory.THIRD_PARTY,
1989
+ text: `Error loading workflow snapshot for workflow ${params.workflowName}, run ${params.runId}`,
1990
+ details: {
1991
+ workflowName: params.workflowName,
1992
+ runId: params.runId
1993
+ }
1994
+ },
1995
+ error$1
1996
+ );
1997
+ this.logger.trackException?.(mastraError);
1998
+ this.logger.error(mastraError.toString());
1999
+ return null;
2000
+ }
2001
+ }
2002
+ parseWorkflowRun(row) {
2003
+ let parsedSnapshot = row.snapshot;
2004
+ if (typeof parsedSnapshot === "string") {
2005
+ try {
2006
+ parsedSnapshot = JSON.parse(row.snapshot);
2007
+ } catch (e) {
2008
+ this.logger.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
2009
+ }
2010
+ }
2011
+ return {
2012
+ workflowName: row.workflow_name,
2013
+ runId: row.run_id,
2014
+ snapshot: parsedSnapshot,
2015
+ createdAt: storage.ensureDate(row.createdAt),
2016
+ updatedAt: storage.ensureDate(row.updatedAt),
2017
+ resourceId: row.resourceId
2018
+ };
2019
+ }
2020
+ buildWorkflowSnapshotPrefix({
2021
+ workflowName,
2022
+ runId,
2023
+ resourceId
2024
+ }) {
2025
+ const prefix = this.#db.namespacePrefix ? `${this.#db.namespacePrefix}:` : "";
2026
+ let key = `${prefix}${storage.TABLE_WORKFLOW_SNAPSHOT}`;
2027
+ if (workflowName) key += `:${workflowName}`;
2028
+ if (runId) key += `:${runId}`;
2029
+ if (resourceId) key += `:${resourceId}`;
2030
+ return key;
2031
+ }
2032
+ async listWorkflowRuns({
2033
+ workflowName,
2034
+ page = 0,
2035
+ perPage = 20,
2036
+ resourceId,
2037
+ fromDate,
2038
+ toDate,
2039
+ status
2040
+ } = {}) {
2041
+ try {
2042
+ if (page < 0 || !Number.isInteger(page)) {
2043
+ throw new error.MastraError(
2044
+ {
2045
+ id: storage.createStorageErrorId("CLOUDFLARE", "LIST_WORKFLOW_RUNS", "INVALID_PAGE"),
2046
+ domain: error.ErrorDomain.STORAGE,
2047
+ category: error.ErrorCategory.USER,
2048
+ details: { page }
2049
+ },
2050
+ new Error("page must be a non-negative integer")
2051
+ );
2052
+ }
2053
+ const normalizedPerPage = storage.normalizePerPage(perPage, 20);
2054
+ const offset = page * normalizedPerPage;
2055
+ const prefix = this.buildWorkflowSnapshotPrefix({ workflowName });
2056
+ const keyObjs = await this.#db.listKV(storage.TABLE_WORKFLOW_SNAPSHOT, { prefix });
2057
+ const runs = [];
2058
+ for (const { name: key } of keyObjs) {
2059
+ const parts = key.split(":");
2060
+ const idx = parts.indexOf(storage.TABLE_WORKFLOW_SNAPSHOT);
2061
+ if (idx === -1 || parts.length < idx + 3) continue;
2062
+ const wfName = parts[idx + 1];
2063
+ const keyResourceId = parts.length > idx + 3 ? parts[idx + 3] : void 0;
2064
+ if (workflowName && wfName !== workflowName) continue;
2065
+ const data = await this.#db.getKV(storage.TABLE_WORKFLOW_SNAPSHOT, key);
2066
+ if (!data) continue;
2067
+ try {
2068
+ const effectiveResourceId = keyResourceId || data.resourceId;
2069
+ if (resourceId && effectiveResourceId !== resourceId) continue;
2070
+ const snapshotData = typeof data.snapshot === "string" ? JSON.parse(data.snapshot) : data.snapshot;
2071
+ if (status && snapshotData.status !== status) continue;
2072
+ const createdAt = storage.ensureDate(data.createdAt);
2073
+ if (fromDate && createdAt && createdAt < fromDate) continue;
2074
+ if (toDate && createdAt && createdAt > toDate) continue;
2075
+ const run = this.parseWorkflowRun({
2076
+ ...data,
2077
+ workflow_name: wfName,
2078
+ resourceId: effectiveResourceId,
2079
+ snapshot: snapshotData
2080
+ });
2081
+ runs.push(run);
2082
+ } catch (err) {
2083
+ this.logger.error("Failed to parse workflow snapshot:", { key, error: err });
2084
+ }
2085
+ }
2086
+ runs.sort((a, b) => {
2087
+ const aDate = a.createdAt ? new Date(a.createdAt).getTime() : 0;
2088
+ const bDate = b.createdAt ? new Date(b.createdAt).getTime() : 0;
2089
+ return bDate - aDate;
2090
+ });
2091
+ const pagedRuns = runs.slice(offset, offset + normalizedPerPage);
2092
+ return {
2093
+ runs: pagedRuns,
2094
+ total: runs.length
2095
+ };
2096
+ } catch (error$1) {
2097
+ const mastraError = new error.MastraError(
2098
+ {
2099
+ id: storage.createStorageErrorId("CLOUDFLARE", "LIST_WORKFLOW_RUNS", "FAILED"),
2100
+ domain: error.ErrorDomain.STORAGE,
2101
+ category: error.ErrorCategory.THIRD_PARTY
2102
+ },
2103
+ error$1
2104
+ );
2105
+ this.logger.trackException?.(mastraError);
2106
+ this.logger.error(mastraError.toString());
2107
+ return { runs: [], total: 0 };
2108
+ }
2109
+ }
2110
+ async getWorkflowRunById({
2111
+ runId,
2112
+ workflowName
2113
+ }) {
2114
+ try {
2115
+ if (!runId || !workflowName) {
2116
+ throw new Error("runId, workflowName, are required");
2117
+ }
2118
+ const prefix = this.buildWorkflowSnapshotPrefix({ workflowName, runId });
2119
+ const keyObjs = await this.#db.listKV(storage.TABLE_WORKFLOW_SNAPSHOT, { prefix });
2120
+ if (!keyObjs.length) return null;
2121
+ const exactKey = keyObjs.find((k) => {
2122
+ const parts = k.name.split(":");
2123
+ const idx = parts.indexOf(storage.TABLE_WORKFLOW_SNAPSHOT);
2124
+ if (idx === -1 || parts.length < idx + 3) return false;
2125
+ const wfName = parts[idx + 1];
2126
+ const rId = parts[idx + 2];
2127
+ return wfName === workflowName && rId === runId;
2128
+ });
2129
+ if (!exactKey) return null;
2130
+ const data = await this.#db.getKV(storage.TABLE_WORKFLOW_SNAPSHOT, exactKey.name);
2131
+ if (!data) return null;
2132
+ const snapshotData = typeof data.snapshot === "string" ? JSON.parse(data.snapshot) : data.snapshot;
2133
+ return this.parseWorkflowRun({ ...data, snapshot: snapshotData });
2134
+ } catch (error$1) {
2135
+ const mastraError = new error.MastraError(
2136
+ {
2137
+ id: storage.createStorageErrorId("CLOUDFLARE", "GET_WORKFLOW_RUN_BY_ID", "FAILED"),
2138
+ domain: error.ErrorDomain.STORAGE,
2139
+ category: error.ErrorCategory.THIRD_PARTY,
2140
+ details: {
2141
+ workflowName,
2142
+ runId
2143
+ }
2144
+ },
2145
+ error$1
2146
+ );
2147
+ this.logger.trackException?.(mastraError);
2148
+ this.logger.error(mastraError.toString());
2149
+ return null;
2150
+ }
2151
+ }
2152
+ async deleteWorkflowRunById({ runId, workflowName }) {
2153
+ try {
2154
+ if (!runId || !workflowName) {
2155
+ throw new Error("runId and workflowName are required");
2156
+ }
2157
+ const key = this.#db.getKey(storage.TABLE_WORKFLOW_SNAPSHOT, { workflow_name: workflowName, run_id: runId });
2158
+ await this.#db.deleteKV(storage.TABLE_WORKFLOW_SNAPSHOT, key);
2159
+ } catch (error$1) {
2160
+ throw new error.MastraError(
2161
+ {
2162
+ id: storage.createStorageErrorId("CLOUDFLARE", "DELETE_WORKFLOW_RUN_BY_ID", "FAILED"),
2163
+ domain: error.ErrorDomain.STORAGE,
2164
+ category: error.ErrorCategory.THIRD_PARTY,
2165
+ details: {
2166
+ workflowName,
2167
+ runId
2168
+ }
2169
+ },
2170
+ error$1
2171
+ );
2172
+ }
2173
+ }
2174
+ };
2175
+
2176
+ // src/kv/storage/types.ts
2177
+ function isWorkersConfig(config) {
2178
+ return "bindings" in config;
2179
+ }
2180
+
2181
+ // src/kv/index.ts
2182
+ var CloudflareKVStorage = class extends storage.MastraCompositeStore {
2183
+ stores;
2184
+ client;
2185
+ accountId;
2186
+ namespacePrefix;
2187
+ bindings;
2188
+ validateWorkersConfig(config) {
2189
+ if (!isWorkersConfig(config)) {
2190
+ throw new Error("Invalid Workers API configuration");
2191
+ }
2192
+ if (!config.bindings) {
2193
+ throw new Error("KV bindings are required when using Workers Binding API");
2194
+ }
2195
+ const requiredTables = [storage.TABLE_THREADS, storage.TABLE_MESSAGES, storage.TABLE_WORKFLOW_SNAPSHOT, storage.TABLE_SCORERS];
2196
+ for (const table of requiredTables) {
2197
+ if (!(table in config.bindings)) {
2198
+ throw new Error(`Missing KV binding for table: ${table}`);
2199
+ }
2200
+ }
2201
+ }
2202
+ validateRestConfig(config) {
2203
+ if (isWorkersConfig(config)) {
2204
+ throw new Error("Invalid REST API configuration");
2205
+ }
2206
+ if (!config.accountId?.trim()) {
2207
+ throw new Error("accountId is required for REST API");
2208
+ }
2209
+ if (!config.apiToken?.trim()) {
2210
+ throw new Error("apiToken is required for REST API");
2211
+ }
2212
+ }
2213
+ constructor(config) {
2214
+ super({ id: config.id, name: "Cloudflare", disableInit: config.disableInit });
2215
+ try {
2216
+ let workflows;
2217
+ let memory;
2218
+ let scores;
2219
+ if (isWorkersConfig(config)) {
2220
+ this.validateWorkersConfig(config);
2221
+ this.bindings = config.bindings;
2222
+ this.namespacePrefix = config.keyPrefix?.trim() || "";
2223
+ this.logger.info("Using Cloudflare KV Workers Binding API");
2224
+ const domainConfig = {
2225
+ bindings: this.bindings,
2226
+ keyPrefix: this.namespacePrefix
2227
+ };
2228
+ workflows = new WorkflowsStorageCloudflare(domainConfig);
2229
+ memory = new MemoryStorageCloudflare(domainConfig);
2230
+ scores = new ScoresStorageCloudflare(domainConfig);
2231
+ } else {
2232
+ this.validateRestConfig(config);
2233
+ this.accountId = config.accountId.trim();
2234
+ this.namespacePrefix = config.namespacePrefix?.trim() || "";
2235
+ this.client = new Cloudflare__default.default({
2236
+ apiToken: config.apiToken.trim()
2237
+ });
2238
+ this.logger.info("Using Cloudflare KV REST API");
2239
+ const domainConfig = {
2240
+ client: this.client,
2241
+ accountId: this.accountId,
2242
+ namespacePrefix: this.namespacePrefix
2243
+ };
2244
+ workflows = new WorkflowsStorageCloudflare(domainConfig);
2245
+ memory = new MemoryStorageCloudflare(domainConfig);
2246
+ scores = new ScoresStorageCloudflare(domainConfig);
2247
+ }
2248
+ this.stores = {
2249
+ workflows,
2250
+ memory,
2251
+ scores
2252
+ };
2253
+ } catch (error$1) {
2254
+ throw new error.MastraError(
2255
+ {
2256
+ id: storage.createStorageErrorId("CLOUDFLARE", "INIT", "FAILED"),
2257
+ domain: error.ErrorDomain.STORAGE,
2258
+ category: error.ErrorCategory.THIRD_PARTY
2259
+ },
2260
+ error$1
2261
+ );
2262
+ }
2263
+ }
2264
+ async close() {
2265
+ }
2266
+ };
2267
+ var CloudflareStore = CloudflareKVStorage;
2268
+
2269
+ exports.CloudflareKVStorage = CloudflareKVStorage;
2270
+ exports.CloudflareStore = CloudflareStore;
2271
+ exports.MemoryStorageCloudflare = MemoryStorageCloudflare;
2272
+ exports.ScoresStorageCloudflare = ScoresStorageCloudflare;
2273
+ exports.WorkflowsStorageCloudflare = WorkflowsStorageCloudflare;
2274
+ //# sourceMappingURL=chunk-ZBYNKKG6.cjs.map
2275
+ //# sourceMappingURL=chunk-ZBYNKKG6.cjs.map