@mastra/cloudflare 1.3.1 → 1.3.2-alpha.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 (33) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/{chunk-ZBYNKKG6.cjs → chunk-3SMRLX2Z.cjs} +141 -6
  3. package/dist/chunk-3SMRLX2Z.cjs.map +1 -0
  4. package/dist/{chunk-NSFHQATX.js → chunk-7THHWTAZ.js} +276 -7
  5. package/dist/chunk-7THHWTAZ.js.map +1 -0
  6. package/dist/{chunk-O57GFJSB.js → chunk-HJZCD37D.js} +142 -8
  7. package/dist/chunk-HJZCD37D.js.map +1 -0
  8. package/dist/{chunk-E4MARS3A.cjs → chunk-T2WSIEXQ.cjs} +275 -5
  9. package/dist/chunk-T2WSIEXQ.cjs.map +1 -0
  10. package/dist/do/index.cjs +11 -7
  11. package/dist/do/index.d.ts +2 -1
  12. package/dist/do/index.d.ts.map +1 -1
  13. package/dist/do/index.js +1 -1
  14. package/dist/do/storage/domains/background-tasks/index.d.ts +18 -0
  15. package/dist/do/storage/domains/background-tasks/index.d.ts.map +1 -0
  16. package/dist/docs/SKILL.md +1 -1
  17. package/dist/docs/assets/SOURCE_MAP.json +20 -20
  18. package/dist/docs/references/reference-storage-cloudflare.md +3 -3
  19. package/dist/index.cjs +13 -13
  20. package/dist/index.js +2 -2
  21. package/dist/kv/index.cjs +10 -6
  22. package/dist/kv/index.d.ts +2 -1
  23. package/dist/kv/index.d.ts.map +1 -1
  24. package/dist/kv/index.js +1 -1
  25. package/dist/kv/storage/domains/background-tasks/index.d.ts +17 -0
  26. package/dist/kv/storage/domains/background-tasks/index.d.ts.map +1 -0
  27. package/dist/kv/storage/types.d.ts +1 -0
  28. package/dist/kv/storage/types.d.ts.map +1 -1
  29. package/package.json +7 -7
  30. package/dist/chunk-E4MARS3A.cjs.map +0 -1
  31. package/dist/chunk-NSFHQATX.js.map +0 -1
  32. package/dist/chunk-O57GFJSB.js.map +0 -1
  33. package/dist/chunk-ZBYNKKG6.cjs.map +0 -1
@@ -2,9 +2,9 @@
2
2
 
3
3
  var error = require('@mastra/core/error');
4
4
  var storage = require('@mastra/core/storage');
5
- var agent = require('@mastra/core/agent');
6
5
  var base = require('@mastra/core/base');
7
6
  var utils = require('@mastra/core/utils');
7
+ var agent = require('@mastra/core/agent');
8
8
  var evals = require('@mastra/core/evals');
9
9
 
10
10
  // src/do/index.ts
@@ -659,7 +659,275 @@ var DODB = class extends base.MastraBase {
659
659
  }
660
660
  };
661
661
 
