@nexart/ai-execution 0.3.0 → 0.4.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/README.md +20 -6
- package/dist/index.cjs +607 -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 +550 -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-DF29BsH5.d.cts} +17 -16
- package/dist/types-DF29BsH5.d.ts +155 -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/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# @nexart/ai-execution v0.
|
|
1
|
+
# @nexart/ai-execution v0.4.0
|
|
2
2
|
|
|
3
3
|
Tamper-evident records and Certified Execution Records (CER) for AI operations.
|
|
4
4
|
|
|
@@ -21,7 +21,7 @@ These records can be verified offline to prove the execution happened as recorde
|
|
|
21
21
|
|
|
22
22
|
## Compatibility Guarantees
|
|
23
23
|
|
|
24
|
-
- **v0.1.0 and v0.
|
|
24
|
+
- **v0.1.0, v0.2.0, and v0.3.0 bundles verify forever.** Any CER bundle produced by any prior version will pass `verify()` in v0.4.0 and all future versions.
|
|
25
25
|
- **Hashing rules are frozen for `cer.ai.execution.v1`.** The canonicalization, SHA-256 computation, and certificate hash inputs (bundleType, version, createdAt, snapshot) are unchanged.
|
|
26
26
|
- **New optional snapshot fields** (runId, stepId, stepIndex, etc.) default to undefined and are excluded from legacy snapshots. They participate in the certificate hash only when present.
|
|
27
27
|
- **Canonicalization is frozen for v1.** Number-to-string conversion uses `JSON.stringify()`, which is consistent across JavaScript engines but does not implement RFC 8785 (JCS) for edge cases like `-0`. If stricter canonicalization is required, it will ship as a new bundle type (`cer.ai.execution.v2`), never as a modification to v1.
|
|
@@ -139,7 +139,7 @@ const restored = importCer(json); // parse + verify (throws on tamper)
|
|
|
139
139
|
| `modelVersion` | Optional | `string \| null` | Defaults to `null` |
|
|
140
140
|
| `parameters.topP` | Optional | `number \| null` | Defaults to `null` |
|
|
141
141
|
| `parameters.seed` | Optional | `number \| null` | Defaults to `null` |
|
|
142
|
-
| `sdkVersion` | Optional | `string \| null` | Defaults to `"0.
|
|
142
|
+
| `sdkVersion` | Optional | `string \| null` | Defaults to `"0.4.0"` |
|
|
143
143
|
| `appId` | Optional | `string \| null` | Defaults to `null` |
|
|
144
144
|
| `runId` | Optional | `string \| null` | Workflow run ID |
|
|
145
145
|
| `stepId` | Optional | `string \| null` | Step identifier within a run |
|
|
@@ -172,7 +172,7 @@ The `certificateHash` is SHA-256 of the UTF-8 bytes of the canonical JSON of exa
|
|
|
172
172
|
Endpoint: `POST {nodeUrl}/api/attest`
|
|
173
173
|
|
|
174
174
|
- Authorization: `Bearer {apiKey}`
|
|
175
|
-
- Body: the full CER bundle as JSON
|
|
175
|
+
- Body: the full CER bundle as JSON (auto-sanitized via `sanitizeForAttestation` in v0.4.0+)
|
|
176
176
|
- Returns: `AttestationResult` with `attestationId`, `nodeRuntimeHash`, `certificateHash`, `protocolVersion`
|
|
177
177
|
- Default timeout: 10 seconds (configurable via `timeoutMs`)
|
|
178
178
|
- Validates: response `certificateHash` matches submitted bundle; all hashes in `sha256:<64hex>` format
|
|
@@ -180,6 +180,17 @@ Endpoint: `POST {nodeUrl}/api/attest`
|
|
|
180
180
|
|
|
181
181
|
Attestation verifies internal integrity only. It does not re-run the model or validate the correctness of the AI output.
|
|
182
182
|
|
|
183
|
+
### Sanitization and Redaction
|
|
184
|
+
|
|
185
|
+
`sanitizeForAttestation(bundle)` returns a JSON-safe deep clone:
|
|
186
|
+
- Removes keys with `undefined` values at all nesting levels
|
|
187
|
+
- Rejects `BigInt`, functions, and symbols (throws)
|
|
188
|
+
- Safe to serialize with `JSON.stringify` or canonical JSON
|
|
189
|
+
|
|
190
|
+
**Recommended redaction pattern:** delete keys or set them to `null` — never set to `undefined`, which is not valid JSON. Call `sanitizeForAttestation` before archiving or attesting if your bundle may contain `undefined` values.
|
|
191
|
+
|
|
192
|
+
**Skip re-attestation:** use `hasAttestation(bundle)` to check if a bundle already includes attestation fields before calling `attest()` again.
|
|
193
|
+
|
|
183
194
|
## Canonical JSON Constraints
|
|
184
195
|
|
|
185
196
|
1. Object keys sorted lexicographically (Unicode codepoint order) at every nesting level.
|
|
@@ -226,7 +237,9 @@ Fixtures at `fixtures/vectors/` and `fixtures/golden/`. Cross-language implement
|
|
|
226
237
|
|
|
227
238
|
| Function | Description |
|
|
228
239
|
|---|---|
|
|
229
|
-
| `attest(bundle, options)` | Post CER to canonical node |
|
|
240
|
+
| `attest(bundle, options)` | Post CER to canonical node (auto-sanitizes) |
|
|
241
|
+
| `sanitizeForAttestation(bundle)` | Remove `undefined` keys, reject BigInt/functions/symbols |
|
|
242
|
+
| `hasAttestation(bundle)` | Check if bundle already has attestation fields |
|
|
230
243
|
| `exportCer(bundle)` | Serialize to canonical JSON string |
|
|
231
244
|
| `importCer(json)` | Parse + verify from JSON string |
|
|
232
245
|
|
|
@@ -244,7 +257,8 @@ Fixtures at `fixtures/vectors/` and `fixtures/golden/`. Cross-language implement
|
|
|
244
257
|
|---|---|
|
|
245
258
|
| v0.1.0 | Core snapshot + CER + verify + OpenAI adapter |
|
|
246
259
|
| v0.2.0 | certifyDecision, RunBuilder, attest, archive, Anthropic, wrapProvider, typed errors, workflow fields |
|
|
247
|
-
|
|
|
260
|
+
| v0.3.0 | Attestation hardening (hash validation, timeout), `verify` alias, `CerAttestationError.details`, release hygiene |
|
|
261
|
+
| **v0.4.0** | Dual ESM/CJS build, `sanitizeForAttestation`, `hasAttestation`, auto-sanitize in `attest()`, fixed `ERR_PACKAGE_PATH_NOT_EXPORTED` |
|
|
248
262
|
| v1.0.0 | Planned: API stabilization, freeze public API surface |
|
|
249
263
|
|
|
250
264
|
## Releasing
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,607 @@
|
|
|
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
|
+
RunBuilder: () => RunBuilder,
|
|
36
|
+
attest: () => attest,
|
|
37
|
+
certifyDecision: () => certifyDecision,
|
|
38
|
+
computeInputHash: () => computeInputHash,
|
|
39
|
+
computeOutputHash: () => computeOutputHash,
|
|
40
|
+
createSnapshot: () => createSnapshot,
|
|
41
|
+
exportCer: () => exportCer,
|
|
42
|
+
hasAttestation: () => hasAttestation,
|
|
43
|
+
hashCanonicalJson: () => hashCanonicalJson,
|
|
44
|
+
hashUtf8: () => hashUtf8,
|
|
45
|
+
importCer: () => importCer,
|
|
46
|
+
sanitizeForAttestation: () => sanitizeForAttestation,
|
|
47
|
+
sealCer: () => sealCer,
|
|
48
|
+
sha256Hex: () => sha256Hex,
|
|
49
|
+
toCanonicalJson: () => toCanonicalJson,
|
|
50
|
+
verify: () => verifyCer,
|
|
51
|
+
verifyCer: () => verifyCer,
|
|
52
|
+
verifySnapshot: () => verifySnapshot,
|
|
53
|
+
wrapProvider: () => wrapProvider
|
|
54
|
+
});
|
|
55
|
+
module.exports = __toCommonJS(src_exports);
|
|
56
|
+
|
|
57
|
+
// src/errors.ts
|
|
58
|
+
var CerVerificationError = class extends Error {
|
|
59
|
+
errors;
|
|
60
|
+
constructor(errors) {
|
|
61
|
+
super(`CER verification failed: ${errors.join("; ")}`);
|
|
62
|
+
this.name = "CerVerificationError";
|
|
63
|
+
this.errors = errors;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
var CerAttestationError = class extends Error {
|
|
67
|
+
statusCode;
|
|
68
|
+
responseBody;
|
|
69
|
+
details;
|
|
70
|
+
constructor(message, statusCode, responseBody, details) {
|
|
71
|
+
super(message);
|
|
72
|
+
this.name = "CerAttestationError";
|
|
73
|
+
this.statusCode = statusCode;
|
|
74
|
+
this.responseBody = responseBody;
|
|
75
|
+
this.details = details;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// src/canonicalJson.ts
|
|
80
|
+
function toCanonicalJson(value) {
|
|
81
|
+
return canonicalize(value);
|
|
82
|
+
}
|
|
83
|
+
function canonicalize(value) {
|
|
84
|
+
if (value === null) {
|
|
85
|
+
return "null";
|
|
86
|
+
}
|
|
87
|
+
if (typeof value === "boolean") {
|
|
88
|
+
return value ? "true" : "false";
|
|
89
|
+
}
|
|
90
|
+
if (typeof value === "number") {
|
|
91
|
+
if (!Number.isFinite(value)) {
|
|
92
|
+
throw new Error(`Non-finite number not allowed in canonical JSON: ${value}`);
|
|
93
|
+
}
|
|
94
|
+
return JSON.stringify(value);
|
|
95
|
+
}
|
|
96
|
+
if (typeof value === "string") {
|
|
97
|
+
return JSON.stringify(value);
|
|
98
|
+
}
|
|
99
|
+
if (Array.isArray(value)) {
|
|
100
|
+
const items = value.map((item) => canonicalize(item));
|
|
101
|
+
return "[" + items.join(",") + "]";
|
|
102
|
+
}
|
|
103
|
+
if (typeof value === "object") {
|
|
104
|
+
const obj = value;
|
|
105
|
+
const keys = Object.keys(obj).sort();
|
|
106
|
+
const entries = keys.map((key) => {
|
|
107
|
+
const val = obj[key];
|
|
108
|
+
if (val === void 0) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
return JSON.stringify(key) + ":" + canonicalize(val);
|
|
112
|
+
}).filter((e) => e !== null);
|
|
113
|
+
return "{" + entries.join(",") + "}";
|
|
114
|
+
}
|
|
115
|
+
throw new Error(`Unsupported type for canonical JSON: ${typeof value}`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// src/hash.ts
|
|
119
|
+
var crypto = __toESM(require("crypto"), 1);
|
|
120
|
+
function sha256Hex(data) {
|
|
121
|
+
const hash = crypto.createHash("sha256");
|
|
122
|
+
if (typeof data === "string") {
|
|
123
|
+
hash.update(data, "utf-8");
|
|
124
|
+
} else {
|
|
125
|
+
hash.update(data);
|
|
126
|
+
}
|
|
127
|
+
return hash.digest("hex");
|
|
128
|
+
}
|
|
129
|
+
function hashUtf8(value) {
|
|
130
|
+
return `sha256:${sha256Hex(value)}`;
|
|
131
|
+
}
|
|
132
|
+
function hashCanonicalJson(value) {
|
|
133
|
+
const canonical = toCanonicalJson(value);
|
|
134
|
+
return `sha256:${sha256Hex(canonical)}`;
|
|
135
|
+
}
|
|
136
|
+
function computeInputHash(input) {
|
|
137
|
+
if (typeof input === "string") {
|
|
138
|
+
return hashUtf8(input);
|
|
139
|
+
}
|
|
140
|
+
return hashCanonicalJson(input);
|
|
141
|
+
}
|
|
142
|
+
function computeOutputHash(output) {
|
|
143
|
+
if (typeof output === "string") {
|
|
144
|
+
return hashUtf8(output);
|
|
145
|
+
}
|
|
146
|
+
return hashCanonicalJson(output);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// src/snapshot.ts
|
|
150
|
+
var PACKAGE_VERSION = "0.4.0";
|
|
151
|
+
function validateParameters(params) {
|
|
152
|
+
const errors = [];
|
|
153
|
+
if (typeof params.temperature !== "number" || !Number.isFinite(params.temperature)) {
|
|
154
|
+
errors.push(`parameters.temperature must be a finite number, got: ${params.temperature}`);
|
|
155
|
+
}
|
|
156
|
+
if (typeof params.maxTokens !== "number" || !Number.isFinite(params.maxTokens)) {
|
|
157
|
+
errors.push(`parameters.maxTokens must be a finite number, got: ${params.maxTokens}`);
|
|
158
|
+
}
|
|
159
|
+
if (params.topP !== null && (typeof params.topP !== "number" || !Number.isFinite(params.topP))) {
|
|
160
|
+
errors.push(`parameters.topP must be a finite number or null, got: ${params.topP}`);
|
|
161
|
+
}
|
|
162
|
+
if (params.seed !== null && (typeof params.seed !== "number" || !Number.isFinite(params.seed))) {
|
|
163
|
+
errors.push(`parameters.seed must be a finite number or null, got: ${params.seed}`);
|
|
164
|
+
}
|
|
165
|
+
return errors;
|
|
166
|
+
}
|
|
167
|
+
function createSnapshot(params) {
|
|
168
|
+
const paramErrors = validateParameters(params.parameters);
|
|
169
|
+
if (paramErrors.length > 0) {
|
|
170
|
+
throw new Error(`Invalid parameters: ${paramErrors.join("; ")}`);
|
|
171
|
+
}
|
|
172
|
+
const inputHash = computeInputHash(params.input);
|
|
173
|
+
const outputHash = computeOutputHash(params.output);
|
|
174
|
+
const snapshot = {
|
|
175
|
+
type: "ai.execution.v1",
|
|
176
|
+
protocolVersion: "1.2.0",
|
|
177
|
+
executionSurface: "ai",
|
|
178
|
+
executionId: params.executionId,
|
|
179
|
+
timestamp: params.timestamp ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
180
|
+
provider: params.provider,
|
|
181
|
+
model: params.model,
|
|
182
|
+
modelVersion: params.modelVersion ?? null,
|
|
183
|
+
prompt: params.prompt,
|
|
184
|
+
input: params.input,
|
|
185
|
+
inputHash,
|
|
186
|
+
parameters: {
|
|
187
|
+
temperature: params.parameters.temperature,
|
|
188
|
+
maxTokens: params.parameters.maxTokens,
|
|
189
|
+
topP: params.parameters.topP ?? null,
|
|
190
|
+
seed: params.parameters.seed ?? null
|
|
191
|
+
},
|
|
192
|
+
output: params.output,
|
|
193
|
+
outputHash,
|
|
194
|
+
sdkVersion: params.sdkVersion ?? PACKAGE_VERSION,
|
|
195
|
+
appId: params.appId ?? null
|
|
196
|
+
};
|
|
197
|
+
if (params.runId !== void 0) snapshot.runId = params.runId ?? null;
|
|
198
|
+
if (params.stepId !== void 0) snapshot.stepId = params.stepId ?? null;
|
|
199
|
+
if (params.stepIndex !== void 0) snapshot.stepIndex = params.stepIndex ?? null;
|
|
200
|
+
if (params.workflowId !== void 0) snapshot.workflowId = params.workflowId ?? null;
|
|
201
|
+
if (params.conversationId !== void 0) snapshot.conversationId = params.conversationId ?? null;
|
|
202
|
+
if (params.prevStepHash !== void 0) snapshot.prevStepHash = params.prevStepHash ?? null;
|
|
203
|
+
return snapshot;
|
|
204
|
+
}
|
|
205
|
+
function verifySnapshot(snapshot) {
|
|
206
|
+
const errors = [];
|
|
207
|
+
if (snapshot.type !== "ai.execution.v1") {
|
|
208
|
+
errors.push(`Expected type "ai.execution.v1", got "${snapshot.type}"`);
|
|
209
|
+
}
|
|
210
|
+
if (snapshot.protocolVersion !== "1.2.0") {
|
|
211
|
+
errors.push(`Expected protocolVersion "1.2.0", got "${snapshot.protocolVersion}"`);
|
|
212
|
+
}
|
|
213
|
+
if (snapshot.executionSurface !== "ai") {
|
|
214
|
+
errors.push(`Expected executionSurface "ai", got "${snapshot.executionSurface}"`);
|
|
215
|
+
}
|
|
216
|
+
if (!snapshot.executionId || typeof snapshot.executionId !== "string") {
|
|
217
|
+
errors.push("executionId must be a non-empty string");
|
|
218
|
+
}
|
|
219
|
+
if (!snapshot.timestamp || typeof snapshot.timestamp !== "string") {
|
|
220
|
+
errors.push("timestamp must be a non-empty string");
|
|
221
|
+
}
|
|
222
|
+
if (!snapshot.provider || typeof snapshot.provider !== "string") {
|
|
223
|
+
errors.push("provider must be a non-empty string");
|
|
224
|
+
}
|
|
225
|
+
if (!snapshot.model || typeof snapshot.model !== "string") {
|
|
226
|
+
errors.push("model must be a non-empty string");
|
|
227
|
+
}
|
|
228
|
+
if (!snapshot.prompt || typeof snapshot.prompt !== "string") {
|
|
229
|
+
errors.push("prompt must be a non-empty string");
|
|
230
|
+
}
|
|
231
|
+
if (snapshot.input === void 0 || snapshot.input === null) {
|
|
232
|
+
errors.push("input must be a string or object");
|
|
233
|
+
}
|
|
234
|
+
if (snapshot.output === void 0 || snapshot.output === null) {
|
|
235
|
+
errors.push("output must be a string or object");
|
|
236
|
+
}
|
|
237
|
+
const paramErrors = validateParameters(snapshot.parameters);
|
|
238
|
+
errors.push(...paramErrors);
|
|
239
|
+
if (!snapshot.inputHash || !snapshot.inputHash.startsWith("sha256:")) {
|
|
240
|
+
errors.push(`inputHash must start with "sha256:", got "${snapshot.inputHash}"`);
|
|
241
|
+
}
|
|
242
|
+
if (!snapshot.outputHash || !snapshot.outputHash.startsWith("sha256:")) {
|
|
243
|
+
errors.push(`outputHash must start with "sha256:", got "${snapshot.outputHash}"`);
|
|
244
|
+
}
|
|
245
|
+
const expectedInputHash = computeInputHash(snapshot.input);
|
|
246
|
+
if (snapshot.inputHash !== expectedInputHash) {
|
|
247
|
+
errors.push(`inputHash mismatch: expected ${expectedInputHash}, got ${snapshot.inputHash}`);
|
|
248
|
+
}
|
|
249
|
+
const expectedOutputHash = computeOutputHash(snapshot.output);
|
|
250
|
+
if (snapshot.outputHash !== expectedOutputHash) {
|
|
251
|
+
errors.push(`outputHash mismatch: expected ${expectedOutputHash}, got ${snapshot.outputHash}`);
|
|
252
|
+
}
|
|
253
|
+
return { ok: errors.length === 0, errors };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// src/cer.ts
|
|
257
|
+
function computeCertificateHash(payload) {
|
|
258
|
+
const canonical = toCanonicalJson(payload);
|
|
259
|
+
return `sha256:${sha256Hex(canonical)}`;
|
|
260
|
+
}
|
|
261
|
+
function sealCer(snapshot, options) {
|
|
262
|
+
const createdAt = options?.createdAt ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
263
|
+
const payload = {
|
|
264
|
+
bundleType: "cer.ai.execution.v1",
|
|
265
|
+
createdAt,
|
|
266
|
+
snapshot,
|
|
267
|
+
version: "0.1"
|
|
268
|
+
};
|
|
269
|
+
const certificateHash = computeCertificateHash(payload);
|
|
270
|
+
const bundle = {
|
|
271
|
+
bundleType: "cer.ai.execution.v1",
|
|
272
|
+
certificateHash,
|
|
273
|
+
createdAt,
|
|
274
|
+
version: "0.1",
|
|
275
|
+
snapshot
|
|
276
|
+
};
|
|
277
|
+
if (options?.meta) {
|
|
278
|
+
bundle.meta = options.meta;
|
|
279
|
+
}
|
|
280
|
+
return bundle;
|
|
281
|
+
}
|
|
282
|
+
function verifyCer(bundle) {
|
|
283
|
+
const errors = [];
|
|
284
|
+
if (bundle.bundleType !== "cer.ai.execution.v1") {
|
|
285
|
+
errors.push(`Expected bundleType "cer.ai.execution.v1", got "${bundle.bundleType}"`);
|
|
286
|
+
}
|
|
287
|
+
if (bundle.version !== "0.1") {
|
|
288
|
+
errors.push(`Expected version "0.1", got "${bundle.version}"`);
|
|
289
|
+
}
|
|
290
|
+
if (!bundle.createdAt || typeof bundle.createdAt !== "string") {
|
|
291
|
+
errors.push("createdAt must be a non-empty string");
|
|
292
|
+
}
|
|
293
|
+
if (!bundle.certificateHash || !bundle.certificateHash.startsWith("sha256:")) {
|
|
294
|
+
errors.push(`certificateHash must start with "sha256:", got "${bundle.certificateHash}"`);
|
|
295
|
+
}
|
|
296
|
+
if (!bundle.snapshot) {
|
|
297
|
+
errors.push("snapshot is required");
|
|
298
|
+
return { ok: false, errors };
|
|
299
|
+
}
|
|
300
|
+
const snapshotResult = verifySnapshot(bundle.snapshot);
|
|
301
|
+
errors.push(...snapshotResult.errors);
|
|
302
|
+
const payload = {
|
|
303
|
+
bundleType: "cer.ai.execution.v1",
|
|
304
|
+
createdAt: bundle.createdAt,
|
|
305
|
+
snapshot: bundle.snapshot,
|
|
306
|
+
version: "0.1"
|
|
307
|
+
};
|
|
308
|
+
const expectedHash = computeCertificateHash(payload);
|
|
309
|
+
if (bundle.certificateHash !== expectedHash) {
|
|
310
|
+
errors.push(`certificateHash mismatch: expected ${expectedHash}, got ${bundle.certificateHash}`);
|
|
311
|
+
}
|
|
312
|
+
return { ok: errors.length === 0, errors };
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// src/certify.ts
|
|
316
|
+
var crypto2 = __toESM(require("crypto"), 1);
|
|
317
|
+
function certifyDecision(params) {
|
|
318
|
+
const executionId = params.executionId ?? crypto2.randomUUID();
|
|
319
|
+
const snapshot = createSnapshot({
|
|
320
|
+
executionId,
|
|
321
|
+
timestamp: params.timestamp,
|
|
322
|
+
provider: params.provider,
|
|
323
|
+
model: params.model,
|
|
324
|
+
modelVersion: params.modelVersion,
|
|
325
|
+
prompt: params.prompt,
|
|
326
|
+
input: params.input,
|
|
327
|
+
parameters: params.parameters,
|
|
328
|
+
output: params.output,
|
|
329
|
+
sdkVersion: params.sdkVersion,
|
|
330
|
+
appId: params.appId,
|
|
331
|
+
runId: params.runId,
|
|
332
|
+
stepId: params.stepId,
|
|
333
|
+
stepIndex: params.stepIndex,
|
|
334
|
+
workflowId: params.workflowId,
|
|
335
|
+
conversationId: params.conversationId,
|
|
336
|
+
prevStepHash: params.prevStepHash
|
|
337
|
+
});
|
|
338
|
+
return sealCer(snapshot, { meta: params.meta });
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// src/run.ts
|
|
342
|
+
var crypto3 = __toESM(require("crypto"), 1);
|
|
343
|
+
var RunBuilder = class {
|
|
344
|
+
runId;
|
|
345
|
+
workflowId;
|
|
346
|
+
conversationId;
|
|
347
|
+
appId;
|
|
348
|
+
stepIndex = 0;
|
|
349
|
+
prevStepHash = null;
|
|
350
|
+
steps = [];
|
|
351
|
+
constructor(options) {
|
|
352
|
+
this.runId = options?.runId ?? crypto3.randomUUID();
|
|
353
|
+
this.workflowId = options?.workflowId ?? null;
|
|
354
|
+
this.conversationId = options?.conversationId ?? null;
|
|
355
|
+
this.appId = options?.appId ?? null;
|
|
356
|
+
}
|
|
357
|
+
step(params) {
|
|
358
|
+
const stepId = params.stepId ?? crypto3.randomUUID();
|
|
359
|
+
const executionId = `${this.runId}-step-${this.stepIndex}`;
|
|
360
|
+
const snapshot = createSnapshot({
|
|
361
|
+
executionId,
|
|
362
|
+
timestamp: params.timestamp,
|
|
363
|
+
provider: params.provider,
|
|
364
|
+
model: params.model,
|
|
365
|
+
modelVersion: params.modelVersion,
|
|
366
|
+
prompt: params.prompt,
|
|
367
|
+
input: params.input,
|
|
368
|
+
parameters: params.parameters,
|
|
369
|
+
output: params.output,
|
|
370
|
+
appId: this.appId,
|
|
371
|
+
runId: this.runId,
|
|
372
|
+
stepId,
|
|
373
|
+
stepIndex: this.stepIndex,
|
|
374
|
+
workflowId: this.workflowId,
|
|
375
|
+
conversationId: this.conversationId,
|
|
376
|
+
prevStepHash: this.prevStepHash
|
|
377
|
+
});
|
|
378
|
+
const bundle = sealCer(snapshot, { meta: params.meta });
|
|
379
|
+
this.steps.push({
|
|
380
|
+
stepIndex: this.stepIndex,
|
|
381
|
+
stepId,
|
|
382
|
+
executionId,
|
|
383
|
+
certificateHash: bundle.certificateHash,
|
|
384
|
+
prevStepHash: this.prevStepHash
|
|
385
|
+
});
|
|
386
|
+
this.prevStepHash = bundle.certificateHash;
|
|
387
|
+
this.stepIndex++;
|
|
388
|
+
return bundle;
|
|
389
|
+
}
|
|
390
|
+
finalize() {
|
|
391
|
+
return {
|
|
392
|
+
runId: this.runId,
|
|
393
|
+
workflowId: this.workflowId,
|
|
394
|
+
conversationId: this.conversationId,
|
|
395
|
+
stepCount: this.steps.length,
|
|
396
|
+
steps: [...this.steps],
|
|
397
|
+
finalStepHash: this.prevStepHash
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
// src/sanitize.ts
|
|
403
|
+
function deepRemoveUndefined(value) {
|
|
404
|
+
if (value === null || value === void 0) return value;
|
|
405
|
+
if (typeof value === "bigint") {
|
|
406
|
+
throw new Error("BigInt values are not JSON-safe and cannot be sanitized");
|
|
407
|
+
}
|
|
408
|
+
if (typeof value === "function") {
|
|
409
|
+
throw new Error("Function values are not JSON-safe and cannot be sanitized");
|
|
410
|
+
}
|
|
411
|
+
if (typeof value === "symbol") {
|
|
412
|
+
throw new Error("Symbol values are not JSON-safe and cannot be sanitized");
|
|
413
|
+
}
|
|
414
|
+
if (Array.isArray(value)) {
|
|
415
|
+
return value.map(deepRemoveUndefined);
|
|
416
|
+
}
|
|
417
|
+
if (typeof value === "object") {
|
|
418
|
+
const result = {};
|
|
419
|
+
for (const [key, val] of Object.entries(value)) {
|
|
420
|
+
if (val === void 0) continue;
|
|
421
|
+
result[key] = deepRemoveUndefined(val);
|
|
422
|
+
}
|
|
423
|
+
return result;
|
|
424
|
+
}
|
|
425
|
+
return value;
|
|
426
|
+
}
|
|
427
|
+
function sanitizeForAttestation(bundle) {
|
|
428
|
+
return deepRemoveUndefined(bundle);
|
|
429
|
+
}
|
|
430
|
+
function hasAttestation(bundle) {
|
|
431
|
+
if (typeof bundle !== "object" || bundle === null) return false;
|
|
432
|
+
const b = bundle;
|
|
433
|
+
if (typeof b.attestationId === "string" && b.attestationId.length > 0) return true;
|
|
434
|
+
if (typeof b.nodeRuntimeHash === "string" && b.nodeRuntimeHash.length > 0) return true;
|
|
435
|
+
if (typeof b.attestation === "object" && b.attestation !== null) {
|
|
436
|
+
const att = b.attestation;
|
|
437
|
+
if (typeof att.attestationId === "string" && att.attestationId.length > 0) return true;
|
|
438
|
+
if (typeof att.nodeRuntimeHash === "string" && att.nodeRuntimeHash.length > 0) return true;
|
|
439
|
+
}
|
|
440
|
+
return false;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// src/attest.ts
|
|
444
|
+
var SHA256_PATTERN = /^sha256:[0-9a-f]{64}$/;
|
|
445
|
+
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
446
|
+
function validateHashFormat(value, fieldName) {
|
|
447
|
+
if (typeof value !== "string") return null;
|
|
448
|
+
if (!SHA256_PATTERN.test(value)) {
|
|
449
|
+
return `${fieldName} is not in sha256:<64hex> format: "${value}"`;
|
|
450
|
+
}
|
|
451
|
+
return null;
|
|
452
|
+
}
|
|
453
|
+
async function attest(bundle, options) {
|
|
454
|
+
const url = `${options.nodeUrl.replace(/\/+$/, "")}/api/attest`;
|
|
455
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
456
|
+
const sanitized = sanitizeForAttestation(bundle);
|
|
457
|
+
const controller = new AbortController();
|
|
458
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
459
|
+
let response;
|
|
460
|
+
try {
|
|
461
|
+
response = await fetch(url, {
|
|
462
|
+
method: "POST",
|
|
463
|
+
headers: {
|
|
464
|
+
"Content-Type": "application/json",
|
|
465
|
+
"Authorization": `Bearer ${options.apiKey}`
|
|
466
|
+
},
|
|
467
|
+
body: JSON.stringify(sanitized),
|
|
468
|
+
signal: controller.signal
|
|
469
|
+
});
|
|
470
|
+
} catch (err) {
|
|
471
|
+
clearTimeout(timer);
|
|
472
|
+
const error = err;
|
|
473
|
+
if (error.name === "AbortError") {
|
|
474
|
+
throw new CerAttestationError(
|
|
475
|
+
`Attestation request timed out after ${timeoutMs}ms`
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
throw new CerAttestationError(
|
|
479
|
+
`Network error contacting attestation node: ${error.message}`
|
|
480
|
+
);
|
|
481
|
+
} finally {
|
|
482
|
+
clearTimeout(timer);
|
|
483
|
+
}
|
|
484
|
+
let body;
|
|
485
|
+
try {
|
|
486
|
+
body = await response.json();
|
|
487
|
+
} catch {
|
|
488
|
+
const text = await response.text().catch(() => "");
|
|
489
|
+
throw new CerAttestationError(
|
|
490
|
+
`Attestation node returned non-JSON response (${response.status}): ${text}`,
|
|
491
|
+
response.status
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
if (!response.ok) {
|
|
495
|
+
const result2 = body;
|
|
496
|
+
const msg = typeof result2.error === "string" ? result2.error : `HTTP ${response.status}`;
|
|
497
|
+
const details = Array.isArray(result2.details) ? result2.details : void 0;
|
|
498
|
+
throw new CerAttestationError(
|
|
499
|
+
`Attestation failed: ${msg}`,
|
|
500
|
+
response.status,
|
|
501
|
+
body,
|
|
502
|
+
details
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
const result = body;
|
|
506
|
+
const errors = [];
|
|
507
|
+
if (typeof result.certificateHash === "string" && result.certificateHash !== bundle.certificateHash) {
|
|
508
|
+
errors.push(
|
|
509
|
+
`Node returned certificateHash "${result.certificateHash}" but bundle has "${bundle.certificateHash}"`
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
const certHashErr = validateHashFormat(result.certificateHash, "response.certificateHash");
|
|
513
|
+
if (certHashErr) errors.push(certHashErr);
|
|
514
|
+
const runtimeHashErr = validateHashFormat(result.nodeRuntimeHash, "response.nodeRuntimeHash");
|
|
515
|
+
if (runtimeHashErr) errors.push(runtimeHashErr);
|
|
516
|
+
if (errors.length > 0) {
|
|
517
|
+
throw new CerAttestationError(
|
|
518
|
+
`Attestation response validation failed: ${errors.join("; ")}`,
|
|
519
|
+
response.status,
|
|
520
|
+
body,
|
|
521
|
+
errors
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
return {
|
|
525
|
+
ok: true,
|
|
526
|
+
attestationId: typeof result.attestationId === "string" ? result.attestationId : void 0,
|
|
527
|
+
nodeRuntimeHash: typeof result.nodeRuntimeHash === "string" ? result.nodeRuntimeHash : void 0,
|
|
528
|
+
certificateHash: typeof result.certificateHash === "string" ? result.certificateHash : void 0,
|
|
529
|
+
protocolVersion: typeof result.protocolVersion === "string" ? result.protocolVersion : void 0,
|
|
530
|
+
raw: body
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// src/archive.ts
|
|
535
|
+
function exportCer(bundle) {
|
|
536
|
+
return toCanonicalJson(bundle);
|
|
537
|
+
}
|
|
538
|
+
function importCer(json) {
|
|
539
|
+
let parsed;
|
|
540
|
+
try {
|
|
541
|
+
parsed = JSON.parse(json);
|
|
542
|
+
} catch (err) {
|
|
543
|
+
throw new CerVerificationError([`Invalid JSON: ${err.message}`]);
|
|
544
|
+
}
|
|
545
|
+
const bundle = parsed;
|
|
546
|
+
if (!bundle || typeof bundle !== "object") {
|
|
547
|
+
throw new CerVerificationError(["Parsed value is not an object"]);
|
|
548
|
+
}
|
|
549
|
+
if (bundle.bundleType !== "cer.ai.execution.v1") {
|
|
550
|
+
throw new CerVerificationError([`Expected bundleType "cer.ai.execution.v1", got "${bundle.bundleType}"`]);
|
|
551
|
+
}
|
|
552
|
+
const result = verifyCer(bundle);
|
|
553
|
+
if (!result.ok) {
|
|
554
|
+
throw new CerVerificationError(result.errors);
|
|
555
|
+
}
|
|
556
|
+
return bundle;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// src/providers/wrap.ts
|
|
560
|
+
var crypto4 = __toESM(require("crypto"), 1);
|
|
561
|
+
function wrapProvider(config) {
|
|
562
|
+
return {
|
|
563
|
+
async execute(params) {
|
|
564
|
+
const raw = await config.callFn(params.providerInput);
|
|
565
|
+
const output = config.extractOutput(raw);
|
|
566
|
+
const modelVersion = config.extractModelVersion ? config.extractModelVersion(raw) : params.modelVersion ?? null;
|
|
567
|
+
const snapshot = createSnapshot({
|
|
568
|
+
executionId: params.executionId ?? crypto4.randomUUID(),
|
|
569
|
+
provider: config.provider,
|
|
570
|
+
model: params.model,
|
|
571
|
+
modelVersion,
|
|
572
|
+
prompt: params.prompt,
|
|
573
|
+
input: params.input,
|
|
574
|
+
parameters: params.parameters,
|
|
575
|
+
output,
|
|
576
|
+
appId: params.appId
|
|
577
|
+
});
|
|
578
|
+
const bundle = sealCer(snapshot, { meta: params.meta });
|
|
579
|
+
return { output, snapshot, bundle };
|
|
580
|
+
}
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
584
|
+
0 && (module.exports = {
|
|
585
|
+
CerAttestationError,
|
|
586
|
+
CerVerificationError,
|
|
587
|
+
RunBuilder,
|
|
588
|
+
attest,
|
|
589
|
+
certifyDecision,
|
|
590
|
+
computeInputHash,
|
|
591
|
+
computeOutputHash,
|
|
592
|
+
createSnapshot,
|
|
593
|
+
exportCer,
|
|
594
|
+
hasAttestation,
|
|
595
|
+
hashCanonicalJson,
|
|
596
|
+
hashUtf8,
|
|
597
|
+
importCer,
|
|
598
|
+
sanitizeForAttestation,
|
|
599
|
+
sealCer,
|
|
600
|
+
sha256Hex,
|
|
601
|
+
toCanonicalJson,
|
|
602
|
+
verify,
|
|
603
|
+
verifyCer,
|
|
604
|
+
verifySnapshot,
|
|
605
|
+
wrapProvider
|
|
606
|
+
});
|
|
607
|
+
//# sourceMappingURL=index.cjs.map
|