@mastra/cloudflare-d1 1.0.5-alpha.0 → 1.0.6-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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @mastra/cloudflare-d1
2
2
 
3
+ ## 1.0.6-alpha.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Track `suspendedAt` and `suspendPayload` on background tasks. SQL adapters auto-migrate the new columns via `alterTable`. ([#16260](https://github.com/mastra-ai/mastra/pull/16260))
8
+
9
+ - Updated dependencies [[`9f17410`](https://github.com/mastra-ai/mastra/commit/9f1741080def23d42ee50b39887a385ae316a3c6), [`c6eb39e`](https://github.com/mastra-ai/mastra/commit/c6eb39ea6dca381c6563cb240237fbe608e02f93), [`900d086`](https://github.com/mastra-ai/mastra/commit/900d086bb737b9cf2fcf68f11b0389b801a2738c), [`4c0e286`](https://github.com/mastra-ai/mastra/commit/4c0e28637c9cfb4f416549b55e97ebfa13319dfc), [`25184ff`](https://github.com/mastra-ai/mastra/commit/25184ffaf1293ec95119426eb1a1f8d38831b96c), [`aebde9c`](https://github.com/mastra-ai/mastra/commit/aebde9cfacf56592c6b6350cae721740fe090b8a)]:
10
+ - @mastra/core@1.33.0-alpha.4
11
+
12
+ ## 1.0.5
13
+
14
+ ### Patch Changes
15
+
16
+ - Add `BackgroundTasksStorage` domain implementation so `@mastra/core` background task execution works with any storage adapter. ([#15307](https://github.com/mastra-ai/mastra/pull/15307))
17
+
18
+ - Updated dependencies [[`20f59b8`](https://github.com/mastra-ai/mastra/commit/20f59b876cf91199efbc49a0e36b391240708f08), [`aba393e`](https://github.com/mastra-ai/mastra/commit/aba393e2da7390c69b80e516a4f153cda6f09376), [`3d83d06`](https://github.com/mastra-ai/mastra/commit/3d83d06f776f00fb5f4163dddd32a030c5c20844), [`e2687a7`](https://github.com/mastra-ai/mastra/commit/e2687a7408790c384563816a9a28ed06735684c9), [`fdd54cf`](https://github.com/mastra-ai/mastra/commit/fdd54cf612a9af876e9fdd85e534454f6e7dd518), [`6315317`](https://github.com/mastra-ai/mastra/commit/63153175fe9a7b224e5be7c209bbebc01dd9b0d5), [`a371ac5`](https://github.com/mastra-ai/mastra/commit/a371ac534aa1bb368a1acf9d8b313378dfdc787e), [`0474c2b`](https://github.com/mastra-ai/mastra/commit/0474c2b2e7c7e1ad8691dca031284841391ff1ef), [`0a5fa1d`](https://github.com/mastra-ai/mastra/commit/0a5fa1d3cb0583889d06687155f26fd7d2edc76c), [`7e0e63e`](https://github.com/mastra-ai/mastra/commit/7e0e63e2e485e84442351f4c7a79a424c83539dc), [`ea43e64`](https://github.com/mastra-ai/mastra/commit/ea43e646dd95d507694b6112b0bf1df22ad552b2), [`f607106`](https://github.com/mastra-ai/mastra/commit/f607106854c6416c4a07d4082604b9f66d047221), [`30456b6`](https://github.com/mastra-ai/mastra/commit/30456b6b08c8fd17e109dd093b73d93b65e83bc5), [`9d11a8c`](https://github.com/mastra-ai/mastra/commit/9d11a8c1c8924eb975a245a5884d40ca1b7e0491), [`9d3b24b`](https://github.com/mastra-ai/mastra/commit/9d3b24b19407ae9c09586cf7766d38dc4dff4a69), [`00d1b16`](https://github.com/mastra-ai/mastra/commit/00d1b16b401199cb294fa23f43336547db4dca9b), [`47cee3e`](https://github.com/mastra-ai/mastra/commit/47cee3e137fe39109cf7fffd2a8cf47b76dc702e), [`62919a6`](https://github.com/mastra-ai/mastra/commit/62919a6ee0fbf3779ad21a97b1ec6696515d5104), [`d246696`](https://github.com/mastra-ai/mastra/commit/d246696139a3144a5b21b042d41c532688e957e1), [`354f9ce`](https://github.com/mastra-ai/mastra/commit/354f9ce1ca6af2074b6a196a23f8ec30012dccca), [`16e34ca`](https://github.com/mastra-ai/mastra/commit/16e34caa98b9a114b17a6125e4e3fd87f169d0d0), [`7020c06`](https://github.com/mastra-ai/mastra/commit/7020c0690b199d9da337f0e805f16948e557922e), [`8786a61`](https://github.com/mastra-ai/mastra/commit/8786a61fa54ba265f85eeff9985ca39863d18bb6), [`9467ea8`](https://github.com/mastra-ai/mastra/commit/9467ea87695749a53dfc041576410ebf9ee7bb67), [`7338d94`](https://github.com/mastra-ai/mastra/commit/7338d949380cf68b095342e8e42610dc51d557c1), [`c80dc16`](https://github.com/mastra-ai/mastra/commit/c80dc16e113e6cc159f510ffde501ad4711b2189), [`af8a57e`](https://github.com/mastra-ai/mastra/commit/af8a57ed9ba9685ad8601d5b71ae3706da6222f9), [`d63ffdb`](https://github.com/mastra-ai/mastra/commit/d63ffdbb2c11e76fe5ea45faab44bc15460f010c), [`47cee3e`](https://github.com/mastra-ai/mastra/commit/47cee3e137fe39109cf7fffd2a8cf47b76dc702e), [`1bd5104`](https://github.com/mastra-ai/mastra/commit/1bd51048b6da93507276d6623e3fd96a9e1a8944), [`e9837b5`](https://github.com/mastra-ai/mastra/commit/e9837b53699e18711b09e0ca010a4106376f2653), [`8f1b280`](https://github.com/mastra-ai/mastra/commit/8f1b280b7fe6999ec654f160cb69c1a8719e7a57), [`92dcf02`](https://github.com/mastra-ai/mastra/commit/92dcf029294210ac91b090900c1a0555a425c57a), [`0fd90a2`](https://github.com/mastra-ai/mastra/commit/0fd90a215caf5fca8099c15a67ca03e4427747a3), [`8fb2405`](https://github.com/mastra-ai/mastra/commit/8fb2405138f2d208b7962ad03f121ca25bcc28c5), [`12df98c`](https://github.com/mastra-ai/mastra/commit/12df98c4904643d9481f5c78f3bed443725b4c96)]:
19
+ - @mastra/core@1.26.0
20
+
3
21
  ## 1.0.5-alpha.0
4
22
 
5
23
  ### Patch Changes
@@ -3,7 +3,7 @@ name: mastra-cloudflare-d1
3
3
  description: Documentation for @mastra/cloudflare-d1. Use when working with @mastra/cloudflare-d1 APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/cloudflare-d1"
6
- version: "1.0.5-alpha.0"
6
+ version: "1.0.6-alpha.0"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.5-alpha.0",
2
+ "version": "1.0.6-alpha.0",
3
3
  "package": "@mastra/cloudflare-d1",
4
4
  "exports": {},
5
5
  "modules": {}
@@ -34,59 +34,59 @@ bun add @mastra/cloudflare-d1@latest
34
34
 
35
35
  ## Usage
36
36
 
37
- ### Using with Cloudflare Workers
37
+ ### Using with Mastra CloudflareDeployer
38
38
 
39
- When using D1Store in a Cloudflare Worker, you need to access the D1 binding from the worker's `env` parameter at runtime. The `D1Database` in your type definition is only for TypeScript type checking—the actual binding is provided by the Workers runtime.
39
+ The standard way to use D1Store with Mastra on Cloudflare is with `CloudflareDeployer`. Import `env` from `cloudflare:workers` and initialize `D1Store` inline inside `new Mastra({...})`.
40
40
 
41
41
  ```typescript
42
+ import { env } from 'cloudflare:workers'
42
43
  import { D1Store } from '@mastra/cloudflare-d1'
43
44
  import { Mastra } from '@mastra/core'
44
45
  import { CloudflareDeployer } from '@mastra/deployer-cloudflare'
45
46
 
46
- type Env = {
47
- D1Database: D1Database // TypeScript type definition
48
- }
47
+ export const mastra = new Mastra({
48
+ storage: new D1Store({ binding: env.DB }),
49
+ deployer: new CloudflareDeployer({
50
+ name: 'my-worker',
51
+ d1_databases: [
52
+ {
53
+ binding: 'DB',
54
+ database_name: 'your-database-name',
55
+ database_id: 'your-database-id',
56
+ },
57
+ ],
58
+ }),
59
+ })
60
+ ```
61
+
62
+ > **Note:** When using `import { env } from 'cloudflare:workers'`, `D1Store` must be initialized inline inside `new Mastra({...})` — not extracted to a module-level variable. Alternatively, initialize `D1Store` inside the `fetch` handler after `env` is available. See [CloudflareDeployer reference](https://mastra.ai/reference/deployer/cloudflare) for details.
49
63
 
50
- // Factory function to create Mastra with D1 binding
51
- function createMastra(env: Env) {
52
- const storage = new D1Store({
53
- binding: env.D1Database, // ✅ Access the actual binding from env
54
- tablePrefix: 'dev_', // Optional: isolate tables per environment
55
- })
56
-
57
- return new Mastra({
58
- storage,
59
- deployer: new CloudflareDeployer({
60
- name: 'my-worker',
61
- d1_databases: [
62
- {
63
- binding: 'D1Database', // Must match the property name in Env type
64
- database_name: 'your-database-name',
65
- database_id: 'your-database-id',
66
- },
67
- ],
68
- }),
69
- })
64
+ ### Using in a Cloudflare Worker without HTTP routes
65
+
66
+ If you want to call Mastra directly in a Worker — for example, to run an agent or trigger a workflow — without serving HTTP routes, you don't need `CloudflareDeployer`. Access the D1 binding from the worker's `env` parameter and call Mastra programmatically.
67
+
68
+ ```typescript
69
+ import { D1Store } from '@mastra/cloudflare-d1'
70
+ import { Mastra } from '@mastra/core'
71
+
72
+ type Env = {
73
+ DB: D1Database
70
74
  }
71
75
 
72
- // Cloudflare Worker export
73
76
  export default {
74
77
  async fetch(request: Request, env: Env, ctx: ExecutionContext) {
75
- const mastra = createMastra(env)
78
+ const mastra = new Mastra({
79
+ storage: new D1Store({ binding: env.DB }),
80
+ })
76
81
 
77
- // Your handler logic here
78
- return new Response('Hello from Mastra with D1!')
82
+ const agent = mastra.getAgent('my-agent')
83
+ const result = await agent.generate('Hello')
84
+
85
+ return Response.json({ text: result.text })
79
86
  },
80
87
  }
81
88
  ```
82
89
 
83
- > **Important: Understanding D1 Bindings:** In the `Env` type definition, `D1Database: D1Database` serves two purposes:
84
- >
85
- > - The **property name** (`D1Database`) must match the `binding` name in your `wrangler.toml`
86
- > - The **type** (`: D1Database`) is from `@cloudflare/workers-types` for TypeScript type checking
87
- >
88
- > At runtime, Cloudflare Workers provides the actual D1 database instance via `env.D1Database`. You can't use `D1Database` directly outside of the worker's context.
89
-
90
90
  ### Using with REST API
91
91
 
92
92
  For non-Workers environments (Node.js, serverless functions, etc.), use the REST API approach:
@@ -108,7 +108,7 @@ Add the D1 database binding to your `wrangler.toml`:
108
108
 
109
109
  ```toml
110
110
  [[d1_databases]]
111
- binding = "D1Database" # Must match the property name in your Env type
111
+ binding = "DB"
112
112
  database_name = "your-database-name"
113
113
  database_id = "your-database-id"
114
114
  ```
@@ -119,7 +119,7 @@ Or in `wrangler.jsonc`:
119
119
  {
120
120
  "d1_databases": [
121
121
  {
122
- "binding": "D1Database",
122
+ "binding": "DB",
123
123
  "database_name": "your-database-name",
124
124
  "database_id": "your-database-id",
125
125
  },
@@ -158,14 +158,14 @@ import { Mastra } from '@mastra/core'
158
158
  import { D1Store } from '@mastra/cloudflare-d1'
159
159
 
160
160
  type Env = {
161
- D1Database: D1Database
161
+ DB: D1Database
162
162
  }
163
163
 
164
164
  // In a Cloudflare Worker
165
165
  export default {
166
166
  async fetch(request: Request, env: Env, ctx: ExecutionContext) {
167
167
  const storage = new D1Store({
168
- binding: env.D1Database, // ✅ Use env.D1Database
168
+ binding: env.DB,
169
169
  })
170
170
 
171
171
  const mastra = new Mastra({
@@ -184,7 +184,7 @@ If you're using storage directly without Mastra, you must call `init()` explicit
184
184
  import { D1Store } from '@mastra/cloudflare-d1'
185
185
 
186
186
  type Env = {
187
- D1Database: D1Database
187
+ DB: D1Database
188
188
  }
189
189
 
190
190
  // In a Cloudflare Worker
@@ -192,7 +192,7 @@ export default {
192
192
  async fetch(request: Request, env: Env, ctx: ExecutionContext) {
193
193
  const storage = new D1Store({
194
194
  id: 'd1-storage',
195
- binding: env.D1Database, // ✅ Use env.D1Database
195
+ binding: env.DB,
196
196
  })
197
197
 
198
198
  // Required when using storage directly
package/dist/index.cjs CHANGED
@@ -759,11 +759,13 @@ function rowToTask(row) {
759
759
  runId: row.run_id ?? "",
760
760
  result: parseJson(row.result),
761
761
  error: parseJson(row.error),
762
+ suspendPayload: parseJson(row.suspend_payload),
762
763
  retryCount: Number(row.retry_count ?? 0),
763
764
  maxRetries: Number(row.max_retries ?? 0),
764
765
  timeoutMs: Number(row.timeout_ms ?? 3e5),
765
766
  createdAt: new Date(row.createdAt),
766
767
  startedAt: row.startedAt ? new Date(row.startedAt) : void 0,
768
+ suspendedAt: row.suspendedAt ? new Date(row.suspendedAt) : void 0,
767
769
  completedAt: row.completedAt ? new Date(row.completedAt) : void 0
768
770
  };
769
771
  }
@@ -776,6 +778,11 @@ var BackgroundTasksStorageD1 = class extends storage.BackgroundTasksStorage {
776
778
  }
777
779
  async init() {
778
780
  await this.#db.createTable({ tableName: storage.TABLE_BACKGROUND_TASKS, schema: storage.TABLE_SCHEMAS[storage.TABLE_BACKGROUND_TASKS] });
781
+ await this.#db.alterTable({
782
+ tableName: storage.TABLE_BACKGROUND_TASKS,
783
+ schema: storage.TABLE_SCHEMAS[storage.TABLE_BACKGROUND_TASKS],
784
+ ifNotExists: ["suspend_payload", "suspendedAt"]
785
+ });
779
786
  }
780
787
  async dangerouslyClearAll() {
781
788
  await this.#db.clearTable({ tableName: storage.TABLE_BACKGROUND_TASKS });
@@ -796,11 +803,13 @@ var BackgroundTasksStorageD1 = class extends storage.BackgroundTasksStorage {
796
803
  "args",
797
804
  "result",
798
805
  "error",
806
+ "suspend_payload",
799
807
  "retry_count",
800
808
  "max_retries",
801
809
  "timeout_ms",
802
810
  "createdAt",
803
811
  "startedAt",
812
+ "suspendedAt",
804
813
  "completedAt"
805
814
  ],
806
815
  [
@@ -815,11 +824,13 @@ var BackgroundTasksStorageD1 = class extends storage.BackgroundTasksStorage {
815
824
  serializeJson(task.args),
816
825
  serializeJson(task.result),
817
826
  serializeJson(task.error),
827
+ serializeJson(task.suspendPayload),
818
828
  task.retryCount,
819
829
  task.maxRetries,
820
830
  task.timeoutMs,
821
831
  task.createdAt.toISOString(),
822
832
  task.startedAt?.toISOString() ?? null,
833
+ task.suspendedAt?.toISOString() ?? null,
823
834
  task.completedAt?.toISOString() ?? null
824
835
  ]
825
836
  ).build();
@@ -840,6 +851,10 @@ var BackgroundTasksStorageD1 = class extends storage.BackgroundTasksStorage {
840
851
  sets.push("error = ?");
841
852
  params.push(serializeJson(update.error));
842
853
  }
854
+ if ("suspendPayload" in update) {
855
+ sets.push("suspend_payload = ?");
856
+ params.push(serializeJson(update.suspendPayload));
857
+ }
843
858
  if ("retryCount" in update) {
844
859
  sets.push("retry_count = ?");
845
860
  params.push(update.retryCount);
@@ -848,6 +863,10 @@ var BackgroundTasksStorageD1 = class extends storage.BackgroundTasksStorage {
848
863
  sets.push("startedAt = ?");
849
864
  params.push(update.startedAt?.toISOString() ?? null);
850
865
  }
866
+ if ("suspendedAt" in update) {
867
+ sets.push("suspendedAt = ?");
868
+ params.push(update.suspendedAt?.toISOString() ?? null);
869
+ }
851
870
  if ("completedAt" in update) {
852
871
  sets.push("completedAt = ?");
853
872
  params.push(update.completedAt?.toISOString() ?? null);
@@ -891,7 +910,11 @@ var BackgroundTasksStorageD1 = class extends storage.BackgroundTasksStorage {
891
910
  builder = builder.whereAnd("tool_name = ?", filter.toolName);
892
911
  countBuilder = countBuilder.whereAnd("tool_name = ?", filter.toolName);
893
912
  }
894
- const dateCol = filter.dateFilterBy === "startedAt" ? "startedAt" : filter.dateFilterBy === "completedAt" ? "completedAt" : "createdAt";
913
+ if (filter.toolCallId) {
914
+ builder = builder.whereAnd("tool_call_id = ?", filter.toolCallId);
915
+ countBuilder = countBuilder.whereAnd("tool_call_id = ?", filter.toolCallId);
916
+ }
917
+ const dateCol = filter.dateFilterBy === "startedAt" ? "startedAt" : filter.dateFilterBy === "suspendedAt" ? "suspendedAt" : filter.dateFilterBy === "completedAt" ? "completedAt" : "createdAt";
895
918
  if (filter.fromDate) {
896
919
  builder = builder.whereAnd(`${dateCol} >= ?`, filter.fromDate.toISOString());
897
920
  countBuilder = countBuilder.whereAnd(`${dateCol} >= ?`, filter.fromDate.toISOString());
@@ -903,7 +926,7 @@ var BackgroundTasksStorageD1 = class extends storage.BackgroundTasksStorage {
903
926
  const { sql: countSql, params: countParams } = countBuilder.build();
904
927
  const countRow = await this.#db.executeQuery({ sql: countSql, params: countParams, first: true });
905
928
  const total = Number(countRow?.count ?? 0);
906
- const orderCol = filter.orderBy === "startedAt" ? "startedAt" : filter.orderBy === "completedAt" ? "completedAt" : "createdAt";
929
+ const orderCol = filter.orderBy === "startedAt" ? "startedAt" : filter.orderBy === "suspendedAt" ? "suspendedAt" : filter.orderBy === "completedAt" ? "completedAt" : "createdAt";
907
930
  builder = builder.orderBy(orderCol, filter.orderDirection === "desc" ? "DESC" : "ASC");
908
931
  if (filter.perPage != null) {
909
932
  builder = builder.limit(filter.perPage);
@@ -927,7 +950,7 @@ var BackgroundTasksStorageD1 = class extends storage.BackgroundTasksStorage {
927
950
  conditions.push(`status IN (${statuses.map(() => "?").join(",")})`);
928
951
  params.push(...statuses);
929
952
  }
930
- const dateCol = filter.dateFilterBy === "startedAt" ? "startedAt" : filter.dateFilterBy === "completedAt" ? "completedAt" : "createdAt";
953
+ const dateCol = filter.dateFilterBy === "startedAt" ? "startedAt" : filter.dateFilterBy === "suspendedAt" ? "suspendedAt" : filter.dateFilterBy === "completedAt" ? "completedAt" : "createdAt";
931
954
  if (filter.fromDate) {
932
955
  conditions.push(`${dateCol} >= ?`);
933
956
  params.push(filter.fromDate.toISOString());
@@ -944,6 +967,10 @@ var BackgroundTasksStorageD1 = class extends storage.BackgroundTasksStorage {
944
967
  conditions.push("run_id = ?");
945
968
  params.push(filter.runId);
946
969
  }
970
+ if (filter.toolCallId) {
971
+ conditions.push("tool_call_id = ?");
972
+ params.push(filter.toolCallId);
973
+ }
947
974
  if (conditions.length === 0) return;
948
975
  const fullTableName = this.#db.getTableName(storage.TABLE_BACKGROUND_TASKS);
949
976
  await this.#db.executeQuery({