@mastra/cloudflare-d1 0.0.0-redis-cloud-transporter-20250508194049 → 0.0.0-refactor-agent-information-for-recomposable-ui-20251112151814
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 +1622 -0
- package/LICENSE.md +11 -42
- package/README.md +25 -5
- package/dist/index.cjs +1720 -617
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +2 -5
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1706 -603
- package/dist/index.js.map +1 -0
- package/dist/storage/domains/memory/index.d.ts +59 -0
- package/dist/storage/domains/memory/index.d.ts.map +1 -0
- package/dist/storage/domains/operations/index.d.ts +72 -0
- package/dist/storage/domains/operations/index.d.ts.map +1 -0
- package/dist/storage/domains/scores/index.d.ts +60 -0
- package/dist/storage/domains/scores/index.d.ts.map +1 -0
- package/dist/storage/domains/utils.d.ts +3 -0
- package/dist/storage/domains/utils.d.ts.map +1 -0
- package/dist/storage/domains/workflows/index.d.ts +45 -0
- package/dist/storage/domains/workflows/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +230 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/sql-builder.d.ts +128 -0
- package/dist/storage/sql-builder.d.ts.map +1 -0
- package/dist/storage/test-utils.d.ts +19 -0
- package/dist/storage/test-utils.d.ts.map +1 -0
- package/package.json +35 -17
- package/dist/_tsup-dts-rollup.d.cts +0 -331
- package/dist/_tsup-dts-rollup.d.ts +0 -331
- package/dist/index.d.cts +0 -5
package/dist/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
2
|
+
import { MastraStorage, StoreOperations, ScoresStorage, TABLE_SCORERS, normalizePerPage, calculatePagination, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ensureDate, MemoryStorage, TABLE_RESOURCES, TABLE_THREADS, TABLE_MESSAGES, serializeDate, safelyParseJSON } from '@mastra/core/storage';
|
|
2
3
|
import Cloudflare from 'cloudflare';
|
|
4
|
+
import { MessageList } from '@mastra/core/agent';
|
|
5
|
+
import { parseSqlIdentifier } from '@mastra/core/utils';
|
|
6
|
+
import { saveScorePayloadSchema } from '@mastra/core/evals';
|
|
3
7
|
|
|
4
8
|
// src/storage/index.ts
|
|
5
|
-
|
|
6
|
-
// src/storage/sql-builder.ts
|
|
7
9
|
var SqlBuilder = class {
|
|
8
10
|
sql = "";
|
|
9
11
|
params = [];
|
|
@@ -13,12 +15,15 @@ var SqlBuilder = class {
|
|
|
13
15
|
if (!columns || Array.isArray(columns) && columns.length === 0) {
|
|
14
16
|
this.sql = "SELECT *";
|
|
15
17
|
} else {
|
|
16
|
-
|
|
18
|
+
const cols = Array.isArray(columns) ? columns : [columns];
|
|
19
|
+
const parsedCols = cols.map((col) => parseSelectIdentifier(col));
|
|
20
|
+
this.sql = `SELECT ${parsedCols.join(", ")}`;
|
|
17
21
|
}
|
|
18
22
|
return this;
|
|
19
23
|
}
|
|
20
24
|
from(table) {
|
|
21
|
-
|
|
25
|
+
const parsedTableName = parseSqlIdentifier(table, "table name");
|
|
26
|
+
this.sql += ` FROM ${parsedTableName}`;
|
|
22
27
|
return this;
|
|
23
28
|
}
|
|
24
29
|
/**
|
|
@@ -55,7 +60,11 @@ var SqlBuilder = class {
|
|
|
55
60
|
return this;
|
|
56
61
|
}
|
|
57
62
|
orderBy(column, direction = "ASC") {
|
|
58
|
-
|
|
63
|
+
const parsedColumn = parseSqlIdentifier(column, "column name");
|
|
64
|
+
if (!["ASC", "DESC"].includes(direction)) {
|
|
65
|
+
throw new Error(`Invalid sort direction: ${direction}`);
|
|
66
|
+
}
|
|
67
|
+
this.sql += ` ORDER BY ${parsedColumn} ${direction}`;
|
|
59
68
|
return this;
|
|
60
69
|
}
|
|
61
70
|
limit(count) {
|
|
@@ -81,27 +90,33 @@ var SqlBuilder = class {
|
|
|
81
90
|
* @param updateMap Object mapping columns to update to their new value (e.g. { name: 'excluded.name' })
|
|
82
91
|
*/
|
|
83
92
|
insert(table, columns, values, conflictColumns, updateMap) {
|
|
84
|
-
const
|
|
93
|
+
const parsedTableName = parseSqlIdentifier(table, "table name");
|
|
94
|
+
const parsedColumns = columns.map((col) => parseSqlIdentifier(col, "column name"));
|
|
95
|
+
const placeholders = parsedColumns.map(() => "?").join(", ");
|
|
85
96
|
if (conflictColumns && updateMap) {
|
|
97
|
+
const parsedConflictColumns = conflictColumns.map((col) => parseSqlIdentifier(col, "column name"));
|
|
86
98
|
const updateClause = Object.entries(updateMap).map(([col, expr]) => `${col} = ${expr}`).join(", ");
|
|
87
|
-
this.sql = `INSERT INTO ${
|
|
99
|
+
this.sql = `INSERT INTO ${parsedTableName} (${parsedColumns.join(", ")}) VALUES (${placeholders}) ON CONFLICT(${parsedConflictColumns.join(", ")}) DO UPDATE SET ${updateClause}`;
|
|
88
100
|
this.params.push(...values);
|
|
89
101
|
return this;
|
|
90
102
|
}
|
|
91
|
-
this.sql = `INSERT INTO ${
|
|
103
|
+
this.sql = `INSERT INTO ${parsedTableName} (${parsedColumns.join(", ")}) VALUES (${placeholders})`;
|
|
92
104
|
this.params.push(...values);
|
|
93
105
|
return this;
|
|
94
106
|
}
|
|
95
107
|
// Update operations
|
|
96
108
|
update(table, columns, values) {
|
|
97
|
-
const
|
|
98
|
-
|
|
109
|
+
const parsedTableName = parseSqlIdentifier(table, "table name");
|
|
110
|
+
const parsedColumns = columns.map((col) => parseSqlIdentifier(col, "column name"));
|
|
111
|
+
const setClause = parsedColumns.map((col) => `${col} = ?`).join(", ");
|
|
112
|
+
this.sql = `UPDATE ${parsedTableName} SET ${setClause}`;
|
|
99
113
|
this.params.push(...values);
|
|
100
114
|
return this;
|
|
101
115
|
}
|
|
102
116
|
// Delete operations
|
|
103
117
|
delete(table) {
|
|
104
|
-
|
|
118
|
+
const parsedTableName = parseSqlIdentifier(table, "table name");
|
|
119
|
+
this.sql = `DELETE FROM ${parsedTableName}`;
|
|
105
120
|
return this;
|
|
106
121
|
}
|
|
107
122
|
/**
|
|
@@ -112,9 +127,16 @@ var SqlBuilder = class {
|
|
|
112
127
|
* @returns The builder instance
|
|
113
128
|
*/
|
|
114
129
|
createTable(table, columnDefinitions, tableConstraints) {
|
|
115
|
-
const
|
|
130
|
+
const parsedTableName = parseSqlIdentifier(table, "table name");
|
|
131
|
+
const parsedColumnDefinitions = columnDefinitions.map((def) => {
|
|
132
|
+
const colName = def.split(/\s+/)[0];
|
|
133
|
+
if (!colName) throw new Error("Empty column name in definition");
|
|
134
|
+
parseSqlIdentifier(colName, "column name");
|
|
135
|
+
return def;
|
|
136
|
+
});
|
|
137
|
+
const columns = parsedColumnDefinitions.join(", ");
|
|
116
138
|
const constraints = tableConstraints && tableConstraints.length > 0 ? ", " + tableConstraints.join(", ") : "";
|
|
117
|
-
this.sql = `CREATE TABLE IF NOT EXISTS ${
|
|
139
|
+
this.sql = `CREATE TABLE IF NOT EXISTS ${parsedTableName} (${columns}${constraints})`;
|
|
118
140
|
return this;
|
|
119
141
|
}
|
|
120
142
|
/**
|
|
@@ -137,13 +159,10 @@ var SqlBuilder = class {
|
|
|
137
159
|
* @returns The builder instance
|
|
138
160
|
*/
|
|
139
161
|
createIndex(indexName, tableName, columnName, indexType = "") {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
raw(sql, ...params) {
|
|
145
|
-
this.sql = sql;
|
|
146
|
-
this.params.push(...params);
|
|
162
|
+
const parsedIndexName = parseSqlIdentifier(indexName, "index name");
|
|
163
|
+
const parsedTableName = parseSqlIdentifier(tableName, "table name");
|
|
164
|
+
const parsedColumnName = parseSqlIdentifier(columnName, "column name");
|
|
165
|
+
this.sql = `CREATE ${indexType ? indexType + " " : ""}INDEX IF NOT EXISTS ${parsedIndexName} ON ${parsedTableName}(${parsedColumnName})`;
|
|
147
166
|
return this;
|
|
148
167
|
}
|
|
149
168
|
/**
|
|
@@ -153,11 +172,12 @@ var SqlBuilder = class {
|
|
|
153
172
|
* @param exact If true, will not add % wildcards
|
|
154
173
|
*/
|
|
155
174
|
like(column, value, exact = false) {
|
|
175
|
+
const parsedColumnName = parseSqlIdentifier(column, "column name");
|
|
156
176
|
const likeValue = exact ? value : `%${value}%`;
|
|
157
177
|
if (this.whereAdded) {
|
|
158
|
-
this.sql += ` AND ${
|
|
178
|
+
this.sql += ` AND ${parsedColumnName} LIKE ?`;
|
|
159
179
|
} else {
|
|
160
|
-
this.sql += ` WHERE ${
|
|
180
|
+
this.sql += ` WHERE ${parsedColumnName} LIKE ?`;
|
|
161
181
|
this.whereAdded = true;
|
|
162
182
|
}
|
|
163
183
|
this.params.push(likeValue);
|
|
@@ -170,11 +190,13 @@ var SqlBuilder = class {
|
|
|
170
190
|
* @param value The value to match
|
|
171
191
|
*/
|
|
172
192
|
jsonLike(column, key, value) {
|
|
173
|
-
const
|
|
193
|
+
const parsedColumnName = parseSqlIdentifier(column, "column name");
|
|
194
|
+
const parsedKey = parseSqlIdentifier(key, "key name");
|
|
195
|
+
const jsonPattern = `%"${parsedKey}":"${value}"%`;
|
|
174
196
|
if (this.whereAdded) {
|
|
175
|
-
this.sql += ` AND ${
|
|
197
|
+
this.sql += ` AND ${parsedColumnName} LIKE ?`;
|
|
176
198
|
} else {
|
|
177
|
-
this.sql += ` WHERE ${
|
|
199
|
+
this.sql += ` WHERE ${parsedColumnName} LIKE ?`;
|
|
178
200
|
this.whereAdded = true;
|
|
179
201
|
}
|
|
180
202
|
this.params.push(jsonPattern);
|
|
@@ -204,332 +226,153 @@ var SqlBuilder = class {
|
|
|
204
226
|
function createSqlBuilder() {
|
|
205
227
|
return new SqlBuilder();
|
|
206
228
|
}
|
|
229
|
+
var SQL_IDENTIFIER_PATTERN = /^[a-zA-Z0-9_]+(\s+AS\s+[a-zA-Z0-9_]+)?$/;
|
|
230
|
+
function parseSelectIdentifier(column) {
|
|
231
|
+
if (column !== "*" && !SQL_IDENTIFIER_PATTERN.test(column)) {
|
|
232
|
+
throw new Error(
|
|
233
|
+
`Invalid column name: "${column}". Must be "*" or a valid identifier (letters, numbers, underscores), optionally with "AS alias".`
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
return column;
|
|
237
|
+
}
|
|
207
238
|
|
|
208
|
-
// src/storage/
|
|
239
|
+
// src/storage/domains/utils.ts
|
|
209
240
|
function isArrayOfRecords(value) {
|
|
210
241
|
return value && Array.isArray(value) && value.length > 0;
|
|
211
242
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
databaseId;
|
|
216
|
-
binding;
|
|
217
|
-
// D1Database binding
|
|
218
|
-
tablePrefix;
|
|
219
|
-
/**
|
|
220
|
-
* Creates a new D1Store instance
|
|
221
|
-
* @param config Configuration for D1 access (either REST API or Workers Binding API)
|
|
222
|
-
*/
|
|
223
|
-
constructor(config) {
|
|
224
|
-
super({ name: "D1" });
|
|
225
|
-
this.tablePrefix = config.tablePrefix || "";
|
|
226
|
-
if ("binding" in config) {
|
|
227
|
-
if (!config.binding) {
|
|
228
|
-
throw new Error("D1 binding is required when using Workers Binding API");
|
|
229
|
-
}
|
|
230
|
-
this.binding = config.binding;
|
|
231
|
-
this.logger.info("Using D1 Workers Binding API");
|
|
232
|
-
} else {
|
|
233
|
-
if (!config.accountId || !config.databaseId || !config.apiToken) {
|
|
234
|
-
throw new Error("accountId, databaseId, and apiToken are required when using REST API");
|
|
235
|
-
}
|
|
236
|
-
this.accountId = config.accountId;
|
|
237
|
-
this.databaseId = config.databaseId;
|
|
238
|
-
this.client = new Cloudflare({
|
|
239
|
-
apiToken: config.apiToken
|
|
240
|
-
});
|
|
241
|
-
this.logger.info("Using D1 REST API");
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
// Helper method to get the full table name with prefix
|
|
245
|
-
getTableName(tableName) {
|
|
246
|
-
return `${this.tablePrefix}${tableName}`;
|
|
247
|
-
}
|
|
248
|
-
formatSqlParams(params) {
|
|
249
|
-
return params.map((p) => p === void 0 || p === null ? null : p);
|
|
250
|
-
}
|
|
251
|
-
// Helper method to create SQL indexes for better query performance
|
|
252
|
-
async createIndexIfNotExists(tableName, columnName, indexType = "") {
|
|
253
|
-
const fullTableName = this.getTableName(tableName);
|
|
254
|
-
const indexName = `idx_${tableName}_${columnName}`;
|
|
255
|
-
try {
|
|
256
|
-
const checkQuery = createSqlBuilder().checkIndexExists(indexName, fullTableName);
|
|
257
|
-
const { sql: checkSql, params: checkParams } = checkQuery.build();
|
|
258
|
-
const indexExists = await this.executeQuery({
|
|
259
|
-
sql: checkSql,
|
|
260
|
-
params: checkParams,
|
|
261
|
-
first: true
|
|
262
|
-
});
|
|
263
|
-
if (!indexExists) {
|
|
264
|
-
const createQuery = createSqlBuilder().createIndex(indexName, fullTableName, columnName, indexType);
|
|
265
|
-
const { sql: createSql, params: createParams } = createQuery.build();
|
|
266
|
-
await this.executeQuery({ sql: createSql, params: createParams });
|
|
267
|
-
this.logger.debug(`Created index ${indexName} on ${fullTableName}(${columnName})`);
|
|
268
|
-
}
|
|
269
|
-
} catch (error) {
|
|
270
|
-
this.logger.error(`Error creating index on ${fullTableName}(${columnName}):`, {
|
|
271
|
-
message: error instanceof Error ? error.message : String(error)
|
|
272
|
-
});
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
async executeWorkersBindingQuery({
|
|
276
|
-
sql,
|
|
277
|
-
params = [],
|
|
278
|
-
first = false
|
|
279
|
-
}) {
|
|
280
|
-
if (!this.binding) {
|
|
281
|
-
throw new Error("Workers binding is not configured");
|
|
282
|
-
}
|
|
283
|
-
try {
|
|
284
|
-
const statement = this.binding.prepare(sql);
|
|
285
|
-
const formattedParams = this.formatSqlParams(params);
|
|
286
|
-
let result;
|
|
287
|
-
if (formattedParams.length > 0) {
|
|
288
|
-
if (first) {
|
|
289
|
-
result = await statement.bind(...formattedParams).first();
|
|
290
|
-
if (!result) return null;
|
|
291
|
-
return result;
|
|
292
|
-
} else {
|
|
293
|
-
result = await statement.bind(...formattedParams).all();
|
|
294
|
-
const results = result.results || [];
|
|
295
|
-
if (result.meta) {
|
|
296
|
-
this.logger.debug("Query metadata", { meta: result.meta });
|
|
297
|
-
}
|
|
298
|
-
return results;
|
|
299
|
-
}
|
|
300
|
-
} else {
|
|
301
|
-
if (first) {
|
|
302
|
-
result = await statement.first();
|
|
303
|
-
if (!result) return null;
|
|
304
|
-
return result;
|
|
305
|
-
} else {
|
|
306
|
-
result = await statement.all();
|
|
307
|
-
const results = result.results || [];
|
|
308
|
-
if (result.meta) {
|
|
309
|
-
this.logger.debug("Query metadata", { meta: result.meta });
|
|
310
|
-
}
|
|
311
|
-
return results;
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
} catch (workerError) {
|
|
315
|
-
this.logger.error("Workers Binding API error", {
|
|
316
|
-
message: workerError instanceof Error ? workerError.message : String(workerError),
|
|
317
|
-
sql
|
|
318
|
-
});
|
|
319
|
-
throw new Error(`D1 Workers API error: ${workerError.message}`);
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
async executeRestQuery({
|
|
323
|
-
sql,
|
|
324
|
-
params = [],
|
|
325
|
-
first = false
|
|
326
|
-
}) {
|
|
327
|
-
if (!this.client || !this.accountId || !this.databaseId) {
|
|
328
|
-
throw new Error("Missing required REST API configuration");
|
|
329
|
-
}
|
|
330
|
-
try {
|
|
331
|
-
const response = await this.client.d1.database.query(this.databaseId, {
|
|
332
|
-
account_id: this.accountId,
|
|
333
|
-
sql,
|
|
334
|
-
params: this.formatSqlParams(params)
|
|
335
|
-
});
|
|
336
|
-
const result = response.result || [];
|
|
337
|
-
const results = result.flatMap((r) => r.results || []);
|
|
338
|
-
if (first) {
|
|
339
|
-
const firstResult = isArrayOfRecords(results) && results.length > 0 ? results[0] : null;
|
|
340
|
-
if (!firstResult) return null;
|
|
341
|
-
return firstResult;
|
|
342
|
-
}
|
|
343
|
-
return results;
|
|
344
|
-
} catch (restError) {
|
|
345
|
-
this.logger.error("REST API error", {
|
|
346
|
-
message: restError instanceof Error ? restError.message : String(restError),
|
|
347
|
-
sql
|
|
348
|
-
});
|
|
349
|
-
throw new Error(`D1 REST API error: ${restError.message}`);
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
/**
|
|
353
|
-
* Execute a SQL query against the D1 database
|
|
354
|
-
* @param options Query options including SQL, parameters, and whether to return only the first result
|
|
355
|
-
* @returns Query results as an array or a single object if first=true
|
|
356
|
-
*/
|
|
357
|
-
async executeQuery(options) {
|
|
358
|
-
const { sql, params = [], first = false } = options;
|
|
243
|
+
function deserializeValue(value, type) {
|
|
244
|
+
if (value === null || value === void 0) return null;
|
|
245
|
+
if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
|
|
359
246
|
try {
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
} else if (this.client && this.accountId && this.databaseId) {
|
|
364
|
-
return this.executeRestQuery({ sql, params, first });
|
|
365
|
-
} else {
|
|
366
|
-
throw new Error("No valid D1 configuration provided");
|
|
367
|
-
}
|
|
368
|
-
} catch (error) {
|
|
369
|
-
this.logger.error("Error executing SQL query", {
|
|
370
|
-
message: error instanceof Error ? error.message : String(error),
|
|
371
|
-
sql,
|
|
372
|
-
params,
|
|
373
|
-
first
|
|
374
|
-
});
|
|
375
|
-
throw new Error(`D1 query error: ${error.message}`);
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
// Helper to convert storage type to SQL type
|
|
379
|
-
getSqlType(type) {
|
|
380
|
-
switch (type) {
|
|
381
|
-
case "text":
|
|
382
|
-
return "TEXT";
|
|
383
|
-
case "timestamp":
|
|
384
|
-
return "TIMESTAMP";
|
|
385
|
-
case "integer":
|
|
386
|
-
return "INTEGER";
|
|
387
|
-
case "bigint":
|
|
388
|
-
return "INTEGER";
|
|
389
|
-
// SQLite doesn't have a separate BIGINT type
|
|
390
|
-
case "jsonb":
|
|
391
|
-
return "TEXT";
|
|
392
|
-
// Store JSON as TEXT in SQLite
|
|
393
|
-
default:
|
|
394
|
-
return "TEXT";
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
ensureDate(date) {
|
|
398
|
-
if (!date) return void 0;
|
|
399
|
-
return date instanceof Date ? date : new Date(date);
|
|
400
|
-
}
|
|
401
|
-
serializeDate(date) {
|
|
402
|
-
if (!date) return void 0;
|
|
403
|
-
const dateObj = this.ensureDate(date);
|
|
404
|
-
return dateObj?.toISOString();
|
|
405
|
-
}
|
|
406
|
-
// Helper to serialize objects to JSON strings
|
|
407
|
-
serializeValue(value) {
|
|
408
|
-
if (value === null || value === void 0) return null;
|
|
409
|
-
if (value instanceof Date) {
|
|
410
|
-
return this.serializeDate(value);
|
|
411
|
-
}
|
|
412
|
-
if (typeof value === "object") {
|
|
413
|
-
return JSON.stringify(value);
|
|
247
|
+
return JSON.parse(value);
|
|
248
|
+
} catch {
|
|
249
|
+
return value;
|
|
414
250
|
}
|
|
415
|
-
return value;
|
|
416
251
|
}
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
} catch {
|
|
427
|
-
return value;
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
|
|
431
|
-
try {
|
|
432
|
-
return JSON.parse(value);
|
|
433
|
-
} catch {
|
|
434
|
-
return value;
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
return value;
|
|
252
|
+
return value;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// src/storage/domains/memory/index.ts
|
|
256
|
+
var MemoryStorageD1 = class extends MemoryStorage {
|
|
257
|
+
operations;
|
|
258
|
+
constructor({ operations }) {
|
|
259
|
+
super();
|
|
260
|
+
this.operations = operations;
|
|
438
261
|
}
|
|
439
|
-
async
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
const fullTableName = this.getTableName(tableName);
|
|
444
|
-
const columnDefinitions = Object.entries(schema).map(([colName, colDef]) => {
|
|
445
|
-
const type = this.getSqlType(colDef.type);
|
|
446
|
-
const nullable = colDef.nullable === false ? "NOT NULL" : "";
|
|
447
|
-
const primaryKey = colDef.primaryKey ? "PRIMARY KEY" : "";
|
|
448
|
-
return `${colName} ${type} ${nullable} ${primaryKey}`.trim();
|
|
262
|
+
async getResourceById({ resourceId }) {
|
|
263
|
+
const resource = await this.operations.load({
|
|
264
|
+
tableName: TABLE_RESOURCES,
|
|
265
|
+
keys: { id: resourceId }
|
|
449
266
|
});
|
|
450
|
-
|
|
451
|
-
if (tableName === TABLE_WORKFLOW_SNAPSHOT) {
|
|
452
|
-
tableConstraints.push("UNIQUE (workflow_name, run_id)");
|
|
453
|
-
}
|
|
454
|
-
const query = createSqlBuilder().createTable(fullTableName, columnDefinitions, tableConstraints);
|
|
455
|
-
const { sql, params } = query.build();
|
|
456
|
-
try {
|
|
457
|
-
await this.executeQuery({ sql, params });
|
|
458
|
-
this.logger.debug(`Created table ${fullTableName}`);
|
|
459
|
-
} catch (error) {
|
|
460
|
-
this.logger.error(`Error creating table ${fullTableName}:`, {
|
|
461
|
-
message: error instanceof Error ? error.message : String(error)
|
|
462
|
-
});
|
|
463
|
-
throw new Error(`Failed to create table ${fullTableName}: ${error}`);
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
async clearTable({ tableName }) {
|
|
467
|
-
const fullTableName = this.getTableName(tableName);
|
|
267
|
+
if (!resource) return null;
|
|
468
268
|
try {
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
269
|
+
return {
|
|
270
|
+
...resource,
|
|
271
|
+
createdAt: ensureDate(resource.createdAt),
|
|
272
|
+
updatedAt: ensureDate(resource.updatedAt),
|
|
273
|
+
metadata: typeof resource.metadata === "string" ? JSON.parse(resource.metadata || "{}") : resource.metadata
|
|
274
|
+
};
|
|
473
275
|
} catch (error) {
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
276
|
+
const mastraError = new MastraError(
|
|
277
|
+
{
|
|
278
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_RESOURCE_BY_ID_ERROR",
|
|
279
|
+
domain: ErrorDomain.STORAGE,
|
|
280
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
281
|
+
text: `Error processing resource ${resourceId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
282
|
+
details: { resourceId }
|
|
283
|
+
},
|
|
284
|
+
error
|
|
285
|
+
);
|
|
286
|
+
this.logger?.error(mastraError.toString());
|
|
287
|
+
this.logger?.trackException(mastraError);
|
|
288
|
+
return null;
|
|
484
289
|
}
|
|
485
|
-
return processedRecord;
|
|
486
290
|
}
|
|
487
|
-
async
|
|
488
|
-
const fullTableName = this.getTableName(
|
|
489
|
-
const
|
|
291
|
+
async saveResource({ resource }) {
|
|
292
|
+
const fullTableName = this.operations.getTableName(TABLE_RESOURCES);
|
|
293
|
+
const resourceToSave = {
|
|
294
|
+
id: resource.id,
|
|
295
|
+
workingMemory: resource.workingMemory,
|
|
296
|
+
metadata: resource.metadata ? JSON.stringify(resource.metadata) : null,
|
|
297
|
+
createdAt: resource.createdAt,
|
|
298
|
+
updatedAt: resource.updatedAt
|
|
299
|
+
};
|
|
300
|
+
const processedRecord = await this.operations.processRecord(resourceToSave);
|
|
490
301
|
const columns = Object.keys(processedRecord);
|
|
491
302
|
const values = Object.values(processedRecord);
|
|
492
|
-
const
|
|
303
|
+
const updateMap = {
|
|
304
|
+
workingMemory: "excluded.workingMemory",
|
|
305
|
+
metadata: "excluded.metadata",
|
|
306
|
+
createdAt: "excluded.createdAt",
|
|
307
|
+
updatedAt: "excluded.updatedAt"
|
|
308
|
+
};
|
|
309
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values, ["id"], updateMap);
|
|
493
310
|
const { sql, params } = query.build();
|
|
494
311
|
try {
|
|
495
|
-
await this.executeQuery({ sql, params });
|
|
312
|
+
await this.operations.executeQuery({ sql, params });
|
|
313
|
+
return resource;
|
|
496
314
|
} catch (error) {
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
315
|
+
throw new MastraError(
|
|
316
|
+
{
|
|
317
|
+
id: "CLOUDFLARE_D1_STORAGE_SAVE_RESOURCE_ERROR",
|
|
318
|
+
domain: ErrorDomain.STORAGE,
|
|
319
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
320
|
+
text: `Failed to save resource to ${fullTableName}: ${error instanceof Error ? error.message : String(error)}`,
|
|
321
|
+
details: { resourceId: resource.id }
|
|
322
|
+
},
|
|
323
|
+
error
|
|
324
|
+
);
|
|
500
325
|
}
|
|
501
326
|
}
|
|
502
|
-
async
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
327
|
+
async updateResource({
|
|
328
|
+
resourceId,
|
|
329
|
+
workingMemory,
|
|
330
|
+
metadata
|
|
331
|
+
}) {
|
|
332
|
+
const existingResource = await this.getResourceById({ resourceId });
|
|
333
|
+
if (!existingResource) {
|
|
334
|
+
const newResource = {
|
|
335
|
+
id: resourceId,
|
|
336
|
+
workingMemory,
|
|
337
|
+
metadata: metadata || {},
|
|
338
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
339
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
340
|
+
};
|
|
341
|
+
return this.saveResource({ resource: newResource });
|
|
513
342
|
}
|
|
514
|
-
|
|
343
|
+
const updatedAt = /* @__PURE__ */ new Date();
|
|
344
|
+
const updatedResource = {
|
|
345
|
+
...existingResource,
|
|
346
|
+
workingMemory: workingMemory !== void 0 ? workingMemory : existingResource.workingMemory,
|
|
347
|
+
metadata: {
|
|
348
|
+
...existingResource.metadata,
|
|
349
|
+
...metadata
|
|
350
|
+
},
|
|
351
|
+
updatedAt
|
|
352
|
+
};
|
|
353
|
+
const fullTableName = this.operations.getTableName(TABLE_RESOURCES);
|
|
354
|
+
const columns = ["workingMemory", "metadata", "updatedAt"];
|
|
355
|
+
const values = [updatedResource.workingMemory, JSON.stringify(updatedResource.metadata), updatedAt.toISOString()];
|
|
356
|
+
const query = createSqlBuilder().update(fullTableName, columns, values).where("id = ?", resourceId);
|
|
515
357
|
const { sql, params } = query.build();
|
|
516
358
|
try {
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
const processedResult = {};
|
|
520
|
-
for (const [key, value] of Object.entries(result)) {
|
|
521
|
-
processedResult[key] = this.deserializeValue(value);
|
|
522
|
-
}
|
|
523
|
-
return processedResult;
|
|
359
|
+
await this.operations.executeQuery({ sql, params });
|
|
360
|
+
return updatedResource;
|
|
524
361
|
} catch (error) {
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
362
|
+
throw new MastraError(
|
|
363
|
+
{
|
|
364
|
+
id: "CLOUDFLARE_D1_STORAGE_UPDATE_RESOURCE_ERROR",
|
|
365
|
+
domain: ErrorDomain.STORAGE,
|
|
366
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
367
|
+
text: `Failed to update resource ${resourceId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
368
|
+
details: { resourceId }
|
|
369
|
+
},
|
|
370
|
+
error
|
|
371
|
+
);
|
|
529
372
|
}
|
|
530
373
|
}
|
|
531
374
|
async getThreadById({ threadId }) {
|
|
532
|
-
const thread = await this.load({
|
|
375
|
+
const thread = await this.operations.load({
|
|
533
376
|
tableName: TABLE_THREADS,
|
|
534
377
|
keys: { id: threadId }
|
|
535
378
|
});
|
|
@@ -537,47 +380,97 @@ var D1Store = class extends MastraStorage {
|
|
|
537
380
|
try {
|
|
538
381
|
return {
|
|
539
382
|
...thread,
|
|
540
|
-
createdAt:
|
|
541
|
-
updatedAt:
|
|
383
|
+
createdAt: ensureDate(thread.createdAt),
|
|
384
|
+
updatedAt: ensureDate(thread.updatedAt),
|
|
542
385
|
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
|
|
543
386
|
};
|
|
544
387
|
} catch (error) {
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
388
|
+
const mastraError = new MastraError(
|
|
389
|
+
{
|
|
390
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_THREAD_BY_ID_ERROR",
|
|
391
|
+
domain: ErrorDomain.STORAGE,
|
|
392
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
393
|
+
text: `Error processing thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
394
|
+
details: { threadId }
|
|
395
|
+
},
|
|
396
|
+
error
|
|
397
|
+
);
|
|
398
|
+
this.logger?.error(mastraError.toString());
|
|
399
|
+
this.logger?.trackException(mastraError);
|
|
548
400
|
return null;
|
|
549
401
|
}
|
|
550
402
|
}
|
|
551
|
-
async
|
|
552
|
-
const
|
|
403
|
+
async listThreadsByResourceId(args) {
|
|
404
|
+
const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
|
|
405
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
406
|
+
if (page < 0) {
|
|
407
|
+
throw new MastraError(
|
|
408
|
+
{
|
|
409
|
+
id: "STORAGE_CLOUDFLARE_D1_LIST_THREADS_BY_RESOURCE_ID_INVALID_PAGE",
|
|
410
|
+
domain: ErrorDomain.STORAGE,
|
|
411
|
+
category: ErrorCategory.USER,
|
|
412
|
+
details: { page }
|
|
413
|
+
},
|
|
414
|
+
new Error("page must be >= 0")
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
418
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
419
|
+
const fullTableName = this.operations.getTableName(TABLE_THREADS);
|
|
420
|
+
const mapRowToStorageThreadType = (row) => ({
|
|
421
|
+
...row,
|
|
422
|
+
createdAt: ensureDate(row.createdAt),
|
|
423
|
+
updatedAt: ensureDate(row.updatedAt),
|
|
424
|
+
metadata: typeof row.metadata === "string" ? JSON.parse(row.metadata || "{}") : row.metadata || {}
|
|
425
|
+
});
|
|
553
426
|
try {
|
|
554
|
-
const
|
|
555
|
-
const
|
|
556
|
-
const
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
427
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("resourceId = ?", resourceId);
|
|
428
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
429
|
+
const total = Number(countResult?.[0]?.count ?? 0);
|
|
430
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
431
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId).orderBy(field, direction).limit(limitValue).offset(offset);
|
|
432
|
+
const results = await this.operations.executeQuery(selectQuery.build());
|
|
433
|
+
const threads = results.map(mapRowToStorageThreadType);
|
|
434
|
+
return {
|
|
435
|
+
threads,
|
|
436
|
+
total,
|
|
437
|
+
page,
|
|
438
|
+
perPage: perPageForResponse,
|
|
439
|
+
hasMore: perPageInput === false ? false : offset + perPage < total
|
|
440
|
+
};
|
|
563
441
|
} catch (error) {
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
442
|
+
const mastraError = new MastraError(
|
|
443
|
+
{
|
|
444
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_THREADS_BY_RESOURCE_ID_ERROR",
|
|
445
|
+
domain: ErrorDomain.STORAGE,
|
|
446
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
447
|
+
text: `Error getting threads by resourceId ${resourceId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
448
|
+
details: { resourceId }
|
|
449
|
+
},
|
|
450
|
+
error
|
|
451
|
+
);
|
|
452
|
+
this.logger?.error(mastraError.toString());
|
|
453
|
+
this.logger?.trackException(mastraError);
|
|
454
|
+
return {
|
|
455
|
+
threads: [],
|
|
456
|
+
total: 0,
|
|
457
|
+
page,
|
|
458
|
+
perPage: perPageForResponse,
|
|
459
|
+
hasMore: false
|
|
460
|
+
};
|
|
568
461
|
}
|
|
569
462
|
}
|
|
570
463
|
async saveThread({ thread }) {
|
|
571
|
-
const fullTableName = this.getTableName(TABLE_THREADS);
|
|
464
|
+
const fullTableName = this.operations.getTableName(TABLE_THREADS);
|
|
572
465
|
const threadToSave = {
|
|
573
466
|
id: thread.id,
|
|
574
467
|
resourceId: thread.resourceId,
|
|
575
468
|
title: thread.title,
|
|
576
469
|
metadata: thread.metadata ? JSON.stringify(thread.metadata) : null,
|
|
577
|
-
createdAt: thread.createdAt,
|
|
578
|
-
updatedAt: thread.updatedAt
|
|
470
|
+
createdAt: thread.createdAt.toISOString(),
|
|
471
|
+
updatedAt: thread.updatedAt.toISOString()
|
|
579
472
|
};
|
|
580
|
-
const processedRecord = await this.processRecord(threadToSave);
|
|
473
|
+
const processedRecord = await this.operations.processRecord(threadToSave);
|
|
581
474
|
const columns = Object.keys(processedRecord);
|
|
582
475
|
const values = Object.values(processedRecord);
|
|
583
476
|
const updateMap = {
|
|
@@ -590,12 +483,19 @@ var D1Store = class extends MastraStorage {
|
|
|
590
483
|
const query = createSqlBuilder().insert(fullTableName, columns, values, ["id"], updateMap);
|
|
591
484
|
const { sql, params } = query.build();
|
|
592
485
|
try {
|
|
593
|
-
await this.executeQuery({ sql, params });
|
|
486
|
+
await this.operations.executeQuery({ sql, params });
|
|
594
487
|
return thread;
|
|
595
488
|
} catch (error) {
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
489
|
+
throw new MastraError(
|
|
490
|
+
{
|
|
491
|
+
id: "CLOUDFLARE_D1_STORAGE_SAVE_THREAD_ERROR",
|
|
492
|
+
domain: ErrorDomain.STORAGE,
|
|
493
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
494
|
+
text: `Failed to save thread to ${fullTableName}: ${error instanceof Error ? error.message : String(error)}`,
|
|
495
|
+
details: { threadId: thread.id }
|
|
496
|
+
},
|
|
497
|
+
error
|
|
498
|
+
);
|
|
599
499
|
}
|
|
600
500
|
}
|
|
601
501
|
async updateThread({
|
|
@@ -604,20 +504,21 @@ var D1Store = class extends MastraStorage {
|
|
|
604
504
|
metadata
|
|
605
505
|
}) {
|
|
606
506
|
const thread = await this.getThreadById({ threadId: id });
|
|
607
|
-
if (!thread) {
|
|
608
|
-
throw new Error(`Thread ${id} not found`);
|
|
609
|
-
}
|
|
610
|
-
const fullTableName = this.getTableName(TABLE_THREADS);
|
|
611
|
-
const mergedMetadata = {
|
|
612
|
-
...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
613
|
-
...metadata
|
|
614
|
-
};
|
|
615
|
-
const columns = ["title", "metadata", "updatedAt"];
|
|
616
|
-
const values = [title, JSON.stringify(mergedMetadata), (/* @__PURE__ */ new Date()).toISOString()];
|
|
617
|
-
const query = createSqlBuilder().update(fullTableName, columns, values).where("id = ?", id);
|
|
618
|
-
const { sql, params } = query.build();
|
|
619
507
|
try {
|
|
620
|
-
|
|
508
|
+
if (!thread) {
|
|
509
|
+
throw new Error(`Thread ${id} not found`);
|
|
510
|
+
}
|
|
511
|
+
const fullTableName = this.operations.getTableName(TABLE_THREADS);
|
|
512
|
+
const mergedMetadata = {
|
|
513
|
+
...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
514
|
+
...metadata
|
|
515
|
+
};
|
|
516
|
+
const updatedAt = /* @__PURE__ */ new Date();
|
|
517
|
+
const columns = ["title", "metadata", "updatedAt"];
|
|
518
|
+
const values = [title, JSON.stringify(mergedMetadata), updatedAt.toISOString()];
|
|
519
|
+
const query = createSqlBuilder().update(fullTableName, columns, values).where("id = ?", id);
|
|
520
|
+
const { sql, params } = query.build();
|
|
521
|
+
await this.operations.executeQuery({ sql, params });
|
|
621
522
|
return {
|
|
622
523
|
...thread,
|
|
623
524
|
title,
|
|
@@ -625,41 +526,64 @@ var D1Store = class extends MastraStorage {
|
|
|
625
526
|
...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
626
527
|
...metadata
|
|
627
528
|
},
|
|
628
|
-
updatedAt
|
|
529
|
+
updatedAt
|
|
629
530
|
};
|
|
630
531
|
} catch (error) {
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
532
|
+
throw new MastraError(
|
|
533
|
+
{
|
|
534
|
+
id: "CLOUDFLARE_D1_STORAGE_UPDATE_THREAD_ERROR",
|
|
535
|
+
domain: ErrorDomain.STORAGE,
|
|
536
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
537
|
+
text: `Failed to update thread ${id}: ${error instanceof Error ? error.message : String(error)}`,
|
|
538
|
+
details: { threadId: id }
|
|
539
|
+
},
|
|
540
|
+
error
|
|
541
|
+
);
|
|
634
542
|
}
|
|
635
543
|
}
|
|
636
544
|
async deleteThread({ threadId }) {
|
|
637
|
-
const fullTableName = this.getTableName(TABLE_THREADS);
|
|
545
|
+
const fullTableName = this.operations.getTableName(TABLE_THREADS);
|
|
638
546
|
try {
|
|
639
547
|
const deleteThreadQuery = createSqlBuilder().delete(fullTableName).where("id = ?", threadId);
|
|
640
548
|
const { sql: threadSql, params: threadParams } = deleteThreadQuery.build();
|
|
641
|
-
await this.executeQuery({ sql: threadSql, params: threadParams });
|
|
642
|
-
const messagesTableName = this.getTableName(TABLE_MESSAGES);
|
|
549
|
+
await this.operations.executeQuery({ sql: threadSql, params: threadParams });
|
|
550
|
+
const messagesTableName = this.operations.getTableName(TABLE_MESSAGES);
|
|
643
551
|
const deleteMessagesQuery = createSqlBuilder().delete(messagesTableName).where("thread_id = ?", threadId);
|
|
644
552
|
const { sql: messagesSql, params: messagesParams } = deleteMessagesQuery.build();
|
|
645
|
-
await this.executeQuery({ sql: messagesSql, params: messagesParams });
|
|
553
|
+
await this.operations.executeQuery({ sql: messagesSql, params: messagesParams });
|
|
646
554
|
} catch (error) {
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
555
|
+
throw new MastraError(
|
|
556
|
+
{
|
|
557
|
+
id: "CLOUDFLARE_D1_STORAGE_DELETE_THREAD_ERROR",
|
|
558
|
+
domain: ErrorDomain.STORAGE,
|
|
559
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
560
|
+
text: `Failed to delete thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
561
|
+
details: { threadId }
|
|
562
|
+
},
|
|
563
|
+
error
|
|
564
|
+
);
|
|
651
565
|
}
|
|
652
566
|
}
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
if (messages.length === 0) return [];
|
|
567
|
+
async saveMessages(args) {
|
|
568
|
+
const { messages } = args;
|
|
569
|
+
if (messages.length === 0) return { messages: [] };
|
|
656
570
|
try {
|
|
657
571
|
const now = /* @__PURE__ */ new Date();
|
|
572
|
+
const threadId = messages[0]?.threadId;
|
|
658
573
|
for (const [i, message] of messages.entries()) {
|
|
659
574
|
if (!message.id) throw new Error(`Message at index ${i} missing id`);
|
|
660
|
-
if (!message.threadId)
|
|
661
|
-
|
|
662
|
-
|
|
575
|
+
if (!message.threadId) {
|
|
576
|
+
throw new Error(`Message at index ${i} missing threadId`);
|
|
577
|
+
}
|
|
578
|
+
if (!message.content) {
|
|
579
|
+
throw new Error(`Message at index ${i} missing content`);
|
|
580
|
+
}
|
|
581
|
+
if (!message.role) {
|
|
582
|
+
throw new Error(`Message at index ${i} missing role`);
|
|
583
|
+
}
|
|
584
|
+
if (!message.resourceId) {
|
|
585
|
+
throw new Error(`Message at index ${i} missing resourceId`);
|
|
586
|
+
}
|
|
663
587
|
const thread = await this.getThreadById({ threadId: message.threadId });
|
|
664
588
|
if (!thread) {
|
|
665
589
|
throw new Error(`Thread ${message.threadId} not found`);
|
|
@@ -673,158 +597,725 @@ var D1Store = class extends MastraStorage {
|
|
|
673
597
|
content: typeof message.content === "string" ? message.content : JSON.stringify(message.content),
|
|
674
598
|
createdAt: createdAt.toISOString(),
|
|
675
599
|
role: message.role,
|
|
676
|
-
type: message.type
|
|
600
|
+
type: message.type || "v2",
|
|
601
|
+
resourceId: message.resourceId
|
|
677
602
|
};
|
|
678
603
|
});
|
|
679
|
-
await
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
604
|
+
await Promise.all([
|
|
605
|
+
this.operations.batchUpsert({
|
|
606
|
+
tableName: TABLE_MESSAGES,
|
|
607
|
+
records: messagesToInsert
|
|
608
|
+
}),
|
|
609
|
+
// Update thread's updatedAt timestamp
|
|
610
|
+
this.operations.executeQuery({
|
|
611
|
+
sql: `UPDATE ${this.operations.getTableName(TABLE_THREADS)} SET updatedAt = ? WHERE id = ?`,
|
|
612
|
+
params: [now.toISOString(), threadId]
|
|
613
|
+
})
|
|
614
|
+
]);
|
|
683
615
|
this.logger.debug(`Saved ${messages.length} messages`);
|
|
684
|
-
|
|
616
|
+
const list = new MessageList().add(messages, "memory");
|
|
617
|
+
return { messages: list.get.all.db() };
|
|
685
618
|
} catch (error) {
|
|
686
|
-
|
|
687
|
-
|
|
619
|
+
throw new MastraError(
|
|
620
|
+
{
|
|
621
|
+
id: "CLOUDFLARE_D1_STORAGE_SAVE_MESSAGES_ERROR",
|
|
622
|
+
domain: ErrorDomain.STORAGE,
|
|
623
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
624
|
+
text: `Failed to save messages: ${error instanceof Error ? error.message : String(error)}`
|
|
625
|
+
},
|
|
626
|
+
error
|
|
627
|
+
);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
async _getIncludedMessages(threadId, include) {
|
|
631
|
+
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
632
|
+
if (!include) return null;
|
|
633
|
+
const unionQueries = [];
|
|
634
|
+
const params = [];
|
|
635
|
+
let paramIdx = 1;
|
|
636
|
+
for (const inc of include) {
|
|
637
|
+
const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
|
|
638
|
+
const searchId = inc.threadId || threadId;
|
|
639
|
+
unionQueries.push(`
|
|
640
|
+
SELECT * FROM (
|
|
641
|
+
WITH ordered_messages AS (
|
|
642
|
+
SELECT
|
|
643
|
+
*,
|
|
644
|
+
ROW_NUMBER() OVER (ORDER BY createdAt ASC) AS row_num
|
|
645
|
+
FROM ${this.operations.getTableName(TABLE_MESSAGES)}
|
|
646
|
+
WHERE thread_id = ?
|
|
647
|
+
)
|
|
648
|
+
SELECT
|
|
649
|
+
m.id,
|
|
650
|
+
m.content,
|
|
651
|
+
m.role,
|
|
652
|
+
m.type,
|
|
653
|
+
m.createdAt,
|
|
654
|
+
m.thread_id AS threadId,
|
|
655
|
+
m.resourceId
|
|
656
|
+
FROM ordered_messages m
|
|
657
|
+
WHERE m.id = ?
|
|
658
|
+
OR EXISTS (
|
|
659
|
+
SELECT 1 FROM ordered_messages target
|
|
660
|
+
WHERE target.id = ?
|
|
661
|
+
AND (
|
|
662
|
+
(m.row_num <= target.row_num + ? AND m.row_num > target.row_num)
|
|
663
|
+
OR
|
|
664
|
+
(m.row_num >= target.row_num - ? AND m.row_num < target.row_num)
|
|
665
|
+
)
|
|
666
|
+
)
|
|
667
|
+
) AS query_${paramIdx}
|
|
668
|
+
`);
|
|
669
|
+
params.push(searchId, id, id, withNextMessages, withPreviousMessages);
|
|
670
|
+
paramIdx++;
|
|
671
|
+
}
|
|
672
|
+
const finalQuery = unionQueries.join(" UNION ALL ") + " ORDER BY createdAt ASC";
|
|
673
|
+
const messages = await this.operations.executeQuery({ sql: finalQuery, params });
|
|
674
|
+
if (!Array.isArray(messages)) {
|
|
675
|
+
return [];
|
|
688
676
|
}
|
|
677
|
+
const processedMessages = messages.map((message) => {
|
|
678
|
+
const processedMsg = {};
|
|
679
|
+
for (const [key, value] of Object.entries(message)) {
|
|
680
|
+
if (key === `type` && value === `v2`) continue;
|
|
681
|
+
processedMsg[key] = deserializeValue(value);
|
|
682
|
+
}
|
|
683
|
+
return processedMsg;
|
|
684
|
+
});
|
|
685
|
+
return processedMessages;
|
|
689
686
|
}
|
|
690
|
-
async
|
|
691
|
-
|
|
692
|
-
const
|
|
693
|
-
const include = selectBy?.include || [];
|
|
687
|
+
async listMessagesById({ messageIds }) {
|
|
688
|
+
if (messageIds.length === 0) return { messages: [] };
|
|
689
|
+
const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
|
|
694
690
|
const messages = [];
|
|
695
691
|
try {
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
const nextMax = Math.max(...include.map((i) => i.withNextMessages || 0));
|
|
699
|
-
const includeIds = include.map((i) => i.id);
|
|
700
|
-
const sql2 = `
|
|
701
|
-
WITH ordered_messages AS (
|
|
702
|
-
SELECT
|
|
703
|
-
*,
|
|
704
|
-
ROW_NUMBER() OVER (ORDER BY createdAt DESC) AS row_num
|
|
705
|
-
FROM ${fullTableName}
|
|
706
|
-
WHERE thread_id = ?
|
|
707
|
-
)
|
|
708
|
-
SELECT
|
|
709
|
-
m.id,
|
|
710
|
-
m.content,
|
|
711
|
-
m.role,
|
|
712
|
-
m.type,
|
|
713
|
-
m.createdAt,
|
|
714
|
-
m.thread_id AS "threadId"
|
|
715
|
-
FROM ordered_messages m
|
|
716
|
-
WHERE m.id IN (${includeIds.map(() => "?").join(",")})
|
|
717
|
-
OR EXISTS (
|
|
718
|
-
SELECT 1 FROM ordered_messages target
|
|
719
|
-
WHERE target.id IN (${includeIds.map(() => "?").join(",")})
|
|
720
|
-
AND (
|
|
721
|
-
(m.row_num <= target.row_num + ? AND m.row_num > target.row_num)
|
|
722
|
-
OR
|
|
723
|
-
(m.row_num >= target.row_num - ? AND m.row_num < target.row_num)
|
|
724
|
-
)
|
|
725
|
-
)
|
|
726
|
-
ORDER BY m.createdAt DESC
|
|
727
|
-
`;
|
|
728
|
-
const params2 = [
|
|
729
|
-
threadId,
|
|
730
|
-
...includeIds,
|
|
731
|
-
// for m.id IN (...)
|
|
732
|
-
...includeIds,
|
|
733
|
-
// for target.id IN (...)
|
|
734
|
-
prevMax,
|
|
735
|
-
nextMax
|
|
736
|
-
];
|
|
737
|
-
const includeResult = await this.executeQuery({ sql: sql2, params: params2 });
|
|
738
|
-
if (Array.isArray(includeResult)) messages.push(...includeResult);
|
|
739
|
-
}
|
|
740
|
-
const excludeIds = messages.map((m) => m.id);
|
|
741
|
-
let query = createSqlBuilder().select(["id", "content", "role", "type", '"createdAt"', 'thread_id AS "threadId"']).from(fullTableName).where("thread_id = ?", threadId).andWhere(`id NOT IN (${excludeIds.map(() => "?").join(",")})`, ...excludeIds).orderBy("createdAt", "DESC").limit(limit);
|
|
692
|
+
const query = createSqlBuilder().select(["id", "content", "role", "type", "createdAt", "thread_id AS threadId", "resourceId"]).from(fullTableName).where(`id in (${messageIds.map(() => "?").join(",")})`, ...messageIds);
|
|
693
|
+
query.orderBy("createdAt", "DESC");
|
|
742
694
|
const { sql, params } = query.build();
|
|
743
|
-
const result = await this.executeQuery({ sql, params });
|
|
695
|
+
const result = await this.operations.executeQuery({ sql, params });
|
|
744
696
|
if (Array.isArray(result)) messages.push(...result);
|
|
745
|
-
messages.sort((a, b) => {
|
|
746
|
-
const aRecord = a;
|
|
747
|
-
const bRecord = b;
|
|
748
|
-
const timeA = new Date(aRecord.createdAt).getTime();
|
|
749
|
-
const timeB = new Date(bRecord.createdAt).getTime();
|
|
750
|
-
return timeA - timeB;
|
|
751
|
-
});
|
|
752
697
|
const processedMessages = messages.map((message) => {
|
|
753
698
|
const processedMsg = {};
|
|
754
699
|
for (const [key, value] of Object.entries(message)) {
|
|
755
|
-
|
|
700
|
+
if (key === `type` && value === `v2`) continue;
|
|
701
|
+
processedMsg[key] = deserializeValue(value);
|
|
756
702
|
}
|
|
757
703
|
return processedMsg;
|
|
758
704
|
});
|
|
759
|
-
this.logger.debug(`Retrieved ${messages.length} messages
|
|
760
|
-
|
|
705
|
+
this.logger.debug(`Retrieved ${messages.length} messages`);
|
|
706
|
+
const list = new MessageList().add(processedMessages, "memory");
|
|
707
|
+
return { messages: list.get.all.db() };
|
|
761
708
|
} catch (error) {
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
709
|
+
const mastraError = new MastraError(
|
|
710
|
+
{
|
|
711
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_MESSAGES_BY_ID_ERROR",
|
|
712
|
+
domain: ErrorDomain.STORAGE,
|
|
713
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
714
|
+
text: `Failed to retrieve messages by ID: ${error instanceof Error ? error.message : String(error)}`,
|
|
715
|
+
details: { messageIds: JSON.stringify(messageIds) }
|
|
716
|
+
},
|
|
717
|
+
error
|
|
718
|
+
);
|
|
719
|
+
this.logger?.error(mastraError.toString());
|
|
720
|
+
this.logger?.trackException(mastraError);
|
|
721
|
+
throw mastraError;
|
|
767
722
|
}
|
|
768
723
|
}
|
|
769
|
-
async
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
const
|
|
794
|
-
const
|
|
795
|
-
snapshot: "excluded.snapshot",
|
|
796
|
-
updatedAt: "excluded.updatedAt"
|
|
797
|
-
};
|
|
798
|
-
this.logger.debug("Persisting workflow snapshot", { workflowName, runId });
|
|
799
|
-
const query = createSqlBuilder().insert(fullTableName, columns, values, ["workflow_name", "run_id"], updateMap);
|
|
800
|
-
const { sql, params } = query.build();
|
|
724
|
+
async listMessages(args) {
|
|
725
|
+
const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
|
|
726
|
+
if (!threadId.trim()) {
|
|
727
|
+
throw new MastraError(
|
|
728
|
+
{
|
|
729
|
+
id: "STORAGE_CLOUDFLARE_D1_LIST_MESSAGES_INVALID_THREAD_ID",
|
|
730
|
+
domain: ErrorDomain.STORAGE,
|
|
731
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
732
|
+
details: { threadId }
|
|
733
|
+
},
|
|
734
|
+
new Error("threadId must be a non-empty string")
|
|
735
|
+
);
|
|
736
|
+
}
|
|
737
|
+
if (page < 0) {
|
|
738
|
+
throw new MastraError(
|
|
739
|
+
{
|
|
740
|
+
id: "STORAGE_CLOUDFLARE_D1_LIST_MESSAGES_INVALID_PAGE",
|
|
741
|
+
domain: ErrorDomain.STORAGE,
|
|
742
|
+
category: ErrorCategory.USER,
|
|
743
|
+
details: { page }
|
|
744
|
+
},
|
|
745
|
+
new Error("page must be >= 0")
|
|
746
|
+
);
|
|
747
|
+
}
|
|
748
|
+
const perPage = normalizePerPage(perPageInput, 40);
|
|
749
|
+
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
801
750
|
try {
|
|
802
|
-
|
|
751
|
+
const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
|
|
752
|
+
let query = `
|
|
753
|
+
SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
|
|
754
|
+
FROM ${fullTableName}
|
|
755
|
+
WHERE thread_id = ?
|
|
756
|
+
`;
|
|
757
|
+
const queryParams = [threadId];
|
|
758
|
+
if (resourceId) {
|
|
759
|
+
query += ` AND resourceId = ?`;
|
|
760
|
+
queryParams.push(resourceId);
|
|
761
|
+
}
|
|
762
|
+
const dateRange = filter?.dateRange;
|
|
763
|
+
if (dateRange?.start) {
|
|
764
|
+
const startDate = dateRange.start instanceof Date ? serializeDate(dateRange.start) : serializeDate(new Date(dateRange.start));
|
|
765
|
+
query += ` AND createdAt >= ?`;
|
|
766
|
+
queryParams.push(startDate);
|
|
767
|
+
}
|
|
768
|
+
if (dateRange?.end) {
|
|
769
|
+
const endDate = dateRange.end instanceof Date ? serializeDate(dateRange.end) : serializeDate(new Date(dateRange.end));
|
|
770
|
+
query += ` AND createdAt <= ?`;
|
|
771
|
+
queryParams.push(endDate);
|
|
772
|
+
}
|
|
773
|
+
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
774
|
+
query += ` ORDER BY "${field}" ${direction}`;
|
|
775
|
+
if (perPage !== Number.MAX_SAFE_INTEGER) {
|
|
776
|
+
query += ` LIMIT ? OFFSET ?`;
|
|
777
|
+
queryParams.push(perPage, offset);
|
|
778
|
+
}
|
|
779
|
+
const results = await this.operations.executeQuery({ sql: query, params: queryParams });
|
|
780
|
+
const paginatedMessages = (isArrayOfRecords(results) ? results : []).map((message) => {
|
|
781
|
+
const processedMsg = {};
|
|
782
|
+
for (const [key, value] of Object.entries(message)) {
|
|
783
|
+
if (key === `type` && value === `v2`) continue;
|
|
784
|
+
processedMsg[key] = deserializeValue(value);
|
|
785
|
+
}
|
|
786
|
+
return processedMsg;
|
|
787
|
+
});
|
|
788
|
+
const paginatedCount = paginatedMessages.length;
|
|
789
|
+
let countQuery = `SELECT count() as count FROM ${fullTableName} WHERE thread_id = ?`;
|
|
790
|
+
const countParams = [threadId];
|
|
791
|
+
if (resourceId) {
|
|
792
|
+
countQuery += ` AND resourceId = ?`;
|
|
793
|
+
countParams.push(resourceId);
|
|
794
|
+
}
|
|
795
|
+
if (dateRange?.start) {
|
|
796
|
+
const startDate = dateRange.start instanceof Date ? serializeDate(dateRange.start) : serializeDate(new Date(dateRange.start));
|
|
797
|
+
countQuery += ` AND createdAt >= ?`;
|
|
798
|
+
countParams.push(startDate);
|
|
799
|
+
}
|
|
800
|
+
if (dateRange?.end) {
|
|
801
|
+
const endDate = dateRange.end instanceof Date ? serializeDate(dateRange.end) : serializeDate(new Date(dateRange.end));
|
|
802
|
+
countQuery += ` AND createdAt <= ?`;
|
|
803
|
+
countParams.push(endDate);
|
|
804
|
+
}
|
|
805
|
+
const countResult = await this.operations.executeQuery({ sql: countQuery, params: countParams });
|
|
806
|
+
const total = Number(countResult[0]?.count ?? 0);
|
|
807
|
+
if (total === 0 && paginatedCount === 0 && (!include || include.length === 0)) {
|
|
808
|
+
return {
|
|
809
|
+
messages: [],
|
|
810
|
+
total: 0,
|
|
811
|
+
page,
|
|
812
|
+
perPage: perPageForResponse,
|
|
813
|
+
hasMore: false
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
const messageIds = new Set(paginatedMessages.map((m) => m.id));
|
|
817
|
+
let includeMessages = [];
|
|
818
|
+
if (include && include.length > 0) {
|
|
819
|
+
const includeResult = await this._getIncludedMessages(threadId, include);
|
|
820
|
+
if (Array.isArray(includeResult)) {
|
|
821
|
+
includeMessages = includeResult;
|
|
822
|
+
for (const includeMsg of includeMessages) {
|
|
823
|
+
if (!messageIds.has(includeMsg.id)) {
|
|
824
|
+
paginatedMessages.push(includeMsg);
|
|
825
|
+
messageIds.add(includeMsg.id);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
const list = new MessageList().add(paginatedMessages, "memory");
|
|
831
|
+
let finalMessages = list.get.all.db();
|
|
832
|
+
finalMessages = finalMessages.sort((a, b) => {
|
|
833
|
+
const isDateField = field === "createdAt" || field === "updatedAt";
|
|
834
|
+
const aValue = isDateField ? new Date(a[field]).getTime() : a[field];
|
|
835
|
+
const bValue = isDateField ? new Date(b[field]).getTime() : b[field];
|
|
836
|
+
if (aValue === bValue) {
|
|
837
|
+
return a.id.localeCompare(b.id);
|
|
838
|
+
}
|
|
839
|
+
if (typeof aValue === "number" && typeof bValue === "number") {
|
|
840
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
841
|
+
}
|
|
842
|
+
return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
|
|
843
|
+
});
|
|
844
|
+
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
845
|
+
const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
|
|
846
|
+
const hasMore = perPageInput === false ? false : allThreadMessagesReturned ? false : offset + paginatedCount < total;
|
|
847
|
+
return {
|
|
848
|
+
messages: finalMessages,
|
|
849
|
+
total,
|
|
850
|
+
page,
|
|
851
|
+
perPage: perPageForResponse,
|
|
852
|
+
hasMore
|
|
853
|
+
};
|
|
803
854
|
} catch (error) {
|
|
804
|
-
|
|
805
|
-
|
|
855
|
+
const mastraError = new MastraError(
|
|
856
|
+
{
|
|
857
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_MESSAGES_ERROR",
|
|
858
|
+
domain: ErrorDomain.STORAGE,
|
|
859
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
860
|
+
text: `Failed to list messages for thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
861
|
+
details: {
|
|
862
|
+
threadId,
|
|
863
|
+
resourceId: resourceId ?? ""
|
|
864
|
+
}
|
|
865
|
+
},
|
|
866
|
+
error
|
|
867
|
+
);
|
|
868
|
+
this.logger?.error?.(mastraError.toString());
|
|
869
|
+
this.logger?.trackException?.(mastraError);
|
|
870
|
+
return {
|
|
871
|
+
messages: [],
|
|
872
|
+
total: 0,
|
|
873
|
+
page,
|
|
874
|
+
perPage: perPageForResponse,
|
|
875
|
+
hasMore: false
|
|
876
|
+
};
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
async updateMessages(args) {
|
|
880
|
+
const { messages } = args;
|
|
881
|
+
this.logger.debug("Updating messages", { count: messages.length });
|
|
882
|
+
if (!messages.length) {
|
|
883
|
+
return [];
|
|
884
|
+
}
|
|
885
|
+
const messageIds = messages.map((m) => m.id);
|
|
886
|
+
const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
|
|
887
|
+
const threadsTableName = this.operations.getTableName(TABLE_THREADS);
|
|
888
|
+
try {
|
|
889
|
+
const placeholders = messageIds.map(() => "?").join(",");
|
|
890
|
+
const selectQuery = `SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId FROM ${fullTableName} WHERE id IN (${placeholders})`;
|
|
891
|
+
const existingMessages = await this.operations.executeQuery({ sql: selectQuery, params: messageIds });
|
|
892
|
+
if (existingMessages.length === 0) {
|
|
893
|
+
return [];
|
|
894
|
+
}
|
|
895
|
+
const parsedExistingMessages = existingMessages.map((msg) => {
|
|
896
|
+
if (typeof msg.content === "string") {
|
|
897
|
+
try {
|
|
898
|
+
msg.content = JSON.parse(msg.content);
|
|
899
|
+
} catch {
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
return msg;
|
|
806
903
|
});
|
|
807
|
-
|
|
904
|
+
const threadIdsToUpdate = /* @__PURE__ */ new Set();
|
|
905
|
+
const updateQueries = [];
|
|
906
|
+
for (const existingMessage of parsedExistingMessages) {
|
|
907
|
+
const updatePayload = messages.find((m) => m.id === existingMessage.id);
|
|
908
|
+
if (!updatePayload) continue;
|
|
909
|
+
const { id, ...fieldsToUpdate } = updatePayload;
|
|
910
|
+
if (Object.keys(fieldsToUpdate).length === 0) continue;
|
|
911
|
+
threadIdsToUpdate.add(existingMessage.threadId);
|
|
912
|
+
if ("threadId" in updatePayload && updatePayload.threadId && updatePayload.threadId !== existingMessage.threadId) {
|
|
913
|
+
threadIdsToUpdate.add(updatePayload.threadId);
|
|
914
|
+
}
|
|
915
|
+
const setClauses = [];
|
|
916
|
+
const values = [];
|
|
917
|
+
const updatableFields = { ...fieldsToUpdate };
|
|
918
|
+
if (updatableFields.content) {
|
|
919
|
+
const existingContent = existingMessage.content || {};
|
|
920
|
+
const newContent = {
|
|
921
|
+
...existingContent,
|
|
922
|
+
...updatableFields.content,
|
|
923
|
+
// Deep merge metadata if it exists on both
|
|
924
|
+
...existingContent?.metadata && updatableFields.content.metadata ? {
|
|
925
|
+
metadata: {
|
|
926
|
+
...existingContent.metadata,
|
|
927
|
+
...updatableFields.content.metadata
|
|
928
|
+
}
|
|
929
|
+
} : {}
|
|
930
|
+
};
|
|
931
|
+
setClauses.push(`content = ?`);
|
|
932
|
+
values.push(JSON.stringify(newContent));
|
|
933
|
+
delete updatableFields.content;
|
|
934
|
+
}
|
|
935
|
+
for (const key in updatableFields) {
|
|
936
|
+
if (Object.prototype.hasOwnProperty.call(updatableFields, key)) {
|
|
937
|
+
const dbColumn = key === "threadId" ? "thread_id" : key;
|
|
938
|
+
setClauses.push(`${dbColumn} = ?`);
|
|
939
|
+
values.push(updatableFields[key]);
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
if (setClauses.length > 0) {
|
|
943
|
+
values.push(id);
|
|
944
|
+
const updateQuery = `UPDATE ${fullTableName} SET ${setClauses.join(", ")} WHERE id = ?`;
|
|
945
|
+
updateQueries.push({ sql: updateQuery, params: values });
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
for (const query of updateQueries) {
|
|
949
|
+
await this.operations.executeQuery(query);
|
|
950
|
+
}
|
|
951
|
+
if (threadIdsToUpdate.size > 0) {
|
|
952
|
+
const threadPlaceholders = Array.from(threadIdsToUpdate).map(() => "?").join(",");
|
|
953
|
+
const threadUpdateQuery = `UPDATE ${threadsTableName} SET updatedAt = ? WHERE id IN (${threadPlaceholders})`;
|
|
954
|
+
const threadUpdateParams = [(/* @__PURE__ */ new Date()).toISOString(), ...Array.from(threadIdsToUpdate)];
|
|
955
|
+
await this.operations.executeQuery({ sql: threadUpdateQuery, params: threadUpdateParams });
|
|
956
|
+
}
|
|
957
|
+
const updatedMessages = await this.operations.executeQuery({ sql: selectQuery, params: messageIds });
|
|
958
|
+
return updatedMessages.map((message) => {
|
|
959
|
+
if (typeof message.content === "string") {
|
|
960
|
+
try {
|
|
961
|
+
message.content = JSON.parse(message.content);
|
|
962
|
+
} catch {
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
return message;
|
|
966
|
+
});
|
|
967
|
+
} catch (error) {
|
|
968
|
+
throw new MastraError(
|
|
969
|
+
{
|
|
970
|
+
id: "CLOUDFLARE_D1_STORAGE_UPDATE_MESSAGES_FAILED",
|
|
971
|
+
domain: ErrorDomain.STORAGE,
|
|
972
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
973
|
+
details: { count: messages.length }
|
|
974
|
+
},
|
|
975
|
+
error
|
|
976
|
+
);
|
|
808
977
|
}
|
|
809
978
|
}
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
979
|
+
};
|
|
980
|
+
var StoreOperationsD1 = class extends StoreOperations {
|
|
981
|
+
client;
|
|
982
|
+
binding;
|
|
983
|
+
tablePrefix;
|
|
984
|
+
constructor(config) {
|
|
985
|
+
super();
|
|
986
|
+
this.client = config.client;
|
|
987
|
+
this.binding = config.binding;
|
|
988
|
+
this.tablePrefix = config.tablePrefix || "";
|
|
989
|
+
}
|
|
990
|
+
async hasColumn(table, column) {
|
|
991
|
+
const fullTableName = table.startsWith(this.tablePrefix) ? table : `${this.tablePrefix}${table}`;
|
|
992
|
+
const sql = `PRAGMA table_info(${fullTableName});`;
|
|
993
|
+
const result = await this.executeQuery({ sql, params: [] });
|
|
994
|
+
if (!result || !Array.isArray(result)) return false;
|
|
995
|
+
return result.some((col) => col.name === column || col.name === column.toLowerCase());
|
|
996
|
+
}
|
|
997
|
+
getTableName(tableName) {
|
|
998
|
+
return `${this.tablePrefix}${tableName}`;
|
|
999
|
+
}
|
|
1000
|
+
formatSqlParams(params) {
|
|
1001
|
+
return params.map((p) => p === void 0 || p === null ? null : p);
|
|
1002
|
+
}
|
|
1003
|
+
async executeWorkersBindingQuery({
|
|
1004
|
+
sql,
|
|
1005
|
+
params = [],
|
|
1006
|
+
first = false
|
|
1007
|
+
}) {
|
|
1008
|
+
if (!this.binding) {
|
|
1009
|
+
throw new Error("Workers binding is not configured");
|
|
1010
|
+
}
|
|
1011
|
+
try {
|
|
1012
|
+
const statement = this.binding.prepare(sql);
|
|
1013
|
+
const formattedParams = this.formatSqlParams(params);
|
|
1014
|
+
let result;
|
|
1015
|
+
if (formattedParams.length > 0) {
|
|
1016
|
+
if (first) {
|
|
1017
|
+
result = await statement.bind(...formattedParams).first();
|
|
1018
|
+
if (!result) return null;
|
|
1019
|
+
return result;
|
|
1020
|
+
} else {
|
|
1021
|
+
result = await statement.bind(...formattedParams).all();
|
|
1022
|
+
const results = result.results || [];
|
|
1023
|
+
return results;
|
|
1024
|
+
}
|
|
1025
|
+
} else {
|
|
1026
|
+
if (first) {
|
|
1027
|
+
result = await statement.first();
|
|
1028
|
+
if (!result) return null;
|
|
1029
|
+
return result;
|
|
1030
|
+
} else {
|
|
1031
|
+
result = await statement.all();
|
|
1032
|
+
const results = result.results || [];
|
|
1033
|
+
return results;
|
|
1034
|
+
}
|
|
818
1035
|
}
|
|
819
|
-
})
|
|
820
|
-
|
|
1036
|
+
} catch (error) {
|
|
1037
|
+
throw new MastraError(
|
|
1038
|
+
{
|
|
1039
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_WORKERS_BINDING_QUERY_FAILED",
|
|
1040
|
+
domain: ErrorDomain.STORAGE,
|
|
1041
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1042
|
+
details: { sql }
|
|
1043
|
+
},
|
|
1044
|
+
error
|
|
1045
|
+
);
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
async executeRestQuery({
|
|
1049
|
+
sql,
|
|
1050
|
+
params = [],
|
|
1051
|
+
first = false
|
|
1052
|
+
}) {
|
|
1053
|
+
if (!this.client) {
|
|
1054
|
+
throw new Error("D1 client is not configured");
|
|
1055
|
+
}
|
|
1056
|
+
try {
|
|
1057
|
+
const formattedParams = this.formatSqlParams(params);
|
|
1058
|
+
const response = await this.client.query({
|
|
1059
|
+
sql,
|
|
1060
|
+
params: formattedParams
|
|
1061
|
+
});
|
|
1062
|
+
const result = response.result || [];
|
|
1063
|
+
const results = result.flatMap((r) => r.results || []);
|
|
1064
|
+
if (first) {
|
|
1065
|
+
return results[0] || null;
|
|
1066
|
+
}
|
|
1067
|
+
return results;
|
|
1068
|
+
} catch (error) {
|
|
1069
|
+
throw new MastraError(
|
|
1070
|
+
{
|
|
1071
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_REST_QUERY_FAILED",
|
|
1072
|
+
domain: ErrorDomain.STORAGE,
|
|
1073
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1074
|
+
details: { sql }
|
|
1075
|
+
},
|
|
1076
|
+
error
|
|
1077
|
+
);
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
async executeQuery(options) {
|
|
1081
|
+
if (this.binding) {
|
|
1082
|
+
return this.executeWorkersBindingQuery(options);
|
|
1083
|
+
} else if (this.client) {
|
|
1084
|
+
return this.executeRestQuery(options);
|
|
1085
|
+
} else {
|
|
1086
|
+
throw new Error("Neither binding nor client is configured");
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
async getTableColumns(tableName) {
|
|
1090
|
+
try {
|
|
1091
|
+
const sql = `PRAGMA table_info(${tableName})`;
|
|
1092
|
+
const result = await this.executeQuery({ sql });
|
|
1093
|
+
if (!result || !Array.isArray(result)) {
|
|
1094
|
+
return [];
|
|
1095
|
+
}
|
|
1096
|
+
return result.map((row) => ({
|
|
1097
|
+
name: row.name,
|
|
1098
|
+
type: row.type
|
|
1099
|
+
}));
|
|
1100
|
+
} catch (error) {
|
|
1101
|
+
this.logger.warn(`Failed to get table columns for ${tableName}:`, error);
|
|
1102
|
+
return [];
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
serializeValue(value) {
|
|
1106
|
+
if (value === null || value === void 0) {
|
|
1107
|
+
return null;
|
|
1108
|
+
}
|
|
1109
|
+
if (value instanceof Date) {
|
|
1110
|
+
return value.toISOString();
|
|
1111
|
+
}
|
|
1112
|
+
if (typeof value === "object") {
|
|
1113
|
+
return JSON.stringify(value);
|
|
1114
|
+
}
|
|
1115
|
+
return value;
|
|
1116
|
+
}
|
|
1117
|
+
getSqlType(type) {
|
|
1118
|
+
switch (type) {
|
|
1119
|
+
case "bigint":
|
|
1120
|
+
return "INTEGER";
|
|
1121
|
+
// SQLite uses INTEGER for all integer sizes
|
|
1122
|
+
case "jsonb":
|
|
1123
|
+
return "TEXT";
|
|
1124
|
+
// Store JSON as TEXT in SQLite
|
|
1125
|
+
default:
|
|
1126
|
+
return super.getSqlType(type);
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
async createTable({
|
|
1130
|
+
tableName,
|
|
1131
|
+
schema
|
|
1132
|
+
}) {
|
|
1133
|
+
try {
|
|
1134
|
+
const fullTableName = this.getTableName(tableName);
|
|
1135
|
+
const columnDefinitions = Object.entries(schema).map(([colName, colDef]) => {
|
|
1136
|
+
const type = this.getSqlType(colDef.type);
|
|
1137
|
+
const nullable = colDef.nullable === false ? "NOT NULL" : "";
|
|
1138
|
+
const primaryKey = colDef.primaryKey ? "PRIMARY KEY" : "";
|
|
1139
|
+
return `${colName} ${type} ${nullable} ${primaryKey}`.trim();
|
|
1140
|
+
});
|
|
1141
|
+
const tableConstraints = [];
|
|
1142
|
+
if (tableName === TABLE_WORKFLOW_SNAPSHOT) {
|
|
1143
|
+
tableConstraints.push("UNIQUE (workflow_name, run_id)");
|
|
1144
|
+
}
|
|
1145
|
+
const query = createSqlBuilder().createTable(fullTableName, columnDefinitions, tableConstraints);
|
|
1146
|
+
const { sql, params } = query.build();
|
|
1147
|
+
await this.executeQuery({ sql, params });
|
|
1148
|
+
this.logger.debug(`Created table ${fullTableName}`);
|
|
1149
|
+
} catch (error) {
|
|
1150
|
+
throw new MastraError(
|
|
1151
|
+
{
|
|
1152
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_CREATE_TABLE_FAILED",
|
|
1153
|
+
domain: ErrorDomain.STORAGE,
|
|
1154
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1155
|
+
details: { tableName }
|
|
1156
|
+
},
|
|
1157
|
+
error
|
|
1158
|
+
);
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
async clearTable({ tableName }) {
|
|
1162
|
+
try {
|
|
1163
|
+
const fullTableName = this.getTableName(tableName);
|
|
1164
|
+
const query = createSqlBuilder().delete(fullTableName);
|
|
1165
|
+
const { sql, params } = query.build();
|
|
1166
|
+
await this.executeQuery({ sql, params });
|
|
1167
|
+
this.logger.debug(`Cleared table ${fullTableName}`);
|
|
1168
|
+
} catch (error) {
|
|
1169
|
+
throw new MastraError(
|
|
1170
|
+
{
|
|
1171
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_CLEAR_TABLE_FAILED",
|
|
1172
|
+
domain: ErrorDomain.STORAGE,
|
|
1173
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1174
|
+
details: { tableName }
|
|
1175
|
+
},
|
|
1176
|
+
error
|
|
1177
|
+
);
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
async dropTable({ tableName }) {
|
|
1181
|
+
try {
|
|
1182
|
+
const fullTableName = this.getTableName(tableName);
|
|
1183
|
+
const sql = `DROP TABLE IF EXISTS ${fullTableName}`;
|
|
1184
|
+
await this.executeQuery({ sql });
|
|
1185
|
+
this.logger.debug(`Dropped table ${fullTableName}`);
|
|
1186
|
+
} catch (error) {
|
|
1187
|
+
throw new MastraError(
|
|
1188
|
+
{
|
|
1189
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_DROP_TABLE_FAILED",
|
|
1190
|
+
domain: ErrorDomain.STORAGE,
|
|
1191
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1192
|
+
details: { tableName }
|
|
1193
|
+
},
|
|
1194
|
+
error
|
|
1195
|
+
);
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
async alterTable(args) {
|
|
1199
|
+
try {
|
|
1200
|
+
const fullTableName = this.getTableName(args.tableName);
|
|
1201
|
+
const existingColumns = await this.getTableColumns(fullTableName);
|
|
1202
|
+
const existingColumnNames = new Set(existingColumns.map((col) => col.name));
|
|
1203
|
+
for (const [columnName, column] of Object.entries(args.schema)) {
|
|
1204
|
+
if (!existingColumnNames.has(columnName) && args.ifNotExists.includes(columnName)) {
|
|
1205
|
+
const sqlType = this.getSqlType(column.type);
|
|
1206
|
+
const defaultValue = this.getDefaultValue(column.type);
|
|
1207
|
+
const sql = `ALTER TABLE ${fullTableName} ADD COLUMN ${columnName} ${sqlType} ${defaultValue}`;
|
|
1208
|
+
await this.executeQuery({ sql });
|
|
1209
|
+
this.logger.debug(`Added column ${columnName} to table ${fullTableName}`);
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
} catch (error) {
|
|
1213
|
+
throw new MastraError(
|
|
1214
|
+
{
|
|
1215
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_ALTER_TABLE_FAILED",
|
|
1216
|
+
domain: ErrorDomain.STORAGE,
|
|
1217
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1218
|
+
details: { tableName: args.tableName }
|
|
1219
|
+
},
|
|
1220
|
+
error
|
|
1221
|
+
);
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
async insert({ tableName, record }) {
|
|
1225
|
+
try {
|
|
1226
|
+
const fullTableName = this.getTableName(tableName);
|
|
1227
|
+
const processedRecord = await this.processRecord(record);
|
|
1228
|
+
const columns = Object.keys(processedRecord);
|
|
1229
|
+
const values = Object.values(processedRecord);
|
|
1230
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values);
|
|
1231
|
+
const { sql, params } = query.build();
|
|
1232
|
+
await this.executeQuery({ sql, params });
|
|
1233
|
+
} catch (error) {
|
|
1234
|
+
throw new MastraError(
|
|
1235
|
+
{
|
|
1236
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_INSERT_FAILED",
|
|
1237
|
+
domain: ErrorDomain.STORAGE,
|
|
1238
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1239
|
+
details: { tableName }
|
|
1240
|
+
},
|
|
1241
|
+
error
|
|
1242
|
+
);
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
async batchInsert({ tableName, records }) {
|
|
1246
|
+
try {
|
|
1247
|
+
if (records.length === 0) return;
|
|
1248
|
+
const fullTableName = this.getTableName(tableName);
|
|
1249
|
+
const processedRecords = await Promise.all(records.map((record) => this.processRecord(record)));
|
|
1250
|
+
const columns = Object.keys(processedRecords[0] || {});
|
|
1251
|
+
for (const record of processedRecords) {
|
|
1252
|
+
const values = Object.values(record);
|
|
1253
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values);
|
|
1254
|
+
const { sql, params } = query.build();
|
|
1255
|
+
await this.executeQuery({ sql, params });
|
|
1256
|
+
}
|
|
1257
|
+
} catch (error) {
|
|
1258
|
+
throw new MastraError(
|
|
1259
|
+
{
|
|
1260
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_BATCH_INSERT_FAILED",
|
|
1261
|
+
domain: ErrorDomain.STORAGE,
|
|
1262
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1263
|
+
details: { tableName }
|
|
1264
|
+
},
|
|
1265
|
+
error
|
|
1266
|
+
);
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
async load({ tableName, keys }) {
|
|
1270
|
+
try {
|
|
1271
|
+
const fullTableName = this.getTableName(tableName);
|
|
1272
|
+
const query = createSqlBuilder().select("*").from(fullTableName);
|
|
1273
|
+
let firstKey = true;
|
|
1274
|
+
for (const [key, value] of Object.entries(keys)) {
|
|
1275
|
+
if (firstKey) {
|
|
1276
|
+
query.where(`${key} = ?`, value);
|
|
1277
|
+
firstKey = false;
|
|
1278
|
+
} else {
|
|
1279
|
+
query.andWhere(`${key} = ?`, value);
|
|
1280
|
+
}
|
|
1281
|
+
}
|
|
1282
|
+
query.orderBy("createdAt", "DESC");
|
|
1283
|
+
query.limit(1);
|
|
1284
|
+
const { sql, params } = query.build();
|
|
1285
|
+
const result = await this.executeQuery({ sql, params, first: true });
|
|
1286
|
+
if (!result) {
|
|
1287
|
+
return null;
|
|
1288
|
+
}
|
|
1289
|
+
const deserializedResult = {};
|
|
1290
|
+
for (const [key, value] of Object.entries(result)) {
|
|
1291
|
+
deserializedResult[key] = deserializeValue(value);
|
|
1292
|
+
}
|
|
1293
|
+
return deserializedResult;
|
|
1294
|
+
} catch (error) {
|
|
1295
|
+
throw new MastraError(
|
|
1296
|
+
{
|
|
1297
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_LOAD_FAILED",
|
|
1298
|
+
domain: ErrorDomain.STORAGE,
|
|
1299
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1300
|
+
details: { tableName }
|
|
1301
|
+
},
|
|
1302
|
+
error
|
|
1303
|
+
);
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
async processRecord(record) {
|
|
1307
|
+
const processed = {};
|
|
1308
|
+
for (const [key, value] of Object.entries(record)) {
|
|
1309
|
+
processed[key] = this.serializeValue(value);
|
|
1310
|
+
}
|
|
1311
|
+
return processed;
|
|
821
1312
|
}
|
|
822
1313
|
/**
|
|
823
|
-
*
|
|
1314
|
+
* Upsert multiple records in a batch operation
|
|
824
1315
|
* @param tableName The table to insert into
|
|
825
1316
|
* @param records The records to insert
|
|
826
1317
|
*/
|
|
827
|
-
async
|
|
1318
|
+
async batchUpsert({ tableName, records }) {
|
|
828
1319
|
if (records.length === 0) return;
|
|
829
1320
|
const fullTableName = this.getTableName(tableName);
|
|
830
1321
|
try {
|
|
@@ -841,7 +1332,14 @@ var D1Store = class extends MastraStorage {
|
|
|
841
1332
|
const value = typeof col === "string" ? record[col] : null;
|
|
842
1333
|
return this.serializeValue(value);
|
|
843
1334
|
});
|
|
844
|
-
const
|
|
1335
|
+
const recordToUpsert = columns.reduce(
|
|
1336
|
+
(acc, col) => {
|
|
1337
|
+
if (col !== "createdAt") acc[col] = `excluded.${col}`;
|
|
1338
|
+
return acc;
|
|
1339
|
+
},
|
|
1340
|
+
{}
|
|
1341
|
+
);
|
|
1342
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values, ["id"], recordToUpsert);
|
|
845
1343
|
const { sql, params } = query.build();
|
|
846
1344
|
await this.executeQuery({ sql, params });
|
|
847
1345
|
}
|
|
@@ -850,92 +1348,430 @@ var D1Store = class extends MastraStorage {
|
|
|
850
1348
|
`Processed batch ${Math.floor(i / batchSize) + 1} of ${Math.ceil(records.length / batchSize)}`
|
|
851
1349
|
);
|
|
852
1350
|
}
|
|
853
|
-
this.logger.debug(`Successfully batch
|
|
1351
|
+
this.logger.debug(`Successfully batch upserted ${records.length} records into ${tableName}`);
|
|
854
1352
|
} catch (error) {
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
1353
|
+
throw new MastraError(
|
|
1354
|
+
{
|
|
1355
|
+
id: "CLOUDFLARE_D1_STORAGE_BATCH_UPSERT_ERROR",
|
|
1356
|
+
domain: ErrorDomain.STORAGE,
|
|
1357
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1358
|
+
text: `Failed to batch upsert into ${tableName}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1359
|
+
details: { tableName }
|
|
1360
|
+
},
|
|
1361
|
+
error
|
|
1362
|
+
);
|
|
859
1363
|
}
|
|
860
1364
|
}
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
1365
|
+
};
|
|
1366
|
+
function transformScoreRow(row) {
|
|
1367
|
+
const deserialized = { ...row };
|
|
1368
|
+
deserialized.input = safelyParseJSON(row.input);
|
|
1369
|
+
deserialized.output = safelyParseJSON(row.output);
|
|
1370
|
+
deserialized.scorer = safelyParseJSON(row.scorer);
|
|
1371
|
+
deserialized.preprocessStepResult = safelyParseJSON(row.preprocessStepResult);
|
|
1372
|
+
deserialized.analyzeStepResult = safelyParseJSON(row.analyzeStepResult);
|
|
1373
|
+
deserialized.metadata = safelyParseJSON(row.metadata);
|
|
1374
|
+
deserialized.additionalContext = safelyParseJSON(row.additionalContext);
|
|
1375
|
+
deserialized.requestContext = safelyParseJSON(row.requestContext);
|
|
1376
|
+
deserialized.entity = safelyParseJSON(row.entity);
|
|
1377
|
+
deserialized.createdAt = row.createdAtZ || row.createdAt;
|
|
1378
|
+
deserialized.updatedAt = row.updatedAtZ || row.updatedAt;
|
|
1379
|
+
return deserialized;
|
|
1380
|
+
}
|
|
1381
|
+
var ScoresStorageD1 = class extends ScoresStorage {
|
|
1382
|
+
operations;
|
|
1383
|
+
constructor({ operations }) {
|
|
1384
|
+
super();
|
|
1385
|
+
this.operations = operations;
|
|
1386
|
+
}
|
|
1387
|
+
async getScoreById({ id }) {
|
|
1388
|
+
try {
|
|
1389
|
+
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1390
|
+
const query = createSqlBuilder().select("*").from(fullTableName).where("id = ?", id);
|
|
1391
|
+
const { sql, params } = query.build();
|
|
1392
|
+
const result = await this.operations.executeQuery({ sql, params, first: true });
|
|
1393
|
+
if (!result) {
|
|
1394
|
+
return null;
|
|
1395
|
+
}
|
|
1396
|
+
return transformScoreRow(result);
|
|
1397
|
+
} catch (error) {
|
|
1398
|
+
throw new MastraError(
|
|
1399
|
+
{
|
|
1400
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORE_BY_ID_FAILED",
|
|
1401
|
+
domain: ErrorDomain.STORAGE,
|
|
1402
|
+
category: ErrorCategory.THIRD_PARTY
|
|
1403
|
+
},
|
|
1404
|
+
error
|
|
1405
|
+
);
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
async saveScore(score) {
|
|
1409
|
+
let parsedScore;
|
|
1410
|
+
try {
|
|
1411
|
+
parsedScore = saveScorePayloadSchema.parse(score);
|
|
1412
|
+
} catch (error) {
|
|
1413
|
+
throw new MastraError(
|
|
1414
|
+
{
|
|
1415
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_SAVE_SCORE_FAILED_INVALID_SCORE_PAYLOAD",
|
|
1416
|
+
domain: ErrorDomain.STORAGE,
|
|
1417
|
+
category: ErrorCategory.USER,
|
|
1418
|
+
details: { scoreId: score.id }
|
|
1419
|
+
},
|
|
1420
|
+
error
|
|
1421
|
+
);
|
|
1422
|
+
}
|
|
1423
|
+
try {
|
|
1424
|
+
const id = crypto.randomUUID();
|
|
1425
|
+
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1426
|
+
const serializedRecord = {};
|
|
1427
|
+
for (const [key, value] of Object.entries(parsedScore)) {
|
|
1428
|
+
if (value !== null && value !== void 0) {
|
|
1429
|
+
if (typeof value === "object") {
|
|
1430
|
+
serializedRecord[key] = JSON.stringify(value);
|
|
1431
|
+
} else {
|
|
1432
|
+
serializedRecord[key] = value;
|
|
1433
|
+
}
|
|
1434
|
+
} else {
|
|
1435
|
+
serializedRecord[key] = null;
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
serializedRecord.id = id;
|
|
1439
|
+
serializedRecord.createdAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1440
|
+
serializedRecord.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1441
|
+
const columns = Object.keys(serializedRecord);
|
|
1442
|
+
const values = Object.values(serializedRecord);
|
|
1443
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values);
|
|
1444
|
+
const { sql, params } = query.build();
|
|
1445
|
+
await this.operations.executeQuery({ sql, params });
|
|
1446
|
+
const scoreFromDb = await this.getScoreById({ id });
|
|
1447
|
+
return { score: scoreFromDb };
|
|
1448
|
+
} catch (error) {
|
|
1449
|
+
throw new MastraError(
|
|
1450
|
+
{
|
|
1451
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_SAVE_SCORE_FAILED",
|
|
1452
|
+
domain: ErrorDomain.STORAGE,
|
|
1453
|
+
category: ErrorCategory.THIRD_PARTY
|
|
1454
|
+
},
|
|
1455
|
+
error
|
|
1456
|
+
);
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
async listScoresByScorerId({
|
|
1460
|
+
scorerId,
|
|
1461
|
+
entityId,
|
|
1462
|
+
entityType,
|
|
1463
|
+
source,
|
|
1464
|
+
pagination
|
|
869
1465
|
}) {
|
|
870
|
-
const fullTableName = this.getTableName(TABLE_TRACES);
|
|
871
1466
|
try {
|
|
872
|
-
const
|
|
873
|
-
|
|
874
|
-
|
|
1467
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1468
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1469
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1470
|
+
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1471
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("scorerId = ?", scorerId);
|
|
1472
|
+
if (entityId) {
|
|
1473
|
+
countQuery.andWhere("entityId = ?", entityId);
|
|
875
1474
|
}
|
|
876
|
-
if (
|
|
877
|
-
|
|
1475
|
+
if (entityType) {
|
|
1476
|
+
countQuery.andWhere("entityType = ?", entityType);
|
|
878
1477
|
}
|
|
879
|
-
if (
|
|
880
|
-
|
|
881
|
-
query.jsonLike("attributes", key, value);
|
|
882
|
-
}
|
|
1478
|
+
if (source) {
|
|
1479
|
+
countQuery.andWhere("source = ?", source);
|
|
883
1480
|
}
|
|
884
|
-
|
|
885
|
-
|
|
1481
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1482
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1483
|
+
if (total === 0) {
|
|
1484
|
+
return {
|
|
1485
|
+
pagination: {
|
|
1486
|
+
total: 0,
|
|
1487
|
+
page,
|
|
1488
|
+
perPage: perPageForResponse,
|
|
1489
|
+
hasMore: false
|
|
1490
|
+
},
|
|
1491
|
+
scores: []
|
|
1492
|
+
};
|
|
886
1493
|
}
|
|
887
|
-
|
|
888
|
-
|
|
1494
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1495
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1496
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("scorerId = ?", scorerId);
|
|
1497
|
+
if (entityId) {
|
|
1498
|
+
selectQuery.andWhere("entityId = ?", entityId);
|
|
889
1499
|
}
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
1500
|
+
if (entityType) {
|
|
1501
|
+
selectQuery.andWhere("entityType = ?", entityType);
|
|
1502
|
+
}
|
|
1503
|
+
if (source) {
|
|
1504
|
+
selectQuery.andWhere("source = ?", source);
|
|
1505
|
+
}
|
|
1506
|
+
selectQuery.limit(limitValue).offset(start);
|
|
1507
|
+
const { sql, params } = selectQuery.build();
|
|
1508
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1509
|
+
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1510
|
+
return {
|
|
1511
|
+
pagination: {
|
|
1512
|
+
total,
|
|
1513
|
+
page,
|
|
1514
|
+
perPage: perPageForResponse,
|
|
1515
|
+
hasMore: end < total
|
|
1516
|
+
},
|
|
1517
|
+
scores
|
|
1518
|
+
};
|
|
901
1519
|
} catch (error) {
|
|
902
|
-
|
|
903
|
-
|
|
1520
|
+
throw new MastraError(
|
|
1521
|
+
{
|
|
1522
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_SCORER_ID_FAILED",
|
|
1523
|
+
domain: ErrorDomain.STORAGE,
|
|
1524
|
+
category: ErrorCategory.THIRD_PARTY
|
|
1525
|
+
},
|
|
1526
|
+
error
|
|
1527
|
+
);
|
|
904
1528
|
}
|
|
905
1529
|
}
|
|
906
|
-
async
|
|
907
|
-
|
|
1530
|
+
async listScoresByRunId({
|
|
1531
|
+
runId,
|
|
1532
|
+
pagination
|
|
1533
|
+
}) {
|
|
908
1534
|
try {
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
1535
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1536
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1537
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1538
|
+
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1539
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("runId = ?", runId);
|
|
1540
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1541
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1542
|
+
if (total === 0) {
|
|
1543
|
+
return {
|
|
1544
|
+
pagination: {
|
|
1545
|
+
total: 0,
|
|
1546
|
+
page,
|
|
1547
|
+
perPage: perPageForResponse,
|
|
1548
|
+
hasMore: false
|
|
1549
|
+
},
|
|
1550
|
+
scores: []
|
|
1551
|
+
};
|
|
914
1552
|
}
|
|
915
|
-
|
|
916
|
-
const
|
|
917
|
-
const
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
1553
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1554
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1555
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("runId = ?", runId).limit(limitValue).offset(start);
|
|
1556
|
+
const { sql, params } = selectQuery.build();
|
|
1557
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1558
|
+
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1559
|
+
return {
|
|
1560
|
+
pagination: {
|
|
1561
|
+
total,
|
|
1562
|
+
page,
|
|
1563
|
+
perPage: perPageForResponse,
|
|
1564
|
+
hasMore: end < total
|
|
1565
|
+
},
|
|
1566
|
+
scores
|
|
1567
|
+
};
|
|
1568
|
+
} catch (error) {
|
|
1569
|
+
throw new MastraError(
|
|
1570
|
+
{
|
|
1571
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_RUN_ID_FAILED",
|
|
1572
|
+
domain: ErrorDomain.STORAGE,
|
|
1573
|
+
category: ErrorCategory.THIRD_PARTY
|
|
1574
|
+
},
|
|
1575
|
+
error
|
|
1576
|
+
);
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
async listScoresByEntityId({
|
|
1580
|
+
entityId,
|
|
1581
|
+
entityType,
|
|
1582
|
+
pagination
|
|
1583
|
+
}) {
|
|
1584
|
+
try {
|
|
1585
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1586
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1587
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1588
|
+
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1589
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType);
|
|
1590
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1591
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1592
|
+
if (total === 0) {
|
|
921
1593
|
return {
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
globalRunId: row.global_run_id || "",
|
|
930
|
-
createdAt: row.created_at || "",
|
|
931
|
-
testInfo
|
|
1594
|
+
pagination: {
|
|
1595
|
+
total: 0,
|
|
1596
|
+
page,
|
|
1597
|
+
perPage: perPageForResponse,
|
|
1598
|
+
hasMore: false
|
|
1599
|
+
},
|
|
1600
|
+
scores: []
|
|
932
1601
|
};
|
|
933
|
-
}
|
|
1602
|
+
}
|
|
1603
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1604
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1605
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType).limit(limitValue).offset(start);
|
|
1606
|
+
const { sql, params } = selectQuery.build();
|
|
1607
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1608
|
+
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1609
|
+
return {
|
|
1610
|
+
pagination: {
|
|
1611
|
+
total,
|
|
1612
|
+
page,
|
|
1613
|
+
perPage: perPageForResponse,
|
|
1614
|
+
hasMore: end < total
|
|
1615
|
+
},
|
|
1616
|
+
scores
|
|
1617
|
+
};
|
|
934
1618
|
} catch (error) {
|
|
935
|
-
|
|
936
|
-
|
|
1619
|
+
throw new MastraError(
|
|
1620
|
+
{
|
|
1621
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_ENTITY_ID_FAILED",
|
|
1622
|
+
domain: ErrorDomain.STORAGE,
|
|
1623
|
+
category: ErrorCategory.THIRD_PARTY
|
|
1624
|
+
},
|
|
1625
|
+
error
|
|
1626
|
+
);
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1629
|
+
async listScoresBySpan({
|
|
1630
|
+
traceId,
|
|
1631
|
+
spanId,
|
|
1632
|
+
pagination
|
|
1633
|
+
}) {
|
|
1634
|
+
try {
|
|
1635
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1636
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1637
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1638
|
+
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1639
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId);
|
|
1640
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1641
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1642
|
+
if (total === 0) {
|
|
1643
|
+
return {
|
|
1644
|
+
pagination: {
|
|
1645
|
+
total: 0,
|
|
1646
|
+
page,
|
|
1647
|
+
perPage: perPageForResponse,
|
|
1648
|
+
hasMore: false
|
|
1649
|
+
},
|
|
1650
|
+
scores: []
|
|
1651
|
+
};
|
|
1652
|
+
}
|
|
1653
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1654
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1655
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId).orderBy("createdAt", "DESC").limit(limitValue).offset(start);
|
|
1656
|
+
const { sql, params } = selectQuery.build();
|
|
1657
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1658
|
+
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1659
|
+
return {
|
|
1660
|
+
pagination: {
|
|
1661
|
+
total,
|
|
1662
|
+
page,
|
|
1663
|
+
perPage: perPageForResponse,
|
|
1664
|
+
hasMore: end < total
|
|
1665
|
+
},
|
|
1666
|
+
scores
|
|
1667
|
+
};
|
|
1668
|
+
} catch (error) {
|
|
1669
|
+
throw new MastraError(
|
|
1670
|
+
{
|
|
1671
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_SPAN_FAILED",
|
|
1672
|
+
domain: ErrorDomain.STORAGE,
|
|
1673
|
+
category: ErrorCategory.THIRD_PARTY
|
|
1674
|
+
},
|
|
1675
|
+
error
|
|
1676
|
+
);
|
|
1677
|
+
}
|
|
1678
|
+
}
|
|
1679
|
+
};
|
|
1680
|
+
var WorkflowsStorageD1 = class extends WorkflowsStorage {
|
|
1681
|
+
operations;
|
|
1682
|
+
constructor({ operations }) {
|
|
1683
|
+
super();
|
|
1684
|
+
this.operations = operations;
|
|
1685
|
+
}
|
|
1686
|
+
updateWorkflowResults({
|
|
1687
|
+
// workflowName,
|
|
1688
|
+
// runId,
|
|
1689
|
+
// stepId,
|
|
1690
|
+
// result,
|
|
1691
|
+
// requestContext,
|
|
1692
|
+
}) {
|
|
1693
|
+
throw new Error("Method not implemented.");
|
|
1694
|
+
}
|
|
1695
|
+
updateWorkflowState({
|
|
1696
|
+
// workflowName,
|
|
1697
|
+
// runId,
|
|
1698
|
+
// opts,
|
|
1699
|
+
}) {
|
|
1700
|
+
throw new Error("Method not implemented.");
|
|
1701
|
+
}
|
|
1702
|
+
async persistWorkflowSnapshot({
|
|
1703
|
+
workflowName,
|
|
1704
|
+
runId,
|
|
1705
|
+
resourceId,
|
|
1706
|
+
snapshot
|
|
1707
|
+
}) {
|
|
1708
|
+
const fullTableName = this.operations.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
1709
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1710
|
+
const currentSnapshot = await this.operations.load({
|
|
1711
|
+
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
1712
|
+
keys: { workflow_name: workflowName, run_id: runId }
|
|
1713
|
+
});
|
|
1714
|
+
const persisting = currentSnapshot ? {
|
|
1715
|
+
...currentSnapshot,
|
|
1716
|
+
resourceId,
|
|
1717
|
+
snapshot: JSON.stringify(snapshot),
|
|
1718
|
+
updatedAt: now
|
|
1719
|
+
} : {
|
|
1720
|
+
workflow_name: workflowName,
|
|
1721
|
+
run_id: runId,
|
|
1722
|
+
resourceId,
|
|
1723
|
+
snapshot,
|
|
1724
|
+
createdAt: now,
|
|
1725
|
+
updatedAt: now
|
|
1726
|
+
};
|
|
1727
|
+
const processedRecord = await this.operations.processRecord(persisting);
|
|
1728
|
+
const columns = Object.keys(processedRecord);
|
|
1729
|
+
const values = Object.values(processedRecord);
|
|
1730
|
+
const updateMap = {
|
|
1731
|
+
snapshot: "excluded.snapshot",
|
|
1732
|
+
updatedAt: "excluded.updatedAt"
|
|
1733
|
+
};
|
|
1734
|
+
this.logger.debug("Persisting workflow snapshot", { workflowName, runId });
|
|
1735
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values, ["workflow_name", "run_id"], updateMap);
|
|
1736
|
+
const { sql, params } = query.build();
|
|
1737
|
+
try {
|
|
1738
|
+
await this.operations.executeQuery({ sql, params });
|
|
1739
|
+
} catch (error) {
|
|
1740
|
+
throw new MastraError(
|
|
1741
|
+
{
|
|
1742
|
+
id: "CLOUDFLARE_D1_STORAGE_PERSIST_WORKFLOW_SNAPSHOT_ERROR",
|
|
1743
|
+
domain: ErrorDomain.STORAGE,
|
|
1744
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1745
|
+
text: `Failed to persist workflow snapshot: ${error instanceof Error ? error.message : String(error)}`,
|
|
1746
|
+
details: { workflowName, runId }
|
|
1747
|
+
},
|
|
1748
|
+
error
|
|
1749
|
+
);
|
|
1750
|
+
}
|
|
1751
|
+
}
|
|
1752
|
+
async loadWorkflowSnapshot(params) {
|
|
1753
|
+
const { workflowName, runId } = params;
|
|
1754
|
+
this.logger.debug("Loading workflow snapshot", { workflowName, runId });
|
|
1755
|
+
try {
|
|
1756
|
+
const d = await this.operations.load({
|
|
1757
|
+
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
1758
|
+
keys: {
|
|
1759
|
+
workflow_name: workflowName,
|
|
1760
|
+
run_id: runId
|
|
1761
|
+
}
|
|
937
1762
|
});
|
|
938
|
-
return
|
|
1763
|
+
return d ? d.snapshot : null;
|
|
1764
|
+
} catch (error) {
|
|
1765
|
+
throw new MastraError(
|
|
1766
|
+
{
|
|
1767
|
+
id: "CLOUDFLARE_D1_STORAGE_LOAD_WORKFLOW_SNAPSHOT_ERROR",
|
|
1768
|
+
domain: ErrorDomain.STORAGE,
|
|
1769
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1770
|
+
text: `Failed to load workflow snapshot: ${error instanceof Error ? error.message : String(error)}`,
|
|
1771
|
+
details: { workflowName, runId }
|
|
1772
|
+
},
|
|
1773
|
+
error
|
|
1774
|
+
);
|
|
939
1775
|
}
|
|
940
1776
|
}
|
|
941
1777
|
parseWorkflowRun(row) {
|
|
@@ -951,32 +1787,31 @@ var D1Store = class extends MastraStorage {
|
|
|
951
1787
|
workflowName: row.workflow_name,
|
|
952
1788
|
runId: row.run_id,
|
|
953
1789
|
snapshot: parsedSnapshot,
|
|
954
|
-
createdAt:
|
|
955
|
-
updatedAt:
|
|
1790
|
+
createdAt: ensureDate(row.createdAt),
|
|
1791
|
+
updatedAt: ensureDate(row.updatedAt),
|
|
956
1792
|
resourceId: row.resourceId
|
|
957
1793
|
};
|
|
958
1794
|
}
|
|
959
|
-
async
|
|
960
|
-
const sql = `PRAGMA table_info(${table});`;
|
|
961
|
-
const result = await this.executeQuery({ sql, params: [] });
|
|
962
|
-
if (!result || !Array.isArray(result)) return false;
|
|
963
|
-
return result.some((col) => col.name === column || col.name === column.toLowerCase());
|
|
964
|
-
}
|
|
965
|
-
async getWorkflowRuns({
|
|
1795
|
+
async listWorkflowRuns({
|
|
966
1796
|
workflowName,
|
|
967
1797
|
fromDate,
|
|
968
1798
|
toDate,
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
resourceId
|
|
1799
|
+
page,
|
|
1800
|
+
perPage,
|
|
1801
|
+
resourceId,
|
|
1802
|
+
status
|
|
972
1803
|
} = {}) {
|
|
973
|
-
const fullTableName = this.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
1804
|
+
const fullTableName = this.operations.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
974
1805
|
try {
|
|
975
1806
|
const builder = createSqlBuilder().select().from(fullTableName);
|
|
976
1807
|
const countBuilder = createSqlBuilder().count().from(fullTableName);
|
|
977
1808
|
if (workflowName) builder.whereAnd("workflow_name = ?", workflowName);
|
|
1809
|
+
if (status) {
|
|
1810
|
+
builder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
|
|
1811
|
+
countBuilder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
|
|
1812
|
+
}
|
|
978
1813
|
if (resourceId) {
|
|
979
|
-
const hasResourceId = await this.hasColumn(fullTableName, "resourceId");
|
|
1814
|
+
const hasResourceId = await this.operations.hasColumn(fullTableName, "resourceId");
|
|
980
1815
|
if (hasResourceId) {
|
|
981
1816
|
builder.whereAnd("resourceId = ?", resourceId);
|
|
982
1817
|
countBuilder.whereAnd("resourceId = ?", resourceId);
|
|
@@ -993,30 +1828,46 @@ var D1Store = class extends MastraStorage {
|
|
|
993
1828
|
countBuilder.whereAnd("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
|
|
994
1829
|
}
|
|
995
1830
|
builder.orderBy("createdAt", "DESC");
|
|
996
|
-
if (typeof
|
|
997
|
-
|
|
1831
|
+
if (typeof perPage === "number" && typeof page === "number") {
|
|
1832
|
+
const offset = page * perPage;
|
|
1833
|
+
builder.limit(perPage);
|
|
1834
|
+
builder.offset(offset);
|
|
1835
|
+
}
|
|
998
1836
|
const { sql, params } = builder.build();
|
|
999
1837
|
let total = 0;
|
|
1000
|
-
if (
|
|
1838
|
+
if (perPage !== void 0 && page !== void 0) {
|
|
1001
1839
|
const { sql: countSql, params: countParams } = countBuilder.build();
|
|
1002
|
-
const countResult = await this.executeQuery({
|
|
1840
|
+
const countResult = await this.operations.executeQuery({
|
|
1841
|
+
sql: countSql,
|
|
1842
|
+
params: countParams,
|
|
1843
|
+
first: true
|
|
1844
|
+
});
|
|
1003
1845
|
total = Number(countResult?.count ?? 0);
|
|
1004
1846
|
}
|
|
1005
|
-
const results = await this.executeQuery({ sql, params });
|
|
1847
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1006
1848
|
const runs = (isArrayOfRecords(results) ? results : []).map((row) => this.parseWorkflowRun(row));
|
|
1007
1849
|
return { runs, total: total || runs.length };
|
|
1008
1850
|
} catch (error) {
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1851
|
+
throw new MastraError(
|
|
1852
|
+
{
|
|
1853
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_WORKFLOW_RUNS_ERROR",
|
|
1854
|
+
domain: ErrorDomain.STORAGE,
|
|
1855
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1856
|
+
text: `Failed to retrieve workflow runs: ${error instanceof Error ? error.message : String(error)}`,
|
|
1857
|
+
details: {
|
|
1858
|
+
workflowName: workflowName ?? "",
|
|
1859
|
+
resourceId: resourceId ?? ""
|
|
1860
|
+
}
|
|
1861
|
+
},
|
|
1862
|
+
error
|
|
1863
|
+
);
|
|
1013
1864
|
}
|
|
1014
1865
|
}
|
|
1015
1866
|
async getWorkflowRunById({
|
|
1016
1867
|
runId,
|
|
1017
1868
|
workflowName
|
|
1018
1869
|
}) {
|
|
1019
|
-
const fullTableName = this.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
1870
|
+
const fullTableName = this.operations.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
1020
1871
|
try {
|
|
1021
1872
|
const conditions = [];
|
|
1022
1873
|
const params = [];
|
|
@@ -1030,15 +1881,265 @@ var D1Store = class extends MastraStorage {
|
|
|
1030
1881
|
}
|
|
1031
1882
|
const whereClause = conditions.length > 0 ? "WHERE " + conditions.join(" AND ") : "";
|
|
1032
1883
|
const sql = `SELECT * FROM ${fullTableName} ${whereClause} ORDER BY createdAt DESC LIMIT 1`;
|
|
1033
|
-
const result = await this.executeQuery({ sql, params, first: true });
|
|
1884
|
+
const result = await this.operations.executeQuery({ sql, params, first: true });
|
|
1034
1885
|
if (!result) return null;
|
|
1035
1886
|
return this.parseWorkflowRun(result);
|
|
1036
1887
|
} catch (error) {
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1888
|
+
throw new MastraError(
|
|
1889
|
+
{
|
|
1890
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_WORKFLOW_RUN_BY_ID_ERROR",
|
|
1891
|
+
domain: ErrorDomain.STORAGE,
|
|
1892
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1893
|
+
text: `Failed to retrieve workflow run by ID: ${error instanceof Error ? error.message : String(error)}`,
|
|
1894
|
+
details: { runId, workflowName: workflowName ?? "" }
|
|
1895
|
+
},
|
|
1896
|
+
error
|
|
1897
|
+
);
|
|
1898
|
+
}
|
|
1899
|
+
}
|
|
1900
|
+
};
|
|
1901
|
+
|
|
1902
|
+
// src/storage/index.ts
|
|
1903
|
+
var D1Store = class extends MastraStorage {
|
|
1904
|
+
client;
|
|
1905
|
+
binding;
|
|
1906
|
+
// D1Database binding
|
|
1907
|
+
tablePrefix;
|
|
1908
|
+
stores;
|
|
1909
|
+
/**
|
|
1910
|
+
* Creates a new D1Store instance
|
|
1911
|
+
* @param config Configuration for D1 access (either REST API or Workers Binding API)
|
|
1912
|
+
*/
|
|
1913
|
+
constructor(config) {
|
|
1914
|
+
try {
|
|
1915
|
+
super({ id: config.id, name: "D1" });
|
|
1916
|
+
if (config.tablePrefix && !/^[a-zA-Z0-9_]*$/.test(config.tablePrefix)) {
|
|
1917
|
+
throw new Error("Invalid tablePrefix: only letters, numbers, and underscores are allowed.");
|
|
1918
|
+
}
|
|
1919
|
+
this.tablePrefix = config.tablePrefix || "";
|
|
1920
|
+
if ("binding" in config) {
|
|
1921
|
+
if (!config.binding) {
|
|
1922
|
+
throw new Error("D1 binding is required when using Workers Binding API");
|
|
1923
|
+
}
|
|
1924
|
+
this.binding = config.binding;
|
|
1925
|
+
this.logger.info("Using D1 Workers Binding API");
|
|
1926
|
+
} else if ("client" in config) {
|
|
1927
|
+
if (!config.client) {
|
|
1928
|
+
throw new Error("D1 client is required when using D1ClientConfig");
|
|
1929
|
+
}
|
|
1930
|
+
this.client = config.client;
|
|
1931
|
+
this.logger.info("Using D1 Client");
|
|
1932
|
+
} else {
|
|
1933
|
+
if (!config.accountId || !config.databaseId || !config.apiToken) {
|
|
1934
|
+
throw new Error("accountId, databaseId, and apiToken are required when using REST API");
|
|
1935
|
+
}
|
|
1936
|
+
const cfClient = new Cloudflare({
|
|
1937
|
+
apiToken: config.apiToken
|
|
1938
|
+
});
|
|
1939
|
+
this.client = {
|
|
1940
|
+
query: ({ sql, params }) => {
|
|
1941
|
+
return cfClient.d1.database.query(config.databaseId, {
|
|
1942
|
+
account_id: config.accountId,
|
|
1943
|
+
sql,
|
|
1944
|
+
params
|
|
1945
|
+
});
|
|
1946
|
+
}
|
|
1947
|
+
};
|
|
1948
|
+
this.logger.info("Using D1 REST API");
|
|
1949
|
+
}
|
|
1950
|
+
} catch (error) {
|
|
1951
|
+
throw new MastraError(
|
|
1952
|
+
{
|
|
1953
|
+
id: "CLOUDFLARE_D1_STORAGE_INITIALIZATION_ERROR",
|
|
1954
|
+
domain: ErrorDomain.STORAGE,
|
|
1955
|
+
category: ErrorCategory.SYSTEM,
|
|
1956
|
+
text: "Error initializing D1Store"
|
|
1957
|
+
},
|
|
1958
|
+
error
|
|
1959
|
+
);
|
|
1041
1960
|
}
|
|
1961
|
+
const operations = new StoreOperationsD1({
|
|
1962
|
+
client: this.client,
|
|
1963
|
+
binding: this.binding,
|
|
1964
|
+
tablePrefix: this.tablePrefix
|
|
1965
|
+
});
|
|
1966
|
+
const scores = new ScoresStorageD1({
|
|
1967
|
+
operations
|
|
1968
|
+
});
|
|
1969
|
+
const workflows = new WorkflowsStorageD1({
|
|
1970
|
+
operations
|
|
1971
|
+
});
|
|
1972
|
+
const memory = new MemoryStorageD1({
|
|
1973
|
+
operations
|
|
1974
|
+
});
|
|
1975
|
+
this.stores = {
|
|
1976
|
+
operations,
|
|
1977
|
+
scores,
|
|
1978
|
+
workflows,
|
|
1979
|
+
memory
|
|
1980
|
+
};
|
|
1981
|
+
}
|
|
1982
|
+
get supports() {
|
|
1983
|
+
return {
|
|
1984
|
+
selectByIncludeResourceScope: true,
|
|
1985
|
+
resourceWorkingMemory: true,
|
|
1986
|
+
hasColumn: true,
|
|
1987
|
+
createTable: true,
|
|
1988
|
+
deleteMessages: false,
|
|
1989
|
+
listScoresBySpan: true
|
|
1990
|
+
};
|
|
1991
|
+
}
|
|
1992
|
+
async createTable({
|
|
1993
|
+
tableName,
|
|
1994
|
+
schema
|
|
1995
|
+
}) {
|
|
1996
|
+
return this.stores.operations.createTable({ tableName, schema });
|
|
1997
|
+
}
|
|
1998
|
+
/**
|
|
1999
|
+
* Alters table schema to add columns if they don't exist
|
|
2000
|
+
* @param tableName Name of the table
|
|
2001
|
+
* @param schema Schema of the table
|
|
2002
|
+
* @param ifNotExists Array of column names to add if they don't exist
|
|
2003
|
+
*/
|
|
2004
|
+
async alterTable({
|
|
2005
|
+
tableName,
|
|
2006
|
+
schema,
|
|
2007
|
+
ifNotExists
|
|
2008
|
+
}) {
|
|
2009
|
+
return this.stores.operations.alterTable({ tableName, schema, ifNotExists });
|
|
2010
|
+
}
|
|
2011
|
+
async clearTable({ tableName }) {
|
|
2012
|
+
return this.stores.operations.clearTable({ tableName });
|
|
2013
|
+
}
|
|
2014
|
+
async dropTable({ tableName }) {
|
|
2015
|
+
return this.stores.operations.dropTable({ tableName });
|
|
2016
|
+
}
|
|
2017
|
+
async hasColumn(table, column) {
|
|
2018
|
+
return this.stores.operations.hasColumn(table, column);
|
|
2019
|
+
}
|
|
2020
|
+
async insert({ tableName, record }) {
|
|
2021
|
+
return this.stores.operations.insert({ tableName, record });
|
|
2022
|
+
}
|
|
2023
|
+
async load({ tableName, keys }) {
|
|
2024
|
+
return this.stores.operations.load({ tableName, keys });
|
|
2025
|
+
}
|
|
2026
|
+
async getThreadById({ threadId }) {
|
|
2027
|
+
return this.stores.memory.getThreadById({ threadId });
|
|
2028
|
+
}
|
|
2029
|
+
async saveThread({ thread }) {
|
|
2030
|
+
return this.stores.memory.saveThread({ thread });
|
|
2031
|
+
}
|
|
2032
|
+
async updateThread({
|
|
2033
|
+
id,
|
|
2034
|
+
title,
|
|
2035
|
+
metadata
|
|
2036
|
+
}) {
|
|
2037
|
+
return this.stores.memory.updateThread({ id, title, metadata });
|
|
2038
|
+
}
|
|
2039
|
+
async deleteThread({ threadId }) {
|
|
2040
|
+
return this.stores.memory.deleteThread({ threadId });
|
|
2041
|
+
}
|
|
2042
|
+
async saveMessages(args) {
|
|
2043
|
+
return this.stores.memory.saveMessages(args);
|
|
2044
|
+
}
|
|
2045
|
+
async updateWorkflowResults({
|
|
2046
|
+
workflowName,
|
|
2047
|
+
runId,
|
|
2048
|
+
stepId,
|
|
2049
|
+
result,
|
|
2050
|
+
requestContext
|
|
2051
|
+
}) {
|
|
2052
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
|
|
2053
|
+
}
|
|
2054
|
+
async updateWorkflowState({
|
|
2055
|
+
workflowName,
|
|
2056
|
+
runId,
|
|
2057
|
+
opts
|
|
2058
|
+
}) {
|
|
2059
|
+
return this.stores.workflows.updateWorkflowState({ workflowName, runId, opts });
|
|
2060
|
+
}
|
|
2061
|
+
async persistWorkflowSnapshot({
|
|
2062
|
+
workflowName,
|
|
2063
|
+
runId,
|
|
2064
|
+
resourceId,
|
|
2065
|
+
snapshot
|
|
2066
|
+
}) {
|
|
2067
|
+
return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot });
|
|
2068
|
+
}
|
|
2069
|
+
async loadWorkflowSnapshot(params) {
|
|
2070
|
+
return this.stores.workflows.loadWorkflowSnapshot(params);
|
|
2071
|
+
}
|
|
2072
|
+
async listWorkflowRuns(args = {}) {
|
|
2073
|
+
return this.stores.workflows.listWorkflowRuns(args);
|
|
2074
|
+
}
|
|
2075
|
+
async getWorkflowRunById({
|
|
2076
|
+
runId,
|
|
2077
|
+
workflowName
|
|
2078
|
+
}) {
|
|
2079
|
+
return this.stores.workflows.getWorkflowRunById({ runId, workflowName });
|
|
2080
|
+
}
|
|
2081
|
+
/**
|
|
2082
|
+
* Insert multiple records in a batch operation
|
|
2083
|
+
* @param tableName The table to insert into
|
|
2084
|
+
* @param records The records to insert
|
|
2085
|
+
*/
|
|
2086
|
+
async batchInsert({ tableName, records }) {
|
|
2087
|
+
return this.stores.operations.batchInsert({ tableName, records });
|
|
2088
|
+
}
|
|
2089
|
+
async updateMessages(_args) {
|
|
2090
|
+
return this.stores.memory.updateMessages(_args);
|
|
2091
|
+
}
|
|
2092
|
+
async getResourceById({ resourceId }) {
|
|
2093
|
+
return this.stores.memory.getResourceById({ resourceId });
|
|
2094
|
+
}
|
|
2095
|
+
async saveResource({ resource }) {
|
|
2096
|
+
return this.stores.memory.saveResource({ resource });
|
|
2097
|
+
}
|
|
2098
|
+
async updateResource({
|
|
2099
|
+
resourceId,
|
|
2100
|
+
workingMemory,
|
|
2101
|
+
metadata
|
|
2102
|
+
}) {
|
|
2103
|
+
return this.stores.memory.updateResource({ resourceId, workingMemory, metadata });
|
|
2104
|
+
}
|
|
2105
|
+
async getScoreById({ id: _id }) {
|
|
2106
|
+
return this.stores.scores.getScoreById({ id: _id });
|
|
2107
|
+
}
|
|
2108
|
+
async saveScore(_score) {
|
|
2109
|
+
return this.stores.scores.saveScore(_score);
|
|
2110
|
+
}
|
|
2111
|
+
async listScoresByRunId({
|
|
2112
|
+
runId: _runId,
|
|
2113
|
+
pagination: _pagination
|
|
2114
|
+
}) {
|
|
2115
|
+
return this.stores.scores.listScoresByRunId({ runId: _runId, pagination: _pagination });
|
|
2116
|
+
}
|
|
2117
|
+
async listScoresByEntityId({
|
|
2118
|
+
entityId: _entityId,
|
|
2119
|
+
entityType: _entityType,
|
|
2120
|
+
pagination: _pagination
|
|
2121
|
+
}) {
|
|
2122
|
+
return this.stores.scores.listScoresByEntityId({
|
|
2123
|
+
entityId: _entityId,
|
|
2124
|
+
entityType: _entityType,
|
|
2125
|
+
pagination: _pagination
|
|
2126
|
+
});
|
|
2127
|
+
}
|
|
2128
|
+
async listScoresByScorerId({
|
|
2129
|
+
scorerId,
|
|
2130
|
+
pagination,
|
|
2131
|
+
entityId,
|
|
2132
|
+
entityType,
|
|
2133
|
+
source
|
|
2134
|
+
}) {
|
|
2135
|
+
return this.stores.scores.listScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
|
|
2136
|
+
}
|
|
2137
|
+
async listScoresBySpan({
|
|
2138
|
+
traceId,
|
|
2139
|
+
spanId,
|
|
2140
|
+
pagination
|
|
2141
|
+
}) {
|
|
2142
|
+
return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
|
|
1042
2143
|
}
|
|
1043
2144
|
/**
|
|
1044
2145
|
* Close the database connection
|
|
@@ -1050,3 +2151,5 @@ var D1Store = class extends MastraStorage {
|
|
|
1050
2151
|
};
|
|
1051
2152
|
|
|
1052
2153
|
export { D1Store };
|
|
2154
|
+
//# sourceMappingURL=index.js.map
|
|
2155
|
+
//# sourceMappingURL=index.js.map
|