@mastra/libsql 1.28.0 → 1.29.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_types/@internal_core@0.1.4/dist/knowledge-compat/index.d.ts +348 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-studio-editor.md +12 -9
- package/dist/index.cjs +1463 -1822
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1464 -1823
- 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 +35 -85
- package/dist/storage/domains/knowledge/index.d.ts.map +1 -1
- package/dist/storage/domains/knowledge/pages.d.ts +162 -0
- package/dist/storage/domains/knowledge/pages.d.ts.map +1 -0
- package/dist/storage/domains/memory/index.d.ts +1 -1
- package/package.json +4 -4
- package/dist/_types/@internal_core/dist/knowledge-compat/index.d.ts +0 -592
- /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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createClient } from "@libsql/client";
|
|
2
2
|
import { ErrorCategory, ErrorDomain, MastraError } from "@mastra/core/error";
|
|
3
|
-
import { AGENTS_SCHEMA, AGENT_VERSIONS_SCHEMA, AgentsStorage, BackgroundTasksStorage, BlobStore, ChannelsStorage, DATASETS_SCHEMA, DATASET_ITEMS_SCHEMA, DATASET_VERSIONS_SCHEMA, DatasetsStorage, EXPERIMENTS_SCHEMA, EXPERIMENT_RESULTS_SCHEMA, ExperimentsStorage, FAVORITES_SCHEMA, FactoryStorage, FavoritesStorage, HarnessStorage,
|
|
3
|
+
import { AGENTS_SCHEMA, AGENT_VERSIONS_SCHEMA, AgentsStorage, BackgroundTasksStorage, BlobStore, ChannelsStorage, DATASETS_SCHEMA, DATASET_ITEMS_SCHEMA, DATASET_VERSIONS_SCHEMA, DatasetsStorage, EXPERIMENTS_SCHEMA, EXPERIMENT_RESULTS_SCHEMA, ExperimentsStorage, FAVORITES_SCHEMA, FactoryStorage, FavoritesStorage, HarnessStorage, KnowledgeConflictError, KnowledgeNotFoundError, KnowledgeSchemaResetRequiredError, KnowledgeStorage, MCPClientsStorage, MCPServersStorage, MCP_CLIENTS_SCHEMA, MCP_CLIENT_VERSIONS_SCHEMA, MCP_SERVERS_SCHEMA, MCP_SERVER_VERSIONS_SCHEMA, MastraCompositeStore, MemoryStorage, NotificationsStorage, OBSERVATIONAL_MEMORY_TABLE_SCHEMA, ObservabilityStorage, PROMPT_BLOCKS_SCHEMA, PROMPT_BLOCK_VERSIONS_SCHEMA, PromptBlocksStorage, RUN_FENCING_TABLE_SCHEMAS, RunFenceConflictError, SCORERS_SCHEMA, SCORER_DEFINITIONS_SCHEMA, SCORER_DEFINITION_VERSIONS_SCHEMA, SKILLS_SCHEMA, SKILL_BLOBS_SCHEMA, SKILL_VERSIONS_SCHEMA, SPAN_SCHEMA, SchedulesStorage, ScorerDefinitionsStorage, ScoresStorage, SkillsStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, TABLE_BACKGROUND_TASKS, TABLE_CHANNEL_CONFIG, TABLE_CHANNEL_INSTALLATIONS, TABLE_CONFIGS, TABLE_DATASETS, TABLE_DATASET_ITEMS, TABLE_DATASET_VERSIONS, TABLE_EXPERIMENTS, TABLE_EXPERIMENT_RESULTS, TABLE_FAVORITES, TABLE_HARNESS_SESSIONS, TABLE_MCP_CLIENTS, TABLE_MCP_CLIENT_VERSIONS, TABLE_MCP_SERVERS, TABLE_MCP_SERVER_VERSIONS, TABLE_MEMORY_RUN_FENCES, TABLE_MESSAGES, TABLE_NOTIFICATIONS, TABLE_PROMPT_BLOCKS, TABLE_PROMPT_BLOCK_VERSIONS, TABLE_RESOURCES, TABLE_SCHEDULES, TABLE_SCHEDULE_TRIGGERS, TABLE_SCHEMAS, TABLE_SCORERS, TABLE_SCORER_DEFINITIONS, TABLE_SCORER_DEFINITION_VERSIONS, TABLE_SKILLS, TABLE_SKILL_BLOBS, TABLE_SKILL_VERSIONS, TABLE_SPANS, TABLE_THREADS, TABLE_THREAD_STATE, TABLE_TOOL_PROVIDER_CONNECTIONS, TABLE_WORKFLOW_DEFINITIONS, TABLE_WORKFLOW_RUN_OWNERS, TABLE_WORKFLOW_SNAPSHOT, TABLE_WORKSPACES, TABLE_WORKSPACE_VERSIONS, THREAD_STATE_SCHEMA, TOOL_PROVIDER_CONNECTIONS_SCHEMA, ThreadStateStorage, ToolProviderConnectionsStorage, TraceStatus, UniqueViolationError, WORKSPACES_SCHEMA, WORKSPACE_VERSIONS_SCHEMA, WorkflowDefinitionsStorage, WorkflowsStorage, WorkspacesStorage, calculatePagination, createKnowledgeUlid, createStorageErrorId, createVectorErrorId, ensureDate, executeRetentionPrune, getSqlType, hasErrorCode, isRunFenceConflictError, listTracesArgsSchema, matchesExpectedWorkflowStatus, matchesRunFence, mergeWorkflowStepResult, normalizePerPage, normalizeScheduleTarget, resolveListOrderBy, resolveRetentionTargets, resolveRunFence, retentionCutoffMs, runRetentionBatches, safelyParseJSON, toTraceSpans, transformScoreRow, validateStorageMetadataFilter } from "@mastra/core/storage";
|
|
4
4
|
import { parseFieldKey, parseSqlIdentifier } from "@mastra/core/utils";
|
|
5
5
|
import { MastraVector, validateTopK, validateUpsertInput } from "@mastra/core/vector";
|
|
6
6
|
import { basename, dirname, isAbsolute, join, resolve } from "path";
|
|
@@ -8,7 +8,6 @@ import { AsyncLocalStorage } from "async_hooks";
|
|
|
8
8
|
import { BaseFilterTranslator } from "@mastra/core/vector/filter";
|
|
9
9
|
import { realpath } from "fs/promises";
|
|
10
10
|
import { MastraBase } from "@mastra/core/base";
|
|
11
|
-
import { randomUUID } from "crypto";
|
|
12
11
|
import { coreFeatures } from "@mastra/core/features";
|
|
13
12
|
import { MessageList } from "@mastra/core/agent";
|
|
14
13
|
import { saveScorePayloadSchema } from "@mastra/core/evals";
|
|
@@ -692,7 +691,7 @@ const FILTER_OPERATORS = {
|
|
|
692
691
|
transformValue: () => {
|
|
693
692
|
if (Array.isArray(value)) return [JSON.stringify(value)];
|
|
694
693
|
if (typeof value === "object" && value !== null) return [JSON.stringify(value)];
|
|
695
|
-
if (typeof value === "string") return [escapeLikePattern
|
|
694
|
+
if (typeof value === "string") return [escapeLikePattern(value)];
|
|
696
695
|
return [value];
|
|
697
696
|
}
|
|
698
697
|
};
|
|
@@ -709,7 +708,7 @@ const parseJsonPathKey = (key) => {
|
|
|
709
708
|
const getJsonPath = (key) => {
|
|
710
709
|
return `'$.${parseJsonPathKey(key)}'`;
|
|
711
710
|
};
|
|
712
|
-
function escapeLikePattern
|
|
711
|
+
function escapeLikePattern(str) {
|
|
713
712
|
return str.replace(/([%_\\])/g, "\\$1");
|
|
714
713
|
}
|
|
715
714
|
function buildFilterQuery(filter) {
|
|
@@ -2644,7 +2643,8 @@ var AgentsLibSQL = class extends AgentsStorage {
|
|
|
2644
2643
|
"skillsFormat",
|
|
2645
2644
|
"durable",
|
|
2646
2645
|
"browser",
|
|
2647
|
-
"toolProviders"
|
|
2646
|
+
"toolProviders",
|
|
2647
|
+
"modelList"
|
|
2648
2648
|
]
|
|
2649
2649
|
});
|
|
2650
2650
|
await this.#migrateToolsToJsonbFormat();
|
|
@@ -3061,8 +3061,9 @@ var AgentsLibSQL = class extends AgentsStorage {
|
|
|
3061
3061
|
versionNumber: input.versionNumber,
|
|
3062
3062
|
name: input.name ?? null,
|
|
3063
3063
|
description: input.description ?? null,
|
|
3064
|
-
instructions: this.serializeInstructions(input.instructions),
|
|
3065
|
-
model: input.model,
|
|
3064
|
+
instructions: this.serializeInstructions(input.instructions ?? ""),
|
|
3065
|
+
model: input.model ?? {},
|
|
3066
|
+
modelList: input.modelList ?? null,
|
|
3066
3067
|
tools: input.tools ?? null,
|
|
3067
3068
|
defaultOptions: input.defaultOptions ?? null,
|
|
3068
3069
|
workflows: input.workflows ?? null,
|
|
@@ -3319,6 +3320,7 @@ var AgentsLibSQL = class extends AgentsStorage {
|
|
|
3319
3320
|
description: row.description,
|
|
3320
3321
|
instructions: this.deserializeInstructions(row.instructions),
|
|
3321
3322
|
model: this.parseJson(row.model, "model"),
|
|
3323
|
+
modelList: this.parseJson(row.modelList, "modelList"),
|
|
3322
3324
|
tools: this.parseJson(row.tools, "tools"),
|
|
3323
3325
|
defaultOptions: this.parseJson(row.defaultOptions, "defaultOptions"),
|
|
3324
3326
|
workflows: this.parseJson(row.workflows, "workflows"),
|
|
@@ -3394,7 +3396,7 @@ function serializeJson(v) {
|
|
|
3394
3396
|
if (serialized === void 0) throw new Error("Failed to serialize background task value as JSON");
|
|
3395
3397
|
return serialized;
|
|
3396
3398
|
}
|
|
3397
|
-
function parseJson$
|
|
3399
|
+
function parseJson$3(val) {
|
|
3398
3400
|
if (val == null) return void 0;
|
|
3399
3401
|
if (typeof val === "string") try {
|
|
3400
3402
|
return JSON.parse(val);
|
|
@@ -3409,14 +3411,14 @@ function rowToTask(row) {
|
|
|
3409
3411
|
status: String(row.status),
|
|
3410
3412
|
toolName: String(row.tool_name),
|
|
3411
3413
|
toolCallId: String(row.tool_call_id),
|
|
3412
|
-
args: parseJson$
|
|
3414
|
+
args: parseJson$3(row.args) ?? {},
|
|
3413
3415
|
agentId: String(row.agent_id),
|
|
3414
3416
|
threadId: row.thread_id != null ? String(row.thread_id) : void 0,
|
|
3415
3417
|
resourceId: row.resource_id != null ? String(row.resource_id) : void 0,
|
|
3416
3418
|
runId: String(row.run_id),
|
|
3417
|
-
result: parseJson$
|
|
3418
|
-
error: parseJson$
|
|
3419
|
-
suspendPayload: parseJson$
|
|
3419
|
+
result: parseJson$3(row.result),
|
|
3420
|
+
error: parseJson$3(row.error),
|
|
3421
|
+
suspendPayload: parseJson$3(row.suspend_payload),
|
|
3420
3422
|
retryCount: Number(row.retry_count),
|
|
3421
3423
|
maxRetries: Number(row.max_retries),
|
|
3422
3424
|
timeoutMs: Number(row.timeout_ms),
|
|
@@ -3427,7 +3429,7 @@ function rowToTask(row) {
|
|
|
3427
3429
|
ownerId: row.ownerId != null ? String(row.ownerId) : void 0,
|
|
3428
3430
|
leaseExpiresAt: row.leaseExpiresAt ? new Date(String(row.leaseExpiresAt)) : void 0,
|
|
3429
3431
|
expiresAt: row.expiresAt ? new Date(String(row.expiresAt)) : void 0,
|
|
3430
|
-
metadata: parseJson$
|
|
3432
|
+
metadata: parseJson$3(row.metadata)
|
|
3431
3433
|
};
|
|
3432
3434
|
}
|
|
3433
3435
|
var BackgroundTasksLibSQL = class BackgroundTasksLibSQL extends BackgroundTasksStorage {
|
|
@@ -6394,10 +6396,10 @@ var FavoritesLibSQL = class extends FavoritesStorage {
|
|
|
6394
6396
|
}
|
|
6395
6397
|
};
|
|
6396
6398
|
// src/storage/domains/harness/index.ts
|
|
6397
|
-
const toDate
|
|
6399
|
+
const toDate = (value) => new Date(value);
|
|
6398
6400
|
const toOptionalDate = (value) => {
|
|
6399
6401
|
if (value == null) return void 0;
|
|
6400
|
-
return toDate
|
|
6402
|
+
return toDate(value);
|
|
6401
6403
|
};
|
|
6402
6404
|
function cloneJson(value) {
|
|
6403
6405
|
return value === void 0 ? void 0 : structuredClone(value);
|
|
@@ -6408,8 +6410,8 @@ function hydratePendingItem(item) {
|
|
|
6408
6410
|
kind: item.kind,
|
|
6409
6411
|
status: item.status,
|
|
6410
6412
|
sessionId: item.sessionId,
|
|
6411
|
-
createdAt: toDate
|
|
6412
|
-
updatedAt: toDate
|
|
6413
|
+
createdAt: toDate(item.createdAt),
|
|
6414
|
+
updatedAt: toDate(item.updatedAt)
|
|
6413
6415
|
};
|
|
6414
6416
|
if (item.runId !== void 0) record.runId = item.runId;
|
|
6415
6417
|
if (item.traceId !== void 0) record.traceId = item.traceId;
|
|
@@ -6427,8 +6429,8 @@ function rowToSession(row) {
|
|
|
6427
6429
|
origin: row.origin,
|
|
6428
6430
|
modeId: row.modeId,
|
|
6429
6431
|
modelId: row.modelId,
|
|
6430
|
-
createdAt: toDate
|
|
6431
|
-
lastActivityAt: toDate
|
|
6432
|
+
createdAt: toDate(row.createdAt),
|
|
6433
|
+
lastActivityAt: toDate(row.lastActivityAt)
|
|
6432
6434
|
};
|
|
6433
6435
|
if (row.parentSessionId != null) record.parentSessionId = row.parentSessionId;
|
|
6434
6436
|
if (row.subagentDepth != null) record.subagentDepth = row.subagentDepth;
|
|
@@ -6537,1973 +6539,1612 @@ var HarnessLibSQL = class HarnessLibSQL extends HarnessStorage {
|
|
|
6537
6539
|
})).map(rowToSession);
|
|
6538
6540
|
}
|
|
6539
6541
|
};
|
|
6540
|
-
// ../../packages/_internal-core/dist/knowledge-compat/index.js
|
|
6541
|
-
const KNOWLEDGE_V2_CORE_FEATURE = "knowledge-v2";
|
|
6542
|
-
/** Hard cap on scope nodes returned by one `listScopeNodes` read. */
|
|
6543
|
-
const MAX_KNOWLEDGE_SCOPE_NODES = 1e3;
|
|
6544
|
-
function knowledgeScopeNodeFilterKey(input) {
|
|
6545
|
-
return JSON.stringify([
|
|
6546
|
-
input.withinAddress ?? null,
|
|
6547
|
-
input.addresses ? [...input.addresses].sort() : null,
|
|
6548
|
-
input.ids ? [...input.ids].sort() : null
|
|
6549
|
-
]);
|
|
6550
|
-
}
|
|
6551
|
-
function createKnowledgeScopeNodeCursor(scope, input) {
|
|
6552
|
-
return encodeURIComponent(JSON.stringify({
|
|
6553
|
-
version: 1,
|
|
6554
|
-
type: "scope",
|
|
6555
|
-
name: scope.name,
|
|
6556
|
-
id: scope.id,
|
|
6557
|
-
filter: knowledgeScopeNodeFilterKey(input)
|
|
6558
|
-
}));
|
|
6559
|
-
}
|
|
6560
|
-
/** Validates the page size and cursor of a `listScopeNodes` query; throws on a cursor from a different query. */
|
|
6561
|
-
function parseListKnowledgeScopeNodesInput(input = {}) {
|
|
6562
|
-
const requested = Number.isFinite(input.limit) ? Math.trunc(input.limit) : MAX_KNOWLEDGE_SCOPE_NODES;
|
|
6563
|
-
const limit = Math.min(Math.max(requested, 1), MAX_KNOWLEDGE_SCOPE_NODES);
|
|
6564
|
-
if (!input.cursor) return {
|
|
6565
|
-
limit,
|
|
6566
|
-
after: null
|
|
6567
|
-
};
|
|
6568
|
-
let value;
|
|
6569
|
-
try {
|
|
6570
|
-
value = JSON.parse(decodeURIComponent(input.cursor));
|
|
6571
|
-
} catch {
|
|
6572
|
-
throw new Error("Invalid Knowledge scope node cursor.");
|
|
6573
|
-
}
|
|
6574
|
-
const parsed = value && typeof value === "object" ? value : {};
|
|
6575
|
-
if (parsed.version !== 1 || parsed.type !== "scope" || typeof parsed.name !== "string" || typeof parsed.id !== "string" || parsed.filter !== knowledgeScopeNodeFilterKey(input)) throw new Error("Knowledge scope node cursor does not match this query.");
|
|
6576
|
-
return {
|
|
6577
|
-
limit,
|
|
6578
|
-
after: {
|
|
6579
|
-
name: parsed.name,
|
|
6580
|
-
id: parsed.id
|
|
6581
|
-
}
|
|
6582
|
-
};
|
|
6583
|
-
}
|
|
6584
|
-
/** Builds a page from up to `limit + 1` name/id-ordered scope nodes. */
|
|
6585
|
-
function pageKnowledgeScopeNodes(rows, limit, input) {
|
|
6586
|
-
const scopes = rows.slice(0, limit);
|
|
6587
|
-
const last = scopes.at(-1);
|
|
6588
|
-
return {
|
|
6589
|
-
scopes,
|
|
6590
|
-
nextCursor: rows.length > limit && last ? createKnowledgeScopeNodeCursor(last, input) : null
|
|
6591
|
-
};
|
|
6592
|
-
}
|
|
6593
|
-
/** Clamps a requested member limit to 1–{@link MAX_KNOWLEDGE_SCOPE_MEMBERS_LIMIT}. */
|
|
6594
|
-
function knowledgeScopeMembersLimit(limit) {
|
|
6595
|
-
if (limit === void 0 || !Number.isFinite(limit)) return 500;
|
|
6596
|
-
return Math.min(Math.max(Math.trunc(limit), 1), 500);
|
|
6597
|
-
}
|
|
6598
|
-
/** Validates the page size and cursor of a `listScopeMembers` query; throws on a cursor from another scope. */
|
|
6599
|
-
function parseListKnowledgeScopeMembersInput(input) {
|
|
6600
|
-
const limit = knowledgeScopeMembersLimit(input.limit);
|
|
6601
|
-
if (!input.cursor) return {
|
|
6602
|
-
limit,
|
|
6603
|
-
after: null
|
|
6604
|
-
};
|
|
6605
|
-
let value;
|
|
6606
|
-
try {
|
|
6607
|
-
value = JSON.parse(decodeURIComponent(input.cursor));
|
|
6608
|
-
} catch {
|
|
6609
|
-
throw new Error("Invalid Knowledge scope member cursor.");
|
|
6610
|
-
}
|
|
6611
|
-
const parsed = value && typeof value === "object" ? value : {};
|
|
6612
|
-
const updatedAt = typeof parsed.updatedAt === "string" ? new Date(parsed.updatedAt) : /* @__PURE__ */ new Date(NaN);
|
|
6613
|
-
if (parsed.version !== 1 || parsed.type !== "scope-member" || parsed.scopeNodeId !== input.scopeNodeId || typeof parsed.name !== "string" || typeof parsed.id !== "string" || Number.isNaN(updatedAt.getTime())) throw new Error("Knowledge scope member cursor does not match this query.");
|
|
6614
|
-
return {
|
|
6615
|
-
limit,
|
|
6616
|
-
after: {
|
|
6617
|
-
updatedAt,
|
|
6618
|
-
name: parsed.name,
|
|
6619
|
-
id: parsed.id
|
|
6620
|
-
}
|
|
6621
|
-
};
|
|
6622
|
-
}
|
|
6623
|
-
/** Builds a page from up to `limit + 1` members in `listScopeMembers` order. */
|
|
6624
|
-
function pageKnowledgeScopeMembers(rows, limit, input) {
|
|
6625
|
-
const members = rows.slice(0, limit);
|
|
6626
|
-
const last = members.at(-1);
|
|
6627
|
-
const hasMore = rows.length > limit;
|
|
6628
|
-
return {
|
|
6629
|
-
members,
|
|
6630
|
-
hasMore,
|
|
6631
|
-
nextCursor: hasMore && last ? encodeURIComponent(JSON.stringify({
|
|
6632
|
-
version: 1,
|
|
6633
|
-
type: "scope-member",
|
|
6634
|
-
scopeNodeId: input.scopeNodeId,
|
|
6635
|
-
updatedAt: last.updatedAt.toISOString(),
|
|
6636
|
-
name: last.name,
|
|
6637
|
-
id: last.id
|
|
6638
|
-
})) : null
|
|
6639
|
-
};
|
|
6640
|
-
}
|
|
6641
|
-
const TABLE_KNOWLEDGE_NODES$1 = "mastra_knowledge_nodes";
|
|
6642
|
-
const TABLE_KNOWLEDGE_RECORDS$1 = "mastra_knowledge_records";
|
|
6643
|
-
const TABLE_KNOWLEDGE_MENTIONS$1 = "mastra_knowledge_mentions";
|
|
6644
|
-
const TABLE_KNOWLEDGE_ACTIVITY$1 = "mastra_knowledge_activity";
|
|
6645
|
-
const TABLE_KNOWLEDGE_SEMANTIC_OUTBOX$1 = "mastra_knowledge_semantic_outbox";
|
|
6646
|
-
const TABLE_KNOWLEDGE_NODE_SCOPES = "mastra_knowledge_node_scopes";
|
|
6647
|
-
const TABLE_KNOWLEDGE_RECORD_SCOPES = "mastra_knowledge_record_scopes";
|
|
6648
|
-
const TABLE_KNOWLEDGE_SCOPE_GRANTS = "mastra_knowledge_scope_grants";
|
|
6649
|
-
const TABLE_KNOWLEDGE_ACCESS_STATE = "mastra_knowledge_access_state";
|
|
6650
|
-
const TABLE_KNOWLEDGE_SCOPE_ADDRESSES = "mastra_knowledge_scope_addresses";
|
|
6651
|
-
const TABLE_KNOWLEDGE_NODE_ADDRESSES = "mastra_knowledge_node_addresses";
|
|
6652
|
-
const TABLE_KNOWLEDGE_IMPORT_STATE = "mastra_knowledge_import_state";
|
|
6653
|
-
const TABLE_KNOWLEDGE_IMPORT_RUNS = "mastra_knowledge_import_runs";
|
|
6654
|
-
const TABLE_KNOWLEDGE_PROPOSALS = "mastra_knowledge_proposals";
|
|
6655
6542
|
const KNOWLEDGE_TABLE_NAMES = [
|
|
6656
|
-
|
|
6657
|
-
|
|
6658
|
-
|
|
6659
|
-
|
|
6660
|
-
|
|
6661
|
-
|
|
6662
|
-
|
|
6663
|
-
|
|
6664
|
-
|
|
6665
|
-
|
|
6666
|
-
|
|
6667
|
-
|
|
6668
|
-
|
|
6669
|
-
|
|
6543
|
+
"mastra_knowledge_nodes",
|
|
6544
|
+
"mastra_knowledge_records",
|
|
6545
|
+
"mastra_knowledge_mentions",
|
|
6546
|
+
"mastra_knowledge_activity",
|
|
6547
|
+
"mastra_knowledge_semantic_outbox",
|
|
6548
|
+
"mastra_knowledge_node_scopes",
|
|
6549
|
+
"mastra_knowledge_record_scopes",
|
|
6550
|
+
"mastra_knowledge_scope_grants",
|
|
6551
|
+
"mastra_knowledge_access_state",
|
|
6552
|
+
"mastra_knowledge_scope_addresses",
|
|
6553
|
+
"mastra_knowledge_node_addresses",
|
|
6554
|
+
"mastra_knowledge_import_state",
|
|
6555
|
+
"mastra_knowledge_import_runs",
|
|
6556
|
+
"mastra_knowledge_proposals"
|
|
6670
6557
|
];
|
|
6671
6558
|
/** Knowledge v1 tables with no v2 equivalent; an explicit schema reset drops them. */
|
|
6672
6559
|
const RETIRED_KNOWLEDGE_TABLE_NAMES = ["mastra_knowledge_cursors"];
|
|
6673
|
-
|
|
6674
|
-
const
|
|
6675
|
-
|
|
6676
|
-
|
|
6677
|
-
|
|
6678
|
-
|
|
6679
|
-
|
|
6560
|
+
const KNOWLEDGE_PAGES_CORE_FEATURE = "knowledge-pages";
|
|
6561
|
+
const TABLE_KNOWLEDGE_PAGES = "mastra_knowledge_pages";
|
|
6562
|
+
const TABLE_KNOWLEDGE_NOTES = "mastra_knowledge_notes";
|
|
6563
|
+
const TABLE_KNOWLEDGE_NOTE_STAMPS = "mastra_knowledge_note_stamps";
|
|
6564
|
+
/** Same name as the schema-2 table; schema 4 re-keys it to note → page. */
|
|
6565
|
+
const TABLE_KNOWLEDGE_PAGE_MENTIONS = "mastra_knowledge_mentions";
|
|
6566
|
+
const TABLE_KNOWLEDGE_PINS = "mastra_knowledge_pins";
|
|
6567
|
+
/** Same name as schema 2; schema 4 re-keys it to scope/page/note. */
|
|
6568
|
+
const TABLE_KNOWLEDGE_PAGE_ACTIVITY = "mastra_knowledge_activity";
|
|
6569
|
+
/** Holds `{ id: 'knowledge', schemaVersion: 4 }`, written when the schema-4 tables are created. */
|
|
6570
|
+
const TABLE_KNOWLEDGE_PAGES_STATE = "mastra_knowledge_access_state";
|
|
6571
|
+
const KNOWLEDGE_PAGES_TABLE_NAMES = [
|
|
6572
|
+
TABLE_KNOWLEDGE_PAGES,
|
|
6573
|
+
TABLE_KNOWLEDGE_NOTES,
|
|
6574
|
+
TABLE_KNOWLEDGE_NOTE_STAMPS,
|
|
6575
|
+
TABLE_KNOWLEDGE_PAGE_MENTIONS,
|
|
6576
|
+
TABLE_KNOWLEDGE_PINS,
|
|
6577
|
+
TABLE_KNOWLEDGE_PAGE_ACTIVITY,
|
|
6578
|
+
TABLE_KNOWLEDGE_PAGES_STATE
|
|
6579
|
+
];
|
|
6580
|
+
/** Every Knowledge table any schema version created. Reset drops these and nothing else. */
|
|
6581
|
+
const KNOWLEDGE_RESETTABLE_TABLE_NAMES = [.../* @__PURE__ */ new Set([
|
|
6582
|
+
...KNOWLEDGE_PAGES_TABLE_NAMES,
|
|
6583
|
+
...KNOWLEDGE_TABLE_NAMES,
|
|
6680
6584
|
...RETIRED_KNOWLEDGE_TABLE_NAMES
|
|
6681
|
-
]);
|
|
6682
|
-
|
|
6683
|
-
|
|
6684
|
-
|
|
6685
|
-
|
|
6686
|
-
|
|
6687
|
-
|
|
6688
|
-
|
|
6689
|
-
|
|
6690
|
-
|
|
6691
|
-
|
|
6692
|
-
|
|
6693
|
-
|
|
6694
|
-
|
|
6695
|
-
|
|
6696
|
-
|
|
6697
|
-
|
|
6698
|
-
|
|
6699
|
-
|
|
6700
|
-
|
|
6701
|
-
|
|
6702
|
-
|
|
6703
|
-
|
|
6704
|
-
|
|
6705
|
-
|
|
6706
|
-
|
|
6707
|
-
|
|
6708
|
-
|
|
6709
|
-
|
|
6710
|
-
metadata: {
|
|
6711
|
-
type: "jsonb",
|
|
6712
|
-
nullable: true
|
|
6713
|
-
},
|
|
6714
|
-
version: {
|
|
6715
|
-
type: "integer",
|
|
6716
|
-
nullable: false
|
|
6717
|
-
},
|
|
6718
|
-
createdAt: {
|
|
6719
|
-
type: "timestamp",
|
|
6720
|
-
nullable: false
|
|
6721
|
-
},
|
|
6722
|
-
updatedAt: {
|
|
6723
|
-
type: "timestamp",
|
|
6724
|
-
nullable: false
|
|
6725
|
-
},
|
|
6726
|
-
deletedAt: {
|
|
6727
|
-
type: "timestamp",
|
|
6728
|
-
nullable: true
|
|
6729
|
-
},
|
|
6730
|
-
deletedBy: {
|
|
6731
|
-
type: "text",
|
|
6732
|
-
nullable: true
|
|
6733
|
-
},
|
|
6734
|
-
type: {
|
|
6735
|
-
type: "text",
|
|
6736
|
-
nullable: true
|
|
6737
|
-
},
|
|
6738
|
-
canonicalName: {
|
|
6739
|
-
type: "text",
|
|
6740
|
-
nullable: true
|
|
6741
|
-
},
|
|
6742
|
-
content: {
|
|
6743
|
-
type: "text",
|
|
6744
|
-
nullable: true
|
|
6745
|
-
},
|
|
6746
|
-
description: {
|
|
6747
|
-
type: "text",
|
|
6748
|
-
nullable: true
|
|
6749
|
-
},
|
|
6750
|
-
scope: {
|
|
6751
|
-
type: "jsonb",
|
|
6752
|
-
nullable: true
|
|
6753
|
-
},
|
|
6754
|
-
scopeKey: {
|
|
6755
|
-
type: "text",
|
|
6756
|
-
nullable: true
|
|
6757
|
-
},
|
|
6758
|
-
mergedInto: {
|
|
6759
|
-
type: "text",
|
|
6760
|
-
nullable: true
|
|
6761
|
-
}
|
|
6762
|
-
};
|
|
6763
|
-
const KNOWLEDGE_V2_RECORDS_SCHEMA = {
|
|
6764
|
-
id: {
|
|
6765
|
-
type: "text",
|
|
6766
|
-
nullable: false,
|
|
6767
|
-
primaryKey: true
|
|
6768
|
-
},
|
|
6769
|
-
node: {
|
|
6770
|
-
type: "text",
|
|
6771
|
-
nullable: false,
|
|
6772
|
-
references: {
|
|
6773
|
-
table: TABLE_KNOWLEDGE_NODES$1,
|
|
6774
|
-
column: "id"
|
|
6775
|
-
}
|
|
6776
|
-
},
|
|
6777
|
-
text: {
|
|
6778
|
-
type: "text",
|
|
6779
|
-
nullable: false
|
|
6780
|
-
},
|
|
6781
|
-
metadata: {
|
|
6782
|
-
type: "jsonb",
|
|
6783
|
-
nullable: true
|
|
6784
|
-
},
|
|
6785
|
-
version: {
|
|
6786
|
-
type: "integer",
|
|
6787
|
-
nullable: false
|
|
6788
|
-
},
|
|
6789
|
-
capturedAt: {
|
|
6790
|
-
type: "timestamp",
|
|
6791
|
-
nullable: false
|
|
6792
|
-
},
|
|
6793
|
-
updatedAt: {
|
|
6794
|
-
type: "timestamp",
|
|
6795
|
-
nullable: false
|
|
6796
|
-
},
|
|
6797
|
-
deletedAt: {
|
|
6798
|
-
type: "timestamp",
|
|
6799
|
-
nullable: true
|
|
6800
|
-
},
|
|
6801
|
-
deletedBy: {
|
|
6802
|
-
type: "text",
|
|
6803
|
-
nullable: true
|
|
6804
|
-
},
|
|
6805
|
-
scope: {
|
|
6806
|
-
type: "jsonb",
|
|
6807
|
-
nullable: true
|
|
6808
|
-
},
|
|
6809
|
-
scopeKey: {
|
|
6810
|
-
type: "text",
|
|
6811
|
-
nullable: true
|
|
6812
|
-
},
|
|
6813
|
-
sourceThreadId: {
|
|
6814
|
-
type: "text",
|
|
6815
|
-
nullable: true
|
|
6816
|
-
},
|
|
6817
|
-
when: {
|
|
6818
|
-
type: "timestamp",
|
|
6819
|
-
nullable: true
|
|
6820
|
-
}
|
|
6821
|
-
};
|
|
6822
|
-
const KNOWLEDGE_V2_MENTIONS_SCHEMA = {
|
|
6823
|
-
recordId: {
|
|
6824
|
-
type: "text",
|
|
6825
|
-
nullable: false,
|
|
6826
|
-
references: {
|
|
6827
|
-
table: TABLE_KNOWLEDGE_RECORDS$1,
|
|
6828
|
-
column: "id"
|
|
6829
|
-
}
|
|
6830
|
-
},
|
|
6831
|
-
targetNodeId: {
|
|
6832
|
-
type: "text",
|
|
6833
|
-
nullable: true,
|
|
6834
|
-
references: {
|
|
6835
|
-
table: TABLE_KNOWLEDGE_NODES$1,
|
|
6836
|
-
column: "id"
|
|
6837
|
-
}
|
|
6838
|
-
},
|
|
6839
|
-
sourceType: {
|
|
6840
|
-
type: "text",
|
|
6841
|
-
nullable: true
|
|
6842
|
-
},
|
|
6843
|
-
sourceId: {
|
|
6844
|
-
type: "text",
|
|
6845
|
-
nullable: true
|
|
6846
|
-
}
|
|
6847
|
-
};
|
|
6848
|
-
const KNOWLEDGE_NODE_SCOPES_SCHEMA = {
|
|
6849
|
-
nodeId: {
|
|
6850
|
-
type: "text",
|
|
6851
|
-
nullable: false,
|
|
6852
|
-
references: {
|
|
6853
|
-
table: TABLE_KNOWLEDGE_NODES$1,
|
|
6854
|
-
column: "id"
|
|
6855
|
-
}
|
|
6856
|
-
},
|
|
6857
|
-
scopeNodeId: {
|
|
6858
|
-
type: "text",
|
|
6859
|
-
nullable: false,
|
|
6860
|
-
references: {
|
|
6861
|
-
table: TABLE_KNOWLEDGE_NODES$1,
|
|
6862
|
-
column: "id"
|
|
6863
|
-
}
|
|
6864
|
-
},
|
|
6865
|
-
addedAt: {
|
|
6866
|
-
type: "timestamp",
|
|
6867
|
-
nullable: false
|
|
6868
|
-
}
|
|
6869
|
-
};
|
|
6870
|
-
const KNOWLEDGE_RECORD_SCOPES_SCHEMA = {
|
|
6871
|
-
recordId: {
|
|
6872
|
-
type: "text",
|
|
6873
|
-
nullable: false,
|
|
6874
|
-
references: {
|
|
6875
|
-
table: TABLE_KNOWLEDGE_RECORDS$1,
|
|
6876
|
-
column: "id"
|
|
6877
|
-
}
|
|
6878
|
-
},
|
|
6879
|
-
scopeNodeId: {
|
|
6880
|
-
type: "text",
|
|
6881
|
-
nullable: false,
|
|
6882
|
-
references: {
|
|
6883
|
-
table: TABLE_KNOWLEDGE_NODES$1,
|
|
6884
|
-
column: "id"
|
|
6885
|
-
}
|
|
6886
|
-
},
|
|
6887
|
-
addedAt: {
|
|
6888
|
-
type: "timestamp",
|
|
6889
|
-
nullable: false
|
|
6890
|
-
}
|
|
6891
|
-
};
|
|
6892
|
-
const KNOWLEDGE_SCOPE_GRANTS_SCHEMA = {
|
|
6893
|
-
scopeNodeId: {
|
|
6894
|
-
type: "text",
|
|
6895
|
-
nullable: false,
|
|
6896
|
-
references: {
|
|
6897
|
-
table: TABLE_KNOWLEDGE_NODES$1,
|
|
6898
|
-
column: "id"
|
|
6899
|
-
}
|
|
6900
|
-
},
|
|
6901
|
-
scopeRefId: {
|
|
6902
|
-
type: "text",
|
|
6903
|
-
nullable: false,
|
|
6904
|
-
references: {
|
|
6905
|
-
table: TABLE_KNOWLEDGE_NODES$1,
|
|
6906
|
-
column: "id"
|
|
6907
|
-
}
|
|
6908
|
-
},
|
|
6909
|
-
role: {
|
|
6910
|
-
type: "text",
|
|
6911
|
-
nullable: false
|
|
6912
|
-
},
|
|
6913
|
-
canSuggest: {
|
|
6914
|
-
type: "boolean",
|
|
6915
|
-
nullable: true
|
|
6916
|
-
}
|
|
6917
|
-
};
|
|
6918
|
-
const KNOWLEDGE_ACCESS_STATE_SCHEMA = {
|
|
6919
|
-
id: {
|
|
6920
|
-
type: "text",
|
|
6921
|
-
nullable: false,
|
|
6922
|
-
primaryKey: true
|
|
6923
|
-
},
|
|
6924
|
-
epoch: {
|
|
6925
|
-
type: "integer",
|
|
6926
|
-
nullable: false
|
|
6927
|
-
},
|
|
6928
|
-
schemaVersion: {
|
|
6929
|
-
type: "integer",
|
|
6930
|
-
nullable: false
|
|
6931
|
-
}
|
|
6932
|
-
};
|
|
6933
|
-
const KNOWLEDGE_SCOPE_ADDRESSES_SCHEMA = {
|
|
6934
|
-
address: {
|
|
6935
|
-
type: "text",
|
|
6936
|
-
nullable: false,
|
|
6937
|
-
primaryKey: true
|
|
6938
|
-
},
|
|
6939
|
-
scopeNodeId: {
|
|
6940
|
-
type: "text",
|
|
6941
|
-
nullable: false,
|
|
6942
|
-
references: {
|
|
6943
|
-
table: TABLE_KNOWLEDGE_NODES$1,
|
|
6944
|
-
column: "id"
|
|
6945
|
-
}
|
|
6946
|
-
}
|
|
6947
|
-
};
|
|
6948
|
-
const KNOWLEDGE_NODE_ADDRESSES_SCHEMA = {
|
|
6949
|
-
source: {
|
|
6950
|
-
type: "text",
|
|
6951
|
-
nullable: false
|
|
6952
|
-
},
|
|
6953
|
-
address: {
|
|
6954
|
-
type: "text",
|
|
6955
|
-
nullable: false
|
|
6956
|
-
},
|
|
6957
|
-
nodeId: {
|
|
6958
|
-
type: "text",
|
|
6959
|
-
nullable: false,
|
|
6960
|
-
references: {
|
|
6961
|
-
table: TABLE_KNOWLEDGE_NODES$1,
|
|
6962
|
-
column: "id"
|
|
6963
|
-
}
|
|
6964
|
-
}
|
|
6965
|
-
};
|
|
6966
|
-
const KNOWLEDGE_IMPORT_STATE_SCHEMA = {
|
|
6967
|
-
importerId: {
|
|
6968
|
-
type: "text",
|
|
6969
|
-
nullable: false
|
|
6970
|
-
},
|
|
6971
|
-
binding: {
|
|
6972
|
-
type: "text",
|
|
6973
|
-
nullable: false
|
|
6974
|
-
},
|
|
6975
|
-
key: {
|
|
6976
|
-
type: "text",
|
|
6977
|
-
nullable: false
|
|
6978
|
-
},
|
|
6979
|
-
value: {
|
|
6980
|
-
type: "text",
|
|
6981
|
-
nullable: false
|
|
6982
|
-
}
|
|
6983
|
-
};
|
|
6984
|
-
const KNOWLEDGE_IMPORT_RUNS_SCHEMA = {
|
|
6985
|
-
id: {
|
|
6986
|
-
type: "text",
|
|
6987
|
-
nullable: false,
|
|
6988
|
-
primaryKey: true
|
|
6989
|
-
},
|
|
6990
|
-
importerId: {
|
|
6991
|
-
type: "text",
|
|
6992
|
-
nullable: false
|
|
6993
|
-
},
|
|
6994
|
-
binding: {
|
|
6995
|
-
type: "text",
|
|
6996
|
-
nullable: false
|
|
6997
|
-
},
|
|
6998
|
-
importKind: {
|
|
6999
|
-
type: "text",
|
|
7000
|
-
nullable: false
|
|
7001
|
-
},
|
|
7002
|
-
triggerKind: {
|
|
7003
|
-
type: "text",
|
|
7004
|
-
nullable: false
|
|
7005
|
-
},
|
|
7006
|
-
status: {
|
|
7007
|
-
type: "text",
|
|
7008
|
-
nullable: false
|
|
7009
|
-
},
|
|
7010
|
-
error: {
|
|
7011
|
-
type: "text",
|
|
7012
|
-
nullable: true
|
|
7013
|
-
},
|
|
7014
|
-
transcriptThreadId: {
|
|
7015
|
-
type: "text",
|
|
7016
|
-
nullable: true
|
|
7017
|
-
},
|
|
7018
|
-
traceId: {
|
|
7019
|
-
type: "text",
|
|
7020
|
-
nullable: true
|
|
7021
|
-
},
|
|
7022
|
-
queuedAt: {
|
|
7023
|
-
type: "timestamp",
|
|
7024
|
-
nullable: false
|
|
7025
|
-
},
|
|
7026
|
-
startedAt: {
|
|
7027
|
-
type: "timestamp",
|
|
7028
|
-
nullable: true
|
|
7029
|
-
},
|
|
7030
|
-
completedAt: {
|
|
7031
|
-
type: "timestamp",
|
|
7032
|
-
nullable: true
|
|
7033
|
-
}
|
|
7034
|
-
};
|
|
7035
|
-
const KNOWLEDGE_V2_ACTIVITY_SCHEMA = {
|
|
7036
|
-
id: {
|
|
7037
|
-
type: "text",
|
|
7038
|
-
nullable: false,
|
|
7039
|
-
primaryKey: true
|
|
7040
|
-
},
|
|
7041
|
-
action: {
|
|
7042
|
-
type: "text",
|
|
7043
|
-
nullable: false
|
|
7044
|
-
},
|
|
7045
|
-
targetType: {
|
|
7046
|
-
type: "text",
|
|
7047
|
-
nullable: true
|
|
7048
|
-
},
|
|
7049
|
-
targetId: {
|
|
7050
|
-
type: "text",
|
|
7051
|
-
nullable: true
|
|
7052
|
-
},
|
|
7053
|
-
contextScopeId: {
|
|
7054
|
-
type: "text",
|
|
7055
|
-
nullable: true,
|
|
7056
|
-
references: {
|
|
7057
|
-
table: TABLE_KNOWLEDGE_NODES$1,
|
|
7058
|
-
column: "id"
|
|
7059
|
-
}
|
|
7060
|
-
},
|
|
7061
|
-
importRunId: {
|
|
7062
|
-
type: "text",
|
|
7063
|
-
nullable: true,
|
|
7064
|
-
references: {
|
|
7065
|
-
table: TABLE_KNOWLEDGE_IMPORT_RUNS,
|
|
7066
|
-
column: "id"
|
|
7067
|
-
}
|
|
7068
|
-
},
|
|
7069
|
-
details: {
|
|
7070
|
-
type: "jsonb",
|
|
7071
|
-
nullable: true
|
|
7072
|
-
},
|
|
7073
|
-
createdAt: {
|
|
7074
|
-
type: "timestamp",
|
|
7075
|
-
nullable: false
|
|
7076
|
-
},
|
|
7077
|
-
recordType: {
|
|
7078
|
-
type: "text",
|
|
7079
|
-
nullable: true
|
|
7080
|
-
},
|
|
7081
|
-
recordId: {
|
|
7082
|
-
type: "text",
|
|
7083
|
-
nullable: true
|
|
7084
|
-
},
|
|
7085
|
-
scope: {
|
|
7086
|
-
type: "jsonb",
|
|
7087
|
-
nullable: true
|
|
7088
|
-
},
|
|
7089
|
-
scopeKey: {
|
|
7090
|
-
type: "text",
|
|
7091
|
-
nullable: true
|
|
7092
|
-
},
|
|
7093
|
-
sourceThreadId: {
|
|
7094
|
-
type: "text",
|
|
7095
|
-
nullable: true
|
|
7096
|
-
}
|
|
7097
|
-
};
|
|
7098
|
-
const KNOWLEDGE_PROPOSALS_SCHEMA = {
|
|
7099
|
-
id: {
|
|
7100
|
-
type: "text",
|
|
7101
|
-
nullable: false,
|
|
7102
|
-
primaryKey: true
|
|
7103
|
-
},
|
|
7104
|
-
targetType: {
|
|
7105
|
-
type: "text",
|
|
7106
|
-
nullable: false
|
|
7107
|
-
},
|
|
7108
|
-
targetId: {
|
|
7109
|
-
type: "text",
|
|
7110
|
-
nullable: false
|
|
7111
|
-
},
|
|
7112
|
-
action: {
|
|
7113
|
-
type: "text",
|
|
7114
|
-
nullable: false
|
|
7115
|
-
},
|
|
7116
|
-
changes: {
|
|
7117
|
-
type: "jsonb",
|
|
7118
|
-
nullable: false
|
|
7119
|
-
},
|
|
7120
|
-
reason: {
|
|
7121
|
-
type: "text",
|
|
7122
|
-
nullable: true
|
|
7123
|
-
},
|
|
7124
|
-
proposerContextScopeId: {
|
|
7125
|
-
type: "text",
|
|
7126
|
-
nullable: false
|
|
7127
|
-
},
|
|
7128
|
-
expectedVersion: {
|
|
7129
|
-
type: "integer",
|
|
7130
|
-
nullable: false
|
|
7131
|
-
},
|
|
7132
|
-
status: {
|
|
7133
|
-
type: "text",
|
|
7134
|
-
nullable: false
|
|
7135
|
-
},
|
|
7136
|
-
reviewerContextScopeId: {
|
|
7137
|
-
type: "text",
|
|
7138
|
-
nullable: true
|
|
7139
|
-
},
|
|
7140
|
-
reviewedAt: {
|
|
7141
|
-
type: "timestamp",
|
|
7142
|
-
nullable: true
|
|
7143
|
-
},
|
|
7144
|
-
createdAt: {
|
|
7145
|
-
type: "timestamp",
|
|
7146
|
-
nullable: false
|
|
6585
|
+
])];
|
|
6586
|
+
/** Schema-1/2 tables whose presence means the store needs a reset before schema 4 can run. */
|
|
6587
|
+
const LEGACY_KNOWLEDGE_TABLE_NAMES = new Set([...KNOWLEDGE_TABLE_NAMES, ...RETIRED_KNOWLEDGE_TABLE_NAMES].filter((name) => !KNOWLEDGE_PAGES_TABLE_NAMES.includes(name)));
|
|
6588
|
+
/**
|
|
6589
|
+
* Classifies what an adapter found before creating schema-4 tables. `tableNames` is every
|
|
6590
|
+
* `mastra_knowledge_*` table present; `schemaVersion` is the state row's version, if any.
|
|
6591
|
+
*/
|
|
6592
|
+
function inspectKnowledgePagesSchema(snapshot) {
|
|
6593
|
+
const legacy = snapshot.tableNames.filter((name) => LEGACY_KNOWLEDGE_TABLE_NAMES.has(name));
|
|
6594
|
+
if (legacy.length > 0) return {
|
|
6595
|
+
status: "incompatible-reset-required",
|
|
6596
|
+
schemaVersion: snapshot.schemaVersion ?? null,
|
|
6597
|
+
reason: `found Knowledge tables from an earlier schema (${legacy.join(", ")})`
|
|
6598
|
+
};
|
|
6599
|
+
if (snapshot.schemaVersion === 4) return {
|
|
6600
|
+
status: "compatible",
|
|
6601
|
+
schemaVersion: 4
|
|
6602
|
+
};
|
|
6603
|
+
if (snapshot.schemaVersion === void 0) {
|
|
6604
|
+
const present = snapshot.tableNames.filter((name) => KNOWLEDGE_PAGES_TABLE_NAMES.includes(name));
|
|
6605
|
+
if (present.length === 0) return {
|
|
6606
|
+
status: "uninitialized",
|
|
6607
|
+
schemaVersion: null
|
|
6608
|
+
};
|
|
6609
|
+
return {
|
|
6610
|
+
status: "incompatible-reset-required",
|
|
6611
|
+
schemaVersion: null,
|
|
6612
|
+
reason: `found Knowledge tables (${present.join(", ")}) without a schema version`
|
|
6613
|
+
};
|
|
7147
6614
|
}
|
|
7148
|
-
};
|
|
7149
|
-
function assertKnowledgeV2CoreSupport(features) {
|
|
7150
|
-
if (!features.has("knowledge-v2")) throw new Error(`Knowledge v2 requires a @mastra/core release with the "${KNOWLEDGE_V2_CORE_FEATURE}" feature. Upgrade @mastra/core to use Knowledge; other storage domains keep working on this version.`);
|
|
7151
|
-
}
|
|
7152
|
-
function resolveKnowledgeV2Core(storageModule) {
|
|
7153
|
-
if (typeof storageModule !== "object" || storageModule === null) throw new Error("@mastra/core advertises Knowledge v2 without the required storage API");
|
|
7154
|
-
const { assertKnowledgeDescriptionWithinBound: assertDescription, assertKnowledgeRecordTextWithinBound: assertRecordText, assertKnowledgeSchemaCompatible: assertCompatible, inspectKnowledgeSchema: inspectSchema } = storageModule;
|
|
7155
|
-
if (typeof assertDescription !== "function" || typeof assertRecordText !== "function" || typeof assertCompatible !== "function" || typeof inspectSchema !== "function") throw new Error("@mastra/core advertises Knowledge v2 without the required storage API");
|
|
7156
6615
|
return {
|
|
7157
|
-
|
|
7158
|
-
|
|
7159
|
-
|
|
7160
|
-
inspectKnowledgeSchema: (snapshot) => inspectSchema(snapshot)
|
|
6616
|
+
status: "incompatible-reset-required",
|
|
6617
|
+
schemaVersion: snapshot.schemaVersion,
|
|
6618
|
+
reason: `Knowledge schema version ${snapshot.schemaVersion} is not 4`
|
|
7161
6619
|
};
|
|
7162
6620
|
}
|
|
7163
|
-
|
|
7164
|
-
|
|
6621
|
+
/** Schema-2 table names schema 4 reuses with a different shape. */
|
|
6622
|
+
const REUSED_KNOWLEDGE_TABLE_NAMES = KNOWLEDGE_TABLE_NAMES.filter((name) => KNOWLEDGE_PAGES_TABLE_NAMES.includes(name));
|
|
6623
|
+
/**
|
|
6624
|
+
* The old Knowledge tables an adapter drops on init, rows included: Knowledge before schema 4 was an
|
|
6625
|
+
* experimental store with no migration. Every schema-1/2-only table present is dropped. The table
|
|
6626
|
+
* names schema 4 reuses (mentions, activity, the state table) are dropped too, but only when an old
|
|
6627
|
+
* table is present and no schema-4 state row exists, so they are old-shaped. Schema-4-only tables
|
|
6628
|
+
* are never listed. On a schema-4 store this lists only tables an older process recreated during a
|
|
6629
|
+
* rolling deploy. Drop order is safe for adapters that enforce references.
|
|
6630
|
+
*/
|
|
6631
|
+
function legacyKnowledgeTablesToDrop(snapshot) {
|
|
6632
|
+
const present = new Set(snapshot.tableNames);
|
|
6633
|
+
const legacy = [...LEGACY_KNOWLEDGE_TABLE_NAMES].filter((name) => present.has(name));
|
|
6634
|
+
if (legacy.length === 0) return [];
|
|
6635
|
+
const reused = snapshot.schemaVersion === 4 ? [] : REUSED_KNOWLEDGE_TABLE_NAMES.filter((name) => present.has(name));
|
|
6636
|
+
const drop = /* @__PURE__ */ new Set([...legacy, ...reused]);
|
|
6637
|
+
return [...RETIRED_KNOWLEDGE_TABLE_NAMES, ...[...KNOWLEDGE_TABLE_NAMES].reverse()].filter((name) => drop.has(name));
|
|
6638
|
+
}
|
|
6639
|
+
const KNOWLEDGE_PAGES_CORE_FUNCTIONS = [
|
|
6640
|
+
"assertKnowledgePageDescriptionWithinBound",
|
|
6641
|
+
"assertKnowledgeNoteTextWithinBound",
|
|
6642
|
+
"canonicalKnowledgeName",
|
|
6643
|
+
"knowledgePositionBetween",
|
|
6644
|
+
"KnowledgeMoveTooLargeError",
|
|
6645
|
+
"KnowledgePageDepthError",
|
|
6646
|
+
"KnowledgePageHasChildrenError",
|
|
6647
|
+
"KnowledgePageKeyConflictError"
|
|
6648
|
+
];
|
|
6649
|
+
const KNOWLEDGE_PAGES_CORE_NUMBERS = [
|
|
6650
|
+
"MAX_KNOWLEDGE_MOVE_PAGES",
|
|
6651
|
+
"MAX_KNOWLEDGE_PAGE_DEPTH",
|
|
6652
|
+
"MAX_KNOWLEDGE_PAGE_LIST_LIMIT"
|
|
6653
|
+
];
|
|
6654
|
+
function resolveKnowledgePagesCore(storageModule) {
|
|
6655
|
+
const module = typeof storageModule === "object" && storageModule !== null ? storageModule : {};
|
|
6656
|
+
const missing = [...KNOWLEDGE_PAGES_CORE_FUNCTIONS.filter((name) => typeof module[name] !== "function"), ...KNOWLEDGE_PAGES_CORE_NUMBERS.filter((name) => typeof module[name] !== "number")];
|
|
6657
|
+
if (missing.length > 0) throw new Error(`@mastra/core advertises Knowledge pages without the required storage API (missing ${missing.join(", ")})`);
|
|
6658
|
+
return module;
|
|
6659
|
+
}
|
|
6660
|
+
/** Loads core's page-store runtime once, after checking core advertises `knowledge-pages`. */
|
|
6661
|
+
function createKnowledgePagesCoreLoader(features, loadStorageModule) {
|
|
6662
|
+
let core;
|
|
7165
6663
|
return async () => {
|
|
7166
|
-
|
|
7167
|
-
|
|
7168
|
-
|
|
6664
|
+
if (!features.has("knowledge-pages")) throw new Error(`Knowledge requires a @mastra/core release with the "${KNOWLEDGE_PAGES_CORE_FEATURE}" feature. Upgrade @mastra/core to use Knowledge; other storage domains keep working on this version.`);
|
|
6665
|
+
core ??= loadStorageModule().then(resolveKnowledgePagesCore).catch((error) => {
|
|
6666
|
+
core = void 0;
|
|
7169
6667
|
throw error;
|
|
7170
6668
|
});
|
|
7171
|
-
return
|
|
6669
|
+
return core;
|
|
7172
6670
|
};
|
|
7173
6671
|
}
|
|
7174
|
-
// src/storage/domains/knowledge/index.ts
|
|
7175
|
-
const loadKnowledgeV2Core = createKnowledgeV2CoreLoader(coreFeatures, () => import("@mastra/core/storage"));
|
|
7176
|
-
async function assertKnowledgeDescriptionWithinBoundCompat(description) {
|
|
7177
|
-
const { assertKnowledgeDescriptionWithinBound } = await loadKnowledgeV2Core();
|
|
7178
|
-
assertKnowledgeDescriptionWithinBound(description);
|
|
7179
|
-
}
|
|
7180
|
-
async function assertKnowledgeRecordTextWithinBoundCompat(text) {
|
|
7181
|
-
const { assertKnowledgeRecordTextWithinBound } = await loadKnowledgeV2Core();
|
|
7182
|
-
assertKnowledgeRecordTextWithinBound(text);
|
|
7183
|
-
}
|
|
7184
|
-
const reconcileChains = /* @__PURE__ */ new Map();
|
|
7185
|
-
const unidentifiedClientReconcileKey = {};
|
|
7186
|
-
function withReconcileLock(key, operation) {
|
|
7187
|
-
const lockKey = typeof key === "string" ? key : unidentifiedClientReconcileKey;
|
|
7188
|
-
const result = (reconcileChains.get(lockKey) ?? Promise.resolve()).then(operation, operation);
|
|
7189
|
-
const tail = result.then(() => void 0, () => void 0);
|
|
7190
|
-
reconcileChains.set(lockKey, tail);
|
|
7191
|
-
tail.finally(() => {
|
|
7192
|
-
if (reconcileChains.get(lockKey) === tail) reconcileChains.delete(lockKey);
|
|
7193
|
-
});
|
|
7194
|
-
return result;
|
|
7195
|
-
}
|
|
7196
6672
|
/**
|
|
7197
|
-
*
|
|
7198
|
-
*
|
|
7199
|
-
* Matching rows by `scopeKey` keeps visibility filters on an index; a caller scope has only a few
|
|
7200
|
-
* entries, so the list stays small.
|
|
6673
|
+
* Keyword-search words: the query split on whitespace, lowercased, deduplicated. A hit needs at
|
|
6674
|
+
* least one of them (as a case-insensitive substring); an empty list means the search returns nothing.
|
|
7201
6675
|
*/
|
|
7202
|
-
function
|
|
7203
|
-
|
|
7204
|
-
const keys = /* @__PURE__ */ new Set();
|
|
7205
|
-
for (let mask = 1; mask < 1 << canonical.length; mask++) try {
|
|
7206
|
-
keys.add(knowledgeScopeKey(canonical.filter((_, index) => mask & 1 << index)));
|
|
7207
|
-
} catch {}
|
|
7208
|
-
return [...keys];
|
|
6676
|
+
function knowledgeSearchWords(query) {
|
|
6677
|
+
return [...new Set(query.toLowerCase().split(/\s+/).filter(Boolean))];
|
|
7209
6678
|
}
|
|
7210
|
-
|
|
7211
|
-
|
|
7212
|
-
|
|
7213
|
-
|
|
7214
|
-
|
|
7215
|
-
|
|
7216
|
-
|
|
7217
|
-
|
|
7218
|
-
|
|
7219
|
-
}
|
|
7220
|
-
|
|
7221
|
-
|
|
6679
|
+
/**
|
|
6680
|
+
* Points one matched search word earns on a page, by the best field it appears in: name, then
|
|
6681
|
+
* description, then body. Pages rank by how many query words they match first; these points only
|
|
6682
|
+
* break ties between pages that match the same number of words.
|
|
6683
|
+
*/
|
|
6684
|
+
const KNOWLEDGE_SEARCH_WEIGHTS = {
|
|
6685
|
+
name: 3,
|
|
6686
|
+
description: 2,
|
|
6687
|
+
body: 1
|
|
6688
|
+
};
|
|
6689
|
+
/**
|
|
6690
|
+
* Why `key` can't be a Knowledge page key, or undefined when it can. Keys are one path segment:
|
|
6691
|
+
* non-empty, with no `/` (path separator), `#` (scope/path separator) or `%` (escape character).
|
|
6692
|
+
*/
|
|
6693
|
+
function knowledgePageKeyProblem(key) {
|
|
6694
|
+
if (!key) return "Knowledge page key must not be empty";
|
|
6695
|
+
if (/[/#%]/.test(key)) return `Knowledge page key "${key}" must not contain "/", "#" or "%"`;
|
|
7222
6696
|
}
|
|
7223
|
-
|
|
7224
|
-
|
|
6697
|
+
// src/storage/domains/knowledge/pages.ts
|
|
6698
|
+
const loadPagesCore = createKnowledgePagesCoreLoader(coreFeatures, () => import("@mastra/core/storage"));
|
|
6699
|
+
const assertKnowledgePageDescriptionWithinBound = async (description) => (await loadPagesCore()).assertKnowledgePageDescriptionWithinBound(description);
|
|
6700
|
+
const assertKnowledgeNoteTextWithinBound = async (text) => (await loadPagesCore()).assertKnowledgeNoteTextWithinBound(text);
|
|
6701
|
+
const KNOWLEDGE_PAGE_TABLES = {
|
|
6702
|
+
pages: "mastra_knowledge_pages",
|
|
6703
|
+
notes: "mastra_knowledge_notes",
|
|
6704
|
+
stamps: "mastra_knowledge_note_stamps",
|
|
6705
|
+
mentions: "mastra_knowledge_mentions",
|
|
6706
|
+
pins: "mastra_knowledge_pins",
|
|
6707
|
+
pinAudiences: "mastra_knowledge_pin_audiences",
|
|
6708
|
+
activity: "mastra_knowledge_activity"
|
|
6709
|
+
};
|
|
6710
|
+
const T = KNOWLEDGE_PAGE_TABLES;
|
|
6711
|
+
/** DDL for the schema-4 page tables, in creation order. Every statement is idempotent. */
|
|
6712
|
+
const KNOWLEDGE_PAGE_DDL = [
|
|
6713
|
+
`CREATE TABLE IF NOT EXISTS "mastra_knowledge_access_state" (
|
|
6714
|
+
id TEXT PRIMARY KEY,
|
|
6715
|
+
schemaVersion INTEGER NOT NULL
|
|
6716
|
+
)`,
|
|
6717
|
+
`CREATE TABLE IF NOT EXISTS "${T.pages}" (
|
|
6718
|
+
id TEXT PRIMARY KEY,
|
|
6719
|
+
scopeId TEXT NOT NULL,
|
|
6720
|
+
isScope INTEGER NOT NULL,
|
|
6721
|
+
address TEXT,
|
|
6722
|
+
declaration TEXT,
|
|
6723
|
+
key TEXT,
|
|
6724
|
+
parentId TEXT,
|
|
6725
|
+
position TEXT NOT NULL,
|
|
6726
|
+
path TEXT NOT NULL,
|
|
6727
|
+
depth INTEGER NOT NULL,
|
|
6728
|
+
name TEXT NOT NULL,
|
|
6729
|
+
canonicalName TEXT NOT NULL,
|
|
6730
|
+
description TEXT,
|
|
6731
|
+
body TEXT,
|
|
6732
|
+
version INTEGER NOT NULL,
|
|
6733
|
+
mergedInto TEXT,
|
|
6734
|
+
createdBy TEXT NOT NULL,
|
|
6735
|
+
updatedBy TEXT NOT NULL,
|
|
6736
|
+
createdAt TEXT NOT NULL,
|
|
6737
|
+
updatedAt TEXT NOT NULL,
|
|
6738
|
+
deletedAt TEXT,
|
|
6739
|
+
deletedBy TEXT
|
|
6740
|
+
)`,
|
|
6741
|
+
`CREATE UNIQUE INDEX IF NOT EXISTS idx_kpages_address ON "${T.pages}" (address) WHERE isScope = 1`,
|
|
6742
|
+
`DROP INDEX IF EXISTS idx_kpages_key`,
|
|
6743
|
+
`CREATE UNIQUE INDEX IF NOT EXISTS idx_kpages_parent_key ON "${T.pages}" (parentId, key) WHERE key IS NOT NULL AND deletedAt IS NULL`,
|
|
6744
|
+
`CREATE INDEX IF NOT EXISTS idx_kpages_scope ON "${T.pages}" (scopeId)`,
|
|
6745
|
+
`CREATE INDEX IF NOT EXISTS idx_kpages_parent ON "${T.pages}" (parentId, position, id)`,
|
|
6746
|
+
`CREATE INDEX IF NOT EXISTS idx_kpages_path ON "${T.pages}" (path)`,
|
|
6747
|
+
`CREATE INDEX IF NOT EXISTS idx_kpages_declaration_parent ON "${T.pages}" (declaration, parentId) WHERE isScope = 1`,
|
|
6748
|
+
`CREATE INDEX IF NOT EXISTS idx_kpages_canonical ON "${T.pages}" (canonicalName)`,
|
|
6749
|
+
`CREATE TABLE IF NOT EXISTS "${T.notes}" (
|
|
6750
|
+
id TEXT PRIMARY KEY,
|
|
6751
|
+
pageId TEXT NOT NULL,
|
|
6752
|
+
text TEXT NOT NULL,
|
|
6753
|
+
"when" TEXT,
|
|
6754
|
+
provenance TEXT NOT NULL,
|
|
6755
|
+
version INTEGER NOT NULL,
|
|
6756
|
+
createdAt TEXT NOT NULL,
|
|
6757
|
+
updatedAt TEXT NOT NULL,
|
|
6758
|
+
deletedAt TEXT,
|
|
6759
|
+
deletedBy TEXT
|
|
6760
|
+
)`,
|
|
6761
|
+
`CREATE INDEX IF NOT EXISTS idx_knotes_page ON "${T.notes}" (pageId, createdAt, id)`,
|
|
6762
|
+
`CREATE TABLE IF NOT EXISTS "${T.stamps}" (
|
|
6763
|
+
noteId TEXT NOT NULL,
|
|
6764
|
+
scopeId TEXT NOT NULL,
|
|
6765
|
+
PRIMARY KEY (noteId, scopeId)
|
|
6766
|
+
)`,
|
|
6767
|
+
`CREATE INDEX IF NOT EXISTS idx_kstamps_scope ON "${T.stamps}" (scopeId)`,
|
|
6768
|
+
`CREATE TABLE IF NOT EXISTS "${T.mentions}" (
|
|
6769
|
+
noteId TEXT NOT NULL,
|
|
6770
|
+
pageId TEXT NOT NULL,
|
|
6771
|
+
PRIMARY KEY (noteId, pageId)
|
|
6772
|
+
)`,
|
|
6773
|
+
`CREATE INDEX IF NOT EXISTS idx_kmentions_page ON "${T.mentions}" (pageId)`,
|
|
6774
|
+
`CREATE TABLE IF NOT EXISTS "${T.pins}" (
|
|
6775
|
+
id TEXT PRIMARY KEY,
|
|
6776
|
+
pageId TEXT NOT NULL UNIQUE,
|
|
6777
|
+
audience TEXT NOT NULL,
|
|
6778
|
+
createdBy TEXT NOT NULL,
|
|
6779
|
+
createdAt TEXT NOT NULL,
|
|
6780
|
+
updatedAt TEXT NOT NULL
|
|
6781
|
+
)`,
|
|
6782
|
+
`CREATE TABLE IF NOT EXISTS "${T.pinAudiences}" (
|
|
6783
|
+
pinId TEXT NOT NULL,
|
|
6784
|
+
audience TEXT NOT NULL,
|
|
6785
|
+
PRIMARY KEY (audience, pinId)
|
|
6786
|
+
) WITHOUT ROWID`,
|
|
6787
|
+
`CREATE INDEX IF NOT EXISTS idx_kpin_audiences_pin ON "${T.pinAudiences}" (pinId)`,
|
|
6788
|
+
`INSERT OR IGNORE INTO "${T.pinAudiences}" (audience, pinId) SELECT j.value, pin.id FROM "${T.pins}" pin, json_each(pin.audience) j`,
|
|
6789
|
+
`CREATE TABLE IF NOT EXISTS "${T.activity}" (
|
|
6790
|
+
id TEXT PRIMARY KEY,
|
|
6791
|
+
action TEXT NOT NULL,
|
|
6792
|
+
scopeId TEXT NOT NULL,
|
|
6793
|
+
pageId TEXT NOT NULL,
|
|
6794
|
+
noteId TEXT,
|
|
6795
|
+
provenance TEXT NOT NULL,
|
|
6796
|
+
createdAt TEXT NOT NULL
|
|
6797
|
+
)`,
|
|
6798
|
+
`CREATE INDEX IF NOT EXISTS idx_kactivity_page ON "${T.activity}" (pageId, id DESC)`
|
|
6799
|
+
];
|
|
6800
|
+
function str(value) {
|
|
6801
|
+
return value === null || value === void 0 ? void 0 : String(value);
|
|
7225
6802
|
}
|
|
7226
|
-
function
|
|
7227
|
-
return value
|
|
6803
|
+
function date(value) {
|
|
6804
|
+
return value === null || value === void 0 ? void 0 : new Date(String(value));
|
|
7228
6805
|
}
|
|
7229
|
-
function
|
|
7230
|
-
return
|
|
6806
|
+
function provenance(raw) {
|
|
6807
|
+
return JSON.parse(String(raw));
|
|
7231
6808
|
}
|
|
7232
|
-
function
|
|
6809
|
+
function toPage(row) {
|
|
7233
6810
|
return {
|
|
7234
6811
|
id: String(row.id),
|
|
7235
|
-
|
|
6812
|
+
scopeId: String(row.scopeId),
|
|
6813
|
+
parentId: str(row.parentId) ?? null,
|
|
6814
|
+
isScope: Number(row.isScope) === 1,
|
|
6815
|
+
...row.address !== null && row.address !== void 0 ? { address: String(row.address) } : {},
|
|
6816
|
+
...row.declaration !== null && row.declaration !== void 0 ? { declaration: String(row.declaration) } : {},
|
|
6817
|
+
...row.key !== null && row.key !== void 0 ? { key: String(row.key) } : {},
|
|
6818
|
+
position: String(row.position),
|
|
7236
6819
|
name: String(row.name),
|
|
7237
|
-
|
|
7238
|
-
|
|
7239
|
-
description: row.description == null ? void 0 : String(row.description),
|
|
7240
|
-
isScope: row.isScope === true || row.isScope === 1 ? true : void 0,
|
|
7241
|
-
scope: parseJson$3(row.scopeJson ?? row.scope),
|
|
6820
|
+
...row.description !== null && row.description !== void 0 ? { description: String(row.description) } : {},
|
|
6821
|
+
...row.body !== null && row.body !== void 0 ? { body: String(row.body) } : {},
|
|
7242
6822
|
version: Number(row.version),
|
|
7243
|
-
|
|
7244
|
-
|
|
7245
|
-
|
|
6823
|
+
...row.mergedInto !== null && row.mergedInto !== void 0 ? { mergedInto: String(row.mergedInto) } : {},
|
|
6824
|
+
createdBy: provenance(row.createdBy),
|
|
6825
|
+
updatedBy: provenance(row.updatedBy),
|
|
6826
|
+
createdAt: new Date(String(row.createdAt)),
|
|
6827
|
+
updatedAt: new Date(String(row.updatedAt)),
|
|
6828
|
+
...row.deletedAt ? { deletedAt: new Date(String(row.deletedAt)) } : {},
|
|
6829
|
+
...row.deletedBy ? { deletedBy: String(row.deletedBy) } : {}
|
|
7246
6830
|
};
|
|
7247
6831
|
}
|
|
7248
|
-
function
|
|
6832
|
+
function toNote(row, stamps, mentions) {
|
|
6833
|
+
const when = date(row.when);
|
|
7249
6834
|
return {
|
|
7250
6835
|
id: String(row.id),
|
|
7251
|
-
|
|
6836
|
+
pageId: String(row.pageId),
|
|
7252
6837
|
text: String(row.text),
|
|
7253
|
-
|
|
7254
|
-
|
|
7255
|
-
|
|
7256
|
-
|
|
7257
|
-
|
|
7258
|
-
|
|
7259
|
-
|
|
6838
|
+
...when ? { when } : {},
|
|
6839
|
+
stampScopeIds: stamps,
|
|
6840
|
+
mentions,
|
|
6841
|
+
provenance: provenance(row.provenance),
|
|
6842
|
+
version: Number(row.version),
|
|
6843
|
+
createdAt: new Date(String(row.createdAt)),
|
|
6844
|
+
updatedAt: new Date(String(row.updatedAt)),
|
|
6845
|
+
...row.deletedAt ? { deletedAt: new Date(String(row.deletedAt)) } : {},
|
|
6846
|
+
...row.deletedBy ? { deletedBy: String(row.deletedBy) } : {}
|
|
7260
6847
|
};
|
|
7261
6848
|
}
|
|
7262
|
-
function
|
|
6849
|
+
function toPin(row) {
|
|
7263
6850
|
return {
|
|
7264
6851
|
id: String(row.id),
|
|
7265
|
-
|
|
7266
|
-
|
|
7267
|
-
|
|
7268
|
-
|
|
7269
|
-
|
|
7270
|
-
status: String(row.status),
|
|
7271
|
-
attempts: Number(row.attempts),
|
|
7272
|
-
availableAt: toDate(row.availableAt),
|
|
7273
|
-
claimedAt: optionalDate(row.claimedAt),
|
|
7274
|
-
claimedBy: row.claimedBy == null ? void 0 : String(row.claimedBy),
|
|
7275
|
-
createdAt: toDate(row.createdAt),
|
|
7276
|
-
completedAt: optionalDate(row.completedAt)
|
|
6852
|
+
pageId: String(row.pageId),
|
|
6853
|
+
audience: JSON.parse(String(row.audience)),
|
|
6854
|
+
createdBy: String(row.createdBy),
|
|
6855
|
+
createdAt: new Date(String(row.createdAt)),
|
|
6856
|
+
updatedAt: new Date(String(row.updatedAt))
|
|
7277
6857
|
};
|
|
7278
6858
|
}
|
|
7279
|
-
|
|
7280
|
-
|
|
7281
|
-
|
|
7282
|
-
|
|
7283
|
-
|
|
7284
|
-
|
|
7285
|
-
|
|
7286
|
-
|
|
7287
|
-
|
|
7288
|
-
|
|
7289
|
-
|
|
7290
|
-
|
|
7291
|
-
|
|
7292
|
-
|
|
7293
|
-
[TABLE_KNOWLEDGE_IMPORT_RUNS, KNOWLEDGE_IMPORT_RUNS_SCHEMA],
|
|
7294
|
-
[TABLE_KNOWLEDGE_PROPOSALS, KNOWLEDGE_PROPOSALS_SCHEMA]
|
|
7295
|
-
]);
|
|
7296
|
-
function canonicalizeLibSQLUrl(url) {
|
|
7297
|
-
if (url.includes(":memory:")) return /[?&]cache=shared(?:&|$)/.test(url) ? "file::memory:?cache=shared" : void 0;
|
|
7298
|
-
if (url.startsWith("file:")) {
|
|
7299
|
-
const [path] = url.slice(5).split("?");
|
|
7300
|
-
if (url.startsWith("file://")) {
|
|
7301
|
-
const parsed = new URL(url);
|
|
7302
|
-
const host = parsed.hostname.toLocaleLowerCase();
|
|
7303
|
-
const filePath = host && host !== "localhost" ? `//${host}${parsed.pathname}` : parsed.pathname;
|
|
7304
|
-
return `file:${resolve(decodeURIComponent(filePath))}`;
|
|
7305
|
-
}
|
|
7306
|
-
return `file:${resolve(decodeURIComponent(path))}`;
|
|
7307
|
-
}
|
|
6859
|
+
function toListedPin(row) {
|
|
6860
|
+
return {
|
|
6861
|
+
...toPin(row),
|
|
6862
|
+
pageName: String(row.pageName)
|
|
6863
|
+
};
|
|
6864
|
+
}
|
|
6865
|
+
function limitOf(limit, max) {
|
|
6866
|
+
return Math.max(1, Math.min(limit ?? max, max));
|
|
6867
|
+
}
|
|
6868
|
+
function encodeCursor(value) {
|
|
6869
|
+
return Buffer.from(JSON.stringify(value)).toString("base64url");
|
|
6870
|
+
}
|
|
6871
|
+
function decodeCursor(cursor, length) {
|
|
6872
|
+
if (cursor === void 0) return void 0;
|
|
7308
6873
|
try {
|
|
7309
|
-
const parsed =
|
|
7310
|
-
|
|
7311
|
-
|
|
7312
|
-
|
|
7313
|
-
|
|
6874
|
+
const parsed = JSON.parse(Buffer.from(cursor, "base64url").toString("utf8"));
|
|
6875
|
+
if (Array.isArray(parsed) && parsed.length === length && parsed.every((item) => typeof item === "string")) return parsed;
|
|
6876
|
+
} catch {}
|
|
6877
|
+
throw new Error("Invalid Knowledge cursor");
|
|
6878
|
+
}
|
|
6879
|
+
function escapeLike(value) {
|
|
6880
|
+
return value.replace(/[=%_]/g, (match) => `=${match}`);
|
|
6881
|
+
}
|
|
6882
|
+
/**
|
|
6883
|
+
* A string above every string that starts with `prefix`, comparing by code point (which is also
|
|
6884
|
+
* UTF-8 byte order, so it matches SQLite's BINARY collation). `column >= prefix AND column < bound`
|
|
6885
|
+
* is an exact, case-sensitive prefix match that an index range scan can serve, where
|
|
6886
|
+
* `substr(column, 1, length(prefix)) = prefix` cannot. When the last code point cannot be raised
|
|
6887
|
+
* (an empty prefix, or one made only of U+10FFFF), the bound is the prefix plus more U+10FFFF than
|
|
6888
|
+
* any stored column can hold.
|
|
6889
|
+
*/
|
|
6890
|
+
function prefixUpperBound(prefix) {
|
|
6891
|
+
const points = Array.from(prefix);
|
|
6892
|
+
while (points.length > 0) {
|
|
6893
|
+
const last = points.pop().codePointAt(0);
|
|
6894
|
+
if (last < 1114111) {
|
|
6895
|
+
const next = last === 55295 ? 57344 : last + 1;
|
|
6896
|
+
return points.join("") + String.fromCodePoint(next);
|
|
6897
|
+
}
|
|
7314
6898
|
}
|
|
6899
|
+
return prefix + "".repeat(4096);
|
|
7315
6900
|
}
|
|
7316
|
-
|
|
7317
|
-
function
|
|
7318
|
-
|
|
7319
|
-
|
|
7320
|
-
|
|
7321
|
-
|
|
6901
|
+
/** `column` starts with `prefix`, as an index-friendly range. */
|
|
6902
|
+
function prefixRange(column, prefix) {
|
|
6903
|
+
return {
|
|
6904
|
+
sql: `(${column} >= ? AND ${column} < ?)`,
|
|
6905
|
+
args: [prefix, prefixUpperBound(prefix)]
|
|
6906
|
+
};
|
|
7322
6907
|
}
|
|
7323
|
-
|
|
7324
|
-
|
|
7325
|
-
|
|
6908
|
+
/**
|
|
6909
|
+
* The readable scope page ids as a subquery. The caller's addresses, prefixes and children go in as
|
|
6910
|
+
* JSON arrays so a large read scope never hits a bind-parameter limit. Each kind is its own index
|
|
6911
|
+
* lookup (address equality, address range for a prefix, `(declaration, parentId)` for children),
|
|
6912
|
+
* unioned, so the cost follows the caller's reach rather than the number of scopes in the store.
|
|
6913
|
+
*/
|
|
6914
|
+
function readableScopes(read) {
|
|
6915
|
+
if (read === "all") return {
|
|
6916
|
+
sql: `SELECT id FROM "${T.pages}" WHERE isScope = 1`,
|
|
6917
|
+
args: []
|
|
6918
|
+
};
|
|
6919
|
+
const parts = [];
|
|
6920
|
+
const args = [];
|
|
6921
|
+
if (read.addresses.length > 0) {
|
|
6922
|
+
parts.push(`SELECT id FROM "${T.pages}" WHERE isScope = 1 AND address IN (SELECT value FROM json_each(?))`);
|
|
6923
|
+
args.push(JSON.stringify(read.addresses));
|
|
6924
|
+
}
|
|
6925
|
+
if (read.prefixes.length > 0) {
|
|
6926
|
+
parts.push(`SELECT sp.id FROM json_each(?) j CROSS JOIN "${T.pages}" sp ON sp.isScope = 1 AND sp.address >= json_extract(j.value, '$.lo') AND sp.address < json_extract(j.value, '$.hi') AND +sp.declaration = json_extract(j.value, '$.declaration')`);
|
|
6927
|
+
args.push(JSON.stringify(read.prefixes.map((entry) => ({
|
|
6928
|
+
declaration: entry.declaration,
|
|
6929
|
+
lo: entry.prefix,
|
|
6930
|
+
hi: prefixUpperBound(entry.prefix)
|
|
6931
|
+
}))));
|
|
6932
|
+
}
|
|
6933
|
+
if (read.children.length > 0) {
|
|
6934
|
+
parts.push(`SELECT sp.id FROM json_each(?) j CROSS JOIN "${T.pages}" sp ON sp.isScope = 1 AND sp.declaration = json_extract(j.value, '$.declaration') AND sp.parentId IS json_extract(j.value, '$.parentId')`);
|
|
6935
|
+
args.push(JSON.stringify(read.children));
|
|
6936
|
+
}
|
|
6937
|
+
if (parts.length === 0) return {
|
|
6938
|
+
sql: `SELECT id FROM "${T.pages}" WHERE 0`,
|
|
6939
|
+
args: []
|
|
6940
|
+
};
|
|
6941
|
+
return {
|
|
6942
|
+
sql: parts.join(" UNION "),
|
|
6943
|
+
args
|
|
6944
|
+
};
|
|
6945
|
+
}
|
|
6946
|
+
/** `alias.scopeId` is readable. */
|
|
6947
|
+
function pageReadable(alias, read) {
|
|
6948
|
+
const scopes = readableScopes(read);
|
|
6949
|
+
return {
|
|
6950
|
+
sql: `${alias}.scopeId IN (${scopes.sql})`,
|
|
6951
|
+
args: scopes.args
|
|
6952
|
+
};
|
|
6953
|
+
}
|
|
6954
|
+
/**
|
|
6955
|
+
* Note `alias` is visible: its page's scope is readable, a stamp is readable, and every resolved
|
|
6956
|
+
* mention's page sits in a readable scope.
|
|
6957
|
+
*/
|
|
6958
|
+
function noteReadable(alias, read) {
|
|
6959
|
+
const scopes = readableScopes(read);
|
|
6960
|
+
return {
|
|
6961
|
+
sql: `EXISTS (SELECT 1 FROM "${T.pages}" vp WHERE vp.id = ${alias}.pageId AND vp.scopeId IN (${scopes.sql}))
|
|
6962
|
+
AND EXISTS (SELECT 1 FROM "${T.stamps}" vs WHERE vs.noteId = ${alias}.id AND vs.scopeId IN (${scopes.sql}))
|
|
6963
|
+
AND NOT EXISTS (SELECT 1 FROM "${T.mentions}" vm JOIN "${T.pages}" vmp ON vmp.id = vm.pageId WHERE vm.noteId = ${alias}.id AND vmp.scopeId NOT IN (${scopes.sql}))`,
|
|
6964
|
+
args: [
|
|
6965
|
+
...scopes.args,
|
|
6966
|
+
...scopes.args,
|
|
6967
|
+
...scopes.args
|
|
6968
|
+
]
|
|
6969
|
+
};
|
|
6970
|
+
}
|
|
6971
|
+
function jsonList(values) {
|
|
6972
|
+
return JSON.stringify(values);
|
|
6973
|
+
}
|
|
6974
|
+
var LibSQLKnowledgePages = class {
|
|
6975
|
+
#read;
|
|
6976
|
+
#transaction;
|
|
7326
6977
|
constructor(config) {
|
|
7327
|
-
|
|
7328
|
-
|
|
7329
|
-
|
|
7330
|
-
|
|
7331
|
-
|
|
7332
|
-
|
|
7333
|
-
|
|
7334
|
-
|
|
6978
|
+
this.#read = config.read;
|
|
6979
|
+
this.#transaction = config.transaction;
|
|
6980
|
+
}
|
|
6981
|
+
/** Deletes every row from the page tables (scopes, pages, notes, pins, activity). */
|
|
6982
|
+
async clearAll() {
|
|
6983
|
+
await this.#transaction(async (tx) => {
|
|
6984
|
+
for (const table of [
|
|
6985
|
+
T.activity,
|
|
6986
|
+
T.pinAudiences,
|
|
6987
|
+
T.pins,
|
|
6988
|
+
T.mentions,
|
|
6989
|
+
T.stamps,
|
|
6990
|
+
T.notes,
|
|
6991
|
+
T.pages
|
|
6992
|
+
]) await tx.execute({
|
|
6993
|
+
sql: `DELETE FROM ${table}`,
|
|
6994
|
+
args: []
|
|
6995
|
+
});
|
|
7335
6996
|
});
|
|
7336
6997
|
}
|
|
7337
|
-
|
|
7338
|
-
return {
|
|
7339
|
-
|
|
7340
|
-
|
|
7341
|
-
|
|
7342
|
-
supportsSchemaInspection: true,
|
|
7343
|
-
supportsExplicitReset: true
|
|
7344
|
-
};
|
|
6998
|
+
async #query(executor, sql, args = []) {
|
|
6999
|
+
return (await executor.execute({
|
|
7000
|
+
sql,
|
|
7001
|
+
args
|
|
7002
|
+
})).rows;
|
|
7345
7003
|
}
|
|
7346
|
-
async
|
|
7347
|
-
return this.#
|
|
7004
|
+
async #pageRow(executor, id) {
|
|
7005
|
+
return (await this.#query(executor, `SELECT * FROM "${T.pages}" WHERE id = ?`, [id]))[0];
|
|
7348
7006
|
}
|
|
7349
|
-
async #
|
|
7350
|
-
const
|
|
7351
|
-
|
|
7352
|
-
|
|
7353
|
-
const tableNames = (await executor.execute({
|
|
7354
|
-
sql: `SELECT name FROM sqlite_master WHERE type = 'table' AND name IN (${placeholders})`,
|
|
7355
|
-
args: [...KNOWLEDGE_TABLE_NAMES]
|
|
7356
|
-
})).rows.map((row) => String(row.name));
|
|
7357
|
-
if (tableNames.length === 0) return inspectKnowledgeSchema({
|
|
7358
|
-
available: true,
|
|
7359
|
-
tableNames
|
|
7360
|
-
});
|
|
7361
|
-
const missingTables = KNOWLEDGE_TABLE_NAMES.filter((table) => !tableNames.includes(table));
|
|
7362
|
-
if (missingTables.length > 0) return inspectKnowledgeSchema({
|
|
7363
|
-
available: true,
|
|
7364
|
-
tableNames,
|
|
7365
|
-
reason: `Missing Knowledge v2 tables: ${missingTables.join(", ")}`
|
|
7366
|
-
});
|
|
7367
|
-
for (const [table, schema] of knowledgeV2TableSchemas) {
|
|
7368
|
-
const columns = await executor.execute(`PRAGMA table_info("${table}")`);
|
|
7369
|
-
const actual = new Set(columns.rows.map((row) => String(row.name)));
|
|
7370
|
-
const missing = Object.keys(schema).filter((column) => !actual.has(column));
|
|
7371
|
-
if (missing.length > 0) return inspectKnowledgeSchema({
|
|
7372
|
-
available: true,
|
|
7373
|
-
tableNames,
|
|
7374
|
-
reason: `Knowledge table ${table} is missing v2 columns: ${missing.join(", ")}`
|
|
7375
|
-
});
|
|
7376
|
-
}
|
|
7377
|
-
const accessState = await executor.execute(`SELECT schemaVersion FROM "${TABLE_KNOWLEDGE_ACCESS_STATE}" WHERE id='global'`);
|
|
7378
|
-
if (Number(accessState.rows[0]?.schemaVersion) !== 2) return inspectKnowledgeSchema({
|
|
7379
|
-
available: true,
|
|
7380
|
-
tableNames,
|
|
7381
|
-
reason: "Knowledge schema version marker is missing or incompatible"
|
|
7382
|
-
});
|
|
7383
|
-
return inspectKnowledgeSchema({
|
|
7384
|
-
available: true,
|
|
7385
|
-
tableNames,
|
|
7386
|
-
schemaVersion: 2
|
|
7387
|
-
});
|
|
7388
|
-
} catch (error) {
|
|
7389
|
-
return inspectKnowledgeSchema({
|
|
7390
|
-
available: false,
|
|
7391
|
-
tableNames: [],
|
|
7392
|
-
reason: error instanceof Error ? error.message : String(error)
|
|
7393
|
-
});
|
|
7394
|
-
}
|
|
7007
|
+
async #livePage(executor, id) {
|
|
7008
|
+
const row = await this.#pageRow(executor, id);
|
|
7009
|
+
if (!row || row.deletedAt) throw new KnowledgeNotFoundError("page", id);
|
|
7010
|
+
return row;
|
|
7395
7011
|
}
|
|
7396
|
-
async
|
|
7397
|
-
await this.#transaction((tx) => this.#initializeSchema(tx));
|
|
7398
|
-
}
|
|
7399
|
-
async #initializeSchema(tx) {
|
|
7400
|
-
const { assertKnowledgeSchemaCompatible } = await loadKnowledgeV2Core();
|
|
7401
|
-
let inspection = await this.#inspectSchema(tx);
|
|
7402
|
-
if (inspection.status === "incompatible-reset-required" && await this.#isPublishedV1Layout(tx)) {
|
|
7403
|
-
for (const table of [...RETIRED_KNOWLEDGE_TABLE_NAMES, ...[...KNOWLEDGE_TABLE_NAMES].reverse()]) await tx.execute(`DROP TABLE IF EXISTS "${table}"`);
|
|
7404
|
-
inspection = await this.#inspectSchema(tx);
|
|
7405
|
-
}
|
|
7406
|
-
assertKnowledgeSchemaCompatible(inspection);
|
|
7407
|
-
const createTable = (input) => this.#db.createTable({
|
|
7408
|
-
...input,
|
|
7409
|
-
executor: tx
|
|
7410
|
-
});
|
|
7411
|
-
await createTable({
|
|
7412
|
-
tableName: TABLE_KNOWLEDGE_NODES,
|
|
7413
|
-
schema: KNOWLEDGE_V2_NODES_SCHEMA
|
|
7414
|
-
});
|
|
7415
|
-
await createTable({
|
|
7416
|
-
tableName: TABLE_KNOWLEDGE_RECORDS,
|
|
7417
|
-
schema: KNOWLEDGE_V2_RECORDS_SCHEMA
|
|
7418
|
-
});
|
|
7419
|
-
await createTable({
|
|
7420
|
-
tableName: TABLE_KNOWLEDGE_MENTIONS,
|
|
7421
|
-
schema: KNOWLEDGE_V2_MENTIONS_SCHEMA,
|
|
7422
|
-
compositePrimaryKey: ["recordId", "sourceId"]
|
|
7423
|
-
});
|
|
7424
|
-
await createTable({
|
|
7425
|
-
tableName: TABLE_KNOWLEDGE_NODE_SCOPES,
|
|
7426
|
-
schema: KNOWLEDGE_NODE_SCOPES_SCHEMA,
|
|
7427
|
-
compositePrimaryKey: ["nodeId", "scopeNodeId"]
|
|
7428
|
-
});
|
|
7429
|
-
await createTable({
|
|
7430
|
-
tableName: TABLE_KNOWLEDGE_RECORD_SCOPES,
|
|
7431
|
-
schema: KNOWLEDGE_RECORD_SCOPES_SCHEMA,
|
|
7432
|
-
compositePrimaryKey: ["recordId", "scopeNodeId"]
|
|
7433
|
-
});
|
|
7434
|
-
await createTable({
|
|
7435
|
-
tableName: TABLE_KNOWLEDGE_SCOPE_GRANTS,
|
|
7436
|
-
schema: KNOWLEDGE_SCOPE_GRANTS_SCHEMA,
|
|
7437
|
-
compositePrimaryKey: ["scopeNodeId", "scopeRefId"]
|
|
7438
|
-
});
|
|
7439
|
-
await createTable({
|
|
7440
|
-
tableName: TABLE_KNOWLEDGE_ACCESS_STATE,
|
|
7441
|
-
schema: KNOWLEDGE_ACCESS_STATE_SCHEMA
|
|
7442
|
-
});
|
|
7443
|
-
await createTable({
|
|
7444
|
-
tableName: TABLE_KNOWLEDGE_SCOPE_ADDRESSES,
|
|
7445
|
-
schema: KNOWLEDGE_SCOPE_ADDRESSES_SCHEMA
|
|
7446
|
-
});
|
|
7447
|
-
await createTable({
|
|
7448
|
-
tableName: TABLE_KNOWLEDGE_NODE_ADDRESSES,
|
|
7449
|
-
schema: KNOWLEDGE_NODE_ADDRESSES_SCHEMA,
|
|
7450
|
-
compositePrimaryKey: ["source", "address"]
|
|
7451
|
-
});
|
|
7452
|
-
await createTable({
|
|
7453
|
-
tableName: TABLE_KNOWLEDGE_IMPORT_STATE,
|
|
7454
|
-
schema: KNOWLEDGE_IMPORT_STATE_SCHEMA,
|
|
7455
|
-
compositePrimaryKey: [
|
|
7456
|
-
"importerId",
|
|
7457
|
-
"binding",
|
|
7458
|
-
"key"
|
|
7459
|
-
]
|
|
7460
|
-
});
|
|
7461
|
-
await createTable({
|
|
7462
|
-
tableName: TABLE_KNOWLEDGE_IMPORT_RUNS,
|
|
7463
|
-
schema: KNOWLEDGE_IMPORT_RUNS_SCHEMA
|
|
7464
|
-
});
|
|
7465
|
-
await createTable({
|
|
7466
|
-
tableName: TABLE_KNOWLEDGE_PROPOSALS,
|
|
7467
|
-
schema: KNOWLEDGE_PROPOSALS_SCHEMA
|
|
7468
|
-
});
|
|
7469
|
-
await createTable({
|
|
7470
|
-
tableName: TABLE_KNOWLEDGE_ACTIVITY,
|
|
7471
|
-
schema: KNOWLEDGE_V2_ACTIVITY_SCHEMA
|
|
7472
|
-
});
|
|
7473
|
-
await createTable({
|
|
7474
|
-
tableName: TABLE_KNOWLEDGE_SEMANTIC_OUTBOX,
|
|
7475
|
-
schema: KNOWLEDGE_SEMANTIC_OUTBOX_SCHEMA
|
|
7476
|
-
});
|
|
7477
|
-
for (const statement of [
|
|
7478
|
-
`CREATE UNIQUE INDEX IF NOT EXISTS idx_knowledge_nodes_identity ON "${TABLE_KNOWLEDGE_NODES}" (type, scopeKey, canonicalName)`,
|
|
7479
|
-
`CREATE INDEX IF NOT EXISTS idx_knowledge_nodes_scope ON "${TABLE_KNOWLEDGE_NODES}" (scopeKey, type)`,
|
|
7480
|
-
`CREATE INDEX IF NOT EXISTS idx_knowledge_nodes_name ON "${TABLE_KNOWLEDGE_NODES}" (type, canonicalName)`,
|
|
7481
|
-
`CREATE INDEX IF NOT EXISTS idx_knowledge_records_node_latest ON "${TABLE_KNOWLEDGE_RECORDS}" (node, id DESC)`,
|
|
7482
|
-
`CREATE INDEX IF NOT EXISTS idx_knowledge_records_thread_latest ON "${TABLE_KNOWLEDGE_RECORDS}" (sourceThreadId, id DESC)`,
|
|
7483
|
-
`CREATE INDEX IF NOT EXISTS idx_knowledge_mentions_record ON "${TABLE_KNOWLEDGE_MENTIONS}" (recordId, sourceType, sourceId)`,
|
|
7484
|
-
`CREATE INDEX IF NOT EXISTS idx_knowledge_activity_latest ON "${TABLE_KNOWLEDGE_ACTIVITY}" (id DESC)`,
|
|
7485
|
-
`CREATE INDEX IF NOT EXISTS idx_knowledge_records_scope ON "${TABLE_KNOWLEDGE_RECORDS}" (scopeKey, id)`,
|
|
7486
|
-
`CREATE INDEX IF NOT EXISTS idx_knowledge_activity_scope ON "${TABLE_KNOWLEDGE_ACTIVITY}" (scopeKey, id DESC)`,
|
|
7487
|
-
`CREATE UNIQUE INDEX IF NOT EXISTS idx_knowledge_outbox_idempotency ON "${TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}" (idempotencyKey)`,
|
|
7488
|
-
`CREATE INDEX IF NOT EXISTS idx_knowledge_outbox_claim ON "${TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}" (status, availableAt, createdAt)`,
|
|
7489
|
-
`CREATE INDEX IF NOT EXISTS idx_knowledge_node_scopes_scope ON "${TABLE_KNOWLEDGE_NODE_SCOPES}" (scopeNodeId, nodeId)`,
|
|
7490
|
-
`CREATE INDEX IF NOT EXISTS idx_knowledge_record_scopes_scope ON "${TABLE_KNOWLEDGE_RECORD_SCOPES}" (scopeNodeId, recordId)`,
|
|
7491
|
-
`CREATE INDEX IF NOT EXISTS idx_knowledge_scope_grants_ref ON "${TABLE_KNOWLEDGE_SCOPE_GRANTS}" (scopeRefId, scopeNodeId)`,
|
|
7492
|
-
`CREATE INDEX IF NOT EXISTS idx_knowledge_node_addresses_node ON "${TABLE_KNOWLEDGE_NODE_ADDRESSES}" (nodeId)`,
|
|
7493
|
-
`CREATE INDEX IF NOT EXISTS idx_knowledge_import_runs_lookup ON "${TABLE_KNOWLEDGE_IMPORT_RUNS}" (importerId, binding, queuedAt DESC)`,
|
|
7494
|
-
`CREATE INDEX IF NOT EXISTS idx_knowledge_activity_import_run ON "${TABLE_KNOWLEDGE_ACTIVITY}" (importRunId, id DESC)`
|
|
7495
|
-
]) await tx.execute(statement);
|
|
7012
|
+
async #logActivity(tx, action, event, now) {
|
|
7496
7013
|
await tx.execute({
|
|
7497
|
-
sql: `INSERT
|
|
7498
|
-
args: [
|
|
7499
|
-
|
|
7500
|
-
|
|
7501
|
-
|
|
7502
|
-
|
|
7503
|
-
|
|
7504
|
-
|
|
7505
|
-
|
|
7506
|
-
|
|
7507
|
-
const objects = await executor.execute("SELECT type, name FROM sqlite_master WHERE name LIKE 'mastra\\_knowledge\\_%' ESCAPE '\\' OR tbl_name LIKE 'mastra\\_knowledge\\_%' ESCAPE '\\' OR sql LIKE '%mastra\\_knowledge\\_%' ESCAPE '\\'");
|
|
7508
|
-
for (const row of objects.rows) {
|
|
7509
|
-
const type = String(row.type);
|
|
7510
|
-
const name = String(row.name);
|
|
7511
|
-
if (type === "table" && PUBLISHED_KNOWLEDGE_V1_TABLE_NAMES.has(name)) continue;
|
|
7512
|
-
const knownIndex = PUBLISHED_KNOWLEDGE_V1_INDEX_NAMES.has(name) || name.startsWith("sqlite_autoindex_");
|
|
7513
|
-
if (type !== "index" || !knownIndex) return false;
|
|
7514
|
-
}
|
|
7515
|
-
return true;
|
|
7516
|
-
}
|
|
7517
|
-
async dangerouslyClearAll() {
|
|
7518
|
-
await this.#transaction(async (tx) => {
|
|
7519
|
-
for (const table of [
|
|
7520
|
-
TABLE_KNOWLEDGE_RECORD_SCOPES,
|
|
7521
|
-
TABLE_KNOWLEDGE_NODE_SCOPES,
|
|
7522
|
-
TABLE_KNOWLEDGE_SCOPE_GRANTS,
|
|
7523
|
-
TABLE_KNOWLEDGE_SCOPE_ADDRESSES,
|
|
7524
|
-
TABLE_KNOWLEDGE_NODE_ADDRESSES,
|
|
7525
|
-
TABLE_KNOWLEDGE_MENTIONS,
|
|
7526
|
-
TABLE_KNOWLEDGE_PROPOSALS,
|
|
7527
|
-
TABLE_KNOWLEDGE_ACTIVITY,
|
|
7528
|
-
TABLE_KNOWLEDGE_IMPORT_STATE,
|
|
7529
|
-
TABLE_KNOWLEDGE_IMPORT_RUNS,
|
|
7530
|
-
TABLE_KNOWLEDGE_SEMANTIC_OUTBOX,
|
|
7531
|
-
TABLE_KNOWLEDGE_RECORDS,
|
|
7532
|
-
TABLE_KNOWLEDGE_NODES
|
|
7533
|
-
]) await tx.execute(`DELETE FROM "${table}"`);
|
|
7534
|
-
await tx.execute(`UPDATE "${TABLE_KNOWLEDGE_ACCESS_STATE}" SET epoch = 0 WHERE id = 'global'`);
|
|
7014
|
+
sql: `INSERT INTO "${T.activity}" (id, action, scopeId, pageId, noteId, provenance, createdAt) VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
|
7015
|
+
args: [
|
|
7016
|
+
createKnowledgeUlid(),
|
|
7017
|
+
action,
|
|
7018
|
+
event.scopeId,
|
|
7019
|
+
event.pageId,
|
|
7020
|
+
event.noteId ?? null,
|
|
7021
|
+
JSON.stringify(event.provenance),
|
|
7022
|
+
now
|
|
7023
|
+
]
|
|
7535
7024
|
});
|
|
7536
7025
|
}
|
|
7537
|
-
async
|
|
7538
|
-
await
|
|
7539
|
-
|
|
7540
|
-
sql: `DROP TABLE IF EXISTS "${table}"`,
|
|
7541
|
-
args: []
|
|
7542
|
-
})), "write");
|
|
7543
|
-
});
|
|
7544
|
-
await this.init();
|
|
7026
|
+
async #lastPosition(tx, parentId, scopesOnly) {
|
|
7027
|
+
const rows = await this.#query(tx, `SELECT position FROM "${T.pages}" WHERE parentId IS ? AND isScope = ? AND deletedAt IS NULL ORDER BY position DESC, id DESC LIMIT 1`, [parentId, scopesOnly ? 1 : 0]);
|
|
7028
|
+
return rows[0] ? String(rows[0].position) : void 0;
|
|
7545
7029
|
}
|
|
7546
|
-
async
|
|
7547
|
-
|
|
7548
|
-
|
|
7549
|
-
|
|
7550
|
-
const
|
|
7551
|
-
const
|
|
7552
|
-
|
|
7553
|
-
|
|
7554
|
-
|
|
7555
|
-
|
|
7556
|
-
|
|
7557
|
-
|
|
7558
|
-
|
|
7559
|
-
|
|
7560
|
-
|
|
7561
|
-
|
|
7562
|
-
if (!row.isScope) throw new Error(`Knowledge address ${address} does not reference a scope`);
|
|
7563
|
-
if (row.deletedAt) deletedScopeAddresses.add(address);
|
|
7564
|
-
scopes[address] = String(row.scopeNodeId);
|
|
7565
|
-
return scopes[address];
|
|
7566
|
-
};
|
|
7567
|
-
for (const scope of plan.scopes) {
|
|
7568
|
-
if (await resolveAddress(scope.address)) continue;
|
|
7569
|
-
const id = randomUUID();
|
|
7570
|
-
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
7030
|
+
async materializeScope(input) {
|
|
7031
|
+
const core = await loadPagesCore();
|
|
7032
|
+
await assertKnowledgePageDescriptionWithinBound(input.description);
|
|
7033
|
+
return this.#transaction(async (tx) => {
|
|
7034
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
7035
|
+
const ensureScope = async (spec, parentId) => {
|
|
7036
|
+
await assertKnowledgePageDescriptionWithinBound(spec.description);
|
|
7037
|
+
const existing = (await this.#query(tx, `SELECT * FROM "${T.pages}" WHERE isScope = 1 AND address = ?`, [spec.address]))[0];
|
|
7038
|
+
if (existing) return {
|
|
7039
|
+
row: existing,
|
|
7040
|
+
created: false
|
|
7041
|
+
};
|
|
7042
|
+
if (parentId !== null) await this.#livePage(tx, parentId);
|
|
7043
|
+
const id = createKnowledgeUlid();
|
|
7044
|
+
const position = core.knowledgePositionBetween(await this.#lastPosition(tx, parentId, true));
|
|
7045
|
+
const who = JSON.stringify(input.provenance);
|
|
7571
7046
|
await tx.execute({
|
|
7572
|
-
sql: `INSERT INTO "${
|
|
7047
|
+
sql: `INSERT INTO "${T.pages}" (id, scopeId, isScope, address, declaration, parentId, position, path, depth, name, canonicalName, description, version, createdBy, updatedBy, createdAt, updatedAt) VALUES (?, ?, 1, ?, ?, ?, ?, ?, 0, ?, ?, ?, 1, ?, ?, ?, ?)`,
|
|
7573
7048
|
args: [
|
|
7574
7049
|
id,
|
|
7575
|
-
|
|
7576
|
-
|
|
7577
|
-
|
|
7578
|
-
|
|
7579
|
-
|
|
7050
|
+
id,
|
|
7051
|
+
spec.address,
|
|
7052
|
+
spec.declaration,
|
|
7053
|
+
parentId,
|
|
7054
|
+
position,
|
|
7055
|
+
`/${id}/`,
|
|
7056
|
+
spec.name,
|
|
7057
|
+
core.canonicalKnowledgeName(spec.name),
|
|
7058
|
+
spec.description ?? null,
|
|
7059
|
+
who,
|
|
7060
|
+
who,
|
|
7580
7061
|
now,
|
|
7581
7062
|
now
|
|
7582
7063
|
]
|
|
7583
7064
|
});
|
|
7065
|
+
await this.#logActivity(tx, "scope-created", {
|
|
7066
|
+
scopeId: id,
|
|
7067
|
+
pageId: id,
|
|
7068
|
+
provenance: input.provenance
|
|
7069
|
+
}, now);
|
|
7070
|
+
return {
|
|
7071
|
+
row: await this.#pageRow(tx, id),
|
|
7072
|
+
created: true
|
|
7073
|
+
};
|
|
7074
|
+
};
|
|
7075
|
+
const ensured = await ensureScope(input, input.parentId ?? null);
|
|
7076
|
+
let row = ensured.row;
|
|
7077
|
+
const created = ensured.created;
|
|
7078
|
+
const scopeId = String(row.id);
|
|
7079
|
+
if (!created && input.parentId != null && str(row.parentId) === void 0) {
|
|
7080
|
+
await this.#livePage(tx, input.parentId);
|
|
7081
|
+
if (input.parentId === scopeId) throw new Error(`Scope ${scopeId} cannot be its own parent`);
|
|
7082
|
+
let ancestor = input.parentId;
|
|
7083
|
+
while (ancestor) {
|
|
7084
|
+
const up = await this.#pageRow(tx, ancestor);
|
|
7085
|
+
if (String(up.id) === scopeId) throw new Error(`Scope ${scopeId} cannot be linked under its own descendant`);
|
|
7086
|
+
ancestor = str(up.parentId);
|
|
7087
|
+
}
|
|
7584
7088
|
await tx.execute({
|
|
7585
|
-
sql: `
|
|
7586
|
-
args: [
|
|
7089
|
+
sql: `UPDATE "${T.pages}" SET parentId = ?, position = ?, version = version + 1, updatedAt = ? WHERE id = ? AND parentId IS NULL`,
|
|
7090
|
+
args: [
|
|
7091
|
+
input.parentId,
|
|
7092
|
+
core.knowledgePositionBetween(await this.#lastPosition(tx, input.parentId, true)),
|
|
7093
|
+
now,
|
|
7094
|
+
scopeId
|
|
7095
|
+
]
|
|
7587
7096
|
});
|
|
7588
|
-
|
|
7589
|
-
createdAddresses.add(scope.address);
|
|
7590
|
-
createdScopeIds.push(id);
|
|
7591
|
-
structureChanged = true;
|
|
7097
|
+
row = await this.#pageRow(tx, scopeId);
|
|
7592
7098
|
}
|
|
7593
|
-
|
|
7594
|
-
|
|
7595
|
-
|
|
7596
|
-
|
|
7597
|
-
|
|
7598
|
-
|
|
7599
|
-
|
|
7600
|
-
|
|
7601
|
-
|
|
7602
|
-
|
|
7603
|
-
|
|
7604
|
-
|
|
7605
|
-
|
|
7606
|
-
|
|
7607
|
-
|
|
7608
|
-
|
|
7609
|
-
|
|
7610
|
-
|
|
7611
|
-
|
|
7612
|
-
args: [
|
|
7613
|
-
parentId,
|
|
7614
|
-
canonicalName(scope.name),
|
|
7615
|
-
scopeNodeId
|
|
7616
|
-
]
|
|
7617
|
-
})).rows.length) throw new Error(`Knowledge scope name ${scope.name} already exists under ${parentAddress}`);
|
|
7618
|
-
const inserted = await tx.execute({
|
|
7619
|
-
sql: `INSERT OR IGNORE INTO "${TABLE_KNOWLEDGE_NODE_SCOPES}" (nodeId,scopeNodeId,addedAt) VALUES (?,?,?)`,
|
|
7620
|
-
args: [
|
|
7621
|
-
scopeNodeId,
|
|
7622
|
-
parentId,
|
|
7623
|
-
(/* @__PURE__ */ new Date()).toISOString()
|
|
7624
|
-
]
|
|
7625
|
-
});
|
|
7626
|
-
structureChanged ||= inserted.rowsAffected > 0;
|
|
7627
|
-
}
|
|
7628
|
-
for (const grant of scope.grants ?? []) {
|
|
7629
|
-
const scopeRefId = await resolveAddress(grant.scopeRefAddress);
|
|
7630
|
-
if (!scopeRefId || deletedScopeAddresses.has(grant.scopeRefAddress)) {
|
|
7631
|
-
const deletedScopeRefId = scopes[grant.scopeRefAddress];
|
|
7632
|
-
if ((deletedScopeRefId ? await tx.execute({
|
|
7633
|
-
sql: `SELECT 1 FROM "mastra_knowledge_scope_grants" WHERE scopeNodeId=? AND scopeRefId=?`,
|
|
7634
|
-
args: [scopeNodeId, deletedScopeRefId]
|
|
7635
|
-
}) : void 0)?.rows.length) continue;
|
|
7636
|
-
throw new Error(`Knowledge grant scope does not exist: ${grant.scopeRefAddress}`);
|
|
7637
|
-
}
|
|
7638
|
-
const inserted = await tx.execute({
|
|
7639
|
-
sql: `INSERT OR IGNORE INTO "${TABLE_KNOWLEDGE_SCOPE_GRANTS}" (scopeNodeId,scopeRefId,role,canSuggest) VALUES (?,?,?,?)`,
|
|
7640
|
-
args: [
|
|
7641
|
-
scopeNodeId,
|
|
7642
|
-
scopeRefId,
|
|
7643
|
-
grant.role,
|
|
7644
|
-
grant.canSuggest ?? null
|
|
7645
|
-
]
|
|
7646
|
-
});
|
|
7647
|
-
structureChanged ||= inserted.rowsAffected > 0;
|
|
7099
|
+
let parentConflict;
|
|
7100
|
+
if (!created && input.parentId !== void 0 && (str(row.parentId) ?? null) !== input.parentId) parentConflict = {
|
|
7101
|
+
existingParentId: str(row.parentId) ?? null,
|
|
7102
|
+
requestedParentId: input.parentId
|
|
7103
|
+
};
|
|
7104
|
+
for (const child of input.children ?? []) await ensureScope(child, scopeId);
|
|
7105
|
+
const ensureTemplates = async (templates, parentId) => {
|
|
7106
|
+
for (const template of templates) {
|
|
7107
|
+
const problem = knowledgePageKeyProblem(template.key);
|
|
7108
|
+
if (problem) throw new Error(problem);
|
|
7109
|
+
const existing = (await this.#query(tx, `SELECT id FROM "${T.pages}" WHERE parentId = ? AND isScope = 0 AND key = ? AND deletedAt IS NULL`, [parentId, template.key]))[0];
|
|
7110
|
+
const id = existing ? String(existing.id) : (await this.#insertPage(tx, {
|
|
7111
|
+
parentId,
|
|
7112
|
+
key: template.key,
|
|
7113
|
+
name: template.name,
|
|
7114
|
+
description: template.description,
|
|
7115
|
+
provenance: input.provenance
|
|
7116
|
+
})).id;
|
|
7117
|
+
if (template.pages?.length) await ensureTemplates(template.pages, id);
|
|
7648
7118
|
}
|
|
7649
|
-
}
|
|
7650
|
-
|
|
7651
|
-
const state = await tx.execute(`SELECT epoch FROM "${TABLE_KNOWLEDGE_ACCESS_STATE}" WHERE id='global'`);
|
|
7119
|
+
};
|
|
7120
|
+
await ensureTemplates(input.pages ?? [], scopeId);
|
|
7652
7121
|
return {
|
|
7653
|
-
|
|
7654
|
-
|
|
7655
|
-
|
|
7656
|
-
changed: structureChanged,
|
|
7657
|
-
accessEpoch: Number(state.rows[0]?.epoch ?? 0)
|
|
7122
|
+
scope: toPage(await this.#pageRow(tx, scopeId)),
|
|
7123
|
+
created,
|
|
7124
|
+
...parentConflict ? { parentConflict } : {}
|
|
7658
7125
|
};
|
|
7126
|
+
});
|
|
7127
|
+
}
|
|
7128
|
+
async listScopeDeclarations() {
|
|
7129
|
+
return (await this.#query(this.#read, `SELECT declaration, COUNT(*) AS scopes FROM "${T.pages}" WHERE isScope = 1 AND declaration IS NOT NULL GROUP BY declaration ORDER BY declaration`)).map((row) => ({
|
|
7130
|
+
declaration: String(row.declaration),
|
|
7131
|
+
scopes: Number(row.scopes)
|
|
7659
7132
|
}));
|
|
7660
7133
|
}
|
|
7661
|
-
async
|
|
7662
|
-
|
|
7663
|
-
|
|
7664
|
-
|
|
7665
|
-
|
|
7134
|
+
async renameScopeDeclaration(input) {
|
|
7135
|
+
return this.#transaction(async (tx) => {
|
|
7136
|
+
const within = prefixRange("declaration", `${input.from}/`);
|
|
7137
|
+
const affected = (await this.#query(tx, `SELECT id, address, declaration FROM "${T.pages}" WHERE isScope = 1 AND (declaration = ? OR ${within.sql})`, [input.from, ...within.args])).map((row) => ({
|
|
7138
|
+
id: String(row.id),
|
|
7139
|
+
address: String(row.address),
|
|
7140
|
+
from: String(row.declaration)
|
|
7141
|
+
})).filter((row) => row.from === input.from || row.from.startsWith(`${input.from}/`)).map((row) => ({
|
|
7142
|
+
...row,
|
|
7143
|
+
to: input.to + row.from.slice(input.from.length)
|
|
7144
|
+
}));
|
|
7145
|
+
input.check(affected.map(({ address, from, to }) => ({
|
|
7146
|
+
address,
|
|
7147
|
+
from,
|
|
7148
|
+
to
|
|
7149
|
+
})));
|
|
7150
|
+
for (const row of affected) await tx.execute({
|
|
7151
|
+
sql: `UPDATE "${T.pages}" SET declaration = ? WHERE id = ?`,
|
|
7152
|
+
args: [row.to, row.id]
|
|
7153
|
+
});
|
|
7154
|
+
return affected.length;
|
|
7155
|
+
});
|
|
7156
|
+
}
|
|
7157
|
+
async getScopesByAddress(addresses) {
|
|
7158
|
+
if (addresses.length === 0) return [];
|
|
7159
|
+
return (await this.#query(this.#read, `SELECT * FROM "${T.pages}" WHERE isScope = 1 AND address IN (SELECT value FROM json_each(?))`, [jsonList(addresses)])).map(toPage);
|
|
7160
|
+
}
|
|
7161
|
+
async listChildScopes(input) {
|
|
7162
|
+
const core = await loadPagesCore();
|
|
7163
|
+
const limit = limitOf(input.limit, core.MAX_KNOWLEDGE_PAGE_LIST_LIMIT);
|
|
7164
|
+
const cursor = decodeCursor(input.cursor, 2);
|
|
7165
|
+
const args = [input.parentId];
|
|
7166
|
+
let after = "";
|
|
7167
|
+
if (cursor) {
|
|
7168
|
+
after = ` AND (position > ? OR (position = ? AND id > ?))`;
|
|
7169
|
+
args.push(cursor[0], cursor[0], cursor[1]);
|
|
7170
|
+
}
|
|
7171
|
+
args.push(limit + 1);
|
|
7172
|
+
const rows = await this.#query(this.#read, `SELECT * FROM "${T.pages}" WHERE isScope = 1 AND parentId IS ?${after} ORDER BY position, id LIMIT ?`, args);
|
|
7173
|
+
return this.#pageOutput(rows, limit, (row) => [String(row.position), String(row.id)]);
|
|
7174
|
+
}
|
|
7175
|
+
#pageOutput(rows, limit, cursorOf) {
|
|
7176
|
+
const page = rows.slice(0, limit);
|
|
7177
|
+
return {
|
|
7178
|
+
pages: page.map(toPage),
|
|
7179
|
+
...rows.length > limit ? { nextCursor: encodeCursor(cursorOf(page[page.length - 1])) } : {}
|
|
7666
7180
|
};
|
|
7667
|
-
|
|
7668
|
-
|
|
7669
|
-
|
|
7670
|
-
|
|
7671
|
-
|
|
7672
|
-
|
|
7673
|
-
|
|
7181
|
+
}
|
|
7182
|
+
async getPage(input) {
|
|
7183
|
+
const readable = pageReadable("p", input.read);
|
|
7184
|
+
const rows = await this.#query(this.#read, `SELECT p.* FROM "${T.pages}" p WHERE p.id = ? AND ${readable.sql}`, [input.id, ...readable.args]);
|
|
7185
|
+
return rows[0] ? toPage(rows[0]) : null;
|
|
7186
|
+
}
|
|
7187
|
+
async getPages(input) {
|
|
7188
|
+
const ids = [...new Set(input.ids)];
|
|
7189
|
+
if (ids.length === 0) return [];
|
|
7190
|
+
const readable = pageReadable("p", input.read);
|
|
7191
|
+
return (await this.#query(this.#read, `SELECT p.* FROM "${T.pages}" p WHERE p.deletedAt IS NULL AND p.id IN (SELECT value FROM json_each(?)) AND ${readable.sql}`, [jsonList(ids), ...readable.args])).map(toPage);
|
|
7192
|
+
}
|
|
7193
|
+
async getPageByPath(input) {
|
|
7194
|
+
const readable = pageReadable("p", input.read);
|
|
7195
|
+
let parentId = input.scopeId;
|
|
7196
|
+
let row;
|
|
7197
|
+
for (const key of input.path) {
|
|
7198
|
+
row = (await this.#query(this.#read, `SELECT p.* FROM "${T.pages}" p WHERE p.parentId = ? AND p.isScope = 0 AND p.key = ? AND p.deletedAt IS NULL AND ${readable.sql}`, [
|
|
7199
|
+
parentId,
|
|
7200
|
+
key,
|
|
7201
|
+
...readable.args
|
|
7202
|
+
]))[0];
|
|
7203
|
+
if (!row) return null;
|
|
7204
|
+
parentId = String(row.id);
|
|
7205
|
+
}
|
|
7206
|
+
return row ? toPage(row) : null;
|
|
7207
|
+
}
|
|
7208
|
+
async findPagesByName(input) {
|
|
7209
|
+
const core = await loadPagesCore();
|
|
7210
|
+
const readable = pageReadable("p", input.read);
|
|
7211
|
+
const args = [core.canonicalKnowledgeName(input.name), ...readable.args];
|
|
7212
|
+
let scope = "";
|
|
7213
|
+
if (input.scopeId !== void 0) {
|
|
7214
|
+
scope = ` AND p.scopeId = ?`;
|
|
7215
|
+
args.push(input.scopeId);
|
|
7216
|
+
}
|
|
7217
|
+
args.push(limitOf(input.limit, core.MAX_KNOWLEDGE_PAGE_LIST_LIMIT));
|
|
7218
|
+
return (await this.#query(this.#read, `SELECT p.* FROM "${T.pages}" p WHERE p.canonicalName = ? AND p.deletedAt IS NULL AND ${readable.sql}${scope} ORDER BY p.createdAt DESC, p.id DESC LIMIT ?`, args)).map(toPage);
|
|
7219
|
+
}
|
|
7220
|
+
async listChildPages(input) {
|
|
7221
|
+
const core = await loadPagesCore();
|
|
7222
|
+
const limit = limitOf(input.limit, core.MAX_KNOWLEDGE_PAGE_LIST_LIMIT);
|
|
7223
|
+
const cursor = decodeCursor(input.cursor, 2);
|
|
7224
|
+
const readable = pageReadable("p", input.read);
|
|
7225
|
+
const args = [input.parentId, ...readable.args];
|
|
7226
|
+
let after = "";
|
|
7227
|
+
if (cursor) {
|
|
7228
|
+
after = ` AND (p.position > ? OR (p.position = ? AND p.id > ?))`;
|
|
7229
|
+
args.push(cursor[0], cursor[0], cursor[1]);
|
|
7674
7230
|
}
|
|
7675
|
-
|
|
7676
|
-
|
|
7677
|
-
|
|
7231
|
+
args.push(limit + 1);
|
|
7232
|
+
const rows = await this.#query(this.#read, `SELECT p.* FROM "${T.pages}" p WHERE p.parentId = ? AND p.isScope = 0 AND p.deletedAt IS NULL AND ${readable.sql}${after} ORDER BY p.position, p.id LIMIT ?`, args);
|
|
7233
|
+
return this.#pageOutput(rows, limit, (row) => [String(row.position), String(row.id)]);
|
|
7234
|
+
}
|
|
7235
|
+
async listPages(input) {
|
|
7236
|
+
const core = await loadPagesCore();
|
|
7237
|
+
const limit = limitOf(input.limit, core.MAX_KNOWLEDGE_PAGE_LIST_LIMIT);
|
|
7238
|
+
const cursor = decodeCursor(input.cursor, 2);
|
|
7239
|
+
const readable = pageReadable("p", input.read);
|
|
7240
|
+
const args = [...readable.args];
|
|
7241
|
+
let filters = "";
|
|
7242
|
+
if (input.scopeId !== void 0) {
|
|
7243
|
+
filters += ` AND p.scopeId = ?`;
|
|
7244
|
+
args.push(input.scopeId);
|
|
7678
7245
|
}
|
|
7679
|
-
if (input.
|
|
7680
|
-
|
|
7681
|
-
|
|
7246
|
+
if (input.namePrefix) {
|
|
7247
|
+
const prefix = core.canonicalKnowledgeName(input.namePrefix);
|
|
7248
|
+
filters += ` AND substr(p.canonicalName, 1, length(?)) = ?`;
|
|
7249
|
+
args.push(prefix, prefix);
|
|
7682
7250
|
}
|
|
7683
|
-
if (
|
|
7684
|
-
|
|
7685
|
-
args.push(
|
|
7251
|
+
if (cursor) {
|
|
7252
|
+
filters += ` AND (p.name > ? OR (p.name = ? AND p.id > ?))`;
|
|
7253
|
+
args.push(cursor[0], cursor[0], cursor[1]);
|
|
7686
7254
|
}
|
|
7687
7255
|
args.push(limit + 1);
|
|
7688
|
-
const rows =
|
|
7689
|
-
|
|
7690
|
-
|
|
7691
|
-
|
|
7692
|
-
|
|
7693
|
-
|
|
7694
|
-
|
|
7695
|
-
|
|
7696
|
-
const
|
|
7697
|
-
|
|
7698
|
-
|
|
7256
|
+
const rows = await this.#query(this.#read, `SELECT p.* FROM "${T.pages}" p WHERE p.isScope = 0 AND p.deletedAt IS NULL AND ${readable.sql}${filters} ORDER BY p.name, p.id LIMIT ?`, args);
|
|
7257
|
+
return this.#pageOutput(rows, limit, (row) => [String(row.name), String(row.id)]);
|
|
7258
|
+
}
|
|
7259
|
+
async #insertPage(tx, input) {
|
|
7260
|
+
const core = await loadPagesCore();
|
|
7261
|
+
await assertKnowledgePageDescriptionWithinBound(input.description);
|
|
7262
|
+
const parent = await this.#livePage(tx, input.parentId);
|
|
7263
|
+
const isScopeParent = Number(parent.isScope) === 1;
|
|
7264
|
+
const scopeId = isScopeParent ? String(parent.id) : String(parent.scopeId);
|
|
7265
|
+
const depth = isScopeParent ? 1 : Number(parent.depth) + 1;
|
|
7266
|
+
if (depth > core.MAX_KNOWLEDGE_PAGE_DEPTH) throw new core.KnowledgePageDepthError(input.parentId);
|
|
7267
|
+
const id = input.id ?? createKnowledgeUlid();
|
|
7268
|
+
if (await this.#pageRow(tx, id)) throw new KnowledgeConflictError(id);
|
|
7269
|
+
if (input.key !== void 0) {
|
|
7270
|
+
const problem = knowledgePageKeyProblem(input.key);
|
|
7271
|
+
if (problem) throw new Error(problem);
|
|
7272
|
+
if ((await this.#query(tx, `SELECT id FROM "${T.pages}" WHERE parentId = ? AND isScope = 0 AND key = ? AND deletedAt IS NULL`, [String(parent.id), input.key])).length > 0) throw new core.KnowledgePageKeyConflictError(input.key, String(parent.id));
|
|
7273
|
+
}
|
|
7274
|
+
const position = input.position ?? core.knowledgePositionBetween(await this.#lastPosition(tx, String(parent.id), false));
|
|
7275
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
7276
|
+
const who = JSON.stringify(input.provenance);
|
|
7277
|
+
await tx.execute({
|
|
7278
|
+
sql: `INSERT INTO "${T.pages}" (id, scopeId, isScope, key, parentId, position, path, depth, name, canonicalName, description, body, version, createdBy, updatedBy, createdAt, updatedAt) VALUES (?, ?, 0, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?, ?, ?)`,
|
|
7279
|
+
args: [
|
|
7280
|
+
id,
|
|
7281
|
+
scopeId,
|
|
7282
|
+
input.key ?? null,
|
|
7283
|
+
String(parent.id),
|
|
7284
|
+
position,
|
|
7285
|
+
`${String(parent.path)}${id}/`,
|
|
7286
|
+
depth,
|
|
7287
|
+
input.name,
|
|
7288
|
+
core.canonicalKnowledgeName(input.name),
|
|
7289
|
+
input.description || null,
|
|
7290
|
+
input.body ?? null,
|
|
7291
|
+
who,
|
|
7292
|
+
who,
|
|
7293
|
+
now,
|
|
7294
|
+
now
|
|
7295
|
+
]
|
|
7699
7296
|
});
|
|
7700
|
-
|
|
7701
|
-
|
|
7702
|
-
|
|
7703
|
-
|
|
7704
|
-
|
|
7705
|
-
|
|
7706
|
-
|
|
7707
|
-
|
|
7708
|
-
|
|
7709
|
-
|
|
7710
|
-
|
|
7711
|
-
|
|
7712
|
-
|
|
7713
|
-
|
|
7714
|
-
parentIds: parentsByScopeId.get(String(row.id)) ?? []
|
|
7715
|
-
})), limit, input);
|
|
7716
|
-
}
|
|
7717
|
-
async listScopeMembers(input) {
|
|
7718
|
-
const { limit, after } = parseListKnowledgeScopeMembersInput(input);
|
|
7719
|
-
const args = [input.scopeNodeId];
|
|
7720
|
-
let keyset = "";
|
|
7721
|
-
if (after) {
|
|
7722
|
-
const updatedAt = after.updatedAt.toISOString();
|
|
7723
|
-
keyset = ` AND (n.updatedAt < ? OR (n.updatedAt = ? AND (n.name > ? OR (n.name = ? AND n.id > ?))))`;
|
|
7724
|
-
args.push(updatedAt, updatedAt, after.name, after.name, after.id);
|
|
7725
|
-
}
|
|
7726
|
-
args.push(limit + 1);
|
|
7727
|
-
return pageKnowledgeScopeMembers((await this.#client.execute({
|
|
7728
|
-
sql: `SELECT *, json(scope) AS scopeJson FROM "${TABLE_KNOWLEDGE_NODES}" n WHERE EXISTS (SELECT 1 FROM "${TABLE_KNOWLEDGE_NODE_SCOPES}" ns WHERE ns.nodeId=n.id AND ns.scopeNodeId=?) AND n.deletedAt IS NULL AND n.mergedInto IS NULL${keyset} ORDER BY n.updatedAt DESC, n.name ASC, n.id ASC LIMIT ?`,
|
|
7729
|
-
args
|
|
7730
|
-
})).rows.map(parseNode), limit, input);
|
|
7731
|
-
}
|
|
7732
|
-
async createNode(input) {
|
|
7733
|
-
await assertKnowledgeDescriptionWithinBoundCompat(input.description);
|
|
7734
|
-
const scope = canonicalizeKnowledgeScope(input.scope);
|
|
7297
|
+
await this.#logActivity(tx, "page-created", {
|
|
7298
|
+
scopeId,
|
|
7299
|
+
pageId: id,
|
|
7300
|
+
provenance: input.provenance
|
|
7301
|
+
}, now);
|
|
7302
|
+
return toPage(await this.#pageRow(tx, id));
|
|
7303
|
+
}
|
|
7304
|
+
async createPage(input) {
|
|
7305
|
+
await assertKnowledgePageDescriptionWithinBound(input.description);
|
|
7306
|
+
return this.#transaction((tx) => this.#insertPage(tx, input));
|
|
7307
|
+
}
|
|
7308
|
+
async updatePage(input) {
|
|
7309
|
+
const core = await loadPagesCore();
|
|
7310
|
+
await assertKnowledgePageDescriptionWithinBound(input.description);
|
|
7735
7311
|
return this.#transaction(async (tx) => {
|
|
7736
|
-
const
|
|
7737
|
-
if (
|
|
7738
|
-
|
|
7739
|
-
|
|
7740
|
-
|
|
7741
|
-
|
|
7742
|
-
}
|
|
7743
|
-
const now = /* @__PURE__ */ new Date();
|
|
7744
|
-
const node = {
|
|
7745
|
-
id: input.id ?? crypto.randomUUID(),
|
|
7746
|
-
type: "node",
|
|
7747
|
-
name: input.name.trim(),
|
|
7748
|
-
kind: input.kind,
|
|
7749
|
-
content: input.content,
|
|
7750
|
-
description: input.description,
|
|
7751
|
-
scope,
|
|
7752
|
-
version: 1,
|
|
7753
|
-
createdAt: now,
|
|
7754
|
-
updatedAt: now
|
|
7755
|
-
};
|
|
7312
|
+
const row = await this.#livePage(tx, input.id);
|
|
7313
|
+
if (Number(row.version) !== input.expectedVersion) throw new KnowledgeConflictError(input.id);
|
|
7314
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
7315
|
+
const name = input.name ?? String(row.name);
|
|
7316
|
+
const description = input.description === void 0 ? row.description ?? null : input.description || null;
|
|
7317
|
+
const body = input.body === void 0 ? row.body ?? null : input.body;
|
|
7756
7318
|
await tx.execute({
|
|
7757
|
-
sql: `
|
|
7319
|
+
sql: `UPDATE "${T.pages}" SET name = ?, canonicalName = ?, description = ?, body = ?, version = version + 1, updatedBy = ?, updatedAt = ? WHERE id = ?`,
|
|
7758
7320
|
args: [
|
|
7759
|
-
|
|
7760
|
-
|
|
7761
|
-
|
|
7762
|
-
|
|
7763
|
-
|
|
7764
|
-
|
|
7765
|
-
|
|
7766
|
-
JSON.stringify(scope),
|
|
7767
|
-
knowledgeScopeKey(scope),
|
|
7768
|
-
node.version,
|
|
7769
|
-
now.toISOString(),
|
|
7770
|
-
now.toISOString()
|
|
7321
|
+
name,
|
|
7322
|
+
core.canonicalKnowledgeName(name),
|
|
7323
|
+
description,
|
|
7324
|
+
body,
|
|
7325
|
+
JSON.stringify(input.provenance),
|
|
7326
|
+
now,
|
|
7327
|
+
input.id
|
|
7771
7328
|
]
|
|
7772
7329
|
});
|
|
7773
|
-
await this.#
|
|
7774
|
-
|
|
7775
|
-
|
|
7776
|
-
|
|
7777
|
-
|
|
7778
|
-
return
|
|
7330
|
+
await this.#logActivity(tx, "page-updated", {
|
|
7331
|
+
scopeId: String(row.scopeId),
|
|
7332
|
+
pageId: input.id,
|
|
7333
|
+
provenance: input.provenance
|
|
7334
|
+
}, now);
|
|
7335
|
+
return toPage(await this.#pageRow(tx, input.id));
|
|
7779
7336
|
});
|
|
7780
7337
|
}
|
|
7781
|
-
|
|
7782
|
-
|
|
7783
|
-
|
|
7784
|
-
|
|
7785
|
-
|
|
7786
|
-
|
|
7787
|
-
|
|
7788
|
-
|
|
7789
|
-
|
|
7790
|
-
|
|
7791
|
-
const
|
|
7792
|
-
const
|
|
7793
|
-
|
|
7794
|
-
|
|
7795
|
-
|
|
7796
|
-
|
|
7797
|
-
|
|
7798
|
-
|
|
7799
|
-
|
|
7800
|
-
|
|
7801
|
-
|
|
7802
|
-
|
|
7803
|
-
|
|
7804
|
-
|
|
7805
|
-
|
|
7806
|
-
|
|
7807
|
-
|
|
7808
|
-
|
|
7809
|
-
|
|
7810
|
-
|
|
7811
|
-
|
|
7812
|
-
|
|
7813
|
-
|
|
7814
|
-
|
|
7815
|
-
|
|
7816
|
-
|
|
7817
|
-
|
|
7818
|
-
|
|
7819
|
-
|
|
7820
|
-
|
|
7821
|
-
|
|
7338
|
+
/**
|
|
7339
|
+
* Rehomes `root` (and its subtree) under `parent`: new path prefix, scope and depth in one go.
|
|
7340
|
+
* Throws when the subtree is too large, too deep, or the root's key clashes with a live child of `parent`.
|
|
7341
|
+
*/
|
|
7342
|
+
async #rehome(tx, root, parent, options) {
|
|
7343
|
+
const core = await loadPagesCore();
|
|
7344
|
+
const isScopeParent = Number(parent.isScope) === 1;
|
|
7345
|
+
const newScopeId = isScopeParent ? String(parent.id) : String(parent.scopeId);
|
|
7346
|
+
const newDepth = isScopeParent ? 1 : Number(parent.depth) + 1;
|
|
7347
|
+
const oldPath = String(root.path);
|
|
7348
|
+
const within = prefixRange("path", oldPath);
|
|
7349
|
+
const live = (await this.#query(tx, `SELECT id, key, depth, deletedAt FROM "${T.pages}" WHERE ${within.sql}`, within.args)).filter((row) => !row.deletedAt);
|
|
7350
|
+
if (live.length > core.MAX_KNOWLEDGE_MOVE_PAGES) throw new core.KnowledgeMoveTooLargeError(options.countLimitId, live.length);
|
|
7351
|
+
const shift = newDepth - Number(root.depth);
|
|
7352
|
+
if (Math.max(...live.map((row) => Number(row.depth))) + shift > core.MAX_KNOWLEDGE_PAGE_DEPTH) throw new core.KnowledgePageDepthError(String(root.id));
|
|
7353
|
+
if (!root.deletedAt && root.key !== null) {
|
|
7354
|
+
if ((await this.#query(tx, `SELECT id FROM "${T.pages}" WHERE parentId = ? AND isScope = 0 AND key = ? AND deletedAt IS NULL AND id <> ?`, [
|
|
7355
|
+
String(parent.id),
|
|
7356
|
+
String(root.key),
|
|
7357
|
+
String(root.id)
|
|
7358
|
+
])).length > 0) throw new core.KnowledgePageKeyConflictError(String(root.key), String(parent.id));
|
|
7359
|
+
}
|
|
7360
|
+
const newPath = `${String(parent.path)}${String(root.id)}/`;
|
|
7361
|
+
const position = options.position ?? core.knowledgePositionBetween(await this.#lastPosition(tx, String(parent.id), false));
|
|
7362
|
+
await tx.execute({
|
|
7363
|
+
sql: `UPDATE "${T.pages}" SET path = ? || substr(path, length(?) + 1), depth = depth + ?, scopeId = ? WHERE ${within.sql}`,
|
|
7364
|
+
args: [
|
|
7365
|
+
newPath,
|
|
7366
|
+
oldPath,
|
|
7367
|
+
shift,
|
|
7368
|
+
newScopeId,
|
|
7369
|
+
...within.args
|
|
7370
|
+
]
|
|
7371
|
+
});
|
|
7372
|
+
await tx.execute({
|
|
7373
|
+
sql: `UPDATE "${T.pages}" SET parentId = ?, position = ? WHERE id = ?`,
|
|
7374
|
+
args: [
|
|
7375
|
+
String(parent.id),
|
|
7376
|
+
position,
|
|
7377
|
+
String(root.id)
|
|
7378
|
+
]
|
|
7379
|
+
});
|
|
7380
|
+
return {
|
|
7381
|
+
moved: live.length,
|
|
7382
|
+
scopeId: newScopeId
|
|
7383
|
+
};
|
|
7822
7384
|
}
|
|
7823
|
-
|
|
7824
|
-
await assertKnowledgeDescriptionWithinBoundCompat(input.description);
|
|
7385
|
+
movePage(input) {
|
|
7825
7386
|
return this.#transaction(async (tx) => {
|
|
7826
|
-
const
|
|
7827
|
-
if (
|
|
7828
|
-
if (
|
|
7829
|
-
const
|
|
7830
|
-
const
|
|
7831
|
-
|
|
7832
|
-
const
|
|
7833
|
-
const
|
|
7834
|
-
|
|
7835
|
-
|
|
7387
|
+
const row = await this.#livePage(tx, input.id);
|
|
7388
|
+
if (Number(row.isScope) === 1) throw new Error(`Cannot move ${input.id}: it is a scope page`);
|
|
7389
|
+
if (Number(row.version) !== input.expectedVersion) throw new KnowledgeConflictError(input.id);
|
|
7390
|
+
const parent = await this.#livePage(tx, input.parentId);
|
|
7391
|
+
const path = String(row.path);
|
|
7392
|
+
if (String(parent.id) === input.id || String(parent.path).startsWith(path)) throw new Error(`Cannot move ${input.id} into its own subtree`);
|
|
7393
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
7394
|
+
const { scopeId } = await this.#rehome(tx, row, parent, {
|
|
7395
|
+
position: input.position,
|
|
7396
|
+
countLimitId: input.id
|
|
7397
|
+
});
|
|
7398
|
+
await tx.execute({
|
|
7399
|
+
sql: `UPDATE "${T.pages}" SET version = version + 1, updatedBy = ?, updatedAt = ? WHERE id = ?`,
|
|
7836
7400
|
args: [
|
|
7837
|
-
|
|
7838
|
-
|
|
7839
|
-
input.
|
|
7840
|
-
content ?? null,
|
|
7841
|
-
description ?? null,
|
|
7842
|
-
JSON.stringify(scope),
|
|
7843
|
-
knowledgeScopeKey(scope),
|
|
7844
|
-
now.toISOString(),
|
|
7845
|
-
input.id,
|
|
7846
|
-
input.version
|
|
7401
|
+
JSON.stringify(input.provenance),
|
|
7402
|
+
now,
|
|
7403
|
+
input.id
|
|
7847
7404
|
]
|
|
7848
|
-
})
|
|
7849
|
-
|
|
7850
|
-
|
|
7851
|
-
|
|
7852
|
-
|
|
7853
|
-
|
|
7854
|
-
|
|
7855
|
-
sql: `SELECT id,json(scope) AS scopeJson,deletedAt FROM "${TABLE_KNOWLEDGE_RECORDS}" WHERE node=?`,
|
|
7856
|
-
args: [input.id]
|
|
7857
|
-
});
|
|
7858
|
-
for (const row of knowledge.rows) {
|
|
7859
|
-
const recordScope = parseJson$3(row.scopeJson);
|
|
7860
|
-
await this.#outbox(tx, "record", String(row.id), "delete", createKnowledgeUlid(), recordScope);
|
|
7861
|
-
if (row.deletedAt == null) await this.#outbox(tx, "record", String(row.id), "upsert", createKnowledgeUlid(), recordScope);
|
|
7862
|
-
}
|
|
7863
|
-
}
|
|
7864
|
-
await this.#outbox(tx, "node", input.id, "upsert", input.version + 1, scope);
|
|
7865
|
-
return {
|
|
7866
|
-
...existing,
|
|
7867
|
-
name,
|
|
7868
|
-
kind: input.kind ?? existing.kind,
|
|
7869
|
-
content,
|
|
7870
|
-
description,
|
|
7871
|
-
scope,
|
|
7872
|
-
version: input.version + 1,
|
|
7873
|
-
updatedAt: now
|
|
7874
|
-
};
|
|
7405
|
+
});
|
|
7406
|
+
await this.#logActivity(tx, "page-moved", {
|
|
7407
|
+
scopeId,
|
|
7408
|
+
pageId: input.id,
|
|
7409
|
+
provenance: input.provenance
|
|
7410
|
+
}, now);
|
|
7411
|
+
return toPage(await this.#pageRow(tx, input.id));
|
|
7875
7412
|
});
|
|
7876
7413
|
}
|
|
7877
|
-
async
|
|
7878
|
-
|
|
7414
|
+
async mergePages(input) {
|
|
7415
|
+
const core = await loadPagesCore();
|
|
7879
7416
|
return this.#transaction(async (tx) => {
|
|
7880
|
-
const source = await this.#
|
|
7881
|
-
|
|
7882
|
-
|
|
7883
|
-
if (
|
|
7884
|
-
|
|
7885
|
-
if (
|
|
7886
|
-
const
|
|
7887
|
-
|
|
7888
|
-
|
|
7889
|
-
|
|
7890
|
-
const movedKnowledge = await tx.execute({
|
|
7891
|
-
sql: `SELECT id,json(scope) AS scopeJson,deletedAt FROM "${TABLE_KNOWLEDGE_RECORDS}" WHERE node=?`,
|
|
7892
|
-
args: [source.id]
|
|
7893
|
-
});
|
|
7894
|
-
if ((await tx.execute({
|
|
7895
|
-
sql: `UPDATE "${TABLE_KNOWLEDGE_NODES}" SET mergedInto=?,version=version+1,updatedAt=? WHERE id=? AND type='node' AND version=? AND mergedInto IS NULL`,
|
|
7896
|
-
args: [
|
|
7897
|
-
target.id,
|
|
7898
|
-
(/* @__PURE__ */ new Date()).toISOString(),
|
|
7899
|
-
source.id,
|
|
7900
|
-
input.sourceVersion
|
|
7901
|
-
]
|
|
7902
|
-
})).rowsAffected === 0) throw new KnowledgeConflictError(source.id);
|
|
7417
|
+
const source = await this.#livePage(tx, input.sourceId);
|
|
7418
|
+
const target = await this.#livePage(tx, input.targetId);
|
|
7419
|
+
if (Number(source.isScope) === 1 || Number(target.isScope) === 1) throw new Error("Cannot merge a scope page: merging applies to non-scope pages only");
|
|
7420
|
+
if (Number(source.version) !== input.sourceVersion) throw new KnowledgeConflictError(input.sourceId);
|
|
7421
|
+
const sourcePath = String(source.path);
|
|
7422
|
+
if (input.sourceId === input.targetId || String(target.path).startsWith(sourcePath)) throw new Error(`Page ${input.targetId} is inside page ${input.sourceId}; it cannot be the merge target (subtree)`);
|
|
7423
|
+
const within = prefixRange("path", sourcePath);
|
|
7424
|
+
const carried = await this.#query(tx, `SELECT COUNT(*) AS n FROM "${T.pages}" WHERE ${within.sql} AND id <> ? AND deletedAt IS NULL`, [...within.args, input.sourceId]);
|
|
7425
|
+
if (Number(carried[0].n) > core.MAX_KNOWLEDGE_MOVE_PAGES) throw new core.KnowledgeMoveTooLargeError(input.sourceId, Number(carried[0].n));
|
|
7426
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
7903
7427
|
await tx.execute({
|
|
7904
|
-
sql: `UPDATE "${
|
|
7428
|
+
sql: `UPDATE "${T.pages}" SET deletedAt = ?, deletedBy = ?, mergedInto = ?, scopeId = ?, version = version + 1, updatedBy = ?, updatedAt = ? WHERE id = ?`,
|
|
7905
7429
|
args: [
|
|
7906
|
-
|
|
7907
|
-
|
|
7908
|
-
|
|
7430
|
+
now,
|
|
7431
|
+
input.provenance.author,
|
|
7432
|
+
input.targetId,
|
|
7433
|
+
String(target.scopeId),
|
|
7434
|
+
JSON.stringify(input.provenance),
|
|
7435
|
+
now,
|
|
7436
|
+
input.sourceId
|
|
7909
7437
|
]
|
|
7910
7438
|
});
|
|
7439
|
+
const children = await this.#query(tx, `SELECT * FROM "${T.pages}" WHERE parentId = ? AND isScope = 0`, [input.sourceId]);
|
|
7440
|
+
for (const child of children) await this.#rehome(tx, child, target, {
|
|
7441
|
+
position: String(child.position),
|
|
7442
|
+
countLimitId: input.sourceId
|
|
7443
|
+
});
|
|
7911
7444
|
await tx.execute({
|
|
7912
|
-
sql: `
|
|
7913
|
-
args: [
|
|
7445
|
+
sql: `UPDATE "${T.notes}" SET pageId = ? WHERE pageId = ?`,
|
|
7446
|
+
args: [input.targetId, input.sourceId]
|
|
7914
7447
|
});
|
|
7915
7448
|
await tx.execute({
|
|
7916
|
-
sql: `UPDATE "${
|
|
7917
|
-
args: [
|
|
7449
|
+
sql: `UPDATE OR IGNORE "${T.mentions}" SET pageId = ? WHERE pageId = ?`,
|
|
7450
|
+
args: [input.targetId, input.sourceId]
|
|
7918
7451
|
});
|
|
7919
|
-
|
|
7920
|
-
|
|
7921
|
-
|
|
7922
|
-
|
|
7923
|
-
|
|
7924
|
-
|
|
7925
|
-
|
|
7452
|
+
await tx.execute({
|
|
7453
|
+
sql: `DELETE FROM "${T.mentions}" WHERE pageId = ?`,
|
|
7454
|
+
args: [input.sourceId]
|
|
7455
|
+
});
|
|
7456
|
+
const pins = await this.#query(tx, `SELECT * FROM "${T.pins}" WHERE pageId IN (?, ?)`, [input.sourceId, input.targetId]);
|
|
7457
|
+
const sourcePin = pins.find((pin) => String(pin.pageId) === input.sourceId);
|
|
7458
|
+
if (sourcePin) {
|
|
7459
|
+
const targetPin = pins.find((pin) => String(pin.pageId) === input.targetId);
|
|
7460
|
+
const audience = JSON.parse(String(sourcePin.audience));
|
|
7461
|
+
if (targetPin) {
|
|
7462
|
+
const merged = [.../* @__PURE__ */ new Set([...JSON.parse(String(targetPin.audience)), ...audience])];
|
|
7463
|
+
await tx.execute({
|
|
7464
|
+
sql: `UPDATE "${T.pins}" SET audience = ?, updatedAt = ? WHERE id = ?`,
|
|
7465
|
+
args: [
|
|
7466
|
+
JSON.stringify(merged),
|
|
7467
|
+
now,
|
|
7468
|
+
String(targetPin.id)
|
|
7469
|
+
]
|
|
7470
|
+
});
|
|
7471
|
+
await this.#indexAudience(tx, String(targetPin.id), merged);
|
|
7472
|
+
await this.#indexAudience(tx, String(sourcePin.id), []);
|
|
7473
|
+
await tx.execute({
|
|
7474
|
+
sql: `DELETE FROM "${T.pins}" WHERE id = ?`,
|
|
7475
|
+
args: [String(sourcePin.id)]
|
|
7476
|
+
});
|
|
7477
|
+
} else await tx.execute({
|
|
7478
|
+
sql: `UPDATE "${T.pins}" SET pageId = ?, updatedAt = ? WHERE id = ?`,
|
|
7926
7479
|
args: [
|
|
7927
|
-
|
|
7928
|
-
|
|
7929
|
-
|
|
7930
|
-
target.version
|
|
7480
|
+
input.targetId,
|
|
7481
|
+
now,
|
|
7482
|
+
String(sourcePin.id)
|
|
7931
7483
|
]
|
|
7932
|
-
})
|
|
7933
|
-
mergedTarget = {
|
|
7934
|
-
...target,
|
|
7935
|
-
description: source.description,
|
|
7936
|
-
version: target.version + 1,
|
|
7937
|
-
updatedAt: adoptedAt
|
|
7938
|
-
};
|
|
7939
|
-
await this.#activity(tx, "node-updated", "node", target.id, target.scope);
|
|
7940
|
-
}
|
|
7484
|
+
});
|
|
7941
7485
|
}
|
|
7942
|
-
await this.#
|
|
7943
|
-
|
|
7944
|
-
|
|
7945
|
-
|
|
7486
|
+
await this.#logActivity(tx, "page-merged", {
|
|
7487
|
+
scopeId: String(target.scopeId),
|
|
7488
|
+
pageId: input.sourceId,
|
|
7489
|
+
provenance: input.provenance
|
|
7490
|
+
}, now);
|
|
7491
|
+
return toPage(await this.#pageRow(tx, input.targetId));
|
|
7946
7492
|
});
|
|
7947
7493
|
}
|
|
7948
|
-
async
|
|
7949
|
-
await
|
|
7950
|
-
const scope = canonicalizeKnowledgeScope(input.scope);
|
|
7951
|
-
const resolutionScope = canonicalizeKnowledgeScope(input.resolutionScope);
|
|
7952
|
-
const defaultScope = canonicalizeKnowledgeScope(input.defaultScope);
|
|
7494
|
+
async deletePage(input) {
|
|
7495
|
+
const core = await loadPagesCore();
|
|
7953
7496
|
return this.#transaction(async (tx) => {
|
|
7954
|
-
const
|
|
7955
|
-
|
|
7956
|
-
if (
|
|
7957
|
-
|
|
7958
|
-
|
|
7959
|
-
node: parent.id,
|
|
7960
|
-
text: input.text,
|
|
7961
|
-
scope,
|
|
7962
|
-
sourceThreadId: input.sourceThreadId,
|
|
7963
|
-
capturedAt: /* @__PURE__ */ new Date(),
|
|
7964
|
-
when: input.when ? new Date(input.when) : void 0,
|
|
7965
|
-
metadata: input.metadata
|
|
7966
|
-
};
|
|
7497
|
+
const row = await this.#livePage(tx, input.id);
|
|
7498
|
+
if (Number(row.isScope) === 1) throw new Error(`Cannot delete ${input.id}: it is a scope page`);
|
|
7499
|
+
if (Number(row.version) !== input.expectedVersion) throw new KnowledgeConflictError(input.id);
|
|
7500
|
+
if ((await this.#query(tx, `SELECT 1 FROM "${T.pages}" WHERE parentId = ? AND deletedAt IS NULL LIMIT 1`, [input.id])).length > 0) throw new core.KnowledgePageHasChildrenError(input.id);
|
|
7501
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
7967
7502
|
await tx.execute({
|
|
7968
|
-
sql: `
|
|
7503
|
+
sql: `UPDATE "${T.pages}" SET deletedAt = ?, deletedBy = ?, version = version + 1, updatedBy = ?, updatedAt = ? WHERE id = ?`,
|
|
7969
7504
|
args: [
|
|
7970
|
-
|
|
7971
|
-
|
|
7972
|
-
|
|
7973
|
-
|
|
7974
|
-
|
|
7975
|
-
record.sourceThreadId,
|
|
7976
|
-
record.capturedAt.toISOString(),
|
|
7977
|
-
record.when?.toISOString() ?? null,
|
|
7978
|
-
record.metadata ? JSON.stringify(record.metadata) : null,
|
|
7979
|
-
1,
|
|
7980
|
-
record.capturedAt.toISOString()
|
|
7505
|
+
now,
|
|
7506
|
+
input.deletedBy,
|
|
7507
|
+
JSON.stringify({ author: input.deletedBy }),
|
|
7508
|
+
now,
|
|
7509
|
+
input.id
|
|
7981
7510
|
]
|
|
7982
7511
|
});
|
|
7983
|
-
await
|
|
7984
|
-
|
|
7985
|
-
|
|
7986
|
-
|
|
7987
|
-
|
|
7988
|
-
|
|
7989
|
-
|
|
7990
|
-
|
|
7991
|
-
|
|
7992
|
-
|
|
7993
|
-
|
|
7512
|
+
await tx.execute({
|
|
7513
|
+
sql: `DELETE FROM "${T.pinAudiences}" WHERE pinId IN (SELECT id FROM "${T.pins}" WHERE pageId = ?)`,
|
|
7514
|
+
args: [input.id]
|
|
7515
|
+
});
|
|
7516
|
+
await tx.execute({
|
|
7517
|
+
sql: `DELETE FROM "${T.pins}" WHERE pageId = ?`,
|
|
7518
|
+
args: [input.id]
|
|
7519
|
+
});
|
|
7520
|
+
await this.#logActivity(tx, "page-deleted", {
|
|
7521
|
+
scopeId: String(row.scopeId),
|
|
7522
|
+
pageId: input.id,
|
|
7523
|
+
provenance: { author: input.deletedBy }
|
|
7524
|
+
}, now);
|
|
7525
|
+
return toPage(await this.#pageRow(tx, input.id));
|
|
7994
7526
|
});
|
|
7995
|
-
return result.rows[0] ? parseKnowledge(result.rows[0]) : null;
|
|
7996
|
-
}
|
|
7997
|
-
async listKnowledgeAbout(input) {
|
|
7998
|
-
return this.#queryKnowledge(input, "about");
|
|
7999
|
-
}
|
|
8000
|
-
async listKnowledgeMentioning(input) {
|
|
8001
|
-
return this.#queryKnowledge(input, "mentioning");
|
|
8002
|
-
}
|
|
8003
|
-
async listKnowledgeRelatedTo(input) {
|
|
8004
|
-
return this.#queryKnowledge(input, "related");
|
|
8005
7527
|
}
|
|
8006
|
-
async
|
|
8007
|
-
|
|
8008
|
-
const
|
|
8009
|
-
|
|
8010
|
-
const
|
|
7528
|
+
async #hydrateNotes(executor, rows) {
|
|
7529
|
+
if (rows.length === 0) return [];
|
|
7530
|
+
const ids = jsonList(rows.map((row) => String(row.id)));
|
|
7531
|
+
const stamps = await this.#query(executor, `SELECT noteId, scopeId FROM "${T.stamps}" WHERE noteId IN (SELECT value FROM json_each(?)) ORDER BY scopeId`, [ids]);
|
|
7532
|
+
const mentions = await this.#query(executor, `SELECT noteId, pageId FROM "${T.mentions}" WHERE noteId IN (SELECT value FROM json_each(?)) ORDER BY pageId`, [ids]);
|
|
7533
|
+
return rows.map((row) => toNote(row, stamps.filter((stamp) => String(stamp.noteId) === String(row.id)).map((stamp) => String(stamp.scopeId)), mentions.filter((mention) => String(mention.noteId) === String(row.id)).map((mention) => String(mention.pageId))));
|
|
7534
|
+
}
|
|
7535
|
+
async #noteById(executor, id) {
|
|
7536
|
+
const rows = await this.#query(executor, `SELECT * FROM "${T.notes}" WHERE id = ?`, [id]);
|
|
7537
|
+
return (await this.#hydrateNotes(executor, rows))[0] ?? null;
|
|
7538
|
+
}
|
|
7539
|
+
async getNote(input) {
|
|
7540
|
+
const readable = noteReadable("n", input.read);
|
|
7541
|
+
const rows = await this.#query(this.#read, `SELECT n.* FROM "${T.notes}" n WHERE n.id = ?${input.includeDeleted ? "" : " AND n.deletedAt IS NULL"} AND ${readable.sql}`, [input.id, ...readable.args]);
|
|
7542
|
+
return (await this.#hydrateNotes(this.#read, rows))[0] ?? null;
|
|
7543
|
+
}
|
|
7544
|
+
async listNotes(input) {
|
|
7545
|
+
const core = await loadPagesCore();
|
|
7546
|
+
const limit = limitOf(input.limit, core.MAX_KNOWLEDGE_PAGE_LIST_LIMIT);
|
|
7547
|
+
const cursor = decodeCursor(input.cursor, 2);
|
|
7548
|
+
const readable = noteReadable("n", input.read);
|
|
7549
|
+
const args = [input.pageId, ...readable.args];
|
|
7550
|
+
let after = "";
|
|
7551
|
+
if (cursor) {
|
|
7552
|
+
after = ` AND (n.createdAt > ? OR (n.createdAt = ? AND n.id > ?))`;
|
|
7553
|
+
args.push(cursor[0], cursor[0], cursor[1]);
|
|
7554
|
+
}
|
|
8011
7555
|
args.push(limit + 1);
|
|
8012
|
-
const
|
|
8013
|
-
|
|
8014
|
-
|
|
8015
|
-
|
|
7556
|
+
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);
|
|
7557
|
+
return this.#noteOutput(rows, limit);
|
|
7558
|
+
}
|
|
7559
|
+
async countChildPages(input) {
|
|
7560
|
+
const ids = [...new Set(input.parentIds)];
|
|
7561
|
+
const counts = Object.fromEntries(ids.map((id) => [id, 0]));
|
|
7562
|
+
if (ids.length === 0) return counts;
|
|
7563
|
+
const readable = pageReadable("p", input.read);
|
|
7564
|
+
const rows = await this.#query(this.#read, `SELECT p.parentId AS parentId, COUNT(*) AS total FROM "${T.pages}" p WHERE p.parentId IN (SELECT value FROM json_each(?)) AND p.isScope = 0 AND p.deletedAt IS NULL AND ${readable.sql} GROUP BY p.parentId`, [jsonList(ids), ...readable.args]);
|
|
7565
|
+
for (const row of rows) counts[String(row.parentId)] = Number(row.total);
|
|
7566
|
+
return counts;
|
|
7567
|
+
}
|
|
7568
|
+
async countNotes(input) {
|
|
7569
|
+
const ids = [...new Set(input.pageIds)];
|
|
7570
|
+
const counts = Object.fromEntries(ids.map((id) => [id, 0]));
|
|
7571
|
+
if (ids.length === 0) return counts;
|
|
7572
|
+
const readable = noteReadable("n", input.read);
|
|
7573
|
+
const rows = await this.#query(this.#read, `SELECT n.pageId AS pageId, COUNT(*) AS total FROM "${T.notes}" n WHERE n.pageId IN (SELECT value FROM json_each(?)) AND n.deletedAt IS NULL AND ${readable.sql} GROUP BY n.pageId`, [jsonList(ids), ...readable.args]);
|
|
7574
|
+
for (const row of rows) counts[String(row.pageId)] = Number(row.total);
|
|
7575
|
+
return counts;
|
|
7576
|
+
}
|
|
7577
|
+
async listNotesMentioning(input) {
|
|
7578
|
+
const core = await loadPagesCore();
|
|
7579
|
+
const limit = limitOf(input.limit, core.MAX_KNOWLEDGE_PAGE_LIST_LIMIT);
|
|
7580
|
+
const cursor = decodeCursor(input.cursor, 2);
|
|
7581
|
+
const readable = noteReadable("n", input.read);
|
|
7582
|
+
const args = [
|
|
7583
|
+
input.pageId,
|
|
7584
|
+
input.pageId,
|
|
7585
|
+
...readable.args
|
|
7586
|
+
];
|
|
7587
|
+
let after = "";
|
|
7588
|
+
if (cursor) {
|
|
7589
|
+
after = ` AND (n.createdAt < ? OR (n.createdAt = ? AND n.id < ?))`;
|
|
7590
|
+
args.push(cursor[0], cursor[0], cursor[1]);
|
|
7591
|
+
}
|
|
7592
|
+
args.push(limit + 1);
|
|
7593
|
+
const rows = await this.#query(this.#read, `SELECT n.* FROM "${T.notes}" n WHERE n.pageId <> ? AND n.deletedAt IS NULL AND EXISTS (SELECT 1 FROM "${T.mentions}" m WHERE m.noteId = n.id AND m.pageId = ?) AND ${readable.sql}${after} ORDER BY n.createdAt DESC, n.id DESC LIMIT ?`, args);
|
|
7594
|
+
return this.#noteOutput(rows, limit);
|
|
7595
|
+
}
|
|
7596
|
+
async #noteOutput(rows, limit) {
|
|
7597
|
+
const page = rows.slice(0, limit);
|
|
8016
7598
|
return {
|
|
8017
|
-
|
|
8018
|
-
|
|
7599
|
+
notes: await this.#hydrateNotes(this.#read, page),
|
|
7600
|
+
...rows.length > limit ? { nextCursor: encodeCursor([String(page[page.length - 1].createdAt), String(page[page.length - 1].id)]) } : {}
|
|
8019
7601
|
};
|
|
8020
7602
|
}
|
|
8021
|
-
async
|
|
7603
|
+
async #assertScopes(tx, ids) {
|
|
7604
|
+
if (ids.length === 0) throw new Error("A note needs at least one stamp scope");
|
|
7605
|
+
const found = await this.#query(tx, `SELECT id FROM "${T.pages}" WHERE isScope = 1 AND id IN (SELECT value FROM json_each(?))`, [jsonList(ids)]);
|
|
7606
|
+
const known = new Set(found.map((row) => String(row.id)));
|
|
7607
|
+
const missing = ids.find((id) => !known.has(id));
|
|
7608
|
+
if (missing) throw new Error(`Note stamp ${missing} is not a scope page`);
|
|
7609
|
+
}
|
|
7610
|
+
async addNote(input) {
|
|
7611
|
+
await assertKnowledgeNoteTextWithinBound(input.text);
|
|
8022
7612
|
return this.#transaction(async (tx) => {
|
|
8023
|
-
const
|
|
8024
|
-
|
|
8025
|
-
|
|
8026
|
-
const
|
|
7613
|
+
const page = await this.#livePage(tx, input.pageId);
|
|
7614
|
+
const stamps = [...new Set(input.stampScopeIds)];
|
|
7615
|
+
await this.#assertScopes(tx, stamps);
|
|
7616
|
+
const mentions = [...new Set(input.mentions)];
|
|
7617
|
+
for (const mention of mentions) await this.#livePage(tx, mention);
|
|
7618
|
+
const id = input.id ?? createKnowledgeUlid();
|
|
7619
|
+
if ((await this.#query(tx, `SELECT 1 FROM "${T.notes}" WHERE id = ?`, [id])).length > 0) throw new KnowledgeConflictError(id);
|
|
7620
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
8027
7621
|
await tx.execute({
|
|
8028
|
-
sql: `
|
|
7622
|
+
sql: `INSERT INTO "${T.notes}" (id, pageId, text, "when", provenance, version, createdAt, updatedAt) VALUES (?, ?, ?, ?, ?, 1, ?, ?)`,
|
|
8029
7623
|
args: [
|
|
8030
|
-
|
|
8031
|
-
input.
|
|
8032
|
-
|
|
8033
|
-
input.
|
|
7624
|
+
id,
|
|
7625
|
+
input.pageId,
|
|
7626
|
+
input.text,
|
|
7627
|
+
input.when ? input.when.toISOString() : null,
|
|
7628
|
+
JSON.stringify(input.provenance),
|
|
7629
|
+
now,
|
|
7630
|
+
now
|
|
8034
7631
|
]
|
|
8035
7632
|
});
|
|
8036
|
-
|
|
8037
|
-
|
|
8038
|
-
|
|
8039
|
-
|
|
8040
|
-
|
|
8041
|
-
|
|
8042
|
-
|
|
7633
|
+
for (const stamp of stamps) await tx.execute({
|
|
7634
|
+
sql: `INSERT INTO "${T.stamps}" (noteId, scopeId) VALUES (?, ?)`,
|
|
7635
|
+
args: [id, stamp]
|
|
7636
|
+
});
|
|
7637
|
+
for (const mention of mentions) await tx.execute({
|
|
7638
|
+
sql: `INSERT INTO "${T.mentions}" (noteId, pageId) VALUES (?, ?)`,
|
|
7639
|
+
args: [id, mention]
|
|
7640
|
+
});
|
|
7641
|
+
await this.#logActivity(tx, "note-added", {
|
|
7642
|
+
scopeId: String(page.scopeId),
|
|
7643
|
+
pageId: input.pageId,
|
|
7644
|
+
noteId: id,
|
|
7645
|
+
provenance: input.provenance
|
|
7646
|
+
}, now);
|
|
7647
|
+
return await this.#noteById(tx, id);
|
|
8043
7648
|
});
|
|
8044
7649
|
}
|
|
8045
|
-
async
|
|
7650
|
+
async #noteWithPage(tx, id) {
|
|
7651
|
+
const note = (await this.#query(tx, `SELECT * FROM "${T.notes}" WHERE id = ?`, [id]))[0];
|
|
7652
|
+
if (!note) throw new KnowledgeNotFoundError("note", id);
|
|
7653
|
+
const page = await this.#pageRow(tx, String(note.pageId));
|
|
7654
|
+
if (!page) throw new KnowledgeNotFoundError("page", String(note.pageId));
|
|
7655
|
+
return {
|
|
7656
|
+
note,
|
|
7657
|
+
page
|
|
7658
|
+
};
|
|
7659
|
+
}
|
|
7660
|
+
restampNote(input) {
|
|
8046
7661
|
return this.#transaction(async (tx) => {
|
|
8047
|
-
const
|
|
8048
|
-
if (
|
|
8049
|
-
if (
|
|
7662
|
+
const { note, page } = await this.#noteWithPage(tx, input.id);
|
|
7663
|
+
if (note.deletedAt) throw new KnowledgeNotFoundError("note", input.id);
|
|
7664
|
+
if (Number(note.version) !== input.expectedVersion) throw new KnowledgeConflictError(input.id);
|
|
7665
|
+
const stamps = [...new Set(input.stampScopeIds)];
|
|
7666
|
+
await this.#assertScopes(tx, stamps);
|
|
7667
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
8050
7668
|
await tx.execute({
|
|
8051
|
-
sql: `
|
|
8052
|
-
args: [
|
|
7669
|
+
sql: `DELETE FROM "${T.stamps}" WHERE noteId = ?`,
|
|
7670
|
+
args: [input.id]
|
|
8053
7671
|
});
|
|
8054
|
-
|
|
8055
|
-
|
|
8056
|
-
|
|
8057
|
-
|
|
8058
|
-
|
|
8059
|
-
|
|
8060
|
-
|
|
7672
|
+
for (const stamp of stamps) await tx.execute({
|
|
7673
|
+
sql: `INSERT INTO "${T.stamps}" (noteId, scopeId) VALUES (?, ?)`,
|
|
7674
|
+
args: [input.id, stamp]
|
|
7675
|
+
});
|
|
7676
|
+
await tx.execute({
|
|
7677
|
+
sql: `UPDATE "${T.notes}" SET version = version + 1, updatedAt = ? WHERE id = ?`,
|
|
7678
|
+
args: [now, input.id]
|
|
7679
|
+
});
|
|
7680
|
+
await this.#logActivity(tx, "note-restamped", {
|
|
7681
|
+
scopeId: String(page.scopeId),
|
|
7682
|
+
pageId: String(page.id),
|
|
7683
|
+
noteId: input.id,
|
|
7684
|
+
provenance: input.provenance
|
|
7685
|
+
}, now);
|
|
7686
|
+
return await this.#noteById(tx, input.id);
|
|
8061
7687
|
});
|
|
8062
7688
|
}
|
|
8063
|
-
|
|
8064
|
-
const scope = canonicalizeKnowledgeScope(input.scope);
|
|
7689
|
+
removeNote(input) {
|
|
8065
7690
|
return this.#transaction(async (tx) => {
|
|
8066
|
-
const
|
|
8067
|
-
if (
|
|
8068
|
-
const
|
|
7691
|
+
const { note, page } = await this.#noteWithPage(tx, input.id);
|
|
7692
|
+
if (note.deletedAt) throw new KnowledgeNotFoundError("note", input.id);
|
|
7693
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
8069
7694
|
await tx.execute({
|
|
8070
|
-
sql: `UPDATE "${
|
|
7695
|
+
sql: `UPDATE "${T.notes}" SET deletedAt = ?, deletedBy = ?, version = version + 1, updatedAt = ? WHERE id = ?`,
|
|
8071
7696
|
args: [
|
|
8072
|
-
|
|
8073
|
-
|
|
8074
|
-
|
|
7697
|
+
now,
|
|
7698
|
+
input.deletedBy,
|
|
7699
|
+
now,
|
|
8075
7700
|
input.id
|
|
8076
7701
|
]
|
|
8077
7702
|
});
|
|
8078
|
-
await this.#
|
|
8079
|
-
|
|
8080
|
-
|
|
8081
|
-
|
|
8082
|
-
|
|
8083
|
-
|
|
8084
|
-
|
|
8085
|
-
};
|
|
7703
|
+
await this.#logActivity(tx, "note-removed", {
|
|
7704
|
+
scopeId: String(page.scopeId),
|
|
7705
|
+
pageId: String(page.id),
|
|
7706
|
+
noteId: input.id,
|
|
7707
|
+
provenance: input.provenance
|
|
7708
|
+
}, now);
|
|
7709
|
+
return await this.#noteById(tx, input.id);
|
|
8086
7710
|
});
|
|
8087
7711
|
}
|
|
8088
|
-
|
|
8089
|
-
|
|
8090
|
-
|
|
8091
|
-
|
|
8092
|
-
|
|
8093
|
-
|
|
8094
|
-
|
|
8095
|
-
|
|
8096
|
-
args: [
|
|
8097
|
-
...keys,
|
|
8098
|
-
query,
|
|
8099
|
-
query,
|
|
8100
|
-
query,
|
|
8101
|
-
query,
|
|
8102
|
-
input.limit ?? 20
|
|
8103
|
-
]
|
|
8104
|
-
})).rows.map((row) => ({
|
|
8105
|
-
type: String(row.type),
|
|
8106
|
-
id: String(row.id),
|
|
8107
|
-
recordId: String(row.id),
|
|
8108
|
-
name: String(row.name),
|
|
8109
|
-
text: [
|
|
8110
|
-
String(row.name),
|
|
8111
|
-
...row.description ? [String(row.description)] : [],
|
|
8112
|
-
...row.content ? [String(row.content)] : []
|
|
8113
|
-
].join("\n"),
|
|
8114
|
-
scope: parseJson$3(row.scopeJson)
|
|
8115
|
-
}));
|
|
8116
|
-
if (results.length < (input.limit ?? 20)) {
|
|
8117
|
-
const knowledge = await this.#client.execute({
|
|
8118
|
-
sql: `SELECT k.*,json(k.scope) AS scopeJson,json(k.metadata) AS metadataJson,n.name,json(n.scope) AS parentScopeJson FROM "${TABLE_KNOWLEDGE_RECORDS}" k CROSS JOIN "${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(keys, "k.scopeKey")} AND lower(k.text) LIKE ? ESCAPE '=' ORDER BY k.id DESC LIMIT ?`,
|
|
8119
|
-
args: [
|
|
8120
|
-
...keys,
|
|
8121
|
-
query,
|
|
8122
|
-
(input.limit ?? 20) - results.length
|
|
8123
|
-
]
|
|
7712
|
+
restoreNote(input) {
|
|
7713
|
+
return this.#transaction(async (tx) => {
|
|
7714
|
+
const { note, page } = await this.#noteWithPage(tx, input.id);
|
|
7715
|
+
if (!note.deletedAt) throw new KnowledgeNotFoundError("note", input.id);
|
|
7716
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
7717
|
+
await tx.execute({
|
|
7718
|
+
sql: `UPDATE "${T.notes}" SET deletedAt = NULL, deletedBy = NULL, version = version + 1, updatedAt = ? WHERE id = ?`,
|
|
7719
|
+
args: [now, input.id]
|
|
8124
7720
|
});
|
|
8125
|
-
|
|
8126
|
-
|
|
8127
|
-
|
|
8128
|
-
|
|
8129
|
-
|
|
8130
|
-
|
|
8131
|
-
|
|
8132
|
-
|
|
8133
|
-
scope: parseJson$3(row.scopeJson)
|
|
8134
|
-
};
|
|
8135
|
-
}));
|
|
8136
|
-
}
|
|
8137
|
-
return results;
|
|
8138
|
-
}
|
|
8139
|
-
/**
|
|
8140
|
-
* @deprecated Curation cursors were removed. Observation-time curate is the only Knowledge writer and needs no
|
|
8141
|
-
* cursor. Always throws.
|
|
8142
|
-
*/
|
|
8143
|
-
async getCurationCursor(_input) {
|
|
8144
|
-
throw new Error(KNOWLEDGE_CURATION_CURSOR_REMOVED_MESSAGE);
|
|
8145
|
-
}
|
|
8146
|
-
/**
|
|
8147
|
-
* @deprecated Curation cursors were removed. Observation-time curate is the only Knowledge writer and needs no
|
|
8148
|
-
* cursor. Always throws.
|
|
8149
|
-
*/
|
|
8150
|
-
async advanceCurationCursor(_input) {
|
|
8151
|
-
throw new Error(KNOWLEDGE_CURATION_CURSOR_REMOVED_MESSAGE);
|
|
7721
|
+
await this.#logActivity(tx, "note-restored", {
|
|
7722
|
+
scopeId: String(page.scopeId),
|
|
7723
|
+
pageId: String(page.id),
|
|
7724
|
+
noteId: input.id,
|
|
7725
|
+
provenance: input.provenance
|
|
7726
|
+
}, now);
|
|
7727
|
+
return await this.#noteById(tx, input.id);
|
|
7728
|
+
});
|
|
8152
7729
|
}
|
|
8153
|
-
async
|
|
8154
|
-
const
|
|
8155
|
-
|
|
8156
|
-
|
|
8157
|
-
|
|
8158
|
-
|
|
8159
|
-
|
|
8160
|
-
|
|
8161
|
-
|
|
8162
|
-
|
|
8163
|
-
|
|
8164
|
-
|
|
8165
|
-
|
|
8166
|
-
|
|
8167
|
-
|
|
8168
|
-
|
|
8169
|
-
|
|
7730
|
+
async searchPages(input) {
|
|
7731
|
+
const words = knowledgeSearchWords(input.query);
|
|
7732
|
+
if (!words.length) return [];
|
|
7733
|
+
const phrase = input.query.trim().toLowerCase().replace(/\s+/g, " ");
|
|
7734
|
+
const limit = Math.max(1, input.limit ?? 20);
|
|
7735
|
+
const likes = words.map((word) => `%${escapeLike(word)}%`);
|
|
7736
|
+
const readable = pageReadable("p", input.read);
|
|
7737
|
+
const scopeFilter = input.scopeIds ? ` AND p.scopeId IN (SELECT value FROM json_each(?))` : "";
|
|
7738
|
+
const scopeArgs = input.scopeIds ? [jsonList(input.scopeIds)] : [];
|
|
7739
|
+
const field = (column) => `lower(COALESCE(${column}, '')) LIKE ? ESCAPE '='`;
|
|
7740
|
+
const pageMatch = words.map(() => `(${field("p.name")} OR ${field("p.description")} OR ${field("p.body")})`).join(" OR ");
|
|
7741
|
+
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(" + ");
|
|
7742
|
+
const pageCount = words.map(() => `(CASE WHEN ${field("p.name")} OR ${field("p.description")} OR ${field("p.body")} THEN 1 ELSE 0 END)`).join(" + ");
|
|
7743
|
+
const triple = likes.flatMap((like) => [
|
|
7744
|
+
like,
|
|
7745
|
+
like,
|
|
7746
|
+
like
|
|
7747
|
+
]);
|
|
7748
|
+
const hits = (await this.#query(this.#read, `SELECT p.* FROM "${T.pages}" p WHERE p.deletedAt IS NULL AND p.isScope = 0 AND ${readable.sql}${scopeFilter} AND (${pageMatch}) ORDER BY CASE WHEN instr(p.canonicalName, ?) > 0 THEN 0 ELSE 1 END, (${pageCount}) DESC, (${pageScore}) DESC, p.updatedAt DESC, p.id ASC LIMIT ?`, [
|
|
7749
|
+
...readable.args,
|
|
7750
|
+
...scopeArgs,
|
|
7751
|
+
...triple,
|
|
7752
|
+
phrase,
|
|
7753
|
+
...triple,
|
|
7754
|
+
...triple,
|
|
7755
|
+
limit
|
|
7756
|
+
])).map((row) => ({
|
|
7757
|
+
type: "page",
|
|
7758
|
+
page: toPage(row)
|
|
8170
7759
|
}));
|
|
8171
|
-
|
|
8172
|
-
|
|
8173
|
-
const
|
|
8174
|
-
const
|
|
8175
|
-
|
|
8176
|
-
|
|
8177
|
-
args
|
|
8178
|
-
|
|
8179
|
-
|
|
8180
|
-
|
|
8181
|
-
|
|
8182
|
-
|
|
7760
|
+
if (hits.length >= limit) return hits;
|
|
7761
|
+
const noteVisible = noteReadable("n", input.read);
|
|
7762
|
+
const noteHas = words.map(() => `lower(n.text) LIKE ? ESCAPE '='`);
|
|
7763
|
+
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 ?`, [
|
|
7764
|
+
...scopeArgs,
|
|
7765
|
+
...likes,
|
|
7766
|
+
...noteVisible.args,
|
|
7767
|
+
...likes,
|
|
7768
|
+
limit - hits.length
|
|
7769
|
+
]);
|
|
7770
|
+
const notes = await this.#hydrateNotes(this.#read, noteRows);
|
|
7771
|
+
for (const note of notes) {
|
|
7772
|
+
const page = await this.#pageRow(this.#read, note.pageId);
|
|
7773
|
+
if (page) hits.push({
|
|
7774
|
+
type: "note",
|
|
7775
|
+
note,
|
|
7776
|
+
page: toPage(page)
|
|
7777
|
+
});
|
|
8183
7778
|
}
|
|
8184
|
-
|
|
8185
|
-
|
|
8186
|
-
|
|
8187
|
-
|
|
8188
|
-
|
|
7779
|
+
return hits;
|
|
7780
|
+
}
|
|
7781
|
+
async listPageActivity(input) {
|
|
7782
|
+
const core = await loadPagesCore();
|
|
7783
|
+
const limit = limitOf(input.limit, core.MAX_KNOWLEDGE_PAGE_LIST_LIMIT);
|
|
7784
|
+
const cursor = decodeCursor(input.cursor, 1);
|
|
7785
|
+
const readable = pageReadable("p", input.read);
|
|
7786
|
+
const noteVisible = noteReadable("n", input.read);
|
|
7787
|
+
const args = [...readable.args];
|
|
7788
|
+
let filters = "";
|
|
7789
|
+
if (input.scopeId !== void 0) {
|
|
7790
|
+
filters += ` AND p.scopeId = ?`;
|
|
7791
|
+
args.push(input.scopeId);
|
|
7792
|
+
}
|
|
7793
|
+
if (input.pageId !== void 0) {
|
|
7794
|
+
filters += ` AND e.pageId = ?`;
|
|
7795
|
+
args.push(input.pageId);
|
|
7796
|
+
}
|
|
7797
|
+
if (cursor) {
|
|
7798
|
+
filters += ` AND e.id < ?`;
|
|
7799
|
+
args.push(cursor[0]);
|
|
7800
|
+
}
|
|
7801
|
+
args.push(...noteVisible.args, limit + 1);
|
|
7802
|
+
const rows = await this.#query(this.#read, `SELECT e.* FROM "${T.activity}" e JOIN "${T.pages}" p ON p.id = e.pageId WHERE ${readable.sql}${filters} AND (e.noteId IS NULL OR EXISTS (SELECT 1 FROM "${T.notes}" n WHERE n.id = e.noteId AND ${noteVisible.sql})) ORDER BY e.id DESC LIMIT ?`, args);
|
|
7803
|
+
const page = rows.slice(0, limit);
|
|
7804
|
+
return {
|
|
7805
|
+
events: page.map((row) => ({
|
|
7806
|
+
id: String(row.id),
|
|
7807
|
+
action: String(row.action),
|
|
7808
|
+
scopeId: String(row.scopeId),
|
|
7809
|
+
pageId: String(row.pageId),
|
|
7810
|
+
...row.noteId ? { noteId: String(row.noteId) } : {},
|
|
7811
|
+
provenance: provenance(row.provenance),
|
|
7812
|
+
createdAt: new Date(String(row.createdAt))
|
|
7813
|
+
})),
|
|
7814
|
+
...rows.length > limit ? { nextCursor: encodeCursor([String(page[page.length - 1].id)]) } : {}
|
|
7815
|
+
};
|
|
8189
7816
|
}
|
|
8190
|
-
|
|
8191
|
-
|
|
8192
|
-
|
|
7817
|
+
/** Rewrites the audience index rows of one pin to match `audience`. */
|
|
7818
|
+
async #indexAudience(tx, pinId, audience) {
|
|
7819
|
+
await tx.execute({
|
|
7820
|
+
sql: `DELETE FROM "${T.pinAudiences}" WHERE pinId = ?`,
|
|
7821
|
+
args: [pinId]
|
|
7822
|
+
});
|
|
7823
|
+
for (const entry of audience) await tx.execute({
|
|
7824
|
+
sql: `INSERT OR IGNORE INTO "${T.pinAudiences}" (pinId, audience) VALUES (?, ?)`,
|
|
7825
|
+
args: [pinId, entry]
|
|
7826
|
+
});
|
|
7827
|
+
}
|
|
7828
|
+
addPinAudience(input) {
|
|
8193
7829
|
return this.#transaction(async (tx) => {
|
|
8194
|
-
const
|
|
8195
|
-
|
|
8196
|
-
|
|
8197
|
-
|
|
8198
|
-
|
|
8199
|
-
|
|
8200
|
-
|
|
8201
|
-
|
|
8202
|
-
|
|
8203
|
-
|
|
7830
|
+
const page = await this.#livePage(tx, input.pageId);
|
|
7831
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
7832
|
+
const existing = (await this.#query(tx, `SELECT * FROM "${T.pins}" WHERE pageId = ?`, [input.pageId]))[0];
|
|
7833
|
+
if (!existing) {
|
|
7834
|
+
const id = createKnowledgeUlid();
|
|
7835
|
+
await tx.execute({
|
|
7836
|
+
sql: `INSERT INTO "${T.pins}" (id, pageId, audience, createdBy, createdAt, updatedAt) VALUES (?, ?, ?, ?, ?, ?)`,
|
|
7837
|
+
args: [
|
|
7838
|
+
id,
|
|
7839
|
+
input.pageId,
|
|
7840
|
+
JSON.stringify([...new Set(input.audience)]),
|
|
7841
|
+
input.createdBy,
|
|
7842
|
+
now,
|
|
7843
|
+
now
|
|
7844
|
+
]
|
|
7845
|
+
});
|
|
7846
|
+
await this.#indexAudience(tx, id, [...new Set(input.audience)]);
|
|
7847
|
+
await this.#logActivity(tx, "pin-added", {
|
|
7848
|
+
scopeId: String(page.scopeId),
|
|
7849
|
+
pageId: input.pageId,
|
|
7850
|
+
provenance: { author: input.createdBy }
|
|
7851
|
+
}, now);
|
|
7852
|
+
return toPin((await this.#query(tx, `SELECT * FROM "${T.pins}" WHERE id = ?`, [id]))[0]);
|
|
8204
7853
|
}
|
|
8205
|
-
|
|
8206
|
-
const
|
|
8207
|
-
|
|
8208
|
-
|
|
8209
|
-
|
|
8210
|
-
|
|
8211
|
-
|
|
8212
|
-
|
|
8213
|
-
|
|
8214
|
-
|
|
8215
|
-
|
|
8216
|
-
|
|
8217
|
-
|
|
8218
|
-
|
|
8219
|
-
|
|
8220
|
-
|
|
8221
|
-
|
|
8222
|
-
}
|
|
7854
|
+
const current = JSON.parse(String(existing.audience));
|
|
7855
|
+
const merged = [.../* @__PURE__ */ new Set([...current, ...input.audience])];
|
|
7856
|
+
if (merged.length !== current.length) {
|
|
7857
|
+
await tx.execute({
|
|
7858
|
+
sql: `UPDATE "${T.pins}" SET audience = ?, updatedAt = ? WHERE id = ?`,
|
|
7859
|
+
args: [
|
|
7860
|
+
JSON.stringify(merged),
|
|
7861
|
+
now,
|
|
7862
|
+
String(existing.id)
|
|
7863
|
+
]
|
|
7864
|
+
});
|
|
7865
|
+
await this.#indexAudience(tx, String(existing.id), merged);
|
|
7866
|
+
await this.#logActivity(tx, "pin-added", {
|
|
7867
|
+
scopeId: String(page.scopeId),
|
|
7868
|
+
pageId: input.pageId,
|
|
7869
|
+
provenance: { author: input.createdBy }
|
|
7870
|
+
}, now);
|
|
7871
|
+
}
|
|
7872
|
+
return toPin((await this.#query(tx, `SELECT * FROM "${T.pins}" WHERE id = ?`, [String(existing.id)]))[0]);
|
|
8223
7873
|
});
|
|
8224
7874
|
}
|
|
8225
|
-
|
|
8226
|
-
|
|
8227
|
-
|
|
8228
|
-
|
|
8229
|
-
|
|
8230
|
-
|
|
7875
|
+
removePinAudience(input) {
|
|
7876
|
+
return this.#transaction(async (tx) => {
|
|
7877
|
+
const existing = (await this.#query(tx, `SELECT * FROM "${T.pins}" WHERE pageId = ?`, [input.pageId]))[0];
|
|
7878
|
+
if (!existing) return null;
|
|
7879
|
+
const current = JSON.parse(String(existing.audience));
|
|
7880
|
+
const remove = new Set(input.audience);
|
|
7881
|
+
const remaining = current.filter((entry) => !remove.has(entry));
|
|
7882
|
+
if (remaining.length === current.length) return toPin(existing);
|
|
7883
|
+
const page = await this.#pageRow(tx, input.pageId);
|
|
7884
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
7885
|
+
if (page) await this.#logActivity(tx, "pin-removed", {
|
|
7886
|
+
scopeId: String(page.scopeId),
|
|
7887
|
+
pageId: input.pageId,
|
|
7888
|
+
provenance: { author: input.removedBy }
|
|
7889
|
+
}, now);
|
|
7890
|
+
if (remaining.length === 0) {
|
|
7891
|
+
await this.#indexAudience(tx, String(existing.id), []);
|
|
7892
|
+
await tx.execute({
|
|
7893
|
+
sql: `DELETE FROM "${T.pins}" WHERE id = ?`,
|
|
7894
|
+
args: [String(existing.id)]
|
|
7895
|
+
});
|
|
7896
|
+
return null;
|
|
7897
|
+
}
|
|
7898
|
+
await tx.execute({
|
|
7899
|
+
sql: `UPDATE "${T.pins}" SET audience = ?, updatedAt = ? WHERE id = ?`,
|
|
8231
7900
|
args: [
|
|
7901
|
+
JSON.stringify(remaining),
|
|
8232
7902
|
now,
|
|
8233
|
-
id
|
|
8234
|
-
input.workerId
|
|
8235
|
-
]
|
|
8236
|
-
});
|
|
8237
|
-
});
|
|
8238
|
-
}
|
|
8239
|
-
async releaseSemanticOutbox(input) {
|
|
8240
|
-
if (!input.ids.length) return;
|
|
8241
|
-
await this.#transaction(async (tx) => {
|
|
8242
|
-
for (const id of input.ids) await tx.execute({
|
|
8243
|
-
sql: `UPDATE "${TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}" SET status='pending',availableAt=?,claimedAt=NULL,claimedBy=NULL WHERE id=? AND status='processing' AND claimedBy=?`,
|
|
8244
|
-
args: [
|
|
8245
|
-
(input.retryAt ?? /* @__PURE__ */ new Date()).toISOString(),
|
|
8246
|
-
id,
|
|
8247
|
-
input.workerId
|
|
7903
|
+
String(existing.id)
|
|
8248
7904
|
]
|
|
8249
7905
|
});
|
|
7906
|
+
await this.#indexAudience(tx, String(existing.id), remaining);
|
|
7907
|
+
return toPin((await this.#query(tx, `SELECT * FROM "${T.pins}" WHERE id = ?`, [String(existing.id)]))[0]);
|
|
8250
7908
|
});
|
|
8251
7909
|
}
|
|
8252
|
-
async
|
|
8253
|
-
|
|
8254
|
-
|
|
8255
|
-
|
|
8256
|
-
|
|
8257
|
-
|
|
8258
|
-
|
|
8259
|
-
|
|
8260
|
-
|
|
8261
|
-
|
|
8262
|
-
|
|
8263
|
-
|
|
8264
|
-
|
|
8265
|
-
|
|
8266
|
-
|
|
8267
|
-
|
|
8268
|
-
|
|
8269
|
-
|
|
8270
|
-
|
|
7910
|
+
async listPins(input) {
|
|
7911
|
+
const core = await loadPagesCore();
|
|
7912
|
+
const limit = limitOf(input.limit, core.MAX_KNOWLEDGE_PAGE_LIST_LIMIT);
|
|
7913
|
+
const cursor = decodeCursor(input.cursor, 2);
|
|
7914
|
+
const readable = pageReadable("p", input.read);
|
|
7915
|
+
const args = [...readable.args];
|
|
7916
|
+
let filters = "";
|
|
7917
|
+
if (input.audience !== "all") {
|
|
7918
|
+
filters += ` AND pin.id IN (SELECT pa.pinId FROM json_each(?) a JOIN "${T.pinAudiences}" pa ON pa.audience = a.value)`;
|
|
7919
|
+
args.push(jsonList(input.audience));
|
|
7920
|
+
}
|
|
7921
|
+
if (input.pageIds) {
|
|
7922
|
+
filters += ` AND pin.pageId IN (SELECT value FROM json_each(?))`;
|
|
7923
|
+
args.push(jsonList(input.pageIds));
|
|
7924
|
+
}
|
|
7925
|
+
if (input.excludePageIds?.length) {
|
|
7926
|
+
filters += ` AND pin.pageId NOT IN (SELECT value FROM json_each(?))`;
|
|
7927
|
+
args.push(jsonList(input.excludePageIds));
|
|
7928
|
+
}
|
|
7929
|
+
if (input.withBody) filters += ` AND p.body IS NOT NULL AND trim(p.body, char(32, 9, 10, 11, 12, 13)) <> ''`;
|
|
7930
|
+
const from = `FROM "${T.pins}" pin JOIN "${T.pages}" p ON p.id = pin.pageId WHERE p.deletedAt IS NULL AND ${readable.sql}${filters}`;
|
|
7931
|
+
let total;
|
|
7932
|
+
if (input.count) total = Number((await this.#query(this.#read, `SELECT COUNT(*) AS n ${from}`, args))[0].n);
|
|
7933
|
+
const pageArgs = [...args];
|
|
7934
|
+
let cursorFilter = "";
|
|
7935
|
+
if (cursor) {
|
|
7936
|
+
cursorFilter = ` AND (pin.createdAt < ? OR (pin.createdAt = ? AND pin.id < ?))`;
|
|
7937
|
+
pageArgs.push(cursor[0], cursor[0], cursor[1]);
|
|
7938
|
+
}
|
|
7939
|
+
pageArgs.push(limit + 1);
|
|
7940
|
+
const rows = await this.#query(this.#read, `SELECT pin.*, p.name AS pageName ${from}${cursorFilter} ORDER BY pin.createdAt DESC, pin.id DESC LIMIT ?`, pageArgs);
|
|
7941
|
+
const page = rows.slice(0, limit);
|
|
7942
|
+
return {
|
|
7943
|
+
pins: page.map(toListedPin),
|
|
7944
|
+
...rows.length > limit ? { nextCursor: encodeCursor([String(page[page.length - 1].createdAt), String(page[page.length - 1].id)]) } : {},
|
|
7945
|
+
...total === void 0 ? {} : { total }
|
|
7946
|
+
};
|
|
8271
7947
|
}
|
|
8272
|
-
|
|
8273
|
-
|
|
8274
|
-
|
|
8275
|
-
|
|
8276
|
-
|
|
8277
|
-
|
|
8278
|
-
|
|
8279
|
-
|
|
8280
|
-
|
|
8281
|
-
|
|
8282
|
-
|
|
8283
|
-
args: [canonicalName(name), ...keys]
|
|
8284
|
-
})).rows.map(parseNode).sort((left, right) => right.scope.length - left.scope.length);
|
|
8285
|
-
for (const candidate of candidates) {
|
|
8286
|
-
const terminal = await this.#resolveTerminalNode(executor, candidate.id);
|
|
8287
|
-
if (terminal && isKnowledgeScopeVisible(terminal.scope, scope)) return terminal;
|
|
8288
|
-
}
|
|
8289
|
-
return null;
|
|
8290
|
-
}
|
|
8291
|
-
async #resolveTerminalNode(executor, id) {
|
|
8292
|
-
let node = await this.#getNode(executor, id);
|
|
8293
|
-
const seen = /* @__PURE__ */ new Set();
|
|
8294
|
-
while (node?.mergedInto) {
|
|
8295
|
-
if (seen.has(node.id)) throw new Error(`Knowledge merge cycle detected at ${node.id}`);
|
|
8296
|
-
seen.add(node.id);
|
|
8297
|
-
node = await this.#getNode(executor, node.mergedInto);
|
|
7948
|
+
};
|
|
7949
|
+
// src/storage/domains/knowledge/index.ts
|
|
7950
|
+
function canonicalizeLibSQLUrl(url) {
|
|
7951
|
+
if (url.includes(":memory:")) return /[?&]cache=shared(?:&|$)/.test(url) ? "file::memory:?cache=shared" : void 0;
|
|
7952
|
+
if (url.startsWith("file:")) {
|
|
7953
|
+
const [path] = url.slice(5).split("?");
|
|
7954
|
+
if (url.startsWith("file://")) {
|
|
7955
|
+
const parsed = new URL(url);
|
|
7956
|
+
const host = parsed.hostname.toLocaleLowerCase();
|
|
7957
|
+
const filePath = host && host !== "localhost" ? `//${host}${parsed.pathname}` : parsed.pathname;
|
|
7958
|
+
return `file:${resolve(decodeURIComponent(filePath))}`;
|
|
8298
7959
|
}
|
|
8299
|
-
return
|
|
7960
|
+
return `file:${resolve(decodeURIComponent(path))}`;
|
|
8300
7961
|
}
|
|
8301
|
-
|
|
8302
|
-
const
|
|
8303
|
-
|
|
8304
|
-
|
|
7962
|
+
try {
|
|
7963
|
+
const parsed = new URL(url);
|
|
7964
|
+
const port = parsed.port || (parsed.protocol === "libsql:" || parsed.protocol === "https:" ? "443" : "80");
|
|
7965
|
+
return `${parsed.protocol}//${parsed.hostname.toLocaleLowerCase()}:${port}${parsed.pathname}`;
|
|
7966
|
+
} catch {
|
|
7967
|
+
return url;
|
|
7968
|
+
}
|
|
7969
|
+
}
|
|
7970
|
+
const unidentifiedClientIsolationKey = {};
|
|
7971
|
+
function getLibSQLKnowledgeIsolationKey(config, client) {
|
|
7972
|
+
if (config.storageIsolationKey !== void 0) return config.storageIsolationKey;
|
|
7973
|
+
if (!config.url) return unidentifiedClientIsolationKey;
|
|
7974
|
+
const urlKey = canonicalizeLibSQLUrl(config.url);
|
|
7975
|
+
return urlKey ? `libsql:${urlKey}` : client ?? config.client ?? config;
|
|
7976
|
+
}
|
|
7977
|
+
var KnowledgeLibSQL = class extends KnowledgeStorage {
|
|
7978
|
+
#client;
|
|
7979
|
+
#db;
|
|
7980
|
+
#pages;
|
|
7981
|
+
constructor(config) {
|
|
7982
|
+
const client = resolveClient(config);
|
|
7983
|
+
const storageIsolationKey = config.storageIsolationKey ?? getLibSQLKnowledgeIsolationKey(config, client);
|
|
7984
|
+
super({ storageIsolationKey });
|
|
7985
|
+
this.#client = client;
|
|
7986
|
+
this.#db = new LibSQLDB({
|
|
7987
|
+
client: this.#client,
|
|
7988
|
+
maxRetries: config.maxRetries,
|
|
7989
|
+
initialBackoffMs: config.initialBackoffMs
|
|
8305
7990
|
});
|
|
8306
|
-
|
|
8307
|
-
|
|
8308
|
-
|
|
8309
|
-
const scope = canonicalizeKnowledgeScope(input.scope);
|
|
8310
|
-
const node = await this.#resolveTerminalNode(this.#client, nodeReferenceId(input.node));
|
|
8311
|
-
if (!node) return { records: [] };
|
|
8312
|
-
const keys = visibleScopeKeys(scope);
|
|
8313
|
-
const includesMentions = relationship !== "about";
|
|
8314
|
-
const relationSql = relationship === "about" ? "k.node=?" : relationship === "mentioning" ? "m.recordId=?" : "(k.node=? OR m.recordId=?)";
|
|
8315
|
-
const args = [
|
|
8316
|
-
node.id,
|
|
8317
|
-
...relationship === "related" ? [node.id] : [],
|
|
8318
|
-
...keys
|
|
8319
|
-
];
|
|
8320
|
-
if (input.after) args.push(input.after);
|
|
8321
|
-
args.push((input.limit ?? 100) + 1);
|
|
8322
|
-
const result = await this.#client.execute({
|
|
8323
|
-
sql: `SELECT DISTINCT k.*,json(k.scope) AS scopeJson,json(k.metadata) AS metadataJson FROM "${TABLE_KNOWLEDGE_RECORDS}" k${includesMentions ? ` LEFT JOIN "${TABLE_KNOWLEDGE_MENTIONS}" m ON m.sourceType='record' AND m.sourceId=k.id` : ""} WHERE ${relationSql} AND ${visibleSql(keys, "k.scopeKey")}${input.includeDeleted ? "" : " AND k.deletedAt IS NULL"}${input.after ? " AND k.id < ?" : ""} ORDER BY k.id DESC LIMIT ?`,
|
|
8324
|
-
args
|
|
7991
|
+
this.#pages = new LibSQLKnowledgePages({
|
|
7992
|
+
read: this.#client,
|
|
7993
|
+
transaction: (operation) => this.#transaction(operation)
|
|
8325
7994
|
});
|
|
8326
|
-
|
|
8327
|
-
|
|
7995
|
+
}
|
|
7996
|
+
getCapabilities() {
|
|
8328
7997
|
return {
|
|
8329
|
-
|
|
8330
|
-
|
|
7998
|
+
contractVersion: 4,
|
|
7999
|
+
schemaVersion: 4,
|
|
8000
|
+
supportsSchemaInspection: true,
|
|
8001
|
+
supportsExplicitReset: true
|
|
8331
8002
|
};
|
|
8332
8003
|
}
|
|
8333
|
-
async
|
|
8334
|
-
|
|
8335
|
-
for (const address of addresses) {
|
|
8336
|
-
const result = await executor.execute({
|
|
8337
|
-
sql: `SELECT scopeNodeId FROM "${TABLE_KNOWLEDGE_SCOPE_ADDRESSES}" WHERE address=?`,
|
|
8338
|
-
args: [address]
|
|
8339
|
-
});
|
|
8340
|
-
if (result.rows[0]?.scopeNodeId != null) scopeNodeIds.add(String(result.rows[0].scopeNodeId));
|
|
8341
|
-
}
|
|
8342
|
-
return [...scopeNodeIds];
|
|
8343
|
-
}
|
|
8344
|
-
async #replaceNodeScopes(executor, nodeId, addresses, addedAt) {
|
|
8345
|
-
await executor.execute({
|
|
8346
|
-
sql: `DELETE FROM "${TABLE_KNOWLEDGE_NODE_SCOPES}" WHERE nodeId=?`,
|
|
8347
|
-
args: [nodeId]
|
|
8348
|
-
});
|
|
8349
|
-
for (const scopeNodeId of await this.#resolveScopeNodeIds(executor, addresses)) await executor.execute({
|
|
8350
|
-
sql: `INSERT OR IGNORE INTO "${TABLE_KNOWLEDGE_NODE_SCOPES}" (nodeId,scopeNodeId,addedAt) VALUES (?,?,?)`,
|
|
8351
|
-
args: [
|
|
8352
|
-
nodeId,
|
|
8353
|
-
scopeNodeId,
|
|
8354
|
-
addedAt.toISOString()
|
|
8355
|
-
]
|
|
8356
|
-
});
|
|
8004
|
+
async dangerouslyClearAll() {
|
|
8005
|
+
await this.#pages.clearAll();
|
|
8357
8006
|
}
|
|
8358
|
-
|
|
8359
|
-
|
|
8360
|
-
* placements added by `#placeNodeInScopes`, which a scope change must not erase.
|
|
8361
|
-
*/
|
|
8362
|
-
async #swapIdentityScopes(executor, nodeId, from, to, addedAt) {
|
|
8363
|
-
const next = await this.#resolveScopeNodeIds(executor, to);
|
|
8364
|
-
for (const scopeNodeId of await this.#resolveScopeNodeIds(executor, from)) {
|
|
8365
|
-
if (next.includes(scopeNodeId)) continue;
|
|
8366
|
-
await executor.execute({
|
|
8367
|
-
sql: `DELETE FROM "${TABLE_KNOWLEDGE_NODE_SCOPES}" WHERE nodeId=? AND scopeNodeId=?`,
|
|
8368
|
-
args: [nodeId, scopeNodeId]
|
|
8369
|
-
});
|
|
8370
|
-
}
|
|
8371
|
-
for (const scopeNodeId of next) await executor.execute({
|
|
8372
|
-
sql: `INSERT OR IGNORE INTO "${TABLE_KNOWLEDGE_NODE_SCOPES}" (nodeId,scopeNodeId,addedAt) VALUES (?,?,?)`,
|
|
8373
|
-
args: [
|
|
8374
|
-
nodeId,
|
|
8375
|
-
scopeNodeId,
|
|
8376
|
-
addedAt.toISOString()
|
|
8377
|
-
]
|
|
8378
|
-
});
|
|
8007
|
+
async #inspectPages(tx) {
|
|
8008
|
+
return inspectKnowledgePagesSchema(await this.#pagesSnapshot(tx));
|
|
8379
8009
|
}
|
|
8380
|
-
|
|
8381
|
-
|
|
8382
|
-
|
|
8383
|
-
|
|
8384
|
-
|
|
8385
|
-
|
|
8386
|
-
async #placeNodeInScopes(executor, nodeId, addresses, addedAt) {
|
|
8387
|
-
for (const address of addresses ?? []) {
|
|
8388
|
-
const scopeNodeId = (await executor.execute({
|
|
8389
|
-
sql: `SELECT sa.scopeNodeId AS scopeNodeId FROM "${TABLE_KNOWLEDGE_SCOPE_ADDRESSES}" sa JOIN "${TABLE_KNOWLEDGE_NODES}" n ON n.id=sa.scopeNodeId WHERE sa.address=? AND n.isScope AND n.deletedAt IS NULL`,
|
|
8390
|
-
args: [address]
|
|
8391
|
-
})).rows[0]?.scopeNodeId;
|
|
8392
|
-
if (scopeNodeId == null) throw new KnowledgeNotFoundError("scope", address);
|
|
8393
|
-
await executor.execute({
|
|
8394
|
-
sql: `INSERT OR IGNORE INTO "${TABLE_KNOWLEDGE_NODE_SCOPES}" (nodeId,scopeNodeId,addedAt) VALUES (?,?,?)`,
|
|
8395
|
-
args: [
|
|
8396
|
-
nodeId,
|
|
8397
|
-
String(scopeNodeId),
|
|
8398
|
-
addedAt.toISOString()
|
|
8399
|
-
]
|
|
8400
|
-
});
|
|
8010
|
+
async #pagesSnapshot(tx) {
|
|
8011
|
+
const tableNames = (await tx.execute({ sql: "SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'mastra\\_knowledge\\_%' ESCAPE '\\'" })).rows.map((row) => String(row.name));
|
|
8012
|
+
let schemaVersion;
|
|
8013
|
+
if (tableNames.includes("mastra_knowledge_access_state")) {
|
|
8014
|
+
const state = await tx.execute({ sql: `SELECT schemaVersion FROM "${TABLE_KNOWLEDGE_PAGES_STATE}" WHERE id = 'knowledge'` });
|
|
8015
|
+
if (state.rows[0]) schemaVersion = Number(state.rows[0].schemaVersion);
|
|
8401
8016
|
}
|
|
8017
|
+
return {
|
|
8018
|
+
tableNames,
|
|
8019
|
+
schemaVersion
|
|
8020
|
+
};
|
|
8402
8021
|
}
|
|
8403
|
-
async
|
|
8404
|
-
|
|
8405
|
-
sql: `DELETE FROM "${TABLE_KNOWLEDGE_RECORD_SCOPES}" WHERE recordId=?`,
|
|
8406
|
-
args: [recordId]
|
|
8407
|
-
});
|
|
8408
|
-
for (const scopeNodeId of await this.#resolveScopeNodeIds(executor, addresses)) await executor.execute({
|
|
8409
|
-
sql: `INSERT OR IGNORE INTO "${TABLE_KNOWLEDGE_RECORD_SCOPES}" (recordId,scopeNodeId,addedAt) VALUES (?,?,?)`,
|
|
8410
|
-
args: [
|
|
8411
|
-
recordId,
|
|
8412
|
-
scopeNodeId,
|
|
8413
|
-
addedAt.toISOString()
|
|
8414
|
-
]
|
|
8415
|
-
});
|
|
8022
|
+
async inspectSchema() {
|
|
8023
|
+
return this.#inspectPages(this.#client);
|
|
8416
8024
|
}
|
|
8417
|
-
async
|
|
8418
|
-
await tx
|
|
8419
|
-
|
|
8420
|
-
|
|
8421
|
-
|
|
8422
|
-
|
|
8423
|
-
let node = await this.#resolveNode(tx, name, resolutionScope);
|
|
8424
|
-
if (!node) {
|
|
8425
|
-
node = await this.#getNodeByName(tx, name, defaultScope);
|
|
8426
|
-
if (node) node = await this.#resolveTerminalNode(tx, node.id);
|
|
8427
|
-
if (!node) {
|
|
8428
|
-
const now = /* @__PURE__ */ new Date();
|
|
8429
|
-
node = {
|
|
8430
|
-
id: crypto.randomUUID(),
|
|
8431
|
-
type: "node",
|
|
8432
|
-
name,
|
|
8433
|
-
kind: "node",
|
|
8434
|
-
scope: defaultScope,
|
|
8435
|
-
version: 1,
|
|
8436
|
-
createdAt: now,
|
|
8437
|
-
updatedAt: now
|
|
8438
|
-
};
|
|
8439
|
-
await tx.execute({
|
|
8440
|
-
sql: `INSERT INTO "${TABLE_KNOWLEDGE_NODES}" (id,type,name,canonicalName,kind,content,isScope,scope,scopeKey,version,mergedInto,createdAt,updatedAt) VALUES (?,?,?,?,?,NULL,FALSE,jsonb(?),?,?,NULL,?,?)`,
|
|
8441
|
-
args: [
|
|
8442
|
-
node.id,
|
|
8443
|
-
"node",
|
|
8444
|
-
node.name,
|
|
8445
|
-
canonicalName(node.name),
|
|
8446
|
-
node.kind,
|
|
8447
|
-
JSON.stringify(defaultScope),
|
|
8448
|
-
knowledgeScopeKey(defaultScope),
|
|
8449
|
-
1,
|
|
8450
|
-
now.toISOString(),
|
|
8451
|
-
now.toISOString()
|
|
8452
|
-
]
|
|
8453
|
-
});
|
|
8454
|
-
await this.#replaceNodeScopes(tx, node.id, defaultScope, now);
|
|
8455
|
-
await this.#activity(tx, "node-created", "node", node.id, defaultScope);
|
|
8456
|
-
await this.#outbox(tx, "node", node.id, "upsert", 1, defaultScope);
|
|
8457
|
-
}
|
|
8458
|
-
}
|
|
8025
|
+
async init() {
|
|
8026
|
+
await this.#transaction(async (tx) => {
|
|
8027
|
+
for (const table of legacyKnowledgeTablesToDrop(await this.#pagesSnapshot(tx))) await tx.execute(`DROP TABLE IF EXISTS "${table}"`);
|
|
8028
|
+
const inspection = await this.#inspectPages(tx);
|
|
8029
|
+
if (inspection.status === "incompatible-reset-required") throw new KnowledgeSchemaResetRequiredError(inspection);
|
|
8030
|
+
for (const statement of KNOWLEDGE_PAGE_DDL) await tx.execute(statement);
|
|
8459
8031
|
await tx.execute({
|
|
8460
|
-
sql: `INSERT OR IGNORE INTO "${
|
|
8461
|
-
args: [
|
|
8462
|
-
sourceType,
|
|
8463
|
-
sourceId,
|
|
8464
|
-
node.id
|
|
8465
|
-
]
|
|
8032
|
+
sql: `INSERT OR IGNORE INTO "${TABLE_KNOWLEDGE_PAGES_STATE}" (id, schemaVersion) VALUES ('knowledge', ?)`,
|
|
8033
|
+
args: [4]
|
|
8466
8034
|
});
|
|
8467
|
-
}
|
|
8468
|
-
}
|
|
8469
|
-
async #activity(executor, action, recordType, recordId, scope, sourceThreadId) {
|
|
8470
|
-
const now = /* @__PURE__ */ new Date();
|
|
8471
|
-
const [contextScopeId] = await this.#resolveScopeNodeIds(executor, scope);
|
|
8472
|
-
await executor.execute({
|
|
8473
|
-
sql: `INSERT INTO "${TABLE_KNOWLEDGE_ACTIVITY}" (id,action,targetType,targetId,contextScopeId,recordType,recordId,scope,scopeKey,sourceThreadId,createdAt) VALUES (?,?,?,?,?,?,?,jsonb(?),?,?,?)`,
|
|
8474
|
-
args: [
|
|
8475
|
-
createKnowledgeUlid(),
|
|
8476
|
-
action,
|
|
8477
|
-
recordType,
|
|
8478
|
-
recordId,
|
|
8479
|
-
contextScopeId ?? null,
|
|
8480
|
-
recordType,
|
|
8481
|
-
recordId,
|
|
8482
|
-
JSON.stringify(scope),
|
|
8483
|
-
knowledgeScopeKey(scope),
|
|
8484
|
-
sourceThreadId ?? null,
|
|
8485
|
-
now.toISOString()
|
|
8486
|
-
]
|
|
8487
8035
|
});
|
|
8488
8036
|
}
|
|
8489
|
-
async
|
|
8490
|
-
|
|
8491
|
-
|
|
8492
|
-
|
|
8493
|
-
|
|
8494
|
-
|
|
8495
|
-
args: [
|
|
8496
|
-
createKnowledgeUlid(),
|
|
8497
|
-
idempotencyKey,
|
|
8498
|
-
documentId,
|
|
8499
|
-
documentType,
|
|
8500
|
-
operation,
|
|
8501
|
-
JSON.stringify(scope),
|
|
8502
|
-
knowledgeScopeKey(scope),
|
|
8503
|
-
now.toISOString(),
|
|
8504
|
-
now.toISOString()
|
|
8505
|
-
]
|
|
8037
|
+
async dangerouslyReset() {
|
|
8038
|
+
await withClientWriteLock(this.#client, async () => {
|
|
8039
|
+
await this.#client.batch([...KNOWLEDGE_RESETTABLE_TABLE_NAMES, KNOWLEDGE_PAGE_TABLES.pinAudiences].map((table) => ({
|
|
8040
|
+
sql: `DROP TABLE IF EXISTS "${table}"`,
|
|
8041
|
+
args: []
|
|
8042
|
+
})), "write");
|
|
8506
8043
|
});
|
|
8044
|
+
await this.init();
|
|
8045
|
+
}
|
|
8046
|
+
materializeScope(input) {
|
|
8047
|
+
return this.#pages.materializeScope(input);
|
|
8048
|
+
}
|
|
8049
|
+
listScopeDeclarations() {
|
|
8050
|
+
return this.#pages.listScopeDeclarations();
|
|
8051
|
+
}
|
|
8052
|
+
renameScopeDeclaration(input) {
|
|
8053
|
+
return this.#pages.renameScopeDeclaration(input);
|
|
8054
|
+
}
|
|
8055
|
+
getScopesByAddress(input) {
|
|
8056
|
+
return this.#pages.getScopesByAddress(input);
|
|
8057
|
+
}
|
|
8058
|
+
listChildScopes(input) {
|
|
8059
|
+
return this.#pages.listChildScopes(input);
|
|
8060
|
+
}
|
|
8061
|
+
getPage(input) {
|
|
8062
|
+
return this.#pages.getPage(input);
|
|
8063
|
+
}
|
|
8064
|
+
getPageByPath(input) {
|
|
8065
|
+
return this.#pages.getPageByPath(input);
|
|
8066
|
+
}
|
|
8067
|
+
findPagesByName(input) {
|
|
8068
|
+
return this.#pages.findPagesByName(input);
|
|
8069
|
+
}
|
|
8070
|
+
listChildPages(input) {
|
|
8071
|
+
return this.#pages.listChildPages(input);
|
|
8072
|
+
}
|
|
8073
|
+
listPages(input) {
|
|
8074
|
+
return this.#pages.listPages(input);
|
|
8075
|
+
}
|
|
8076
|
+
createPage(input) {
|
|
8077
|
+
return this.#pages.createPage(input);
|
|
8078
|
+
}
|
|
8079
|
+
updatePage(input) {
|
|
8080
|
+
return this.#pages.updatePage(input);
|
|
8081
|
+
}
|
|
8082
|
+
movePage(input) {
|
|
8083
|
+
return this.#pages.movePage(input);
|
|
8084
|
+
}
|
|
8085
|
+
mergePages(input) {
|
|
8086
|
+
return this.#pages.mergePages(input);
|
|
8087
|
+
}
|
|
8088
|
+
deletePage(input) {
|
|
8089
|
+
return this.#pages.deletePage(input);
|
|
8090
|
+
}
|
|
8091
|
+
getNote(input) {
|
|
8092
|
+
return this.#pages.getNote(input);
|
|
8093
|
+
}
|
|
8094
|
+
listNotes(input) {
|
|
8095
|
+
return this.#pages.listNotes(input);
|
|
8096
|
+
}
|
|
8097
|
+
countChildPages(input) {
|
|
8098
|
+
return this.#pages.countChildPages(input);
|
|
8099
|
+
}
|
|
8100
|
+
countNotes(input) {
|
|
8101
|
+
return this.#pages.countNotes(input);
|
|
8102
|
+
}
|
|
8103
|
+
listNotesMentioning(input) {
|
|
8104
|
+
return this.#pages.listNotesMentioning(input);
|
|
8105
|
+
}
|
|
8106
|
+
addNote(input) {
|
|
8107
|
+
return this.#pages.addNote(input);
|
|
8108
|
+
}
|
|
8109
|
+
restampNote(input) {
|
|
8110
|
+
return this.#pages.restampNote(input);
|
|
8111
|
+
}
|
|
8112
|
+
removeNote(input) {
|
|
8113
|
+
return this.#pages.removeNote(input);
|
|
8114
|
+
}
|
|
8115
|
+
restoreNote(input) {
|
|
8116
|
+
return this.#pages.restoreNote(input);
|
|
8117
|
+
}
|
|
8118
|
+
searchPages(input) {
|
|
8119
|
+
return this.#pages.searchPages(input);
|
|
8120
|
+
}
|
|
8121
|
+
listPageActivity(input) {
|
|
8122
|
+
return this.#pages.listPageActivity(input);
|
|
8123
|
+
}
|
|
8124
|
+
addPinAudience(input) {
|
|
8125
|
+
return this.#pages.addPinAudience(input);
|
|
8126
|
+
}
|
|
8127
|
+
removePinAudience(input) {
|
|
8128
|
+
return this.#pages.removePinAudience(input);
|
|
8129
|
+
}
|
|
8130
|
+
getPages(input) {
|
|
8131
|
+
return this.#pages.getPages(input);
|
|
8132
|
+
}
|
|
8133
|
+
listPins(input) {
|
|
8134
|
+
return this.#pages.listPins(input);
|
|
8135
|
+
}
|
|
8136
|
+
async #transaction(operation) {
|
|
8137
|
+
return this.#db.executeWriteOperationWithRetry(() => withClientWriteLock(this.#client, async () => {
|
|
8138
|
+
const tx = await this.#client.transaction("write");
|
|
8139
|
+
try {
|
|
8140
|
+
const result = await operation(tx);
|
|
8141
|
+
await tx.commit();
|
|
8142
|
+
return result;
|
|
8143
|
+
} catch (error) {
|
|
8144
|
+
if (!tx.closed) await tx.rollback();
|
|
8145
|
+
throw error;
|
|
8146
|
+
}
|
|
8147
|
+
}), "write knowledge state");
|
|
8507
8148
|
}
|
|
8508
8149
|
};
|
|
8509
8150
|
// src/storage/domains/mcp-clients/index.ts
|