@nexart/codemode-sdk 1.7.0 → 1.8.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/CHANGELOG.md +99 -0
- package/COMMERCIAL.md +41 -0
- package/README.md +223 -608
- package/dist/core-index.d.ts +1 -1
- package/dist/core-index.js +1 -1
- package/dist/entry/browser.d.ts +8 -2
- package/dist/entry/browser.d.ts.map +1 -1
- package/dist/entry/browser.js +10 -2
- package/dist/entry/node.d.ts +3 -1
- package/dist/entry/node.d.ts.map +1 -1
- package/dist/entry/node.js +3 -1
- package/dist/runtime.d.ts +52 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +219 -0
- package/examples/basic.ts +61 -0
- package/examples/preflight-test.ts +275 -0
- package/examples/verify.ts +151 -0
- package/package.json +20 -3
package/dist/core-index.d.ts
CHANGED
|
@@ -16,6 +16,6 @@ export { createEngine, } from './engine';
|
|
|
16
16
|
/**
|
|
17
17
|
* SDK Identity
|
|
18
18
|
*/
|
|
19
|
-
export declare const SDK_VERSION = "1.
|
|
19
|
+
export declare const SDK_VERSION = "1.8.0";
|
|
20
20
|
export declare const SDK_NAME = "@nexart/codemode-sdk";
|
|
21
21
|
//# sourceMappingURL=core-index.d.ts.map
|
package/dist/core-index.js
CHANGED
package/dist/entry/browser.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @nexart/codemode-sdk/browser — Browser-Safe Entry Point
|
|
3
3
|
*
|
|
4
4
|
* ╔══════════════════════════════════════════════════════════════════════════╗
|
|
5
|
-
* ║ BROWSER-SAFE SDK ENTRY POINT (v1.
|
|
5
|
+
* ║ BROWSER-SAFE SDK ENTRY POINT (v1.8.0) ║
|
|
6
6
|
* ║ ║
|
|
7
7
|
* ║ This entrypoint exports ONLY browser-safe modules. ║
|
|
8
8
|
* ║ It does NOT include static-engine or any Node.js dependencies. ║
|
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
* ║ Use this for Vite, React, Next.js, or any browser environment. ║
|
|
11
11
|
* ║ ║
|
|
12
12
|
* ║ For Node.js/server: import from '@nexart/codemode-sdk/node' ║
|
|
13
|
+
* ║ ║
|
|
14
|
+
* ║ AI AGENTS: Start with createRuntime({ seed, strict: true }) ║
|
|
13
15
|
* ╚══════════════════════════════════════════════════════════════════════════╝
|
|
14
16
|
*/
|
|
15
17
|
export type { RenderMode, RuntimeCanvas, EngineConfig, RenderResult, RunOptions, ProgressInfo, Engine, TimeVariables, ProtocolVariables, ProtocolMetadata, ExecuteCodeModeInput, ExecuteCodeModeResult, NexArtBuilderManifest, } from '../types';
|
|
@@ -21,7 +23,9 @@ export { runLoopMode, cancelLoopMode, } from '../loop-engine';
|
|
|
21
23
|
export { validateCodeModeSource, } from '../execute';
|
|
22
24
|
export { createEngine, } from '../engine';
|
|
23
25
|
export { registerBuilderManifest, } from '../builder-manifest';
|
|
24
|
-
export
|
|
26
|
+
export { createRuntime, NexArtRuntime, RUNTIME_VERSION, } from '../runtime';
|
|
27
|
+
export type { RuntimeOptions, RuntimeState, NexArtRuntime as NexArtRuntimeType, } from '../runtime';
|
|
28
|
+
export declare const SDK_VERSION = "1.8.0";
|
|
25
29
|
export declare const SDK_NAME = "@nexart/codemode-sdk";
|
|
26
30
|
export declare const SDK_ENTRY = "browser";
|
|
27
31
|
/**
|
|
@@ -33,5 +37,7 @@ export declare const SDK_ENTRY = "browser";
|
|
|
33
37
|
* - Use runLoopMode() directly for animations
|
|
34
38
|
* - For static rendering, use the server-side endpoint
|
|
35
39
|
* - Or import from '@nexart/codemode-sdk/node' in SSR contexts
|
|
40
|
+
*
|
|
41
|
+
* AI AGENTS: Use createRuntime({ seed, strict: true }) for deterministic execution.
|
|
36
42
|
*/
|
|
37
43
|
//# sourceMappingURL=browser.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../../entry/browser.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../../entry/browser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAKH,YAAY,EACV,UAAU,EACV,aAAa,EACb,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,MAAM,EACN,aAAa,EACb,iBAAiB,EACjB,gBAAgB,EAChB,oBAAoB,EACpB,qBAAqB,EACrB,qBAAqB,GACtB,MAAM,UAAU,CAAC;AAElB,OAAO,EACL,iBAAiB,EACjB,YAAY,EACZ,cAAc,GACf,MAAM,UAAU,CAAC;AAKlB,OAAO,EACL,cAAc,EACd,mBAAmB,EACnB,cAAc,EACd,mBAAmB,EACnB,uBAAuB,EACvB,gBAAgB,GACjB,MAAM,sBAAsB,CAAC;AAK9B,OAAO,EACL,eAAe,EACf,mBAAmB,EACnB,uBAAuB,EACvB,iBAAiB,EACjB,SAAS,EACT,OAAO,EACP,OAAO,EACP,0BAA0B,EAC1B,wBAAwB,EACxB,qBAAqB,GACtB,MAAM,eAAe,CAAC;AAEvB,YAAY,EACV,SAAS,EACT,eAAe,GAChB,MAAM,eAAe,CAAC;AAKvB,OAAO,EACL,WAAW,EACX,cAAc,GACf,MAAM,gBAAgB,CAAC;AAKxB,OAAO,EACL,sBAAsB,GACvB,MAAM,YAAY,CAAC;AAKpB,OAAO,EACL,YAAY,GACb,MAAM,WAAW,CAAC;AAKnB,OAAO,EACL,uBAAuB,GACxB,MAAM,qBAAqB,CAAC;AAK7B,OAAO,EACL,aAAa,EACb,aAAa,EACb,eAAe,GAChB,MAAM,YAAY,CAAC;AAEpB,YAAY,EACV,cAAc,EACd,YAAY,EACZ,aAAa,IAAI,iBAAiB,GACnC,MAAM,YAAY,CAAC;AAKpB,eAAO,MAAM,WAAW,UAAU,CAAC;AACnC,eAAO,MAAM,QAAQ,yBAAyB,CAAC;AAC/C,eAAO,MAAM,SAAS,YAAY,CAAC;AAEnC;;;;;;;;;;;GAWG"}
|
package/dist/entry/browser.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @nexart/codemode-sdk/browser — Browser-Safe Entry Point
|
|
3
3
|
*
|
|
4
4
|
* ╔══════════════════════════════════════════════════════════════════════════╗
|
|
5
|
-
* ║ BROWSER-SAFE SDK ENTRY POINT (v1.
|
|
5
|
+
* ║ BROWSER-SAFE SDK ENTRY POINT (v1.8.0) ║
|
|
6
6
|
* ║ ║
|
|
7
7
|
* ║ This entrypoint exports ONLY browser-safe modules. ║
|
|
8
8
|
* ║ It does NOT include static-engine or any Node.js dependencies. ║
|
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
* ║ Use this for Vite, React, Next.js, or any browser environment. ║
|
|
11
11
|
* ║ ║
|
|
12
12
|
* ║ For Node.js/server: import from '@nexart/codemode-sdk/node' ║
|
|
13
|
+
* ║ ║
|
|
14
|
+
* ║ AI AGENTS: Start with createRuntime({ seed, strict: true }) ║
|
|
13
15
|
* ╚══════════════════════════════════════════════════════════════════════════╝
|
|
14
16
|
*/
|
|
15
17
|
export { PROTOCOL_IDENTITY, DEFAULT_VARS, DEFAULT_CONFIG, } from '../types';
|
|
@@ -38,9 +40,13 @@ export { createEngine, } from '../engine';
|
|
|
38
40
|
// ═══════════════════════════════════════════════════════════════════════════
|
|
39
41
|
export { registerBuilderManifest, } from '../builder-manifest';
|
|
40
42
|
// ═══════════════════════════════════════════════════════════════════════════
|
|
43
|
+
// AGENT-FIRST RUNTIME — v1.8.0 (Browser-safe, no Node dependencies)
|
|
44
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
45
|
+
export { createRuntime, NexArtRuntime, RUNTIME_VERSION, } from '../runtime';
|
|
46
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
41
47
|
// SDK IDENTITY
|
|
42
48
|
// ═══════════════════════════════════════════════════════════════════════════
|
|
43
|
-
export const SDK_VERSION = '1.
|
|
49
|
+
export const SDK_VERSION = '1.8.0';
|
|
44
50
|
export const SDK_NAME = '@nexart/codemode-sdk';
|
|
45
51
|
export const SDK_ENTRY = 'browser';
|
|
46
52
|
/**
|
|
@@ -52,4 +58,6 @@ export const SDK_ENTRY = 'browser';
|
|
|
52
58
|
* - Use runLoopMode() directly for animations
|
|
53
59
|
* - For static rendering, use the server-side endpoint
|
|
54
60
|
* - Or import from '@nexart/codemode-sdk/node' in SSR contexts
|
|
61
|
+
*
|
|
62
|
+
* AI AGENTS: Use createRuntime({ seed, strict: true }) for deterministic execution.
|
|
55
63
|
*/
|
package/dist/entry/node.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @nexart/codemode-sdk/node — Node.js Entry Point
|
|
3
3
|
*
|
|
4
4
|
* ╔══════════════════════════════════════════════════════════════════════════╗
|
|
5
|
-
* ║ NODE.JS SDK ENTRY POINT (v1.
|
|
5
|
+
* ║ NODE.JS SDK ENTRY POINT (v1.8.0) ║
|
|
6
6
|
* ║ ║
|
|
7
7
|
* ║ This entrypoint exports Node.js-specific modules that require: ║
|
|
8
8
|
* ║ - canvas package (node-canvas) ║
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
* ║ Use this for server-side rendering, oracles, or CLI tools. ║
|
|
12
12
|
* ║ ║
|
|
13
13
|
* ║ For browser/Vite: import from '@nexart/codemode-sdk/browser' ║
|
|
14
|
+
* ║ ║
|
|
15
|
+
* ║ AI AGENTS: Start with createRuntime({ seed, strict: true }) ║
|
|
14
16
|
* ╚══════════════════════════════════════════════════════════════════════════╝
|
|
15
17
|
*/
|
|
16
18
|
export * from './browser';
|
package/dist/entry/node.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../../entry/node.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../../entry/node.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAKH,cAAc,WAAW,CAAC;AAK1B,OAAO,EACL,aAAa,GACd,MAAM,kBAAkB,CAAC;AAK1B,OAAO,EACL,eAAe,GAChB,MAAM,YAAY,CAAC;AAKpB,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAClD,eAAO,MAAM,SAAS,SAAS,CAAC"}
|
package/dist/entry/node.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @nexart/codemode-sdk/node — Node.js Entry Point
|
|
3
3
|
*
|
|
4
4
|
* ╔══════════════════════════════════════════════════════════════════════════╗
|
|
5
|
-
* ║ NODE.JS SDK ENTRY POINT (v1.
|
|
5
|
+
* ║ NODE.JS SDK ENTRY POINT (v1.8.0) ║
|
|
6
6
|
* ║ ║
|
|
7
7
|
* ║ This entrypoint exports Node.js-specific modules that require: ║
|
|
8
8
|
* ║ - canvas package (node-canvas) ║
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
* ║ Use this for server-side rendering, oracles, or CLI tools. ║
|
|
12
12
|
* ║ ║
|
|
13
13
|
* ║ For browser/Vite: import from '@nexart/codemode-sdk/browser' ║
|
|
14
|
+
* ║ ║
|
|
15
|
+
* ║ AI AGENTS: Start with createRuntime({ seed, strict: true }) ║
|
|
14
16
|
* ╚══════════════════════════════════════════════════════════════════════════╝
|
|
15
17
|
*/
|
|
16
18
|
// ═══════════════════════════════════════════════════════════════════════════
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NexArt Code Mode SDK - Agent-First Runtime Authority Layer
|
|
3
|
+
* Version: 1.8.0 (Protocol v1.2.0)
|
|
4
|
+
*
|
|
5
|
+
* ╔══════════════════════════════════════════════════════════════════════════╗
|
|
6
|
+
* ║ AGENT-FIRST RUNTIME — DETERMINISTIC EXECUTION AUTHORITY ║
|
|
7
|
+
* ║ ║
|
|
8
|
+
* ║ This module provides a high-level runtime API designed for AI agents ║
|
|
9
|
+
* ║ (Replit, Lovable, Claude Code) to reliably execute deterministic code. ║
|
|
10
|
+
* ║ ║
|
|
11
|
+
* ║ Key Features: ║
|
|
12
|
+
* ║ - Deterministic PRNG: runtime.random() (seeded Mulberry32) ║
|
|
13
|
+
* ║ - Deterministic noise: runtime.noise(x, y?, z?) (seeded Perlin) ║
|
|
14
|
+
* ║ - Strict mode: Throws on Math.random, Date.now, performance.now ║
|
|
15
|
+
* ║ - Digest: Stable hash for verification and replay ║
|
|
16
|
+
* ║ - State snapshot: Canonical state for bundles ║
|
|
17
|
+
* ║ ║
|
|
18
|
+
* ║ BROWSER-SAFE: No Node.js dependencies. Works in Vite/Next/React. ║
|
|
19
|
+
* ╚══════════════════════════════════════════════════════════════════════════╝
|
|
20
|
+
*/
|
|
21
|
+
export declare const RUNTIME_VERSION = "1.8.0";
|
|
22
|
+
export interface RuntimeOptions {
|
|
23
|
+
seed: string | number;
|
|
24
|
+
vars?: number[];
|
|
25
|
+
strict?: boolean;
|
|
26
|
+
mode?: 'static' | 'loop';
|
|
27
|
+
metadata?: Record<string, unknown>;
|
|
28
|
+
}
|
|
29
|
+
export interface RuntimeState {
|
|
30
|
+
sdkVersion: string;
|
|
31
|
+
seed: number;
|
|
32
|
+
vars: number[];
|
|
33
|
+
mode: 'static' | 'loop';
|
|
34
|
+
metadata?: Record<string, unknown>;
|
|
35
|
+
}
|
|
36
|
+
export interface NexArtRuntime {
|
|
37
|
+
random(): number;
|
|
38
|
+
randomInt(min: number, max: number): number;
|
|
39
|
+
randomRange(min: number, max: number): number;
|
|
40
|
+
noise(x: number, y?: number, z?: number): number;
|
|
41
|
+
run<T>(fn: () => T): T;
|
|
42
|
+
digest(): string;
|
|
43
|
+
getState(): RuntimeState;
|
|
44
|
+
getSeed(): number;
|
|
45
|
+
readonly strict: boolean;
|
|
46
|
+
}
|
|
47
|
+
export declare function createRuntime(options: RuntimeOptions): NexArtRuntime;
|
|
48
|
+
export declare const NexArtRuntime: {
|
|
49
|
+
create: typeof createRuntime;
|
|
50
|
+
VERSION: string;
|
|
51
|
+
};
|
|
52
|
+
//# sourceMappingURL=runtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,eAAO,MAAM,eAAe,UAAU,CAAC;AAEvC,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;IACtB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,IAAI,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAC;IACzB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED,MAAM,WAAW,YAAY;IAC3B,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,IAAI,EAAE,QAAQ,GAAG,MAAM,CAAC;IACxB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED,MAAM,WAAW,aAAa;IAC5B,MAAM,IAAI,MAAM,CAAC;IACjB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;IAC5C,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;IAC9C,KAAK,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACjD,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;IACvB,MAAM,IAAI,MAAM,CAAC;IACjB,QAAQ,IAAI,YAAY,CAAC;IACzB,OAAO,IAAI,MAAM,CAAC;IAClB,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;CAC1B;AAqGD,wBAAgB,aAAa,CAAC,OAAO,EAAE,cAAc,GAAG,aAAa,CAuHpE;AAED,eAAO,MAAM,aAAa;;;CAGzB,CAAC"}
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NexArt Code Mode SDK - Agent-First Runtime Authority Layer
|
|
3
|
+
* Version: 1.8.0 (Protocol v1.2.0)
|
|
4
|
+
*
|
|
5
|
+
* ╔══════════════════════════════════════════════════════════════════════════╗
|
|
6
|
+
* ║ AGENT-FIRST RUNTIME — DETERMINISTIC EXECUTION AUTHORITY ║
|
|
7
|
+
* ║ ║
|
|
8
|
+
* ║ This module provides a high-level runtime API designed for AI agents ║
|
|
9
|
+
* ║ (Replit, Lovable, Claude Code) to reliably execute deterministic code. ║
|
|
10
|
+
* ║ ║
|
|
11
|
+
* ║ Key Features: ║
|
|
12
|
+
* ║ - Deterministic PRNG: runtime.random() (seeded Mulberry32) ║
|
|
13
|
+
* ║ - Deterministic noise: runtime.noise(x, y?, z?) (seeded Perlin) ║
|
|
14
|
+
* ║ - Strict mode: Throws on Math.random, Date.now, performance.now ║
|
|
15
|
+
* ║ - Digest: Stable hash for verification and replay ║
|
|
16
|
+
* ║ - State snapshot: Canonical state for bundles ║
|
|
17
|
+
* ║ ║
|
|
18
|
+
* ║ BROWSER-SAFE: No Node.js dependencies. Works in Vite/Next/React. ║
|
|
19
|
+
* ╚══════════════════════════════════════════════════════════════════════════╝
|
|
20
|
+
*/
|
|
21
|
+
export const RUNTIME_VERSION = '1.8.0';
|
|
22
|
+
function hashSeed(seed) {
|
|
23
|
+
if (typeof seed === 'number') {
|
|
24
|
+
return Math.floor(seed) >>> 0;
|
|
25
|
+
}
|
|
26
|
+
let hash = 0;
|
|
27
|
+
for (let i = 0; i < seed.length; i++) {
|
|
28
|
+
const char = seed.charCodeAt(i);
|
|
29
|
+
hash = ((hash << 5) - hash) + char;
|
|
30
|
+
hash = hash >>> 0;
|
|
31
|
+
}
|
|
32
|
+
return hash || 1;
|
|
33
|
+
}
|
|
34
|
+
function createSeededRNG(seed) {
|
|
35
|
+
let a = seed >>> 0;
|
|
36
|
+
return () => {
|
|
37
|
+
a += 0x6D2B79F5;
|
|
38
|
+
let t = Math.imul(a ^ (a >>> 15), a | 1);
|
|
39
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
40
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function createSeededNoise(seed) {
|
|
44
|
+
const permutation = [];
|
|
45
|
+
const rng = createSeededRNG(seed);
|
|
46
|
+
for (let i = 0; i < 256; i++) {
|
|
47
|
+
permutation[i] = i;
|
|
48
|
+
}
|
|
49
|
+
for (let i = 255; i > 0; i--) {
|
|
50
|
+
const j = Math.floor(rng() * (i + 1));
|
|
51
|
+
[permutation[i], permutation[j]] = [permutation[j], permutation[i]];
|
|
52
|
+
}
|
|
53
|
+
for (let i = 0; i < 256; i++) {
|
|
54
|
+
permutation[256 + i] = permutation[i];
|
|
55
|
+
}
|
|
56
|
+
const fade = (t) => t * t * t * (t * (t * 6 - 15) + 10);
|
|
57
|
+
const lerp = (a, b, t) => a + t * (b - a);
|
|
58
|
+
const grad = (hash, x, y, z) => {
|
|
59
|
+
const h = hash & 15;
|
|
60
|
+
const u = h < 8 ? x : y;
|
|
61
|
+
const v = h < 4 ? y : h === 12 || h === 14 ? x : z;
|
|
62
|
+
return ((h & 1) === 0 ? u : -u) + ((h & 2) === 0 ? v : -v);
|
|
63
|
+
};
|
|
64
|
+
return (x, y = 0, z = 0) => {
|
|
65
|
+
const X = Math.floor(x) & 255;
|
|
66
|
+
const Y = Math.floor(y) & 255;
|
|
67
|
+
const Z = Math.floor(z) & 255;
|
|
68
|
+
x -= Math.floor(x);
|
|
69
|
+
y -= Math.floor(y);
|
|
70
|
+
z -= Math.floor(z);
|
|
71
|
+
const u = fade(x);
|
|
72
|
+
const v = fade(y);
|
|
73
|
+
const w = fade(z);
|
|
74
|
+
const A = permutation[X] + Y;
|
|
75
|
+
const AA = permutation[A] + Z;
|
|
76
|
+
const AB = permutation[A + 1] + Z;
|
|
77
|
+
const B = permutation[X + 1] + Y;
|
|
78
|
+
const BA = permutation[B] + Z;
|
|
79
|
+
const BB = permutation[B + 1] + Z;
|
|
80
|
+
return (lerp(lerp(lerp(grad(permutation[AA], x, y, z), grad(permutation[BA], x - 1, y, z), u), lerp(grad(permutation[AB], x, y - 1, z), grad(permutation[BB], x - 1, y - 1, z), u), v), lerp(lerp(grad(permutation[AA + 1], x, y, z - 1), grad(permutation[BA + 1], x - 1, y, z - 1), u), lerp(grad(permutation[AB + 1], x, y - 1, z - 1), grad(permutation[BB + 1], x - 1, y - 1, z - 1), u), v), w) + 1) / 2;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function stableStringify(obj) {
|
|
84
|
+
if (obj === null)
|
|
85
|
+
return 'null';
|
|
86
|
+
if (obj === undefined)
|
|
87
|
+
return 'undefined';
|
|
88
|
+
if (typeof obj !== 'object')
|
|
89
|
+
return JSON.stringify(obj);
|
|
90
|
+
if (Array.isArray(obj)) {
|
|
91
|
+
return '[' + obj.map(stableStringify).join(',') + ']';
|
|
92
|
+
}
|
|
93
|
+
const keys = Object.keys(obj).sort();
|
|
94
|
+
const pairs = keys.map(k => `${JSON.stringify(k)}:${stableStringify(obj[k])}`);
|
|
95
|
+
return '{' + pairs.join(',') + '}';
|
|
96
|
+
}
|
|
97
|
+
function fnv1aHash(str) {
|
|
98
|
+
let hash = 0x811c9dc5;
|
|
99
|
+
for (let i = 0; i < str.length; i++) {
|
|
100
|
+
hash ^= str.charCodeAt(i);
|
|
101
|
+
hash = Math.imul(hash, 0x01000193);
|
|
102
|
+
}
|
|
103
|
+
const h1 = (hash >>> 0).toString(16).padStart(8, '0');
|
|
104
|
+
let hash2 = 0x811c9dc5;
|
|
105
|
+
for (let i = str.length - 1; i >= 0; i--) {
|
|
106
|
+
hash2 ^= str.charCodeAt(i);
|
|
107
|
+
hash2 = Math.imul(hash2, 0x01000193);
|
|
108
|
+
}
|
|
109
|
+
const h2 = (hash2 >>> 0).toString(16).padStart(8, '0');
|
|
110
|
+
return h1 + h2;
|
|
111
|
+
}
|
|
112
|
+
export function createRuntime(options) {
|
|
113
|
+
const numericSeed = hashSeed(options.seed);
|
|
114
|
+
const vars = options.vars ?? [];
|
|
115
|
+
const mode = options.mode ?? 'static';
|
|
116
|
+
const strict = options.strict ?? false;
|
|
117
|
+
const metadata = options.metadata;
|
|
118
|
+
if (vars.length > 10) {
|
|
119
|
+
throw new Error(`[NexArt Runtime] vars array must have 0-10 elements, got ${vars.length}`);
|
|
120
|
+
}
|
|
121
|
+
for (let i = 0; i < vars.length; i++) {
|
|
122
|
+
if (typeof vars[i] !== 'number' || !Number.isFinite(vars[i])) {
|
|
123
|
+
throw new Error(`[NexArt Runtime] vars[${i}] must be a finite number`);
|
|
124
|
+
}
|
|
125
|
+
if (vars[i] < 0 || vars[i] > 100) {
|
|
126
|
+
throw new Error(`[NexArt Runtime] vars[${i}] must be in range 0-100, got ${vars[i]}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const paddedVars = [...vars];
|
|
130
|
+
while (paddedVars.length < 10) {
|
|
131
|
+
paddedVars.push(0);
|
|
132
|
+
}
|
|
133
|
+
const rng = createSeededRNG(numericSeed);
|
|
134
|
+
const noiseFunc = createSeededNoise(numericSeed);
|
|
135
|
+
const state = {
|
|
136
|
+
sdkVersion: RUNTIME_VERSION,
|
|
137
|
+
seed: numericSeed,
|
|
138
|
+
vars: paddedVars,
|
|
139
|
+
mode,
|
|
140
|
+
...(metadata !== undefined && { metadata }),
|
|
141
|
+
};
|
|
142
|
+
function random() {
|
|
143
|
+
return rng();
|
|
144
|
+
}
|
|
145
|
+
function randomInt(min, max) {
|
|
146
|
+
const range = max - min + 1;
|
|
147
|
+
return Math.floor(rng() * range) + min;
|
|
148
|
+
}
|
|
149
|
+
function randomRange(min, max) {
|
|
150
|
+
return rng() * (max - min) + min;
|
|
151
|
+
}
|
|
152
|
+
function noise(x, y, z) {
|
|
153
|
+
return noiseFunc(x, y ?? 0, z ?? 0);
|
|
154
|
+
}
|
|
155
|
+
function run(fn) {
|
|
156
|
+
if (!strict) {
|
|
157
|
+
return fn();
|
|
158
|
+
}
|
|
159
|
+
const originalMathRandom = Math.random;
|
|
160
|
+
const originalDateNow = Date.now;
|
|
161
|
+
const hasPerformance = typeof performance !== 'undefined' && performance !== null;
|
|
162
|
+
const originalPerformanceNow = hasPerformance ? performance.now : undefined;
|
|
163
|
+
const throwMathRandom = () => {
|
|
164
|
+
throw new Error('NEXART_STRICT: Non-deterministic API used: Math.random. Use runtime.random() instead.');
|
|
165
|
+
};
|
|
166
|
+
const throwDateNow = () => {
|
|
167
|
+
throw new Error('NEXART_STRICT: Non-deterministic API used: Date.now. Pass time as an input or use deterministic counters.');
|
|
168
|
+
};
|
|
169
|
+
const throwPerformanceNow = () => {
|
|
170
|
+
throw new Error('NEXART_STRICT: Non-deterministic API used: performance.now.');
|
|
171
|
+
};
|
|
172
|
+
try {
|
|
173
|
+
Math.random = throwMathRandom;
|
|
174
|
+
Date.now = throwDateNow;
|
|
175
|
+
if (hasPerformance && originalPerformanceNow) {
|
|
176
|
+
performance.now = throwPerformanceNow;
|
|
177
|
+
}
|
|
178
|
+
return fn();
|
|
179
|
+
}
|
|
180
|
+
finally {
|
|
181
|
+
Math.random = originalMathRandom;
|
|
182
|
+
Date.now = originalDateNow;
|
|
183
|
+
if (hasPerformance && originalPerformanceNow) {
|
|
184
|
+
performance.now = originalPerformanceNow;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function digest() {
|
|
189
|
+
const input = stableStringify({
|
|
190
|
+
sdkVersion: RUNTIME_VERSION,
|
|
191
|
+
seed: numericSeed,
|
|
192
|
+
vars: paddedVars,
|
|
193
|
+
mode,
|
|
194
|
+
...(metadata !== undefined && { metadata }),
|
|
195
|
+
});
|
|
196
|
+
return fnv1aHash(input);
|
|
197
|
+
}
|
|
198
|
+
function getState() {
|
|
199
|
+
return { ...state, vars: [...state.vars] };
|
|
200
|
+
}
|
|
201
|
+
function getSeed() {
|
|
202
|
+
return numericSeed;
|
|
203
|
+
}
|
|
204
|
+
return Object.freeze({
|
|
205
|
+
random,
|
|
206
|
+
randomInt,
|
|
207
|
+
randomRange,
|
|
208
|
+
noise,
|
|
209
|
+
run,
|
|
210
|
+
digest,
|
|
211
|
+
getState,
|
|
212
|
+
getSeed,
|
|
213
|
+
strict,
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
export const NexArtRuntime = {
|
|
217
|
+
create: createRuntime,
|
|
218
|
+
VERSION: RUNTIME_VERSION,
|
|
219
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @nexart/codemode-sdk — Basic Example
|
|
3
|
+
*
|
|
4
|
+
* Demonstrates the agent-first runtime API for deterministic execution.
|
|
5
|
+
*
|
|
6
|
+
* Run: npm run example:basic
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { createRuntime } from '../runtime';
|
|
10
|
+
|
|
11
|
+
console.log('╔══════════════════════════════════════════════════════════════╗');
|
|
12
|
+
console.log('║ NexArt Code Mode SDK — Basic Example ║');
|
|
13
|
+
console.log('╚══════════════════════════════════════════════════════════════╝');
|
|
14
|
+
console.log();
|
|
15
|
+
|
|
16
|
+
const runtime = createRuntime({
|
|
17
|
+
seed: 'demo-seed-42',
|
|
18
|
+
vars: [50, 75, 25],
|
|
19
|
+
strict: true,
|
|
20
|
+
mode: 'static',
|
|
21
|
+
metadata: { artist: 'demo', project: 'example' }
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
console.log('Runtime State:');
|
|
25
|
+
console.log(JSON.stringify(runtime.getState(), null, 2));
|
|
26
|
+
console.log();
|
|
27
|
+
|
|
28
|
+
console.log('Digest:', runtime.digest());
|
|
29
|
+
console.log('Seed (numeric):', runtime.getSeed());
|
|
30
|
+
console.log('Strict mode:', runtime.strict);
|
|
31
|
+
console.log();
|
|
32
|
+
|
|
33
|
+
console.log('Deterministic random values:');
|
|
34
|
+
const randoms: number[] = [];
|
|
35
|
+
for (let i = 0; i < 5; i++) {
|
|
36
|
+
const val = runtime.random();
|
|
37
|
+
randoms.push(val);
|
|
38
|
+
console.log(` random() #${i + 1}: ${val.toFixed(8)}`);
|
|
39
|
+
}
|
|
40
|
+
console.log();
|
|
41
|
+
|
|
42
|
+
console.log('Deterministic noise values:');
|
|
43
|
+
for (let i = 0; i < 5; i++) {
|
|
44
|
+
const x = i * 0.5;
|
|
45
|
+
const val = runtime.noise(x, 0.5);
|
|
46
|
+
console.log(` noise(${x}, 0.5): ${val.toFixed(8)}`);
|
|
47
|
+
}
|
|
48
|
+
console.log();
|
|
49
|
+
|
|
50
|
+
console.log('Running code in strict mode...');
|
|
51
|
+
const result = runtime.run(() => {
|
|
52
|
+
let sum = 0;
|
|
53
|
+
for (let i = 0; i < 10; i++) {
|
|
54
|
+
sum += runtime.random();
|
|
55
|
+
}
|
|
56
|
+
return sum;
|
|
57
|
+
});
|
|
58
|
+
console.log(` Sum of 10 random values: ${result.toFixed(8)}`);
|
|
59
|
+
console.log();
|
|
60
|
+
|
|
61
|
+
console.log('✅ Basic example completed successfully!');
|