Initial pass at wasm support via cffi #2330
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What problem(s) was I solving?
The BAML runtime needed WebAssembly support to enable browser-based execution. Previously, BAML only supported server-side runtimes (Python, Ruby, Node.js). This PR implements WASM compilation of the FFI layer and creates a TypeScript client that can load and execute BAML functions directly in web browsers.
What user-facing changes did I ship?
language_client_wasm
) for browser environmentsHow I implemented it
Implemented a 5-phase approach:
cfg(target_arch = "wasm32")
and wasm-bindgen supportAsyncRuntime
that uses Tokio on native andspawn_local
on WASMBamlWasmRuntime
class with module loading and function invocationKey technical decisions:
native
vswasm
) to conditionally compile platform-specific codewindow.__baml_callbacks
registryBuilt in close collaboration with @hellovai who sat in my apartment and shouted at claude with me all day
plan/Research artifacts in thoughts/ can be removed once they get put in a good location, leaving them in for now
How to verify it
To verify the WASM implementation:
cd engine/language_client_cffi && ./build_wasm.sh
cd engine/language_client_wasm && npm run build
python3 -m http.server 3000
inengine/language_client_wasm
Current status:
Description for the changelog
Add WebAssembly support for BAML runtime, enabling browser-based execution of BAML functions through a new TypeScript client library