@mastra/mongodb 1.26.0-alpha.0 → 1.26.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/_types/{@internal_core → @internal_core@0.1.4}/dist/knowledge-compat/index.d.ts +17 -2
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/index.cjs +338 -54
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +338 -54
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/agents/index.d.ts.map +1 -1
- package/dist/storage/domains/knowledge/index.d.ts +9 -2
- package/dist/storage/domains/knowledge/index.d.ts.map +1 -1
- package/dist/storage/domains/knowledge/pages.d.ts +35 -12
- package/dist/storage/domains/knowledge/pages.d.ts.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +1 -1
- package/package.json +7 -7
- /package/dist/_types/{@internal_core → @internal_core@0.1.4}/dist/memory/index.d.ts +0 -0
- /package/dist/_types/{@internal_core → @internal_core@0.1.4}/package.json +0 -0
package/dist/_types/{@internal_core → @internal_core@0.1.4}/dist/knowledge-compat/index.d.ts
RENAMED
|
@@ -318,6 +318,7 @@ export interface KnowledgePagesCore {
|
|
|
318
318
|
MAX_KNOWLEDGE_MOVE_PAGES: number;
|
|
319
319
|
MAX_KNOWLEDGE_PAGE_DEPTH: number;
|
|
320
320
|
MAX_KNOWLEDGE_PAGE_LIST_LIMIT: number;
|
|
321
|
+
MAX_KNOWLEDGE_PAGE_LINKS: number;
|
|
321
322
|
KnowledgeMoveTooLargeError: new (pageId: string, pages: number) => Error;
|
|
322
323
|
KnowledgePageDepthError: new (pageId: string) => Error;
|
|
323
324
|
KnowledgePageHasChildrenError: new (pageId: string) => Error;
|
|
@@ -325,11 +326,25 @@ export interface KnowledgePagesCore {
|
|
|
325
326
|
}
|
|
326
327
|
/** Loads core's page-store runtime once, after checking core advertises `knowledge-pages`. */
|
|
327
328
|
export declare function createKnowledgePagesCoreLoader(features: ReadonlySet<string>, loadStorageModule: () => Promise<unknown>): () => Promise<KnowledgePagesCore>;
|
|
329
|
+
/** Most words one keyword search uses; every word adds a substring check per page in each store. */
|
|
330
|
+
export declare const KNOWLEDGE_SEARCH_MAX_WORDS = 16;
|
|
328
331
|
/**
|
|
329
|
-
* Keyword-search words: the query split on whitespace, lowercased, deduplicated
|
|
330
|
-
*
|
|
332
|
+
* Keyword-search words: the query split on whitespace, lowercased, deduplicated, keeping the first
|
|
333
|
+
* {@link KNOWLEDGE_SEARCH_MAX_WORDS}. A hit needs at least one of them (as a case-insensitive
|
|
334
|
+
* substring); an empty list means the search returns nothing.
|
|
331
335
|
*/
|
|
332
336
|
export declare function knowledgeSearchWords(query: string): string[];
|
|
337
|
+
/**
|
|
338
|
+
* Points one matched search word earns on a page, by the best field it appears in: name, then
|
|
339
|
+
* description, then body. A page whose name contains the whole query ranks first; below that, pages
|
|
340
|
+
* rank by how many query words they match, and these points only break ties between pages that
|
|
341
|
+
* match the same number of words.
|
|
342
|
+
*/
|
|
343
|
+
export declare const KNOWLEDGE_SEARCH_WEIGHTS: {
|
|
344
|
+
readonly name: 3;
|
|
345
|
+
readonly description: 2;
|
|
346
|
+
readonly body: 1;
|
|
347
|
+
};
|
|
333
348
|
/**
|
|
334
349
|
* Why `key` can't be a Knowledge page key, or undefined when it can. Keys are one path segment:
|
|
335
350
|
* non-empty, with no `/` (path separator), `#` (scope/path separator) or `%` (escape character).
|
package/dist/docs/SKILL.md
CHANGED
|
@@ -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.
|
|
6
|
+
version: "1.26.0-alpha.2"
|
|
7
7
|
---
|
|
8
8
|
|
|
9
9
|
## When to use
|
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.
|
|
14
|
+
var version = "1.26.0-alpha.2";
|
|
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",
|
|
@@ -4874,7 +4875,8 @@ const KNOWLEDGE_PAGES_CORE_FUNCTIONS = [
|
|
|
4874
4875
|
const KNOWLEDGE_PAGES_CORE_NUMBERS = [
|
|
4875
4876
|
"MAX_KNOWLEDGE_MOVE_PAGES",
|
|
4876
4877
|
"MAX_KNOWLEDGE_PAGE_DEPTH",
|
|
4877
|
-
"MAX_KNOWLEDGE_PAGE_LIST_LIMIT"
|
|
4878
|
+
"MAX_KNOWLEDGE_PAGE_LIST_LIMIT",
|
|
4879
|
+
"MAX_KNOWLEDGE_PAGE_LINKS"
|
|
4878
4880
|
];
|
|
4879
4881
|
function resolveKnowledgePagesCore(storageModule) {
|
|
4880
4882
|
const module = typeof storageModule === "object" && storageModule !== null ? storageModule : {};
|
|
@@ -4895,13 +4897,25 @@ function createKnowledgePagesCoreLoader(features, loadStorageModule) {
|
|
|
4895
4897
|
};
|
|
4896
4898
|
}
|
|
4897
4899
|
/**
|
|
4898
|
-
* Keyword-search words: the query split on whitespace, lowercased, deduplicated
|
|
4899
|
-
*
|
|
4900
|
+
* Keyword-search words: the query split on whitespace, lowercased, deduplicated, keeping the first
|
|
4901
|
+
* {@link KNOWLEDGE_SEARCH_MAX_WORDS}. A hit needs at least one of them (as a case-insensitive
|
|
4902
|
+
* substring); an empty list means the search returns nothing.
|
|
4900
4903
|
*/
|
|
4901
4904
|
function knowledgeSearchWords(query) {
|
|
4902
|
-
return [...new Set(query.toLowerCase().split(/\s+/).filter(Boolean))];
|
|
4905
|
+
return [...new Set(query.toLowerCase().split(/\s+/).filter(Boolean))].slice(0, 16);
|
|
4903
4906
|
}
|
|
4904
4907
|
/**
|
|
4908
|
+
* Points one matched search word earns on a page, by the best field it appears in: name, then
|
|
4909
|
+
* description, then body. A page whose name contains the whole query ranks first; below that, pages
|
|
4910
|
+
* rank by how many query words they match, and these points only break ties between pages that
|
|
4911
|
+
* match the same number of words.
|
|
4912
|
+
*/
|
|
4913
|
+
const KNOWLEDGE_SEARCH_WEIGHTS = {
|
|
4914
|
+
name: 3,
|
|
4915
|
+
description: 2,
|
|
4916
|
+
body: 1
|
|
4917
|
+
};
|
|
4918
|
+
/**
|
|
4905
4919
|
* Why `key` can't be a Knowledge page key, or undefined when it can. Keys are one path segment:
|
|
4906
4920
|
* non-empty, with no `/` (path separator), `#` (scope/path separator) or `%` (escape character).
|
|
4907
4921
|
*/
|
|
@@ -4913,11 +4927,13 @@ function knowledgePageKeyProblem(key) {
|
|
|
4913
4927
|
const loadPagesCore = createKnowledgePagesCoreLoader(_mastra_core_features.coreFeatures, () => import("@mastra/core/storage"));
|
|
4914
4928
|
const DEFAULT_LIST_LIMIT = 50;
|
|
4915
4929
|
const MONGO_DUPLICATE_KEY = 11e3;
|
|
4930
|
+
const DEFAULT_LINK_LIMIT = 1e3;
|
|
4916
4931
|
const MONGO_KNOWLEDGE_PAGES_COLLECTIONS = {
|
|
4917
4932
|
pages: "mastra_knowledge_pages",
|
|
4918
4933
|
notes: "mastra_knowledge_notes",
|
|
4919
4934
|
pins: "mastra_knowledge_pins",
|
|
4920
4935
|
activity: "mastra_knowledge_activity",
|
|
4936
|
+
links: "mastra_knowledge_page_links",
|
|
4921
4937
|
state: "mastra_knowledge_access_state"
|
|
4922
4938
|
};
|
|
4923
4939
|
/** Thrown when existing Knowledge collections can't be used as schema 4 without a reset. */
|
|
@@ -5013,10 +5029,17 @@ function toListedPin(row) {
|
|
|
5013
5029
|
pageName: String(row.pageName)
|
|
5014
5030
|
};
|
|
5015
5031
|
}
|
|
5032
|
+
/** IndexNotFound, or NamespaceNotFound when the collection doesn't exist yet. */
|
|
5033
|
+
function isMissingIndex(error) {
|
|
5034
|
+
const code = error?.code;
|
|
5035
|
+
return code === 27 || code === 26;
|
|
5036
|
+
}
|
|
5016
5037
|
function toPin(row) {
|
|
5017
5038
|
return {
|
|
5018
5039
|
id: String(row.id),
|
|
5019
5040
|
pageId: String(row.pageId),
|
|
5041
|
+
kind: row.kind ?? "page",
|
|
5042
|
+
...row.depth == null ? {} : { depth: Number(row.depth) },
|
|
5020
5043
|
audience: [...row.audience ?? []],
|
|
5021
5044
|
createdBy: String(row.createdBy),
|
|
5022
5045
|
createdAt: new Date(row.createdAt),
|
|
@@ -5079,12 +5102,17 @@ var MongoKnowledgePages = class {
|
|
|
5079
5102
|
} }, { upsert: true });
|
|
5080
5103
|
const inspection = await this.inspectSchema();
|
|
5081
5104
|
if (inspection.status === "incompatible-reset-required") throw new MongoKnowledgeResetRequiredError(inspection);
|
|
5082
|
-
const [pages, notes, pins, activity] = await Promise.all([
|
|
5105
|
+
const [pages, notes, pins, activity, links] = await Promise.all([
|
|
5083
5106
|
this.#c("pages"),
|
|
5084
5107
|
this.#c("notes"),
|
|
5085
5108
|
this.#c("pins"),
|
|
5086
|
-
this.#c("activity")
|
|
5109
|
+
this.#c("activity"),
|
|
5110
|
+
this.#c("links")
|
|
5087
5111
|
]);
|
|
5112
|
+
await pins.updateMany({ kind: { $exists: false } }, { $set: { kind: "page" } });
|
|
5113
|
+
await pins.dropIndex("pageId_1").catch((error) => {
|
|
5114
|
+
if (!isMissingIndex(error)) throw error;
|
|
5115
|
+
});
|
|
5088
5116
|
await Promise.all([
|
|
5089
5117
|
pages.createIndex({ id: 1 }, { unique: true }),
|
|
5090
5118
|
pages.createIndex({ address: 1 }, {
|
|
@@ -5124,7 +5152,10 @@ var MongoKnowledgePages = class {
|
|
|
5124
5152
|
notes.createIndex({ pageScopeId: 1 }),
|
|
5125
5153
|
notes.createIndex({ stampScopeIds: 1 }),
|
|
5126
5154
|
pins.createIndex({ id: 1 }, { unique: true }),
|
|
5127
|
-
pins.createIndex({
|
|
5155
|
+
pins.createIndex({
|
|
5156
|
+
pageId: 1,
|
|
5157
|
+
kind: 1
|
|
5158
|
+
}, { unique: true }),
|
|
5128
5159
|
pins.createIndex({ audience: 1 }),
|
|
5129
5160
|
activity.createIndex({ id: 1 }, { unique: true }),
|
|
5130
5161
|
activity.createIndex({
|
|
@@ -5136,12 +5167,19 @@ var MongoKnowledgePages = class {
|
|
|
5136
5167
|
pageId: 1,
|
|
5137
5168
|
createdAt: -1,
|
|
5138
5169
|
id: -1
|
|
5139
|
-
})
|
|
5170
|
+
}),
|
|
5171
|
+
links.createIndex({
|
|
5172
|
+
fromPageId: 1,
|
|
5173
|
+
ref: 1
|
|
5174
|
+
}, { unique: true }),
|
|
5175
|
+
links.createIndex({ toPageId: 1 }),
|
|
5176
|
+
links.createIndex({ ref: 1 })
|
|
5140
5177
|
]);
|
|
5141
5178
|
}
|
|
5142
5179
|
/** Drops exactly the Knowledge collections (never Memory's) and recreates schema 4. */
|
|
5143
5180
|
async reset() {
|
|
5144
5181
|
for (const name of KNOWLEDGE_RESETTABLE_TABLE_NAMES) await this.#drop(name);
|
|
5182
|
+
await this.#drop(MONGO_KNOWLEDGE_PAGES_COLLECTIONS.links);
|
|
5145
5183
|
await this.init();
|
|
5146
5184
|
}
|
|
5147
5185
|
/** The ids of every scope page the read scope can see; `null` means everything. */
|
|
@@ -5534,10 +5572,12 @@ var MongoKnowledgePages = class {
|
|
|
5534
5572
|
async listChildScopes(input) {
|
|
5535
5573
|
const core = await loadPagesCore();
|
|
5536
5574
|
const limit = clampLimit(input.limit, core.MAX_KNOWLEDGE_PAGE_LIST_LIMIT);
|
|
5575
|
+
const readable = input.read ? await this.#readable(input.read) : null;
|
|
5537
5576
|
const filter = {
|
|
5538
5577
|
isScope: true,
|
|
5539
5578
|
parentId: input.parentId,
|
|
5540
|
-
...live
|
|
5579
|
+
...live,
|
|
5580
|
+
...this.#inScopes("id", readable)
|
|
5541
5581
|
};
|
|
5542
5582
|
if (input.cursor) {
|
|
5543
5583
|
const [position, id] = decodeCursor(input.cursor);
|
|
@@ -5919,12 +5959,20 @@ var MongoKnowledgePages = class {
|
|
|
5919
5959
|
} }] } } }], { session });
|
|
5920
5960
|
await this.#refreshMentionScopes([target.id, ...tree.ids], session);
|
|
5921
5961
|
const pins = await this.#c("pins");
|
|
5922
|
-
const
|
|
5962
|
+
const sourcePins = await pins.find({ pageId: source.id }, {
|
|
5923
5963
|
...noProjection,
|
|
5924
5964
|
session
|
|
5925
|
-
});
|
|
5926
|
-
|
|
5927
|
-
await this.#upsertPinAudience(session,
|
|
5965
|
+
}).sort({ kind: 1 }).toArray();
|
|
5966
|
+
for (const sourcePin of sourcePins) {
|
|
5967
|
+
await this.#upsertPinAudience(session, {
|
|
5968
|
+
pageId: target.id,
|
|
5969
|
+
kind: sourcePin.kind ?? "page",
|
|
5970
|
+
depth: sourcePin.depth == null ? null : Number(sourcePin.depth),
|
|
5971
|
+
audience: sourcePin.audience ?? [],
|
|
5972
|
+
createdBy: String(sourcePin.createdBy),
|
|
5973
|
+
now,
|
|
5974
|
+
keepDepth: true
|
|
5975
|
+
});
|
|
5928
5976
|
await pins.deleteOne({ id: sourcePin.id }, { session });
|
|
5929
5977
|
}
|
|
5930
5978
|
await (await this.#c("activity")).updateMany({ pageId: source.id }, { $set: { pageScopeId: target.scopeId } }, { session });
|
|
@@ -6017,6 +6065,151 @@ var MongoKnowledgePages = class {
|
|
|
6017
6065
|
}).limit(limit + 1).toArray();
|
|
6018
6066
|
return this.#noteOutput(rows, limit);
|
|
6019
6067
|
}
|
|
6068
|
+
async countChildScopes(input) {
|
|
6069
|
+
const ids = [...new Set(input.parentIds)];
|
|
6070
|
+
const counts = Object.fromEntries(ids.map((id) => [id, 0]));
|
|
6071
|
+
if (!ids.length) return counts;
|
|
6072
|
+
const readable = await this.#readable(input.read);
|
|
6073
|
+
const rows = await (await this.#c("pages")).aggregate([{ $match: {
|
|
6074
|
+
parentId: { $in: ids },
|
|
6075
|
+
isScope: true,
|
|
6076
|
+
...live,
|
|
6077
|
+
...this.#inScopes("id", readable)
|
|
6078
|
+
} }, { $group: {
|
|
6079
|
+
_id: "$parentId",
|
|
6080
|
+
count: { $sum: 1 }
|
|
6081
|
+
} }]).toArray();
|
|
6082
|
+
for (const row of rows) counts[row._id] = row.count;
|
|
6083
|
+
return counts;
|
|
6084
|
+
}
|
|
6085
|
+
async countChildPages(input) {
|
|
6086
|
+
const ids = [...new Set(input.parentIds)];
|
|
6087
|
+
const counts = Object.fromEntries(ids.map((id) => [id, 0]));
|
|
6088
|
+
if (!ids.length) return counts;
|
|
6089
|
+
const readable = await this.#readable(input.read);
|
|
6090
|
+
const rows = await (await this.#c("pages")).aggregate([{ $match: {
|
|
6091
|
+
parentId: { $in: ids },
|
|
6092
|
+
...notScope,
|
|
6093
|
+
...live,
|
|
6094
|
+
...this.#inScopes("scopeId", readable)
|
|
6095
|
+
} }, { $group: {
|
|
6096
|
+
_id: "$parentId",
|
|
6097
|
+
count: { $sum: 1 }
|
|
6098
|
+
} }]).toArray();
|
|
6099
|
+
for (const row of rows) counts[row._id] = row.count;
|
|
6100
|
+
return counts;
|
|
6101
|
+
}
|
|
6102
|
+
async replacePageBodyLinks(input) {
|
|
6103
|
+
await this.#tx(async (session) => {
|
|
6104
|
+
const links = await this.#c("links");
|
|
6105
|
+
await links.deleteMany({ fromPageId: input.pageId }, { session });
|
|
6106
|
+
if (!input.links.length) return;
|
|
6107
|
+
await links.insertMany(input.links.map((link) => ({
|
|
6108
|
+
fromPageId: input.pageId,
|
|
6109
|
+
ref: link.ref,
|
|
6110
|
+
toPageId: link.toPageId,
|
|
6111
|
+
count: link.count
|
|
6112
|
+
})), { session });
|
|
6113
|
+
});
|
|
6114
|
+
}
|
|
6115
|
+
async listPageBodyLinkSources(input) {
|
|
6116
|
+
const any = [];
|
|
6117
|
+
if (input.refs?.length) any.push({
|
|
6118
|
+
toPageId: null,
|
|
6119
|
+
ref: { $in: input.refs }
|
|
6120
|
+
});
|
|
6121
|
+
if (input.toPageIds?.length) any.push({ toPageId: { $in: input.toPageIds } });
|
|
6122
|
+
if (!any.length) return [];
|
|
6123
|
+
return (await (await this.#c("links")).distinct("fromPageId", { $or: any })).map(String);
|
|
6124
|
+
}
|
|
6125
|
+
/** Resolved links touching `pageIds`, both ends live and readable, notes only through visible notes. */
|
|
6126
|
+
async #visibleLinks(pageIds, read, direction, kinds) {
|
|
6127
|
+
const ids = [...new Set(pageIds)];
|
|
6128
|
+
if (!ids.length) return [];
|
|
6129
|
+
const wanted = new Set(kinds ?? ["body", "note"]);
|
|
6130
|
+
const touching = (from, to) => direction !== "in" && ids.includes(from) || direction !== "out" && !!to && ids.includes(to);
|
|
6131
|
+
const readable = await this.#readable(read);
|
|
6132
|
+
const raw = /* @__PURE__ */ new Map();
|
|
6133
|
+
const add = (fromPageId, toPageId, kind, count) => {
|
|
6134
|
+
if (fromPageId === toPageId) return;
|
|
6135
|
+
const key = `${fromPageId}\u0000${toPageId}\u0000${kind}`;
|
|
6136
|
+
const edge = raw.get(key);
|
|
6137
|
+
if (edge) edge.count += count;
|
|
6138
|
+
else raw.set(key, {
|
|
6139
|
+
fromPageId,
|
|
6140
|
+
toPageId,
|
|
6141
|
+
kind,
|
|
6142
|
+
count
|
|
6143
|
+
});
|
|
6144
|
+
};
|
|
6145
|
+
const side = [];
|
|
6146
|
+
if (direction !== "in") side.push({ fromPageId: { $in: ids } });
|
|
6147
|
+
if (direction !== "out") side.push({ toPageId: { $in: ids } });
|
|
6148
|
+
if (wanted.has("body")) {
|
|
6149
|
+
const rows = await (await this.#c("links")).find({
|
|
6150
|
+
toPageId: { $ne: null },
|
|
6151
|
+
$or: side
|
|
6152
|
+
}, { projection: {
|
|
6153
|
+
_id: 0,
|
|
6154
|
+
fromPageId: 1,
|
|
6155
|
+
toPageId: 1,
|
|
6156
|
+
count: 1
|
|
6157
|
+
} }).toArray();
|
|
6158
|
+
for (const row of rows) if (touching(String(row.fromPageId), String(row.toPageId))) add(String(row.fromPageId), String(row.toPageId), "body", Number(row.count));
|
|
6159
|
+
}
|
|
6160
|
+
if (wanted.has("note")) {
|
|
6161
|
+
const noteSide = [];
|
|
6162
|
+
if (direction !== "in") noteSide.push({ pageId: { $in: ids } });
|
|
6163
|
+
if (direction !== "out") noteSide.push({ mentions: { $in: ids } });
|
|
6164
|
+
const rows = await (await this.#c("notes")).find({
|
|
6165
|
+
...live,
|
|
6166
|
+
...this.#visibleNotes(readable),
|
|
6167
|
+
mentions: {
|
|
6168
|
+
$exists: true,
|
|
6169
|
+
$ne: []
|
|
6170
|
+
},
|
|
6171
|
+
$or: noteSide
|
|
6172
|
+
}, { projection: {
|
|
6173
|
+
_id: 0,
|
|
6174
|
+
pageId: 1,
|
|
6175
|
+
mentions: 1
|
|
6176
|
+
} }).toArray();
|
|
6177
|
+
for (const row of rows) for (const mentioned of new Set(row.mentions ?? [])) if (touching(String(row.pageId), mentioned)) add(String(row.pageId), mentioned, "note", 1);
|
|
6178
|
+
}
|
|
6179
|
+
if (!raw.size) return [];
|
|
6180
|
+
const ends = [...new Set([...raw.values()].flatMap((edge) => [edge.fromPageId, edge.toPageId]))];
|
|
6181
|
+
const visible = new Set((await (await this.#c("pages")).find({
|
|
6182
|
+
id: { $in: ends },
|
|
6183
|
+
...live,
|
|
6184
|
+
...this.#inScopes("scopeId", readable)
|
|
6185
|
+
}, { projection: {
|
|
6186
|
+
_id: 0,
|
|
6187
|
+
id: 1
|
|
6188
|
+
} }).toArray()).map((row) => String(row.id)));
|
|
6189
|
+
return [...raw.values()].filter((edge) => visible.has(edge.fromPageId) && visible.has(edge.toPageId));
|
|
6190
|
+
}
|
|
6191
|
+
async countPageLinks(input) {
|
|
6192
|
+
const linked = new Map(input.pageIds.map((id) => [id, {
|
|
6193
|
+
in: /* @__PURE__ */ new Set(),
|
|
6194
|
+
out: /* @__PURE__ */ new Set()
|
|
6195
|
+
}]));
|
|
6196
|
+
for (const edge of await this.#visibleLinks(input.pageIds, input.read, "both", input.kinds)) {
|
|
6197
|
+
linked.get(edge.fromPageId)?.out.add(edge.toPageId);
|
|
6198
|
+
linked.get(edge.toPageId)?.in.add(edge.fromPageId);
|
|
6199
|
+
}
|
|
6200
|
+
return Object.fromEntries([...linked].map(([id, sets]) => [id, {
|
|
6201
|
+
in: sets.in.size,
|
|
6202
|
+
out: sets.out.size
|
|
6203
|
+
}]));
|
|
6204
|
+
}
|
|
6205
|
+
async listPageLinks(input) {
|
|
6206
|
+
const limit = input.limit ?? DEFAULT_LINK_LIMIT;
|
|
6207
|
+
if (!Number.isInteger(limit) || limit < 1) throw new RangeError("Knowledge link limit must be a positive integer");
|
|
6208
|
+
const core = await loadPagesCore();
|
|
6209
|
+
const kindOrder = (kind) => kind === "body" ? 0 : 1;
|
|
6210
|
+
const compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
6211
|
+
return (await this.#visibleLinks(input.pageIds, input.read, input.direction ?? "both", input.kinds)).sort((a, b) => compare(a.fromPageId, b.fromPageId) || compare(a.toPageId, b.toPageId) || kindOrder(a.kind) - kindOrder(b.kind)).slice(0, Math.min(limit, core.MAX_KNOWLEDGE_PAGE_LINKS));
|
|
6212
|
+
}
|
|
6020
6213
|
async countNotes(input) {
|
|
6021
6214
|
const ids = [...new Set(input.pageIds)];
|
|
6022
6215
|
const counts = Object.fromEntries(ids.map((id) => [id, 0]));
|
|
@@ -6197,7 +6390,7 @@ var MongoKnowledgePages = class {
|
|
|
6197
6390
|
async removeNote(input) {
|
|
6198
6391
|
return this.#tx(async (session) => {
|
|
6199
6392
|
const note = await this.#requireNote(input.id, session);
|
|
6200
|
-
if (note.deletedAt)
|
|
6393
|
+
if (note.deletedAt) throw new _mastra_core_storage.KnowledgeNotFoundError("note", input.id);
|
|
6201
6394
|
const now = /* @__PURE__ */ new Date();
|
|
6202
6395
|
const updated = await (await this.#c("notes")).findOneAndUpdate({ id: input.id }, {
|
|
6203
6396
|
$set: {
|
|
@@ -6272,38 +6465,67 @@ var MongoKnowledgePages = class {
|
|
|
6272
6465
|
...live,
|
|
6273
6466
|
...notScope,
|
|
6274
6467
|
...inScope("scopeId"),
|
|
6275
|
-
$
|
|
6468
|
+
$or: words.flatMap((word) => [
|
|
6276
6469
|
{ name: matcher(word) },
|
|
6277
6470
|
{ description: matcher(word) },
|
|
6278
6471
|
{ body: matcher(word) }
|
|
6279
|
-
]
|
|
6472
|
+
])
|
|
6280
6473
|
};
|
|
6474
|
+
const has = (field, word) => ({ $regexMatch: {
|
|
6475
|
+
input: { $ifNull: [`$${field}`, ""] },
|
|
6476
|
+
regex: escapeRegex(word),
|
|
6477
|
+
options: "i"
|
|
6478
|
+
} });
|
|
6281
6479
|
const hits = (await pages.aggregate([
|
|
6282
6480
|
{ $match: pageFilter },
|
|
6283
|
-
{ $addFields: {
|
|
6284
|
-
{ $
|
|
6285
|
-
|
|
6286
|
-
|
|
6287
|
-
|
|
6288
|
-
|
|
6289
|
-
|
|
6290
|
-
|
|
6291
|
-
|
|
6292
|
-
|
|
6293
|
-
|
|
6294
|
-
|
|
6481
|
+
{ $addFields: {
|
|
6482
|
+
searchTier: { $cond: [
|
|
6483
|
+
{ $regexMatch: {
|
|
6484
|
+
input: "$canonicalName",
|
|
6485
|
+
regex: escapeRegex(phrase)
|
|
6486
|
+
} },
|
|
6487
|
+
0,
|
|
6488
|
+
1
|
|
6489
|
+
] },
|
|
6490
|
+
searchCount: { $add: words.map((word) => ({ $cond: [
|
|
6491
|
+
{ $or: [
|
|
6492
|
+
has("name", word),
|
|
6493
|
+
has("description", word),
|
|
6494
|
+
has("body", word)
|
|
6495
|
+
] },
|
|
6295
6496
|
1,
|
|
6296
|
-
|
|
6297
|
-
] }
|
|
6298
|
-
|
|
6497
|
+
0
|
|
6498
|
+
] })) },
|
|
6499
|
+
searchScore: { $add: words.map((word) => ({ $switch: {
|
|
6500
|
+
branches: [
|
|
6501
|
+
{
|
|
6502
|
+
case: has("name", word),
|
|
6503
|
+
then: KNOWLEDGE_SEARCH_WEIGHTS.name
|
|
6504
|
+
},
|
|
6505
|
+
{
|
|
6506
|
+
case: has("description", word),
|
|
6507
|
+
then: KNOWLEDGE_SEARCH_WEIGHTS.description
|
|
6508
|
+
},
|
|
6509
|
+
{
|
|
6510
|
+
case: has("body", word),
|
|
6511
|
+
then: KNOWLEDGE_SEARCH_WEIGHTS.body
|
|
6512
|
+
}
|
|
6513
|
+
],
|
|
6514
|
+
default: 0
|
|
6515
|
+
} })) }
|
|
6516
|
+
} },
|
|
6299
6517
|
{ $sort: {
|
|
6300
6518
|
searchTier: 1,
|
|
6519
|
+
searchCount: -1,
|
|
6520
|
+
searchScore: -1,
|
|
6301
6521
|
updatedAt: -1,
|
|
6302
6522
|
id: 1
|
|
6303
6523
|
} },
|
|
6304
6524
|
{ $limit: limit },
|
|
6305
6525
|
{ $project: {
|
|
6306
6526
|
searchTier: 0,
|
|
6527
|
+
searchCount: 0,
|
|
6528
|
+
searchScore: 0,
|
|
6307
6529
|
_id: 0
|
|
6308
6530
|
} }
|
|
6309
6531
|
]).toArray()).map((row) => ({
|
|
@@ -6311,15 +6533,29 @@ var MongoKnowledgePages = class {
|
|
|
6311
6533
|
page: toPage(row)
|
|
6312
6534
|
}));
|
|
6313
6535
|
if (hits.length >= limit) return hits;
|
|
6314
|
-
const noteRows = await (await this.#c("notes")).
|
|
6315
|
-
|
|
6316
|
-
|
|
6317
|
-
|
|
6318
|
-
|
|
6319
|
-
|
|
6320
|
-
|
|
6321
|
-
|
|
6322
|
-
|
|
6536
|
+
const noteRows = await (await this.#c("notes")).aggregate([
|
|
6537
|
+
{ $match: {
|
|
6538
|
+
...live,
|
|
6539
|
+
...this.#visibleNotes(ids),
|
|
6540
|
+
...scopes ? { pageScopeId: { $in: ids === null ? scopes : scopes.filter((id) => ids.includes(id)) } } : {},
|
|
6541
|
+
$or: words.map((word) => ({ text: matcher(word) }))
|
|
6542
|
+
} },
|
|
6543
|
+
{ $addFields: { searchScore: { $add: words.map((word) => ({ $cond: [
|
|
6544
|
+
has("text", word),
|
|
6545
|
+
1,
|
|
6546
|
+
0
|
|
6547
|
+
] })) } } },
|
|
6548
|
+
{ $sort: {
|
|
6549
|
+
searchScore: -1,
|
|
6550
|
+
updatedAt: -1,
|
|
6551
|
+
id: -1
|
|
6552
|
+
} },
|
|
6553
|
+
{ $limit: limit - hits.length },
|
|
6554
|
+
{ $project: {
|
|
6555
|
+
searchScore: 0,
|
|
6556
|
+
_id: 0
|
|
6557
|
+
} }
|
|
6558
|
+
]).toArray();
|
|
6323
6559
|
const notePages = new Map((await pages.find({
|
|
6324
6560
|
id: { $in: [...new Set(noteRows.map((row) => String(row.pageId)))] },
|
|
6325
6561
|
...live
|
|
@@ -6408,15 +6644,24 @@ var MongoKnowledgePages = class {
|
|
|
6408
6644
|
"activity",
|
|
6409
6645
|
"pins",
|
|
6410
6646
|
"notes",
|
|
6647
|
+
"links",
|
|
6411
6648
|
"pages"
|
|
6412
6649
|
]) await (await this.#c(name)).deleteMany({}, { session });
|
|
6413
6650
|
});
|
|
6414
6651
|
}
|
|
6415
6652
|
async addPinAudience(input) {
|
|
6653
|
+
const kind = input.kind ?? "page";
|
|
6416
6654
|
return this.#tx(async (session) => {
|
|
6417
6655
|
const page = await this.#requireLivePage(input.pageId, session);
|
|
6418
6656
|
const now = /* @__PURE__ */ new Date();
|
|
6419
|
-
const { pin, changed } = await this.#upsertPinAudience(session,
|
|
6657
|
+
const { pin, changed } = await this.#upsertPinAudience(session, {
|
|
6658
|
+
pageId: page.id,
|
|
6659
|
+
kind,
|
|
6660
|
+
depth: kind === "list" ? input.depth ?? 1 : null,
|
|
6661
|
+
audience: input.audience,
|
|
6662
|
+
createdBy: input.createdBy,
|
|
6663
|
+
now
|
|
6664
|
+
});
|
|
6420
6665
|
if (changed) await this.#log(session, {
|
|
6421
6666
|
action: "pin-added",
|
|
6422
6667
|
scopeId: page.scopeId,
|
|
@@ -6427,9 +6672,14 @@ var MongoKnowledgePages = class {
|
|
|
6427
6672
|
return pin;
|
|
6428
6673
|
});
|
|
6429
6674
|
}
|
|
6430
|
-
|
|
6675
|
+
/** `keepDepth` (merges) leaves an existing pin's depth as it is instead of setting `depth`. */
|
|
6676
|
+
async #upsertPinAudience(session, input) {
|
|
6677
|
+
const { pageId, kind, audience, now } = input;
|
|
6431
6678
|
const pins = await this.#c("pins");
|
|
6432
|
-
const existing = await pins.findOne({
|
|
6679
|
+
const existing = await pins.findOne({
|
|
6680
|
+
pageId,
|
|
6681
|
+
kind
|
|
6682
|
+
}, {
|
|
6433
6683
|
...noProjection,
|
|
6434
6684
|
session
|
|
6435
6685
|
});
|
|
@@ -6437,8 +6687,10 @@ var MongoKnowledgePages = class {
|
|
|
6437
6687
|
const doc = {
|
|
6438
6688
|
id: (0, _mastra_core_storage.createKnowledgeUlid)(),
|
|
6439
6689
|
pageId,
|
|
6690
|
+
kind,
|
|
6691
|
+
...input.depth === null ? {} : { depth: input.depth },
|
|
6440
6692
|
audience: [...new Set(audience)],
|
|
6441
|
-
createdBy,
|
|
6693
|
+
createdBy: input.createdBy,
|
|
6442
6694
|
createdAt: now,
|
|
6443
6695
|
updatedAt: now
|
|
6444
6696
|
};
|
|
@@ -6455,13 +6707,22 @@ var MongoKnowledgePages = class {
|
|
|
6455
6707
|
}
|
|
6456
6708
|
const current = existing.audience ?? [];
|
|
6457
6709
|
const merged = [.../* @__PURE__ */ new Set([...current, ...audience])];
|
|
6458
|
-
|
|
6710
|
+
const currentDepth = existing.depth == null ? null : Number(existing.depth);
|
|
6711
|
+
const depth = input.keepDepth ? currentDepth : input.depth;
|
|
6712
|
+
if (merged.length === current.length && depth === currentDepth) return {
|
|
6459
6713
|
pin: toPin(existing),
|
|
6460
6714
|
changed: false
|
|
6461
6715
|
};
|
|
6462
6716
|
return {
|
|
6463
|
-
pin: toPin(await pins.findOneAndUpdate({ id: existing.id },
|
|
6717
|
+
pin: toPin(await pins.findOneAndUpdate({ id: existing.id }, depth === null ? {
|
|
6718
|
+
$set: {
|
|
6719
|
+
audience: merged,
|
|
6720
|
+
updatedAt: now
|
|
6721
|
+
},
|
|
6722
|
+
$unset: { depth: "" }
|
|
6723
|
+
} : { $set: {
|
|
6464
6724
|
audience: merged,
|
|
6725
|
+
depth,
|
|
6465
6726
|
updatedAt: now
|
|
6466
6727
|
} }, {
|
|
6467
6728
|
returnDocument: "after",
|
|
@@ -6474,7 +6735,10 @@ var MongoKnowledgePages = class {
|
|
|
6474
6735
|
async removePinAudience(input) {
|
|
6475
6736
|
return this.#tx(async (session) => {
|
|
6476
6737
|
const pins = await this.#c("pins");
|
|
6477
|
-
const existing = await pins.findOne({
|
|
6738
|
+
const existing = await pins.findOne({
|
|
6739
|
+
pageId: input.pageId,
|
|
6740
|
+
kind: input.kind ?? "page"
|
|
6741
|
+
}, {
|
|
6478
6742
|
...noProjection,
|
|
6479
6743
|
session
|
|
6480
6744
|
});
|
|
@@ -6534,10 +6798,11 @@ var MongoKnowledgePages = class {
|
|
|
6534
6798
|
const ids = await this.#readable(input.read);
|
|
6535
6799
|
const match = {};
|
|
6536
6800
|
if (input.audience !== "all") match.audience = { $in: input.audience };
|
|
6537
|
-
|
|
6538
|
-
if (input.pageIds) pageId
|
|
6539
|
-
|
|
6540
|
-
if (
|
|
6801
|
+
if (input.kind) match.kind = input.kind;
|
|
6802
|
+
if (input.pageIds) match.pageId = { $in: input.pageIds };
|
|
6803
|
+
const pageOnly = [];
|
|
6804
|
+
if (input.excludePageIds?.length) pageOnly.push({ $or: [{ kind: { $ne: "page" } }, { pageId: { $nin: input.excludePageIds } }] });
|
|
6805
|
+
if (input.withBody) pageOnly.push({ $or: [{ kind: { $ne: "page" } }, { "page.body": { $regex: "\\S" } }] });
|
|
6541
6806
|
const pins = await this.#c("pins");
|
|
6542
6807
|
const base = [
|
|
6543
6808
|
{ $match: match },
|
|
@@ -6550,7 +6815,7 @@ var MongoKnowledgePages = class {
|
|
|
6550
6815
|
{ $match: {
|
|
6551
6816
|
"page.deletedAt": { $exists: false },
|
|
6552
6817
|
...ids === null ? {} : { "page.scopeId": { $in: ids } },
|
|
6553
|
-
...
|
|
6818
|
+
...pageOnly.length ? { $and: pageOnly } : {}
|
|
6554
6819
|
} }
|
|
6555
6820
|
];
|
|
6556
6821
|
const pageMatch = [];
|
|
@@ -6616,7 +6881,8 @@ var KnowledgeMongoDB = class extends _mastra_core_storage.KnowledgeStorage {
|
|
|
6616
6881
|
...super.getCapabilities(),
|
|
6617
6882
|
schemaVersion: 4,
|
|
6618
6883
|
supportsSchemaInspection: true,
|
|
6619
|
-
supportsExplicitReset: true
|
|
6884
|
+
supportsExplicitReset: true,
|
|
6885
|
+
supportsPinKinds: true
|
|
6620
6886
|
};
|
|
6621
6887
|
}
|
|
6622
6888
|
async init() {
|
|
@@ -6646,6 +6912,9 @@ var KnowledgeMongoDB = class extends _mastra_core_storage.KnowledgeStorage {
|
|
|
6646
6912
|
listChildScopes(input) {
|
|
6647
6913
|
return this.#pages.listChildScopes(input);
|
|
6648
6914
|
}
|
|
6915
|
+
countChildScopes(input) {
|
|
6916
|
+
return this.#pages.countChildScopes(input);
|
|
6917
|
+
}
|
|
6649
6918
|
getPages(input) {
|
|
6650
6919
|
return this.#pages.getPages(input);
|
|
6651
6920
|
}
|
|
@@ -6685,6 +6954,21 @@ var KnowledgeMongoDB = class extends _mastra_core_storage.KnowledgeStorage {
|
|
|
6685
6954
|
listNotes(input) {
|
|
6686
6955
|
return this.#pages.listNotes(input);
|
|
6687
6956
|
}
|
|
6957
|
+
replacePageBodyLinks(input) {
|
|
6958
|
+
return this.#pages.replacePageBodyLinks(input);
|
|
6959
|
+
}
|
|
6960
|
+
listPageBodyLinkSources(input) {
|
|
6961
|
+
return this.#pages.listPageBodyLinkSources(input);
|
|
6962
|
+
}
|
|
6963
|
+
countPageLinks(input) {
|
|
6964
|
+
return this.#pages.countPageLinks(input);
|
|
6965
|
+
}
|
|
6966
|
+
listPageLinks(input) {
|
|
6967
|
+
return this.#pages.listPageLinks(input);
|
|
6968
|
+
}
|
|
6969
|
+
countChildPages(input) {
|
|
6970
|
+
return this.#pages.countChildPages(input);
|
|
6971
|
+
}
|
|
6688
6972
|
countNotes(input) {
|
|
6689
6973
|
return this.#pages.countNotes(input);
|
|
6690
6974
|
}
|