@mastra/mongodb 0.14.2 → 0.14.3

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
@@ -5,6 +5,7 @@ import { v4 } from 'uuid';
5
5
  import { BaseFilterTranslator } from '@mastra/core/vector/filter';
6
6
  import { MastraStorage, StoreOperations, TABLE_SCHEMAS, safelyParseJSON, MemoryStorage, TABLE_MESSAGES, resolveMessageLimit, TABLE_THREADS, TABLE_RESOURCES, TracesStorage, TABLE_TRACES, LegacyEvalsStorage, TABLE_EVALS, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT } from '@mastra/core/storage';
7
7
  import { MessageList } from '@mastra/core/agent';
8
+ import { saveScorePayloadSchema } from '@mastra/core/scores';
8
9
 
9
10
  // src/vector/index.ts
10
11
  var MongoDBFilterTranslator = class extends BaseFilterTranslator {
@@ -1607,21 +1608,31 @@ function transformScoreRow(row) {
1607
1608
  console.warn("Failed to parse runtimeContext:", e);
1608
1609
  }
1609
1610
  }
1611
+ let metadataValue = null;
1612
+ if (row.metadata) {
1613
+ try {
1614
+ metadataValue = typeof row.metadata === "string" ? safelyParseJSON(row.metadata) : row.metadata;
1615
+ } catch (e) {
1616
+ console.warn("Failed to parse metadata:", e);
1617
+ }
1618
+ }
1610
1619
  return {
1611
1620
  id: row.id,
1612
1621
  entityId: row.entityId,
1613
1622
  entityType: row.entityType,
1614
1623
  scorerId: row.scorerId,
1615
1624
  traceId: row.traceId,
1625
+ spanId: row.spanId,
1616
1626
  runId: row.runId,
1617
1627
  scorer: scorerValue,
1618
1628
  preprocessStepResult: preprocessStepResultValue,
1629
+ preprocessPrompt: row.preprocessPrompt,
1619
1630
  analyzeStepResult: analyzeStepResultValue,
1631
+ generateScorePrompt: row.generateScorePrompt,
1620
1632
  score: row.score,
1621
- reason: row.reason,
1622
- extractPrompt: row.extractPrompt,
1623
1633
  analyzePrompt: row.analyzePrompt,
1624
1634
  reasonPrompt: row.reasonPrompt,
1635
+ metadata: metadataValue,
1625
1636
  input: inputValue,
1626
1637
  output: outputValue,
1627
1638
  additionalContext: row.additionalContext,
@@ -1661,34 +1672,47 @@ var ScoresStorageMongoDB = class extends ScoresStorage {
1661
1672
  }
1662
1673
  }
