@womp/spark-agent 1.0.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -19
- package/dist/capabilities/health.d.ts +9 -0
- package/dist/capabilities/health.js +27 -0
- package/dist/capabilities/scene-draft.d.ts +2 -404
- package/dist/capabilities/scene.d.ts +1 -1
- package/dist/capabilities/scene.js +13 -4
- package/dist/capabilities.d.ts +3 -1
- package/dist/capabilities.js +1 -0
- package/dist/cli.js +20 -2
- package/dist/client.js +15 -15
- package/dist/coordinates.d.ts +23 -0
- package/dist/coordinates.js +132 -0
- package/dist/generated/health-declarations.d.ts +1 -0
- package/dist/generated/health-declarations.js +3 -0
- package/dist/generated/scene-declarations.d.ts +1 -1
- package/dist/generated/scene-declarations.js +1 -1
- package/dist/geometry/backendRegistry.d.ts +37 -0
- package/dist/geometry/backendRegistry.js +61 -0
- package/dist/geometry/common.d.ts +26 -0
- package/dist/geometry/common.js +78 -0
- package/dist/geometry/compiler.d.ts +35 -0
- package/dist/geometry/compiler.js +115 -0
- package/dist/geometry/graph.d.ts +39 -0
- package/dist/geometry/graph.js +32 -0
- package/dist/geometry/index.d.ts +9 -0
- package/dist/geometry/index.js +12 -0
- package/dist/geometry/lift.d.ts +7 -0
- package/dist/geometry/lift.js +376 -0
- package/dist/geometry/lower.d.ts +9 -0
- package/dist/geometry/lower.js +363 -0
- package/dist/geometry/parameters.d.ts +23 -0
- package/dist/geometry/parameters.js +46 -0
- package/dist/geometry/schemas/backend-schema.json +3394 -0
- package/dist/geometry/schemas/flowir-dialect.json +976 -0
- package/dist/geometry/writeParamData.d.ts +18 -0
- package/dist/geometry/writeParamData.js +47 -0
- package/dist/geometry.d.ts +10 -0
- package/dist/geometry.js +19 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/kakapo.d.ts +9 -5
- package/dist/kakapo.js +7 -6
- package/dist/openscad.d.ts +85 -0
- package/dist/openscad.js +223 -0
- package/dist/pricing.js +9 -29
- package/dist/repair-state.d.ts +50 -0
- package/dist/repair-state.js +96 -0
- package/dist/runtime.d.ts +16 -36
- package/dist/runtime.js +132 -139
- package/dist/scene-environment.d.ts +25 -0
- package/dist/scene-environment.js +76 -0
- package/dist/scene-health.d.ts +49 -0
- package/dist/scene-health.js +176 -0
- package/dist/scene-metadata.d.ts +6 -0
- package/dist/scene-metadata.js +65 -0
- package/dist/scene-operations.d.ts +47 -0
- package/dist/scene-operations.js +126 -0
- package/dist/sdk-geometry-host.d.ts +12 -0
- package/dist/sdk-geometry-host.js +306 -0
- package/dist/sdk-openscad-host.d.ts +14 -0
- package/dist/sdk-openscad-host.js +296 -0
- package/dist/sdk-openscad-renderer.d.ts +11 -0
- package/dist/sdk-openscad-renderer.js +71 -0
- package/dist/sdk-scene-health-host.d.ts +4 -0
- package/dist/sdk-scene-health-host.js +13 -0
- package/dist/sdk-scene-host.d.ts +312 -0
- package/dist/sdk-scene-host.js +202 -0
- package/dist/sdk-scene-screenshot.d.ts +7 -0
- package/dist/sdk-scene-screenshot.js +17 -0
- package/dist/server/demo-scene-materials.d.ts +3 -0
- package/dist/server/demo-scene-materials.js +18 -0
- package/dist/server/express.js +2 -4
- package/dist/server/fixtures/materials/matte-blue.json +14 -0
- package/dist/server/fixtures/materials/polished-copper.json +14 -0
- package/dist/server/index.d.ts +2 -1
- package/dist/server/index.js +2 -1
- package/dist/server/service.d.ts +4 -0
- package/dist/server/service.js +26 -2
- package/dist/server/sqlite-store.d.ts +31 -0
- package/dist/server/sqlite-store.js +224 -0
- package/dist/server/standalone.d.ts +14 -1
- package/dist/server/standalone.js +160 -27
- package/dist/server/store.js +3 -1
- package/dist/types.d.ts +1 -1
- package/dist/womp-scene-api.d.ts +283 -0
- package/dist/womp-scene-api.js +974 -0
- package/package.json +22 -10
- package/dist/kakapo-capability.d.ts +0 -45
- package/dist/kakapo-capability.js +0 -169
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { hasGeometryOverflow, MAX_GEOMETRY_REPAIR_ATTEMPTS, } from "./scene-health.js";
|
|
2
|
+
export function emptyRepairState() {
|
|
3
|
+
return { schemaVersion: 1, revision: 0, targets: {} };
|
|
4
|
+
}
|
|
5
|
+
export function createMemoryRepairStateStore(initial = emptyRepairState()) {
|
|
6
|
+
let state = structuredClone(initial);
|
|
7
|
+
return {
|
|
8
|
+
async read() {
|
|
9
|
+
return structuredClone(state);
|
|
10
|
+
},
|
|
11
|
+
async compareAndSwap(expectedRevision, next) {
|
|
12
|
+
if (state.revision !== expectedRevision)
|
|
13
|
+
return false;
|
|
14
|
+
state = structuredClone({ ...next, revision: expectedRevision + 1 });
|
|
15
|
+
return true;
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
async function update(store, change) {
|
|
20
|
+
for (;;) {
|
|
21
|
+
const state = await store.read();
|
|
22
|
+
const next = structuredClone(state);
|
|
23
|
+
change(next);
|
|
24
|
+
if (await store.compareAndSwap(state.revision, next))
|
|
25
|
+
return { ...next, revision: state.revision + 1 };
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function createGeometryRepairTracker(store, namespace) {
|
|
29
|
+
return createRepairTracker(store, namespace, MAX_GEOMETRY_REPAIR_ATTEMPTS, (health) => health.status === "unverified" ? undefined : hasGeometryOverflow(health));
|
|
30
|
+
}
|
|
31
|
+
export function createRepairTracker(store, namespace, limit, failed) {
|
|
32
|
+
const key = (target) => `${namespace}:${target}`;
|
|
33
|
+
return {
|
|
34
|
+
async isPending(target) {
|
|
35
|
+
return (await store.read()).targets[key(target)]?.failed ?? false;
|
|
36
|
+
},
|
|
37
|
+
async assertCanWrite(target) {
|
|
38
|
+
const current = (await store.read()).targets[key(target)];
|
|
39
|
+
if (current?.failed && current.attempts >= limit)
|
|
40
|
+
throw new Error(`The ${limit} repairs for this target have been used. Report the remaining issue; do not recreate it or restart the repair loop.`);
|
|
41
|
+
},
|
|
42
|
+
async reserve(target, operationId) {
|
|
43
|
+
await update(store, (state) => {
|
|
44
|
+
const current = state.targets[key(target)] ?? {
|
|
45
|
+
attempts: 0,
|
|
46
|
+
failed: false,
|
|
47
|
+
reservations: {},
|
|
48
|
+
};
|
|
49
|
+
if (current.reservations[operationId] !== undefined)
|
|
50
|
+
return;
|
|
51
|
+
current.reservations[operationId] = { operationId, status: "reserved" };
|
|
52
|
+
if (current.failed)
|
|
53
|
+
current.attempts += 1;
|
|
54
|
+
state.targets[key(target)] = current;
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
async record(target, operationId, result) {
|
|
58
|
+
const state = await update(store, (snapshot) => {
|
|
59
|
+
const current = snapshot.targets[key(target)] ?? {
|
|
60
|
+
attempts: 0,
|
|
61
|
+
failed: false,
|
|
62
|
+
reservations: {},
|
|
63
|
+
};
|
|
64
|
+
const reservation = current.reservations[operationId];
|
|
65
|
+
if (reservation !== undefined)
|
|
66
|
+
reservation.status = "committed";
|
|
67
|
+
const failure = failed(result);
|
|
68
|
+
if (failure !== undefined) {
|
|
69
|
+
current.failed = failure;
|
|
70
|
+
current.lastVerifiedFailure = failure ? "failed" : undefined;
|
|
71
|
+
}
|
|
72
|
+
snapshot.targets[key(target)] = current;
|
|
73
|
+
});
|
|
74
|
+
return {
|
|
75
|
+
result,
|
|
76
|
+
repairAttempts: state.targets[key(target)]?.attempts ?? 0,
|
|
77
|
+
repairLimit: limit,
|
|
78
|
+
};
|
|
79
|
+
},
|
|
80
|
+
async refund(target, operationId) {
|
|
81
|
+
const state = await update(store, (state) => {
|
|
82
|
+
const current = state.targets[key(target)];
|
|
83
|
+
const reservation = current?.reservations[operationId];
|
|
84
|
+
if (current === undefined || reservation?.status !== "reserved")
|
|
85
|
+
return;
|
|
86
|
+
reservation.status = "refunded";
|
|
87
|
+
if (current.failed && current.attempts > 0)
|
|
88
|
+
current.attempts -= 1;
|
|
89
|
+
});
|
|
90
|
+
return {
|
|
91
|
+
repairAttempts: state.targets[key(target)]?.attempts ?? 0,
|
|
92
|
+
repairLimit: limit,
|
|
93
|
+
};
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
}
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,43 +1,17 @@
|
|
|
1
|
-
import { KakapoAPI, type
|
|
2
|
-
import type { ScreenshotView } from "@womp/kakapo-sdk";
|
|
3
|
-
import type { SceneStore, SceneTransaction } from "@womp/scene-api";
|
|
4
|
-
import { type KakapoEngine } from "@womp/scene-api/adapters/kakapo";
|
|
1
|
+
import { KakapoAPI, type ScreenshotView } from "@womp/kakapo-sdk";
|
|
5
2
|
import type { JavaScriptExecutor } from "./executor.js";
|
|
6
3
|
import type { ExecutionResult, ExecutionScreenshot, SparkRuntimeEventSink, SparkRuntime } from "./types.js";
|
|
7
|
-
import type
|
|
8
|
-
|
|
9
|
-
* The `@womp/scene-api` seams over one Kakapo engine session, built once and
|
|
10
|
-
* reused for every read and every transaction.
|
|
11
|
-
*
|
|
12
|
-
* WHY `editScene` IS OPENED AND IMMEDIATELY CLOSED. `createKakapoSceneStore`
|
|
13
|
-
* needs a `SceneEdit`, and the only way to obtain one from a Kakapo engine is
|
|
14
|
-
* inside `editScene()`'s callback. The handle stays usable for every later
|
|
15
|
-
* transaction because its closures re-read the engine's current scene at call
|
|
16
|
-
* time rather than capturing a document eagerly -- which is also why `scene()`
|
|
17
|
-
* below is a fresh read each call and not a cached snapshot. Same construction
|
|
18
|
-
* as `@womp/spark-agent-host`'s `KakapoSparkRuntime.connect()`.
|
|
19
|
-
*/
|
|
20
|
-
export type KakapoSceneSeams = {
|
|
21
|
-
store: SceneStore;
|
|
22
|
-
transaction: SceneTransaction;
|
|
23
|
-
/** The engine's CURRENT scene document, for `kakapoSceneRevision`. */
|
|
24
|
-
scene(): Scene;
|
|
25
|
-
};
|
|
26
|
-
export declare function composeKakapoSceneSeams(engine: KakapoEngine, fonts?: FontFamily[]): Promise<KakapoSceneSeams>;
|
|
27
|
-
/**
|
|
28
|
-
* `createSceneApi` returns `Record<string, unknown>` (its `SceneScriptApi`), so
|
|
29
|
-
* naming it as the draft the capability declares takes one cast. It is not a
|
|
30
|
-
* blind one: `test/kakapo-capability.test.ts`'s "the generated skill documents
|
|
31
|
-
* the exact executable draft surface" asserts set equality between
|
|
32
|
-
* `WompSceneDraft`'s members and the functions this object actually exposes, in
|
|
33
|
-
* both directions, so a drift on either side fails a test rather than surfacing
|
|
34
|
-
* as a `TypeError` inside a model-authored script.
|
|
35
|
-
*/
|
|
36
|
-
export declare function asSceneDraft(api: Record<string, unknown>): WompSceneDraft;
|
|
4
|
+
import { type WompSceneApi } from "./womp-scene-api.js";
|
|
5
|
+
import type { SparkInvocationContext } from "./capabilities.js";
|
|
37
6
|
export declare class KakapoRuntime implements SparkRuntime {
|
|
38
7
|
readonly api: KakapoAPI;
|
|
8
|
+
rootId: number;
|
|
9
|
+
defaultMaterialId: number | undefined;
|
|
39
10
|
private calls;
|
|
40
|
-
private
|
|
11
|
+
private readonly materialIds;
|
|
12
|
+
private readonly createdIds;
|
|
13
|
+
private readonly editOrdinals;
|
|
14
|
+
private fonts;
|
|
41
15
|
private readonly executor;
|
|
42
16
|
private eventSink;
|
|
43
17
|
constructor(url: string);
|
|
@@ -49,8 +23,14 @@ export declare class KakapoRuntime implements SparkRuntime {
|
|
|
49
23
|
connect(): Promise<void>;
|
|
50
24
|
disconnect(): void;
|
|
51
25
|
private sdkCall;
|
|
52
|
-
private
|
|
26
|
+
private sceneCall;
|
|
27
|
+
private requireNode;
|
|
53
28
|
private sceneApi;
|
|
29
|
+
inspect(): Promise<{
|
|
30
|
+
revision: string;
|
|
31
|
+
scene: import("@womp/kakapo-sdk").Scene;
|
|
32
|
+
}>;
|
|
33
|
+
edit<T>(callback: (draft: WompSceneApi) => Promise<T> | T, expectedRevision: string, context: SparkInvocationContext): Promise<T>;
|
|
54
34
|
screenshot(input: {
|
|
55
35
|
targetId?: number;
|
|
56
36
|
view?: ScreenshotView;
|
package/dist/runtime.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { KakapoAPI } from "@womp/kakapo-sdk";
|
|
2
|
-
import { createSceneApi } from "@womp/scene-api";
|
|
3
|
-
import { createKakapoSceneStore, createKakapoTransaction, } from "@womp/scene-api/adapters/kakapo";
|
|
1
|
+
import { KakapoAPI, KakapoSceneCommitError } from "@womp/kakapo-sdk";
|
|
4
2
|
import { QuickJSJavaScriptExecutor } from "./quickjs-executor.js";
|
|
3
|
+
import { createWompSceneApi } from "./womp-scene-api.js";
|
|
4
|
+
import { writeMaterialMetadata } from "./scene-metadata.js";
|
|
5
|
+
import { captureSdkSceneScreenshot } from "./sdk-scene-screenshot.js";
|
|
5
6
|
function safeValue(value) {
|
|
6
7
|
if (value instanceof Uint8Array)
|
|
7
8
|
return { bytes: value.byteLength };
|
|
@@ -14,73 +15,15 @@ function safeValue(value) {
|
|
|
14
15
|
return String(value);
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
|
-
/**
|
|
18
|
-
* Kakapo has no library-material concept, so `pending` stays absent -- the
|
|
19
|
-
* "backing with no queue" case `@womp/scene-api`'s `MaterialCatalog` documents.
|
|
20
|
-
* `nodeSetMaterialId` with a NAME still fails loudly rather than silently
|
|
21
|
-
* dropping the assignment.
|
|
22
|
-
*/
|
|
23
|
-
const KAKAPO_MATERIAL_CATALOG = {
|
|
24
|
-
listSuperMaterialNames: () => [],
|
|
25
|
-
};
|
|
26
|
-
export async function composeKakapoSceneSeams(engine, fonts) {
|
|
27
|
-
// Prefetched because `listFonts()` is an async RPC while the seam's
|
|
28
|
-
// `listFontFamilies` is synchronous.
|
|
29
|
-
const resolved = fonts ?? (await engine.listFonts());
|
|
30
|
-
let captured;
|
|
31
|
-
await engine.editScene((scene) => {
|
|
32
|
-
captured = scene;
|
|
33
|
-
});
|
|
34
|
-
if (captured === undefined) {
|
|
35
|
-
throw new Error("engine.editScene() resolved without invoking its callback.");
|
|
36
|
-
}
|
|
37
|
-
const edit = captured;
|
|
38
|
-
return {
|
|
39
|
-
store: createKakapoSceneStore(edit, engine, { fonts: resolved }),
|
|
40
|
-
transaction: createKakapoTransaction(engine),
|
|
41
|
-
scene: () => edit.getScene(),
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* `createSceneApi` returns `Record<string, unknown>` (its `SceneScriptApi`), so
|
|
46
|
-
* naming it as the draft the capability declares takes one cast. It is not a
|
|
47
|
-
* blind one: `test/kakapo-capability.test.ts`'s "the generated skill documents
|
|
48
|
-
* the exact executable draft surface" asserts set equality between
|
|
49
|
-
* `WompSceneDraft`'s members and the functions this object actually exposes, in
|
|
50
|
-
* both directions, so a drift on either side fails a test rather than surfacing
|
|
51
|
-
* as a `TypeError` inside a model-authored script.
|
|
52
|
-
*/
|
|
53
|
-
export function asSceneDraft(api) {
|
|
54
|
-
return api;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Records every WRITE method invoked on `store`, in call order, once the call
|
|
58
|
-
* has succeeded -- the `sdkCalls` the old per-call `sceneCall` wrapper
|
|
59
|
-
* reported. Read methods (`get*`/`list*`/`child*`) were never routed through it
|
|
60
|
-
* either, and are excluded so `sdkCalls` says what the script CHANGED rather
|
|
61
|
-
* than what it merely inspected. Ported from `@womp/spark-agent-host`.
|
|
62
|
-
*/
|
|
63
|
-
function trackedStore(store, calls) {
|
|
64
|
-
const readPrefixes = ["get", "list", "child"];
|
|
65
|
-
return new Proxy(store, {
|
|
66
|
-
get(target, property, receiver) {
|
|
67
|
-
const value = Reflect.get(target, property, receiver);
|
|
68
|
-
if (typeof value !== "function" || typeof property !== "string")
|
|
69
|
-
return value;
|
|
70
|
-
if (readPrefixes.some((prefix) => property.startsWith(prefix)))
|
|
71
|
-
return value.bind(target);
|
|
72
|
-
return (...args) => {
|
|
73
|
-
const result = value.apply(target, args);
|
|
74
|
-
calls.push(property);
|
|
75
|
-
return result;
|
|
76
|
-
};
|
|
77
|
-
},
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
18
|
export class KakapoRuntime {
|
|
81
19
|
api;
|
|
20
|
+
rootId = 0;
|
|
21
|
+
defaultMaterialId;
|
|
82
22
|
calls = [];
|
|
83
|
-
|
|
23
|
+
materialIds = new Set();
|
|
24
|
+
createdIds = new Set();
|
|
25
|
+
editOrdinals = new WeakMap();
|
|
26
|
+
fonts = [];
|
|
84
27
|
executor;
|
|
85
28
|
eventSink;
|
|
86
29
|
constructor(url, executorOrEventSink, eventSink) {
|
|
@@ -104,21 +47,15 @@ export class KakapoRuntime {
|
|
|
104
47
|
}
|
|
105
48
|
async connect() {
|
|
106
49
|
await this.sdkCall("connect", [], () => this.api.connect());
|
|
107
|
-
|
|
108
|
-
this.
|
|
50
|
+
this.fonts = await this.sdkCall("listFonts", [], () => this.api.listFonts());
|
|
51
|
+
this.rootId = 0;
|
|
109
52
|
}
|
|
110
53
|
disconnect() {
|
|
111
54
|
this.api.disconnect();
|
|
112
55
|
}
|
|
113
56
|
async sdkCall(method, args, action) {
|
|
114
57
|
const callId = crypto.randomUUID();
|
|
115
|
-
this.emit({
|
|
116
|
-
type: "sdk-call",
|
|
117
|
-
callId,
|
|
118
|
-
method,
|
|
119
|
-
status: "running",
|
|
120
|
-
args: safeValue(args),
|
|
121
|
-
});
|
|
58
|
+
this.emit({ type: "sdk-call", callId, method, status: "running", args: safeValue(args) });
|
|
122
59
|
try {
|
|
123
60
|
const value = await action();
|
|
124
61
|
this.calls.push(method);
|
|
@@ -142,43 +79,103 @@ export class KakapoRuntime {
|
|
|
142
79
|
throw error;
|
|
143
80
|
}
|
|
144
81
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
82
|
+
sceneCall(method, args, action) {
|
|
83
|
+
const callId = crypto.randomUUID();
|
|
84
|
+
this.emit({ type: "sdk-call", callId, method, status: "running", args: safeValue(args) });
|
|
85
|
+
try {
|
|
86
|
+
const value = action();
|
|
87
|
+
this.calls.push(method);
|
|
88
|
+
this.emit({ type: "sdk-call", callId, method, status: "complete", result: safeValue(value) });
|
|
89
|
+
return value;
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
this.emit({
|
|
93
|
+
type: "sdk-call",
|
|
94
|
+
callId,
|
|
95
|
+
method,
|
|
96
|
+
status: "error",
|
|
97
|
+
error: error instanceof Error ? error.message : String(error),
|
|
98
|
+
});
|
|
99
|
+
throw error;
|
|
148
100
|
}
|
|
149
|
-
return this.seams;
|
|
150
101
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
102
|
+
requireNode(id) {
|
|
103
|
+
this.api.getNode(id);
|
|
104
|
+
}
|
|
105
|
+
sceneApi(scene, print) {
|
|
106
|
+
return createWompSceneApi({
|
|
107
|
+
scene,
|
|
108
|
+
engine: this.api,
|
|
109
|
+
rootId: this.rootId,
|
|
110
|
+
getDefaultMaterialId: () => {
|
|
111
|
+
if (this.defaultMaterialId !== undefined)
|
|
112
|
+
return this.defaultMaterialId;
|
|
113
|
+
const material = this.sceneCall("createMaterial", [], () => scene.createMaterial({
|
|
114
|
+
color: { x: 189 / 255, y: 189 / 255, z: 189 / 255 },
|
|
115
|
+
roughness: 0.22,
|
|
116
|
+
}));
|
|
117
|
+
this.defaultMaterialId = material.id;
|
|
118
|
+
this.materialIds.add(material.id);
|
|
119
|
+
writeMaterialMetadata(scene, material.id, { name: "Default Material", tier: 0 });
|
|
120
|
+
return material.id;
|
|
121
|
+
},
|
|
122
|
+
fonts: this.fonts,
|
|
123
|
+
requireNode: (id) => this.requireNode(id),
|
|
124
|
+
trackNode: (id) => this.createdIds.add(id),
|
|
125
|
+
trackMaterial: (id) => this.materialIds.add(id),
|
|
126
|
+
untrackMaterial: (id) => {
|
|
127
|
+
this.materialIds.delete(id);
|
|
128
|
+
},
|
|
129
|
+
listMaterialIds: () => this.api.listMaterials().map(({ id }) => id),
|
|
130
|
+
requireMaterial: (id) => {
|
|
131
|
+
this.api.getMaterial(id);
|
|
132
|
+
},
|
|
133
|
+
call: (method, args, action) => this.sceneCall(method, args, action),
|
|
134
|
+
callAsync: (method, args, action) => this.sdkCall(method, args, action),
|
|
156
135
|
print,
|
|
157
136
|
});
|
|
158
137
|
}
|
|
159
|
-
async
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
data: image.data,
|
|
172
|
-
mediaType: image.mediaType,
|
|
173
|
-
width: image.width,
|
|
174
|
-
height: image.height,
|
|
175
|
-
view,
|
|
176
|
-
targetId: image.targetId,
|
|
138
|
+
async inspect() {
|
|
139
|
+
await this.api.refreshScene();
|
|
140
|
+
return { revision: await this.api.getSceneRevision(), scene: this.api.getScene() };
|
|
141
|
+
}
|
|
142
|
+
async edit(callback, expectedRevision, context) {
|
|
143
|
+
let changed = false;
|
|
144
|
+
const ordinal = this.editOrdinals.get(context) ?? 0;
|
|
145
|
+
this.editOrdinals.set(context, ordinal + 1);
|
|
146
|
+
const receipt = {
|
|
147
|
+
id: `${context.runId}:${context.toolCallId}:scene.edit:${ordinal}`,
|
|
148
|
+
capability: "scene",
|
|
149
|
+
operation: "edit",
|
|
177
150
|
};
|
|
178
|
-
|
|
151
|
+
let value;
|
|
152
|
+
try {
|
|
153
|
+
value = await this.api.editScene(async (scene) => {
|
|
154
|
+
context.signal.throwIfAborted();
|
|
155
|
+
if ((await this.api.getSceneRevision()) !== expectedRevision) {
|
|
156
|
+
throw new Error("Scene changed since inspection. Read the scene again before editing.");
|
|
157
|
+
}
|
|
158
|
+
const result = await callback(this.sceneApi(scene, () => undefined));
|
|
159
|
+
context.signal.throwIfAborted();
|
|
160
|
+
changed = (await this.api.getSceneRevision()) !== expectedRevision;
|
|
161
|
+
return result;
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
await context.recordReceipt({
|
|
166
|
+
...receipt,
|
|
167
|
+
status: error instanceof KakapoSceneCommitError ? error.outcome : "rolled_back",
|
|
168
|
+
});
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
if (changed)
|
|
172
|
+
await context.recordReceipt({ ...receipt, status: "committed" });
|
|
173
|
+
return value;
|
|
174
|
+
}
|
|
175
|
+
async screenshot(input) {
|
|
176
|
+
return this.sdkCall("captureScreenshot", [input], () => captureSdkSceneScreenshot(this.api, input));
|
|
179
177
|
}
|
|
180
178
|
async execute(code, purpose) {
|
|
181
|
-
const seams = this.requireSeams("execute");
|
|
182
179
|
this.calls = [];
|
|
183
180
|
const logs = [];
|
|
184
181
|
this.emit({ type: "javascript", code, purpose });
|
|
@@ -193,15 +190,11 @@ export class KakapoRuntime {
|
|
|
193
190
|
warn: (...values) => consoleFacade.log("WARN", ...values),
|
|
194
191
|
error: (...values) => consoleFacade.log("ERROR", ...values),
|
|
195
192
|
};
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
const failure = await seams.transaction.run(async () => {
|
|
202
|
-
value = await this.executor.execute(code, globals, 60_000);
|
|
203
|
-
});
|
|
204
|
-
if (failure === null) {
|
|
193
|
+
try {
|
|
194
|
+
const value = await this.api.editScene((scene) => {
|
|
195
|
+
const api = this.sceneApi(scene, (...values) => consoleFacade.log(...values));
|
|
196
|
+
return this.executor.execute(code, { ...api, console: consoleFacade }, 60_000);
|
|
197
|
+
});
|
|
205
198
|
const result = {
|
|
206
199
|
ok: true,
|
|
207
200
|
purpose,
|
|
@@ -219,30 +212,30 @@ export class KakapoRuntime {
|
|
|
219
212
|
});
|
|
220
213
|
return result;
|
|
221
214
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
text: `Execution error: ${transactionError}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
215
|
+
catch (error) {
|
|
216
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
217
|
+
const transactionError = `Transaction rolled back; no scene changes were applied. ${message}`;
|
|
218
|
+
this.emit({ type: "console", text: `Execution error: ${transactionError}` });
|
|
219
|
+
const result = {
|
|
220
|
+
ok: false,
|
|
221
|
+
purpose,
|
|
222
|
+
value: null,
|
|
223
|
+
logs,
|
|
224
|
+
sdkCalls: [...this.calls],
|
|
225
|
+
error: transactionError,
|
|
226
|
+
rolledBack: true,
|
|
227
|
+
};
|
|
228
|
+
this.emit({
|
|
229
|
+
type: "execution-result",
|
|
230
|
+
ok: result.ok,
|
|
231
|
+
purpose: result.purpose,
|
|
232
|
+
value: result.value,
|
|
233
|
+
logs: result.logs,
|
|
234
|
+
sdkCalls: result.sdkCalls,
|
|
235
|
+
error: result.error,
|
|
236
|
+
rolledBack: result.rolledBack,
|
|
237
|
+
});
|
|
238
|
+
return result;
|
|
239
|
+
}
|
|
247
240
|
}
|
|
248
241
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { SceneEdit, Vec2, Vec3 } from "@womp/kakapo-sdk";
|
|
2
|
+
export type SceneHdr = {
|
|
3
|
+
id: string;
|
|
4
|
+
name: string;
|
|
5
|
+
defaultExposure: number;
|
|
6
|
+
};
|
|
7
|
+
export declare function createSceneEnvironment(scene: SceneEdit, hdrs: readonly SceneHdr[] | undefined, print: (...values: unknown[]) => void): {
|
|
8
|
+
scenePropertiesRead: () => {
|
|
9
|
+
hdr_name: string | undefined;
|
|
10
|
+
hdr_multiplier: number | undefined;
|
|
11
|
+
hdr_orientation: Vec2 | undefined;
|
|
12
|
+
backdrop_color1: Vec3 | undefined;
|
|
13
|
+
backdrop_color2: Vec3 | undefined;
|
|
14
|
+
backdrop_grad_offset: Vec2 | undefined;
|
|
15
|
+
};
|
|
16
|
+
listHDRs: () => string[];
|
|
17
|
+
setHdrName: (name: string) => void;
|
|
18
|
+
setHdrMultiplier: (multiplier: number) => void;
|
|
19
|
+
setHdrOrientation: (orientation: Vec2) => void;
|
|
20
|
+
setBackgroundColor: (color: {
|
|
21
|
+
r: number;
|
|
22
|
+
g: number;
|
|
23
|
+
b: number;
|
|
24
|
+
}) => void;
|
|
25
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { metadataObject } from "./scene-metadata.js";
|
|
2
|
+
function finite(value, label) {
|
|
3
|
+
if (!Number.isFinite(value))
|
|
4
|
+
throw new Error(`${label} must be a finite number`);
|
|
5
|
+
return value;
|
|
6
|
+
}
|
|
7
|
+
function publicColor(value) {
|
|
8
|
+
const color = metadataObject(value);
|
|
9
|
+
if (typeof color.x !== "number")
|
|
10
|
+
return undefined;
|
|
11
|
+
return {
|
|
12
|
+
x: Math.round(color.x * 255),
|
|
13
|
+
y: Math.round(Number(color.y ?? 0) * 255),
|
|
14
|
+
z: Math.round(Number(color.z ?? 0) * 255),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function vector2(value) {
|
|
18
|
+
const vector = metadataObject(value);
|
|
19
|
+
if (typeof vector.x !== "number" || typeof vector.y !== "number")
|
|
20
|
+
return undefined;
|
|
21
|
+
return { x: vector.x, y: vector.y };
|
|
22
|
+
}
|
|
23
|
+
export function createSceneEnvironment(scene, hdrs, print) {
|
|
24
|
+
const catalog = () => {
|
|
25
|
+
if (!hdrs)
|
|
26
|
+
throw new Error("The HDR catalog is unavailable in this host");
|
|
27
|
+
return hdrs;
|
|
28
|
+
};
|
|
29
|
+
const write = (values) => {
|
|
30
|
+
scene.patchSceneProperties(Object.entries(values).map(([key, value]) => ({
|
|
31
|
+
op: "add",
|
|
32
|
+
path: `/${key}`,
|
|
33
|
+
value,
|
|
34
|
+
})));
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
scenePropertiesRead: () => {
|
|
38
|
+
const properties = scene.getSceneProperties();
|
|
39
|
+
return {
|
|
40
|
+
hdr_name: typeof properties.hdr_name === "string" ? properties.hdr_name : undefined,
|
|
41
|
+
hdr_multiplier: typeof properties.hdr_multiplier === "number"
|
|
42
|
+
? properties.hdr_multiplier * 10 : undefined,
|
|
43
|
+
hdr_orientation: vector2(properties.hdr_orientation),
|
|
44
|
+
backdrop_color1: publicColor(properties.backdrop_color1),
|
|
45
|
+
backdrop_color2: publicColor(properties.backdrop_color2),
|
|
46
|
+
backdrop_grad_offset: vector2(properties.backdrop_grad_offset),
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
listHDRs: () => catalog().map(({ name }) => name),
|
|
50
|
+
setHdrName: (name) => {
|
|
51
|
+
const hdr = catalog().find((entry) => entry.name === name || entry.id === name);
|
|
52
|
+
if (!hdr)
|
|
53
|
+
throw new Error(`Unknown HDR: ${name}. Use listHDRs() to select an available HDR`);
|
|
54
|
+
const exposure = finite(hdr.defaultExposure, "HDR default exposure");
|
|
55
|
+
write({ hdr_name: hdr.id, hdr_multiplier: exposure / 10 });
|
|
56
|
+
print(`Set HDR name to: ${hdr.id}\nApplied default luminance: ${exposure}`);
|
|
57
|
+
},
|
|
58
|
+
setHdrMultiplier: (multiplier) => {
|
|
59
|
+
write({ hdr_multiplier: finite(multiplier, "HDR multiplier") / 10 });
|
|
60
|
+
print(`Set HDR multiplier to: ${multiplier}`);
|
|
61
|
+
},
|
|
62
|
+
setHdrOrientation: (orientation) => {
|
|
63
|
+
const x = finite(orientation?.x, "HDR orientation.x");
|
|
64
|
+
const y = finite(orientation?.y, "HDR orientation.y");
|
|
65
|
+
write({ hdr_orientation: { x, y } });
|
|
66
|
+
print(`Set HDR orientation to: (${x}, ${y})`);
|
|
67
|
+
},
|
|
68
|
+
setBackgroundColor: (color) => {
|
|
69
|
+
const r = finite(color?.r, "Background red");
|
|
70
|
+
const g = finite(color?.g, "Background green");
|
|
71
|
+
const b = finite(color?.b, "Background blue");
|
|
72
|
+
write({ backdrop_color1: { x: r / 255, y: g / 255, z: b / 255 } });
|
|
73
|
+
print(`Set background color to: (${r}, ${g}, ${b})`);
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { KakapoAPI } from "@womp/kakapo-sdk";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
export declare const MAX_GEOMETRY_REPAIR_ATTEMPTS = 2;
|
|
4
|
+
export declare const sceneHealthObjectSchema: z.ZodObject<{
|
|
5
|
+
num_prims: z.ZodNumber;
|
|
6
|
+
flag_prim_overflow: z.ZodNumber;
|
|
7
|
+
flag_depth_overflow: z.ZodNumber;
|
|
8
|
+
flag_vox_overflow: z.ZodNumber;
|
|
9
|
+
voxels: z.ZodNumber;
|
|
10
|
+
}, z.core.$strip>;
|
|
11
|
+
export declare const thermometerSchema: z.ZodObject<{
|
|
12
|
+
objects: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
13
|
+
num_prims: z.ZodNumber;
|
|
14
|
+
flag_prim_overflow: z.ZodNumber;
|
|
15
|
+
flag_depth_overflow: z.ZodNumber;
|
|
16
|
+
flag_vox_overflow: z.ZodNumber;
|
|
17
|
+
voxels: z.ZodNumber;
|
|
18
|
+
}, z.core.$strip>>;
|
|
19
|
+
max_num_prims: z.ZodNumber;
|
|
20
|
+
max_total_depth: z.ZodNumber;
|
|
21
|
+
max_per_object: z.ZodNumber;
|
|
22
|
+
total: z.ZodNumber;
|
|
23
|
+
max_total: z.ZodNumber;
|
|
24
|
+
assets_budget_used: z.ZodNumber;
|
|
25
|
+
assets_budget_total: z.ZodNumber;
|
|
26
|
+
mesh_instances_used: z.ZodOptional<z.ZodNumber>;
|
|
27
|
+
mesh_instances_total: z.ZodOptional<z.ZodNumber>;
|
|
28
|
+
decal_instances_used: z.ZodOptional<z.ZodNumber>;
|
|
29
|
+
decal_instances_total: z.ZodOptional<z.ZodNumber>;
|
|
30
|
+
field_instances_used: z.ZodOptional<z.ZodNumber>;
|
|
31
|
+
field_instances_total: z.ZodOptional<z.ZodNumber>;
|
|
32
|
+
}, z.core.$strip>;
|
|
33
|
+
export type SceneHealth = {
|
|
34
|
+
status: "within_limits" | "overflow" | "unverified";
|
|
35
|
+
freshness: "snapshot" | "post_ack_event" | "unverified";
|
|
36
|
+
revisionVerified: false;
|
|
37
|
+
affectedAreaIds: string[];
|
|
38
|
+
emptyAreaIds?: string[];
|
|
39
|
+
thermometer?: z.infer<typeof thermometerSchema>;
|
|
40
|
+
reason?: string;
|
|
41
|
+
};
|
|
42
|
+
export declare function unverifiedSceneHealth(affectedAreaIds: string[], reason: string): SceneHealth;
|
|
43
|
+
export declare function getSdkSceneAreaId(api: KakapoAPI, objectId: number): string;
|
|
44
|
+
export declare function getSdkSceneAreaIds(api: KakapoAPI): string[];
|
|
45
|
+
export declare function hasGeometryOverflow(health: SceneHealth): boolean;
|
|
46
|
+
export declare function hasSceneResourcePressure(health: SceneHealth): boolean;
|
|
47
|
+
export declare function parseSceneHealth(response: unknown, affectedAreaIds: string[]): SceneHealth;
|
|
48
|
+
export declare function readSdkSceneHealth(api: KakapoAPI, affectedAreaIds: string[], signal?: AbortSignal): Promise<SceneHealth>;
|
|
49
|
+
export declare function readStableSdkSceneHealth(api: KakapoAPI, affectedAreaIds: string[], signal?: AbortSignal): Promise<SceneHealth>;
|