@nexart/ai-execution 0.2.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 +100 -330
- 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} +18 -16
- package/dist/types-DF29BsH5.d.ts +155 -0
- package/package.json +18 -9
- package/dist/__tests__/fixtures.test.d.ts +0 -2
- package/dist/__tests__/fixtures.test.d.ts.map +0 -1
- package/dist/__tests__/fixtures.test.js +0 -37
- package/dist/__tests__/fixtures.test.js.map +0 -1
- package/dist/__tests__/v020.test.d.ts +0 -2
- package/dist/__tests__/v020.test.d.ts.map +0 -1
- package/dist/__tests__/v020.test.js +0 -408
- package/dist/__tests__/v020.test.js.map +0 -1
- package/dist/__tests__/vectors.test.d.ts +0 -2
- package/dist/__tests__/vectors.test.d.ts.map +0 -1
- package/dist/__tests__/vectors.test.js +0 -261
- package/dist/__tests__/vectors.test.js.map +0 -1
- 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 -42
- 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 -10
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js +0 -19
- 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.d.cts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { C as CreateSnapshotParams, A as AiExecutionSnapshotV1, V as VerificationResult, a as CerMeta, b as CerAiExecutionBundle, c as CertifyDecisionParams, R as RunBuilderOptions, S as StepParams, d as RunSummary, e as AttestOptions, f as AttestationResult } from './types-DF29BsH5.cjs';
|
|
2
|
+
export { g as AiExecutionParameters, P as ProviderConfig, W as WrappedExecutionParams, h as WrappedExecutionResult } from './types-DF29BsH5.cjs';
|
|
3
|
+
export { wrapProvider } from './providers/wrap.cjs';
|
|
4
|
+
|
|
5
|
+
declare class CerVerificationError extends Error {
|
|
6
|
+
readonly errors: string[];
|
|
7
|
+
constructor(errors: string[]);
|
|
8
|
+
}
|
|
9
|
+
declare class CerAttestationError extends Error {
|
|
10
|
+
readonly statusCode?: number;
|
|
11
|
+
readonly responseBody?: unknown;
|
|
12
|
+
readonly details?: string[];
|
|
13
|
+
constructor(message: string, statusCode?: number, responseBody?: unknown, details?: string[]);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare function toCanonicalJson(value: unknown): string;
|
|
17
|
+
|
|
18
|
+
declare function sha256Hex(data: string | Uint8Array): string;
|
|
19
|
+
declare function hashUtf8(value: string): string;
|
|
20
|
+
declare function hashCanonicalJson(value: unknown): string;
|
|
21
|
+
declare function computeInputHash(input: string | Record<string, unknown>): string;
|
|
22
|
+
declare function computeOutputHash(output: string | Record<string, unknown>): string;
|
|
23
|
+
|
|
24
|
+
declare function createSnapshot(params: CreateSnapshotParams): AiExecutionSnapshotV1;
|
|
25
|
+
declare function verifySnapshot(snapshot: AiExecutionSnapshotV1): VerificationResult;
|
|
26
|
+
|
|
27
|
+
declare function sealCer(snapshot: AiExecutionSnapshotV1, options?: {
|
|
28
|
+
createdAt?: string;
|
|
29
|
+
meta?: CerMeta;
|
|
30
|
+
}): CerAiExecutionBundle;
|
|
31
|
+
declare function verifyCer(bundle: CerAiExecutionBundle): VerificationResult;
|
|
32
|
+
|
|
33
|
+
declare function certifyDecision(params: CertifyDecisionParams): CerAiExecutionBundle;
|
|
34
|
+
|
|
35
|
+
declare class RunBuilder {
|
|
36
|
+
private readonly runId;
|
|
37
|
+
private readonly workflowId;
|
|
38
|
+
private readonly conversationId;
|
|
39
|
+
private readonly appId;
|
|
40
|
+
private stepIndex;
|
|
41
|
+
private prevStepHash;
|
|
42
|
+
private steps;
|
|
43
|
+
constructor(options?: RunBuilderOptions);
|
|
44
|
+
step(params: StepParams): CerAiExecutionBundle;
|
|
45
|
+
finalize(): RunSummary;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
declare function attest(bundle: CerAiExecutionBundle, options: AttestOptions): Promise<AttestationResult>;
|
|
49
|
+
|
|
50
|
+
declare function exportCer(bundle: CerAiExecutionBundle): string;
|
|
51
|
+
declare function importCer(json: string): CerAiExecutionBundle;
|
|
52
|
+
|
|
53
|
+
declare function sanitizeForAttestation(bundle: CerAiExecutionBundle): CerAiExecutionBundle;
|
|
54
|
+
declare function hasAttestation(bundle: unknown): boolean;
|
|
55
|
+
|
|
56
|
+
export { AiExecutionSnapshotV1, AttestOptions, AttestationResult, CerAiExecutionBundle, CerAttestationError, CerMeta, CerVerificationError, CertifyDecisionParams, CreateSnapshotParams, RunBuilder, RunBuilderOptions, RunSummary, StepParams, VerificationResult, attest, certifyDecision, computeInputHash, computeOutputHash, createSnapshot, exportCer, hasAttestation, hashCanonicalJson, hashUtf8, importCer, sanitizeForAttestation, sealCer, sha256Hex, toCanonicalJson, verifyCer as verify, verifyCer, verifySnapshot };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,56 @@
|
|
|
1
|
-
|
|
2
|
-
export {
|
|
3
|
-
export { toCanonicalJson } from './canonicalJson.js';
|
|
4
|
-
export { sha256Hex, hashUtf8, hashCanonicalJson, computeInputHash, computeOutputHash } from './hash.js';
|
|
5
|
-
export { createSnapshot, verifySnapshot } from './snapshot.js';
|
|
6
|
-
export { sealCer, verifyCer } from './cer.js';
|
|
7
|
-
export { certifyDecision } from './certify.js';
|
|
8
|
-
export { RunBuilder } from './run.js';
|
|
9
|
-
export { attest } from './attest.js';
|
|
10
|
-
export { exportCer, importCer } from './archive.js';
|
|
1
|
+
import { C as CreateSnapshotParams, A as AiExecutionSnapshotV1, V as VerificationResult, a as CerMeta, b as CerAiExecutionBundle, c as CertifyDecisionParams, R as RunBuilderOptions, S as StepParams, d as RunSummary, e as AttestOptions, f as AttestationResult } from './types-DF29BsH5.js';
|
|
2
|
+
export { g as AiExecutionParameters, P as ProviderConfig, W as WrappedExecutionParams, h as WrappedExecutionResult } from './types-DF29BsH5.js';
|
|
11
3
|
export { wrapProvider } from './providers/wrap.js';
|
|
12
|
-
|
|
4
|
+
|
|
5
|
+
declare class CerVerificationError extends Error {
|
|
6
|
+
readonly errors: string[];
|
|
7
|
+
constructor(errors: string[]);
|
|
8
|
+
}
|
|
9
|
+
declare class CerAttestationError extends Error {
|
|
10
|
+
readonly statusCode?: number;
|
|
11
|
+
readonly responseBody?: unknown;
|
|
12
|
+
readonly details?: string[];
|
|
13
|
+
constructor(message: string, statusCode?: number, responseBody?: unknown, details?: string[]);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare function toCanonicalJson(value: unknown): string;
|
|
17
|
+
|
|
18
|
+
declare function sha256Hex(data: string | Uint8Array): string;
|
|
19
|
+
declare function hashUtf8(value: string): string;
|
|
20
|
+
declare function hashCanonicalJson(value: unknown): string;
|
|
21
|
+
declare function computeInputHash(input: string | Record<string, unknown>): string;
|
|
22
|
+
declare function computeOutputHash(output: string | Record<string, unknown>): string;
|
|
23
|
+
|
|
24
|
+
declare function createSnapshot(params: CreateSnapshotParams): AiExecutionSnapshotV1;
|
|
25
|
+
declare function verifySnapshot(snapshot: AiExecutionSnapshotV1): VerificationResult;
|
|
26
|
+
|
|
27
|
+
declare function sealCer(snapshot: AiExecutionSnapshotV1, options?: {
|
|
28
|
+
createdAt?: string;
|
|
29
|
+
meta?: CerMeta;
|
|
30
|
+
}): CerAiExecutionBundle;
|
|
31
|
+
declare function verifyCer(bundle: CerAiExecutionBundle): VerificationResult;
|
|
32
|
+
|
|
33
|
+
declare function certifyDecision(params: CertifyDecisionParams): CerAiExecutionBundle;
|
|
34
|
+
|
|
35
|
+
declare class RunBuilder {
|
|
36
|
+
private readonly runId;
|
|
37
|
+
private readonly workflowId;
|
|
38
|
+
private readonly conversationId;
|
|
39
|
+
private readonly appId;
|
|
40
|
+
private stepIndex;
|
|
41
|
+
private prevStepHash;
|
|
42
|
+
private steps;
|
|
43
|
+
constructor(options?: RunBuilderOptions);
|
|
44
|
+
step(params: StepParams): CerAiExecutionBundle;
|
|
45
|
+
finalize(): RunSummary;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
declare function attest(bundle: CerAiExecutionBundle, options: AttestOptions): Promise<AttestationResult>;
|
|
49
|
+
|
|
50
|
+
declare function exportCer(bundle: CerAiExecutionBundle): string;
|
|
51
|
+
declare function importCer(json: string): CerAiExecutionBundle;
|
|
52
|
+
|
|
53
|
+
declare function sanitizeForAttestation(bundle: CerAiExecutionBundle): CerAiExecutionBundle;
|
|
54
|
+
declare function hasAttestation(bundle: unknown): boolean;
|
|
55
|
+
|
|
56
|
+
export { AiExecutionSnapshotV1, AttestOptions, AttestationResult, CerAiExecutionBundle, CerAttestationError, CerMeta, CerVerificationError, CertifyDecisionParams, CreateSnapshotParams, RunBuilder, RunBuilderOptions, RunSummary, StepParams, VerificationResult, attest, certifyDecision, computeInputHash, computeOutputHash, createSnapshot, exportCer, hasAttestation, hashCanonicalJson, hashUtf8, importCer, sanitizeForAttestation, sealCer, sha256Hex, toCanonicalJson, verifyCer as verify, verifyCer, verifySnapshot };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,550 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var CerVerificationError = class extends Error {
|
|
3
|
+
errors;
|
|
4
|
+
constructor(errors) {
|
|
5
|
+
super(`CER verification failed: ${errors.join("; ")}`);
|
|
6
|
+
this.name = "CerVerificationError";
|
|
7
|
+
this.errors = errors;
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var CerAttestationError = class extends Error {
|
|
11
|
+
statusCode;
|
|
12
|
+
responseBody;
|
|
13
|
+
details;
|
|
14
|
+
constructor(message, statusCode, responseBody, details) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "CerAttestationError";
|
|
17
|
+
this.statusCode = statusCode;
|
|
18
|
+
this.responseBody = responseBody;
|
|
19
|
+
this.details = details;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// src/canonicalJson.ts
|
|
24
|
+
function toCanonicalJson(value) {
|
|
25
|
+
return canonicalize(value);
|
|
26
|
+
}
|
|
27
|
+
function canonicalize(value) {
|
|
28
|
+
if (value === null) {
|
|
29
|
+
return "null";
|
|
30
|
+
}
|
|
31
|
+
if (typeof value === "boolean") {
|
|
32
|
+
return value ? "true" : "false";
|
|
33
|
+
}
|
|
34
|
+
if (typeof value === "number") {
|
|
35
|
+
if (!Number.isFinite(value)) {
|
|
36
|
+
throw new Error(`Non-finite number not allowed in canonical JSON: ${value}`);
|
|
37
|
+
}
|
|
38
|
+
return JSON.stringify(value);
|
|
39
|
+
}
|
|
40
|
+
if (typeof value === "string") {
|
|
41
|
+
return JSON.stringify(value);
|
|
42
|
+
}
|
|
43
|
+
if (Array.isArray(value)) {
|
|
44
|
+
const items = value.map((item) => canonicalize(item));
|
|
45
|
+
return "[" + items.join(",") + "]";
|
|
46
|
+
}
|
|
47
|
+
if (typeof value === "object") {
|
|
48
|
+
const obj = value;
|
|
49
|
+
const keys = Object.keys(obj).sort();
|
|
50
|
+
const entries = keys.map((key) => {
|
|
51
|
+
const val = obj[key];
|
|
52
|
+
if (val === void 0) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
return JSON.stringify(key) + ":" + canonicalize(val);
|
|
56
|
+
}).filter((e) => e !== null);
|
|
57
|
+
return "{" + entries.join(",") + "}";
|
|
58
|
+
}
|
|
59
|
+
throw new Error(`Unsupported type for canonical JSON: ${typeof value}`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// src/hash.ts
|
|
63
|
+
import * as crypto from "crypto";
|
|
64
|
+
function sha256Hex(data) {
|
|
65
|
+
const hash = crypto.createHash("sha256");
|
|
66
|
+
if (typeof data === "string") {
|
|
67
|
+
hash.update(data, "utf-8");
|
|
68
|
+
} else {
|
|
69
|
+
hash.update(data);
|
|
70
|
+
}
|
|
71
|
+
return hash.digest("hex");
|
|
72
|
+
}
|
|
73
|
+
function hashUtf8(value) {
|
|
74
|
+
return `sha256:${sha256Hex(value)}`;
|
|
75
|
+
}
|
|
76
|
+
function hashCanonicalJson(value) {
|
|
77
|
+
const canonical = toCanonicalJson(value);
|
|
78
|
+
return `sha256:${sha256Hex(canonical)}`;
|
|
79
|
+
}
|
|
80
|
+
function computeInputHash(input) {
|
|
81
|
+
if (typeof input === "string") {
|
|
82
|
+
return hashUtf8(input);
|
|
83
|
+
}
|
|
84
|
+
return hashCanonicalJson(input);
|
|
85
|
+
}
|
|
86
|
+
function computeOutputHash(output) {
|
|
87
|
+
if (typeof output === "string") {
|
|
88
|
+
return hashUtf8(output);
|
|
89
|
+
}
|
|
90
|
+
return hashCanonicalJson(output);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/snapshot.ts
|
|
94
|
+
var PACKAGE_VERSION = "0.4.0";
|
|
95
|
+
function validateParameters(params) {
|
|
96
|
+
const errors = [];
|
|
97
|
+
if (typeof params.temperature !== "number" || !Number.isFinite(params.temperature)) {
|
|
98
|
+
errors.push(`parameters.temperature must be a finite number, got: ${params.temperature}`);
|
|
99
|
+
}
|
|
100
|
+
if (typeof params.maxTokens !== "number" || !Number.isFinite(params.maxTokens)) {
|
|
101
|
+
errors.push(`parameters.maxTokens must be a finite number, got: ${params.maxTokens}`);
|
|
102
|
+
}
|
|
103
|
+
if (params.topP !== null && (typeof params.topP !== "number" || !Number.isFinite(params.topP))) {
|
|
104
|
+
errors.push(`parameters.topP must be a finite number or null, got: ${params.topP}`);
|
|
105
|
+
}
|
|
106
|
+
if (params.seed !== null && (typeof params.seed !== "number" || !Number.isFinite(params.seed))) {
|
|
107
|
+
errors.push(`parameters.seed must be a finite number or null, got: ${params.seed}`);
|
|
108
|
+
}
|
|
109
|
+
return errors;
|
|
110
|
+
}
|
|
111
|
+
function createSnapshot(params) {
|
|
112
|
+
const paramErrors = validateParameters(params.parameters);
|
|
113
|
+
if (paramErrors.length > 0) {
|
|
114
|
+
throw new Error(`Invalid parameters: ${paramErrors.join("; ")}`);
|
|
115
|
+
}
|
|
116
|
+
const inputHash = computeInputHash(params.input);
|
|
117
|
+
const outputHash = computeOutputHash(params.output);
|
|
118
|
+
const snapshot = {
|
|
119
|
+
type: "ai.execution.v1",
|
|
120
|
+
protocolVersion: "1.2.0",
|
|
121
|
+
executionSurface: "ai",
|
|
122
|
+
executionId: params.executionId,
|
|
123
|
+
timestamp: params.timestamp ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
124
|
+
provider: params.provider,
|
|
125
|
+
model: params.model,
|
|
126
|
+
modelVersion: params.modelVersion ?? null,
|
|
127
|
+
prompt: params.prompt,
|
|
128
|
+
input: params.input,
|
|
129
|
+
inputHash,
|
|
130
|
+
parameters: {
|
|
131
|
+
temperature: params.parameters.temperature,
|
|
132
|
+
maxTokens: params.parameters.maxTokens,
|
|
133
|
+
topP: params.parameters.topP ?? null,
|
|
134
|
+
seed: params.parameters.seed ?? null
|
|
135
|
+
},
|
|
136
|
+
output: params.output,
|
|
137
|
+
outputHash,
|
|
138
|
+
sdkVersion: params.sdkVersion ?? PACKAGE_VERSION,
|
|
139
|
+
appId: params.appId ?? null
|
|
140
|
+
};
|
|
141
|
+
if (params.runId !== void 0) snapshot.runId = params.runId ?? null;
|
|
142
|
+
if (params.stepId !== void 0) snapshot.stepId = params.stepId ?? null;
|
|
143
|
+
if (params.stepIndex !== void 0) snapshot.stepIndex = params.stepIndex ?? null;
|
|
144
|
+
if (params.workflowId !== void 0) snapshot.workflowId = params.workflowId ?? null;
|
|
145
|
+
if (params.conversationId !== void 0) snapshot.conversationId = params.conversationId ?? null;
|
|
146
|
+
if (params.prevStepHash !== void 0) snapshot.prevStepHash = params.prevStepHash ?? null;
|
|
147
|
+
return snapshot;
|
|
148
|
+
}
|
|
149
|
+
function verifySnapshot(snapshot) {
|
|
150
|
+
const errors = [];
|
|
151
|
+
if (snapshot.type !== "ai.execution.v1") {
|
|
152
|
+
errors.push(`Expected type "ai.execution.v1", got "${snapshot.type}"`);
|
|
153
|
+
}
|
|
154
|
+
if (snapshot.protocolVersion !== "1.2.0") {
|
|
155
|
+
errors.push(`Expected protocolVersion "1.2.0", got "${snapshot.protocolVersion}"`);
|
|
156
|
+
}
|
|
157
|
+
if (snapshot.executionSurface !== "ai") {
|
|
158
|
+
errors.push(`Expected executionSurface "ai", got "${snapshot.executionSurface}"`);
|
|
159
|
+
}
|
|
160
|
+
if (!snapshot.executionId || typeof snapshot.executionId !== "string") {
|
|
161
|
+
errors.push("executionId must be a non-empty string");
|
|
162
|
+
}
|
|
163
|
+
if (!snapshot.timestamp || typeof snapshot.timestamp !== "string") {
|
|
164
|
+
errors.push("timestamp must be a non-empty string");
|
|
165
|
+
}
|
|
166
|
+
if (!snapshot.provider || typeof snapshot.provider !== "string") {
|
|
167
|
+
errors.push("provider must be a non-empty string");
|
|
168
|
+
}
|
|
169
|
+
if (!snapshot.model || typeof snapshot.model !== "string") {
|
|
170
|
+
errors.push("model must be a non-empty string");
|
|
171
|
+
}
|
|
172
|
+
if (!snapshot.prompt || typeof snapshot.prompt !== "string") {
|
|
173
|
+
errors.push("prompt must be a non-empty string");
|
|
174
|
+
}
|
|
175
|
+
if (snapshot.input === void 0 || snapshot.input === null) {
|
|
176
|
+
errors.push("input must be a string or object");
|
|
177
|
+
}
|
|
178
|
+
if (snapshot.output === void 0 || snapshot.output === null) {
|
|
179
|
+
errors.push("output must be a string or object");
|
|
180
|
+
}
|
|
181
|
+
const paramErrors = validateParameters(snapshot.parameters);
|
|
182
|
+
errors.push(...paramErrors);
|
|
183
|
+
if (!snapshot.inputHash || !snapshot.inputHash.startsWith("sha256:")) {
|
|
184
|
+
errors.push(`inputHash must start with "sha256:", got "${snapshot.inputHash}"`);
|
|
185
|
+
}
|
|
186
|
+
if (!snapshot.outputHash || !snapshot.outputHash.startsWith("sha256:")) {
|
|
187
|
+
errors.push(`outputHash must start with "sha256:", got "${snapshot.outputHash}"`);
|
|
188
|
+
}
|
|
189
|
+
const expectedInputHash = computeInputHash(snapshot.input);
|
|
190
|
+
if (snapshot.inputHash !== expectedInputHash) {
|
|
191
|
+
errors.push(`inputHash mismatch: expected ${expectedInputHash}, got ${snapshot.inputHash}`);
|
|
192
|
+
}
|
|
193
|
+
const expectedOutputHash = computeOutputHash(snapshot.output);
|
|
194
|
+
if (snapshot.outputHash !== expectedOutputHash) {
|
|
195
|
+
errors.push(`outputHash mismatch: expected ${expectedOutputHash}, got ${snapshot.outputHash}`);
|
|
196
|
+
}
|
|
197
|
+
return { ok: errors.length === 0, errors };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// src/cer.ts
|
|
201
|
+
function computeCertificateHash(payload) {
|
|
202
|
+
const canonical = toCanonicalJson(payload);
|
|
203
|
+
return `sha256:${sha256Hex(canonical)}`;
|
|
204
|
+
}
|
|
205
|
+
function sealCer(snapshot, options) {
|
|
206
|
+
const createdAt = options?.createdAt ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
207
|
+
const payload = {
|
|
208
|
+
bundleType: "cer.ai.execution.v1",
|
|
209
|
+
createdAt,
|
|
210
|
+
snapshot,
|
|
211
|
+
version: "0.1"
|
|
212
|
+
};
|
|
213
|
+
const certificateHash = computeCertificateHash(payload);
|
|
214
|
+
const bundle = {
|
|
215
|
+
bundleType: "cer.ai.execution.v1",
|
|
216
|
+
certificateHash,
|
|
217
|
+
createdAt,
|
|
218
|
+
version: "0.1",
|
|
219
|
+
snapshot
|
|
220
|
+
};
|
|
221
|
+
if (options?.meta) {
|
|
222
|
+
bundle.meta = options.meta;
|
|
223
|
+
}
|
|
224
|
+
return bundle;
|
|
225
|
+
}
|
|
226
|
+
function verifyCer(bundle) {
|
|
227
|
+
const errors = [];
|
|
228
|
+
if (bundle.bundleType !== "cer.ai.execution.v1") {
|
|
229
|
+
errors.push(`Expected bundleType "cer.ai.execution.v1", got "${bundle.bundleType}"`);
|
|
230
|
+
}
|
|
231
|
+
if (bundle.version !== "0.1") {
|
|
232
|
+
errors.push(`Expected version "0.1", got "${bundle.version}"`);
|
|
233
|
+
}
|
|
234
|
+
if (!bundle.createdAt || typeof bundle.createdAt !== "string") {
|
|
235
|
+
errors.push("createdAt must be a non-empty string");
|
|
236
|
+
}
|
|
237
|
+
if (!bundle.certificateHash || !bundle.certificateHash.startsWith("sha256:")) {
|
|
238
|
+
errors.push(`certificateHash must start with "sha256:", got "${bundle.certificateHash}"`);
|
|
239
|
+
}
|
|
240
|
+
if (!bundle.snapshot) {
|
|
241
|
+
errors.push("snapshot is required");
|
|
242
|
+
return { ok: false, errors };
|
|
243
|
+
}
|
|
244
|
+
const snapshotResult = verifySnapshot(bundle.snapshot);
|
|
245
|
+
errors.push(...snapshotResult.errors);
|
|
246
|
+
const payload = {
|
|
247
|
+
bundleType: "cer.ai.execution.v1",
|
|
248
|
+
createdAt: bundle.createdAt,
|
|
249
|
+
snapshot: bundle.snapshot,
|
|
250
|
+
version: "0.1"
|
|
251
|
+
};
|
|
252
|
+
const expectedHash = computeCertificateHash(payload);
|
|
253
|
+
if (bundle.certificateHash !== expectedHash) {
|
|
254
|
+
errors.push(`certificateHash mismatch: expected ${expectedHash}, got ${bundle.certificateHash}`);
|
|
255
|
+
}
|
|
256
|
+
return { ok: errors.length === 0, errors };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// src/certify.ts
|
|
260
|
+
import * as crypto2 from "crypto";
|
|
261
|
+
function certifyDecision(params) {
|
|
262
|
+
const executionId = params.executionId ?? crypto2.randomUUID();
|
|
263
|
+
const snapshot = createSnapshot({
|
|
264
|
+
executionId,
|
|
265
|
+
timestamp: params.timestamp,
|
|
266
|
+
provider: params.provider,
|
|
267
|
+
model: params.model,
|
|
268
|
+
modelVersion: params.modelVersion,
|
|
269
|
+
prompt: params.prompt,
|
|
270
|
+
input: params.input,
|
|
271
|
+
parameters: params.parameters,
|
|
272
|
+
output: params.output,
|
|
273
|
+
sdkVersion: params.sdkVersion,
|
|
274
|
+
appId: params.appId,
|
|
275
|
+
runId: params.runId,
|
|
276
|
+
stepId: params.stepId,
|
|
277
|
+
stepIndex: params.stepIndex,
|
|
278
|
+
workflowId: params.workflowId,
|
|
279
|
+
conversationId: params.conversationId,
|
|
280
|
+
prevStepHash: params.prevStepHash
|
|
281
|
+
});
|
|
282
|
+
return sealCer(snapshot, { meta: params.meta });
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// src/run.ts
|
|
286
|
+
import * as crypto3 from "crypto";
|
|
287
|
+
var RunBuilder = class {
|
|
288
|
+
runId;
|
|
289
|
+
workflowId;
|
|
290
|
+
conversationId;
|
|
291
|
+
appId;
|
|
292
|
+
stepIndex = 0;
|
|
293
|
+
prevStepHash = null;
|
|
294
|
+
steps = [];
|
|
295
|
+
constructor(options) {
|
|
296
|
+
this.runId = options?.runId ?? crypto3.randomUUID();
|
|
297
|
+
this.workflowId = options?.workflowId ?? null;
|
|
298
|
+
this.conversationId = options?.conversationId ?? null;
|
|
299
|
+
this.appId = options?.appId ?? null;
|
|
300
|
+
}
|
|
301
|
+
step(params) {
|
|
302
|
+
const stepId = params.stepId ?? crypto3.randomUUID();
|
|
303
|
+
const executionId = `${this.runId}-step-${this.stepIndex}`;
|
|
304
|
+
const snapshot = createSnapshot({
|
|
305
|
+
executionId,
|
|
306
|
+
timestamp: params.timestamp,
|
|
307
|
+
provider: params.provider,
|
|
308
|
+
model: params.model,
|
|
309
|
+
modelVersion: params.modelVersion,
|
|
310
|
+
prompt: params.prompt,
|
|
311
|
+
input: params.input,
|
|
312
|
+
parameters: params.parameters,
|
|
313
|
+
output: params.output,
|
|
314
|
+
appId: this.appId,
|
|
315
|
+
runId: this.runId,
|
|
316
|
+
stepId,
|
|
317
|
+
stepIndex: this.stepIndex,
|
|
318
|
+
workflowId: this.workflowId,
|
|
319
|
+
conversationId: this.conversationId,
|
|
320
|
+
prevStepHash: this.prevStepHash
|
|
321
|
+
});
|
|
322
|
+
const bundle = sealCer(snapshot, { meta: params.meta });
|
|
323
|
+
this.steps.push({
|
|
324
|
+
stepIndex: this.stepIndex,
|
|
325
|
+
stepId,
|
|
326
|
+
executionId,
|
|
327
|
+
certificateHash: bundle.certificateHash,
|
|
328
|
+
prevStepHash: this.prevStepHash
|
|
329
|
+
});
|
|
330
|
+
this.prevStepHash = bundle.certificateHash;
|
|
331
|
+
this.stepIndex++;
|
|
332
|
+
return bundle;
|
|
333
|
+
}
|
|
334
|
+
finalize() {
|
|
335
|
+
return {
|
|
336
|
+
runId: this.runId,
|
|
337
|
+
workflowId: this.workflowId,
|
|
338
|
+
conversationId: this.conversationId,
|
|
339
|
+
stepCount: this.steps.length,
|
|
340
|
+
steps: [...this.steps],
|
|
341
|
+
finalStepHash: this.prevStepHash
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
// src/sanitize.ts
|
|
347
|
+
function deepRemoveUndefined(value) {
|
|
348
|
+
if (value === null || value === void 0) return value;
|
|
349
|
+
if (typeof value === "bigint") {
|
|
350
|
+
throw new Error("BigInt values are not JSON-safe and cannot be sanitized");
|
|
351
|
+
}
|
|
352
|
+
if (typeof value === "function") {
|
|
353
|
+
throw new Error("Function values are not JSON-safe and cannot be sanitized");
|
|
354
|
+
}
|
|
355
|
+
if (typeof value === "symbol") {
|
|
356
|
+
throw new Error("Symbol values are not JSON-safe and cannot be sanitized");
|
|
357
|
+
}
|
|
358
|
+
if (Array.isArray(value)) {
|
|
359
|
+
return value.map(deepRemoveUndefined);
|
|
360
|
+
}
|
|
361
|
+
if (typeof value === "object") {
|
|
362
|
+
const result = {};
|
|
363
|
+
for (const [key, val] of Object.entries(value)) {
|
|
364
|
+
if (val === void 0) continue;
|
|
365
|
+
result[key] = deepRemoveUndefined(val);
|
|
366
|
+
}
|
|
367
|
+
return result;
|
|
368
|
+
}
|
|
369
|
+
return value;
|
|
370
|
+
}
|
|
371
|
+
function sanitizeForAttestation(bundle) {
|
|
372
|
+
return deepRemoveUndefined(bundle);
|
|
373
|
+
}
|
|
374
|
+
function hasAttestation(bundle) {
|
|
375
|
+
if (typeof bundle !== "object" || bundle === null) return false;
|
|
376
|
+
const b = bundle;
|
|
377
|
+
if (typeof b.attestationId === "string" && b.attestationId.length > 0) return true;
|
|
378
|
+
if (typeof b.nodeRuntimeHash === "string" && b.nodeRuntimeHash.length > 0) return true;
|
|
379
|
+
if (typeof b.attestation === "object" && b.attestation !== null) {
|
|
380
|
+
const att = b.attestation;
|
|
381
|
+
if (typeof att.attestationId === "string" && att.attestationId.length > 0) return true;
|
|
382
|
+
if (typeof att.nodeRuntimeHash === "string" && att.nodeRuntimeHash.length > 0) return true;
|
|
383
|
+
}
|
|
384
|
+
return false;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// src/attest.ts
|
|
388
|
+
var SHA256_PATTERN = /^sha256:[0-9a-f]{64}$/;
|
|
389
|
+
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
390
|
+
function validateHashFormat(value, fieldName) {
|
|
391
|
+
if (typeof value !== "string") return null;
|
|
392
|
+
if (!SHA256_PATTERN.test(value)) {
|
|
393
|
+
return `${fieldName} is not in sha256:<64hex> format: "${value}"`;
|
|
394
|
+
}
|
|
395
|
+
return null;
|
|
396
|
+
}
|
|
397
|
+
async function attest(bundle, options) {
|
|
398
|
+
const url = `${options.nodeUrl.replace(/\/+$/, "")}/api/attest`;
|
|
399
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
400
|
+
const sanitized = sanitizeForAttestation(bundle);
|
|
401
|
+
const controller = new AbortController();
|
|
402
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
403
|
+
let response;
|
|
404
|
+
try {
|
|
405
|
+
response = await fetch(url, {
|
|
406
|
+
method: "POST",
|
|
407
|
+
headers: {
|
|
408
|
+
"Content-Type": "application/json",
|
|
409
|
+
"Authorization": `Bearer ${options.apiKey}`
|
|
410
|
+
},
|
|
411
|
+
body: JSON.stringify(sanitized),
|
|
412
|
+
signal: controller.signal
|
|
413
|
+
});
|
|
414
|
+
} catch (err) {
|
|
415
|
+
clearTimeout(timer);
|
|
416
|
+
const error = err;
|
|
417
|
+
if (error.name === "AbortError") {
|
|
418
|
+
throw new CerAttestationError(
|
|
419
|
+
`Attestation request timed out after ${timeoutMs}ms`
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
throw new CerAttestationError(
|
|
423
|
+
`Network error contacting attestation node: ${error.message}`
|
|
424
|
+
);
|
|
425
|
+
} finally {
|
|
426
|
+
clearTimeout(timer);
|
|
427
|
+
}
|
|
428
|
+
let body;
|
|
429
|
+
try {
|
|
430
|
+
body = await response.json();
|
|
431
|
+
} catch {
|
|
432
|
+
const text = await response.text().catch(() => "");
|
|
433
|
+
throw new CerAttestationError(
|
|
434
|
+
`Attestation node returned non-JSON response (${response.status}): ${text}`,
|
|
435
|
+
response.status
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
if (!response.ok) {
|
|
439
|
+
const result2 = body;
|
|
440
|
+
const msg = typeof result2.error === "string" ? result2.error : `HTTP ${response.status}`;
|
|
441
|
+
const details = Array.isArray(result2.details) ? result2.details : void 0;
|
|
442
|
+
throw new CerAttestationError(
|
|
443
|
+
`Attestation failed: ${msg}`,
|
|
444
|
+
response.status,
|
|
445
|
+
body,
|
|
446
|
+
details
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
const result = body;
|
|
450
|
+
const errors = [];
|
|
451
|
+
if (typeof result.certificateHash === "string" && result.certificateHash !== bundle.certificateHash) {
|
|
452
|
+
errors.push(
|
|
453
|
+
`Node returned certificateHash "${result.certificateHash}" but bundle has "${bundle.certificateHash}"`
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
const certHashErr = validateHashFormat(result.certificateHash, "response.certificateHash");
|
|
457
|
+
if (certHashErr) errors.push(certHashErr);
|
|
458
|
+
const runtimeHashErr = validateHashFormat(result.nodeRuntimeHash, "response.nodeRuntimeHash");
|
|
459
|
+
if (runtimeHashErr) errors.push(runtimeHashErr);
|
|
460
|
+
if (errors.length > 0) {
|
|
461
|
+
throw new CerAttestationError(
|
|
462
|
+
`Attestation response validation failed: ${errors.join("; ")}`,
|
|
463
|
+
response.status,
|
|
464
|
+
body,
|
|
465
|
+
errors
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
return {
|
|
469
|
+
ok: true,
|
|
470
|
+
attestationId: typeof result.attestationId === "string" ? result.attestationId : void 0,
|
|
471
|
+
nodeRuntimeHash: typeof result.nodeRuntimeHash === "string" ? result.nodeRuntimeHash : void 0,
|
|
472
|
+
certificateHash: typeof result.certificateHash === "string" ? result.certificateHash : void 0,
|
|
473
|
+
protocolVersion: typeof result.protocolVersion === "string" ? result.protocolVersion : void 0,
|
|
474
|
+
raw: body
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// src/archive.ts
|
|
479
|
+
function exportCer(bundle) {
|
|
480
|
+
return toCanonicalJson(bundle);
|
|
481
|
+
}
|
|
482
|
+
function importCer(json) {
|
|
483
|
+
let parsed;
|
|
484
|
+
try {
|
|
485
|
+
parsed = JSON.parse(json);
|
|
486
|
+
} catch (err) {
|
|
487
|
+
throw new CerVerificationError([`Invalid JSON: ${err.message}`]);
|
|
488
|
+
}
|
|
489
|
+
const bundle = parsed;
|
|
490
|
+
if (!bundle || typeof bundle !== "object") {
|
|
491
|
+
throw new CerVerificationError(["Parsed value is not an object"]);
|
|
492
|
+
}
|
|
493
|
+
if (bundle.bundleType !== "cer.ai.execution.v1") {
|
|
494
|
+
throw new CerVerificationError([`Expected bundleType "cer.ai.execution.v1", got "${bundle.bundleType}"`]);
|
|
495
|
+
}
|
|
496
|
+
const result = verifyCer(bundle);
|
|
497
|
+
if (!result.ok) {
|
|
498
|
+
throw new CerVerificationError(result.errors);
|
|
499
|
+
}
|
|
500
|
+
return bundle;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// src/providers/wrap.ts
|
|
504
|
+
import * as crypto4 from "crypto";
|
|
505
|
+
function wrapProvider(config) {
|
|
506
|
+
return {
|
|
507
|
+
async execute(params) {
|
|
508
|
+
const raw = await config.callFn(params.providerInput);
|
|
509
|
+
const output = config.extractOutput(raw);
|
|
510
|
+
const modelVersion = config.extractModelVersion ? config.extractModelVersion(raw) : params.modelVersion ?? null;
|
|
511
|
+
const snapshot = createSnapshot({
|
|
512
|
+
executionId: params.executionId ?? crypto4.randomUUID(),
|
|
513
|
+
provider: config.provider,
|
|
514
|
+
model: params.model,
|
|
515
|
+
modelVersion,
|
|
516
|
+
prompt: params.prompt,
|
|
517
|
+
input: params.input,
|
|
518
|
+
parameters: params.parameters,
|
|
519
|
+
output,
|
|
520
|
+
appId: params.appId
|
|
521
|
+
});
|
|
522
|
+
const bundle = sealCer(snapshot, { meta: params.meta });
|
|
523
|
+
return { output, snapshot, bundle };
|
|
524
|
+
}
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
export {
|
|
528
|
+
CerAttestationError,
|
|
529
|
+
CerVerificationError,
|
|
530
|
+
RunBuilder,
|
|
531
|
+
attest,
|
|
532
|
+
certifyDecision,
|
|
533
|
+
computeInputHash,
|
|
534
|
+
computeOutputHash,
|
|
535
|
+
createSnapshot,
|
|
536
|
+
exportCer,
|
|
537
|
+
hasAttestation,
|
|
538
|
+
hashCanonicalJson,
|
|
539
|
+
hashUtf8,
|
|
540
|
+
importCer,
|
|
541
|
+
sanitizeForAttestation,
|
|
542
|
+
sealCer,
|
|
543
|
+
sha256Hex,
|
|
544
|
+
toCanonicalJson,
|
|
545
|
+
verifyCer as verify,
|
|
546
|
+
verifyCer,
|
|
547
|
+
verifySnapshot,
|
|
548
|
+
wrapProvider
|
|
549
|
+
};
|
|
550
|
+
//# sourceMappingURL=index.mjs.map
|