@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,1650 @@
|
|
|
1
|
+
import { v } from 'convex/values';
|
|
2
|
+
import { componentsGeneric, mutationGeneric, anyApi, internalMutationGeneric, queryGeneric } from 'convex/server';
|
|
3
|
+
|
|
4
|
+
// src/epistemicAnswers.ts
|
|
5
|
+
var api = anyApi;
|
|
6
|
+
componentsGeneric();
|
|
7
|
+
var internal = anyApi;
|
|
8
|
+
var internalMutation = internalMutationGeneric;
|
|
9
|
+
var mutation = mutationGeneric;
|
|
10
|
+
var query = queryGeneric;
|
|
11
|
+
var api2 = anyApi;
|
|
12
|
+
componentsGeneric();
|
|
13
|
+
|
|
14
|
+
// ../access-control/src/topicProjectOverlay.ts
|
|
15
|
+
var LEGACY_SCOPE_FIELD = "graphScopeProjectId";
|
|
16
|
+
function readNonEmptyString(value) {
|
|
17
|
+
if (typeof value !== "string") {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const normalized = value.trim();
|
|
21
|
+
return normalized.length > 0 ? normalized : void 0;
|
|
22
|
+
}
|
|
23
|
+
function readStringArray(value) {
|
|
24
|
+
if (!Array.isArray(value)) {
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
return value.map((entry) => readNonEmptyString(entry)).filter((entry) => Boolean(entry));
|
|
28
|
+
}
|
|
29
|
+
function readMetadata(topic) {
|
|
30
|
+
return topic.metadata && typeof topic.metadata === "object" ? topic.metadata : {};
|
|
31
|
+
}
|
|
32
|
+
function readLegacyProjectId(value) {
|
|
33
|
+
if (!value) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
return readNonEmptyString(value[LEGACY_SCOPE_FIELD]);
|
|
37
|
+
}
|
|
38
|
+
function coerceVisibility(value) {
|
|
39
|
+
return value === "private" || value === "team" || value === "firm" || value === "external" || value === "public" ? value : void 0;
|
|
40
|
+
}
|
|
41
|
+
function coerceStatus(value) {
|
|
42
|
+
return value === "active" || value === "archived" || value === "watching" ? value : void 0;
|
|
43
|
+
}
|
|
44
|
+
function mapProjectType(topic, metadata) {
|
|
45
|
+
const explicit = readNonEmptyString(metadata.projectType);
|
|
46
|
+
if (explicit) {
|
|
47
|
+
return explicit;
|
|
48
|
+
}
|
|
49
|
+
if (topic.type === "theme") {
|
|
50
|
+
return "thematic";
|
|
51
|
+
}
|
|
52
|
+
return readNonEmptyString(topic.type) || "general";
|
|
53
|
+
}
|
|
54
|
+
function isProjectLikeTopic(topic) {
|
|
55
|
+
const metadata = readMetadata(topic);
|
|
56
|
+
return topic.type === "theme" || topic.type === "thematic" || topic.type === "deal" || topic.type === "monitoring" || readLegacyProjectId(topic) !== void 0 || readNonEmptyString(metadata.projectType) !== void 0;
|
|
57
|
+
}
|
|
58
|
+
async function resolveTopicDoc(ctx, scopeId) {
|
|
59
|
+
if (ctx?.db && typeof ctx.db.get === "function") {
|
|
60
|
+
try {
|
|
61
|
+
const directTopic = await ctx.db.get(scopeId);
|
|
62
|
+
if (directTopic) {
|
|
63
|
+
return directTopic;
|
|
64
|
+
}
|
|
65
|
+
} catch {
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (typeof ctx.runQuery !== "function") {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
const topic = await ctx.runQuery(api2.topics.get, {
|
|
73
|
+
id: String(scopeId)
|
|
74
|
+
});
|
|
75
|
+
if (topic?.name !== void 0 && topic?.type !== void 0) {
|
|
76
|
+
return topic;
|
|
77
|
+
}
|
|
78
|
+
} catch {
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
const topic = await ctx.runQuery(api2.topics.getByLegacyScopeId, {
|
|
82
|
+
projectId: String(scopeId)
|
|
83
|
+
});
|
|
84
|
+
if (topic?.name !== void 0 && topic?.type !== void 0) {
|
|
85
|
+
return topic;
|
|
86
|
+
}
|
|
87
|
+
} catch {
|
|
88
|
+
}
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
function materializeTopicProjectOverlay(topic, idMode = "legacy") {
|
|
92
|
+
const metadata = readMetadata(topic);
|
|
93
|
+
const topicId = String(topic._id);
|
|
94
|
+
const legacyProjectId = readLegacyProjectId(topic) || readLegacyProjectId(metadata) || readNonEmptyString(metadata.legacyProjectId);
|
|
95
|
+
const storageProjectId = legacyProjectId || topicId;
|
|
96
|
+
const outwardId = idMode === "topic" ? topicId : storageProjectId;
|
|
97
|
+
const visibility = coerceVisibility(topic.visibility) || coerceVisibility(metadata.visibility) || "private";
|
|
98
|
+
const status = coerceStatus(topic.status) || coerceStatus(metadata.status) || "active";
|
|
99
|
+
const createdAt = typeof topic.createdAt === "number" ? topic.createdAt : typeof topic._creationTime === "number" ? topic._creationTime : 0;
|
|
100
|
+
const updatedAt = typeof topic.updatedAt === "number" ? topic.updatedAt : typeof metadata.updatedAt === "number" ? metadata.updatedAt : createdAt;
|
|
101
|
+
return {
|
|
102
|
+
...metadata,
|
|
103
|
+
_id: outwardId,
|
|
104
|
+
projectId: outwardId,
|
|
105
|
+
topicId,
|
|
106
|
+
storageProjectId,
|
|
107
|
+
legacyProjectId,
|
|
108
|
+
name: readNonEmptyString(topic.name) || "Untitled Theme",
|
|
109
|
+
type: mapProjectType(topic, metadata),
|
|
110
|
+
description: readNonEmptyString(topic.description),
|
|
111
|
+
ownerId: readNonEmptyString(metadata.ownerId) || readNonEmptyString(topic.createdBy) || "system",
|
|
112
|
+
sharedWith: readStringArray(metadata.sharedWith),
|
|
113
|
+
visibility,
|
|
114
|
+
tenantId: readNonEmptyString(topic.tenantId) || readNonEmptyString(metadata.tenantId),
|
|
115
|
+
workspaceId: readNonEmptyString(topic.workspaceId) || readNonEmptyString(metadata.workspaceId),
|
|
116
|
+
status,
|
|
117
|
+
tags: readStringArray(metadata.tags),
|
|
118
|
+
chatCount: typeof metadata.chatCount === "number" ? metadata.chatCount : 0,
|
|
119
|
+
artifactCount: typeof metadata.artifactCount === "number" ? metadata.artifactCount : 0,
|
|
120
|
+
lastActivityAt: typeof metadata.lastActivityAt === "number" ? metadata.lastActivityAt : updatedAt,
|
|
121
|
+
_creationTime: typeof topic._creationTime === "number" ? topic._creationTime : createdAt,
|
|
122
|
+
createdAt,
|
|
123
|
+
updatedAt
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
async function resolveTopicProjectOverlay(ctx, scopeId, options = {}) {
|
|
127
|
+
const topic = await resolveTopicDoc(ctx, scopeId);
|
|
128
|
+
if (!topic) {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
if (options.projectLikeOnly !== false && !isProjectLikeTopic(topic)) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
return materializeTopicProjectOverlay(topic, options.idMode);
|
|
135
|
+
}
|
|
136
|
+
async function listTopicProjectOverlays(ctx, options = {}) {
|
|
137
|
+
let allTopics = [];
|
|
138
|
+
if (ctx?.db?.query && typeof ctx.db.query === "function") {
|
|
139
|
+
try {
|
|
140
|
+
allTopics = await ctx.db.query("topics").collect();
|
|
141
|
+
} catch {
|
|
142
|
+
allTopics = [];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (allTopics.length === 0 && typeof ctx.runQuery === "function") {
|
|
146
|
+
allTopics = (await ctx.runQuery(api2.topics.list, {}) ?? []) || [];
|
|
147
|
+
}
|
|
148
|
+
return allTopics.filter(
|
|
149
|
+
(topic) => options.projectLikeOnly === false || isProjectLikeTopic(topic)
|
|
150
|
+
).map((topic) => materializeTopicProjectOverlay(topic, options.idMode));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// ../access-control/src/projectGrantsBridge.ts
|
|
154
|
+
var PROJECT_GRANT_STATUSES = ["active", "revoked", "expired"];
|
|
155
|
+
function normalizeString(value) {
|
|
156
|
+
if (typeof value !== "string") {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const trimmed = value.trim();
|
|
160
|
+
return trimmed.length > 0 ? trimmed : void 0;
|
|
161
|
+
}
|
|
162
|
+
async function resolveGrantScopeIds(ctx, args) {
|
|
163
|
+
const topicId = normalizeString(args.topicId);
|
|
164
|
+
const projectId = normalizeString(args.projectId);
|
|
165
|
+
for (const scopeId of [topicId, projectId]) {
|
|
166
|
+
if (!scopeId) {
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
try {
|
|
170
|
+
const overlay = await resolveTopicProjectOverlay(ctx, scopeId, {
|
|
171
|
+
idMode: "legacy",
|
|
172
|
+
projectLikeOnly: false
|
|
173
|
+
});
|
|
174
|
+
if (overlay) {
|
|
175
|
+
return {
|
|
176
|
+
topicId: normalizeString(overlay.topicId) ?? topicId,
|
|
177
|
+
projectId: normalizeString(overlay.projectId) ?? projectId ?? scopeId
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
} catch {
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return { topicId, projectId };
|
|
184
|
+
}
|
|
185
|
+
async function normalizeProjectGrantRow(ctx, row) {
|
|
186
|
+
const scope = await resolveGrantScopeIds(ctx, {
|
|
187
|
+
topicId: row.topicId,
|
|
188
|
+
projectId: row.projectId
|
|
189
|
+
});
|
|
190
|
+
return {
|
|
191
|
+
...row,
|
|
192
|
+
...scope.topicId ? { topicId: scope.topicId } : {},
|
|
193
|
+
...scope.projectId ?? scope.topicId ? { projectId: scope.projectId ?? scope.topicId } : {}
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
async function normalizeProjectGrantRows(ctx, rows) {
|
|
197
|
+
return await Promise.all(rows.map((row) => normalizeProjectGrantRow(ctx, row)));
|
|
198
|
+
}
|
|
199
|
+
async function listProjectGrantsByPrincipal(ctx, principalId) {
|
|
200
|
+
const rows = await Promise.all(
|
|
201
|
+
PROJECT_GRANT_STATUSES.map(
|
|
202
|
+
(status) => ctx.db.query("projectGrants").withIndex(
|
|
203
|
+
"by_principal_status",
|
|
204
|
+
(q) => q.eq("principalId", principalId).eq("status", status)
|
|
205
|
+
).collect()
|
|
206
|
+
)
|
|
207
|
+
);
|
|
208
|
+
return await normalizeProjectGrantRows(ctx, rows.flat());
|
|
209
|
+
}
|
|
210
|
+
async function listProjectGrantsByGroup(ctx, groupId) {
|
|
211
|
+
const rows = await Promise.all(
|
|
212
|
+
PROJECT_GRANT_STATUSES.map(
|
|
213
|
+
(status) => ctx.db.query("projectGrants").withIndex(
|
|
214
|
+
"by_group_status",
|
|
215
|
+
(q) => q.eq("groupId", groupId).eq("status", status)
|
|
216
|
+
).collect()
|
|
217
|
+
)
|
|
218
|
+
);
|
|
219
|
+
return await normalizeProjectGrantRows(ctx, rows.flat());
|
|
220
|
+
}
|
|
221
|
+
function buildScopeMatchers(inputScopeId, resolved) {
|
|
222
|
+
return new Set(
|
|
223
|
+
[inputScopeId, resolved.topicId, resolved.projectId].map((value) => normalizeString(value)).filter((value) => Boolean(value))
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
function matchesResolvedScope(row, scopeIds) {
|
|
227
|
+
const rowTopicId = normalizeString(row.topicId);
|
|
228
|
+
const rowProjectId = normalizeString(row.projectId);
|
|
229
|
+
return rowTopicId !== void 0 && scopeIds.has(rowTopicId) || rowProjectId !== void 0 && scopeIds.has(rowProjectId);
|
|
230
|
+
}
|
|
231
|
+
async function bridgeListProjectGrantsByTopicAndPrincipal(ctx, topicId, principalId) {
|
|
232
|
+
const resolved = await resolveGrantScopeIds(ctx, { topicId });
|
|
233
|
+
const scopeIds = buildScopeMatchers(topicId, resolved);
|
|
234
|
+
const rows = await listProjectGrantsByPrincipal(ctx, principalId);
|
|
235
|
+
return rows.filter((row) => matchesResolvedScope(row, scopeIds));
|
|
236
|
+
}
|
|
237
|
+
async function bridgeListProjectGrantsByTopicAndGroup(ctx, topicId, groupId) {
|
|
238
|
+
const resolved = await resolveGrantScopeIds(ctx, { topicId });
|
|
239
|
+
const scopeIds = buildScopeMatchers(topicId, resolved);
|
|
240
|
+
const rows = await listProjectGrantsByGroup(ctx, groupId);
|
|
241
|
+
return rows.filter((row) => matchesResolvedScope(row, scopeIds));
|
|
242
|
+
}
|
|
243
|
+
async function bridgeListProjectGrantsByPrincipalStatus(ctx, principalId, status) {
|
|
244
|
+
const rows = await listProjectGrantsByPrincipal(ctx, principalId);
|
|
245
|
+
return rows.filter((row) => row.status === status);
|
|
246
|
+
}
|
|
247
|
+
async function bridgeListProjectGrantsByGroupStatus(ctx, groupId, status) {
|
|
248
|
+
const rows = await listProjectGrantsByGroup(ctx, groupId);
|
|
249
|
+
return rows.filter((row) => row.status === status);
|
|
250
|
+
}
|
|
251
|
+
async function bridgeInsertProjectGrant(ctx, value) {
|
|
252
|
+
const resolved = await resolveGrantScopeIds(ctx, value);
|
|
253
|
+
return await ctx.db.insert("projectGrants", {
|
|
254
|
+
...value,
|
|
255
|
+
...resolved.topicId ? { topicId: resolved.topicId } : {},
|
|
256
|
+
...resolved.projectId ?? resolved.topicId ? { projectId: resolved.projectId ?? resolved.topicId } : {}
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// ../access-control/src/resolvers.ts
|
|
261
|
+
async function findUserByClerkId(ctx, clerkId) {
|
|
262
|
+
const normalizedClerkId = clerkId.trim();
|
|
263
|
+
if (!normalizedClerkId) {
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
if (typeof ctx.runQuery === "function") {
|
|
267
|
+
try {
|
|
268
|
+
const bridgedUser = await ctx.runQuery(api2.users.getUserByClerkId, {
|
|
269
|
+
clerkId: normalizedClerkId
|
|
270
|
+
});
|
|
271
|
+
if (bridgedUser) {
|
|
272
|
+
return bridgedUser;
|
|
273
|
+
}
|
|
274
|
+
} catch {
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
try {
|
|
278
|
+
const users = await ctx.db.query("users").collect();
|
|
279
|
+
return users.find((user) => String(user.clerkId ?? "") === normalizedClerkId) ?? null;
|
|
280
|
+
} catch {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
async function findUserByPrincipalId(ctx, principalId) {
|
|
285
|
+
const normalizedPrincipalId = principalId.trim();
|
|
286
|
+
if (!normalizedPrincipalId) {
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
try {
|
|
290
|
+
const users = await ctx.db.query("users").collect();
|
|
291
|
+
return users.find(
|
|
292
|
+
(user) => String(user.defaultPrincipalId ?? "") === normalizedPrincipalId
|
|
293
|
+
) ?? null;
|
|
294
|
+
} catch {
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
async function findAgentByPrincipalId(ctx, principalId) {
|
|
299
|
+
const normalizedPrincipalId = principalId.trim();
|
|
300
|
+
if (!normalizedPrincipalId) {
|
|
301
|
+
return null;
|
|
302
|
+
}
|
|
303
|
+
if (typeof ctx.runQuery === "function") {
|
|
304
|
+
try {
|
|
305
|
+
const bridgedAgent = await ctx.runQuery(
|
|
306
|
+
api2.agents.getAgentByPrincipalId,
|
|
307
|
+
{
|
|
308
|
+
principalId: normalizedPrincipalId
|
|
309
|
+
}
|
|
310
|
+
);
|
|
311
|
+
if (bridgedAgent) {
|
|
312
|
+
return bridgedAgent;
|
|
313
|
+
}
|
|
314
|
+
} catch {
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
try {
|
|
318
|
+
const agents = await ctx.db.query("agents").collect();
|
|
319
|
+
return agents.find(
|
|
320
|
+
(agent) => String(agent.principalId ?? "") === normalizedPrincipalId
|
|
321
|
+
) ?? null;
|
|
322
|
+
} catch {
|
|
323
|
+
return null;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
function defaultResolvers() {
|
|
327
|
+
return {
|
|
328
|
+
async getProject(ctx, topicId) {
|
|
329
|
+
return await resolveTopicProjectOverlay(ctx, topicId, {
|
|
330
|
+
idMode: "legacy",
|
|
331
|
+
projectLikeOnly: false
|
|
332
|
+
});
|
|
333
|
+
},
|
|
334
|
+
async listTopics(ctx) {
|
|
335
|
+
return await listTopicProjectOverlays(ctx, { idMode: "legacy" });
|
|
336
|
+
},
|
|
337
|
+
async listTopicsByOwner(ctx, ownerId) {
|
|
338
|
+
const topics = await listTopicProjectOverlays(ctx, { idMode: "legacy" });
|
|
339
|
+
return topics.filter((topic) => topic.ownerId === ownerId);
|
|
340
|
+
},
|
|
341
|
+
async listTopicsByVisibility(ctx, visibility) {
|
|
342
|
+
const topics = await listTopicProjectOverlays(ctx, { idMode: "legacy" });
|
|
343
|
+
return topics.filter((topic) => topic.visibility === visibility);
|
|
344
|
+
},
|
|
345
|
+
async listProjectGrantsByProjectAndPrincipal(ctx, topicId, principalId) {
|
|
346
|
+
return await bridgeListProjectGrantsByTopicAndPrincipal(
|
|
347
|
+
ctx,
|
|
348
|
+
topicId,
|
|
349
|
+
principalId
|
|
350
|
+
);
|
|
351
|
+
},
|
|
352
|
+
async listProjectGrantsByProjectAndGroup(ctx, topicId, groupId) {
|
|
353
|
+
return await bridgeListProjectGrantsByTopicAndGroup(ctx, topicId, groupId);
|
|
354
|
+
},
|
|
355
|
+
async listProjectGrantsByPrincipalStatus(ctx, principalId, status) {
|
|
356
|
+
return await bridgeListProjectGrantsByPrincipalStatus(
|
|
357
|
+
ctx,
|
|
358
|
+
principalId,
|
|
359
|
+
status
|
|
360
|
+
);
|
|
361
|
+
},
|
|
362
|
+
async listProjectGrantsByGroupStatus(ctx, groupId, status) {
|
|
363
|
+
return await bridgeListProjectGrantsByGroupStatus(ctx, groupId, status);
|
|
364
|
+
},
|
|
365
|
+
async insertProjectGrant(ctx, value) {
|
|
366
|
+
return await bridgeInsertProjectGrant(ctx, value);
|
|
367
|
+
},
|
|
368
|
+
async getAgentByPrincipalId(ctx, principalId) {
|
|
369
|
+
return await findAgentByPrincipalId(ctx, principalId);
|
|
370
|
+
},
|
|
371
|
+
async getUserByClerkId(ctx, clerkId) {
|
|
372
|
+
return await findUserByClerkId(ctx, clerkId);
|
|
373
|
+
},
|
|
374
|
+
async getUserByPrincipalId(ctx, principalId) {
|
|
375
|
+
return await findUserByPrincipalId(ctx, principalId);
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
var resolverOverrides = {};
|
|
380
|
+
function resolveAccessControlAppResolvers(_ctx) {
|
|
381
|
+
return {
|
|
382
|
+
...defaultResolvers(),
|
|
383
|
+
...resolverOverrides
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// ../access-control/src/principalContext.ts
|
|
388
|
+
function requireCanonicalResolvedUser(user, clerkId) {
|
|
389
|
+
const resolved = user;
|
|
390
|
+
if (!resolved) {
|
|
391
|
+
throw new Error(
|
|
392
|
+
`[AccessControl] Canonical user identity required for ${clerkId}. Sync users.upsertUser before user-bound access checks.`
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
const { mcRole, defaultTenantId, defaultWorkspaceId, defaultPrincipalId } = resolved;
|
|
396
|
+
if (mcRole !== "platform_admin" && mcRole !== "tenant_admin" && mcRole !== "workspace_admin" && mcRole !== "editor" && mcRole !== "viewer" && mcRole !== "auditor" && mcRole !== "service_agent") {
|
|
397
|
+
throw new Error(
|
|
398
|
+
`[AccessControl] Canonical MC role required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
if (typeof defaultTenantId !== "string" || defaultTenantId.trim().length === 0) {
|
|
402
|
+
throw new Error(
|
|
403
|
+
`[AccessControl] Canonical home tenant required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
if (typeof defaultWorkspaceId !== "string" || defaultWorkspaceId.trim().length === 0) {
|
|
407
|
+
throw new Error(
|
|
408
|
+
`[AccessControl] Canonical home workspace required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
if (typeof defaultPrincipalId !== "string" || defaultPrincipalId.trim().length === 0) {
|
|
412
|
+
throw new Error(
|
|
413
|
+
`[AccessControl] Canonical federated principal required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
|
|
414
|
+
);
|
|
415
|
+
}
|
|
416
|
+
return {
|
|
417
|
+
mcRole,
|
|
418
|
+
defaultTenantId: defaultTenantId.trim(),
|
|
419
|
+
defaultWorkspaceId: defaultWorkspaceId.trim(),
|
|
420
|
+
defaultPrincipalId: defaultPrincipalId.trim()
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
function isPrincipalIdInput(value) {
|
|
424
|
+
return value.startsWith("user:") || value.startsWith("group:") || value.startsWith("service:") || value.startsWith("agent:") || value.startsWith("external_viewer:");
|
|
425
|
+
}
|
|
426
|
+
async function resolveCanonicalUserRecord(ctx, actorId) {
|
|
427
|
+
const normalizedActorId = actorId.trim();
|
|
428
|
+
const clerkId = isPrincipalIdInput(normalizedActorId) && normalizedActorId.startsWith("user:") ? normalizedActorId.slice("user:".length) : normalizedActorId;
|
|
429
|
+
const resolvers = resolveAccessControlAppResolvers();
|
|
430
|
+
const resolvedByClerkId = await resolvers.getUserByClerkId(ctx, clerkId);
|
|
431
|
+
if (resolvedByClerkId) {
|
|
432
|
+
return {
|
|
433
|
+
resolvedUser: resolvedByClerkId,
|
|
434
|
+
clerkId,
|
|
435
|
+
contextClerkId: clerkId
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
const resolvedByPrincipalId = await resolvers.getUserByPrincipalId(
|
|
439
|
+
ctx,
|
|
440
|
+
normalizedActorId
|
|
441
|
+
);
|
|
442
|
+
return {
|
|
443
|
+
resolvedUser: resolvedByPrincipalId ?? null,
|
|
444
|
+
clerkId,
|
|
445
|
+
contextClerkId: normalizedActorId.startsWith("user:") && clerkId.length > 0 ? clerkId : normalizedActorId
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
function uniqRoles(roles) {
|
|
449
|
+
const roleSet = /* @__PURE__ */ new Set();
|
|
450
|
+
for (const role of roles) {
|
|
451
|
+
if (role === "platform_admin" || role === "tenant_admin" || role === "workspace_admin" || role === "editor" || role === "viewer" || role === "auditor" || role === "service_agent") {
|
|
452
|
+
roleSet.add(role);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
return [...roleSet];
|
|
456
|
+
}
|
|
457
|
+
function normalizeGroupIds(value) {
|
|
458
|
+
if (!Array.isArray(value)) {
|
|
459
|
+
return [];
|
|
460
|
+
}
|
|
461
|
+
return [...new Set(
|
|
462
|
+
value.filter((entry) => typeof entry === "string").map((entry) => entry.trim()).filter(Boolean)
|
|
463
|
+
)];
|
|
464
|
+
}
|
|
465
|
+
function requireServiceAgentUser(user, actorId) {
|
|
466
|
+
const canonicalUser = requireCanonicalResolvedUser(user, actorId);
|
|
467
|
+
if (canonicalUser.mcRole !== "service_agent") {
|
|
468
|
+
throw new Error(
|
|
469
|
+
`[AccessControl] Canonical service_agent identity required for ${actorId}. Sync users.upsertUser before agent-bound access checks.`
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
return canonicalUser;
|
|
473
|
+
}
|
|
474
|
+
function requireCanonicalResolvedAgent(agent, actorId) {
|
|
475
|
+
const resolved = agent;
|
|
476
|
+
if (!resolved) {
|
|
477
|
+
throw new Error(
|
|
478
|
+
`[AccessControl] Agent "${actorId}" not found in agents or users table.`
|
|
479
|
+
);
|
|
480
|
+
}
|
|
481
|
+
if (typeof resolved.principalId !== "string" || resolved.principalId.trim().length === 0) {
|
|
482
|
+
throw new Error(
|
|
483
|
+
`[AccessControl] Canonical agent principalId required for ${actorId}.`
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
if (typeof resolved.tenantId !== "string" || resolved.tenantId.trim().length === 0) {
|
|
487
|
+
throw new Error(
|
|
488
|
+
`[AccessControl] Canonical home tenant required for ${actorId}.`
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
if (typeof resolved.workspaceId !== "string" || resolved.workspaceId.trim().length === 0) {
|
|
492
|
+
throw new Error(
|
|
493
|
+
`[AccessControl] Canonical home workspace required for ${actorId}.`
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
return {
|
|
497
|
+
principalId: resolved.principalId.trim(),
|
|
498
|
+
tenantId: resolved.tenantId.trim(),
|
|
499
|
+
workspaceId: resolved.workspaceId.trim(),
|
|
500
|
+
roles: uniqRoles(Array.isArray(resolved.roles) ? resolved.roles : []) ?? ["service_agent"],
|
|
501
|
+
groupIds: normalizeGroupIds(resolved.groupIds)
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
async function resolvePrincipalContext(ctx, actorId) {
|
|
505
|
+
if (actorId.startsWith("agent:")) {
|
|
506
|
+
const resolvers = resolveAccessControlAppResolvers();
|
|
507
|
+
const resolvedAgent = await resolvers.getAgentByPrincipalId(ctx, actorId);
|
|
508
|
+
if (resolvedAgent) {
|
|
509
|
+
const agent = requireCanonicalResolvedAgent(
|
|
510
|
+
resolvedAgent,
|
|
511
|
+
actorId
|
|
512
|
+
);
|
|
513
|
+
return {
|
|
514
|
+
principalId: agent.principalId,
|
|
515
|
+
principalType: "service",
|
|
516
|
+
clerkId: actorId,
|
|
517
|
+
tenantId: agent.tenantId,
|
|
518
|
+
workspaceId: agent.workspaceId,
|
|
519
|
+
roles: agent.roles.length > 0 ? agent.roles : ["service_agent"],
|
|
520
|
+
groupIds: agent.groupIds,
|
|
521
|
+
isPlatformAdmin: false,
|
|
522
|
+
isTenantAdmin: false,
|
|
523
|
+
isWorkspaceAdmin: false,
|
|
524
|
+
isSystemFallback: false
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
const resolvedUser2 = await resolvers.getUserByClerkId(
|
|
528
|
+
ctx,
|
|
529
|
+
actorId
|
|
530
|
+
);
|
|
531
|
+
if (!resolvedUser2) {
|
|
532
|
+
throw new Error(
|
|
533
|
+
`[AccessControl] Agent "${actorId}" not found in agents or users table.`
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
const user2 = requireServiceAgentUser(
|
|
537
|
+
resolvedUser2,
|
|
538
|
+
actorId
|
|
539
|
+
);
|
|
540
|
+
console.warn(
|
|
541
|
+
`[AccessControl] Deprecated legacy service-agent fallback for ${actorId}; migrate this principal into identity.agents.`
|
|
542
|
+
);
|
|
543
|
+
return {
|
|
544
|
+
principalId: user2.defaultPrincipalId,
|
|
545
|
+
principalType: "service",
|
|
546
|
+
clerkId: actorId,
|
|
547
|
+
tenantId: user2.defaultTenantId,
|
|
548
|
+
workspaceId: user2.defaultWorkspaceId,
|
|
549
|
+
roles: ["service_agent"],
|
|
550
|
+
groupIds: normalizeGroupIds(resolvedUser2?.principalGroupIds),
|
|
551
|
+
isPlatformAdmin: false,
|
|
552
|
+
isTenantAdmin: false,
|
|
553
|
+
isWorkspaceAdmin: false,
|
|
554
|
+
isSystemFallback: false
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
const {
|
|
558
|
+
resolvedUser,
|
|
559
|
+
contextClerkId
|
|
560
|
+
} = await resolveCanonicalUserRecord(ctx, actorId);
|
|
561
|
+
const user = requireCanonicalResolvedUser(
|
|
562
|
+
resolvedUser,
|
|
563
|
+
contextClerkId
|
|
564
|
+
);
|
|
565
|
+
if (!user.defaultPrincipalId) {
|
|
566
|
+
throw new Error(
|
|
567
|
+
`[AccessControl] Canonical federated principal required for ${contextClerkId}. Re-sync Master Control identity before user-bound access checks.`
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
if (user.mcRole === "service_agent") {
|
|
571
|
+
return {
|
|
572
|
+
principalId: user.defaultPrincipalId,
|
|
573
|
+
principalType: "service",
|
|
574
|
+
clerkId: contextClerkId,
|
|
575
|
+
tenantId: user.defaultTenantId,
|
|
576
|
+
workspaceId: user.defaultWorkspaceId,
|
|
577
|
+
roles: ["service_agent"],
|
|
578
|
+
groupIds: normalizeGroupIds(resolvedUser?.principalGroupIds),
|
|
579
|
+
isPlatformAdmin: false,
|
|
580
|
+
isTenantAdmin: false,
|
|
581
|
+
isWorkspaceAdmin: false,
|
|
582
|
+
isSystemFallback: false
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
const principalId = user.defaultPrincipalId;
|
|
586
|
+
const effectiveRole = user.mcRole;
|
|
587
|
+
const roles = effectiveRole === "platform_admin" ? ["platform_admin", "tenant_admin"] : effectiveRole === "tenant_admin" ? ["tenant_admin"] : [effectiveRole];
|
|
588
|
+
const tenantId = user.defaultTenantId;
|
|
589
|
+
const workspaceId = user.defaultWorkspaceId;
|
|
590
|
+
const isPlatformAdmin = effectiveRole === "platform_admin";
|
|
591
|
+
return {
|
|
592
|
+
principalId,
|
|
593
|
+
principalType: "user",
|
|
594
|
+
clerkId: contextClerkId,
|
|
595
|
+
tenantId,
|
|
596
|
+
workspaceId,
|
|
597
|
+
roles: uniqRoles(roles),
|
|
598
|
+
groupIds: normalizeGroupIds(resolvedUser?.principalGroupIds),
|
|
599
|
+
isPlatformAdmin,
|
|
600
|
+
isTenantAdmin: isPlatformAdmin || effectiveRole === "tenant_admin",
|
|
601
|
+
isWorkspaceAdmin: isPlatformAdmin || effectiveRole === "tenant_admin" || effectiveRole === "workspace_admin",
|
|
602
|
+
isSystemFallback: false
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
// ../access-control/src/access.ts
|
|
607
|
+
function isTopicInPrincipalTenant(topic, principalTenantId) {
|
|
608
|
+
if (!topic.tenantId) {
|
|
609
|
+
return false;
|
|
610
|
+
}
|
|
611
|
+
if (!principalTenantId) {
|
|
612
|
+
return false;
|
|
613
|
+
}
|
|
614
|
+
return String(topic.tenantId) === String(principalTenantId);
|
|
615
|
+
}
|
|
616
|
+
function isTopicInPrincipalWorkspace(topic, principalWorkspaceId) {
|
|
617
|
+
if (!topic.workspaceId) {
|
|
618
|
+
return false;
|
|
619
|
+
}
|
|
620
|
+
if (!principalWorkspaceId) {
|
|
621
|
+
return false;
|
|
622
|
+
}
|
|
623
|
+
return String(topic.workspaceId) === String(principalWorkspaceId);
|
|
624
|
+
}
|
|
625
|
+
function isLegacyUnscopedTopic(topic) {
|
|
626
|
+
return !topic.tenantId || !topic.workspaceId;
|
|
627
|
+
}
|
|
628
|
+
function isGrantScopeAlignedToTopic(topic, grant) {
|
|
629
|
+
if (topic.tenantId && grant.tenantId && String(topic.tenantId) !== String(grant.tenantId)) {
|
|
630
|
+
return false;
|
|
631
|
+
}
|
|
632
|
+
if (topic.workspaceId && grant.workspaceId && String(topic.workspaceId) !== String(grant.workspaceId)) {
|
|
633
|
+
return false;
|
|
634
|
+
}
|
|
635
|
+
return true;
|
|
636
|
+
}
|
|
637
|
+
function isGrantSourceAllowedForVisibility(visibility, source) {
|
|
638
|
+
if (source !== "external_share") {
|
|
639
|
+
return true;
|
|
640
|
+
}
|
|
641
|
+
return visibility === "external" || visibility === "public";
|
|
642
|
+
}
|
|
643
|
+
function isGrantActive(grant) {
|
|
644
|
+
if (grant.status !== "active") {
|
|
645
|
+
return false;
|
|
646
|
+
}
|
|
647
|
+
if (grant.expiresAt !== void 0 && grant.expiresAt <= Date.now()) {
|
|
648
|
+
return false;
|
|
649
|
+
}
|
|
650
|
+
return true;
|
|
651
|
+
}
|
|
652
|
+
async function hasPrincipalGrant(ctx, args) {
|
|
653
|
+
const grants = await resolveAccessControlAppResolvers().listProjectGrantsByProjectAndPrincipal(
|
|
654
|
+
ctx,
|
|
655
|
+
args.topic._id,
|
|
656
|
+
args.principalId
|
|
657
|
+
);
|
|
658
|
+
if (grants.some(
|
|
659
|
+
(grant) => isGrantActive(grant) && isGrantScopeAlignedToTopic(args.topic, grant) && isGrantSourceAllowedForVisibility(
|
|
660
|
+
args.topic.visibility,
|
|
661
|
+
grant.source
|
|
662
|
+
) && (!args.principalIsExternal || args.topic.visibility === "public" || grant.source === "external_share")
|
|
663
|
+
)) {
|
|
664
|
+
return true;
|
|
665
|
+
}
|
|
666
|
+
return false;
|
|
667
|
+
}
|
|
668
|
+
async function hasGroupGrant(ctx, args) {
|
|
669
|
+
if (args.groupIds.length === 0) {
|
|
670
|
+
return false;
|
|
671
|
+
}
|
|
672
|
+
for (const groupId of args.groupIds) {
|
|
673
|
+
const grants = await resolveAccessControlAppResolvers().listProjectGrantsByProjectAndGroup(ctx, args.topic._id, groupId);
|
|
674
|
+
if (grants.some(
|
|
675
|
+
(grant) => isGrantActive(grant) && isGrantScopeAlignedToTopic(args.topic, grant) && isGrantSourceAllowedForVisibility(
|
|
676
|
+
args.topic.visibility,
|
|
677
|
+
grant.source
|
|
678
|
+
)
|
|
679
|
+
)) {
|
|
680
|
+
return true;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
return false;
|
|
684
|
+
}
|
|
685
|
+
function isExternalPrincipal(_ctx, _args) {
|
|
686
|
+
return false;
|
|
687
|
+
}
|
|
688
|
+
async function evaluateTopicAccessDetailed(ctx, args) {
|
|
689
|
+
if (args.legacyUserId) {
|
|
690
|
+
return {
|
|
691
|
+
hasAccess: true,
|
|
692
|
+
isAdmin: false,
|
|
693
|
+
isOwner: false,
|
|
694
|
+
isShared: false,
|
|
695
|
+
hasGrant: true,
|
|
696
|
+
isFirmVisible: true,
|
|
697
|
+
isExternalVisible: false,
|
|
698
|
+
isPublicVisible: false,
|
|
699
|
+
isTenantScopeMatch: true,
|
|
700
|
+
isWorkspaceScopeMatch: true,
|
|
701
|
+
isPrincipalExternal: false
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
const topic = await resolveAccessControlAppResolvers().getProject(
|
|
705
|
+
ctx,
|
|
706
|
+
args.topicId
|
|
707
|
+
);
|
|
708
|
+
if (!topic) {
|
|
709
|
+
return {
|
|
710
|
+
hasAccess: false,
|
|
711
|
+
isAdmin: false,
|
|
712
|
+
isOwner: false,
|
|
713
|
+
isShared: false,
|
|
714
|
+
hasGrant: false,
|
|
715
|
+
isFirmVisible: false,
|
|
716
|
+
isExternalVisible: false,
|
|
717
|
+
isPublicVisible: false,
|
|
718
|
+
isTenantScopeMatch: false,
|
|
719
|
+
isWorkspaceScopeMatch: false,
|
|
720
|
+
isPrincipalExternal: false
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
const { principalContext, legacyUserId } = args;
|
|
724
|
+
const userIsAdmin = principalContext.isPlatformAdmin;
|
|
725
|
+
const isOwner = topic.ownerId === legacyUserId;
|
|
726
|
+
const isShared = (topic.sharedWith ?? []).includes(legacyUserId);
|
|
727
|
+
const principalIsExternal = await isExternalPrincipal(ctx, {
|
|
728
|
+
groupIds: principalContext.groupIds,
|
|
729
|
+
topicTenantId: topic.tenantId,
|
|
730
|
+
topicWorkspaceId: topic.workspaceId
|
|
731
|
+
});
|
|
732
|
+
const hasPrincipalGrantResult = await hasPrincipalGrant(ctx, {
|
|
733
|
+
topic,
|
|
734
|
+
principalId: principalContext.principalId,
|
|
735
|
+
principalIsExternal
|
|
736
|
+
});
|
|
737
|
+
const hasGroupGrantResult = await hasGroupGrant(ctx, {
|
|
738
|
+
topic,
|
|
739
|
+
groupIds: principalContext.groupIds
|
|
740
|
+
});
|
|
741
|
+
const hasGrant = isShared || hasPrincipalGrantResult || hasGroupGrantResult;
|
|
742
|
+
const legacyUnscoped = isLegacyUnscopedTopic(topic);
|
|
743
|
+
const tenantScopeMatch = isTopicInPrincipalTenant(
|
|
744
|
+
topic,
|
|
745
|
+
principalContext.tenantId
|
|
746
|
+
);
|
|
747
|
+
const workspaceScopeMatch = isTopicInPrincipalWorkspace(
|
|
748
|
+
topic,
|
|
749
|
+
principalContext.workspaceId
|
|
750
|
+
);
|
|
751
|
+
const isPublicVisible = topic.visibility === "public";
|
|
752
|
+
const isFirmVisible = topic.visibility === "firm" && !legacyUnscoped && tenantScopeMatch && workspaceScopeMatch && !principalIsExternal;
|
|
753
|
+
const hasScopedGrant = hasGrant && (legacyUnscoped || tenantScopeMatch && workspaceScopeMatch);
|
|
754
|
+
const isExternalVisible = topic.visibility === "external" && hasScopedGrant;
|
|
755
|
+
const hasAccess = userIsAdmin || isOwner || hasScopedGrant || isPublicVisible || isFirmVisible;
|
|
756
|
+
return {
|
|
757
|
+
hasAccess,
|
|
758
|
+
isAdmin: userIsAdmin,
|
|
759
|
+
isOwner,
|
|
760
|
+
isShared,
|
|
761
|
+
hasGrant,
|
|
762
|
+
isFirmVisible,
|
|
763
|
+
isExternalVisible,
|
|
764
|
+
isPublicVisible,
|
|
765
|
+
isTenantScopeMatch: tenantScopeMatch,
|
|
766
|
+
isWorkspaceScopeMatch: workspaceScopeMatch,
|
|
767
|
+
isPrincipalExternal: principalIsExternal
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
async function checkTopicAccessDetailed(ctx, topicId, userId) {
|
|
771
|
+
const principalContext = await resolvePrincipalContext(ctx, userId);
|
|
772
|
+
return evaluateTopicAccessDetailed(ctx, {
|
|
773
|
+
topicId,
|
|
774
|
+
legacyUserId: userId,
|
|
775
|
+
principalContext
|
|
776
|
+
});
|
|
777
|
+
}
|
|
778
|
+
async function checkTopicAccess(ctx, topicId, userId) {
|
|
779
|
+
const result = await checkTopicAccessDetailed(ctx, topicId, userId);
|
|
780
|
+
return result.hasAccess;
|
|
781
|
+
}
|
|
782
|
+
async function checkScopeAccess(ctx, scopeId, userId) {
|
|
783
|
+
try {
|
|
784
|
+
const topic = await ctx.db.get(scopeId);
|
|
785
|
+
if (topic && topic.name !== void 0 && topic.type !== void 0) {
|
|
786
|
+
return true;
|
|
787
|
+
}
|
|
788
|
+
} catch {
|
|
789
|
+
}
|
|
790
|
+
try {
|
|
791
|
+
return await checkTopicAccess(ctx, scopeId, userId);
|
|
792
|
+
} catch {
|
|
793
|
+
return false;
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
// src/globalId.ts
|
|
798
|
+
function generateGlobalId() {
|
|
799
|
+
const bytes = new Uint8Array(16);
|
|
800
|
+
crypto.getRandomValues(bytes);
|
|
801
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
802
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
803
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(
|
|
804
|
+
""
|
|
805
|
+
);
|
|
806
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
807
|
+
}
|
|
808
|
+
var LEGACY_SCOPE_FIELD2 = "graphScopeProjectId";
|
|
809
|
+
function asMappedProjectId(topic) {
|
|
810
|
+
if (!topic) {
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
813
|
+
const directLegacyProjectId = normalizeScopeValue(topic[LEGACY_SCOPE_FIELD2]);
|
|
814
|
+
if (directLegacyProjectId) {
|
|
815
|
+
return directLegacyProjectId;
|
|
816
|
+
}
|
|
817
|
+
const metadata = topic.metadata || {};
|
|
818
|
+
const candidate = metadata[LEGACY_SCOPE_FIELD2] || metadata.legacyProjectId || metadata.projectId || metadata.scopeProjectId;
|
|
819
|
+
return candidate ? candidate : void 0;
|
|
820
|
+
}
|
|
821
|
+
function normalizeScopeValue(value) {
|
|
822
|
+
if (typeof value !== "string") {
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
const normalized = value.trim();
|
|
826
|
+
return normalized.length > 0 ? normalized : void 0;
|
|
827
|
+
}
|
|
828
|
+
function pickPrimaryTopic(candidates) {
|
|
829
|
+
return [...candidates].sort((a, b) => {
|
|
830
|
+
const depthA = a.depth ?? 9999;
|
|
831
|
+
const depthB = b.depth ?? 9999;
|
|
832
|
+
if (depthA !== depthB) {
|
|
833
|
+
return depthA - depthB;
|
|
834
|
+
}
|
|
835
|
+
const createdA = a.createdAt ?? Number.MAX_SAFE_INTEGER;
|
|
836
|
+
const createdB = b.createdAt ?? Number.MAX_SAFE_INTEGER;
|
|
837
|
+
if (createdA !== createdB) {
|
|
838
|
+
return createdA - createdB;
|
|
839
|
+
}
|
|
840
|
+
return String(a.name || "").localeCompare(String(b.name || ""));
|
|
841
|
+
})[0];
|
|
842
|
+
}
|
|
843
|
+
async function findTopicsByScopeAlias(ctx, scopeId) {
|
|
844
|
+
try {
|
|
845
|
+
return await ctx.db.query("topics").withIndex(
|
|
846
|
+
"by_graph_scope_project",
|
|
847
|
+
(q) => q.eq(LEGACY_SCOPE_FIELD2, scopeId)
|
|
848
|
+
).collect();
|
|
849
|
+
} catch {
|
|
850
|
+
const topics = await ctx.db.query("topics").collect();
|
|
851
|
+
return topics.filter((topic) => {
|
|
852
|
+
const normalizedGlobalId = normalizeScopeValue(topic.globalId);
|
|
853
|
+
const mappedProjectId = asMappedProjectId(topic);
|
|
854
|
+
return String(topic._id) === scopeId || normalizedGlobalId === scopeId || mappedProjectId === scopeId;
|
|
855
|
+
});
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
async function tryResolveHostTopicById(ctx, topicId) {
|
|
859
|
+
if (typeof ctx.runQuery !== "function") {
|
|
860
|
+
return null;
|
|
861
|
+
}
|
|
862
|
+
try {
|
|
863
|
+
return await ctx.runQuery(api.topics.get, {
|
|
864
|
+
id: topicId
|
|
865
|
+
}) ?? null;
|
|
866
|
+
} catch {
|
|
867
|
+
return null;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
async function tryResolveHostTopicByLegacyScope(ctx, legacyScopeId) {
|
|
871
|
+
if (typeof ctx.runQuery !== "function") {
|
|
872
|
+
return null;
|
|
873
|
+
}
|
|
874
|
+
try {
|
|
875
|
+
return await ctx.runQuery(api.topics.getByLegacyScopeId, {
|
|
876
|
+
projectId: legacyScopeId
|
|
877
|
+
}) ?? null;
|
|
878
|
+
} catch {
|
|
879
|
+
return null;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
async function resolveInheritedWorkspaceScope(ctx, topic) {
|
|
883
|
+
const MAX_DEPTH = 10;
|
|
884
|
+
let tenantId = normalizeScopeValue(topic.tenantId);
|
|
885
|
+
let workspaceId = normalizeScopeValue(topic.workspaceId);
|
|
886
|
+
if (tenantId && workspaceId) {
|
|
887
|
+
return { tenantId, workspaceId };
|
|
888
|
+
}
|
|
889
|
+
let current = topic;
|
|
890
|
+
for (let i = 0; i < MAX_DEPTH && current?.parentTopicId; i++) {
|
|
891
|
+
current = await ctx.db.get(current.parentTopicId);
|
|
892
|
+
if (!current) break;
|
|
893
|
+
if (!tenantId) {
|
|
894
|
+
tenantId = normalizeScopeValue(current.tenantId);
|
|
895
|
+
}
|
|
896
|
+
if (!workspaceId) {
|
|
897
|
+
workspaceId = normalizeScopeValue(current.workspaceId);
|
|
898
|
+
}
|
|
899
|
+
if (tenantId && workspaceId) break;
|
|
900
|
+
}
|
|
901
|
+
return { tenantId, workspaceId };
|
|
902
|
+
}
|
|
903
|
+
async function resolveTopicProjectScope(ctx, args) {
|
|
904
|
+
if (args.topicId) {
|
|
905
|
+
let topic = null;
|
|
906
|
+
try {
|
|
907
|
+
topic = await ctx.db.get(args.topicId);
|
|
908
|
+
} catch {
|
|
909
|
+
}
|
|
910
|
+
if (!topic) {
|
|
911
|
+
topic = await tryResolveHostTopicById(ctx, String(args.topicId));
|
|
912
|
+
}
|
|
913
|
+
if (!topic) {
|
|
914
|
+
topic = pickPrimaryTopic(
|
|
915
|
+
await findTopicsByScopeAlias(ctx, String(args.topicId))
|
|
916
|
+
) ?? null;
|
|
917
|
+
}
|
|
918
|
+
if (!topic) {
|
|
919
|
+
throw new Error(`Topic not found: ${String(args.topicId)}`);
|
|
920
|
+
}
|
|
921
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, topic);
|
|
922
|
+
const mapped = asMappedProjectId(topic);
|
|
923
|
+
if (mapped) {
|
|
924
|
+
return {
|
|
925
|
+
topicId: topic._id,
|
|
926
|
+
projectId: mapped,
|
|
927
|
+
tenantId: inherited.tenantId,
|
|
928
|
+
workspaceId: inherited.workspaceId,
|
|
929
|
+
source: "topic"
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
return {
|
|
933
|
+
topicId: topic._id,
|
|
934
|
+
tenantId: inherited.tenantId,
|
|
935
|
+
workspaceId: inherited.workspaceId,
|
|
936
|
+
source: "topic"
|
|
937
|
+
};
|
|
938
|
+
}
|
|
939
|
+
if (args.projectId) {
|
|
940
|
+
let directTopic = null;
|
|
941
|
+
try {
|
|
942
|
+
directTopic = await ctx.db.get(
|
|
943
|
+
args.projectId
|
|
944
|
+
);
|
|
945
|
+
} catch {
|
|
946
|
+
}
|
|
947
|
+
if (directTopic) {
|
|
948
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
|
|
949
|
+
const mapped = asMappedProjectId(directTopic);
|
|
950
|
+
return {
|
|
951
|
+
topicId: directTopic._id,
|
|
952
|
+
projectId: mapped ?? args.projectId,
|
|
953
|
+
tenantId: inherited.tenantId,
|
|
954
|
+
workspaceId: inherited.workspaceId,
|
|
955
|
+
source: "topic_inferred"
|
|
956
|
+
};
|
|
957
|
+
}
|
|
958
|
+
directTopic = await tryResolveHostTopicByLegacyScope(ctx, args.projectId);
|
|
959
|
+
if (directTopic) {
|
|
960
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
|
|
961
|
+
const mapped = asMappedProjectId(directTopic);
|
|
962
|
+
return {
|
|
963
|
+
topicId: directTopic._id,
|
|
964
|
+
projectId: mapped ?? args.projectId,
|
|
965
|
+
tenantId: inherited.tenantId,
|
|
966
|
+
workspaceId: inherited.workspaceId,
|
|
967
|
+
source: "topic_inferred"
|
|
968
|
+
};
|
|
969
|
+
}
|
|
970
|
+
const topics = await findTopicsByScopeAlias(ctx, args.projectId);
|
|
971
|
+
const primary = pickPrimaryTopic(topics);
|
|
972
|
+
if (primary) {
|
|
973
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, primary);
|
|
974
|
+
return {
|
|
975
|
+
topicId: primary._id,
|
|
976
|
+
projectId: args.projectId,
|
|
977
|
+
tenantId: inherited.tenantId,
|
|
978
|
+
workspaceId: inherited.workspaceId,
|
|
979
|
+
source: "project_mapped_topic"
|
|
980
|
+
};
|
|
981
|
+
}
|
|
982
|
+
throw new Error(
|
|
983
|
+
`Legacy project scope ${String(args.projectId)} has no mapped topic.`
|
|
984
|
+
);
|
|
985
|
+
}
|
|
986
|
+
throw new Error(
|
|
987
|
+
"Missing scope: provide topicId (preferred) or legacy projectId alias."
|
|
988
|
+
);
|
|
989
|
+
}
|
|
990
|
+
var optionalScopeArgs = {
|
|
991
|
+
projectId: v.optional(v.string()),
|
|
992
|
+
topicId: v.optional(v.string())
|
|
993
|
+
};
|
|
994
|
+
|
|
995
|
+
// src/scopeResolverCompat.ts
|
|
996
|
+
var scopeArgs = optionalScopeArgs;
|
|
997
|
+
function normalizeScopeValue2(value) {
|
|
998
|
+
if (typeof value !== "string") {
|
|
999
|
+
return;
|
|
1000
|
+
}
|
|
1001
|
+
const normalized = value.trim();
|
|
1002
|
+
return normalized.length > 0 ? normalized : void 0;
|
|
1003
|
+
}
|
|
1004
|
+
async function resolveScope(ctx, args) {
|
|
1005
|
+
const topicId = normalizeScopeValue2(args.topicId);
|
|
1006
|
+
const projectId = normalizeScopeValue2(args.projectId);
|
|
1007
|
+
if (!topicId && !projectId) {
|
|
1008
|
+
return null;
|
|
1009
|
+
}
|
|
1010
|
+
try {
|
|
1011
|
+
return await resolveTopicProjectScope(ctx, {
|
|
1012
|
+
topicId,
|
|
1013
|
+
projectId
|
|
1014
|
+
});
|
|
1015
|
+
} catch {
|
|
1016
|
+
return null;
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
var permissiveReturn = v.optional(v.any());
|
|
1020
|
+
var looseJsonObject = v.record(v.string(), v.any());
|
|
1021
|
+
var looseJsonArray = v.array(v.any());
|
|
1022
|
+
v.union(
|
|
1023
|
+
v.string(),
|
|
1024
|
+
v.number(),
|
|
1025
|
+
v.boolean(),
|
|
1026
|
+
v.null(),
|
|
1027
|
+
looseJsonObject,
|
|
1028
|
+
looseJsonArray
|
|
1029
|
+
);
|
|
1030
|
+
|
|
1031
|
+
// src/epistemicAnswers.ts
|
|
1032
|
+
function generateContentHash(text) {
|
|
1033
|
+
const content = `answer:${text.trim().toLowerCase().replace(/\s+/g, " ").slice(0, 500)}`;
|
|
1034
|
+
let hash = 5381;
|
|
1035
|
+
for (let i = 0; i < content.length; i++) {
|
|
1036
|
+
hash = (hash << 5) + hash + content.charCodeAt(i);
|
|
1037
|
+
hash &= hash;
|
|
1038
|
+
}
|
|
1039
|
+
return Math.abs(hash).toString(16).padStart(8, "0");
|
|
1040
|
+
}
|
|
1041
|
+
var create = mutation({
|
|
1042
|
+
args: {
|
|
1043
|
+
...scopeArgs,
|
|
1044
|
+
questionNodeId: v.id("epistemicNodes"),
|
|
1045
|
+
answerText: v.string(),
|
|
1046
|
+
confidence: v.optional(
|
|
1047
|
+
v.union(
|
|
1048
|
+
v.literal("definitive"),
|
|
1049
|
+
v.literal("strong"),
|
|
1050
|
+
v.literal("moderate"),
|
|
1051
|
+
v.literal("weak"),
|
|
1052
|
+
v.literal("speculative")
|
|
1053
|
+
)
|
|
1054
|
+
),
|
|
1055
|
+
evidenceNodeIds: v.optional(v.array(v.id("epistemicNodes"))),
|
|
1056
|
+
answerSource: v.optional(v.string()),
|
|
1057
|
+
sprintId: v.optional(v.string()),
|
|
1058
|
+
userId: v.string()
|
|
1059
|
+
},
|
|
1060
|
+
returns: permissiveReturn,
|
|
1061
|
+
handler: async (ctx, args) => {
|
|
1062
|
+
const scope = await resolveScope(ctx, args);
|
|
1063
|
+
const topicId = scope?.topicId ? String(scope.topicId) : args.topicId || "";
|
|
1064
|
+
await checkScopeAccess(ctx, topicId, args.userId);
|
|
1065
|
+
const questionNode = await ctx.db.get(args.questionNodeId);
|
|
1066
|
+
if (!questionNode || questionNode.nodeType !== "question") {
|
|
1067
|
+
throw new Error("Question node not found or not a question type");
|
|
1068
|
+
}
|
|
1069
|
+
const now = Date.now();
|
|
1070
|
+
const globalId = generateGlobalId();
|
|
1071
|
+
const contentHash = generateContentHash(args.answerText);
|
|
1072
|
+
const existingAnswers = await ctx.db.query("epistemicNodes").withIndex(
|
|
1073
|
+
"by_topic_type",
|
|
1074
|
+
(q) => q.eq("topicId", questionNode.topicId ?? topicId).eq("nodeType", "answer")
|
|
1075
|
+
).collect();
|
|
1076
|
+
const activeAnswersForQuestion = existingAnswers.filter((n) => {
|
|
1077
|
+
const meta = n.metadata || {};
|
|
1078
|
+
return meta.questionNodeId === args.questionNodeId && meta.isLatest === true && n.status === "active";
|
|
1079
|
+
});
|
|
1080
|
+
const versionNumber = activeAnswersForQuestion.length > 0 ? Math.max(
|
|
1081
|
+
...activeAnswersForQuestion.map((a) => {
|
|
1082
|
+
const meta = a.metadata || {};
|
|
1083
|
+
return meta.versionNumber || 0;
|
|
1084
|
+
})
|
|
1085
|
+
) + 1 : 1;
|
|
1086
|
+
for (const oldAnswer of activeAnswersForQuestion) {
|
|
1087
|
+
await ctx.db.patch(oldAnswer._id, {
|
|
1088
|
+
status: "superseded",
|
|
1089
|
+
supersededBy: void 0,
|
|
1090
|
+
// Will be set after insert
|
|
1091
|
+
updatedAt: now,
|
|
1092
|
+
metadata: {
|
|
1093
|
+
...oldAnswer.metadata || {},
|
|
1094
|
+
isLatest: false
|
|
1095
|
+
}
|
|
1096
|
+
});
|
|
1097
|
+
}
|
|
1098
|
+
const evidenceGlobalIds = [];
|
|
1099
|
+
if (args.evidenceNodeIds) {
|
|
1100
|
+
for (const evId of args.evidenceNodeIds) {
|
|
1101
|
+
const evNode = await ctx.db.get(evId);
|
|
1102
|
+
if (evNode) {
|
|
1103
|
+
evidenceGlobalIds.push(evNode.globalId);
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
const nodeId = await ctx.db.insert("epistemicNodes", {
|
|
1108
|
+
globalId,
|
|
1109
|
+
projectId: topicId,
|
|
1110
|
+
topicId: questionNode.topicId,
|
|
1111
|
+
nodeType: "answer",
|
|
1112
|
+
epistemicLayer: "L2",
|
|
1113
|
+
canonicalText: args.answerText,
|
|
1114
|
+
contentHash,
|
|
1115
|
+
status: "active",
|
|
1116
|
+
sourceType: args.answerSource === "human" ? "human" : "ai_generated",
|
|
1117
|
+
createdBy: args.userId,
|
|
1118
|
+
createdAt: now,
|
|
1119
|
+
updatedAt: now,
|
|
1120
|
+
metadata: {
|
|
1121
|
+
questionGlobalId: questionNode.globalId,
|
|
1122
|
+
questionNodeId: String(args.questionNodeId),
|
|
1123
|
+
versionNumber,
|
|
1124
|
+
isLatest: true,
|
|
1125
|
+
evidenceGlobalIds,
|
|
1126
|
+
evidenceCount: evidenceGlobalIds.length,
|
|
1127
|
+
answeredBy: args.userId,
|
|
1128
|
+
answerSource: args.answerSource || "human",
|
|
1129
|
+
sprintId: args.sprintId
|
|
1130
|
+
}
|
|
1131
|
+
});
|
|
1132
|
+
for (const oldAnswer of activeAnswersForQuestion) {
|
|
1133
|
+
await ctx.db.patch(oldAnswer._id, {
|
|
1134
|
+
supersededBy: nodeId
|
|
1135
|
+
});
|
|
1136
|
+
}
|
|
1137
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
1138
|
+
nodeId,
|
|
1139
|
+
operation: "upsert"
|
|
1140
|
+
});
|
|
1141
|
+
await ctx.scheduler.runAfter(
|
|
1142
|
+
0,
|
|
1143
|
+
api.embeddingActions.generateEpistemicNodeEmbedding,
|
|
1144
|
+
{
|
|
1145
|
+
nodeId,
|
|
1146
|
+
projectId: topicId,
|
|
1147
|
+
topicId: questionNode.topicId ? String(questionNode.topicId) : void 0,
|
|
1148
|
+
createdBy: args.userId,
|
|
1149
|
+
nodeType: "answer",
|
|
1150
|
+
text: args.answerText.slice(0, 2e4)
|
|
1151
|
+
}
|
|
1152
|
+
);
|
|
1153
|
+
await ctx.scheduler.runAfter(
|
|
1154
|
+
0,
|
|
1155
|
+
internal.sprints.embedProjectThemeCollectiveInternal,
|
|
1156
|
+
{
|
|
1157
|
+
projectId: topicId,
|
|
1158
|
+
userId: args.userId
|
|
1159
|
+
}
|
|
1160
|
+
);
|
|
1161
|
+
await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.createEdge, {
|
|
1162
|
+
globalId: crypto.randomUUID(),
|
|
1163
|
+
fromGlobalId: globalId,
|
|
1164
|
+
toGlobalId: questionNode.globalId,
|
|
1165
|
+
edgeType: "responds_to",
|
|
1166
|
+
weight: 1,
|
|
1167
|
+
createdBy: args.userId,
|
|
1168
|
+
topicId: String(args.topicId ?? ""),
|
|
1169
|
+
fromNodeType: "answer",
|
|
1170
|
+
toNodeType: "question",
|
|
1171
|
+
fromLayer: "L2",
|
|
1172
|
+
toLayer: "L3"
|
|
1173
|
+
});
|
|
1174
|
+
for (const oldAnswer of activeAnswersForQuestion) {
|
|
1175
|
+
await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.createEdge, {
|
|
1176
|
+
globalId: crypto.randomUUID(),
|
|
1177
|
+
fromGlobalId: globalId,
|
|
1178
|
+
toGlobalId: oldAnswer.globalId,
|
|
1179
|
+
edgeType: "supersedes",
|
|
1180
|
+
createdBy: args.userId,
|
|
1181
|
+
topicId: String(args.topicId ?? ""),
|
|
1182
|
+
fromNodeType: "answer",
|
|
1183
|
+
toNodeType: "answer",
|
|
1184
|
+
fromLayer: "L2",
|
|
1185
|
+
toLayer: "L2"
|
|
1186
|
+
});
|
|
1187
|
+
}
|
|
1188
|
+
if (args.evidenceNodeIds) {
|
|
1189
|
+
for (const evId of args.evidenceNodeIds) {
|
|
1190
|
+
const evNode = await ctx.db.get(evId);
|
|
1191
|
+
if (evNode) {
|
|
1192
|
+
await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.createEdge, {
|
|
1193
|
+
globalId: crypto.randomUUID(),
|
|
1194
|
+
fromGlobalId: globalId,
|
|
1195
|
+
toGlobalId: evNode.globalId,
|
|
1196
|
+
edgeType: "derived_from",
|
|
1197
|
+
createdBy: args.userId,
|
|
1198
|
+
topicId: String(args.topicId ?? ""),
|
|
1199
|
+
fromNodeType: "answer",
|
|
1200
|
+
toNodeType: "evidence",
|
|
1201
|
+
fromLayer: "L2",
|
|
1202
|
+
toLayer: "L2"
|
|
1203
|
+
});
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
const evidenceEdges = await ctx.db.query("epistemicEdges").withIndex(
|
|
1208
|
+
"by_to_type",
|
|
1209
|
+
(q) => q.eq("toNodeId", args.questionNodeId).eq("edgeType", "derived_from")
|
|
1210
|
+
).collect();
|
|
1211
|
+
for (const edge of evidenceEdges) {
|
|
1212
|
+
const evidenceNode = await ctx.db.get(edge.fromNodeId);
|
|
1213
|
+
if (evidenceNode) {
|
|
1214
|
+
await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.createEdge, {
|
|
1215
|
+
globalId: crypto.randomUUID(),
|
|
1216
|
+
fromGlobalId: evidenceNode.globalId,
|
|
1217
|
+
toGlobalId: globalId,
|
|
1218
|
+
edgeType: "derived_from",
|
|
1219
|
+
createdBy: args.userId,
|
|
1220
|
+
topicId: String(args.topicId ?? ""),
|
|
1221
|
+
fromNodeType: "evidence",
|
|
1222
|
+
toNodeType: "answer",
|
|
1223
|
+
fromLayer: "L2",
|
|
1224
|
+
toLayer: "L2"
|
|
1225
|
+
});
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
const answerQuality = args.confidence || "moderate";
|
|
1229
|
+
await ctx.db.patch(args.questionNodeId, {
|
|
1230
|
+
answerQuality,
|
|
1231
|
+
updatedAt: now
|
|
1232
|
+
});
|
|
1233
|
+
await ctx.db.insert("epistemicAudit", {
|
|
1234
|
+
entityType: "answer",
|
|
1235
|
+
entityId: nodeId,
|
|
1236
|
+
changeType: "created",
|
|
1237
|
+
changedAt: now,
|
|
1238
|
+
changedBy: args.userId,
|
|
1239
|
+
isAgent: false,
|
|
1240
|
+
projectId: topicId,
|
|
1241
|
+
newState: {
|
|
1242
|
+
answerText: args.answerText.slice(0, 200),
|
|
1243
|
+
versionNumber,
|
|
1244
|
+
questionNodeId: String(args.questionNodeId),
|
|
1245
|
+
confidence: answerQuality
|
|
1246
|
+
}
|
|
1247
|
+
});
|
|
1248
|
+
return { nodeId, globalId, versionNumber };
|
|
1249
|
+
}
|
|
1250
|
+
});
|
|
1251
|
+
var createInternal = internalMutation({
|
|
1252
|
+
args: {
|
|
1253
|
+
...scopeArgs,
|
|
1254
|
+
questionNodeId: v.id("epistemicNodes"),
|
|
1255
|
+
answerText: v.string(),
|
|
1256
|
+
confidence: v.optional(v.string()),
|
|
1257
|
+
evidenceNodeIds: v.optional(v.array(v.id("epistemicNodes"))),
|
|
1258
|
+
answerSource: v.optional(v.string()),
|
|
1259
|
+
sprintId: v.optional(v.string()),
|
|
1260
|
+
userId: v.string(),
|
|
1261
|
+
backfilledFromQuestionId: v.optional(v.string())
|
|
1262
|
+
},
|
|
1263
|
+
returns: permissiveReturn,
|
|
1264
|
+
handler: async (ctx, args) => {
|
|
1265
|
+
const scope = await resolveScope(ctx, args);
|
|
1266
|
+
const topicId = scope?.topicId ? String(scope.topicId) : args.topicId || "";
|
|
1267
|
+
const questionNode = await ctx.db.get(args.questionNodeId);
|
|
1268
|
+
if (!questionNode || questionNode.nodeType !== "question") {
|
|
1269
|
+
throw new Error("Question node not found or not a question type");
|
|
1270
|
+
}
|
|
1271
|
+
const now = Date.now();
|
|
1272
|
+
const globalId = generateGlobalId();
|
|
1273
|
+
const contentHash = generateContentHash(args.answerText);
|
|
1274
|
+
const existingAnswers = await ctx.db.query("epistemicNodes").withIndex(
|
|
1275
|
+
"by_topic_type",
|
|
1276
|
+
(q) => q.eq("topicId", questionNode.topicId ?? topicId).eq("nodeType", "answer")
|
|
1277
|
+
).collect();
|
|
1278
|
+
const activeAnswersForQuestion = existingAnswers.filter((n) => {
|
|
1279
|
+
const meta = n.metadata || {};
|
|
1280
|
+
return meta.questionNodeId === String(args.questionNodeId) && meta.isLatest === true && n.status === "active";
|
|
1281
|
+
});
|
|
1282
|
+
const versionNumber = activeAnswersForQuestion.length > 0 ? Math.max(
|
|
1283
|
+
...activeAnswersForQuestion.map((a) => {
|
|
1284
|
+
const meta = a.metadata || {};
|
|
1285
|
+
return meta.versionNumber || 0;
|
|
1286
|
+
})
|
|
1287
|
+
) + 1 : 1;
|
|
1288
|
+
for (const oldAnswer of activeAnswersForQuestion) {
|
|
1289
|
+
await ctx.db.patch(oldAnswer._id, {
|
|
1290
|
+
status: "superseded",
|
|
1291
|
+
updatedAt: now,
|
|
1292
|
+
metadata: {
|
|
1293
|
+
...oldAnswer.metadata || {},
|
|
1294
|
+
isLatest: false
|
|
1295
|
+
}
|
|
1296
|
+
});
|
|
1297
|
+
}
|
|
1298
|
+
const evidenceGlobalIds = [];
|
|
1299
|
+
if (args.evidenceNodeIds) {
|
|
1300
|
+
for (const evId of args.evidenceNodeIds) {
|
|
1301
|
+
const evNode = await ctx.db.get(evId);
|
|
1302
|
+
if (evNode) {
|
|
1303
|
+
evidenceGlobalIds.push(evNode.globalId);
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
const nodeId = await ctx.db.insert("epistemicNodes", {
|
|
1308
|
+
globalId,
|
|
1309
|
+
projectId: topicId,
|
|
1310
|
+
topicId: questionNode.topicId,
|
|
1311
|
+
nodeType: "answer",
|
|
1312
|
+
epistemicLayer: "L2",
|
|
1313
|
+
canonicalText: args.answerText,
|
|
1314
|
+
contentHash,
|
|
1315
|
+
status: "active",
|
|
1316
|
+
sourceType: args.answerSource === "human" ? "human" : "ai_generated",
|
|
1317
|
+
createdBy: args.userId,
|
|
1318
|
+
createdAt: now,
|
|
1319
|
+
updatedAt: now,
|
|
1320
|
+
metadata: {
|
|
1321
|
+
questionGlobalId: questionNode.globalId,
|
|
1322
|
+
questionNodeId: String(args.questionNodeId),
|
|
1323
|
+
versionNumber,
|
|
1324
|
+
isLatest: true,
|
|
1325
|
+
evidenceGlobalIds,
|
|
1326
|
+
evidenceCount: evidenceGlobalIds.length,
|
|
1327
|
+
answeredBy: args.userId,
|
|
1328
|
+
answerSource: args.answerSource || "human",
|
|
1329
|
+
sprintId: args.sprintId,
|
|
1330
|
+
backfilledFromQuestionId: args.backfilledFromQuestionId
|
|
1331
|
+
}
|
|
1332
|
+
});
|
|
1333
|
+
for (const oldAnswer of activeAnswersForQuestion) {
|
|
1334
|
+
await ctx.db.patch(oldAnswer._id, {
|
|
1335
|
+
supersededBy: nodeId
|
|
1336
|
+
});
|
|
1337
|
+
}
|
|
1338
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
1339
|
+
nodeId,
|
|
1340
|
+
operation: "upsert"
|
|
1341
|
+
});
|
|
1342
|
+
await ctx.scheduler.runAfter(
|
|
1343
|
+
0,
|
|
1344
|
+
api.embeddingActions.generateEpistemicNodeEmbedding,
|
|
1345
|
+
{
|
|
1346
|
+
nodeId,
|
|
1347
|
+
projectId: topicId,
|
|
1348
|
+
topicId: questionNode.topicId ? String(questionNode.topicId) : void 0,
|
|
1349
|
+
createdBy: args.userId,
|
|
1350
|
+
nodeType: "answer",
|
|
1351
|
+
text: args.answerText.slice(0, 2e4)
|
|
1352
|
+
}
|
|
1353
|
+
);
|
|
1354
|
+
await ctx.scheduler.runAfter(
|
|
1355
|
+
0,
|
|
1356
|
+
internal.sprints.embedProjectThemeCollectiveInternal,
|
|
1357
|
+
{
|
|
1358
|
+
projectId: topicId,
|
|
1359
|
+
userId: args.userId
|
|
1360
|
+
}
|
|
1361
|
+
);
|
|
1362
|
+
await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.createEdge, {
|
|
1363
|
+
globalId: crypto.randomUUID(),
|
|
1364
|
+
fromGlobalId: globalId,
|
|
1365
|
+
toGlobalId: questionNode.globalId,
|
|
1366
|
+
edgeType: "responds_to",
|
|
1367
|
+
weight: 1,
|
|
1368
|
+
createdBy: args.userId,
|
|
1369
|
+
topicId: String(args.topicId ?? ""),
|
|
1370
|
+
fromNodeType: "answer",
|
|
1371
|
+
toNodeType: "question",
|
|
1372
|
+
fromLayer: "L2",
|
|
1373
|
+
toLayer: "L3"
|
|
1374
|
+
});
|
|
1375
|
+
for (const oldAnswer of activeAnswersForQuestion) {
|
|
1376
|
+
await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.createEdge, {
|
|
1377
|
+
globalId: crypto.randomUUID(),
|
|
1378
|
+
fromGlobalId: globalId,
|
|
1379
|
+
toGlobalId: oldAnswer.globalId,
|
|
1380
|
+
edgeType: "supersedes",
|
|
1381
|
+
createdBy: args.userId,
|
|
1382
|
+
topicId: String(args.topicId ?? ""),
|
|
1383
|
+
fromNodeType: "answer",
|
|
1384
|
+
toNodeType: "answer",
|
|
1385
|
+
fromLayer: "L2",
|
|
1386
|
+
toLayer: "L2"
|
|
1387
|
+
});
|
|
1388
|
+
}
|
|
1389
|
+
if (args.evidenceNodeIds) {
|
|
1390
|
+
for (const evId of args.evidenceNodeIds) {
|
|
1391
|
+
const evNode = await ctx.db.get(evId);
|
|
1392
|
+
if (evNode) {
|
|
1393
|
+
await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.createEdge, {
|
|
1394
|
+
globalId: crypto.randomUUID(),
|
|
1395
|
+
fromGlobalId: globalId,
|
|
1396
|
+
toGlobalId: evNode.globalId,
|
|
1397
|
+
edgeType: "derived_from",
|
|
1398
|
+
createdBy: args.userId,
|
|
1399
|
+
topicId: String(args.topicId ?? ""),
|
|
1400
|
+
fromNodeType: "answer",
|
|
1401
|
+
toNodeType: "evidence",
|
|
1402
|
+
fromLayer: "L2",
|
|
1403
|
+
toLayer: "L2"
|
|
1404
|
+
});
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
const evidenceEdges = await ctx.db.query("epistemicEdges").withIndex(
|
|
1409
|
+
"by_to_type",
|
|
1410
|
+
(q) => q.eq("toNodeId", args.questionNodeId).eq("edgeType", "derived_from")
|
|
1411
|
+
).collect();
|
|
1412
|
+
for (const edge of evidenceEdges) {
|
|
1413
|
+
const evidenceNode = await ctx.db.get(edge.fromNodeId);
|
|
1414
|
+
if (evidenceNode) {
|
|
1415
|
+
await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.createEdge, {
|
|
1416
|
+
globalId: crypto.randomUUID(),
|
|
1417
|
+
fromGlobalId: evidenceNode.globalId,
|
|
1418
|
+
toGlobalId: globalId,
|
|
1419
|
+
edgeType: "derived_from",
|
|
1420
|
+
createdBy: args.userId,
|
|
1421
|
+
topicId: String(args.topicId ?? ""),
|
|
1422
|
+
fromNodeType: "evidence",
|
|
1423
|
+
toNodeType: "answer",
|
|
1424
|
+
fromLayer: "L2",
|
|
1425
|
+
toLayer: "L2"
|
|
1426
|
+
});
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
const answerQuality = args.confidence || "moderate";
|
|
1430
|
+
await ctx.db.patch(args.questionNodeId, {
|
|
1431
|
+
answerQuality,
|
|
1432
|
+
updatedAt: now
|
|
1433
|
+
});
|
|
1434
|
+
await ctx.db.insert("epistemicAudit", {
|
|
1435
|
+
entityType: "answer",
|
|
1436
|
+
entityId: nodeId,
|
|
1437
|
+
changeType: "created",
|
|
1438
|
+
changedAt: now,
|
|
1439
|
+
changedBy: args.userId,
|
|
1440
|
+
isAgent: false,
|
|
1441
|
+
projectId: topicId,
|
|
1442
|
+
newState: {
|
|
1443
|
+
answerText: args.answerText.slice(0, 200),
|
|
1444
|
+
versionNumber,
|
|
1445
|
+
questionNodeId: String(args.questionNodeId),
|
|
1446
|
+
backfilled: !!args.backfilledFromQuestionId
|
|
1447
|
+
}
|
|
1448
|
+
});
|
|
1449
|
+
return { nodeId, globalId, versionNumber };
|
|
1450
|
+
}
|
|
1451
|
+
});
|
|
1452
|
+
var getByQuestion = query({
|
|
1453
|
+
args: {
|
|
1454
|
+
questionNodeId: v.id("epistemicNodes"),
|
|
1455
|
+
includeSuperseded: v.optional(v.boolean())
|
|
1456
|
+
},
|
|
1457
|
+
returns: permissiveReturn,
|
|
1458
|
+
handler: async (ctx, args) => {
|
|
1459
|
+
const questionNode = await ctx.db.get(args.questionNodeId);
|
|
1460
|
+
if (!questionNode || questionNode.nodeType !== "question") {
|
|
1461
|
+
return [];
|
|
1462
|
+
}
|
|
1463
|
+
const topicId = questionNode.topicId;
|
|
1464
|
+
const answers = topicId ? await ctx.db.query("epistemicNodes").withIndex(
|
|
1465
|
+
"by_topic_type",
|
|
1466
|
+
(q) => q.eq("topicId", topicId).eq("nodeType", "answer")
|
|
1467
|
+
).collect() : await ctx.db.query("epistemicNodes").withIndex(
|
|
1468
|
+
"by_project_type",
|
|
1469
|
+
(q) => q.eq("projectId", questionNode.projectId).eq("nodeType", "answer")
|
|
1470
|
+
).collect();
|
|
1471
|
+
const questionIdStr = String(args.questionNodeId);
|
|
1472
|
+
const filtered = answers.filter((a) => {
|
|
1473
|
+
const meta = a.metadata || {};
|
|
1474
|
+
return meta.questionNodeId === questionIdStr;
|
|
1475
|
+
});
|
|
1476
|
+
if (!args.includeSuperseded) {
|
|
1477
|
+
return filtered.filter((a) => a.status === "active");
|
|
1478
|
+
}
|
|
1479
|
+
filtered.sort((a, b) => {
|
|
1480
|
+
const metaA = a.metadata || {};
|
|
1481
|
+
const metaB = b.metadata || {};
|
|
1482
|
+
return (metaB.versionNumber || 0) - (metaA.versionNumber || 0);
|
|
1483
|
+
});
|
|
1484
|
+
return filtered;
|
|
1485
|
+
}
|
|
1486
|
+
});
|
|
1487
|
+
var getLatestForQuestion = query({
|
|
1488
|
+
args: {
|
|
1489
|
+
questionNodeId: v.id("epistemicNodes")
|
|
1490
|
+
},
|
|
1491
|
+
returns: permissiveReturn,
|
|
1492
|
+
handler: async (ctx, args) => {
|
|
1493
|
+
const questionNode = await ctx.db.get(args.questionNodeId);
|
|
1494
|
+
if (!questionNode || questionNode.nodeType !== "question") {
|
|
1495
|
+
return null;
|
|
1496
|
+
}
|
|
1497
|
+
let answers;
|
|
1498
|
+
if (questionNode.topicId) {
|
|
1499
|
+
answers = await ctx.db.query("epistemicNodes").withIndex(
|
|
1500
|
+
"by_topic_type",
|
|
1501
|
+
(q) => q.eq("topicId", questionNode.topicId).eq("nodeType", "answer")
|
|
1502
|
+
).filter((q) => q.eq(q.field("status"), "active")).collect();
|
|
1503
|
+
} else {
|
|
1504
|
+
answers = await ctx.db.query("epistemicNodes").withIndex(
|
|
1505
|
+
"by_project_type",
|
|
1506
|
+
(q) => q.eq("projectId", questionNode.projectId).eq("nodeType", "answer")
|
|
1507
|
+
).filter((q) => q.eq(q.field("status"), "active")).collect();
|
|
1508
|
+
}
|
|
1509
|
+
const questionIdStr = String(args.questionNodeId);
|
|
1510
|
+
const latest = answers.find((a) => {
|
|
1511
|
+
const meta = a.metadata || {};
|
|
1512
|
+
return meta.questionNodeId === questionIdStr && meta.isLatest === true;
|
|
1513
|
+
});
|
|
1514
|
+
return latest || null;
|
|
1515
|
+
}
|
|
1516
|
+
});
|
|
1517
|
+
var getVersionHistory = query({
|
|
1518
|
+
args: {
|
|
1519
|
+
questionNodeId: v.id("epistemicNodes")
|
|
1520
|
+
},
|
|
1521
|
+
returns: permissiveReturn,
|
|
1522
|
+
handler: async (ctx, args) => {
|
|
1523
|
+
const questionNode = await ctx.db.get(args.questionNodeId);
|
|
1524
|
+
if (!questionNode || questionNode.nodeType !== "question") {
|
|
1525
|
+
return [];
|
|
1526
|
+
}
|
|
1527
|
+
let answers;
|
|
1528
|
+
if (questionNode.topicId) {
|
|
1529
|
+
answers = await ctx.db.query("epistemicNodes").withIndex(
|
|
1530
|
+
"by_topic_type",
|
|
1531
|
+
(q) => q.eq("topicId", questionNode.topicId).eq("nodeType", "answer")
|
|
1532
|
+
).filter(
|
|
1533
|
+
(q) => q.or(
|
|
1534
|
+
q.eq(q.field("status"), "active"),
|
|
1535
|
+
q.eq(q.field("status"), "superseded")
|
|
1536
|
+
)
|
|
1537
|
+
).collect();
|
|
1538
|
+
} else {
|
|
1539
|
+
answers = await ctx.db.query("epistemicNodes").withIndex(
|
|
1540
|
+
"by_project_type",
|
|
1541
|
+
(q) => q.eq("projectId", questionNode.projectId).eq("nodeType", "answer")
|
|
1542
|
+
).filter(
|
|
1543
|
+
(q) => q.or(
|
|
1544
|
+
q.eq(q.field("status"), "active"),
|
|
1545
|
+
q.eq(q.field("status"), "superseded")
|
|
1546
|
+
)
|
|
1547
|
+
).collect();
|
|
1548
|
+
}
|
|
1549
|
+
const questionIdStr = String(args.questionNodeId);
|
|
1550
|
+
const history = answers.filter((a) => {
|
|
1551
|
+
const meta = a.metadata || {};
|
|
1552
|
+
return meta.questionNodeId === questionIdStr;
|
|
1553
|
+
}).map((a) => {
|
|
1554
|
+
const meta = a.metadata || {};
|
|
1555
|
+
return {
|
|
1556
|
+
_id: a._id,
|
|
1557
|
+
globalId: a.globalId,
|
|
1558
|
+
answerText: a.canonicalText,
|
|
1559
|
+
versionNumber: meta.versionNumber || 1,
|
|
1560
|
+
isLatest: meta.isLatest || false,
|
|
1561
|
+
status: a.status,
|
|
1562
|
+
answeredBy: meta.answeredBy || a.createdBy,
|
|
1563
|
+
answerSource: meta.answerSource,
|
|
1564
|
+
evidenceCount: meta.evidenceCount || 0,
|
|
1565
|
+
createdAt: a.createdAt,
|
|
1566
|
+
supersededBy: a.supersededBy
|
|
1567
|
+
};
|
|
1568
|
+
}).sort((a, b) => b.versionNumber - a.versionNumber);
|
|
1569
|
+
return history;
|
|
1570
|
+
}
|
|
1571
|
+
});
|
|
1572
|
+
var backfillAnswerNodes = internalMutation({
|
|
1573
|
+
args: {
|
|
1574
|
+
...scopeArgs,
|
|
1575
|
+
userId: v.string(),
|
|
1576
|
+
dryRun: v.optional(v.boolean())
|
|
1577
|
+
},
|
|
1578
|
+
returns: permissiveReturn,
|
|
1579
|
+
handler: async (ctx, args) => {
|
|
1580
|
+
const scope = await resolveScope(ctx, args);
|
|
1581
|
+
const topicId = scope?.topicId ?? args.topicId;
|
|
1582
|
+
const dryRun = args.dryRun ?? false;
|
|
1583
|
+
const questions = await ctx.db.query("epistemicNodes").withIndex(
|
|
1584
|
+
"by_topic_type",
|
|
1585
|
+
(q) => q.eq("topicId", topicId).eq("nodeType", "question")
|
|
1586
|
+
).collect();
|
|
1587
|
+
const questionsWithAnswers = questions.filter((q) => {
|
|
1588
|
+
const meta = q.metadata || {};
|
|
1589
|
+
const answer = meta.answer;
|
|
1590
|
+
return answer && answer.trim().length > 0;
|
|
1591
|
+
});
|
|
1592
|
+
const existingAnswers = await ctx.db.query("epistemicNodes").withIndex(
|
|
1593
|
+
"by_topic_type",
|
|
1594
|
+
(q) => q.eq("topicId", topicId).eq("nodeType", "answer")
|
|
1595
|
+
).collect();
|
|
1596
|
+
const questionIdsWithAnswerNodes = new Set(
|
|
1597
|
+
existingAnswers.map((a) => {
|
|
1598
|
+
const meta = a.metadata || {};
|
|
1599
|
+
return meta.questionNodeId;
|
|
1600
|
+
})
|
|
1601
|
+
);
|
|
1602
|
+
const needsBackfill = questionsWithAnswers.filter(
|
|
1603
|
+
(q) => !questionIdsWithAnswerNodes.has(String(q._id))
|
|
1604
|
+
);
|
|
1605
|
+
console.log(
|
|
1606
|
+
`[backfillAnswerNodes] Found ${questionsWithAnswers.length} questions with answers, ${questionIdsWithAnswerNodes.size} already have answer nodes, ${needsBackfill.length} need backfill. dryRun=${dryRun}`
|
|
1607
|
+
);
|
|
1608
|
+
if (dryRun) {
|
|
1609
|
+
return {
|
|
1610
|
+
questionsWithAnswers: questionsWithAnswers.length,
|
|
1611
|
+
alreadyHaveNodes: questionIdsWithAnswerNodes.size,
|
|
1612
|
+
needsBackfill: needsBackfill.length,
|
|
1613
|
+
questions: needsBackfill.map((q) => ({
|
|
1614
|
+
id: q._id,
|
|
1615
|
+
globalId: q.globalId,
|
|
1616
|
+
text: q.canonicalText?.slice(0, 80),
|
|
1617
|
+
answer: q.metadata?.answer?.slice(0, 80)
|
|
1618
|
+
}))
|
|
1619
|
+
};
|
|
1620
|
+
}
|
|
1621
|
+
let created = 0;
|
|
1622
|
+
for (const question of needsBackfill) {
|
|
1623
|
+
const meta = question.metadata || {};
|
|
1624
|
+
const answerText = meta.answer;
|
|
1625
|
+
await ctx.scheduler.runAfter(
|
|
1626
|
+
0,
|
|
1627
|
+
internal.epistemicAnswers.createInternal,
|
|
1628
|
+
{
|
|
1629
|
+
topicId: String(topicId),
|
|
1630
|
+
// TC-C: migrated from projectId
|
|
1631
|
+
questionNodeId: question._id,
|
|
1632
|
+
answerText,
|
|
1633
|
+
answerSource: meta.answeredBy ? "human" : "ai_generated",
|
|
1634
|
+
userId: meta.answeredBy || args.userId,
|
|
1635
|
+
backfilledFromQuestionId: String(question._id)
|
|
1636
|
+
}
|
|
1637
|
+
);
|
|
1638
|
+
created++;
|
|
1639
|
+
}
|
|
1640
|
+
return {
|
|
1641
|
+
questionsWithAnswers: questionsWithAnswers.length,
|
|
1642
|
+
alreadyHaveNodes: questionIdsWithAnswerNodes.size,
|
|
1643
|
+
backfilled: created
|
|
1644
|
+
};
|
|
1645
|
+
}
|
|
1646
|
+
});
|
|
1647
|
+
|
|
1648
|
+
export { backfillAnswerNodes, create, createInternal, getByQuestion, getLatestForQuestion, getVersionHistory };
|
|
1649
|
+
//# sourceMappingURL=epistemicAnswers.js.map
|
|
1650
|
+
//# sourceMappingURL=epistemicAnswers.js.map
|