@mastra/mongodb 1.7.1 → 1.7.2-alpha.1

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,23 @@
1
1
  # @mastra/mongodb
2
2
 
3
+ ## 1.7.2-alpha.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Added `getTraceLight` method to the observability storage, returning only lightweight span fields needed for timeline rendering. This avoids transferring heavy fields like `input`, `output`, `attributes`, and `metadata` when they are not needed. ([#15574](https://github.com/mastra-ai/mastra/pull/15574))
8
+
9
+ - Updated dependencies [[`20f59b8`](https://github.com/mastra-ai/mastra/commit/20f59b876cf91199efbc49a0e36b391240708f08), [`e2687a7`](https://github.com/mastra-ai/mastra/commit/e2687a7408790c384563816a9a28ed06735684c9), [`8f1b280`](https://github.com/mastra-ai/mastra/commit/8f1b280b7fe6999ec654f160cb69c1a8719e7a57), [`12df98c`](https://github.com/mastra-ai/mastra/commit/12df98c4904643d9481f5c78f3bed443725b4c96)]:
10
+ - @mastra/core@1.26.0-alpha.11
11
+
12
+ ## 1.7.2-alpha.0
13
+
14
+ ### Patch Changes
15
+
16
+ - Add `BackgroundTasksStorage` domain implementation so `@mastra/core` background task execution works with any storage adapter. ([#15307](https://github.com/mastra-ai/mastra/pull/15307))
17
+
18
+ - Updated dependencies [[`d63ffdb`](https://github.com/mastra-ai/mastra/commit/d63ffdbb2c11e76fe5ea45faab44bc15460f010c)]:
19
+ - @mastra/core@1.25.1-alpha.0
20
+
3
21
  ## 1.7.1
4
22
 
5
23
  ### Patch Changes
@@ -3,7 +3,7 @@ name: mastra-mongodb
3
3
  description: Documentation for @mastra/mongodb. Use when working with @mastra/mongodb APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/mongodb"
6
- version: "1.7.1"
6
+ version: "1.7.2-alpha.1"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.7.1",
2
+ "version": "1.7.2-alpha.1",
3
3
  "package": "@mastra/mongodb",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -14,7 +14,7 @@ var evals = require('@mastra/core/evals');
14
14
 
15
15
  // package.json
16
16
  var package_default = {
17
- version: "1.7.1"};
17
+ version: "1.7.2-alpha.1"};
18
18
  var MongoDBFilterTranslator = class extends filter.BaseFilterTranslator {
19
19
  getSupportedOperators() {
20
20
  return {
@@ -1505,6 +1505,191 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends storage.AgentsSto
1505
1505
  return result;
1506
1506
  }
1507
1507
  };
1508
+ function toDoc(task) {
1509
+ return {
1510
+ id: task.id,
1511
+ tool_call_id: task.toolCallId,
1512
+ tool_name: task.toolName,
1513
+ agent_id: task.agentId,
1514
+ thread_id: task.threadId ?? null,
1515
+ resource_id: task.resourceId ?? null,
1516
+ run_id: task.runId,
1517
+ status: task.status,
1518
+ args: task.args,
1519
+ result: task.result ?? null,
1520
+ error: task.error ?? null,
1521
+ retry_count: task.retryCount,
1522
+ max_retries: task.maxRetries,
1523
+ timeout_ms: task.timeoutMs,
1524
+ createdAt: task.createdAt.toISOString(),
1525
+ startedAt: task.startedAt?.toISOString() ?? null,
1526
+ completedAt: task.completedAt?.toISOString() ?? null
1527
+ };
1528
+ }
1529
+ function fromDoc(doc) {
1530
+ return {
1531
+ id: doc.id,
1532
+ status: doc.status,
1533
+ toolName: doc.tool_name,
1534
+ toolCallId: doc.tool_call_id,
1535
+ args: doc.args ?? {},
1536
+ agentId: doc.agent_id,
1537
+ threadId: doc.thread_id ?? void 0,
1538
+ resourceId: doc.resource_id ?? void 0,
1539
+ runId: doc.run_id ?? "",
1540
+ result: doc.result ?? void 0,
1541
+ error: doc.error ?? void 0,
1542
+ retryCount: Number(doc.retry_count ?? 0),
1543
+ maxRetries: Number(doc.max_retries ?? 0),
1544
+ timeoutMs: Number(doc.timeout_ms ?? 3e5),
1545
+ createdAt: new Date(doc.createdAt),
1546
+ startedAt: doc.startedAt ? new Date(doc.startedAt) : void 0,
1547
+ completedAt: doc.completedAt ? new Date(doc.completedAt) : void 0
1548
+ };
1549
+ }
1550
+ var BackgroundTasksStorageMongoDB = class _BackgroundTasksStorageMongoDB extends storage.BackgroundTasksStorage {
1551
+ #connector;
1552
+ #skipDefaultIndexes;
1553
+ #indexes;
1554
+ static MANAGED_COLLECTIONS = [storage.TABLE_BACKGROUND_TASKS];
1555
+ constructor(config) {
1556
+ super();
1557
+ this.#connector = resolveMongoDBConfig(config);
1558
+ this.#skipDefaultIndexes = config.skipDefaultIndexes;
1559
+ this.#indexes = config.indexes?.filter(
1560
+ (idx) => _BackgroundTasksStorageMongoDB.MANAGED_COLLECTIONS.includes(idx.collection)
1561
+ );
1562
+ }
1563
+ async getCollection() {
1564
+ return this.#connector.getCollection(storage.TABLE_BACKGROUND_TASKS);
1565
+ }
1566
+ getDefaultIndexDefinitions() {
1567
+ return [
1568
+ { collection: storage.TABLE_BACKGROUND_TASKS, keys: { id: 1 }, options: { unique: true } },
1569
+ { collection: storage.TABLE_BACKGROUND_TASKS, keys: { status: 1, createdAt: 1 } },
1570
+ { collection: storage.TABLE_BACKGROUND_TASKS, keys: { agent_id: 1, status: 1 } },
1571
+ { collection: storage.TABLE_BACKGROUND_TASKS, keys: { thread_id: 1, createdAt: 1 } },
1572
+ { collection: storage.TABLE_BACKGROUND_TASKS, keys: { tool_call_id: 1 } }
1573
+ ];
1574
+ }
1575
+ async createDefaultIndexes() {
1576
+ if (this.#skipDefaultIndexes) return;
1577
+ for (const indexDef of this.getDefaultIndexDefinitions()) {
1578
+ try {
1579
+ const collection = await this.getCollection();
1580
+ await collection.createIndex(indexDef.keys, indexDef.options);
1581
+ } catch (error) {
1582
+ this.logger?.warn?.(`Failed to create index on ${storage.TABLE_BACKGROUND_TASKS}:`, error);
1583
+ }
1584
+ }
1585
+ }
1586
+ async createCustomIndexes() {
1587
+ if (!this.#indexes || this.#indexes.length === 0) return;
1588
+ for (const indexDef of this.#indexes) {
1589
+ try {
1590
+ const collection = await this.getCollection();
1591
+ await collection.createIndex(indexDef.keys, indexDef.options);
1592
+ } catch (error) {
1593
+ this.logger?.warn?.(`Failed to create custom index on ${storage.TABLE_BACKGROUND_TASKS}:`, error);
1594
+ }
1595
+ }
1596
+ }
1597
+ async init() {
1598
+ await this.createDefaultIndexes();
1599
+ await this.createCustomIndexes();
1600
+ }
1601
+ async dangerouslyClearAll() {
1602
+ const collection = await this.getCollection();
1603
+ await collection.deleteMany({});
1604
+ }
1605
+ async createTask(task) {
1606
+ const collection = await this.getCollection();
1607
+ await collection.insertOne(toDoc(task));
1608
+ }
1609
+ async updateTask(taskId, update) {
1610
+ const $set = {};
1611
+ if ("status" in update) $set.status = update.status;
1612
+ if ("result" in update) $set.result = update.result ?? null;
1613
+ if ("error" in update) $set.error = update.error ?? null;
1614
+ if ("retryCount" in update) $set.retry_count = update.retryCount;
1615
+ if ("startedAt" in update) $set.startedAt = update.startedAt?.toISOString() ?? null;
1616
+ if ("completedAt" in update) $set.completedAt = update.completedAt?.toISOString() ?? null;
1617
+ if (Object.keys($set).length === 0) return;
1618
+ const collection = await this.getCollection();
1619
+ await collection.updateOne({ id: taskId }, { $set });
1620
+ }
1621
+ async getTask(taskId) {
1622
+ const collection = await this.getCollection();
1623
+ const doc = await collection.findOne({ id: taskId });
1624
+ return doc ? fromDoc(doc) : null;
1625
+ }
1626
+ async listTasks(filter) {
1627
+ const query = {};
1628
+ if (filter.status) {
1629
+ const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
1630
+ query.status = statuses.length === 1 ? statuses[0] : { $in: statuses };
1631
+ }
1632
+ if (filter.agentId) query.agent_id = filter.agentId;
1633
+ if (filter.threadId) query.thread_id = filter.threadId;
1634
+ if (filter.resourceId) query.resource_id = filter.resourceId;
1635
+ if (filter.runId) query.run_id = filter.runId;
1636
+ if (filter.toolName) query.tool_name = filter.toolName;
1637
+ const dateCol = filter.dateFilterBy === "startedAt" ? "startedAt" : filter.dateFilterBy === "completedAt" ? "completedAt" : "createdAt";
1638
+ if (filter.fromDate) {
1639
+ query[dateCol] = { ...query[dateCol] || {}, $gte: filter.fromDate.toISOString() };
1640
+ }
1641
+ if (filter.toDate) {
1642
+ query[dateCol] = { ...query[dateCol] || {}, $lt: filter.toDate.toISOString() };
1643
+ }
1644
+ const orderCol = filter.orderBy === "startedAt" ? "startedAt" : filter.orderBy === "completedAt" ? "completedAt" : "createdAt";
1645
+ const direction = filter.orderDirection === "desc" ? -1 : 1;
1646
+ const collection = await this.getCollection();
1647
+ const total = await collection.countDocuments(query);
1648
+ let cursor = collection.find(query).sort({ [orderCol]: direction });
1649
+ if (filter.perPage != null) {
1650
+ if (filter.page != null) {
1651
+ cursor = cursor.skip(filter.page * filter.perPage);
1652
+ }
1653
+ cursor = cursor.limit(filter.perPage);
1654
+ }
1655
+ const docs = await cursor.toArray();
1656
+ return { tasks: docs.map(fromDoc), total };
1657
+ }
1658
+ async deleteTask(taskId) {
1659
+ const collection = await this.getCollection();
1660
+ await collection.deleteOne({ id: taskId });
1661
+ }
1662
+ async deleteTasks(filter) {
1663
+ const query = {};
1664
+ if (filter.status) {
1665
+ const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
1666
+ query.status = statuses.length === 1 ? statuses[0] : { $in: statuses };
1667
+ }
1668
+ const dateCol = filter.dateFilterBy === "startedAt" ? "startedAt" : filter.dateFilterBy === "completedAt" ? "completedAt" : "createdAt";
1669
+ if (filter.fromDate) {
1670
+ query[dateCol] = { ...query[dateCol] || {}, $gte: filter.fromDate.toISOString() };
1671
+ }
1672
+ if (filter.toDate) {
1673
+ query[dateCol] = { ...query[dateCol] || {}, $lt: filter.toDate.toISOString() };
1674
+ }
1675
+ if (filter.agentId) query.agent_id = filter.agentId;
1676
+ if (filter.threadId) query.thread_id = filter.threadId;
1677
+ if (filter.resourceId) query.resource_id = filter.resourceId;
1678
+ if (filter.runId) query.run_id = filter.runId;
1679
+ if (filter.toolName) query.tool_name = filter.toolName;
1680
+ if (Object.keys(query).length === 0) return;
1681
+ const collection = await this.getCollection();
1682
+ await collection.deleteMany(query);
1683
+ }
1684
+ async getRunningCount() {
1685
+ const collection = await this.getCollection();
1686
+ return collection.countDocuments({ status: "running" });
1687
+ }
1688
+ async getRunningCountByAgent(agentId) {
1689
+ const collection = await this.getCollection();
1690
+ return collection.countDocuments({ status: "running", agent_id: agentId });
1691
+ }
1692
+ };
1508
1693
  var MongoDBBlobStore = class _MongoDBBlobStore extends storage.BlobStore {
1509
1694
  #connector;
1510
1695
  #skipDefaultIndexes;
@@ -6465,6 +6650,44 @@ Note: This migration may take some time for large collections.
6465
6650
  );
6466
6651
  }
6467
6652
  }
6653
+ async getTraceLight(args) {
6654
+ const { traceId } = args;
6655
+ try {
6656
+ const collection = await this.getCollection(storage.TABLE_SPANS);
6657
+ const spans = await collection.find(
6658
+ { traceId },
6659
+ {
6660
+ projection: {
6661
+ input: 0,
6662
+ output: 0,
6663
+ attributes: 0,
6664
+ metadata: 0,
6665
+ tags: 0,
6666
+ links: 0
6667
+ }
6668
+ }
6669
+ ).sort({ startedAt: 1 }).toArray();
6670
+ if (!spans || spans.length === 0) {
6671
+ return null;
6672
+ }
6673
+ return {
6674
+ traceId,
6675
+ spans: spans.map((span) => this.transformSpanFromMongo(span))
6676
+ };
6677
+ } catch (error$1) {
6678
+ throw new error.MastraError(
6679
+ {
6680
+ id: storage.createStorageErrorId("MONGODB", "GET_TRACE_LIGHT", "FAILED"),
6681
+ domain: error.ErrorDomain.STORAGE,
6682
+ category: error.ErrorCategory.USER,
6683
+ details: {
6684
+ traceId
6685
+ }
6686
+ },
6687
+ error$1
6688
+ );
6689
+ }
6690
+ }
6468
6691
  async updateSpan(args) {
6469
6692
  const { traceId, spanId, updates } = args;
6470
6693
  try {
@@ -9883,6 +10106,7 @@ var MongoDBStore = class extends storage.MastraCompositeStore {
9883
10106
  const blobs = new MongoDBBlobStore(domainConfig);
9884
10107
  const datasets = new MongoDBDatasetsStorage(domainConfig);
9885
10108
  const experiments = new MongoDBExperimentsStorage(domainConfig);
10109
+ const backgroundTasks = new BackgroundTasksStorageMongoDB(domainConfig);
9886
10110
  this.stores = {
9887
10111
  memory,
9888
10112
  scores,
@@ -9896,6 +10120,7 @@ var MongoDBStore = class extends storage.MastraCompositeStore {
9896
10120
  workspaces,
9897
10121
  skills,
9898
10122
  blobs,
10123
+ backgroundTasks,
9899
10124
  datasets,
9900
10125
  experiments
9901
10126
  };
@@ -10016,6 +10241,7 @@ Example Complex Query:
10016
10241
  ]
10017
10242
  }`;
10018
10243
 
10244
+ exports.BackgroundTasksStorageMongoDB = BackgroundTasksStorageMongoDB;
10019
10245
  exports.MONGODB_PROMPT = MONGODB_PROMPT;
10020
10246
  exports.MemoryStorageMongoDB = MemoryStorageMongoDB;
10021
10247
  exports.MongoDBAgentsStorage = MongoDBAgentsStorage;