@mastra/mongodb 1.26.0-alpha.0 → 1.26.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.
@@ -326,10 +326,20 @@ export interface KnowledgePagesCore {
326
326
  /** Loads core's page-store runtime once, after checking core advertises `knowledge-pages`. */
327
327
  export declare function createKnowledgePagesCoreLoader(features: ReadonlySet<string>, loadStorageModule: () => Promise<unknown>): () => Promise<KnowledgePagesCore>;
328
328
  /**
329
- * Keyword-search words: the query split on whitespace, lowercased, deduplicated. Every word must
330
- * appear (as a case-insensitive substring) in a hit; an empty list means the search returns nothing.
329
+ * Keyword-search words: the query split on whitespace, lowercased, deduplicated. A hit needs at
330
+ * least one of them (as a case-insensitive substring); an empty list means the search returns nothing.
331
331
  */
332
332
  export declare function knowledgeSearchWords(query: string): string[];
333
+ /**
334
+ * Points one matched search word earns on a page, by the best field it appears in: name, then
335
+ * description, then body. Pages rank by how many query words they match first; these points only
336
+ * break ties between pages that match the same number of words.
337
+ */
338
+ export declare const KNOWLEDGE_SEARCH_WEIGHTS: {
339
+ readonly name: 3;
340
+ readonly description: 2;
341
+ readonly body: 1;
342
+ };
333
343
  /**
334
344
  * Why `key` can't be a Knowledge page key, or undefined when it can. Keys are one path segment:
335
345
  * non-empty, with no `/` (path separator), `#` (scope/path separator) or `%` (escape character).
@@ -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.26.0-alpha.0"
6
+ version: "1.26.0-alpha.1"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.26.0-alpha.0",
2
+ "version": "1.26.0-alpha.1",
3
3
  "package": "@mastra/mongodb",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -11,7 +11,7 @@ let _mastra_core_agent = require("@mastra/core/agent");
11
11
  let _mastra_core_evals = require("@mastra/core/evals");
12
12
  let _mastra_core_storage_domains_skills = require("@mastra/core/storage/domains/skills");
13
13
  // package.json
14
- var version = "1.26.0-alpha.0";
14
+ var version = "1.26.0-alpha.1";
15
15
  // src/vector/filter.ts
16
16
  /**
17
17
  * Translator for MongoDB filter queries.
@@ -1730,6 +1730,7 @@ const SNAPSHOT_FIELDS$6 = [
1730
1730
  "description",
1731
1731
  "instructions",
1732
1732
  "model",
1733
+ "modelList",
1733
1734
  "tools",
1734
1735
  "defaultOptions",
1735
1736
  "workflows",
@@ -4895,13 +4896,23 @@ function createKnowledgePagesCoreLoader(features, loadStorageModule) {
4895
4896
  };
4896
4897
  }
4897
4898
  /**
4898
- * Keyword-search words: the query split on whitespace, lowercased, deduplicated. Every word must
4899
- * appear (as a case-insensitive substring) in a hit; an empty list means the search returns nothing.
4899
+ * Keyword-search words: the query split on whitespace, lowercased, deduplicated. A hit needs at
4900
+ * least one of them (as a case-insensitive substring); an empty list means the search returns nothing.
4900
4901
  */
4901
4902
  function knowledgeSearchWords(query) {
4902
4903
  return [...new Set(query.toLowerCase().split(/\s+/).filter(Boolean))];
4903
4904
  }
4904
4905
  /**
4906
+ * Points one matched search word earns on a page, by the best field it appears in: name, then
4907
+ * description, then body. Pages rank by how many query words they match first; these points only
4908
+ * break ties between pages that match the same number of words.
4909
+ */
4910
+ const KNOWLEDGE_SEARCH_WEIGHTS = {
4911
+ name: 3,
4912
+ description: 2,
4913
+ body: 1
4914
+ };
4915
+ /**
4905
4916
  * Why `key` can't be a Knowledge page key, or undefined when it can. Keys are one path segment:
4906
4917
  * non-empty, with no `/` (path separator), `#` (scope/path separator) or `%` (escape character).
4907
4918
  */
@@ -6017,6 +6028,23 @@ var MongoKnowledgePages = class {
6017
6028
  }).limit(limit + 1).toArray();
6018
6029
  return this.#noteOutput(rows, limit);
6019
6030
  }
