@mastra/mysql 0.16.0-alpha.2 → 0.16.0-alpha.3
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@0.1.4/dist/knowledge-compat/index.d.ts +9 -4
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/index.cjs +212 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +212 -22
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/knowledge/index.d.ts +6 -0
- package/dist/storage/domains/knowledge/index.d.ts.map +1 -1
- package/dist/storage/domains/knowledge/pages.d.ts +30 -11
- package/dist/storage/domains/knowledge/pages.d.ts.map +1 -1
- package/package.json +6 -6
|
@@ -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,15 +326,19 @@ 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
|
-
* least one of them (as a case-insensitive
|
|
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[];
|
|
333
337
|
/**
|
|
334
338
|
* Points one matched search word earns on a page, by the best field it appears in: name, then
|
|
335
|
-
* description, then body.
|
|
336
|
-
*
|
|
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.
|
|
337
342
|
*/
|
|
338
343
|
export declare const KNOWLEDGE_SEARCH_WEIGHTS: {
|
|
339
344
|
readonly name: 3;
|
package/dist/docs/SKILL.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -4301,7 +4301,8 @@ const KNOWLEDGE_PAGES_CORE_FUNCTIONS = [
|
|
|
4301
4301
|
const KNOWLEDGE_PAGES_CORE_NUMBERS = [
|
|
4302
4302
|
"MAX_KNOWLEDGE_MOVE_PAGES",
|
|
4303
4303
|
"MAX_KNOWLEDGE_PAGE_DEPTH",
|
|
4304
|
-
"MAX_KNOWLEDGE_PAGE_LIST_LIMIT"
|
|
4304
|
+
"MAX_KNOWLEDGE_PAGE_LIST_LIMIT",
|
|
4305
|
+
"MAX_KNOWLEDGE_PAGE_LINKS"
|
|
4305
4306
|
];
|
|
4306
4307
|
function resolveKnowledgePagesCore(storageModule) {
|
|
4307
4308
|
const module = typeof storageModule === "object" && storageModule !== null ? storageModule : {};
|
|
@@ -4322,16 +4323,18 @@ function createKnowledgePagesCoreLoader(features, loadStorageModule) {
|
|
|
4322
4323
|
};
|
|
4323
4324
|
}
|
|
4324
4325
|
/**
|
|
4325
|
-
* Keyword-search words: the query split on whitespace, lowercased, deduplicated
|
|
4326
|
-
* least one of them (as a case-insensitive
|
|
4326
|
+
* Keyword-search words: the query split on whitespace, lowercased, deduplicated, keeping the first
|
|
4327
|
+
* {@link KNOWLEDGE_SEARCH_MAX_WORDS}. A hit needs at least one of them (as a case-insensitive
|
|
4328
|
+
* substring); an empty list means the search returns nothing.
|
|
4327
4329
|
*/
|
|
4328
4330
|
function knowledgeSearchWords(query) {
|
|
4329
|
-
return [...new Set(query.toLowerCase().split(/\s+/).filter(Boolean))];
|
|
4331
|
+
return [...new Set(query.toLowerCase().split(/\s+/).filter(Boolean))].slice(0, 16);
|
|
4330
4332
|
}
|
|
4331
4333
|
/**
|
|
4332
4334
|
* Points one matched search word earns on a page, by the best field it appears in: name, then
|
|
4333
|
-
* description, then body.
|
|
4334
|
-
*
|
|
4335
|
+
* description, then body. A page whose name contains the whole query ranks first; below that, pages
|
|
4336
|
+
* rank by how many query words they match, and these points only break ties between pages that
|
|
4337
|
+
* match the same number of words.
|
|
4335
4338
|
*/
|
|
4336
4339
|
const KNOWLEDGE_SEARCH_WEIGHTS = {
|
|
4337
4340
|
name: 3,
|
|
@@ -4357,7 +4360,8 @@ const T = {
|
|
|
4357
4360
|
mentions: "`mastra_knowledge_mentions`",
|
|
4358
4361
|
pins: "`mastra_knowledge_pins`",
|
|
4359
4362
|
pinAudiences: "`mastra_knowledge_pin_audiences`",
|
|
4360
|
-
activity: "`mastra_knowledge_activity`"
|
|
4363
|
+
activity: "`mastra_knowledge_activity`",
|
|
4364
|
+
links: "`mastra_knowledge_page_links`"
|
|
4361
4365
|
};
|
|
4362
4366
|
const KEY = "`key`";
|
|
4363
4367
|
const WHEN = "`when`";
|
|
@@ -4368,6 +4372,11 @@ const LIVE_KEY_COLUMN = `VARCHAR(768) ${BIN} AS (IF(deletedAt IS NULL AND \`key\
|
|
|
4368
4372
|
const MISSING_PAGE_INDEX = `SELECT 1 FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM information_schema.statistics WHERE table_schema = DATABASE() AND table_name = 'mastra_knowledge_pages' AND index_name = ?)`;
|
|
4369
4373
|
/** Upgrades for Knowledge databases created by an earlier alpha build; `needed` returns a row when `apply` must run. */
|
|
4370
4374
|
const KNOWLEDGE_PAGE_UPGRADES = [
|
|
4375
|
+
{
|
|
4376
|
+
needed: `SELECT 1 FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'mastra_knowledge_pins' AND column_name = 'kind')`,
|
|
4377
|
+
args: [],
|
|
4378
|
+
apply: ["ALTER TABLE `mastra_knowledge_pins` ADD COLUMN kind VARCHAR(16) NOT NULL DEFAULT 'page', ADD COLUMN depth INT NULL, DROP INDEX idx_kpins_page, ADD UNIQUE KEY idx_kpins_page_kind (pageId, kind)"]
|
|
4379
|
+
},
|
|
4371
4380
|
{
|
|
4372
4381
|
needed: `SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'mastra_knowledge_pages' AND column_name = 'liveKey' AND generation_expression LIKE '%scopeId%'`,
|
|
4373
4382
|
args: [],
|
|
@@ -4460,17 +4469,27 @@ const KNOWLEDGE_PAGE_DDL = [
|
|
|
4460
4469
|
`CREATE TABLE IF NOT EXISTS ${T.pins} (
|
|
4461
4470
|
id VARCHAR(255) ${BIN} NOT NULL PRIMARY KEY,
|
|
4462
4471
|
pageId VARCHAR(255) ${BIN} NOT NULL,
|
|
4472
|
+
kind VARCHAR(16) NOT NULL DEFAULT 'page',
|
|
4473
|
+
depth INT NULL,
|
|
4463
4474
|
audience JSON NOT NULL,
|
|
4464
4475
|
createdBy VARCHAR(255) NOT NULL,
|
|
4465
4476
|
createdAt VARCHAR(32) NOT NULL,
|
|
4466
4477
|
updatedAt VARCHAR(32) NOT NULL,
|
|
4467
|
-
UNIQUE KEY
|
|
4478
|
+
UNIQUE KEY idx_kpins_page_kind (pageId, kind)
|
|
4468
4479
|
)`,
|
|
4469
4480
|
`CREATE TABLE IF NOT EXISTS ${T.pinAudiences} (
|
|
4470
4481
|
audience VARCHAR(512) ${BIN} NOT NULL,
|
|
4471
4482
|
pinId VARCHAR(255) ${BIN} NOT NULL,
|
|
4472
4483
|
PRIMARY KEY (audience, pinId),
|
|
4473
4484
|
KEY idx_kpin_audiences_pin (pinId)
|
|
4485
|
+
)`,
|
|
4486
|
+
`CREATE TABLE IF NOT EXISTS ${T.links} (
|
|
4487
|
+
fromPageId VARCHAR(255) ${BIN} NOT NULL,
|
|
4488
|
+
ref VARCHAR(512) ${BIN} NOT NULL,
|
|
4489
|
+
toPageId VARCHAR(255) ${BIN} NULL,
|
|
4490
|
+
linkCount INT NOT NULL,
|
|
4491
|
+
PRIMARY KEY (fromPageId, ref),
|
|
4492
|
+
KEY idx_klinks_to_ref (toPageId, ref)
|
|
4474
4493
|
)`,
|
|
4475
4494
|
`CREATE TABLE IF NOT EXISTS ${T.activity} (
|
|
4476
4495
|
id VARCHAR(255) ${BIN} NOT NULL PRIMARY KEY,
|
|
@@ -4536,6 +4555,8 @@ function toPin(row) {
|
|
|
4536
4555
|
return {
|
|
4537
4556
|
id: String(row.id),
|
|
4538
4557
|
pageId: String(row.pageId),
|
|
4558
|
+
kind: String(row.kind),
|
|
4559
|
+
...row.depth == null ? {} : { depth: Number(row.depth) },
|
|
4539
4560
|
audience: parseJsonList(row.audience),
|
|
4540
4561
|
createdBy: String(row.createdBy),
|
|
4541
4562
|
createdAt: new Date(String(row.createdAt)),
|
|
@@ -4707,6 +4728,7 @@ var MySQLKnowledgePages = class {
|
|
|
4707
4728
|
T.mentions,
|
|
4708
4729
|
T.stamps,
|
|
4709
4730
|
T.notes,
|
|
4731
|
+
T.links,
|
|
4710
4732
|
T.pages
|
|
4711
4733
|
]) await tx.execute({
|
|
4712
4734
|
sql: `DELETE FROM ${table}`,
|
|
@@ -4889,13 +4911,19 @@ var MySQLKnowledgePages = class {
|
|
|
4889
4911
|
const limit = limitOf(input.limit, core.MAX_KNOWLEDGE_PAGE_LIST_LIMIT);
|
|
4890
4912
|
const cursor = decodeCursor(input.cursor, 2);
|
|
4891
4913
|
const args = [input.parentId];
|
|
4914
|
+
let filter = "";
|
|
4915
|
+
if (input.read) {
|
|
4916
|
+
const readable = pageReadable("p", input.read);
|
|
4917
|
+
filter = ` AND ${readable.sql}`;
|
|
4918
|
+
args.push(...readable.args);
|
|
4919
|
+
}
|
|
4892
4920
|
let after = "";
|
|
4893
4921
|
if (cursor) {
|
|
4894
|
-
after = ` AND (position > ? OR (position = ? AND id > ?))`;
|
|
4922
|
+
after = ` AND (p.position > ? OR (p.position = ? AND p.id > ?))`;
|
|
4895
4923
|
args.push(cursor[0], cursor[0], cursor[1]);
|
|
4896
4924
|
}
|
|
4897
4925
|
args.push(limit + 1);
|
|
4898
|
-
const rows = await this.#query(this.#read, `SELECT
|
|
4926
|
+
const rows = await this.#query(this.#read, `SELECT p.* FROM ${T.pages} p WHERE p.isScope = 1 AND p.parentId <=> ?${filter}${after} ORDER BY p.position, p.id LIMIT ?`, args);
|
|
4899
4927
|
return this.#pageOutput(rows, limit, (row) => [String(row.position), String(row.id)]);
|
|
4900
4928
|
}
|
|
4901
4929
|
#pageOutput(rows, limit, cursorOf) {
|
|
@@ -5185,9 +5213,8 @@ var MySQLKnowledgePages = class {
|
|
|
5185
5213
|
args: [input.sourceId]
|
|
5186
5214
|
});
|
|
5187
5215
|
const pins = await this.#query(tx, `SELECT * FROM ${T.pins} WHERE pageId IN (?, ?)`, [input.sourceId, input.targetId]);
|
|
5188
|
-
const sourcePin
|
|
5189
|
-
|
|
5190
|
-
const targetPin = pins.find((pin) => String(pin.pageId) === input.targetId);
|
|
5216
|
+
for (const sourcePin of pins.filter((pin) => String(pin.pageId) === input.sourceId)) {
|
|
5217
|
+
const targetPin = pins.find((pin) => String(pin.pageId) === input.targetId && pin.kind === sourcePin.kind);
|
|
5191
5218
|
const audience = parseJsonList(sourcePin.audience);
|
|
5192
5219
|
if (targetPin) {
|
|
5193
5220
|
const merged = [.../* @__PURE__ */ new Set([...parseJsonList(targetPin.audience), ...audience])];
|
|
@@ -5287,6 +5314,15 @@ var MySQLKnowledgePages = class {
|
|
|
5287
5314
|
const rows = await this.#query(this.#read, `SELECT n.* FROM ${T.notes} n WHERE n.pageId = ?${input.includeDeleted ? "" : " AND n.deletedAt IS NULL"} AND ${readable.sql}${after} ORDER BY n.createdAt, n.id LIMIT ?`, args);
|
|
5288
5315
|
return this.#noteOutput(rows, limit);
|
|
5289
5316
|
}
|
|
5317
|
+
async countChildScopes(input) {
|
|
5318
|
+
const ids = [...new Set(input.parentIds)];
|
|
5319
|
+
const counts = Object.fromEntries(ids.map((id) => [id, 0]));
|
|
5320
|
+
if (ids.length === 0) return counts;
|
|
5321
|
+
const readable = pageReadable("p", input.read);
|
|
5322
|
+
const rows = await this.#query(this.#read, `SELECT p.parentId AS parentId, COUNT(*) AS total FROM ${T.pages} p WHERE p.parentId IN ${JV} AND p.isScope = 1 AND ${readable.sql} GROUP BY p.parentId`, [jsonList(ids), ...readable.args]);
|
|
5323
|
+
for (const row of rows) counts[String(row.parentId)] = Number(row.total);
|
|
5324
|
+
return counts;
|
|
5325
|
+
}
|
|
5290
5326
|
async countChildPages(input) {
|
|
5291
5327
|
const ids = [...new Set(input.parentIds)];
|
|
5292
5328
|
const counts = Object.fromEntries(ids.map((id) => [id, 0]));
|
|
@@ -5296,6 +5332,130 @@ var MySQLKnowledgePages = class {
|
|
|
5296
5332
|
for (const row of rows) counts[String(row.parentId)] = Number(row.total);
|
|
5297
5333
|
return counts;
|
|
5298
5334
|
}
|
|
5335
|
+
async replacePageBodyLinks(input) {
|
|
5336
|
+
await this.#transaction(async (tx) => {
|
|
5337
|
+
await tx.execute({
|
|
5338
|
+
sql: `DELETE FROM ${T.links} WHERE fromPageId = ?`,
|
|
5339
|
+
args: [input.pageId]
|
|
5340
|
+
});
|
|
5341
|
+
if (input.links.length === 0) return;
|
|
5342
|
+
await tx.execute({
|
|
5343
|
+
sql: `INSERT INTO ${T.links} (fromPageId, ref, toPageId, linkCount) VALUES ${input.links.map(() => "(?, ?, ?, ?)").join(", ")}`,
|
|
5344
|
+
args: input.links.flatMap((link) => [
|
|
5345
|
+
input.pageId,
|
|
5346
|
+
link.ref,
|
|
5347
|
+
link.toPageId,
|
|
5348
|
+
link.count
|
|
5349
|
+
])
|
|
5350
|
+
});
|
|
5351
|
+
});
|
|
5352
|
+
}
|
|
5353
|
+
async listPageBodyLinkSources(input) {
|
|
5354
|
+
const refs = [...new Set(input.refs ?? [])];
|
|
5355
|
+
const targets = [...new Set(input.toPageIds ?? [])];
|
|
5356
|
+
const parts = [];
|
|
5357
|
+
const args = [];
|
|
5358
|
+
if (refs.length) {
|
|
5359
|
+
parts.push(`(toPageId IS NULL AND ref IN ${JV})`);
|
|
5360
|
+
args.push(jsonList(refs));
|
|
5361
|
+
}
|
|
5362
|
+
if (targets.length) {
|
|
5363
|
+
parts.push(`toPageId IN ${JV}`);
|
|
5364
|
+
args.push(jsonList(targets));
|
|
5365
|
+
}
|
|
5366
|
+
if (!parts.length) return [];
|
|
5367
|
+
return (await this.#query(this.#read, `SELECT DISTINCT fromPageId FROM ${T.links} WHERE ${parts.join(" OR ")}`, args)).map((row) => String(row.fromPageId));
|
|
5368
|
+
}
|
|
5369
|
+
/**
|
|
5370
|
+
* Every resolved link touching `ids` whose both ends are live and readable, one row per
|
|
5371
|
+
* (from, to, kind); no self-links. Note links go through the same visible-note rule as listNotes.
|
|
5372
|
+
*/
|
|
5373
|
+
#visibleLinkEdges(ids, read, direction, kinds) {
|
|
5374
|
+
const wanted = new Set(kinds ?? ["body", "note"]);
|
|
5375
|
+
const idList = jsonList(ids);
|
|
5376
|
+
const touch = (from, to) => direction === "out" ? {
|
|
5377
|
+
sql: `${from} IN ${JV}`,
|
|
5378
|
+
args: [idList]
|
|
5379
|
+
} : direction === "in" ? {
|
|
5380
|
+
sql: `${to} IN ${JV}`,
|
|
5381
|
+
args: [idList]
|
|
5382
|
+
} : {
|
|
5383
|
+
sql: `(${from} IN ${JV} OR ${to} IN ${JV})`,
|
|
5384
|
+
args: [idList, idList]
|
|
5385
|
+
};
|
|
5386
|
+
const branches = [];
|
|
5387
|
+
const args = [];
|
|
5388
|
+
if (wanted.has("body")) {
|
|
5389
|
+
const fromReadable = pageReadable("fp", read);
|
|
5390
|
+
const toReadable = pageReadable("tp", read);
|
|
5391
|
+
const t = touch("l.fromPageId", "l.toPageId");
|
|
5392
|
+
branches.push(`SELECT l.fromPageId AS fromPageId, l.toPageId AS toPageId, 'body' AS kind, CAST(SUM(l.linkCount) AS SIGNED) AS cnt
|
|
5393
|
+
FROM ${T.links} l
|
|
5394
|
+
JOIN ${T.pages} fp ON fp.id = l.fromPageId AND fp.deletedAt IS NULL AND ${fromReadable.sql}
|
|
5395
|
+
JOIN ${T.pages} tp ON tp.id = l.toPageId AND tp.deletedAt IS NULL AND ${toReadable.sql}
|
|
5396
|
+
WHERE l.toPageId IS NOT NULL AND l.fromPageId <> l.toPageId AND ${t.sql}
|
|
5397
|
+
GROUP BY l.fromPageId, l.toPageId`);
|
|
5398
|
+
args.push(...fromReadable.args, ...toReadable.args, ...t.args);
|
|
5399
|
+
}
|
|
5400
|
+
if (wanted.has("note")) {
|
|
5401
|
+
const visible = noteReadable("n", read);
|
|
5402
|
+
const toReadable = pageReadable("tp", read);
|
|
5403
|
+
const t = touch("vn.pageId", "mn.pageId");
|
|
5404
|
+
branches.push(`SELECT vn.pageId AS fromPageId, mn.pageId AS toPageId, 'note' AS kind, CAST(COUNT(*) AS SIGNED) AS cnt
|
|
5405
|
+
FROM (SELECT n.id AS id, n.pageId AS pageId FROM ${T.notes} n
|
|
5406
|
+
JOIN ${T.pages} np ON np.id = n.pageId AND np.deletedAt IS NULL
|
|
5407
|
+
WHERE n.deletedAt IS NULL AND (n.pageId IN ${JV} OR n.id IN (SELECT mm.noteId FROM ${T.mentions} mm WHERE mm.pageId IN ${JV}))
|
|
5408
|
+
AND ${visible.sql}) vn
|
|
5409
|
+
JOIN ${T.mentions} mn ON mn.noteId = vn.id
|
|
5410
|
+
JOIN ${T.pages} tp ON tp.id = mn.pageId AND tp.deletedAt IS NULL AND ${toReadable.sql}
|
|
5411
|
+
WHERE mn.pageId <> vn.pageId AND ${t.sql}
|
|
5412
|
+
GROUP BY vn.pageId, mn.pageId`);
|
|
5413
|
+
args.push(idList, idList, ...visible.args, ...toReadable.args, ...t.args);
|
|
5414
|
+
}
|
|
5415
|
+
return {
|
|
5416
|
+
sql: branches.length ? branches.join(" UNION ALL ") : `SELECT NULL AS fromPageId, NULL AS toPageId, NULL AS kind, NULL AS cnt FROM DUAL WHERE FALSE`,
|
|
5417
|
+
args
|
|
5418
|
+
};
|
|
5419
|
+
}
|
|
5420
|
+
async countPageLinks(input) {
|
|
5421
|
+
const ids = [...new Set(input.pageIds)];
|
|
5422
|
+
const counts = Object.fromEntries(ids.map((id) => [id, {
|
|
5423
|
+
in: 0,
|
|
5424
|
+
out: 0
|
|
5425
|
+
}]));
|
|
5426
|
+
if (ids.length === 0) return counts;
|
|
5427
|
+
const edges = this.#visibleLinkEdges(ids, input.read, "both", input.kinds);
|
|
5428
|
+
const idList = jsonList(ids);
|
|
5429
|
+
const rows = await this.#query(this.#read, `WITH e AS (${edges.sql})
|
|
5430
|
+
SELECT r.id AS id, CAST(SUM(r.o) AS SIGNED) AS outCount, CAST(SUM(r.i) AS SIGNED) AS inCount FROM (
|
|
5431
|
+
SELECT fromPageId AS id, COUNT(DISTINCT toPageId) AS o, 0 AS i FROM e WHERE fromPageId IN ${JV} GROUP BY fromPageId
|
|
5432
|
+
UNION ALL
|
|
5433
|
+
SELECT toPageId AS id, 0 AS o, COUNT(DISTINCT fromPageId) AS i FROM e WHERE toPageId IN ${JV} GROUP BY toPageId
|
|
5434
|
+
) r GROUP BY r.id`, [
|
|
5435
|
+
...edges.args,
|
|
5436
|
+
idList,
|
|
5437
|
+
idList
|
|
5438
|
+
]);
|
|
5439
|
+
for (const row of rows) counts[String(row.id)] = {
|
|
5440
|
+
in: Number(row.inCount),
|
|
5441
|
+
out: Number(row.outCount)
|
|
5442
|
+
};
|
|
5443
|
+
return counts;
|
|
5444
|
+
}
|
|
5445
|
+
async listPageLinks(input) {
|
|
5446
|
+
const limit = input.limit ?? 1e3;
|
|
5447
|
+
if (!Number.isInteger(limit) || limit < 1) throw new RangeError("Knowledge link limit must be a positive integer");
|
|
5448
|
+
const core = await loadPagesCore();
|
|
5449
|
+
const ids = [...new Set(input.pageIds)];
|
|
5450
|
+
if (ids.length === 0) return [];
|
|
5451
|
+
const edges = this.#visibleLinkEdges(ids, input.read, input.direction ?? "both", input.kinds);
|
|
5452
|
+
return (await this.#query(this.#read, `SELECT * FROM (${edges.sql}) e ORDER BY fromPageId, toPageId, (kind = 'note') LIMIT ${Math.min(limit, core.MAX_KNOWLEDGE_PAGE_LINKS)}`, edges.args)).map((row) => ({
|
|
5453
|
+
fromPageId: String(row.fromPageId),
|
|
5454
|
+
toPageId: String(row.toPageId),
|
|
5455
|
+
kind: String(row.kind),
|
|
5456
|
+
count: Number(row.cnt)
|
|
5457
|
+
}));
|
|
5458
|
+
}
|
|
5299
5459
|
async countNotes(input) {
|
|
5300
5460
|
const ids = [...new Set(input.pageIds)];
|
|
5301
5461
|
const counts = Object.fromEntries(ids.map((id) => [id, 0]));
|
|
@@ -5557,17 +5717,21 @@ var MySQLKnowledgePages = class {
|
|
|
5557
5717
|
});
|
|
5558
5718
|
}
|
|
5559
5719
|
addPinAudience(input) {
|
|
5720
|
+
const kind = input.kind ?? "page";
|
|
5721
|
+
const depth = kind === "list" ? input.depth ?? 1 : null;
|
|
5560
5722
|
return this.#transaction(async (tx) => {
|
|
5561
5723
|
const page = await this.#livePage(tx, input.pageId);
|
|
5562
5724
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5563
|
-
const existing = (await this.#query(tx, `SELECT * FROM ${T.pins} WHERE pageId = ?`, [input.pageId]))[0];
|
|
5725
|
+
const existing = (await this.#query(tx, `SELECT * FROM ${T.pins} WHERE pageId = ? AND kind = ?`, [input.pageId, kind]))[0];
|
|
5564
5726
|
if (!existing) {
|
|
5565
5727
|
const id = (0, _mastra_core_storage.createKnowledgeUlid)();
|
|
5566
5728
|
await tx.execute({
|
|
5567
|
-
sql: `INSERT INTO ${T.pins} (id, pageId, audience, createdBy, createdAt, updatedAt) VALUES (?, ?, ?, ?, ?, ?)`,
|
|
5729
|
+
sql: `INSERT INTO ${T.pins} (id, pageId, kind, depth, audience, createdBy, createdAt, updatedAt) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
5568
5730
|
args: [
|
|
5569
5731
|
id,
|
|
5570
5732
|
input.pageId,
|
|
5733
|
+
kind,
|
|
5734
|
+
depth,
|
|
5571
5735
|
JSON.stringify([...new Set(input.audience)]),
|
|
5572
5736
|
input.createdBy,
|
|
5573
5737
|
now,
|
|
@@ -5584,11 +5748,13 @@ var MySQLKnowledgePages = class {
|
|
|
5584
5748
|
}
|
|
5585
5749
|
const current = parseJsonList(existing.audience);
|
|
5586
5750
|
const merged = [.../* @__PURE__ */ new Set([...current, ...input.audience])];
|
|
5587
|
-
|
|
5751
|
+
const depthChanged = (existing.depth == null ? null : Number(existing.depth)) !== depth;
|
|
5752
|
+
if (merged.length !== current.length || depthChanged) {
|
|
5588
5753
|
await tx.execute({
|
|
5589
|
-
sql: `UPDATE ${T.pins} SET audience = ?, updatedAt = ? WHERE id = ?`,
|
|
5754
|
+
sql: `UPDATE ${T.pins} SET audience = ?, depth = ?, updatedAt = ? WHERE id = ?`,
|
|
5590
5755
|
args: [
|
|
5591
5756
|
JSON.stringify(merged),
|
|
5757
|
+
depth,
|
|
5592
5758
|
now,
|
|
5593
5759
|
String(existing.id)
|
|
5594
5760
|
]
|
|
@@ -5605,7 +5771,7 @@ var MySQLKnowledgePages = class {
|
|
|
5605
5771
|
}
|
|
5606
5772
|
removePinAudience(input) {
|
|
5607
5773
|
return this.#transaction(async (tx) => {
|
|
5608
|
-
const existing = (await this.#query(tx, `SELECT * FROM ${T.pins} WHERE pageId = ?`, [input.pageId]))[0];
|
|
5774
|
+
const existing = (await this.#query(tx, `SELECT * FROM ${T.pins} WHERE pageId = ? AND kind = ?`, [input.pageId, input.kind ?? "page"]))[0];
|
|
5609
5775
|
if (!existing) return null;
|
|
5610
5776
|
const current = parseJsonList(existing.audience);
|
|
5611
5777
|
const remove = new Set(input.audience);
|
|
@@ -5649,15 +5815,19 @@ var MySQLKnowledgePages = class {
|
|
|
5649
5815
|
filters += ` AND pin.id IN (SELECT pa.pinId FROM (SELECT jv.v FROM JSON_TABLE(?, '$[*]' COLUMNS (v VARCHAR(512) ${BIN} PATH '$')) jv) aud STRAIGHT_JOIN ${T.pinAudiences} pa ON pa.audience = aud.v)`;
|
|
5650
5816
|
args.push(jsonList(input.audience));
|
|
5651
5817
|
}
|
|
5818
|
+
if (input.kind) {
|
|
5819
|
+
filters += ` AND pin.kind = ?`;
|
|
5820
|
+
args.push(input.kind);
|
|
5821
|
+
}
|
|
5652
5822
|
if (input.pageIds) {
|
|
5653
5823
|
filters += ` AND pin.pageId IN ${JV}`;
|
|
5654
5824
|
args.push(jsonList(input.pageIds));
|
|
5655
5825
|
}
|
|
5656
5826
|
if (input.excludePageIds?.length) {
|
|
5657
|
-
filters += ` AND pin.pageId NOT IN ${JV}`;
|
|
5827
|
+
filters += ` AND (pin.kind <> 'page' OR pin.pageId NOT IN ${JV})`;
|
|
5658
5828
|
args.push(jsonList(input.excludePageIds));
|
|
5659
5829
|
}
|
|
5660
|
-
if (input.withBody) filters += ` AND REGEXP_LIKE(p.body, '[^[:space:]]')`;
|
|
5830
|
+
if (input.withBody) filters += ` AND (pin.kind <> 'page' OR REGEXP_LIKE(p.body, '[^[:space:]]'))`;
|
|
5661
5831
|
const from = `FROM ${T.pins} pin JOIN ${T.pages} p ON p.id = pin.pageId WHERE p.deletedAt IS NULL AND ${readable.sql}${filters}`;
|
|
5662
5832
|
let total;
|
|
5663
5833
|
if (input.count) total = Number((await this.#query(this.#read, `SELECT COUNT(*) AS n ${from}`, args))[0].n);
|
|
@@ -5718,7 +5888,8 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
|
|
|
5718
5888
|
contractVersion: 4,
|
|
5719
5889
|
schemaVersion: 4,
|
|
5720
5890
|
supportsSchemaInspection: true,
|
|
5721
|
-
supportsExplicitReset: true
|
|
5891
|
+
supportsExplicitReset: true,
|
|
5892
|
+
supportsPinKinds: true
|
|
5722
5893
|
};
|
|
5723
5894
|
}
|
|
5724
5895
|
async #inspectPages(client = this.#pool) {
|
|
@@ -5774,7 +5945,11 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
|
|
|
5774
5945
|
await this.#upgrade();
|
|
5775
5946
|
}
|
|
5776
5947
|
async dangerouslyReset() {
|
|
5777
|
-
for (const table of [
|
|
5948
|
+
for (const table of [
|
|
5949
|
+
...KNOWLEDGE_RESETTABLE_TABLE_NAMES,
|
|
5950
|
+
"mastra_knowledge_pin_audiences",
|
|
5951
|
+
"mastra_knowledge_page_links"
|
|
5952
|
+
]) await this.#pool.query(`DROP TABLE IF EXISTS \`${table}\``);
|
|
5778
5953
|
await this.init();
|
|
5779
5954
|
}
|
|
5780
5955
|
materializeScope(input) {
|
|
@@ -5792,6 +5967,9 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
|
|
|
5792
5967
|
listChildScopes(input) {
|
|
5793
5968
|
return this.#pages.listChildScopes(input);
|
|
5794
5969
|
}
|
|
5970
|
+
countChildScopes(input) {
|
|
5971
|
+
return this.#pages.countChildScopes(input);
|
|
5972
|
+
}
|
|
5795
5973
|
getPage(input) {
|
|
5796
5974
|
return this.#pages.getPage(input);
|
|
5797
5975
|
}
|
|
@@ -5831,6 +6009,18 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
|
|
|
5831
6009
|
countChildPages(input) {
|
|
5832
6010
|
return this.#pages.countChildPages(input);
|
|
5833
6011
|
}
|
|
6012
|
+
replacePageBodyLinks(input) {
|
|
6013
|
+
return this.#pages.replacePageBodyLinks(input);
|
|
6014
|
+
}
|
|
6015
|
+
listPageBodyLinkSources(input) {
|
|
6016
|
+
return this.#pages.listPageBodyLinkSources(input);
|
|
6017
|
+
}
|
|
6018
|
+
countPageLinks(input) {
|
|
6019
|
+
return this.#pages.countPageLinks(input);
|
|
6020
|
+
}
|
|
6021
|
+
listPageLinks(input) {
|
|
6022
|
+
return this.#pages.listPageLinks(input);
|
|
6023
|
+
}
|
|
5834
6024
|
countNotes(input) {
|
|
5835
6025
|
return this.#pages.countNotes(input);
|
|
5836
6026
|
}
|