@lucern/graph-primitives 0.1.0-alpha.2
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/README.md +29 -0
- package/dist/beliefDecay-Q_26RTc-.d.ts +72 -0
- package/dist/beliefDecay.d.ts +2 -0
- package/dist/beliefDecay.js +1628 -0
- package/dist/beliefDecay.js.map +1 -0
- package/dist/beliefEvidenceLinks-42FlR48t.d.ts +77 -0
- package/dist/beliefEvidenceLinks.d.ts +1 -0
- package/dist/beliefEvidenceLinks.js +1978 -0
- package/dist/beliefEvidenceLinks.js.map +1 -0
- package/dist/beliefLifecycle-C-AehZgF.d.ts +43 -0
- package/dist/beliefLifecycle.d.ts +1 -0
- package/dist/beliefLifecycle.js +98 -0
- package/dist/beliefLifecycle.js.map +1 -0
- package/dist/confidencePropagationDispatch.d.ts +46 -0
- package/dist/confidencePropagationDispatch.js +744 -0
- package/dist/confidencePropagationDispatch.js.map +1 -0
- package/dist/contradictions-Hdwl7zid.d.ts +71 -0
- package/dist/contradictions.d.ts +1 -0
- package/dist/contradictions.js +1557 -0
- package/dist/contradictions.js.map +1 -0
- package/dist/convex.d.ts +23 -0
- package/dist/convex.js +17 -0
- package/dist/convex.js.map +1 -0
- package/dist/edgeValidation-CeI0wc0r.d.ts +35 -0
- package/dist/edgeValidation.d.ts +2 -0
- package/dist/edgeValidation.js +307 -0
- package/dist/edgeValidation.js.map +1 -0
- package/dist/edges/contains.d.ts +6 -0
- package/dist/edges/contains.js +14 -0
- package/dist/edges/contains.js.map +1 -0
- package/dist/edges/contradicts.d.ts +6 -0
- package/dist/edges/contradicts.js +183 -0
- package/dist/edges/contradicts.js.map +1 -0
- package/dist/edges/dependsOn.d.ts +6 -0
- package/dist/edges/dependsOn.js +240 -0
- package/dist/edges/dependsOn.js.map +1 -0
- package/dist/edges/derivedFrom.d.ts +6 -0
- package/dist/edges/derivedFrom.js +14 -0
- package/dist/edges/derivedFrom.js.map +1 -0
- package/dist/edges/elaborates.d.ts +6 -0
- package/dist/edges/elaborates.js +100 -0
- package/dist/edges/elaborates.js.map +1 -0
- package/dist/edges/index.d.ts +3 -0
- package/dist/edges/index.js +556 -0
- package/dist/edges/index.js.map +1 -0
- package/dist/edges/informs.d.ts +6 -0
- package/dist/edges/informs.js +112 -0
- package/dist/edges/informs.js.map +1 -0
- package/dist/edges/propagationTypes.d.ts +39 -0
- package/dist/edges/propagationTypes.js +17 -0
- package/dist/edges/propagationTypes.js.map +1 -0
- package/dist/edges/refutes.d.ts +6 -0
- package/dist/edges/refutes.js +108 -0
- package/dist/edges/refutes.js.map +1 -0
- package/dist/edges/supports.d.ts +6 -0
- package/dist/edges/supports.js +193 -0
- package/dist/edges/supports.js.map +1 -0
- package/dist/edges/tests.d.ts +6 -0
- package/dist/edges/tests.js +14 -0
- package/dist/edges/tests.js.map +1 -0
- package/dist/edges/utils.d.ts +12 -0
- package/dist/edges/utils.js +188 -0
- package/dist/edges/utils.js.map +1 -0
- package/dist/embeddingTrigger.d.ts +24 -0
- package/dist/embeddingTrigger.js +24 -0
- package/dist/embeddingTrigger.js.map +1 -0
- package/dist/entityBridge-DMaKooYn.d.ts +59 -0
- package/dist/entityBridge.d.ts +1 -0
- package/dist/entityBridge.js +663 -0
- package/dist/entityBridge.js.map +1 -0
- package/dist/entityLifecycle-BkhRJ-XI.d.ts +69 -0
- package/dist/entityLifecycle.d.ts +1 -0
- package/dist/entityLifecycle.js +2083 -0
- package/dist/entityLifecycle.js.map +1 -0
- package/dist/entityValidation-KLZ_Xl2D.d.ts +50 -0
- package/dist/entityValidation.d.ts +3 -0
- package/dist/entityValidation.js +71 -0
- package/dist/entityValidation.js.map +1 -0
- package/dist/epistemicAnswers-DSP1slZ9.d.ts +67 -0
- package/dist/epistemicAnswers.d.ts +1 -0
- package/dist/epistemicAnswers.js +1650 -0
- package/dist/epistemicAnswers.js.map +1 -0
- package/dist/epistemicBeliefs-DtFVTp-k.d.ts +377 -0
- package/dist/epistemicBeliefs.d.ts +5 -0
- package/dist/epistemicBeliefs.js +6386 -0
- package/dist/epistemicBeliefs.js.map +1 -0
- package/dist/epistemicContractHelpers.d.ts +1 -0
- package/dist/epistemicContractHelpers.js +320 -0
- package/dist/epistemicContractHelpers.js.map +1 -0
- package/dist/epistemicContracts.d.ts +77 -0
- package/dist/epistemicContracts.js +8436 -0
- package/dist/epistemicContracts.js.map +1 -0
- package/dist/epistemicEdges-DcA8ErUG.d.ts +191 -0
- package/dist/epistemicEdges.d.ts +2 -0
- package/dist/epistemicEdges.js +2749 -0
- package/dist/epistemicEdges.js.map +1 -0
- package/dist/epistemicEvidence-Bo638XDP.d.ts +128 -0
- package/dist/epistemicEvidence.d.ts +3 -0
- package/dist/epistemicEvidence.js +3282 -0
- package/dist/epistemicEvidence.js.map +1 -0
- package/dist/epistemicHelpers-Bd9xbaib.d.ts +329 -0
- package/dist/epistemicHelpers.d.ts +4 -0
- package/dist/epistemicHelpers.js +999 -0
- package/dist/epistemicHelpers.js.map +1 -0
- package/dist/epistemicLinking-CyeLOIzN.d.ts +35 -0
- package/dist/epistemicLinking.d.ts +1 -0
- package/dist/epistemicLinking.js +1391 -0
- package/dist/epistemicLinking.js.map +1 -0
- package/dist/epistemicNodes-BpD6Koud.d.ts +167 -0
- package/dist/epistemicNodes.d.ts +2 -0
- package/dist/epistemicNodes.js +2942 -0
- package/dist/epistemicNodes.js.map +1 -0
- package/dist/epistemicQuestions-CmEeY6zQ.d.ts +214 -0
- package/dist/epistemicQuestions.d.ts +3 -0
- package/dist/epistemicQuestions.js +4993 -0
- package/dist/epistemicQuestions.js.map +1 -0
- package/dist/epistemicSources-ZazxHOK1.d.ts +25 -0
- package/dist/epistemicSources.d.ts +1 -0
- package/dist/epistemicSources.js +2025 -0
- package/dist/epistemicSources.js.map +1 -0
- package/dist/evaluators/index.d.ts +9 -0
- package/dist/evaluators/index.js +8440 -0
- package/dist/evaluators/index.js.map +1 -0
- package/dist/evaluators/lintCheckerEvaluator.d.ts +11 -0
- package/dist/evaluators/lintCheckerEvaluator.js +155 -0
- package/dist/evaluators/lintCheckerEvaluator.js.map +1 -0
- package/dist/evaluators/sentryCheckerEvaluator.d.ts +11 -0
- package/dist/evaluators/sentryCheckerEvaluator.js +126 -0
- package/dist/evaluators/sentryCheckerEvaluator.js.map +1 -0
- package/dist/evaluators/shared.d.ts +27 -0
- package/dist/evaluators/shared.js +92 -0
- package/dist/evaluators/shared.js.map +1 -0
- package/dist/evaluators/testRunnerEvaluator.d.ts +17 -0
- package/dist/evaluators/testRunnerEvaluator.js +232 -0
- package/dist/evaluators/testRunnerEvaluator.js.map +1 -0
- package/dist/evaluators/tscCheckerEvaluator.d.ts +11 -0
- package/dist/evaluators/tscCheckerEvaluator.js +189 -0
- package/dist/evaluators/tscCheckerEvaluator.js.map +1 -0
- package/dist/globalId-DKh9d_uD.d.ts +20 -0
- package/dist/globalId.d.ts +1 -0
- package/dist/globalId.js +15 -0
- package/dist/globalId.js.map +1 -0
- package/dist/graphTypes-CpgIuCdo.d.ts +52 -0
- package/dist/graphTypes.d.ts +1 -0
- package/dist/graphTypes.js +120 -0
- package/dist/graphTypes.js.map +1 -0
- package/dist/helpers-BYHIk5vU.d.ts +27 -0
- package/dist/helpers.d.ts +4 -0
- package/dist/helpers.js +313 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index-Dct1T70K.d.ts +25 -0
- package/dist/index-Dq-7R-gi.d.ts +31 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +22294 -0
- package/dist/index.js.map +1 -0
- package/dist/invariantEnforcement.d.ts +52 -0
- package/dist/invariantEnforcement.js +231 -0
- package/dist/invariantEnforcement.js.map +1 -0
- package/dist/logicalRoleInference-CJxqWi3u.d.ts +16 -0
- package/dist/logicalRoleInference.d.ts +3 -0
- package/dist/logicalRoleInference.js +64 -0
- package/dist/logicalRoleInference.js.map +1 -0
- package/dist/matcherFeedbackUtils.d.ts +33 -0
- package/dist/matcherFeedbackUtils.js +95 -0
- package/dist/matcherFeedbackUtils.js.map +1 -0
- package/dist/ontology-matching-Buhu23ss.d.ts +48 -0
- package/dist/ontology-matching.d.ts +2 -0
- package/dist/ontology-matching.js +346 -0
- package/dist/ontology-matching.js.map +1 -0
- package/dist/ontologyApproval-Ba0Jjk1k.d.ts +26 -0
- package/dist/ontologyApproval.d.ts +1 -0
- package/dist/ontologyApproval.js +78 -0
- package/dist/ontologyApproval.js.map +1 -0
- package/dist/ontologyDefinitions.d.ts +72 -0
- package/dist/ontologyDefinitions.js +635 -0
- package/dist/ontologyDefinitions.js.map +1 -0
- package/dist/ontologyHelpers.d.ts +79 -0
- package/dist/ontologyHelpers.js +81 -0
- package/dist/ontologyHelpers.js.map +1 -0
- package/dist/ontologyRegistry-B67rPJ16.d.ts +31 -0
- package/dist/ontologyRegistry.d.ts +1 -0
- package/dist/ontologyRegistry.js +296 -0
- package/dist/ontologyRegistry.js.map +1 -0
- package/dist/projectionReconciliation-CxrXYGaB.d.ts +20 -0
- package/dist/projectionReconciliation.d.ts +1 -0
- package/dist/projectionReconciliation.js +261 -0
- package/dist/projectionReconciliation.js.map +1 -0
- package/dist/projectionStaleness-CAdpIsaW.d.ts +51 -0
- package/dist/projectionStaleness.d.ts +1 -0
- package/dist/projectionStaleness.js +57 -0
- package/dist/projectionStaleness.js.map +1 -0
- package/dist/questionEvidenceLinks-BdQD0TkM.d.ts +34 -0
- package/dist/questionEvidenceLinks.d.ts +1 -0
- package/dist/questionEvidenceLinks.js +1690 -0
- package/dist/questionEvidenceLinks.js.map +1 -0
- package/dist/resolverTypes-CC8Ea2E2.d.ts +20 -0
- package/dist/resolverTypes.d.ts +4 -0
- package/dist/resolverTypes.js +3 -0
- package/dist/resolverTypes.js.map +1 -0
- package/dist/resolvers-Br1a6eLV.d.ts +14 -0
- package/dist/resolvers.d.ts +5 -0
- package/dist/resolvers.js +308 -0
- package/dist/resolvers.js.map +1 -0
- package/dist/scopeResolverCompat.d.ts +26 -0
- package/dist/scopeResolverCompat.js +242 -0
- package/dist/scopeResolverCompat.js.map +1 -0
- package/dist/text-matching-CMn2WnVD.d.ts +40 -0
- package/dist/text-matching.d.ts +2 -0
- package/dist/text-matching.js +246 -0
- package/dist/text-matching.js.map +1 -0
- package/dist/topicOntologyResolver.d.ts +80 -0
- package/dist/topicOntologyResolver.js +67 -0
- package/dist/topicOntologyResolver.js.map +1 -0
- package/dist/topicProjectOverlay.d.ts +92 -0
- package/dist/topicProjectOverlay.js +249 -0
- package/dist/topicProjectOverlay.js.map +1 -0
- package/dist/topicScope-By_zp4tt.d.ts +34 -0
- package/dist/topicScope.d.ts +3 -0
- package/dist/topicScope.js +206 -0
- package/dist/topicScope.js.map +1 -0
- package/dist/workspaceIsolation.d.ts +44 -0
- package/dist/workspaceIsolation.js +950 -0
- package/dist/workspaceIsolation.js.map +1 -0
- package/package.json +46 -0
|
@@ -0,0 +1,2083 @@
|
|
|
1
|
+
import { v } from 'convex/values';
|
|
2
|
+
import { componentsGeneric, queryGeneric, mutationGeneric, anyApi } from 'convex/server';
|
|
3
|
+
|
|
4
|
+
// src/entityLifecycle.ts
|
|
5
|
+
var api = anyApi;
|
|
6
|
+
componentsGeneric();
|
|
7
|
+
|
|
8
|
+
// ../access-control/src/topicProjectOverlay.ts
|
|
9
|
+
var LEGACY_SCOPE_FIELD = "graphScopeProjectId";
|
|
10
|
+
function readNonEmptyString(value) {
|
|
11
|
+
if (typeof value !== "string") {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
const normalized = value.trim();
|
|
15
|
+
return normalized.length > 0 ? normalized : void 0;
|
|
16
|
+
}
|
|
17
|
+
function readStringArray(value) {
|
|
18
|
+
if (!Array.isArray(value)) {
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
return value.map((entry) => readNonEmptyString(entry)).filter((entry) => Boolean(entry));
|
|
22
|
+
}
|
|
23
|
+
function readMetadata(topic) {
|
|
24
|
+
return topic.metadata && typeof topic.metadata === "object" ? topic.metadata : {};
|
|
25
|
+
}
|
|
26
|
+
function readLegacyProjectId(value) {
|
|
27
|
+
if (!value) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
return readNonEmptyString(value[LEGACY_SCOPE_FIELD]);
|
|
31
|
+
}
|
|
32
|
+
function coerceVisibility(value) {
|
|
33
|
+
return value === "private" || value === "team" || value === "firm" || value === "external" || value === "public" ? value : void 0;
|
|
34
|
+
}
|
|
35
|
+
function coerceStatus(value) {
|
|
36
|
+
return value === "active" || value === "archived" || value === "watching" ? value : void 0;
|
|
37
|
+
}
|
|
38
|
+
function mapProjectType(topic, metadata) {
|
|
39
|
+
const explicit = readNonEmptyString(metadata.projectType);
|
|
40
|
+
if (explicit) {
|
|
41
|
+
return explicit;
|
|
42
|
+
}
|
|
43
|
+
if (topic.type === "theme") {
|
|
44
|
+
return "thematic";
|
|
45
|
+
}
|
|
46
|
+
return readNonEmptyString(topic.type) || "general";
|
|
47
|
+
}
|
|
48
|
+
function isProjectLikeTopic(topic) {
|
|
49
|
+
const metadata = readMetadata(topic);
|
|
50
|
+
return topic.type === "theme" || topic.type === "thematic" || topic.type === "deal" || topic.type === "monitoring" || readLegacyProjectId(topic) !== void 0 || readNonEmptyString(metadata.projectType) !== void 0;
|
|
51
|
+
}
|
|
52
|
+
async function resolveTopicDoc(ctx, scopeId) {
|
|
53
|
+
if (ctx?.db && typeof ctx.db.get === "function") {
|
|
54
|
+
try {
|
|
55
|
+
const directTopic = await ctx.db.get(scopeId);
|
|
56
|
+
if (directTopic) {
|
|
57
|
+
return directTopic;
|
|
58
|
+
}
|
|
59
|
+
} catch {
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (typeof ctx.runQuery !== "function") {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
const topic = await ctx.runQuery(api.topics.get, {
|
|
67
|
+
id: String(scopeId)
|
|
68
|
+
});
|
|
69
|
+
if (topic?.name !== void 0 && topic?.type !== void 0) {
|
|
70
|
+
return topic;
|
|
71
|
+
}
|
|
72
|
+
} catch {
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
const topic = await ctx.runQuery(api.topics.getByLegacyScopeId, {
|
|
76
|
+
projectId: String(scopeId)
|
|
77
|
+
});
|
|
78
|
+
if (topic?.name !== void 0 && topic?.type !== void 0) {
|
|
79
|
+
return topic;
|
|
80
|
+
}
|
|
81
|
+
} catch {
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
function materializeTopicProjectOverlay(topic, idMode = "legacy") {
|
|
86
|
+
const metadata = readMetadata(topic);
|
|
87
|
+
const topicId = String(topic._id);
|
|
88
|
+
const legacyProjectId = readLegacyProjectId(topic) || readLegacyProjectId(metadata) || readNonEmptyString(metadata.legacyProjectId);
|
|
89
|
+
const storageProjectId = legacyProjectId || topicId;
|
|
90
|
+
const outwardId = idMode === "topic" ? topicId : storageProjectId;
|
|
91
|
+
const visibility = coerceVisibility(topic.visibility) || coerceVisibility(metadata.visibility) || "private";
|
|
92
|
+
const status = coerceStatus(topic.status) || coerceStatus(metadata.status) || "active";
|
|
93
|
+
const createdAt = typeof topic.createdAt === "number" ? topic.createdAt : typeof topic._creationTime === "number" ? topic._creationTime : 0;
|
|
94
|
+
const updatedAt = typeof topic.updatedAt === "number" ? topic.updatedAt : typeof metadata.updatedAt === "number" ? metadata.updatedAt : createdAt;
|
|
95
|
+
return {
|
|
96
|
+
...metadata,
|
|
97
|
+
_id: outwardId,
|
|
98
|
+
projectId: outwardId,
|
|
99
|
+
topicId,
|
|
100
|
+
storageProjectId,
|
|
101
|
+
legacyProjectId,
|
|
102
|
+
name: readNonEmptyString(topic.name) || "Untitled Theme",
|
|
103
|
+
type: mapProjectType(topic, metadata),
|
|
104
|
+
description: readNonEmptyString(topic.description),
|
|
105
|
+
ownerId: readNonEmptyString(metadata.ownerId) || readNonEmptyString(topic.createdBy) || "system",
|
|
106
|
+
sharedWith: readStringArray(metadata.sharedWith),
|
|
107
|
+
visibility,
|
|
108
|
+
tenantId: readNonEmptyString(topic.tenantId) || readNonEmptyString(metadata.tenantId),
|
|
109
|
+
workspaceId: readNonEmptyString(topic.workspaceId) || readNonEmptyString(metadata.workspaceId),
|
|
110
|
+
status,
|
|
111
|
+
tags: readStringArray(metadata.tags),
|
|
112
|
+
chatCount: typeof metadata.chatCount === "number" ? metadata.chatCount : 0,
|
|
113
|
+
artifactCount: typeof metadata.artifactCount === "number" ? metadata.artifactCount : 0,
|
|
114
|
+
lastActivityAt: typeof metadata.lastActivityAt === "number" ? metadata.lastActivityAt : updatedAt,
|
|
115
|
+
_creationTime: typeof topic._creationTime === "number" ? topic._creationTime : createdAt,
|
|
116
|
+
createdAt,
|
|
117
|
+
updatedAt
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
async function resolveTopicProjectOverlay(ctx, scopeId, options = {}) {
|
|
121
|
+
const topic = await resolveTopicDoc(ctx, scopeId);
|
|
122
|
+
if (!topic) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
if (options.projectLikeOnly !== false && !isProjectLikeTopic(topic)) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
return materializeTopicProjectOverlay(topic, options.idMode);
|
|
129
|
+
}
|
|
130
|
+
async function listTopicProjectOverlays(ctx, options = {}) {
|
|
131
|
+
let allTopics = [];
|
|
132
|
+
if (ctx?.db?.query && typeof ctx.db.query === "function") {
|
|
133
|
+
try {
|
|
134
|
+
allTopics = await ctx.db.query("topics").collect();
|
|
135
|
+
} catch {
|
|
136
|
+
allTopics = [];
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (allTopics.length === 0 && typeof ctx.runQuery === "function") {
|
|
140
|
+
allTopics = (await ctx.runQuery(api.topics.list, {}) ?? []) || [];
|
|
141
|
+
}
|
|
142
|
+
return allTopics.filter(
|
|
143
|
+
(topic) => options.projectLikeOnly === false || isProjectLikeTopic(topic)
|
|
144
|
+
).map((topic) => materializeTopicProjectOverlay(topic, options.idMode));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// ../access-control/src/projectGrantsBridge.ts
|
|
148
|
+
var PROJECT_GRANT_STATUSES = ["active", "revoked", "expired"];
|
|
149
|
+
function normalizeString(value) {
|
|
150
|
+
if (typeof value !== "string") {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const trimmed = value.trim();
|
|
154
|
+
return trimmed.length > 0 ? trimmed : void 0;
|
|
155
|
+
}
|
|
156
|
+
async function resolveGrantScopeIds(ctx, args) {
|
|
157
|
+
const topicId = normalizeString(args.topicId);
|
|
158
|
+
const projectId = normalizeString(args.projectId);
|
|
159
|
+
for (const scopeId of [topicId, projectId]) {
|
|
160
|
+
if (!scopeId) {
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
const overlay = await resolveTopicProjectOverlay(ctx, scopeId, {
|
|
165
|
+
idMode: "legacy",
|
|
166
|
+
projectLikeOnly: false
|
|
167
|
+
});
|
|
168
|
+
if (overlay) {
|
|
169
|
+
return {
|
|
170
|
+
topicId: normalizeString(overlay.topicId) ?? topicId,
|
|
171
|
+
projectId: normalizeString(overlay.projectId) ?? projectId ?? scopeId
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
} catch {
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return { topicId, projectId };
|
|
178
|
+
}
|
|
179
|
+
async function normalizeProjectGrantRow(ctx, row) {
|
|
180
|
+
const scope = await resolveGrantScopeIds(ctx, {
|
|
181
|
+
topicId: row.topicId,
|
|
182
|
+
projectId: row.projectId
|
|
183
|
+
});
|
|
184
|
+
return {
|
|
185
|
+
...row,
|
|
186
|
+
...scope.topicId ? { topicId: scope.topicId } : {},
|
|
187
|
+
...scope.projectId ?? scope.topicId ? { projectId: scope.projectId ?? scope.topicId } : {}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
async function normalizeProjectGrantRows(ctx, rows) {
|
|
191
|
+
return await Promise.all(rows.map((row) => normalizeProjectGrantRow(ctx, row)));
|
|
192
|
+
}
|
|
193
|
+
async function listProjectGrantsByPrincipal(ctx, principalId) {
|
|
194
|
+
const rows = await Promise.all(
|
|
195
|
+
PROJECT_GRANT_STATUSES.map(
|
|
196
|
+
(status) => ctx.db.query("projectGrants").withIndex(
|
|
197
|
+
"by_principal_status",
|
|
198
|
+
(q) => q.eq("principalId", principalId).eq("status", status)
|
|
199
|
+
).collect()
|
|
200
|
+
)
|
|
201
|
+
);
|
|
202
|
+
return await normalizeProjectGrantRows(ctx, rows.flat());
|
|
203
|
+
}
|
|
204
|
+
async function listProjectGrantsByGroup(ctx, groupId) {
|
|
205
|
+
const rows = await Promise.all(
|
|
206
|
+
PROJECT_GRANT_STATUSES.map(
|
|
207
|
+
(status) => ctx.db.query("projectGrants").withIndex(
|
|
208
|
+
"by_group_status",
|
|
209
|
+
(q) => q.eq("groupId", groupId).eq("status", status)
|
|
210
|
+
).collect()
|
|
211
|
+
)
|
|
212
|
+
);
|
|
213
|
+
return await normalizeProjectGrantRows(ctx, rows.flat());
|
|
214
|
+
}
|
|
215
|
+
function buildScopeMatchers(inputScopeId, resolved) {
|
|
216
|
+
return new Set(
|
|
217
|
+
[inputScopeId, resolved.topicId, resolved.projectId].map((value) => normalizeString(value)).filter((value) => Boolean(value))
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
function matchesResolvedScope(row, scopeIds) {
|
|
221
|
+
const rowTopicId = normalizeString(row.topicId);
|
|
222
|
+
const rowProjectId = normalizeString(row.projectId);
|
|
223
|
+
return rowTopicId !== void 0 && scopeIds.has(rowTopicId) || rowProjectId !== void 0 && scopeIds.has(rowProjectId);
|
|
224
|
+
}
|
|
225
|
+
async function bridgeListProjectGrantsByTopicAndPrincipal(ctx, topicId, principalId) {
|
|
226
|
+
const resolved = await resolveGrantScopeIds(ctx, { topicId });
|
|
227
|
+
const scopeIds = buildScopeMatchers(topicId, resolved);
|
|
228
|
+
const rows = await listProjectGrantsByPrincipal(ctx, principalId);
|
|
229
|
+
return rows.filter((row) => matchesResolvedScope(row, scopeIds));
|
|
230
|
+
}
|
|
231
|
+
async function bridgeListProjectGrantsByTopicAndGroup(ctx, topicId, groupId) {
|
|
232
|
+
const resolved = await resolveGrantScopeIds(ctx, { topicId });
|
|
233
|
+
const scopeIds = buildScopeMatchers(topicId, resolved);
|
|
234
|
+
const rows = await listProjectGrantsByGroup(ctx, groupId);
|
|
235
|
+
return rows.filter((row) => matchesResolvedScope(row, scopeIds));
|
|
236
|
+
}
|
|
237
|
+
async function bridgeListProjectGrantsByPrincipalStatus(ctx, principalId, status) {
|
|
238
|
+
const rows = await listProjectGrantsByPrincipal(ctx, principalId);
|
|
239
|
+
return rows.filter((row) => row.status === status);
|
|
240
|
+
}
|
|
241
|
+
async function bridgeListProjectGrantsByGroupStatus(ctx, groupId, status) {
|
|
242
|
+
const rows = await listProjectGrantsByGroup(ctx, groupId);
|
|
243
|
+
return rows.filter((row) => row.status === status);
|
|
244
|
+
}
|
|
245
|
+
async function bridgeInsertProjectGrant(ctx, value) {
|
|
246
|
+
const resolved = await resolveGrantScopeIds(ctx, value);
|
|
247
|
+
return await ctx.db.insert("projectGrants", {
|
|
248
|
+
...value,
|
|
249
|
+
...resolved.topicId ? { topicId: resolved.topicId } : {},
|
|
250
|
+
...resolved.projectId ?? resolved.topicId ? { projectId: resolved.projectId ?? resolved.topicId } : {}
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// ../access-control/src/resolvers.ts
|
|
255
|
+
async function findUserByClerkId(ctx, clerkId) {
|
|
256
|
+
const normalizedClerkId = clerkId.trim();
|
|
257
|
+
if (!normalizedClerkId) {
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
if (typeof ctx.runQuery === "function") {
|
|
261
|
+
try {
|
|
262
|
+
const bridgedUser = await ctx.runQuery(api.users.getUserByClerkId, {
|
|
263
|
+
clerkId: normalizedClerkId
|
|
264
|
+
});
|
|
265
|
+
if (bridgedUser) {
|
|
266
|
+
return bridgedUser;
|
|
267
|
+
}
|
|
268
|
+
} catch {
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
try {
|
|
272
|
+
const users = await ctx.db.query("users").collect();
|
|
273
|
+
return users.find((user) => String(user.clerkId ?? "") === normalizedClerkId) ?? null;
|
|
274
|
+
} catch {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
async function findUserByPrincipalId(ctx, principalId) {
|
|
279
|
+
const normalizedPrincipalId = principalId.trim();
|
|
280
|
+
if (!normalizedPrincipalId) {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
try {
|
|
284
|
+
const users = await ctx.db.query("users").collect();
|
|
285
|
+
return users.find(
|
|
286
|
+
(user) => String(user.defaultPrincipalId ?? "") === normalizedPrincipalId
|
|
287
|
+
) ?? null;
|
|
288
|
+
} catch {
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
async function findAgentByPrincipalId(ctx, principalId) {
|
|
293
|
+
const normalizedPrincipalId = principalId.trim();
|
|
294
|
+
if (!normalizedPrincipalId) {
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
if (typeof ctx.runQuery === "function") {
|
|
298
|
+
try {
|
|
299
|
+
const bridgedAgent = await ctx.runQuery(
|
|
300
|
+
api.agents.getAgentByPrincipalId,
|
|
301
|
+
{
|
|
302
|
+
principalId: normalizedPrincipalId
|
|
303
|
+
}
|
|
304
|
+
);
|
|
305
|
+
if (bridgedAgent) {
|
|
306
|
+
return bridgedAgent;
|
|
307
|
+
}
|
|
308
|
+
} catch {
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
try {
|
|
312
|
+
const agents = await ctx.db.query("agents").collect();
|
|
313
|
+
return agents.find(
|
|
314
|
+
(agent) => String(agent.principalId ?? "") === normalizedPrincipalId
|
|
315
|
+
) ?? null;
|
|
316
|
+
} catch {
|
|
317
|
+
return null;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
function defaultResolvers() {
|
|
321
|
+
return {
|
|
322
|
+
async getProject(ctx, topicId) {
|
|
323
|
+
return await resolveTopicProjectOverlay(ctx, topicId, {
|
|
324
|
+
idMode: "legacy",
|
|
325
|
+
projectLikeOnly: false
|
|
326
|
+
});
|
|
327
|
+
},
|
|
328
|
+
async listTopics(ctx) {
|
|
329
|
+
return await listTopicProjectOverlays(ctx, { idMode: "legacy" });
|
|
330
|
+
},
|
|
331
|
+
async listTopicsByOwner(ctx, ownerId) {
|
|
332
|
+
const topics = await listTopicProjectOverlays(ctx, { idMode: "legacy" });
|
|
333
|
+
return topics.filter((topic) => topic.ownerId === ownerId);
|
|
334
|
+
},
|
|
335
|
+
async listTopicsByVisibility(ctx, visibility) {
|
|
336
|
+
const topics = await listTopicProjectOverlays(ctx, { idMode: "legacy" });
|
|
337
|
+
return topics.filter((topic) => topic.visibility === visibility);
|
|
338
|
+
},
|
|
339
|
+
async listProjectGrantsByProjectAndPrincipal(ctx, topicId, principalId) {
|
|
340
|
+
return await bridgeListProjectGrantsByTopicAndPrincipal(
|
|
341
|
+
ctx,
|
|
342
|
+
topicId,
|
|
343
|
+
principalId
|
|
344
|
+
);
|
|
345
|
+
},
|
|
346
|
+
async listProjectGrantsByProjectAndGroup(ctx, topicId, groupId) {
|
|
347
|
+
return await bridgeListProjectGrantsByTopicAndGroup(ctx, topicId, groupId);
|
|
348
|
+
},
|
|
349
|
+
async listProjectGrantsByPrincipalStatus(ctx, principalId, status) {
|
|
350
|
+
return await bridgeListProjectGrantsByPrincipalStatus(
|
|
351
|
+
ctx,
|
|
352
|
+
principalId,
|
|
353
|
+
status
|
|
354
|
+
);
|
|
355
|
+
},
|
|
356
|
+
async listProjectGrantsByGroupStatus(ctx, groupId, status) {
|
|
357
|
+
return await bridgeListProjectGrantsByGroupStatus(ctx, groupId, status);
|
|
358
|
+
},
|
|
359
|
+
async insertProjectGrant(ctx, value) {
|
|
360
|
+
return await bridgeInsertProjectGrant(ctx, value);
|
|
361
|
+
},
|
|
362
|
+
async getAgentByPrincipalId(ctx, principalId) {
|
|
363
|
+
return await findAgentByPrincipalId(ctx, principalId);
|
|
364
|
+
},
|
|
365
|
+
async getUserByClerkId(ctx, clerkId) {
|
|
366
|
+
return await findUserByClerkId(ctx, clerkId);
|
|
367
|
+
},
|
|
368
|
+
async getUserByPrincipalId(ctx, principalId) {
|
|
369
|
+
return await findUserByPrincipalId(ctx, principalId);
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
var resolverOverrides = {};
|
|
374
|
+
function resolveAccessControlAppResolvers(_ctx) {
|
|
375
|
+
return {
|
|
376
|
+
...defaultResolvers(),
|
|
377
|
+
...resolverOverrides
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// ../access-control/src/principalContext.ts
|
|
382
|
+
function requireCanonicalResolvedUser(user, clerkId) {
|
|
383
|
+
const resolved = user;
|
|
384
|
+
if (!resolved) {
|
|
385
|
+
throw new Error(
|
|
386
|
+
`[AccessControl] Canonical user identity required for ${clerkId}. Sync users.upsertUser before user-bound access checks.`
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
const { mcRole, defaultTenantId, defaultWorkspaceId, defaultPrincipalId } = resolved;
|
|
390
|
+
if (mcRole !== "platform_admin" && mcRole !== "tenant_admin" && mcRole !== "workspace_admin" && mcRole !== "editor" && mcRole !== "viewer" && mcRole !== "auditor" && mcRole !== "service_agent") {
|
|
391
|
+
throw new Error(
|
|
392
|
+
`[AccessControl] Canonical MC role required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
if (typeof defaultTenantId !== "string" || defaultTenantId.trim().length === 0) {
|
|
396
|
+
throw new Error(
|
|
397
|
+
`[AccessControl] Canonical home tenant required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
if (typeof defaultWorkspaceId !== "string" || defaultWorkspaceId.trim().length === 0) {
|
|
401
|
+
throw new Error(
|
|
402
|
+
`[AccessControl] Canonical home workspace required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
if (typeof defaultPrincipalId !== "string" || defaultPrincipalId.trim().length === 0) {
|
|
406
|
+
throw new Error(
|
|
407
|
+
`[AccessControl] Canonical federated principal required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
return {
|
|
411
|
+
mcRole,
|
|
412
|
+
defaultTenantId: defaultTenantId.trim(),
|
|
413
|
+
defaultWorkspaceId: defaultWorkspaceId.trim(),
|
|
414
|
+
defaultPrincipalId: defaultPrincipalId.trim()
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
function isPrincipalIdInput(value) {
|
|
418
|
+
return value.startsWith("user:") || value.startsWith("group:") || value.startsWith("service:") || value.startsWith("agent:") || value.startsWith("external_viewer:");
|
|
419
|
+
}
|
|
420
|
+
async function resolveCanonicalUserRecord(ctx, actorId) {
|
|
421
|
+
const normalizedActorId = actorId.trim();
|
|
422
|
+
const clerkId = isPrincipalIdInput(normalizedActorId) && normalizedActorId.startsWith("user:") ? normalizedActorId.slice("user:".length) : normalizedActorId;
|
|
423
|
+
const resolvers = resolveAccessControlAppResolvers();
|
|
424
|
+
const resolvedByClerkId = await resolvers.getUserByClerkId(ctx, clerkId);
|
|
425
|
+
if (resolvedByClerkId) {
|
|
426
|
+
return {
|
|
427
|
+
resolvedUser: resolvedByClerkId,
|
|
428
|
+
clerkId,
|
|
429
|
+
contextClerkId: clerkId
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
const resolvedByPrincipalId = await resolvers.getUserByPrincipalId(
|
|
433
|
+
ctx,
|
|
434
|
+
normalizedActorId
|
|
435
|
+
);
|
|
436
|
+
return {
|
|
437
|
+
resolvedUser: resolvedByPrincipalId ?? null,
|
|
438
|
+
clerkId,
|
|
439
|
+
contextClerkId: normalizedActorId.startsWith("user:") && clerkId.length > 0 ? clerkId : normalizedActorId
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
function uniqRoles(roles) {
|
|
443
|
+
const roleSet = /* @__PURE__ */ new Set();
|
|
444
|
+
for (const role of roles) {
|
|
445
|
+
if (role === "platform_admin" || role === "tenant_admin" || role === "workspace_admin" || role === "editor" || role === "viewer" || role === "auditor" || role === "service_agent") {
|
|
446
|
+
roleSet.add(role);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return [...roleSet];
|
|
450
|
+
}
|
|
451
|
+
function normalizeGroupIds(value) {
|
|
452
|
+
if (!Array.isArray(value)) {
|
|
453
|
+
return [];
|
|
454
|
+
}
|
|
455
|
+
return [...new Set(
|
|
456
|
+
value.filter((entry) => typeof entry === "string").map((entry) => entry.trim()).filter(Boolean)
|
|
457
|
+
)];
|
|
458
|
+
}
|
|
459
|
+
function requireServiceAgentUser(user, actorId) {
|
|
460
|
+
const canonicalUser = requireCanonicalResolvedUser(user, actorId);
|
|
461
|
+
if (canonicalUser.mcRole !== "service_agent") {
|
|
462
|
+
throw new Error(
|
|
463
|
+
`[AccessControl] Canonical service_agent identity required for ${actorId}. Sync users.upsertUser before agent-bound access checks.`
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
return canonicalUser;
|
|
467
|
+
}
|
|
468
|
+
function requireCanonicalResolvedAgent(agent, actorId) {
|
|
469
|
+
const resolved = agent;
|
|
470
|
+
if (!resolved) {
|
|
471
|
+
throw new Error(
|
|
472
|
+
`[AccessControl] Agent "${actorId}" not found in agents or users table.`
|
|
473
|
+
);
|
|
474
|
+
}
|
|
475
|
+
if (typeof resolved.principalId !== "string" || resolved.principalId.trim().length === 0) {
|
|
476
|
+
throw new Error(
|
|
477
|
+
`[AccessControl] Canonical agent principalId required for ${actorId}.`
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
if (typeof resolved.tenantId !== "string" || resolved.tenantId.trim().length === 0) {
|
|
481
|
+
throw new Error(
|
|
482
|
+
`[AccessControl] Canonical home tenant required for ${actorId}.`
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
if (typeof resolved.workspaceId !== "string" || resolved.workspaceId.trim().length === 0) {
|
|
486
|
+
throw new Error(
|
|
487
|
+
`[AccessControl] Canonical home workspace required for ${actorId}.`
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
return {
|
|
491
|
+
principalId: resolved.principalId.trim(),
|
|
492
|
+
tenantId: resolved.tenantId.trim(),
|
|
493
|
+
workspaceId: resolved.workspaceId.trim(),
|
|
494
|
+
roles: uniqRoles(Array.isArray(resolved.roles) ? resolved.roles : []) ?? ["service_agent"],
|
|
495
|
+
groupIds: normalizeGroupIds(resolved.groupIds)
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
async function resolvePrincipalContext(ctx, actorId) {
|
|
499
|
+
if (actorId.startsWith("agent:")) {
|
|
500
|
+
const resolvers = resolveAccessControlAppResolvers();
|
|
501
|
+
const resolvedAgent = await resolvers.getAgentByPrincipalId(ctx, actorId);
|
|
502
|
+
if (resolvedAgent) {
|
|
503
|
+
const agent = requireCanonicalResolvedAgent(
|
|
504
|
+
resolvedAgent,
|
|
505
|
+
actorId
|
|
506
|
+
);
|
|
507
|
+
return {
|
|
508
|
+
principalId: agent.principalId,
|
|
509
|
+
principalType: "service",
|
|
510
|
+
clerkId: actorId,
|
|
511
|
+
tenantId: agent.tenantId,
|
|
512
|
+
workspaceId: agent.workspaceId,
|
|
513
|
+
roles: agent.roles.length > 0 ? agent.roles : ["service_agent"],
|
|
514
|
+
groupIds: agent.groupIds,
|
|
515
|
+
isPlatformAdmin: false,
|
|
516
|
+
isTenantAdmin: false,
|
|
517
|
+
isWorkspaceAdmin: false,
|
|
518
|
+
isSystemFallback: false
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
const resolvedUser2 = await resolvers.getUserByClerkId(
|
|
522
|
+
ctx,
|
|
523
|
+
actorId
|
|
524
|
+
);
|
|
525
|
+
if (!resolvedUser2) {
|
|
526
|
+
throw new Error(
|
|
527
|
+
`[AccessControl] Agent "${actorId}" not found in agents or users table.`
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
const user2 = requireServiceAgentUser(
|
|
531
|
+
resolvedUser2,
|
|
532
|
+
actorId
|
|
533
|
+
);
|
|
534
|
+
console.warn(
|
|
535
|
+
`[AccessControl] Deprecated legacy service-agent fallback for ${actorId}; migrate this principal into identity.agents.`
|
|
536
|
+
);
|
|
537
|
+
return {
|
|
538
|
+
principalId: user2.defaultPrincipalId,
|
|
539
|
+
principalType: "service",
|
|
540
|
+
clerkId: actorId,
|
|
541
|
+
tenantId: user2.defaultTenantId,
|
|
542
|
+
workspaceId: user2.defaultWorkspaceId,
|
|
543
|
+
roles: ["service_agent"],
|
|
544
|
+
groupIds: normalizeGroupIds(resolvedUser2?.principalGroupIds),
|
|
545
|
+
isPlatformAdmin: false,
|
|
546
|
+
isTenantAdmin: false,
|
|
547
|
+
isWorkspaceAdmin: false,
|
|
548
|
+
isSystemFallback: false
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
const {
|
|
552
|
+
resolvedUser,
|
|
553
|
+
contextClerkId
|
|
554
|
+
} = await resolveCanonicalUserRecord(ctx, actorId);
|
|
555
|
+
const user = requireCanonicalResolvedUser(
|
|
556
|
+
resolvedUser,
|
|
557
|
+
contextClerkId
|
|
558
|
+
);
|
|
559
|
+
if (!user.defaultPrincipalId) {
|
|
560
|
+
throw new Error(
|
|
561
|
+
`[AccessControl] Canonical federated principal required for ${contextClerkId}. Re-sync Master Control identity before user-bound access checks.`
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
if (user.mcRole === "service_agent") {
|
|
565
|
+
return {
|
|
566
|
+
principalId: user.defaultPrincipalId,
|
|
567
|
+
principalType: "service",
|
|
568
|
+
clerkId: contextClerkId,
|
|
569
|
+
tenantId: user.defaultTenantId,
|
|
570
|
+
workspaceId: user.defaultWorkspaceId,
|
|
571
|
+
roles: ["service_agent"],
|
|
572
|
+
groupIds: normalizeGroupIds(resolvedUser?.principalGroupIds),
|
|
573
|
+
isPlatformAdmin: false,
|
|
574
|
+
isTenantAdmin: false,
|
|
575
|
+
isWorkspaceAdmin: false,
|
|
576
|
+
isSystemFallback: false
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
const principalId = user.defaultPrincipalId;
|
|
580
|
+
const effectiveRole = user.mcRole;
|
|
581
|
+
const roles = effectiveRole === "platform_admin" ? ["platform_admin", "tenant_admin"] : effectiveRole === "tenant_admin" ? ["tenant_admin"] : [effectiveRole];
|
|
582
|
+
const tenantId = user.defaultTenantId;
|
|
583
|
+
const workspaceId = user.defaultWorkspaceId;
|
|
584
|
+
const isPlatformAdmin = effectiveRole === "platform_admin";
|
|
585
|
+
return {
|
|
586
|
+
principalId,
|
|
587
|
+
principalType: "user",
|
|
588
|
+
clerkId: contextClerkId,
|
|
589
|
+
tenantId,
|
|
590
|
+
workspaceId,
|
|
591
|
+
roles: uniqRoles(roles),
|
|
592
|
+
groupIds: normalizeGroupIds(resolvedUser?.principalGroupIds),
|
|
593
|
+
isPlatformAdmin,
|
|
594
|
+
isTenantAdmin: isPlatformAdmin || effectiveRole === "tenant_admin",
|
|
595
|
+
isWorkspaceAdmin: isPlatformAdmin || effectiveRole === "tenant_admin" || effectiveRole === "workspace_admin",
|
|
596
|
+
isSystemFallback: false
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// ../access-control/src/access.ts
|
|
601
|
+
function isTopicInPrincipalTenant(topic, principalTenantId) {
|
|
602
|
+
if (!topic.tenantId) {
|
|
603
|
+
return false;
|
|
604
|
+
}
|
|
605
|
+
if (!principalTenantId) {
|
|
606
|
+
return false;
|
|
607
|
+
}
|
|
608
|
+
return String(topic.tenantId) === String(principalTenantId);
|
|
609
|
+
}
|
|
610
|
+
function isTopicInPrincipalWorkspace(topic, principalWorkspaceId) {
|
|
611
|
+
if (!topic.workspaceId) {
|
|
612
|
+
return false;
|
|
613
|
+
}
|
|
614
|
+
if (!principalWorkspaceId) {
|
|
615
|
+
return false;
|
|
616
|
+
}
|
|
617
|
+
return String(topic.workspaceId) === String(principalWorkspaceId);
|
|
618
|
+
}
|
|
619
|
+
function isLegacyUnscopedTopic(topic) {
|
|
620
|
+
return !topic.tenantId || !topic.workspaceId;
|
|
621
|
+
}
|
|
622
|
+
function isGrantScopeAlignedToTopic(topic, grant) {
|
|
623
|
+
if (topic.tenantId && grant.tenantId && String(topic.tenantId) !== String(grant.tenantId)) {
|
|
624
|
+
return false;
|
|
625
|
+
}
|
|
626
|
+
if (topic.workspaceId && grant.workspaceId && String(topic.workspaceId) !== String(grant.workspaceId)) {
|
|
627
|
+
return false;
|
|
628
|
+
}
|
|
629
|
+
return true;
|
|
630
|
+
}
|
|
631
|
+
function isGrantSourceAllowedForVisibility(visibility, source) {
|
|
632
|
+
if (source !== "external_share") {
|
|
633
|
+
return true;
|
|
634
|
+
}
|
|
635
|
+
return visibility === "external" || visibility === "public";
|
|
636
|
+
}
|
|
637
|
+
function isGrantActive(grant) {
|
|
638
|
+
if (grant.status !== "active") {
|
|
639
|
+
return false;
|
|
640
|
+
}
|
|
641
|
+
if (grant.expiresAt !== void 0 && grant.expiresAt <= Date.now()) {
|
|
642
|
+
return false;
|
|
643
|
+
}
|
|
644
|
+
return true;
|
|
645
|
+
}
|
|
646
|
+
async function hasPrincipalGrant(ctx, args) {
|
|
647
|
+
const grants = await resolveAccessControlAppResolvers().listProjectGrantsByProjectAndPrincipal(
|
|
648
|
+
ctx,
|
|
649
|
+
args.topic._id,
|
|
650
|
+
args.principalId
|
|
651
|
+
);
|
|
652
|
+
if (grants.some(
|
|
653
|
+
(grant) => isGrantActive(grant) && isGrantScopeAlignedToTopic(args.topic, grant) && isGrantSourceAllowedForVisibility(
|
|
654
|
+
args.topic.visibility,
|
|
655
|
+
grant.source
|
|
656
|
+
) && (!args.principalIsExternal || args.topic.visibility === "public" || grant.source === "external_share")
|
|
657
|
+
)) {
|
|
658
|
+
return true;
|
|
659
|
+
}
|
|
660
|
+
return false;
|
|
661
|
+
}
|
|
662
|
+
async function hasGroupGrant(ctx, args) {
|
|
663
|
+
if (args.groupIds.length === 0) {
|
|
664
|
+
return false;
|
|
665
|
+
}
|
|
666
|
+
for (const groupId of args.groupIds) {
|
|
667
|
+
const grants = await resolveAccessControlAppResolvers().listProjectGrantsByProjectAndGroup(ctx, args.topic._id, groupId);
|
|
668
|
+
if (grants.some(
|
|
669
|
+
(grant) => isGrantActive(grant) && isGrantScopeAlignedToTopic(args.topic, grant) && isGrantSourceAllowedForVisibility(
|
|
670
|
+
args.topic.visibility,
|
|
671
|
+
grant.source
|
|
672
|
+
)
|
|
673
|
+
)) {
|
|
674
|
+
return true;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
return false;
|
|
678
|
+
}
|
|
679
|
+
function isExternalPrincipal(_ctx, _args) {
|
|
680
|
+
return false;
|
|
681
|
+
}
|
|
682
|
+
async function evaluateTopicAccessDetailed(ctx, args) {
|
|
683
|
+
if (args.legacyUserId) {
|
|
684
|
+
return {
|
|
685
|
+
hasAccess: true,
|
|
686
|
+
isAdmin: false,
|
|
687
|
+
isOwner: false,
|
|
688
|
+
isShared: false,
|
|
689
|
+
hasGrant: true,
|
|
690
|
+
isFirmVisible: true,
|
|
691
|
+
isExternalVisible: false,
|
|
692
|
+
isPublicVisible: false,
|
|
693
|
+
isTenantScopeMatch: true,
|
|
694
|
+
isWorkspaceScopeMatch: true,
|
|
695
|
+
isPrincipalExternal: false
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
const topic = await resolveAccessControlAppResolvers().getProject(
|
|
699
|
+
ctx,
|
|
700
|
+
args.topicId
|
|
701
|
+
);
|
|
702
|
+
if (!topic) {
|
|
703
|
+
return {
|
|
704
|
+
hasAccess: false,
|
|
705
|
+
isAdmin: false,
|
|
706
|
+
isOwner: false,
|
|
707
|
+
isShared: false,
|
|
708
|
+
hasGrant: false,
|
|
709
|
+
isFirmVisible: false,
|
|
710
|
+
isExternalVisible: false,
|
|
711
|
+
isPublicVisible: false,
|
|
712
|
+
isTenantScopeMatch: false,
|
|
713
|
+
isWorkspaceScopeMatch: false,
|
|
714
|
+
isPrincipalExternal: false
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
const { principalContext, legacyUserId } = args;
|
|
718
|
+
const userIsAdmin = principalContext.isPlatformAdmin;
|
|
719
|
+
const isOwner = topic.ownerId === legacyUserId;
|
|
720
|
+
const isShared = (topic.sharedWith ?? []).includes(legacyUserId);
|
|
721
|
+
const principalIsExternal = await isExternalPrincipal(ctx, {
|
|
722
|
+
groupIds: principalContext.groupIds,
|
|
723
|
+
topicTenantId: topic.tenantId,
|
|
724
|
+
topicWorkspaceId: topic.workspaceId
|
|
725
|
+
});
|
|
726
|
+
const hasPrincipalGrantResult = await hasPrincipalGrant(ctx, {
|
|
727
|
+
topic,
|
|
728
|
+
principalId: principalContext.principalId,
|
|
729
|
+
principalIsExternal
|
|
730
|
+
});
|
|
731
|
+
const hasGroupGrantResult = await hasGroupGrant(ctx, {
|
|
732
|
+
topic,
|
|
733
|
+
groupIds: principalContext.groupIds
|
|
734
|
+
});
|
|
735
|
+
const hasGrant = isShared || hasPrincipalGrantResult || hasGroupGrantResult;
|
|
736
|
+
const legacyUnscoped = isLegacyUnscopedTopic(topic);
|
|
737
|
+
const tenantScopeMatch = isTopicInPrincipalTenant(
|
|
738
|
+
topic,
|
|
739
|
+
principalContext.tenantId
|
|
740
|
+
);
|
|
741
|
+
const workspaceScopeMatch = isTopicInPrincipalWorkspace(
|
|
742
|
+
topic,
|
|
743
|
+
principalContext.workspaceId
|
|
744
|
+
);
|
|
745
|
+
const isPublicVisible = topic.visibility === "public";
|
|
746
|
+
const isFirmVisible = topic.visibility === "firm" && !legacyUnscoped && tenantScopeMatch && workspaceScopeMatch && !principalIsExternal;
|
|
747
|
+
const hasScopedGrant = hasGrant && (legacyUnscoped || tenantScopeMatch && workspaceScopeMatch);
|
|
748
|
+
const isExternalVisible = topic.visibility === "external" && hasScopedGrant;
|
|
749
|
+
const hasAccess = userIsAdmin || isOwner || hasScopedGrant || isPublicVisible || isFirmVisible;
|
|
750
|
+
return {
|
|
751
|
+
hasAccess,
|
|
752
|
+
isAdmin: userIsAdmin,
|
|
753
|
+
isOwner,
|
|
754
|
+
isShared,
|
|
755
|
+
hasGrant,
|
|
756
|
+
isFirmVisible,
|
|
757
|
+
isExternalVisible,
|
|
758
|
+
isPublicVisible,
|
|
759
|
+
isTenantScopeMatch: tenantScopeMatch,
|
|
760
|
+
isWorkspaceScopeMatch: workspaceScopeMatch,
|
|
761
|
+
isPrincipalExternal: principalIsExternal
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
async function checkTopicAccessDetailed(ctx, topicId, userId) {
|
|
765
|
+
const principalContext = await resolvePrincipalContext(ctx, userId);
|
|
766
|
+
return evaluateTopicAccessDetailed(ctx, {
|
|
767
|
+
topicId,
|
|
768
|
+
legacyUserId: userId,
|
|
769
|
+
principalContext
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
async function checkTopicAccess(ctx, topicId, userId) {
|
|
773
|
+
const result = await checkTopicAccessDetailed(ctx, topicId, userId);
|
|
774
|
+
return result.hasAccess;
|
|
775
|
+
}
|
|
776
|
+
var checkProjectAccess = checkTopicAccess;
|
|
777
|
+
|
|
778
|
+
// ../access-control/src/auth.ts
|
|
779
|
+
async function getCurrentUserId(ctx) {
|
|
780
|
+
const identity = await ctx.auth.getUserIdentity();
|
|
781
|
+
return identity?.subject ?? null;
|
|
782
|
+
}
|
|
783
|
+
var permissiveReturn = v.optional(v.any());
|
|
784
|
+
var looseJsonObject = v.record(v.string(), v.any());
|
|
785
|
+
var looseJsonArray = v.array(v.any());
|
|
786
|
+
v.union(
|
|
787
|
+
v.string(),
|
|
788
|
+
v.number(),
|
|
789
|
+
v.boolean(),
|
|
790
|
+
v.null(),
|
|
791
|
+
looseJsonObject,
|
|
792
|
+
looseJsonArray
|
|
793
|
+
);
|
|
794
|
+
var api2 = anyApi;
|
|
795
|
+
componentsGeneric();
|
|
796
|
+
var internal = anyApi;
|
|
797
|
+
var mutation = mutationGeneric;
|
|
798
|
+
var query = queryGeneric;
|
|
799
|
+
|
|
800
|
+
// src/entityValidation.ts
|
|
801
|
+
async function getEntityTypeSchema(ctx, nodeType, tenantId) {
|
|
802
|
+
if (tenantId) {
|
|
803
|
+
const tenantEntry = await ctx.db.query("schemaEnumConfig").withIndex(
|
|
804
|
+
"by_tenant_category",
|
|
805
|
+
(q) => q.eq("tenantId", tenantId).eq("category", "entity_type")
|
|
806
|
+
).collect();
|
|
807
|
+
const tenantMatch = tenantEntry.find(
|
|
808
|
+
(e) => e.value === nodeType && e.status === "active"
|
|
809
|
+
);
|
|
810
|
+
if (tenantMatch?.metadata?.schema) {
|
|
811
|
+
return tenantMatch.metadata.schema;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
const platformEntry = await ctx.db.query("schemaEnumConfig").withIndex(
|
|
815
|
+
"by_category_value",
|
|
816
|
+
(q) => q.eq("category", "entity_type").eq("value", nodeType)
|
|
817
|
+
).first();
|
|
818
|
+
if (platformEntry?.metadata?.schema && platformEntry.status === "active") {
|
|
819
|
+
return platformEntry.metadata.schema;
|
|
820
|
+
}
|
|
821
|
+
return null;
|
|
822
|
+
}
|
|
823
|
+
async function validateEntityMetadata(ctx, nodeType, metadata, tenantId) {
|
|
824
|
+
const errors = [];
|
|
825
|
+
const schema = await getEntityTypeSchema(ctx, nodeType, tenantId);
|
|
826
|
+
if (!schema) {
|
|
827
|
+
return {
|
|
828
|
+
valid: false,
|
|
829
|
+
errors: [
|
|
830
|
+
`No registered schema for entity type "${nodeType}". Register it in schemaEnumConfig (category="entity_type") before creating entities of this type.`
|
|
831
|
+
]
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
const meta = metadata || {};
|
|
835
|
+
for (const [fieldName, fieldDef] of Object.entries(schema)) {
|
|
836
|
+
if (fieldDef.required) {
|
|
837
|
+
const value = meta[fieldName];
|
|
838
|
+
if (value === void 0 || value === null || value === "") {
|
|
839
|
+
errors.push(`Required field "${fieldName}" is missing or empty`);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
for (const [fieldName, value] of Object.entries(meta)) {
|
|
844
|
+
const fieldDef = schema[fieldName];
|
|
845
|
+
if (!fieldDef) {
|
|
846
|
+
continue;
|
|
847
|
+
}
|
|
848
|
+
if (value === void 0 || value === null) {
|
|
849
|
+
continue;
|
|
850
|
+
}
|
|
851
|
+
const actualType = typeof value;
|
|
852
|
+
if (actualType !== fieldDef.type) {
|
|
853
|
+
errors.push(
|
|
854
|
+
`Field "${fieldName}" expected type "${fieldDef.type}" but got "${actualType}"`
|
|
855
|
+
);
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
return {
|
|
859
|
+
valid: errors.length === 0,
|
|
860
|
+
errors
|
|
861
|
+
};
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
// src/globalId.ts
|
|
865
|
+
function generateGlobalId() {
|
|
866
|
+
const bytes = new Uint8Array(16);
|
|
867
|
+
crypto.getRandomValues(bytes);
|
|
868
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
869
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
870
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(
|
|
871
|
+
""
|
|
872
|
+
);
|
|
873
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
// src/topicOntologyResolver.ts
|
|
877
|
+
var MAX_RESOLUTION_DEPTH = 10;
|
|
878
|
+
async function resolveTopicOntologyInternal(ctx, topicId) {
|
|
879
|
+
let current = await ctx.db.get(topicId);
|
|
880
|
+
if (!current) {
|
|
881
|
+
return null;
|
|
882
|
+
}
|
|
883
|
+
const startTopicId = topicId;
|
|
884
|
+
for (let i = 0; i < MAX_RESOLUTION_DEPTH && current; i++) {
|
|
885
|
+
if (current.ontologyId) {
|
|
886
|
+
const ontologyDef = await ctx.db.get(
|
|
887
|
+
current.ontologyId
|
|
888
|
+
);
|
|
889
|
+
if (!ontologyDef || ontologyDef.status === "archived") {
|
|
890
|
+
if (current.parentTopicId) {
|
|
891
|
+
current = await ctx.db.get(
|
|
892
|
+
current.parentTopicId
|
|
893
|
+
);
|
|
894
|
+
continue;
|
|
895
|
+
}
|
|
896
|
+
return null;
|
|
897
|
+
}
|
|
898
|
+
const versions = await ctx.db.query("ontologyVersions").withIndex(
|
|
899
|
+
"by_ontologyId",
|
|
900
|
+
(q) => q.eq("ontologyId", current?.ontologyId)
|
|
901
|
+
).collect();
|
|
902
|
+
const published = versions.filter((v4) => v4.status === "published").sort((a, b) => (b.publishedAt ?? 0) - (a.publishedAt ?? 0));
|
|
903
|
+
const latestPublished = published[0] ?? null;
|
|
904
|
+
return {
|
|
905
|
+
ontologyId: ontologyDef._id,
|
|
906
|
+
ontologyKey: ontologyDef.ontologyKey,
|
|
907
|
+
ontologyName: ontologyDef.name,
|
|
908
|
+
tier: ontologyDef.tier,
|
|
909
|
+
source: current._id === startTopicId ? "direct" : "inherited",
|
|
910
|
+
sourceTopicId: current._id,
|
|
911
|
+
validEntityTypes: latestPublished ? latestPublished.entityTypes.map((et) => et.value) : [],
|
|
912
|
+
validEdgeTypes: latestPublished ? latestPublished.edgeTypes.map((et) => et.value) : [],
|
|
913
|
+
publishedVersion: latestPublished
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
if (!current.parentTopicId) {
|
|
917
|
+
break;
|
|
918
|
+
}
|
|
919
|
+
current = await ctx.db.get(current.parentTopicId);
|
|
920
|
+
}
|
|
921
|
+
return null;
|
|
922
|
+
}
|
|
923
|
+
async function validateEntityTypeForTopic(ctx, topicId, nodeType) {
|
|
924
|
+
const resolved = await resolveTopicOntologyInternal(ctx, topicId);
|
|
925
|
+
if (!resolved) {
|
|
926
|
+
return { valid: true };
|
|
927
|
+
}
|
|
928
|
+
if (resolved.validEntityTypes.length === 0) {
|
|
929
|
+
return { valid: true };
|
|
930
|
+
}
|
|
931
|
+
if (resolved.validEntityTypes.includes(nodeType)) {
|
|
932
|
+
return { valid: true };
|
|
933
|
+
}
|
|
934
|
+
return {
|
|
935
|
+
valid: false,
|
|
936
|
+
error: `Entity type "${nodeType}" is not defined in the ontology "${resolved.ontologyKey}" (${resolved.ontologyName}). Valid entity types: ${resolved.validEntityTypes.join(", ")}. Source: ${resolved.source} from topic ${resolved.sourceTopicId}.`
|
|
937
|
+
};
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
// src/topicProjectOverlay.ts
|
|
941
|
+
var LEGACY_SCOPE_FIELD2 = "graphScopeProjectId";
|
|
942
|
+
function readNonEmptyString2(value) {
|
|
943
|
+
if (typeof value !== "string") {
|
|
944
|
+
return;
|
|
945
|
+
}
|
|
946
|
+
const normalized = value.trim();
|
|
947
|
+
return normalized.length > 0 ? normalized : void 0;
|
|
948
|
+
}
|
|
949
|
+
function readStringArray2(value) {
|
|
950
|
+
if (!Array.isArray(value)) {
|
|
951
|
+
return [];
|
|
952
|
+
}
|
|
953
|
+
return value.map((entry) => readNonEmptyString2(entry)).filter((entry) => Boolean(entry));
|
|
954
|
+
}
|
|
955
|
+
function readMetadata2(topic) {
|
|
956
|
+
return topic.metadata && typeof topic.metadata === "object" ? topic.metadata : {};
|
|
957
|
+
}
|
|
958
|
+
function readLegacyProjectId2(value) {
|
|
959
|
+
if (!value) {
|
|
960
|
+
return;
|
|
961
|
+
}
|
|
962
|
+
return readNonEmptyString2(value[LEGACY_SCOPE_FIELD2]);
|
|
963
|
+
}
|
|
964
|
+
function coerceVisibility2(value) {
|
|
965
|
+
return value === "private" || value === "team" || value === "firm" || value === "external" || value === "public" ? value : void 0;
|
|
966
|
+
}
|
|
967
|
+
function coerceStatus2(value) {
|
|
968
|
+
return value === "active" || value === "archived" || value === "watching" ? value : void 0;
|
|
969
|
+
}
|
|
970
|
+
function mapProjectType2(topic, metadata) {
|
|
971
|
+
const explicit = readNonEmptyString2(metadata.projectType);
|
|
972
|
+
if (explicit) {
|
|
973
|
+
return explicit;
|
|
974
|
+
}
|
|
975
|
+
if (topic.type === "theme") {
|
|
976
|
+
return "thematic";
|
|
977
|
+
}
|
|
978
|
+
return readNonEmptyString2(topic.type) || "general";
|
|
979
|
+
}
|
|
980
|
+
function isProjectLikeTopic2(topic) {
|
|
981
|
+
const metadata = readMetadata2(topic);
|
|
982
|
+
return topic.type === "theme" || topic.type === "thematic" || topic.type === "deal" || topic.type === "monitoring" || readLegacyProjectId2(topic) !== void 0 || readNonEmptyString2(metadata.projectType) !== void 0;
|
|
983
|
+
}
|
|
984
|
+
function isMissingLucernChildComponentError(error) {
|
|
985
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
986
|
+
return message.includes(
|
|
987
|
+
'Child component ComponentName(Identifier("lucern")) not found'
|
|
988
|
+
) || message.includes("Child component") && message.includes("lucern") && message.includes("not found");
|
|
989
|
+
}
|
|
990
|
+
async function resolveTopicDoc2(ctx, scopeId) {
|
|
991
|
+
if (ctx?.db && typeof ctx.db.get === "function") {
|
|
992
|
+
try {
|
|
993
|
+
const directTopic = await ctx.db.get(scopeId);
|
|
994
|
+
if (directTopic) {
|
|
995
|
+
return directTopic;
|
|
996
|
+
}
|
|
997
|
+
} catch {
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
if (typeof ctx.runQuery !== "function") {
|
|
1001
|
+
return null;
|
|
1002
|
+
}
|
|
1003
|
+
try {
|
|
1004
|
+
const topic = await ctx.runQuery(api2.topics.get, {
|
|
1005
|
+
id: String(scopeId)
|
|
1006
|
+
});
|
|
1007
|
+
if (topic?.name !== void 0 && topic?.type !== void 0) {
|
|
1008
|
+
return topic;
|
|
1009
|
+
}
|
|
1010
|
+
} catch {
|
|
1011
|
+
}
|
|
1012
|
+
try {
|
|
1013
|
+
const topic = await ctx.runQuery(api2.topics.getByLegacyScopeId, {
|
|
1014
|
+
projectId: String(scopeId)
|
|
1015
|
+
});
|
|
1016
|
+
if (topic?.name !== void 0 && topic?.type !== void 0) {
|
|
1017
|
+
return topic;
|
|
1018
|
+
}
|
|
1019
|
+
} catch {
|
|
1020
|
+
}
|
|
1021
|
+
return null;
|
|
1022
|
+
}
|
|
1023
|
+
function materializeTopicProjectOverlay2(topic, idMode = "legacy") {
|
|
1024
|
+
const metadata = readMetadata2(topic);
|
|
1025
|
+
const topicId = String(topic._id);
|
|
1026
|
+
const legacyProjectId = readLegacyProjectId2(topic) || readLegacyProjectId2(metadata) || readNonEmptyString2(metadata.legacyProjectId);
|
|
1027
|
+
const storageProjectId = legacyProjectId || topicId;
|
|
1028
|
+
const outwardId = idMode === "topic" ? topicId : storageProjectId;
|
|
1029
|
+
const visibility = coerceVisibility2(topic.visibility) || coerceVisibility2(metadata.visibility) || "private";
|
|
1030
|
+
const status = coerceStatus2(topic.status) || coerceStatus2(metadata.status) || "active";
|
|
1031
|
+
const createdAt = typeof topic.createdAt === "number" ? topic.createdAt : typeof topic._creationTime === "number" ? topic._creationTime : 0;
|
|
1032
|
+
const updatedAt = typeof topic.updatedAt === "number" ? topic.updatedAt : typeof metadata.updatedAt === "number" ? metadata.updatedAt : createdAt;
|
|
1033
|
+
return {
|
|
1034
|
+
...metadata,
|
|
1035
|
+
_id: outwardId,
|
|
1036
|
+
projectId: outwardId,
|
|
1037
|
+
topicId,
|
|
1038
|
+
storageProjectId,
|
|
1039
|
+
legacyProjectId,
|
|
1040
|
+
name: readNonEmptyString2(topic.name) || "Untitled Theme",
|
|
1041
|
+
type: mapProjectType2(topic, metadata),
|
|
1042
|
+
description: readNonEmptyString2(topic.description),
|
|
1043
|
+
ownerId: readNonEmptyString2(metadata.ownerId) || readNonEmptyString2(topic.createdBy) || "system",
|
|
1044
|
+
sharedWith: readStringArray2(metadata.sharedWith),
|
|
1045
|
+
visibility,
|
|
1046
|
+
tenantId: readNonEmptyString2(topic.tenantId) || readNonEmptyString2(metadata.tenantId),
|
|
1047
|
+
workspaceId: readNonEmptyString2(topic.workspaceId) || readNonEmptyString2(metadata.workspaceId),
|
|
1048
|
+
status,
|
|
1049
|
+
tags: readStringArray2(metadata.tags),
|
|
1050
|
+
chatCount: typeof metadata.chatCount === "number" ? metadata.chatCount : 0,
|
|
1051
|
+
artifactCount: typeof metadata.artifactCount === "number" ? metadata.artifactCount : 0,
|
|
1052
|
+
lastActivityAt: typeof metadata.lastActivityAt === "number" ? metadata.lastActivityAt : updatedAt,
|
|
1053
|
+
_creationTime: typeof topic._creationTime === "number" ? topic._creationTime : createdAt,
|
|
1054
|
+
createdAt,
|
|
1055
|
+
updatedAt
|
|
1056
|
+
};
|
|
1057
|
+
}
|
|
1058
|
+
async function resolveTopicProjectOverlay2(ctx, scopeId, options = {}) {
|
|
1059
|
+
const topic = await resolveTopicDoc2(ctx, scopeId);
|
|
1060
|
+
if (!topic) {
|
|
1061
|
+
return null;
|
|
1062
|
+
}
|
|
1063
|
+
if (options.projectLikeOnly !== false && !isProjectLikeTopic2(topic)) {
|
|
1064
|
+
return null;
|
|
1065
|
+
}
|
|
1066
|
+
return materializeTopicProjectOverlay2(topic, options.idMode);
|
|
1067
|
+
}
|
|
1068
|
+
async function listTopicProjectOverlays2(ctx, options = {}) {
|
|
1069
|
+
let allTopics = [];
|
|
1070
|
+
if (ctx?.db?.query && typeof ctx.db.query === "function") {
|
|
1071
|
+
try {
|
|
1072
|
+
allTopics = await ctx.db.query("topics").collect();
|
|
1073
|
+
} catch {
|
|
1074
|
+
allTopics = [];
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
if (allTopics.length === 0 && typeof ctx.runQuery === "function") {
|
|
1078
|
+
allTopics = (await ctx.runQuery(api2.topics.list, {}) ?? []) || [];
|
|
1079
|
+
}
|
|
1080
|
+
return allTopics.filter(
|
|
1081
|
+
(topic) => options.projectLikeOnly === false || isProjectLikeTopic2(topic)
|
|
1082
|
+
).map((topic) => materializeTopicProjectOverlay2(topic, options.idMode));
|
|
1083
|
+
}
|
|
1084
|
+
async function patchTopicProjectOverlay(ctx, scopeId, value) {
|
|
1085
|
+
const topic = await resolveTopicDoc2(ctx, scopeId);
|
|
1086
|
+
if (!topic) {
|
|
1087
|
+
return null;
|
|
1088
|
+
}
|
|
1089
|
+
const nextMetadata = { ...readMetadata2(topic) };
|
|
1090
|
+
const patch = {};
|
|
1091
|
+
const topicUpdateArgs = {
|
|
1092
|
+
id: String(topic._id)
|
|
1093
|
+
};
|
|
1094
|
+
for (const [key, rawValue] of Object.entries(value)) {
|
|
1095
|
+
switch (key) {
|
|
1096
|
+
case "_id":
|
|
1097
|
+
case "projectId":
|
|
1098
|
+
case "topicId":
|
|
1099
|
+
case "legacyProjectId":
|
|
1100
|
+
case "storageProjectId":
|
|
1101
|
+
break;
|
|
1102
|
+
case "name":
|
|
1103
|
+
case "description":
|
|
1104
|
+
patch[key] = rawValue;
|
|
1105
|
+
topicUpdateArgs[key] = rawValue;
|
|
1106
|
+
break;
|
|
1107
|
+
case "tenantId":
|
|
1108
|
+
case "workspaceId":
|
|
1109
|
+
case "ownerId":
|
|
1110
|
+
throw new Error(
|
|
1111
|
+
`patchTopicProjectOverlay cannot mutate ${key} via component-owned topics`
|
|
1112
|
+
);
|
|
1113
|
+
case "status": {
|
|
1114
|
+
const status = coerceStatus2(rawValue);
|
|
1115
|
+
if (status) {
|
|
1116
|
+
patch.status = status;
|
|
1117
|
+
topicUpdateArgs.status = status;
|
|
1118
|
+
}
|
|
1119
|
+
break;
|
|
1120
|
+
}
|
|
1121
|
+
case "visibility": {
|
|
1122
|
+
const visibility = coerceVisibility2(rawValue);
|
|
1123
|
+
if (visibility) {
|
|
1124
|
+
patch.visibility = visibility;
|
|
1125
|
+
topicUpdateArgs.visibility = visibility;
|
|
1126
|
+
}
|
|
1127
|
+
break;
|
|
1128
|
+
}
|
|
1129
|
+
case "type": {
|
|
1130
|
+
const projectType = readNonEmptyString2(rawValue);
|
|
1131
|
+
if (projectType) {
|
|
1132
|
+
nextMetadata.projectType = projectType;
|
|
1133
|
+
} else {
|
|
1134
|
+
delete nextMetadata.projectType;
|
|
1135
|
+
}
|
|
1136
|
+
break;
|
|
1137
|
+
}
|
|
1138
|
+
case "updatedAt":
|
|
1139
|
+
case "createdAt":
|
|
1140
|
+
break;
|
|
1141
|
+
default:
|
|
1142
|
+
if (rawValue === void 0) {
|
|
1143
|
+
delete nextMetadata[key];
|
|
1144
|
+
} else {
|
|
1145
|
+
nextMetadata[key] = rawValue;
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
patch.updatedAt = Date.now();
|
|
1150
|
+
patch.metadata = nextMetadata;
|
|
1151
|
+
topicUpdateArgs.metadata = nextMetadata;
|
|
1152
|
+
if (typeof ctx.runMutation === "function") {
|
|
1153
|
+
try {
|
|
1154
|
+
await ctx.runMutation(api2.topics.update, topicUpdateArgs);
|
|
1155
|
+
} catch (error) {
|
|
1156
|
+
if (!isMissingLucernChildComponentError(error) || !ctx?.db || typeof ctx.db.patch !== "function") {
|
|
1157
|
+
throw error;
|
|
1158
|
+
}
|
|
1159
|
+
await ctx.db.patch(String(topic._id), patch);
|
|
1160
|
+
}
|
|
1161
|
+
} else if (ctx?.db && typeof ctx.db.patch === "function") {
|
|
1162
|
+
await ctx.db.patch(String(topic._id), patch);
|
|
1163
|
+
} else {
|
|
1164
|
+
throw new Error(
|
|
1165
|
+
"Cannot patch topic without component adapter (ctx.runMutation unavailable)"
|
|
1166
|
+
);
|
|
1167
|
+
}
|
|
1168
|
+
return materializeTopicProjectOverlay2(
|
|
1169
|
+
{
|
|
1170
|
+
...topic,
|
|
1171
|
+
...patch,
|
|
1172
|
+
metadata: nextMetadata
|
|
1173
|
+
}
|
|
1174
|
+
);
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
// src/resolvers.ts
|
|
1178
|
+
function isMissingLucernChildComponentError2(error) {
|
|
1179
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1180
|
+
return message.includes('Child component ComponentName(Identifier("lucern")) not found') || message.includes("Child component") && message.includes("lucern") && message.includes("not found");
|
|
1181
|
+
}
|
|
1182
|
+
function isAdvisoryTopicPatch(value) {
|
|
1183
|
+
const advisoryKeys = /* @__PURE__ */ new Set(["lastActivityAt", "updatedAt"]);
|
|
1184
|
+
const keys = Object.keys(value);
|
|
1185
|
+
return keys.length > 0 && keys.every((key) => advisoryKeys.has(key));
|
|
1186
|
+
}
|
|
1187
|
+
async function patchProjectWithTolerance(ctx, projectId, value) {
|
|
1188
|
+
try {
|
|
1189
|
+
await patchTopicProjectOverlay(ctx, projectId, value);
|
|
1190
|
+
} catch (error) {
|
|
1191
|
+
if (!isAdvisoryTopicPatch(value) || !isMissingLucernChildComponentError2(error)) {
|
|
1192
|
+
throw error;
|
|
1193
|
+
}
|
|
1194
|
+
console.warn("[lucern graph-primitives] Non-fatal advisory topic patch failure", {
|
|
1195
|
+
projectId,
|
|
1196
|
+
keys: Object.keys(value),
|
|
1197
|
+
error: error instanceof Error ? error.message : error
|
|
1198
|
+
});
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
function defaultResolvers2() {
|
|
1202
|
+
return {
|
|
1203
|
+
async getProject(ctx, projectId) {
|
|
1204
|
+
return await resolveTopicProjectOverlay2(ctx, projectId, {
|
|
1205
|
+
idMode: "legacy",
|
|
1206
|
+
projectLikeOnly: false
|
|
1207
|
+
});
|
|
1208
|
+
},
|
|
1209
|
+
async patchProject(ctx, projectId, value) {
|
|
1210
|
+
await patchProjectWithTolerance(ctx, projectId, value);
|
|
1211
|
+
},
|
|
1212
|
+
async listTopics(ctx) {
|
|
1213
|
+
return await listTopicProjectOverlays2(ctx, {
|
|
1214
|
+
idMode: "legacy"
|
|
1215
|
+
});
|
|
1216
|
+
},
|
|
1217
|
+
async getFinalArtifact(ctx, artifactId) {
|
|
1218
|
+
return await ctx.db.get(artifactId);
|
|
1219
|
+
}
|
|
1220
|
+
};
|
|
1221
|
+
}
|
|
1222
|
+
var resolverOverrides2 = {};
|
|
1223
|
+
function resolveGraphPrimitivesAppResolvers(_ctx) {
|
|
1224
|
+
return {
|
|
1225
|
+
...defaultResolvers2(),
|
|
1226
|
+
...resolverOverrides2
|
|
1227
|
+
};
|
|
1228
|
+
}
|
|
1229
|
+
var LEGACY_SCOPE_FIELD3 = "graphScopeProjectId";
|
|
1230
|
+
function asMappedProjectId(topic) {
|
|
1231
|
+
if (!topic) {
|
|
1232
|
+
return;
|
|
1233
|
+
}
|
|
1234
|
+
const directLegacyProjectId = normalizeScopeValue(topic[LEGACY_SCOPE_FIELD3]);
|
|
1235
|
+
if (directLegacyProjectId) {
|
|
1236
|
+
return directLegacyProjectId;
|
|
1237
|
+
}
|
|
1238
|
+
const metadata = topic.metadata || {};
|
|
1239
|
+
const candidate = metadata[LEGACY_SCOPE_FIELD3] || metadata.legacyProjectId || metadata.projectId || metadata.scopeProjectId;
|
|
1240
|
+
return candidate ? candidate : void 0;
|
|
1241
|
+
}
|
|
1242
|
+
function normalizeScopeValue(value) {
|
|
1243
|
+
if (typeof value !== "string") {
|
|
1244
|
+
return;
|
|
1245
|
+
}
|
|
1246
|
+
const normalized = value.trim();
|
|
1247
|
+
return normalized.length > 0 ? normalized : void 0;
|
|
1248
|
+
}
|
|
1249
|
+
function pickPrimaryTopic(candidates) {
|
|
1250
|
+
return [...candidates].sort((a, b) => {
|
|
1251
|
+
const depthA = a.depth ?? 9999;
|
|
1252
|
+
const depthB = b.depth ?? 9999;
|
|
1253
|
+
if (depthA !== depthB) {
|
|
1254
|
+
return depthA - depthB;
|
|
1255
|
+
}
|
|
1256
|
+
const createdA = a.createdAt ?? Number.MAX_SAFE_INTEGER;
|
|
1257
|
+
const createdB = b.createdAt ?? Number.MAX_SAFE_INTEGER;
|
|
1258
|
+
if (createdA !== createdB) {
|
|
1259
|
+
return createdA - createdB;
|
|
1260
|
+
}
|
|
1261
|
+
return String(a.name || "").localeCompare(String(b.name || ""));
|
|
1262
|
+
})[0];
|
|
1263
|
+
}
|
|
1264
|
+
async function findTopicsByScopeAlias(ctx, scopeId) {
|
|
1265
|
+
try {
|
|
1266
|
+
return await ctx.db.query("topics").withIndex(
|
|
1267
|
+
"by_graph_scope_project",
|
|
1268
|
+
(q) => q.eq(LEGACY_SCOPE_FIELD3, scopeId)
|
|
1269
|
+
).collect();
|
|
1270
|
+
} catch {
|
|
1271
|
+
const topics = await ctx.db.query("topics").collect();
|
|
1272
|
+
return topics.filter((topic) => {
|
|
1273
|
+
const normalizedGlobalId = normalizeScopeValue(topic.globalId);
|
|
1274
|
+
const mappedProjectId = asMappedProjectId(topic);
|
|
1275
|
+
return String(topic._id) === scopeId || normalizedGlobalId === scopeId || mappedProjectId === scopeId;
|
|
1276
|
+
});
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
async function tryResolveHostTopicById(ctx, topicId) {
|
|
1280
|
+
if (typeof ctx.runQuery !== "function") {
|
|
1281
|
+
return null;
|
|
1282
|
+
}
|
|
1283
|
+
try {
|
|
1284
|
+
return await ctx.runQuery(api2.topics.get, {
|
|
1285
|
+
id: topicId
|
|
1286
|
+
}) ?? null;
|
|
1287
|
+
} catch {
|
|
1288
|
+
return null;
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
async function tryResolveHostTopicByLegacyScope(ctx, legacyScopeId) {
|
|
1292
|
+
if (typeof ctx.runQuery !== "function") {
|
|
1293
|
+
return null;
|
|
1294
|
+
}
|
|
1295
|
+
try {
|
|
1296
|
+
return await ctx.runQuery(api2.topics.getByLegacyScopeId, {
|
|
1297
|
+
projectId: legacyScopeId
|
|
1298
|
+
}) ?? null;
|
|
1299
|
+
} catch {
|
|
1300
|
+
return null;
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
async function resolveInheritedWorkspaceScope(ctx, topic) {
|
|
1304
|
+
const MAX_DEPTH = 10;
|
|
1305
|
+
let tenantId = normalizeScopeValue(topic.tenantId);
|
|
1306
|
+
let workspaceId = normalizeScopeValue(topic.workspaceId);
|
|
1307
|
+
if (tenantId && workspaceId) {
|
|
1308
|
+
return { tenantId, workspaceId };
|
|
1309
|
+
}
|
|
1310
|
+
let current = topic;
|
|
1311
|
+
for (let i = 0; i < MAX_DEPTH && current?.parentTopicId; i++) {
|
|
1312
|
+
current = await ctx.db.get(current.parentTopicId);
|
|
1313
|
+
if (!current) break;
|
|
1314
|
+
if (!tenantId) {
|
|
1315
|
+
tenantId = normalizeScopeValue(current.tenantId);
|
|
1316
|
+
}
|
|
1317
|
+
if (!workspaceId) {
|
|
1318
|
+
workspaceId = normalizeScopeValue(current.workspaceId);
|
|
1319
|
+
}
|
|
1320
|
+
if (tenantId && workspaceId) break;
|
|
1321
|
+
}
|
|
1322
|
+
return { tenantId, workspaceId };
|
|
1323
|
+
}
|
|
1324
|
+
async function resolveTopicProjectScope(ctx, args) {
|
|
1325
|
+
if (args.topicId) {
|
|
1326
|
+
let topic = null;
|
|
1327
|
+
try {
|
|
1328
|
+
topic = await ctx.db.get(args.topicId);
|
|
1329
|
+
} catch {
|
|
1330
|
+
}
|
|
1331
|
+
if (!topic) {
|
|
1332
|
+
topic = await tryResolveHostTopicById(ctx, String(args.topicId));
|
|
1333
|
+
}
|
|
1334
|
+
if (!topic) {
|
|
1335
|
+
topic = pickPrimaryTopic(
|
|
1336
|
+
await findTopicsByScopeAlias(ctx, String(args.topicId))
|
|
1337
|
+
) ?? null;
|
|
1338
|
+
}
|
|
1339
|
+
if (!topic) {
|
|
1340
|
+
throw new Error(`Topic not found: ${String(args.topicId)}`);
|
|
1341
|
+
}
|
|
1342
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, topic);
|
|
1343
|
+
const mapped = asMappedProjectId(topic);
|
|
1344
|
+
if (mapped) {
|
|
1345
|
+
return {
|
|
1346
|
+
topicId: topic._id,
|
|
1347
|
+
projectId: mapped,
|
|
1348
|
+
tenantId: inherited.tenantId,
|
|
1349
|
+
workspaceId: inherited.workspaceId,
|
|
1350
|
+
source: "topic"
|
|
1351
|
+
};
|
|
1352
|
+
}
|
|
1353
|
+
return {
|
|
1354
|
+
topicId: topic._id,
|
|
1355
|
+
tenantId: inherited.tenantId,
|
|
1356
|
+
workspaceId: inherited.workspaceId,
|
|
1357
|
+
source: "topic"
|
|
1358
|
+
};
|
|
1359
|
+
}
|
|
1360
|
+
if (args.projectId) {
|
|
1361
|
+
let directTopic = null;
|
|
1362
|
+
try {
|
|
1363
|
+
directTopic = await ctx.db.get(
|
|
1364
|
+
args.projectId
|
|
1365
|
+
);
|
|
1366
|
+
} catch {
|
|
1367
|
+
}
|
|
1368
|
+
if (directTopic) {
|
|
1369
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
|
|
1370
|
+
const mapped = asMappedProjectId(directTopic);
|
|
1371
|
+
return {
|
|
1372
|
+
topicId: directTopic._id,
|
|
1373
|
+
projectId: mapped ?? args.projectId,
|
|
1374
|
+
tenantId: inherited.tenantId,
|
|
1375
|
+
workspaceId: inherited.workspaceId,
|
|
1376
|
+
source: "topic_inferred"
|
|
1377
|
+
};
|
|
1378
|
+
}
|
|
1379
|
+
directTopic = await tryResolveHostTopicByLegacyScope(ctx, args.projectId);
|
|
1380
|
+
if (directTopic) {
|
|
1381
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
|
|
1382
|
+
const mapped = asMappedProjectId(directTopic);
|
|
1383
|
+
return {
|
|
1384
|
+
topicId: directTopic._id,
|
|
1385
|
+
projectId: mapped ?? args.projectId,
|
|
1386
|
+
tenantId: inherited.tenantId,
|
|
1387
|
+
workspaceId: inherited.workspaceId,
|
|
1388
|
+
source: "topic_inferred"
|
|
1389
|
+
};
|
|
1390
|
+
}
|
|
1391
|
+
const topics = await findTopicsByScopeAlias(ctx, args.projectId);
|
|
1392
|
+
const primary = pickPrimaryTopic(topics);
|
|
1393
|
+
if (primary) {
|
|
1394
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, primary);
|
|
1395
|
+
return {
|
|
1396
|
+
topicId: primary._id,
|
|
1397
|
+
projectId: args.projectId,
|
|
1398
|
+
tenantId: inherited.tenantId,
|
|
1399
|
+
workspaceId: inherited.workspaceId,
|
|
1400
|
+
source: "project_mapped_topic"
|
|
1401
|
+
};
|
|
1402
|
+
}
|
|
1403
|
+
throw new Error(
|
|
1404
|
+
`Legacy project scope ${String(args.projectId)} has no mapped topic.`
|
|
1405
|
+
);
|
|
1406
|
+
}
|
|
1407
|
+
throw new Error(
|
|
1408
|
+
"Missing scope: provide topicId (preferred) or legacy projectId alias."
|
|
1409
|
+
);
|
|
1410
|
+
}
|
|
1411
|
+
var optionalScopeArgs = {
|
|
1412
|
+
projectId: v.optional(v.string()),
|
|
1413
|
+
topicId: v.optional(v.string())
|
|
1414
|
+
};
|
|
1415
|
+
|
|
1416
|
+
// src/entityLifecycle.ts
|
|
1417
|
+
function throwStructuredMutationError(args) {
|
|
1418
|
+
const error = new Error(args.message);
|
|
1419
|
+
error.status = args.status;
|
|
1420
|
+
error.code = args.code;
|
|
1421
|
+
error.invariantCode = args.invariantCode;
|
|
1422
|
+
error.suggestion = args.suggestion;
|
|
1423
|
+
error.details = args.details;
|
|
1424
|
+
throw error;
|
|
1425
|
+
}
|
|
1426
|
+
async function requireAuthenticatedUserId(ctx) {
|
|
1427
|
+
const userId = await getCurrentUserId(ctx);
|
|
1428
|
+
if (!userId) {
|
|
1429
|
+
throwStructuredMutationError({
|
|
1430
|
+
message: "Authentication required.",
|
|
1431
|
+
status: 401,
|
|
1432
|
+
code: "AUTHENTICATION_REQUIRED",
|
|
1433
|
+
invariantCode: "auth.required",
|
|
1434
|
+
suggestion: "Provide a valid bearer token before invoking entity mutations."
|
|
1435
|
+
});
|
|
1436
|
+
}
|
|
1437
|
+
return userId;
|
|
1438
|
+
}
|
|
1439
|
+
async function requireProjectWriteAccess(ctx, projectId, userId) {
|
|
1440
|
+
const hasAccess = await checkProjectAccess(ctx, projectId, userId);
|
|
1441
|
+
if (!hasAccess) {
|
|
1442
|
+
throwStructuredMutationError({
|
|
1443
|
+
message: "Project access required.",
|
|
1444
|
+
status: 403,
|
|
1445
|
+
code: "FORBIDDEN",
|
|
1446
|
+
invariantCode: "policy.scope_required",
|
|
1447
|
+
suggestion: "Request write access for the project and retry.",
|
|
1448
|
+
details: { projectId, userId }
|
|
1449
|
+
});
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
function generateContentHash(nodeType, text) {
|
|
1453
|
+
const content = `${nodeType}:${text.trim().toLowerCase().replace(/\s+/g, " ")}`;
|
|
1454
|
+
let hash = 5381;
|
|
1455
|
+
for (let i = 0; i < content.length; i++) {
|
|
1456
|
+
hash = (hash << 5) + hash + content.charCodeAt(i);
|
|
1457
|
+
hash &= hash;
|
|
1458
|
+
}
|
|
1459
|
+
return Math.abs(hash).toString(16).padStart(8, "0");
|
|
1460
|
+
}
|
|
1461
|
+
var ONTOLOGICAL_NODE_TYPES = [
|
|
1462
|
+
"company",
|
|
1463
|
+
"person",
|
|
1464
|
+
"investor",
|
|
1465
|
+
"function",
|
|
1466
|
+
"value_chain"
|
|
1467
|
+
];
|
|
1468
|
+
function isOntologicalNodeType(nodeType) {
|
|
1469
|
+
return ONTOLOGICAL_NODE_TYPES.includes(nodeType);
|
|
1470
|
+
}
|
|
1471
|
+
function normalizeCanonicalEntityText(value) {
|
|
1472
|
+
return value.trim().toLowerCase().replace(/\s+/g, " ");
|
|
1473
|
+
}
|
|
1474
|
+
function buildEntityTitle(canonicalText) {
|
|
1475
|
+
return canonicalText.slice(0, 100) + (canonicalText.length > 100 ? "..." : "");
|
|
1476
|
+
}
|
|
1477
|
+
async function resolveCanonicalEntityScope(ctx, args) {
|
|
1478
|
+
const scope = await resolveTopicProjectScope(ctx, args);
|
|
1479
|
+
const topic = scope.topicId ? await ctx.db.get(scope.topicId) ?? null : null;
|
|
1480
|
+
const project = scope.projectId !== void 0 ? await resolveGraphPrimitivesAppResolvers().getProject(
|
|
1481
|
+
ctx,
|
|
1482
|
+
scope.projectId
|
|
1483
|
+
) ?? null : null;
|
|
1484
|
+
const tenantId = typeof topic?.tenantId === "string" && topic.tenantId.trim().length > 0 ? topic.tenantId.trim() : typeof project?.tenantId === "string" && project.tenantId.trim().length > 0 ? project.tenantId.trim() : void 0;
|
|
1485
|
+
return {
|
|
1486
|
+
...scope,
|
|
1487
|
+
tenantId,
|
|
1488
|
+
topic,
|
|
1489
|
+
project
|
|
1490
|
+
};
|
|
1491
|
+
}
|
|
1492
|
+
function matchesCanonicalEntityRecord(node, args) {
|
|
1493
|
+
if (node.epistemicLayer !== "ontological") {
|
|
1494
|
+
return false;
|
|
1495
|
+
}
|
|
1496
|
+
if (node.workspaceId !== void 0) {
|
|
1497
|
+
return false;
|
|
1498
|
+
}
|
|
1499
|
+
if (args.nodeType && node.nodeType !== args.nodeType) {
|
|
1500
|
+
return false;
|
|
1501
|
+
}
|
|
1502
|
+
if (args.contentHash && node.contentHash !== args.contentHash) {
|
|
1503
|
+
return false;
|
|
1504
|
+
}
|
|
1505
|
+
const rowTenantId = typeof node.tenantId === "string" && node.tenantId.trim().length > 0 ? node.tenantId.trim() : void 0;
|
|
1506
|
+
if (rowTenantId !== args.tenantId) {
|
|
1507
|
+
return false;
|
|
1508
|
+
}
|
|
1509
|
+
if (args.includeArchived) {
|
|
1510
|
+
return node.status !== "deleted";
|
|
1511
|
+
}
|
|
1512
|
+
return node.status === "active";
|
|
1513
|
+
}
|
|
1514
|
+
async function findExistingCanonicalEntity(ctx, args) {
|
|
1515
|
+
const contentHash = generateContentHash(args.nodeType, args.canonicalText);
|
|
1516
|
+
const candidates = await ctx.db.query("epistemicNodes").withIndex("by_contentHash", (q) => q.eq("contentHash", contentHash)).collect();
|
|
1517
|
+
return candidates.find(
|
|
1518
|
+
(node) => matchesCanonicalEntityRecord(node, {
|
|
1519
|
+
nodeType: args.nodeType,
|
|
1520
|
+
tenantId: args.tenantId,
|
|
1521
|
+
contentHash
|
|
1522
|
+
})
|
|
1523
|
+
) ?? null;
|
|
1524
|
+
}
|
|
1525
|
+
async function listCanonicalEntitiesForScope(ctx, args) {
|
|
1526
|
+
const baseRows = args.tenantId !== void 0 ? await ctx.db.query("epistemicNodes").withIndex("by_tenantId", (q) => q.eq("tenantId", args.tenantId)).collect() : args.nodeType ? await ctx.db.query("epistemicNodes").withIndex("by_nodeType", (q) => q.eq("nodeType", args.nodeType)).collect() : await ctx.db.query("epistemicNodes").collect();
|
|
1527
|
+
const normalizedSearch = typeof args.searchTerm === "string" && args.searchTerm.trim().length > 0 ? normalizeCanonicalEntityText(args.searchTerm) : void 0;
|
|
1528
|
+
return baseRows.filter(
|
|
1529
|
+
(node) => matchesCanonicalEntityRecord(node, {
|
|
1530
|
+
nodeType: args.nodeType,
|
|
1531
|
+
tenantId: args.tenantId,
|
|
1532
|
+
includeArchived: args.includeArchived
|
|
1533
|
+
})
|
|
1534
|
+
).filter((node) => {
|
|
1535
|
+
if (!normalizedSearch) {
|
|
1536
|
+
return true;
|
|
1537
|
+
}
|
|
1538
|
+
const text = typeof node.canonicalText === "string" ? node.canonicalText : "";
|
|
1539
|
+
return normalizeCanonicalEntityText(text).includes(normalizedSearch);
|
|
1540
|
+
}).sort(
|
|
1541
|
+
(a, b) => String(a.title ?? a.canonicalText ?? "").localeCompare(
|
|
1542
|
+
String(b.title ?? b.canonicalText ?? "")
|
|
1543
|
+
)
|
|
1544
|
+
).slice(0, args.limit);
|
|
1545
|
+
}
|
|
1546
|
+
var entityVerificationStatusValidator = v.union(
|
|
1547
|
+
v.literal("unverified"),
|
|
1548
|
+
v.literal("human_verified"),
|
|
1549
|
+
v.literal("ai_verified"),
|
|
1550
|
+
v.literal("contradicted"),
|
|
1551
|
+
v.literal("outdated")
|
|
1552
|
+
);
|
|
1553
|
+
var listCanonicalEntities = query({
|
|
1554
|
+
args: {
|
|
1555
|
+
topicId: v.optional(v.string()),
|
|
1556
|
+
scopeProjectId: v.optional(v.string()),
|
|
1557
|
+
nodeType: v.optional(v.string()),
|
|
1558
|
+
searchTerm: v.optional(v.string()),
|
|
1559
|
+
includeArchived: v.optional(v.boolean()),
|
|
1560
|
+
limit: v.optional(v.number())
|
|
1561
|
+
},
|
|
1562
|
+
returns: permissiveReturn,
|
|
1563
|
+
handler: async (ctx, args) => {
|
|
1564
|
+
const scope = await resolveCanonicalEntityScope(ctx, {
|
|
1565
|
+
topicId: args.topicId,
|
|
1566
|
+
projectId: args.scopeProjectId
|
|
1567
|
+
});
|
|
1568
|
+
const limit = Math.max(1, Math.min(Math.floor(args.limit ?? 100), 500));
|
|
1569
|
+
return listCanonicalEntitiesForScope(ctx, {
|
|
1570
|
+
tenantId: scope.tenantId,
|
|
1571
|
+
nodeType: args.nodeType,
|
|
1572
|
+
searchTerm: args.searchTerm,
|
|
1573
|
+
includeArchived: args.includeArchived,
|
|
1574
|
+
limit
|
|
1575
|
+
});
|
|
1576
|
+
}
|
|
1577
|
+
});
|
|
1578
|
+
var createEntity = mutation({
|
|
1579
|
+
args: {
|
|
1580
|
+
nodeType: v.string(),
|
|
1581
|
+
// "company", "person", "investor", "function", "value_chain"
|
|
1582
|
+
canonicalText: v.string(),
|
|
1583
|
+
// Display name (e.g., "Anthropic", "Dario Amodei")
|
|
1584
|
+
metadata: v.optional(v.any()),
|
|
1585
|
+
// Type-specific attributes validated against schema
|
|
1586
|
+
subtype: v.optional(v.string()),
|
|
1587
|
+
// e.g., "company/private", "investor/vc"
|
|
1588
|
+
...optionalScopeArgs
|
|
1589
|
+
},
|
|
1590
|
+
returns: permissiveReturn,
|
|
1591
|
+
handler: async (ctx, args) => {
|
|
1592
|
+
const authenticatedUserId = await requireAuthenticatedUserId(ctx);
|
|
1593
|
+
const now = Date.now();
|
|
1594
|
+
const canonicalText = args.canonicalText.trim();
|
|
1595
|
+
if (!canonicalText) {
|
|
1596
|
+
throwStructuredMutationError({
|
|
1597
|
+
message: "canonicalText is required.",
|
|
1598
|
+
status: 400,
|
|
1599
|
+
code: "INVALID_REQUEST",
|
|
1600
|
+
invariantCode: "entity.type_check"
|
|
1601
|
+
});
|
|
1602
|
+
}
|
|
1603
|
+
if (!isOntologicalNodeType(args.nodeType)) {
|
|
1604
|
+
throwStructuredMutationError({
|
|
1605
|
+
message: `"${args.nodeType}" is not an ontological entity type. Use epistemicBeliefs.create for epistemic nodes.`,
|
|
1606
|
+
status: 400,
|
|
1607
|
+
code: "INVALID_NODE_TYPE",
|
|
1608
|
+
invariantCode: "entity.type_check",
|
|
1609
|
+
suggestion: `Valid entity types: ${ONTOLOGICAL_NODE_TYPES.join(", ")}`
|
|
1610
|
+
});
|
|
1611
|
+
}
|
|
1612
|
+
const scope = await resolveCanonicalEntityScope(ctx, {
|
|
1613
|
+
topicId: args.topicId,
|
|
1614
|
+
projectId: args.projectId
|
|
1615
|
+
});
|
|
1616
|
+
if (scope.projectId) {
|
|
1617
|
+
await requireProjectWriteAccess(
|
|
1618
|
+
ctx,
|
|
1619
|
+
scope.projectId,
|
|
1620
|
+
authenticatedUserId
|
|
1621
|
+
);
|
|
1622
|
+
}
|
|
1623
|
+
if (scope.topicId) {
|
|
1624
|
+
const ontologyValidation = await validateEntityTypeForTopic(
|
|
1625
|
+
ctx,
|
|
1626
|
+
scope.topicId,
|
|
1627
|
+
args.nodeType
|
|
1628
|
+
);
|
|
1629
|
+
if (!ontologyValidation.valid) {
|
|
1630
|
+
throwStructuredMutationError({
|
|
1631
|
+
message: `Ontology validation failed: ${ontologyValidation.error}`,
|
|
1632
|
+
status: 400,
|
|
1633
|
+
code: "ONTOLOGY_VALIDATION_ERROR",
|
|
1634
|
+
invariantCode: "entity.ontology_type_check",
|
|
1635
|
+
suggestion: "Use a nodeType that is defined in the ontology bound to this topic, or update the ontology to include this entity type.",
|
|
1636
|
+
details: { nodeType: args.nodeType, topicId: scope.topicId }
|
|
1637
|
+
});
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
const metadata = args.metadata || {};
|
|
1641
|
+
const validation = await validateEntityMetadata(
|
|
1642
|
+
ctx,
|
|
1643
|
+
args.nodeType,
|
|
1644
|
+
metadata,
|
|
1645
|
+
scope.tenantId
|
|
1646
|
+
);
|
|
1647
|
+
if (!validation.valid) {
|
|
1648
|
+
throwStructuredMutationError({
|
|
1649
|
+
message: `Entity metadata validation failed: ${validation.errors.join("; ")}`,
|
|
1650
|
+
status: 400,
|
|
1651
|
+
code: "VALIDATION_ERROR",
|
|
1652
|
+
invariantCode: "entity.metadata_schema",
|
|
1653
|
+
suggestion: "Check the registered schema for this entity type in schemaEnumConfig.",
|
|
1654
|
+
details: { errors: validation.errors }
|
|
1655
|
+
});
|
|
1656
|
+
}
|
|
1657
|
+
const existing = await findExistingCanonicalEntity(ctx, {
|
|
1658
|
+
nodeType: args.nodeType,
|
|
1659
|
+
canonicalText,
|
|
1660
|
+
tenantId: scope.tenantId
|
|
1661
|
+
});
|
|
1662
|
+
if (existing) {
|
|
1663
|
+
return {
|
|
1664
|
+
nodeId: existing._id,
|
|
1665
|
+
globalId: existing.globalId,
|
|
1666
|
+
isDuplicate: true
|
|
1667
|
+
};
|
|
1668
|
+
}
|
|
1669
|
+
const entityGlobalId = generateGlobalId();
|
|
1670
|
+
const nodeId = await ctx.db.insert("epistemicNodes", {
|
|
1671
|
+
globalId: entityGlobalId,
|
|
1672
|
+
nodeType: args.nodeType,
|
|
1673
|
+
epistemicLayer: "ontological",
|
|
1674
|
+
subtype: args.subtype,
|
|
1675
|
+
canonicalText,
|
|
1676
|
+
contentHash: generateContentHash(args.nodeType, canonicalText),
|
|
1677
|
+
title: buildEntityTitle(canonicalText),
|
|
1678
|
+
metadata: {
|
|
1679
|
+
...metadata,
|
|
1680
|
+
...args.subtype ? { subtype: args.subtype } : {},
|
|
1681
|
+
status: "active"
|
|
1682
|
+
},
|
|
1683
|
+
tenantId: scope.tenantId,
|
|
1684
|
+
sourceType: "human",
|
|
1685
|
+
verificationStatus: "unverified",
|
|
1686
|
+
status: "active",
|
|
1687
|
+
projectId: scope.projectId,
|
|
1688
|
+
createdBy: authenticatedUserId,
|
|
1689
|
+
createdAt: now,
|
|
1690
|
+
updatedAt: now
|
|
1691
|
+
});
|
|
1692
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
1693
|
+
nodeId,
|
|
1694
|
+
operation: "upsert"
|
|
1695
|
+
});
|
|
1696
|
+
await ctx.db.insert("epistemicAudit", {
|
|
1697
|
+
entityType: "entity",
|
|
1698
|
+
entityId: nodeId,
|
|
1699
|
+
changeType: "created",
|
|
1700
|
+
changedAt: now,
|
|
1701
|
+
changedBy: authenticatedUserId,
|
|
1702
|
+
isAgent: false,
|
|
1703
|
+
previousState: null,
|
|
1704
|
+
newState: {
|
|
1705
|
+
canonicalText,
|
|
1706
|
+
nodeType: args.nodeType,
|
|
1707
|
+
tenantId: scope.tenantId,
|
|
1708
|
+
workspaceId: null
|
|
1709
|
+
},
|
|
1710
|
+
projectId: scope.projectId
|
|
1711
|
+
});
|
|
1712
|
+
return { nodeId, globalId: entityGlobalId, isDuplicate: false };
|
|
1713
|
+
}
|
|
1714
|
+
});
|
|
1715
|
+
var updateEntityAttributes = mutation({
|
|
1716
|
+
args: {
|
|
1717
|
+
nodeId: v.id("epistemicNodes"),
|
|
1718
|
+
canonicalText: v.optional(v.string()),
|
|
1719
|
+
title: v.optional(v.string()),
|
|
1720
|
+
metadata: v.optional(v.any()),
|
|
1721
|
+
// Partial metadata to merge
|
|
1722
|
+
subtype: v.optional(v.string()),
|
|
1723
|
+
domain: v.optional(v.string()),
|
|
1724
|
+
tags: v.optional(v.array(v.string())),
|
|
1725
|
+
verificationStatus: v.optional(entityVerificationStatusValidator),
|
|
1726
|
+
externalIds: v.optional(
|
|
1727
|
+
v.object({
|
|
1728
|
+
crunchbase: v.optional(v.string()),
|
|
1729
|
+
linkedin: v.optional(v.string()),
|
|
1730
|
+
pitchbook: v.optional(v.string()),
|
|
1731
|
+
twitter: v.optional(v.string()),
|
|
1732
|
+
website: v.optional(v.string())
|
|
1733
|
+
})
|
|
1734
|
+
)
|
|
1735
|
+
},
|
|
1736
|
+
returns: permissiveReturn,
|
|
1737
|
+
handler: async (ctx, args) => {
|
|
1738
|
+
const authenticatedUserId = await requireAuthenticatedUserId(ctx);
|
|
1739
|
+
const now = Date.now();
|
|
1740
|
+
const node = await ctx.db.get(args.nodeId);
|
|
1741
|
+
if (!node) {
|
|
1742
|
+
throwStructuredMutationError({
|
|
1743
|
+
message: "Entity not found.",
|
|
1744
|
+
status: 404,
|
|
1745
|
+
code: "NOT_FOUND",
|
|
1746
|
+
details: { nodeId: args.nodeId }
|
|
1747
|
+
});
|
|
1748
|
+
}
|
|
1749
|
+
if (!isOntologicalNodeType(node.nodeType)) {
|
|
1750
|
+
throwStructuredMutationError({
|
|
1751
|
+
message: `Node "${args.nodeId}" is a ${node.nodeType}, not an entity. Use belief/question APIs for epistemic nodes.`,
|
|
1752
|
+
status: 400,
|
|
1753
|
+
code: "LIFECYCLE_VIOLATION",
|
|
1754
|
+
invariantCode: "entity.type_check",
|
|
1755
|
+
suggestion: "Only ontological entities can be updated via updateEntityAttributes."
|
|
1756
|
+
});
|
|
1757
|
+
}
|
|
1758
|
+
const scopeProjectId = typeof node.projectId === "string" && node.projectId.trim().length > 0 ? node.projectId : void 0;
|
|
1759
|
+
if (scopeProjectId) {
|
|
1760
|
+
await requireProjectWriteAccess(ctx, scopeProjectId, authenticatedUserId);
|
|
1761
|
+
}
|
|
1762
|
+
const existingMetadata = node.metadata || {};
|
|
1763
|
+
const newMetadataFields = args.metadata !== void 0 ? args.metadata || {} : {};
|
|
1764
|
+
const mergedMetadata = { ...existingMetadata, ...newMetadataFields };
|
|
1765
|
+
if (args.subtype !== void 0) {
|
|
1766
|
+
mergedMetadata.subtype = args.subtype;
|
|
1767
|
+
}
|
|
1768
|
+
if (args.canonicalText !== void 0 && args.canonicalText.trim().length === 0) {
|
|
1769
|
+
throwStructuredMutationError({
|
|
1770
|
+
message: "canonicalText cannot be empty.",
|
|
1771
|
+
status: 400,
|
|
1772
|
+
code: "INVALID_REQUEST",
|
|
1773
|
+
invariantCode: "entity.update_requires_entity_lifecycle"
|
|
1774
|
+
});
|
|
1775
|
+
}
|
|
1776
|
+
const nextCanonicalText = typeof args.canonicalText === "string" && args.canonicalText.trim().length > 0 ? args.canonicalText.trim() : node.canonicalText;
|
|
1777
|
+
if (args.canonicalText !== void 0) {
|
|
1778
|
+
const duplicate = await findExistingCanonicalEntity(ctx, {
|
|
1779
|
+
nodeType: node.nodeType,
|
|
1780
|
+
canonicalText: nextCanonicalText,
|
|
1781
|
+
tenantId: typeof node.tenantId === "string" ? node.tenantId : void 0
|
|
1782
|
+
});
|
|
1783
|
+
if (duplicate && String(duplicate._id) !== String(args.nodeId)) {
|
|
1784
|
+
throwStructuredMutationError({
|
|
1785
|
+
message: "A canonical entity with this name already exists in the tenant scope.",
|
|
1786
|
+
status: 409,
|
|
1787
|
+
code: "CONFLICT",
|
|
1788
|
+
invariantCode: "entity.canonical_text_unique_per_tenant",
|
|
1789
|
+
suggestion: "Merge the duplicate entity instead of renaming this node onto an existing canonical record.",
|
|
1790
|
+
details: {
|
|
1791
|
+
nodeId: args.nodeId,
|
|
1792
|
+
duplicateNodeId: duplicate._id,
|
|
1793
|
+
canonicalText: nextCanonicalText
|
|
1794
|
+
}
|
|
1795
|
+
});
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
const validation = await validateEntityMetadata(
|
|
1799
|
+
ctx,
|
|
1800
|
+
node.nodeType,
|
|
1801
|
+
mergedMetadata,
|
|
1802
|
+
typeof node.tenantId === "string" ? node.tenantId : void 0
|
|
1803
|
+
);
|
|
1804
|
+
if (!validation.valid) {
|
|
1805
|
+
throwStructuredMutationError({
|
|
1806
|
+
message: `Entity metadata validation failed: ${validation.errors.join("; ")}`,
|
|
1807
|
+
status: 400,
|
|
1808
|
+
code: "VALIDATION_ERROR",
|
|
1809
|
+
invariantCode: "entity.metadata_schema",
|
|
1810
|
+
details: { errors: validation.errors }
|
|
1811
|
+
});
|
|
1812
|
+
}
|
|
1813
|
+
const updates = {
|
|
1814
|
+
metadata: mergedMetadata,
|
|
1815
|
+
updatedAt: now
|
|
1816
|
+
};
|
|
1817
|
+
if (args.canonicalText !== void 0) {
|
|
1818
|
+
updates.canonicalText = nextCanonicalText;
|
|
1819
|
+
updates.contentHash = generateContentHash(node.nodeType, nextCanonicalText);
|
|
1820
|
+
updates.title = args.title !== void 0 ? args.title : buildEntityTitle(nextCanonicalText);
|
|
1821
|
+
} else if (args.title !== void 0) {
|
|
1822
|
+
updates.title = args.title;
|
|
1823
|
+
}
|
|
1824
|
+
if (args.subtype !== void 0) {
|
|
1825
|
+
updates.subtype = args.subtype;
|
|
1826
|
+
}
|
|
1827
|
+
if (args.domain !== void 0) {
|
|
1828
|
+
updates.domain = args.domain;
|
|
1829
|
+
}
|
|
1830
|
+
if (args.tags !== void 0) {
|
|
1831
|
+
updates.tags = args.tags;
|
|
1832
|
+
}
|
|
1833
|
+
if (args.verificationStatus !== void 0) {
|
|
1834
|
+
updates.verificationStatus = args.verificationStatus;
|
|
1835
|
+
}
|
|
1836
|
+
if (args.externalIds !== void 0) {
|
|
1837
|
+
updates.externalIds = args.externalIds;
|
|
1838
|
+
}
|
|
1839
|
+
await ctx.db.patch(args.nodeId, updates);
|
|
1840
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
1841
|
+
nodeId: args.nodeId,
|
|
1842
|
+
operation: "upsert"
|
|
1843
|
+
});
|
|
1844
|
+
await ctx.db.insert("epistemicAudit", {
|
|
1845
|
+
entityType: "entity",
|
|
1846
|
+
entityId: args.nodeId,
|
|
1847
|
+
changeType: "entity_attributes_updated",
|
|
1848
|
+
changedAt: now,
|
|
1849
|
+
changedBy: authenticatedUserId,
|
|
1850
|
+
isAgent: false,
|
|
1851
|
+
previousState: {
|
|
1852
|
+
canonicalText: node.canonicalText,
|
|
1853
|
+
title: node.title,
|
|
1854
|
+
subtype: node.subtype,
|
|
1855
|
+
domain: node.domain,
|
|
1856
|
+
tags: node.tags,
|
|
1857
|
+
verificationStatus: node.verificationStatus,
|
|
1858
|
+
externalIds: node.externalIds,
|
|
1859
|
+
metadata: existingMetadata
|
|
1860
|
+
},
|
|
1861
|
+
newState: updates,
|
|
1862
|
+
projectId: scopeProjectId
|
|
1863
|
+
});
|
|
1864
|
+
return { nodeId: args.nodeId, updated: true };
|
|
1865
|
+
}
|
|
1866
|
+
});
|
|
1867
|
+
var mergeEntities = mutation({
|
|
1868
|
+
args: {
|
|
1869
|
+
canonicalNodeId: v.id("epistemicNodes"),
|
|
1870
|
+
duplicateNodeId: v.id("epistemicNodes")
|
|
1871
|
+
},
|
|
1872
|
+
returns: permissiveReturn,
|
|
1873
|
+
handler: async (ctx, args) => {
|
|
1874
|
+
const authenticatedUserId = await requireAuthenticatedUserId(ctx);
|
|
1875
|
+
const now = Date.now();
|
|
1876
|
+
const canonical = await ctx.db.get(args.canonicalNodeId);
|
|
1877
|
+
const duplicate = await ctx.db.get(args.duplicateNodeId);
|
|
1878
|
+
if (!canonical || !duplicate) {
|
|
1879
|
+
throwStructuredMutationError({
|
|
1880
|
+
message: "One or both entity nodes not found.",
|
|
1881
|
+
status: 404,
|
|
1882
|
+
code: "NOT_FOUND",
|
|
1883
|
+
details: {
|
|
1884
|
+
canonicalNodeId: args.canonicalNodeId,
|
|
1885
|
+
duplicateNodeId: args.duplicateNodeId
|
|
1886
|
+
}
|
|
1887
|
+
});
|
|
1888
|
+
}
|
|
1889
|
+
if (!isOntologicalNodeType(canonical.nodeType) || !isOntologicalNodeType(duplicate.nodeType)) {
|
|
1890
|
+
throwStructuredMutationError({
|
|
1891
|
+
message: "Both nodes must be ontological entities to merge.",
|
|
1892
|
+
status: 400,
|
|
1893
|
+
code: "LIFECYCLE_VIOLATION",
|
|
1894
|
+
invariantCode: "entity.type_check"
|
|
1895
|
+
});
|
|
1896
|
+
}
|
|
1897
|
+
if (args.canonicalNodeId === args.duplicateNodeId) {
|
|
1898
|
+
throwStructuredMutationError({
|
|
1899
|
+
message: "Cannot merge an entity with itself.",
|
|
1900
|
+
status: 400,
|
|
1901
|
+
code: "SELF_MERGE",
|
|
1902
|
+
invariantCode: "entity.no_self_merge",
|
|
1903
|
+
suggestion: "Provide two different entity IDs to merge.",
|
|
1904
|
+
details: { canonicalNodeId: args.canonicalNodeId }
|
|
1905
|
+
});
|
|
1906
|
+
}
|
|
1907
|
+
if (canonical.nodeType !== duplicate.nodeType) {
|
|
1908
|
+
throwStructuredMutationError({
|
|
1909
|
+
message: `Cannot merge different entity types: ${canonical.nodeType} vs ${duplicate.nodeType}.`,
|
|
1910
|
+
status: 400,
|
|
1911
|
+
code: "TYPE_MISMATCH"
|
|
1912
|
+
});
|
|
1913
|
+
}
|
|
1914
|
+
if (canonical.projectId) {
|
|
1915
|
+
await requireProjectWriteAccess(
|
|
1916
|
+
ctx,
|
|
1917
|
+
canonical.projectId,
|
|
1918
|
+
authenticatedUserId
|
|
1919
|
+
);
|
|
1920
|
+
}
|
|
1921
|
+
const edgeGlobalId = generateGlobalId();
|
|
1922
|
+
await ctx.db.insert("epistemicEdges", {
|
|
1923
|
+
globalId: edgeGlobalId,
|
|
1924
|
+
fromNodeId: args.duplicateNodeId,
|
|
1925
|
+
toNodeId: args.canonicalNodeId,
|
|
1926
|
+
sourceGlobalId: duplicate.globalId,
|
|
1927
|
+
targetGlobalId: canonical.globalId,
|
|
1928
|
+
edgeType: "derived_from",
|
|
1929
|
+
weight: 1,
|
|
1930
|
+
context: `Entity merge: ${duplicate.canonicalText} \u2192 ${canonical.canonicalText}`,
|
|
1931
|
+
createdBy: authenticatedUserId,
|
|
1932
|
+
createdAt: now,
|
|
1933
|
+
updatedAt: now
|
|
1934
|
+
});
|
|
1935
|
+
const outgoingEdges = await ctx.db.query("epistemicEdges").withIndex(
|
|
1936
|
+
"by_from",
|
|
1937
|
+
(q) => q.eq("fromNodeId", args.duplicateNodeId)
|
|
1938
|
+
).collect();
|
|
1939
|
+
for (const edge of outgoingEdges) {
|
|
1940
|
+
if (edge.toNodeId === args.canonicalNodeId) {
|
|
1941
|
+
continue;
|
|
1942
|
+
}
|
|
1943
|
+
await ctx.db.patch(edge._id, {
|
|
1944
|
+
fromNodeId: args.canonicalNodeId,
|
|
1945
|
+
sourceGlobalId: canonical.globalId,
|
|
1946
|
+
updatedAt: now
|
|
1947
|
+
});
|
|
1948
|
+
}
|
|
1949
|
+
const incomingEdges = await ctx.db.query("epistemicEdges").withIndex("by_to", (q) => q.eq("toNodeId", args.duplicateNodeId)).collect();
|
|
1950
|
+
for (const edge of incomingEdges) {
|
|
1951
|
+
if (edge.fromNodeId === args.canonicalNodeId) {
|
|
1952
|
+
continue;
|
|
1953
|
+
}
|
|
1954
|
+
await ctx.db.patch(edge._id, {
|
|
1955
|
+
toNodeId: args.canonicalNodeId,
|
|
1956
|
+
targetGlobalId: canonical.globalId,
|
|
1957
|
+
updatedAt: now
|
|
1958
|
+
});
|
|
1959
|
+
}
|
|
1960
|
+
const duplicateMetadata = duplicate.metadata || {};
|
|
1961
|
+
await ctx.db.patch(args.duplicateNodeId, {
|
|
1962
|
+
status: "superseded",
|
|
1963
|
+
metadata: {
|
|
1964
|
+
...duplicateMetadata,
|
|
1965
|
+
status: "superseded",
|
|
1966
|
+
supersededBy: args.canonicalNodeId,
|
|
1967
|
+
mergedAt: now,
|
|
1968
|
+
mergedBy: authenticatedUserId
|
|
1969
|
+
},
|
|
1970
|
+
updatedAt: now
|
|
1971
|
+
});
|
|
1972
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
1973
|
+
nodeId: args.canonicalNodeId,
|
|
1974
|
+
operation: "upsert"
|
|
1975
|
+
});
|
|
1976
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
1977
|
+
nodeId: args.duplicateNodeId,
|
|
1978
|
+
operation: "upsert"
|
|
1979
|
+
});
|
|
1980
|
+
await ctx.db.insert("epistemicAudit", {
|
|
1981
|
+
entityType: "entity",
|
|
1982
|
+
entityId: args.canonicalNodeId,
|
|
1983
|
+
changeType: "entity_merged",
|
|
1984
|
+
changedAt: now,
|
|
1985
|
+
changedBy: authenticatedUserId,
|
|
1986
|
+
isAgent: false,
|
|
1987
|
+
rationale: `Entity merge: absorbed ${args.duplicateNodeId} (${duplicate.canonicalText}). ${outgoingEdges.length + incomingEdges.length} edges re-pointed.`,
|
|
1988
|
+
previousState: null,
|
|
1989
|
+
newState: {
|
|
1990
|
+
nodeType: canonical.nodeType,
|
|
1991
|
+
canonicalText: canonical.canonicalText
|
|
1992
|
+
},
|
|
1993
|
+
projectId: canonical.projectId
|
|
1994
|
+
});
|
|
1995
|
+
const outgoingRepointed = outgoingEdges.filter(
|
|
1996
|
+
(e) => e.toNodeId !== args.canonicalNodeId
|
|
1997
|
+
).length;
|
|
1998
|
+
const incomingRepointed = incomingEdges.filter(
|
|
1999
|
+
(e) => e.fromNodeId !== args.canonicalNodeId
|
|
2000
|
+
).length;
|
|
2001
|
+
await ctx.scheduler.runAfter(
|
|
2002
|
+
0,
|
|
2003
|
+
internal.lucernMcp.triggerEntityChangeReview,
|
|
2004
|
+
{
|
|
2005
|
+
entityNodeId: args.canonicalNodeId,
|
|
2006
|
+
changeDescription: `Merged with duplicate "${duplicate.canonicalText}". ${outgoingRepointed + incomingRepointed} edges re-pointed.`,
|
|
2007
|
+
userId: authenticatedUserId
|
|
2008
|
+
}
|
|
2009
|
+
);
|
|
2010
|
+
return {
|
|
2011
|
+
canonicalNodeId: args.canonicalNodeId,
|
|
2012
|
+
duplicateNodeId: args.duplicateNodeId,
|
|
2013
|
+
edgesRepointed: outgoingRepointed + incomingRepointed
|
|
2014
|
+
};
|
|
2015
|
+
}
|
|
2016
|
+
});
|
|
2017
|
+
var archiveEntity = mutation({
|
|
2018
|
+
args: {
|
|
2019
|
+
nodeId: v.id("epistemicNodes")
|
|
2020
|
+
},
|
|
2021
|
+
returns: permissiveReturn,
|
|
2022
|
+
handler: async (ctx, args) => {
|
|
2023
|
+
const authenticatedUserId = await requireAuthenticatedUserId(ctx);
|
|
2024
|
+
const now = Date.now();
|
|
2025
|
+
const node = await ctx.db.get(args.nodeId);
|
|
2026
|
+
if (!node) {
|
|
2027
|
+
throwStructuredMutationError({
|
|
2028
|
+
message: "Entity not found.",
|
|
2029
|
+
status: 404,
|
|
2030
|
+
code: "NOT_FOUND",
|
|
2031
|
+
details: { nodeId: args.nodeId }
|
|
2032
|
+
});
|
|
2033
|
+
}
|
|
2034
|
+
if (!isOntologicalNodeType(node.nodeType)) {
|
|
2035
|
+
throwStructuredMutationError({
|
|
2036
|
+
message: `Node "${args.nodeId}" is a ${node.nodeType}, not an entity. Use belief archive APIs for epistemic nodes.`,
|
|
2037
|
+
status: 400,
|
|
2038
|
+
code: "LIFECYCLE_VIOLATION",
|
|
2039
|
+
invariantCode: "entity.type_check"
|
|
2040
|
+
});
|
|
2041
|
+
}
|
|
2042
|
+
if (node.projectId) {
|
|
2043
|
+
await requireProjectWriteAccess(
|
|
2044
|
+
ctx,
|
|
2045
|
+
node.projectId,
|
|
2046
|
+
authenticatedUserId
|
|
2047
|
+
);
|
|
2048
|
+
}
|
|
2049
|
+
await ctx.db.patch(args.nodeId, {
|
|
2050
|
+
status: "archived",
|
|
2051
|
+
updatedAt: now
|
|
2052
|
+
});
|
|
2053
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
2054
|
+
nodeId: args.nodeId,
|
|
2055
|
+
operation: "upsert"
|
|
2056
|
+
});
|
|
2057
|
+
await ctx.db.insert("epistemicAudit", {
|
|
2058
|
+
entityType: "entity",
|
|
2059
|
+
entityId: args.nodeId,
|
|
2060
|
+
changeType: "entity_archived",
|
|
2061
|
+
changedAt: now,
|
|
2062
|
+
changedBy: authenticatedUserId,
|
|
2063
|
+
isAgent: false,
|
|
2064
|
+
previousState: { status: node.status },
|
|
2065
|
+
newState: { status: "archived" },
|
|
2066
|
+
projectId: node.projectId
|
|
2067
|
+
});
|
|
2068
|
+
await ctx.scheduler.runAfter(
|
|
2069
|
+
0,
|
|
2070
|
+
internal.lucernMcp.triggerEntityChangeReview,
|
|
2071
|
+
{
|
|
2072
|
+
entityNodeId: args.nodeId,
|
|
2073
|
+
changeDescription: `Entity "${node.canonicalText}" was archived.`,
|
|
2074
|
+
userId: authenticatedUserId
|
|
2075
|
+
}
|
|
2076
|
+
);
|
|
2077
|
+
return { nodeId: args.nodeId, archived: true };
|
|
2078
|
+
}
|
|
2079
|
+
});
|
|
2080
|
+
|
|
2081
|
+
export { ONTOLOGICAL_NODE_TYPES, archiveEntity, createEntity, isOntologicalNodeType, listCanonicalEntities, mergeEntities, updateEntityAttributes };
|
|
2082
|
+
//# sourceMappingURL=entityLifecycle.js.map
|
|
2083
|
+
//# sourceMappingURL=entityLifecycle.js.map
|