@mastra/cloudflare-d1 0.0.0-zod-v4-compat-part-2-20250820135355 → 0.0.0-zod-v4-stuff-20250825154219

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.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from './storage';
1
+ export * from './storage/index.js';
2
2
  //# sourceMappingURL=index.d.ts.map
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';
@@ -920,6 +920,47 @@ var MemoryStorageD1 = class extends MemoryStorage {
920
920
  throw mastraError;
921
921
  }
922
922
  }
923
+ async getMessagesById({
924
+ messageIds,
925
+ format
926
+ }) {
927
+ if (messageIds.length === 0) return [];
928
+ const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
929
+ const messages = [];
930
+ try {
931
+ const query = createSqlBuilder().select(["id", "content", "role", "type", "createdAt", "thread_id AS threadId", "resourceId"]).from(fullTableName).where(`id in (${messageIds.map(() => "?").join(",")})`, ...messageIds);
932
+ query.orderBy("createdAt", "DESC");
933
+ const { sql, params } = query.build();
934
+ const result = await this.operations.executeQuery({ sql, params });
935
+ if (Array.isArray(result)) messages.push(...result);
936
+ const processedMessages = messages.map((message) => {
937
+ const processedMsg = {};
938
+ for (const [key, value] of Object.entries(message)) {
939
+ if (key === `type` && value === `v2`) continue;
940
+ processedMsg[key] = deserializeValue(value);
941
+ }
942
+ return processedMsg;
943
+ });
944
+ this.logger.debug(`Retrieved ${messages.length} messages`);
945
+ const list = new MessageList().add(processedMessages, "memory");
946
+ if (format === `v1`) return list.get.all.v1();
947
+ return list.get.all.v2();
948
+ } catch (error) {
949
+ const mastraError = new MastraError(
950
+ {
951
+ id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_BY_ID_ERROR",
952
+ domain: ErrorDomain.STORAGE,
953
+ category: ErrorCategory.THIRD_PARTY,
954
+ text: `Failed to retrieve messages by ID: ${error instanceof Error ? error.message : String(error)}`,
955
+ details: { messageIds: JSON.stringify(messageIds) }
956
+ },
957
+ error
958
+ );
959
+ this.logger?.error(mastraError.toString());
960
+ this.logger?.trackException(mastraError);
961
+ throw mastraError;
962
+ }
963
+ }
923
964
  async getMessagesPaginated({
924
965
  threadId,
925
966
  selectBy,
@@ -1437,6 +1478,7 @@ var StoreOperationsD1 = class extends StoreOperations {
1437
1478
  query.andWhere(`${key} = ?`, value);
1438
1479
  }
1439
1480
  }
1481
+ query.orderBy("createdAt", "DESC");
1440
1482
  query.limit(1);
1441
1483
  const { sql, params } = query.build();
1442
1484
  const result = await this.executeQuery({ sql, params, first: true });
@@ -1521,20 +1563,19 @@ var StoreOperationsD1 = class extends StoreOperations {
1521
1563
  }
1522
1564
  };
1523
1565
  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
- };
1566
+ const deserialized = { ...row };
1567
+ deserialized.input = safelyParseJSON(row.input);
1568
+ deserialized.output = safelyParseJSON(row.output);
1569
+ deserialized.scorer = safelyParseJSON(row.scorer);
1570
+ deserialized.preprocessStepResult = safelyParseJSON(row.preprocessStepResult);
1571
+ deserialized.analyzeStepResult = safelyParseJSON(row.analyzeStepResult);
1572
+ deserialized.metadata = safelyParseJSON(row.metadata);
1573
+ deserialized.additionalContext = safelyParseJSON(row.additionalContext);
1574
+ deserialized.runtimeContext = safelyParseJSON(row.runtimeContext);
1575
+ deserialized.entity = safelyParseJSON(row.entity);
1576
+ deserialized.createdAt = row.createdAtZ || row.createdAt;
1577
+ deserialized.updatedAt = row.updatedAtZ || row.updatedAt;
1578
+ return deserialized;
1538
1579
  }
