@mastra/cloudflare-d1 0.0.0-pgvector-index-fix-20250905222058 → 0.0.0-playground-studio-again-20251114100107

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,8 +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';
5
+ import { parseSqlIdentifier } from '@mastra/core/utils';
6
+ import { saveScorePayloadSchema } from '@mastra/core/evals';
6
7
 
7
8
  // src/storage/index.ts
8
9
  var SqlBuilder = class {
@@ -241,16 +242,6 @@ function isArrayOfRecords(value) {
241
242
  }
242
243
  function deserializeValue(value, type) {
243
244
  if (value === null || value === void 0) return null;
244
- if (type === "date" && typeof value === "string") {
245
- return new Date(value);
246
- }
247
- if (type === "jsonb" && typeof value === "string") {
248
- try {
249
- return JSON.parse(value);
250
- } catch {
251
- return value;
252
- }
253
- }
254
245
  if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
255
246
  try {
256
247
  return JSON.parse(value);
@@ -261,155 +252,7 @@ function deserializeValue(value, type) {
261
252
  return value;
262
253
  }
263
254
 
264
- // src/storage/domains/legacy-evals/index.ts
265
- var LegacyEvalsStorageD1 = class extends LegacyEvalsStorage {
266
- operations;
267
- constructor({ operations }) {
268
- super();
269
- this.operations = operations;
270
- }
271
- async getEvals(options) {
272
- const { agentName, type, page = 0, perPage = 40, dateRange } = options || {};
273
- const fullTableName = this.operations.getTableName(TABLE_EVALS);
274
- const conditions = [];
275
- const queryParams = [];
276
- if (agentName) {
277
- conditions.push(`agent_name = ?`);
278
- queryParams.push(agentName);
279
- }
280
- if (type === "test") {
281
- conditions.push(`(test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL)`);
282
- } else if (type === "live") {
283
- conditions.push(`(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)`);
284
- }
285
- if (dateRange?.start) {
286
- conditions.push(`created_at >= ?`);
287
- queryParams.push(serializeDate(dateRange.start));
288
- }
289
- if (dateRange?.end) {
290
- conditions.push(`created_at <= ?`);
291
- queryParams.push(serializeDate(dateRange.end));
292
- }
293
- const countQueryBuilder = createSqlBuilder().count().from(fullTableName);
294
- if (conditions.length > 0) {
295
- countQueryBuilder.where(conditions.join(" AND "), ...queryParams);
296
- }
297
- const { sql: countSql, params: countParams } = countQueryBuilder.build();
298
- try {
299
- const countResult = await this.operations.executeQuery({
300
- sql: countSql,
301
- params: countParams,
302
- first: true
303
- });
304
- const total = Number(countResult?.count || 0);
305
- const currentOffset = page * perPage;
306
- if (total === 0) {
307
- return {
308
- evals: [],
309
- total: 0,
310
- page,
311
- perPage,
312
- hasMore: false
313
- };
314
- }
315
- const dataQueryBuilder = createSqlBuilder().select("*").from(fullTableName);
316
- if (conditions.length > 0) {
317
- dataQueryBuilder.where(conditions.join(" AND "), ...queryParams);
318
- }
319
- dataQueryBuilder.orderBy("created_at", "DESC").limit(perPage).offset(currentOffset);
320
- const { sql: dataSql, params: dataParams } = dataQueryBuilder.build();
321
- const rows = await this.operations.executeQuery({
322
- sql: dataSql,
323
- params: dataParams
324
- });
325
- const evals = (isArrayOfRecords(rows) ? rows : []).map((row) => {
326
- const result = deserializeValue(row.result);
327
- const testInfo = row.test_info ? deserializeValue(row.test_info) : void 0;
328
- if (!result || typeof result !== "object" || !("score" in result)) {
329
- throw new Error(`Invalid MetricResult format: ${JSON.stringify(result)}`);
330
- }
331
- return {
332
- input: row.input,
333
- output: row.output,
334
- result,
335
- agentName: row.agent_name,
336
- metricName: row.metric_name,
337
- instructions: row.instructions,
338
- testInfo,
339
- globalRunId: row.global_run_id,
340
- runId: row.run_id,
341
- createdAt: row.created_at
342
- };
343
- });
344
- const hasMore = currentOffset + evals.length < total;
345
- return {
346
- evals,
347
- total,
348
- page,
349
- perPage,
350
- hasMore
351
- };
352
- } catch (error) {
353
- throw new MastraError(
354
- {
355
- id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
356
- domain: ErrorDomain.STORAGE,
357
- category: ErrorCategory.THIRD_PARTY,
358
- text: `Failed to retrieve evals for agent ${agentName}: ${error instanceof Error ? error.message : String(error)}`,
359
- details: { agentName: agentName ?? "", type: type ?? "" }
360
- },
361
- error
362
- );
363
- }
364
- }
365
- /**
366
- * @deprecated use getEvals instead
367
- */
368
- async getEvalsByAgentName(agentName, type) {
369
- const fullTableName = this.operations.getTableName(TABLE_EVALS);
370
- try {
371
- let query = createSqlBuilder().select("*").from(fullTableName).where("agent_name = ?", agentName);
372
- if (type === "test") {
373
- query = query.andWhere("test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL");
374
- } else if (type === "live") {
375
- query = query.andWhere("(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)");
376
- }
377
- query.orderBy("created_at", "DESC");
378
- const { sql, params } = query.build();
379
- const results = await this.operations.executeQuery({ sql, params });
380
- return isArrayOfRecords(results) ? results.map((row) => {
381
- const result = deserializeValue(row.result);
382
- const testInfo = row.test_info ? deserializeValue(row.test_info) : void 0;
383
- return {
384
- input: row.input || "",
385
- output: row.output || "",
386
- result,
387
- agentName: row.agent_name || "",
388
- metricName: row.metric_name || "",
389
- instructions: row.instructions || "",
390
- runId: row.run_id || "",
391
- globalRunId: row.global_run_id || "",
392
- createdAt: row.created_at || "",
393
- testInfo
394
- };
395
- }) : [];
396
- } catch (error) {
397
- const mastraError = new MastraError(
398
- {
399
- id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
400
- domain: ErrorDomain.STORAGE,
401
- category: ErrorCategory.THIRD_PARTY,
402
- text: `Failed to retrieve evals for agent ${agentName}: ${error instanceof Error ? error.message : String(error)}`,
403
- details: { agentName }
404
- },
405
- error
406
- );
407
- this.logger?.error(mastraError.toString());
408
- this.logger?.trackException(mastraError);
409
- return [];
410
- }
411
- }
412
- };
255
+ // src/storage/domains/memory/index.ts
413
256
  var MemoryStorageD1 = class extends MemoryStorage {
414
257
  operations;
415
258
  constructor({ operations }) {
@@ -557,39 +400,22 @@ var MemoryStorageD1 = class extends MemoryStorage {
557
400
  return null;
558
401
  }
559
402
  }
560
- /**
561
- * @deprecated use getThreadsByResourceIdPaginated instead
562
- */
563
- async getThreadsByResourceId({ resourceId }) {
564
- const fullTableName = this.operations.getTableName(TABLE_THREADS);
565
- try {
566
- const query = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId);
567
- const { sql, params } = query.build();
568
- const results = await this.operations.executeQuery({ sql, params });
569
- return (isArrayOfRecords(results) ? results : []).map((thread) => ({
570
- ...thread,
571
- createdAt: ensureDate(thread.createdAt),
572
- updatedAt: ensureDate(thread.updatedAt),
573
- metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
574
- }));
575
- } catch (error) {
576
- 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(
577
408
  {
578
- id: "CLOUDFLARE_D1_STORAGE_GET_THREADS_BY_RESOURCE_ID_ERROR",
409
+ id: "STORAGE_CLOUDFLARE_D1_LIST_THREADS_BY_RESOURCE_ID_INVALID_PAGE",
579
410
  domain: ErrorDomain.STORAGE,
580
- category: ErrorCategory.THIRD_PARTY,
581
- text: `Error getting threads by resourceId ${resourceId}: ${error instanceof Error ? error.message : String(error)}`,
582
- details: { resourceId }
411
+ category: ErrorCategory.USER,
412
+ details: { page }
583
413
  },
584
- error
414
+ new Error("page must be >= 0")
585
415
  );
586
- this.logger?.error(mastraError.toString());
587
- this.logger?.trackException(mastraError);
588
- return [];
589
416
  }
590
- }
591
- async getThreadsByResourceIdPaginated(args) {
592
- const { resourceId, page, perPage } = args;
417
+ const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
418
+ const { field, direction } = this.parseOrderBy(orderBy);
593
419
  const fullTableName = this.operations.getTableName(TABLE_THREADS);
594
420
  const mapRowToStorageThreadType = (row) => ({
595
421
  ...row,
@@ -601,20 +427,21 @@ var MemoryStorageD1 = class extends MemoryStorage {
601
427
  const countQuery = createSqlBuilder().count().from(fullTableName).where("resourceId = ?", resourceId);
602
428
  const countResult = await this.operations.executeQuery(countQuery.build());
603
429
  const total = Number(countResult?.[0]?.count ?? 0);
604
- const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId).orderBy("createdAt", "DESC").limit(perPage).offset(page * perPage);
430
+ const limitValue = perPageInput === false ? total : perPage;
431
+ const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId).orderBy(field, direction).limit(limitValue).offset(offset);
605
432
  const results = await this.operations.executeQuery(selectQuery.build());
606
433
  const threads = results.map(mapRowToStorageThreadType);
607
434
  return {
608
435
  threads,
609
436
  total,
610
437
  page,
611
- perPage,
612
- hasMore: page * perPage + threads.length < total
438
+ perPage: perPageForResponse,
439
+ hasMore: perPageInput === false ? false : offset + perPage < total
613
440
  };
614
441
  } catch (error) {
615
442
  const mastraError = new MastraError(
616
443
  {
617
- id: "CLOUDFLARE_D1_STORAGE_GET_THREADS_BY_RESOURCE_ID_PAGINATED_ERROR",
444
+ id: "CLOUDFLARE_D1_STORAGE_LIST_THREADS_BY_RESOURCE_ID_ERROR",
618
445
  domain: ErrorDomain.STORAGE,
619
446
  category: ErrorCategory.THIRD_PARTY,
620
447
  text: `Error getting threads by resourceId ${resourceId}: ${error instanceof Error ? error.message : String(error)}`,
@@ -628,7 +455,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
628
455
  threads: [],
629
456
  total: 0,
630
457
  page,
631
- perPage,
458
+ perPage: perPageForResponse,
632
459
  hasMore: false
633
460
  };
634
461
  }
@@ -738,8 +565,8 @@ var MemoryStorageD1 = class extends MemoryStorage {
738
565
  }
739
566
  }
740
567
  async saveMessages(args) {
741
- const { messages, format = "v1" } = args;
742
- if (messages.length === 0) return [];
568
+ const { messages } = args;
569
+ if (messages.length === 0) return { messages: [] };
743
570
  try {
744
571
  const now = /* @__PURE__ */ new Date();
745
572
  const threadId = messages[0]?.threadId;
@@ -787,8 +614,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
787
614
  ]);
788
615
  this.logger.debug(`Saved ${messages.length} messages`);
789
616
  const list = new MessageList().add(messages, "memory");
790
- if (format === `v2`) return list.get.all.v2();
791
- return list.get.all.v1();
617
+ return { messages: list.get.all.db() };
792
618
  } catch (error) {
793
619
  throw new MastraError(
794
620
  {
@@ -801,9 +627,8 @@ var MemoryStorageD1 = class extends MemoryStorage {
801
627
  );
802
628
  }
803
629
  }
804
- async _getIncludedMessages(threadId, selectBy) {
630
+ async _getIncludedMessages(threadId, include) {
805
631
  if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
806
- const include = selectBy?.include;
807
632
  if (!include) return null;
808
633
  const unionQueries = [];
809
634
  const params = [];
@@ -859,74 +684,8 @@ var MemoryStorageD1 = class extends MemoryStorage {
859
684
  });
860
685
  return processedMessages;
861
686
  }
862
- async getMessages({
863
- threadId,
864
- resourceId,
865
- selectBy,
866
- format
867
- }) {
868
- try {
869
- if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
870
- const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
871
- const limit = resolveMessageLimit({
872
- last: selectBy?.last,
873
- defaultLimit: 40
874
- });
875
- const include = selectBy?.include || [];
876
- const messages = [];
877
- if (include.length) {
878
- const includeResult = await this._getIncludedMessages(threadId, selectBy);
879
- if (Array.isArray(includeResult)) messages.push(...includeResult);
880
- }
881
- const excludeIds = messages.map((m) => m.id);
882
- const query = createSqlBuilder().select(["id", "content", "role", "type", "createdAt", "thread_id AS threadId"]).from(fullTableName).where("thread_id = ?", threadId);
883
- if (excludeIds.length > 0) {
884
- query.andWhere(`id NOT IN (${excludeIds.map(() => "?").join(",")})`, ...excludeIds);
885
- }
886
- query.orderBy("createdAt", "DESC").limit(limit);
887
- const { sql, params } = query.build();
888
- const result = await this.operations.executeQuery({ sql, params });
889
- if (Array.isArray(result)) messages.push(...result);
890
- messages.sort((a, b) => {
891
- const aRecord = a;
892
- const bRecord = b;
893
- const timeA = new Date(aRecord.createdAt).getTime();
894
- const timeB = new Date(bRecord.createdAt).getTime();
895
- return timeA - timeB;
896
- });
897
- const processedMessages = messages.map((message) => {
898
- const processedMsg = {};
899
- for (const [key, value] of Object.entries(message)) {
900
- if (key === `type` && value === `v2`) continue;
901
- processedMsg[key] = deserializeValue(value);
902
- }
903
- return processedMsg;
904
- });
905
- this.logger.debug(`Retrieved ${messages.length} messages for thread ${threadId}`);
906
- const list = new MessageList().add(processedMessages, "memory");
907
- if (format === `v2`) return list.get.all.v2();
908
- return list.get.all.v1();
909
- } catch (error) {
910
- const mastraError = new MastraError(
911
- {
912
- id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_ERROR",
913
- domain: ErrorDomain.STORAGE,
914
- category: ErrorCategory.THIRD_PARTY,
915
- text: `Failed to retrieve messages for thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
916
- details: { threadId, resourceId: resourceId ?? "" }
917
- },
918
- error
919
- );
920
- this.logger?.error(mastraError.toString());
921
- this.logger?.trackException(mastraError);
922
- throw mastraError;
923
- }
924
- }
925
- async getMessagesById({
926
- messageIds,
927
- format
928
- }) {
929
- if (messageIds.length === 0) return [];
687
+ async listMessagesById({ messageIds }) {
688
+ if (messageIds.length === 0) return { messages: [] };
930
689
  const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
931
690
  const messages = [];
932
691
  try {
@@ -945,12 +704,11 @@ var MemoryStorageD1 = class extends MemoryStorage {
945
704
  });
946
705
  this.logger.debug(`Retrieved ${messages.length} messages`);
947
706
  const list = new MessageList().add(processedMessages, "memory");
948
- if (format === `v1`) return list.get.all.v1();
949
- return list.get.all.v2();
707
+ return { messages: list.get.all.db() };
950
708
  } catch (error) {
951
709
  const mastraError = new MastraError(
952
710
  {
953
- id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_BY_ID_ERROR",
711
+ id: "CLOUDFLARE_D1_STORAGE_LIST_MESSAGES_BY_ID_ERROR",
954
712
  domain: ErrorDomain.STORAGE,
955
713
  category: ErrorCategory.THIRD_PARTY,
956
714
  text: `Failed to retrieve messages by ID: ${error instanceof Error ? error.message : String(error)}`,
@@ -963,118 +721,157 @@ var MemoryStorageD1 = class extends MemoryStorage {
963
721
  throw mastraError;
964
722
  }
965
723
  }
966
- async getMessagesPaginated({
967
- threadId,
968
- resourceId,
969
- selectBy,
970
- format
971
- }) {
972
- const { dateRange, page = 0, perPage: perPageInput } = selectBy?.pagination || {};
973
- const { start: fromDate, end: toDate } = dateRange || {};
974
- const perPage = perPageInput !== void 0 ? perPageInput : resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
975
- const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
976
- const messages = [];
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);
977
750
  try {
978
- if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
979
- if (selectBy?.include?.length) {
980
- const includeResult = await this._getIncludedMessages(threadId, selectBy);
981
- if (Array.isArray(includeResult)) messages.push(...includeResult);
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);
982
761
  }
983
- const countQuery = createSqlBuilder().count().from(fullTableName).where("thread_id = ?", threadId);
984
- if (fromDate) {
985
- 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);
986
767
  }
987
- if (toDate) {
988
- 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);
989
772
  }
990
- 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 });
991
806
  const total = Number(countResult[0]?.count ?? 0);
992
- if (total === 0 && messages.length === 0) {
807
+ if (total === 0 && paginatedCount === 0 && (!include || include.length === 0)) {
993
808
  return {
994
809
  messages: [],
995
810
  total: 0,
996
811
  page,
997
- perPage,
812
+ perPage: perPageForResponse,
998
813
  hasMore: false
999
814
  };
1000
815
  }
1001
- const excludeIds = messages.map((m) => m.id);
1002
- const excludeCondition = excludeIds.length > 0 ? `AND id NOT IN (${excludeIds.map(() => "?").join(",")})` : "";
1003
- let query;
1004
- let queryParams = [threadId];
1005
- if (fromDate) {
1006
- queryParams.push(serializeDate(fromDate));
1007
- }
1008
- if (toDate) {
1009
- queryParams.push(serializeDate(toDate));
1010
- }
1011
- if (excludeIds.length > 0) {
1012
- queryParams.push(...excludeIds);
1013
- }
1014
- if (selectBy?.last && selectBy.last > 0) {
1015
- query = `
1016
- SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
1017
- FROM ${fullTableName}
1018
- WHERE thread_id = ?
1019
- ${fromDate ? "AND createdAt >= ?" : ""}
1020
- ${toDate ? "AND createdAt <= ?" : ""}
1021
- ${excludeCondition}
1022
- ORDER BY createdAt DESC
1023
- LIMIT ?
1024
- `;
1025
- queryParams.push(selectBy.last);
1026
- } else {
1027
- query = `
1028
- SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
1029
- FROM ${fullTableName}
1030
- WHERE thread_id = ?
1031
- ${fromDate ? "AND createdAt >= ?" : ""}
1032
- ${toDate ? "AND createdAt <= ?" : ""}
1033
- ${excludeCondition}
1034
- ORDER BY createdAt DESC
1035
- LIMIT ? OFFSET ?
1036
- `;
1037
- queryParams.push(perPage, page * perPage);
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
+ }
1038
829
  }
1039
- const results = await this.operations.executeQuery({ sql: query, params: queryParams });
1040
- const processedMessages = results.map((message) => {
1041
- const processedMsg = {};
1042
- for (const [key, value] of Object.entries(message)) {
1043
- if (key === `type` && value === `v2`) continue;
1044
- processedMsg[key] = deserializeValue(value);
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);
1045
838
  }
1046
- 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));
1047
843
  });
1048
- if (selectBy?.last) {
1049
- processedMessages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
1050
- }
1051
- const list = new MessageList().add(processedMessages, "memory");
1052
- messages.push(...format === `v2` ? list.get.all.v2() : list.get.all.v1());
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;
1053
847
  return {
1054
- messages,
848
+ messages: finalMessages,
1055
849
  total,
1056
850
  page,
1057
- perPage,
1058
- hasMore: selectBy?.last ? false : page * perPage + messages.length < total
851
+ perPage: perPageForResponse,
852
+ hasMore
1059
853
  };
1060
854
  } catch (error) {
1061
855
  const mastraError = new MastraError(
1062
856
  {
1063
- id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_PAGINATED_ERROR",
857
+ id: "CLOUDFLARE_D1_STORAGE_LIST_MESSAGES_ERROR",
1064
858
  domain: ErrorDomain.STORAGE,
1065
859
  category: ErrorCategory.THIRD_PARTY,
1066
- text: `Failed to retrieve messages for thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
1067
- details: { threadId, resourceId: resourceId ?? "" }
860
+ text: `Failed to list messages for thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
861
+ details: {
862
+ threadId,
863
+ resourceId: resourceId ?? ""
864
+ }
1068
865
  },
1069
866
  error
1070
867
  );
1071
- this.logger?.error(mastraError.toString());
1072
- this.logger?.trackException(mastraError);
868
+ this.logger?.error?.(mastraError.toString());
869
+ this.logger?.trackException?.(mastraError);
1073
870
  return {
1074
871
  messages: [],
1075
872
  total: 0,
1076
873
  page,
1077
- perPage,
874
+ perPage: perPageForResponse,
1078
875
  hasMore: false
1079
876
  };
1080
877
  }
@@ -1575,7 +1372,7 @@ function transformScoreRow(row) {
1575
1372
  deserialized.analyzeStepResult = safelyParseJSON(row.analyzeStepResult);
1576
1373
  deserialized.metadata = safelyParseJSON(row.metadata);
1577
1374
  deserialized.additionalContext = safelyParseJSON(row.additionalContext);
1578
- deserialized.runtimeContext = safelyParseJSON(row.runtimeContext);
1375
+ deserialized.requestContext = safelyParseJSON(row.requestContext);
1579
1376
  deserialized.entity = safelyParseJSON(row.entity);
1580
1377
  deserialized.createdAt = row.createdAtZ || row.createdAt;
1581
1378
  deserialized.updatedAt = row.updatedAtZ || row.updatedAt;
@@ -1609,12 +1406,25 @@ var ScoresStorageD1 = class extends ScoresStorage {
1609
1406
  }
1610
1407
  }
1611
1408
  async saveScore(score) {
1409
+ let parsedScore;
1410
+ try {
1411
+ parsedScore = saveScorePayloadSchema.parse(score);
1412
+ } catch (error) {
1413
+ throw new MastraError(
1414
+ {
1415
+ id: "CLOUDFLARE_D1_STORE_SCORES_SAVE_SCORE_FAILED_INVALID_SCORE_PAYLOAD",
1416
+ domain: ErrorDomain.STORAGE,
1417
+ category: ErrorCategory.USER,
1418
+ details: { scoreId: score.id }
1419
+ },
1420
+ error
1421
+ );
1422
+ }
1612
1423
  try {
1613
1424
  const id = crypto.randomUUID();
1614
1425
  const fullTableName = this.operations.getTableName(TABLE_SCORERS);
1615
- const { input, ...rest } = score;
1616
1426
  const serializedRecord = {};
1617
- for (const [key, value] of Object.entries(rest)) {
1427
+ for (const [key, value] of Object.entries(parsedScore)) {
1618
1428
  if (value !== null && value !== void 0) {
1619
1429
  if (typeof value === "object") {
1620
1430
  serializedRecord[key] = JSON.stringify(value);
@@ -1626,7 +1436,6 @@ var ScoresStorageD1 = class extends ScoresStorage {
1626
1436
  }
1627
1437
  }
1628
1438
  serializedRecord.id = id;
1629
- serializedRecord.input = JSON.stringify(input);
1630
1439
  serializedRecord.createdAt = (/* @__PURE__ */ new Date()).toISOString();
1631
1440
  serializedRecord.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
1632
1441
  const columns = Object.keys(serializedRecord);
@@ -1647,7 +1456,7 @@ var ScoresStorageD1 = class extends ScoresStorage {
1647
1456
  );
1648
1457
  }
1649
1458
  }
1650
- async getScoresByScorerId({
1459
+ async listScoresByScorerId({
1651
1460
  scorerId,
1652
1461
  entityId,
1653
1462
  entityType,
@@ -1655,6 +1464,9 @@ var ScoresStorageD1 = class extends ScoresStorage {
1655
1464
  pagination
1656
1465
  }) {
1657
1466
  try {
1467
+ const { page, perPage: perPageInput } = pagination;
1468
+ const perPage = normalizePerPage(perPageInput, 100);
1469
+ const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1658
1470
  const fullTableName = this.operations.getTableName(TABLE_SCORERS);
1659
1471
  const countQuery = createSqlBuilder().count().from(fullTableName).where("scorerId = ?", scorerId);
1660
1472
  if (entityId) {
@@ -1672,13 +1484,15 @@ var ScoresStorageD1 = class extends ScoresStorage {
1672
1484
  return {
1673
1485
  pagination: {
1674
1486
  total: 0,
1675
- page: pagination.page,
1676
- perPage: pagination.perPage,
1487
+ page,
1488
+ perPage: perPageForResponse,
1677
1489
  hasMore: false
1678
1490
  },
1679
1491
  scores: []
1680
1492
  };
1681
1493
  }
1494
+ const end = perPageInput === false ? total : start + perPage;
1495
+ const limitValue = perPageInput === false ? total : perPage;
1682
1496
  const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("scorerId = ?", scorerId);
1683
1497
  if (entityId) {
1684
1498
  selectQuery.andWhere("entityId = ?", entityId);
@@ -1689,16 +1503,16 @@ var ScoresStorageD1 = class extends ScoresStorage {
1689
1503
  if (source) {
1690
1504
  selectQuery.andWhere("source = ?", source);
1691
1505
  }
1692
- selectQuery.limit(pagination.perPage).offset(pagination.page * pagination.perPage);
1506
+ selectQuery.limit(limitValue).offset(start);
1693
1507
  const { sql, params } = selectQuery.build();
1694
1508
  const results = await this.operations.executeQuery({ sql, params });
1695
1509
  const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
1696
1510
  return {
1697
1511
  pagination: {
1698
1512
  total,
1699
- page: pagination.page,
1700
- perPage: pagination.perPage,
1701
- hasMore: total > (pagination.page + 1) * pagination.perPage
1513
+ page,
1514
+ perPage: perPageForResponse,
1515
+ hasMore: end < total
1702
1516
  },
1703
1517
  scores
1704
1518
  };
@@ -1713,11 +1527,14 @@ var ScoresStorageD1 = class extends ScoresStorage {
1713
1527
  );
1714
1528
  }
1715
1529
  }
1716
- async getScoresByRunId({
1530
+ async listScoresByRunId({
1717
1531
  runId,
1718
1532
  pagination
1719
1533
  }) {
1720
1534
  try {
1535
+ const { page, perPage: perPageInput } = pagination;
1536
+ const perPage = normalizePerPage(perPageInput, 100);
1537
+ const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1721
1538
  const fullTableName = this.operations.getTableName(TABLE_SCORERS);
1722
1539
  const countQuery = createSqlBuilder().count().from(fullTableName).where("runId = ?", runId);
1723
1540
  const countResult = await this.operations.executeQuery(countQuery.build());
@@ -1726,23 +1543,25 @@ var ScoresStorageD1 = class extends ScoresStorage {
1726
1543
  return {
1727
1544
  pagination: {
1728
1545
  total: 0,
1729
- page: pagination.page,
1730
- perPage: pagination.perPage,
1546
+ page,
1547
+ perPage: perPageForResponse,
1731
1548
  hasMore: false
1732
1549
  },
1733
1550
  scores: []
1734
1551
  };
1735
1552
  }
1736
- 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);
1737
1556
  const { sql, params } = selectQuery.build();
1738
1557
  const results = await this.operations.executeQuery({ sql, params });
1739
1558
  const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
1740
1559
  return {
1741
1560
  pagination: {
1742
1561
  total,
1743
- page: pagination.page,
1744
- perPage: pagination.perPage,
1745
- hasMore: total > (pagination.page + 1) * pagination.perPage
1562
+ page,
1563
+ perPage: perPageForResponse,
1564
+ hasMore: end < total
1746
1565
  },
1747
1566
  scores
1748
1567
  };
@@ -1757,12 +1576,15 @@ var ScoresStorageD1 = class extends ScoresStorage {
1757
1576
  );
1758
1577
  }
1759
1578
  }
1760
- async getScoresByEntityId({
1579
+ async listScoresByEntityId({
1761
1580
  entityId,
1762
1581
  entityType,
1763
1582
  pagination
1764
1583
  }) {
1765
1584
  try {
1585
+ const { page, perPage: perPageInput } = pagination;
1586
+ const perPage = normalizePerPage(perPageInput, 100);
1587
+ const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1766
1588
  const fullTableName = this.operations.getTableName(TABLE_SCORERS);
1767
1589
  const countQuery = createSqlBuilder().count().from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType);
1768
1590
  const countResult = await this.operations.executeQuery(countQuery.build());
@@ -1771,23 +1593,25 @@ var ScoresStorageD1 = class extends ScoresStorage {
1771
1593
  return {
1772
1594
  pagination: {
1773
1595
  total: 0,
1774
- page: pagination.page,
1775
- perPage: pagination.perPage,
1596
+ page,
1597
+ perPage: perPageForResponse,
1776
1598
  hasMore: false
1777
1599
  },
1778
1600
  scores: []
1779
1601
  };
1780
1602
  }
1781
- 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);
1782
1606
  const { sql, params } = selectQuery.build();
1783
1607
  const results = await this.operations.executeQuery({ sql, params });
1784
1608
  const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
1785
1609
  return {
1786
1610
  pagination: {
1787
1611
  total,
1788
- page: pagination.page,
1789
- perPage: pagination.perPage,
1790
- hasMore: total > (pagination.page + 1) * pagination.perPage
1612
+ page,
1613
+ perPage: perPageForResponse,
1614
+ hasMore: end < total
1791
1615
  },
1792
1616
  scores
1793
1617
  };
@@ -1802,128 +1626,56 @@ var ScoresStorageD1 = class extends ScoresStorage {
1802
1626
  );
1803
1627
  }
1804
1628
  }
1805
- };
1806
- function isArrayOfRecords2(value) {
1807
- return value && Array.isArray(value) && value.length > 0;
1808
- }
1809
- var TracesStorageD1 = class extends TracesStorage {
1810
- operations;
1811
- constructor({ operations }) {
1812
- super();
1813
- this.operations = operations;
1814
- }
1815
- async getTraces(args) {
1816
- const paginatedArgs = {
1817
- name: args.name,
1818
- scope: args.scope,
1819
- page: args.page,
1820
- perPage: args.perPage,
1821
- attributes: args.attributes,
1822
- filters: args.filters,
1823
- dateRange: args.fromDate || args.toDate ? {
1824
- start: args.fromDate,
1825
- end: args.toDate
1826
- } : void 0
1827
- };
1828
- try {
1829
- const result = await this.getTracesPaginated(paginatedArgs);
1830
- return result.traces;
1831
- } catch (error) {
1832
- throw new MastraError(
1833
- {
1834
- id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_ERROR",
1835
- domain: ErrorDomain.STORAGE,
1836
- category: ErrorCategory.THIRD_PARTY,
1837
- text: `Failed to retrieve traces: ${error instanceof Error ? error.message : String(error)}`,
1838
- details: {
1839
- name: args.name ?? "",
1840
- scope: args.scope ?? ""
1841
- }
1842
- },
1843
- error
1844
- );
1845
- }
1846
- }
1847
- async getTracesPaginated(args) {
1848
- const { name, scope, page = 0, perPage = 100, attributes, dateRange } = args;
1849
- const fromDate = dateRange?.start;
1850
- const toDate = dateRange?.end;
1851
- const fullTableName = this.operations.getTableName(TABLE_TRACES);
1629
+ async listScoresBySpan({
1630
+ traceId,
1631
+ spanId,
1632
+ pagination
1633
+ }) {
1852
1634
  try {
1853
- const dataQuery = createSqlBuilder().select("*").from(fullTableName).where("1=1");
1854
- const countQuery = createSqlBuilder().count().from(fullTableName).where("1=1");
1855
- if (name) {
1856
- dataQuery.andWhere("name LIKE ?", `%${name}%`);
1857
- countQuery.andWhere("name LIKE ?", `%${name}%`);
1858
- }
1859
- if (scope) {
1860
- dataQuery.andWhere("scope = ?", scope);
1861
- countQuery.andWhere("scope = ?", scope);
1862
- }
1863
- if (attributes && Object.keys(attributes).length > 0) {
1864
- for (const [key, value] of Object.entries(attributes)) {
1865
- dataQuery.jsonLike("attributes", key, value);
1866
- countQuery.jsonLike("attributes", key, value);
1867
- }
1868
- }
1869
- if (fromDate) {
1870
- const fromDateStr = fromDate instanceof Date ? fromDate.toISOString() : fromDate;
1871
- dataQuery.andWhere("createdAt >= ?", fromDateStr);
1872
- countQuery.andWhere("createdAt >= ?", fromDateStr);
1873
- }
1874
- if (toDate) {
1875
- const toDateStr = toDate instanceof Date ? toDate.toISOString() : toDate;
1876
- dataQuery.andWhere("createdAt <= ?", toDateStr);
1877
- countQuery.andWhere("createdAt <= ?", toDateStr);
1878
- }
1879
- const allDataResult = await this.operations.executeQuery(
1880
- createSqlBuilder().select("*").from(fullTableName).where("1=1").build()
1881
- );
1882
- console.log("allDataResult", allDataResult);
1635
+ const { page, perPage: perPageInput } = pagination;
1636
+ const perPage = normalizePerPage(perPageInput, 100);
1637
+ const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1638
+ const fullTableName = this.operations.getTableName(TABLE_SCORERS);
1639
+ const countQuery = createSqlBuilder().count().from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId);
1883
1640
  const countResult = await this.operations.executeQuery(countQuery.build());
1884
- const total = Number(countResult?.[0]?.count ?? 0);
1885
- dataQuery.orderBy("startTime", "DESC").limit(perPage).offset(page * perPage);
1886
- const results = await this.operations.executeQuery(dataQuery.build());
1887
- const traces = isArrayOfRecords2(results) ? results.map(
1888
- (trace) => ({
1889
- ...trace,
1890
- attributes: deserializeValue(trace.attributes, "jsonb"),
1891
- status: deserializeValue(trace.status, "jsonb"),
1892
- events: deserializeValue(trace.events, "jsonb"),
1893
- links: deserializeValue(trace.links, "jsonb"),
1894
- other: deserializeValue(trace.other, "jsonb")
1895
- })
1896
- ) : [];
1641
+ const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
1642
+ if (total === 0) {
1643
+ return {
1644
+ pagination: {
1645
+ total: 0,
1646
+ page,
1647
+ perPage: perPageForResponse,
1648
+ hasMore: false
1649
+ },
1650
+ scores: []
1651
+ };
1652
+ }
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);
1656
+ const { sql, params } = selectQuery.build();
1657
+ const results = await this.operations.executeQuery({ sql, params });
1658
+ const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
1897
1659
  return {
1898
- traces,
1899
- total,
1900
- page,
1901
- perPage,
1902
- hasMore: page * perPage + traces.length < total
1660
+ pagination: {
1661
+ total,
1662
+ page,
1663
+ perPage: perPageForResponse,
1664
+ hasMore: end < total
1665
+ },
1666
+ scores
1903
1667
  };
1904
1668
  } catch (error) {
1905
- const mastraError = new MastraError(
1669
+ throw new MastraError(
1906
1670
  {
1907
- id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_PAGINATED_ERROR",
1671
+ id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_SPAN_FAILED",
1908
1672
  domain: ErrorDomain.STORAGE,
1909
- category: ErrorCategory.THIRD_PARTY,
1910
- text: `Failed to retrieve traces: ${error instanceof Error ? error.message : String(error)}`,
1911
- details: { name: name ?? "", scope: scope ?? "" }
1673
+ category: ErrorCategory.THIRD_PARTY
1912
1674
  },
1913
1675
  error
1914
1676
  );
1915
- this.logger?.error(mastraError.toString());
1916
- this.logger?.trackException(mastraError);
1917
- return { traces: [], total: 0, page, perPage, hasMore: false };
1918
1677
  }
1919
1678
  }
1920
- async batchTraceInsert({ records }) {
1921
- this.logger.debug("Batch inserting traces", { count: records.length });
1922
- await this.operations.batchInsert({
1923
- tableName: TABLE_TRACES,
1924
- records
1925
- });
1926
- }
1927
1679
  };
1928
1680
  var WorkflowsStorageD1 = class extends WorkflowsStorage {
1929
1681
  operations;
@@ -1936,7 +1688,7 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
1936
1688
  // runId,
1937
1689
  // stepId,
1938
1690
  // result,
1939
- // runtimeContext,
1691
+ // requestContext,
1940
1692
  }) {
1941
1693
  throw new Error("Method not implemented.");
1942
1694
  }
@@ -1950,6 +1702,7 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
1950
1702
  async persistWorkflowSnapshot({
1951
1703
  workflowName,
1952
1704
  runId,
1705
+ resourceId,
1953
1706
  snapshot
1954
1707
  }) {
1955
1708
  const fullTableName = this.operations.getTableName(TABLE_WORKFLOW_SNAPSHOT);
@@ -1960,11 +1713,13 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
1960
1713
  });
1961
1714
  const persisting = currentSnapshot ? {
1962
1715
  ...currentSnapshot,
1716
+ resourceId,
1963
1717
  snapshot: JSON.stringify(snapshot),
1964
1718
  updatedAt: now
1965
1719
  } : {
1966
1720
  workflow_name: workflowName,
1967
1721
  run_id: runId,
1722
+ resourceId,
1968
1723
  snapshot,
1969
1724
  createdAt: now,
1970
1725
  updatedAt: now
@@ -2037,19 +1792,24 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
2037
1792
  resourceId: row.resourceId
2038
1793
  };
2039
1794
  }
2040
- async getWorkflowRuns({
1795
+ async listWorkflowRuns({
2041
1796
  workflowName,
2042
1797
  fromDate,
2043
1798
  toDate,
2044
- limit,
2045
- offset,
2046
- resourceId
1799
+ page,
1800
+ perPage,
1801
+ resourceId,
1802
+ status
2047
1803
  } = {}) {
2048
1804
  const fullTableName = this.operations.getTableName(TABLE_WORKFLOW_SNAPSHOT);
2049
1805
  try {
2050
1806
  const builder = createSqlBuilder().select().from(fullTableName);
2051
1807
  const countBuilder = createSqlBuilder().count().from(fullTableName);
2052
1808
  if (workflowName) builder.whereAnd("workflow_name = ?", workflowName);
1809
+ if (status) {
1810
+ builder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
1811
+ countBuilder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
1812
+ }
2053
1813
  if (resourceId) {
2054
1814
  const hasResourceId = await this.operations.hasColumn(fullTableName, "resourceId");
2055
1815
  if (hasResourceId) {
@@ -2068,11 +1828,14 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
2068
1828
  countBuilder.whereAnd("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
2069
1829
  }
2070
1830
  builder.orderBy("createdAt", "DESC");
2071
- if (typeof limit === "number") builder.limit(limit);
2072
- if (typeof offset === "number") builder.offset(offset);
1831
+ if (typeof perPage === "number" && typeof page === "number") {
1832
+ const offset = page * perPage;
1833
+ builder.limit(perPage);
1834
+ builder.offset(offset);
1835
+ }
2073
1836
  const { sql, params } = builder.build();
2074
1837
  let total = 0;
2075
- if (limit !== void 0 && offset !== void 0) {
1838
+ if (perPage !== void 0 && page !== void 0) {
2076
1839
  const { sql: countSql, params: countParams } = countBuilder.build();
2077
1840
  const countResult = await this.operations.executeQuery({
2078
1841
  sql: countSql,
@@ -2087,7 +1850,7 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
2087
1850
  } catch (error) {
2088
1851
  throw new MastraError(
2089
1852
  {
2090
- id: "CLOUDFLARE_D1_STORAGE_GET_WORKFLOW_RUNS_ERROR",
1853
+ id: "CLOUDFLARE_D1_STORAGE_LIST_WORKFLOW_RUNS_ERROR",
2091
1854
  domain: ErrorDomain.STORAGE,
2092
1855
  category: ErrorCategory.THIRD_PARTY,
2093
1856
  text: `Failed to retrieve workflow runs: ${error instanceof Error ? error.message : String(error)}`,
@@ -2149,7 +1912,7 @@ var D1Store = class extends MastraStorage {
2149
1912
  */
2150
1913
  constructor(config) {
2151
1914
  try {
2152
- super({ name: "D1" });
1915
+ super({ id: config.id, name: "D1" });
2153
1916
  if (config.tablePrefix && !/^[a-zA-Z0-9_]*$/.test(config.tablePrefix)) {
2154
1917
  throw new Error("Invalid tablePrefix: only letters, numbers, and underscores are allowed.");
2155
1918
  }
@@ -2203,12 +1966,6 @@ var D1Store = class extends MastraStorage {
2203
1966
  const scores = new ScoresStorageD1({
2204
1967
  operations
2205
1968
  });
2206
- const legacyEvals = new LegacyEvalsStorageD1({
2207
- operations
2208
- });
2209
- const traces = new TracesStorageD1({
2210
- operations
2211
- });
2212
1969
  const workflows = new WorkflowsStorageD1({
2213
1970
  operations
2214
1971
  });
@@ -2218,8 +1975,6 @@ var D1Store = class extends MastraStorage {
2218
1975
  this.stores = {
2219
1976
  operations,
2220
1977
  scores,
2221
- legacyEvals,
2222
- traces,
2223
1978
  workflows,
2224
1979
  memory
2225
1980
  };
@@ -2230,7 +1985,8 @@ var D1Store = class extends MastraStorage {
2230
1985
  resourceWorkingMemory: true,
2231
1986
  hasColumn: true,
2232
1987
  createTable: true,
2233
- deleteMessages: false
1988
+ deleteMessages: false,
1989
+ listScoresBySpan: true
2234
1990
  };
2235
1991
  }
2236
1992
  async createTable({
@@ -2270,15 +2026,6 @@ var D1Store = class extends MastraStorage {
2270
2026
  async getThreadById({ threadId }) {
2271
2027
  return this.stores.memory.getThreadById({ threadId });
2272
2028
  }
2273
- /**
2274
- * @deprecated use getThreadsByResourceIdPaginated instead
2275
- */
2276
- async getThreadsByResourceId({ resourceId }) {
2277
- return this.stores.memory.getThreadsByResourceId({ resourceId });
2278
- }
2279
- async getThreadsByResourceIdPaginated(args) {
2280
- return this.stores.memory.getThreadsByResourceIdPaginated(args);
2281
- }
2282
2029
  async saveThread({ thread }) {
2283
2030
  return this.stores.memory.saveThread({ thread });
2284
2031
  }
@@ -2295,34 +2042,14 @@ var D1Store = class extends MastraStorage {
2295
2042
  async saveMessages(args) {
2296
2043
  return this.stores.memory.saveMessages(args);
2297
2044
  }
2298
- async getMessages({
2299
- threadId,
2300
- selectBy,
2301
- format
2302
- }) {
2303
- return this.stores.memory.getMessages({ threadId, selectBy, format });
2304
- }
2305
- async getMessagesById({
2306
- messageIds,
2307
- format
2308
- }) {
2309
- return this.stores.memory.getMessagesById({ messageIds, format });
2310
- }
2311
- async getMessagesPaginated({
2312
- threadId,
2313
- selectBy,
2314
- format
2315
- }) {
2316
- return this.stores.memory.getMessagesPaginated({ threadId, selectBy, format });
2317
- }
2318
2045
  async updateWorkflowResults({
2319
2046
  workflowName,
2320
2047
  runId,
2321
2048
  stepId,
2322
2049
  result,
2323
- runtimeContext
2050
+ requestContext
2324
2051
  }) {
2325
- return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, runtimeContext });
2052
+ return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
2326
2053
  }
2327
2054
  async updateWorkflowState({
2328
2055
  workflowName,
@@ -2334,22 +2061,16 @@ var D1Store = class extends MastraStorage {
2334
2061
  async persistWorkflowSnapshot({
2335
2062
  workflowName,
2336
2063
  runId,
2064
+ resourceId,
2337
2065
  snapshot
2338
2066
  }) {
2339
- return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, snapshot });
2067
+ return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot });
2340
2068
  }
2341
2069
  async loadWorkflowSnapshot(params) {
2342
2070
  return this.stores.workflows.loadWorkflowSnapshot(params);
2343
2071
  }
2344
- async getWorkflowRuns({
2345
- workflowName,
2346
- fromDate,
2347
- toDate,
2348
- limit,
2349
- offset,
2350
- resourceId
2351
- } = {}) {
2352
- return this.stores.workflows.getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId });
2072
+ async listWorkflowRuns(args = {}) {
2073
+ return this.stores.workflows.listWorkflowRuns(args);
2353
2074
  }
2354
2075
  async getWorkflowRunById({
2355
2076
  runId,
@@ -2365,24 +2086,6 @@ var D1Store = class extends MastraStorage {
2365
2086
  async batchInsert({ tableName, records }) {
2366
2087
  return this.stores.operations.batchInsert({ tableName, records });
2367
2088
  }
2368
- /**
2369
- * @deprecated use getTracesPaginated instead
2370
- */
2371
- async getTraces(args) {
2372
- return this.stores.traces.getTraces(args);
2373
- }
2374
- async getTracesPaginated(args) {
2375
- return this.stores.traces.getTracesPaginated(args);
2376
- }
2377
- /**
2378
- * @deprecated use getEvals instead
2379
- */
2380
- async getEvalsByAgentName(agentName, type) {
2381
- return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
2382
- }
2383
- async getEvals(options) {
2384
- return this.stores.legacyEvals.getEvals(options);
2385
- }
2386
2089
  async updateMessages(_args) {
2387
2090
  return this.stores.memory.updateMessages(_args);
2388
2091
  }
@@ -2405,31 +2108,38 @@ var D1Store = class extends MastraStorage {
2405
2108
  async saveScore(_score) {
2406
2109
  return this.stores.scores.saveScore(_score);
2407
2110
  }
2408
- async getScoresByRunId({
2111
+ async listScoresByRunId({
2409
2112
  runId: _runId,
2410
2113
  pagination: _pagination
2411
2114
  }) {
2412
- return this.stores.scores.getScoresByRunId({ runId: _runId, pagination: _pagination });
2115
+ return this.stores.scores.listScoresByRunId({ runId: _runId, pagination: _pagination });
2413
2116
  }
2414
- async getScoresByEntityId({
2117
+ async listScoresByEntityId({
2415
2118
  entityId: _entityId,
2416
2119
  entityType: _entityType,
2417
2120
  pagination: _pagination
2418
2121
  }) {
2419
- return this.stores.scores.getScoresByEntityId({
2122
+ return this.stores.scores.listScoresByEntityId({
2420
2123
  entityId: _entityId,
2421
2124
  entityType: _entityType,
2422
2125
  pagination: _pagination
2423
2126
  });
2424
2127
  }
2425
- async getScoresByScorerId({
2128
+ async listScoresByScorerId({
2426
2129
  scorerId,
2427
2130
  pagination,
2428
2131
  entityId,
2429
2132
  entityType,
2430
2133
  source
2431
2134
  }) {
2432
- return this.stores.scores.getScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
2135
+ return this.stores.scores.listScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
2136
+ }
2137
+ async listScoresBySpan({
2138
+ traceId,
2139
+ spanId,
2140
+ pagination
2141
+ }) {
2142
+ return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
2433
2143
  }
2434
2144
  /**
2435
2145
  * Close the database connection