@nexart/ai-execution 0.3.0 → 0.4.1
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 +44 -9
- package/dist/index.cjs +696 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +56 -0
- package/dist/index.d.ts +55 -11
- package/dist/index.mjs +638 -0
- package/dist/index.mjs.map +1 -0
- package/dist/providers/anthropic.cjs +258 -0
- package/dist/providers/anthropic.cjs.map +1 -0
- package/dist/providers/anthropic.d.cts +24 -0
- package/dist/providers/anthropic.d.ts +7 -5
- package/dist/providers/anthropic.mjs +221 -0
- package/dist/providers/anthropic.mjs.map +1 -0
- package/dist/providers/openai.cjs +259 -0
- package/dist/providers/openai.cjs.map +1 -0
- package/dist/providers/openai.d.cts +24 -0
- package/dist/providers/openai.d.ts +7 -5
- package/dist/providers/openai.mjs +222 -0
- package/dist/providers/openai.mjs.map +1 -0
- package/dist/providers/wrap.cjs +221 -0
- package/dist/providers/wrap.cjs.map +1 -0
- package/dist/providers/wrap.d.cts +9 -0
- package/dist/providers/wrap.d.ts +5 -3
- package/dist/providers/wrap.mjs +186 -0
- package/dist/providers/wrap.mjs.map +1 -0
- package/dist/{types.d.ts → types-342Snbrb.d.cts} +31 -16
- package/dist/types-342Snbrb.d.ts +169 -0
- package/package.json +18 -11
- package/dist/archive.d.ts +0 -4
- package/dist/archive.d.ts.map +0 -1
- package/dist/archive.js +0 -28
- package/dist/archive.js.map +0 -1
- package/dist/attest.d.ts +0 -3
- package/dist/attest.d.ts.map +0 -1
- package/dist/attest.js +0 -79
- package/dist/attest.js.map +0 -1
- package/dist/canonicalJson.d.ts +0 -2
- package/dist/canonicalJson.d.ts.map +0 -1
- package/dist/canonicalJson.js +0 -38
- package/dist/canonicalJson.js.map +0 -1
- package/dist/cer.d.ts +0 -7
- package/dist/cer.d.ts.map +0 -1
- package/dist/cer.js +0 -61
- package/dist/cer.js.map +0 -1
- package/dist/certify.d.ts +0 -3
- package/dist/certify.d.ts.map +0 -1
- package/dist/certify.js +0 -27
- package/dist/certify.js.map +0 -1
- package/dist/errors.d.ts +0 -11
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js +0 -21
- package/dist/errors.js.map +0 -1
- package/dist/hash.d.ts +0 -6
- package/dist/hash.d.ts.map +0 -1
- package/dist/hash.js +0 -32
- package/dist/hash.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -11
- package/dist/index.js.map +0 -1
- package/dist/providers/anthropic.d.ts.map +0 -1
- package/dist/providers/anthropic.js +0 -61
- package/dist/providers/anthropic.js.map +0 -1
- package/dist/providers/openai.d.ts.map +0 -1
- package/dist/providers/openai.js +0 -62
- package/dist/providers/openai.js.map +0 -1
- package/dist/providers/wrap.d.ts.map +0 -1
- package/dist/providers/wrap.js +0 -28
- package/dist/providers/wrap.js.map +0 -1
- package/dist/run.d.ts +0 -14
- package/dist/run.d.ts.map +0 -1
- package/dist/run.js +0 -62
- package/dist/run.js.map +0 -1
- package/dist/snapshot.d.ts +0 -4
- package/dist/snapshot.d.ts.map +0 -1
- package/dist/snapshot.js +0 -113
- package/dist/snapshot.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,696 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
CerAttestationError: () => CerAttestationError,
|
|
34
|
+
CerVerificationError: () => CerVerificationError,
|
|
35
|
+
CerVerifyCode: () => CerVerifyCode,
|
|
36
|
+
RunBuilder: () => RunBuilder,
|
|
37
|
+
attest: () => attest,
|
|
38
|
+
certifyDecision: () => certifyDecision,
|
|
39
|
+
computeInputHash: () => computeInputHash,
|
|
40
|
+
computeOutputHash: () => computeOutputHash,
|
|
41
|
+
createSnapshot: () => createSnapshot,
|
|
42
|
+
exportCer: () => exportCer,
|
|
43
|
+
hasAttestation: () => hasAttestation,
|
|
44
|
+
hashCanonicalJson: () => hashCanonicalJson,
|
|
45
|
+
hashUtf8: () => hashUtf8,
|
|
46
|
+
importCer: () => importCer,
|
|
47
|
+
sanitizeForAttestation: () => sanitizeForAttestation,
|
|
48
|
+
sealCer: () => sealCer,
|
|
49
|
+
sha256Hex: () => sha256Hex,
|
|
50
|
+
toCanonicalJson: () => toCanonicalJson,
|
|
51
|
+
verify: () => verifyCer,
|
|
52
|
+
verifyCer: () => verifyCer,
|
|
53
|
+
verifySnapshot: () => verifySnapshot,
|
|
54
|
+
wrapProvider: () => wrapProvider
|
|
55
|
+
});
|
|
56
|
+
module.exports = __toCommonJS(src_exports);
|
|
57
|
+
|
|
58
|
+
// src/types.ts
|
|
59
|
+
var CerVerifyCode = {
|
|
60
|
+
OK: "OK",
|
|
61
|
+
CERTIFICATE_HASH_MISMATCH: "CERTIFICATE_HASH_MISMATCH",
|
|
62
|
+
SNAPSHOT_HASH_MISMATCH: "SNAPSHOT_HASH_MISMATCH",
|
|
63
|
+
INPUT_HASH_MISMATCH: "INPUT_HASH_MISMATCH",
|
|
64
|
+
OUTPUT_HASH_MISMATCH: "OUTPUT_HASH_MISMATCH",
|
|
65
|
+
INVALID_SHA256_FORMAT: "INVALID_SHA256_FORMAT",
|
|
66
|
+
CANONICALIZATION_ERROR: "CANONICALIZATION_ERROR",
|
|
67
|
+
SCHEMA_ERROR: "SCHEMA_ERROR",
|
|
68
|
+
UNKNOWN_ERROR: "UNKNOWN_ERROR"
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// src/errors.ts
|
|
72
|
+
var CerVerificationError = class extends Error {
|
|
73
|
+
errors;
|
|
74
|
+
constructor(errors) {
|
|
75
|
+
super(`CER verification failed: ${errors.join("; ")}`);
|
|
76
|
+
this.name = "CerVerificationError";
|
|
77
|
+
this.errors = errors;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
var CerAttestationError = class extends Error {
|
|
81
|
+
statusCode;
|
|
82
|
+
responseBody;
|
|
83
|
+
details;
|
|
84
|
+
constructor(message, statusCode, responseBody, details) {
|
|
85
|
+
super(message);
|
|
86
|
+
this.name = "CerAttestationError";
|
|
87
|
+
this.statusCode = statusCode;
|
|
88
|
+
this.responseBody = responseBody;
|
|
89
|
+
this.details = details;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// src/canonicalJson.ts
|
|
94
|
+
function toCanonicalJson(value) {
|
|
95
|
+
return canonicalize(value);
|
|
96
|
+
}
|
|
97
|
+
function canonicalize(value) {
|
|
98
|
+
if (value === null) {
|
|
99
|
+
return "null";
|
|
100
|
+
}
|
|
101
|
+
if (typeof value === "boolean") {
|
|
102
|
+
return value ? "true" : "false";
|
|
103
|
+
}
|
|
104
|
+
if (typeof value === "number") {
|
|
105
|
+
if (!Number.isFinite(value)) {
|
|
106
|
+
throw new Error(`Non-finite number not allowed in canonical JSON: ${value}`);
|
|
107
|
+
}
|
|
108
|
+
return JSON.stringify(value);
|
|
109
|
+
}
|
|
110
|
+
if (typeof value === "string") {
|
|
111
|
+
return JSON.stringify(value);
|
|
112
|
+
}
|
|
113
|
+
if (Array.isArray(value)) {
|
|
114
|
+
const items = value.map((item) => canonicalize(item));
|
|
115
|
+
return "[" + items.join(",") + "]";
|
|
116
|
+
}
|
|
117
|
+
if (typeof value === "object") {
|
|
118
|
+
const obj = value;
|
|
119
|
+
const keys = Object.keys(obj).sort();
|
|
120
|
+
const entries = keys.map((key) => {
|
|
121
|
+
const val = obj[key];
|
|
122
|
+
if (val === void 0) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
return JSON.stringify(key) + ":" + canonicalize(val);
|
|
126
|
+
}).filter((e) => e !== null);
|
|
127
|
+
return "{" + entries.join(",") + "}";
|
|
128
|
+
}
|
|
129
|
+
throw new Error(`Unsupported type for canonical JSON: ${typeof value}`);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// src/hash.ts
|
|
133
|
+
var crypto = __toESM(require("crypto"), 1);
|
|
134
|
+
function sha256Hex(data) {
|
|
135
|
+
const hash = crypto.createHash("sha256");
|
|
136
|
+
if (typeof data === "string") {
|
|
137
|
+
hash.update(data, "utf-8");
|
|
138
|
+
} else {
|
|
139
|
+
hash.update(data);
|
|
140
|
+
}
|
|
141
|
+
return hash.digest("hex");
|
|
142
|
+
}
|
|
143
|
+
function hashUtf8(value) {
|
|
144
|
+
return `sha256:${sha256Hex(value)}`;
|
|
145
|
+
}
|
|
146
|
+
function hashCanonicalJson(value) {
|
|
147
|
+
const canonical = toCanonicalJson(value);
|
|
148
|
+
return `sha256:${sha256Hex(canonical)}`;
|
|
149
|
+
}
|
|
150
|
+
function computeInputHash(input) {
|
|
151
|
+
if (typeof input === "string") {
|
|
152
|
+
return hashUtf8(input);
|
|
153
|
+
}
|
|
154
|
+
return hashCanonicalJson(input);
|
|
155
|
+
}
|
|
156
|
+
function computeOutputHash(output) {
|
|
157
|
+
if (typeof output === "string") {
|
|
158
|
+
return hashUtf8(output);
|
|
159
|
+
}
|
|
160
|
+
return hashCanonicalJson(output);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// src/snapshot.ts
|
|
164
|
+
var PACKAGE_VERSION = "0.4.1";
|
|
165
|
+
function validateParameters(params) {
|
|
166
|
+
const errors = [];
|
|
167
|
+
if (typeof params.temperature !== "number" || !Number.isFinite(params.temperature)) {
|
|
168
|
+
errors.push(`parameters.temperature must be a finite number, got: ${params.temperature}`);
|
|
169
|
+
}
|
|
170
|
+
if (typeof params.maxTokens !== "number" || !Number.isFinite(params.maxTokens)) {
|
|
171
|
+
errors.push(`parameters.maxTokens must be a finite number, got: ${params.maxTokens}`);
|
|
172
|
+
}
|
|
173
|
+
if (params.topP !== null && (typeof params.topP !== "number" || !Number.isFinite(params.topP))) {
|
|
174
|
+
errors.push(`parameters.topP must be a finite number or null, got: ${params.topP}`);
|
|
175
|
+
}
|
|
176
|
+
if (params.seed !== null && (typeof params.seed !== "number" || !Number.isFinite(params.seed))) {
|
|
177
|
+
errors.push(`parameters.seed must be a finite number or null, got: ${params.seed}`);
|
|
178
|
+
}
|
|
179
|
+
return errors;
|
|
180
|
+
}
|
|
181
|
+
function createSnapshot(params) {
|
|
182
|
+
const paramErrors = validateParameters(params.parameters);
|
|
183
|
+
if (paramErrors.length > 0) {
|
|
184
|
+
throw new Error(`Invalid parameters: ${paramErrors.join("; ")}`);
|
|
185
|
+
}
|
|
186
|
+
const inputHash = computeInputHash(params.input);
|
|
187
|
+
const outputHash = computeOutputHash(params.output);
|
|
188
|
+
const snapshot = {
|
|
189
|
+
type: "ai.execution.v1",
|
|
190
|
+
protocolVersion: "1.2.0",
|
|
191
|
+
executionSurface: "ai",
|
|
192
|
+
executionId: params.executionId,
|
|
193
|
+
timestamp: params.timestamp ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
194
|
+
provider: params.provider,
|
|
195
|
+
model: params.model,
|
|
196
|
+
modelVersion: params.modelVersion ?? null,
|
|
197
|
+
prompt: params.prompt,
|
|
198
|
+
input: params.input,
|
|
199
|
+
inputHash,
|
|
200
|
+
parameters: {
|
|
201
|
+
temperature: params.parameters.temperature,
|
|
202
|
+
maxTokens: params.parameters.maxTokens,
|
|
203
|
+
topP: params.parameters.topP ?? null,
|
|
204
|
+
seed: params.parameters.seed ?? null
|
|
205
|
+
},
|
|
206
|
+
output: params.output,
|
|
207
|
+
outputHash,
|
|
208
|
+
sdkVersion: params.sdkVersion ?? PACKAGE_VERSION,
|
|
209
|
+
appId: params.appId ?? null
|
|
210
|
+
};
|
|
211
|
+
if (params.runId !== void 0) snapshot.runId = params.runId ?? null;
|
|
212
|
+
if (params.stepId !== void 0) snapshot.stepId = params.stepId ?? null;
|
|
213
|
+
if (params.stepIndex !== void 0) snapshot.stepIndex = params.stepIndex ?? null;
|
|
214
|
+
if (params.workflowId !== void 0) snapshot.workflowId = params.workflowId ?? null;
|
|
215
|
+
if (params.conversationId !== void 0) snapshot.conversationId = params.conversationId ?? null;
|
|
216
|
+
if (params.prevStepHash !== void 0) snapshot.prevStepHash = params.prevStepHash ?? null;
|
|
217
|
+
return snapshot;
|
|
218
|
+
}
|
|
219
|
+
function verifySnapshot(snapshot) {
|
|
220
|
+
const schemaErrors = [];
|
|
221
|
+
const formatErrors = [];
|
|
222
|
+
const inputHashErrors = [];
|
|
223
|
+
const outputHashErrors = [];
|
|
224
|
+
if (snapshot.type !== "ai.execution.v1") {
|
|
225
|
+
schemaErrors.push(`Expected type "ai.execution.v1", got "${snapshot.type}"`);
|
|
226
|
+
}
|
|
227
|
+
if (snapshot.protocolVersion !== "1.2.0") {
|
|
228
|
+
schemaErrors.push(`Expected protocolVersion "1.2.0", got "${snapshot.protocolVersion}"`);
|
|
229
|
+
}
|
|
230
|
+
if (snapshot.executionSurface !== "ai") {
|
|
231
|
+
schemaErrors.push(`Expected executionSurface "ai", got "${snapshot.executionSurface}"`);
|
|
232
|
+
}
|
|
233
|
+
if (!snapshot.executionId || typeof snapshot.executionId !== "string") {
|
|
234
|
+
schemaErrors.push("executionId must be a non-empty string");
|
|
235
|
+
}
|
|
236
|
+
if (!snapshot.timestamp || typeof snapshot.timestamp !== "string") {
|
|
237
|
+
schemaErrors.push("timestamp must be a non-empty string");
|
|
238
|
+
}
|
|
239
|
+
if (!snapshot.provider || typeof snapshot.provider !== "string") {
|
|
240
|
+
schemaErrors.push("provider must be a non-empty string");
|
|
241
|
+
}
|
|
242
|
+
if (!snapshot.model || typeof snapshot.model !== "string") {
|
|
243
|
+
schemaErrors.push("model must be a non-empty string");
|
|
244
|
+
}
|
|
245
|
+
if (!snapshot.prompt || typeof snapshot.prompt !== "string") {
|
|
246
|
+
schemaErrors.push("prompt must be a non-empty string");
|
|
247
|
+
}
|
|
248
|
+
if (snapshot.input === void 0 || snapshot.input === null) {
|
|
249
|
+
schemaErrors.push("input must be a string or object");
|
|
250
|
+
}
|
|
251
|
+
if (snapshot.output === void 0 || snapshot.output === null) {
|
|
252
|
+
schemaErrors.push("output must be a string or object");
|
|
253
|
+
}
|
|
254
|
+
const paramErrors = validateParameters(snapshot.parameters);
|
|
255
|
+
schemaErrors.push(...paramErrors);
|
|
256
|
+
if (!snapshot.inputHash || !snapshot.inputHash.startsWith("sha256:")) {
|
|
257
|
+
formatErrors.push(`inputHash must start with "sha256:", got "${snapshot.inputHash}"`);
|
|
258
|
+
}
|
|
259
|
+
if (!snapshot.outputHash || !snapshot.outputHash.startsWith("sha256:")) {
|
|
260
|
+
formatErrors.push(`outputHash must start with "sha256:", got "${snapshot.outputHash}"`);
|
|
261
|
+
}
|
|
262
|
+
if (formatErrors.length === 0) {
|
|
263
|
+
const expectedInputHash = computeInputHash(snapshot.input);
|
|
264
|
+
if (snapshot.inputHash !== expectedInputHash) {
|
|
265
|
+
inputHashErrors.push(`inputHash mismatch: expected ${expectedInputHash}, got ${snapshot.inputHash}`);
|
|
266
|
+
}
|
|
267
|
+
const expectedOutputHash = computeOutputHash(snapshot.output);
|
|
268
|
+
if (snapshot.outputHash !== expectedOutputHash) {
|
|
269
|
+
outputHashErrors.push(`outputHash mismatch: expected ${expectedOutputHash}, got ${snapshot.outputHash}`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
const errors = [...schemaErrors, ...formatErrors, ...inputHashErrors, ...outputHashErrors];
|
|
273
|
+
if (errors.length === 0) {
|
|
274
|
+
return { ok: true, errors: [], code: CerVerifyCode.OK };
|
|
275
|
+
}
|
|
276
|
+
let code;
|
|
277
|
+
let details;
|
|
278
|
+
if (schemaErrors.length > 0) {
|
|
279
|
+
code = CerVerifyCode.SCHEMA_ERROR;
|
|
280
|
+
details = schemaErrors;
|
|
281
|
+
} else if (formatErrors.length > 0) {
|
|
282
|
+
code = CerVerifyCode.INVALID_SHA256_FORMAT;
|
|
283
|
+
details = formatErrors;
|
|
284
|
+
} else if (inputHashErrors.length > 0 && outputHashErrors.length > 0) {
|
|
285
|
+
code = CerVerifyCode.SNAPSHOT_HASH_MISMATCH;
|
|
286
|
+
details = [...inputHashErrors, ...outputHashErrors];
|
|
287
|
+
} else if (inputHashErrors.length > 0) {
|
|
288
|
+
code = CerVerifyCode.INPUT_HASH_MISMATCH;
|
|
289
|
+
details = inputHashErrors;
|
|
290
|
+
} else if (outputHashErrors.length > 0) {
|
|
291
|
+
code = CerVerifyCode.OUTPUT_HASH_MISMATCH;
|
|
292
|
+
details = outputHashErrors;
|
|
293
|
+
} else {
|
|
294
|
+
code = CerVerifyCode.UNKNOWN_ERROR;
|
|
295
|
+
details = errors;
|
|
296
|
+
}
|
|
297
|
+
return { ok: false, errors, code, details };
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// src/cer.ts
|
|
301
|
+
function computeCertificateHash(payload) {
|
|
302
|
+
const canonical = toCanonicalJson(payload);
|
|
303
|
+
return `sha256:${sha256Hex(canonical)}`;
|
|
304
|
+
}
|
|
305
|
+
function sealCer(snapshot, options) {
|
|
306
|
+
const createdAt = options?.createdAt ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
307
|
+
const payload = {
|
|
308
|
+
bundleType: "cer.ai.execution.v1",
|
|
309
|
+
createdAt,
|
|
310
|
+
snapshot,
|
|
311
|
+
version: "0.1"
|
|
312
|
+
};
|
|
313
|
+
const certificateHash = computeCertificateHash(payload);
|
|
314
|
+
const bundle = {
|
|
315
|
+
bundleType: "cer.ai.execution.v1",
|
|
316
|
+
certificateHash,
|
|
317
|
+
createdAt,
|
|
318
|
+
version: "0.1",
|
|
319
|
+
snapshot
|
|
320
|
+
};
|
|
321
|
+
if (options?.meta) {
|
|
322
|
+
bundle.meta = options.meta;
|
|
323
|
+
}
|
|
324
|
+
return bundle;
|
|
325
|
+
}
|
|
326
|
+
function verifyCer(bundle) {
|
|
327
|
+
const schemaErrors = [];
|
|
328
|
+
const formatErrors = [];
|
|
329
|
+
if (bundle.bundleType !== "cer.ai.execution.v1") {
|
|
330
|
+
schemaErrors.push(`Expected bundleType "cer.ai.execution.v1", got "${bundle.bundleType}"`);
|
|
331
|
+
}
|
|
332
|
+
if (bundle.version !== "0.1") {
|
|
333
|
+
schemaErrors.push(`Expected version "0.1", got "${bundle.version}"`);
|
|
334
|
+
}
|
|
335
|
+
if (!bundle.createdAt || typeof bundle.createdAt !== "string") {
|
|
336
|
+
schemaErrors.push("createdAt must be a non-empty string");
|
|
337
|
+
}
|
|
338
|
+
if (!bundle.certificateHash || !bundle.certificateHash.startsWith("sha256:")) {
|
|
339
|
+
formatErrors.push(`certificateHash must start with "sha256:", got "${bundle.certificateHash}"`);
|
|
340
|
+
}
|
|
341
|
+
if (!bundle.snapshot) {
|
|
342
|
+
schemaErrors.push("snapshot is required");
|
|
343
|
+
const allErrors = [...schemaErrors, ...formatErrors];
|
|
344
|
+
return { ok: false, errors: allErrors, code: CerVerifyCode.SCHEMA_ERROR, details: schemaErrors };
|
|
345
|
+
}
|
|
346
|
+
let canonicalizationError = null;
|
|
347
|
+
let snapshotResult = null;
|
|
348
|
+
try {
|
|
349
|
+
snapshotResult = verifySnapshot(bundle.snapshot);
|
|
350
|
+
} catch (err) {
|
|
351
|
+
canonicalizationError = err instanceof Error ? err.message : String(err);
|
|
352
|
+
}
|
|
353
|
+
if (canonicalizationError !== null) {
|
|
354
|
+
const errors2 = [...schemaErrors, ...formatErrors, canonicalizationError];
|
|
355
|
+
return { ok: false, errors: errors2, code: CerVerifyCode.CANONICALIZATION_ERROR, details: [canonicalizationError] };
|
|
356
|
+
}
|
|
357
|
+
const snapshotErrors = snapshotResult.errors;
|
|
358
|
+
const certHashErrors = [];
|
|
359
|
+
try {
|
|
360
|
+
const payload = {
|
|
361
|
+
bundleType: "cer.ai.execution.v1",
|
|
362
|
+
createdAt: bundle.createdAt,
|
|
363
|
+
snapshot: bundle.snapshot,
|
|
364
|
+
version: "0.1"
|
|
365
|
+
};
|
|
366
|
+
const expectedHash = computeCertificateHash(payload);
|
|
367
|
+
if (bundle.certificateHash !== expectedHash) {
|
|
368
|
+
certHashErrors.push(`certificateHash mismatch: expected ${expectedHash}, got ${bundle.certificateHash}`);
|
|
369
|
+
}
|
|
370
|
+
} catch (err) {
|
|
371
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
372
|
+
const errors2 = [...schemaErrors, ...formatErrors, ...snapshotErrors, msg];
|
|
373
|
+
return { ok: false, errors: errors2, code: CerVerifyCode.CANONICALIZATION_ERROR, details: [msg] };
|
|
374
|
+
}
|
|
375
|
+
const errors = [...schemaErrors, ...formatErrors, ...snapshotErrors, ...certHashErrors];
|
|
376
|
+
if (errors.length === 0) {
|
|
377
|
+
return { ok: true, errors: [], code: CerVerifyCode.OK };
|
|
378
|
+
}
|
|
379
|
+
let code;
|
|
380
|
+
let details;
|
|
381
|
+
if (schemaErrors.length > 0) {
|
|
382
|
+
code = CerVerifyCode.SCHEMA_ERROR;
|
|
383
|
+
details = schemaErrors;
|
|
384
|
+
} else if (formatErrors.length > 0) {
|
|
385
|
+
code = CerVerifyCode.INVALID_SHA256_FORMAT;
|
|
386
|
+
details = formatErrors;
|
|
387
|
+
} else if (certHashErrors.length > 0 && snapshotErrors.length === 0) {
|
|
388
|
+
code = CerVerifyCode.CERTIFICATE_HASH_MISMATCH;
|
|
389
|
+
details = certHashErrors;
|
|
390
|
+
} else if (snapshotResult && snapshotResult.code !== CerVerifyCode.OK) {
|
|
391
|
+
code = snapshotResult.code;
|
|
392
|
+
details = snapshotResult.details ?? snapshotErrors;
|
|
393
|
+
} else if (certHashErrors.length > 0) {
|
|
394
|
+
code = CerVerifyCode.CERTIFICATE_HASH_MISMATCH;
|
|
395
|
+
details = certHashErrors;
|
|
396
|
+
} else {
|
|
397
|
+
code = CerVerifyCode.UNKNOWN_ERROR;
|
|
398
|
+
details = errors;
|
|
399
|
+
}
|
|
400
|
+
return { ok: false, errors, code, details };
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// src/certify.ts
|
|
404
|
+
var crypto2 = __toESM(require("crypto"), 1);
|
|
405
|
+
function certifyDecision(params) {
|
|
406
|
+
const executionId = params.executionId ?? crypto2.randomUUID();
|
|
407
|
+
const snapshot = createSnapshot({
|
|
408
|
+
executionId,
|
|
409
|
+
timestamp: params.timestamp,
|
|
410
|
+
provider: params.provider,
|
|
411
|
+
model: params.model,
|
|
412
|
+
modelVersion: params.modelVersion,
|
|
413
|
+
prompt: params.prompt,
|
|
414
|
+
input: params.input,
|
|
415
|
+
parameters: params.parameters,
|
|
416
|
+
output: params.output,
|
|
417
|
+
sdkVersion: params.sdkVersion,
|
|
418
|
+
appId: params.appId,
|
|
419
|
+
runId: params.runId,
|
|
420
|
+
stepId: params.stepId,
|
|
421
|
+
stepIndex: params.stepIndex,
|
|
422
|
+
workflowId: params.workflowId,
|
|
423
|
+
conversationId: params.conversationId,
|
|
424
|
+
prevStepHash: params.prevStepHash
|
|
425
|
+
});
|
|
426
|
+
return sealCer(snapshot, { meta: params.meta });
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// src/run.ts
|
|
430
|
+
var crypto3 = __toESM(require("crypto"), 1);
|
|
431
|
+
var RunBuilder = class {
|
|
432
|
+
runId;
|
|
433
|
+
workflowId;
|
|
434
|
+
conversationId;
|
|
435
|
+
appId;
|
|
436
|
+
stepIndex = 0;
|
|
437
|
+
prevStepHash = null;
|
|
438
|
+
steps = [];
|
|
439
|
+
constructor(options) {
|
|
440
|
+
this.runId = options?.runId ?? crypto3.randomUUID();
|
|
441
|
+
this.workflowId = options?.workflowId ?? null;
|
|
442
|
+
this.conversationId = options?.conversationId ?? null;
|
|
443
|
+
this.appId = options?.appId ?? null;
|
|
444
|
+
}
|
|
445
|
+
step(params) {
|
|
446
|
+
const stepId = params.stepId ?? crypto3.randomUUID();
|
|
447
|
+
const executionId = `${this.runId}-step-${this.stepIndex}`;
|
|
448
|
+
const snapshot = createSnapshot({
|
|
449
|
+
executionId,
|
|
450
|
+
timestamp: params.timestamp,
|
|
451
|
+
provider: params.provider,
|
|
452
|
+
model: params.model,
|
|
453
|
+
modelVersion: params.modelVersion,
|
|
454
|
+
prompt: params.prompt,
|
|
455
|
+
input: params.input,
|
|
456
|
+
parameters: params.parameters,
|
|
457
|
+
output: params.output,
|
|
458
|
+
appId: this.appId,
|
|
459
|
+
runId: this.runId,
|
|
460
|
+
stepId,
|
|
461
|
+
stepIndex: this.stepIndex,
|
|
462
|
+
workflowId: this.workflowId,
|
|
463
|
+
conversationId: this.conversationId,
|
|
464
|
+
prevStepHash: this.prevStepHash
|
|
465
|
+
});
|
|
466
|
+
const bundle = sealCer(snapshot, { meta: params.meta });
|
|
467
|
+
this.steps.push({
|
|
468
|
+
stepIndex: this.stepIndex,
|
|
469
|
+
stepId,
|
|
470
|
+
executionId,
|
|
471
|
+
certificateHash: bundle.certificateHash,
|
|
472
|
+
prevStepHash: this.prevStepHash
|
|
473
|
+
});
|
|
474
|
+
this.prevStepHash = bundle.certificateHash;
|
|
475
|
+
this.stepIndex++;
|
|
476
|
+
return bundle;
|
|
477
|
+
}
|
|
478
|
+
finalize() {
|
|
479
|
+
return {
|
|
480
|
+
runId: this.runId,
|
|
481
|
+
workflowId: this.workflowId,
|
|
482
|
+
conversationId: this.conversationId,
|
|
483
|
+
stepCount: this.steps.length,
|
|
484
|
+
steps: [...this.steps],
|
|
485
|
+
finalStepHash: this.prevStepHash
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
// src/sanitize.ts
|
|
491
|
+
function deepRemoveUndefined(value) {
|
|
492
|
+
if (value === null || value === void 0) return value;
|
|
493
|
+
if (typeof value === "bigint") {
|
|
494
|
+
throw new Error("BigInt values are not JSON-safe and cannot be sanitized");
|
|
495
|
+
}
|
|
496
|
+
if (typeof value === "function") {
|
|
497
|
+
throw new Error("Function values are not JSON-safe and cannot be sanitized");
|
|
498
|
+
}
|
|
499
|
+
if (typeof value === "symbol") {
|
|
500
|
+
throw new Error("Symbol values are not JSON-safe and cannot be sanitized");
|
|
501
|
+
}
|
|
502
|
+
if (Array.isArray(value)) {
|
|
503
|
+
return value.map(deepRemoveUndefined);
|
|
504
|
+
}
|
|
505
|
+
if (typeof value === "object") {
|
|
506
|
+
const result = {};
|
|
507
|
+
for (const [key, val] of Object.entries(value)) {
|
|
508
|
+
if (val === void 0) continue;
|
|
509
|
+
result[key] = deepRemoveUndefined(val);
|
|
510
|
+
}
|
|
511
|
+
return result;
|
|
512
|
+
}
|
|
513
|
+
return value;
|
|
514
|
+
}
|
|
515
|
+
function sanitizeForAttestation(bundle) {
|
|
516
|
+
return deepRemoveUndefined(bundle);
|
|
517
|
+
}
|
|
518
|
+
function hasAttestation(bundle) {
|
|
519
|
+
if (typeof bundle !== "object" || bundle === null) return false;
|
|
520
|
+
const b = bundle;
|
|
521
|
+
if (typeof b.attestationId === "string" && b.attestationId.length > 0) return true;
|
|
522
|
+
if (typeof b.nodeRuntimeHash === "string" && b.nodeRuntimeHash.length > 0) return true;
|
|
523
|
+
if (typeof b.attestation === "object" && b.attestation !== null) {
|
|
524
|
+
const att = b.attestation;
|
|
525
|
+
if (typeof att.attestationId === "string" && att.attestationId.length > 0) return true;
|
|
526
|
+
if (typeof att.nodeRuntimeHash === "string" && att.nodeRuntimeHash.length > 0) return true;
|
|
527
|
+
}
|
|
528
|
+
return false;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// src/attest.ts
|
|
532
|
+
var SHA256_PATTERN = /^sha256:[0-9a-f]{64}$/;
|
|
533
|
+
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
534
|
+
function validateHashFormat(value, fieldName) {
|
|
535
|
+
if (typeof value !== "string") return null;
|
|
536
|
+
if (!SHA256_PATTERN.test(value)) {
|
|
537
|
+
return `${fieldName} is not in sha256:<64hex> format: "${value}"`;
|
|
538
|
+
}
|
|
539
|
+
return null;
|
|
540
|
+
}
|
|
541
|
+
async function attest(bundle, options) {
|
|
542
|
+
const url = `${options.nodeUrl.replace(/\/+$/, "")}/api/attest`;
|
|
543
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
544
|
+
const sanitized = sanitizeForAttestation(bundle);
|
|
545
|
+
const controller = new AbortController();
|
|
546
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
547
|
+
let response;
|
|
548
|
+
try {
|
|
549
|
+
response = await fetch(url, {
|
|
550
|
+
method: "POST",
|
|
551
|
+
headers: {
|
|
552
|
+
"Content-Type": "application/json",
|
|
553
|
+
"Authorization": `Bearer ${options.apiKey}`
|
|
554
|
+
},
|
|
555
|
+
body: JSON.stringify(sanitized),
|
|
556
|
+
signal: controller.signal
|
|
557
|
+
});
|
|
558
|
+
} catch (err) {
|
|
559
|
+
clearTimeout(timer);
|
|
560
|
+
const error = err;
|
|
561
|
+
if (error.name === "AbortError") {
|
|
562
|
+
throw new CerAttestationError(
|
|
563
|
+
`Attestation request timed out after ${timeoutMs}ms`
|
|
564
|
+
);
|
|
565
|
+
}
|
|
566
|
+
throw new CerAttestationError(
|
|
567
|
+
`Network error contacting attestation node: ${error.message}`
|
|
568
|
+
);
|
|
569
|
+
} finally {
|
|
570
|
+
clearTimeout(timer);
|
|
571
|
+
}
|
|
572
|
+
let body;
|
|
573
|
+
try {
|
|
574
|
+
body = await response.json();
|
|
575
|
+
} catch {
|
|
576
|
+
const text = await response.text().catch(() => "");
|
|
577
|
+
throw new CerAttestationError(
|
|
578
|
+
`Attestation node returned non-JSON response (${response.status}): ${text}`,
|
|
579
|
+
response.status
|
|
580
|
+
);
|
|
581
|
+
}
|
|
582
|
+
if (!response.ok) {
|
|
583
|
+
const result2 = body;
|
|
584
|
+
const msg = typeof result2.error === "string" ? result2.error : `HTTP ${response.status}`;
|
|
585
|
+
const details = Array.isArray(result2.details) ? result2.details : void 0;
|
|
586
|
+
throw new CerAttestationError(
|
|
587
|
+
`Attestation failed: ${msg}`,
|
|
588
|
+
response.status,
|
|
589
|
+
body,
|
|
590
|
+
details
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
const result = body;
|
|
594
|
+
const errors = [];
|
|
595
|
+
if (typeof result.certificateHash === "string" && result.certificateHash !== bundle.certificateHash) {
|
|
596
|
+
errors.push(
|
|
597
|
+
`Node returned certificateHash "${result.certificateHash}" but bundle has "${bundle.certificateHash}"`
|
|
598
|
+
);
|
|
599
|
+
}
|
|
600
|
+
const certHashErr = validateHashFormat(result.certificateHash, "response.certificateHash");
|
|
601
|
+
if (certHashErr) errors.push(certHashErr);
|
|
602
|
+
const runtimeHashErr = validateHashFormat(result.nodeRuntimeHash, "response.nodeRuntimeHash");
|
|
603
|
+
if (runtimeHashErr) errors.push(runtimeHashErr);
|
|
604
|
+
if (errors.length > 0) {
|
|
605
|
+
throw new CerAttestationError(
|
|
606
|
+
`Attestation response validation failed: ${errors.join("; ")}`,
|
|
607
|
+
response.status,
|
|
608
|
+
body,
|
|
609
|
+
errors
|
|
610
|
+
);
|
|
611
|
+
}
|
|
612
|
+
return {
|
|
613
|
+
ok: true,
|
|
614
|
+
attestationId: typeof result.attestationId === "string" ? result.attestationId : void 0,
|
|
615
|
+
nodeRuntimeHash: typeof result.nodeRuntimeHash === "string" ? result.nodeRuntimeHash : void 0,
|
|
616
|
+
certificateHash: typeof result.certificateHash === "string" ? result.certificateHash : void 0,
|
|
617
|
+
protocolVersion: typeof result.protocolVersion === "string" ? result.protocolVersion : void 0,
|
|
618
|
+
raw: body
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// src/archive.ts
|
|
623
|
+
function exportCer(bundle) {
|
|
624
|
+
return toCanonicalJson(bundle);
|
|
625
|
+
}
|
|
626
|
+
function importCer(json) {
|
|
627
|
+
let parsed;
|
|
628
|
+
try {
|
|
629
|
+
parsed = JSON.parse(json);
|
|
630
|
+
} catch (err) {
|
|
631
|
+
throw new CerVerificationError([`Invalid JSON: ${err.message}`]);
|
|
632
|
+
}
|
|
633
|
+
const bundle = parsed;
|
|
634
|
+
if (!bundle || typeof bundle !== "object") {
|
|
635
|
+
throw new CerVerificationError(["Parsed value is not an object"]);
|
|
636
|
+
}
|
|
637
|
+
if (bundle.bundleType !== "cer.ai.execution.v1") {
|
|
638
|
+
throw new CerVerificationError([`Expected bundleType "cer.ai.execution.v1", got "${bundle.bundleType}"`]);
|
|
639
|
+
}
|
|
640
|
+
const result = verifyCer(bundle);
|
|
641
|
+
if (!result.ok) {
|
|
642
|
+
throw new CerVerificationError(result.errors);
|
|
643
|
+
}
|
|
644
|
+
return bundle;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
// src/providers/wrap.ts
|
|
648
|
+
var crypto4 = __toESM(require("crypto"), 1);
|
|
649
|
+
function wrapProvider(config) {
|
|
650
|
+
return {
|
|
651
|
+
async execute(params) {
|
|
652
|
+
const raw = await config.callFn(params.providerInput);
|
|
653
|
+
const output = config.extractOutput(raw);
|
|
654
|
+
const modelVersion = config.extractModelVersion ? config.extractModelVersion(raw) : params.modelVersion ?? null;
|
|
655
|
+
const snapshot = createSnapshot({
|
|
656
|
+
executionId: params.executionId ?? crypto4.randomUUID(),
|
|
657
|
+
provider: config.provider,
|
|
658
|
+
model: params.model,
|
|
659
|
+
modelVersion,
|
|
660
|
+
prompt: params.prompt,
|
|
661
|
+
input: params.input,
|
|
662
|
+
parameters: params.parameters,
|
|
663
|
+
output,
|
|
664
|
+
appId: params.appId
|
|
665
|
+
});
|
|
666
|
+
const bundle = sealCer(snapshot, { meta: params.meta });
|
|
667
|
+
return { output, snapshot, bundle };
|
|
668
|
+
}
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
672
|
+
0 && (module.exports = {
|
|
673
|
+
CerAttestationError,
|
|
674
|
+
CerVerificationError,
|
|
675
|
+
CerVerifyCode,
|
|
676
|
+
RunBuilder,
|
|
677
|
+
attest,
|
|
678
|
+
certifyDecision,
|
|
679
|
+
computeInputHash,
|
|
680
|
+
computeOutputHash,
|
|
681
|
+
createSnapshot,
|
|
682
|
+
exportCer,
|
|
683
|
+
hasAttestation,
|
|
684
|
+
hashCanonicalJson,
|
|
685
|
+
hashUtf8,
|
|
686
|
+
importCer,
|
|
687
|
+
sanitizeForAttestation,
|
|
688
|
+
sealCer,
|
|
689
|
+
sha256Hex,
|
|
690
|
+
toCanonicalJson,
|
|
691
|
+
verify,
|
|
692
|
+
verifyCer,
|
|
693
|
+
verifySnapshot,
|
|
694
|
+
wrapProvider
|
|
695
|
+
});
|
|
696
|
+
//# sourceMappingURL=index.cjs.map
|