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