@mastra/cloudflare-d1 0.0.0-pg-pool-options-20250428183821 → 0.0.0-pgvector-index-fix-20250905222058

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