@mastra/mongodb 1.22.1-alpha.0 → 1.23.0-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.
@@ -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.22.1-alpha.0"
6
+ version: "1.23.0-alpha.1"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.22.1-alpha.0",
2
+ "version": "1.23.0-alpha.1",
3
3
  "package": "@mastra/mongodb",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -9,7 +9,7 @@ let _mastra_core_agent = require("@mastra/core/agent");
9
9
  let _mastra_core_evals = require("@mastra/core/evals");
10
10
  let _mastra_core_storage_domains_skills = require("@mastra/core/storage/domains/skills");
11
11
  //#region package.json
12
- var version = "1.22.1-alpha.0";
12
+ var version = "1.23.0-alpha.1";
13
13
  //#endregion
14
14
  //#region src/vector/filter.ts
15
15
  /**
@@ -4764,13 +4764,26 @@ async function assertKnowledgeDescriptionWithinBoundCompat(description) {
4764
4764
  }
4765
4765
  assertWithinBound(description);
4766
4766
  }
4767
+ let assertRecordTextWithinBound;
4768
+ async function assertKnowledgeRecordTextWithinBoundCompat(text) {
4769
+ if (!assertRecordTextWithinBound) assertRecordTextWithinBound = (await import("@mastra/core/storage")).assertKnowledgeRecordTextWithinBound ?? ((value) => {
4770
+ if (value.length > 1e3) throw new Error("Knowledge record text exceeds the 1000 UTF-16 code unit limit; split it into separate facts or summarize it");
4771
+ });
4772
+ assertRecordTextWithinBound(text);
4773
+ }
4767
4774
  const cloneScope = (scope) => [...scope];
4768
4775
  const canonicalName = (name) => name.trim().toLocaleLowerCase();
4769
4776
  const nodeReferenceId = (node) => typeof node === "string" ? node : node.id;
4770
4777
  const sessionOptions = (session) => session ? { session } : {};
4771
4778
  function visibleScopeKeys(scope) {
4772
4779
  const canonical = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(scope);
4773
- return canonical.map((_, index) => (0, _mastra_core_storage.knowledgeScopeKey)(canonical.slice(0, index + 1)));
4780
+ const subsets = [[]];
4781
+ for (const entry of canonical) subsets.push(...subsets.map((subset) => [...subset, entry]));
4782
+ const keys = /* @__PURE__ */ new Set();
4783
+ for (const subset of subsets.slice(1)) try {
4784
+ keys.add((0, _mastra_core_storage.knowledgeScopeKey)(subset));
4785
+ } catch {}
4786
+ return [...keys];
4774
4787
  }
