@mastra/cloudflare-d1 0.12.5 → 0.12.6

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,5 +1,5 @@
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 } from '@mastra/core/storage';
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';
3
3
  import Cloudflare from 'cloudflare';
4
4
  import { parseSqlIdentifier } from '@mastra/core/utils';
5
5
  import { MessageList } from '@mastra/core/agent';
@@ -1437,6 +1437,7 @@ var StoreOperationsD1 = class extends StoreOperations {
1437
1437
  query.andWhere(`${key} = ?`, value);
1438
1438
  }
1439
1439
  }
1440
+ query.orderBy("createdAt", "DESC");
1440
1441
  query.limit(1);
1441
1442
  const { sql, params } = query.build();
1442
1443
  const result = await this.executeQuery({ sql, params, first: true });
@@ -1521,20 +1522,19 @@ var StoreOperationsD1 = class extends StoreOperations {
1521
1522
  }
1522
1523
  };
1523
1524
  function transformScoreRow(row) {
1524
- let input = void 0;
1525
- if (row.input) {
1526
- try {
1527
- input = JSON.parse(row.input);
1528
- } catch {
1529
- input = row.input;
1530
- }
1531
- }
1532
- return {
1533
- ...row,
1534
- input,
1535
- createdAt: row.createdAtZ || row.createdAt,
1536
- updatedAt: row.updatedAtZ || row.updatedAt
1537
- };
1525
+ const deserialized = { ...row };
1526
+ deserialized.input = safelyParseJSON(row.input);
1527
+ deserialized.output = safelyParseJSON(row.output);
1528
+ deserialized.scorer = safelyParseJSON(row.scorer);
1529
+ deserialized.preprocessStepResult = safelyParseJSON(row.preprocessStepResult);
1530
+ deserialized.analyzeStepResult = safelyParseJSON(row.analyzeStepResult);
1531
+ deserialized.metadata = safelyParseJSON(row.metadata);
1532
+ deserialized.additionalContext = safelyParseJSON(row.additionalContext);
1533
+ deserialized.runtimeContext = safelyParseJSON(row.runtimeContext);
1534
+ deserialized.entity = safelyParseJSON(row.entity);
1535
+ deserialized.createdAt = row.createdAtZ || row.createdAt;
1536
+ deserialized.updatedAt = row.updatedAtZ || row.updatedAt;
1537
+ return deserialized;
1538
1538
  }
1539
1539
  var ScoresStorageD1 = class extends ScoresStorage {
1540
1540
  operations;
@@ -1565,6 +1565,7 @@ var ScoresStorageD1 = class extends ScoresStorage {
1565
1565
  }
1566
1566
  async saveScore(score) {
1567
1567
  try {
1568
+ const id = crypto.randomUUID();
1568
1569
  const fullTableName = this.operations.getTableName(TABLE_SCORERS);
1569
1570
  const { input, ...rest } = score;
1570
1571
  const serializedRecord = {};
@@ -1579,6 +1580,7 @@ var ScoresStorageD1 = class extends ScoresStorage {
1579
1580
  serializedRecord[key] = null;
1580
1581
  }
1581
1582
  }
1583
+ serializedRecord.id = id;
1582
1584
  serializedRecord.input = JSON.stringify(input);
1583
1585
  serializedRecord.createdAt = (/* @__PURE__ */ new Date()).toISOString();
1584
1586
  serializedRecord.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
@@ -1587,7 +1589,7 @@ var ScoresStorageD1 = class extends ScoresStorage {
1587
1589
  const query = createSqlBuilder().insert(fullTableName, columns, values);
1588
1590
  const { sql, params } = query.build();
1589
1591
  await this.operations.executeQuery({ sql, params });
1590
- const scoreFromDb = await this.getScoreById({ id: score.id });
1592
+ const scoreFromDb = await this.getScoreById({ id });
1591
1593
  return { score: scoreFromDb };
1592
1594
  } catch (error) {
1593
1595
  throw new MastraError(
@@ -1602,11 +1604,23 @@ var ScoresStorageD1 = class extends ScoresStorage {
1602
1604
  }
1603
1605
  async getScoresByScorerId({
1604
1606
  scorerId,
1607
+ entityId,
1608
+ entityType,
1609
+ source,
1605
1610
  pagination
1606
1611
  }) {
1607
1612
  try {
1608
1613
  const fullTableName = this.operations.getTableName(TABLE_SCORERS);
1609
1614
  const countQuery = createSqlBuilder().count().from(fullTableName).where("scorerId = ?", scorerId);
1615
+ if (entityId) {
1616
+ countQuery.andWhere("entityId = ?", entityId);
1617
+ }
1618
+ if (entityType) {
1619
+ countQuery.andWhere("entityType = ?", entityType);
1620
+ }
1621
+ if (source) {
1622
+ countQuery.andWhere("source = ?", source);
1623
+ }
1610
1624
  const countResult = await this.operations.executeQuery(countQuery.build());
1611
1625
  const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
1612
1626
  if (total === 0) {
@@ -1620,7 +1634,17 @@ var ScoresStorageD1 = class extends ScoresStorage {
1620
1634
  scores: []
1621
1635
  };
1622
1636
  }
1623
- const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("scorerId = ?", scorerId).limit(pagination.perPage).offset(pagination.page * pagination.perPage);
1637
+ const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("scorerId = ?", scorerId);
1638
+ if (entityId) {
1639
+ selectQuery.andWhere("entityId = ?", entityId);
1640
+ }
1641
+ if (entityType) {
1642
+ selectQuery.andWhere("entityType = ?", entityType);
1643
+ }
1644
+ if (source) {
1645
+ selectQuery.andWhere("source = ?", source);
1646
+ }
1647
+ selectQuery.limit(pagination.perPage).offset(pagination.page * pagination.perPage);
1624
1648
  const { sql, params } = selectQuery.build();
1625
1649
  const results = await this.operations.executeQuery({ sql, params });
1626
1650
  const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
@@ -2316,10 +2340,13 @@ var D1Store = class extends MastraStorage {
2316
2340
  });
2317
2341
  }
2318
2342
  async getScoresByScorerId({
2319
- scorerId: _scorerId,
2320
- pagination: _pagination
2343
+ scorerId,
2344
+ pagination,
2345
+ entityId,
2346
+ entityType,
2347
+ source
2321
2348
  }) {
2322
- return this.stores.scores.getScoresByScorerId({ scorerId: _scorerId, pagination: _pagination });
2349
+ return this.stores.scores.getScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
2323
2350
  }
2324
2351
  /**
2325
2352
  * Close the database connection