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