4775
4788
  function recordCursorFilter(cursor, expected) {
4776
4789
  const parsed = (0, _mastra_core_storage.parseKnowledgeNodeCursor)(cursor, expected);
@@ -4806,7 +4819,6 @@ function recordFromDocument(row) {
4806
4819
  sourceThreadId: String(row.sourceThreadId),
4807
4820
  capturedAt: new Date(row.capturedAt),
4808
4821
  when: row.when ? new Date(row.when) : void 0,
4809
- maxScope: row.maxScope ?? void 0,
4810
4822
  metadata: row.metadata ?? void 0,
4811
4823
  deletedAt: row.deletedAt ? new Date(row.deletedAt) : void 0,
4812
4824
  deletedBy: row.deletedBy ?? void 0
@@ -4829,12 +4841,12 @@ function outboxFromDocument(row) {
4829
4841
  completedAt: row.completedAt ? new Date(row.completedAt) : void 0
4830
4842
  };
4831
4843
  }
4844
+ const KNOWLEDGE_CURATION_CURSOR_REMOVED_MESSAGE = "Knowledge curation cursors were removed: observation-time curate is the only Knowledge writer and needs no cursor.";
4832
4845
  var KnowledgeMongoDB = class KnowledgeMongoDB extends _mastra_core_storage.KnowledgeStorage {
4833
4846
  static MANAGED_COLLECTIONS = [
4834
4847
  _mastra_core_storage.TABLE_KNOWLEDGE_NODES,
4835
4848
  _mastra_core_storage.TABLE_KNOWLEDGE_RECORDS,
4836
4849
  _mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS,
4837
- _mastra_core_storage.TABLE_KNOWLEDGE_CURSORS,
4838
4850
  _mastra_core_storage.TABLE_KNOWLEDGE_ACTIVITY,
4839
4851
  _mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX
4840
4852
  ];
@@ -4847,7 +4859,6 @@ var KnowledgeMongoDB = class KnowledgeMongoDB extends _mastra_core_storage.Knowl
4847
4859
  const nodes = await this.#collection(_mastra_core_storage.TABLE_KNOWLEDGE_NODES);
4848
4860
  const knowledge = await this.#collection(_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS);
4849
4861
  const mentions = await this.#collection(_mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS);
4850
- const cursors = await this.#collection(_mastra_core_storage.TABLE_KNOWLEDGE_CURSORS);
4851
4862
  const activity = await this.#collection(_mastra_core_storage.TABLE_KNOWLEDGE_ACTIVITY);
4852
4863
  const outbox = await this.#collection(_mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX);
4853
4864
  await Promise.all([
@@ -4860,6 +4871,10 @@ var KnowledgeMongoDB = class KnowledgeMongoDB extends _mastra_core_storage.Knowl
4860
4871
  scopeKey: 1,
4861
4872
  type: 1
4862
4873
  }),
4874
+ nodes.createIndex({
4875
+ type: 1,
4876
+ canonicalName: 1
4877
+ }),
4863
4878
  knowledge.createIndex({
4864
4879
  node: 1,
4865
4880
  id: -1
@@ -4868,6 +4883,10 @@ var KnowledgeMongoDB = class KnowledgeMongoDB extends _mastra_core_storage.Knowl
4868
4883
  sourceThreadId: 1,
4869
4884
  id: -1
4870
4885
  }),
4886
+ knowledge.createIndex({
4887
+ scopeKey: 1,
4888
+ id: -1
4889
+ }),
4871
4890
  mentions.createIndex({
4872
4891
  sourceType: 1,
4873
4892
  sourceId: 1,
@@ -4878,11 +4897,11 @@ var KnowledgeMongoDB = class KnowledgeMongoDB extends _mastra_core_storage.Knowl
4878
4897
  sourceType: 1,
4879
4898
  sourceId: 1
4880
4899
  }),
4881
- cursors.createIndex({
4882
- sourceThreadId: 1,
4883
- agent: 1
4884
- }, { unique: true }),
4885
4900
  activity.createIndex({ id: -1 }),
4901
+ activity.createIndex({
4902
+ scopeKey: 1,
4903
+ id: -1
4904
+ }),
4886
4905
  outbox.createIndex({ idempotencyKey: 1 }, { unique: true }),
4887
4906
  outbox.createIndex({
4888
4907
  status: 1,
@@ -4898,6 +4917,11 @@ var KnowledgeMongoDB = class KnowledgeMongoDB extends _mastra_core_storage.Knowl
4898
4917
  }
4899
4918
  async createNode(input) {
4900
4919
  await assertKnowledgeDescriptionWithinBoundCompat(input.description);
4920
+ if (input.scopeAddresses?.length) {
4921
+ const error = /* @__PURE__ */ new Error("This Knowledge storage adapter does not expose structural scope placement.");
4922
+ error.name = "KnowledgeUnsupportedCapabilityError";
4923
+ throw error;
4924
+ }
4901
4925
  const scope = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope);
4902
4926
  return this.#connector.withTransaction(async (session) => {
4903
4927
  const existing = await this.#getNodeByName(input.name, scope, session);
@@ -5090,9 +5114,9 @@ var KnowledgeMongoDB = class KnowledgeMongoDB extends _mastra_core_storage.Knowl
5090
5114
  });
5091
5115
  }
5092
5116
  async appendKnowledge(input) {
5117
+ await assertKnowledgeRecordTextWithinBoundCompat(input.text);
5093
5118
  const scope = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope);