1539
1580
  var ScoresStorageD1 = class extends ScoresStorage {
1540
1581
  operations;
@@ -1565,6 +1606,7 @@ var ScoresStorageD1 = class extends ScoresStorage {
1565
1606
  }
1566
1607
  async saveScore(score) {
1567
1608
  try {
1609
+ const id = crypto.randomUUID();
1568
1610
  const fullTableName = this.operations.getTableName(TABLE_SCORERS);
1569
1611
  const { input, ...rest } = score;
1570
1612
  const serializedRecord = {};
@@ -1579,6 +1621,7 @@ var ScoresStorageD1 = class extends ScoresStorage {
1579
1621
  serializedRecord[key] = null;
1580
1622
  }
1581
1623
  }
1624
+ serializedRecord.id = id;
1582
1625
  serializedRecord.input = JSON.stringify(input);
1583
1626
  serializedRecord.createdAt = (/* @__PURE__ */ new Date()).toISOString();
1584
1627
  serializedRecord.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
@@ -1587,7 +1630,7 @@ var ScoresStorageD1 = class extends ScoresStorage {
1587
1630
  const query = createSqlBuilder().insert(fullTableName, columns, values);
1588
1631
  const { sql, params } = query.build();
1589
1632
  await this.operations.executeQuery({ sql, params });
1590
- const scoreFromDb = await this.getScoreById({ id: score.id });
1633
+ const scoreFromDb = await this.getScoreById({ id });
1591
1634
  return { score: scoreFromDb };
1592
1635
  } catch (error) {
1593
1636
  throw new MastraError(
@@ -1602,11 +1645,23 @@ var ScoresStorageD1 = class extends ScoresStorage {
1602
1645
  }
1603
1646
  async getScoresByScorerId({
1604
1647
  scorerId,
1648
+ entityId,
1649
+ entityType,
1650
+ source,
1605
1651
  pagination
1606
1652
  }) {
1607
1653
  try {
1608
1654
  const fullTableName = this.operations.getTableName(TABLE_SCORERS);
1609
1655
  const countQuery = createSqlBuilder().count().from(fullTableName).where("scorerId = ?", scorerId);
1656
+ if (entityId) {
1657
+ countQuery.andWhere("entityId = ?", entityId);
1658
+ }
1659
+ if (entityType) {
1660
+ countQuery.andWhere("entityType = ?", entityType);
1661
+ }
1662
+ if (source) {
1663
+ countQuery.andWhere("source = ?", source);
1664
+ }
1610
1665
  const countResult = await this.operations.executeQuery(countQuery.build());
1611
1666
  const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
1612
1667
  if (total === 0) {
@@ -1620,7 +1675,17 @@ var ScoresStorageD1 = class extends ScoresStorage {
1620
1675
  scores: []
1621
1676
  };
1622
1677
  }
1623
- const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("scorerId = ?", scorerId).limit(pagination.perPage).offset(pagination.page * pagination.perPage);
1678
+ const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("scorerId = ?", scorerId);
1679
+ if (entityId) {
1680
+ selectQuery.andWhere("entityId = ?", entityId);
1681
+ }
1682
+ if (entityType) {
1683
+ selectQuery.andWhere("entityType = ?", entityType);
1684
+ }
1685
+ if (source) {
1686
+ selectQuery.andWhere("source = ?", source);
1687
+ }
1688
+ selectQuery.limit(pagination.perPage).offset(pagination.page * pagination.perPage);
1624
1689
  const { sql, params } = selectQuery.build();
1625
1690
  const results = await this.operations.executeQuery({ sql, params });
1626
1691
  const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
@@ -2217,6 +2282,12 @@ var D1Store = class extends MastraStorage {
2217
2282
  }) {
2218
2283
  return this.stores.memory.getMessages({ threadId, selectBy, format });
2219
2284
  }
2285
+ async getMessagesById({
2286
+ messageIds,
2287
+ format
2288
+ }) {
2289
+ return this.stores.memory.getMessagesById({ messageIds, format });
2290
+ }
2220
2291
  async getMessagesPaginated({
2221
2292
  threadId,
2222
2293
  selectBy,
@@ -2316,10 +2387,13 @@ var D1Store = class extends MastraStorage {
2316
2387
  });
2317
2388
  }
2318
2389
  async getScoresByScorerId({
2319
- scorerId: _scorerId,
2320
- pagination: _pagination
2390
+ scorerId,
2391
+ pagination,
2392
+ entityId,
2393
+ entityType,
2394
+ source
2321
2395
  }) {
2322
- return this.stores.scores.getScoresByScorerId({ scorerId: _scorerId, pagination: _pagination });
2396
+ return this.stores.scores.getScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
2323
2397
  }
2324
2398
  /**
2325
2399
  * Close the database connection