Switchyard lets LLM applications route traffic across models and providers while preserving native OpenAI and Anthropic API compatibility - enabling flexible model selection, benchmarking, and cost/performance optimization.
Rust
feat(ffi): support host-driven WASI routing (#1128)
Alex Fournier409d18189b6a
diff --git a/crates/switchyard-ffi/include/switchyard.h b/crates/switchyard-ffi/include/switchyard.hindex 3ff8fe08a..5c53473c9 100644--- a/crates/switchyard-ffi/include/switchyard.h+++ b/crates/switchyard-ffi/include/switchyard.h@@ -14,17 +14,27 @@ typedef struct SyRouter SyRouter;typedef struct SyRun SyRun;typedef void (*SyNextCallback)(uintptr_t user_data, char *error, char *step_json);+#if defined(__wasm32__)+uint8_t *sy_alloc(uint32_t len);+void sy_free(uint8_t *ptr, uint32_t len);+// Writes the next step to *out_json, or NULL when the runtime is idle.+// Retry idle reads to advance timers and work waiting on other runs.+char *sy_run_next(const SyRun *run, char **out_json);+#endif+// Fallible calls return NULL on success or an owned error string.// JSON inputs are borrowed for the call and must be NUL-terminated.// Free all output strings, including callback strings, with sy_string_free.char *sy_router_new(const char *config_json, SyRouter **out);char *sy_run_new(const SyRouter *router, const char *input_json, SyRun **out);+#if !defined(__wasm32__)// One pending read per run; other runs execute independently. Replies and cancellation may overlap.// NULL accepts one read (one callback, even if cancelled); errors reject it (no callback).// Callbacks may overlap or run before return. Keep callback state alive until they return.// Callbacks must return promptly, not unwind, and not call sy_run_respond or free handles.char *sy_run_next_async(const SyRun *run, SyNextCallback callback, uintptr_t user_data);+#endifchar *sy_run_respond(const SyRun *run, uint64_t call_id, const char *reply_json);char *sy_decode_request(const char *input_json, char **out_json);diff --git a/crates/switchyard-ffi/src/lib.rs b/crates/switchyard-ffi/src/lib.rsindex b004b5ecd..270105906 100644--- a/crates/switchyard-ffi/src/lib.rs+++ b/crates/switchyard-ffi/src/lib.rs@@ -30,6 +30,29 @@ pub extern "C" fn sy_abi_version() -> u32 {1}+/// WASI reactor initialization hook; Rust initializes static data during instantiation.+#[cfg(target_arch = "wasm32")]+#[unsafe(no_mangle)]+pub extern "C" fn _initialize() {}++/// Allocates guest memory for a host-provided buffer.+#[cfg(target_arch = "wasm32")]+#[unsafe(no_mangle)]+pub extern "C" fn sy_alloc(len: usize) -> *mut u8 {+ Box::into_raw(vec![0_u8; len].into_boxed_slice()).cast()+}++/// Releases a buffer returned by `sy_alloc`.+/// # Safety+/// `ptr` and `len` must identify an allocation returned by `sy_alloc`.+#[cfg(target_arch = "wasm32")]+#[unsafe(no_mangle)]+pub unsafe extern "C" fn sy_free(ptr: *mut u8, len: usize) {+ if !ptr.is_null() {+ drop(unsafe { Box::from_raw(std::ptr::slice_from_raw_parts_mut(ptr, len)) });+ }+}+#[derive(Deserialize)]struct RouterConfig {algorithm: AlgorithmSpec,@@ -431,7 +454,7 @@ pub unsafe extern "C" fn sy_router_new(*out = std::ptr::null_mut();let config: RouterConfig = unsafe { parse_json_input(config_json) }?;let algorithm = config.algorithm.build("embedded", &config.targets)?;- let runtime = Runtime::new()?;+ let runtime = new_runtime()?;*out = Box::into_raw(Box::new(SyRouter {engine: Arc::new(Engine { algorithm, runtime }),}));@@ -439,6 +462,68 @@ pub unsafe extern "C" fn sy_router_new(})}+#[cfg(not(target_arch = "wasm32"))]+fn new_runtime() -> std::io::Result<Runtime> {+ Runtime::new()+}++#[cfg(target_arch = "wasm32")]+fn new_runtime() -> std::io::Result<Runtime> {+ tokio::runtime::Builder::new_current_thread()+ .enable_time()+ .on_thread_park(idle::on_park)+ .build()+}++/// Detects when a wasm read cannot make immediate progress.+/// Parking a thread is impossible on wasip1, so the read returns instead of parking.+#[cfg(target_arch = "wasm32")]+mod idle {+ use std::future::Future;+ use std::sync::Mutex;+ use std::sync::atomic::{AtomicBool, Ordering};+ use std::task::{Poll, Waker};++ static WATCHING: AtomicBool = AtomicBool::new(false);+ static IDLE: AtomicBool = AtomicBool::new(false);+ static WAKER: Mutex<Option<Waker>> = Mutex::new(None);++ // Tokio calls this when no task is ready; waking the read makes it return.+ pub(super) fn on_park() {+ if WATCHING.load(Ordering::Relaxed) {+ IDLE.store(true, Ordering::Relaxed);+ if let Some(waker) = WAKER.lock().ok().and_then(|mut waker| waker.take()) {+ waker.wake();+ }+ }+ }++ /// Resolves to `None` when the runtime would park before `read` finishes.+ pub(super) async fn until_idle<T>(read: impl Future<Output = T>) -> Option<T> {+ let mut read = std::pin::pin!(read);+ IDLE.store(false, Ordering::Relaxed);+ WATCHING.store(true, Ordering::Relaxed);+ let output = std::future::poll_fn(|cx| {+ if let Poll::Ready(output) = read.as_mut().poll(cx) {+ return Poll::Ready(Some(output));+ }+ if IDLE.swap(false, Ordering::Relaxed) {+ return Poll::Ready(None);+ }+ if let Ok(mut waker) = WAKER.lock() {+ *waker = Some(cx.waker().clone());+ }+ Poll::Pending+ })+ .await;+ WATCHING.store(false, Ordering::Relaxed);+ if let Ok(mut waker) = WAKER.lock() {+ waker.take();+ }+ output+ }+}+/// Starts a run with host-approved model groups./// # Safety/// Handles must be live, JSON NUL-terminated, and `out` writable.@@ -488,6 +573,7 @@ pub unsafe extern "C" fn sy_run_new(/// Callbacks may overlap or run before this call returns. Free their strings with `sy_string_free`./// Callbacks must return promptly without unwinding, calling `sy_run_respond`, or freeing handles.#[unsafe(no_mangle)]+#[cfg(not(target_arch = "wasm32"))]pub unsafe extern "C" fn sy_run_next_async(run: *const SyRun,callback: Option<unsafe extern "C" fn(usize, *mut c_char, *mut c_char)>,@@ -511,6 +597,31 @@ pub unsafe extern "C" fn sy_run_next_async(})}+/// Advances a run to its next step or returns NULL output when the runtime goes idle.+/// Retry idle reads to advance timers and work waiting on other runs.+/// Dropping an idle read preserves the stream and assigns no call ID.+/// # Safety+/// `run` must be live and `out_json` writable. Free output with `sy_string_free`.+#[cfg(target_arch = "wasm32")]+#[unsafe(no_mangle)]+pub unsafe extern "C" fn sy_run_next(run: *const SyRun, out_json: *mut *mut c_char) -> *mut c_char {+ let result = catch_unwind(AssertUnwindSafe(|| -> Result<()> {+ let run = unsafe { run.as_ref() }.ok_or("missing run")?;+ let out = unsafe { out_json.as_mut() }.ok_or("missing step output")?;+ *out = std::ptr::null_mut();+ let read = run.state.begin_read()?;+ if let Some(output) = run.engine.runtime.block_on(idle::until_idle(read.next())) {+ *out = output?.into_raw();+ }+ Ok(())+ }));+ match result {+ Ok(Ok(())) => std::ptr::null_mut(),+ Ok(Err(error)) => error_string(error.to_string()),+ Err(_) => error_string("Switchyard binding panicked".to_string()),+ }+}+/// Delivers a buffered host reply to its pending call./// # Safety/// The run must remain live and JSON must be NUL-terminated. May overlap an async read.