@mastra/mongodb 1.16.0-alpha.0 → 1.16.0-alpha.2

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,6 +1,6 @@
1
1
  import { v4 } from "@lukeed/uuid";
2
2
  import { ErrorCategory, ErrorDomain, MastraError } from "@mastra/core/error";
3
- import { AgentsStorage, BackgroundTasksStorage, BlobStore, DatasetsStorage, ExperimentsStorage, MCPClientsStorage, MCPServersStorage, MastraCompositeStore, MemoryStorage, NotificationsStorage, ObservabilityStorage, PromptBlocksStorage, SchedulesStorage, ScorerDefinitionsStorage, ScoresStorage, SkillsStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, TABLE_BACKGROUND_TASKS, TABLE_DATASETS, TABLE_DATASET_ITEMS, TABLE_DATASET_VERSIONS, TABLE_EXPERIMENTS, TABLE_EXPERIMENT_RESULTS, TABLE_MCP_CLIENTS, TABLE_MCP_CLIENT_VERSIONS, TABLE_MCP_SERVERS, TABLE_MCP_SERVER_VERSIONS, TABLE_MESSAGES, TABLE_NOTIFICATIONS, TABLE_PROMPT_BLOCKS, TABLE_PROMPT_BLOCK_VERSIONS, TABLE_RESOURCES, TABLE_SCHEDULES, TABLE_SCHEDULE_TRIGGERS, TABLE_SCORERS, TABLE_SCORER_DEFINITIONS, TABLE_SCORER_DEFINITION_VERSIONS, TABLE_SKILLS, TABLE_SKILL_BLOBS, TABLE_SKILL_VERSIONS, TABLE_SPANS, TABLE_THREADS, TABLE_WORKFLOW_SNAPSHOT, TABLE_WORKSPACES, TABLE_WORKSPACE_VERSIONS, TraceStatus, WorkflowsStorage, WorkspacesStorage, calculatePagination, createStorageErrorId, createVectorErrorId, ensureDate, hasErrorCode, listTracesArgsSchema, normalizePerPage, normalizeScheduleTarget, parseDuration, safelyParseJSON, storageMessageMatchesMetadataFilter, toTraceSpans, transformScoreRow, validateStorageMetadataFilter } from "@mastra/core/storage";
3
+ import { AgentsStorage, BackgroundTasksStorage, BlobStore, DatasetsStorage, ExperimentsStorage, MCPClientsStorage, MCPServersStorage, MastraCompositeStore, MemoryStorage, NotificationsStorage, ObservabilityStorage, PromptBlocksStorage, SchedulesStorage, ScorerDefinitionsStorage, ScoresStorage, SkillsStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, TABLE_BACKGROUND_TASKS, TABLE_DATASETS, TABLE_DATASET_ITEMS, TABLE_DATASET_VERSIONS, TABLE_EXPERIMENTS, TABLE_EXPERIMENT_RESULTS, TABLE_MCP_CLIENTS, TABLE_MCP_CLIENT_VERSIONS, TABLE_MCP_SERVERS, TABLE_MCP_SERVER_VERSIONS, TABLE_MESSAGES, TABLE_NOTIFICATIONS, TABLE_PROMPT_BLOCKS, TABLE_PROMPT_BLOCK_VERSIONS, TABLE_RESOURCES, TABLE_SCHEDULES, TABLE_SCHEDULE_TRIGGERS, TABLE_SCORERS, TABLE_SCORER_DEFINITIONS, TABLE_SCORER_DEFINITION_VERSIONS, TABLE_SKILLS, TABLE_SKILL_BLOBS, TABLE_SKILL_VERSIONS, TABLE_SPANS, TABLE_THREADS, TABLE_WORKFLOW_DEFINITIONS, TABLE_WORKFLOW_SNAPSHOT, TABLE_WORKSPACES, TABLE_WORKSPACE_VERSIONS, TraceStatus, WorkflowDefinitionsStorage, WorkflowsStorage, WorkspacesStorage, calculatePagination, createStorageErrorId, createVectorErrorId, ensureDate, hasErrorCode, listTracesArgsSchema, normalizePerPage, normalizeScheduleTarget, parseDuration, safelyParseJSON, storageMessageMatchesMetadataFilter, toTraceSpans, transformScoreRow, validateStorageMetadataFilter } from "@mastra/core/storage";
4
4
  import { MastraVector, validateUpsertInput, validateVectorValues } from "@mastra/core/vector";
5
5
  import { MongoBulkWriteError, MongoClient, ObjectId } from "mongodb";
