@mastra/cloudflare 1.2.3 → 1.3.0

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