@mastra/libsql 1.20.1-alpha.0 → 1.21.0
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/CHANGELOG.md +36 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-storage.md +1 -0
- package/dist/docs/references/reference-rag-retrieval.md +26 -18
- package/dist/index.cjs +851 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +852 -15
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/knowledge/index.d.ts +81 -0
- package/dist/storage/domains/knowledge/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +2 -1
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -269,7 +269,7 @@ const FILTER_OPERATORS = {
|
|
|
269
269
|
transformValue: () => {
|
|
270
270
|
if (Array.isArray(value)) return [JSON.stringify(value)];
|
|
271
271
|
if (typeof value === "object" && value !== null) return [JSON.stringify(value)];
|
|
272
|
-
if (typeof value === "string") return [escapeLikePattern(value)];
|
|
272
|
+
if (typeof value === "string") return [escapeLikePattern$1(value)];
|
|
273
273
|
return [value];
|
|
274
274
|
}
|
|
275
275
|
};
|
|
@@ -286,7 +286,7 @@ const parseJsonPathKey = (key) => {
|
|
|
286
286
|
const getJsonPath = (key) => {
|
|
287
287
|
return `'$.${parseJsonPathKey(key)}'`;
|
|
288
288
|
};
|
|
289
|
-
function escapeLikePattern(str) {
|
|
289
|
+
function escapeLikePattern$1(str) {
|
|
290
290
|
return str.replace(/([%_\\])/g, "\\$1");
|
|
291
291
|
}
|
|
292
292
|
function buildFilterQuery(filter) {
|
|
@@ -2857,7 +2857,7 @@ function serializeJson(v) {
|
|
|
2857
2857
|
if (typeof v === "object" && v != null) return JSON.stringify(v);
|
|
2858
2858
|
return v ?? null;
|
|
2859
2859
|
}
|
|
2860
|
-
function parseJson$
|
|
2860
|
+
function parseJson$4(val) {
|
|
2861
2861
|
if (val == null) return void 0;
|
|
2862
2862
|
if (typeof val === "string") try {
|
|
2863
2863
|
return JSON.parse(val);
|
|
@@ -2872,14 +2872,14 @@ function rowToTask(row) {
|
|
|
2872
2872
|
status: String(row.status),
|
|
2873
2873
|
toolName: String(row.tool_name),
|
|
2874
2874
|
toolCallId: String(row.tool_call_id),
|
|
2875
|
-
args: parseJson$
|
|
2875
|
+
args: parseJson$4(row.args) ?? {},
|
|
2876
2876
|
agentId: String(row.agent_id),
|
|
2877
2877
|
threadId: row.thread_id != null ? String(row.thread_id) : void 0,
|
|
2878
2878
|
resourceId: row.resource_id != null ? String(row.resource_id) : void 0,
|
|
2879
2879
|
runId: String(row.run_id),
|
|
2880
|
-
result: parseJson$
|
|
2881
|
-
error: parseJson$
|
|
2882
|
-
suspendPayload: parseJson$
|
|
2880
|
+
result: parseJson$4(row.result),
|
|
2881
|
+
error: parseJson$4(row.error),
|
|
2882
|
+
suspendPayload: parseJson$4(row.suspend_payload),
|
|
2883
2883
|
retryCount: Number(row.retry_count),
|
|
2884
2884
|
maxRetries: Number(row.max_retries),
|
|
2885
2885
|
timeoutMs: Number(row.timeout_ms),
|
|
@@ -5540,10 +5540,10 @@ var FavoritesLibSQL = class extends _mastra_core_storage.FavoritesStorage {
|
|
|
5540
5540
|
};
|
|
5541
5541
|
//#endregion
|
|
5542
5542
|
//#region src/storage/domains/harness/index.ts
|
|
5543
|
-
const toDate = (value) => new Date(value);
|
|
5543
|
+
const toDate$1 = (value) => new Date(value);
|
|
5544
5544
|
const toOptionalDate = (value) => {
|
|
5545
5545
|
if (value == null) return void 0;
|
|
5546
|
-
return toDate(value);
|
|
5546
|
+
return toDate$1(value);
|
|
5547
5547
|
};
|
|
5548
5548
|
function cloneJson(value) {
|
|
5549
5549
|
return value === void 0 ? void 0 : structuredClone(value);
|
|
@@ -5554,8 +5554,8 @@ function hydratePendingItem(item) {
|
|
|
5554
5554
|
kind: item.kind,
|
|
5555
5555
|
status: item.status,
|
|
5556
5556
|
sessionId: item.sessionId,
|
|
5557
|
-
createdAt: toDate(item.createdAt),
|
|
5558
|
-
updatedAt: toDate(item.updatedAt)
|
|
5557
|
+
createdAt: toDate$1(item.createdAt),
|
|
5558
|
+
updatedAt: toDate$1(item.updatedAt)
|
|
5559
5559
|
};
|
|
5560
5560
|
if (item.runId !== void 0) record.runId = item.runId;
|
|
5561
5561
|
if (item.traceId !== void 0) record.traceId = item.traceId;
|
|
@@ -5573,8 +5573,8 @@ function rowToSession(row) {
|
|
|
5573
5573
|
origin: row.origin,
|
|
5574
5574
|
modeId: row.modeId,
|
|
5575
5575
|
modelId: row.modelId,
|
|
5576
|
-
createdAt: toDate(row.createdAt),
|
|
5577
|
-
lastActivityAt: toDate(row.lastActivityAt)
|
|
5576
|
+
createdAt: toDate$1(row.createdAt),
|
|
5577
|
+
lastActivityAt: toDate$1(row.lastActivityAt)
|
|
5578
5578
|
};
|
|
5579
5579
|
if (row.parentSessionId != null) record.parentSessionId = row.parentSessionId;
|
|
5580
5580
|
if (row.subagentDepth != null) record.subagentDepth = row.subagentDepth;
|
|
@@ -5684,6 +5684,841 @@ var HarnessLibSQL = class HarnessLibSQL extends _mastra_core_storage.HarnessStor
|
|
|
5684
5684
|
}
|
|
5685
5685
|
};
|
|
5686
5686
|
//#endregion
|
|
5687
|
+
//#region src/storage/domains/knowledge/index.ts
|
|
5688
|
+
const visibleSql = `(scopeKey = ? OR substr(?, 1, length(scopeKey) + 1) = scopeKey || char(31))`;
|
|
5689
|
+
function parseJson$3(value) {
|
|
5690
|
+
if (typeof value === "string") return JSON.parse(value);
|
|
5691
|
+
if (value instanceof Uint8Array) return JSON.parse(new TextDecoder().decode(value));
|
|
5692
|
+
if (value instanceof ArrayBuffer) return JSON.parse(new TextDecoder().decode(value));
|
|
5693
|
+
return value;
|
|
5694
|
+
}
|
|
5695
|
+
function toDate(value) {
|
|
5696
|
+
return new Date(String(value));
|
|
5697
|
+
}
|
|
5698
|
+
function optionalDate(value) {
|
|
5699
|
+
return value == null ? void 0 : toDate(value);
|
|
5700
|
+
}
|
|
5701
|
+
function canonicalName(name) {
|
|
5702
|
+
return name.trim().toLocaleLowerCase();
|
|
5703
|
+
}
|
|
5704
|
+
function escapeLikePattern(value) {
|
|
5705
|
+
return value.replaceAll("=", "==").replaceAll("%", "=%").replaceAll("_", "=_");
|
|
5706
|
+
}
|
|
5707
|
+
function nodeReferenceId(node) {
|
|
5708
|
+
return typeof node === "string" ? node : node.id;
|
|
5709
|
+
}
|
|
5710
|
+
function parseNode(row) {
|
|
5711
|
+
return {
|
|
5712
|
+
id: String(row.id),
|
|
5713
|
+
type: "node",
|
|
5714
|
+
name: String(row.name),
|
|
5715
|
+
kind: row.kind == null ? "" : String(row.kind),
|
|
5716
|
+
content: row.content == null ? void 0 : String(row.content),
|
|
5717
|
+
scope: parseJson$3(row.scopeJson ?? row.scope),
|
|
5718
|
+
version: Number(row.version),
|
|
5719
|
+
mergedInto: row.mergedInto == null ? void 0 : String(row.mergedInto),
|
|
5720
|
+
createdAt: toDate(row.createdAt),
|
|
5721
|
+
updatedAt: toDate(row.updatedAt)
|
|
5722
|
+
};
|
|
5723
|
+
}
|
|
5724
|
+
function parseKnowledge(row) {
|
|
5725
|
+
return {
|
|
5726
|
+
id: String(row.id),
|
|
5727
|
+
node: String(row.node),
|
|
5728
|
+
text: String(row.text),
|
|
5729
|
+
scope: parseJson$3(row.scopeJson ?? row.scope),
|
|
5730
|
+
sourceThreadId: String(row.sourceThreadId),
|
|
5731
|
+
capturedAt: toDate(row.capturedAt),
|
|
5732
|
+
when: optionalDate(row.when),
|
|
5733
|
+
maxScope: row.maxScope == null ? void 0 : String(row.maxScope),
|
|
5734
|
+
metadata: (row.metadataJson ?? row.metadata) == null ? void 0 : parseJson$3(row.metadataJson ?? row.metadata),
|
|
5735
|
+
deletedAt: optionalDate(row.deletedAt),
|
|
5736
|
+
deletedBy: row.deletedBy == null ? void 0 : String(row.deletedBy)
|
|
5737
|
+
};
|
|
5738
|
+
}
|
|
5739
|
+
function parseOutbox(row) {
|
|
5740
|
+
return {
|
|
5741
|
+
id: String(row.id),
|
|
5742
|
+
idempotencyKey: String(row.idempotencyKey),
|
|
5743
|
+
documentId: String(row.documentId),
|
|
5744
|
+
documentType: String(row.documentType),
|
|
5745
|
+
operation: String(row.operation),
|
|
5746
|
+
scope: parseJson$3(row.scopeJson ?? row.scope),
|
|
5747
|
+
status: String(row.status),
|
|
5748
|
+
attempts: Number(row.attempts),
|
|
5749
|
+
availableAt: toDate(row.availableAt),
|
|
5750
|
+
claimedAt: optionalDate(row.claimedAt),
|
|
5751
|
+
claimedBy: row.claimedBy == null ? void 0 : String(row.claimedBy),
|
|
5752
|
+
createdAt: toDate(row.createdAt),
|
|
5753
|
+
completedAt: optionalDate(row.completedAt)
|
|
5754
|
+
};
|
|
5755
|
+
}
|
|
5756
|
+
var KnowledgeLibSQL = class extends _mastra_core_storage.KnowledgeStorage {
|
|
5757
|
+
#client;
|
|
5758
|
+
#db;
|
|
5759
|
+
constructor(config) {
|
|
5760
|
+
super();
|
|
5761
|
+
this.#client = resolveClient(config);
|
|
5762
|
+
this.#db = new LibSQLDB({
|
|
5763
|
+
client: this.#client,
|
|
5764
|
+
maxRetries: config.maxRetries,
|
|
5765
|
+
initialBackoffMs: config.initialBackoffMs
|
|
5766
|
+
});
|
|
5767
|
+
}
|
|
5768
|
+
async init() {
|
|
5769
|
+
await this.#db.createTable({
|
|
5770
|
+
tableName: _mastra_core_storage.TABLE_KNOWLEDGE_NODES,
|
|
5771
|
+
schema: _mastra_core_storage.KNOWLEDGE_NODES_SCHEMA
|
|
5772
|
+
});
|
|
5773
|
+
await this.#db.createTable({
|
|
5774
|
+
tableName: _mastra_core_storage.TABLE_KNOWLEDGE_RECORDS,
|
|
5775
|
+
schema: _mastra_core_storage.KNOWLEDGE_RECORDS_SCHEMA
|
|
5776
|
+
});
|
|
5777
|
+
await this.#db.createTable({
|
|
5778
|
+
tableName: _mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS,
|
|
5779
|
+
schema: _mastra_core_storage.KNOWLEDGE_MENTIONS_SCHEMA,
|
|
5780
|
+
compositePrimaryKey: [
|
|
5781
|
+
"sourceType",
|
|
5782
|
+
"sourceId",
|
|
5783
|
+
"recordId"
|
|
5784
|
+
]
|
|
5785
|
+
});
|
|
5786
|
+
await this.#db.createTable({
|
|
5787
|
+
tableName: _mastra_core_storage.TABLE_KNOWLEDGE_CURSORS,
|
|
5788
|
+
schema: _mastra_core_storage.KNOWLEDGE_CURSORS_SCHEMA,
|
|
5789
|
+
compositePrimaryKey: ["sourceThreadId", "agent"]
|
|
5790
|
+
});
|
|
5791
|
+
await this.#db.createTable({
|
|
5792
|
+
tableName: _mastra_core_storage.TABLE_KNOWLEDGE_ACTIVITY,
|
|
5793
|
+
schema: _mastra_core_storage.KNOWLEDGE_ACTIVITY_SCHEMA
|
|
5794
|
+
});
|
|
5795
|
+
await this.#db.createTable({
|
|
5796
|
+
tableName: _mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX,
|
|
5797
|
+
schema: _mastra_core_storage.KNOWLEDGE_SEMANTIC_OUTBOX_SCHEMA
|
|
5798
|
+
});
|
|
5799
|
+
await this.#client.batch([
|
|
5800
|
+
{
|
|
5801
|
+
sql: `CREATE UNIQUE INDEX IF NOT EXISTS idx_knowledge_nodes_identity ON "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" (type, scopeKey, canonicalName)`,
|
|
5802
|
+
args: []
|
|
5803
|
+
},
|
|
5804
|
+
{
|
|
5805
|
+
sql: `CREATE INDEX IF NOT EXISTS idx_knowledge_nodes_scope ON "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" (scopeKey, type)`,
|
|
5806
|
+
args: []
|
|
5807
|
+
},
|
|
5808
|
+
{
|
|
5809
|
+
sql: `CREATE INDEX IF NOT EXISTS idx_knowledge_records_node_latest ON "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" (node, id DESC)`,
|
|
5810
|
+
args: []
|
|
5811
|
+
},
|
|
5812
|
+
{
|
|
5813
|
+
sql: `CREATE INDEX IF NOT EXISTS idx_knowledge_records_thread_latest ON "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" (sourceThreadId, id DESC)`,
|
|
5814
|
+
args: []
|
|
5815
|
+
},
|
|
5816
|
+
{
|
|
5817
|
+
sql: `CREATE INDEX IF NOT EXISTS idx_knowledge_mentions_record ON "${_mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS}" (recordId, sourceType, sourceId)`,
|
|
5818
|
+
args: []
|
|
5819
|
+
},
|
|
5820
|
+
{
|
|
5821
|
+
sql: `CREATE INDEX IF NOT EXISTS idx_knowledge_activity_latest ON "${_mastra_core_storage.TABLE_KNOWLEDGE_ACTIVITY}" (id DESC)`,
|
|
5822
|
+
args: []
|
|
5823
|
+
},
|
|
5824
|
+
{
|
|
5825
|
+
sql: `CREATE UNIQUE INDEX IF NOT EXISTS idx_knowledge_outbox_idempotency ON "${_mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}" (idempotencyKey)`,
|
|
5826
|
+
args: []
|
|
5827
|
+
},
|
|
5828
|
+
{
|
|
5829
|
+
sql: `CREATE INDEX IF NOT EXISTS idx_knowledge_outbox_claim ON "${_mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}" (status, availableAt, createdAt)`,
|
|
5830
|
+
args: []
|
|
5831
|
+
}
|
|
5832
|
+
], "write");
|
|
5833
|
+
}
|
|
5834
|
+
async dangerouslyClearAll() {
|
|
5835
|
+
await this.#transaction(async (tx) => {
|
|
5836
|
+
for (const table of [
|
|
5837
|
+
_mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS,
|
|
5838
|
+
_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS,
|
|
5839
|
+
_mastra_core_storage.TABLE_KNOWLEDGE_NODES,
|
|
5840
|
+
_mastra_core_storage.TABLE_KNOWLEDGE_CURSORS,
|
|
5841
|
+
_mastra_core_storage.TABLE_KNOWLEDGE_ACTIVITY,
|
|
5842
|
+
_mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX
|
|
5843
|
+
]) await tx.execute(`DELETE FROM "${table}"`);
|
|
5844
|
+
});
|
|
5845
|
+
}
|
|
5846
|
+
async createNode(input) {
|
|
5847
|
+
const scope = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope);
|
|
5848
|
+
return this.#transaction(async (tx) => {
|
|
5849
|
+
const existing = await this.#getNodeByName(tx, input.name, scope);
|
|
5850
|
+
if (existing) {
|
|
5851
|
+
const terminal = await this.#resolveTerminalNode(tx, existing.id);
|
|
5852
|
+
if (!(0, _mastra_core_storage.isKnowledgeScopeVisible)(terminal.scope, scope)) throw new Error(`Merged knowledge node is not visible from scope: ${input.name}`);
|
|
5853
|
+
return terminal;
|
|
5854
|
+
}
|
|
5855
|
+
const now = /* @__PURE__ */ new Date();
|
|
5856
|
+
const node = {
|
|
5857
|
+
id: input.id ?? crypto.randomUUID(),
|
|
5858
|
+
type: "node",
|
|
5859
|
+
name: input.name.trim(),
|
|
5860
|
+
kind: input.kind,
|
|
5861
|
+
content: input.content,
|
|
5862
|
+
scope,
|
|
5863
|
+
version: 1,
|
|
5864
|
+
createdAt: now,
|
|
5865
|
+
updatedAt: now
|
|
5866
|
+
};
|
|
5867
|
+
await tx.execute({
|
|
5868
|
+
sql: `INSERT INTO "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" (id,type,name,canonicalName,kind,content,scope,scopeKey,version,mergedInto,createdAt,updatedAt) VALUES (?,?,?,?,?,?,jsonb(?),?,?,NULL,?,?)`,
|
|
5869
|
+
args: [
|
|
5870
|
+
node.id,
|
|
5871
|
+
"node",
|
|
5872
|
+
node.name,
|
|
5873
|
+
canonicalName(node.name),
|
|
5874
|
+
node.kind,
|
|
5875
|
+
node.content ?? null,
|
|
5876
|
+
JSON.stringify(scope),
|
|
5877
|
+
(0, _mastra_core_storage.knowledgeScopeKey)(scope),
|
|
5878
|
+
node.version,
|
|
5879
|
+
now.toISOString(),
|
|
5880
|
+
now.toISOString()
|
|
5881
|
+
]
|
|
5882
|
+
});
|
|
5883
|
+
await this.#replaceMentions(tx, "node", node.id, node.content ?? "", input.resolutionScope ?? scope, scope);
|
|
5884
|
+
await this.#activity(tx, "node-created", "node", node.id, scope);
|
|
5885
|
+
await this.#outbox(tx, "node", node.id, "upsert", node.version, scope);
|
|
5886
|
+
return node;
|
|
5887
|
+
});
|
|
5888
|
+
}
|
|
5889
|
+
async getNode(id) {
|
|
5890
|
+
return this.#getNode(this.#client, id);
|
|
5891
|
+
}
|
|
5892
|
+
async getNodeByName(input) {
|
|
5893
|
+
return this.#getNodeByName(this.#client, input.name, (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope));
|
|
5894
|
+
}
|
|
5895
|
+
async resolveNode(input) {
|
|
5896
|
+
return this.#resolveNode(this.#client, input.name, (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope));
|
|
5897
|
+
}
|
|
5898
|
+
async listNodes(input) {
|
|
5899
|
+
const key = (0, _mastra_core_storage.knowledgeScopeKey)((0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope));
|
|
5900
|
+
const clauses = [
|
|
5901
|
+
`type = 'node'`,
|
|
5902
|
+
"mergedInto IS NULL",
|
|
5903
|
+
visibleSql
|
|
5904
|
+
];
|
|
5905
|
+
const args = [key, key];
|
|
5906
|
+
if (input.namePrefix) {
|
|
5907
|
+
clauses.push("canonicalName LIKE ? ESCAPE '='");
|
|
5908
|
+
args.push(`${escapeLikePattern(canonicalName(input.namePrefix))}%`);
|
|
5909
|
+
}
|
|
5910
|
+
if (input.kind) {
|
|
5911
|
+
clauses.push("kind = ?");
|
|
5912
|
+
args.push(input.kind);
|
|
5913
|
+
}
|
|
5914
|
+
if (input.hasContent !== void 0) clauses.push(input.hasContent ? "content IS NOT NULL AND content <> ''" : "(content IS NULL OR content = '')");
|
|
5915
|
+
if (input.cursor) {
|
|
5916
|
+
const cursor = (0, _mastra_core_storage.parseKnowledgeNodeCursor)(input.cursor, {
|
|
5917
|
+
namePrefix: input.namePrefix,
|
|
5918
|
+
kind: input.kind,
|
|
5919
|
+
hasContent: input.hasContent
|
|
5920
|
+
});
|
|
5921
|
+
clauses.push("(updatedAt < ? OR (updatedAt = ? AND (name > ? OR (name = ? AND id > ?))))");
|
|
5922
|
+
const updatedAt = cursor.updatedAt.toISOString();
|
|
5923
|
+
args.push(updatedAt, updatedAt, cursor.name, cursor.name, cursor.id);
|
|
5924
|
+
}
|
|
5925
|
+
args.push(input.limit ?? 100);
|
|
5926
|
+
return (await this.#client.execute({
|
|
5927
|
+
sql: `SELECT *, json(scope) AS scopeJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" WHERE ${clauses.join(" AND ")} ORDER BY updatedAt DESC, name ASC, id ASC LIMIT ?`,
|
|
5928
|
+
args
|
|
5929
|
+
})).rows.map(parseNode);
|
|
5930
|
+
}
|
|
5931
|
+
async updateNode(input) {
|
|
5932
|
+
return this.#transaction(async (tx) => {
|
|
5933
|
+
const existing = await this.#getNode(tx, input.id);
|
|
5934
|
+
if (!existing) throw new _mastra_core_storage.KnowledgeNotFoundError("node", input.id);
|
|
5935
|
+
if (existing.mergedInto) throw new Error(`Cannot update merged knowledge node: ${input.id}`);
|
|
5936
|
+
const scope = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope ?? existing.scope);
|
|
5937
|
+
const name = (input.name ?? existing.name).trim();
|
|
5938
|
+
const content = input.content ?? existing.content;
|
|
5939
|
+
const now = /* @__PURE__ */ new Date();
|
|
5940
|
+
if ((await tx.execute({
|
|
5941
|
+
sql: `UPDATE "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" SET name=?,canonicalName=?,kind=?,content=?,scope=jsonb(?),scopeKey=?,version=version+1,updatedAt=? WHERE id=? AND type='node' AND version=?`,
|
|
5942
|
+
args: [
|
|
5943
|
+
name,
|
|
5944
|
+
canonicalName(name),
|
|
5945
|
+
input.kind ?? existing.kind,
|
|
5946
|
+
content ?? null,
|
|
5947
|
+
JSON.stringify(scope),
|
|
5948
|
+
(0, _mastra_core_storage.knowledgeScopeKey)(scope),
|
|
5949
|
+
now.toISOString(),
|
|
5950
|
+
input.id,
|
|
5951
|
+
input.version
|
|
5952
|
+
]
|
|
5953
|
+
})).rowsAffected === 0) throw new _mastra_core_storage.KnowledgeConflictError(input.id);
|
|
5954
|
+
if (input.content !== void 0 || input.name !== void 0 || input.scope !== void 0) await this.#replaceMentions(tx, "node", input.id, content ?? "", input.resolutionScope ?? scope, scope);
|
|
5955
|
+
await this.#activity(tx, "node-updated", "node", input.id, scope);
|
|
5956
|
+
if ((0, _mastra_core_storage.knowledgeScopeKey)(existing.scope) !== (0, _mastra_core_storage.knowledgeScopeKey)(scope)) {
|
|
5957
|
+
await this.#outbox(tx, "node", input.id, "delete", (0, _mastra_core_storage.createKnowledgeUlid)(), existing.scope);
|
|
5958
|
+
const knowledge = await tx.execute({
|
|
5959
|
+
sql: `SELECT id,json(scope) AS scopeJson,deletedAt FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" WHERE node=?`,
|
|
5960
|
+
args: [input.id]
|
|
5961
|
+
});
|
|
5962
|
+
for (const row of knowledge.rows) {
|
|
5963
|
+
const recordScope = parseJson$3(row.scopeJson);
|
|
5964
|
+
await this.#outbox(tx, "record", String(row.id), "delete", (0, _mastra_core_storage.createKnowledgeUlid)(), recordScope);
|
|
5965
|
+
if (row.deletedAt == null) await this.#outbox(tx, "record", String(row.id), "upsert", (0, _mastra_core_storage.createKnowledgeUlid)(), recordScope);
|
|
5966
|
+
}
|
|
5967
|
+
}
|
|
5968
|
+
await this.#outbox(tx, "node", input.id, "upsert", input.version + 1, scope);
|
|
5969
|
+
return {
|
|
5970
|
+
...existing,
|
|
5971
|
+
name,
|
|
5972
|
+
kind: input.kind ?? existing.kind,
|
|
5973
|
+
content,
|
|
5974
|
+
scope,
|
|
5975
|
+
version: input.version + 1,
|
|
5976
|
+
updatedAt: now
|
|
5977
|
+
};
|
|
5978
|
+
});
|
|
5979
|
+
}
|
|
5980
|
+
async mergeNodes(input) {
|
|
5981
|
+
if (input.sourceId === input.targetId) throw new Error("Cannot merge a knowledge node into itself");
|
|
5982
|
+
return this.#transaction(async (tx) => {
|
|
5983
|
+
const source = await this.#getNode(tx, input.sourceId);
|
|
5984
|
+
if (!source) throw new _mastra_core_storage.KnowledgeNotFoundError("node", input.sourceId);
|
|
5985
|
+
const target = await this.#resolveTerminalNode(tx, input.targetId);
|
|
5986
|
+
if (!target) throw new _mastra_core_storage.KnowledgeNotFoundError("node", input.targetId);
|
|
5987
|
+
if (target.id === source.id) throw new Error("Cannot create a knowledge merge cycle");
|
|
5988
|
+
if (!(0, _mastra_core_storage.isKnowledgeScopeVisible)(target.scope, source.scope)) throw new Error("Cannot merge a knowledge node into a target that is narrower than its source scope");
|
|
5989
|
+
const affected = await tx.execute({
|
|
5990
|
+
sql: `SELECT DISTINCT m.sourceType,m.sourceId,json(COALESCE(f.scope,r.scope)) AS scopeJson,CASE WHEN f.deletedAt IS NULL THEN 0 ELSE 1 END AS deleted FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS}" m LEFT JOIN "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" f ON m.sourceType='record' AND f.id=m.sourceId LEFT JOIN "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" r ON m.sourceType='node' AND r.id=m.sourceId WHERE m.recordId=?`,
|
|
5991
|
+
args: [source.id]
|
|
5992
|
+
});
|
|
5993
|
+
const movedKnowledge = await tx.execute({
|
|
5994
|
+
sql: `SELECT id,json(scope) AS scopeJson,deletedAt FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" WHERE node=?`,
|
|
5995
|
+
args: [source.id]
|
|
5996
|
+
});
|
|
5997
|
+
if ((await tx.execute({
|
|
5998
|
+
sql: `UPDATE "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" SET mergedInto=?,version=version+1,updatedAt=? WHERE id=? AND type='node' AND version=? AND mergedInto IS NULL`,
|
|
5999
|
+
args: [
|
|
6000
|
+
target.id,
|
|
6001
|
+
(/* @__PURE__ */ new Date()).toISOString(),
|
|
6002
|
+
source.id,
|
|
6003
|
+
input.sourceVersion
|
|
6004
|
+
]
|
|
6005
|
+
})).rowsAffected === 0) throw new _mastra_core_storage.KnowledgeConflictError(source.id);
|
|
6006
|
+
await tx.execute({
|
|
6007
|
+
sql: `UPDATE "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" SET node=? WHERE node=?`,
|
|
6008
|
+
args: [target.id, source.id]
|
|
6009
|
+
});
|
|
6010
|
+
await tx.execute({
|
|
6011
|
+
sql: `DELETE FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS}" WHERE recordId=? AND EXISTS (SELECT 1 FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS}" target WHERE target.sourceType="${_mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS}".sourceType AND target.sourceId="${_mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS}".sourceId AND target.recordId=?)`,
|
|
6012
|
+
args: [source.id, target.id]
|
|
6013
|
+
});
|
|
6014
|
+
await tx.execute({
|
|
6015
|
+
sql: `UPDATE "${_mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS}" SET recordId=? WHERE recordId=?`,
|
|
6016
|
+
args: [target.id, source.id]
|
|
6017
|
+
});
|
|
6018
|
+
for (const row of movedKnowledge.rows) await this.#outbox(tx, "record", String(row.id), row.deletedAt == null ? "upsert" : "delete", (0, _mastra_core_storage.createKnowledgeUlid)(), parseJson$3(row.scopeJson));
|
|
6019
|
+
for (const row of affected.rows) await this.#outbox(tx, String(row.sourceType), String(row.sourceId), Number(row.deleted) ? "delete" : "upsert", (0, _mastra_core_storage.createKnowledgeUlid)(), parseJson$3(row.scopeJson));
|
|
6020
|
+
await this.#activity(tx, "node-merged", "node", source.id, source.scope);
|
|
6021
|
+
await this.#outbox(tx, "node", source.id, "delete", input.sourceVersion + 1, source.scope);
|
|
6022
|
+
await this.#outbox(tx, "node", target.id, "upsert", (0, _mastra_core_storage.createKnowledgeUlid)(), target.scope);
|
|
6023
|
+
return target;
|
|
6024
|
+
});
|
|
6025
|
+
}
|
|
6026
|
+
async appendKnowledge(input) {
|
|
6027
|
+
const scope = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope);
|
|
6028
|
+
const resolutionScope = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.resolutionScope);
|
|
6029
|
+
const defaultScope = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.defaultScope);
|
|
6030
|
+
(0, _mastra_core_storage.assertKnowledgeScopeWithinCeiling)(scope, input.maxScope);
|
|
6031
|
+
return this.#transaction(async (tx) => {
|
|
6032
|
+
const node = nodeReferenceId(input.node);
|
|
6033
|
+
const parent = await this.#resolveTerminalNode(tx, node);
|
|
6034
|
+
if (!parent) throw new _mastra_core_storage.KnowledgeNotFoundError("node", node);
|
|
6035
|
+
const record = {
|
|
6036
|
+
id: input.id ?? (0, _mastra_core_storage.createKnowledgeUlid)(),
|
|
6037
|
+
node: parent.id,
|
|
6038
|
+
text: input.text,
|
|
6039
|
+
scope,
|
|
6040
|
+
sourceThreadId: input.sourceThreadId,
|
|
6041
|
+
capturedAt: /* @__PURE__ */ new Date(),
|
|
6042
|
+
when: input.when ? new Date(input.when) : void 0,
|
|
6043
|
+
maxScope: input.maxScope,
|
|
6044
|
+
metadata: input.metadata
|
|
6045
|
+
};
|
|
6046
|
+
await tx.execute({
|
|
6047
|
+
sql: `INSERT INTO "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" (id,node,text,scope,scopeKey,sourceThreadId,capturedAt,"when",maxScope,metadata,deletedAt,deletedBy) VALUES (?,?,?,jsonb(?),?,?,?,?,?,jsonb(?),NULL,NULL)`,
|
|
6048
|
+
args: [
|
|
6049
|
+
record.id,
|
|
6050
|
+
record.node,
|
|
6051
|
+
record.text,
|
|
6052
|
+
JSON.stringify(scope),
|
|
6053
|
+
(0, _mastra_core_storage.knowledgeScopeKey)(scope),
|
|
6054
|
+
record.sourceThreadId,
|
|
6055
|
+
record.capturedAt.toISOString(),
|
|
6056
|
+
record.when?.toISOString() ?? null,
|
|
6057
|
+
record.maxScope ?? null,
|
|
6058
|
+
record.metadata ? JSON.stringify(record.metadata) : null
|
|
6059
|
+
]
|
|
6060
|
+
});
|
|
6061
|
+
await this.#replaceMentions(tx, "record", record.id, record.text, resolutionScope, defaultScope);
|
|
6062
|
+
await this.#activity(tx, "record-created", "record", record.id, scope, record.sourceThreadId);
|
|
6063
|
+
await this.#outbox(tx, "record", record.id, "upsert", record.id, scope);
|
|
6064
|
+
return record;
|
|
6065
|
+
});
|
|
6066
|
+
}
|
|
6067
|
+
async getKnowledge(input) {
|
|
6068
|
+
const result = await this.#client.execute({
|
|
6069
|
+
sql: `SELECT *,json(scope) AS scopeJson,json(metadata) AS metadataJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" WHERE id=?${input.includeDeleted ? "" : " AND deletedAt IS NULL"}`,
|
|
6070
|
+
args: [input.id]
|
|
6071
|
+
});
|
|
6072
|
+
return result.rows[0] ? parseKnowledge(result.rows[0]) : null;
|
|
6073
|
+
}
|
|
6074
|
+
async listKnowledgeAbout(input) {
|
|
6075
|
+
return this.#queryKnowledge(input, "about");
|
|
6076
|
+
}
|
|
6077
|
+
async listKnowledgeMentioning(input) {
|
|
6078
|
+
return this.#queryKnowledge(input, "mentioning");
|
|
6079
|
+
}
|
|
6080
|
+
async listKnowledgeRelatedTo(input) {
|
|
6081
|
+
return this.#queryKnowledge(input, "related");
|
|
6082
|
+
}
|
|
6083
|
+
async knowledgeBySource(input) {
|
|
6084
|
+
const key = (0, _mastra_core_storage.knowledgeScopeKey)((0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope));
|
|
6085
|
+
const args = [
|
|
6086
|
+
input.sourceThreadId,
|
|
6087
|
+
key,
|
|
6088
|
+
key
|
|
6089
|
+
];
|
|
6090
|
+
if (input.after) args.push(input.after);
|
|
6091
|
+
const limit = input.limit ?? 100;
|
|
6092
|
+
args.push(limit + 1);
|
|
6093
|
+
const records = (await this.#client.execute({
|
|
6094
|
+
sql: `SELECT *,json(scope) AS scopeJson,json(metadata) AS metadataJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" WHERE sourceThreadId=? AND ${visibleSql}${input.includeDeleted ? "" : " AND deletedAt IS NULL"}${input.after ? " AND id > ?" : ""} ORDER BY id ASC LIMIT ?`,
|
|
6095
|
+
args
|
|
6096
|
+
})).rows.map(parseKnowledge);
|
|
6097
|
+
return {
|
|
6098
|
+
records: records.slice(0, limit),
|
|
6099
|
+
nextCursor: records.length > limit ? records[limit - 1]?.id : void 0
|
|
6100
|
+
};
|
|
6101
|
+
}
|
|
6102
|
+
async removeKnowledge(input) {
|
|
6103
|
+
return this.#transaction(async (tx) => {
|
|
6104
|
+
const record = await this.#getKnowledge(tx, input.id, true);
|
|
6105
|
+
if (!record) throw new _mastra_core_storage.KnowledgeNotFoundError("record", input.id);
|
|
6106
|
+
if (record.deletedAt) return record;
|
|
6107
|
+
const deletedAt = /* @__PURE__ */ new Date();
|
|
6108
|
+
await tx.execute({
|
|
6109
|
+
sql: `UPDATE "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" SET deletedAt=?,deletedBy=? WHERE id=? AND deletedAt IS NULL`,
|
|
6110
|
+
args: [
|
|
6111
|
+
deletedAt.toISOString(),
|
|
6112
|
+
input.deletedBy,
|
|
6113
|
+
input.id
|
|
6114
|
+
]
|
|
6115
|
+
});
|
|
6116
|
+
await this.#activity(tx, "record-deleted", "record", input.id, record.scope, record.sourceThreadId);
|
|
6117
|
+
await this.#outbox(tx, "record", input.id, "delete", deletedAt.toISOString(), record.scope);
|
|
6118
|
+
return {
|
|
6119
|
+
...record,
|
|
6120
|
+
deletedAt,
|
|
6121
|
+
deletedBy: input.deletedBy
|
|
6122
|
+
};
|
|
6123
|
+
});
|
|
6124
|
+
}
|
|
6125
|
+
async restoreKnowledge(input) {
|
|
6126
|
+
return this.#transaction(async (tx) => {
|
|
6127
|
+
const record = await this.#getKnowledge(tx, input.id, true);
|
|
6128
|
+
if (!record) throw new _mastra_core_storage.KnowledgeNotFoundError("record", input.id);
|
|
6129
|
+
if (!record.deletedAt) return record;
|
|
6130
|
+
await tx.execute({
|
|
6131
|
+
sql: `UPDATE "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" SET deletedAt=NULL,deletedBy=NULL WHERE id=?`,
|
|
6132
|
+
args: [input.id]
|
|
6133
|
+
});
|
|
6134
|
+
await this.#activity(tx, "record-restored", "record", input.id, record.scope, record.sourceThreadId);
|
|
6135
|
+
await this.#outbox(tx, "record", input.id, "upsert", (0, _mastra_core_storage.createKnowledgeUlid)(), record.scope);
|
|
6136
|
+
return {
|
|
6137
|
+
...record,
|
|
6138
|
+
deletedAt: void 0,
|
|
6139
|
+
deletedBy: void 0
|
|
6140
|
+
};
|
|
6141
|
+
});
|
|
6142
|
+
}
|
|
6143
|
+
async rescopeKnowledge(input) {
|
|
6144
|
+
const scope = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope);
|
|
6145
|
+
return this.#transaction(async (tx) => {
|
|
6146
|
+
const record = await this.#getKnowledge(tx, input.id, true);
|
|
6147
|
+
if (!record) throw new _mastra_core_storage.KnowledgeNotFoundError("record", input.id);
|
|
6148
|
+
(0, _mastra_core_storage.assertKnowledgeScopeWithinCeiling)(scope, record.maxScope);
|
|
6149
|
+
await tx.execute({
|
|
6150
|
+
sql: `UPDATE "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" SET scope=jsonb(?),scopeKey=? WHERE id=?`,
|
|
6151
|
+
args: [
|
|
6152
|
+
JSON.stringify(scope),
|
|
6153
|
+
(0, _mastra_core_storage.knowledgeScopeKey)(scope),
|
|
6154
|
+
input.id
|
|
6155
|
+
]
|
|
6156
|
+
});
|
|
6157
|
+
await this.#activity(tx, "record-rescoped", "record", input.id, scope, record.sourceThreadId);
|
|
6158
|
+
if ((0, _mastra_core_storage.knowledgeScopeKey)(record.scope) !== (0, _mastra_core_storage.knowledgeScopeKey)(scope)) await this.#outbox(tx, "record", input.id, "delete", (0, _mastra_core_storage.createKnowledgeUlid)(), record.scope);
|
|
6159
|
+
if (!record.deletedAt) await this.#outbox(tx, "record", input.id, "upsert", (0, _mastra_core_storage.createKnowledgeUlid)(), scope);
|
|
6160
|
+
return {
|
|
6161
|
+
...record,
|
|
6162
|
+
scope
|
|
6163
|
+
};
|
|
6164
|
+
});
|
|
6165
|
+
}
|
|
6166
|
+
async raiseKnowledgeCeiling(input) {
|
|
6167
|
+
return this.#transaction(async (tx) => {
|
|
6168
|
+
const record = await this.#getKnowledge(tx, input.id, true);
|
|
6169
|
+
if (!record) throw new _mastra_core_storage.KnowledgeNotFoundError("record", input.id);
|
|
6170
|
+
(0, _mastra_core_storage.assertKnowledgeScopeWithinCeiling)(record.scope, input.maxScope);
|
|
6171
|
+
(0, _mastra_core_storage.assertKnowledgeCeilingRaised)(record.maxScope, input.maxScope);
|
|
6172
|
+
await tx.execute({
|
|
6173
|
+
sql: `UPDATE "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" SET maxScope=? WHERE id=?`,
|
|
6174
|
+
args: [input.maxScope ?? null, input.id]
|
|
6175
|
+
});
|
|
6176
|
+
return {
|
|
6177
|
+
...record,
|
|
6178
|
+
maxScope: input.maxScope
|
|
6179
|
+
};
|
|
6180
|
+
});
|
|
6181
|
+
}
|
|
6182
|
+
async search(input) {
|
|
6183
|
+
const key = (0, _mastra_core_storage.knowledgeScopeKey)((0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope));
|
|
6184
|
+
const normalizedQuery = input.query.trim().toLocaleLowerCase();
|
|
6185
|
+
if (!normalizedQuery) return [];
|
|
6186
|
+
const query = `%${escapeLikePattern(normalizedQuery)}%`;
|
|
6187
|
+
const results = (await this.#client.execute({
|
|
6188
|
+
sql: `SELECT *,json(scope) AS scopeJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" WHERE mergedInto IS NULL AND ${visibleSql} AND (canonicalName LIKE ? ESCAPE '=' OR lower(COALESCE(kind,'')) LIKE ? ESCAPE '=' OR lower(COALESCE(content,'')) LIKE ? ESCAPE '=') ORDER BY updatedAt DESC LIMIT ?`,
|
|
6189
|
+
args: [
|
|
6190
|
+
key,
|
|
6191
|
+
key,
|
|
6192
|
+
query,
|
|
6193
|
+
query,
|
|
6194
|
+
query,
|
|
6195
|
+
input.limit ?? 20
|
|
6196
|
+
]
|
|
6197
|
+
})).rows.map((row) => ({
|
|
6198
|
+
type: String(row.type),
|
|
6199
|
+
id: String(row.id),
|
|
6200
|
+
recordId: String(row.id),
|
|
6201
|
+
name: String(row.name),
|
|
6202
|
+
text: row.content ? `${String(row.name)}\n${String(row.content)}` : String(row.name),
|
|
6203
|
+
scope: parseJson$3(row.scopeJson)
|
|
6204
|
+
}));
|
|
6205
|
+
if (results.length < (input.limit ?? 20)) {
|
|
6206
|
+
const knowledge = await this.#client.execute({
|
|
6207
|
+
sql: `SELECT k.*,json(k.scope) AS scopeJson,json(k.metadata) AS metadataJson,n.name FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" k JOIN "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" n ON n.id=k.node AND n.type='node' AND n.mergedInto IS NULL WHERE k.deletedAt IS NULL AND ${visibleSql.replaceAll("scopeKey", "k.scopeKey")} AND lower(k.text) LIKE ? ESCAPE '=' ORDER BY k.id DESC LIMIT ?`,
|
|
6208
|
+
args: [
|
|
6209
|
+
key,
|
|
6210
|
+
key,
|
|
6211
|
+
query,
|
|
6212
|
+
(input.limit ?? 20) - results.length
|
|
6213
|
+
]
|
|
6214
|
+
});
|
|
6215
|
+
results.push(...knowledge.rows.map((row) => ({
|
|
6216
|
+
type: "record",
|
|
6217
|
+
id: String(row.id),
|
|
6218
|
+
recordId: String(row.node),
|
|
6219
|
+
name: String(row.name),
|
|
6220
|
+
text: String(row.text),
|
|
6221
|
+
scope: parseJson$3(row.scopeJson)
|
|
6222
|
+
})));
|
|
6223
|
+
}
|
|
6224
|
+
return results;
|
|
6225
|
+
}
|
|
6226
|
+
async getCurationCursor(input) {
|
|
6227
|
+
const row = (await this.#client.execute({
|
|
6228
|
+
sql: `SELECT * FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_CURSORS}" WHERE sourceThreadId=? AND agent=?`,
|
|
6229
|
+
args: [input.sourceThreadId, input.agent]
|
|
6230
|
+
})).rows[0];
|
|
6231
|
+
return row ? {
|
|
6232
|
+
sourceThreadId: String(row.sourceThreadId),
|
|
6233
|
+
agent: String(row.agent),
|
|
6234
|
+
lastKnowledgeId: String(row.lastKnowledgeId),
|
|
6235
|
+
updatedAt: toDate(row.updatedAt)
|
|
6236
|
+
} : null;
|
|
6237
|
+
}
|
|
6238
|
+
async advanceCurationCursor(input) {
|
|
6239
|
+
const updatedAt = /* @__PURE__ */ new Date();
|
|
6240
|
+
if ((await this.#db.executeWriteOperationWithRetry(() => withClientWriteLock(this.#client, () => this.#client.execute({
|
|
6241
|
+
sql: `INSERT INTO "${_mastra_core_storage.TABLE_KNOWLEDGE_CURSORS}" (sourceThreadId,agent,lastKnowledgeId,updatedAt) VALUES (?,?,?,?) ON CONFLICT(sourceThreadId,agent) DO UPDATE SET lastKnowledgeId=excluded.lastKnowledgeId,updatedAt=excluded.updatedAt WHERE excluded.lastKnowledgeId >= "${_mastra_core_storage.TABLE_KNOWLEDGE_CURSORS}".lastKnowledgeId`,
|
|
6242
|
+
args: [
|
|
6243
|
+
input.sourceThreadId,
|
|
6244
|
+
input.agent,
|
|
6245
|
+
input.lastKnowledgeId,
|
|
6246
|
+
updatedAt.toISOString()
|
|
6247
|
+
]
|
|
6248
|
+
})), "advance knowledge curation cursor")).rowsAffected === 0) throw new Error("Knowledge curation cursor cannot move backwards");
|
|
6249
|
+
return {
|
|
6250
|
+
...input,
|
|
6251
|
+
updatedAt
|
|
6252
|
+
};
|
|
6253
|
+
}
|
|
6254
|
+
async listActivity(input) {
|
|
6255
|
+
const key = (0, _mastra_core_storage.knowledgeScopeKey)((0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope));
|
|
6256
|
+
return (await this.#client.execute({
|
|
6257
|
+
sql: `SELECT *,json(scope) AS scopeJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_ACTIVITY}" WHERE ${visibleSql}${input.after ? " AND id < ?" : ""} ORDER BY id DESC LIMIT ?`,
|
|
6258
|
+
args: [
|
|
6259
|
+
key,
|
|
6260
|
+
key,
|
|
6261
|
+
...input.after ? [input.after] : [],
|
|
6262
|
+
input.limit ?? 100
|
|
6263
|
+
]
|
|
6264
|
+
})).rows.map((row) => ({
|
|
6265
|
+
id: String(row.id),
|
|
6266
|
+
action: String(row.action),
|
|
6267
|
+
recordType: String(row.recordType),
|
|
6268
|
+
recordId: String(row.recordId),
|
|
6269
|
+
scope: parseJson$3(row.scopeJson),
|
|
6270
|
+
sourceThreadId: row.sourceThreadId == null ? void 0 : String(row.sourceThreadId),
|
|
6271
|
+
createdAt: toDate(row.createdAt)
|
|
6272
|
+
}));
|
|
6273
|
+
}
|
|
6274
|
+
async listSemanticOutbox(input = {}) {
|
|
6275
|
+
const clauses = [];
|
|
6276
|
+
const args = [];
|
|
6277
|
+
if (input.status) {
|
|
6278
|
+
clauses.push("status=?");
|
|
6279
|
+
args.push(input.status);
|
|
6280
|
+
}
|
|
6281
|
+
if (input.scope) {
|
|
6282
|
+
const key = (0, _mastra_core_storage.knowledgeScopeKey)((0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope));
|
|
6283
|
+
clauses.push(visibleSql);
|
|
6284
|
+
args.push(key, key);
|
|
6285
|
+
}
|
|
6286
|
+
args.push(input.limit ?? 100);
|
|
6287
|
+
return (await this.#client.execute({
|
|
6288
|
+
sql: `SELECT *,json(scope) AS scopeJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}"${clauses.length ? ` WHERE ${clauses.join(" AND ")}` : ""} ORDER BY createdAt ASC,id ASC LIMIT ?`,
|
|
6289
|
+
args
|
|
6290
|
+
})).rows.map(parseOutbox);
|
|
6291
|
+
}
|
|
6292
|
+
async claimSemanticOutbox(input) {
|
|
6293
|
+
const now = input.now ?? /* @__PURE__ */ new Date();
|
|
6294
|
+
const stale = new Date(now.getTime() - (input.claimTimeoutMs ?? 6e4));
|
|
6295
|
+
return this.#transaction(async (tx) => {
|
|
6296
|
+
const clauses = [
|
|
6297
|
+
`availableAt <= ?`,
|
|
6298
|
+
`(status='pending' OR (status='processing' AND claimedAt <= ?))`,
|
|
6299
|
+
`NOT EXISTS (SELECT 1 FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}" AS earlier WHERE earlier.documentId = "${_mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}".documentId AND earlier.status != 'completed' AND (earlier.createdAt < "${_mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}".createdAt OR (earlier.createdAt = "${_mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}".createdAt AND earlier.id < "${_mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}".id)))`
|
|
6300
|
+
];
|
|
6301
|
+
const args = [now.toISOString(), stale.toISOString()];
|
|
6302
|
+
if (input.scope) {
|
|
6303
|
+
const key = (0, _mastra_core_storage.knowledgeScopeKey)((0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope));
|
|
6304
|
+
clauses.push(visibleSql);
|
|
6305
|
+
args.push(key, key);
|
|
6306
|
+
}
|
|
6307
|
+
args.push(input.limit ?? 100);
|
|
6308
|
+
const ids = (await tx.execute({
|
|
6309
|
+
sql: `SELECT id FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}" WHERE ${clauses.join(" AND ")} ORDER BY createdAt ASC,id ASC LIMIT ?`,
|
|
6310
|
+
args
|
|
6311
|
+
})).rows.map((row) => String(row.id));
|
|
6312
|
+
for (const id of ids) await tx.execute({
|
|
6313
|
+
sql: `UPDATE "${_mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}" SET status='processing',attempts=attempts+1,claimedAt=?,claimedBy=? WHERE id=?`,
|
|
6314
|
+
args: [
|
|
6315
|
+
now.toISOString(),
|
|
6316
|
+
input.workerId,
|
|
6317
|
+
id
|
|
6318
|
+
]
|
|
6319
|
+
});
|
|
6320
|
+
if (!ids.length) return [];
|
|
6321
|
+
return (await tx.execute({
|
|
6322
|
+
sql: `SELECT *,json(scope) AS scopeJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}" WHERE id IN (${ids.map(() => "?").join(",")}) ORDER BY createdAt ASC,id ASC`,
|
|
6323
|
+
args: ids
|
|
6324
|
+
})).rows.map(parseOutbox);
|
|
6325
|
+
});
|
|
6326
|
+
}
|
|
6327
|
+
async completeSemanticOutbox(input) {
|
|
6328
|
+
if (!input.ids.length) return;
|
|
6329
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
6330
|
+
await this.#transaction(async (tx) => {
|
|
6331
|
+
for (const id of input.ids) await tx.execute({
|
|
6332
|
+
sql: `UPDATE "${_mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}" SET status='completed',completedAt=? WHERE id=? AND status='processing' AND claimedBy=?`,
|
|
6333
|
+
args: [
|
|
6334
|
+
now,
|
|
6335
|
+
id,
|
|
6336
|
+
input.workerId
|
|
6337
|
+
]
|
|
6338
|
+
});
|
|
6339
|
+
});
|
|
6340
|
+
}
|
|
6341
|
+
async releaseSemanticOutbox(input) {
|
|
6342
|
+
if (!input.ids.length) return;
|
|
6343
|
+
await this.#transaction(async (tx) => {
|
|
6344
|
+
for (const id of input.ids) await tx.execute({
|
|
6345
|
+
sql: `UPDATE "${_mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}" SET status='pending',availableAt=?,claimedAt=NULL,claimedBy=NULL WHERE id=? AND status='processing' AND claimedBy=?`,
|
|
6346
|
+
args: [
|
|
6347
|
+
(input.retryAt ?? /* @__PURE__ */ new Date()).toISOString(),
|
|
6348
|
+
id,
|
|
6349
|
+
input.workerId
|
|
6350
|
+
]
|
|
6351
|
+
});
|
|
6352
|
+
});
|
|
6353
|
+
}
|
|
6354
|
+
async #transaction(operation) {
|
|
6355
|
+
return this.#db.executeWriteOperationWithRetry(() => withClientWriteLock(this.#client, async () => {
|
|
6356
|
+
const tx = await this.#client.transaction("write");
|
|
6357
|
+
try {
|
|
6358
|
+
const result = await operation(tx);
|
|
6359
|
+
await tx.commit();
|
|
6360
|
+
return result;
|
|
6361
|
+
} catch (error) {
|
|
6362
|
+
if (!tx.closed) await tx.rollback();
|
|
6363
|
+
throw error;
|
|
6364
|
+
}
|
|
6365
|
+
}), "write knowledge state");
|
|
6366
|
+
}
|
|
6367
|
+
async #getNode(executor, id) {
|
|
6368
|
+
const result = await executor.execute({
|
|
6369
|
+
sql: `SELECT *,json(scope) AS scopeJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" WHERE id=? AND type='node'`,
|
|
6370
|
+
args: [id]
|
|
6371
|
+
});
|
|
6372
|
+
return result.rows[0] ? parseNode(result.rows[0]) : null;
|
|
6373
|
+
}
|
|
6374
|
+
async #getNodeByName(executor, name, scope) {
|
|
6375
|
+
const result = await executor.execute({
|
|
6376
|
+
sql: `SELECT *,json(scope) AS scopeJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" WHERE type='node' AND scopeKey=? AND canonicalName=?`,
|
|
6377
|
+
args: [(0, _mastra_core_storage.knowledgeScopeKey)(scope), canonicalName(name)]
|
|
6378
|
+
});
|
|
6379
|
+
return result.rows[0] ? parseNode(result.rows[0]) : null;
|
|
6380
|
+
}
|
|
6381
|
+
async #resolveNode(executor, name, scope) {
|
|
6382
|
+
for (let length = scope.length; length > 0; length--) {
|
|
6383
|
+
const node = await this.#getNodeByName(executor, name, scope.slice(0, length));
|
|
6384
|
+
if (node) {
|
|
6385
|
+
const terminal = await this.#resolveTerminalNode(executor, node.id);
|
|
6386
|
+
if (terminal && (0, _mastra_core_storage.isKnowledgeScopeVisible)(terminal.scope, scope)) return terminal;
|
|
6387
|
+
}
|
|
6388
|
+
}
|
|
6389
|
+
return null;
|
|
6390
|
+
}
|
|
6391
|
+
async #resolveTerminalNode(executor, id) {
|
|
6392
|
+
let node = await this.#getNode(executor, id);
|
|
6393
|
+
const seen = /* @__PURE__ */ new Set();
|
|
6394
|
+
while (node?.mergedInto) {
|
|
6395
|
+
if (seen.has(node.id)) throw new Error(`Knowledge merge cycle detected at ${node.id}`);
|
|
6396
|
+
seen.add(node.id);
|
|
6397
|
+
node = await this.#getNode(executor, node.mergedInto);
|
|
6398
|
+
}
|
|
6399
|
+
return node;
|
|
6400
|
+
}
|
|
6401
|
+
async #getKnowledge(executor, id, includeDeleted) {
|
|
6402
|
+
const result = await executor.execute({
|
|
6403
|
+
sql: `SELECT *,json(scope) AS scopeJson,json(metadata) AS metadataJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" WHERE id=?${includeDeleted ? "" : " AND deletedAt IS NULL"}`,
|
|
6404
|
+
args: [id]
|
|
6405
|
+
});
|
|
6406
|
+
return result.rows[0] ? parseKnowledge(result.rows[0]) : null;
|
|
6407
|
+
}
|
|
6408
|
+
async #queryKnowledge(input, relationship) {
|
|
6409
|
+
const scope = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope);
|
|
6410
|
+
const node = await this.#resolveTerminalNode(this.#client, nodeReferenceId(input.node));
|
|
6411
|
+
if (!node) return { records: [] };
|
|
6412
|
+
const key = (0, _mastra_core_storage.knowledgeScopeKey)(scope);
|
|
6413
|
+
const includesMentions = relationship !== "about";
|
|
6414
|
+
const relationSql = relationship === "about" ? "k.node=?" : relationship === "mentioning" ? "m.recordId=?" : "(k.node=? OR m.recordId=?)";
|
|
6415
|
+
const args = [
|
|
6416
|
+
node.id,
|
|
6417
|
+
...relationship === "related" ? [node.id] : [],
|
|
6418
|
+
key,
|
|
6419
|
+
key
|
|
6420
|
+
];
|
|
6421
|
+
if (input.after) args.push(input.after);
|
|
6422
|
+
args.push((input.limit ?? 100) + 1);
|
|
6423
|
+
const result = await this.#client.execute({
|
|
6424
|
+
sql: `SELECT DISTINCT k.*,json(k.scope) AS scopeJson,json(k.metadata) AS metadataJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" k${includesMentions ? ` LEFT JOIN "${_mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS}" m ON m.sourceType='record' AND m.sourceId=k.id` : ""} WHERE ${relationSql} AND ${visibleSql.replaceAll("scopeKey", "k.scopeKey")}${input.includeDeleted ? "" : " AND k.deletedAt IS NULL"}${input.after ? " AND k.id < ?" : ""} ORDER BY k.id DESC LIMIT ?`,
|
|
6425
|
+
args
|
|
6426
|
+
});
|
|
6427
|
+
const limit = input.limit ?? 100;
|
|
6428
|
+
const rows = result.rows.slice(0, limit);
|
|
6429
|
+
return {
|
|
6430
|
+
records: rows.map(parseKnowledge),
|
|
6431
|
+
nextCursor: result.rows.length > limit ? String(rows.at(-1)?.id) : void 0
|
|
6432
|
+
};
|
|
6433
|
+
}
|
|
6434
|
+
async #replaceMentions(tx, sourceType, sourceId, text, resolutionScope, defaultScope) {
|
|
6435
|
+
await tx.execute({
|
|
6436
|
+
sql: `DELETE FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS}" WHERE sourceType=? AND sourceId=?`,
|
|
6437
|
+
args: [sourceType, sourceId]
|
|
6438
|
+
});
|
|
6439
|
+
for (const name of (0, _mastra_core_storage.parseKnowledgeWikilinks)(text)) {
|
|
6440
|
+
let node = await this.#resolveNode(tx, name, resolutionScope);
|
|
6441
|
+
if (!node) {
|
|
6442
|
+
node = await this.#getNodeByName(tx, name, defaultScope);
|
|
6443
|
+
if (node) node = await this.#resolveTerminalNode(tx, node.id);
|
|
6444
|
+
if (!node) {
|
|
6445
|
+
const now = /* @__PURE__ */ new Date();
|
|
6446
|
+
node = {
|
|
6447
|
+
id: crypto.randomUUID(),
|
|
6448
|
+
type: "node",
|
|
6449
|
+
name,
|
|
6450
|
+
kind: "node",
|
|
6451
|
+
scope: defaultScope,
|
|
6452
|
+
version: 1,
|
|
6453
|
+
createdAt: now,
|
|
6454
|
+
updatedAt: now
|
|
6455
|
+
};
|
|
6456
|
+
await tx.execute({
|
|
6457
|
+
sql: `INSERT INTO "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" (id,type,name,canonicalName,kind,content,scope,scopeKey,version,mergedInto,createdAt,updatedAt) VALUES (?,?,?,?,?,NULL,jsonb(?),?,?,NULL,?,?)`,
|
|
6458
|
+
args: [
|
|
6459
|
+
node.id,
|
|
6460
|
+
"node",
|
|
6461
|
+
node.name,
|
|
6462
|
+
canonicalName(node.name),
|
|
6463
|
+
node.kind,
|
|
6464
|
+
JSON.stringify(defaultScope),
|
|
6465
|
+
(0, _mastra_core_storage.knowledgeScopeKey)(defaultScope),
|
|
6466
|
+
1,
|
|
6467
|
+
now.toISOString(),
|
|
6468
|
+
now.toISOString()
|
|
6469
|
+
]
|
|
6470
|
+
});
|
|
6471
|
+
await this.#activity(tx, "node-created", "node", node.id, defaultScope);
|
|
6472
|
+
await this.#outbox(tx, "node", node.id, "upsert", 1, defaultScope);
|
|
6473
|
+
}
|
|
6474
|
+
}
|
|
6475
|
+
await tx.execute({
|
|
6476
|
+
sql: `INSERT OR IGNORE INTO "${_mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS}" (sourceType,sourceId,recordId) VALUES (?,?,?)`,
|
|
6477
|
+
args: [
|
|
6478
|
+
sourceType,
|
|
6479
|
+
sourceId,
|
|
6480
|
+
node.id
|
|
6481
|
+
]
|
|
6482
|
+
});
|
|
6483
|
+
}
|
|
6484
|
+
}
|
|
6485
|
+
async #activity(executor, action, recordType, recordId, scope, sourceThreadId) {
|
|
6486
|
+
const now = /* @__PURE__ */ new Date();
|
|
6487
|
+
await executor.execute({
|
|
6488
|
+
sql: `INSERT INTO "${_mastra_core_storage.TABLE_KNOWLEDGE_ACTIVITY}" (id,action,recordType,recordId,scope,scopeKey,sourceThreadId,createdAt) VALUES (?,?,?,?,jsonb(?),?,?,?)`,
|
|
6489
|
+
args: [
|
|
6490
|
+
(0, _mastra_core_storage.createKnowledgeUlid)(),
|
|
6491
|
+
action,
|
|
6492
|
+
recordType,
|
|
6493
|
+
recordId,
|
|
6494
|
+
JSON.stringify(scope),
|
|
6495
|
+
(0, _mastra_core_storage.knowledgeScopeKey)(scope),
|
|
6496
|
+
sourceThreadId ?? null,
|
|
6497
|
+
now.toISOString()
|
|
6498
|
+
]
|
|
6499
|
+
});
|
|
6500
|
+
}
|
|
6501
|
+
async #outbox(executor, documentType, id, operation, version, scope) {
|
|
6502
|
+
const documentId = (0, _mastra_core_storage.knowledgeSemanticDocumentId)(documentType, id);
|
|
6503
|
+
const idempotencyKey = (0, _mastra_core_storage.knowledgeSemanticIdempotencyKey)(documentId, operation, version);
|
|
6504
|
+
const now = /* @__PURE__ */ new Date();
|
|
6505
|
+
await executor.execute({
|
|
6506
|
+
sql: `INSERT OR IGNORE INTO "${_mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}" (id,idempotencyKey,documentId,documentType,operation,scope,scopeKey,status,attempts,availableAt,claimedAt,claimedBy,createdAt,completedAt) VALUES (?,?,?,?,?,jsonb(?),?,'pending',0,?,NULL,NULL,?,NULL)`,
|
|
6507
|
+
args: [
|
|
6508
|
+
(0, _mastra_core_storage.createKnowledgeUlid)(),
|
|
6509
|
+
idempotencyKey,
|
|
6510
|
+
documentId,
|
|
6511
|
+
documentType,
|
|
6512
|
+
operation,
|
|
6513
|
+
JSON.stringify(scope),
|
|
6514
|
+
(0, _mastra_core_storage.knowledgeScopeKey)(scope),
|
|
6515
|
+
now.toISOString(),
|
|
6516
|
+
now.toISOString()
|
|
6517
|
+
]
|
|
6518
|
+
});
|
|
6519
|
+
}
|
|
6520
|
+
};
|
|
6521
|
+
//#endregion
|
|
5687
6522
|
//#region src/storage/domains/mcp-clients/index.ts
|
|
5688
6523
|
var MCPClientsLibSQL = class extends _mastra_core_storage.MCPClientsStorage {
|
|
5689
6524
|
#db;
|
|
@@ -13209,6 +14044,7 @@ var LibSQLStore = class extends _mastra_core_storage.MastraCompositeStore {
|
|
|
13209
14044
|
const workflows = new WorkflowsLibSQL(domainConfig);
|
|
13210
14045
|
const workflowDefinitions = new WorkflowDefinitionsLibSQL(domainConfig);
|
|
13211
14046
|
const memory = new MemoryLibSQL(domainConfig);
|
|
14047
|
+
const knowledge = new KnowledgeLibSQL(domainConfig);
|
|
13212
14048
|
const observability = new ObservabilityLibSQL(domainConfig);
|
|
13213
14049
|
const agents = new AgentsLibSQL(domainConfig);
|
|
13214
14050
|
const channels = new ChannelsLibSQL(domainConfig);
|
|
@@ -13233,6 +14069,7 @@ var LibSQLStore = class extends _mastra_core_storage.MastraCompositeStore {
|
|
|
13233
14069
|
workflows,
|
|
13234
14070
|
workflowDefinitions,
|
|
13235
14071
|
memory,
|
|
14072
|
+
knowledge,
|
|
13236
14073
|
observability,
|
|
13237
14074
|
agents,
|
|
13238
14075
|
channels,
|
|
@@ -13430,6 +14267,7 @@ exports.LibSQLStore = LibSQLStore;
|
|
|
13430
14267
|
exports.ExperimentsLibSQL = ExperimentsLibSQL;
|
|
13431
14268
|
exports.FavoritesLibSQL = FavoritesLibSQL;
|
|
13432
14269
|
exports.HarnessLibSQL = HarnessLibSQL;
|
|
14270
|
+
exports.KnowledgeLibSQL = KnowledgeLibSQL;
|
|
13433
14271
|
exports.LIBSQL_PROMPT = LIBSQL_PROMPT;
|
|
13434
14272
|
exports.LibSQLFactoryStorage = LibSQLFactoryStorage;
|
|
13435
14273
|
exports.LibSQLVector = LibSQLVector;
|