6
6
  import { BaseFilterTranslator } from "@mastra/core/vector/filter";
@@ -9,7 +9,7 @@ import { MessageList } from "@mastra/core/agent";
9
9
  import { saveScorePayloadSchema } from "@mastra/core/evals";
10
10
  import { skillSnapshotFieldValuesEqual } from "@mastra/core/storage/domains/skills";
11
11
  //#region package.json
12
- var version = "1.16.0-alpha.0";
12
+ var version = "1.16.0-alpha.2";
13
13
  //#endregion
14
14
  //#region src/vector/filter.ts
15
15
  /**
@@ -5328,11 +5328,15 @@ var MemoryStorageMongoDB = class MemoryStorageMongoDB extends MemoryStorage {
5328
5328
  for (const indexDef of this.getDefaultIndexDefinitions()) try {
5329
5329
  await (await this.getCollection(indexDef.collection)).createIndex(indexDef.keys, indexDef.options);
5330
5330
  } catch (error) {
5331
+ const isUniqueConflict = error?.code === 85 && indexDef.options?.unique === true;
5332
+ const field = Object.keys(indexDef.keys)[0] ?? "id";
5333
+ const indexName = Object.entries(indexDef.keys).map(([k, v]) => `${k}_${v}`).join("_");
5334
+ const text = isUniqueConflict ? `Index conflict on collection "${indexDef.collection}": an existing non-unique index on { ${field}: 1 } conflicts with Mastra's required unique index.\n\nTo migrate:\n 1. Check for duplicates: db.${indexDef.collection}.aggregate([{ $group: { _id: "$${field}", n: { $sum: 1 } } }, { $match: { n: { $gt: 1 } } }])\n 2. Drop the old index: db.${indexDef.collection}.dropIndex("${indexName}")\n 3. Recreate as unique: db.${indexDef.collection}.createIndex({ ${field}: 1 }, { unique: true })\n\nAlternatively, set skipDefaultIndexes: true to manage indexes yourself.` : `Failed to create default index on collection "${indexDef.collection}". Set skipDefaultIndexes to manage indexes yourself.`;
5331
5335
  throw new MastraError({
5332
5336
  id: createStorageErrorId("MONGODB", "CREATE_DEFAULT_INDEXES", "FAILED"),
5333
5337
  domain: ErrorDomain.STORAGE,
5334
5338
  category: ErrorCategory.THIRD_PARTY,
5335
- text: `Failed to create default index on collection "${indexDef.collection}". Set skipDefaultIndexes to manage indexes yourself.`,
5339
+ text,
5336
5340
  details: { collection: indexDef.collection }
5337
5341
  }, error);
5338
5342
  }
@@ -9931,6 +9935,141 @@ var MongoDBSkillsStorage = class MongoDBSkillsStorage extends SkillsStorage {
9931
9935
  }
9932
9936
  };
9933
9937
  //#endregion
9938
+ //#region src/storage/domains/workflow-definitions/index.ts
9939
+ function docToDefinition(doc) {
9940
+ const def = {
9941
+ id: String(doc.id),
9942
+ inputSchema: doc.inputSchema,
9943
+ outputSchema: doc.outputSchema,
9944
+ graph: doc.graph,
9945
+ status: doc.status,
9946
+ source: doc.source,
9947
+ createdAt: doc.createdAt instanceof Date ? doc.createdAt : new Date(doc.createdAt),
9948
+ updatedAt: doc.updatedAt instanceof Date ? doc.updatedAt : new Date(doc.updatedAt)
9949
+ };
9950
+ if (doc.description != null) def.description = doc.description;
9951
+ if (doc.metadata != null) def.metadata = doc.metadata;
9952
+ if (doc.stateSchema != null) def.stateSchema = doc.stateSchema;
9953
+ if (doc.requestContextSchema != null) def.requestContextSchema = doc.requestContextSchema;
9954
+ if (doc.authorId != null) def.authorId = doc.authorId;
9955
+ return def;
9956
+ }
9957
+ var MongoDBWorkflowDefinitionsStore = class MongoDBWorkflowDefinitionsStore extends WorkflowDefinitionsStorage {
9958
+ #connector;
9959
+ #skipDefaultIndexes;
9960
+ #indexes;
9961
+ static MANAGED_COLLECTIONS = [TABLE_WORKFLOW_DEFINITIONS];
9962
+ constructor(config) {
9963
+ super();
9964
+ this.#connector = resolveMongoDBConfig(config);
9965
+ this.#skipDefaultIndexes = config.skipDefaultIndexes;
9966
+ this.#indexes = config.indexes?.filter((idx) => MongoDBWorkflowDefinitionsStore.MANAGED_COLLECTIONS.includes(idx.collection));
9967
+ }
9968
+ async getCollection(name) {
9969
+ return this.#connector.getCollection(name);
9970
+ }
9971
+ getDefaultIndexDefinitions() {
9972
+ return [{
9973
+ collection: TABLE_WORKFLOW_DEFINITIONS,
9974
+ keys: { id: 1 },
9975
+ options: { unique: true }
9976
+ }, {
9977
+ collection: TABLE_WORKFLOW_DEFINITIONS,
9978
+ keys: { status: 1 }
9979
+ }];
9980
+ }
9981
+ async createDefaultIndexes() {
9982
+ if (this.#skipDefaultIndexes) return;
9983
+ for (const indexDef of this.getDefaultIndexDefinitions()) try {
9984
+ await (await this.getCollection(indexDef.collection)).createIndex(indexDef.keys, indexDef.options);
9985
+ } catch (error) {
9986
+ this.logger?.warn?.(`Failed to create index on ${indexDef.collection}:`, error);
9987
+ }
9988
+ }
9989
+ async createCustomIndexes() {
9990
+ if (!this.#indexes || this.#indexes.length === 0) return;
9991
+ for (const indexDef of this.#indexes) try {
9992
+ await (await this.getCollection(indexDef.collection)).createIndex(indexDef.keys, indexDef.options);
9993
+ } catch (error) {
9994
+ this.logger?.warn?.(`Failed to create custom index on ${indexDef.collection}:`, error);
9995
+ }
9996
+ }
9997
+ async init() {
9998
+ await this.createDefaultIndexes();
9999
+ await this.createCustomIndexes();
10000
+ }
10001
+ async dangerouslyClearAll() {
10002
+ await (await this.getCollection(TABLE_WORKFLOW_DEFINITIONS)).deleteMany({});
10003
+ }
10004
+ async upsert(input) {
10005
+ const now = /* @__PURE__ */ new Date();
10006
+ const collection = await this.getCollection(TABLE_WORKFLOW_DEFINITIONS);
10007
+ if (!await collection.findOne({ id: input.id })) {
10008
+ if (!("inputSchema" in input) || input.inputSchema === void 0) throw new Error(`Cannot create workflow definition "${input.id}": inputSchema is required.`);
10009
+ if (!("outputSchema" in input) || input.outputSchema === void 0) throw new Error(`Cannot create workflow definition "${input.id}": outputSchema is required.`);
10010
+ if (!("graph" in input) || input.graph === void 0) throw new Error(`Cannot create workflow definition "${input.id}": graph is required.`);
10011
+ const doc = {
10012
+ id: input.id,
10013
+ description: input.description ?? null,
10014
+ metadata: input.metadata ?? null,
10015
+ inputSchema: input.inputSchema,
10016
+ outputSchema: input.outputSchema,
10017
+ stateSchema: input.stateSchema ?? null,
10018
+ requestContextSchema: input.requestContextSchema ?? null,
10019
+ graph: input.graph,
10020
+ status: "active",
10021
+ source: "storage",
10022
+ authorId: "authorId" in input ? input.authorId ?? null : null,
10023
+ createdAt: now,
10024
+ updatedAt: now
10025
+ };
10026
+ try {
10027
+ await collection.insertOne(doc);
10028
+ } catch (error) {
10029
+ if (!(error?.code === 11e3) || !await collection.findOne({ id: input.id })) throw error;
10030
+ return this.applyUpdate(input, now);
10031
+ }
10032
+ return docToDefinition(doc);
10033
+ }
10034
+ return this.applyUpdate(input, now);
10035
+ }
10036
+ async applyUpdate(input, now) {
10037
+ const collection = await this.getCollection(TABLE_WORKFLOW_DEFINITIONS);
10038
+ const update = { updatedAt: now };
10039
+ if ("description" in input && input.description !== void 0) update.description = input.description;
10040
+ if ("metadata" in input && input.metadata !== void 0) update.metadata = input.metadata;
10041
+ if ("inputSchema" in input && input.inputSchema !== void 0) update.inputSchema = input.inputSchema;
10042
+ if ("outputSchema" in input && input.outputSchema !== void 0) update.outputSchema = input.outputSchema;
10043
+ if ("stateSchema" in input && input.stateSchema !== void 0) update.stateSchema = input.stateSchema;
10044
+ if ("requestContextSchema" in input && input.requestContextSchema !== void 0) update.requestContextSchema = input.requestContextSchema;
10045
+ if ("graph" in input && input.graph !== void 0) update.graph = input.graph;
10046
+ if ("status" in input && input.status !== void 0) update.status = input.status;
10047
+ if ("authorId" in input && input.authorId !== void 0) update.authorId = input.authorId;
10048
+ await collection.updateOne({ id: input.id }, { $set: update });
10049
+ const updated = await collection.findOne({ id: input.id });
10050
+ if (!updated) throw new Error(`Failed to update workflow definition "${input.id}".`);
10051
+ return docToDefinition(updated);
10052
+ }
10053
+ async get(id) {
10054
+ const doc = await (await this.getCollection(TABLE_WORKFLOW_DEFINITIONS)).findOne({ id });
10055
+ return doc ? docToDefinition(doc) : null;
10056
+ }
10057
+ async list(args) {
10058
+ const collection = await this.getCollection(TABLE_WORKFLOW_DEFINITIONS);
10059
+ const filter = {};
10060
+ if (args?.status) filter.status = args.status;
10061
+ if (args?.authorId !== void 0) filter.authorId = args.authorId;
10062
+ const definitions = (await collection.find(filter).sort({ updatedAt: -1 }).toArray()).map(docToDefinition);
10063
+ return {
10064
+ definitions,
10065
+ total: definitions.length
10066
+ };
10067
+ }
10068
+ async delete(id) {
10069
+ await (await this.getCollection(TABLE_WORKFLOW_DEFINITIONS)).deleteOne({ id });
10070
+ }
10071
+ };
10072
+ //#endregion
9934
10073
  //#region src/storage/domains/workflows/index.ts