5094
5119
  const defaultScope = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.defaultScope);
5095
- (0, _mastra_core_storage.assertKnowledgeScopeWithinCeiling)(scope, input.maxScope);
5096
5120
  return this.#connector.withTransaction(async (session) => {
5097
5121
  const parent = await this.#resolveTerminalNode(nodeReferenceId(input.node), session);
5098
5122
  if (!parent) throw new _mastra_core_storage.KnowledgeNotFoundError("node", nodeReferenceId(input.node));
@@ -5107,14 +5131,12 @@ var KnowledgeMongoDB = class KnowledgeMongoDB extends _mastra_core_storage.Knowl
5107
5131
  sourceThreadId: input.sourceThreadId,
5108
5132
  capturedAt: /* @__PURE__ */ new Date(),
5109
5133
  when: input.when,
5110
- maxScope: input.maxScope,
5111
5134
  metadata: input.metadata
5112
5135
  };
5113
5136
  await (await this.#knowledge()).insertOne({
5114
5137
  ...record,
5115
5138
  scopeKey: (0, _mastra_core_storage.knowledgeScopeKey)(scope),
5116
5139
  when: record.when ?? null,
5117
- maxScope: record.maxScope ?? null,
5118
5140
  deletedAt: null,
5119
5141
  deletedBy: null
5120
5142
  }, sessionOptions(session));
@@ -5199,7 +5221,6 @@ var KnowledgeMongoDB = class KnowledgeMongoDB extends _mastra_core_storage.Knowl
5199
5221
  return this.#connector.withTransaction(async (session) => {
5200
5222
  const record = await this.#getKnowledge(input.id, true, session);
5201
5223
  if (!record) throw new _mastra_core_storage.KnowledgeNotFoundError("record", input.id);
5202
- (0, _mastra_core_storage.assertKnowledgeScopeWithinCeiling)(scope, record.maxScope);
5203
5224
  await (await this.#knowledge()).updateOne({ id: input.id }, { $set: {
5204
5225
  scope,
5205
5226
  scopeKey: (0, _mastra_core_storage.knowledgeScopeKey)(scope)
@@ -5213,17 +5234,6 @@ var KnowledgeMongoDB = class KnowledgeMongoDB extends _mastra_core_storage.Knowl
5213
5234
  };
5214
5235
  });
5215
5236
  }
