@mastra/cloudflare 0.12.2 → 0.12.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
@@ -2,6 +2,7 @@ import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
2
2
  import { MastraStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_WORKFLOW_SNAPSHOT, TABLE_EVALS, TABLE_SCORERS, TABLE_TRACES, StoreOperations, serializeDate, ensureDate, LegacyEvalsStorage, WorkflowsStorage, TracesStorage, MemoryStorage, resolveMessageLimit, TABLE_RESOURCES, ScoresStorage, safelyParseJSON } from '@mastra/core/storage';
3
3
  import Cloudflare from 'cloudflare';
4
4
  import { MessageList } from '@mastra/core/agent';
5
+ import { saveScorePayloadSchema } from '@mastra/core/scores';
5
6
 
6
7
  // src/storage/index.ts
7
8
  var LegacyEvalsStorageCloudflare = class extends LegacyEvalsStorage {
@@ -1614,11 +1615,24 @@ var ScoresStorageCloudflare = class extends ScoresStorage {
1614
1615
  }
1615
1616
  }
1616
1617
  async saveScore(score) {
1618
+ let parsedScore;
1619
+ try {
1620
+ parsedScore = saveScorePayloadSchema.parse(score);
1621
+ } catch (error) {
1622
+ throw new MastraError(
1623
+ {
1624
+ id: "CLOUDFLARE_STORAGE_SAVE_SCORE_FAILED_INVALID_SCORE_PAYLOAD",
1625
+ domain: ErrorDomain.STORAGE,
1626
+ category: ErrorCategory.USER,
1627
+ details: { scoreId: score.id }
1628
+ },
1629
+ error
1630
+ );
1631
+ }
1617
1632
  try {
1618
1633
  const id = crypto.randomUUID();
1619
- const { input, ...rest } = score;
1620
1634
  const serializedRecord = {};
1621
- for (const [key, value] of Object.entries(rest)) {
1635
+ for (const [key, value] of Object.entries(parsedScore)) {
1622
1636
  if (value !== null && value !== void 0) {
1623
1637
  if (typeof value === "object") {
1624
1638
  serializedRecord[key] = JSON.stringify(value);
@@ -1805,6 +1819,50 @@ var ScoresStorageCloudflare = class extends ScoresStorage {
1805
1819
  return { pagination: { total: 0, page: 0, perPage: 100, hasMore: false }, scores: [] };
1806
1820
  }
1807
1821
  }
1822
+ async getScoresBySpan({
1823
+ traceId,
1824
+ spanId,
1825
+ pagination
1826
+ }) {
1827
+ try {
1828
+ const keys = await this.operations.listKV(TABLE_SCORERS);
1829
+ const scores = [];
1830
+ for (const { name: key } of keys) {
1831
+ const score = await this.operations.getKV(TABLE_SCORERS, key);
1832
+ if (score && score.traceId === traceId && score.spanId === spanId) {
1833
+ scores.push(transformScoreRow(score));
1834
+ }
1835
+ }
1836
+ scores.sort((a, b) => {
1837
+ const dateA = new Date(a.createdAt || 0).getTime();
1838
+ const dateB = new Date(b.createdAt || 0).getTime();
1839
+ return dateB - dateA;
1840
+ });
1841
+ const total = scores.length;
1842
+ const start = pagination.page * pagination.perPage;
1843
+ const end = start + pagination.perPage;
1844
+ const pagedScores = scores.slice(start, end);
1845
+ return {
1846
+ pagination: {
1847
+ total,
1848
+ page: pagination.page,
1849
+ perPage: pagination.perPage,
1850
+ hasMore: end < total
1851
+ },
1852
+ scores: pagedScores
1853
+ };
1854
+ } catch (error) {
1855
+ throw new MastraError(
1856
+ {
1857
+ id: "CLOUDFLARE_STORAGE_SCORES_GET_SCORES_BY_SPAN_FAILED",
1858
+ domain: ErrorDomain.STORAGE,
1859
+ category: ErrorCategory.THIRD_PARTY,
1860
+ text: `Failed to get scores by span: traceId=${traceId}, spanId=${spanId}`
1861
+ },
1862
+ error
1863
+ );
1864
+ }
1865
+ }
1808
1866
  };
1809
1867
  var TracesStorageCloudflare = class extends TracesStorage {
1810
1868
  operations;
@@ -2195,6 +2253,11 @@ var CloudflareStore = class extends MastraStorage {
2195
2253
  throw new Error("apiToken is required for REST API");
2196
2254
  }
2197
2255
  }
2256
+ get supports() {
2257
+ const supports = super.supports;
2258
+ supports.getScoresBySpan = true;
2259
+ return supports;
2260
+ }
2198
2261
  constructor(config) {
2199
2262
  super({ name: "Cloudflare" });
2200
2263
  try {
@@ -2425,6 +2488,13 @@ var CloudflareStore = class extends MastraStorage {
2425
2488
  }) {
2426
2489
  return this.stores.scores.getScoresByScorerId({ scorerId, entityId, entityType, source, pagination });
2427
2490
  }
2491
+ async getScoresBySpan({
2492
+ traceId,
2493
+ spanId,
2494
+ pagination
2495
+ }) {
2496
+ return this.stores.scores.getScoresBySpan({ traceId, spanId, pagination });
2497
+ }
2428
2498
  async getResourceById({ resourceId }) {
2429
2499
  return this.stores.memory.getResourceById({ resourceId });
2430
2500
  }