@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/_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
package/dist/index.cjs
CHANGED
|
@@ -852,6 +852,7 @@ var AgentsMySQL = class AgentsMySQL extends _mastra_core_storage.AgentsStorage {
|
|
|
852
852
|
tableName: _mastra_core_storage.TABLE_AGENT_VERSIONS,
|
|
853
853
|
schema: _mastra_core_storage.AGENT_VERSIONS_SCHEMA,
|
|
854
854
|
ifNotExists: [
|
|
855
|
+
"modelList",
|
|
855
856
|
"mcpClients",
|
|
856
857
|
"requestContextSchema",
|
|
857
858
|
"workspace",
|
|
@@ -1133,8 +1134,9 @@ var AgentsMySQL = class AgentsMySQL extends _mastra_core_storage.AgentsStorage {
|
|
|
1133
1134
|
versionNumber: input.versionNumber,
|
|
1134
1135
|
name: input.name ?? null,
|
|
1135
1136
|
description: input.description ?? null,
|
|
1136
|
-
instructions: this.serializeInstructions(input.instructions),
|
|
1137
|
-
model: input.model,
|
|
1137
|
+
instructions: this.serializeInstructions(input.instructions ?? ""),
|
|
1138
|
+
model: input.model ?? {},
|
|
1139
|
+
modelList: input.modelList ?? null,
|
|
1138
1140
|
tools: input.tools ?? null,
|
|
1139
1141
|
defaultOptions: input.defaultOptions ?? null,
|
|
1140
1142
|
workflows: input.workflows ?? null,
|
|
@@ -1351,6 +1353,7 @@ var AgentsMySQL = class AgentsMySQL extends _mastra_core_storage.AgentsStorage {
|
|
|
1351
1353
|
description: row.description ?? void 0,
|
|
1352
1354
|
instructions: this.deserializeInstructions(row.instructions),
|
|
1353
1355
|
model: this.safeParseJSON(row.model),
|
|
1356
|
+
modelList: this.safeParseJSON(row.modelList),
|
|
1354
1357
|
tools: this.safeParseJSON(row.tools),
|
|
1355
1358
|
defaultOptions: this.safeParseJSON(row.defaultOptions),
|
|
1356
1359
|
workflows: this.safeParseJSON(row.workflows),
|
|
@@ -4298,7 +4301,8 @@ const KNOWLEDGE_PAGES_CORE_FUNCTIONS = [
|
|
|
4298
4301
|
const KNOWLEDGE_PAGES_CORE_NUMBERS = [
|
|
4299
4302
|
"MAX_KNOWLEDGE_MOVE_PAGES",
|
|
4300
4303
|
"MAX_KNOWLEDGE_PAGE_DEPTH",
|
|
4301
|
-
"MAX_KNOWLEDGE_PAGE_LIST_LIMIT"
|
|
4304
|
+
"MAX_KNOWLEDGE_PAGE_LIST_LIMIT",
|
|
4305
|
+
"MAX_KNOWLEDGE_PAGE_LINKS"
|
|
4302
4306
|
];
|
|
4303
4307
|
function resolveKnowledgePagesCore(storageModule) {
|
|
4304
4308
|
const module = typeof storageModule === "object" && storageModule !== null ? storageModule : {};
|
|
@@ -4319,13 +4323,25 @@ function createKnowledgePagesCoreLoader(features, loadStorageModule) {
|
|
|
4319
4323
|
};
|
|
4320
4324
|
}
|
|
4321
4325
|
/**
|
|
4322
|
-
* Keyword-search words: the query split on whitespace, lowercased, deduplicated
|
|
4323
|
-
*
|
|
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.
|
|
4324
4329
|
*/
|
|
4325
4330
|
function knowledgeSearchWords(query) {
|
|
4326
|
-
return [...new Set(query.toLowerCase().split(/\s+/).filter(Boolean))];
|
|
4331
|
+
return [...new Set(query.toLowerCase().split(/\s+/).filter(Boolean))].slice(0, 16);
|
|
4327
4332
|
}
|
|
4328
4333
|
/**
|
|
4334
|
+
* Points one matched search word earns on a page, by the best field it appears in: name, then
|
|
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.
|
|
4338
|
+
*/
|
|
4339
|
+
const KNOWLEDGE_SEARCH_WEIGHTS = {
|
|
4340
|
+
name: 3,
|
|
4341
|
+
description: 2,
|
|
4342
|
+
body: 1
|
|
4343
|
+
};
|
|
4344
|
+
/**
|
|
4329
4345
|
* Why `key` can't be a Knowledge page key, or undefined when it can. Keys are one path segment:
|
|
4330
4346
|
* non-empty, with no `/` (path separator), `#` (scope/path separator) or `%` (escape character).
|
|
4331
4347
|
*/
|
|
@@ -4344,7 +4360,8 @@ const T = {
|
|
|
4344
4360
|
mentions: "`mastra_knowledge_mentions`",
|
|
4345
4361
|
pins: "`mastra_knowledge_pins`",
|
|
4346
4362
|
pinAudiences: "`mastra_knowledge_pin_audiences`",
|
|
4347
|
-
activity: "`mastra_knowledge_activity`"
|
|
4363
|
+
activity: "`mastra_knowledge_activity`",
|
|
4364
|
+
links: "`mastra_knowledge_page_links`"
|
|
4348
4365
|
};
|
|
4349
4366
|
const KEY = "`key`";
|
|
4350
4367
|
const WHEN = "`when`";
|
|
@@ -4355,6 +4372,11 @@ const LIVE_KEY_COLUMN = `VARCHAR(768) ${BIN} AS (IF(deletedAt IS NULL AND \`key\
|
|
|
4355
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 = ?)`;
|
|
4356
4373
|
/** Upgrades for Knowledge databases created by an earlier alpha build; `needed` returns a row when `apply` must run. */
|
|
4357
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
|
+
},
|
|
4358
4380
|
{
|
|
4359
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%'`,
|
|
4360
4382
|
args: [],
|
|
@@ -4447,17 +4469,27 @@ const KNOWLEDGE_PAGE_DDL = [
|
|
|
4447
4469
|
`CREATE TABLE IF NOT EXISTS ${T.pins} (
|
|
4448
4470
|
id VARCHAR(255) ${BIN} NOT NULL PRIMARY KEY,
|
|
4449
4471
|
pageId VARCHAR(255) ${BIN} NOT NULL,
|
|
4472
|
+
kind VARCHAR(16) NOT NULL DEFAULT 'page',
|
|
4473
|
+
depth INT NULL,
|
|
4450
4474
|
audience JSON NOT NULL,
|
|
4451
4475
|
createdBy VARCHAR(255) NOT NULL,
|
|
4452
4476
|
createdAt VARCHAR(32) NOT NULL,
|
|
4453
4477
|
updatedAt VARCHAR(32) NOT NULL,
|
|
4454
|
-
UNIQUE KEY
|
|
4478
|
+
UNIQUE KEY idx_kpins_page_kind (pageId, kind)
|
|
4455
4479
|
)`,
|
|
4456
4480
|
`CREATE TABLE IF NOT EXISTS ${T.pinAudiences} (
|
|
4457
4481
|
audience VARCHAR(512) ${BIN} NOT NULL,
|
|
4458
4482
|
pinId VARCHAR(255) ${BIN} NOT NULL,
|
|
4459
4483
|
PRIMARY KEY (audience, pinId),
|
|
4460
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)
|
|
4461
4493
|
)`,
|
|
4462
4494
|
`CREATE TABLE IF NOT EXISTS ${T.activity} (
|
|
4463
4495
|
id VARCHAR(255) ${BIN} NOT NULL PRIMARY KEY,
|
|
@@ -4523,6 +4555,8 @@ function toPin(row) {
|
|
|
4523
4555
|
return {
|
|
4524
4556
|
id: String(row.id),
|
|
4525
4557
|
pageId: String(row.pageId),
|
|
4558
|
+
kind: String(row.kind),
|
|
4559
|
+
...row.depth == null ? {} : { depth: Number(row.depth) },
|
|
4526
4560
|
audience: parseJsonList(row.audience),
|
|
4527
4561
|
createdBy: String(row.createdBy),
|
|
4528
4562
|
createdAt: new Date(String(row.createdAt)),
|
|
@@ -4556,6 +4590,13 @@ function isRetryableRace(error) {
|
|
|
4556
4590
|
}
|
|
4557
4591
|
return false;
|
|
4558
4592
|
}
|
|
4593
|
+
function isLiveKeyClash(error) {
|
|
4594
|
+
for (let current = error; current && typeof current === "object"; current = current.cause) {
|
|
4595
|
+
const { code, errno, message } = current;
|
|
4596
|
+
if ((code === "ER_DUP_ENTRY" || errno === 1062) && String(message).includes("idx_kpages_live_key")) return true;
|
|
4597
|
+
}
|
|
4598
|
+
return false;
|
|
4599
|
+
}
|
|
4559
4600
|
function escapeLike(value) {
|
|
4560
4601
|
return value.replace(/[=%_]/g, (match) => `=${match}`);
|
|
4561
4602
|
}
|
|
@@ -4687,6 +4728,7 @@ var MySQLKnowledgePages = class {
|
|
|
4687
4728
|
T.mentions,
|
|
4688
4729
|
T.stamps,
|
|
4689
4730
|
T.notes,
|
|
4731
|
+
T.links,
|
|
4690
4732
|
T.pages
|
|
4691
4733
|
]) await tx.execute({
|
|
4692
4734
|
sql: `DELETE FROM ${table}`,
|
|
@@ -4869,13 +4911,19 @@ var MySQLKnowledgePages = class {
|
|
|
4869
4911
|
const limit = limitOf(input.limit, core.MAX_KNOWLEDGE_PAGE_LIST_LIMIT);
|
|
4870
4912
|
const cursor = decodeCursor(input.cursor, 2);
|
|
4871
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
|
+
}
|
|
4872
4920
|
let after = "";
|
|
4873
4921
|
if (cursor) {
|
|
4874
|
-
after = ` AND (position > ? OR (position = ? AND id > ?))`;
|
|
4922
|
+
after = ` AND (p.position > ? OR (p.position = ? AND p.id > ?))`;
|
|
4875
4923
|
args.push(cursor[0], cursor[0], cursor[1]);
|
|
4876
4924
|
}
|
|
4877
4925
|
args.push(limit + 1);
|
|
4878
|
-
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);
|
|
4879
4927
|
return this.#pageOutput(rows, limit, (row) => [String(row.position), String(row.id)]);
|
|
4880
4928
|
}
|
|
4881
4929
|
#pageOutput(rows, limit, cursorOf) {
|
|
@@ -5009,7 +5057,12 @@ var MySQLKnowledgePages = class {
|
|
|
5009
5057
|
}
|
|
5010
5058
|
async createPage(input) {
|
|
5011
5059
|
await assertKnowledgePageDescriptionWithinBound(input.description);
|
|
5012
|
-
|
|
5060
|
+
try {
|
|
5061
|
+
return await this.#transaction((tx) => this.#insertPage(tx, input));
|
|
5062
|
+
} catch (error) {
|
|
5063
|
+
if (input.key !== void 0 && isLiveKeyClash(error)) throw new (await (loadPagesCore())).KnowledgePageKeyConflictError(input.key, input.parentId);
|
|
5064
|
+
throw error;
|
|
5065
|
+
}
|
|
5013
5066
|
}
|
|
5014
5067
|
async updatePage(input) {
|
|
5015
5068
|
const core = await loadPagesCore();
|
|
@@ -5160,9 +5213,8 @@ var MySQLKnowledgePages = class {
|
|
|
5160
5213
|
args: [input.sourceId]
|
|
5161
5214
|
});
|
|
5162
5215
|
const pins = await this.#query(tx, `SELECT * FROM ${T.pins} WHERE pageId IN (?, ?)`, [input.sourceId, input.targetId]);
|
|
5163
|
-
const sourcePin
|
|
5164
|
-
|
|
5165
|
-
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);
|
|
5166
5218
|
const audience = parseJsonList(sourcePin.audience);
|
|
5167
5219
|
if (targetPin) {
|
|
5168
5220
|
const merged = [.../* @__PURE__ */ new Set([...parseJsonList(targetPin.audience), ...audience])];
|
|
@@ -5262,6 +5314,148 @@ var MySQLKnowledgePages = class {
|
|
|
5262
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);
|
|
5263
5315
|
return this.#noteOutput(rows, limit);
|
|
5264
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
|
+
}
|
|
5326
|
+
async countChildPages(input) {
|
|
5327
|
+
const ids = [...new Set(input.parentIds)];
|
|
5328
|
+
const counts = Object.fromEntries(ids.map((id) => [id, 0]));
|
|
5329
|
+
if (ids.length === 0) return counts;
|
|
5330
|
+
const readable = pageReadable("p", input.read);
|
|
5331
|
+
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]);
|
|
5332
|
+
for (const row of rows) counts[String(row.parentId)] = Number(row.total);
|
|
5333
|
+
return counts;
|
|
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
|
+
}
|
|
5265
5459
|
async countNotes(input) {
|
|
5266
5460
|
const ids = [...new Set(input.pageIds)];
|
|
5267
5461
|
const counts = Object.fromEntries(ids.map((id) => [id, 0]));
|
|
@@ -5433,18 +5627,22 @@ var MySQLKnowledgePages = class {
|
|
|
5433
5627
|
const readable = pagesInReadableScopes("p", input.read);
|
|
5434
5628
|
const scopeFilter = input.scopeIds ? ` AND p.scopeId IN ${JV}` : "";
|
|
5435
5629
|
const scopeArgs = input.scopeIds ? [jsonList(input.scopeIds)] : [];
|
|
5436
|
-
const
|
|
5437
|
-
const
|
|
5438
|
-
const
|
|
5630
|
+
const field = (column) => `LOWER(COALESCE(${column}, '')) LIKE ? ESCAPE '='`;
|
|
5631
|
+
const pageMatch = words.map(() => `(${field("p.name")} OR ${field("p.description")} OR ${field("p.body")})`).join(" OR ");
|
|
5632
|
+
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(" + ");
|
|
5633
|
+
const pageCount = words.map(() => `(CASE WHEN ${field("p.name")} OR ${field("p.description")} OR ${field("p.body")} THEN 1 ELSE 0 END)`).join(" + ");
|
|
5634
|
+
const triple = likes.flatMap((like) => [
|
|
5635
|
+
like,
|
|
5636
|
+
like,
|
|
5637
|
+
like
|
|
5638
|
+
]);
|
|
5639
|
+
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 ?`, [
|
|
5439
5640
|
...readable.args,
|
|
5440
5641
|
...scopeArgs,
|
|
5441
|
-
...
|
|
5442
|
-
like,
|
|
5443
|
-
like,
|
|
5444
|
-
like
|
|
5445
|
-
]),
|
|
5642
|
+
...triple,
|
|
5446
5643
|
phrase,
|
|
5447
|
-
...
|
|
5644
|
+
...triple,
|
|
5645
|
+
...triple,
|
|
5448
5646
|
limit
|
|
5449
5647
|
])).map((row) => ({
|
|
5450
5648
|
type: "page",
|
|
@@ -5452,11 +5650,12 @@ var MySQLKnowledgePages = class {
|
|
|
5452
5650
|
}));
|
|
5453
5651
|
if (hits.length >= limit) return hits;
|
|
5454
5652
|
const noteVisible = noteReadable("n", input.read);
|
|
5455
|
-
const
|
|
5456
|
-
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 ${
|
|
5653
|
+
const noteHas = words.map(() => `LOWER(n.text) LIKE ? ESCAPE '='`);
|
|
5654
|
+
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 ?`, [
|
|
5457
5655
|
...scopeArgs,
|
|
5458
5656
|
...likes,
|
|
5459
5657
|
...noteVisible.args,
|
|
5658
|
+
...likes,
|
|
5460
5659
|
limit - hits.length
|
|
5461
5660
|
]);
|
|
5462
5661
|
const notes = await this.#hydrateNotes(this.#read, noteRows);
|
|
@@ -5518,17 +5717,21 @@ var MySQLKnowledgePages = class {
|
|
|
5518
5717
|
});
|
|
5519
5718
|
}
|
|
5520
5719
|
addPinAudience(input) {
|
|
5720
|
+
const kind = input.kind ?? "page";
|
|
5721
|
+
const depth = kind === "list" ? input.depth ?? 1 : null;
|
|
5521
5722
|
return this.#transaction(async (tx) => {
|
|
5522
5723
|
const page = await this.#livePage(tx, input.pageId);
|
|
5523
5724
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5524
|
-
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];
|
|
5525
5726
|
if (!existing) {
|
|
5526
5727
|
const id = (0, _mastra_core_storage.createKnowledgeUlid)();
|
|
5527
5728
|
await tx.execute({
|
|
5528
|
-
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 (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
5529
5730
|
args: [
|
|
5530
5731
|
id,
|
|
5531
5732
|
input.pageId,
|
|
5733
|
+
kind,
|
|
5734
|
+
depth,
|
|
5532
5735
|
JSON.stringify([...new Set(input.audience)]),
|
|
5533
5736
|
input.createdBy,
|
|
5534
5737
|
now,
|
|
@@ -5545,11 +5748,13 @@ var MySQLKnowledgePages = class {
|
|
|
5545
5748
|
}
|
|
5546
5749
|
const current = parseJsonList(existing.audience);
|
|
5547
5750
|
const merged = [.../* @__PURE__ */ new Set([...current, ...input.audience])];
|
|
5548
|
-
|
|
5751
|
+
const depthChanged = (existing.depth == null ? null : Number(existing.depth)) !== depth;
|
|
5752
|
+
if (merged.length !== current.length || depthChanged) {
|
|
5549
5753
|
await tx.execute({
|
|
5550
|
-
sql: `UPDATE ${T.pins} SET audience = ?, updatedAt = ? WHERE id = ?`,
|
|
5754
|
+
sql: `UPDATE ${T.pins} SET audience = ?, depth = ?, updatedAt = ? WHERE id = ?`,
|
|
5551
5755
|
args: [
|
|
5552
5756
|
JSON.stringify(merged),
|
|
5757
|
+
depth,
|
|
5553
5758
|
now,
|
|
5554
5759
|
String(existing.id)
|
|
5555
5760
|
]
|
|
@@ -5566,7 +5771,7 @@ var MySQLKnowledgePages = class {
|
|
|
5566
5771
|
}
|
|
5567
5772
|
removePinAudience(input) {
|
|
5568
5773
|
return this.#transaction(async (tx) => {
|
|
5569
|
-
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];
|
|
5570
5775
|
if (!existing) return null;
|
|
5571
5776
|
const current = parseJsonList(existing.audience);
|
|
5572
5777
|
const remove = new Set(input.audience);
|
|
@@ -5610,15 +5815,19 @@ var MySQLKnowledgePages = class {
|
|
|
5610
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)`;
|
|
5611
5816
|
args.push(jsonList(input.audience));
|
|
5612
5817
|
}
|
|
5818
|
+
if (input.kind) {
|
|
5819
|
+
filters += ` AND pin.kind = ?`;
|
|
5820
|
+
args.push(input.kind);
|
|
5821
|
+
}
|
|
5613
5822
|
if (input.pageIds) {
|
|
5614
5823
|
filters += ` AND pin.pageId IN ${JV}`;
|
|
5615
5824
|
args.push(jsonList(input.pageIds));
|
|
5616
5825
|
}
|
|
5617
5826
|
if (input.excludePageIds?.length) {
|
|
5618
|
-
filters += ` AND pin.pageId NOT IN ${JV}`;
|
|
5827
|
+
filters += ` AND (pin.kind <> 'page' OR pin.pageId NOT IN ${JV})`;
|
|
5619
5828
|
args.push(jsonList(input.excludePageIds));
|
|
5620
5829
|
}
|
|
5621
|
-
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:]]'))`;
|
|
5622
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}`;
|
|
5623
5832
|
let total;
|
|
5624
5833
|
if (input.count) total = Number((await this.#query(this.#read, `SELECT COUNT(*) AS n ${from}`, args))[0].n);
|
|
@@ -5679,7 +5888,8 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
|
|
|
5679
5888
|
contractVersion: 4,
|
|
5680
5889
|
schemaVersion: 4,
|
|
5681
5890
|
supportsSchemaInspection: true,
|
|
5682
|
-
supportsExplicitReset: true
|
|
5891
|
+
supportsExplicitReset: true,
|
|
5892
|
+
supportsPinKinds: true
|
|
5683
5893
|
};
|
|
5684
5894
|
}
|
|
5685
5895
|
async #inspectPages(client = this.#pool) {
|
|
@@ -5735,7 +5945,11 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
|
|
|
5735
5945
|
await this.#upgrade();
|
|
5736
5946
|
}
|
|
5737
5947
|
async dangerouslyReset() {
|
|
5738
|
-
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}\``);
|
|
5739
5953
|
await this.init();
|
|
5740
5954
|
}
|
|
5741
5955
|
materializeScope(input) {
|
|
@@ -5753,6 +5967,9 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
|
|
|
5753
5967
|
listChildScopes(input) {
|
|
5754
5968
|
return this.#pages.listChildScopes(input);
|
|
5755
5969
|
}
|
|
5970
|
+
countChildScopes(input) {
|
|
5971
|
+
return this.#pages.countChildScopes(input);
|
|
5972
|
+
}
|
|
5756
5973
|
getPage(input) {
|
|
5757
5974
|
return this.#pages.getPage(input);
|
|
5758
5975
|
}
|
|
@@ -5789,6 +6006,21 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
|
|
|
5789
6006
|
listNotes(input) {
|
|
5790
6007
|
return this.#pages.listNotes(input);
|
|
5791
6008
|
}
|
|
6009
|
+
countChildPages(input) {
|
|
6010
|
+
return this.#pages.countChildPages(input);
|
|
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
|
+
}
|
|
5792
6024
|
countNotes(input) {
|
|
5793
6025
|
return this.#pages.countNotes(input);
|
|
5794
6026
|
}
|