5216
- async raiseKnowledgeCeiling(input) {
5217
- const record = await this.#getKnowledge(input.id, true);
5218
- if (!record) throw new _mastra_core_storage.KnowledgeNotFoundError("record", input.id);
5219
- (0, _mastra_core_storage.assertKnowledgeScopeWithinCeiling)(record.scope, input.maxScope);
5220
- (0, _mastra_core_storage.assertKnowledgeCeilingRaised)(record.maxScope, input.maxScope);
5221
- await (await this.#knowledge()).updateOne({ id: input.id }, { $set: { maxScope: input.maxScope ?? null } });
5222
- return {
5223
- ...record,
5224
- maxScope: input.maxScope
5225
- };
5226
- }
5227
5237
  async search(input) {
5228
5238
  const scope = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope);
5229
5239
  const query = input.query.trim();
@@ -5263,7 +5273,7 @@ var KnowledgeMongoDB = class KnowledgeMongoDB extends _mastra_core_storage.Knowl
5263
5273
  results.push({
5264
5274
  type: "record",
5265
5275
  id: record.id,
5266
- recordId: record.node,
5276
+ recordId: parentVisible ? record.node : record.id,
5267
5277
  name: parentVisible ? parent.name : "(private node)",
5268
5278
  text: record.text,
5269
5279
  scope: cloneScope(record.scope)
@@ -5272,36 +5282,19 @@ var KnowledgeMongoDB = class KnowledgeMongoDB extends _mastra_core_storage.Knowl
5272
5282
  }
5273
5283
  return results.slice(0, limit);
5274
5284
  }
5275
- async getCurationCursor(input) {
5276
- const row = await (await this.#cursors()).findOne(input);
5277
- return row ? {
5278
- sourceThreadId: row.sourceThreadId,
5279
- agent: row.agent,
5280
- lastKnowledgeId: row.lastKnowledgeId,
5281
- updatedAt: new Date(row.updatedAt)
5282
- } : null;
5283
- }
5284
- async advanceCurationCursor(input) {
5285
- const row = await (await this.#cursors()).findOneAndUpdate({
5286
- sourceThreadId: input.sourceThreadId,
5287
- agent: input.agent
5288
- }, {
5289
- $max: { lastKnowledgeId: input.lastKnowledgeId },
5290
- $set: { updatedAt: /* @__PURE__ */ new Date() },
5291
- $setOnInsert: {
5292
- sourceThreadId: input.sourceThreadId,
5293
- agent: input.agent
5294
- }
5295
- }, {
5296
- upsert: true,
5297
- returnDocument: "after"
5298
- });
5299
- return {
5300
- sourceThreadId: row.sourceThreadId,
5301
- agent: row.agent,
5302
- lastKnowledgeId: row.lastKnowledgeId,
5303
- updatedAt: new Date(row.updatedAt)
5304
- };
5285
+ /**
5286
+ * @deprecated Curation cursors were removed. Observation-time curate is the only Knowledge writer and needs no
5287
+ * cursor. Always throws.
5288
+ */
5289
+ async getCurationCursor(_input) {
5290
+ throw new Error(KNOWLEDGE_CURATION_CURSOR_REMOVED_MESSAGE);
5291
+ }
5292
+ /**
5293
+ * @deprecated Curation cursors were removed. Observation-time curate is the only Knowledge writer and needs no
5294
+ * cursor. Always throws.
5295
+ */
5296
+ async advanceCurationCursor(_input) {
5297
+ throw new Error(KNOWLEDGE_CURATION_CURSOR_REMOVED_MESSAGE);
5305
5298
  }
5306
5299
  async listActivity(input) {
5307
5300
  const scope = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope);
@@ -5422,9 +5415,6 @@ var KnowledgeMongoDB = class KnowledgeMongoDB extends _mastra_core_storage.Knowl
5422
5415
  #mentions() {
5423
5416
  return this.#collection(_mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS);
5424
5417
  }
5425
- #cursors() {
5426
- return this.#collection(_mastra_core_storage.TABLE_KNOWLEDGE_CURSORS);
5427
- }
5428
5418
  #activityCollection() {
5429
5419
  return this.#collection(_mastra_core_storage.TABLE_KNOWLEDGE_ACTIVITY);
5430
5420
  }
@@ -5447,12 +5437,14 @@ var KnowledgeMongoDB = class KnowledgeMongoDB extends _mastra_core_storage.Knowl
5447
5437
  return row ? nodeFromDocument(row) : null;
5448
5438
  }
5449
5439
  async #resolveNode(name, scope, session) {
5450
- for (let length = scope.length; length > 0; length--) {
5451
- const node = await this.#getNodeByName(name, scope.slice(0, length), session);
5452
- if (node) {
5453
- const terminal = await this.#resolveTerminalNode(node.id, session);
5454
- if (terminal && (0, _mastra_core_storage.isKnowledgeScopeVisible)(terminal.scope, scope)) return terminal;
5455
- }
5440
+ const candidates = (await (await this.#nodes()).find({
5441
+ type: "node",
5442
+ canonicalName: canonicalName(name),
5443
+ $or: [{ scopeKey: { $in: visibleScopeKeys(scope) } }, { mergedInto: { $ne: null } }]
5444
+ }, sessionOptions(session)).toArray()).map(nodeFromDocument).sort((left, right) => right.scope.length - left.scope.length);
5445
+ for (const candidate of candidates) {
5446
+ const terminal = await this.#resolveTerminalNode(candidate.id, session);
5447
+ if (terminal && (0, _mastra_core_storage.isKnowledgeScopeVisible)(terminal.scope, scope)) return terminal;
5456
5448
  }
5457
5449
  return null;
5458
5450
  }