@mastra/cloudflare-d1 0.11.1-alpha.0 → 0.11.1-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +434 -206
- package/dist/index.js +414 -186
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var agent = require('@mastra/core/agent');
|
|
4
|
+
var error = require('@mastra/core/error');
|
|
4
5
|
var storage = require('@mastra/core/storage');
|
|
5
6
|
var Cloudflare = require('cloudflare');
|
|
6
7
|
var utils = require('@mastra/core/utils');
|
|
@@ -256,27 +257,39 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
256
257
|
* @param config Configuration for D1 access (either REST API or Workers Binding API)
|
|
257
258
|
*/
|
|
258
259
|
constructor(config) {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
this.tablePrefix = config.tablePrefix || "";
|
|
264
|
-
if ("binding" in config) {
|
|
265
|
-
if (!config.binding) {
|
|
266
|
-
throw new Error("D1 binding is required when using Workers Binding API");
|
|
260
|
+
try {
|
|
261
|
+
super({ name: "D1" });
|
|
262
|
+
if (config.tablePrefix && !/^[a-zA-Z0-9_]*$/.test(config.tablePrefix)) {
|
|
263
|
+
throw new Error("Invalid tablePrefix: only letters, numbers, and underscores are allowed.");
|
|
267
264
|
}
|
|
268
|
-
this.
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
265
|
+
this.tablePrefix = config.tablePrefix || "";
|
|
266
|
+
if ("binding" in config) {
|
|
267
|
+
if (!config.binding) {
|
|
268
|
+
throw new Error("D1 binding is required when using Workers Binding API");
|
|
269
|
+
}
|
|
270
|
+
this.binding = config.binding;
|
|
271
|
+
this.logger.info("Using D1 Workers Binding API");
|
|
272
|
+
} else {
|
|
273
|
+
if (!config.accountId || !config.databaseId || !config.apiToken) {
|
|
274
|
+
throw new Error("accountId, databaseId, and apiToken are required when using REST API");
|
|
275
|
+
}
|
|
276
|
+
this.accountId = config.accountId;
|
|
277
|
+
this.databaseId = config.databaseId;
|
|
278
|
+
this.client = new Cloudflare__default.default({
|
|
279
|
+
apiToken: config.apiToken
|
|
280
|
+
});
|
|
281
|
+
this.logger.info("Using D1 REST API");
|
|
273
282
|
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
283
|
+
} catch (error$1) {
|
|
284
|
+
throw new error.MastraError(
|
|
285
|
+
{
|
|
286
|
+
id: "CLOUDFLARE_D1_STORAGE_INITIALIZATION_ERROR",
|
|
287
|
+
domain: error.ErrorDomain.STORAGE,
|
|
288
|
+
category: error.ErrorCategory.SYSTEM,
|
|
289
|
+
text: "Error initializing D1Store"
|
|
290
|
+
},
|
|
291
|
+
error$1
|
|
292
|
+
);
|
|
280
293
|
}
|
|
281
294
|
}
|
|
282
295
|
// Helper method to get the full table name with prefix
|
|
@@ -468,16 +481,25 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
468
481
|
if (tableName === storage.TABLE_WORKFLOW_SNAPSHOT) {
|
|
469
482
|
tableConstraints.push("UNIQUE (workflow_name, run_id)");
|
|
470
483
|
}
|
|
471
|
-
const query = createSqlBuilder().createTable(fullTableName, columnDefinitions, tableConstraints);
|
|
472
|
-
const { sql, params } = query.build();
|
|
473
484
|
try {
|
|
485
|
+
const query = createSqlBuilder().createTable(fullTableName, columnDefinitions, tableConstraints);
|
|
486
|
+
const { sql, params } = query.build();
|
|
474
487
|
await this.executeQuery({ sql, params });
|
|
475
488
|
this.logger.debug(`Created table ${fullTableName}`);
|
|
476
|
-
} catch (error) {
|
|
489
|
+
} catch (error$1) {
|
|
477
490
|
this.logger.error(`Error creating table ${fullTableName}:`, {
|
|
478
|
-
message: error instanceof Error ? error.message : String(error)
|
|
491
|
+
message: error$1 instanceof Error ? error$1.message : String(error$1)
|
|
479
492
|
});
|
|
480
|
-
throw new
|
|
493
|
+
throw new error.MastraError(
|
|
494
|
+
{
|
|
495
|
+
id: "CLOUDFLARE_D1_STORAGE_CREATE_TABLE_ERROR",
|
|
496
|
+
domain: error.ErrorDomain.STORAGE,
|
|
497
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
498
|
+
text: `Failed to create table ${fullTableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
499
|
+
details: { tableName }
|
|
500
|
+
},
|
|
501
|
+
error$1
|
|
502
|
+
);
|
|
481
503
|
}
|
|
482
504
|
}
|
|
483
505
|
/**
|
|
@@ -506,11 +528,17 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
506
528
|
this.logger.debug(`Added column ${columnName} to table ${fullTableName}`);
|
|
507
529
|
}
|
|
508
530
|
}
|
|
509
|
-
} catch (error) {
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
531
|
+
} catch (error$1) {
|
|
532
|
+
throw new error.MastraError(
|
|
533
|
+
{
|
|
534
|
+
id: "CLOUDFLARE_D1_STORAGE_ALTER_TABLE_ERROR",
|
|
535
|
+
domain: error.ErrorDomain.STORAGE,
|
|
536
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
537
|
+
text: `Failed to alter table ${fullTableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
538
|
+
details: { tableName }
|
|
539
|
+
},
|
|
540
|
+
error$1
|
|
541
|
+
);
|
|
514
542
|
}
|
|
515
543
|
}
|
|
516
544
|
async clearTable({ tableName }) {
|
|
@@ -520,11 +548,17 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
520
548
|
const { sql, params } = query.build();
|
|
521
549
|
await this.executeQuery({ sql, params });
|
|
522
550
|
this.logger.debug(`Cleared table ${fullTableName}`);
|
|
523
|
-
} catch (error) {
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
551
|
+
} catch (error$1) {
|
|
552
|
+
throw new error.MastraError(
|
|
553
|
+
{
|
|
554
|
+
id: "CLOUDFLARE_D1_STORAGE_CLEAR_TABLE_ERROR",
|
|
555
|
+
domain: error.ErrorDomain.STORAGE,
|
|
556
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
557
|
+
text: `Failed to clear table ${fullTableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
558
|
+
details: { tableName }
|
|
559
|
+
},
|
|
560
|
+
error$1
|
|
561
|
+
);
|
|
528
562
|
}
|
|
529
563
|
}
|
|
530
564
|
async processRecord(record) {
|
|
@@ -543,10 +577,17 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
543
577
|
const { sql, params } = query.build();
|
|
544
578
|
try {
|
|
545
579
|
await this.executeQuery({ sql, params });
|
|
546
|
-
} catch (error) {
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
580
|
+
} catch (error$1) {
|
|
581
|
+
throw new error.MastraError(
|
|
582
|
+
{
|
|
583
|
+
id: "CLOUDFLARE_D1_STORAGE_INSERT_ERROR",
|
|
584
|
+
domain: error.ErrorDomain.STORAGE,
|
|
585
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
586
|
+
text: `Failed to insert into ${fullTableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
587
|
+
details: { tableName }
|
|
588
|
+
},
|
|
589
|
+
error$1
|
|
590
|
+
);
|
|
550
591
|
}
|
|
551
592
|
}
|
|
552
593
|
async load({ tableName, keys }) {
|
|
@@ -571,11 +612,17 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
571
612
|
processedResult[key] = this.deserializeValue(value);
|
|
572
613
|
}
|
|
573
614
|
return processedResult;
|
|
574
|
-
} catch (error) {
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
615
|
+
} catch (error$1) {
|
|
616
|
+
throw new error.MastraError(
|
|
617
|
+
{
|
|
618
|
+
id: "CLOUDFLARE_D1_STORAGE_LOAD_ERROR",
|
|
619
|
+
domain: error.ErrorDomain.STORAGE,
|
|
620
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
621
|
+
text: `Failed to load from ${fullTableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
622
|
+
details: { tableName }
|
|
623
|
+
},
|
|
624
|
+
error$1
|
|
625
|
+
);
|
|
579
626
|
}
|
|
580
627
|
}
|
|
581
628
|
async getThreadById({ threadId }) {
|
|
@@ -591,10 +638,19 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
591
638
|
updatedAt: this.ensureDate(thread.updatedAt),
|
|
592
639
|
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
|
|
593
640
|
};
|
|
594
|
-
} catch (error) {
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
641
|
+
} catch (error$1) {
|
|
642
|
+
const mastraError = new error.MastraError(
|
|
643
|
+
{
|
|
644
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_THREAD_BY_ID_ERROR",
|
|
645
|
+
domain: error.ErrorDomain.STORAGE,
|
|
646
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
647
|
+
text: `Error processing thread ${threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
648
|
+
details: { threadId }
|
|
649
|
+
},
|
|
650
|
+
error$1
|
|
651
|
+
);
|
|
652
|
+
this.logger?.error(mastraError.toString());
|
|
653
|
+
this.logger?.trackException(mastraError);
|
|
598
654
|
return null;
|
|
599
655
|
}
|
|
600
656
|
}
|
|
@@ -613,10 +669,19 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
613
669
|
updatedAt: this.ensureDate(thread.updatedAt),
|
|
614
670
|
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
|
|
615
671
|
}));
|
|
616
|
-
} catch (error) {
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
672
|
+
} catch (error$1) {
|
|
673
|
+
const mastraError = new error.MastraError(
|
|
674
|
+
{
|
|
675
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_THREADS_BY_RESOURCE_ID_ERROR",
|
|
676
|
+
domain: error.ErrorDomain.STORAGE,
|
|
677
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
678
|
+
text: `Error getting threads by resourceId ${resourceId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
679
|
+
details: { resourceId }
|
|
680
|
+
},
|
|
681
|
+
error$1
|
|
682
|
+
);
|
|
683
|
+
this.logger?.error(mastraError.toString());
|
|
684
|
+
this.logger?.trackException(mastraError);
|
|
620
685
|
return [];
|
|
621
686
|
}
|
|
622
687
|
}
|
|
@@ -629,19 +694,41 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
629
694
|
updatedAt: this.ensureDate(row.updatedAt),
|
|
630
695
|
metadata: typeof row.metadata === "string" ? JSON.parse(row.metadata || "{}") : row.metadata || {}
|
|
631
696
|
});
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
697
|
+
try {
|
|
698
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("resourceId = ?", resourceId);
|
|
699
|
+
const countResult = await this.executeQuery(countQuery.build());
|
|
700
|
+
const total = Number(countResult?.[0]?.count ?? 0);
|
|
701
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId).orderBy("createdAt", "DESC").limit(perPage).offset(page * perPage);
|
|
702
|
+
const results = await this.executeQuery(selectQuery.build());
|
|
703
|
+
const threads = results.map(mapRowToStorageThreadType);
|
|
704
|
+
return {
|
|
705
|
+
threads,
|
|
706
|
+
total,
|
|
707
|
+
page,
|
|
708
|
+
perPage,
|
|
709
|
+
hasMore: page * perPage + threads.length < total
|
|
710
|
+
};
|
|
711
|
+
} catch (error$1) {
|
|
712
|
+
const mastraError = new error.MastraError(
|
|
713
|
+
{
|
|
714
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_THREADS_BY_RESOURCE_ID_PAGINATED_ERROR",
|
|
715
|
+
domain: error.ErrorDomain.STORAGE,
|
|
716
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
717
|
+
text: `Error getting threads by resourceId ${resourceId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
718
|
+
details: { resourceId }
|
|
719
|
+
},
|
|
720
|
+
error$1
|
|
721
|
+
);
|
|
722
|
+
this.logger?.error(mastraError.toString());
|
|
723
|
+
this.logger?.trackException(mastraError);
|
|
724
|
+
return {
|
|
725
|
+
threads: [],
|
|
726
|
+
total: 0,
|
|
727
|
+
page,
|
|
728
|
+
perPage,
|
|
729
|
+
hasMore: false
|
|
730
|
+
};
|
|
731
|
+
}
|
|
645
732
|
}
|
|
646
733
|
async saveThread({ thread }) {
|
|
647
734
|
const fullTableName = this.getTableName(storage.TABLE_THREADS);
|
|
@@ -668,10 +755,17 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
668
755
|
try {
|
|
669
756
|
await this.executeQuery({ sql, params });
|
|
670
757
|
return thread;
|
|
671
|
-
} catch (error) {
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
758
|
+
} catch (error$1) {
|
|
759
|
+
throw new error.MastraError(
|
|
760
|
+
{
|
|
761
|
+
id: "CLOUDFLARE_D1_STORAGE_SAVE_THREAD_ERROR",
|
|
762
|
+
domain: error.ErrorDomain.STORAGE,
|
|
763
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
764
|
+
text: `Failed to save thread to ${fullTableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
765
|
+
details: { threadId: thread.id }
|
|
766
|
+
},
|
|
767
|
+
error$1
|
|
768
|
+
);
|
|
675
769
|
}
|
|
676
770
|
}
|
|
677
771
|
async updateThread({
|
|
@@ -680,19 +774,19 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
680
774
|
metadata
|
|
681
775
|
}) {
|
|
682
776
|
const thread = await this.getThreadById({ threadId: id });
|
|
683
|
-
if (!thread) {
|
|
684
|
-
throw new Error(`Thread ${id} not found`);
|
|
685
|
-
}
|
|
686
|
-
const fullTableName = this.getTableName(storage.TABLE_THREADS);
|
|
687
|
-
const mergedMetadata = {
|
|
688
|
-
...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
689
|
-
...metadata
|
|
690
|
-
};
|
|
691
|
-
const columns = ["title", "metadata", "updatedAt"];
|
|
692
|
-
const values = [title, JSON.stringify(mergedMetadata), (/* @__PURE__ */ new Date()).toISOString()];
|
|
693
|
-
const query = createSqlBuilder().update(fullTableName, columns, values).where("id = ?", id);
|
|
694
|
-
const { sql, params } = query.build();
|
|
695
777
|
try {
|
|
778
|
+
if (!thread) {
|
|
779
|
+
throw new Error(`Thread ${id} not found`);
|
|
780
|
+
}
|
|
781
|
+
const fullTableName = this.getTableName(storage.TABLE_THREADS);
|
|
782
|
+
const mergedMetadata = {
|
|
783
|
+
...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
784
|
+
...metadata
|
|
785
|
+
};
|
|
786
|
+
const columns = ["title", "metadata", "updatedAt"];
|
|
787
|
+
const values = [title, JSON.stringify(mergedMetadata), (/* @__PURE__ */ new Date()).toISOString()];
|
|
788
|
+
const query = createSqlBuilder().update(fullTableName, columns, values).where("id = ?", id);
|
|
789
|
+
const { sql, params } = query.build();
|
|
696
790
|
await this.executeQuery({ sql, params });
|
|
697
791
|
return {
|
|
698
792
|
...thread,
|
|
@@ -703,10 +797,17 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
703
797
|
},
|
|
704
798
|
updatedAt: /* @__PURE__ */ new Date()
|
|
705
799
|
};
|
|
706
|
-
} catch (error) {
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
800
|
+
} catch (error$1) {
|
|
801
|
+
throw new error.MastraError(
|
|
802
|
+
{
|
|
803
|
+
id: "CLOUDFLARE_D1_STORAGE_UPDATE_THREAD_ERROR",
|
|
804
|
+
domain: error.ErrorDomain.STORAGE,
|
|
805
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
806
|
+
text: `Failed to update thread ${id}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
807
|
+
details: { threadId: id }
|
|
808
|
+
},
|
|
809
|
+
error$1
|
|
810
|
+
);
|
|
710
811
|
}
|
|
711
812
|
}
|
|
712
813
|
async deleteThread({ threadId }) {
|
|
@@ -719,11 +820,17 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
719
820
|
const deleteMessagesQuery = createSqlBuilder().delete(messagesTableName).where("thread_id = ?", threadId);
|
|
720
821
|
const { sql: messagesSql, params: messagesParams } = deleteMessagesQuery.build();
|
|
721
822
|
await this.executeQuery({ sql: messagesSql, params: messagesParams });
|
|
722
|
-
} catch (error) {
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
823
|
+
} catch (error$1) {
|
|
824
|
+
throw new error.MastraError(
|
|
825
|
+
{
|
|
826
|
+
id: "CLOUDFLARE_D1_STORAGE_DELETE_THREAD_ERROR",
|
|
827
|
+
domain: error.ErrorDomain.STORAGE,
|
|
828
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
829
|
+
text: `Failed to delete thread ${threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
830
|
+
details: { threadId }
|
|
831
|
+
},
|
|
832
|
+
error$1
|
|
833
|
+
);
|
|
727
834
|
}
|
|
728
835
|
}
|
|
729
836
|
async saveMessages(args) {
|
|
@@ -769,9 +876,16 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
769
876
|
const list = new agent.MessageList().add(messages, "memory");
|
|
770
877
|
if (format === `v2`) return list.get.all.v2();
|
|
771
878
|
return list.get.all.v1();
|
|
772
|
-
} catch (error) {
|
|
773
|
-
|
|
774
|
-
|
|
879
|
+
} catch (error$1) {
|
|
880
|
+
throw new error.MastraError(
|
|
881
|
+
{
|
|
882
|
+
id: "CLOUDFLARE_D1_STORAGE_SAVE_MESSAGES_ERROR",
|
|
883
|
+
domain: error.ErrorDomain.STORAGE,
|
|
884
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
885
|
+
text: `Failed to save messages: ${error$1 instanceof Error ? error$1.message : String(error$1)}`
|
|
886
|
+
},
|
|
887
|
+
error$1
|
|
888
|
+
);
|
|
775
889
|
}
|
|
776
890
|
}
|
|
777
891
|
async _getIncludedMessages(threadId, selectBy) {
|
|
@@ -826,7 +940,7 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
826
940
|
format
|
|
827
941
|
}) {
|
|
828
942
|
const fullTableName = this.getTableName(storage.TABLE_MESSAGES);
|
|
829
|
-
const limit =
|
|
943
|
+
const limit = this.resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
|
|
830
944
|
const include = selectBy?.include || [];
|
|
831
945
|
const messages = [];
|
|
832
946
|
try {
|
|
@@ -862,12 +976,20 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
862
976
|
const list = new agent.MessageList().add(processedMessages, "memory");
|
|
863
977
|
if (format === `v2`) return list.get.all.v2();
|
|
864
978
|
return list.get.all.v1();
|
|
865
|
-
} catch (error) {
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
979
|
+
} catch (error$1) {
|
|
980
|
+
const mastraError = new error.MastraError(
|
|
981
|
+
{
|
|
982
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_ERROR",
|
|
983
|
+
domain: error.ErrorDomain.STORAGE,
|
|
984
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
985
|
+
text: `Failed to retrieve messages for thread ${threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
986
|
+
details: { threadId }
|
|
987
|
+
},
|
|
988
|
+
error$1
|
|
989
|
+
);
|
|
990
|
+
this.logger?.error(mastraError.toString());
|
|
991
|
+
this.logger?.trackException(mastraError);
|
|
992
|
+
throw mastraError;
|
|
871
993
|
}
|
|
872
994
|
}
|
|
873
995
|
async getMessagesPaginated({
|
|
@@ -879,37 +1001,59 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
879
1001
|
const { start: fromDate, end: toDate } = dateRange || {};
|
|
880
1002
|
const fullTableName = this.getTableName(storage.TABLE_MESSAGES);
|
|
881
1003
|
const messages = [];
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
1004
|
+
try {
|
|
1005
|
+
if (selectBy?.include?.length) {
|
|
1006
|
+
const includeResult = await this._getIncludedMessages(threadId, selectBy);
|
|
1007
|
+
if (Array.isArray(includeResult)) messages.push(...includeResult);
|
|
1008
|
+
}
|
|
1009
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("thread_id = ?", threadId);
|
|
1010
|
+
if (fromDate) {
|
|
1011
|
+
countQuery.andWhere("createdAt >= ?", this.serializeDate(fromDate));
|
|
1012
|
+
}
|
|
1013
|
+
if (toDate) {
|
|
1014
|
+
countQuery.andWhere("createdAt <= ?", this.serializeDate(toDate));
|
|
1015
|
+
}
|
|
1016
|
+
const countResult = await this.executeQuery(countQuery.build());
|
|
1017
|
+
const total = Number(countResult[0]?.count ?? 0);
|
|
1018
|
+
const query = createSqlBuilder().select(["id", "content", "role", "type", "createdAt", "thread_id AS threadId"]).from(fullTableName).where("thread_id = ?", threadId);
|
|
1019
|
+
if (fromDate) {
|
|
1020
|
+
query.andWhere("createdAt >= ?", this.serializeDate(fromDate));
|
|
1021
|
+
}
|
|
1022
|
+
if (toDate) {
|
|
1023
|
+
query.andWhere("createdAt <= ?", this.serializeDate(toDate));
|
|
1024
|
+
}
|
|
1025
|
+
query.orderBy("createdAt", "DESC").limit(perPage).offset(page * perPage);
|
|
1026
|
+
const results = await this.executeQuery(query.build());
|
|
1027
|
+
const list = new agent.MessageList().add(results, "memory");
|
|
1028
|
+
messages.push(...format === `v2` ? list.get.all.v2() : list.get.all.v1());
|
|
1029
|
+
return {
|
|
1030
|
+
messages,
|
|
1031
|
+
total,
|
|
1032
|
+
page,
|
|
1033
|
+
perPage,
|
|
1034
|
+
hasMore: page * perPage + messages.length < total
|
|
1035
|
+
};
|
|
1036
|
+
} catch (error$1) {
|
|
1037
|
+
const mastraError = new error.MastraError(
|
|
1038
|
+
{
|
|
1039
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_PAGINATED_ERROR",
|
|
1040
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1041
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1042
|
+
text: `Failed to retrieve messages for thread ${threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1043
|
+
details: { threadId }
|
|
1044
|
+
},
|
|
1045
|
+
error$1
|
|
1046
|
+
);
|
|
1047
|
+
this.logger?.error(mastraError.toString());
|
|
1048
|
+
this.logger?.trackException(mastraError);
|
|
1049
|
+
return {
|
|
1050
|
+
messages: [],
|
|
1051
|
+
total: 0,
|
|
1052
|
+
page,
|
|
1053
|
+
perPage,
|
|
1054
|
+
hasMore: false
|
|
1055
|
+
};
|
|
901
1056
|
}
|
|
902
|
-
query.orderBy("createdAt", "DESC").limit(perPage).offset(page * perPage);
|
|
903
|
-
const results = await this.executeQuery(query.build());
|
|
904
|
-
const list = new agent.MessageList().add(results, "memory");
|
|
905
|
-
messages.push(...format === `v2` ? list.get.all.v2() : list.get.all.v1());
|
|
906
|
-
return {
|
|
907
|
-
messages,
|
|
908
|
-
total,
|
|
909
|
-
page,
|
|
910
|
-
perPage,
|
|
911
|
-
hasMore: page * perPage + messages.length < total
|
|
912
|
-
};
|
|
913
1057
|
}
|
|
914
1058
|
async persistWorkflowSnapshot({
|
|
915
1059
|
workflowName,
|
|
@@ -945,24 +1089,43 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
945
1089
|
const { sql, params } = query.build();
|
|
946
1090
|
try {
|
|
947
1091
|
await this.executeQuery({ sql, params });
|
|
948
|
-
} catch (error) {
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
1092
|
+
} catch (error$1) {
|
|
1093
|
+
throw new error.MastraError(
|
|
1094
|
+
{
|
|
1095
|
+
id: "CLOUDFLARE_D1_STORAGE_PERSIST_WORKFLOW_SNAPSHOT_ERROR",
|
|
1096
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1097
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1098
|
+
text: `Failed to persist workflow snapshot: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1099
|
+
details: { workflowName, runId }
|
|
1100
|
+
},
|
|
1101
|
+
error$1
|
|
1102
|
+
);
|
|
953
1103
|
}
|
|
954
1104
|
}
|
|
955
1105
|
async loadWorkflowSnapshot(params) {
|
|
956
1106
|
const { workflowName, runId } = params;
|
|
957
1107
|
this.logger.debug("Loading workflow snapshot", { workflowName, runId });
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
1108
|
+
try {
|
|
1109
|
+
const d = await this.load({
|
|
1110
|
+
tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
|
|
1111
|
+
keys: {
|
|
1112
|
+
workflow_name: workflowName,
|
|
1113
|
+
run_id: runId
|
|
1114
|
+
}
|
|
1115
|
+
});
|
|
1116
|
+
return d ? d.snapshot : null;
|
|
1117
|
+
} catch (error$1) {
|
|
1118
|
+
throw new error.MastraError(
|
|
1119
|
+
{
|
|
1120
|
+
id: "CLOUDFLARE_D1_STORAGE_LOAD_WORKFLOW_SNAPSHOT_ERROR",
|
|
1121
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1122
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1123
|
+
text: `Failed to load workflow snapshot: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1124
|
+
details: { workflowName, runId }
|
|
1125
|
+
},
|
|
1126
|
+
error$1
|
|
1127
|
+
);
|
|
1128
|
+
}
|
|
966
1129
|
}
|
|
967
1130
|
/**
|
|
968
1131
|
* Insert multiple records in a batch operation
|
|
@@ -996,11 +1159,17 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
996
1159
|
);
|
|
997
1160
|
}
|
|
998
1161
|
this.logger.debug(`Successfully batch inserted ${records.length} records into ${tableName}`);
|
|
999
|
-
} catch (error) {
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1162
|
+
} catch (error$1) {
|
|
1163
|
+
throw new error.MastraError(
|
|
1164
|
+
{
|
|
1165
|
+
id: "CLOUDFLARE_D1_STORAGE_BATCH_INSERT_ERROR",
|
|
1166
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1167
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1168
|
+
text: `Failed to batch insert into ${tableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1169
|
+
details: { tableName }
|
|
1170
|
+
},
|
|
1171
|
+
error$1
|
|
1172
|
+
);
|
|
1004
1173
|
}
|
|
1005
1174
|
}
|
|
1006
1175
|
/**
|
|
@@ -1048,8 +1217,22 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
1048
1217
|
other: this.deserializeValue(trace.other, "jsonb")
|
|
1049
1218
|
})
|
|
1050
1219
|
) : [];
|
|
1051
|
-
} catch (error) {
|
|
1052
|
-
|
|
1220
|
+
} catch (error$1) {
|
|
1221
|
+
const mastraError = new error.MastraError(
|
|
1222
|
+
{
|
|
1223
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_ERROR",
|
|
1224
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1225
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1226
|
+
text: `Failed to retrieve traces: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1227
|
+
details: {
|
|
1228
|
+
name: name ?? "",
|
|
1229
|
+
scope: scope ?? ""
|
|
1230
|
+
}
|
|
1231
|
+
},
|
|
1232
|
+
error$1
|
|
1233
|
+
);
|
|
1234
|
+
this.logger?.error(mastraError.toString());
|
|
1235
|
+
this.logger?.trackException(mastraError);
|
|
1053
1236
|
return [];
|
|
1054
1237
|
}
|
|
1055
1238
|
}
|
|
@@ -1104,8 +1287,19 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
1104
1287
|
perPage,
|
|
1105
1288
|
hasMore: page * perPage + traces.length < total
|
|
1106
1289
|
};
|
|
1107
|
-
} catch (error) {
|
|
1108
|
-
|
|
1290
|
+
} catch (error$1) {
|
|
1291
|
+
const mastraError = new error.MastraError(
|
|
1292
|
+
{
|
|
1293
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_ERROR",
|
|
1294
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1295
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1296
|
+
text: `Failed to retrieve traces: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1297
|
+
details: { name: name ?? "", scope: scope ?? "" }
|
|
1298
|
+
},
|
|
1299
|
+
error$1
|
|
1300
|
+
);
|
|
1301
|
+
this.logger?.error(mastraError.toString());
|
|
1302
|
+
this.logger?.trackException(mastraError);
|
|
1109
1303
|
return { traces: [], total: 0, page, perPage, hasMore: false };
|
|
1110
1304
|
}
|
|
1111
1305
|
}
|
|
@@ -1140,10 +1334,19 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
1140
1334
|
testInfo
|
|
1141
1335
|
};
|
|
1142
1336
|
}) : [];
|
|
1143
|
-
} catch (error) {
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1337
|
+
} catch (error$1) {
|
|
1338
|
+
const mastraError = new error.MastraError(
|
|
1339
|
+
{
|
|
1340
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
|
|
1341
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1342
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1343
|
+
text: `Failed to retrieve evals for agent ${agentName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1344
|
+
details: { agentName }
|
|
1345
|
+
},
|
|
1346
|
+
error$1
|
|
1347
|
+
);
|
|
1348
|
+
this.logger?.error(mastraError.toString());
|
|
1349
|
+
this.logger?.trackException(mastraError);
|
|
1147
1350
|
return [];
|
|
1148
1351
|
}
|
|
1149
1352
|
}
|
|
@@ -1174,52 +1377,65 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
1174
1377
|
countQueryBuilder.where(conditions.join(" AND "), ...queryParams);
|
|
1175
1378
|
}
|
|
1176
1379
|
const { sql: countSql, params: countParams } = countQueryBuilder.build();
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1380
|
+
try {
|
|
1381
|
+
const countResult = await this.executeQuery({ sql: countSql, params: countParams, first: true });
|
|
1382
|
+
const total = Number(countResult?.count || 0);
|
|
1383
|
+
const currentOffset = page * perPage;
|
|
1384
|
+
if (total === 0) {
|
|
1385
|
+
return {
|
|
1386
|
+
evals: [],
|
|
1387
|
+
total: 0,
|
|
1388
|
+
page,
|
|
1389
|
+
perPage,
|
|
1390
|
+
hasMore: false
|
|
1391
|
+
};
|
|
1392
|
+
}
|
|
1393
|
+
const dataQueryBuilder = createSqlBuilder().select("*").from(fullTableName);
|
|
1394
|
+
if (conditions.length > 0) {
|
|
1395
|
+
dataQueryBuilder.where(conditions.join(" AND "), ...queryParams);
|
|
1396
|
+
}
|
|
1397
|
+
dataQueryBuilder.orderBy("createdAt", "DESC").limit(perPage).offset(currentOffset);
|
|
1398
|
+
const { sql: dataSql, params: dataParams } = dataQueryBuilder.build();
|
|
1399
|
+
const rows = await this.executeQuery({ sql: dataSql, params: dataParams });
|
|
1400
|
+
const evals = (isArrayOfRecords(rows) ? rows : []).map((row) => {
|
|
1401
|
+
const result = this.deserializeValue(row.result);
|
|
1402
|
+
const testInfo = row.test_info ? this.deserializeValue(row.test_info) : void 0;
|
|
1403
|
+
if (!result || typeof result !== "object" || !("score" in result)) {
|
|
1404
|
+
throw new Error(`Invalid MetricResult format: ${JSON.stringify(result)}`);
|
|
1405
|
+
}
|
|
1406
|
+
return {
|
|
1407
|
+
input: row.input,
|
|
1408
|
+
output: row.output,
|
|
1409
|
+
result,
|
|
1410
|
+
agentName: row.agent_name,
|
|
1411
|
+
metricName: row.metric_name,
|
|
1412
|
+
instructions: row.instructions,
|
|
1413
|
+
testInfo,
|
|
1414
|
+
globalRunId: row.global_run_id,
|
|
1415
|
+
runId: row.run_id,
|
|
1416
|
+
createdAt: row.createdAt
|
|
1417
|
+
};
|
|
1418
|
+
});
|
|
1419
|
+
const hasMore = currentOffset + evals.length < total;
|
|
1181
1420
|
return {
|
|
1182
|
-
evals
|
|
1183
|
-
total
|
|
1421
|
+
evals,
|
|
1422
|
+
total,
|
|
1184
1423
|
page,
|
|
1185
1424
|
perPage,
|
|
1186
|
-
hasMore
|
|
1425
|
+
hasMore
|
|
1187
1426
|
};
|
|
1427
|
+
} catch (error$1) {
|
|
1428
|
+
throw new error.MastraError(
|
|
1429
|
+
{
|
|
1430
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
|
|
1431
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1432
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1433
|
+
text: `Failed to retrieve evals for agent ${agentName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1434
|
+
details: { agentName: agentName ?? "", type: type ?? "" }
|
|
1435
|
+
},
|
|
1436
|
+
error$1
|
|
1437
|
+
);
|
|
1188
1438
|
}
|
|
1189
|
-
const dataQueryBuilder = createSqlBuilder().select("*").from(fullTableName);
|
|
1190
|
-
if (conditions.length > 0) {
|
|
1191
|
-
dataQueryBuilder.where(conditions.join(" AND "), ...queryParams);
|
|
1192
|
-
}
|
|
1193
|
-
dataQueryBuilder.orderBy("createdAt", "DESC").limit(perPage).offset(currentOffset);
|
|
1194
|
-
const { sql: dataSql, params: dataParams } = dataQueryBuilder.build();
|
|
1195
|
-
const rows = await this.executeQuery({ sql: dataSql, params: dataParams });
|
|
1196
|
-
const evals = (isArrayOfRecords(rows) ? rows : []).map((row) => {
|
|
1197
|
-
const result = this.deserializeValue(row.result);
|
|
1198
|
-
const testInfo = row.test_info ? this.deserializeValue(row.test_info) : void 0;
|
|
1199
|
-
if (!result || typeof result !== "object" || !("score" in result)) {
|
|
1200
|
-
throw new Error(`Invalid MetricResult format: ${JSON.stringify(result)}`);
|
|
1201
|
-
}
|
|
1202
|
-
return {
|
|
1203
|
-
input: row.input,
|
|
1204
|
-
output: row.output,
|
|
1205
|
-
result,
|
|
1206
|
-
agentName: row.agent_name,
|
|
1207
|
-
metricName: row.metric_name,
|
|
1208
|
-
instructions: row.instructions,
|
|
1209
|
-
testInfo,
|
|
1210
|
-
globalRunId: row.global_run_id,
|
|
1211
|
-
runId: row.run_id,
|
|
1212
|
-
createdAt: row.createdAt
|
|
1213
|
-
};
|
|
1214
|
-
});
|
|
1215
|
-
const hasMore = currentOffset + evals.length < total;
|
|
1216
|
-
return {
|
|
1217
|
-
evals,
|
|
1218
|
-
total,
|
|
1219
|
-
page,
|
|
1220
|
-
perPage,
|
|
1221
|
-
hasMore
|
|
1222
|
-
};
|
|
1223
1439
|
}
|
|
1224
1440
|
parseWorkflowRun(row) {
|
|
1225
1441
|
let parsedSnapshot = row.snapshot;
|
|
@@ -1288,11 +1504,17 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
1288
1504
|
const results = await this.executeQuery({ sql, params });
|
|
1289
1505
|
const runs = (isArrayOfRecords(results) ? results : []).map((row) => this.parseWorkflowRun(row));
|
|
1290
1506
|
return { runs, total: total || runs.length };
|
|
1291
|
-
} catch (error) {
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1507
|
+
} catch (error$1) {
|
|
1508
|
+
throw new error.MastraError(
|
|
1509
|
+
{
|
|
1510
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_WORKFLOW_RUNS_ERROR",
|
|
1511
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1512
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1513
|
+
text: `Failed to retrieve workflow runs: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1514
|
+
details: { workflowName: workflowName ?? "", resourceId: resourceId ?? "" }
|
|
1515
|
+
},
|
|
1516
|
+
error$1
|
|
1517
|
+
);
|
|
1296
1518
|
}
|
|
1297
1519
|
}
|
|
1298
1520
|
async getWorkflowRunById({
|
|
@@ -1316,11 +1538,17 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
1316
1538
|
const result = await this.executeQuery({ sql, params, first: true });
|
|
1317
1539
|
if (!result) return null;
|
|
1318
1540
|
return this.parseWorkflowRun(result);
|
|
1319
|
-
} catch (error) {
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1541
|
+
} catch (error$1) {
|
|
1542
|
+
throw new error.MastraError(
|
|
1543
|
+
{
|
|
1544
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_WORKFLOW_RUN_BY_ID_ERROR",
|
|
1545
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1546
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1547
|
+
text: `Failed to retrieve workflow run by ID: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1548
|
+
details: { runId, workflowName: workflowName ?? "" }
|
|
1549
|
+
},
|
|
1550
|
+
error$1
|
|
1551
|
+
);
|
|
1324
1552
|
}
|
|
1325
1553
|
}
|
|
1326
1554
|
/**
|