@mastra/mongodb 0.14.2 → 0.14.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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @mastra/mongodb
2
2
 
3
+ ## 0.14.3-alpha.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Mongodb support to fetch scores by trace and spanId ([#8154](https://github.com/mastra-ai/mastra/pull/8154))
8
+
9
+ - Update peer deps ([#8154](https://github.com/mastra-ai/mastra/pull/8154))
10
+
11
+ - Updated dependencies [[`504438b`](https://github.com/mastra-ai/mastra/commit/504438b961bde211071186bba63a842c4e3db879), [`a7243e2`](https://github.com/mastra-ai/mastra/commit/a7243e2e58762667a6e3921e755e89d6bb0a3282), [`7fceb0a`](https://github.com/mastra-ai/mastra/commit/7fceb0a327d678e812f90f5387c5bc4f38bd039e), [`df64f9e`](https://github.com/mastra-ai/mastra/commit/df64f9ef814916fff9baedd861c988084e7c41de), [`809eea0`](https://github.com/mastra-ai/mastra/commit/809eea092fa80c3f69b9eaf078d843b57fd2a88e), [`683e5a1`](https://github.com/mastra-ai/mastra/commit/683e5a1466e48b686825b2c11f84680f296138e4), [`3679378`](https://github.com/mastra-ai/mastra/commit/3679378673350aa314741dc826f837b1984149bc), [`7775bc2`](https://github.com/mastra-ai/mastra/commit/7775bc20bb1ad1ab24797fb420e4f96c65b0d8ec), [`db1891a`](https://github.com/mastra-ai/mastra/commit/db1891a4707443720b7cd8a260dc7e1d49b3609c), [`e8f379d`](https://github.com/mastra-ai/mastra/commit/e8f379d390efa264c4e0874f9ac0cf8839b07777), [`652066b`](https://github.com/mastra-ai/mastra/commit/652066bd1efc6bb6813ba950ed1d7573e8b7d9d4), [`ea8d386`](https://github.com/mastra-ai/mastra/commit/ea8d386cd8c5593664515fd5770c06bf2aa980ef), [`c2a4919`](https://github.com/mastra-ai/mastra/commit/c2a4919ba6797d8bdb1509e02287496eef69303e), [`0130986`](https://github.com/mastra-ai/mastra/commit/0130986fc62d0edcc626dd593282661dbb9af141)]:
12
+ - @mastra/core@0.19.0-alpha.1
13
+
3
14
  ## 0.14.2
4
15
 
5
16
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -7,6 +7,7 @@ var uuid = require('uuid');
7
7
  var filter = require('@mastra/core/vector/filter');
8
8
  var storage = require('@mastra/core/storage');
9
9
  var agent = require('@mastra/core/agent');
10
+ var scores = require('@mastra/core/scores');
10
11
 
11
12
  // src/vector/index.ts
12
13
  var MongoDBFilterTranslator = class extends filter.BaseFilterTranslator {
@@ -1609,21 +1610,31 @@ function transformScoreRow(row) {
1609
1610
  console.warn("Failed to parse runtimeContext:", e);
1610
1611
  }
1611
1612
  }
1613
+ let metadataValue = null;
1614
+ if (row.metadata) {
1615
+ try {
1616
+ metadataValue = typeof row.metadata === "string" ? storage.safelyParseJSON(row.metadata) : row.metadata;
1617
+ } catch (e) {
1618
+ console.warn("Failed to parse metadata:", e);
1619
+ }
1620
+ }
1612
1621
  return {
1613
1622
  id: row.id,
1614
1623
  entityId: row.entityId,
1615
1624
  entityType: row.entityType,
1616
1625
  scorerId: row.scorerId,
1617
1626
  traceId: row.traceId,
1627
+ spanId: row.spanId,
1618
1628
  runId: row.runId,
1619
1629
  scorer: scorerValue,
1620
1630
  preprocessStepResult: preprocessStepResultValue,
1631
+ preprocessPrompt: row.preprocessPrompt,
1621
1632
  analyzeStepResult: analyzeStepResultValue,
1633
+ generateScorePrompt: row.generateScorePrompt,
1622
1634
  score: row.score,
1623
- reason: row.reason,
1624
- extractPrompt: row.extractPrompt,
1625
1635
  analyzePrompt: row.analyzePrompt,
1626
1636
  reasonPrompt: row.reasonPrompt,
1637
+ metadata: metadataValue,
1627
1638
  input: inputValue,
1628
1639
  output: outputValue,
1629
1640
  additionalContext: row.additionalContext,
@@ -1663,34 +1674,47 @@ var ScoresStorageMongoDB = class extends storage.ScoresStorage {
1663
1674
  }
1664
1675
  }
1665
1676
  async saveScore(score) {
1677
+ let validatedScore;
1678
+ try {
1679
+ validatedScore = scores.saveScorePayloadSchema.parse(score);
1680
+ } catch (error$1) {
1681
+ throw new error.MastraError(
1682
+ {
1683
+ id: "STORAGE_MONGODB_STORE_SAVE_SCORE_VALIDATION_FAILED",
1684
+ domain: error.ErrorDomain.STORAGE,
1685
+ category: error.ErrorCategory.THIRD_PARTY
1686
+ },
1687
+ error$1
1688
+ );
1689
+ }
1666
1690
  try {
1667
1691
  const now = /* @__PURE__ */ new Date();
1668
1692
  const scoreId = `score-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
1669
1693
  const scoreData = {
1670
1694
  id: scoreId,
1671
- entityId: score.entityId,
1672
- entityType: score.entityType,
1673
- scorerId: score.scorerId,
1674
- traceId: score.traceId || "",
1675
- runId: score.runId,
1676
- scorer: typeof score.scorer === "string" ? storage.safelyParseJSON(score.scorer) : score.scorer,
1677
- preprocessStepResult: typeof score.preprocessStepResult === "string" ? storage.safelyParseJSON(score.preprocessStepResult) : score.preprocessStepResult,
1678
- analyzeStepResult: typeof score.analyzeStepResult === "string" ? storage.safelyParseJSON(score.analyzeStepResult) : score.analyzeStepResult,
1679
- score: score.score,
1680
- reason: score.reason,
1681
- preprocessPrompt: score.preprocessPrompt,
1682
- generateScorePrompt: score.generateScorePrompt,
1683
- generateReasonPrompt: score.generateReasonPrompt,
1684
- analyzePrompt: score.analyzePrompt,
1685
- reasonPrompt: score.reasonPrompt,
1686
- input: typeof score.input === "string" ? storage.safelyParseJSON(score.input) : score.input,
1687
- output: typeof score.output === "string" ? storage.safelyParseJSON(score.output) : score.output,
1688
- additionalContext: score.additionalContext,
1689
- runtimeContext: typeof score.runtimeContext === "string" ? storage.safelyParseJSON(score.runtimeContext) : score.runtimeContext,
1690
- entity: typeof score.entity === "string" ? storage.safelyParseJSON(score.entity) : score.entity,
1691
- source: score.source,
1692
- resourceId: score.resourceId || "",
1693
- threadId: score.threadId || "",
1695
+ entityId: validatedScore.entityId,
1696
+ entityType: validatedScore.entityType,
1697
+ scorerId: validatedScore.scorerId,
1698
+ traceId: validatedScore.traceId || "",
1699
+ spanId: validatedScore.spanId || "",
1700
+ runId: validatedScore.runId,
1701
+ scorer: typeof validatedScore.scorer === "string" ? storage.safelyParseJSON(validatedScore.scorer) : validatedScore.scorer,
1702
+ preprocessStepResult: typeof validatedScore.preprocessStepResult === "string" ? storage.safelyParseJSON(validatedScore.preprocessStepResult) : validatedScore.preprocessStepResult,
1703
+ analyzeStepResult: typeof validatedScore.analyzeStepResult === "string" ? storage.safelyParseJSON(validatedScore.analyzeStepResult) : validatedScore.analyzeStepResult,
1704
+ score: validatedScore.score,
1705
+ reason: validatedScore.reason,
1706
+ preprocessPrompt: validatedScore.preprocessPrompt,
1707
+ generateScorePrompt: validatedScore.generateScorePrompt,
1708
+ generateReasonPrompt: validatedScore.generateReasonPrompt,
1709
+ analyzePrompt: validatedScore.analyzePrompt,
1710
+ input: typeof validatedScore.input === "string" ? storage.safelyParseJSON(validatedScore.input) : validatedScore.input,
1711
+ output: typeof validatedScore.output === "string" ? storage.safelyParseJSON(validatedScore.output) : validatedScore.output,
1712
+ additionalContext: validatedScore.additionalContext,
1713
+ runtimeContext: typeof validatedScore.runtimeContext === "string" ? storage.safelyParseJSON(validatedScore.runtimeContext) : validatedScore.runtimeContext,
1714
+ entity: typeof validatedScore.entity === "string" ? storage.safelyParseJSON(validatedScore.entity) : validatedScore.entity,
1715
+ source: validatedScore.source,
1716
+ resourceId: validatedScore.resourceId || "",
1717
+ threadId: validatedScore.threadId || "",
1694
1718
  createdAt: now,
1695
1719
  updatedAt: now
1696
1720
  };
@@ -1858,6 +1882,51 @@ var ScoresStorageMongoDB = class extends storage.ScoresStorage {
1858
1882
  );
1859
1883
  }
1860
1884
  }
1885
+ async getScoresBySpan({
1886
+ traceId,
1887
+ spanId,
1888
+ pagination
1889
+ }) {
1890
+ try {
1891
+ const query = { traceId, spanId };
1892
+ const collection = await this.operations.getCollection(storage.TABLE_SCORERS);
1893
+ const total = await collection.countDocuments(query);
1894
+ const currentOffset = pagination.page * pagination.perPage;
1895
+ if (total === 0) {
1896
+ return {
1897
+ scores: [],
1898
+ pagination: {
1899
+ total: 0,
1900
+ page: pagination.page,
1901
+ perPage: pagination.perPage,
1902
+ hasMore: false
1903
+ }
1904
+ };
1905
+ }
1906
+ const documents = await collection.find(query).sort({ createdAt: "desc" }).skip(currentOffset).limit(pagination.perPage).toArray();
1907
+ const scores = documents.map((row) => transformScoreRow(row));
1908
+ const hasMore = currentOffset + scores.length < total;
1909
+ return {
1910
+ scores,
1911
+ pagination: {
1912
+ total,
1913
+ page: pagination.page,
1914
+ perPage: pagination.perPage,
1915
+ hasMore
1916
+ }
1917
+ };
1918
+ } catch (error$1) {
1919
+ throw new error.MastraError(
1920
+ {
1921
+ id: "STORAGE_MONGODB_STORE_GET_SCORES_BY_SPAN_FAILED",
1922
+ domain: error.ErrorDomain.STORAGE,
1923
+ category: error.ErrorCategory.THIRD_PARTY,
1924
+ details: { traceId, spanId, page: pagination.page, perPage: pagination.perPage }
1925
+ },
1926
+ error$1
1927
+ );
1928
+ }
1929
+ }
1861
1930
  };
1862
1931
  var TracesStorageMongoDB = class extends storage.TracesStorage {
1863
1932
  operations;
@@ -2196,7 +2265,8 @@ var MongoDBStore = class extends storage.MastraStorage {
2196
2265
  resourceWorkingMemory: true,
2197
2266
  hasColumn: false,
2198
2267
  createTable: false,
2199
- deleteMessages: false
2268
+ deleteMessages: false,
2269
+ getScoresBySpan: true
2200
2270
  };
2201
2271
  }
2202
2272
  constructor(config) {
@@ -2394,6 +2464,13 @@ var MongoDBStore = class extends storage.MastraStorage {
2394
2464
  }) {
2395
2465
  return this.stores.scores.getScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
2396
2466
  }
2467
+ async getScoresBySpan({
2468
+ traceId,
2469
+ spanId,
2470
+ pagination
2471
+ }) {
2472
+ return this.stores.scores.getScoresBySpan({ traceId, spanId, pagination });
2473
+ }
2397
2474
  /**
2398
2475
  * RESOURCES
2399
2476
  */