Sandboxed Extensions
Python, TypeScript, and other protobuf clients call the engine from outside. Your application controls when that code runs and when it submits statements.
An extension is code the engine itself executes. You package a workflow as a WebAssembly component, upload it, and ask the engine to run it in a sandbox. The workflow can call engine statements to read data, edit projects, and control the scene. Both native and browser engines support extensions.
Execution model
- You build a workflow into an
extension.wasmcomponent. - Your application loads that component through the engine’s extension API.
- The engine creates a sandbox and calls the component’s
run()function. - The workflow calls a host function to submit statements to the engine. Each call waits for its
result, and the workflow continues until
run()returns.
The component has its own memory. It receives engine access through the interface the host supplies. It receives no direct filesystem or network access. Use the runtime limits to decide whether a workflow fits this environment.
The WIT interface
WIT is the Wasm Interface Type language. It describes the functions and types that a WebAssembly component imports and exports. A WIT world groups the imports and exports a component needs. The WIT reference describes the language.
SpatialEngine’s extension interface defines an extension
world with two parts:
| Function | Who provides it | What it does |
|---|---|---|
run() | Your component | Executes the workflow when the engine calls it. |
engine.execute-statement(...) | SpatialEngine | Runs a statement and returns its result. |
Your language’s component toolchain reads the WIT file and generates bindings for these functions. The protobuf schema defines the individual statements and their request and response messages. The host function passes the encoded messages between the extension and engine.
The Python SDK connects its generated statement methods to that host function through
ExtensionTransport. Your workflow uses the same typed SDK calls as a native application.
The transport handles the component bindings and protobuf encoding.
Build a Python extension
The supported Python workflow uses componentize-py to package your code and its Python runtime
as a WebAssembly component. Libraries with native extensions need compatible WebAssembly builds.
For another language, see custom bindings.
Write the workflow
Save this workflow as app.py. The engine calls WitWorld.run() when it executes the component:
import spatialengine
from spatialengine.types.scene import Cube, Geometry, Shape, ShapeAppearance
class WitWorld:
def run(self):
client = spatialengine.Client(transport=spatialengine.ExtensionTransport())
node = client.scene.spawn()
client.scene.shape.set(
node=node,
shape=Shape(
geometry=Geometry.Cube(Cube(half_extents=1.0)),
appearance=ShapeAppearance(wireframe=False),
),
)Build the component
Create a virtual environment for the component. Install the Python SDK wheel without the grpc extra, and install componentize-py in that environment:
python -m pip install componentize-pyDownload the WIT interface as extension.wit beside app.py. In that directory, generate bindings and build the component:
componentize-py -d extension.wit -w extension bindings .
componentize-py -d extension.wit -w extension componentize app -o extension.wasmThe resulting extension.wasm contains your Python workflow. The host bindings become available when the engine instantiates the component.
Run an extension
The application that uploads and runs a component is separate from the workflow inside it. Use the SDK for the engine your application runs.
This example controls a native engine. Enable extensions when you start it:
SpatialEngine --allow-extensionsUse a separate Python environment with the SDK’s grpc extra to connect to the engine. Load the component and run it:
from pathlib import Path
import spatialengine
with spatialengine.connect() as client:
extension = client.extension.load(component=Path("extension.wasm").read_bytes())
client.extension.run(id=extension)
client.extension.unload(id=extension)run() returns when the workflow finishes. Each run starts a fresh sandbox. Use reset to
cancel an active run while keeping the component loaded. unload cancels an active run and
removes the component.
Custom bindings
Use a language with WebAssembly component support. The WIT interface and engine protobuf schema provide the contract for a custom implementation.
Generate guest bindings for the WIT extension world with your language’s component toolchain. Implement its exported run() function. The engine calls this function to execute your workflow and supplies the imported engine.execute-statement function.
WIT describes the component boundary. The engine’s protobuf schema describes the statements you can call. Export the schema and generate protobuf message bindings for your language.
To call a statement, pass its full gRPC method path, the serialized protobuf request, and an optional transaction UUID to execute-statement. For example, print uses /spatialengine.v1.Root/Print. The request bytes contain the protobuf message without gRPC framing.
Decode a successful result as that method’s protobuf response. Decode a failure as spatialengine.v1.StatementError. Pass a transaction UUID when a call joins an existing transaction. Calls follow the same transaction rules as SDK calls.
Compile against the interface and SDK from the engine release you target. Desktop and browser hosts have different restrictions. Exercise the component on each target you distribute.
Runtime limits
Extensions are disabled until startup enables them. When extensions are enabled, any client with statement access can upload and run an extension. An extension can invoke the same engine statements as its caller.
Extensions receive no direct filesystem, environment, network, or standard-input access. Each engine stores at most 32 uploaded extensions, with a 32 MiB limit per upload. Engines allow four concurrent runs. Runs have a 30-second deadline. Native engines limit each run to 256 MiB of linear memory. Browser workers use the browser’s memory limits.
Reset and unload stop execution. Statements the component already applied remain in the engine. Use a transaction when project edits must succeed or fail together.
Choose an integration before moving an existing workflow into a component.