@mastra/cloudflare-d1 1.0.4 → 1.0.5
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 +18 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/reference-storage-cloudflare-d1.md +43 -43
- package/dist/index.cjs +247 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +248 -5
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/background-tasks/index.d.ts +18 -0
- package/dist/storage/domains/background-tasks/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +2 -1
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +11 -11
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @mastra/cloudflare-d1
|
|
2
2
|
|
|
3
|
+
## 1.0.5
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Add `BackgroundTasksStorage` domain implementation so `@mastra/core` background task execution works with any storage adapter. ([#15307](https://github.com/mastra-ai/mastra/pull/15307))
|
|
8
|
+
|
|
9
|
+
- 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)]:
|
|
10
|
+
- @mastra/core@1.26.0
|
|
11
|
+
|
|
12
|
+
## 1.0.5-alpha.0
|
|
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 [[`d63ffdb`](https://github.com/mastra-ai/mastra/commit/d63ffdbb2c11e76fe5ea45faab44bc15460f010c)]:
|
|
19
|
+
- @mastra/core@1.25.1-alpha.0
|
|
20
|
+
|
|
3
21
|
## 1.0.4
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
package/dist/docs/SKILL.md
CHANGED
|
@@ -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.
|
|
6
|
+
version: "1.0.5"
|
|
7
7
|
---
|
|
8
8
|
|
|
9
9
|
## When to use
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
The Cloudflare D1 storage implementation provides a serverless SQL database solution using Cloudflare D1, supporting relational operations and transactional consistency.
|
|
4
4
|
|
|
5
|
-
> **Observability Not Supported:** Cloudflare D1 storage **doesn't support the observability domain**. Traces from the `DefaultExporter` can't be persisted to D1, and
|
|
5
|
+
> **Observability Not Supported:** Cloudflare D1 storage **doesn't support the observability domain**. Traces from the `DefaultExporter` can't be persisted to D1, and [Studio's](https://mastra.ai/docs/studio/overview) observability features won't work with D1 as your only storage provider. To enable observability, use [composite storage](https://mastra.ai/reference/storage/composite) to route observability data to a supported provider like ClickHouse or PostgreSQL.
|
|
6
6
|
|
|
7
7
|
> **Row Size Limit:** Cloudflare D1 enforces a **1 MiB maximum row size**. This limit can be exceeded when storing messages with base64-encoded attachments such as images. See [Handling large attachments](https://mastra.ai/docs/memory/storage) for workarounds including uploading attachments to external storage.
|
|
8
8
|
|
|
@@ -34,59 +34,59 @@ bun add @mastra/cloudflare-d1@latest
|
|
|
34
34
|
|
|
35
35
|
## Usage
|
|
36
36
|
|
|
37
|
-
### Using with
|
|
37
|
+
### Using with Mastra CloudflareDeployer
|
|
38
38
|
|
|
39
|
-
|
|
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
|
-
|
|
47
|
-
|
|
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
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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 =
|
|
78
|
+
const mastra = new Mastra({
|
|
79
|
+
storage: new D1Store({ binding: env.DB }),
|
|
80
|
+
})
|
|
76
81
|
|
|
77
|
-
|
|
78
|
-
|
|
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 = "
|
|
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": "
|
|
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
|
-
|
|
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.
|
|
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
|
-
|
|
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.
|
|
195
|
+
binding: env.DB,
|
|
196
196
|
})
|
|
197
197
|
|
|
198
198
|
// Required when using storage directly
|
package/dist/index.cjs
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
var error = require('@mastra/core/error');
|
|
4
4
|
var storage = require('@mastra/core/storage');
|
|
5
5
|
var Cloudflare = require('cloudflare');
|
|
6
|
-
var agent = require('@mastra/core/agent');
|
|
7
6
|
var base = require('@mastra/core/base');
|
|
8
7
|
var utils = require('@mastra/core/utils');
|
|
8
|
+
var agent = require('@mastra/core/agent');
|
|
9
9
|
var evals = require('@mastra/core/evals');
|
|
10
10
|
|
|
11
11
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
@@ -730,7 +730,246 @@ var D1DB = class extends base.MastraBase {
|
|
|
730
730
|
}
|
|
731
731
|
};
|
|
732
732
|
|
|
733
|
-
// src/storage/domains/
|
|
733
|
+
// src/storage/domains/background-tasks/index.ts
|
|
734
|
+
function serializeJson(v) {
|
|
735
|
+
if (typeof v === "object" && v != null) return JSON.stringify(v);
|
|
736
|
+
return v ?? null;
|
|
737
|
+
}
|
|
738
|
+
function rowToTask(row) {
|
|
739
|
+
const parseJson = (val) => {
|
|
740
|
+
if (val == null || val === "") return void 0;
|
|
741
|
+
if (typeof val === "string") {
|
|
742
|
+
try {
|
|
743
|
+
return JSON.parse(val);
|
|
744
|
+
} catch {
|
|
745
|
+
return val;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
return val;
|
|
749
|
+
};
|
|
750
|
+
return {
|
|
751
|
+
id: row.id,
|
|
752
|
+
status: row.status,
|
|
753
|
+
toolName: row.tool_name,
|
|
754
|
+
toolCallId: row.tool_call_id,
|
|
755
|
+
args: parseJson(row.args) ?? {},
|
|
756
|
+
agentId: row.agent_id,
|
|
757
|
+
threadId: row.thread_id || void 0,
|
|
758
|
+
resourceId: row.resource_id || void 0,
|
|
759
|
+
runId: row.run_id ?? "",
|
|
760
|
+
result: parseJson(row.result),
|
|
761
|
+
error: parseJson(row.error),
|
|
762
|
+
retryCount: Number(row.retry_count ?? 0),
|
|
763
|
+
maxRetries: Number(row.max_retries ?? 0),
|
|
764
|
+
timeoutMs: Number(row.timeout_ms ?? 3e5),
|
|
765
|
+
createdAt: new Date(row.createdAt),
|
|
766
|
+
startedAt: row.startedAt ? new Date(row.startedAt) : void 0,
|
|
767
|
+
completedAt: row.completedAt ? new Date(row.completedAt) : void 0
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
var BackgroundTasksStorageD1 = class extends storage.BackgroundTasksStorage {
|
|
771
|
+
#db;
|
|
772
|
+
constructor(config) {
|
|
773
|
+
super();
|
|
774
|
+
const resolved = resolveD1Config(config);
|
|
775
|
+
this.#db = new D1DB(resolved);
|
|
776
|
+
}
|
|
777
|
+
async init() {
|
|
778
|
+
await this.#db.createTable({ tableName: storage.TABLE_BACKGROUND_TASKS, schema: storage.TABLE_SCHEMAS[storage.TABLE_BACKGROUND_TASKS] });
|
|
779
|
+
}
|
|
780
|
+
async dangerouslyClearAll() {
|
|
781
|
+
await this.#db.clearTable({ tableName: storage.TABLE_BACKGROUND_TASKS });
|
|
782
|
+
}
|
|
783
|
+
async createTask(task) {
|
|
784
|
+
const fullTableName = this.#db.getTableName(storage.TABLE_BACKGROUND_TASKS);
|
|
785
|
+
const { sql, params } = createSqlBuilder().insert(
|
|
786
|
+
fullTableName,
|
|
787
|
+
[
|
|
788
|
+
"id",
|
|
789
|
+
"tool_call_id",
|
|
790
|
+
"tool_name",
|
|
791
|
+
"agent_id",
|
|
792
|
+
"thread_id",
|
|
793
|
+
"resource_id",
|
|
794
|
+
"run_id",
|
|
795
|
+
"status",
|
|
796
|
+
"args",
|
|
797
|
+
"result",
|
|
798
|
+
"error",
|
|
799
|
+
"retry_count",
|
|
800
|
+
"max_retries",
|
|
801
|
+
"timeout_ms",
|
|
802
|
+
"createdAt",
|
|
803
|
+
"startedAt",
|
|
804
|
+
"completedAt"
|
|
805
|
+
],
|
|
806
|
+
[
|
|
807
|
+
task.id,
|
|
808
|
+
task.toolCallId,
|
|
809
|
+
task.toolName,
|
|
810
|
+
task.agentId,
|
|
811
|
+
task.threadId ?? null,
|
|
812
|
+
task.resourceId ?? null,
|
|
813
|
+
task.runId,
|
|
814
|
+
task.status,
|
|
815
|
+
serializeJson(task.args),
|
|
816
|
+
serializeJson(task.result),
|
|
817
|
+
serializeJson(task.error),
|
|
818
|
+
task.retryCount,
|
|
819
|
+
task.maxRetries,
|
|
820
|
+
task.timeoutMs,
|
|
821
|
+
task.createdAt.toISOString(),
|
|
822
|
+
task.startedAt?.toISOString() ?? null,
|
|
823
|
+
task.completedAt?.toISOString() ?? null
|
|
824
|
+
]
|
|
825
|
+
).build();
|
|
826
|
+
await this.#db.executeQuery({ sql, params });
|
|
827
|
+
}
|
|
828
|
+
async updateTask(taskId, update) {
|
|
829
|
+
const sets = [];
|
|
830
|
+
const params = [];
|
|
831
|
+
if ("status" in update) {
|
|
832
|
+
sets.push("status = ?");
|
|
833
|
+
params.push(update.status);
|
|
834
|
+
}
|
|
835
|
+
if ("result" in update) {
|
|
836
|
+
sets.push("result = ?");
|
|
837
|
+
params.push(serializeJson(update.result));
|
|
838
|
+
}
|
|
839
|
+
if ("error" in update) {
|
|
840
|
+
sets.push("error = ?");
|
|
841
|
+
params.push(serializeJson(update.error));
|
|
842
|
+
}
|
|
843
|
+
if ("retryCount" in update) {
|
|
844
|
+
sets.push("retry_count = ?");
|
|
845
|
+
params.push(update.retryCount);
|
|
846
|
+
}
|
|
847
|
+
if ("startedAt" in update) {
|
|
848
|
+
sets.push("startedAt = ?");
|
|
849
|
+
params.push(update.startedAt?.toISOString() ?? null);
|
|
850
|
+
}
|
|
851
|
+
if ("completedAt" in update) {
|
|
852
|
+
sets.push("completedAt = ?");
|
|
853
|
+
params.push(update.completedAt?.toISOString() ?? null);
|
|
854
|
+
}
|
|
855
|
+
if (sets.length === 0) return;
|
|
856
|
+
params.push(taskId);
|
|
857
|
+
const fullTableName = this.#db.getTableName(storage.TABLE_BACKGROUND_TASKS);
|
|
858
|
+
await this.#db.executeQuery({
|
|
859
|
+
sql: `UPDATE ${fullTableName} SET ${sets.join(", ")} WHERE id = ?`,
|
|
860
|
+
params
|
|
861
|
+
});
|
|
862
|
+
}
|
|
863
|
+
async getTask(taskId) {
|
|
864
|
+
const fullTableName = this.#db.getTableName(storage.TABLE_BACKGROUND_TASKS);
|
|
865
|
+
const { sql, params } = createSqlBuilder().select("*").from(fullTableName).where("id = ?", taskId).build();
|
|
866
|
+
const row = await this.#db.executeQuery({ sql, params, first: true });
|
|
867
|
+
return row ? rowToTask(row) : null;
|
|
868
|
+
}
|
|
869
|
+
async listTasks(filter) {
|
|
870
|
+
const fullTableName = this.#db.getTableName(storage.TABLE_BACKGROUND_TASKS);
|
|
871
|
+
let builder = createSqlBuilder().select("*").from(fullTableName);
|
|
872
|
+
let countBuilder = createSqlBuilder().count().from(fullTableName);
|
|
873
|
+
if (filter.status) {
|
|
874
|
+
const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
|
|
875
|
+
builder = builder.where(`status IN (${statuses.map(() => "?").join(",")})`, ...statuses);
|
|
876
|
+
countBuilder = countBuilder.where(`status IN (${statuses.map(() => "?").join(",")})`, ...statuses);
|
|
877
|
+
}
|
|
878
|
+
if (filter.agentId) {
|
|
879
|
+
builder = builder.whereAnd("agent_id = ?", filter.agentId);
|
|
880
|
+
countBuilder = countBuilder.whereAnd("agent_id = ?", filter.agentId);
|
|
881
|
+
}
|
|
882
|
+
if (filter.threadId) {
|
|
883
|
+
builder = builder.whereAnd("thread_id = ?", filter.threadId);
|
|
884
|
+
countBuilder = countBuilder.whereAnd("thread_id = ?", filter.threadId);
|
|
885
|
+
}
|
|
886
|
+
if (filter.runId) {
|
|
887
|
+
builder = builder.whereAnd("run_id = ?", filter.runId);
|
|
888
|
+
countBuilder = countBuilder.whereAnd("run_id = ?", filter.runId);
|
|
889
|
+
}
|
|
890
|
+
if (filter.toolName) {
|
|
891
|
+
builder = builder.whereAnd("tool_name = ?", filter.toolName);
|
|
892
|
+
countBuilder = countBuilder.whereAnd("tool_name = ?", filter.toolName);
|
|
893
|
+
}
|
|
894
|
+
const dateCol = filter.dateFilterBy === "startedAt" ? "startedAt" : filter.dateFilterBy === "completedAt" ? "completedAt" : "createdAt";
|
|
895
|
+
if (filter.fromDate) {
|
|
896
|
+
builder = builder.whereAnd(`${dateCol} >= ?`, filter.fromDate.toISOString());
|
|
897
|
+
countBuilder = countBuilder.whereAnd(`${dateCol} >= ?`, filter.fromDate.toISOString());
|
|
898
|
+
}
|
|
899
|
+
if (filter.toDate) {
|
|
900
|
+
builder = builder.whereAnd(`${dateCol} < ?`, filter.toDate.toISOString());
|
|
901
|
+
countBuilder = countBuilder.whereAnd(`${dateCol} < ?`, filter.toDate.toISOString());
|
|
902
|
+
}
|
|
903
|
+
const { sql: countSql, params: countParams } = countBuilder.build();
|
|
904
|
+
const countRow = await this.#db.executeQuery({ sql: countSql, params: countParams, first: true });
|
|
905
|
+
const total = Number(countRow?.count ?? 0);
|
|
906
|
+
const orderCol = filter.orderBy === "startedAt" ? "startedAt" : filter.orderBy === "completedAt" ? "completedAt" : "createdAt";
|
|
907
|
+
builder = builder.orderBy(orderCol, filter.orderDirection === "desc" ? "DESC" : "ASC");
|
|
908
|
+
if (filter.perPage != null) {
|
|
909
|
+
builder = builder.limit(filter.perPage);
|
|
910
|
+
if (filter.page != null) {
|
|
911
|
+
builder = builder.offset(filter.page * filter.perPage);
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
const { sql, params } = builder.build();
|
|
915
|
+
const rows = await this.#db.executeQuery({ sql, params });
|
|
916
|
+
return { tasks: rows.map(rowToTask), total };
|
|
917
|
+
}
|
|
918
|
+
async deleteTask(taskId) {
|
|
919
|
+
const fullTableName = this.#db.getTableName(storage.TABLE_BACKGROUND_TASKS);
|
|
920
|
+
await this.#db.executeQuery({ sql: `DELETE FROM ${fullTableName} WHERE id = ?`, params: [taskId] });
|
|
921
|
+
}
|
|
922
|
+
async deleteTasks(filter) {
|
|
923
|
+
const conditions = [];
|
|
924
|
+
const params = [];
|
|
925
|
+
if (filter.status) {
|
|
926
|
+
const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
|
|
927
|
+
conditions.push(`status IN (${statuses.map(() => "?").join(",")})`);
|
|
928
|
+
params.push(...statuses);
|
|
929
|
+
}
|
|
930
|
+
const dateCol = filter.dateFilterBy === "startedAt" ? "startedAt" : filter.dateFilterBy === "completedAt" ? "completedAt" : "createdAt";
|
|
931
|
+
if (filter.fromDate) {
|
|
932
|
+
conditions.push(`${dateCol} >= ?`);
|
|
933
|
+
params.push(filter.fromDate.toISOString());
|
|
934
|
+
}
|
|
935
|
+
if (filter.toDate) {
|
|
936
|
+
conditions.push(`${dateCol} < ?`);
|
|
937
|
+
params.push(filter.toDate.toISOString());
|
|
938
|
+
}
|
|
939
|
+
if (filter.agentId) {
|
|
940
|
+
conditions.push("agent_id = ?");
|
|
941
|
+
params.push(filter.agentId);
|
|
942
|
+
}
|
|
943
|
+
if (filter.runId) {
|
|
944
|
+
conditions.push("run_id = ?");
|
|
945
|
+
params.push(filter.runId);
|
|
946
|
+
}
|
|
947
|
+
if (conditions.length === 0) return;
|
|
948
|
+
const fullTableName = this.#db.getTableName(storage.TABLE_BACKGROUND_TASKS);
|
|
949
|
+
await this.#db.executeQuery({
|
|
950
|
+
sql: `DELETE FROM ${fullTableName} WHERE ${conditions.join(" AND ")}`,
|
|
951
|
+
params
|
|
952
|
+
});
|
|
953
|
+
}
|
|
954
|
+
async getRunningCount() {
|
|
955
|
+
const fullTableName = this.#db.getTableName(storage.TABLE_BACKGROUND_TASKS);
|
|
956
|
+
const row = await this.#db.executeQuery({
|
|
957
|
+
sql: `SELECT COUNT(*) as count FROM ${fullTableName} WHERE status = 'running'`,
|
|
958
|
+
params: [],
|
|
959
|
+
first: true
|
|
960
|
+
});
|
|
961
|
+
return Number(row?.count ?? 0);
|
|
962
|
+
}
|
|
963
|
+
async getRunningCountByAgent(agentId) {
|
|
964
|
+
const fullTableName = this.#db.getTableName(storage.TABLE_BACKGROUND_TASKS);
|
|
965
|
+
const row = await this.#db.executeQuery({
|
|
966
|
+
sql: `SELECT COUNT(*) as count FROM ${fullTableName} WHERE status = 'running' AND agent_id = ?`,
|
|
967
|
+
params: [agentId],
|
|
968
|
+
first: true
|
|
969
|
+
});
|
|
970
|
+
return Number(row?.count ?? 0);
|
|
971
|
+
}
|
|
972
|
+
};
|
|
734
973
|
var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
735
974
|
#db;
|
|
736
975
|
constructor(config) {
|
|
@@ -2229,21 +2468,25 @@ var D1Store = class extends storage.MastraCompositeStore {
|
|
|
2229
2468
|
let scores;
|
|
2230
2469
|
let workflows;
|
|
2231
2470
|
let memory;
|
|
2471
|
+
let backgroundTasks;
|
|
2232
2472
|
if (this.binding) {
|
|
2233
2473
|
const domainConfig = { binding: this.binding, tablePrefix: this.tablePrefix };
|
|
2234
2474
|
scores = new ScoresStorageD1(domainConfig);
|
|
2235
2475
|
workflows = new WorkflowsStorageD1(domainConfig);
|
|
2236
2476
|
memory = new MemoryStorageD1(domainConfig);
|
|
2477
|
+
backgroundTasks = new BackgroundTasksStorageD1(domainConfig);
|
|
2237
2478
|
} else {
|
|
2238
2479
|
const domainConfig = { client: this.client, tablePrefix: this.tablePrefix };
|
|
2239
2480
|
scores = new ScoresStorageD1(domainConfig);
|
|
2240
2481
|
workflows = new WorkflowsStorageD1(domainConfig);
|
|
2241
2482
|
memory = new MemoryStorageD1(domainConfig);
|
|
2483
|
+
backgroundTasks = new BackgroundTasksStorageD1(domainConfig);
|
|
2242
2484
|
}
|
|
2243
2485
|
this.stores = {
|
|
2244
2486
|
scores,
|
|
2245
2487
|
workflows,
|
|
2246
|
-
memory
|
|
2488
|
+
memory,
|
|
2489
|
+
backgroundTasks
|
|
2247
2490
|
};
|
|
2248
2491
|
}
|
|
2249
2492
|
/**
|
|
@@ -2255,6 +2498,7 @@ var D1Store = class extends storage.MastraCompositeStore {
|
|
|
2255
2498
|
}
|
|
2256
2499
|
};
|
|
2257
2500
|
|
|
2501
|
+
exports.BackgroundTasksStorageD1 = BackgroundTasksStorageD1;
|
|
2258
2502
|
exports.D1Store = D1Store;
|
|
2259
2503
|
exports.MemoryStorageD1 = MemoryStorageD1;
|
|
2260
2504
|
exports.ScoresStorageD1 = ScoresStorageD1;
|