@zensorum/sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +130 -0
- package/dist/index.js +290 -0
- package/package.json +32 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zensorum Public SDK
|
|
3
|
+
*
|
|
4
|
+
* Public developer contracts and reference implementation.
|
|
5
|
+
*
|
|
6
|
+
* The SDK intentionally does not expose Zensorum's private
|
|
7
|
+
* execution engine, persistence, governance infrastructure,
|
|
8
|
+
* replay, reconstruction, or runtime internals.
|
|
9
|
+
*/
|
|
10
|
+
export interface Institution {
|
|
11
|
+
readonly id: string;
|
|
12
|
+
readonly name: string;
|
|
13
|
+
}
|
|
14
|
+
export interface ScenarioStage {
|
|
15
|
+
readonly id: string;
|
|
16
|
+
readonly mandatory?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface Scenario {
|
|
19
|
+
readonly id: string;
|
|
20
|
+
readonly name: string;
|
|
21
|
+
readonly version: string;
|
|
22
|
+
readonly stages: readonly ScenarioStage[];
|
|
23
|
+
}
|
|
24
|
+
export interface InstitutionalInput<TPayload = unknown> {
|
|
25
|
+
readonly id: string;
|
|
26
|
+
readonly source: string;
|
|
27
|
+
readonly occurredAt: string;
|
|
28
|
+
readonly payload: TPayload;
|
|
29
|
+
}
|
|
30
|
+
export interface AIInput<TRecommendation = unknown> {
|
|
31
|
+
readonly source: string;
|
|
32
|
+
readonly recommendation: TRecommendation;
|
|
33
|
+
}
|
|
34
|
+
export interface Policy {
|
|
35
|
+
readonly id: string;
|
|
36
|
+
readonly name: string;
|
|
37
|
+
readonly version: string;
|
|
38
|
+
readonly specification: unknown;
|
|
39
|
+
}
|
|
40
|
+
export interface Governance {
|
|
41
|
+
readonly policies: readonly Policy[];
|
|
42
|
+
}
|
|
43
|
+
export interface Authority {
|
|
44
|
+
readonly id: string;
|
|
45
|
+
readonly name: string;
|
|
46
|
+
}
|
|
47
|
+
export interface Capability<TInput = unknown> {
|
|
48
|
+
readonly name: string;
|
|
49
|
+
readonly execute: (input: TInput) => Promise<unknown> | unknown;
|
|
50
|
+
}
|
|
51
|
+
export interface ExecutionRequest<TContext = unknown, TRecommendation = unknown, TPayload = unknown> {
|
|
52
|
+
readonly institution: Institution;
|
|
53
|
+
readonly scenario: Scenario;
|
|
54
|
+
readonly context: TContext;
|
|
55
|
+
readonly institutionalInputs?: readonly InstitutionalInput<TPayload>[];
|
|
56
|
+
readonly aiInput: AIInput<TRecommendation>;
|
|
57
|
+
readonly governance: Governance;
|
|
58
|
+
readonly authority: Authority;
|
|
59
|
+
readonly capabilities: readonly Capability<TRecommendation>[];
|
|
60
|
+
}
|
|
61
|
+
export interface ExecutionDecision {
|
|
62
|
+
readonly status: "approved" | "blocked";
|
|
63
|
+
readonly reason: string;
|
|
64
|
+
}
|
|
65
|
+
export interface ExecutionOutcome {
|
|
66
|
+
readonly status: "completed" | "not-executed" | "failed";
|
|
67
|
+
}
|
|
68
|
+
export interface ExecutionAction {
|
|
69
|
+
readonly capability?: string;
|
|
70
|
+
readonly status: "executed" | "not-executed";
|
|
71
|
+
readonly result?: unknown;
|
|
72
|
+
}
|
|
73
|
+
export interface ExecutionEvidence {
|
|
74
|
+
readonly available: boolean;
|
|
75
|
+
}
|
|
76
|
+
export interface ExecutionResult<TContext = unknown, TRecommendation = unknown> {
|
|
77
|
+
readonly executionId: string;
|
|
78
|
+
readonly institutionId: string;
|
|
79
|
+
readonly scenarioId: string;
|
|
80
|
+
readonly context: TContext;
|
|
81
|
+
readonly aiInput: AIInput<TRecommendation>;
|
|
82
|
+
readonly decision: ExecutionDecision;
|
|
83
|
+
readonly action: ExecutionAction;
|
|
84
|
+
readonly outcome: ExecutionOutcome;
|
|
85
|
+
readonly evidence: ExecutionEvidence;
|
|
86
|
+
readonly previousExecutionId?: string;
|
|
87
|
+
}
|
|
88
|
+
export interface ReevaluationRequest<TContext = unknown, TRecommendation = unknown, TPayload = unknown> {
|
|
89
|
+
readonly execution: ExecutionResult<TContext, TRecommendation>;
|
|
90
|
+
readonly context: TContext;
|
|
91
|
+
readonly institutionalInputs?: readonly InstitutionalInput<TPayload>[];
|
|
92
|
+
readonly aiInput: AIInput<TRecommendation>;
|
|
93
|
+
readonly governance: Governance;
|
|
94
|
+
readonly authority: Authority;
|
|
95
|
+
readonly capabilities: readonly Capability<TRecommendation>[];
|
|
96
|
+
}
|
|
97
|
+
export interface InstitutionalHistory {
|
|
98
|
+
readonly institutionId: string;
|
|
99
|
+
readonly scenarioId: string;
|
|
100
|
+
readonly executions: readonly ExecutionResult[];
|
|
101
|
+
}
|
|
102
|
+
export interface ZensorumClient {
|
|
103
|
+
execute<TContext = unknown, TRecommendation = unknown, TPayload = unknown>(request: ExecutionRequest<TContext, TRecommendation, TPayload>): Promise<ExecutionResult<TContext, TRecommendation>>;
|
|
104
|
+
reevaluate<TContext = unknown, TRecommendation = unknown, TPayload = unknown>(request: ReevaluationRequest<TContext, TRecommendation, TPayload>): Promise<ExecutionResult<TContext, TRecommendation>>;
|
|
105
|
+
history(input: {
|
|
106
|
+
institution: Institution;
|
|
107
|
+
scenario: Scenario;
|
|
108
|
+
}): Promise<InstitutionalHistory>;
|
|
109
|
+
}
|
|
110
|
+
export interface Zensorum {
|
|
111
|
+
institution(input: {
|
|
112
|
+
id: string;
|
|
113
|
+
name: string;
|
|
114
|
+
}): Institution;
|
|
115
|
+
scenario(input: {
|
|
116
|
+
id: string;
|
|
117
|
+
name: string;
|
|
118
|
+
version: string;
|
|
119
|
+
stages: readonly ScenarioStage[];
|
|
120
|
+
}): Scenario;
|
|
121
|
+
governance(input: {
|
|
122
|
+
policies: readonly Policy[];
|
|
123
|
+
}): Governance;
|
|
124
|
+
authority(input: {
|
|
125
|
+
id: string;
|
|
126
|
+
name: string;
|
|
127
|
+
}): Authority;
|
|
128
|
+
client(): ZensorumClient;
|
|
129
|
+
}
|
|
130
|
+
export declare function createZensorum(): Zensorum;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Zensorum Public SDK
|
|
4
|
+
*
|
|
5
|
+
* Public developer contracts and reference implementation.
|
|
6
|
+
*
|
|
7
|
+
* The SDK intentionally does not expose Zensorum's private
|
|
8
|
+
* execution engine, persistence, governance infrastructure,
|
|
9
|
+
* replay, reconstruction, or runtime internals.
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.createZensorum = createZensorum;
|
|
13
|
+
function createExecutionId() {
|
|
14
|
+
return `execution-${Date.now()}-${Math.random()
|
|
15
|
+
.toString(36)
|
|
16
|
+
.slice(2, 10)}`;
|
|
17
|
+
}
|
|
18
|
+
function evaluateGovernance(request) {
|
|
19
|
+
if (request.governance.policies.length === 0) {
|
|
20
|
+
return {
|
|
21
|
+
status: "blocked",
|
|
22
|
+
reason: "No institutional governance policy was supplied.",
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
if (request.authority.id.length === 0) {
|
|
26
|
+
return {
|
|
27
|
+
status: "blocked",
|
|
28
|
+
reason: "No valid execution authority was supplied.",
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
for (const policy of request.governance.policies) {
|
|
32
|
+
const specification = policy.specification;
|
|
33
|
+
if (typeof specification === "object" &&
|
|
34
|
+
specification !== null &&
|
|
35
|
+
"executionPermitted" in specification &&
|
|
36
|
+
specification
|
|
37
|
+
.executionPermitted === false) {
|
|
38
|
+
return {
|
|
39
|
+
status: "blocked",
|
|
40
|
+
reason: `Execution is prohibited by institutional policy '${policy.id}'.`,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (typeof specification === "object" &&
|
|
44
|
+
specification !== null &&
|
|
45
|
+
"requiredCondition" in specification) {
|
|
46
|
+
const requiredCondition = specification.requiredCondition;
|
|
47
|
+
const context = request.context;
|
|
48
|
+
if (typeof context === "object" &&
|
|
49
|
+
context !== null &&
|
|
50
|
+
"operationalCondition" in context &&
|
|
51
|
+
requiredCondition !==
|
|
52
|
+
context
|
|
53
|
+
.operationalCondition) {
|
|
54
|
+
return {
|
|
55
|
+
status: "blocked",
|
|
56
|
+
reason: `Institutional context does not satisfy policy '${policy.id}'.`,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (typeof specification === "object" &&
|
|
61
|
+
specification !== null &&
|
|
62
|
+
"internalCapacityRequired" in specification) {
|
|
63
|
+
const required = specification.internalCapacityRequired;
|
|
64
|
+
const context = request.context;
|
|
65
|
+
if (required === true &&
|
|
66
|
+
typeof context === "object" &&
|
|
67
|
+
context !== null &&
|
|
68
|
+
"internalResponseCapacity" in context &&
|
|
69
|
+
context
|
|
70
|
+
.internalResponseCapacity !== "available") {
|
|
71
|
+
return {
|
|
72
|
+
status: "blocked",
|
|
73
|
+
reason: "Institutional context does not provide the required internal response capacity.",
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (typeof specification === "object" &&
|
|
78
|
+
specification !== null &&
|
|
79
|
+
"externalCapacityRequired" in specification) {
|
|
80
|
+
const required = specification.externalCapacityRequired;
|
|
81
|
+
const context = request.context;
|
|
82
|
+
if (required === true &&
|
|
83
|
+
typeof context === "object" &&
|
|
84
|
+
context !== null &&
|
|
85
|
+
"externalResponseCapacity" in context &&
|
|
86
|
+
context
|
|
87
|
+
.externalResponseCapacity !== "available") {
|
|
88
|
+
return {
|
|
89
|
+
status: "blocked",
|
|
90
|
+
reason: "Institutional context does not provide the required external response capacity.",
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
status: "approved",
|
|
97
|
+
reason: "Execution satisfies the supplied institutional governance boundary.",
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function createClient() {
|
|
101
|
+
const executions = [];
|
|
102
|
+
const metadata = new Map();
|
|
103
|
+
async function execute(request, previousExecutionId) {
|
|
104
|
+
const executionId = createExecutionId();
|
|
105
|
+
const decision = evaluateGovernance(request);
|
|
106
|
+
metadata.set(executionId, {
|
|
107
|
+
institution: request.institution,
|
|
108
|
+
scenario: request.scenario,
|
|
109
|
+
institutionalInputs: request.institutionalInputs ?? [],
|
|
110
|
+
});
|
|
111
|
+
if (decision.status !== "approved") {
|
|
112
|
+
const result = {
|
|
113
|
+
executionId,
|
|
114
|
+
institutionId: request.institution.id,
|
|
115
|
+
scenarioId: request.scenario.id,
|
|
116
|
+
context: request.context,
|
|
117
|
+
aiInput: request.aiInput,
|
|
118
|
+
decision,
|
|
119
|
+
action: {
|
|
120
|
+
status: "not-executed",
|
|
121
|
+
},
|
|
122
|
+
outcome: {
|
|
123
|
+
status: "not-executed",
|
|
124
|
+
},
|
|
125
|
+
evidence: {
|
|
126
|
+
available: true,
|
|
127
|
+
},
|
|
128
|
+
...(previousExecutionId
|
|
129
|
+
? { previousExecutionId }
|
|
130
|
+
: {}),
|
|
131
|
+
};
|
|
132
|
+
executions.push(result);
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
const capability = request.capabilities[0];
|
|
136
|
+
if (!capability) {
|
|
137
|
+
const result = {
|
|
138
|
+
executionId,
|
|
139
|
+
institutionId: request.institution.id,
|
|
140
|
+
scenarioId: request.scenario.id,
|
|
141
|
+
context: request.context,
|
|
142
|
+
aiInput: request.aiInput,
|
|
143
|
+
decision: {
|
|
144
|
+
status: "blocked",
|
|
145
|
+
reason: "No application capability was supplied for governed execution.",
|
|
146
|
+
},
|
|
147
|
+
action: {
|
|
148
|
+
status: "not-executed",
|
|
149
|
+
},
|
|
150
|
+
outcome: {
|
|
151
|
+
status: "not-executed",
|
|
152
|
+
},
|
|
153
|
+
evidence: {
|
|
154
|
+
available: true,
|
|
155
|
+
},
|
|
156
|
+
...(previousExecutionId
|
|
157
|
+
? { previousExecutionId }
|
|
158
|
+
: {}),
|
|
159
|
+
};
|
|
160
|
+
executions.push(result);
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
const resultValue = await capability.execute(request.aiInput.recommendation);
|
|
165
|
+
const result = {
|
|
166
|
+
executionId,
|
|
167
|
+
institutionId: request.institution.id,
|
|
168
|
+
scenarioId: request.scenario.id,
|
|
169
|
+
context: request.context,
|
|
170
|
+
aiInput: request.aiInput,
|
|
171
|
+
decision,
|
|
172
|
+
action: {
|
|
173
|
+
capability: capability.name,
|
|
174
|
+
status: "executed",
|
|
175
|
+
result: resultValue,
|
|
176
|
+
},
|
|
177
|
+
outcome: {
|
|
178
|
+
status: "completed",
|
|
179
|
+
},
|
|
180
|
+
evidence: {
|
|
181
|
+
available: true,
|
|
182
|
+
},
|
|
183
|
+
...(previousExecutionId
|
|
184
|
+
? { previousExecutionId }
|
|
185
|
+
: {}),
|
|
186
|
+
};
|
|
187
|
+
executions.push(result);
|
|
188
|
+
return result;
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
const result = {
|
|
192
|
+
executionId,
|
|
193
|
+
institutionId: request.institution.id,
|
|
194
|
+
scenarioId: request.scenario.id,
|
|
195
|
+
context: request.context,
|
|
196
|
+
aiInput: request.aiInput,
|
|
197
|
+
decision,
|
|
198
|
+
action: {
|
|
199
|
+
capability: capability.name,
|
|
200
|
+
status: "not-executed",
|
|
201
|
+
},
|
|
202
|
+
outcome: {
|
|
203
|
+
status: "failed",
|
|
204
|
+
},
|
|
205
|
+
evidence: {
|
|
206
|
+
available: true,
|
|
207
|
+
},
|
|
208
|
+
...(previousExecutionId
|
|
209
|
+
? { previousExecutionId }
|
|
210
|
+
: {}),
|
|
211
|
+
};
|
|
212
|
+
executions.push(result);
|
|
213
|
+
return result;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
execute,
|
|
218
|
+
async reevaluate(request) {
|
|
219
|
+
const original = metadata.get(request.execution.executionId);
|
|
220
|
+
if (original) {
|
|
221
|
+
return execute({
|
|
222
|
+
institution: original.institution,
|
|
223
|
+
scenario: original.scenario,
|
|
224
|
+
context: request.context,
|
|
225
|
+
institutionalInputs: request.institutionalInputs,
|
|
226
|
+
aiInput: request.aiInput,
|
|
227
|
+
governance: request.governance,
|
|
228
|
+
authority: request.authority,
|
|
229
|
+
capabilities: request.capabilities,
|
|
230
|
+
}, request.execution.executionId);
|
|
231
|
+
}
|
|
232
|
+
return execute({
|
|
233
|
+
institution: {
|
|
234
|
+
id: request.execution.institutionId,
|
|
235
|
+
name: request.execution.institutionId,
|
|
236
|
+
},
|
|
237
|
+
scenario: {
|
|
238
|
+
id: request.execution.scenarioId,
|
|
239
|
+
name: request.execution.scenarioId,
|
|
240
|
+
version: "unknown",
|
|
241
|
+
stages: [],
|
|
242
|
+
},
|
|
243
|
+
context: request.context,
|
|
244
|
+
institutionalInputs: request.institutionalInputs,
|
|
245
|
+
aiInput: request.aiInput,
|
|
246
|
+
governance: request.governance,
|
|
247
|
+
authority: request.authority,
|
|
248
|
+
capabilities: request.capabilities,
|
|
249
|
+
}, request.execution.executionId);
|
|
250
|
+
},
|
|
251
|
+
async history({ institution, scenario }) {
|
|
252
|
+
return {
|
|
253
|
+
institutionId: institution.id,
|
|
254
|
+
scenarioId: scenario.id,
|
|
255
|
+
executions: executions.filter((execution) => execution.institutionId === institution.id &&
|
|
256
|
+
execution.scenarioId === scenario.id),
|
|
257
|
+
};
|
|
258
|
+
},
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
function createZensorum() {
|
|
262
|
+
return {
|
|
263
|
+
institution(input) {
|
|
264
|
+
return {
|
|
265
|
+
id: input.id,
|
|
266
|
+
name: input.name,
|
|
267
|
+
};
|
|
268
|
+
},
|
|
269
|
+
scenario(input) {
|
|
270
|
+
return {
|
|
271
|
+
id: input.id,
|
|
272
|
+
name: input.name,
|
|
273
|
+
version: input.version,
|
|
274
|
+
stages: [...input.stages],
|
|
275
|
+
};
|
|
276
|
+
},
|
|
277
|
+
governance(input) {
|
|
278
|
+
return {
|
|
279
|
+
policies: [...input.policies],
|
|
280
|
+
};
|
|
281
|
+
},
|
|
282
|
+
authority(input) {
|
|
283
|
+
return {
|
|
284
|
+
id: input.id,
|
|
285
|
+
name: input.name,
|
|
286
|
+
};
|
|
287
|
+
},
|
|
288
|
+
client: createClient,
|
|
289
|
+
};
|
|
290
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zensorum/sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Developer SDK for building governed institutional applications with Zensorum.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/khpf88/zensorum-public-sdk.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/khpf88/zensorum-public-sdk",
|
|
11
|
+
"main": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc",
|
|
27
|
+
"typecheck": "tsc --noEmit"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"typescript": "^5.0.0"
|
|
31
|
+
}
|
|
32
|
+
}
|