@mastra/cloudflare-d1 0.0.0-remove-cloud-span-transform-20250425214156 → 0.0.0-remove-unused-import-20250909212718

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