@mastra/cloudflare-d1 0.13.2-alpha.0 → 0.13.3-alpha.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
@@ -3,6 +3,7 @@ import { MastraStorage, StoreOperations, ScoresStorage, TABLE_SCORERS, LegacyEva
3
3
  import Cloudflare from 'cloudflare';
4
4
  import { parseSqlIdentifier } from '@mastra/core/utils';
5
5
  import { MessageList } from '@mastra/core/agent';
6
+ import { saveScorePayloadSchema } from '@mastra/core/scores';
6
7
 
7
8
  // src/storage/index.ts
8
9
  var SqlBuilder = class {
@@ -1609,12 +1610,25 @@ var ScoresStorageD1 = class extends ScoresStorage {
1609
1610
  }
1610
1611
  }
1611
1612
  async saveScore(score) {
1613
+ let parsedScore;
1614
+ try {
1615
+ parsedScore = saveScorePayloadSchema.parse(score);
1616
+ } catch (error) {
1617
+ throw new MastraError(
1618
+ {
1619
+ id: "CLOUDFLARE_D1_STORE_SCORES_SAVE_SCORE_FAILED_INVALID_SCORE_PAYLOAD",
1620
+ domain: ErrorDomain.STORAGE,
1621
+ category: ErrorCategory.USER,
1622
+ details: { scoreId: score.id }
1623
+ },
1624
+ error
1625
+ );
1626
+ }
1612
1627
  try {
1613
1628
  const id = crypto.randomUUID();
1614
1629
  const fullTableName = this.operations.getTableName(TABLE_SCORERS);
1615
- const { input, ...rest } = score;
1616
1630
  const serializedRecord = {};
1617
- for (const [key, value] of Object.entries(rest)) {
1631
+ for (const [key, value] of Object.entries(parsedScore)) {
1618
1632
  if (value !== null && value !== void 0) {
1619
1633
  if (typeof value === "object") {
1620
1634
  serializedRecord[key] = JSON.stringify(value);
@@ -1626,7 +1640,6 @@ var ScoresStorageD1 = class extends ScoresStorage {
1626
1640
  }
1627
1641
  }
1628
1642
  serializedRecord.id = id;
1629
- serializedRecord.input = JSON.stringify(input);
1630
1643
  serializedRecord.createdAt = (/* @__PURE__ */ new Date()).toISOString();
1631
1644
  serializedRecord.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
1632
1645
  const columns = Object.keys(serializedRecord);
@@ -1802,6 +1815,53 @@ var ScoresStorageD1 = class extends ScoresStorage {
1802
1815
  );
1803
1816
  }
1804
1817
  }
1818
+ async getScoresBySpan({
1819
+ traceId,
1820
+ spanId,
1821
+ pagination
1822
+ }) {
1823
+ try {
1824
+ const fullTableName = this.operations.getTableName(TABLE_SCORERS);
1825
+ const countQuery = createSqlBuilder().count().from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId);
1826
+ const countResult = await this.operations.executeQuery(countQuery.build());
1827
+ const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
1828
+ if (total === 0) {
1829
+ return {
1830
+ pagination: {
1831
+ total: 0,
1832
+ page: pagination.page,
1833
+ perPage: pagination.perPage,
1834
+ hasMore: false
1835
+ },
1836
+ scores: []
1837
+ };
1838
+ }
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);
1841
+ const { sql, params } = selectQuery.build();
1842
+ 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);
1845
+ return {
1846
+ pagination: {
1847
+ total,
1848
+ page: pagination.page,
1849
+ perPage: pagination.perPage,
1850
+ hasMore: rows.length > pagination.perPage
1851
+ },
1852
+ scores
1853
+ };
1854
+ } catch (error) {
1855
+ throw new MastraError(
1856
+ {
1857
+ id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_SPAN_FAILED",
1858
+ domain: ErrorDomain.STORAGE,
1859
+ category: ErrorCategory.THIRD_PARTY
1860
+ },
1861
+ error
1862
+ );
1863
+ }
1864
+ }
1805
1865
  };
1806
1866
  function isArrayOfRecords2(value) {
1807
1867
  return value && Array.isArray(value) && value.length > 0;
@@ -2233,7 +2293,8 @@ var D1Store = class extends MastraStorage {
2233
2293
  resourceWorkingMemory: true,
2234
2294
  hasColumn: true,
2235
2295
  createTable: true,
2236
- deleteMessages: false
2296
+ deleteMessages: false,
2297
+ getScoresBySpan: true
2237
2298
  };
2238
2299
  }
2239
2300
  async createTable({
@@ -2435,6 +2496,13 @@ var D1Store = class extends MastraStorage {
2435
2496
  }) {
2436
2497
  return this.stores.scores.getScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
2437
2498
  }
2499
+ async getScoresBySpan({
2500
+ traceId,
2501
+ spanId,
2502
+ pagination
2503
+ }) {
2504
+ return this.stores.scores.getScoresBySpan({ traceId, spanId, pagination });
2505
+ }
2438
2506
  /**
2439
2507
  * Close the database connection
2440
2508
  * No explicit cleanup needed for D1 in either REST or Workers Binding mode