@lucern/sdk 1.0.10 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/README.md +35 -0
- package/dist/.generated +2 -0
- package/dist/accessControl.d.ts +19 -26
- package/dist/accessControl.js +195 -1423
- package/dist/adminClient.d.ts +52 -59
- package/dist/adminClient.js +364 -1142
- package/dist/answersClient.d.ts +5 -14
- package/dist/answersClient.js +19 -737
- package/dist/audience/index.d.ts +18 -18
- package/dist/audience/index.js +87 -90
- package/dist/audiencesClient.d.ts +19 -27
- package/dist/audiencesClient.js +107 -868
- package/dist/auditClient.d.ts +8 -15
- package/dist/auditClient.js +18 -791
- package/dist/authContext.d.ts +11 -16
- package/dist/authContext.js +122 -154
- package/dist/authDeviceClient.d.ts +8 -17
- package/dist/authDeviceClient.js +113 -102
- package/dist/beliefs/index.d.ts +15 -67
- package/dist/beliefs/index.js +17 -10172
- package/dist/beliefs/lifecycle.d.ts +10 -11
- package/dist/beliefs/lifecycle.js +78 -80
- package/dist/beliefsClient.d.ts +26 -32
- package/dist/beliefsClient.js +250 -990
- package/dist/boundaryClientSurface.d.ts +11 -16
- package/dist/boundaryClientSurface.js +49 -68
- package/dist/client.d.ts +73 -112
- package/dist/client.js +232 -10146
- package/dist/clientAssemblyTypes.d.ts +3 -3
- package/dist/clientAssemblyTypes.js +1 -2
- package/dist/clientConfig.d.ts +45 -59
- package/dist/clientConfig.js +1 -2
- package/dist/clientEvidenceCompat.d.ts +7 -14
- package/dist/clientEvidenceCompat.js +50 -64
- package/dist/clientGraphNamespaces.d.ts +3 -5
- package/dist/clientGraphNamespaces.js +170 -245
- package/dist/clientHelpers.d.ts +20 -25
- package/dist/clientHelpers.js +104 -127
- package/dist/clientKnowledgeNamespaces.d.ts +6 -53
- package/dist/clientKnowledgeNamespaces.js +502 -506
- package/dist/clientLocalHelpers.d.ts +11 -56
- package/dist/clientLocalHelpers.js +503 -732
- package/dist/clientPlatformNamespaces.d.ts +14 -53
- package/dist/clientPlatformNamespaces.js +229 -314
- package/dist/clientRuntime.d.ts +5 -53
- package/dist/clientRuntime.js +26 -30
- package/dist/clientWorkflowNamespaces.d.ts +6 -15
- package/dist/clientWorkflowNamespaces.js +529 -596
- package/dist/contextClient.d.ts +9 -17
- package/dist/contextClient.js +92 -805
- package/dist/contextFacade.d.ts +11 -2
- package/dist/contextFacade.js +10 -81
- package/dist/contextPackCompiler.d.ts +10 -11
- package/dist/contextPackCompiler.js +494 -1040
- package/dist/contextPackPolicy.d.ts +14 -15
- package/dist/contextPackPolicy.js +227 -305
- package/dist/contextPackSchema.d.ts +3 -3
- package/dist/contextPackSchema.js +169 -176
- package/dist/contextTypes.d.ts +14 -15
- package/dist/contextTypes.js +1 -2
- package/dist/contracts/api-enums.contract.d.ts +29 -30
- package/dist/contracts/api-enums.contract.js +162 -88
- package/dist/contracts/auth-session.contract.d.ts +13 -14
- package/dist/contracts/auth-session.contract.js +55 -52
- package/dist/contracts/context-pack.contract.d.ts +54 -55
- package/dist/contracts/context-pack.contract.js +160 -88
- package/dist/contracts/contextPack.d.ts +2 -1
- package/dist/contracts/contextPack.js +1 -97
- package/dist/contracts/index.d.ts +11 -12
- package/dist/contracts/index.js +10 -854
- package/dist/contracts/lens-filter.contract.d.ts +9 -10
- package/dist/contracts/lens-filter.contract.js +82 -58
- package/dist/contracts/lens-workflow.contract.d.ts +21 -23
- package/dist/contracts/lens-workflow.contract.js +48 -117
- package/dist/contracts/lensFilter.d.ts +2 -1
- package/dist/contracts/lensFilter.js +1 -71
- package/dist/contracts/lensWorkflow.d.ts +2 -2
- package/dist/contracts/lensWorkflow.js +1 -123
- package/dist/contracts/mcpTools.d.ts +16 -18
- package/dist/contracts/mcpTools.js +89 -123
- package/dist/contracts/prompt.contract.d.ts +4 -5
- package/dist/contracts/prompt.contract.js +23 -10
- package/dist/contracts/prompt.d.ts +2 -1
- package/dist/contracts/prompt.js +1 -11
- package/dist/contracts/sdk-tools.contract.d.ts +2 -1
- package/dist/contracts/sdk-tools.contract.js +1 -2
- package/dist/contracts/sdkTools.d.ts +2 -1
- package/dist/contracts/sdkTools.js +1 -26
- package/dist/contracts/tool-contracts.d.ts +2 -1
- package/dist/contracts/tool-contracts.js +1 -2
- package/dist/contracts/workflow-runtime.contract.d.ts +45 -46
- package/dist/contracts/workflow-runtime.contract.js +241 -228
- package/dist/contracts/workflowRuntime.d.ts +2 -1
- package/dist/contracts/workflowRuntime.js +1 -244
- package/dist/contradictions/index.d.ts +8 -60
- package/dist/contradictions/index.js +11 -10166
- package/dist/control-plane.d.ts +17 -24
- package/dist/control-plane.js +124 -840
- package/dist/controlObjectOwnership.d.ts +19 -20
- package/dist/controlObjectOwnership.js +207 -201
- package/dist/coreClient.d.ts +23 -28
- package/dist/coreClient.js +567 -692
- package/dist/customTools.d.ts +17 -21
- package/dist/customTools.js +221 -221
- package/dist/decisions/index.d.ts +7 -58
- package/dist/decisions/index.js +14 -10168
- package/dist/decisionsClient.d.ts +25 -32
- package/dist/decisionsClient.js +113 -913
- package/dist/domainContext.d.ts +2 -1
- package/dist/domainContext.js +1 -2
- package/dist/edges/index.d.ts +21 -73
- package/dist/edges/index.js +12 -10167
- package/dist/embeddingsClient.d.ts +22 -30
- package/dist/embeddingsClient.js +73 -922
- package/dist/eventingClient.d.ts +23 -31
- package/dist/eventingClient.js +89 -918
- package/dist/events.d.ts +48 -49
- package/dist/events.js +257 -241
- package/dist/eventsCore.d.ts +20 -29
- package/dist/eventsCore.js +86 -830
- package/dist/evidence/index.d.ts +9 -60
- package/dist/evidence/index.js +13 -10167
- package/dist/evidenceClient.d.ts +13 -22
- package/dist/evidenceClient.js +34 -751
- package/dist/facade/context.d.ts +7 -8
- package/dist/facade/context.js +73 -72
- package/dist/functionSurface.d.ts +2 -156
- package/dist/functionSurface.js +1 -1460
- package/dist/functionSurfaceClient.d.ts +2 -9
- package/dist/functionSurfaceClient.js +1 -1460
- package/dist/gatewayFacades.d.ts +79 -296
- package/dist/gatewayFacades.factories.d.ts +209 -14
- package/dist/gatewayFacades.factories.js +561 -2227
- package/dist/gatewayFacades.js +284 -2627
- package/dist/generated/functionSurface.d.ts +149 -0
- package/dist/generated/functionSurface.js +749 -0
- package/dist/graphAnalysisClient.d.ts +41 -49
- package/dist/graphAnalysisClient.js +185 -974
- package/dist/graphClient.d.ts +53 -60
- package/dist/graphClient.js +219 -1090
- package/dist/graphIntel.d.ts +2 -4
- package/dist/graphIntel.js +1 -2
- package/dist/graphIntelligence.d.ts +4 -2
- package/dist/graphIntelligence.js +2 -46
- package/dist/graphRecommendationsClient.d.ts +15 -23
- package/dist/graphRecommendationsClient.js +70 -849
- package/dist/graphStateClassifierClient.d.ts +17 -25
- package/dist/graphStateClassifierClient.js +67 -908
- package/dist/harnessClient.d.ts +40 -47
- package/dist/harnessClient.js +198 -993
- package/dist/identityClient.d.ts +25 -33
- package/dist/identityClient.js +245 -1186
- package/dist/index.d.ts +73 -69
- package/dist/index.js +72 -13304
- package/dist/infisicalRuntime.d.ts +12 -14
- package/dist/infisicalRuntime.js +290 -297
- package/dist/jobsClient.d.ts +24 -32
- package/dist/jobsClient.js +101 -916
- package/dist/learningClient.d.ts +8 -16
- package/dist/learningClient.js +45 -809
- package/dist/lenses/index.d.ts +13 -65
- package/dist/lenses/index.js +11 -10166
- package/dist/mcpClient.d.ts +14 -23
- package/dist/mcpClient.js +115 -856
- package/dist/modelRuntimeClient.d.ts +18 -26
- package/dist/modelRuntimeClient.js +74 -894
- package/dist/nodes/index.d.ts +7 -58
- package/dist/nodes/index.js +14 -10168
- package/dist/ontologies/index.d.ts +21 -73
- package/dist/ontologies/index.js +14 -10169
- package/dist/ontologyClient.d.ts +23 -31
- package/dist/ontologyClient.js +138 -924
- package/dist/ontologyLinksClient.d.ts +16 -24
- package/dist/ontologyLinksClient.js +76 -886
- package/dist/opinion.d.ts +5 -6
- package/dist/opinion.js +21 -25
- package/dist/orgGraphSearchClient.d.ts +19 -27
- package/dist/orgGraphSearchClient.js +89 -857
- package/dist/packRuntime.d.ts +2 -2
- package/dist/packRuntime.js +1 -2
- package/dist/packsClient.d.ts +30 -37
- package/dist/packsClient.js +131 -906
- package/dist/policyClient.d.ts +21 -29
- package/dist/policyClient.js +267 -1026
- package/dist/proof-attestation.json +1 -1
- package/dist/questions/index.d.ts +9 -60
- package/dist/questions/index.js +15 -10169
- package/dist/realtime/index.d.ts +20 -16
- package/dist/realtime/index.js +30 -19
- package/dist/realtime/refs.d.ts +4 -6
- package/dist/realtime/refs.js +12 -7
- package/dist/realtime-refs.d.ts +1 -0
- package/dist/realtime-refs.js +1 -0
- package/dist/realtime.d.ts +1 -0
- package/dist/realtime.js +1 -0
- package/dist/reportsClient.d.ts +10 -19
- package/dist/reportsClient.js +48 -836
- package/dist/schemaClient.d.ts +16 -23
- package/dist/schemaClient.js +62 -832
- package/dist/sdkSurface.d.ts +18 -25
- package/dist/sdkSurface.js +135 -106
- package/dist/secrets.d.ts +2 -1
- package/dist/secrets.js +1 -2
- package/dist/sourcesClient.d.ts +11 -18
- package/dist/sourcesClient.js +18 -741
- package/dist/telemetryClient.d.ts +22 -30
- package/dist/telemetryClient.js +107 -931
- package/dist/toolRegistryClient.d.ts +27 -35
- package/dist/toolRegistryClient.js +116 -954
- package/dist/topics/index.d.ts +13 -64
- package/dist/topics/index.js +15 -10169
- package/dist/topicsClient.d.ts +19 -27
- package/dist/topicsClient.js +106 -894
- package/dist/types.d.ts +84 -87
- package/dist/types.js +1 -2
- package/dist/version.d.ts +2 -3
- package/dist/version.js +2 -5
- package/dist/workflowClient.d.ts +60 -65
- package/dist/workflowClient.js +343 -1219
- package/dist/worktrees/index.d.ts +16 -68
- package/dist/worktrees/index.js +14 -10169
- package/package.json +6 -6
- package/dist/accessControl.js.map +0 -1
- package/dist/adminClient.js.map +0 -1
- package/dist/answersClient.js.map +0 -1
- package/dist/audience/index.js.map +0 -1
- package/dist/audiencesClient.js.map +0 -1
- package/dist/auditClient.js.map +0 -1
- package/dist/authContext.js.map +0 -1
- package/dist/authDeviceClient.js.map +0 -1
- package/dist/beliefs/index.js.map +0 -1
- package/dist/beliefs/lifecycle.js.map +0 -1
- package/dist/beliefsClient.js.map +0 -1
- package/dist/boundaryClientSurface.js.map +0 -1
- package/dist/client.js.map +0 -1
- package/dist/clientAssemblyTypes.js.map +0 -1
- package/dist/clientConfig.js.map +0 -1
- package/dist/clientEvidenceCompat.js.map +0 -1
- package/dist/clientGraphNamespaces.js.map +0 -1
- package/dist/clientHelpers.js.map +0 -1
- package/dist/clientKnowledgeNamespaces.js.map +0 -1
- package/dist/clientLocalHelpers.js.map +0 -1
- package/dist/clientPlatformNamespaces.js.map +0 -1
- package/dist/clientRuntime.js.map +0 -1
- package/dist/clientWorkflowNamespaces.js.map +0 -1
- package/dist/contextClient.js.map +0 -1
- package/dist/contextFacade.js.map +0 -1
- package/dist/contextPackCompiler.js.map +0 -1
- package/dist/contextPackPolicy.js.map +0 -1
- package/dist/contextPackSchema.js.map +0 -1
- package/dist/contextTypes.js.map +0 -1
- package/dist/contracts/api-enums.contract.js.map +0 -1
- package/dist/contracts/auth-session.contract.js.map +0 -1
- package/dist/contracts/context-pack.contract.js.map +0 -1
- package/dist/contracts/contextPack.js.map +0 -1
- package/dist/contracts/index.js.map +0 -1
- package/dist/contracts/lens-filter.contract.js.map +0 -1
- package/dist/contracts/lens-workflow.contract.js.map +0 -1
- package/dist/contracts/lensFilter.js.map +0 -1
- package/dist/contracts/lensWorkflow.js.map +0 -1
- package/dist/contracts/mcpTools.js.map +0 -1
- package/dist/contracts/prompt.contract.js.map +0 -1
- package/dist/contracts/prompt.js.map +0 -1
- package/dist/contracts/sdk-tools.contract.js.map +0 -1
- package/dist/contracts/sdkTools.js.map +0 -1
- package/dist/contracts/tool-contracts.js.map +0 -1
- package/dist/contracts/workflow-runtime.contract.js.map +0 -1
- package/dist/contracts/workflowRuntime.js.map +0 -1
- package/dist/contradictions/index.js.map +0 -1
- package/dist/control-plane.js.map +0 -1
- package/dist/controlObjectOwnership.js.map +0 -1
- package/dist/coreClient.js.map +0 -1
- package/dist/customTools.js.map +0 -1
- package/dist/decisions/index.js.map +0 -1
- package/dist/decisionsClient.js.map +0 -1
- package/dist/domainContext.js.map +0 -1
- package/dist/edges/index.js.map +0 -1
- package/dist/embeddingsClient.js.map +0 -1
- package/dist/eventingClient.js.map +0 -1
- package/dist/events.js.map +0 -1
- package/dist/eventsCore.js.map +0 -1
- package/dist/evidence/index.js.map +0 -1
- package/dist/evidenceClient.js.map +0 -1
- package/dist/facade/context.js.map +0 -1
- package/dist/functionSurface.js.map +0 -1
- package/dist/functionSurfaceClient.js.map +0 -1
- package/dist/gatewayFacades.factories.js.map +0 -1
- package/dist/gatewayFacades.js.map +0 -1
- package/dist/graphAnalysisClient.js.map +0 -1
- package/dist/graphClient.js.map +0 -1
- package/dist/graphIntel.js.map +0 -1
- package/dist/graphIntelligence.js.map +0 -1
- package/dist/graphRecommendationsClient.js.map +0 -1
- package/dist/graphStateClassifierClient.js.map +0 -1
- package/dist/harnessClient.js.map +0 -1
- package/dist/identityClient.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/infisicalRuntime.js.map +0 -1
- package/dist/jobsClient.js.map +0 -1
- package/dist/learningClient.js.map +0 -1
- package/dist/lenses/index.js.map +0 -1
- package/dist/mcpClient.js.map +0 -1
- package/dist/modelRuntimeClient.js.map +0 -1
- package/dist/nodes/index.js.map +0 -1
- package/dist/ontologies/index.js.map +0 -1
- package/dist/ontologyClient.js.map +0 -1
- package/dist/ontologyLinksClient.js.map +0 -1
- package/dist/opinion.js.map +0 -1
- package/dist/orgGraphSearchClient.js.map +0 -1
- package/dist/packRuntime.js.map +0 -1
- package/dist/packsClient.js.map +0 -1
- package/dist/policyClient.js.map +0 -1
- package/dist/questions/index.js.map +0 -1
- package/dist/realtime/index.js.map +0 -1
- package/dist/realtime/refs.js.map +0 -1
- package/dist/reportsClient.js.map +0 -1
- package/dist/schemaClient.js.map +0 -1
- package/dist/sdk-tools.contract-B4c1Zr1o.d.ts +0 -22
- package/dist/sdkSurface.js.map +0 -1
- package/dist/secrets.js.map +0 -1
- package/dist/sourcesClient.js.map +0 -1
- package/dist/telemetryClient.js.map +0 -1
- package/dist/tool-contracts-BUiL9P6z.d.ts +0 -22
- package/dist/toolRegistryClient.js.map +0 -1
- package/dist/topics/index.js.map +0 -1
- package/dist/topicsClient.js.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/version.js.map +0 -1
- package/dist/workflowClient.js.map +0 -1
- package/dist/worktrees/index.js.map +0 -1
package/dist/beliefsClient.js
CHANGED
|
@@ -1,1010 +1,270 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import { Effect, Exit, Cause } from 'effect';
|
|
5
|
-
|
|
6
|
-
// src/coreClient.ts
|
|
7
|
-
|
|
8
|
-
// src/authContext.ts
|
|
9
|
-
var LucernSdkAuthContextError = class extends Error {
|
|
10
|
-
reason;
|
|
11
|
-
constructor(reason, message) {
|
|
12
|
-
super(message);
|
|
13
|
-
this.name = "LucernSdkAuthContextError";
|
|
14
|
-
this.reason = reason;
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
function cleanString(value) {
|
|
18
|
-
const normalized = value?.trim();
|
|
19
|
-
return normalized ? normalized : void 0;
|
|
20
|
-
}
|
|
21
|
-
function cleanStringList(values) {
|
|
22
|
-
if (!values) {
|
|
23
|
-
return [];
|
|
24
|
-
}
|
|
25
|
-
return values.map((value) => value.trim()).filter(
|
|
26
|
-
(value, index, list) => value.length > 0 && list.indexOf(value) === index
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
|
-
function requireString(value, reason, label) {
|
|
30
|
-
const normalized = cleanString(value);
|
|
31
|
-
if (!normalized) {
|
|
32
|
-
throw new LucernSdkAuthContextError(
|
|
33
|
-
reason,
|
|
34
|
-
`Canonical Lucern SDK auth context is missing ${label}.`
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
return normalized;
|
|
38
|
-
}
|
|
39
|
-
function requirePrincipalType(principalType) {
|
|
40
|
-
if (!principalType) {
|
|
41
|
-
throw new LucernSdkAuthContextError(
|
|
42
|
-
"principal_missing",
|
|
43
|
-
"Canonical Lucern SDK auth context is missing principalType."
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
return principalType;
|
|
47
|
-
}
|
|
48
|
-
function requireAuthMode(authMode) {
|
|
49
|
-
if (!authMode) {
|
|
50
|
-
throw new LucernSdkAuthContextError(
|
|
51
|
-
"principal_missing",
|
|
52
|
-
"Canonical Lucern SDK auth context is missing authMode."
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
return authMode;
|
|
56
|
-
}
|
|
57
|
-
function ensurePermitMatch(args) {
|
|
58
|
-
const actual = cleanString(args.actual);
|
|
59
|
-
if (actual && actual !== args.expected) {
|
|
60
|
-
throw new LucernSdkAuthContextError(
|
|
61
|
-
"policy_denied",
|
|
62
|
-
`Canonical Lucern SDK auth context has conflicting Permit ${args.field}.`
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
function normalizeCanonicalLucernAuthContext(input) {
|
|
67
|
-
if (!input) {
|
|
68
|
-
throw new LucernSdkAuthContextError(
|
|
69
|
-
"principal_missing",
|
|
70
|
-
"Canonical Lucern SDK auth context is required."
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
if (input.policyDecision === "deny") {
|
|
74
|
-
throw new LucernSdkAuthContextError(
|
|
75
|
-
"policy_denied",
|
|
76
|
-
"Canonical Lucern SDK auth context carries a denied policy decision."
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
const principalId = requireString(
|
|
80
|
-
input.principalId,
|
|
81
|
-
"principal_missing",
|
|
82
|
-
"principalId"
|
|
83
|
-
);
|
|
84
|
-
const tenantId = requireString(input.tenantId, "tenant_missing", "tenantId");
|
|
85
|
-
const workspaceId = requireString(
|
|
86
|
-
input.workspaceId,
|
|
87
|
-
"workspace_missing",
|
|
88
|
-
"workspaceId"
|
|
89
|
-
);
|
|
90
|
-
const roles = cleanStringList(input.roles);
|
|
91
|
-
const scopes = cleanStringList(input.scopes);
|
|
92
|
-
const principalType = requirePrincipalType(input.principalType);
|
|
93
|
-
const authMode = requireAuthMode(input.authMode);
|
|
94
|
-
const roleBasedInteractiveAuth = authMode === "interactive_user" && roles.length > 0;
|
|
95
|
-
if (roles.length === 0 || scopes.length === 0 && !roleBasedInteractiveAuth) {
|
|
96
|
-
throw new LucernSdkAuthContextError(
|
|
97
|
-
"membership_missing",
|
|
98
|
-
"Canonical Lucern SDK auth context requires non-empty roles and scopes."
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
const subject = cleanString(input.permit?.subject) ?? principalId;
|
|
102
|
-
const tenant = cleanString(input.permit?.tenant) ?? tenantId;
|
|
103
|
-
const workspace = cleanString(input.permit?.workspace) ?? workspaceId;
|
|
104
|
-
ensurePermitMatch({
|
|
105
|
-
field: "subject",
|
|
106
|
-
expected: principalId,
|
|
107
|
-
actual: subject
|
|
108
|
-
});
|
|
109
|
-
ensurePermitMatch({ field: "tenant", expected: tenantId, actual: tenant });
|
|
110
|
-
ensurePermitMatch({
|
|
111
|
-
field: "workspace",
|
|
112
|
-
expected: workspaceId,
|
|
113
|
-
actual: workspace
|
|
114
|
-
});
|
|
115
|
-
const context = input.permit?.context ? { ...input.permit.context } : void 0;
|
|
116
|
-
return {
|
|
117
|
-
clerkId: cleanString(input.clerkId),
|
|
118
|
-
principalId,
|
|
119
|
-
tenantId,
|
|
120
|
-
workspaceId,
|
|
121
|
-
principalType,
|
|
122
|
-
authMode,
|
|
123
|
-
roles,
|
|
124
|
-
scopes,
|
|
125
|
-
delegationChain: input.delegationChain ? [...input.delegationChain] : [],
|
|
126
|
-
policyTraceId: cleanString(input.policyTraceId),
|
|
127
|
-
correlationId: cleanString(input.correlationId),
|
|
128
|
-
membershipId: cleanString(input.membershipId),
|
|
129
|
-
permit: {
|
|
130
|
-
subject,
|
|
131
|
-
tenant,
|
|
132
|
-
workspace,
|
|
133
|
-
resource: cleanString(input.permit?.resource),
|
|
134
|
-
action: cleanString(input.permit?.action),
|
|
135
|
-
relation: cleanString(input.permit?.relation),
|
|
136
|
-
context
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
function createCanonicalAuthHeaders(authContext) {
|
|
141
|
-
const headers = {
|
|
142
|
-
"x-lucern-principal-id": authContext.principalId,
|
|
143
|
-
"x-lucern-principal-type": authContext.principalType,
|
|
144
|
-
"x-lucern-tenant": authContext.tenantId,
|
|
145
|
-
"x-lucern-tenant-id": authContext.tenantId,
|
|
146
|
-
"x-lucern-workspace": authContext.workspaceId,
|
|
147
|
-
"x-lucern-workspace-id": authContext.workspaceId,
|
|
148
|
-
"x-lucern-auth-mode": authContext.authMode,
|
|
149
|
-
"x-lucern-roles": authContext.roles.join(","),
|
|
150
|
-
"x-lucern-scopes": authContext.scopes.join(","),
|
|
151
|
-
"x-lucern-permit-context": JSON.stringify(authContext.permit)
|
|
152
|
-
};
|
|
153
|
-
if (authContext.clerkId) {
|
|
154
|
-
headers["x-lucern-clerk-id"] = authContext.clerkId;
|
|
155
|
-
headers["x-lucern-user-id"] = authContext.clerkId;
|
|
156
|
-
}
|
|
157
|
-
if (authContext.delegationChain.length > 0) {
|
|
158
|
-
headers["x-lucern-delegation-chain"] = JSON.stringify(
|
|
159
|
-
authContext.delegationChain
|
|
160
|
-
);
|
|
161
|
-
}
|
|
162
|
-
if (authContext.policyTraceId) {
|
|
163
|
-
headers["x-lucern-policy-trace-id"] = authContext.policyTraceId;
|
|
164
|
-
}
|
|
165
|
-
if (authContext.correlationId) {
|
|
166
|
-
headers["x-correlation-id"] = authContext.correlationId;
|
|
167
|
-
headers["x-lucern-correlation-id"] = authContext.correlationId;
|
|
168
|
-
}
|
|
169
|
-
if (authContext.membershipId) {
|
|
170
|
-
headers["x-lucern-membership-id"] = authContext.membershipId;
|
|
171
|
-
}
|
|
172
|
-
return headers;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// src/coreClient.ts
|
|
176
|
-
var DEFAULT_GATEWAY_TIMEOUT_MS = 15e3;
|
|
177
|
-
var DEFAULT_GATEWAY_MAX_RETRIES = 2;
|
|
178
|
-
var DEFAULT_ENV_TIMEOUT_MS = "LUCERN_REQUEST_TIMEOUT_MS";
|
|
179
|
-
var DEFAULT_ENV_MAX_RETRIES = "LUCERN_GATEWAY_MAX_RETRIES";
|
|
180
|
-
var ENV_TIMEOUT_BY_METHOD_PREFIX = "LUCERN_REQUEST_TIMEOUT_MS_";
|
|
181
|
-
var GatewayTimeoutError = class extends Error {
|
|
182
|
-
retryable = true;
|
|
183
|
-
timeoutMs;
|
|
184
|
-
constructor(timeoutMs) {
|
|
185
|
-
super(`Request timed out after ${timeoutMs}ms`);
|
|
186
|
-
this.name = "AbortError";
|
|
187
|
-
this.timeoutMs = timeoutMs;
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
var GatewayTransportError = class extends Error {
|
|
191
|
-
retryable;
|
|
192
|
-
cause;
|
|
193
|
-
constructor(message, options) {
|
|
194
|
-
super(message);
|
|
195
|
-
this.name = "GatewayTransportError";
|
|
196
|
-
this.retryable = options?.retryable ?? true;
|
|
197
|
-
this.cause = options?.cause;
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
function isGatewayRetryableError(error) {
|
|
201
|
-
return error instanceof GatewayTimeoutError && error.retryable || error instanceof GatewayTransportError && error.retryable || false;
|
|
202
|
-
}
|
|
203
|
-
var LucernApiError = class extends Error {
|
|
204
|
-
code;
|
|
205
|
-
status;
|
|
206
|
-
invariant;
|
|
207
|
-
suggestion;
|
|
208
|
-
details;
|
|
209
|
-
requestId;
|
|
210
|
-
correlationId;
|
|
211
|
-
policyTraceId;
|
|
212
|
-
constructor(args) {
|
|
213
|
-
super(args.message);
|
|
214
|
-
this.name = "LucernApiError";
|
|
215
|
-
this.code = args.code;
|
|
216
|
-
this.status = args.status;
|
|
217
|
-
this.invariant = args.invariant;
|
|
218
|
-
this.suggestion = args.suggestion;
|
|
219
|
-
this.details = args.details;
|
|
220
|
-
this.requestId = args.requestId;
|
|
221
|
-
this.correlationId = args.correlationId;
|
|
222
|
-
this.policyTraceId = args.policyTraceId;
|
|
223
|
-
}
|
|
224
|
-
};
|
|
225
|
-
function fillRandomBytes(length) {
|
|
226
|
-
const bytes = new Uint8Array(length);
|
|
227
|
-
if (typeof globalThis.crypto?.getRandomValues === "function") {
|
|
228
|
-
globalThis.crypto.getRandomValues(bytes);
|
|
229
|
-
return bytes;
|
|
230
|
-
}
|
|
231
|
-
for (let index = 0; index < length; index += 1) {
|
|
232
|
-
bytes[index] = Math.floor(Math.random() * 256);
|
|
233
|
-
}
|
|
234
|
-
return bytes;
|
|
235
|
-
}
|
|
236
|
-
function generatePortableRequestId() {
|
|
237
|
-
if (typeof globalThis.crypto?.randomUUID === "function") {
|
|
238
|
-
return globalThis.crypto.randomUUID();
|
|
239
|
-
}
|
|
240
|
-
const bytes = fillRandomBytes(16);
|
|
241
|
-
bytes[6] = bytes[6] & 15 | 64;
|
|
242
|
-
bytes[8] = bytes[8] & 63 | 128;
|
|
243
|
-
const hex = Array.from(bytes, (value) => value.toString(16).padStart(2, "0"));
|
|
244
|
-
return `${hex.slice(0, 4).join("")}-${hex.slice(4, 6).join("")}-${hex.slice(
|
|
245
|
-
6,
|
|
246
|
-
8
|
|
247
|
-
).join("")}-${hex.slice(8, 10).join("")}-${hex.slice(10).join("")}`;
|
|
248
|
-
}
|
|
249
|
-
function resolveEnvironment() {
|
|
250
|
-
const processEnv = typeof globalThis === "object" && globalThis !== null && "process" in globalThis ? globalThis.process : void 0;
|
|
251
|
-
const env = processEnv !== void 0 && typeof processEnv === "object" && processEnv !== null && typeof processEnv.env === "object" ? processEnv.env : void 0;
|
|
252
|
-
return {
|
|
253
|
-
get: (name) => {
|
|
254
|
-
const value = env?.[name];
|
|
255
|
-
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
256
|
-
}
|
|
257
|
-
};
|
|
258
|
-
}
|
|
259
|
-
function telemetryEnvironmentRecord(environment) {
|
|
260
|
-
const names = [
|
|
261
|
-
"LUCERN_TELEMETRY_ENABLED",
|
|
262
|
-
"AXIOM_TELEMETRY_ENABLED",
|
|
263
|
-
"LUCERN_AXIOM_TOKEN",
|
|
264
|
-
"AXIOM_TOKEN",
|
|
265
|
-
"LUCERN_AXIOM_EVENTS_DATASET",
|
|
266
|
-
"LUCERN_AXIOM_DATASET",
|
|
267
|
-
"AXIOM_EVENTS_DATASET",
|
|
268
|
-
"AXIOM_DATASET",
|
|
269
|
-
"LUCERN_AXIOM_API_URL",
|
|
270
|
-
"AXIOM_URL",
|
|
271
|
-
"LUCERN_ENVIRONMENT",
|
|
272
|
-
"NODE_ENV",
|
|
273
|
-
"LUCERN_RELEASE",
|
|
274
|
-
"SENTRY_RELEASE",
|
|
275
|
-
"VERCEL_GIT_COMMIT_SHA"
|
|
276
|
-
];
|
|
277
|
-
return Object.fromEntries(
|
|
278
|
-
names.map((name) => [name, environment.get(name)])
|
|
279
|
-
);
|
|
280
|
-
}
|
|
281
|
-
function resolveRequestProfile(config, environment) {
|
|
282
|
-
const requestIdFactory = config.requestIdFactory ?? (() => generatePortableRequestId());
|
|
283
|
-
const parsedMaxRetries = parseIntegerFromString(
|
|
284
|
-
config.maxRetries,
|
|
285
|
-
environment.get(DEFAULT_ENV_MAX_RETRIES)
|
|
286
|
-
);
|
|
287
|
-
const parsedTimeoutMs = parseIntegerFromString(
|
|
288
|
-
config.timeoutMs,
|
|
289
|
-
environment.get(DEFAULT_ENV_TIMEOUT_MS)
|
|
290
|
-
);
|
|
291
|
-
const methodTimeouts = {
|
|
292
|
-
...config.timeoutMsByMethod
|
|
293
|
-
};
|
|
294
|
-
for (const method of ["GET", "POST", "PUT", "PATCH", "DELETE"]) {
|
|
295
|
-
const envKey = `${ENV_TIMEOUT_BY_METHOD_PREFIX}${method}`;
|
|
296
|
-
const raw = environment.get(envKey);
|
|
297
|
-
if (!raw || methodTimeouts[method] !== void 0) {
|
|
298
|
-
continue;
|
|
299
|
-
}
|
|
300
|
-
const parsed = parseIntegerFromString(void 0, raw);
|
|
301
|
-
if (typeof parsed === "number") {
|
|
302
|
-
methodTimeouts[method] = parsed;
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
return {
|
|
306
|
-
maxRetries: parsedMaxRetries ?? DEFAULT_GATEWAY_MAX_RETRIES,
|
|
307
|
-
timeoutMs: parsedTimeoutMs ?? DEFAULT_GATEWAY_TIMEOUT_MS,
|
|
308
|
-
timeoutMsByMethod: methodTimeouts,
|
|
309
|
-
requestIdFactory
|
|
310
|
-
};
|
|
311
|
-
}
|
|
312
|
-
function createGatewayRuntime(config, environment) {
|
|
313
|
-
return {
|
|
314
|
-
fetch: config.fetchImpl ?? fetch,
|
|
315
|
-
now: () => Date.now(),
|
|
316
|
-
sleep: (ms) => delay(ms),
|
|
317
|
-
env: environment,
|
|
318
|
-
redaction: resolveRequestRedactionValue,
|
|
319
|
-
profile: resolveRequestProfile(config, environment)
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
function parseIntegerFromString(value, rawValue) {
|
|
323
|
-
if (typeof value === "number" && Number.isInteger(value) && value >= 0) {
|
|
324
|
-
return value;
|
|
325
|
-
}
|
|
326
|
-
if (typeof rawValue !== "string" || !rawValue.trim()) {
|
|
327
|
-
return void 0;
|
|
328
|
-
}
|
|
329
|
-
const parsed = Number.parseInt(rawValue, 10);
|
|
330
|
-
return Number.isInteger(parsed) && parsed >= 0 ? parsed : void 0;
|
|
331
|
-
}
|
|
332
|
-
function resolveRequestRedactionValue(value) {
|
|
333
|
-
return redactDiagnosticValue(value);
|
|
334
|
-
}
|
|
335
|
-
function resolveGatewayBaseUrl(configBaseUrl, environment) {
|
|
336
|
-
const envBaseUrl = environment.get("LUCERN_API_URL") ?? environment.get("LUCERN_BASE_URL") ?? environment.get("LUCERN_GATEWAY_BASE_URL");
|
|
337
|
-
return (configBaseUrl ?? envBaseUrl ?? "").replace(/\/+$/, "");
|
|
338
|
-
}
|
|
339
|
-
function normalizeGatewayEnvironment(value) {
|
|
340
|
-
return value === "sandbox" || value === "production" ? value : void 0;
|
|
341
|
-
}
|
|
342
|
-
var randomIdempotencyKey = generatePortableRequestId;
|
|
343
|
-
function fallbackErrorCode(status) {
|
|
344
|
-
if (status === 401) {
|
|
345
|
-
return "AUTHENTICATION_REQUIRED";
|
|
346
|
-
}
|
|
347
|
-
if (status === 403) {
|
|
348
|
-
return "FORBIDDEN";
|
|
349
|
-
}
|
|
350
|
-
if (status === 404) {
|
|
351
|
-
return "NOT_FOUND";
|
|
352
|
-
}
|
|
353
|
-
if (status === 408) {
|
|
354
|
-
return "UPSTREAM_ERROR";
|
|
355
|
-
}
|
|
356
|
-
if (status === 409) {
|
|
357
|
-
return "CONFLICT";
|
|
358
|
-
}
|
|
359
|
-
if (status === 429) {
|
|
360
|
-
return "RATE_LIMIT_EXCEEDED";
|
|
361
|
-
}
|
|
362
|
-
if (status >= 500) {
|
|
363
|
-
return "UPSTREAM_ERROR";
|
|
364
|
-
}
|
|
365
|
-
return "INTERNAL_ERROR";
|
|
366
|
-
}
|
|
367
|
-
function delay(ms) {
|
|
368
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
369
|
-
}
|
|
370
|
-
function computeRetryDelayMs(args) {
|
|
371
|
-
const baseDelay = args.status === 429 ? Math.max(
|
|
372
|
-
args.retryAfterMs ?? 0,
|
|
373
|
-
Math.min(1e3 * 2 ** args.attempt, 1e4)
|
|
374
|
-
) : Math.min(1e3 * 2 ** args.attempt, 4e3);
|
|
375
|
-
if (args.status !== 429) {
|
|
376
|
-
return baseDelay;
|
|
377
|
-
}
|
|
378
|
-
const jitterWindow = Math.max(250, Math.round(baseDelay * 0.25));
|
|
379
|
-
return baseDelay + Math.round(Math.random() * jitterWindow);
|
|
380
|
-
}
|
|
381
|
-
function classifyGatewayErrorForRetry(error) {
|
|
382
|
-
return isGatewayRetryableError(error) || classifyRetry({ error }).retryable;
|
|
383
|
-
}
|
|
384
|
-
function isRecord(value) {
|
|
385
|
-
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
386
|
-
}
|
|
387
|
-
function readPolicySummaryFromDetails(details) {
|
|
388
|
-
if (!isRecord(details)) {
|
|
389
|
-
return null;
|
|
390
|
-
}
|
|
391
|
-
const directSummary = details.summary;
|
|
392
|
-
if (typeof directSummary === "string" && directSummary.trim().length > 0) {
|
|
393
|
-
return directSummary.trim();
|
|
394
|
-
}
|
|
395
|
-
const policy = details.policy;
|
|
396
|
-
if (!isRecord(policy)) {
|
|
397
|
-
return null;
|
|
398
|
-
}
|
|
399
|
-
const explanation = policy.explanation;
|
|
400
|
-
if (!isRecord(explanation)) {
|
|
401
|
-
return null;
|
|
402
|
-
}
|
|
403
|
-
const nestedSummary = explanation.summary;
|
|
404
|
-
if (typeof nestedSummary === "string" && nestedSummary.trim().length > 0) {
|
|
405
|
-
return nestedSummary.trim();
|
|
406
|
-
}
|
|
407
|
-
return null;
|
|
408
|
-
}
|
|
409
|
-
function redactJsonDiagnosticValue(value) {
|
|
410
|
-
return value === void 0 ? void 0 : redactDiagnosticValue(value);
|
|
411
|
-
}
|
|
412
|
-
async function resolveConfiguredAuthContext(authContext) {
|
|
413
|
-
if (typeof authContext === "function") {
|
|
414
|
-
return await authContext();
|
|
415
|
-
}
|
|
416
|
-
return authContext;
|
|
417
|
-
}
|
|
418
|
-
function mergeHeaderRecord(base, addition) {
|
|
419
|
-
const headers = new Headers(base);
|
|
420
|
-
for (const [key, value] of Object.entries(addition)) {
|
|
421
|
-
const existing = headers.get(key);
|
|
422
|
-
if (existing !== null && existing !== value) {
|
|
423
|
-
throw new LucernSdkAuthContextError(
|
|
424
|
-
"policy_denied",
|
|
425
|
-
`Canonical Lucern SDK auth context conflicts with existing ${key} header.`
|
|
426
|
-
);
|
|
427
|
-
}
|
|
428
|
-
headers.set(key, value);
|
|
429
|
-
}
|
|
430
|
-
return Object.fromEntries(headers.entries());
|
|
431
|
-
}
|
|
432
|
-
function cleanHeaderValue(value) {
|
|
433
|
-
const normalized = value?.trim();
|
|
434
|
-
return normalized ? normalized : void 0;
|
|
435
|
-
}
|
|
436
|
-
function createGatewayRequestClient(config = {}) {
|
|
437
|
-
const env = resolveEnvironment();
|
|
438
|
-
const runtime = createGatewayRuntime(config, env);
|
|
439
|
-
const baseUrl = resolveGatewayBaseUrl(config.baseUrl, env);
|
|
440
|
-
const maxRetries = runtime.profile.maxRetries;
|
|
441
|
-
const requestIdFactory = runtime.profile.requestIdFactory;
|
|
442
|
-
const requestTimeoutByMethod = runtime.profile.timeoutMsByMethod;
|
|
443
|
-
const defaultRequestTimeoutMs = runtime.profile.timeoutMs;
|
|
444
|
-
const normalizedEnvironment = normalizeGatewayEnvironment(config.environment);
|
|
445
|
-
const telemetryExporter = config.telemetryEnabled === false ? null : config.telemetryExporter ?? createTelemetryExporterFromEnv(telemetryEnvironmentRecord(env), {
|
|
446
|
-
service: "lucern-sdk",
|
|
447
|
-
environment: normalizedEnvironment
|
|
448
|
-
});
|
|
449
|
-
async function resolveAuthHeaders() {
|
|
450
|
-
const provided = config.getAuthHeaders ? await config.getAuthHeaders() : {};
|
|
451
|
-
const headers = new Headers(provided);
|
|
452
|
-
const setIfAbsent = (name, value) => {
|
|
453
|
-
const normalized = cleanHeaderValue(value);
|
|
454
|
-
if (normalized && !headers.has(name)) {
|
|
455
|
-
headers.set(name, normalized);
|
|
456
|
-
}
|
|
457
|
-
};
|
|
458
|
-
setIfAbsent("x-lucern-key", config.apiKey);
|
|
459
|
-
setIfAbsent("x-lucern-session-token", config.userToken);
|
|
460
|
-
setIfAbsent("x-lucern-environment", normalizedEnvironment);
|
|
461
|
-
setIfAbsent("x-lucern-clerk-id", config.clerkId);
|
|
462
|
-
setIfAbsent("x-lucern-user-id", config.userId ?? config.clerkId);
|
|
463
|
-
setIfAbsent("x-lucern-deployment-host", config.deploymentHost);
|
|
464
|
-
const base = Object.fromEntries(headers.entries());
|
|
465
|
-
const authContextInput = await resolveConfiguredAuthContext(
|
|
466
|
-
config.authContext
|
|
467
|
-
);
|
|
468
|
-
if (!authContextInput && !config.requireCanonicalAuthContext) {
|
|
469
|
-
return base;
|
|
470
|
-
}
|
|
471
|
-
const authContext = normalizeCanonicalLucernAuthContext(authContextInput);
|
|
472
|
-
return mergeHeaderRecord(base, createCanonicalAuthHeaders(authContext));
|
|
473
|
-
}
|
|
474
|
-
async function fetchWithTimeout(url, init, timeoutMs) {
|
|
475
|
-
const normalizeTransportError = (error, isTimeout) => {
|
|
476
|
-
if (isTimeout) {
|
|
477
|
-
return new GatewayTimeoutError(timeoutMs);
|
|
478
|
-
}
|
|
479
|
-
return error instanceof GatewayTimeoutError || error instanceof GatewayTransportError ? error : new GatewayTransportError(
|
|
480
|
-
error instanceof Error ? error.message : "Gateway transport error",
|
|
481
|
-
{
|
|
482
|
-
cause: error,
|
|
483
|
-
retryable: classifyGatewayErrorForRetry(error)
|
|
484
|
-
}
|
|
485
|
-
);
|
|
486
|
-
};
|
|
487
|
-
const controller = new AbortController();
|
|
488
|
-
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
489
|
-
const requestEffect = Effect.tryPromise({
|
|
490
|
-
try: () => runtime.fetch(url, { ...init, signal: controller.signal }),
|
|
491
|
-
catch: (error) => normalizeTransportError(error, controller.signal.aborted)
|
|
492
|
-
});
|
|
493
|
-
try {
|
|
494
|
-
const exit = await Effect.runPromiseExit(requestEffect);
|
|
495
|
-
if (Exit.isSuccess(exit)) {
|
|
496
|
-
return exit.value;
|
|
497
|
-
}
|
|
498
|
-
const failure = Array.from(Cause.failures(exit.cause))[0];
|
|
499
|
-
if (failure !== void 0) {
|
|
500
|
-
throw failure;
|
|
501
|
-
}
|
|
502
|
-
throw Cause.squash(exit.cause);
|
|
503
|
-
} finally {
|
|
504
|
-
clearTimeout(timer);
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
async function emitSdkResponseTelemetry(context) {
|
|
508
|
-
const retry = classifyRetry({
|
|
509
|
-
status: context.status,
|
|
510
|
-
error: context.error,
|
|
511
|
-
retryAfter: context.retryAfterMs !== null && context.retryAfterMs !== void 0 ? String(context.retryAfterMs / 1e3) : void 0
|
|
512
|
-
});
|
|
513
|
-
await emitTelemetrySignal(telemetryExporter, {
|
|
514
|
-
signalType: "trace",
|
|
515
|
-
surface: "sdk-retry",
|
|
516
|
-
eventName: context.willRetry ? "sdk.retry" : context.error ? "sdk.request.error" : "sdk.request.complete",
|
|
517
|
-
severity: context.error ? context.willRetry ? "warn" : "error" : "info",
|
|
518
|
-
durationMs: context.durationMs,
|
|
519
|
-
metricName: "sdk.request.duration_ms",
|
|
520
|
-
metricValue: context.durationMs,
|
|
521
|
-
correlationId: context.correlationId ?? context.requestId,
|
|
522
|
-
policyTraceId: context.policyTraceId ?? null,
|
|
523
|
-
tenantId: context.headers.get("x-lucern-tenant-id") ?? context.headers.get("x-lucern-tenant") ?? void 0,
|
|
524
|
-
workspaceId: context.headers.get("x-lucern-workspace-id") ?? context.headers.get("x-lucern-workspace") ?? void 0,
|
|
525
|
-
attributes: {
|
|
526
|
-
service: "lucern-sdk",
|
|
527
|
-
operation: "gateway.request",
|
|
528
|
-
path: context.path,
|
|
529
|
-
httpMethod: context.method,
|
|
530
|
-
httpStatus: context.status,
|
|
531
|
-
attempt: context.attempt,
|
|
532
|
-
maxRetries: context.maxRetries,
|
|
533
|
-
retryReason: retry.reason,
|
|
534
|
-
retryAfterMs: context.retryAfterMs ?? retry.retryAfterMs,
|
|
535
|
-
willRetry: context.willRetry,
|
|
536
|
-
retryable: retry.retryable,
|
|
537
|
-
errorName: context.error instanceof Error ? context.error.name : void 0,
|
|
538
|
-
errorMessage: context.error instanceof Error ? context.error.message : void 0
|
|
539
|
-
}
|
|
540
|
-
});
|
|
541
|
-
}
|
|
542
|
-
async function parsePayload(response) {
|
|
543
|
-
const text = await response.text();
|
|
544
|
-
if (!text) {
|
|
545
|
-
return null;
|
|
546
|
-
}
|
|
547
|
-
const parsed = tryParseGatewayEnvelopeJson(text);
|
|
548
|
-
if (!parsed.ok) {
|
|
549
|
-
return null;
|
|
550
|
-
}
|
|
551
|
-
return isRecord(parsed.value) ? parsed.value : null;
|
|
552
|
-
}
|
|
553
|
-
function resolveTimeoutMs(method, requestTimeoutMs) {
|
|
554
|
-
if (typeof requestTimeoutMs === "number") {
|
|
555
|
-
return requestTimeoutMs;
|
|
556
|
-
}
|
|
557
|
-
const methodTimeoutMs = requestTimeoutByMethod?.[method];
|
|
558
|
-
if (typeof methodTimeoutMs === "number") {
|
|
559
|
-
return methodTimeoutMs;
|
|
560
|
-
}
|
|
561
|
-
return defaultRequestTimeoutMs;
|
|
562
|
-
}
|
|
563
|
-
function tryParseGatewayEnvelopeJson(text) {
|
|
564
|
-
const trimmed = text.trim();
|
|
565
|
-
if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) {
|
|
566
|
-
return { ok: false, reason: "non-json" };
|
|
567
|
-
}
|
|
568
|
-
try {
|
|
569
|
-
return { ok: true, value: JSON.parse(trimmed) };
|
|
570
|
-
} catch (error) {
|
|
571
|
-
if (error instanceof SyntaxError) {
|
|
572
|
-
return { ok: false, reason: "invalid-json", error };
|
|
573
|
-
}
|
|
574
|
-
throw error;
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
function buildApiError(args) {
|
|
578
|
-
const failure = args.failure;
|
|
579
|
-
const legacyError = failure && isRecord(failure.error) ? failure.error : failure?.legacyError;
|
|
580
|
-
const correlationId = failure?.correlationId ?? args.response.headers.get("x-lucern-correlation-id")?.trim() ?? args.requestId;
|
|
581
|
-
const policyTraceId = failure?.policyTraceId ?? args.response.headers.get("x-lucern-policy-trace-id")?.trim() ?? null;
|
|
582
|
-
const details = runtime.redaction(
|
|
583
|
-
redactJsonDiagnosticValue(failure?.details ?? legacyError?.details)
|
|
584
|
-
);
|
|
585
|
-
const policySummary = readPolicySummaryFromDetails(details);
|
|
586
|
-
const failureMessage = typeof failure?.error === "string" ? failure.error : legacyError?.message;
|
|
587
|
-
return new LucernApiError({
|
|
588
|
-
code: failure?.code ?? legacyError?.code ?? fallbackErrorCode(args.response.status),
|
|
589
|
-
message: policySummary ?? failureMessage ?? (args.response.ok ? "Platform API returned an invalid success payload." : "Platform API request failed."),
|
|
590
|
-
status: args.response.status,
|
|
591
|
-
invariant: failure?.invariant,
|
|
592
|
-
suggestion: failure?.suggestion,
|
|
593
|
-
details,
|
|
594
|
-
requestId: args.requestId,
|
|
595
|
-
correlationId,
|
|
596
|
-
policyTraceId
|
|
597
|
-
});
|
|
598
|
-
}
|
|
599
|
-
async function request(args) {
|
|
600
|
-
const authHeaders = await resolveAuthHeaders();
|
|
601
|
-
const method = args.method ?? "GET";
|
|
602
|
-
const timeoutMs = resolveTimeoutMs(method, args.timeoutMs);
|
|
603
|
-
const headers = new Headers({
|
|
604
|
-
"content-type": "application/json",
|
|
605
|
-
...authHeaders
|
|
606
|
-
});
|
|
607
|
-
if (args.idempotencyKey) {
|
|
608
|
-
headers.set("idempotency-key", args.idempotencyKey);
|
|
609
|
-
}
|
|
610
|
-
const requestId = headers.get("x-correlation-id")?.trim() || headers.get("x-request-id")?.trim() || args.requestId || requestIdFactory();
|
|
611
|
-
if (!headers.has("x-correlation-id") && !headers.has("x-request-id")) {
|
|
612
|
-
headers.set("x-correlation-id", requestId);
|
|
613
|
-
}
|
|
614
|
-
const url = `${baseUrl}${args.path}`;
|
|
615
|
-
const serializedBody = args.body ? JSON.stringify(args.body) : void 0;
|
|
616
|
-
const init = {
|
|
617
|
-
method,
|
|
618
|
-
headers,
|
|
619
|
-
body: serializedBody
|
|
620
|
-
};
|
|
621
|
-
let lastError;
|
|
622
|
-
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
623
|
-
const hookRequestContext = {
|
|
624
|
-
requestId,
|
|
625
|
-
attempt,
|
|
626
|
-
maxRetries,
|
|
627
|
-
method,
|
|
628
|
-
path: args.path,
|
|
629
|
-
url,
|
|
630
|
-
headers: new Headers(headers),
|
|
631
|
-
body: serializedBody,
|
|
632
|
-
timeoutMs
|
|
633
|
-
};
|
|
634
|
-
await config.onRequest?.(hookRequestContext);
|
|
635
|
-
const startedAt = Date.now();
|
|
636
|
-
try {
|
|
637
|
-
const response = await fetchWithTimeout(url, init, timeoutMs);
|
|
638
|
-
const responseClone = response.clone();
|
|
639
|
-
const payload = await parsePayload(response);
|
|
640
|
-
const retry = classifyRetry({
|
|
641
|
-
status: response.status,
|
|
642
|
-
retryAfter: response.headers.get("Retry-After")
|
|
643
|
-
});
|
|
644
|
-
const retryAfterMs = retry.retryAfterMs ?? null;
|
|
645
|
-
if (!response.ok || !payload?.success) {
|
|
646
|
-
const failure = payload && !payload.success ? payload : null;
|
|
647
|
-
const apiError = buildApiError({
|
|
648
|
-
requestId,
|
|
649
|
-
response,
|
|
650
|
-
failure
|
|
651
|
-
});
|
|
652
|
-
const willRetry = attempt < maxRetries && retry.retryable;
|
|
653
|
-
const responseContext2 = {
|
|
654
|
-
...hookRequestContext,
|
|
655
|
-
durationMs: Date.now() - startedAt,
|
|
656
|
-
status: response.status,
|
|
657
|
-
response: responseClone,
|
|
658
|
-
error: apiError,
|
|
659
|
-
correlationId: apiError.correlationId ?? requestId,
|
|
660
|
-
policyTraceId: apiError.policyTraceId ?? null,
|
|
661
|
-
retryAfterMs,
|
|
662
|
-
willRetry
|
|
663
|
-
};
|
|
664
|
-
await config.onResponse?.(responseContext2);
|
|
665
|
-
await emitSdkResponseTelemetry(responseContext2);
|
|
666
|
-
if (willRetry) {
|
|
667
|
-
lastError = apiError;
|
|
668
|
-
await delay(
|
|
669
|
-
computeRetryDelayMs({
|
|
670
|
-
attempt,
|
|
671
|
-
status: response.status,
|
|
672
|
-
retryAfterMs
|
|
673
|
-
})
|
|
674
|
-
);
|
|
675
|
-
continue;
|
|
676
|
-
}
|
|
677
|
-
throw apiError;
|
|
678
|
-
}
|
|
679
|
-
const successPayload = payload;
|
|
680
|
-
const responseContext = {
|
|
681
|
-
...hookRequestContext,
|
|
682
|
-
durationMs: Date.now() - startedAt,
|
|
683
|
-
status: response.status,
|
|
684
|
-
response: responseClone,
|
|
685
|
-
correlationId: successPayload.correlationId ?? response.headers.get("x-lucern-correlation-id")?.trim() ?? requestId,
|
|
686
|
-
policyTraceId: successPayload.policyTraceId ?? response.headers.get("x-lucern-policy-trace-id")?.trim() ?? null,
|
|
687
|
-
idempotentReplay: successPayload.idempotentReplay,
|
|
688
|
-
retryAfterMs,
|
|
689
|
-
willRetry: false
|
|
690
|
-
};
|
|
691
|
-
await config.onResponse?.(responseContext);
|
|
692
|
-
await emitSdkResponseTelemetry(responseContext);
|
|
693
|
-
return successPayload;
|
|
694
|
-
} catch (fetchError) {
|
|
695
|
-
if (fetchError instanceof LucernApiError) {
|
|
696
|
-
throw fetchError;
|
|
697
|
-
}
|
|
698
|
-
const willRetry = attempt < maxRetries && classifyGatewayErrorForRetry(fetchError);
|
|
699
|
-
const responseContext = {
|
|
700
|
-
...hookRequestContext,
|
|
701
|
-
durationMs: Date.now() - startedAt,
|
|
702
|
-
error: fetchError,
|
|
703
|
-
correlationId: requestId,
|
|
704
|
-
policyTraceId: null,
|
|
705
|
-
willRetry
|
|
706
|
-
};
|
|
707
|
-
await config.onResponse?.(responseContext);
|
|
708
|
-
await emitSdkResponseTelemetry(responseContext);
|
|
709
|
-
lastError = fetchError;
|
|
710
|
-
if (willRetry) {
|
|
711
|
-
await delay(computeRetryDelayMs({ attempt }));
|
|
712
|
-
}
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
|
-
throw lastError instanceof Error ? lastError : new Error("Platform API request failed after retries.");
|
|
716
|
-
}
|
|
717
|
-
return {
|
|
718
|
-
request
|
|
719
|
-
};
|
|
720
|
-
}
|
|
721
|
-
|
|
722
|
-
// src/sdkSurface.ts
|
|
723
|
-
function cleanString2(value) {
|
|
724
|
-
return typeof value === "string" && value.trim().length > 0 ? value.trim() : void 0;
|
|
725
|
-
}
|
|
726
|
-
function normalizeVerificationStatus(value) {
|
|
727
|
-
const status = cleanString2(value);
|
|
728
|
-
if (!status) {
|
|
729
|
-
return void 0;
|
|
730
|
-
}
|
|
731
|
-
if (status === "verified") {
|
|
732
|
-
return "human_verified";
|
|
733
|
-
}
|
|
734
|
-
if (status === "rejected") {
|
|
735
|
-
return "contradicted";
|
|
736
|
-
}
|
|
737
|
-
return status;
|
|
738
|
-
}
|
|
739
|
-
function resolveTopicId(value) {
|
|
740
|
-
return cleanString2(value.topicId);
|
|
741
|
-
}
|
|
742
|
-
function resolveText(value) {
|
|
743
|
-
return cleanString2(value.text) ?? cleanString2(value.canonicalText);
|
|
744
|
-
}
|
|
745
|
-
function normalizeNodeWriteInput(value) {
|
|
746
|
-
const topicId = resolveTopicId(value);
|
|
747
|
-
const text = resolveText(value);
|
|
748
|
-
const verificationStatus = normalizeVerificationStatus(value.verificationStatus);
|
|
749
|
-
const next = { ...value };
|
|
750
|
-
if (topicId) {
|
|
751
|
-
next.topicId = topicId;
|
|
752
|
-
}
|
|
753
|
-
if (text) {
|
|
754
|
-
next.text = text;
|
|
755
|
-
next.canonicalText = text;
|
|
756
|
-
}
|
|
757
|
-
if (verificationStatus) {
|
|
758
|
-
next.verificationStatus = verificationStatus;
|
|
759
|
-
}
|
|
760
|
-
return next;
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
// src/beliefsClient.ts
|
|
1
|
+
import { createGatewayRequestClient, LucernApiError, randomIdempotencyKey, } from "./coreClient.js";
|
|
2
|
+
import { normalizeNodeWriteInput } from "./sdkSurface.js";
|
|
3
|
+
export { LucernApiError };
|
|
764
4
|
function asRecord(value) {
|
|
765
|
-
|
|
5
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
6
|
+
? value
|
|
7
|
+
: {};
|
|
766
8
|
}
|
|
767
9
|
function readString(value) {
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
10
|
+
if (typeof value !== "string") {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
const normalized = value.trim();
|
|
14
|
+
return normalized.length > 0 ? normalized : undefined;
|
|
773
15
|
}
|
|
774
16
|
function readNumber(value) {
|
|
775
|
-
|
|
17
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
776
18
|
}
|
|
777
19
|
function clamp01(value) {
|
|
778
|
-
|
|
20
|
+
return Math.max(0, Math.min(1, value));
|
|
779
21
|
}
|
|
780
22
|
function normalizeOpinionTuple(record) {
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
return {
|
|
792
|
-
b: clamp01(rawBelief),
|
|
793
|
-
d: clamp01(rawDisbelief),
|
|
794
|
-
u: clamp01(rawUncertainty),
|
|
795
|
-
a: clamp01(rawBaseRate)
|
|
796
|
-
};
|
|
797
|
-
}
|
|
798
|
-
function mapOpinionHistoryEntriesFromGatewayData(payload) {
|
|
799
|
-
const entries = Array.isArray(payload.entries) ? payload.entries : [];
|
|
800
|
-
return entries.map((value) => {
|
|
801
|
-
const record = asRecord(value);
|
|
802
|
-
const tuple = normalizeOpinionTuple(record);
|
|
803
|
-
const projected = readNumber(record.confidence) ?? clamp01(tuple.b + tuple.a * tuple.u);
|
|
804
|
-
const triggeringEvidenceId = readString(record.triggeringEvidenceId);
|
|
805
|
-
const triggeringQuestionId = readString(record.triggeringQuestionId);
|
|
806
|
-
const triggeringAnswerId = readString(record.triggeringAnswerId);
|
|
807
|
-
const triggeringContradictionId = readString(
|
|
808
|
-
record.triggeringContradictionId
|
|
809
|
-
);
|
|
810
|
-
const triggeringWorktreeId = readString(record.triggeringWorktreeId);
|
|
811
|
-
const triggeringRef = triggeringEvidenceId ? { kind: "evidence", id: triggeringEvidenceId } : triggeringQuestionId ? { kind: "question", id: triggeringQuestionId } : triggeringAnswerId ? { kind: "answer", id: triggeringAnswerId } : triggeringContradictionId ? { kind: "contradiction", id: triggeringContradictionId } : triggeringWorktreeId ? { kind: "worktree", id: triggeringWorktreeId } : void 0;
|
|
812
|
-
const trigger = readString(record.trigger);
|
|
813
|
-
if (!trigger) {
|
|
814
|
-
throw new Error("Gateway opinion history entries must include trigger.");
|
|
23
|
+
const opinion = asRecord(record.opinion);
|
|
24
|
+
const rawBelief = readNumber(opinion.b) ?? readNumber(record.belief);
|
|
25
|
+
const rawDisbelief = readNumber(opinion.d) ?? readNumber(record.disbelief);
|
|
26
|
+
const rawUncertainty = readNumber(opinion.u) ?? readNumber(record.uncertainty);
|
|
27
|
+
const rawBaseRate = readNumber(opinion.a) ?? readNumber(record.baseRate);
|
|
28
|
+
if (rawBelief === undefined ||
|
|
29
|
+
rawDisbelief === undefined ||
|
|
30
|
+
rawUncertainty === undefined ||
|
|
31
|
+
rawBaseRate === undefined) {
|
|
32
|
+
throw new Error("Gateway opinion history entries must include belief, disbelief, uncertainty, and baseRate.");
|
|
815
33
|
}
|
|
816
34
|
return {
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
a: tuple.a,
|
|
822
|
-
P: clamp01(projected),
|
|
823
|
-
trigger,
|
|
824
|
-
...triggeringRef ? { triggeringRef } : {},
|
|
825
|
-
...readString(record.rationale) ? { rationale: readString(record.rationale) } : {},
|
|
826
|
-
...readString(record.userId) ? { userId: readString(record.userId) } : {},
|
|
827
|
-
...readString(record.slOperator) ? { slOperator: readString(record.slOperator) } : {}
|
|
35
|
+
b: clamp01(rawBelief),
|
|
36
|
+
d: clamp01(rawDisbelief),
|
|
37
|
+
u: clamp01(rawUncertainty),
|
|
38
|
+
a: clamp01(rawBaseRate),
|
|
828
39
|
};
|
|
829
|
-
|
|
40
|
+
}
|
|
41
|
+
export function mapOpinionHistoryEntriesFromGatewayData(payload) {
|
|
42
|
+
const entries = Array.isArray(payload.entries) ? payload.entries : [];
|
|
43
|
+
return entries
|
|
44
|
+
.map((value) => {
|
|
45
|
+
const record = asRecord(value);
|
|
46
|
+
const tuple = normalizeOpinionTuple(record);
|
|
47
|
+
const projected = readNumber(record.confidence) ?? clamp01(tuple.b + tuple.a * tuple.u);
|
|
48
|
+
const triggeringEvidenceId = readString(record.triggeringEvidenceId);
|
|
49
|
+
const triggeringQuestionId = readString(record.triggeringQuestionId);
|
|
50
|
+
const triggeringAnswerId = readString(record.triggeringAnswerId);
|
|
51
|
+
const triggeringContradictionId = readString(record.triggeringContradictionId);
|
|
52
|
+
const triggeringWorktreeId = readString(record.triggeringWorktreeId);
|
|
53
|
+
const triggeringRef = triggeringEvidenceId
|
|
54
|
+
? { kind: "evidence", id: triggeringEvidenceId }
|
|
55
|
+
: triggeringQuestionId
|
|
56
|
+
? { kind: "question", id: triggeringQuestionId }
|
|
57
|
+
: triggeringAnswerId
|
|
58
|
+
? { kind: "answer", id: triggeringAnswerId }
|
|
59
|
+
: triggeringContradictionId
|
|
60
|
+
? { kind: "contradiction", id: triggeringContradictionId }
|
|
61
|
+
: triggeringWorktreeId
|
|
62
|
+
? { kind: "worktree", id: triggeringWorktreeId }
|
|
63
|
+
: undefined;
|
|
64
|
+
const trigger = readString(record.trigger);
|
|
65
|
+
if (!trigger) {
|
|
66
|
+
throw new Error("Gateway opinion history entries must include trigger.");
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
t: readNumber(record.timestamp) ?? readNumber(record.assessedAt) ?? 0,
|
|
70
|
+
b: tuple.b,
|
|
71
|
+
d: tuple.d,
|
|
72
|
+
u: tuple.u,
|
|
73
|
+
a: tuple.a,
|
|
74
|
+
P: clamp01(projected),
|
|
75
|
+
trigger,
|
|
76
|
+
...(triggeringRef ? { triggeringRef } : {}),
|
|
77
|
+
...(readString(record.rationale)
|
|
78
|
+
? { rationale: readString(record.rationale) }
|
|
79
|
+
: {}),
|
|
80
|
+
...(readString(record.userId) ? { userId: readString(record.userId) } : {}),
|
|
81
|
+
...(readString(record.slOperator)
|
|
82
|
+
? { slOperator: readString(record.slOperator) }
|
|
83
|
+
: {}),
|
|
84
|
+
};
|
|
85
|
+
})
|
|
86
|
+
.sort((left, right) => left.t - right.t);
|
|
830
87
|
}
|
|
831
88
|
function normalizeModulateConfidenceInput(input) {
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
89
|
+
const opinion = input.opinion;
|
|
90
|
+
return {
|
|
91
|
+
belief: opinion.b,
|
|
92
|
+
disbelief: opinion.d,
|
|
93
|
+
uncertainty: opinion.u,
|
|
94
|
+
baseRate: opinion.a,
|
|
95
|
+
trigger: input.trigger,
|
|
96
|
+
rationale: input.rationale,
|
|
97
|
+
triggeringEvidenceId: input.triggeringEvidenceId,
|
|
98
|
+
triggeringQuestionId: input.triggeringQuestionId,
|
|
99
|
+
triggeringAnswerId: input.triggeringAnswerId,
|
|
100
|
+
triggeringContradictionId: input.triggeringContradictionId,
|
|
101
|
+
triggeringWorktreeId: input.triggeringWorktreeId,
|
|
102
|
+
maxInlinePropagationTargets: input.maxInlinePropagationTargets,
|
|
103
|
+
};
|
|
847
104
|
}
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
105
|
+
/**
|
|
106
|
+
* Create the beliefs client for belief lifecycle operations.
|
|
107
|
+
*
|
|
108
|
+
* @param config - Gateway transport configuration.
|
|
109
|
+
* @returns An object with `createBelief`, `refineBelief`, `modulateConfidence`, and `forkBelief` methods.
|
|
110
|
+
*/
|
|
111
|
+
export function createBeliefsClient(config = {}) {
|
|
112
|
+
const gateway = createGatewayRequestClient(config);
|
|
113
|
+
function normalizeBaseRate(value) {
|
|
114
|
+
const baseRate = readNumber(value);
|
|
115
|
+
const normalized = baseRate ?? 0.5;
|
|
116
|
+
if (normalized < 0 || normalized > 1) {
|
|
117
|
+
throw new Error("baseRate must be within [0, 1].");
|
|
118
|
+
}
|
|
119
|
+
return normalized;
|
|
855
120
|
}
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
const modulateConfidence = async (beliefId, input, idempotencyKey) => gateway.request({
|
|
859
|
-
path: `/api/platform/v1/beliefs/${encodeURIComponent(beliefId)}/confidence`,
|
|
860
|
-
method: "POST",
|
|
861
|
-
body: normalizeModulateConfidenceInput(input),
|
|
862
|
-
idempotencyKey: idempotencyKey ?? randomIdempotencyKey()
|
|
863
|
-
});
|
|
864
|
-
const getOpinionHistory = async (beliefId) => {
|
|
865
|
-
const response = await gateway.request({
|
|
866
|
-
path: `/api/platform/v1/beliefs/${encodeURIComponent(beliefId)}/confidence-history`
|
|
867
|
-
});
|
|
868
|
-
return mapOpinionHistoryEntriesFromGatewayData(response.data);
|
|
869
|
-
};
|
|
870
|
-
return {
|
|
871
|
-
/**
|
|
872
|
-
* Create a belief within a topic scope.
|
|
873
|
-
*/
|
|
874
|
-
async createBelief(input, idempotencyKey) {
|
|
875
|
-
const baseRate = normalizeBaseRate(input.baseRate);
|
|
876
|
-
return gateway.request({
|
|
877
|
-
path: "/api/platform/v1/beliefs",
|
|
121
|
+
const modulateConfidence = async (beliefId, input, idempotencyKey) => gateway.request({
|
|
122
|
+
path: `/api/platform/v1/beliefs/${encodeURIComponent(beliefId)}/confidence`,
|
|
878
123
|
method: "POST",
|
|
879
|
-
body:
|
|
880
|
-
|
|
881
|
-
|
|
124
|
+
body: normalizeModulateConfidenceInput(input),
|
|
125
|
+
idempotencyKey: idempotencyKey ?? randomIdempotencyKey(),
|
|
126
|
+
});
|
|
127
|
+
const getOpinionHistory = async (beliefId) => {
|
|
128
|
+
const response = await gateway.request({
|
|
129
|
+
path: `/api/platform/v1/beliefs/${encodeURIComponent(beliefId)}/confidence-history`,
|
|
130
|
+
});
|
|
131
|
+
return mapOpinionHistoryEntriesFromGatewayData(response.data);
|
|
132
|
+
};
|
|
133
|
+
return {
|
|
134
|
+
/**
|
|
135
|
+
* Create a belief within a topic scope.
|
|
136
|
+
*/
|
|
137
|
+
async createBelief(input, idempotencyKey) {
|
|
138
|
+
const baseRate = normalizeBaseRate(input.baseRate);
|
|
139
|
+
return gateway.request({
|
|
140
|
+
path: "/api/platform/v1/beliefs",
|
|
141
|
+
method: "POST",
|
|
142
|
+
body: {
|
|
143
|
+
...normalizeNodeWriteInput(input),
|
|
144
|
+
baseRate,
|
|
145
|
+
},
|
|
146
|
+
idempotencyKey: idempotencyKey ?? randomIdempotencyKey(),
|
|
147
|
+
});
|
|
882
148
|
},
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
});
|
|
1004
|
-
}
|
|
1005
|
-
};
|
|
149
|
+
/**
|
|
150
|
+
* Refine a draft belief in place.
|
|
151
|
+
*/
|
|
152
|
+
async refineBelief(beliefId, input, idempotencyKey) {
|
|
153
|
+
return gateway.request({
|
|
154
|
+
path: `/api/platform/v1/beliefs/${encodeURIComponent(beliefId)}`,
|
|
155
|
+
method: "PATCH",
|
|
156
|
+
body: normalizeNodeWriteInput(input),
|
|
157
|
+
idempotencyKey: idempotencyKey ?? randomIdempotencyKey(),
|
|
158
|
+
});
|
|
159
|
+
},
|
|
160
|
+
/**
|
|
161
|
+
* Record a confidence change for an existing belief.
|
|
162
|
+
*/
|
|
163
|
+
modulateConfidence,
|
|
164
|
+
/**
|
|
165
|
+
* Returns the belief's confidence trajectory as a chronological array.
|
|
166
|
+
*
|
|
167
|
+
* Canonical UI trend shape (what every trend component consumes):
|
|
168
|
+
* { t: number, b: number, d: number, u: number, a: number, P: number, trigger: string, triggeringRef?: { kind: "evidence" | "worktree"; id: string } }[]
|
|
169
|
+
*
|
|
170
|
+
* Where:
|
|
171
|
+
* t = assessedAt (epoch ms)
|
|
172
|
+
* b, d, u, a = Opinion 4-tuple components
|
|
173
|
+
* P = projected probability = b + a*u (precomputed for UI convenience)
|
|
174
|
+
* trigger = cause of the score change
|
|
175
|
+
* triggeringRef = optional pointer to the evidence or worktree that drove the change
|
|
176
|
+
*/
|
|
177
|
+
getOpinionHistory,
|
|
178
|
+
/** @deprecated Use getOpinionHistory(). */
|
|
179
|
+
getConfidenceHistory: getOpinionHistory,
|
|
180
|
+
/**
|
|
181
|
+
* Fork a scored belief into a new formulation.
|
|
182
|
+
*/
|
|
183
|
+
async forkBelief(beliefId, input, idempotencyKey) {
|
|
184
|
+
return gateway.request({
|
|
185
|
+
path: `/api/platform/v1/beliefs/${encodeURIComponent(beliefId)}/fork`,
|
|
186
|
+
method: "POST",
|
|
187
|
+
body: input,
|
|
188
|
+
idempotencyKey: idempotencyKey ?? randomIdempotencyKey(),
|
|
189
|
+
});
|
|
190
|
+
},
|
|
191
|
+
/**
|
|
192
|
+
* Update a belief's lifecycle status.
|
|
193
|
+
*/
|
|
194
|
+
async updateBeliefStatus(input, idempotencyKey) {
|
|
195
|
+
return gateway.request({
|
|
196
|
+
path: "/api/platform/v1/beliefs/update-status",
|
|
197
|
+
method: "POST",
|
|
198
|
+
body: input,
|
|
199
|
+
idempotencyKey: idempotencyKey ?? randomIdempotencyKey(),
|
|
200
|
+
});
|
|
201
|
+
},
|
|
202
|
+
/**
|
|
203
|
+
* Update a belief's rationale without changing its text.
|
|
204
|
+
*/
|
|
205
|
+
async updateBeliefRationale(input, idempotencyKey) {
|
|
206
|
+
return gateway.request({
|
|
207
|
+
path: "/api/platform/v1/beliefs/update-rationale",
|
|
208
|
+
method: "POST",
|
|
209
|
+
body: input,
|
|
210
|
+
idempotencyKey: idempotencyKey ?? randomIdempotencyKey(),
|
|
211
|
+
});
|
|
212
|
+
},
|
|
213
|
+
/**
|
|
214
|
+
* Create a typed relationship between two beliefs.
|
|
215
|
+
*/
|
|
216
|
+
async linkBeliefs(input, idempotencyKey) {
|
|
217
|
+
return gateway.request({
|
|
218
|
+
path: "/api/platform/v1/beliefs/link",
|
|
219
|
+
method: "POST",
|
|
220
|
+
body: input,
|
|
221
|
+
idempotencyKey: idempotencyKey ?? randomIdempotencyKey(),
|
|
222
|
+
});
|
|
223
|
+
},
|
|
224
|
+
/**
|
|
225
|
+
* Remove an evidence attachment from a belief.
|
|
226
|
+
*/
|
|
227
|
+
async unlinkBeliefEvidence(input, idempotencyKey) {
|
|
228
|
+
return gateway.request({
|
|
229
|
+
path: "/api/platform/v1/beliefs/unlink-evidence",
|
|
230
|
+
method: "POST",
|
|
231
|
+
body: input,
|
|
232
|
+
idempotencyKey: idempotencyKey ?? randomIdempotencyKey(),
|
|
233
|
+
});
|
|
234
|
+
},
|
|
235
|
+
/**
|
|
236
|
+
* Update a belief's criticality metadata.
|
|
237
|
+
*/
|
|
238
|
+
async updateBeliefCriticality(input, idempotencyKey) {
|
|
239
|
+
return gateway.request({
|
|
240
|
+
path: "/api/platform/v1/beliefs/update-criticality",
|
|
241
|
+
method: "POST",
|
|
242
|
+
body: input,
|
|
243
|
+
idempotencyKey: idempotencyKey ?? randomIdempotencyKey(),
|
|
244
|
+
});
|
|
245
|
+
},
|
|
246
|
+
/**
|
|
247
|
+
* Update multiple beliefs' criticality in one mutation.
|
|
248
|
+
*/
|
|
249
|
+
async batchUpdateBeliefCriticality(input, idempotencyKey) {
|
|
250
|
+
return gateway.request({
|
|
251
|
+
path: "/api/platform/v1/beliefs/batch-update-criticality",
|
|
252
|
+
method: "POST",
|
|
253
|
+
body: input,
|
|
254
|
+
idempotencyKey: idempotencyKey ?? randomIdempotencyKey(),
|
|
255
|
+
});
|
|
256
|
+
},
|
|
257
|
+
/**
|
|
258
|
+
* Reassign beliefs to a different topic scope.
|
|
259
|
+
*/
|
|
260
|
+
async reassignBeliefsTopic(input, idempotencyKey) {
|
|
261
|
+
return gateway.request({
|
|
262
|
+
path: "/api/platform/v1/beliefs/reassign-topic",
|
|
263
|
+
method: "POST",
|
|
264
|
+
body: input,
|
|
265
|
+
idempotencyKey: idempotencyKey ?? randomIdempotencyKey(),
|
|
266
|
+
});
|
|
267
|
+
},
|
|
268
|
+
};
|
|
1006
269
|
}
|
|
1007
|
-
|
|
1008
|
-
export { LucernApiError, createBeliefsClient, mapOpinionHistoryEntriesFromGatewayData };
|
|
1009
|
-
//# sourceMappingURL=beliefsClient.js.map
|
|
1010
270
|
//# sourceMappingURL=beliefsClient.js.map
|