@mastra/cloudflare 0.11.5 → 0.11.7-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
@@ -1,5 +1,5 @@
1
1
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
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 } from '@mastra/core/storage';
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
5
 
@@ -727,6 +727,41 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
727
727
  return [];
728
728
  }
729
729
  }
730
+ async getMessagesById({
731
+ messageIds,
732
+ format
733
+ }) {
734
+ if (messageIds.length === 0) return [];
735
+ try {
736
+ const messages = (await Promise.all(messageIds.map((id) => this.findMessageInAnyThread(id)))).filter(
737
+ (result) => !!result
738
+ );
739
+ const prepared = messages.map(({ _index, ...message }) => ({
740
+ ...message,
741
+ ...message.type !== `v2` && { type: message.type },
742
+ createdAt: ensureDate(message.createdAt)
743
+ }));
744
+ const list = new MessageList().add(prepared, "memory");
745
+ if (format === `v1`) return list.get.all.v1();
746
+ return list.get.all.v2();
747
+ } catch (error) {
748
+ const mastraError = new MastraError(
749
+ {
750
+ id: "CLOUDFLARE_STORAGE_GET_MESSAGES_BY_ID_FAILED",
751
+ domain: ErrorDomain.STORAGE,
752
+ category: ErrorCategory.THIRD_PARTY,
753
+ text: `Error retrieving messages by ID`,
754
+ details: {
755
+ messageIds: JSON.stringify(messageIds)
756
+ }
757
+ },
758
+ error
759
+ );
760
+ this.logger?.trackException(mastraError);
761
+ this.logger?.error(mastraError.toString());
762
+ return [];
763
+ }
764
+ }
730
765
  async getMessagesPaginated(args) {
731
766
  try {
732
767
  const { threadId, selectBy, format = "v1" } = args;
@@ -1529,18 +1564,17 @@ var StoreOperationsCloudflare = class extends StoreOperations {
1529
1564
  }
1530
1565
  };
1531
1566
  function transformScoreRow(row) {
1532
- let input = void 0;
1533
- if (row.input) {
1534
- try {
1535
- input = JSON.parse(row.input);
1536
- } catch {
1537
- input = row.input;
1538
- }
1539
- }
1540
- return {
1541
- ...row,
1542
- input
1543
- };
1567
+ const deserialized = { ...row };
1568
+ deserialized.input = safelyParseJSON(row.input);
1569
+ deserialized.output = safelyParseJSON(row.output);
1570
+ deserialized.scorer = safelyParseJSON(row.scorer);
1571
+ deserialized.preprocessStepResult = safelyParseJSON(row.preprocessStepResult);
1572
+ deserialized.analyzeStepResult = safelyParseJSON(row.analyzeStepResult);
1573
+ deserialized.metadata = safelyParseJSON(row.metadata);
1574
+ deserialized.additionalContext = safelyParseJSON(row.additionalContext);
1575
+ deserialized.runtimeContext = safelyParseJSON(row.runtimeContext);
1576
+ deserialized.entity = safelyParseJSON(row.entity);
1577
+ return deserialized;
1544
1578
  }
1545
1579
  var ScoresStorageCloudflare = class extends ScoresStorage {
1546
1580
  operations;
@@ -1572,6 +1606,7 @@ var ScoresStorageCloudflare = class extends ScoresStorage {
1572
1606
  }
1573
1607
  async saveScore(score) {
1574
1608
  try {
1609
+ const id = crypto.randomUUID();
1575
1610
  const { input, ...rest } = score;
1576
1611
  const serializedRecord = {};
1577
1612
  for (const [key, value] of Object.entries(rest)) {
@@ -1585,12 +1620,12 @@ var ScoresStorageCloudflare = class extends ScoresStorage {
1585
1620
  serializedRecord[key] = null;
1586
1621
  }
1587
1622
  }
1588
- serializedRecord.input = JSON.stringify(input);
1623
+ serializedRecord.id = id;
1589
1624
  serializedRecord.createdAt = (/* @__PURE__ */ new Date()).toISOString();
1590
1625
  serializedRecord.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
1591
1626
  await this.operations.putKV({
1592
1627
  tableName: TABLE_SCORERS,
1593
- key: score.id,
1628
+ key: id,
1594
1629
  value: serializedRecord
1595
1630
  });
1596
1631
  const scoreFromDb = await this.getScoreById({ id: score.id });
@@ -1612,6 +1647,9 @@ var ScoresStorageCloudflare = class extends ScoresStorage {
1612
1647
  }
1613
1648
  async getScoresByScorerId({
1614
1649
  scorerId,
1650
+ entityId,
1651
+ entityType,
1652
+ source,
1615
1653
  pagination
1616
1654
  }) {
1617
1655
  try {
@@ -1619,6 +1657,15 @@ var ScoresStorageCloudflare = class extends ScoresStorage {
1619
1657
  const scores = [];
1620
1658
  for (const { name: key } of keys) {
1621
1659
  const score = await this.operations.getKV(TABLE_SCORERS, key);
1660
+ if (entityId && score.entityId !== entityId) {
1661
+ continue;
1662
+ }
1663
+ if (entityType && score.entityType !== entityType) {
1664
+ continue;
1665
+ }
1666
+ if (source && score.source !== source) {
1667
+ continue;
1668
+ }
1622
1669
  if (score && score.scorerId === scorerId) {
1623
1670
  scores.push(transformScoreRow(score));
1624
1671
  }
@@ -2233,6 +2280,12 @@ var CloudflareStore = class extends MastraStorage {
2233
2280
  }) {
2234
2281
  return this.stores.memory.getMessages({ threadId, resourceId, selectBy, format });
2235
2282
  }
2283
+ async getMessagesById({
2284
+ messageIds,
2285
+ format
2286
+ }) {
2287
+ return this.stores.memory.getMessagesById({ messageIds, format });
2288
+ }
2236
2289
  async persistWorkflowSnapshot(params) {
2237
2290
  return this.stores.workflows.persistWorkflowSnapshot(params);
2238
2291
  }
@@ -2323,9 +2376,12 @@ var CloudflareStore = class extends MastraStorage {
2323
2376
  }
2324
2377
  async getScoresByScorerId({
2325
2378
  scorerId,
2379
+ entityId,
2380
+ entityType,
2381
+ source,
2326
2382
  pagination
2327
2383
  }) {
2328
- return this.stores.scores.getScoresByScorerId({ scorerId, pagination });
2384
+ return this.stores.scores.getScoresByScorerId({ scorerId, entityId, entityType, source, pagination });
2329
2385
  }
2330
2386
  async getResourceById({ resourceId }) {
2331
2387
  return this.stores.memory.getResourceById({ resourceId });