6031
+ async countChildPages(input) {
6032
+ const ids = [...new Set(input.parentIds)];
6033
+ const counts = Object.fromEntries(ids.map((id) => [id, 0]));
6034
+ if (!ids.length) return counts;
6035
+ const readable = await this.#readable(input.read);
6036
+ const rows = await (await this.#c("pages")).aggregate([{ $match: {
6037
+ parentId: { $in: ids },
6038
+ ...notScope,
6039
+ ...live,
6040
+ ...this.#inScopes("scopeId", readable)
6041
+ } }, { $group: {
6042
+ _id: "$parentId",
6043
+ count: { $sum: 1 }
6044
+ } }]).toArray();
6045
+ for (const row of rows) counts[row._id] = row.count;
6046
+ return counts;
6047
+ }
6020
6048
  async countNotes(input) {
6021
6049
  const ids = [...new Set(input.pageIds)];
6022
6050
  const counts = Object.fromEntries(ids.map((id) => [id, 0]));
@@ -6272,38 +6300,67 @@ var MongoKnowledgePages = class {
6272
6300
  ...live,
6273
6301
  ...notScope,
6274
6302
  ...inScope("scopeId"),
6275
- $and: words.map((word) => ({ $or: [
6303
+ $or: words.flatMap((word) => [
6276
6304
  { name: matcher(word) },
6277
6305
  { description: matcher(word) },
6278
6306
  { body: matcher(word) }
6279
- ] }))
6307
+ ])
6280
6308
  };
6309
+ const has = (field, word) => ({ $regexMatch: {
6310
+ input: { $ifNull: [`$${field}`, ""] },
6311
+ regex: escapeRegex(word),
6312
+ options: "i"
6313
+ } });
6281
6314
  const hits = (await pages.aggregate([
6282
6315
  { $match: pageFilter },
6283
- { $addFields: { searchTier: { $cond: [
6284
- { $regexMatch: {
6285
- input: "$canonicalName",
6286
- regex: escapeRegex(phrase)
6287
- } },
6288
- 0,
6289
- { $cond: [
6290
- { $and: words.map((word) => ({ $regexMatch: {
6291
- input: "$name",
6292
- regex: escapeRegex(word),
6293
- options: "i"
6294
- } })) },
6316
+ { $addFields: {
6317
+ searchTier: { $cond: [
6318
+ { $regexMatch: {
6319
+ input: "$canonicalName",
6320
+ regex: escapeRegex(phrase)
6321
+ } },
6322
+ 0,
6323
+ 1
6324
+ ] },
6325
+ searchCount: { $add: words.map((word) => ({ $cond: [
6326
+ { $or: [
6327
+ has("name", word),
6328
+ has("description", word),
6329
+ has("body", word)
6330
+ ] },
6295
6331
  1,
6296
- 2
6297
- ] }
6298
- ] } } },
6332
+ 0
6333
+ ] })) },
6334
+ searchScore: { $add: words.map((word) => ({ $switch: {
6335
+ branches: [
6336
+ {
6337
+ case: has("name", word),
6338
+ then: KNOWLEDGE_SEARCH_WEIGHTS.name
6339
+ },
6340
+ {
6341
+ case: has("description", word),
6342
+ then: KNOWLEDGE_SEARCH_WEIGHTS.description
6343
+ },
6344
+ {
6345
+ case: has("body", word),
6346
+ then: KNOWLEDGE_SEARCH_WEIGHTS.body
6347
+ }
6348
+ ],
6349
+ default: 0
6350
+ } })) }
6351
+ } },
6299
6352
  { $sort: {
6300
6353
  searchTier: 1,
6354
+ searchCount: -1,
6355
+ searchScore: -1,
6301
6356
  updatedAt: -1,
6302
6357
  id: 1
6303
6358
  } },
6304
6359
  { $limit: limit },
6305
6360
  { $project: {
6306
6361
  searchTier: 0,
6362
+ searchCount: 0,
6363
+ searchScore: 0,
6307
6364
  _id: 0
6308
6365
  } }
6309
6366
  ]).toArray()).map((row) => ({
@@ -6311,15 +6368,29 @@ var MongoKnowledgePages = class {
6311
6368
  page: toPage(row)
6312
6369
  }));
6313
6370
  if (hits.length >= limit) return hits;
6314
- const noteRows = await (await this.#c("notes")).find({
6315
- ...live,
6316
- ...this.#visibleNotes(ids),
6317
- ...scopes ? { pageScopeId: { $in: ids === null ? scopes : scopes.filter((id) => ids.includes(id)) } } : {},
6318
- $and: words.map((word) => ({ text: matcher(word) }))
6319
- }, noProjection).sort({
6320
- updatedAt: -1,
6321
- id: -1
6322
- }).limit(limit - hits.length).toArray();
6371
+ const noteRows = await (await this.#c("notes")).aggregate([
6372
+ { $match: {
6373
+ ...live,
6374
+ ...this.#visibleNotes(ids),
6375
+ ...scopes ? { pageScopeId: { $in: ids === null ? scopes : scopes.filter((id) => ids.includes(id)) } } : {},
6376
+ $or: words.map((word) => ({ text: matcher(word) }))
6377
+ } },
6378
+ { $addFields: { searchScore: { $add: words.map((word) => ({ $cond: [
6379
+ has("text", word),
6380
+ 1,
6381
+ 0
6382
+ ] })) } } },
6383
+ { $sort: {
6384
+ searchScore: -1,
6385
+ updatedAt: -1,
6386
+ id: -1
6387
+ } },
6388
+ { $limit: limit - hits.length },
6389
+ { $project: {
6390
+ searchScore: 0,
6391
+ _id: 0
6392
+ } }
6393
+ ]).toArray();
6323
6394
  const notePages = new Map((await pages.find({
6324
6395
  id: { $in: [...new Set(noteRows.map((row) => String(row.pageId)))] },
6325
6396
  ...live
@@ -6685,6 +6756,9 @@ var KnowledgeMongoDB = class extends _mastra_core_storage.KnowledgeStorage {
6685
6756
  listNotes(input) {
6686
6757
  return this.#pages.listNotes(input);
6687
6758
  }
6759
+ countChildPages(input) {
6760
+ return this.#pages.countChildPages(input);
6761
+ }
6688
6762
  countNotes(input) {
6689
6763
  return this.#pages.countNotes(input);
6690
6764
  }