1663
1674
  async saveScore(score) {
1675
+ let validatedScore;
1676
+ try {
1677
+ validatedScore = saveScorePayloadSchema.parse(score);
1678
+ } catch (error) {
1679
+ throw new MastraError(
1680
+ {
1681
+ id: "STORAGE_MONGODB_STORE_SAVE_SCORE_VALIDATION_FAILED",
1682
+ domain: ErrorDomain.STORAGE,
1683
+ category: ErrorCategory.THIRD_PARTY
1684
+ },
1685
+ error
1686
+ );
1687
+ }
1664
1688
  try {
1665
1689
  const now = /* @__PURE__ */ new Date();
1666
1690
  const scoreId = `score-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
1667
1691
  const scoreData = {
1668
1692
  id: scoreId,
1669
- entityId: score.entityId,
1670
- entityType: score.entityType,
1671
- scorerId: score.scorerId,
1672
- traceId: score.traceId || "",
1673
- runId: score.runId,
1674
- scorer: typeof score.scorer === "string" ? safelyParseJSON(score.scorer) : score.scorer,
1675
- preprocessStepResult: typeof score.preprocessStepResult === "string" ? safelyParseJSON(score.preprocessStepResult) : score.preprocessStepResult,
1676
- analyzeStepResult: typeof score.analyzeStepResult === "string" ? safelyParseJSON(score.analyzeStepResult) : score.analyzeStepResult,
1677
- score: score.score,
1678
- reason: score.reason,
1679
- preprocessPrompt: score.preprocessPrompt,
1680
- generateScorePrompt: score.generateScorePrompt,
1681
- generateReasonPrompt: score.generateReasonPrompt,
1682
- analyzePrompt: score.analyzePrompt,
1683
- reasonPrompt: score.reasonPrompt,
1684
- input: typeof score.input === "string" ? safelyParseJSON(score.input) : score.input,
1685
- output: typeof score.output === "string" ? safelyParseJSON(score.output) : score.output,
1686
- additionalContext: score.additionalContext,
1687
- runtimeContext: typeof score.runtimeContext === "string" ? safelyParseJSON(score.runtimeContext) : score.runtimeContext,
1688
- entity: typeof score.entity === "string" ? safelyParseJSON(score.entity) : score.entity,
1689
- source: score.source,
1690
- resourceId: score.resourceId || "",
1691
- threadId: score.threadId || "",
1693
+ entityId: validatedScore.entityId,
1694
+ entityType: validatedScore.entityType,
1695
+ scorerId: validatedScore.scorerId,
1696
+ traceId: validatedScore.traceId || "",
1697
+ spanId: validatedScore.spanId || "",
1698
+ runId: validatedScore.runId,
1699
+ scorer: typeof validatedScore.scorer === "string" ? safelyParseJSON(validatedScore.scorer) : validatedScore.scorer,
1700
+ preprocessStepResult: typeof validatedScore.preprocessStepResult === "string" ? safelyParseJSON(validatedScore.preprocessStepResult) : validatedScore.preprocessStepResult,
1701
+ analyzeStepResult: typeof validatedScore.analyzeStepResult === "string" ? safelyParseJSON(validatedScore.analyzeStepResult) : validatedScore.analyzeStepResult,
1702
+ score: validatedScore.score,
1703
+ reason: validatedScore.reason,
1704
+ preprocessPrompt: validatedScore.preprocessPrompt,
1705
+ generateScorePrompt: validatedScore.generateScorePrompt,
1706
+ generateReasonPrompt: validatedScore.generateReasonPrompt,
1707
+ analyzePrompt: validatedScore.analyzePrompt,
1708
+ input: typeof validatedScore.input === "string" ? safelyParseJSON(validatedScore.input) : validatedScore.input,
1709
+ output: typeof validatedScore.output === "string" ? safelyParseJSON(validatedScore.output) : validatedScore.output,
1710
+ additionalContext: validatedScore.additionalContext,
1711
+ runtimeContext: typeof validatedScore.runtimeContext === "string" ? safelyParseJSON(validatedScore.runtimeContext) : validatedScore.runtimeContext,
1712
+ entity: typeof validatedScore.entity === "string" ? safelyParseJSON(validatedScore.entity) : validatedScore.entity,
1713
+ source: validatedScore.source,
1714
+ resourceId: validatedScore.resourceId || "",
1715
+ threadId: validatedScore.threadId || "",
1692
1716
  createdAt: now,
1693
1717
  updatedAt: now
1694
1718
  };
@@ -1856,6 +1880,51 @@ var ScoresStorageMongoDB = class extends ScoresStorage {
1856
1880
  );
1857
1881
  }
1858
1882
  }
1883
+ async getScoresBySpan({
1884
+ traceId,
1885
+ spanId,
1886
+ pagination
1887
+ }) {
1888
+ try {
1889
+ const query = { traceId, spanId };
1890
+ const collection = await this.operations.getCollection(TABLE_SCORERS);
1891
+ const total = await collection.countDocuments(query);
1892
+ const currentOffset = pagination.page * pagination.perPage;
1893
+ if (total === 0) {
1894
+ return {
1895
+ scores: [],
1896
+ pagination: {
1897
+ total: 0,
1898
+ page: pagination.page,
1899
+ perPage: pagination.perPage,
1900
+ hasMore: false
1901
+ }
1902
+ };
1903
+ }
1904
+ const documents = await collection.find(query).sort({ createdAt: "desc" }).skip(currentOffset).limit(pagination.perPage).toArray();
1905
+ const scores = documents.map((row) => transformScoreRow(row));
1906
+ const hasMore = currentOffset + scores.length < total;
1907
+ return {
1908
+ scores,
1909
+ pagination: {
1910
+ total,
1911
+ page: pagination.page,
1912
+ perPage: pagination.perPage,
1913
+ hasMore
1914
+ }
1915
+ };
1916
+ } catch (error) {
1917
+ throw new MastraError(
1918
+ {
1919
+ id: "STORAGE_MONGODB_STORE_GET_SCORES_BY_SPAN_FAILED",
1920
+ domain: ErrorDomain.STORAGE,
1921
+ category: ErrorCategory.THIRD_PARTY,
1922
+ details: { traceId, spanId, page: pagination.page, perPage: pagination.perPage }
1923
+ },
1924
+ error
1925
+ );
1926
+ }
1927
+ }
1859
1928
  };
1860
1929
  var TracesStorageMongoDB = class extends TracesStorage {
1861
1930
  operations;
@@ -2194,7 +2263,8 @@ var MongoDBStore = class extends MastraStorage {
2194
2263
  resourceWorkingMemory: true,
2195
2264
  hasColumn: false,
2196
2265
  createTable: false,
2197
- deleteMessages: false
2266
+ deleteMessages: false,
2267
+ getScoresBySpan: true
2198
2268
  };
2199
2269
  }
2200
2270
  constructor(config) {
@@ -2392,6 +2462,13 @@ var MongoDBStore = class extends MastraStorage {
2392
2462
  }) {
2393
2463
  return this.stores.scores.getScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
2394
2464
  }
2465
+ async getScoresBySpan({
2466
+ traceId,
2467
+ spanId,
2468
+ pagination
2469
+ }) {
2470
+ return this.stores.scores.getScoresBySpan({ traceId, spanId, pagination });
2471
+ }
2395
2472
  /**
2396
2473
  * RESOURCES
2397
2474
  */