diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 51076e2..1ff419b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -98,9 +98,38 @@ jobs: - run: cargo clippy --locked --workspace --all-targets -- -D warnings - run: cargo test --locked --workspace + check-wasm32: + name: Check wasm32 + needs: changes + if: needs.changes.outputs.rust == 'true' + runs-on: ubuntu-latest + defaults: + run: + working-directory: ed + steps: + - uses: actions/checkout@v6 + with: + path: ed + - name: Read Rust toolchain + run: | + rust_toolchain="$(sed -n 's/^channel = "\(.*\)"/\1/p' rust-toolchain.toml | head -n 1)" + test -n "$rust_toolchain" + echo "RUST_TOOLCHAIN=$rust_toolchain" >> "$GITHUB_ENV" + - uses: dtolnay/rust-toolchain@stable + with: + toolchain: ${{ env.RUST_TOOLCHAIN }} + targets: wasm32-unknown-unknown + components: clippy + - uses: Swatinem/rust-cache@v2 + with: + workspaces: ed + key: wasm32 + - run: cargo check --locked --target wasm32-unknown-unknown -p ed-agent --all-targets + - run: cargo clippy --locked --target wasm32-unknown-unknown -p ed-agent --all-targets -- -D warnings + ci: name: CI status - needs: [changes, actionlint, check] + needs: [changes, actionlint, check, check-wasm32] if: always() runs-on: ubuntu-latest steps: diff --git a/Cargo.lock b/Cargo.lock index fc3becb..a217efd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1725,17 +1725,6 @@ dependencies = [ "serde_core", ] -[[package]] -name = "derive-new" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2cdc8d50f426189eef89dac62fabfa0abb27d5cc008f25bf4156a0203325becc" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.119", -] - [[package]] name = "derive_builder" version = "0.20.2" @@ -2065,6 +2054,7 @@ dependencies = [ "serde_json", "thiserror 2.0.21", "tokio", + "web-time", ] [[package]] @@ -4488,6 +4478,16 @@ dependencies = [ "paste", ] +[[package]] +name = "metrics" +version = "0.24.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89550ee9f79e88fef3119de263694973a8adb26c21d75322164fb8c493039fe2" +dependencies = [ + "portable-atomic", + "rapidhash", +] + [[package]] name = "mime" version = "0.3.17" @@ -4510,6 +4510,7 @@ version = "2.24.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86886cf6dbf4e614b19c9a1eec9775f021869d7eadde0fc73921a81b90c9b4c9" dependencies = [ + "indexmap 2.14.2", "memo-map", "serde", "serde_json", @@ -5153,9 +5154,9 @@ checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" [[package]] name = "onde" -version = "1.3.2" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc87dc77609c5ed63840b263bc548acf50c1a6992a0a0834c032482e3cddc63b" +checksum = "8eb18dc9ca66ea6a7e45e06b2fcc83c1f6b58a705641afe320d563a06dab77be" dependencies = [ "anyhow", "cc", @@ -5225,9 +5226,9 @@ dependencies = [ [[package]] name = "onde-mistralrs" -version = "0.9.3" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "689f038ebc5d6d11e4880a24e50d62f91bbf21358b92c9b5cf7bf5e65af232bd" +checksum = "0d9f41269feaffb3e452022f04a9cfb95859ed950889b6c1d010391531847260" dependencies = [ "anyhow", "clap 4.6.7", @@ -5253,9 +5254,9 @@ dependencies = [ [[package]] name = "onde-mistralrs-audio" -version = "0.9.3" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71b9a72504697c5b91e1d0448d67883e8d3046ad060200610c0484aef5dfaee5" +checksum = "787f40176eda80bdef527227ba43a7d4dac3287a3d7e28af5b4072b92abee042" dependencies = [ "anyhow", "apodize", @@ -5265,9 +5266,9 @@ dependencies = [ [[package]] name = "onde-mistralrs-code-exec" -version = "0.9.3" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4016265a19fa51ad973e99a6746904e432d3140b5c2ca14a41077edc6b8648c2" +checksum = "b3851924d7b5dfceaa8309ada9d04d983a4b6cf8761117145e1a035011e4078f" dependencies = [ "anyhow", "base64 0.22.1", @@ -5285,9 +5286,9 @@ dependencies = [ [[package]] name = "onde-mistralrs-core" -version = "0.9.3" +version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e9266eb62ceb5a9cf8fb5342e6a9b78e286f47b670762559ee800a5a2db5132" +checksum = "40fc05573a7a15f901856e1b0c6341282693156cffbc05c6dfa9b9d49c0570eb" dependencies = [ "ahash", "akin", @@ -5304,9 +5305,9 @@ dependencies = [ "chrono", "clap 4.6.7", "csv", - "derive-new", "derive_more", "dirs 6.0.0", + "dispatch2", "either", "float8", "futures", @@ -5325,6 +5326,7 @@ dependencies = [ "libc", "llguidance", "lrtable", + "metrics", "mime_guess", "minijinja", "minijinja-contrib", @@ -5336,11 +5338,11 @@ dependencies = [ "onde-mistralrs-audio", "onde-mistralrs-code-exec", "onde-mistralrs-mcp", + "onde-mistralrs-metal-compile", "onde-mistralrs-paged-attn", "onde-mistralrs-quant", "onde-mistralrs-sandbox", "onde-mistralrs-vision", - "onde-tqdm", "openai-harmony", "ordered-float", "parking_lot", @@ -5364,10 +5366,12 @@ dependencies = [ "serde-saphyr", "serde_json", "serde_plain", + "sha2", "statrs", "strum 0.27.2", "symphonia", "sysinfo", + "tempfile", "thiserror 2.0.21", "tokenizers 0.21.4", "tokio", @@ -5376,6 +5380,7 @@ dependencies = [ "toktrie", "toktrie_hf_tokenizers", "toml 0.9.12+spec-1.1.0", + "tqdm", "tracing", "tracing-subscriber", "urlencoding", @@ -5386,9 +5391,9 @@ dependencies = [ [[package]] name = "onde-mistralrs-macros" -version = "0.9.3" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e624ab1f0896a07ff437f4c49050c891918c0f01a008113c788b64e5f76c1d9" +checksum = "8dd60c4cb0dc37105b335451643d075af0d293b8203df9bb038e88d83df8e475" dependencies = [ "darling 0.23.0", "proc-macro2", @@ -5398,9 +5403,9 @@ dependencies = [ [[package]] name = "onde-mistralrs-mcp" -version = "0.9.3" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8bcb140ed84c4dd2683f1533fc58905f959e7394843bc02a315bfdd3ffcbc25" +checksum = "f5b5bf129c896c975a98575b9752291bb47b38a5bd3170944a8c171cc491af1b" dependencies = [ "anyhow", "async-trait", @@ -5420,9 +5425,9 @@ dependencies = [ [[package]] name = "onde-mistralrs-metal-compile" -version = "0.9.3" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44109415be2971d311b403095d3c278443210d0ccc1c383a84ad44da5f5b1abf" +checksum = "02e15b1154394bc9750f69c52b55de0295e6f7d3a3f3151f9ad8d88e066629af" dependencies = [ "candle-metal-kernels", "objc2-metal", @@ -5430,9 +5435,9 @@ dependencies = [ [[package]] name = "onde-mistralrs-paged-attn" -version = "0.9.3" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f93c8c04d15ebce014e80675387d269bcead85be063bf9afce8b353d64e61b8" +checksum = "9ba6ae15cc8ce43c949fd038a8df0858e41dca38e4b4b77925aa04e6dc4aee84" dependencies = [ "anyhow", "candle-metal-kernels", @@ -5448,17 +5453,20 @@ dependencies = [ [[package]] name = "onde-mistralrs-quant" -version = "0.9.3" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d4ed48af14e3e5ff318a6ea6cefbc1e50ee753f143bd1ae6c0f188c33033d5a" +checksum = "a25f0259f21cf04d86f18b3b0f48b072bcc4a989cadae513ca142ab1ccb0c60a" dependencies = [ + "bytemuck", "byteorder", "candle-metal-kernels", "dispatch2", + "fancy-regex 0.14.0", "float8", "futures", "half", "hf-hub", + "indexmap 2.14.2", "indicatif 0.18.6", "lazy_static", "libc", @@ -5483,9 +5491,9 @@ dependencies = [ [[package]] name = "onde-mistralrs-sandbox" -version = "0.9.3" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77845cc6e16e3b1eebed11d3ba6a3a56785f0031421bf46d1deefbe281aaa19e" +checksum = "93b8ba017e502f2b7f2da67ced422ae73a7ea2494759b8c7a8e5fa38b341b3f5" dependencies = [ "anyhow", "itoa", @@ -5501,26 +5509,15 @@ dependencies = [ [[package]] name = "onde-mistralrs-vision" -version = "0.9.3" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cdc7ae244c416860838e9cb3ff5cd8ae8acf474f48f2fe15d45bc5491215760" +checksum = "f231953141403eb6a825d6c247dffd7aa2e99b01b7a70d075bb72533c8dc10e8" dependencies = [ "image", "onde-candle-core", "rayon", ] -[[package]] -name = "onde-tqdm" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d96f4fcb4af06959bf9c9b81ace0502e2be2d11d7e037c94f3f7851bde720ea" -dependencies = [ - "anyhow", - "crossterm", - "once_cell", -] - [[package]] name = "onig" version = "6.5.3" @@ -6339,6 +6336,15 @@ dependencies = [ "rand_core 0.10.1", ] +[[package]] +name = "rapidhash" +version = "4.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5da7e78a036ce858e8d55b7e7dc8ba3a88b78350fd2155d3591bbd966b58589e" +dependencies = [ + "rustversion", +] + [[package]] name = "ratatui" version = "0.30.2" @@ -9008,6 +9014,17 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" +[[package]] +name = "tqdm" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a882329b482cea1d2762e9b9ff1ed0f0327a1ec3a9ba7b8e8c4e3635908a4ae5" +dependencies = [ + "anyhow", + "crossterm", + "once_cell", +] + [[package]] name = "tracing" version = "0.1.44" diff --git a/Cargo.toml b/Cargo.toml index 2243b9c..737d364 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,9 +27,11 @@ homepage = "https://ondeinference.com" [workspace.dependencies] # The inference engine Ed wraps, resolved from crates.io like any dependency -# (there is no path override to a sibling checkout). 1.3.2 is the first release -# with ChatEngine::complete, which ed-acp's local provider calls. -onde = { version = "1.3.2" } +# (there is no path override to a sibling checkout). 1.4.0 is the first release +# whose wasm32 stub engine matches the real engine's signatures, so ed-agent +# compiles for wasm32-unknown-unknown. ChatEngine::complete, which ed-acp's +# local provider calls, arrived in 1.3.2. +onde = { version = "1.4.0" } async-trait = "0.1" serde = { version = "1", features = ["derive"] } serde_json = "1" diff --git a/crates/ed-agent/Cargo.toml b/crates/ed-agent/Cargo.toml index e7cf2d5..650e57e 100644 --- a/crates/ed-agent/Cargo.toml +++ b/crates/ed-agent/Cargo.toml @@ -20,6 +20,8 @@ log.workspace = true thiserror.workspace = true tokio.workspace = true futures-core = "0.3" +# std::time::Instant::now() panics on wasm32-unknown-unknown; web-time re-exports std on native. +web-time = "1" [dev-dependencies] tokio = { version = "1", features = ["macros", "rt", "sync"] } diff --git a/crates/ed-agent/src/ed.rs b/crates/ed-agent/src/ed.rs index 1860237..c1a4b16 100644 --- a/crates/ed-agent/src/ed.rs +++ b/crates/ed-agent/src/ed.rs @@ -4,7 +4,7 @@ use std::collections::{HashMap, HashSet}; use std::future::Future; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; -use std::time::{Duration, Instant}; +use std::time::Duration; use log::{error, info}; use onde::inference::types::{format_duration, ChatMessage}; @@ -12,6 +12,7 @@ use onde::inference::{ ChatEngine, EngineInfo, EngineStatus, GgufModelConfig, InferenceError, SamplingConfig, ToolCallingSupport, ToolDefinition, ToolResult, }; +use web_time::Instant; use crate::{ stream::chunk_error, tool::truncate_output, AgentConfig, AgentError, AgentReply, @@ -653,7 +654,11 @@ mod tests { struct CountingExecutor(AtomicUsize); - #[async_trait::async_trait] + #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), async_trait::async_trait(?Send))] + #[cfg_attr( + not(all(target_arch = "wasm32", target_os = "unknown")), + async_trait::async_trait + )] impl ToolExecutor for CountingExecutor { async fn execute(&self, tool_name: &str, _: &str) -> Result { self.0.fetch_add(1, Ordering::Relaxed); @@ -663,7 +668,11 @@ mod tests { struct FixedApproval(ApprovalDecision); - #[async_trait::async_trait] + #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), async_trait::async_trait(?Send))] + #[cfg_attr( + not(all(target_arch = "wasm32", target_os = "unknown")), + async_trait::async_trait + )] impl ApprovalHandler for FixedApproval { async fn approve(&self, _: ApprovalRequest) -> ApprovalDecision { self.0 diff --git a/crates/ed-agent/src/lib.rs b/crates/ed-agent/src/lib.rs index 0a78ec8..f199352 100644 --- a/crates/ed-agent/src/lib.rs +++ b/crates/ed-agent/src/lib.rs @@ -23,9 +23,10 @@ //! Ed has no `cfg(target_os)` gates. `onde` already splits real and fallback //! implementations internally, so this crate compiles on every platform `onde` //! supports (macOS, iOS, tvOS, visionOS, watchOS, Windows, Linux, Android) and -//! simply surfaces `onde`'s error where it can't run inference. Outside that -//! list `onde` falls back to a stub whose signatures differ, so Ed does not -//! build there either. +//! simply surfaces `onde`'s error where it can't run inference. On +//! `wasm32-unknown-unknown` it builds against `onde`'s web target. Other +//! targets outside that list get a stub engine with the same signatures, +//! whose calls all return an "unsupported" error. //! //! Do not add platform gates here, and be careful about adding them in a host: //! a gate narrower than `onde`'s own support list silently disables chat on a @@ -49,6 +50,7 @@ #![forbid(unsafe_code)] mod ed; +pub mod maybe; mod reply; mod session; mod sink; diff --git a/crates/ed-agent/src/maybe.rs b/crates/ed-agent/src/maybe.rs new file mode 100644 index 0000000..5f427c2 --- /dev/null +++ b/crates/ed-agent/src/maybe.rs @@ -0,0 +1,36 @@ +//! Thread-safety bounds that relax on browser wasm. +//! +//! The host-facing seams ([`ToolExecutor`](crate::ToolExecutor), +//! [`ApprovalHandler`](crate::ApprovalHandler), [`EventSink`](crate::EventSink), +//! [`SessionStore`](crate::SessionStore)) must be `Send + Sync` on native +//! targets, where a turn can hop between runtime threads. In a browser the +//! agent lives on one JS thread and its implementations hold `JsValue`s, which +//! are `!Send`. These aliases resolve to exactly `Send` / `Sync` everywhere +//! except `wasm32-unknown-unknown`, so native signatures are unchanged. Other +//! wasm targets (`wasm32-wasip1-threads`, Emscripten with pthreads) can run +//! threads, so they keep the real bounds. It is the same pattern +//! wgpu uses (`WasmNotSend`). +//! +//! Soundness rests on the single-thread guarantee: nothing holding a `JsValue` +//! may be handed to another thread. Do not paper over a `!Send` type with +//! `unsafe impl Send`. + +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] +pub trait MaybeSend: Send {} +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] +impl MaybeSend for T {} + +#[cfg(all(target_arch = "wasm32", target_os = "unknown"))] +pub trait MaybeSend {} +#[cfg(all(target_arch = "wasm32", target_os = "unknown"))] +impl MaybeSend for T {} + +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] +pub trait MaybeSync: Sync {} +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] +impl MaybeSync for T {} + +#[cfg(all(target_arch = "wasm32", target_os = "unknown"))] +pub trait MaybeSync {} +#[cfg(all(target_arch = "wasm32", target_os = "unknown"))] +impl MaybeSync for T {} diff --git a/crates/ed-agent/src/session.rs b/crates/ed-agent/src/session.rs index a6c1440..a078ea8 100644 --- a/crates/ed-agent/src/session.rs +++ b/crates/ed-agent/src/session.rs @@ -5,7 +5,7 @@ //! and hands the messages to [`Ed::restore_history`](crate::Ed::restore_history) //! when the user switches conversation. -use async_trait::async_trait; +use crate::maybe::{MaybeSend, MaybeSync}; use onde::inference::types::ChatMessage; use serde::{Deserialize, Serialize}; @@ -34,8 +34,12 @@ pub struct SessionError(pub String); /// messages if it does. /// - `delete` of an unknown id succeeds. /// - `list` is ordered most recently updated first. -#[async_trait] -pub trait SessionStore: Send + Sync + 'static { +#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), async_trait::async_trait(?Send))] +#[cfg_attr( + not(all(target_arch = "wasm32", target_os = "unknown")), + async_trait::async_trait +)] +pub trait SessionStore: MaybeSend + MaybeSync + 'static { async fn list(&self) -> Result, SessionError>; async fn load(&self, id: &str) -> Result>, SessionError>; async fn save(&self, id: &str, messages: &[ChatMessage]) -> Result<(), SessionError>; @@ -70,7 +74,11 @@ mod tests { } } - #[async_trait] + #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), async_trait::async_trait(?Send))] + #[cfg_attr( + not(all(target_arch = "wasm32", target_os = "unknown")), + async_trait::async_trait + )] impl SessionStore for MemoryStore { async fn list(&self) -> Result, SessionError> { let mut metas: Vec<_> = self diff --git a/crates/ed-agent/src/sink.rs b/crates/ed-agent/src/sink.rs index 4069bdd..d5eaa37 100644 --- a/crates/ed-agent/src/sink.rs +++ b/crates/ed-agent/src/sink.rs @@ -2,6 +2,7 @@ use onde::inference::EngineStatus; +use crate::maybe::{MaybeSend, MaybeSync}; use crate::{AgentReply, ApprovalRequest, ChatReply, ToolCall, ToolExecutionResult}; /// Receives lifecycle transitions and completed replies. @@ -15,9 +16,9 @@ use crate::{AgentReply, ApprovalRequest, ChatReply, ToolCall, ToolExecutionResul /// /// Both methods are synchronous and should stay cheap — they are called while /// Ed is mid-operation. Hand off to a channel or an event bus rather than -/// blocking. Implementations must be `Send + Sync` because a load or a send +/// blocking. Implementations must be `Send + Sync` (on `wasm32-unknown-unknown`, where the agent is single-threaded, that relaxes to nothing; see [`maybe`](crate::maybe)) because a load or a send /// may be driven from any task. -pub trait EventSink: Send + Sync + 'static { +pub trait EventSink: MaybeSend + MaybeSync + 'static { /// The engine moved to a new lifecycle state. /// /// `model_name` is the model involved where one is known, and `error` diff --git a/crates/ed-agent/src/tool.rs b/crates/ed-agent/src/tool.rs index 526663b..b4b6f05 100644 --- a/crates/ed-agent/src/tool.rs +++ b/crates/ed-agent/src/tool.rs @@ -1,6 +1,6 @@ //! Host-defined tools and the policies that guard them. -use async_trait::async_trait; +use crate::maybe::{MaybeSend, MaybeSync}; use serde::{Deserialize, Serialize}; /// A tool the model may ask the host application to run. @@ -105,20 +105,32 @@ pub enum ApprovalDecision { Deny, } -#[async_trait] -pub trait ToolExecutor: Send + Sync + 'static { +#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), async_trait::async_trait(?Send))] +#[cfg_attr( + not(all(target_arch = "wasm32", target_os = "unknown")), + async_trait::async_trait +)] +pub trait ToolExecutor: MaybeSend + MaybeSync + 'static { async fn execute(&self, tool_name: &str, arguments: &str) -> Result; } -#[async_trait] -pub trait ApprovalHandler: Send + Sync + 'static { +#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), async_trait::async_trait(?Send))] +#[cfg_attr( + not(all(target_arch = "wasm32", target_os = "unknown")), + async_trait::async_trait +)] +pub trait ApprovalHandler: MaybeSend + MaybeSync + 'static { async fn approve(&self, request: ApprovalRequest) -> ApprovalDecision; } #[derive(Debug, Default)] pub struct RejectingExecutor; -#[async_trait] +#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), async_trait::async_trait(?Send))] +#[cfg_attr( + not(all(target_arch = "wasm32", target_os = "unknown")), + async_trait::async_trait +)] impl ToolExecutor for RejectingExecutor { async fn execute(&self, tool_name: &str, _: &str) -> Result { Err(format!("no executor is registered for tool {tool_name:?}")) @@ -128,7 +140,11 @@ impl ToolExecutor for RejectingExecutor { #[derive(Debug, Default)] pub struct DenyApprovals; -#[async_trait] +#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), async_trait::async_trait(?Send))] +#[cfg_attr( + not(all(target_arch = "wasm32", target_os = "unknown")), + async_trait::async_trait +)] impl ApprovalHandler for DenyApprovals { async fn approve(&self, _: ApprovalRequest) -> ApprovalDecision { ApprovalDecision::Deny