@womp/spark-agent 0.1.0 → 0.3.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/.env.example +4 -0
- package/README.md +303 -49
- package/dist/agent-tools.d.ts +30 -0
- package/dist/agent-tools.js +189 -0
- package/dist/agent.d.ts +34 -7
- package/dist/agent.js +223 -158
- package/dist/browser.d.ts +2 -0
- package/dist/browser.js +2 -0
- package/dist/capabilities/account.d.ts +216 -0
- package/dist/capabilities/account.js +63 -0
- package/dist/capabilities/asset-imports.d.ts +55 -0
- package/dist/capabilities/asset-imports.js +117 -0
- package/dist/capabilities/assets.d.ts +29 -0
- package/dist/capabilities/assets.js +48 -0
- package/dist/capabilities/flow.d.ts +29 -0
- package/dist/capabilities/flow.js +52 -0
- package/dist/capabilities/geometry.d.ts +48 -0
- package/dist/capabilities/geometry.js +51 -0
- package/dist/capabilities/knowledge.d.ts +11 -0
- package/dist/capabilities/knowledge.js +23 -0
- package/dist/capabilities/openscad-instructions.d.ts +1 -0
- package/dist/capabilities/openscad-instructions.js +81 -0
- package/dist/capabilities/openscad.d.ts +54 -0
- package/dist/capabilities/openscad.js +34 -0
- package/dist/capabilities/operations.d.ts +104 -0
- package/dist/capabilities/operations.js +145 -0
- package/dist/capabilities/scene-draft.d.ts +404 -0
- package/dist/capabilities/scene-draft.js +1 -0
- package/dist/capabilities/scene.d.ts +28 -0
- package/dist/capabilities/scene.js +51 -0
- package/dist/capabilities.d.ts +67 -0
- package/dist/capabilities.js +25 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +24 -0
- package/dist/client.d.ts +43 -0
- package/dist/client.js +159 -0
- package/dist/context.d.ts +15 -0
- package/dist/context.js +266 -0
- package/dist/execution-lifetime.d.ts +32 -0
- package/dist/execution-lifetime.js +113 -0
- package/dist/executor.d.ts +6 -1
- package/dist/generated/account-declarations.d.ts +1 -0
- package/dist/generated/account-declarations.js +3 -0
- package/dist/generated/asset-import-declarations.d.ts +1 -0
- package/dist/generated/asset-import-declarations.js +3 -0
- package/dist/generated/assets-declarations.d.ts +1 -0
- package/dist/generated/assets-declarations.js +3 -0
- package/dist/generated/flow-declarations.d.ts +1 -0
- package/dist/generated/flow-declarations.js +3 -0
- package/dist/generated/geometry-declarations.d.ts +1 -0
- package/dist/generated/geometry-declarations.js +3 -0
- package/dist/generated/kakapo-declarations.d.ts +1 -0
- package/dist/generated/kakapo-declarations.js +3 -0
- package/dist/generated/knowledge-declarations.d.ts +1 -0
- package/dist/generated/knowledge-declarations.js +3 -0
- package/dist/generated/openscad-declarations.d.ts +1 -0
- package/dist/generated/openscad-declarations.js +3 -0
- package/dist/generated/operations-declarations.d.ts +1 -0
- package/dist/generated/operations-declarations.js +3 -0
- package/dist/generated/scene-declarations.d.ts +1 -0
- package/dist/generated/scene-declarations.js +3 -0
- package/dist/index.d.ts +7 -6
- package/dist/index.js +5 -3
- package/dist/kakapo-capability-api.d.ts +18 -0
- package/dist/kakapo-capability-api.js +1 -0
- package/dist/kakapo-capability.d.ts +6 -0
- package/dist/kakapo-capability.js +50 -0
- package/dist/kakapo-scene-skill.d.ts +1 -0
- package/dist/kakapo-scene-skill.js +54 -0
- package/dist/kakapo.d.ts +5 -0
- package/dist/kakapo.js +3 -0
- package/dist/local-transport.d.ts +11 -0
- package/dist/local-transport.js +15 -0
- package/dist/model-history.d.ts +5 -0
- package/dist/model-history.js +34 -0
- package/dist/model-options.d.ts +4 -0
- package/dist/model-options.js +25 -0
- package/dist/models.d.ts +3 -15
- package/dist/models.js +2 -16
- package/dist/native-executor.d.ts +5 -0
- package/dist/native-executor.js +92 -0
- package/dist/pricing.d.ts +3 -0
- package/dist/pricing.js +70 -0
- package/dist/prompt.d.ts +1 -1
- package/dist/prompt.js +19 -209
- package/dist/quickjs-executor.d.ts +13 -0
- package/dist/quickjs-executor.js +309 -0
- package/dist/runtime.d.ts +16 -5
- package/dist/runtime.js +61 -10
- package/dist/server/artifacts.d.ts +17 -0
- package/dist/server/artifacts.js +36 -0
- package/dist/server/demo-account.d.ts +3 -0
- package/dist/server/demo-account.js +38 -0
- package/dist/server/executor.d.ts +17 -0
- package/dist/server/executor.js +35 -0
- package/dist/server/express.d.ts +13 -0
- package/dist/server/express.js +159 -0
- package/dist/server/index.d.ts +8 -0
- package/dist/server/index.js +5 -0
- package/dist/server/service.d.ts +41 -0
- package/dist/server/service.js +402 -0
- package/dist/server/standalone.d.ts +9 -0
- package/dist/server/standalone.js +85 -0
- package/dist/server/store.d.ts +16 -0
- package/dist/server/store.js +70 -0
- package/dist/server/types.d.ts +53 -0
- package/dist/server/types.js +1 -0
- package/dist/types.d.ts +3 -27
- package/dist/ui.d.ts +224 -0
- package/dist/ui.js +71 -0
- package/dist/womp-scene-api.d.ts +195 -3
- package/dist/womp-scene-api.js +102 -73
- package/package.json +66 -10
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import * as RELEASE_SYNC_MODULE from "@jitl/quickjs-wasmfile-release-sync";
|
|
2
|
+
import { newQuickJSWASMModuleFromVariant, } from "quickjs-emscripten-core";
|
|
3
|
+
import { ExecutionBudget } from "./execution-lifetime.js";
|
|
4
|
+
const DEFAULT_OPTIONS = {
|
|
5
|
+
memoryLimitBytes: 64 * 1024 * 1024,
|
|
6
|
+
maxStackSizeBytes: 512 * 1024,
|
|
7
|
+
maxHostCalls: 10_000,
|
|
8
|
+
maxBridgeBytes: 1024 * 1024,
|
|
9
|
+
maxCodeBytes: 256 * 1024,
|
|
10
|
+
};
|
|
11
|
+
const RELEASE_SYNC = RELEASE_SYNC_MODULE;
|
|
12
|
+
function byteLength(value) {
|
|
13
|
+
if (value === undefined)
|
|
14
|
+
return 0;
|
|
15
|
+
const serialized = JSON.stringify(value, (_key, item) => typeof item === "bigint" ? item.toString() : item);
|
|
16
|
+
return new TextEncoder().encode(serialized ?? "").byteLength;
|
|
17
|
+
}
|
|
18
|
+
function errorMessage(error) {
|
|
19
|
+
if (error instanceof Error)
|
|
20
|
+
return error.message;
|
|
21
|
+
if (typeof error === "object" && error !== null && "message" in error) {
|
|
22
|
+
return String(error.message);
|
|
23
|
+
}
|
|
24
|
+
return String(error);
|
|
25
|
+
}
|
|
26
|
+
function unwrapResult(context, result) {
|
|
27
|
+
if (result.error === undefined)
|
|
28
|
+
return result.value;
|
|
29
|
+
const error = result.error.consume(context.dump);
|
|
30
|
+
throw new Error(errorMessage(error));
|
|
31
|
+
}
|
|
32
|
+
function isPromise(value) {
|
|
33
|
+
return ((typeof value === "object" || typeof value === "function") &&
|
|
34
|
+
value !== null &&
|
|
35
|
+
"then" in value &&
|
|
36
|
+
typeof value.then === "function");
|
|
37
|
+
}
|
|
38
|
+
class QuickJSBridge {
|
|
39
|
+
context;
|
|
40
|
+
runtime;
|
|
41
|
+
options;
|
|
42
|
+
budget;
|
|
43
|
+
active = true;
|
|
44
|
+
hostCallCount = 0;
|
|
45
|
+
deferredPromises = new Set();
|
|
46
|
+
hostTasks = new Set();
|
|
47
|
+
hostFailures = [];
|
|
48
|
+
callbackScopes = new Set();
|
|
49
|
+
pendingCallbacks = new Set();
|
|
50
|
+
constructor(context, runtime, options, budget) {
|
|
51
|
+
this.context = context;
|
|
52
|
+
this.runtime = runtime;
|
|
53
|
+
this.options = options;
|
|
54
|
+
this.budget = budget;
|
|
55
|
+
}
|
|
56
|
+
setGlobal(name, value) {
|
|
57
|
+
this.toQuickJS(value, true).consume((handle) => this.context.setProp(this.context.global, name, handle));
|
|
58
|
+
}
|
|
59
|
+
async waitForHostTasks(timeout) {
|
|
60
|
+
while (this.hostTasks.size > 0) {
|
|
61
|
+
await Promise.race([Promise.all([...this.hostTasks]), timeout]);
|
|
62
|
+
}
|
|
63
|
+
if (this.hostFailures.length > 0)
|
|
64
|
+
throw this.hostFailures[0];
|
|
65
|
+
}
|
|
66
|
+
dispose() {
|
|
67
|
+
this.active = false;
|
|
68
|
+
for (const reject of this.pendingCallbacks) {
|
|
69
|
+
reject(new Error("JavaScript execution has ended."));
|
|
70
|
+
}
|
|
71
|
+
this.pendingCallbacks.clear();
|
|
72
|
+
for (const scope of this.callbackScopes)
|
|
73
|
+
this.closeScope(scope);
|
|
74
|
+
for (const deferred of this.deferredPromises)
|
|
75
|
+
deferred.dispose();
|
|
76
|
+
}
|
|
77
|
+
closeScope(scope) {
|
|
78
|
+
if (scope.active === false)
|
|
79
|
+
return;
|
|
80
|
+
scope.active = false;
|
|
81
|
+
for (const handle of scope.handles)
|
|
82
|
+
handle.dispose();
|
|
83
|
+
this.callbackScopes.delete(scope);
|
|
84
|
+
}
|
|
85
|
+
countCall(scope) {
|
|
86
|
+
if (this.active === false || scope?.active === false) {
|
|
87
|
+
throw new Error("JavaScript callback or draft is no longer active.");
|
|
88
|
+
}
|
|
89
|
+
this.hostCallCount += 1;
|
|
90
|
+
if (this.hostCallCount > this.options.maxHostCalls) {
|
|
91
|
+
throw new Error(`JavaScript exceeded the ${this.options.maxHostCalls} host call limit.`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
fromArgument(handle, scope) {
|
|
95
|
+
if (this.context.typeof(handle) !== "function")
|
|
96
|
+
return this.context.dump(handle);
|
|
97
|
+
const callback = handle.dup();
|
|
98
|
+
scope.handles.push(callback);
|
|
99
|
+
return (...args) => {
|
|
100
|
+
this.countCall(scope);
|
|
101
|
+
this.ensureWithinLimit(args, "Callback arguments");
|
|
102
|
+
const handles = [];
|
|
103
|
+
try {
|
|
104
|
+
for (const arg of args)
|
|
105
|
+
handles.push(this.toQuickJS(arg, true, scope));
|
|
106
|
+
const result = unwrapResult(this.context, this.budget.runGuest(() => this.context.callFunction(callback, this.context.undefined, ...handles)));
|
|
107
|
+
return new Promise((resolve, reject) => {
|
|
108
|
+
this.pendingCallbacks.add(reject);
|
|
109
|
+
result.consume(this.context.resolvePromise).then(async (settled) => {
|
|
110
|
+
if (this.active === false)
|
|
111
|
+
return;
|
|
112
|
+
try {
|
|
113
|
+
const value = unwrapResult(this.context, settled).consume(this.context.dump);
|
|
114
|
+
this.ensureWithinLimit(value, "Callback result");
|
|
115
|
+
await Promise.all(scope.pending);
|
|
116
|
+
resolve(value);
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
reject(error);
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
this.pendingCallbacks.delete(reject);
|
|
123
|
+
}
|
|
124
|
+
}, reject);
|
|
125
|
+
// A callback can enqueue guest promises while a host function is running.
|
|
126
|
+
queueMicrotask(() => {
|
|
127
|
+
if (this.active)
|
|
128
|
+
this.executePendingJobs();
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
for (const argument of handles)
|
|
134
|
+
argument.dispose();
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
ensureWithinLimit(value, label) {
|
|
139
|
+
if (byteLength(value) <= this.options.maxBridgeBytes)
|
|
140
|
+
return;
|
|
141
|
+
throw new Error(`${label} exceeded the ${this.options.maxBridgeBytes} byte bridge limit.`);
|
|
142
|
+
}
|
|
143
|
+
toQuickJS(value, allowFunctions, scope) {
|
|
144
|
+
if (value === undefined)
|
|
145
|
+
return this.context.undefined;
|
|
146
|
+
if (value === null)
|
|
147
|
+
return this.context.null;
|
|
148
|
+
if (typeof value === "boolean")
|
|
149
|
+
return value ? this.context.true : this.context.false;
|
|
150
|
+
if (typeof value === "number")
|
|
151
|
+
return this.context.newNumber(value);
|
|
152
|
+
if (typeof value === "string")
|
|
153
|
+
return this.context.newString(value);
|
|
154
|
+
if (typeof value === "bigint")
|
|
155
|
+
return this.context.newBigInt(value);
|
|
156
|
+
if (value instanceof Uint8Array) {
|
|
157
|
+
return this.context.newArrayBuffer(new Uint8Array(value).buffer);
|
|
158
|
+
}
|
|
159
|
+
if (Array.isArray(value)) {
|
|
160
|
+
const array = this.context.newArray();
|
|
161
|
+
value.forEach((item, index) => {
|
|
162
|
+
this.toQuickJS(item, allowFunctions, scope).consume((handle) => this.context.setProp(array, index, handle));
|
|
163
|
+
});
|
|
164
|
+
return array;
|
|
165
|
+
}
|
|
166
|
+
if (typeof value === "function") {
|
|
167
|
+
if (allowFunctions === false) {
|
|
168
|
+
throw new Error("Host functions cannot be returned to JavaScript.");
|
|
169
|
+
}
|
|
170
|
+
return this.createFunction(value.name, value, scope);
|
|
171
|
+
}
|
|
172
|
+
if (typeof value === "object") {
|
|
173
|
+
const object = this.context.newObject(this.context.null);
|
|
174
|
+
for (const [key, item] of Object.entries(value)) {
|
|
175
|
+
this.toQuickJS(item, allowFunctions, scope).consume((handle) => this.context.setProp(object, key, handle));
|
|
176
|
+
}
|
|
177
|
+
return object;
|
|
178
|
+
}
|
|
179
|
+
throw new Error(`Unsupported host value type: ${typeof value}.`);
|
|
180
|
+
}
|
|
181
|
+
createFunction(name, fn, parentScope) {
|
|
182
|
+
return this.context.newFunction(name, (...argumentHandles) => {
|
|
183
|
+
this.countCall(parentScope);
|
|
184
|
+
const scope = { active: true, handles: [], pending: [] };
|
|
185
|
+
this.callbackScopes.add(scope);
|
|
186
|
+
try {
|
|
187
|
+
const args = argumentHandles.map((handle) => this.fromArgument(handle, scope));
|
|
188
|
+
this.ensureWithinLimit(args, `${name || "Host function"} arguments`);
|
|
189
|
+
const value = fn(...args);
|
|
190
|
+
if (isPromise(value)) {
|
|
191
|
+
const pending = Promise.resolve(value).finally(() => this.closeScope(scope));
|
|
192
|
+
parentScope?.pending.push(pending);
|
|
193
|
+
return this.createPromise(name, pending);
|
|
194
|
+
}
|
|
195
|
+
this.ensureWithinLimit(value, `${name || "Host function"} result`);
|
|
196
|
+
this.closeScope(scope);
|
|
197
|
+
return this.toQuickJS(value, false);
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
this.closeScope(scope);
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
createPromise(name, value) {
|
|
206
|
+
const deferred = this.context.newPromise();
|
|
207
|
+
this.deferredPromises.add(deferred);
|
|
208
|
+
const task = Promise.resolve(value)
|
|
209
|
+
.then((result) => this.resolve(deferred, name, result), (error) => {
|
|
210
|
+
this.hostFailures.push(error);
|
|
211
|
+
this.reject(deferred, error);
|
|
212
|
+
})
|
|
213
|
+
.finally(() => {
|
|
214
|
+
this.deferredPromises.delete(deferred);
|
|
215
|
+
this.hostTasks.delete(task);
|
|
216
|
+
if (this.active)
|
|
217
|
+
this.executePendingJobs();
|
|
218
|
+
});
|
|
219
|
+
this.hostTasks.add(task);
|
|
220
|
+
return deferred.handle;
|
|
221
|
+
}
|
|
222
|
+
resolve(deferred, name, value) {
|
|
223
|
+
if (this.active === false)
|
|
224
|
+
return;
|
|
225
|
+
try {
|
|
226
|
+
this.ensureWithinLimit(value, `${name || "Host function"} result`);
|
|
227
|
+
this.toQuickJS(value, false).consume(deferred.resolve);
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
this.reject(deferred, error);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
executePendingJobs() {
|
|
234
|
+
try {
|
|
235
|
+
this.budget.runGuest(() => {
|
|
236
|
+
const result = this.runtime.executePendingJobs();
|
|
237
|
+
if (result.error !== undefined) {
|
|
238
|
+
const error = result.error.consume(this.context.dump);
|
|
239
|
+
throw new Error(errorMessage(error));
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
catch (error) {
|
|
244
|
+
this.hostFailures.push(error);
|
|
245
|
+
for (const reject of this.pendingCallbacks)
|
|
246
|
+
reject(new Error(errorMessage(error)));
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
reject(deferred, error) {
|
|
250
|
+
if (this.active === false)
|
|
251
|
+
return;
|
|
252
|
+
this.context.newError(errorMessage(error)).consume(deferred.reject);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
export class QuickJSJavaScriptExecutor {
|
|
256
|
+
options;
|
|
257
|
+
constructor(options = {}) {
|
|
258
|
+
this.options = { ...DEFAULT_OPTIONS, ...options };
|
|
259
|
+
}
|
|
260
|
+
async execute(code, globals, timeoutMs, signal, userWait) {
|
|
261
|
+
signal?.throwIfAborted();
|
|
262
|
+
if (new TextEncoder().encode(code).byteLength > this.options.maxCodeBytes) {
|
|
263
|
+
throw new Error(`JavaScript exceeded the ${this.options.maxCodeBytes} byte code limit.`);
|
|
264
|
+
}
|
|
265
|
+
if (Number.isFinite(timeoutMs) === false || timeoutMs <= 0) {
|
|
266
|
+
throw new Error("JavaScript timeout must be a positive finite number.");
|
|
267
|
+
}
|
|
268
|
+
const module = await newQuickJSWASMModuleFromVariant(RELEASE_SYNC);
|
|
269
|
+
const budget = new ExecutionBudget(timeoutMs, signal, userWait);
|
|
270
|
+
const runtime = module.newRuntime();
|
|
271
|
+
const context = runtime.newContext();
|
|
272
|
+
const bridge = new QuickJSBridge(context, runtime, this.options, budget);
|
|
273
|
+
runtime.setMemoryLimit(this.options.memoryLimitBytes);
|
|
274
|
+
runtime.setMaxStackSize(this.options.maxStackSizeBytes);
|
|
275
|
+
runtime.setInterruptHandler(() => signal?.aborted === true || budget.expired());
|
|
276
|
+
try {
|
|
277
|
+
for (const [name, value] of Object.entries(globals))
|
|
278
|
+
bridge.setGlobal(name, value);
|
|
279
|
+
const evaluation = budget.runGuest(() => context.evalCode(`(async () => {\n"use strict";\n${code}\n})()`, "spark-agent.js", {
|
|
280
|
+
type: "global",
|
|
281
|
+
strict: true,
|
|
282
|
+
}));
|
|
283
|
+
const promiseHandle = unwrapResult(context, evaluation);
|
|
284
|
+
const execution = promiseHandle.consume(context.resolvePromise);
|
|
285
|
+
budget.runGuest(() => runtime.executePendingJobs());
|
|
286
|
+
const resultHandle = unwrapResult(context, await Promise.race([execution, budget.interrupted]));
|
|
287
|
+
const result = resultHandle.consume(context.dump);
|
|
288
|
+
if (byteLength(result) > this.options.maxBridgeBytes) {
|
|
289
|
+
throw new Error(`JavaScript result exceeded the ${this.options.maxBridgeBytes} byte bridge limit.`);
|
|
290
|
+
}
|
|
291
|
+
await bridge.waitForHostTasks(budget.interrupted);
|
|
292
|
+
if (budget.expired())
|
|
293
|
+
throw budget.timeoutError();
|
|
294
|
+
return result;
|
|
295
|
+
}
|
|
296
|
+
catch (error) {
|
|
297
|
+
if (budget.expired() && errorMessage(error).includes("interrupted")) {
|
|
298
|
+
throw budget.timeoutError();
|
|
299
|
+
}
|
|
300
|
+
throw error;
|
|
301
|
+
}
|
|
302
|
+
finally {
|
|
303
|
+
bridge.dispose();
|
|
304
|
+
budget.dispose();
|
|
305
|
+
context.dispose();
|
|
306
|
+
runtime.dispose();
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { KakapoAPI, type ScreenshotView } from "@womp/kakapo-sdk";
|
|
2
2
|
import type { JavaScriptExecutor } from "./executor.js";
|
|
3
|
-
import type { ExecutionResult, ExecutionScreenshot,
|
|
3
|
+
import type { ExecutionResult, ExecutionScreenshot, SparkRuntimeEventSink, SparkRuntime } from "./types.js";
|
|
4
|
+
import { type WompSceneApi } from "./womp-scene-api.js";
|
|
5
|
+
import type { SparkInvocationContext } from "./capabilities.js";
|
|
4
6
|
export declare class KakapoRuntime implements SparkRuntime {
|
|
5
|
-
private readonly executor;
|
|
6
|
-
private readonly eventSink;
|
|
7
7
|
readonly api: KakapoAPI;
|
|
8
8
|
rootId: number;
|
|
9
9
|
defaultMaterialId: number | undefined;
|
|
@@ -12,15 +12,26 @@ export declare class KakapoRuntime implements SparkRuntime {
|
|
|
12
12
|
private readonly materialNames;
|
|
13
13
|
private readonly createdIds;
|
|
14
14
|
private fonts;
|
|
15
|
-
|
|
15
|
+
private readonly executor;
|
|
16
|
+
private eventSink;
|
|
17
|
+
constructor(url: string);
|
|
18
|
+
constructor(url: string, eventSink: SparkRuntimeEventSink);
|
|
19
|
+
constructor(url: string, executor: JavaScriptExecutor, eventSink?: SparkRuntimeEventSink);
|
|
16
20
|
get connected(): boolean;
|
|
17
|
-
|
|
21
|
+
setEventSink(eventSink: SparkRuntimeEventSink): void;
|
|
22
|
+
private emit;
|
|
18
23
|
connect(): Promise<void>;
|
|
19
24
|
disconnect(): void;
|
|
20
25
|
private sdkCall;
|
|
21
26
|
private sceneCall;
|
|
22
27
|
private requireNode;
|
|
23
28
|
private sceneApi;
|
|
29
|
+
inspect(): Promise<{
|
|
30
|
+
revision: string;
|
|
31
|
+
scene: import("@womp/kakapo-sdk").Scene;
|
|
32
|
+
}>;
|
|
33
|
+
private sceneRevision;
|
|
34
|
+
edit<T>(callback: (draft: WompSceneApi) => Promise<T> | T, expectedRevision: string, context: SparkInvocationContext): Promise<T>;
|
|
24
35
|
screenshot(input: {
|
|
25
36
|
targetId?: number;
|
|
26
37
|
view?: ScreenshotView;
|
package/dist/runtime.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { KakapoAPI
|
|
1
|
+
import { KakapoAPI } from "@womp/kakapo-sdk";
|
|
2
|
+
import { QuickJSJavaScriptExecutor } from "./quickjs-executor.js";
|
|
2
3
|
import { createWompSceneApi } from "./womp-scene-api.js";
|
|
3
4
|
function safeValue(value) {
|
|
4
5
|
if (value instanceof Uint8Array)
|
|
@@ -13,8 +14,6 @@ function safeValue(value) {
|
|
|
13
14
|
}
|
|
14
15
|
}
|
|
15
16
|
export class KakapoRuntime {
|
|
16
|
-
executor;
|
|
17
|
-
eventSink;
|
|
18
17
|
api;
|
|
19
18
|
rootId = 0;
|
|
20
19
|
defaultMaterialId;
|
|
@@ -23,14 +22,24 @@ export class KakapoRuntime {
|
|
|
23
22
|
materialNames = new Map();
|
|
24
23
|
createdIds = new Set();
|
|
25
24
|
fonts = [];
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
executor;
|
|
26
|
+
eventSink;
|
|
27
|
+
constructor(url, executorOrEventSink, eventSink) {
|
|
29
28
|
this.api = new KakapoAPI({ url, requestTimeoutMs: 30_000 });
|
|
29
|
+
if (typeof executorOrEventSink === "function") {
|
|
30
|
+
this.executor = new QuickJSJavaScriptExecutor();
|
|
31
|
+
this.eventSink = executorOrEventSink;
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
this.executor = executorOrEventSink ?? new QuickJSJavaScriptExecutor();
|
|
35
|
+
this.eventSink = eventSink ?? (() => undefined);
|
|
30
36
|
}
|
|
31
37
|
get connected() {
|
|
32
38
|
return this.api.isConnected;
|
|
33
39
|
}
|
|
40
|
+
setEventSink(eventSink) {
|
|
41
|
+
this.eventSink = eventSink;
|
|
42
|
+
}
|
|
34
43
|
emit(event) {
|
|
35
44
|
this.eventSink(event);
|
|
36
45
|
}
|
|
@@ -127,6 +136,48 @@ export class KakapoRuntime {
|
|
|
127
136
|
print,
|
|
128
137
|
});
|
|
129
138
|
}
|
|
139
|
+
async inspect() {
|
|
140
|
+
await this.api.refreshScene();
|
|
141
|
+
return { revision: await this.sceneRevision(), scene: this.api.getScene() };
|
|
142
|
+
}
|
|
143
|
+
async sceneRevision() {
|
|
144
|
+
const bytes = new TextEncoder().encode(JSON.stringify(this.api.getScene()));
|
|
145
|
+
const digest = await crypto.subtle.digest("SHA-256", bytes);
|
|
146
|
+
return Array.from(new Uint8Array(digest), (value) => value.toString(16).padStart(2, "0")).join("");
|
|
147
|
+
}
|
|
148
|
+
async edit(callback, expectedRevision, context) {
|
|
149
|
+
let callbackCompleted = false;
|
|
150
|
+
let changed = false;
|
|
151
|
+
const receipt = {
|
|
152
|
+
id: crypto.randomUUID(),
|
|
153
|
+
capability: "scene",
|
|
154
|
+
operation: "edit",
|
|
155
|
+
};
|
|
156
|
+
let value;
|
|
157
|
+
try {
|
|
158
|
+
value = await this.api.editScene(async (scene) => {
|
|
159
|
+
context.signal.throwIfAborted();
|
|
160
|
+
if ((await this.sceneRevision()) !== expectedRevision) {
|
|
161
|
+
throw new Error("Scene changed since inspection. Read the scene again before editing.");
|
|
162
|
+
}
|
|
163
|
+
const result = await callback(this.sceneApi(scene, () => undefined));
|
|
164
|
+
context.signal.throwIfAborted();
|
|
165
|
+
changed = (await this.sceneRevision()) !== expectedRevision;
|
|
166
|
+
callbackCompleted = true;
|
|
167
|
+
return result;
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
await context.recordReceipt({
|
|
172
|
+
...receipt,
|
|
173
|
+
status: callbackCompleted ? "unknown" : "rolled_back",
|
|
174
|
+
});
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
if (changed)
|
|
178
|
+
await context.recordReceipt({ ...receipt, status: "committed" });
|
|
179
|
+
return value;
|
|
180
|
+
}
|
|
130
181
|
async screenshot(input) {
|
|
131
182
|
let targetId;
|
|
132
183
|
if (input.targetId !== undefined) {
|
|
@@ -135,9 +186,7 @@ export class KakapoRuntime {
|
|
|
135
186
|
while (target.parentId !== null && target.parentId !== 0) {
|
|
136
187
|
target = this.api.getNode(target.parentId);
|
|
137
188
|
}
|
|
138
|
-
targetId = target.kind === "union" || target.kind === "group"
|
|
139
|
-
? target.id
|
|
140
|
-
: input.targetId;
|
|
189
|
+
targetId = target.kind === "union" || target.kind === "group" ? target.id : input.targetId;
|
|
141
190
|
}
|
|
142
191
|
const view = input.view ?? "isometric";
|
|
143
192
|
const image = await this.sdkCall("captureScreenshot", [{ targetId, view }], () => this.api.captureScreenshot({ targetId, view, timeoutMs: 30_000 }));
|
|
@@ -157,7 +206,9 @@ export class KakapoRuntime {
|
|
|
157
206
|
this.emit({ type: "javascript", code, purpose });
|
|
158
207
|
const consoleFacade = {
|
|
159
208
|
log: (...values) => {
|
|
160
|
-
const text = values
|
|
209
|
+
const text = values
|
|
210
|
+
.map((value) => (typeof value === "string" ? value : JSON.stringify(safeValue(value))))
|
|
211
|
+
.join(" ");
|
|
161
212
|
logs.push(text);
|
|
162
213
|
this.emit({ type: "console", text });
|
|
163
214
|
},
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ExecutionScreenshot } from "../types.js";
|
|
2
|
+
import type { SparkArtifact } from "../ui.js";
|
|
3
|
+
export type StoredSparkArtifact = SparkArtifact & {
|
|
4
|
+
ownerId: string;
|
|
5
|
+
data: Uint8Array;
|
|
6
|
+
};
|
|
7
|
+
export interface SparkArtifactStore {
|
|
8
|
+
write(ownerId: string, screenshot: ExecutionScreenshot): Promise<SparkArtifact>;
|
|
9
|
+
get(ownerId: string, artifactId: string): Promise<StoredSparkArtifact | undefined>;
|
|
10
|
+
}
|
|
11
|
+
export declare class MemorySparkArtifactStore implements SparkArtifactStore {
|
|
12
|
+
private readonly artifacts;
|
|
13
|
+
private readonly publicBaseUrl;
|
|
14
|
+
constructor(publicBaseUrl?: string);
|
|
15
|
+
write(ownerId: string, screenshot: ExecutionScreenshot): Promise<SparkArtifact>;
|
|
16
|
+
get(ownerId: string, artifactId: string): Promise<StoredSparkArtifact | undefined>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export class MemorySparkArtifactStore {
|
|
2
|
+
artifacts = new Map();
|
|
3
|
+
publicBaseUrl;
|
|
4
|
+
constructor(publicBaseUrl = "/agent/artifacts") {
|
|
5
|
+
this.publicBaseUrl = publicBaseUrl.replace(/\/$/u, "");
|
|
6
|
+
}
|
|
7
|
+
async write(ownerId, screenshot) {
|
|
8
|
+
const id = crypto.randomUUID();
|
|
9
|
+
const artifact = {
|
|
10
|
+
id,
|
|
11
|
+
ownerId,
|
|
12
|
+
url: `${this.publicBaseUrl}/${id}`,
|
|
13
|
+
mediaType: screenshot.mediaType,
|
|
14
|
+
name: `spark-${screenshot.view}.jpg`,
|
|
15
|
+
width: screenshot.width,
|
|
16
|
+
height: screenshot.height,
|
|
17
|
+
metadata: {
|
|
18
|
+
view: screenshot.view,
|
|
19
|
+
targetId: screenshot.targetId,
|
|
20
|
+
},
|
|
21
|
+
data: screenshot.data.slice(),
|
|
22
|
+
};
|
|
23
|
+
this.artifacts.set(id, artifact);
|
|
24
|
+
const { data: _data, ownerId: _ownerId, ...reference } = artifact;
|
|
25
|
+
return structuredClone(reference);
|
|
26
|
+
}
|
|
27
|
+
async get(ownerId, artifactId) {
|
|
28
|
+
const artifact = this.artifacts.get(artifactId);
|
|
29
|
+
if (artifact === undefined || artifact.ownerId !== ownerId)
|
|
30
|
+
return undefined;
|
|
31
|
+
return {
|
|
32
|
+
...structuredClone(artifact),
|
|
33
|
+
data: artifact.data.slice(),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** Explicit development fixture. Never imported by the production browser composition. */
|
|
2
|
+
export function createDemoAccountHost() {
|
|
3
|
+
return {
|
|
4
|
+
async read({ sections }, context) {
|
|
5
|
+
context.signal.throwIfAborted();
|
|
6
|
+
const scope = { userId: "demo-user", teamId: null };
|
|
7
|
+
const result = {
|
|
8
|
+
provenance: { kind: "fixture", profile: "demo-account", fictional: true },
|
|
9
|
+
};
|
|
10
|
+
for (const section of sections) {
|
|
11
|
+
if (section === "profile") {
|
|
12
|
+
result.profile = {
|
|
13
|
+
status: "available",
|
|
14
|
+
scope,
|
|
15
|
+
data: {
|
|
16
|
+
userId: "demo-user",
|
|
17
|
+
name: "Alex Demo",
|
|
18
|
+
email: "alex@example.test",
|
|
19
|
+
biography: "Fictional account for local Spark development.",
|
|
20
|
+
isPro: false,
|
|
21
|
+
createdAt: "2026-01-01T00:00:00.000Z",
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (section === "teams") {
|
|
27
|
+
result.teams = { status: "available", scope, data: [] };
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
result[section] = {
|
|
31
|
+
status: "unavailable",
|
|
32
|
+
reason: `The fictional demo-account profile does not supply ${section} or access to real account services.`,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { LanguageModel } from "ai";
|
|
2
|
+
import { type SparkAgentOptions } from "../agent.js";
|
|
3
|
+
import type { SparkRunExecutionContext, SparkRunExecutor, SparkRunSettings } from "./types.js";
|
|
4
|
+
export type ResolvedSparkModel = {
|
|
5
|
+
model: LanguageModel;
|
|
6
|
+
modelId: string;
|
|
7
|
+
providerOptions?: SparkAgentOptions["providerOptions"];
|
|
8
|
+
maxOutputTokens?: number;
|
|
9
|
+
maxInputTokens?: number;
|
|
10
|
+
};
|
|
11
|
+
export type SparkRunExecutorOptions = {
|
|
12
|
+
resolveModel(settings: SparkRunSettings): ResolvedSparkModel | Promise<ResolvedSparkModel>;
|
|
13
|
+
createHost(context: SparkRunExecutionContext): Promise<Pick<SparkAgentOptions, "capabilities" | "executor" | "artifacts" | "persistence"> & {
|
|
14
|
+
dispose(): void;
|
|
15
|
+
}>;
|
|
16
|
+
};
|
|
17
|
+
export declare function createSparkRunExecutor(options: SparkRunExecutorOptions): SparkRunExecutor;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { convertSparkMessages } from "../model-history.js";
|
|
2
|
+
import { runSparkAgent } from "../agent.js";
|
|
3
|
+
import { createSparkTools } from "../agent-tools.js";
|
|
4
|
+
export function createSparkRunExecutor(options) {
|
|
5
|
+
return async (context) => {
|
|
6
|
+
const resolved = await options.resolveModel(context.settings);
|
|
7
|
+
const host = await options.createHost(context);
|
|
8
|
+
try {
|
|
9
|
+
const tools = createSparkTools({
|
|
10
|
+
...host,
|
|
11
|
+
runId: context.runId,
|
|
12
|
+
controller: new AbortController(),
|
|
13
|
+
write: () => undefined,
|
|
14
|
+
});
|
|
15
|
+
const messages = await convertSparkMessages(context.uiMessages, tools);
|
|
16
|
+
const run = runSparkAgent({
|
|
17
|
+
...host,
|
|
18
|
+
messages,
|
|
19
|
+
model: resolved.model,
|
|
20
|
+
modelId: resolved.modelId,
|
|
21
|
+
providerOptions: resolved.providerOptions,
|
|
22
|
+
maxOutputTokens: resolved.maxOutputTokens,
|
|
23
|
+
maxInputTokens: resolved.maxInputTokens,
|
|
24
|
+
runId: context.runId,
|
|
25
|
+
messageId: context.assistantMessageId,
|
|
26
|
+
signal: context.signal,
|
|
27
|
+
});
|
|
28
|
+
return { stream: run.stream, result: run.result.finally(() => host.dispose()) };
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
host.dispose();
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type Request, type Router } from "express";
|
|
2
|
+
import { SPARK_RUN_STATUSES } from "../ui.js";
|
|
3
|
+
import type { SparkArtifactStore } from "./artifacts.js";
|
|
4
|
+
import { SparkAgentService } from "./service.js";
|
|
5
|
+
export type SparkAgentRouterOptions = {
|
|
6
|
+
service: SparkAgentService;
|
|
7
|
+
artifactStore?: SparkArtifactStore;
|
|
8
|
+
resolveOwnerId?: (request: Request) => string | Promise<string>;
|
|
9
|
+
heartbeatMs?: number;
|
|
10
|
+
jsonLimit?: string;
|
|
11
|
+
};
|
|
12
|
+
export declare function createSparkAgentRouter(options: SparkAgentRouterOptions): Router;
|
|
13
|
+
export { SPARK_RUN_STATUSES };
|