@mastra/cloudflare-d1 0.12.1 → 0.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,10 +1,10 @@
1
1
  'use strict';
2
2
 
3
- var agent = require('@mastra/core/agent');
4
3
  var error = require('@mastra/core/error');
5
4
  var storage = require('@mastra/core/storage');
6
5
  var Cloudflare = require('cloudflare');
7
6
  var utils = require('@mastra/core/utils');
7
+ var agent = require('@mastra/core/agent');
8
8
 
9
9
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
10
10
 
@@ -241,411 +241,311 @@ function parseSelectIdentifier(column) {
241
241
  return column;
242
242
  }
243
243
 
244
- // src/storage/index.ts
244
+ // src/storage/domains/utils.ts
245
245
  function isArrayOfRecords(value) {
246
246
  return value && Array.isArray(value) && value.length > 0;
247
247
  }
248
- var D1Store = class extends storage.MastraStorage {
249
- client;
250
- binding;
251
- // D1Database binding
252
- tablePrefix;
253
- /**
254
- * Creates a new D1Store instance
255
- * @param config Configuration for D1 access (either REST API or Workers Binding API)
256
- */
257
- constructor(config) {
258
- try {
259
- super({ name: "D1" });
260
- if (config.tablePrefix && !/^[a-zA-Z0-9_]*$/.test(config.tablePrefix)) {
261
- throw new Error("Invalid tablePrefix: only letters, numbers, and underscores are allowed.");
262
- }
263
- this.tablePrefix = config.tablePrefix || "";
264
- if ("binding" in config) {
265
- if (!config.binding) {
266
- throw new Error("D1 binding is required when using Workers Binding API");
267
- }
268
- this.binding = config.binding;
269
- this.logger.info("Using D1 Workers Binding API");
270
- } else if ("client" in config) {
271
- if (!config.client) {
272
- throw new Error("D1 client is required when using D1ClientConfig");
273
- }
274
- this.client = config.client;
275
- this.logger.info("Using D1 Client");
276
- } else {
277
- if (!config.accountId || !config.databaseId || !config.apiToken) {
278
- throw new Error("accountId, databaseId, and apiToken are required when using REST API");
279
- }
280
- const cfClient = new Cloudflare__default.default({
281
- apiToken: config.apiToken
282
- });
283
- this.client = {
284
- query: ({ sql, params }) => {
285
- return cfClient.d1.database.query(config.databaseId, {
286
- account_id: config.accountId,
287
- sql,
288
- params
289
- });
290
- }
291
- };
292
- this.logger.info("Using D1 REST API");
293
- }
294
- } catch (error$1) {
295
- throw new error.MastraError(
296
- {
297
- id: "CLOUDFLARE_D1_STORAGE_INITIALIZATION_ERROR",
298
- domain: error.ErrorDomain.STORAGE,
299
- category: error.ErrorCategory.SYSTEM,
300
- text: "Error initializing D1Store"
301
- },
302
- error$1
303
- );
304
- }
305
- }
306
- // Helper method to get the full table name with prefix
307
- getTableName(tableName) {
308
- return `${this.tablePrefix}${tableName}`;
309
- }
310
- formatSqlParams(params) {
311
- return params.map((p) => p === void 0 || p === null ? null : p);
312
- }
313
- async executeWorkersBindingQuery({
314
- sql,
315
- params = [],
316
- first = false
317
- }) {
318
- if (!this.binding) {
319
- throw new Error("Workers binding is not configured");
320
- }
321
- try {
322
- const statement = this.binding.prepare(sql);
323
- const formattedParams = this.formatSqlParams(params);
324
- let result;
325
- if (formattedParams.length > 0) {
326
- if (first) {
327
- result = await statement.bind(...formattedParams).first();
328
- if (!result) return null;
329
- return result;
330
- } else {
331
- result = await statement.bind(...formattedParams).all();
332
- const results = result.results || [];
333
- if (result.meta) {
334
- this.logger.debug("Query metadata", { meta: result.meta });
335
- }
336
- return results;
337
- }
338
- } else {
339
- if (first) {
340
- result = await statement.first();
341
- if (!result) return null;
342
- return result;
343
- } else {
344
- result = await statement.all();
345
- const results = result.results || [];
346
- if (result.meta) {
347
- this.logger.debug("Query metadata", { meta: result.meta });
348
- }
349
- return results;
350
- }
351
- }
352
- } catch (workerError) {
353
- this.logger.error("Workers Binding API error", {
354
- message: workerError instanceof Error ? workerError.message : String(workerError),
355
- sql
356
- });
357
- throw new Error(`D1 Workers API error: ${workerError.message}`);
358
- }
359
- }
360
- async executeRestQuery({
361
- sql,
362
- params = [],
363
- first = false
364
- }) {
365
- if (!this.client) {
366
- throw new Error("Missing required REST API configuration");
367
- }
368
- try {
369
- const response = await this.client.query({
370
- sql,
371
- params: this.formatSqlParams(params)
372
- });
373
- const result = response.result || [];
374
- const results = result.flatMap((r) => r.results || []);
375
- if (first) {
376
- const firstResult = isArrayOfRecords(results) && results.length > 0 ? results[0] : null;
377
- if (!firstResult) return null;
378
- return firstResult;
379
- }
380
- return results;
381
- } catch (restError) {
382
- this.logger.error("REST API error", {
383
- message: restError instanceof Error ? restError.message : String(restError),
384
- sql
385
- });
386
- throw new Error(`D1 REST API error: ${restError.message}`);
387
- }
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);
388
252
  }
389
- /**
390
- * Execute a SQL query against the D1 database
391
- * @param options Query options including SQL, parameters, and whether to return only the first result
392
- * @returns Query results as an array or a single object if first=true
393
- */
394
- async executeQuery(options) {
395
- const { sql, params = [], first = false } = options;
253
+ if (type === "jsonb" && typeof value === "string") {
396
254
  try {
397
- this.logger.debug("Executing SQL query", { sql, params, first });
398
- if (this.binding) {
399
- return this.executeWorkersBindingQuery({ sql, params, first });
400
- } else if (this.client) {
401
- return this.executeRestQuery({ sql, params, first });
402
- } else {
403
- throw new Error("No valid D1 configuration provided");
404
- }
405
- } catch (error) {
406
- this.logger.error("Error executing SQL query", {
407
- message: error instanceof Error ? error.message : String(error),
408
- sql,
409
- params,
410
- first
411
- });
412
- throw new Error(`D1 query error: ${error.message}`);
255
+ return JSON.parse(value);
256
+ } catch {
257
+ return value;
413
258
  }
414
259
  }
415
- // Helper to get existing table columns
416
- async getTableColumns(tableName) {
260
+ if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
417
261
  try {
418
- const sql = `PRAGMA table_info(${tableName})`;
419
- const result = await this.executeQuery({ sql, params: [] });
420
- if (!result || !Array.isArray(result)) {
421
- return [];
422
- }
423
- return result.map((row) => ({
424
- name: row.name,
425
- type: row.type
426
- }));
427
- } catch (error) {
428
- this.logger.error(`Error getting table columns for ${tableName}:`, {
429
- message: error instanceof Error ? error.message : String(error)
430
- });
431
- return [];
262
+ return JSON.parse(value);
263
+ } catch {
264
+ return value;
432
265
  }
433
266
  }
434
- // Helper to serialize objects to JSON strings
435
- serializeValue(value) {
436
- if (value === null || value === void 0) return null;
437
- if (value instanceof Date) {
438
- return this.serializeDate(value);
439
- }
440
- if (typeof value === "object") {
441
- return JSON.stringify(value);
442
- }
443
- return value;
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;
444
276
  }
445
- // Helper to deserialize JSON strings to objects
446
- deserializeValue(value, type) {
447
- if (value === null || value === void 0) return null;
448
- if (type === "date" && typeof value === "string") {
449
- return new Date(value);
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);
450
285
  }
451
- if (type === "jsonb" && typeof value === "string") {
452
- try {
453
- return JSON.parse(value);
454
- } catch {
455
- return value;
456
- }
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)`);
457
290
  }
458
- if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
459
- try {
460
- return JSON.parse(value);
461
- } catch {
462
- return value;
463
- }
291
+ if (dateRange?.start) {
292
+ conditions.push(`created_at >= ?`);
293
+ queryParams.push(storage.serializeDate(dateRange.start));
464
294
  }
465
- return value;
466
- }
467
- getSqlType(type) {
468
- switch (type) {
469
- case "bigint":
470
- return "INTEGER";
471
- // SQLite uses INTEGER for all integer sizes
472
- case "jsonb":
473
- return "TEXT";
474
- // Store JSON as TEXT in SQLite
475
- default:
476
- return super.getSqlType(type);
295
+ if (dateRange?.end) {
296
+ conditions.push(`created_at <= ?`);
297
+ queryParams.push(storage.serializeDate(dateRange.end));
477
298
  }
478
- }
479
- async createTable({
480
- tableName,
481
- schema
482
- }) {
483
- const fullTableName = this.getTableName(tableName);
484
- const columnDefinitions = Object.entries(schema).map(([colName, colDef]) => {
485
- const type = this.getSqlType(colDef.type);
486
- const nullable = colDef.nullable === false ? "NOT NULL" : "";
487
- const primaryKey = colDef.primaryKey ? "PRIMARY KEY" : "";
488
- return `${colName} ${type} ${nullable} ${primaryKey}`.trim();
489
- });
490
- const tableConstraints = [];
491
- if (tableName === storage.TABLE_WORKFLOW_SNAPSHOT) {
492
- tableConstraints.push("UNIQUE (workflow_name, run_id)");
299
+ const countQueryBuilder = createSqlBuilder().count().from(fullTableName);
300
+ if (conditions.length > 0) {
301
+ countQueryBuilder.where(conditions.join(" AND "), ...queryParams);
493
302
  }
303
+ const { sql: countSql, params: countParams } = countQueryBuilder.build();
494
304
  try {
495
- const query = createSqlBuilder().createTable(fullTableName, columnDefinitions, tableConstraints);
496
- const { sql, params } = query.build();
497
- await this.executeQuery({ sql, params });
498
- this.logger.debug(`Created table ${fullTableName}`);
499
- } catch (error$1) {
500
- this.logger.error(`Error creating table ${fullTableName}:`, {
501
- message: error$1 instanceof Error ? error$1.message : String(error$1)
305
+ const countResult = await this.operations.executeQuery({
306
+ sql: countSql,
307
+ params: countParams,
308
+ first: true
309
+ });
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
+ };
320
+ }
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
+ };
502
349
  });
350
+ const hasMore = currentOffset + evals.length < total;
351
+ return {
352
+ evals,
353
+ total,
354
+ page,
355
+ perPage,
356
+ hasMore
357
+ };
358
+ } catch (error$1) {
503
359
  throw new error.MastraError(
504
360
  {
505
- id: "CLOUDFLARE_D1_STORAGE_CREATE_TABLE_ERROR",
361
+ id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
506
362
  domain: error.ErrorDomain.STORAGE,
507
363
  category: error.ErrorCategory.THIRD_PARTY,
508
- text: `Failed to create table ${fullTableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
509
- details: { tableName }
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 ?? "" }
510
366
  },
511
367
  error$1
512
368
  );
513
369
  }
514
370
  }
515
371
  /**
516
- * Alters table schema to add columns if they don't exist
517
- * @param tableName Name of the table
518
- * @param schema Schema of the table
519
- * @param ifNotExists Array of column names to add if they don't exist
372
+ * @deprecated use getEvals instead
520
373
  */
