@mastra/cloudflare-d1 0.13.8 → 1.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
2
- import { MastraStorage, StoreOperations, ScoresStorage, TABLE_SCORERS, LegacyEvalsStorage, TABLE_EVALS, serializeDate, TracesStorage, TABLE_TRACES, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ensureDate, MemoryStorage, TABLE_RESOURCES, TABLE_THREADS, TABLE_MESSAGES, resolveMessageLimit, safelyParseJSON } from '@mastra/core/storage';
2
+ import { MastraStorage, StoreOperations, ScoresStorage, TABLE_SCORERS, normalizePerPage, calculatePagination, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ensureDate, MemoryStorage, TABLE_RESOURCES, TABLE_THREADS, TABLE_MESSAGES, serializeDate, safelyParseJSON } from '@mastra/core/storage';
3
3
  import Cloudflare from 'cloudflare';
4
- import { parseSqlIdentifier } from '@mastra/core/utils';
5
4
  import { MessageList } from '@mastra/core/agent';
6
- import { saveScorePayloadSchema } from '@mastra/core/scores';
5
+ import { parseSqlIdentifier } from '@mastra/core/utils';
6
+ import { saveScorePayloadSchema } from '@mastra/core/evals';
7
7
 
8
8
  // src/storage/index.ts
9
9
  var SqlBuilder = class {
@@ -242,16 +242,6 @@ function isArrayOfRecords(value) {
242
242
  }
243
243
  function deserializeValue(value, type) {
244
244
  if (value === null || value === void 0) return null;
245
- if (type === "date" && typeof value === "string") {
246
- return new Date(value);
247
- }
248
- if (type === "jsonb" && typeof value === "string") {
249
- try {
250
- return JSON.parse(value);
251
- } catch {
252
- return value;
253
- }
254
- }
255
245
  if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
256
246
  try {
257
247
  return JSON.parse(value);
@@ -262,155 +252,7 @@ function deserializeValue(value, type) {
262
252
  return value;
263
253
  }
264
254
 
265
- // src/storage/domains/legacy-evals/index.ts
266
- var LegacyEvalsStorageD1 = class extends LegacyEvalsStorage {
267
- operations;
268
- constructor({ operations }) {
269
- super();
270
- this.operations = operations;
271
- }
272
- async getEvals(options) {
273
- const { agentName, type, page = 0, perPage = 40, dateRange } = options || {};
274
- const fullTableName = this.operations.getTableName(TABLE_EVALS);
275
- const conditions = [];
276
- const queryParams = [];
277
- if (agentName) {
278
- conditions.push(`agent_name = ?`);
279
- queryParams.push(agentName);
280
- }
281
- if (type === "test") {
282
- conditions.push(`(test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL)`);
283
- } else if (type === "live") {
284
- conditions.push(`(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)`);
285
- }
286
- if (dateRange?.start) {
287
- conditions.push(`created_at >= ?`);
288
- queryParams.push(serializeDate(dateRange.start));
289
- }
290
- if (dateRange?.end) {
291
- conditions.push(`created_at <= ?`);
292
- queryParams.push(serializeDate(dateRange.end));
293
- }
294
- const countQueryBuilder = createSqlBuilder().count().from(fullTableName);
295
- if (conditions.length > 0) {
296
- countQueryBuilder.where(conditions.join(" AND "), ...queryParams);
297
- }
298
- const { sql: countSql, params: countParams } = countQueryBuilder.build();
299
- try {
300
- const countResult = await this.operations.executeQuery({
301
- sql: countSql,
302
- params: countParams,
303
- first: true
304
- });
305
- const total = Number(countResult?.count || 0);
306
- const currentOffset = page * perPage;
307
- if (total === 0) {
308
- return {
309
- evals: [],
310
- total: 0,
311
- page,
312
- perPage,
313
- hasMore: false
314
- };
315
- }
316
- const dataQueryBuilder = createSqlBuilder().select("*").from(fullTableName);
317
- if (conditions.length > 0) {
318
- dataQueryBuilder.where(conditions.join(" AND "), ...queryParams);
319
- }
320
- dataQueryBuilder.orderBy("created_at", "DESC").limit(perPage).offset(currentOffset);
321
- const { sql: dataSql, params: dataParams } = dataQueryBuilder.build();
322
- const rows = await this.operations.executeQuery({
323
- sql: dataSql,
324
- params: dataParams
325
- });
326
- const evals = (isArrayOfRecords(rows) ? rows : []).map((row) => {
327
- const result = deserializeValue(row.result);
328
- const testInfo = row.test_info ? deserializeValue(row.test_info) : void 0;
329
- if (!result || typeof result !== "object" || !("score" in result)) {
330
- throw new Error(`Invalid MetricResult format: ${JSON.stringify(result)}`);
331
- }
332
- return {
333
- input: row.input,
334
- output: row.output,
335
- result,
336
- agentName: row.agent_name,
337
- metricName: row.metric_name,
338
- instructions: row.instructions,
339
- testInfo,
340
- globalRunId: row.global_run_id,
341
- runId: row.run_id,
342
- createdAt: row.created_at
343
- };
344
- });
345
- const hasMore = currentOffset + evals.length < total;
346
- return {
347
- evals,
348
- total,
349
- page,
350
- perPage,
351
- hasMore
352
- };
353
- } catch (error) {
354
- throw new MastraError(
355
- {
356
- id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
357
- domain: ErrorDomain.STORAGE,
358
- category: ErrorCategory.THIRD_PARTY,
359
- text: `Failed to retrieve evals for agent ${agentName}: ${error instanceof Error ? error.message : String(error)}`,
360
- details: { agentName: agentName ?? "", type: type ?? "" }
361
- },
362
- error
363
- );
364
- }
365
- }
366
- /**
367
- * @deprecated use getEvals instead
368
- */
369
- async getEvalsByAgentName(agentName, type) {
370
- const fullTableName = this.operations.getTableName(TABLE_EVALS);
371
- try {
372
- let query = createSqlBuilder().select("*").from(fullTableName).where("agent_name = ?", agentName);
373
- if (type === "test") {
374
- query = query.andWhere("test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL");
375
- } else if (type === "live") {
376
- query = query.andWhere("(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)");
377
- }
378
- query.orderBy("created_at", "DESC");
379
- const { sql, params } = query.build();
380
- const results = await this.operations.executeQuery({ sql, params });
381
- return isArrayOfRecords(results) ? results.map((row) => {
382
- const result = deserializeValue(row.result);
383
- const testInfo = row.test_info ? deserializeValue(row.test_info) : void 0;
384
- return {
385
- input: row.input || "",
386
- output: row.output || "",
387
- result,
388
- agentName: row.agent_name || "",
389
- metricName: row.metric_name || "",
390
- instructions: row.instructions || "",
391
- runId: row.run_id || "",
392
- globalRunId: row.global_run_id || "",
393
- createdAt: row.created_at || "",
394
- testInfo
395
- };
396
- }) : [];
397
- } catch (error) {
398
- const mastraError = new MastraError(
399
- {
400
- id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
401
- domain: ErrorDomain.STORAGE,
402
- category: ErrorCategory.THIRD_PARTY,
403
- text: `Failed to retrieve evals for agent ${agentName}: ${error instanceof Error ? error.message : String(error)}`,
404
- details: { agentName }
405
- },
406
- error
407
- );
408
- this.logger?.error(mastraError.toString());
409
- this.logger?.trackException(mastraError);
410
- return [];
411
- }
412
- }
413
- };
255
+ // src/storage/domains/memory/index.ts
414
256
  var MemoryStorageD1 = class extends MemoryStorage {
415
257
  operations;
416
258
  constructor({ operations }) {
@@ -558,39 +400,22 @@ var MemoryStorageD1 = class extends MemoryStorage {
558
400
  return null;
559
401
  }
560
402
  }
561
- /**
562
- * @deprecated use getThreadsByResourceIdPaginated instead
563
- */
564
- async getThreadsByResourceId({ resourceId }) {
565
- const fullTableName = this.operations.getTableName(TABLE_THREADS);
566
- try {
567
- const query = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId);
568
- const { sql, params } = query.build();
569
- const results = await this.operations.executeQuery({ sql, params });
570
- return (isArrayOfRecords(results) ? results : []).map((thread) => ({
571
- ...thread,
572
- createdAt: ensureDate(thread.createdAt),
573
- updatedAt: ensureDate(thread.updatedAt),
574
- metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
575
- }));
576
- } catch (error) {
577
- const mastraError = new MastraError(
403
+ async listThreadsByResourceId(args) {
404
+ const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
405
+ const perPage = normalizePerPage(perPageInput, 100);
406
+ if (page < 0) {
407
+ throw new MastraError(
578
408
  {
579
- id: "CLOUDFLARE_D1_STORAGE_GET_THREADS_BY_RESOURCE_ID_ERROR",
409
+ id: "STORAGE_CLOUDFLARE_D1_LIST_THREADS_BY_RESOURCE_ID_INVALID_PAGE",
580
410
  domain: ErrorDomain.STORAGE,
581
- category: ErrorCategory.THIRD_PARTY,
582
- text: `Error getting threads by resourceId ${resourceId}: ${error instanceof Error ? error.message : String(error)}`,
583
- details: { resourceId }
411
+ category: ErrorCategory.USER,
412
+ details: { page }
584
413
  },
585
- error
414
+ new Error("page must be >= 0")
586
415
  );
587
- this.logger?.error(mastraError.toString());
588
- this.logger?.trackException(mastraError);
589
- return [];
590
416
  }
591
- }
592
- async getThreadsByResourceIdPaginated(args) {
593
- const { resourceId, page, perPage } = args;
417
+ const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
418
+ const { field, direction } = this.parseOrderBy(orderBy);
594
419
  const fullTableName = this.operations.getTableName(TABLE_THREADS);
595
420
  const mapRowToStorageThreadType = (row) => ({
596
421
  ...row,
@@ -602,20 +427,21 @@ var MemoryStorageD1 = class extends MemoryStorage {
602
427
  const countQuery = createSqlBuilder().count().from(fullTableName).where("resourceId = ?", resourceId);
603
428
  const countResult = await this.operations.executeQuery(countQuery.build());
604
429
  const total = Number(countResult?.[0]?.count ?? 0);
605
- const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId).orderBy("createdAt", "DESC").limit(perPage).offset(page * perPage);
430
+ const limitValue = perPageInput === false ? total : perPage;
431
+ const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId).orderBy(field, direction).limit(limitValue).offset(offset);
606
432
  const results = await this.operations.executeQuery(selectQuery.build());
607
433
  const threads = results.map(mapRowToStorageThreadType);
608
434
  return {
609
435
  threads,
610
436
  total,
611
437
  page,
612
- perPage,
613
- hasMore: page * perPage + threads.length < total
438
+ perPage: perPageForResponse,
439
+ hasMore: perPageInput === false ? false : offset + perPage < total
614
440
  };
615
441
  } catch (error) {
616
442
  const mastraError = new MastraError(
617
443
  {
618
- id: "CLOUDFLARE_D1_STORAGE_GET_THREADS_BY_RESOURCE_ID_PAGINATED_ERROR",
444
+ id: "CLOUDFLARE_D1_STORAGE_LIST_THREADS_BY_RESOURCE_ID_ERROR",
619
445
  domain: ErrorDomain.STORAGE,
620
446
  category: ErrorCategory.THIRD_PARTY,
621
447
  text: `Error getting threads by resourceId ${resourceId}: ${error instanceof Error ? error.message : String(error)}`,
@@ -629,7 +455,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
629
455
  threads: [],
630
456
  total: 0,
631
457
  page,
632
- perPage,
458
+ perPage: perPageForResponse,
633
459
  hasMore: false
634
460
  };
635
461
  }
@@ -739,8 +565,8 @@ var MemoryStorageD1 = class extends MemoryStorage {
739
565
  }
740
566
  }
741
567
  async saveMessages(args) {
742
- const { messages, format = "v1" } = args;
743
- if (messages.length === 0) return [];
568
+ const { messages } = args;
569
+ if (messages.length === 0) return { messages: [] };
744
570
  try {
745
571
  const now = /* @__PURE__ */ new Date();
746
572
  const threadId = messages[0]?.threadId;
@@ -788,8 +614,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
788
614
  ]);
789
615
  this.logger.debug(`Saved ${messages.length} messages`);
790
616
  const list = new MessageList().add(messages, "memory");
791
- if (format === `v2`) return list.get.all.v2();
792
- return list.get.all.v1();
617
+ return { messages: list.get.all.db() };
793
618
  } catch (error) {
794
619
  throw new MastraError(
795
620
  {
@@ -802,9 +627,8 @@ var MemoryStorageD1 = class extends MemoryStorage {
802
627
  );
803
628
  }
804
629
  }
805
- async _getIncludedMessages(threadId, selectBy) {
630
+ async _getIncludedMessages(threadId, include) {
806
631
  if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
807
- const include = selectBy?.include;
808
632
  if (!include) return null;
809
633
  const unionQueries = [];
810
634
  const params = [];
@@ -860,74 +684,8 @@ var MemoryStorageD1 = class extends MemoryStorage {
860
684
  });
861
685
  return processedMessages;
862
686
  }
863
- async getMessages({
864
- threadId,
865
- resourceId,
866
- selectBy,
867
- format
868
- }) {
869
- try {
870
- if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
871
- const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
872
- const limit = resolveMessageLimit({
873
- last: selectBy?.last,
874
- defaultLimit: 40
875
- });
876
- const include = selectBy?.include || [];
877
- const messages = [];
878
- if (include.length) {
879
- const includeResult = await this._getIncludedMessages(threadId, selectBy);
880
- if (Array.isArray(includeResult)) messages.push(...includeResult);
881
- }
882
- const excludeIds = messages.map((m) => m.id);
883
- const query = createSqlBuilder().select(["id", "content", "role", "type", "createdAt", "thread_id AS threadId"]).from(fullTableName).where("thread_id = ?", threadId);
884
- if (excludeIds.length > 0) {
885
- query.andWhere(`id NOT IN (${excludeIds.map(() => "?").join(",")})`, ...excludeIds);
886
- }
887
- query.orderBy("createdAt", "DESC").limit(limit);
888
- const { sql, params } = query.build();
889
- const result = await this.operations.executeQuery({ sql, params });
890
- if (Array.isArray(result)) messages.push(...result);
891
- messages.sort((a, b) => {
892
- const aRecord = a;
893
- const bRecord = b;
894
- const timeA = new Date(aRecord.createdAt).getTime();
895
- const timeB = new Date(bRecord.createdAt).getTime();
896
- return timeA - timeB;
897
- });
898
- const processedMessages = messages.map((message) => {
899
- const processedMsg = {};
900
- for (const [key, value] of Object.entries(message)) {
901
- if (key === `type` && value === `v2`) continue;
902
- processedMsg[key] = deserializeValue(value);
903
- }
904
- return processedMsg;
905
- });
906
- this.logger.debug(`Retrieved ${messages.length} messages for thread ${threadId}`);
907
- const list = new MessageList().add(processedMessages, "memory");
908
- if (format === `v2`) return list.get.all.v2();
909
- return list.get.all.v1();
910
- } catch (error) {
911
- const mastraError = new MastraError(
912
- {
913
- id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_ERROR",
914
- domain: ErrorDomain.STORAGE,
915
- category: ErrorCategory.THIRD_PARTY,
916
- text: `Failed to retrieve messages for thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
917
- details: { threadId, resourceId: resourceId ?? "" }
918
- },
919
- error
920
- );
921
- this.logger?.error(mastraError.toString());
922
- this.logger?.trackException(mastraError);
923
- throw mastraError;
924
- }
925
- }
926
- async getMessagesById({
927
- messageIds,
928
- format
929
- }) {
930
- if (messageIds.length === 0) return [];
687
+ async listMessagesById({ messageIds }) {
688
+ if (messageIds.length === 0) return { messages: [] };
931
689
  const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
932
690
  const messages = [];
933
691
  try {
@@ -946,12 +704,11 @@ var MemoryStorageD1 = class extends MemoryStorage {
946
704
  });
947
705
  this.logger.debug(`Retrieved ${messages.length} messages`);
948
706
  const list = new MessageList().add(processedMessages, "memory");
949
- if (format === `v1`) return list.get.all.v1();
950
- return list.get.all.v2();
707
+ return { messages: list.get.all.db() };
951
708
  } catch (error) {
952
709
  const mastraError = new MastraError(
953
710
  {
954
- id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_BY_ID_ERROR",
711
+ id: "CLOUDFLARE_D1_STORAGE_LIST_MESSAGES_BY_ID_ERROR",
955
712
  domain: ErrorDomain.STORAGE,
956
713
  category: ErrorCategory.THIRD_PARTY,
957
714
  text: `Failed to retrieve messages by ID: ${error instanceof Error ? error.message : String(error)}`,
@@ -964,118 +721,157 @@ var MemoryStorageD1 = class extends MemoryStorage {
964
721
  throw mastraError;
965
722
  }
966
723
  }
967
- async getMessagesPaginated({
968
- threadId,
969
- resourceId,
970
- selectBy,
971
- format
972
- }) {
973
- const { dateRange, page = 0, perPage: perPageInput } = selectBy?.pagination || {};
974
- const { start: fromDate, end: toDate } = dateRange || {};
975
- const perPage = perPageInput !== void 0 ? perPageInput : resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
976
- const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
977
- const messages = [];
724
+ async listMessages(args) {
725
+ const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
726
+ if (!threadId.trim()) {
727
+ throw new MastraError(
728
+ {
729
+ id: "STORAGE_CLOUDFLARE_D1_LIST_MESSAGES_INVALID_THREAD_ID",
730
+ domain: ErrorDomain.STORAGE,
731
+ category: ErrorCategory.THIRD_PARTY,
732
+ details: { threadId }
733
+ },
734
+ new Error("threadId must be a non-empty string")
735
+ );
736
+ }
737
+ if (page < 0) {
738
+ throw new MastraError(
739
+ {
740
+ id: "STORAGE_CLOUDFLARE_D1_LIST_MESSAGES_INVALID_PAGE",
741
+ domain: ErrorDomain.STORAGE,
742
+ category: ErrorCategory.USER,
743
+ details: { page }
744
+ },
745
+ new Error("page must be >= 0")
746
+ );
747
+ }
748
+ const perPage = normalizePerPage(perPageInput, 40);
749
+ const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
978
750
  try {
979
- if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
980
- if (selectBy?.include?.length) {
981
- const includeResult = await this._getIncludedMessages(threadId, selectBy);
982
- if (Array.isArray(includeResult)) messages.push(...includeResult);
751
+ const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
752
+ let query = `
753
+ SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
754
+ FROM ${fullTableName}
755
+ WHERE thread_id = ?
756
+ `;
757
+ const queryParams = [threadId];
758
+ if (resourceId) {
759
+ query += ` AND resourceId = ?`;
760
+ queryParams.push(resourceId);
983
761
  }
984
- const countQuery = createSqlBuilder().count().from(fullTableName).where("thread_id = ?", threadId);
985
- if (fromDate) {
986
- countQuery.andWhere("createdAt >= ?", serializeDate(fromDate));
762
+ const dateRange = filter?.dateRange;
763
+ if (dateRange?.start) {
764
+ const startDate = dateRange.start instanceof Date ? serializeDate(dateRange.start) : serializeDate(new Date(dateRange.start));
765
+ query += ` AND createdAt >= ?`;
766
+ queryParams.push(startDate);
987
767
  }
988
- if (toDate) {
989
- countQuery.andWhere("createdAt <= ?", serializeDate(toDate));
768
+ if (dateRange?.end) {
769
+ const endDate = dateRange.end instanceof Date ? serializeDate(dateRange.end) : serializeDate(new Date(dateRange.end));
770
+ query += ` AND createdAt <= ?`;
771
+ queryParams.push(endDate);
990
772
  }
991
- const countResult = await this.operations.executeQuery(countQuery.build());
773
+ const { field, direction } = this.parseOrderBy(orderBy, "ASC");
774
+ query += ` ORDER BY "${field}" ${direction}`;
775
+ if (perPage !== Number.MAX_SAFE_INTEGER) {
776
+ query += ` LIMIT ? OFFSET ?`;
777
+ queryParams.push(perPage, offset);
778
+ }
779
+ const results = await this.operations.executeQuery({ sql: query, params: queryParams });
780
+ const paginatedMessages = (isArrayOfRecords(results) ? results : []).map((message) => {
781
+ const processedMsg = {};
782
+ for (const [key, value] of Object.entries(message)) {
783
+ if (key === `type` && value === `v2`) continue;
784
+ processedMsg[key] = deserializeValue(value);
785
+ }
786
+ return processedMsg;
787
+ });
788
+ const paginatedCount = paginatedMessages.length;
789
+ let countQuery = `SELECT count() as count FROM ${fullTableName} WHERE thread_id = ?`;
790
+ const countParams = [threadId];
791
+ if (resourceId) {
792
+ countQuery += ` AND resourceId = ?`;
793
+ countParams.push(resourceId);
794
+ }
795
+ if (dateRange?.start) {
796
+ const startDate = dateRange.start instanceof Date ? serializeDate(dateRange.start) : serializeDate(new Date(dateRange.start));
797
+ countQuery += ` AND createdAt >= ?`;
798
+ countParams.push(startDate);
799
+ }
800
+ if (dateRange?.end) {
801
+ const endDate = dateRange.end instanceof Date ? serializeDate(dateRange.end) : serializeDate(new Date(dateRange.end));
802
+ countQuery += ` AND createdAt <= ?`;
803
+ countParams.push(endDate);
804
+ }
805
+ const countResult = await this.operations.executeQuery({ sql: countQuery, params: countParams });
992
806
  const total = Number(countResult[0]?.count ?? 0);
993
- if (total === 0 && messages.length === 0) {
807
+ if (total === 0 && paginatedCount === 0 && (!include || include.length === 0)) {
994
808
  return {
995
809
  messages: [],
996
810
  total: 0,
997
811
  page,
998
- perPage,
812
+ perPage: perPageForResponse,
999
813
  hasMore: false
1000
814
  };
1001
815
  }
1002
- const excludeIds = messages.map((m) => m.id);
1003
- const excludeCondition = excludeIds.length > 0 ? `AND id NOT IN (${excludeIds.map(() => "?").join(",")})` : "";
1004
- let query;
1005
- let queryParams = [threadId];
1006
- if (fromDate) {
1007
- queryParams.push(serializeDate(fromDate));
1008
- }
1009
- if (toDate) {
1010
- queryParams.push(serializeDate(toDate));
1011
- }
1012
- if (excludeIds.length > 0) {
1013
- queryParams.push(...excludeIds);
1014
- }
1015
- if (selectBy?.last && selectBy.last > 0) {
1016
- query = `
1017
- SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
1018
- FROM ${fullTableName}
1019
- WHERE thread_id = ?
1020
- ${fromDate ? "AND createdAt >= ?" : ""}
1021
- ${toDate ? "AND createdAt <= ?" : ""}
1022
- ${excludeCondition}
1023
- ORDER BY createdAt DESC
1024
- LIMIT ?
1025
- `;
1026
- queryParams.push(selectBy.last);
1027
- } else {
1028
- query = `
1029
- SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
1030
- FROM ${fullTableName}
1031
- WHERE thread_id = ?
1032
- ${fromDate ? "AND createdAt >= ?" : ""}
1033
- ${toDate ? "AND createdAt <= ?" : ""}
1034
- ${excludeCondition}
1035
- ORDER BY createdAt DESC
1036
- LIMIT ? OFFSET ?
1037
- `;
1038
- queryParams.push(perPage, page * perPage);
816
+ const messageIds = new Set(paginatedMessages.map((m) => m.id));
817
+ let includeMessages = [];
818
+ if (include && include.length > 0) {
819
+ const includeResult = await this._getIncludedMessages(threadId, include);
820
+ if (Array.isArray(includeResult)) {
821
+ includeMessages = includeResult;
822
+ for (const includeMsg of includeMessages) {
823
+ if (!messageIds.has(includeMsg.id)) {
824
+ paginatedMessages.push(includeMsg);
825
+ messageIds.add(includeMsg.id);
826
+ }
827
+ }
828
+ }
1039
829
  }
1040
- const results = await this.operations.executeQuery({ sql: query, params: queryParams });
1041
- const processedMessages = results.map((message) => {
1042
- const processedMsg = {};
1043
- for (const [key, value] of Object.entries(message)) {
1044
- if (key === `type` && value === `v2`) continue;
1045
- processedMsg[key] = deserializeValue(value);
830
+ const list = new MessageList().add(paginatedMessages, "memory");
831
+ let finalMessages = list.get.all.db();
832
+ finalMessages = finalMessages.sort((a, b) => {
833
+ const isDateField = field === "createdAt" || field === "updatedAt";
834
+ const aValue = isDateField ? new Date(a[field]).getTime() : a[field];
835
+ const bValue = isDateField ? new Date(b[field]).getTime() : b[field];
836
+ if (aValue === bValue) {
837
+ return a.id.localeCompare(b.id);
1046
838
  }
1047
- return processedMsg;
839
+ if (typeof aValue === "number" && typeof bValue === "number") {
840
+ return direction === "ASC" ? aValue - bValue : bValue - aValue;
841
+ }
842
+ return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
1048
843
  });
1049
- if (selectBy?.last) {
1050
- processedMessages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
1051
- }
1052
- const list = new MessageList().add(processedMessages, "memory");
1053
- messages.push(...format === `v2` ? list.get.all.v2() : list.get.all.v1());
844
+ const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
845
+ const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
846
+ const hasMore = perPageInput === false ? false : allThreadMessagesReturned ? false : offset + paginatedCount < total;
1054
847
  return {
1055
- messages,
848
+ messages: finalMessages,
1056
849
  total,
1057
850
  page,
1058
- perPage,
1059
- hasMore: selectBy?.last ? false : page * perPage + messages.length < total
851
+ perPage: perPageForResponse,
852
+ hasMore
1060
853
  };
1061
854
  } catch (error) {
1062
855
  const mastraError = new MastraError(
1063
856
  {
1064
- id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_PAGINATED_ERROR",
857
+ id: "CLOUDFLARE_D1_STORAGE_LIST_MESSAGES_ERROR",
1065
858
  domain: ErrorDomain.STORAGE,
1066
859
  category: ErrorCategory.THIRD_PARTY,
1067
- text: `Failed to retrieve messages for thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
1068
- details: { threadId, resourceId: resourceId ?? "" }
860
+ text: `Failed to list messages for thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
861
+ details: {
862
+ threadId,
863
+ resourceId: resourceId ?? ""
864
+ }
1069
865
  },
1070
866
  error
1071
867
  );
1072
- this.logger?.error(mastraError.toString());
1073
- this.logger?.trackException(mastraError);
868
+ this.logger?.error?.(mastraError.toString());
869
+ this.logger?.trackException?.(mastraError);
1074
870
  return {
1075
871
  messages: [],
1076
872
  total: 0,
1077
873
  page,
1078
- perPage,
874
+ perPage: perPageForResponse,
1079
875
  hasMore: false
1080
876
  };
1081
877
  }
@@ -1576,7 +1372,7 @@ function transformScoreRow(row) {
1576
1372
  deserialized.analyzeStepResult = safelyParseJSON(row.analyzeStepResult);
1577
1373
  deserialized.metadata = safelyParseJSON(row.metadata);
1578
1374
  deserialized.additionalContext = safelyParseJSON(row.additionalContext);
1579
- deserialized.runtimeContext = safelyParseJSON(row.runtimeContext);
1375
+ deserialized.requestContext = safelyParseJSON(row.requestContext);
1580
1376
  deserialized.entity = safelyParseJSON(row.entity);
1581
1377
  deserialized.createdAt = row.createdAtZ || row.createdAt;
1582
1378
  deserialized.updatedAt = row.updatedAtZ || row.updatedAt;
@@ -1660,7 +1456,7 @@ var ScoresStorageD1 = class extends ScoresStorage {
1660
1456
  );
1661
1457
  }
1662
1458
  }
1663
- async getScoresByScorerId({
1459
+ async listScoresByScorerId({
1664
1460
  scorerId,
1665
1461
  entityId,
1666
1462
  entityType,
@@ -1668,6 +1464,9 @@ var ScoresStorageD1 = class extends ScoresStorage {
1668
1464
  pagination
1669
1465
  }) {
1670
1466
  try {
1467
+ const { page, perPage: perPageInput } = pagination;
1468
+ const perPage = normalizePerPage(perPageInput, 100);
1469
+ const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1671
1470
  const fullTableName = this.operations.getTableName(TABLE_SCORERS);
1672
1471
  const countQuery = createSqlBuilder().count().from(fullTableName).where("scorerId = ?", scorerId);
1673
1472
  if (entityId) {
@@ -1685,13 +1484,15 @@ var ScoresStorageD1 = class extends ScoresStorage {
1685
1484
  return {
1686
1485
  pagination: {
1687
1486
  total: 0,
1688
- page: pagination.page,
1689
- perPage: pagination.perPage,
1487
+ page,
1488
+ perPage: perPageForResponse,
1690
1489
  hasMore: false
1691
1490
  },
1692
1491
  scores: []
1693
1492
  };
1694
1493
  }
1494
+ const end = perPageInput === false ? total : start + perPage;
1495
+ const limitValue = perPageInput === false ? total : perPage;
1695
1496
  const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("scorerId = ?", scorerId);
1696
1497
  if (entityId) {
1697
1498
  selectQuery.andWhere("entityId = ?", entityId);
@@ -1702,16 +1503,16 @@ var ScoresStorageD1 = class extends ScoresStorage {
1702
1503
  if (source) {
1703
1504
  selectQuery.andWhere("source = ?", source);
1704
1505
  }
1705
- selectQuery.limit(pagination.perPage).offset(pagination.page * pagination.perPage);
1506
+ selectQuery.limit(limitValue).offset(start);
1706
1507
  const { sql, params } = selectQuery.build();
1707
1508
  const results = await this.operations.executeQuery({ sql, params });
1708
1509
  const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
1709
1510
  return {
1710
1511
  pagination: {
1711
1512
  total,
1712
- page: pagination.page,
1713
- perPage: pagination.perPage,
1714
- hasMore: total > (pagination.page + 1) * pagination.perPage
1513
+ page,
1514
+ perPage: perPageForResponse,
1515
+ hasMore: end < total
1715
1516
  },
1716
1517
  scores
1717
1518
  };
@@ -1726,11 +1527,14 @@ var ScoresStorageD1 = class extends ScoresStorage {
1726
1527
  );
1727
1528
  }
1728
1529
  }
1729
- async getScoresByRunId({
1530
+ async listScoresByRunId({
1730
1531
  runId,
1731
1532
  pagination
1732
1533
  }) {
1733
1534
  try {
1535
+ const { page, perPage: perPageInput } = pagination;
1536
+ const perPage = normalizePerPage(perPageInput, 100);
1537
+ const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1734
1538
  const fullTableName = this.operations.getTableName(TABLE_SCORERS);
1735
1539
  const countQuery = createSqlBuilder().count().from(fullTableName).where("runId = ?", runId);
1736
1540
  const countResult = await this.operations.executeQuery(countQuery.build());
@@ -1739,23 +1543,25 @@ var ScoresStorageD1 = class extends ScoresStorage {
1739
1543
  return {
1740
1544
  pagination: {
1741
1545
  total: 0,
1742
- page: pagination.page,
1743
- perPage: pagination.perPage,
1546
+ page,
1547
+ perPage: perPageForResponse,
1744
1548
  hasMore: false
1745
1549
  },
1746
1550
  scores: []
1747
1551
  };
1748
1552
  }
1749
- const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("runId = ?", runId).limit(pagination.perPage).offset(pagination.page * pagination.perPage);
1553
+ const end = perPageInput === false ? total : start + perPage;
1554
+ const limitValue = perPageInput === false ? total : perPage;
1555
+ const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("runId = ?", runId).limit(limitValue).offset(start);
1750
1556
  const { sql, params } = selectQuery.build();
1751
1557
  const results = await this.operations.executeQuery({ sql, params });
1752
1558
  const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
1753
1559
  return {
1754
1560
  pagination: {
1755
1561
  total,
1756
- page: pagination.page,
1757
- perPage: pagination.perPage,
1758
- hasMore: total > (pagination.page + 1) * pagination.perPage
1562
+ page,
1563
+ perPage: perPageForResponse,
1564
+ hasMore: end < total
1759
1565
  },
1760
1566
  scores
1761
1567
  };
@@ -1770,12 +1576,15 @@ var ScoresStorageD1 = class extends ScoresStorage {
1770
1576
  );
1771
1577
  }
1772
1578
  }
1773
- async getScoresByEntityId({
1579
+ async listScoresByEntityId({
1774
1580
  entityId,
1775
1581
  entityType,
1776
1582
  pagination
1777
1583
  }) {
1778
1584
  try {
1585
+ const { page, perPage: perPageInput } = pagination;
1586
+ const perPage = normalizePerPage(perPageInput, 100);
1587
+ const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1779
1588
  const fullTableName = this.operations.getTableName(TABLE_SCORERS);
1780
1589
  const countQuery = createSqlBuilder().count().from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType);
1781
1590
  const countResult = await this.operations.executeQuery(countQuery.build());
@@ -1784,23 +1593,25 @@ var ScoresStorageD1 = class extends ScoresStorage {
1784
1593
  return {
1785
1594
  pagination: {
1786
1595
  total: 0,
1787
- page: pagination.page,
1788
- perPage: pagination.perPage,
1596
+ page,
1597
+ perPage: perPageForResponse,
1789
1598
  hasMore: false
1790
1599
  },
1791
1600
  scores: []
1792
1601
  };
1793
1602
  }
1794
- const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType).limit(pagination.perPage).offset(pagination.page * pagination.perPage);
1603
+ const end = perPageInput === false ? total : start + perPage;
1604
+ const limitValue = perPageInput === false ? total : perPage;
1605
+ const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType).limit(limitValue).offset(start);
1795
1606
  const { sql, params } = selectQuery.build();
1796
1607
  const results = await this.operations.executeQuery({ sql, params });
1797
1608
  const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
1798
1609
  return {
1799
1610
  pagination: {
1800
1611
  total,
1801
- page: pagination.page,
1802
- perPage: pagination.perPage,
1803
- hasMore: total > (pagination.page + 1) * pagination.perPage
1612
+ page,
1613
+ perPage: perPageForResponse,
1614
+ hasMore: end < total
1804
1615
  },
1805
1616
  scores
1806
1617
  };
@@ -1815,12 +1626,15 @@ var ScoresStorageD1 = class extends ScoresStorage {
1815
1626
  );
1816
1627
  }
1817
1628
  }
1818
- async getScoresBySpan({
1629
+ async listScoresBySpan({
1819
1630
  traceId,
1820
1631
  spanId,
1821
1632
  pagination
1822
1633
  }) {
1823
1634
  try {
1635
+ const { page, perPage: perPageInput } = pagination;
1636
+ const perPage = normalizePerPage(perPageInput, 100);
1637
+ const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1824
1638
  const fullTableName = this.operations.getTableName(TABLE_SCORERS);
1825
1639
  const countQuery = createSqlBuilder().count().from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId);
1826
1640
  const countResult = await this.operations.executeQuery(countQuery.build());
@@ -1829,25 +1643,25 @@ var ScoresStorageD1 = class extends ScoresStorage {
1829
1643
  return {
1830
1644
  pagination: {
1831
1645
  total: 0,
1832
- page: pagination.page,
1833
- perPage: pagination.perPage,
1646
+ page,
1647
+ perPage: perPageForResponse,
1834
1648
  hasMore: false
1835
1649
  },
1836
1650
  scores: []
1837
1651
  };
1838
1652
  }
1839
- const limit = pagination.perPage + 1;
1840
- const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId).orderBy("createdAt", "DESC").limit(limit).offset(pagination.page * pagination.perPage);
1653
+ const end = perPageInput === false ? total : start + perPage;
1654
+ const limitValue = perPageInput === false ? total : perPage;
1655
+ const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId).orderBy("createdAt", "DESC").limit(limitValue).offset(start);
1841
1656
  const { sql, params } = selectQuery.build();
1842
1657
  const results = await this.operations.executeQuery({ sql, params });
1843
- const rows = Array.isArray(results) ? results : [];
1844
- const scores = rows.slice(0, pagination.perPage).map(transformScoreRow);
1658
+ const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
1845
1659
  return {
1846
1660
  pagination: {
1847
1661
  total,
1848
- page: pagination.page,
1849
- perPage: pagination.perPage,
1850
- hasMore: rows.length > pagination.perPage
1662
+ page,
1663
+ perPage: perPageForResponse,
1664
+ hasMore: end < total
1851
1665
  },
1852
1666
  scores
1853
1667
  };
@@ -1863,128 +1677,6 @@ var ScoresStorageD1 = class extends ScoresStorage {
1863
1677
  }
1864
1678
  }
1865
1679
  };
1866
- function isArrayOfRecords2(value) {
1867
- return value && Array.isArray(value) && value.length > 0;
1868
- }
1869
- var TracesStorageD1 = class extends TracesStorage {
1870
- operations;
1871
- constructor({ operations }) {
1872
- super();
1873
- this.operations = operations;
1874
- }
1875
- async getTraces(args) {
1876
- const paginatedArgs = {
1877
- name: args.name,
1878
- scope: args.scope,
1879
- page: args.page,
1880
- perPage: args.perPage,
1881
- attributes: args.attributes,
1882
- filters: args.filters,
1883
- dateRange: args.fromDate || args.toDate ? {
1884
- start: args.fromDate,
1885
- end: args.toDate
1886
- } : void 0
1887
- };
1888
- try {
1889
- const result = await this.getTracesPaginated(paginatedArgs);
1890
- return result.traces;
1891
- } catch (error) {
1892
- throw new MastraError(
1893
- {
1894
- id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_ERROR",
1895
- domain: ErrorDomain.STORAGE,
1896
- category: ErrorCategory.THIRD_PARTY,
1897
- text: `Failed to retrieve traces: ${error instanceof Error ? error.message : String(error)}`,
1898
- details: {
1899
- name: args.name ?? "",
1900
- scope: args.scope ?? ""
1901
- }
1902
- },
1903
- error
1904
- );
1905
- }
1906
- }
1907
- async getTracesPaginated(args) {
1908
- const { name, scope, page = 0, perPage = 100, attributes, dateRange } = args;
1909
- const fromDate = dateRange?.start;
1910
- const toDate = dateRange?.end;
1911
- const fullTableName = this.operations.getTableName(TABLE_TRACES);
1912
- try {
1913
- const dataQuery = createSqlBuilder().select("*").from(fullTableName).where("1=1");
1914
- const countQuery = createSqlBuilder().count().from(fullTableName).where("1=1");
1915
- if (name) {
1916
- dataQuery.andWhere("name LIKE ?", `%${name}%`);
1917
- countQuery.andWhere("name LIKE ?", `%${name}%`);
1918
- }
1919
- if (scope) {
1920
- dataQuery.andWhere("scope = ?", scope);
1921
- countQuery.andWhere("scope = ?", scope);
1922
- }
1923
- if (attributes && Object.keys(attributes).length > 0) {
1924
- for (const [key, value] of Object.entries(attributes)) {
1925
- dataQuery.jsonLike("attributes", key, value);
1926
- countQuery.jsonLike("attributes", key, value);
1927
- }
1928
- }
1929
- if (fromDate) {
1930
- const fromDateStr = fromDate instanceof Date ? fromDate.toISOString() : fromDate;
1931
- dataQuery.andWhere("createdAt >= ?", fromDateStr);
1932
- countQuery.andWhere("createdAt >= ?", fromDateStr);
1933
- }
1934
- if (toDate) {
1935
- const toDateStr = toDate instanceof Date ? toDate.toISOString() : toDate;
1936
- dataQuery.andWhere("createdAt <= ?", toDateStr);
1937
- countQuery.andWhere("createdAt <= ?", toDateStr);
1938
- }
1939
- const allDataResult = await this.operations.executeQuery(
1940
- createSqlBuilder().select("*").from(fullTableName).where("1=1").build()
1941
- );
1942
- console.info("allDataResult", allDataResult);
1943
- const countResult = await this.operations.executeQuery(countQuery.build());
1944
- const total = Number(countResult?.[0]?.count ?? 0);
1945
- dataQuery.orderBy("startTime", "DESC").limit(perPage).offset(page * perPage);
1946
- const results = await this.operations.executeQuery(dataQuery.build());
1947
- const traces = isArrayOfRecords2(results) ? results.map(
1948
- (trace) => ({
1949
- ...trace,
1950
- attributes: deserializeValue(trace.attributes, "jsonb"),
1951
- status: deserializeValue(trace.status, "jsonb"),
1952
- events: deserializeValue(trace.events, "jsonb"),
1953
- links: deserializeValue(trace.links, "jsonb"),
1954
- other: deserializeValue(trace.other, "jsonb")
1955
- })
1956
- ) : [];
1957
- return {
1958
- traces,
1959
- total,
1960
- page,
1961
- perPage,
1962
- hasMore: page * perPage + traces.length < total
1963
- };
1964
- } catch (error) {
1965
- const mastraError = new MastraError(
1966
- {
1967
- id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_PAGINATED_ERROR",
1968
- domain: ErrorDomain.STORAGE,
1969
- category: ErrorCategory.THIRD_PARTY,
1970
- text: `Failed to retrieve traces: ${error instanceof Error ? error.message : String(error)}`,
1971
- details: { name: name ?? "", scope: scope ?? "" }
1972
- },
1973
- error
1974
- );
1975
- this.logger?.error(mastraError.toString());
1976
- this.logger?.trackException(mastraError);
1977
- return { traces: [], total: 0, page, perPage, hasMore: false };
1978
- }
1979
- }
1980
- async batchTraceInsert({ records }) {
1981
- this.logger.debug("Batch inserting traces", { count: records.length });
1982
- await this.operations.batchInsert({
1983
- tableName: TABLE_TRACES,
1984
- records
1985
- });
1986
- }
1987
- };
1988
1680
  var WorkflowsStorageD1 = class extends WorkflowsStorage {
1989
1681
  operations;
1990
1682
  constructor({ operations }) {
@@ -1996,7 +1688,7 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
1996
1688
  // runId,
1997
1689
  // stepId,
1998
1690
  // result,
1999
- // runtimeContext,
1691
+ // requestContext,
2000
1692
  }) {
2001
1693
  throw new Error("Method not implemented.");
2002
1694
  }
@@ -2100,12 +1792,12 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
2100
1792
  resourceId: row.resourceId
2101
1793
  };
2102
1794
  }
2103
- async getWorkflowRuns({
1795
+ async listWorkflowRuns({
2104
1796
  workflowName,
2105
1797
  fromDate,
2106
1798
  toDate,
2107
- limit,
2108
- offset,
1799
+ page,
1800
+ perPage,
2109
1801
  resourceId
2110
1802
  } = {}) {
2111
1803
  const fullTableName = this.operations.getTableName(TABLE_WORKFLOW_SNAPSHOT);
@@ -2131,11 +1823,14 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
2131
1823
  countBuilder.whereAnd("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
2132
1824
  }
2133
1825
  builder.orderBy("createdAt", "DESC");
2134
- if (typeof limit === "number") builder.limit(limit);
2135
- if (typeof offset === "number") builder.offset(offset);
1826
+ if (typeof perPage === "number" && typeof page === "number") {
1827
+ const offset = page * perPage;
1828
+ builder.limit(perPage);
1829
+ builder.offset(offset);
1830
+ }
2136
1831
  const { sql, params } = builder.build();
2137
1832
  let total = 0;
2138
- if (limit !== void 0 && offset !== void 0) {
1833
+ if (perPage !== void 0 && page !== void 0) {
2139
1834
  const { sql: countSql, params: countParams } = countBuilder.build();
2140
1835
  const countResult = await this.operations.executeQuery({
2141
1836
  sql: countSql,
@@ -2150,7 +1845,7 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
2150
1845
  } catch (error) {
2151
1846
  throw new MastraError(
2152
1847
  {
2153
- id: "CLOUDFLARE_D1_STORAGE_GET_WORKFLOW_RUNS_ERROR",
1848
+ id: "CLOUDFLARE_D1_STORAGE_LIST_WORKFLOW_RUNS_ERROR",
2154
1849
  domain: ErrorDomain.STORAGE,
2155
1850
  category: ErrorCategory.THIRD_PARTY,
2156
1851
  text: `Failed to retrieve workflow runs: ${error instanceof Error ? error.message : String(error)}`,
@@ -2212,7 +1907,7 @@ var D1Store = class extends MastraStorage {
2212
1907
  */
2213
1908
  constructor(config) {
2214
1909
  try {
2215
- super({ name: "D1" });
1910
+ super({ id: config.id, name: "D1" });
2216
1911
  if (config.tablePrefix && !/^[a-zA-Z0-9_]*$/.test(config.tablePrefix)) {
2217
1912
  throw new Error("Invalid tablePrefix: only letters, numbers, and underscores are allowed.");
2218
1913
  }
@@ -2266,12 +1961,6 @@ var D1Store = class extends MastraStorage {
2266
1961
  const scores = new ScoresStorageD1({
2267
1962
  operations
2268
1963
  });
2269
- const legacyEvals = new LegacyEvalsStorageD1({
2270
- operations
2271
- });
2272
- const traces = new TracesStorageD1({
2273
- operations
2274
- });
2275
1964
  const workflows = new WorkflowsStorageD1({
2276
1965
  operations
2277
1966
  });
@@ -2281,8 +1970,6 @@ var D1Store = class extends MastraStorage {
2281
1970
  this.stores = {
2282
1971
  operations,
2283
1972
  scores,
2284
- legacyEvals,
2285
- traces,
2286
1973
  workflows,
2287
1974
  memory
2288
1975
  };
@@ -2294,7 +1981,7 @@ var D1Store = class extends MastraStorage {
2294
1981
  hasColumn: true,
2295
1982
  createTable: true,
2296
1983
  deleteMessages: false,
2297
- getScoresBySpan: true
1984
+ listScoresBySpan: true
2298
1985
  };
2299
1986
  }
2300
1987
  async createTable({
@@ -2334,15 +2021,6 @@ var D1Store = class extends MastraStorage {
2334
2021
  async getThreadById({ threadId }) {
2335
2022
  return this.stores.memory.getThreadById({ threadId });
2336
2023
  }
2337
- /**
2338
- * @deprecated use getThreadsByResourceIdPaginated instead
2339
- */
2340
- async getThreadsByResourceId({ resourceId }) {
2341
- return this.stores.memory.getThreadsByResourceId({ resourceId });
2342
- }
2343
- async getThreadsByResourceIdPaginated(args) {
2344
- return this.stores.memory.getThreadsByResourceIdPaginated(args);
2345
- }
2346
2024
  async saveThread({ thread }) {
2347
2025
  return this.stores.memory.saveThread({ thread });
2348
2026
  }
@@ -2359,34 +2037,14 @@ var D1Store = class extends MastraStorage {
2359
2037
  async saveMessages(args) {
2360
2038
  return this.stores.memory.saveMessages(args);
2361
2039
  }
2362
- async getMessages({
2363
- threadId,
2364
- selectBy,
2365
- format
2366
- }) {
2367
- return this.stores.memory.getMessages({ threadId, selectBy, format });
2368
- }
2369
- async getMessagesById({
2370
- messageIds,
2371
- format
2372
- }) {
2373
- return this.stores.memory.getMessagesById({ messageIds, format });
2374
- }
2375
- async getMessagesPaginated({
2376
- threadId,
2377
- selectBy,
2378
- format
2379
- }) {
2380
- return this.stores.memory.getMessagesPaginated({ threadId, selectBy, format });
2381
- }
2382
2040
  async updateWorkflowResults({
2383
2041
  workflowName,
2384
2042
  runId,
2385
2043
  stepId,
2386
2044
  result,
2387
- runtimeContext
2045
+ requestContext
2388
2046
  }) {
2389
- return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, runtimeContext });
2047
+ return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
2390
2048
  }
2391
2049
  async updateWorkflowState({
2392
2050
  workflowName,
@@ -2406,15 +2064,15 @@ var D1Store = class extends MastraStorage {
2406
2064
  async loadWorkflowSnapshot(params) {
2407
2065
  return this.stores.workflows.loadWorkflowSnapshot(params);
2408
2066
  }
2409
- async getWorkflowRuns({
2067
+ async listWorkflowRuns({
2410
2068
  workflowName,
2411
2069
  fromDate,
2412
2070
  toDate,
2413
- limit,
2414
- offset,
2071
+ perPage,
2072
+ page,
2415
2073
  resourceId
2416
2074
  } = {}) {
2417
- return this.stores.workflows.getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId });
2075
+ return this.stores.workflows.listWorkflowRuns({ workflowName, fromDate, toDate, perPage, page, resourceId });
2418
2076
  }
2419
2077
  async getWorkflowRunById({
2420
2078
  runId,
@@ -2430,24 +2088,6 @@ var D1Store = class extends MastraStorage {
2430
2088
  async batchInsert({ tableName, records }) {
2431
2089
  return this.stores.operations.batchInsert({ tableName, records });
2432
2090
  }
2433
- /**
2434
- * @deprecated use getTracesPaginated instead
2435
- */
2436
- async getTraces(args) {
2437
- return this.stores.traces.getTraces(args);
2438
- }
2439
- async getTracesPaginated(args) {
2440
- return this.stores.traces.getTracesPaginated(args);
2441
- }
2442
- /**
2443
- * @deprecated use getEvals instead
2444
- */
2445
- async getEvalsByAgentName(agentName, type) {
2446
- return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
2447
- }
2448
- async getEvals(options) {
2449
- return this.stores.legacyEvals.getEvals(options);
2450
- }
2451
2091
  async updateMessages(_args) {
2452
2092
  return this.stores.memory.updateMessages(_args);
2453
2093
  }
@@ -2470,38 +2110,38 @@ var D1Store = class extends MastraStorage {
2470
2110
  async saveScore(_score) {
2471
2111
  return this.stores.scores.saveScore(_score);
2472
2112
  }
2473
- async getScoresByRunId({
2113
+ async listScoresByRunId({
2474
2114
  runId: _runId,
2475
2115
  pagination: _pagination
2476
2116
  }) {
2477
- return this.stores.scores.getScoresByRunId({ runId: _runId, pagination: _pagination });
2117
+ return this.stores.scores.listScoresByRunId({ runId: _runId, pagination: _pagination });
2478
2118
  }
2479
- async getScoresByEntityId({
2119
+ async listScoresByEntityId({
2480
2120
  entityId: _entityId,
2481
2121
  entityType: _entityType,
2482
2122
  pagination: _pagination
2483
2123
  }) {
2484
- return this.stores.scores.getScoresByEntityId({
2124
+ return this.stores.scores.listScoresByEntityId({
2485
2125
  entityId: _entityId,
2486
2126
  entityType: _entityType,
2487
2127
  pagination: _pagination
2488
2128
  });
2489
2129
  }
2490
- async getScoresByScorerId({
2130
+ async listScoresByScorerId({
2491
2131
  scorerId,
2492
2132
  pagination,
2493
2133
  entityId,
2494
2134
  entityType,
2495
2135
  source
2496
2136
  }) {
2497
- return this.stores.scores.getScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
2137
+ return this.stores.scores.listScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
2498
2138
  }
2499
- async getScoresBySpan({
2139
+ async listScoresBySpan({
2500
2140
  traceId,
2501
2141
  spanId,
2502
2142
  pagination
2503
2143
  }) {
2504
- return this.stores.scores.getScoresBySpan({ traceId, spanId, pagination });
2144
+ return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
2505
2145
  }
2506
2146
  /**
2507
2147
  * Close the database connection