@mastra/mysql 0.16.0-alpha.1 → 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 → @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 +266 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +266 -34
- 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 +8 -1
- package/dist/storage/domains/knowledge/index.d.ts.map +1 -1
- package/dist/storage/domains/knowledge/pages.d.ts +34 -11
- package/dist/storage/domains/knowledge/pages.d.ts.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +1 -1
- package/package.json +5 -5
- /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/index.js
CHANGED
|
@@ -851,6 +851,7 @@ var AgentsMySQL = class AgentsMySQL extends AgentsStorage {
|
|
|
851
851
|
tableName: TABLE_AGENT_VERSIONS,
|
|
852
852
|
schema: AGENT_VERSIONS_SCHEMA,
|
|
853
853
|
ifNotExists: [
|
|
854
|
+
"modelList",
|
|
854
855
|
"mcpClients",
|
|
855
856
|
"requestContextSchema",
|
|
856
857
|
"workspace",
|
|
@@ -1132,8 +1133,9 @@ var AgentsMySQL = class AgentsMySQL extends AgentsStorage {
|
|
|
1132
1133
|
versionNumber: input.versionNumber,
|
|
1133
1134
|
name: input.name ?? null,
|
|
1134
1135
|
description: input.description ?? null,
|
|
1135
|
-
instructions: this.serializeInstructions(input.instructions),
|
|
1136
|
-
model: input.model,
|
|
1136
|
+
instructions: this.serializeInstructions(input.instructions ?? ""),
|
|
1137
|
+
model: input.model ?? {},
|
|
1138
|
+
modelList: input.modelList ?? null,
|
|
1137
1139
|
tools: input.tools ?? null,
|
|
1138
1140
|
defaultOptions: input.defaultOptions ?? null,
|
|
1139
1141
|
workflows: input.workflows ?? null,
|
|
@@ -1350,6 +1352,7 @@ var AgentsMySQL = class AgentsMySQL extends AgentsStorage {
|
|
|
1350
1352
|
description: row.description ?? void 0,
|
|
1351
1353
|
instructions: this.deserializeInstructions(row.instructions),
|
|
1352
1354
|
model: this.safeParseJSON(row.model),
|
|
1355
|
+
modelList: this.safeParseJSON(row.modelList),
|
|
1353
1356
|
tools: this.safeParseJSON(row.tools),
|
|
1354
1357
|
defaultOptions: this.safeParseJSON(row.defaultOptions),
|
|
1355
1358
|
workflows: this.safeParseJSON(row.workflows),
|
|
@@ -4297,7 +4300,8 @@ const KNOWLEDGE_PAGES_CORE_FUNCTIONS = [
|
|
|
4297
4300
|
const KNOWLEDGE_PAGES_CORE_NUMBERS = [
|
|
4298
4301
|
"MAX_KNOWLEDGE_MOVE_PAGES",
|
|
4299
4302
|
"MAX_KNOWLEDGE_PAGE_DEPTH",
|
|
4300
|
-
"MAX_KNOWLEDGE_PAGE_LIST_LIMIT"
|
|
4303
|
+
"MAX_KNOWLEDGE_PAGE_LIST_LIMIT",
|
|
4304
|
+
"MAX_KNOWLEDGE_PAGE_LINKS"
|
|
4301
4305
|
];
|
|
4302
4306
|
function resolveKnowledgePagesCore(storageModule) {
|
|
4303
4307
|
const module = typeof storageModule === "object" && storageModule !== null ? storageModule : {};
|
|
@@ -4318,13 +4322,25 @@ function createKnowledgePagesCoreLoader(features, loadStorageModule) {
|
|
|
4318
4322
|
};
|
|
4319
4323
|
}
|
|
4320
4324
|
/**
|
|
4321
|
-
* Keyword-search words: the query split on whitespace, lowercased, deduplicated
|
|
4322
|
-
*
|
|
4325
|
+
* Keyword-search words: the query split on whitespace, lowercased, deduplicated, keeping the first
|
|
4326
|
+
* {@link KNOWLEDGE_SEARCH_MAX_WORDS}. A hit needs at least one of them (as a case-insensitive
|
|
4327
|
+
* substring); an empty list means the search returns nothing.
|
|
4323
4328
|
*/
|
|
4324
4329
|
function knowledgeSearchWords(query) {
|
|
4325
|
-
return [...new Set(query.toLowerCase().split(/\s+/).filter(Boolean))];
|
|
4330
|
+
return [...new Set(query.toLowerCase().split(/\s+/).filter(Boolean))].slice(0, 16);
|
|
4326
4331
|
}
|
|
4327
4332
|
/**
|
|
4333
|
+
* Points one matched search word earns on a page, by the best field it appears in: name, then
|
|
4334
|
+
* description, then body. A page whose name contains the whole query ranks first; below that, pages
|
|
4335
|
+
* rank by how many query words they match, and these points only break ties between pages that
|
|
4336
|
+
* match the same number of words.
|
|
4337
|
+
*/
|
|
4338
|
+
const KNOWLEDGE_SEARCH_WEIGHTS = {
|
|
4339
|
+
name: 3,
|
|
4340
|
+
description: 2,
|
|
4341
|
+
body: 1
|
|
4342
|
+
};
|
|
4343
|
+
/**
|
|
4328
4344
|
* Why `key` can't be a Knowledge page key, or undefined when it can. Keys are one path segment:
|
|
4329
4345
|
* non-empty, with no `/` (path separator), `#` (scope/path separator) or `%` (escape character).
|
|
4330
4346
|
*/
|
|
@@ -4343,7 +4359,8 @@ const T = {
|
|
|
4343
4359
|
mentions: "`mastra_knowledge_mentions`",
|
|
4344
4360
|
pins: "`mastra_knowledge_pins`",
|
|
4345
4361
|
pinAudiences: "`mastra_knowledge_pin_audiences`",
|
|
4346
|
-
activity: "`mastra_knowledge_activity`"
|
|
4362
|
+
activity: "`mastra_knowledge_activity`",
|
|
4363
|
+
links: "`mastra_knowledge_page_links`"
|
|
4347
4364
|
};
|
|
4348
4365
|
const KEY = "`key`";
|
|
4349
4366
|
const WHEN = "`when`";
|
|
@@ -4354,6 +4371,11 @@ const LIVE_KEY_COLUMN = `VARCHAR(768) ${BIN} AS (IF(deletedAt IS NULL AND \`key\
|
|
|
4354
4371
|
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 = ?)`;
|
|
4355
4372
|
/** Upgrades for Knowledge databases created by an earlier alpha build; `needed` returns a row when `apply` must run. */
|
|
4356
4373
|
const KNOWLEDGE_PAGE_UPGRADES = [
|
|
4374
|
+
{
|
|
4375
|
+
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')`,
|
|
4376
|
+
args: [],
|
|
4377
|
+
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)"]
|
|
4378
|
+
},
|
|
4357
4379
|
{
|
|
4358
4380
|
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%'`,
|
|
4359
4381
|
args: [],
|
|
@@ -4446,17 +4468,27 @@ const KNOWLEDGE_PAGE_DDL = [
|
|
|
4446
4468
|
`CREATE TABLE IF NOT EXISTS ${T.pins} (
|
|
4447
4469
|
id VARCHAR(255) ${BIN} NOT NULL PRIMARY KEY,
|
|
4448
4470
|
pageId VARCHAR(255) ${BIN} NOT NULL,
|
|
4471
|
+
kind VARCHAR(16) NOT NULL DEFAULT 'page',
|
|
4472
|
+
depth INT NULL,
|
|
4449
4473
|
audience JSON NOT NULL,
|
|
4450
4474
|
createdBy VARCHAR(255) NOT NULL,
|
|
4451
4475
|
createdAt VARCHAR(32) NOT NULL,
|
|
4452
4476
|
updatedAt VARCHAR(32) NOT NULL,
|
|
4453
|
-
UNIQUE KEY
|
|
4477
|
+
UNIQUE KEY idx_kpins_page_kind (pageId, kind)
|
|
4454
4478
|
)`,
|
|
4455
4479
|
`CREATE TABLE IF NOT EXISTS ${T.pinAudiences} (
|
|
4456
4480
|
audience VARCHAR(512) ${BIN} NOT NULL,
|
|
4457
4481
|
pinId VARCHAR(255) ${BIN} NOT NULL,
|
|
4458
4482
|
PRIMARY KEY (audience, pinId),
|
|
4459
4483
|
KEY idx_kpin_audiences_pin (pinId)
|
|
4484
|
+
)`,
|
|
4485
|
+
`CREATE TABLE IF NOT EXISTS ${T.links} (
|
|
4486
|
+
fromPageId VARCHAR(255) ${BIN} NOT NULL,
|
|
4487
|
+
ref VARCHAR(512) ${BIN} NOT NULL,
|
|
4488
|
+
toPageId VARCHAR(255) ${BIN} NULL,
|
|
4489
|
+
linkCount INT NOT NULL,
|
|
4490
|
+
PRIMARY KEY (fromPageId, ref),
|
|
4491
|
+
KEY idx_klinks_to_ref (toPageId, ref)
|
|
4460
4492
|
)`,
|
|
4461
4493
|
`CREATE TABLE IF NOT EXISTS ${T.activity} (
|
|
4462
4494
|
id VARCHAR(255) ${BIN} NOT NULL PRIMARY KEY,
|
|
@@ -4522,6 +4554,8 @@ function toPin(row) {
|
|
|
4522
4554
|
return {
|
|
4523
4555
|
id: String(row.id),
|
|
4524
4556
|
pageId: String(row.pageId),
|
|
4557
|
+
kind: String(row.kind),
|
|
4558
|
+
...row.depth == null ? {} : { depth: Number(row.depth) },
|
|
4525
4559
|
audience: parseJsonList(row.audience),
|
|
4526
4560
|
createdBy: String(row.createdBy),
|
|
4527
4561
|
createdAt: new Date(String(row.createdAt)),
|
|
@@ -4555,6 +4589,13 @@ function isRetryableRace(error) {
|
|
|
4555
4589
|
}
|
|
4556
4590
|
return false;
|
|
4557
4591
|
}
|
|
4592
|
+
function isLiveKeyClash(error) {
|
|
4593
|
+
for (let current = error; current && typeof current === "object"; current = current.cause) {
|
|
4594
|
+
const { code, errno, message } = current;
|
|
4595
|
+
if ((code === "ER_DUP_ENTRY" || errno === 1062) && String(message).includes("idx_kpages_live_key")) return true;
|
|
4596
|
+
}
|
|
4597
|
+
return false;
|
|
4598
|
+
}
|
|
4558
4599
|
function escapeLike(value) {
|
|
4559
4600
|
return value.replace(/[=%_]/g, (match) => `=${match}`);
|
|
4560
4601
|
}
|
|
@@ -4686,6 +4727,7 @@ var MySQLKnowledgePages = class {
|
|
|
4686
4727
|
T.mentions,
|
|
4687
4728
|
T.stamps,
|
|
4688
4729
|
T.notes,
|
|
4730
|
+
T.links,
|
|
4689
4731
|
T.pages
|
|
4690
4732
|
]) await tx.execute({
|
|
4691
4733
|
sql: `DELETE FROM ${table}`,
|
|
@@ -4868,13 +4910,19 @@ var MySQLKnowledgePages = class {
|
|
|
4868
4910
|
const limit = limitOf(input.limit, core.MAX_KNOWLEDGE_PAGE_LIST_LIMIT);
|
|
4869
4911
|
const cursor = decodeCursor(input.cursor, 2);
|
|
4870
4912
|
const args = [input.parentId];
|
|
4913
|
+
let filter = "";
|
|
4914
|
+
if (input.read) {
|
|
4915
|
+
const readable = pageReadable("p", input.read);
|
|
4916
|
+
filter = ` AND ${readable.sql}`;
|
|
4917
|
+
args.push(...readable.args);
|
|
4918
|
+
}
|
|
4871
4919
|
let after = "";
|
|
4872
4920
|
if (cursor) {
|
|
4873
|
-
after = ` AND (position > ? OR (position = ? AND id > ?))`;
|
|
4921
|
+
after = ` AND (p.position > ? OR (p.position = ? AND p.id > ?))`;
|
|
4874
4922
|
args.push(cursor[0], cursor[0], cursor[1]);
|
|
4875
4923
|
}
|
|
4876
4924
|
args.push(limit + 1);
|
|
4877
|
-
const rows = await this.#query(this.#read, `SELECT
|
|
4925
|
+
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);
|
|
4878
4926
|
return this.#pageOutput(rows, limit, (row) => [String(row.position), String(row.id)]);
|
|
4879
4927
|
}
|
|
4880
4928
|
#pageOutput(rows, limit, cursorOf) {
|
|
@@ -5008,7 +5056,12 @@ var MySQLKnowledgePages = class {
|
|
|
5008
5056
|
}
|
|
5009
5057
|
async createPage(input) {
|
|
5010
5058
|
await assertKnowledgePageDescriptionWithinBound(input.description);
|
|
5011
|
-
|
|
5059
|
+
try {
|
|
5060
|
+
return await this.#transaction((tx) => this.#insertPage(tx, input));
|
|
5061
|
+
} catch (error) {
|
|
5062
|
+
if (input.key !== void 0 && isLiveKeyClash(error)) throw new (await (loadPagesCore())).KnowledgePageKeyConflictError(input.key, input.parentId);
|
|
5063
|
+
throw error;
|
|
5064
|
+
}
|
|
5012
5065
|
}
|
|
5013
5066
|
async updatePage(input) {
|
|
5014
5067
|
const core = await loadPagesCore();
|
|
@@ -5159,9 +5212,8 @@ var MySQLKnowledgePages = class {
|
|
|
5159
5212
|
args: [input.sourceId]
|
|
5160
5213
|
});
|
|
5161
5214
|
const pins = await this.#query(tx, `SELECT * FROM ${T.pins} WHERE pageId IN (?, ?)`, [input.sourceId, input.targetId]);
|
|
5162
|
-
const sourcePin
|
|
5163
|
-
|
|
5164
|
-
const targetPin = pins.find((pin) => String(pin.pageId) === input.targetId);
|
|
5215
|
+
for (const sourcePin of pins.filter((pin) => String(pin.pageId) === input.sourceId)) {
|
|
5216
|
+
const targetPin = pins.find((pin) => String(pin.pageId) === input.targetId && pin.kind === sourcePin.kind);
|
|
5165
5217
|
const audience = parseJsonList(sourcePin.audience);
|
|
5166
5218
|
if (targetPin) {
|
|
5167
5219
|
const merged = [.../* @__PURE__ */ new Set([...parseJsonList(targetPin.audience), ...audience])];
|
|
@@ -5261,6 +5313,148 @@ var MySQLKnowledgePages = class {
|
|
|
5261
5313
|
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);
|
|
5262
5314
|
return this.#noteOutput(rows, limit);
|
|
5263
5315
|
}
|
|
5316
|
+
async countChildScopes(input) {
|
|
5317
|
+
const ids = [...new Set(input.parentIds)];
|
|
5318
|
+
const counts = Object.fromEntries(ids.map((id) => [id, 0]));
|
|
5319
|
+
if (ids.length === 0) return counts;
|
|
5320
|
+
const readable = pageReadable("p", input.read);
|
|
5321
|
+
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]);
|
|
5322
|
+
for (const row of rows) counts[String(row.parentId)] = Number(row.total);
|
|
5323
|
+
return counts;
|
|
5324
|
+
}
|
|
5325
|
+
async countChildPages(input) {
|
|
5326
|
+
const ids = [...new Set(input.parentIds)];
|
|
5327
|
+
const counts = Object.fromEntries(ids.map((id) => [id, 0]));
|
|
5328
|
+
if (ids.length === 0) return counts;
|
|
5329
|
+
const readable = pageReadable("p", input.read);
|
|
5330
|
+
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 = 0 AND p.deletedAt IS NULL AND ${readable.sql} GROUP BY p.parentId`, [jsonList(ids), ...readable.args]);
|
|
5331
|
+
for (const row of rows) counts[String(row.parentId)] = Number(row.total);
|
|
5332
|
+
return counts;
|
|
5333
|
+
}
|
|
5334
|
+
async replacePageBodyLinks(input) {
|
|
5335
|
+
await this.#transaction(async (tx) => {
|
|
5336
|
+
await tx.execute({
|
|
5337
|
+
sql: `DELETE FROM ${T.links} WHERE fromPageId = ?`,
|
|
5338
|
+
args: [input.pageId]
|
|
5339
|
+
});
|
|
5340
|
+
if (input.links.length === 0) return;
|
|
5341
|
+
await tx.execute({
|
|
5342
|
+
sql: `INSERT INTO ${T.links} (fromPageId, ref, toPageId, linkCount) VALUES ${input.links.map(() => "(?, ?, ?, ?)").join(", ")}`,
|
|
5343
|
+
args: input.links.flatMap((link) => [
|
|
5344
|
+
input.pageId,
|
|
5345
|
+
link.ref,
|
|
5346
|
+
link.toPageId,
|
|
5347
|
+
link.count
|
|
5348
|
+
])
|
|
5349
|
+
});
|
|
5350
|
+
});
|
|
5351
|
+
}
|
|
5352
|
+
async listPageBodyLinkSources(input) {
|
|
5353
|
+
const refs = [...new Set(input.refs ?? [])];
|
|
5354
|
+
const targets = [...new Set(input.toPageIds ?? [])];
|
|
5355
|
+
const parts = [];
|
|
5356
|
+
const args = [];
|
|
5357
|
+
if (refs.length) {
|
|
5358
|
+
parts.push(`(toPageId IS NULL AND ref IN ${JV})`);
|
|
5359
|
+
args.push(jsonList(refs));
|
|
5360
|
+
}
|
|
5361
|
+
if (targets.length) {
|
|
5362
|
+
parts.push(`toPageId IN ${JV}`);
|
|
5363
|
+
args.push(jsonList(targets));
|
|
5364
|
+
}
|
|
5365
|
+
if (!parts.length) return [];
|
|
5366
|
+
return (await this.#query(this.#read, `SELECT DISTINCT fromPageId FROM ${T.links} WHERE ${parts.join(" OR ")}`, args)).map((row) => String(row.fromPageId));
|
|
5367
|
+
}
|
|
5368
|
+
/**
|
|
5369
|
+
* Every resolved link touching `ids` whose both ends are live and readable, one row per
|
|
5370
|
+
* (from, to, kind); no self-links. Note links go through the same visible-note rule as listNotes.
|
|
5371
|
+
*/
|
|
5372
|
+
#visibleLinkEdges(ids, read, direction, kinds) {
|
|
5373
|
+
const wanted = new Set(kinds ?? ["body", "note"]);
|
|
5374
|
+
const idList = jsonList(ids);
|
|
5375
|
+
const touch = (from, to) => direction === "out" ? {
|
|
5376
|
+
sql: `${from} IN ${JV}`,
|
|
5377
|
+
args: [idList]
|
|
5378
|
+
} : direction === "in" ? {
|
|
5379
|
+
sql: `${to} IN ${JV}`,
|
|
5380
|
+
args: [idList]
|
|
5381
|
+
} : {
|
|
5382
|
+
sql: `(${from} IN ${JV} OR ${to} IN ${JV})`,
|
|
5383
|
+
args: [idList, idList]
|
|
5384
|
+
};
|
|
5385
|
+
const branches = [];
|
|
5386
|
+
const args = [];
|
|
5387
|
+
if (wanted.has("body")) {
|
|
5388
|
+
const fromReadable = pageReadable("fp", read);
|
|
5389
|
+
const toReadable = pageReadable("tp", read);
|
|
5390
|
+
const t = touch("l.fromPageId", "l.toPageId");
|
|
5391
|
+
branches.push(`SELECT l.fromPageId AS fromPageId, l.toPageId AS toPageId, 'body' AS kind, CAST(SUM(l.linkCount) AS SIGNED) AS cnt
|
|
5392
|
+
FROM ${T.links} l
|
|
5393
|
+
JOIN ${T.pages} fp ON fp.id = l.fromPageId AND fp.deletedAt IS NULL AND ${fromReadable.sql}
|
|
5394
|
+
JOIN ${T.pages} tp ON tp.id = l.toPageId AND tp.deletedAt IS NULL AND ${toReadable.sql}
|
|
5395
|
+
WHERE l.toPageId IS NOT NULL AND l.fromPageId <> l.toPageId AND ${t.sql}
|
|
5396
|
+
GROUP BY l.fromPageId, l.toPageId`);
|
|
5397
|
+
args.push(...fromReadable.args, ...toReadable.args, ...t.args);
|
|
5398
|
+
}
|
|
5399
|
+
if (wanted.has("note")) {
|
|
5400
|
+
const visible = noteReadable("n", read);
|
|
5401
|
+
const toReadable = pageReadable("tp", read);
|
|
5402
|
+
const t = touch("vn.pageId", "mn.pageId");
|
|
5403
|
+
branches.push(`SELECT vn.pageId AS fromPageId, mn.pageId AS toPageId, 'note' AS kind, CAST(COUNT(*) AS SIGNED) AS cnt
|
|
5404
|
+
FROM (SELECT n.id AS id, n.pageId AS pageId FROM ${T.notes} n
|
|
5405
|
+
JOIN ${T.pages} np ON np.id = n.pageId AND np.deletedAt IS NULL
|
|
5406
|
+
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}))
|
|
5407
|
+
AND ${visible.sql}) vn
|
|
5408
|
+
JOIN ${T.mentions} mn ON mn.noteId = vn.id
|
|
5409
|
+
JOIN ${T.pages} tp ON tp.id = mn.pageId AND tp.deletedAt IS NULL AND ${toReadable.sql}
|
|
5410
|
+
WHERE mn.pageId <> vn.pageId AND ${t.sql}
|
|
5411
|
+
GROUP BY vn.pageId, mn.pageId`);
|
|
5412
|
+
args.push(idList, idList, ...visible.args, ...toReadable.args, ...t.args);
|
|
5413
|
+
}
|
|
5414
|
+
return {
|
|
5415
|
+
sql: branches.length ? branches.join(" UNION ALL ") : `SELECT NULL AS fromPageId, NULL AS toPageId, NULL AS kind, NULL AS cnt FROM DUAL WHERE FALSE`,
|
|
5416
|
+
args
|
|
5417
|
+
};
|
|
5418
|
+
}
|
|
5419
|
+
async countPageLinks(input) {
|
|
5420
|
+
const ids = [...new Set(input.pageIds)];
|
|
5421
|
+
const counts = Object.fromEntries(ids.map((id) => [id, {
|
|
5422
|
+
in: 0,
|
|
5423
|
+
out: 0
|
|
5424
|
+
}]));
|
|
5425
|
+
if (ids.length === 0) return counts;
|
|
5426
|
+
const edges = this.#visibleLinkEdges(ids, input.read, "both", input.kinds);
|
|
5427
|
+
const idList = jsonList(ids);
|
|
5428
|
+
const rows = await this.#query(this.#read, `WITH e AS (${edges.sql})
|
|
5429
|
+
SELECT r.id AS id, CAST(SUM(r.o) AS SIGNED) AS outCount, CAST(SUM(r.i) AS SIGNED) AS inCount FROM (
|
|
5430
|
+
SELECT fromPageId AS id, COUNT(DISTINCT toPageId) AS o, 0 AS i FROM e WHERE fromPageId IN ${JV} GROUP BY fromPageId
|
|
5431
|
+
UNION ALL
|
|
5432
|
+
SELECT toPageId AS id, 0 AS o, COUNT(DISTINCT fromPageId) AS i FROM e WHERE toPageId IN ${JV} GROUP BY toPageId
|
|
5433
|
+
) r GROUP BY r.id`, [
|
|
5434
|
+
...edges.args,
|
|
5435
|
+
idList,
|
|
5436
|
+
idList
|
|
5437
|
+
]);
|
|
5438
|
+
for (const row of rows) counts[String(row.id)] = {
|
|
5439
|
+
in: Number(row.inCount),
|
|
5440
|
+
out: Number(row.outCount)
|
|
5441
|
+
};
|
|
5442
|
+
return counts;
|
|
5443
|
+
}
|
|
5444
|
+
async listPageLinks(input) {
|
|
5445
|
+
const limit = input.limit ?? 1e3;
|
|
5446
|
+
if (!Number.isInteger(limit) || limit < 1) throw new RangeError("Knowledge link limit must be a positive integer");
|
|
5447
|
+
const core = await loadPagesCore();
|
|
5448
|
+
const ids = [...new Set(input.pageIds)];
|
|
5449
|
+
if (ids.length === 0) return [];
|
|
5450
|
+
const edges = this.#visibleLinkEdges(ids, input.read, input.direction ?? "both", input.kinds);
|
|
5451
|
+
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) => ({
|
|
5452
|
+
fromPageId: String(row.fromPageId),
|
|
5453
|
+
toPageId: String(row.toPageId),
|
|
5454
|
+
kind: String(row.kind),
|
|
5455
|
+
count: Number(row.cnt)
|
|
5456
|
+
}));
|
|
5457
|
+
}
|
|
5264
5458
|
async countNotes(input) {
|
|
5265
5459
|
const ids = [...new Set(input.pageIds)];
|
|
5266
5460
|
const counts = Object.fromEntries(ids.map((id) => [id, 0]));
|
|
@@ -5432,18 +5626,22 @@ var MySQLKnowledgePages = class {
|
|
|
5432
5626
|
const readable = pagesInReadableScopes("p", input.read);
|
|
5433
5627
|
const scopeFilter = input.scopeIds ? ` AND p.scopeId IN ${JV}` : "";
|
|
5434
5628
|
const scopeArgs = input.scopeIds ? [jsonList(input.scopeIds)] : [];
|
|
5435
|
-
const
|
|
5436
|
-
const
|
|
5437
|
-
const
|
|
5629
|
+
const field = (column) => `LOWER(COALESCE(${column}, '')) LIKE ? ESCAPE '='`;
|
|
5630
|
+
const pageMatch = words.map(() => `(${field("p.name")} OR ${field("p.description")} OR ${field("p.body")})`).join(" OR ");
|
|
5631
|
+
const pageScore = words.map(() => `(CASE WHEN ${field("p.name")} THEN ${KNOWLEDGE_SEARCH_WEIGHTS.name} WHEN ${field("p.description")} THEN ${KNOWLEDGE_SEARCH_WEIGHTS.description} WHEN ${field("p.body")} THEN ${KNOWLEDGE_SEARCH_WEIGHTS.body} ELSE 0 END)`).join(" + ");
|
|
5632
|
+
const pageCount = words.map(() => `(CASE WHEN ${field("p.name")} OR ${field("p.description")} OR ${field("p.body")} THEN 1 ELSE 0 END)`).join(" + ");
|
|
5633
|
+
const triple = likes.flatMap((like) => [
|
|
5634
|
+
like,
|
|
5635
|
+
like,
|
|
5636
|
+
like
|
|
5637
|
+
]);
|
|
5638
|
+
const hits = (await this.#query(this.#read, `SELECT p.* ${readable.sql} WHERE p.deletedAt IS NULL AND p.isScope = 0${scopeFilter} AND (${pageMatch}) ORDER BY CASE WHEN LOCATE(?, p.canonicalName) > 0 THEN 0 ELSE 1 END, (${pageCount}) DESC, (${pageScore}) DESC, p.updatedAt DESC, p.id ASC LIMIT ?`, [
|
|
5438
5639
|
...readable.args,
|
|
5439
5640
|
...scopeArgs,
|
|
5440
|
-
...
|
|
5441
|
-
like,
|
|
5442
|
-
like,
|
|
5443
|
-
like
|
|
5444
|
-
]),
|
|
5641
|
+
...triple,
|
|
5445
5642
|
phrase,
|
|
5446
|
-
...
|
|
5643
|
+
...triple,
|
|
5644
|
+
...triple,
|
|
5447
5645
|
limit
|
|
5448
5646
|
])).map((row) => ({
|
|
5449
5647
|
type: "page",
|
|
@@ -5451,11 +5649,12 @@ var MySQLKnowledgePages = class {
|
|
|
5451
5649
|
}));
|
|
5452
5650
|
if (hits.length >= limit) return hits;
|
|
5453
5651
|
const noteVisible = noteReadable("n", input.read);
|
|
5454
|
-
const
|
|
5455
|
-
const noteRows = await this.#query(this.#read, `SELECT n.* FROM ${T.notes} n JOIN ${T.pages} p ON p.id = n.pageId WHERE n.deletedAt IS NULL AND p.deletedAt IS NULL${scopeFilter} AND ${
|
|
5652
|
+
const noteHas = words.map(() => `LOWER(n.text) LIKE ? ESCAPE '='`);
|
|
5653
|
+
const noteRows = await this.#query(this.#read, `SELECT n.* FROM ${T.notes} n JOIN ${T.pages} p ON p.id = n.pageId WHERE n.deletedAt IS NULL AND p.deletedAt IS NULL${scopeFilter} AND (${noteHas.join(" OR ")}) AND ${noteVisible.sql} ORDER BY (${noteHas.map((has) => `(CASE WHEN ${has} THEN 1 ELSE 0 END)`).join(" + ")}) DESC, n.updatedAt DESC, n.id DESC LIMIT ?`, [
|
|
5456
5654
|
...scopeArgs,
|
|
5457
5655
|
...likes,
|
|
5458
5656
|
...noteVisible.args,
|
|
5657
|
+
...likes,
|
|
5459
5658
|
limit - hits.length
|
|
5460
5659
|
]);
|
|
5461
5660
|
const notes = await this.#hydrateNotes(this.#read, noteRows);
|
|
@@ -5517,17 +5716,21 @@ var MySQLKnowledgePages = class {
|
|
|
5517
5716
|
});
|
|
5518
5717
|
}
|
|
5519
5718
|
addPinAudience(input) {
|
|
5719
|
+
const kind = input.kind ?? "page";
|
|
5720
|
+
const depth = kind === "list" ? input.depth ?? 1 : null;
|
|
5520
5721
|
return this.#transaction(async (tx) => {
|
|
5521
5722
|
const page = await this.#livePage(tx, input.pageId);
|
|
5522
5723
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5523
|
-
const existing = (await this.#query(tx, `SELECT * FROM ${T.pins} WHERE pageId = ?`, [input.pageId]))[0];
|
|
5724
|
+
const existing = (await this.#query(tx, `SELECT * FROM ${T.pins} WHERE pageId = ? AND kind = ?`, [input.pageId, kind]))[0];
|
|
5524
5725
|
if (!existing) {
|
|
5525
5726
|
const id = createKnowledgeUlid();
|
|
5526
5727
|
await tx.execute({
|
|
5527
|
-
sql: `INSERT INTO ${T.pins} (id, pageId, audience, createdBy, createdAt, updatedAt) VALUES (?, ?, ?, ?, ?, ?)`,
|
|
5728
|
+
sql: `INSERT INTO ${T.pins} (id, pageId, kind, depth, audience, createdBy, createdAt, updatedAt) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
5528
5729
|
args: [
|
|
5529
5730
|
id,
|
|
5530
5731
|
input.pageId,
|
|
5732
|
+
kind,
|
|
5733
|
+
depth,
|
|
5531
5734
|
JSON.stringify([...new Set(input.audience)]),
|
|
5532
5735
|
input.createdBy,
|
|
5533
5736
|
now,
|
|
@@ -5544,11 +5747,13 @@ var MySQLKnowledgePages = class {
|
|
|
5544
5747
|
}
|
|
5545
5748
|
const current = parseJsonList(existing.audience);
|
|
5546
5749
|
const merged = [.../* @__PURE__ */ new Set([...current, ...input.audience])];
|
|
5547
|
-
|
|
5750
|
+
const depthChanged = (existing.depth == null ? null : Number(existing.depth)) !== depth;
|
|
5751
|
+
if (merged.length !== current.length || depthChanged) {
|
|
5548
5752
|
await tx.execute({
|
|
5549
|
-
sql: `UPDATE ${T.pins} SET audience = ?, updatedAt = ? WHERE id = ?`,
|
|
5753
|
+
sql: `UPDATE ${T.pins} SET audience = ?, depth = ?, updatedAt = ? WHERE id = ?`,
|
|
5550
5754
|
args: [
|
|
5551
5755
|
JSON.stringify(merged),
|
|
5756
|
+
depth,
|
|
5552
5757
|
now,
|
|
5553
5758
|
String(existing.id)
|
|
5554
5759
|
]
|
|
@@ -5565,7 +5770,7 @@ var MySQLKnowledgePages = class {
|
|
|
5565
5770
|
}
|
|
5566
5771
|
removePinAudience(input) {
|
|
5567
5772
|
return this.#transaction(async (tx) => {
|
|
5568
|
-
const existing = (await this.#query(tx, `SELECT * FROM ${T.pins} WHERE pageId = ?`, [input.pageId]))[0];
|
|
5773
|
+
const existing = (await this.#query(tx, `SELECT * FROM ${T.pins} WHERE pageId = ? AND kind = ?`, [input.pageId, input.kind ?? "page"]))[0];
|
|
5569
5774
|
if (!existing) return null;
|
|
5570
5775
|
const current = parseJsonList(existing.audience);
|
|
5571
5776
|
const remove = new Set(input.audience);
|
|
@@ -5609,15 +5814,19 @@ var MySQLKnowledgePages = class {
|
|
|
5609
5814
|
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)`;
|
|
5610
5815
|
args.push(jsonList(input.audience));
|
|
5611
5816
|
}
|
|
5817
|
+
if (input.kind) {
|
|
5818
|
+
filters += ` AND pin.kind = ?`;
|
|
5819
|
+
args.push(input.kind);
|
|
5820
|
+
}
|
|
5612
5821
|
if (input.pageIds) {
|
|
5613
5822
|
filters += ` AND pin.pageId IN ${JV}`;
|
|
5614
5823
|
args.push(jsonList(input.pageIds));
|
|
5615
5824
|
}
|
|
5616
5825
|
if (input.excludePageIds?.length) {
|
|
5617
|
-
filters += ` AND pin.pageId NOT IN ${JV}`;
|
|
5826
|
+
filters += ` AND (pin.kind <> 'page' OR pin.pageId NOT IN ${JV})`;
|
|
5618
5827
|
args.push(jsonList(input.excludePageIds));
|
|
5619
5828
|
}
|
|
5620
|
-
if (input.withBody) filters += ` AND REGEXP_LIKE(p.body, '[^[:space:]]')`;
|
|
5829
|
+
if (input.withBody) filters += ` AND (pin.kind <> 'page' OR REGEXP_LIKE(p.body, '[^[:space:]]'))`;
|
|
5621
5830
|
const from = `FROM ${T.pins} pin JOIN ${T.pages} p ON p.id = pin.pageId WHERE p.deletedAt IS NULL AND ${readable.sql}${filters}`;
|
|
5622
5831
|
let total;
|
|
5623
5832
|
if (input.count) total = Number((await this.#query(this.#read, `SELECT COUNT(*) AS n ${from}`, args))[0].n);
|
|
@@ -5678,7 +5887,8 @@ var KnowledgeMySQL = class extends KnowledgeStorage {
|
|
|
5678
5887
|
contractVersion: 4,
|
|
5679
5888
|
schemaVersion: 4,
|
|
5680
5889
|
supportsSchemaInspection: true,
|
|
5681
|
-
supportsExplicitReset: true
|
|
5890
|
+
supportsExplicitReset: true,
|
|
5891
|
+
supportsPinKinds: true
|
|
5682
5892
|
};
|
|
5683
5893
|
}
|
|
5684
5894
|
async #inspectPages(client = this.#pool) {
|
|
@@ -5734,7 +5944,11 @@ var KnowledgeMySQL = class extends KnowledgeStorage {
|
|
|
5734
5944
|
await this.#upgrade();
|
|
5735
5945
|
}
|
|
5736
5946
|
async dangerouslyReset() {
|
|
5737
|
-
for (const table of [
|
|
5947
|
+
for (const table of [
|
|
5948
|
+
...KNOWLEDGE_RESETTABLE_TABLE_NAMES,
|
|
5949
|
+
"mastra_knowledge_pin_audiences",
|
|
5950
|
+
"mastra_knowledge_page_links"
|
|
5951
|
+
]) await this.#pool.query(`DROP TABLE IF EXISTS \`${table}\``);
|
|
5738
5952
|
await this.init();
|
|
5739
5953
|
}
|
|
5740
5954
|
materializeScope(input) {
|
|
@@ -5752,6 +5966,9 @@ var KnowledgeMySQL = class extends KnowledgeStorage {
|
|
|
5752
5966
|
listChildScopes(input) {
|
|
5753
5967
|
return this.#pages.listChildScopes(input);
|
|
5754
5968
|
}
|
|
5969
|
+
countChildScopes(input) {
|
|
5970
|
+
return this.#pages.countChildScopes(input);
|
|
5971
|
+
}
|
|
5755
5972
|
getPage(input) {
|
|
5756
5973
|
return this.#pages.getPage(input);
|
|
5757
5974
|
}
|
|
@@ -5788,6 +6005,21 @@ var KnowledgeMySQL = class extends KnowledgeStorage {
|
|
|
5788
6005
|
listNotes(input) {
|
|
5789
6006
|
return this.#pages.listNotes(input);
|
|
5790
6007
|
}
|
|
6008
|
+
countChildPages(input) {
|
|
6009
|
+
return this.#pages.countChildPages(input);
|
|
6010
|
+
}
|
|
6011
|
+
replacePageBodyLinks(input) {
|
|
6012
|
+
return this.#pages.replacePageBodyLinks(input);
|
|
6013
|
+
}
|
|
6014
|
+
listPageBodyLinkSources(input) {
|
|
6015
|
+
return this.#pages.listPageBodyLinkSources(input);
|
|
6016
|
+
}
|
|
6017
|
+
countPageLinks(input) {
|
|
6018
|
+
return this.#pages.countPageLinks(input);
|
|
6019
|
+
}
|
|
6020
|
+
listPageLinks(input) {
|
|
6021
|
+
return this.#pages.listPageLinks(input);
|
|
6022
|
+
}
|
|
5791
6023
|
countNotes(input) {
|
|
5792
6024
|
return this.#pages.countNotes(input);
|
|
5793
6025
|
}
|