521
- async alterTable({
522
- tableName,
523
- schema,
524
- ifNotExists
525
- }) {
526
- const fullTableName = this.getTableName(tableName);
374
+ async getEvalsByAgentName(agentName, type) {
375
+ const fullTableName = this.operations.getTableName(storage.TABLE_EVALS);
527
376
  try {
528
- const existingColumns = await this.getTableColumns(fullTableName);
529
- const existingColumnNames = new Set(existingColumns.map((col) => col.name.toLowerCase()));
530
- for (const columnName of ifNotExists) {
531
- if (!existingColumnNames.has(columnName.toLowerCase()) && schema[columnName]) {
532
- const columnDef = schema[columnName];
533
- const sqlType = this.getSqlType(columnDef.type);
534
- const nullable = columnDef.nullable === false ? "NOT NULL" : "";
535
- const defaultValue = columnDef.nullable === false ? this.getDefaultValue(columnDef.type) : "";
536
- const alterSql = `ALTER TABLE ${fullTableName} ADD COLUMN ${columnName} ${sqlType} ${nullable} ${defaultValue}`.trim();
537
- await this.executeQuery({ sql: alterSql, params: [] });
538
- this.logger.debug(`Added column ${columnName} to table ${fullTableName}`);
539
- }
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)");
540
382
  }
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
+ }) : [];
541
402
  } catch (error$1) {
542
- throw new error.MastraError(
403
+ const mastraError = new error.MastraError(
543
404
  {
544
- id: "CLOUDFLARE_D1_STORAGE_ALTER_TABLE_ERROR",
405
+ id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
545
406
  domain: error.ErrorDomain.STORAGE,
546
407
  category: error.ErrorCategory.THIRD_PARTY,
547
- text: `Failed to alter table ${fullTableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
548
- details: { tableName }
408
+ text: `Failed to retrieve evals for agent ${agentName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
409
+ details: { agentName }
549
410
  },
550
411
  error$1
551
412
  );
413
+ this.logger?.error(mastraError.toString());
414
+ this.logger?.trackException(mastraError);
415
+ return [];
552
416
  }
553
417
  }
554
- async clearTable({ tableName }) {
555
- const fullTableName = this.getTableName(tableName);
418
+ };
419
+ var MemoryStorageD1 = class extends storage.MemoryStorage {
420
+ operations;
421
+ constructor({ operations }) {
422
+ super();
423
+ this.operations = operations;
424
+ }
425
+ async getResourceById({ resourceId }) {
426
+ const resource = await this.operations.load({
427
+ tableName: storage.TABLE_RESOURCES,
428
+ keys: { id: resourceId }
429
+ });
430
+ if (!resource) return null;
556
431
  try {
557
- const query = createSqlBuilder().delete(fullTableName);
558
- const { sql, params } = query.build();
559
- await this.executeQuery({ sql, params });
560
- this.logger.debug(`Cleared table ${fullTableName}`);
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
+ };
561
438
  } catch (error$1) {
562
- throw new error.MastraError(
439
+ const mastraError = new error.MastraError(
563
440
  {
564
- id: "CLOUDFLARE_D1_STORAGE_CLEAR_TABLE_ERROR",
441
+ id: "CLOUDFLARE_D1_STORAGE_GET_RESOURCE_BY_ID_ERROR",
565
442
  domain: error.ErrorDomain.STORAGE,
566
443
  category: error.ErrorCategory.THIRD_PARTY,
567
- text: `Failed to clear table ${fullTableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
568
- details: { tableName }
444
+ text: `Error processing resource ${resourceId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
445
+ details: { resourceId }
569
446
  },
570
447
  error$1
571
448
  );
449
+ this.logger?.error(mastraError.toString());
450
+ this.logger?.trackException(mastraError);
451
+ return null;
572
452
  }
573
453
  }
574
- async processRecord(record) {
575
- const processedRecord = {};
576
- for (const [key, value] of Object.entries(record)) {
577
- processedRecord[key] = this.serializeValue(value);
578
- }
579
- return processedRecord;
580
- }
581
- async insert({ tableName, record }) {
582
- const fullTableName = this.getTableName(tableName);
583
- 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);
584
464
  const columns = Object.keys(processedRecord);
585
465
  const values = Object.values(processedRecord);
586
- 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);
587
473
  const { sql, params } = query.build();
588
474
  try {
589
- await this.executeQuery({ sql, params });
475
+ await this.operations.executeQuery({ sql, params });
476
+ return resource;
590
477
  } catch (error$1) {
591
478
  throw new error.MastraError(
592
479
  {
593
- id: "CLOUDFLARE_D1_STORAGE_INSERT_ERROR",
480
+ id: "CLOUDFLARE_D1_STORAGE_SAVE_RESOURCE_ERROR",
594
481
  domain: error.ErrorDomain.STORAGE,
595
482
  category: error.ErrorCategory.THIRD_PARTY,
596
- text: `Failed to insert into ${fullTableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
597
- details: { tableName }
483
+ text: `Failed to save resource to ${fullTableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
484
+ details: { resourceId: resource.id }
598
485
  },
599
486
  error$1
600
487
  );
601
488
  }
602
489
  }
603
- async load({ tableName, keys }) {
604
- const fullTableName = this.getTableName(tableName);
605
- const query = createSqlBuilder().select("*").from(fullTableName);
606
- let firstKey = true;
607
- for (const [key, value] of Object.entries(keys)) {
608
- if (firstKey) {
609
- query.where(`${key} = ?`, value);
610
- firstKey = false;
611
- } else {
612
- query.andWhere(`${key} = ?`, value);
613
- }
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 });
614
505
  }
615
- 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);
616
520
  const { sql, params } = query.build();
617
521
  try {
618
- const result = await this.executeQuery({ sql, params, first: true });
619
- if (!result) return null;
620
- const processedResult = {};
621
- for (const [key, value] of Object.entries(result)) {
622
- processedResult[key] = this.deserializeValue(value);
623
- }
624
- return processedResult;
522
+ await this.operations.executeQuery({ sql, params });
523
+ return updatedResource;
625
524
  } catch (error$1) {
626
525
  throw new error.MastraError(
627
526
  {
628
- id: "CLOUDFLARE_D1_STORAGE_LOAD_ERROR",
527
+ id: "CLOUDFLARE_D1_STORAGE_UPDATE_RESOURCE_ERROR",
629
528
  domain: error.ErrorDomain.STORAGE,
630
529
  category: error.ErrorCategory.THIRD_PARTY,
631
- text: `Failed to load from ${fullTableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
632
- details: { tableName }
530
+ text: `Failed to update resource ${resourceId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
531
+ details: { resourceId }
633
532
  },
634
533
  error$1
635
534
  );
636
535
  }
637
536
  }