9935
10074
  var WorkflowsStorageMongoDB = class WorkflowsStorageMongoDB extends WorkflowsStorage {
9936
10075
  #connector;
@@ -10819,6 +10958,7 @@ var MongoDBStore = class extends MastraCompositeStore {
10819
10958
  const experiments = new MongoDBExperimentsStorage(domainConfig);
10820
10959
  const backgroundTasks = new BackgroundTasksStorageMongoDB(domainConfig);
10821
10960
  const schedules = new SchedulesMongoDB(domainConfig);
10961
+ const workflowDefinitions = new MongoDBWorkflowDefinitionsStore(domainConfig);
10822
10962
  this.stores = {
10823
10963
  memory,
10824
10964
  notifications,
@@ -10836,7 +10976,8 @@ var MongoDBStore = class extends MastraCompositeStore {
10836
10976
  backgroundTasks,
10837
10977
  datasets,
10838
10978
  experiments,
10839
- schedules
10979
+ schedules,
10980
+ workflowDefinitions
10840
10981
  };
10841
10982
  }
10842
10983
  /**
@@ -10956,6 +11097,6 @@ Example Complex Query:
10956
11097
  ]
10957
11098
  }`;
10958
11099
  //#endregion
10959
- export { BackgroundTasksStorageMongoDB, MONGODB_PROMPT, MemoryStorageMongoDB, MongoDBAgentsStorage, MongoDBBlobStore, MongoDBDatasetsStorage, MongoDBExperimentsStorage, MongoDBMCPClientsStorage, MongoDBMCPServersStorage, MongoDBPromptBlocksStorage, MongoDBScorerDefinitionsStorage, MongoDBSkillsStorage, MongoDBStore, MongoDBVector, MongoDBWorkspacesStorage, NotificationsMongoDB, ObservabilityMongoDB, SchedulesMongoDB, ScoresStorageMongoDB, WorkflowsStorageMongoDB };
11100
+ export { BackgroundTasksStorageMongoDB, MONGODB_PROMPT, MemoryStorageMongoDB, MongoDBAgentsStorage, MongoDBBlobStore, MongoDBDatasetsStorage, MongoDBExperimentsStorage, MongoDBMCPClientsStorage, MongoDBMCPServersStorage, MongoDBPromptBlocksStorage, MongoDBScorerDefinitionsStorage, MongoDBSkillsStorage, MongoDBStore, MongoDBVector, MongoDBWorkflowDefinitionsStore, MongoDBWorkspacesStorage, NotificationsMongoDB, ObservabilityMongoDB, SchedulesMongoDB, ScoresStorageMongoDB, WorkflowsStorageMongoDB };
10960
11101
 
10961
11102
  //# sourceMappingURL=index.js.map