662
- // src/do/storage/domains/memory/index.ts
662
+ // src/do/storage/domains/background-tasks/index.ts
663
+ function serializeJson(v) {
664
+ if (typeof v === "object" && v != null) return JSON.stringify(v);
665
+ return v ?? void 0;
666
+ }
667
+ function rowToTask(row) {
668
+ const parseJson = (v) => {
669
+ if (v == null) return void 0;
670
+ if (typeof v === "string") {
671
+ try {
672
+ return JSON.parse(v);
673
+ } catch {
674
+ return v;
675
+ }
676
+ }
677
+ return v;
678
+ };
679
+ const asDate = (v) => v ? new Date(String(v)) : void 0;
680
+ return {
681
+ id: String(row.id),
682
+ status: String(row.status),
683
+ toolName: String(row.tool_name),
684
+ toolCallId: String(row.tool_call_id),
685
+ args: parseJson(row.args) ?? {},
686
+ agentId: String(row.agent_id),
687
+ threadId: row.thread_id != null ? String(row.thread_id) : void 0,
688
+ resourceId: row.resource_id != null ? String(row.resource_id) : void 0,
689
+ runId: String(row.run_id),
690
+ result: parseJson(row.result),
691
+ error: parseJson(row.error),
692
+ retryCount: Number(row.retry_count ?? 0),
693
+ maxRetries: Number(row.max_retries ?? 0),
694
+ timeoutMs: Number(row.timeout_ms ?? 3e5),
695
+ createdAt: asDate(row.createdAt) ?? /* @__PURE__ */ new Date(),
696
+ startedAt: asDate(row.startedAt),
697
+ completedAt: asDate(row.completedAt)
698
+ };
699
+ }
700
+ function dateColumnName(col) {
701
+ return col;
702
+ }
703
+ var BackgroundTasksStorageDO = class extends storage.BackgroundTasksStorage {
704
+ #db;
705
+ constructor(config) {
706
+ super();
707
+ this.#db = new DODB(config);
708
+ }
709
+ async init() {
710
+ await this.#db.createTable({
711
+ tableName: storage.TABLE_BACKGROUND_TASKS,
712
+ schema: storage.TABLE_SCHEMAS[storage.TABLE_BACKGROUND_TASKS]
713
+ });
714
+ }
715
+ async dangerouslyClearAll() {
716
+ await this.#db.clearTable({ tableName: storage.TABLE_BACKGROUND_TASKS });
717
+ }
718
+ async createTask(task) {
719
+ try {
720
+ await this.#db.insert({
721
+ tableName: storage.TABLE_BACKGROUND_TASKS,
722
+ record: {
723
+ id: task.id,
724
+ tool_call_id: task.toolCallId,
725
+ tool_name: task.toolName,
726
+ agent_id: task.agentId,
727
+ thread_id: task.threadId ?? null,
728
+ resource_id: task.resourceId ?? null,
729
+ run_id: task.runId,
730
+ status: task.status,
731
+ args: serializeJson(task.args),
732
+ result: serializeJson(task.result),
733
+ error: serializeJson(task.error),
734
+ retry_count: task.retryCount,
735
+ max_retries: task.maxRetries,
736
+ timeout_ms: task.timeoutMs,
737
+ createdAt: task.createdAt.toISOString(),
738
+ startedAt: task.startedAt?.toISOString() ?? null,
739
+ completedAt: task.completedAt?.toISOString() ?? null
740
+ }
741
+ });
742
+ } catch (error$1) {
743
+ throw new error.MastraError(
744
+ {
745
+ id: storage.createStorageErrorId("CLOUDFLARE_DO", "BACKGROUND_TASKS_CREATE", "FAILED"),
746
+ domain: error.ErrorDomain.STORAGE,
747
+ category: error.ErrorCategory.THIRD_PARTY
748
+ },
749
+ error$1
750
+ );
751
+ }
752
+ }
753
+ async updateTask(taskId, update) {
754
+ const columns = [];
755
+ const values = [];
756
+ if ("status" in update) {
757
+ columns.push("status");
758
+ values.push(update.status);
759
+ }
760
+ if ("result" in update) {
761
+ columns.push("result");
762
+ values.push(serializeJson(update.result));
763
+ }
764
+ if ("error" in update) {
765
+ columns.push("error");
766
+ values.push(serializeJson(update.error));
767
+ }
768
+ if ("retryCount" in update) {
769
+ columns.push("retry_count");
770
+ values.push(update.retryCount);
771
+ }
772
+ if ("startedAt" in update) {
773
+ columns.push("startedAt");
774
+ values.push(update.startedAt?.toISOString() ?? null);
775
+ }
776
+ if ("completedAt" in update) {
777
+ columns.push("completedAt");
778
+ values.push(update.completedAt?.toISOString() ?? null);
779
+ }
780
+ if (columns.length === 0) return;
781
+ try {
782
+ const fullTableName = this.#db.getTableName(storage.TABLE_BACKGROUND_TASKS);
783
+ const query = createSqlBuilder().update(fullTableName, columns, values).where("id = ?", taskId);
784
+ const { sql, params } = query.build();
785
+ await this.#db.executeQuery({ sql, params });
786
+ } catch (error$1) {
787
+ throw new error.MastraError(
788
+ {
789
+ id: storage.createStorageErrorId("CLOUDFLARE_DO", "BACKGROUND_TASKS_UPDATE", "FAILED"),
790
+ domain: error.ErrorDomain.STORAGE,
791
+ category: error.ErrorCategory.THIRD_PARTY
792
+ },
793
+ error$1
794
+ );
795
+ }
796
+ }
797
+ async getTask(taskId) {
798
+ try {
799
+ const fullTableName = this.#db.getTableName(storage.TABLE_BACKGROUND_TASKS);
800
+ const query = createSqlBuilder().select("*").from(fullTableName).where("id = ?", taskId);
801
+ const { sql, params } = query.build();
802
+ const result = await this.#db.executeQuery({ sql, params, first: true });
803
+ if (!result) return null;
804
+ const deserialized = {};
805
+ for (const [k, v] of Object.entries(result)) {
806
+ deserialized[k] = deserializeValue(v);
807
+ }
808
+ return rowToTask(deserialized);
809
+ } catch (error$1) {
810
+ throw new error.MastraError(
811
+ {
812
+ id: storage.createStorageErrorId("CLOUDFLARE_DO", "BACKGROUND_TASKS_GET", "FAILED"),
813
+ domain: error.ErrorDomain.STORAGE,
814
+ category: error.ErrorCategory.THIRD_PARTY
815
+ },
816
+ error$1
817
+ );
818
+ }
819
+ }
820
+ async listTasks(filter) {
821
+ try {
822
+ const fullTableName = this.#db.getTableName(storage.TABLE_BACKGROUND_TASKS);
823
+ const applyConditions = (builder) => {
824
+ if (filter.status) {
825
+ const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
826
+ const placeholders = statuses.map(() => "?").join(", ");
827
+ builder.whereAnd(`status IN (${placeholders})`, ...statuses);
828
+ }
829
+ if (filter.agentId) builder.whereAnd("agent_id = ?", filter.agentId);
830
+ if (filter.threadId) builder.whereAnd("thread_id = ?", filter.threadId);
831
+ if (filter.resourceId) builder.whereAnd("resource_id = ?", filter.resourceId);
832
+ if (filter.toolName) builder.whereAnd("tool_name = ?", filter.toolName);
833
+ if (filter.runId) builder.whereAnd("run_id = ?", filter.runId);
834
+ const dateCol = dateColumnName(filter.dateFilterBy ?? "createdAt");
835
+ if (filter.fromDate) builder.whereAnd(`${dateCol} >= ?`, filter.fromDate.toISOString());
836
+ if (filter.toDate) builder.whereAnd(`${dateCol} < ?`, filter.toDate.toISOString());
837
+ };
838
+ const countQuery = createSqlBuilder().count().from(fullTableName);
839
+ applyConditions(countQuery);
840
+ const { sql: countSql, params: countParams } = countQuery.build();
841
+ const countResult = await this.#db.executeQuery({ sql: countSql, params: countParams, first: true });
842
+ const total = Number(countResult?.count ?? 0);
843
+ const dataQuery = createSqlBuilder().select("*").from(fullTableName);
844
+ applyConditions(dataQuery);
845
+ const orderBy = dateColumnName(filter.orderBy ?? "createdAt");
846
+ const direction = filter.orderDirection === "desc" ? "DESC" : "ASC";
847
+ dataQuery.orderBy(orderBy, direction);
848
+ if (filter.perPage != null) {
849
+ dataQuery.limit(filter.perPage);
850
+ if (filter.page != null) {
851
+ dataQuery.offset(filter.page * filter.perPage);
852
+ }
853
+ }
854
+ const { sql, params } = dataQuery.build();
855
+ const rows = await this.#db.executeQuery({ sql, params });
856
+ const tasks = (rows ?? []).map((row) => {
857
+ const deserialized = {};
858
+ for (const [k, v] of Object.entries(row)) {
859
+ deserialized[k] = deserializeValue(v);
860
+ }
861
+ return rowToTask(deserialized);
862
+ });
863
+ return { tasks, total };
864
+ } catch (error$1) {
865
+ throw new error.MastraError(
866
+ {
867
+ id: storage.createStorageErrorId("CLOUDFLARE_DO", "BACKGROUND_TASKS_LIST", "FAILED"),
868
+ domain: error.ErrorDomain.STORAGE,
869
+ category: error.ErrorCategory.THIRD_PARTY
870
+ },
871
+ error$1
872
+ );
873
+ }
874
+ }
875
+ async deleteTask(taskId) {
876
+ try {
877
+ const fullTableName = this.#db.getTableName(storage.TABLE_BACKGROUND_TASKS);
878
+ const query = createSqlBuilder().delete(fullTableName).where("id = ?", taskId);
879
+ const { sql, params } = query.build();
880
+ await this.#db.executeQuery({ sql, params });
881
+ } catch (error$1) {
882
+ throw new error.MastraError(
883
+ {
884
+ id: storage.createStorageErrorId("CLOUDFLARE_DO", "BACKGROUND_TASKS_DELETE", "FAILED"),
885
+ domain: error.ErrorDomain.STORAGE,
886
+ category: error.ErrorCategory.THIRD_PARTY
887
+ },
888
+ error$1
889
+ );
890
+ }
891
+ }
892
+ async deleteTasks(filter) {
893
+ try {
894
+ const fullTableName = this.#db.getTableName(storage.TABLE_BACKGROUND_TASKS);
895
+ const query = createSqlBuilder().delete(fullTableName);
896
+ if (filter.status) {
897
+ const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
898
+ const placeholders = statuses.map(() => "?").join(", ");
899
+ query.whereAnd(`status IN (${placeholders})`, ...statuses);
900
+ }
901
+ if (filter.agentId) query.whereAnd("agent_id = ?", filter.agentId);
902
+ if (filter.threadId) query.whereAnd("thread_id = ?", filter.threadId);
903
+ if (filter.resourceId) query.whereAnd("resource_id = ?", filter.resourceId);
904
+ if (filter.toolName) query.whereAnd("tool_name = ?", filter.toolName);
905
+ if (filter.runId) query.whereAnd("run_id = ?", filter.runId);
906
+ const dateCol = dateColumnName(filter.dateFilterBy ?? "createdAt");
907
+ if (filter.fromDate) query.whereAnd(`${dateCol} >= ?`, filter.fromDate.toISOString());
908
+ if (filter.toDate) query.whereAnd(`${dateCol} < ?`, filter.toDate.toISOString());
909
+ const { sql, params } = query.build();
910
+ await this.#db.executeQuery({ sql, params });
911
+ } catch (error$1) {
912
+ throw new error.MastraError(
913
+ {
914
+ id: storage.createStorageErrorId("CLOUDFLARE_DO", "BACKGROUND_TASKS_DELETE_MANY", "FAILED"),
915
+ domain: error.ErrorDomain.STORAGE,
916
+ category: error.ErrorCategory.THIRD_PARTY
917
+ },
918
+ error$1
919
+ );
920
+ }
921
+ }
922
+ async getRunningCount() {
923
+ const { total } = await this.listTasks({ status: "running" });
924
+ return total;
925
+ }
926
+ async getRunningCountByAgent(agentId) {
927
+ const { total } = await this.listTasks({ status: "running", agentId });
928
+ return total;
929
+ }
930
+ };
663
931
  var MemoryStorageDO = class extends storage.MemoryStorage {
664
932
  #db;
665
933
  constructor(config) {
@@ -2100,7 +2368,8 @@ var CloudflareDOStorage = class extends storage.MastraCompositeStore {
2100
2368
  this.stores = {
2101
2369
  memory: new MemoryStorageDO(domainConfig),
2102
2370
  workflows: new WorkflowsStorageDO(domainConfig),
2103
- scores: new ScoresStorageDO(domainConfig)
2371
+ scores: new ScoresStorageDO(domainConfig),
2372
+ backgroundTasks: new BackgroundTasksStorageDO(domainConfig)
2104
2373
  };
2105
2374
  this.logger.info("Using Durable Objects SqlStorage");
2106
2375
  } catch (error$1) {
@@ -2125,11 +2394,12 @@ var CloudflareDOStorage = class extends storage.MastraCompositeStore {
2125
2394
  };
2126
2395
  var DOStore = CloudflareDOStorage;
2127
2396
 
2397
+ exports.BackgroundTasksStorageDO = BackgroundTasksStorageDO;
2128
2398
  exports.CloudflareDOStorage = CloudflareDOStorage;
2129
2399
  exports.DODB = DODB;
2130
2400
  exports.DOStore = DOStore;
2131
2401
  exports.MemoryStorageDO = MemoryStorageDO;
2132
2402
  exports.ScoresStorageDO = ScoresStorageDO;
2133
2403
  exports.WorkflowsStorageDO = WorkflowsStorageDO;
2134
- //# sourceMappingURL=chunk-E4MARS3A.cjs.map
2135
- //# sourceMappingURL=chunk-E4MARS3A.cjs.map
2404
+ //# sourceMappingURL=chunk-T2WSIEXQ.cjs.map
2405
+ //# sourceMappingURL=chunk-T2WSIEXQ.cjs.map