638
537
  async getThreadById({ threadId }) {
639
- const thread = await this.load({
538
+ const thread = await this.operations.load({
640
539
  tableName: storage.TABLE_THREADS,
641
540
  keys: { id: threadId }
642
541
  });
643
542
  if (!thread) return null;
543
+ console.log("thread", thread);
644
544
  try {
645
545
  return {
646
546
  ...thread,
647
- createdAt: this.ensureDate(thread.createdAt),
648
- updatedAt: this.ensureDate(thread.updatedAt),
547
+ createdAt: storage.ensureDate(thread.createdAt),
548
+ updatedAt: storage.ensureDate(thread.updatedAt),
649
549
  metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
650
550
  };
651
551
  } catch (error$1) {
@@ -668,15 +568,15 @@ var D1Store = class extends storage.MastraStorage {
668
568
  * @deprecated use getThreadsByResourceIdPaginated instead
669
569
  */
670
570
  async getThreadsByResourceId({ resourceId }) {
671
- const fullTableName = this.getTableName(storage.TABLE_THREADS);
571
+ const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
672
572
  try {
673
573
  const query = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId);
674
574
  const { sql, params } = query.build();
675
- const results = await this.executeQuery({ sql, params });
575
+ const results = await this.operations.executeQuery({ sql, params });
676
576
  return (isArrayOfRecords(results) ? results : []).map((thread) => ({
677
577
  ...thread,
678
- createdAt: this.ensureDate(thread.createdAt),
679
- updatedAt: this.ensureDate(thread.updatedAt),
578
+ createdAt: storage.ensureDate(thread.createdAt),
579
+ updatedAt: storage.ensureDate(thread.updatedAt),
680
580
  metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
681
581
  }));
682
582
  } catch (error$1) {
@@ -697,19 +597,19 @@ var D1Store = class extends storage.MastraStorage {
697
597
  }
698
598
  async getThreadsByResourceIdPaginated(args) {
699
599
  const { resourceId, page, perPage } = args;
700
- const fullTableName = this.getTableName(storage.TABLE_THREADS);
600
+ const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
701
601
  const mapRowToStorageThreadType = (row) => ({
702
602
  ...row,
703
- createdAt: this.ensureDate(row.createdAt),
704
- updatedAt: this.ensureDate(row.updatedAt),
603
+ createdAt: storage.ensureDate(row.createdAt),
604
+ updatedAt: storage.ensureDate(row.updatedAt),
705
605
  metadata: typeof row.metadata === "string" ? JSON.parse(row.metadata || "{}") : row.metadata || {}
706
606
  });
707
607
  try {
708
608
  const countQuery = createSqlBuilder().count().from(fullTableName).where("resourceId = ?", resourceId);
709
- const countResult = await this.executeQuery(countQuery.build());
609
+ const countResult = await this.operations.executeQuery(countQuery.build());
710
610
  const total = Number(countResult?.[0]?.count ?? 0);
711
611
  const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId).orderBy("createdAt", "DESC").limit(perPage).offset(page * perPage);
712
- const results = await this.executeQuery(selectQuery.build());
612
+ const results = await this.operations.executeQuery(selectQuery.build());
713
613
  const threads = results.map(mapRowToStorageThreadType);
714
614
  return {
715
615
  threads,
@@ -741,16 +641,16 @@ var D1Store = class extends storage.MastraStorage {
741
641
  }
742
642
  }
743
643
  async saveThread({ thread }) {
744
- const fullTableName = this.getTableName(storage.TABLE_THREADS);
644
+ const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
745
645
  const threadToSave = {
746
646
  id: thread.id,
747
647
  resourceId: thread.resourceId,
748
648
  title: thread.title,
749
649
  metadata: thread.metadata ? JSON.stringify(thread.metadata) : null,
750
- createdAt: thread.createdAt,
751
- updatedAt: thread.updatedAt
650
+ createdAt: thread.createdAt.toISOString(),
651
+ updatedAt: thread.updatedAt.toISOString()
752
652
  };
753
- const processedRecord = await this.processRecord(threadToSave);
653
+ const processedRecord = await this.operations.processRecord(threadToSave);
754
654
  const columns = Object.keys(processedRecord);
755
655
  const values = Object.values(processedRecord);
756
656
  const updateMap = {
@@ -763,7 +663,7 @@ var D1Store = class extends storage.MastraStorage {
763
663
  const query = createSqlBuilder().insert(fullTableName, columns, values, ["id"], updateMap);
764
664
  const { sql, params } = query.build();
765
665
  try {
766
- await this.executeQuery({ sql, params });
666
+ await this.operations.executeQuery({ sql, params });
767
667
  return thread;
768
668
  } catch (error$1) {
769
669
  throw new error.MastraError(
@@ -788,16 +688,17 @@ var D1Store = class extends storage.MastraStorage {
788
688
  if (!thread) {
789
689
  throw new Error(`Thread ${id} not found`);
790
690
  }
791
- const fullTableName = this.getTableName(storage.TABLE_THREADS);
691
+ const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
792
692
  const mergedMetadata = {
793
693
  ...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
794
694
  ...metadata
795
695
  };
696
+ const updatedAt = /* @__PURE__ */ new Date();
796
697
  const columns = ["title", "metadata", "updatedAt"];
797
- const values = [title, JSON.stringify(mergedMetadata), (/* @__PURE__ */ new Date()).toISOString()];
698
+ const values = [title, JSON.stringify(mergedMetadata), updatedAt.toISOString()];
798
699
  const query = createSqlBuilder().update(fullTableName, columns, values).where("id = ?", id);
799
700
  const { sql, params } = query.build();
800
- await this.executeQuery({ sql, params });
701
+ await this.operations.executeQuery({ sql, params });
801
702
  return {
802
703
  ...thread,
803
704
  title,
@@ -805,7 +706,7 @@ var D1Store = class extends storage.MastraStorage {
805
706
  ...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
806
707
  ...metadata
807
708
  },
808
- updatedAt: /* @__PURE__ */ new Date()
709
+ updatedAt
809
710
  };
810
711
  } catch (error$1) {
811
712
  throw new error.MastraError(
@@ -821,15 +722,15 @@ var D1Store = class extends storage.MastraStorage {
821
722
  }
822
723
  }
823
724
  async deleteThread({ threadId }) {
824
- const fullTableName = this.getTableName(storage.TABLE_THREADS);
725
+ const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
825
726
  try {
826
727
  const deleteThreadQuery = createSqlBuilder().delete(fullTableName).where("id = ?", threadId);
827
728
  const { sql: threadSql, params: threadParams } = deleteThreadQuery.build();
828
- await this.executeQuery({ sql: threadSql, params: threadParams });
829
- const messagesTableName = this.getTableName(storage.TABLE_MESSAGES);
729
+ await this.operations.executeQuery({ sql: threadSql, params: threadParams });
730
+ const messagesTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
830
731
  const deleteMessagesQuery = createSqlBuilder().delete(messagesTableName).where("thread_id = ?", threadId);
831
732
  const { sql: messagesSql, params: messagesParams } = deleteMessagesQuery.build();
832
- await this.executeQuery({ sql: messagesSql, params: messagesParams });
733
+ await this.operations.executeQuery({ sql: messagesSql, params: messagesParams });
833
734
  } catch (error$1) {
834
735
  throw new error.MastraError(
835
736
  {
@@ -860,6 +761,9 @@ var D1Store = class extends storage.MastraStorage {
860
761
  if (!message.role) {
861
762
  throw new Error(`Message at index ${i} missing role`);
862
763
  }
764
+ if (!message.resourceId) {
765
+ throw new Error(`Message at index ${i} missing resourceId`);
766
+ }
863
767
  const thread = await this.getThreadById({ threadId: message.threadId });
864
768
  if (!thread) {
865
769
  throw new Error(`Thread ${message.threadId} not found`);
@@ -878,13 +782,13 @@ var D1Store = class extends storage.MastraStorage {
878
782
  };
879
783
  });
880
784
  await Promise.all([
881
- this.batchUpsert({
785
+ this.operations.batchUpsert({
882
786
  tableName: storage.TABLE_MESSAGES,
883
787
  records: messagesToInsert
884
788
  }),
885
789
  // Update thread's updatedAt timestamp
886
- this.executeQuery({
887
- sql: `UPDATE ${this.getTableName(storage.TABLE_THREADS)} SET updatedAt = ? WHERE id = ?`,
790
+ this.operations.executeQuery({
791
+ sql: `UPDATE ${this.operations.getTableName(storage.TABLE_THREADS)} SET updatedAt = ? WHERE id = ?`,
888
792
  params: [now.toISOString(), threadId]
889
793
  })
890
794
  ]);
@@ -907,56 +811,67 @@ var D1Store = class extends storage.MastraStorage {
907
811
  async _getIncludedMessages(threadId, selectBy) {
908
812
  const include = selectBy?.include;
909
813
  if (!include) return null;
910
- const prevMax = Math.max(...include.map((i) => i.withPreviousMessages || 0));
911
- const nextMax = Math.max(...include.map((i) => i.withNextMessages || 0));
912
- const includeIds = include.map((i) => i.id);
913
- const sql = `
914
- WITH ordered_messages AS (
915
- SELECT
916
- *,
917
- ROW_NUMBER() OVER (ORDER BY createdAt DESC) AS row_num
918
- FROM ${this.getTableName(storage.TABLE_MESSAGES)}
919
- WHERE thread_id = ?
920
- )
921
- SELECT
922
- m.id,
923
- m.content,
924
- m.role,
925
- m.type,
926
- m.createdAt,
927
- m.thread_id AS threadId
928
- FROM ordered_messages m
929
- WHERE m.id IN (${includeIds.map(() => "?").join(",")})
930
- OR EXISTS (
931
- SELECT 1 FROM ordered_messages target
932
- WHERE target.id IN (${includeIds.map(() => "?").join(",")})
933
- AND (
934
- (m.row_num <= target.row_num + ? AND m.row_num > target.row_num)
935
- OR
936
- (m.row_num >= target.row_num - ? AND m.row_num < target.row_num)
937
- )
938
- )
939
- ORDER BY m.createdAt DESC
940
- `;
941
- const params = [
942
- threadId,
943
- ...includeIds,
944
- // for m.id IN (...)
945
- ...includeIds,
946
- // for target.id IN (...)
947
- prevMax,
948
- nextMax
949
- ];
950
- const messages = await this.executeQuery({ sql, params });
951
- return messages;
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;
952
867
  }
953
868
  async getMessages({
954
869
  threadId,
955
870
  selectBy,
956
871
  format
957
872
  }) {
958
- const fullTableName = this.getTableName(storage.TABLE_MESSAGES);
959
- const limit = this.resolveMessageLimit({
873
+ const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
874
+ const limit = storage.resolveMessageLimit({
960
875
  last: selectBy?.last,
961
876
  defaultLimit: 40
962
877
  });
@@ -974,7 +889,7 @@ var D1Store = class extends storage.MastraStorage {
974
889
  }
975
890
  query.orderBy("createdAt", "DESC").limit(limit);
976
891
  const { sql, params } = query.build();
977
- const result = await this.executeQuery({ sql, params });
892
+ const result = await this.operations.executeQuery({ sql, params });
978
893
  if (Array.isArray(result)) messages.push(...result);
979
894
  messages.sort((a, b) => {
980
895
  const aRecord = a;
@@ -987,7 +902,7 @@ var D1Store = class extends storage.MastraStorage {
987
902
  const processedMsg = {};
988
903
  for (const [key, value] of Object.entries(message)) {
989
904
  if (key === `type` && value === `v2`) continue;
990
- processedMsg[key] = this.deserializeValue(value);
905
+ processedMsg[key] = deserializeValue(value);
991
906
  }
992
907
  return processedMsg;
993
908
  });
@@ -1016,9 +931,10 @@ var D1Store = class extends storage.MastraStorage {
1016
931
  selectBy,
1017
932
  format
1018
933
  }) {
1019
- const { dateRange, page = 0, perPage = 40 } = selectBy?.pagination || {};
934
+ const { dateRange, page = 0, perPage: perPageInput } = selectBy?.pagination || {};
1020
935
  const { start: fromDate, end: toDate } = dateRange || {};
1021
- const fullTableName = this.getTableName(storage.TABLE_MESSAGES);
936
+ const perPage = perPageInput !== void 0 ? perPageInput : storage.resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
937
+ const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
1022
938
  const messages = [];
1023
939
  try {
1024
940
  if (selectBy?.include?.length) {
@@ -1027,30 +943,80 @@ var D1Store = class extends storage.MastraStorage {
1027
943
  }
1028
944
  const countQuery = createSqlBuilder().count().from(fullTableName).where("thread_id = ?", threadId);
1029
945
  if (fromDate) {
1030
- countQuery.andWhere("createdAt >= ?", this.serializeDate(fromDate));
946
+ countQuery.andWhere("createdAt >= ?", storage.serializeDate(fromDate));
1031
947
  }
1032
948
  if (toDate) {
1033
- countQuery.andWhere("createdAt <= ?", this.serializeDate(toDate));
949
+ countQuery.andWhere("createdAt <= ?", storage.serializeDate(toDate));
1034
950
  }
1035
- const countResult = await this.executeQuery(countQuery.build());
951
+ const countResult = await this.operations.executeQuery(countQuery.build());
1036
952
  const total = Number(countResult[0]?.count ?? 0);
1037
- const query = createSqlBuilder().select(["id", "content", "role", "type", "createdAt", "thread_id AS threadId"]).from(fullTableName).where("thread_id = ?", threadId);
953
+ if (total === 0 && messages.length === 0) {
954
+ return {
955
+ messages: [],
956
+ total: 0,
957
+ page,
958
+ perPage,
959
+ hasMore: false
960
+ };
961
+ }
962
+ const excludeIds = messages.map((m) => m.id);
963
+ const excludeCondition = excludeIds.length > 0 ? `AND id NOT IN (${excludeIds.map(() => "?").join(",")})` : "";
964
+ let query;
965
+ let queryParams = [threadId];
1038
966
  if (fromDate) {
1039
- query.andWhere("createdAt >= ?", this.serializeDate(fromDate));
967
+ queryParams.push(storage.serializeDate(fromDate));
1040
968
  }
1041
969
  if (toDate) {
1042
- query.andWhere("createdAt <= ?", this.serializeDate(toDate));
970
+ queryParams.push(storage.serializeDate(toDate));
971
+ }
972
+ if (excludeIds.length > 0) {
973
+ queryParams.push(...excludeIds);
974
+ }
975
+ if (selectBy?.last && selectBy.last > 0) {
976
+ query = `
977
+ SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
978
+ FROM ${fullTableName}
979
+ WHERE thread_id = ?
980
+ ${fromDate ? "AND createdAt >= ?" : ""}
981
+ ${toDate ? "AND createdAt <= ?" : ""}
982
+ ${excludeCondition}
983
+ ORDER BY createdAt DESC
984
+ LIMIT ?
985
+ `;
986
+ queryParams.push(selectBy.last);
987
+ } else {
988
+ query = `
989
+ SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
990
+ FROM ${fullTableName}
991
+ WHERE thread_id = ?
992
+ ${fromDate ? "AND createdAt >= ?" : ""}
993
+ ${toDate ? "AND createdAt <= ?" : ""}
994
+ ${excludeCondition}
995
+ ORDER BY createdAt DESC
996
+ LIMIT ? OFFSET ?
997
+ `;
998
+ queryParams.push(perPage, page * perPage);
999
+ }
1000
+ const results = await this.operations.executeQuery({ sql: query, params: queryParams });
1001
+ const processedMessages = results.map((message) => {
1002
+ const processedMsg = {};
1003
+ for (const [key, value] of Object.entries(message)) {
1004
+ if (key === `type` && value === `v2`) continue;
1005
+ processedMsg[key] = deserializeValue(value);
1006
+ }
1007
+ return processedMsg;
1008
+ });
1009
+ if (selectBy?.last) {
1010
+ processedMessages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
1043
1011
  }
1044
- query.orderBy("createdAt", "DESC").limit(perPage).offset(page * perPage);
1045
- const results = await this.executeQuery(query.build());
1046
- const list = new agent.MessageList().add(results, "memory");
1012
+ const list = new agent.MessageList().add(processedMessages, "memory");
1047
1013
  messages.push(...format === `v2` ? list.get.all.v2() : list.get.all.v1());
1048
1014
  return {
1049
1015
  messages,
1050
1016
  total,
1051
1017
  page,
1052
1018
  perPage,
1053
- hasMore: page * perPage + messages.length < total
1019
+ hasMore: selectBy?.last ? false : page * perPage + messages.length < total
1054
1020
  };
1055
1021
  } catch (error$1) {
1056
1022
  const mastraError = new error.MastraError(
@@ -1074,132 +1040,446 @@ var D1Store = class extends storage.MastraStorage {
1074
1040
  };
1075
1041
  }
1076
1042
  }
1077
- async persistWorkflowSnapshot({
1078
- workflowName,
1079
- runId,
1080
- snapshot
1081
- }) {
1082
- const fullTableName = this.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
1083
- const now = (/* @__PURE__ */ new Date()).toISOString();
1084
- const currentSnapshot = await this.load({
1085
- tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
1086
- keys: { workflow_name: workflowName, run_id: runId }
1087
- });
1088
- const persisting = currentSnapshot ? {
1089
- ...currentSnapshot,
1090
- snapshot: JSON.stringify(snapshot),
1091
- updatedAt: now
1092
- } : {
1093
- workflow_name: workflowName,
1094
- run_id: runId,
1095
- snapshot,
1096
- createdAt: now,
1097
- updatedAt: now
1098
- };
1099
- const processedRecord = await this.processRecord(persisting);
1100
- const columns = Object.keys(processedRecord);
1101
- const values = Object.values(processedRecord);
1102
- const updateMap = {
1103
- snapshot: "excluded.snapshot",
1104
- updatedAt: "excluded.updatedAt"
1105
- };
1106
- this.logger.debug("Persisting workflow snapshot", { workflowName, runId });
1107
- const query = createSqlBuilder().insert(fullTableName, columns, values, ["workflow_name", "run_id"], updateMap);
1108
- const { sql, params } = query.build();
1109
- try {
1110
- await this.executeQuery({ sql, params });
1111
- } catch (error$1) {
1112
- throw new error.MastraError(
1113
- {
1114
- id: "CLOUDFLARE_D1_STORAGE_PERSIST_WORKFLOW_SNAPSHOT_ERROR",
1115
- domain: error.ErrorDomain.STORAGE,
1116
- category: error.ErrorCategory.THIRD_PARTY,
1117
- text: `Failed to persist workflow snapshot: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
1118
- details: { workflowName, runId }
1119
- },
1120
- error$1
1121
- );
1043
+ async updateMessages(args) {
1044
+ const { messages } = args;
1045
+ this.logger.debug("Updating messages", { count: messages.length });
1046
+ if (!messages.length) {
1047
+ return [];
1122
1048
  }
1123
- }
1124
- async loadWorkflowSnapshot(params) {
1125
- const { workflowName, runId } = params;
1126
- this.logger.debug("Loading workflow snapshot", { workflowName, runId });
1049
+ const messageIds = messages.map((m) => m.id);
1050
+ const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
1051
+ const threadsTableName = this.operations.getTableName(storage.TABLE_THREADS);
1127
1052
  try {
1128
- const d = await this.load({
1129
- tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
1130
- keys: {
1131
- workflow_name: workflowName,
1132
- run_id: runId
1053
+ const placeholders = messageIds.map(() => "?").join(",");
1054
+ const selectQuery = `SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId FROM ${fullTableName} WHERE id IN (${placeholders})`;
1055
+ const existingMessages = await this.operations.executeQuery({ sql: selectQuery, params: messageIds });
1056
+ if (existingMessages.length === 0) {
1057
+ return [];
1058
+ }
1059
+ const parsedExistingMessages = existingMessages.map((msg) => {
1060
+ if (typeof msg.content === "string") {
1061
+ try {
1062
+ msg.content = JSON.parse(msg.content);
1063
+ } catch {
1064
+ }
1133
1065
  }
1066
+ return msg;
1067
+ });
1068
+ const threadIdsToUpdate = /* @__PURE__ */ new Set();
1069
+ const updateQueries = [];
1070
+ for (const existingMessage of parsedExistingMessages) {
1071
+ const updatePayload = messages.find((m) => m.id === existingMessage.id);
1072
+ if (!updatePayload) continue;
1073
+ const { id, ...fieldsToUpdate } = updatePayload;
1074
+ if (Object.keys(fieldsToUpdate).length === 0) continue;
1075
+ threadIdsToUpdate.add(existingMessage.threadId);
1076
+ if ("threadId" in updatePayload && updatePayload.threadId && updatePayload.threadId !== existingMessage.threadId) {
1077
+ threadIdsToUpdate.add(updatePayload.threadId);
1078
+ }
1079
+ const setClauses = [];
1080
+ const values = [];
1081
+ const updatableFields = { ...fieldsToUpdate };
1082
+ if (updatableFields.content) {
1083
+ const existingContent = existingMessage.content || {};
1084
+ const newContent = {
1085
+ ...existingContent,
1086
+ ...updatableFields.content,
1087
+ // Deep merge metadata if it exists on both
1088
+ ...existingContent?.metadata && updatableFields.content.metadata ? {
1089
+ metadata: {
1090
+ ...existingContent.metadata,
1091
+ ...updatableFields.content.metadata
1092
+ }
1093
+ } : {}
1094
+ };
1095
+ setClauses.push(`content = ?`);
1096
+ values.push(JSON.stringify(newContent));
1097
+ delete updatableFields.content;
1098
+ }
1099
+ for (const key in updatableFields) {
1100
+ if (Object.prototype.hasOwnProperty.call(updatableFields, key)) {
1101
+ const dbColumn = key === "threadId" ? "thread_id" : key;
1102
+ setClauses.push(`${dbColumn} = ?`);
1103
+ values.push(updatableFields[key]);
1104
+ }
1105
+ }
1106
+ if (setClauses.length > 0) {
1107
+ values.push(id);
1108
+ const updateQuery = `UPDATE ${fullTableName} SET ${setClauses.join(", ")} WHERE id = ?`;
1109
+ updateQueries.push({ sql: updateQuery, params: values });
1110
+ }
1111
+ }
1112
+ for (const query of updateQueries) {
1113
+ await this.operations.executeQuery(query);
1114
+ }
1115
+ if (threadIdsToUpdate.size > 0) {
1116
+ const threadPlaceholders = Array.from(threadIdsToUpdate).map(() => "?").join(",");
1117
+ const threadUpdateQuery = `UPDATE ${threadsTableName} SET updatedAt = ? WHERE id IN (${threadPlaceholders})`;
1118
+ const threadUpdateParams = [(/* @__PURE__ */ new Date()).toISOString(), ...Array.from(threadIdsToUpdate)];
1119
+ await this.operations.executeQuery({ sql: threadUpdateQuery, params: threadUpdateParams });
1120
+ }
1121
+ const updatedMessages = await this.operations.executeQuery({ sql: selectQuery, params: messageIds });
1122
+ return updatedMessages.map((message) => {
1123
+ if (typeof message.content === "string") {
1124
+ try {
1125
+ message.content = JSON.parse(message.content);
1126
+ } catch {
1127
+ }
1128
+ }
1129
+ return message;
1134
1130
  });
1135
- return d ? d.snapshot : null;
1136
1131
  } catch (error$1) {
1137
1132
  throw new error.MastraError(
1138
1133
  {
1139
- id: "CLOUDFLARE_D1_STORAGE_LOAD_WORKFLOW_SNAPSHOT_ERROR",
1134
+ id: "CLOUDFLARE_D1_STORAGE_UPDATE_MESSAGES_FAILED",
1140
1135
  domain: error.ErrorDomain.STORAGE,
1141
1136
  category: error.ErrorCategory.THIRD_PARTY,
1142
- text: `Failed to load workflow snapshot: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
1143
- details: { workflowName, runId }
1137
+ details: { count: messages.length }
1144
1138
  },
1145
1139
  error$1
1146
1140
  );
1147
1141
  }
1148
1142
  }
1149
- /**
1150
- * Insert multiple records in a batch operation
1151
- * @param tableName The table to insert into
1152
- * @param records The records to insert
1153
- */
1154
- async batchInsert({ tableName, records }) {
1155
- if (records.length === 0) return;
1156
- const fullTableName = this.getTableName(tableName);
1143
+ };
1144
+ var StoreOperationsD1 = class extends storage.StoreOperations {
1145
+ client;
1146
+ binding;
1147
+ tablePrefix;
1148
+ constructor(config) {
1149
+ super();
1150
+ this.client = config.client;
1151
+ this.binding = config.binding;
1152
+ this.tablePrefix = config.tablePrefix || "";
1153
+ }
1154
+ async hasColumn(table, column) {
1155
+ const fullTableName = table.startsWith(this.tablePrefix) ? table : `${this.tablePrefix}${table}`;
1156
+ const sql = `PRAGMA table_info(${fullTableName});`;
1157
+ const result = await this.executeQuery({ sql, params: [] });
1158
+ if (!result || !Array.isArray(result)) return false;
1159
+ return result.some((col) => col.name === column || col.name === column.toLowerCase());
1160
+ }
1161
+ getTableName(tableName) {
1162
+ return `${this.tablePrefix}${tableName}`;
1163
+ }
1164
+ formatSqlParams(params) {
1165
+ return params.map((p) => p === void 0 || p === null ? null : p);
1166
+ }
1167
+ async executeWorkersBindingQuery({
1168
+ sql,
1169
+ params = [],
1170
+ first = false
1171
+ }) {
1172
+ if (!this.binding) {
1173
+ throw new Error("Workers binding is not configured");
1174
+ }
1157
1175
  try {
1158
- const batchSize = 50;
1159
- for (let i = 0; i < records.length; i += batchSize) {
1160
- const batch = records.slice(i, i + batchSize);
1161
- const recordsToInsert = batch;
1162
- if (recordsToInsert.length > 0) {
1163
- const firstRecord = recordsToInsert[0];
1164
- const columns = Object.keys(firstRecord || {});
1165
- for (const record of recordsToInsert) {
1166
- const values = columns.map((col) => {
1167
- if (!record) return null;
1168
- const value = typeof col === "string" ? record[col] : null;
1169
- return this.serializeValue(value);
1170
- });
1171
- const query = createSqlBuilder().insert(fullTableName, columns, values);
1172
- const { sql, params } = query.build();
1173
- await this.executeQuery({ sql, params });
1174
- }
1176
+ const statement = this.binding.prepare(sql);
1177
+ const formattedParams = this.formatSqlParams(params);
1178
+ let result;
1179
+ if (formattedParams.length > 0) {
1180
+ if (first) {
1181
+ result = await statement.bind(...formattedParams).first();
1182
+ if (!result) return null;
1183
+ return result;
1184
+ } else {
1185
+ result = await statement.bind(...formattedParams).all();
1186
+ const results = result.results || [];
1187
+ return results;
1188
+ }
1189
+ } else {
1190
+ if (first) {
1191
+ result = await statement.first();
1192
+ if (!result) return null;
1193
+ return result;
1194
+ } else {
1195
+ result = await statement.all();
1196
+ const results = result.results || [];
1197
+ return results;
1175
1198
  }
1176
- this.logger.debug(
1177
- `Processed batch ${Math.floor(i / batchSize) + 1} of ${Math.ceil(records.length / batchSize)}`
1178
- );
1179
1199
  }
1180
- this.logger.debug(`Successfully batch inserted ${records.length} records into ${tableName}`);
1181
1200
  } catch (error$1) {
1182
1201
  throw new error.MastraError(
1183
1202
  {
1184
- id: "CLOUDFLARE_D1_STORAGE_BATCH_INSERT_ERROR",
1203
+ id: "CLOUDFLARE_D1_STORE_OPERATIONS_WORKERS_BINDING_QUERY_FAILED",
1185
1204
  domain: error.ErrorDomain.STORAGE,
1186
1205
  category: error.ErrorCategory.THIRD_PARTY,
1187
- text: `Failed to batch insert into ${tableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
1188
- details: { tableName }
1206
+ details: { sql }
1189
1207
  },
1190
1208
  error$1
1191
1209
  );
1192
1210
  }
1193
1211
  }
1194
- /**
1195
- * Upsert multiple records in a batch operation
1212
+ async executeRestQuery({
1213
+ sql,
1214
+ params = [],
1215
+ first = false
1216
+ }) {
1217
+ if (!this.client) {
1218
+ throw new Error("D1 client is not configured");
1219
+ }
1220
+ try {
1221
+ const formattedParams = this.formatSqlParams(params);
1222
+ const response = await this.client.query({
1223
+ sql,
1224
+ params: formattedParams
1225
+ });
1226
+ if (!response.result) {
1227
+ return first ? null : [];
1228
+ }
1229
+ if (first) {
1230
+ return response.result[0] || null;
1231
+ }
1232
+ return response.result;
1233
+ } catch (error$1) {
1234
+ throw new error.MastraError(
1235
+ {
1236
+ id: "CLOUDFLARE_D1_STORE_OPERATIONS_REST_QUERY_FAILED",
1237
+ domain: error.ErrorDomain.STORAGE,
1238
+ category: error.ErrorCategory.THIRD_PARTY,
1239
+ details: { sql }
1240
+ },
1241
+ error$1
1242
+ );
1243
+ }
1244
+ }
1245
+ async executeQuery(options) {
1246
+ if (this.binding) {
1247
+ return this.executeWorkersBindingQuery(options);
1248
+ } else if (this.client) {
1249
+ return this.executeRestQuery(options);
1250
+ } else {
1251
+ throw new Error("Neither binding nor client is configured");
1252
+ }
1253
+ }
1254
+ async getTableColumns(tableName) {
1255
+ try {
1256
+ const sql = `PRAGMA table_info(${tableName})`;
1257
+ const result = await this.executeQuery({ sql });
1258
+ if (!result || !Array.isArray(result)) {
1259
+ return [];
1260
+ }
1261
+ return result.map((row) => ({
1262
+ name: row.name,
1263
+ type: row.type
1264
+ }));
1265
+ } catch (error) {
1266
+ this.logger.warn(`Failed to get table columns for ${tableName}:`, error);
1267
+ return [];
1268
+ }
1269
+ }
1270
+ serializeValue(value) {
1271
+ if (value === null || value === void 0) {
1272
+ return null;
1273
+ }
1274
+ if (value instanceof Date) {
1275
+ return value.toISOString();
1276
+ }
1277
+ if (typeof value === "object") {
1278
+ return JSON.stringify(value);
1279
+ }
1280
+ return value;
1281
+ }
1282
+ getSqlType(type) {
1283
+ switch (type) {
1284
+ case "bigint":
1285
+ return "INTEGER";
1286
+ // SQLite uses INTEGER for all integer sizes
1287
+ case "jsonb":
1288
+ return "TEXT";
1289
+ // Store JSON as TEXT in SQLite
1290
+ default:
1291
+ return super.getSqlType(type);
1292
+ }
1293
+ }
1294
+ async createTable({
1295
+ tableName,
1296
+ schema
1297
+ }) {
1298
+ try {
1299
+ const fullTableName = this.getTableName(tableName);
1300
+ const columnDefinitions = Object.entries(schema).map(([colName, colDef]) => {
1301
+ const type = this.getSqlType(colDef.type);
1302
+ const nullable = colDef.nullable === false ? "NOT NULL" : "";
1303
+ const primaryKey = colDef.primaryKey ? "PRIMARY KEY" : "";
1304
+ return `${colName} ${type} ${nullable} ${primaryKey}`.trim();
1305
+ });
1306
+ const tableConstraints = [];
1307
+ if (tableName === storage.TABLE_WORKFLOW_SNAPSHOT) {
1308
+ tableConstraints.push("UNIQUE (workflow_name, run_id)");
1309
+ }
1310
+ const query = createSqlBuilder().createTable(fullTableName, columnDefinitions, tableConstraints);
1311
+ const { sql, params } = query.build();
1312
+ await this.executeQuery({ sql, params });
1313
+ this.logger.debug(`Created table ${fullTableName}`);
1314
+ } catch (error$1) {
1315
+ throw new error.MastraError(
1316
+ {
1317
+ id: "CLOUDFLARE_D1_STORE_OPERATIONS_CREATE_TABLE_FAILED",
1318
+ domain: error.ErrorDomain.STORAGE,
1319
+ category: error.ErrorCategory.THIRD_PARTY,
1320
+ details: { tableName }
1321
+ },
1322
+ error$1
1323
+ );
1324
+ }
1325
+ }
1326
+ async clearTable({ tableName }) {
1327
+ try {
1328
+ const fullTableName = this.getTableName(tableName);
1329
+ const query = createSqlBuilder().delete(fullTableName);
1330
+ const { sql, params } = query.build();
1331
+ await this.executeQuery({ sql, params });
1332
+ this.logger.debug(`Cleared table ${fullTableName}`);
1333
+ } catch (error$1) {
1334
+ throw new error.MastraError(
1335
+ {
1336
+ id: "CLOUDFLARE_D1_STORE_OPERATIONS_CLEAR_TABLE_FAILED",
1337
+ domain: error.ErrorDomain.STORAGE,
1338
+ category: error.ErrorCategory.THIRD_PARTY,
1339
+ details: { tableName }
1340
+ },
1341
+ error$1
1342
+ );
1343
+ }
1344
+ }
1345
+ async dropTable({ tableName }) {
1346
+ try {
1347
+ const fullTableName = this.getTableName(tableName);
1348
+ const sql = `DROP TABLE IF EXISTS ${fullTableName}`;
1349
+ await this.executeQuery({ sql });
1350
+ this.logger.debug(`Dropped table ${fullTableName}`);
1351
+ } catch (error$1) {
1352
+ throw new error.MastraError(
1353
+ {
1354
+ id: "CLOUDFLARE_D1_STORE_OPERATIONS_DROP_TABLE_FAILED",
1355
+ domain: error.ErrorDomain.STORAGE,
1356
+ category: error.ErrorCategory.THIRD_PARTY,
1357
+ details: { tableName }
1358
+ },
1359
+ error$1
1360
+ );
1361
+ }
1362
+ }
1363
+ async alterTable(args) {
1364
+ try {
1365
+ const fullTableName = this.getTableName(args.tableName);
1366
+ const existingColumns = await this.getTableColumns(fullTableName);
1367
+ const existingColumnNames = new Set(existingColumns.map((col) => col.name));
1368
+ for (const [columnName, column] of Object.entries(args.schema)) {
1369
+ if (!existingColumnNames.has(columnName) && args.ifNotExists.includes(columnName)) {
1370
+ const sqlType = this.getSqlType(column.type);
1371
+ const defaultValue = this.getDefaultValue(column.type);
1372
+ const sql = `ALTER TABLE ${fullTableName} ADD COLUMN ${columnName} ${sqlType} ${defaultValue}`;
1373
+ await this.executeQuery({ sql });
1374
+ this.logger.debug(`Added column ${columnName} to table ${fullTableName}`);
1375
+ }
1376
+ }
1377
+ } catch (error$1) {
1378
+ throw new error.MastraError(
1379
+ {
1380
+ id: "CLOUDFLARE_D1_STORE_OPERATIONS_ALTER_TABLE_FAILED",
1381
+ domain: error.ErrorDomain.STORAGE,
1382
+ category: error.ErrorCategory.THIRD_PARTY,
1383
+ details: { tableName: args.tableName }
1384
+ },
1385
+ error$1
1386
+ );
1387
+ }
1388
+ }
1389
+ async insert({ tableName, record }) {
1390
+ try {
1391
+ const fullTableName = this.getTableName(tableName);
1392
+ const processedRecord = await this.processRecord(record);
1393
+ const columns = Object.keys(processedRecord);
1394
+ const values = Object.values(processedRecord);
1395
+ const query = createSqlBuilder().insert(fullTableName, columns, values);
1396
+ const { sql, params } = query.build();
1397
+ await this.executeQuery({ sql, params });
1398
+ } catch (error$1) {
1399
+ throw new error.MastraError(
1400
+ {
1401
+ id: "CLOUDFLARE_D1_STORE_OPERATIONS_INSERT_FAILED",
1402
+ domain: error.ErrorDomain.STORAGE,
1403
+ category: error.ErrorCategory.THIRD_PARTY,
1404
+ details: { tableName }
1405
+ },
1406
+ error$1
1407
+ );
1408
+ }
1409
+ }
1410
+ async batchInsert({ tableName, records }) {
1411
+ try {
1412
+ if (records.length === 0) return;
1413
+ const fullTableName = this.getTableName(tableName);
1414
+ const processedRecords = await Promise.all(records.map((record) => this.processRecord(record)));
1415
+ const columns = Object.keys(processedRecords[0] || {});
1416
+ for (const record of processedRecords) {
1417
+ const values = Object.values(record);
1418
+ const query = createSqlBuilder().insert(fullTableName, columns, values);
1419
+ const { sql, params } = query.build();
1420
+ await this.executeQuery({ sql, params });
1421
+ }
1422
+ } catch (error$1) {
1423
+ throw new error.MastraError(
1424
+ {
1425
+ id: "CLOUDFLARE_D1_STORE_OPERATIONS_BATCH_INSERT_FAILED",
1426
+ domain: error.ErrorDomain.STORAGE,
1427
+ category: error.ErrorCategory.THIRD_PARTY,
1428
+ details: { tableName }
1429
+ },
1430
+ error$1
1431
+ );
1432
+ }
1433
+ }
1434
+ async load({ tableName, keys }) {
1435
+ try {
1436
+ const fullTableName = this.getTableName(tableName);
1437
+ const query = createSqlBuilder().select("*").from(fullTableName);
1438
+ let firstKey = true;
1439
+ for (const [key, value] of Object.entries(keys)) {
1440
+ if (firstKey) {
1441
+ query.where(`${key} = ?`, value);
1442
+ firstKey = false;
1443
+ } else {
1444
+ query.andWhere(`${key} = ?`, value);
1445
+ }
1446
+ }
1447
+ query.limit(1);
1448
+ const { sql, params } = query.build();
1449
+ const result = await this.executeQuery({ sql, params, first: true });
1450
+ if (!result) {
1451
+ return null;
1452
+ }
1453
+ const deserializedResult = {};
1454
+ for (const [key, value] of Object.entries(result)) {
1455
+ deserializedResult[key] = deserializeValue(value);
1456
+ }
1457
+ return deserializedResult;
1458
+ } catch (error$1) {
1459
+ throw new error.MastraError(
1460
+ {
1461
+ id: "CLOUDFLARE_D1_STORE_OPERATIONS_LOAD_FAILED",
1462
+ domain: error.ErrorDomain.STORAGE,
1463
+ category: error.ErrorCategory.THIRD_PARTY,
1464
+ details: { tableName }
1465
+ },
1466
+ error$1
1467
+ );
1468
+ }
1469
+ }
1470
+ async processRecord(record) {
1471
+ const processed = {};
1472
+ for (const [key, value] of Object.entries(record)) {
1473
+ processed[key] = this.serializeValue(value);
1474
+ }
1475
+ return processed;
1476
+ }
1477
+ /**
1478
+ * Upsert multiple records in a batch operation
1196
1479
  * @param tableName The table to insert into
1197
1480
  * @param records The records to insert
1198
1481
  */
1199
- async batchUpsert({
1200
- tableName,
1201
- records
1202
- }) {
1482
+ async batchUpsert({ tableName, records }) {
1203
1483
  if (records.length === 0) return;
1204
1484
  const fullTableName = this.getTableName(tableName);
1205
1485
  try {
@@ -1246,73 +1526,267 @@ var D1Store = class extends storage.MastraStorage {
1246
1526
  );
1247
1527
  }
1248
1528
  }
1249
- /**
1250
- * @deprecated use getTracesPaginated instead
1251
- */
1252
- async getTraces({
1253
- name,
1254
- scope,
1255
- page,
1256
- perPage,
1257
- attributes,
1258
- fromDate,
1259
- toDate
1260
- }) {
1261
- const fullTableName = this.getTableName(storage.TABLE_TRACES);
1529
+ };
1530
+ function transformScoreRow(row) {
1531
+ let input = void 0;
1532
+ if (row.input) {
1262
1533
  try {
1263
- const query = createSqlBuilder().select("*").from(fullTableName).where("1=1");
1264
- if (name) {
1265
- query.andWhere("name LIKE ?", `%${name}%`);
1266
- }
1267
- if (scope) {
1268
- query.andWhere("scope = ?", scope);
1534
+ input = JSON.parse(row.input);
1535
+ } catch {
1536
+ input = row.input;
1537
+ }
1538
+ }
1539
+ return {
1540
+ ...row,
1541
+ input,
1542
+ createdAt: row.createdAtZ || row.createdAt,
1543
+ updatedAt: row.updatedAtZ || row.updatedAt
1544
+ };
1545
+ }
1546
+ var ScoresStorageD1 = class extends storage.ScoresStorage {
1547
+ operations;
1548
+ constructor({ operations }) {
1549
+ super();
1550
+ this.operations = operations;
1551
+ }
1552
+ async getScoreById({ id }) {
1553
+ try {
1554
+ const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
1555
+ const query = createSqlBuilder().select("*").from(fullTableName).where("id = ?", id);
1556
+ const { sql, params } = query.build();
1557
+ const result = await this.operations.executeQuery({ sql, params, first: true });
1558
+ if (!result) {
1559
+ return null;
1269
1560
  }
1270
- if (attributes && Object.keys(attributes).length > 0) {
1271
- for (const [key, value] of Object.entries(attributes)) {
1272
- query.jsonLike("attributes", key, value);
1561
+ return transformScoreRow(result);
1562
+ } catch (error$1) {
1563
+ throw new error.MastraError(
1564
+ {
1565
+ id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORE_BY_ID_FAILED",
1566
+ domain: error.ErrorDomain.STORAGE,
1567
+ category: error.ErrorCategory.THIRD_PARTY
1568
+ },
1569
+ error$1
1570
+ );
1571
+ }
1572
+ }
1573
+ async saveScore(score) {
1574
+ try {
1575
+ const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
1576
+ const { input, ...rest } = score;
1577
+ const serializedRecord = {};
1578
+ for (const [key, value] of Object.entries(rest)) {
1579
+ if (value !== null && value !== void 0) {
1580
+ if (typeof value === "object") {
1581
+ serializedRecord[key] = JSON.stringify(value);
1582
+ } else {
1583
+ serializedRecord[key] = value;
1584
+ }
1585
+ } else {
1586
+ serializedRecord[key] = null;
1273
1587
  }
1274
1588
  }
1275
- if (fromDate) {
1276
- query.andWhere("createdAt >= ?", fromDate instanceof Date ? fromDate.toISOString() : fromDate);
1589
+ serializedRecord.input = JSON.stringify(input);
1590
+ serializedRecord.createdAt = (/* @__PURE__ */ new Date()).toISOString();
1591
+ serializedRecord.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
1592
+ const columns = Object.keys(serializedRecord);
1593
+ const values = Object.values(serializedRecord);
1594
+ const query = createSqlBuilder().insert(fullTableName, columns, values);
1595
+ const { sql, params } = query.build();
1596
+ await this.operations.executeQuery({ sql, params });
1597
+ const scoreFromDb = await this.getScoreById({ id: score.id });
1598
+ return { score: scoreFromDb };
1599
+ } catch (error$1) {
1600
+ throw new error.MastraError(
1601
+ {
1602
+ id: "CLOUDFLARE_D1_STORE_SCORES_SAVE_SCORE_FAILED",
1603
+ domain: error.ErrorDomain.STORAGE,
1604
+ category: error.ErrorCategory.THIRD_PARTY
1605
+ },
1606
+ error$1
1607
+ );
1608
+ }
1609
+ }
1610
+ async getScoresByScorerId({
1611
+ scorerId,
1612
+ pagination
1613
+ }) {
1614
+ try {
1615
+ const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
1616
+ const countQuery = createSqlBuilder().count().from(fullTableName).where("scorerId = ?", scorerId);
1617
+ const countResult = await this.operations.executeQuery(countQuery.build());
1618
+ const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
1619
+ if (total === 0) {
1620
+ return {
1621
+ pagination: {
1622
+ total: 0,
1623
+ page: pagination.page,
1624
+ perPage: pagination.perPage,
1625
+ hasMore: false
1626
+ },
1627
+ scores: []
1628
+ };
1277
1629
  }
1278
- if (toDate) {
1279
- query.andWhere("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
1630
+ const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("scorerId = ?", scorerId).limit(pagination.perPage).offset(pagination.page * pagination.perPage);
1631
+ const { sql, params } = selectQuery.build();
1632
+ const results = await this.operations.executeQuery({ sql, params });
1633
+ const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
1634
+ return {
1635
+ pagination: {
1636
+ total,
1637
+ page: pagination.page,
1638
+ perPage: pagination.perPage,
1639
+ hasMore: total > (pagination.page + 1) * pagination.perPage
1640
+ },
1641
+ scores
1642
+ };
1643
+ } catch (error$1) {
1644
+ throw new error.MastraError(
1645
+ {
1646
+ id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_SCORER_ID_FAILED",
1647
+ domain: error.ErrorDomain.STORAGE,
1648
+ category: error.ErrorCategory.THIRD_PARTY
1649
+ },
1650
+ error$1
1651
+ );
1652
+ }
1653
+ }
1654
+ async getScoresByRunId({
1655
+ runId,
1656
+ pagination
1657
+ }) {
1658
+ try {
1659
+ const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
1660
+ const countQuery = createSqlBuilder().count().from(fullTableName).where("runId = ?", runId);
1661
+ const countResult = await this.operations.executeQuery(countQuery.build());
1662
+ const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
1663
+ if (total === 0) {
1664
+ return {
1665
+ pagination: {
1666
+ total: 0,
1667
+ page: pagination.page,
1668
+ perPage: pagination.perPage,
1669
+ hasMore: false
1670
+ },
1671
+ scores: []
1672
+ };
1280
1673
  }
1281
- query.orderBy("startTime", "DESC").limit(perPage).offset(page * perPage);
1282
- const { sql, params } = query.build();
1283
- const results = await this.executeQuery({ sql, params });
1284
- return isArrayOfRecords(results) ? results.map(
1285
- (trace) => ({
1286
- ...trace,
1287
- attributes: this.deserializeValue(trace.attributes, "jsonb"),
1288
- status: this.deserializeValue(trace.status, "jsonb"),
1289
- events: this.deserializeValue(trace.events, "jsonb"),
1290
- links: this.deserializeValue(trace.links, "jsonb"),
1291
- other: this.deserializeValue(trace.other, "jsonb")
1292
- })
1293
- ) : [];
1674
+ const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("runId = ?", runId).limit(pagination.perPage).offset(pagination.page * pagination.perPage);
1675
+ const { sql, params } = selectQuery.build();
1676
+ const results = await this.operations.executeQuery({ sql, params });
1677
+ const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
1678
+ return {
1679
+ pagination: {
1680
+ total,
1681
+ page: pagination.page,
1682
+ perPage: pagination.perPage,
1683
+ hasMore: total > (pagination.page + 1) * pagination.perPage
1684
+ },
1685
+ scores
1686
+ };
1294
1687
  } catch (error$1) {
1295
- const mastraError = new error.MastraError(
1688
+ throw new error.MastraError(
1689
+ {
1690
+ id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_RUN_ID_FAILED",
1691
+ domain: error.ErrorDomain.STORAGE,
1692
+ category: error.ErrorCategory.THIRD_PARTY
1693
+ },
1694
+ error$1
1695
+ );
1696
+ }
1697
+ }
1698
+ async getScoresByEntityId({
1699
+ entityId,
1700
+ entityType,
1701
+ pagination
1702
+ }) {
1703
+ try {
1704
+ const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
1705
+ const countQuery = createSqlBuilder().count().from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType);
1706
+ const countResult = await this.operations.executeQuery(countQuery.build());
1707
+ const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
1708
+ if (total === 0) {
1709
+ return {
1710
+ pagination: {
1711
+ total: 0,
1712
+ page: pagination.page,
1713
+ perPage: pagination.perPage,
1714
+ hasMore: false
1715
+ },
1716
+ scores: []
1717
+ };
1718
+ }
1719
+ const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType).limit(pagination.perPage).offset(pagination.page * pagination.perPage);
1720
+ const { sql, params } = selectQuery.build();
1721
+ const results = await this.operations.executeQuery({ sql, params });
1722
+ const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
1723
+ return {
1724
+ pagination: {
1725
+ total,
1726
+ page: pagination.page,
1727
+ perPage: pagination.perPage,
1728
+ hasMore: total > (pagination.page + 1) * pagination.perPage
1729
+ },
1730
+ scores
1731
+ };
1732
+ } catch (error$1) {
1733
+ throw new error.MastraError(
1734
+ {
1735
+ id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_ENTITY_ID_FAILED",
1736
+ domain: error.ErrorDomain.STORAGE,
1737
+ category: error.ErrorCategory.THIRD_PARTY
1738
+ },
1739
+ error$1
1740
+ );
1741
+ }
1742
+ }
1743
+ };
1744
+ function isArrayOfRecords2(value) {
1745
+ return value && Array.isArray(value) && value.length > 0;
1746
+ }
1747
+ var TracesStorageD1 = class extends storage.TracesStorage {
1748
+ operations;
1749
+ constructor({ operations }) {
1750
+ super();
1751
+ this.operations = operations;
1752
+ }
1753
+ async getTraces(args) {
1754
+ const paginatedArgs = {
1755
+ name: args.name,
1756
+ scope: args.scope,
1757
+ page: args.page,
1758
+ perPage: args.perPage,
1759
+ attributes: args.attributes,
1760
+ filters: args.filters,
1761
+ dateRange: args.fromDate || args.toDate ? {
1762
+ start: args.fromDate,
1763
+ end: args.toDate
1764
+ } : void 0
1765
+ };
1766
+ try {
1767
+ const result = await this.getTracesPaginated(paginatedArgs);
1768
+ return result.traces;
1769
+ } catch (error$1) {
1770
+ throw new error.MastraError(
1296
1771
  {
1297
1772
  id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_ERROR",
1298
1773
  domain: error.ErrorDomain.STORAGE,
1299
1774
  category: error.ErrorCategory.THIRD_PARTY,
1300
1775
  text: `Failed to retrieve traces: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
1301
1776
  details: {
1302
- name: name ?? "",
1303
- scope: scope ?? ""
1777
+ name: args.name ?? "",
1778
+ scope: args.scope ?? ""
1304
1779
  }
1305
1780
  },
1306
1781
  error$1
1307
1782
  );
1308
- this.logger?.error(mastraError.toString());
1309
- this.logger?.trackException(mastraError);
1310
- return [];
1311
1783
  }
1312
1784
  }
1313
1785
  async getTracesPaginated(args) {
1314
- const { name, scope, page, perPage, attributes, fromDate, toDate } = args;
1315
- const fullTableName = this.getTableName(storage.TABLE_TRACES);
1786
+ const { name, scope, page = 0, perPage = 100, attributes, dateRange } = args;
1787
+ const fromDate = dateRange?.start;
1788
+ const toDate = dateRange?.end;
1789
+ const fullTableName = this.operations.getTableName(storage.TABLE_TRACES);
1316
1790
  try {
1317
1791
  const dataQuery = createSqlBuilder().select("*").from(fullTableName).where("1=1");
1318
1792
  const countQuery = createSqlBuilder().count().from(fullTableName).where("1=1");
@@ -1340,18 +1814,22 @@ var D1Store = class extends storage.MastraStorage {
1340
1814
  dataQuery.andWhere("createdAt <= ?", toDateStr);
1341
1815
  countQuery.andWhere("createdAt <= ?", toDateStr);
1342
1816
  }
1343
- const countResult = await this.executeQuery(countQuery.build());
1817
+ const allDataResult = await this.operations.executeQuery(
1818
+ createSqlBuilder().select("*").from(fullTableName).where("1=1").build()
1819
+ );
1820
+ console.log("allDataResult", allDataResult);
1821
+ const countResult = await this.operations.executeQuery(countQuery.build());
1344
1822
  const total = Number(countResult?.[0]?.count ?? 0);
1345
1823
  dataQuery.orderBy("startTime", "DESC").limit(perPage).offset(page * perPage);
1346
- const results = await this.executeQuery(dataQuery.build());
1347
- const traces = isArrayOfRecords(results) ? results.map(
1824
+ const results = await this.operations.executeQuery(dataQuery.build());
1825
+ const traces = isArrayOfRecords2(results) ? results.map(
1348
1826
  (trace) => ({
1349
1827
  ...trace,
1350
- attributes: this.deserializeValue(trace.attributes, "jsonb"),
1351
- status: this.deserializeValue(trace.status, "jsonb"),
1352
- events: this.deserializeValue(trace.events, "jsonb"),
1353
- links: this.deserializeValue(trace.links, "jsonb"),
1354
- other: this.deserializeValue(trace.other, "jsonb")
1828
+ attributes: deserializeValue(trace.attributes, "jsonb"),
1829
+ status: deserializeValue(trace.status, "jsonb"),
1830
+ events: deserializeValue(trace.events, "jsonb"),
1831
+ links: deserializeValue(trace.links, "jsonb"),
1832
+ other: deserializeValue(trace.other, "jsonb")
1355
1833
  })
1356
1834
  ) : [];
1357
1835
  return {
@@ -1364,7 +1842,7 @@ var D1Store = class extends storage.MastraStorage {
1364
1842
  } catch (error$1) {
1365
1843
  const mastraError = new error.MastraError(
1366
1844
  {
1367
- id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_ERROR",
1845
+ id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_PAGINATED_ERROR",
1368
1846
  domain: error.ErrorDomain.STORAGE,
1369
1847
  category: error.ErrorCategory.THIRD_PARTY,
1370
1848
  text: `Failed to retrieve traces: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
@@ -1377,142 +1855,87 @@ var D1Store = class extends storage.MastraStorage {
1377
1855
  return { traces: [], total: 0, page, perPage, hasMore: false };
1378
1856
  }
1379
1857
  }
1380
- /**
1381
- * @deprecated use getEvals instead
1382
- */
1383
- async getEvalsByAgentName(agentName, type) {
1384
- const fullTableName = this.getTableName(storage.TABLE_EVALS);
1858
+ async batchTraceInsert({ records }) {
1859
+ this.logger.debug("Batch inserting traces", { count: records.length });
1860
+ await this.operations.batchInsert({
1861
+ tableName: storage.TABLE_TRACES,
1862
+ records
1863
+ });
1864
+ }
1865
+ };
1866
+ var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
1867
+ operations;
1868
+ constructor({ operations }) {
1869
+ super();
1870
+ this.operations = operations;
1871
+ }
1872
+ async persistWorkflowSnapshot({
1873
+ workflowName,
1874
+ runId,
1875
+ snapshot
1876
+ }) {
1877
+ const fullTableName = this.operations.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
1878
+ const now = (/* @__PURE__ */ new Date()).toISOString();
1879
+ const currentSnapshot = await this.operations.load({
1880
+ tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
1881
+ keys: { workflow_name: workflowName, run_id: runId }
1882
+ });
1883
+ const persisting = currentSnapshot ? {
1884
+ ...currentSnapshot,
1885
+ snapshot: JSON.stringify(snapshot),
1886
+ updatedAt: now
1887
+ } : {
1888
+ workflow_name: workflowName,
1889
+ run_id: runId,
1890
+ snapshot,
1891
+ createdAt: now,
1892
+ updatedAt: now
1893
+ };
1894
+ const processedRecord = await this.operations.processRecord(persisting);
1895
+ const columns = Object.keys(processedRecord);
1896
+ const values = Object.values(processedRecord);
1897
+ const updateMap = {
1898
+ snapshot: "excluded.snapshot",
1899
+ updatedAt: "excluded.updatedAt"
1900
+ };
1901
+ this.logger.debug("Persisting workflow snapshot", { workflowName, runId });
1902
+ const query = createSqlBuilder().insert(fullTableName, columns, values, ["workflow_name", "run_id"], updateMap);
1903
+ const { sql, params } = query.build();
1385
1904
  try {
1386
- let query = createSqlBuilder().select("*").from(fullTableName).where("agent_name = ?", agentName);
1387
- if (type === "test") {
1388
- query = query.andWhere("test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL");
1389
- } else if (type === "live") {
1390
- query = query.andWhere("(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)");
1391
- }
1392
- query.orderBy("created_at", "DESC");
1393
- const { sql, params } = query.build();
1394
- const results = await this.executeQuery({ sql, params });
1395
- return isArrayOfRecords(results) ? results.map((row) => {
1396
- const result = this.deserializeValue(row.result);
1397
- const testInfo = row.test_info ? this.deserializeValue(row.test_info) : void 0;
1398
- return {
1399
- input: row.input || "",
1400
- output: row.output || "",
1401
- result,
1402
- agentName: row.agent_name || "",
1403
- metricName: row.metric_name || "",
1404
- instructions: row.instructions || "",
1405
- runId: row.run_id || "",
1406
- globalRunId: row.global_run_id || "",
1407
- createdAt: row.created_at || "",
1408
- testInfo
1409
- };
1410
- }) : [];
1905
+ await this.operations.executeQuery({ sql, params });
1411
1906
  } catch (error$1) {
1412
- const mastraError = new error.MastraError(
1907
+ throw new error.MastraError(
1413
1908
  {
1414
- id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
1909
+ id: "CLOUDFLARE_D1_STORAGE_PERSIST_WORKFLOW_SNAPSHOT_ERROR",
1415
1910
  domain: error.ErrorDomain.STORAGE,
1416
1911
  category: error.ErrorCategory.THIRD_PARTY,
1417
- text: `Failed to retrieve evals for agent ${agentName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
1418
- details: { agentName }
1912
+ text: `Failed to persist workflow snapshot: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
1913
+ details: { workflowName, runId }
1419
1914
  },
1420
1915
  error$1
1421
1916
  );
1422
- this.logger?.error(mastraError.toString());
1423
- this.logger?.trackException(mastraError);
1424
- return [];
1425
- }
1426
- }
1427
- async getEvals(options) {
1428
- const { agentName, type, page = 0, perPage = 40, fromDate, toDate } = options || {};
1429
- const fullTableName = this.getTableName(storage.TABLE_EVALS);
1430
- const conditions = [];
1431
- const queryParams = [];
1432
- if (agentName) {
1433
- conditions.push(`agent_name = ?`);
1434
- queryParams.push(agentName);
1435
1917
  }
1436
- if (type === "test") {
1437
- conditions.push(`(test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL)`);
1438
- } else if (type === "live") {
1439
- conditions.push(`(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)`);
1440
- }
1441
- if (fromDate) {
1442
- conditions.push(`createdAt >= ?`);
1443
- queryParams.push(this.serializeDate(fromDate));
1444
- }
1445
- if (toDate) {
1446
- conditions.push(`createdAt <= ?`);
1447
- queryParams.push(this.serializeDate(toDate));
1448
- }
1449
- const countQueryBuilder = createSqlBuilder().count().from(fullTableName);
1450
- if (conditions.length > 0) {
1451
- countQueryBuilder.where(conditions.join(" AND "), ...queryParams);
1452
- }
1453
- const { sql: countSql, params: countParams } = countQueryBuilder.build();
1918
+ }
1919
+ async loadWorkflowSnapshot(params) {
1920
+ const { workflowName, runId } = params;
1921
+ this.logger.debug("Loading workflow snapshot", { workflowName, runId });
1454
1922
  try {
1455
- const countResult = await this.executeQuery({
1456
- sql: countSql,
1457
- params: countParams,
1458
- first: true
1459
- });
1460
- const total = Number(countResult?.count || 0);
1461
- const currentOffset = page * perPage;
1462
- if (total === 0) {
1463
- return {
1464
- evals: [],
1465
- total: 0,
1466
- page,
1467
- perPage,
1468
- hasMore: false
1469
- };
1470
- }
1471
- const dataQueryBuilder = createSqlBuilder().select("*").from(fullTableName);
1472
- if (conditions.length > 0) {
1473
- dataQueryBuilder.where(conditions.join(" AND "), ...queryParams);
1474
- }
1475
- dataQueryBuilder.orderBy("createdAt", "DESC").limit(perPage).offset(currentOffset);
1476
- const { sql: dataSql, params: dataParams } = dataQueryBuilder.build();
1477
- const rows = await this.executeQuery({
1478
- sql: dataSql,
1479
- params: dataParams
1480
- });
1481
- const evals = (isArrayOfRecords(rows) ? rows : []).map((row) => {
1482
- const result = this.deserializeValue(row.result);
1483
- const testInfo = row.test_info ? this.deserializeValue(row.test_info) : void 0;
1484
- if (!result || typeof result !== "object" || !("score" in result)) {
1485
- throw new Error(`Invalid MetricResult format: ${JSON.stringify(result)}`);
1923
+ const d = await this.operations.load({
1924
+ tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
1925
+ keys: {
1926
+ workflow_name: workflowName,
1927
+ run_id: runId
1486
1928
  }
1487
- return {
1488
- input: row.input,
1489
- output: row.output,
1490
- result,
1491
- agentName: row.agent_name,
1492
- metricName: row.metric_name,
1493
- instructions: row.instructions,
1494
- testInfo,
1495
- globalRunId: row.global_run_id,
1496
- runId: row.run_id,
1497
- createdAt: row.createdAt
1498
- };
1499
1929
  });
1500
- const hasMore = currentOffset + evals.length < total;
1501
- return {
1502
- evals,
1503
- total,
1504
- page,
1505
- perPage,
1506
- hasMore
1507
- };
1930
+ return d ? d.snapshot : null;
1508
1931
  } catch (error$1) {
1509
1932
  throw new error.MastraError(
1510
1933
  {
1511
- id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
1934
+ id: "CLOUDFLARE_D1_STORAGE_LOAD_WORKFLOW_SNAPSHOT_ERROR",
1512
1935
  domain: error.ErrorDomain.STORAGE,
1513
1936
  category: error.ErrorCategory.THIRD_PARTY,
1514
- text: `Failed to retrieve evals for agent ${agentName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
1515
- details: { agentName: agentName ?? "", type: type ?? "" }
1937
+ text: `Failed to load workflow snapshot: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
1938
+ details: { workflowName, runId }
1516
1939
  },
1517
1940
  error$1
1518
1941
  );
@@ -1531,17 +1954,11 @@ var D1Store = class extends storage.MastraStorage {
1531
1954
  workflowName: row.workflow_name,
1532
1955
  runId: row.run_id,
1533
1956
  snapshot: parsedSnapshot,
1534
- createdAt: this.ensureDate(row.createdAt),
1535
- updatedAt: this.ensureDate(row.updatedAt),
1957
+ createdAt: storage.ensureDate(row.createdAt),
1958
+ updatedAt: storage.ensureDate(row.updatedAt),
1536
1959
  resourceId: row.resourceId
1537
1960
  };
1538
1961
  }
1539
- async hasColumn(table, column) {
1540
- const sql = `PRAGMA table_info(${table});`;
1541
- const result = await this.executeQuery({ sql, params: [] });
1542
- if (!result || !Array.isArray(result)) return false;
1543
- return result.some((col) => col.name === column || col.name === column.toLowerCase());
1544
- }
1545
1962
  async getWorkflowRuns({
1546
1963
  workflowName,
1547
1964
  fromDate,
@@ -1550,13 +1967,13 @@ var D1Store = class extends storage.MastraStorage {
1550
1967
  offset,
1551
1968
  resourceId
1552
1969
  } = {}) {
1553
- const fullTableName = this.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
1970
+ const fullTableName = this.operations.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
1554
1971
  try {
1555
1972
  const builder = createSqlBuilder().select().from(fullTableName);
1556
1973
  const countBuilder = createSqlBuilder().count().from(fullTableName);
1557
1974
  if (workflowName) builder.whereAnd("workflow_name = ?", workflowName);
1558
1975
  if (resourceId) {
1559
- const hasResourceId = await this.hasColumn(fullTableName, "resourceId");
1976
+ const hasResourceId = await this.operations.hasColumn(fullTableName, "resourceId");
1560
1977
  if (hasResourceId) {
1561
1978
  builder.whereAnd("resourceId = ?", resourceId);
1562
1979
  countBuilder.whereAnd("resourceId = ?", resourceId);
@@ -1579,14 +1996,14 @@ var D1Store = class extends storage.MastraStorage {
1579
1996
  let total = 0;
1580
1997
  if (limit !== void 0 && offset !== void 0) {
1581
1998
  const { sql: countSql, params: countParams } = countBuilder.build();
1582
- const countResult = await this.executeQuery({
1999
+ const countResult = await this.operations.executeQuery({
1583
2000
  sql: countSql,
1584
2001
  params: countParams,
1585
2002
  first: true
1586
2003
  });
1587
2004
  total = Number(countResult?.count ?? 0);
1588
2005
  }
1589
- const results = await this.executeQuery({ sql, params });
2006
+ const results = await this.operations.executeQuery({ sql, params });
1590
2007
  const runs = (isArrayOfRecords(results) ? results : []).map((row) => this.parseWorkflowRun(row));
1591
2008
  return { runs, total: total || runs.length };
1592
2009
  } catch (error$1) {
@@ -1609,7 +2026,7 @@ var D1Store = class extends storage.MastraStorage {
1609
2026
  runId,
1610
2027
  workflowName
1611
2028
  }) {
1612
- const fullTableName = this.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
2029
+ const fullTableName = this.operations.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
1613
2030
  try {
1614
2031
  const conditions = [];
1615
2032
  const params = [];
@@ -1623,7 +2040,7 @@ var D1Store = class extends storage.MastraStorage {
1623
2040
  }
1624
2041
  const whereClause = conditions.length > 0 ? "WHERE " + conditions.join(" AND ") : "";
1625
2042
  const sql = `SELECT * FROM ${fullTableName} ${whereClause} ORDER BY createdAt DESC LIMIT 1`;
1626
- const result = await this.executeQuery({ sql, params, first: true });
2043
+ const result = await this.operations.executeQuery({ sql, params, first: true });
1627
2044
  if (!result) return null;
1628
2045
  return this.parseWorkflowRun(result);
1629
2046
  } catch (error$1) {
@@ -1639,6 +2056,277 @@ var D1Store = class extends storage.MastraStorage {
1639
2056
  );
1640
2057
  }
1641
2058
  }
2059
+ };
2060
+
2061
+ // src/storage/index.ts
2062
+ var D1Store = class extends storage.MastraStorage {
2063
+ client;
2064
+ binding;
2065
+ // D1Database binding
2066
+ tablePrefix;
2067
+ stores;
2068
+ /**
2069
+ * Creates a new D1Store instance
2070
+ * @param config Configuration for D1 access (either REST API or Workers Binding API)
2071
+ */
2072
+ constructor(config) {
2073
+ try {
2074
+ super({ name: "D1" });
2075
+ if (config.tablePrefix && !/^[a-zA-Z0-9_]*$/.test(config.tablePrefix)) {
2076
+ throw new Error("Invalid tablePrefix: only letters, numbers, and underscores are allowed.");
2077
+ }
2078
+ this.tablePrefix = config.tablePrefix || "";
2079
+ if ("binding" in config) {
2080
+ if (!config.binding) {
2081
+ throw new Error("D1 binding is required when using Workers Binding API");
2082
+ }
2083
+ this.binding = config.binding;
2084
+ this.logger.info("Using D1 Workers Binding API");
2085
+ } else if ("client" in config) {
2086
+ if (!config.client) {
2087
+ throw new Error("D1 client is required when using D1ClientConfig");
2088
+ }
2089
+ this.client = config.client;
2090
+ this.logger.info("Using D1 Client");
2091
+ } else {
2092
+ if (!config.accountId || !config.databaseId || !config.apiToken) {
2093
+ throw new Error("accountId, databaseId, and apiToken are required when using REST API");
2094
+ }
2095
+ const cfClient = new Cloudflare__default.default({
2096
+ apiToken: config.apiToken
2097
+ });
2098
+ this.client = {
2099
+ query: ({ sql, params }) => {
2100
+ return cfClient.d1.database.query(config.databaseId, {
2101
+ account_id: config.accountId,
2102
+ sql,
2103
+ params
2104
+ });
2105
+ }
2106
+ };
2107
+ this.logger.info("Using D1 REST API");
2108
+ }
2109
+ } catch (error$1) {
2110
+ throw new error.MastraError(
2111
+ {
2112
+ id: "CLOUDFLARE_D1_STORAGE_INITIALIZATION_ERROR",
2113
+ domain: error.ErrorDomain.STORAGE,
2114
+ category: error.ErrorCategory.SYSTEM,
2115
+ text: "Error initializing D1Store"
2116
+ },
2117
+ error$1
2118
+ );
2119
+ }
2120
+ const operations = new StoreOperationsD1({
2121
+ client: this.client,
2122
+ binding: this.binding,
2123
+ tablePrefix: this.tablePrefix
2124
+ });
2125
+ const scores = new ScoresStorageD1({
2126
+ operations
2127
+ });
2128
+ const legacyEvals = new LegacyEvalsStorageD1({
2129
+ operations
2130
+ });
2131
+ const traces = new TracesStorageD1({
2132
+ operations
2133
+ });
2134
+ const workflows = new WorkflowsStorageD1({
2135
+ operations
2136
+ });
2137
+ const memory = new MemoryStorageD1({
2138
+ operations
2139
+ });
2140
+ this.stores = {
2141
+ operations,
2142
+ scores,
2143
+ legacyEvals,
2144
+ traces,
2145
+ workflows,
2146
+ memory
2147
+ };
2148
+ }
2149
+ get supports() {
2150
+ return {
2151
+ selectByIncludeResourceScope: true,
2152
+ resourceWorkingMemory: true,
2153
+ hasColumn: true,
2154
+ createTable: true
2155
+ };
2156
+ }
2157
+ async createTable({
2158
+ tableName,
2159
+ schema
2160
+ }) {
2161
+ return this.stores.operations.createTable({ tableName, schema });
2162
+ }
2163
+ /**
2164
+ * Alters table schema to add columns if they don't exist
2165
+ * @param tableName Name of the table
2166
+ * @param schema Schema of the table
2167
+ * @param ifNotExists Array of column names to add if they don't exist
2168
+ */
2169
+ async alterTable({
2170
+ tableName,
2171
+ schema,
2172
+ ifNotExists
2173
+ }) {
2174
+ return this.stores.operations.alterTable({ tableName, schema, ifNotExists });
2175
+ }
2176
+ async clearTable({ tableName }) {
2177
+ return this.stores.operations.clearTable({ tableName });
2178
+ }
2179
+ async dropTable({ tableName }) {
2180
+ return this.stores.operations.dropTable({ tableName });
2181
+ }
2182
+ async hasColumn(table, column) {
2183
+ return this.stores.operations.hasColumn(table, column);
2184
+ }
2185
+ async insert({ tableName, record }) {
2186
+ return this.stores.operations.insert({ tableName, record });
2187
+ }
2188
+ async load({ tableName, keys }) {
2189
+ return this.stores.operations.load({ tableName, keys });
2190
+ }
2191
+ async getThreadById({ threadId }) {
2192
+ return this.stores.memory.getThreadById({ threadId });
2193
+ }
2194
+ /**
2195
+ * @deprecated use getThreadsByResourceIdPaginated instead
2196
+ */
2197
+ async getThreadsByResourceId({ resourceId }) {
2198
+ return this.stores.memory.getThreadsByResourceId({ resourceId });
2199
+ }
2200
+ async getThreadsByResourceIdPaginated(args) {
2201
+ return this.stores.memory.getThreadsByResourceIdPaginated(args);
2202
+ }
2203
+ async saveThread({ thread }) {
2204
+ return this.stores.memory.saveThread({ thread });
2205
+ }
2206
+ async updateThread({
2207
+ id,
2208
+ title,
2209
+ metadata
2210
+ }) {
2211
+ return this.stores.memory.updateThread({ id, title, metadata });
2212
+ }
2213
+ async deleteThread({ threadId }) {
2214
+ return this.stores.memory.deleteThread({ threadId });
2215
+ }
2216
+ async saveMessages(args) {
2217
+ return this.stores.memory.saveMessages(args);
2218
+ }
2219
+ async getMessages({
2220
+ threadId,
2221
+ selectBy,
2222
+ format
2223
+ }) {
2224
+ return this.stores.memory.getMessages({ threadId, selectBy, format });
2225
+ }
2226
+ async getMessagesPaginated({
2227
+ threadId,
2228
+ selectBy,
2229
+ format
2230
+ }) {
2231
+ return this.stores.memory.getMessagesPaginated({ threadId, selectBy, format });
2232
+ }
2233
+ async persistWorkflowSnapshot({
2234
+ workflowName,
2235
+ runId,
2236
+ snapshot
2237
+ }) {
2238
+ return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, snapshot });
2239
+ }
2240
+ async loadWorkflowSnapshot(params) {
2241
+ return this.stores.workflows.loadWorkflowSnapshot(params);
2242
+ }
2243
+ async getWorkflowRuns({
2244
+ workflowName,
2245
+ fromDate,
2246
+ toDate,
2247
+ limit,
2248
+ offset,
2249
+ resourceId
2250
+ } = {}) {
2251
+ return this.stores.workflows.getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId });
2252
+ }
2253
+ async getWorkflowRunById({
2254
+ runId,
2255
+ workflowName
2256
+ }) {
2257
+ return this.stores.workflows.getWorkflowRunById({ runId, workflowName });
2258
+ }
2259
+ /**
2260
+ * Insert multiple records in a batch operation
2261
+ * @param tableName The table to insert into
2262
+ * @param records The records to insert
2263
+ */
2264
+ async batchInsert({ tableName, records }) {
2265
+ return this.stores.operations.batchInsert({ tableName, records });
2266
+ }
2267
+ /**
2268
+ * @deprecated use getTracesPaginated instead
2269
+ */
2270
+ async getTraces(args) {
2271
+ return this.stores.traces.getTraces(args);
2272
+ }
2273
+ async getTracesPaginated(args) {
2274
+ return this.stores.traces.getTracesPaginated(args);
2275
+ }
2276
+ /**
2277
+ * @deprecated use getEvals instead
2278
+ */
2279
+ async getEvalsByAgentName(agentName, type) {
2280
+ return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
2281
+ }
2282
+ async getEvals(options) {
2283
+ return this.stores.legacyEvals.getEvals(options);
2284
+ }
2285
+ async updateMessages(_args) {
2286
+ return this.stores.memory.updateMessages(_args);
2287
+ }
2288
+ async getResourceById({ resourceId }) {
2289
+ return this.stores.memory.getResourceById({ resourceId });
2290
+ }
2291
+ async saveResource({ resource }) {
2292
+ return this.stores.memory.saveResource({ resource });
2293
+ }
2294
+ async updateResource({
2295
+ resourceId,
2296
+ workingMemory,
2297
+ metadata
2298
+ }) {
2299
+ return this.stores.memory.updateResource({ resourceId, workingMemory, metadata });
2300
+ }
2301
+ async getScoreById({ id: _id }) {
2302
+ return this.stores.scores.getScoreById({ id: _id });
2303
+ }
2304
+ async saveScore(_score) {
2305
+ return this.stores.scores.saveScore(_score);
2306
+ }
2307
+ async getScoresByRunId({
2308
+ runId: _runId,
2309
+ pagination: _pagination
2310
+ }) {
2311
+ return this.stores.scores.getScoresByRunId({ runId: _runId, pagination: _pagination });
2312
+ }
2313
+ async getScoresByEntityId({
2314
+ entityId: _entityId,
2315
+ entityType: _entityType,
2316
+ pagination: _pagination
2317
+ }) {
2318
+ return this.stores.scores.getScoresByEntityId({
2319
+ entityId: _entityId,
2320
+ entityType: _entityType,
2321
+ pagination: _pagination
2322
+ });
2323
+ }
2324
+ async getScoresByScorerId({
2325
+ scorerId: _scorerId,
2326
+ pagination: _pagination
2327
+ }) {
2328
+ return this.stores.scores.getScoresByScorerId({ scorerId: _scorerId, pagination: _pagination });
2329
+ }
1642
2330
  /**
1643
2331
  * Close the database connection
1644
2332
  * No explicit cleanup needed for D1 in either REST or Workers Binding mode
@@ -1646,10 +2334,6 @@ var D1Store = class extends storage.MastraStorage {
1646
2334
  async close() {
1647
2335
  this.logger.debug("Closing D1 connection");
1648
2336
  }
1649
- async updateMessages(_args) {
1650
- this.logger.error("updateMessages is not yet implemented in CloudflareD1Store");
1651
- throw new Error("Method not implemented");
1652
- }
1653
2337
  };
1654
2338
 
1655
2339
  exports.D1Store = D1Store;