@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/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
2
- import { createVectorErrorId, AgentsStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, createStorageErrorId, normalizePerPage, calculatePagination, BlobStore, TABLE_SKILL_BLOBS, DatasetsStorage, TABLE_DATASETS, TABLE_DATASET_ITEMS, TABLE_DATASET_VERSIONS, ensureDate, safelyParseJSON, TABLE_EXPERIMENTS, TABLE_EXPERIMENT_RESULTS, ExperimentsStorage, MCPClientsStorage, TABLE_MCP_CLIENTS, TABLE_MCP_CLIENT_VERSIONS, MCPServersStorage, TABLE_MCP_SERVERS, TABLE_MCP_SERVER_VERSIONS, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, ObservabilityStorage, TABLE_SPANS, listTracesArgsSchema, toTraceSpans, PromptBlocksStorage, TABLE_PROMPT_BLOCKS, TABLE_PROMPT_BLOCK_VERSIONS, ScorerDefinitionsStorage, TABLE_SCORER_DEFINITIONS, TABLE_SCORER_DEFINITION_VERSIONS, ScoresStorage, TABLE_SCORERS, SkillsStorage, TABLE_SKILLS, TABLE_SKILL_VERSIONS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, WorkspacesStorage, TABLE_WORKSPACES, TABLE_WORKSPACE_VERSIONS, MastraCompositeStore, TraceStatus, transformScoreRow as transformScoreRow$1 } from '@mastra/core/storage';
2
+ import { createVectorErrorId, AgentsStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, createStorageErrorId, normalizePerPage, calculatePagination, BackgroundTasksStorage, TABLE_BACKGROUND_TASKS, BlobStore, TABLE_SKILL_BLOBS, DatasetsStorage, TABLE_DATASETS, TABLE_DATASET_ITEMS, TABLE_DATASET_VERSIONS, ensureDate, safelyParseJSON, TABLE_EXPERIMENTS, TABLE_EXPERIMENT_RESULTS, ExperimentsStorage, MCPClientsStorage, TABLE_MCP_CLIENTS, TABLE_MCP_CLIENT_VERSIONS, MCPServersStorage, TABLE_MCP_SERVERS, TABLE_MCP_SERVER_VERSIONS, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, ObservabilityStorage, TABLE_SPANS, listTracesArgsSchema, toTraceSpans, PromptBlocksStorage, TABLE_PROMPT_BLOCKS, TABLE_PROMPT_BLOCK_VERSIONS, ScorerDefinitionsStorage, TABLE_SCORER_DEFINITIONS, TABLE_SCORER_DEFINITION_VERSIONS, ScoresStorage, TABLE_SCORERS, SkillsStorage, TABLE_SKILLS, TABLE_SKILL_VERSIONS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, WorkspacesStorage, TABLE_WORKSPACES, TABLE_WORKSPACE_VERSIONS, MastraCompositeStore, TraceStatus, transformScoreRow as transformScoreRow$1 } from '@mastra/core/storage';
3
3
  import { MastraVector, validateUpsertInput, validateVectorValues } from '@mastra/core/vector';
4
4
  import { MongoClient } from 'mongodb';
5
5
  import { v4 } from 'uuid';
@@ -12,7 +12,7 @@ import { saveScorePayloadSchema } from '@mastra/core/evals';
12
12
 
13
13
  // package.json
