@mastra/mongodb 1.7.1 → 1.7.2-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,14 @@
1
1
  # @mastra/mongodb
2
2
 
3
+ ## 1.7.2-alpha.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Add `BackgroundTasksStorage` domain implementation so `@mastra/core` background task execution works with any storage adapter. ([#15307](https://github.com/mastra-ai/mastra/pull/15307))
8
+
9
+ - Updated dependencies [[`d63ffdb`](https://github.com/mastra-ai/mastra/commit/d63ffdbb2c11e76fe5ea45faab44bc15460f010c)]:
10
+ - @mastra/core@1.25.1-alpha.0
11
+
3
12
  ## 1.7.1
4
13
 
5
14
  ### 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.0"
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.0",
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.0"};
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;
@@ -9883,6 +10068,7 @@ var MongoDBStore = class extends storage.MastraCompositeStore {
9883
10068
  const blobs = new MongoDBBlobStore(domainConfig);
9884
10069
  const datasets = new MongoDBDatasetsStorage(domainConfig);
9885
10070
  const experiments = new MongoDBExperimentsStorage(domainConfig);
10071
+ const backgroundTasks = new BackgroundTasksStorageMongoDB(domainConfig);
9886
10072
  this.stores = {
9887
10073
  memory,
9888
10074
  scores,
@@ -9896,6 +10082,7 @@ var MongoDBStore = class extends storage.MastraCompositeStore {
9896
10082
  workspaces,
9897
10083
  skills,
9898
10084
  blobs,
10085
+ backgroundTasks,
9899
10086
  datasets,
9900
10087
  experiments
9901
10088
  };
@@ -10016,6 +10203,7 @@ Example Complex Query:
10016
10203
  ]
10017
10204
  }`;
10018
10205
 
10206
+ exports.BackgroundTasksStorageMongoDB = BackgroundTasksStorageMongoDB;
10019
10207
  exports.MONGODB_PROMPT = MONGODB_PROMPT;
10020
10208
  exports.MemoryStorageMongoDB = MemoryStorageMongoDB;
10021
10209
  exports.MongoDBAgentsStorage = MongoDBAgentsStorage;