14
14
  var package_default = {
15
- version: "1.7.1"};
15
+ version: "1.7.2-alpha.1"};
16
16
  var MongoDBFilterTranslator = class extends BaseFilterTranslator {
17
17
  getSupportedOperators() {
18
18
  return {
@@ -1503,6 +1503,191 @@ var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends AgentsStorage {
1503
1503
  return result;
1504
1504
  }
1505
1505
  };
1506
+ function toDoc(task) {
1507
+ return {
1508
+ id: task.id,
1509
+ tool_call_id: task.toolCallId,
1510
+ tool_name: task.toolName,
1511
+ agent_id: task.agentId,
1512
+ thread_id: task.threadId ?? null,
1513
+ resource_id: task.resourceId ?? null,
1514
+ run_id: task.runId,
1515
+ status: task.status,
1516
+ args: task.args,
1517
+ result: task.result ?? null,
1518
+ error: task.error ?? null,
1519
+ retry_count: task.retryCount,
1520
+ max_retries: task.maxRetries,
1521
+ timeout_ms: task.timeoutMs,
1522
+ createdAt: task.createdAt.toISOString(),
1523
+ startedAt: task.startedAt?.toISOString() ?? null,
1524
+ completedAt: task.completedAt?.toISOString() ?? null
1525
+ };
1526
+ }
1527
+ function fromDoc(doc) {
1528
+ return {
1529
+ id: doc.id,
1530
+ status: doc.status,
1531
+ toolName: doc.tool_name,
1532
+ toolCallId: doc.tool_call_id,
1533
+ args: doc.args ?? {},
1534
+ agentId: doc.agent_id,
1535
+ threadId: doc.thread_id ?? void 0,
1536
+ resourceId: doc.resource_id ?? void 0,
1537
+ runId: doc.run_id ?? "",
1538
+ result: doc.result ?? void 0,
1539
+ error: doc.error ?? void 0,
1540
+ retryCount: Number(doc.retry_count ?? 0),
1541
+ maxRetries: Number(doc.max_retries ?? 0),
1542
+ timeoutMs: Number(doc.timeout_ms ?? 3e5),
1543
+ createdAt: new Date(doc.createdAt),
1544
+ startedAt: doc.startedAt ? new Date(doc.startedAt) : void 0,
1545
+ completedAt: doc.completedAt ? new Date(doc.completedAt) : void 0
1546
+ };
1547
+ }
1548
+ var BackgroundTasksStorageMongoDB = class _BackgroundTasksStorageMongoDB extends BackgroundTasksStorage {
1549
+ #connector;
1550
+ #skipDefaultIndexes;
1551
+ #indexes;
1552
+ static MANAGED_COLLECTIONS = [TABLE_BACKGROUND_TASKS];
1553
+ constructor(config) {
1554
+ super();
1555
+ this.#connector = resolveMongoDBConfig(config);
1556
+ this.#skipDefaultIndexes = config.skipDefaultIndexes;
1557
+ this.#indexes = config.indexes?.filter(
1558
+ (idx) => _BackgroundTasksStorageMongoDB.MANAGED_COLLECTIONS.includes(idx.collection)
1559
+ );
1560
+ }
1561
+ async getCollection() {
1562
+ return this.#connector.getCollection(TABLE_BACKGROUND_TASKS);
1563
+ }
1564
+ getDefaultIndexDefinitions() {
1565
+ return [
1566
+ { collection: TABLE_BACKGROUND_TASKS, keys: { id: 1 }, options: { unique: true } },
1567
+ { collection: TABLE_BACKGROUND_TASKS, keys: { status: 1, createdAt: 1 } },
1568
+ { collection: TABLE_BACKGROUND_TASKS, keys: { agent_id: 1, status: 1 } },
1569
+ { collection: TABLE_BACKGROUND_TASKS, keys: { thread_id: 1, createdAt: 1 } },
1570
+ { collection: TABLE_BACKGROUND_TASKS, keys: { tool_call_id: 1 } }
1571
+ ];
1572
+ }
1573
+ async createDefaultIndexes() {
1574
+ if (this.#skipDefaultIndexes) return;
1575
+ for (const indexDef of this.getDefaultIndexDefinitions()) {
1576
+ try {
1577
+ const collection = await this.getCollection();
1578
+ await collection.createIndex(indexDef.keys, indexDef.options);
1579
+ } catch (error) {
1580
+ this.logger?.warn?.(`Failed to create index on ${TABLE_BACKGROUND_TASKS}:`, error);
1581
+ }
1582
+ }
1583
+ }
1584
+ async createCustomIndexes() {
1585
+ if (!this.#indexes || this.#indexes.length === 0) return;
1586
+ for (const indexDef of this.#indexes) {
1587
+ try {
1588
+ const collection = await this.getCollection();
1589
+ await collection.createIndex(indexDef.keys, indexDef.options);
1590
+ } catch (error) {
1591
+ this.logger?.warn?.(`Failed to create custom index on ${TABLE_BACKGROUND_TASKS}:`, error);
1592
+ }
1593
+ }
1594
+ }
1595
+ async init() {
1596
+ await this.createDefaultIndexes();
1597
+ await this.createCustomIndexes();
1598
+ }
1599
+ async dangerouslyClearAll() {
1600
+ const collection = await this.getCollection();
1601
+ await collection.deleteMany({});
1602
+ }
1603
+ async createTask(task) {
1604
+ const collection = await this.getCollection();
1605
+ await collection.insertOne(toDoc(task));
1606
+ }
1607
+ async updateTask(taskId, update) {
1608
+ const $set = {};
1609
+ if ("status" in update) $set.status = update.status;
1610
+ if ("result" in update) $set.result = update.result ?? null;
1611
+ if ("error" in update) $set.error = update.error ?? null;
1612
+ if ("retryCount" in update) $set.retry_count = update.retryCount;
1613
+ if ("startedAt" in update) $set.startedAt = update.startedAt?.toISOString() ?? null;
1614
+ if ("completedAt" in update) $set.completedAt = update.completedAt?.toISOString() ?? null;
1615
+ if (Object.keys($set).length === 0) return;
1616
+ const collection = await this.getCollection();
1617
+ await collection.updateOne({ id: taskId }, { $set });
1618
+ }
1619
+ async getTask(taskId) {
1620
+ const collection = await this.getCollection();
1621
+ const doc = await collection.findOne({ id: taskId });
1622
+ return doc ? fromDoc(doc) : null;
1623
+ }
1624
+ async listTasks(filter) {
1625
+ const query = {};
1626
+ if (filter.status) {
1627
+ const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
1628
+ query.status = statuses.length === 1 ? statuses[0] : { $in: statuses };
1629
+ }
1630
+ if (filter.agentId) query.agent_id = filter.agentId;
1631
+ if (filter.threadId) query.thread_id = filter.threadId;
1632
+ if (filter.resourceId) query.resource_id = filter.resourceId;
1633
+ if (filter.runId) query.run_id = filter.runId;
1634
+ if (filter.toolName) query.tool_name = filter.toolName;
1635
+ const dateCol = filter.dateFilterBy === "startedAt" ? "startedAt" : filter.dateFilterBy === "completedAt" ? "completedAt" : "createdAt";
1636
+ if (filter.fromDate) {
1637
+ query[dateCol] = { ...query[dateCol] || {}, $gte: filter.fromDate.toISOString() };
1638
+ }
1639
+ if (filter.toDate) {
1640
+ query[dateCol] = { ...query[dateCol] || {}, $lt: filter.toDate.toISOString() };
1641
+ }
1642
+ const orderCol = filter.orderBy === "startedAt" ? "startedAt" : filter.orderBy === "completedAt" ? "completedAt" : "createdAt";
1643
+ const direction = filter.orderDirection === "desc" ? -1 : 1;
1644
+ const collection = await this.getCollection();
1645
+ const total = await collection.countDocuments(query);
1646
+ let cursor = collection.find(query).sort({ [orderCol]: direction });
1647
+ if (filter.perPage != null) {
1648
+ if (filter.page != null) {
1649
+ cursor = cursor.skip(filter.page * filter.perPage);
1650
+ }
1651
+ cursor = cursor.limit(filter.perPage);
1652
+ }
1653
+ const docs = await cursor.toArray();
1654
+ return { tasks: docs.map(fromDoc), total };
1655
+ }
1656
+ async deleteTask(taskId) {
1657
+ const collection = await this.getCollection();
1658
+ await collection.deleteOne({ id: taskId });
1659
+ }
1660
+ async deleteTasks(filter) {
1661
+ const query = {};
1662
+ if (filter.status) {
1663
+ const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
1664
+ query.status = statuses.length === 1 ? statuses[0] : { $in: statuses };
1665
+ }
1666
+ const dateCol = filter.dateFilterBy === "startedAt" ? "startedAt" : filter.dateFilterBy === "completedAt" ? "completedAt" : "createdAt";
1667
+ if (filter.fromDate) {
1668
+ query[dateCol] = { ...query[dateCol] || {}, $gte: filter.fromDate.toISOString() };
1669
+ }
1670
+ if (filter.toDate) {
1671
+ query[dateCol] = { ...query[dateCol] || {}, $lt: filter.toDate.toISOString() };
1672
+ }
1673
+ if (filter.agentId) query.agent_id = filter.agentId;
1674
+ if (filter.threadId) query.thread_id = filter.threadId;
1675
+ if (filter.resourceId) query.resource_id = filter.resourceId;
1676
+ if (filter.runId) query.run_id = filter.runId;
1677
+ if (filter.toolName) query.tool_name = filter.toolName;
1678
+ if (Object.keys(query).length === 0) return;
1679
+ const collection = await this.getCollection();
1680
+ await collection.deleteMany(query);
1681
+ }
1682
+ async getRunningCount() {
1683
+ const collection = await this.getCollection();
1684
+ return collection.countDocuments({ status: "running" });
1685
+ }
1686
+ async getRunningCountByAgent(agentId) {
1687
+ const collection = await this.getCollection();
1688
+ return collection.countDocuments({ status: "running", agent_id: agentId });
1689
+ }
1690
+ };
1506
1691
  var MongoDBBlobStore = class _MongoDBBlobStore extends BlobStore {
1507
1692
  #connector;
1508
1693
  #skipDefaultIndexes;
@@ -6463,6 +6648,44 @@ Note: This migration may take some time for large collections.
6463
6648
  );
6464
6649
  }
6465
6650
  }
6651
+ async getTraceLight(args) {
6652
+ const { traceId } = args;
6653
+ try {
6654
+ const collection = await this.getCollection(TABLE_SPANS);
6655
+ const spans = await collection.find(
6656
+ { traceId },
6657
+ {
6658
+ projection: {
6659
+ input: 0,
6660
+ output: 0,
6661
+ attributes: 0,
6662
+ metadata: 0,
6663
+ tags: 0,
6664
+ links: 0
6665
+ }
6666
+ }
6667
+ ).sort({ startedAt: 1 }).toArray();
6668
+ if (!spans || spans.length === 0) {
6669
+ return null;
6670
+ }
6671
+ return {
6672
+ traceId,
6673
+ spans: spans.map((span) => this.transformSpanFromMongo(span))
6674
+ };
6675
+ } catch (error) {
6676
+ throw new MastraError(
6677
+ {
6678
+ id: createStorageErrorId("MONGODB", "GET_TRACE_LIGHT", "FAILED"),
6679
+ domain: ErrorDomain.STORAGE,
6680
+ category: ErrorCategory.USER,
6681
+ details: {
6682
+ traceId
6683
+ }
6684
+ },
6685
+ error
6686
+ );
6687
+ }
6688
+ }
6466
6689
  async updateSpan(args) {
6467
6690
  const { traceId, spanId, updates } = args;
6468
6691
  try {
@@ -9881,6 +10104,7 @@ var MongoDBStore = class extends MastraCompositeStore {
9881
10104
  const blobs = new MongoDBBlobStore(domainConfig);
9882
10105
  const datasets = new MongoDBDatasetsStorage(domainConfig);
9883
10106
  const experiments = new MongoDBExperimentsStorage(domainConfig);
10107
+ const backgroundTasks = new BackgroundTasksStorageMongoDB(domainConfig);
9884
10108
  this.stores = {
9885
10109
  memory,
9886
10110
  scores,
@@ -9894,6 +10118,7 @@ var MongoDBStore = class extends MastraCompositeStore {
9894
10118
  workspaces,
9895
10119
  skills,
9896
10120
  blobs,
10121
+ backgroundTasks,
9897
10122
  datasets,
9898
10123
  experiments
9899
10124
  };
@@ -10014,6 +10239,6 @@ Example Complex Query:
10014
10239
  ]
10015
10240
  }`;
10016
10241
 
10017
- export { MONGODB_PROMPT, MemoryStorageMongoDB, MongoDBAgentsStorage, MongoDBBlobStore, MongoDBDatasetsStorage, MongoDBExperimentsStorage, MongoDBMCPClientsStorage, MongoDBMCPServersStorage, MongoDBPromptBlocksStorage, MongoDBScorerDefinitionsStorage, MongoDBSkillsStorage, MongoDBStore, MongoDBVector, MongoDBWorkspacesStorage, ObservabilityMongoDB, ScoresStorageMongoDB, WorkflowsStorageMongoDB };
10242
+ export { BackgroundTasksStorageMongoDB, MONGODB_PROMPT, MemoryStorageMongoDB, MongoDBAgentsStorage, MongoDBBlobStore, MongoDBDatasetsStorage, MongoDBExperimentsStorage, MongoDBMCPClientsStorage, MongoDBMCPServersStorage, MongoDBPromptBlocksStorage, MongoDBScorerDefinitionsStorage, MongoDBSkillsStorage, MongoDBStore, MongoDBVector, MongoDBWorkspacesStorage, ObservabilityMongoDB, ScoresStorageMongoDB, WorkflowsStorageMongoDB };
10018
10243
  //# sourceMappingURL=index.js.map
10019
10244
  //# sourceMappingURL=index.js.map