Read-only mirror of github.com/ondeinference/ed.
Mirrored from https://github.com/ondeinference/ed.git
Synced Nov 23, 01:00 UTC
Add wasm-friendly seams to ed-agent
Introduce MaybeSend and MaybeSync marker traits that are Send/Sync on native targets and blanket-implemented on wasm32, switch the async_trait attributes to ?Send on wasm, and use web_time::Instant so timing works in the browser. Native behavior and public signatures are unchanged. The Platforms section of the crate docs no longer claims the onde stub has different signatures; that was fixed on the onde side. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Seto Elkahfi committed
Nov 10, 2026 at 13:38 UTC
fc653a8a94c0a5c8a39086076cb21aefc20f9b86
8 files changed
+64
-16
Cargo.lock
+1
@@ -2065,6 +2065,7 @@ dependencies = [
2065
"serde_json",
2066
"thiserror 2.0.21",
2067
"tokio",
2068
+ "web-time",
2069
]
2070
2071
[[package]]
crates/ed-agent/Cargo.toml
+2
@@ -20,6 +20,8 @@ log.workspace = true
20
thiserror.workspace = true
21
tokio.workspace = true
22
futures-core = "0.3"
23
+# std::time::Instant::now() panics on wasm32-unknown-unknown; web-time re-exports std on native.
24
+web-time = "1"
25
26
[dev-dependencies]
27
tokio = { version = "1", features = ["macros", "rt", "sync"] }
crates/ed-agent/src/ed.rs
+2
-1
@@ -4,7 +4,7 @@ use std::collections::{HashMap, HashSet};
4
use std::future::Future;
5
use std::sync::atomic::{AtomicBool, Ordering};
6
use std::sync::Arc;
7
-use std::time::{Duration, Instant};
7
+use std::time::Duration;
8
9
use log::{error, info};
10
use onde::inference::types::{format_duration, ChatMessage};
@@ -12,6 +12,7 @@ use onde::inference::{
12
ChatEngine, EngineInfo, EngineStatus, GgufModelConfig, InferenceError, SamplingConfig,
13
ToolCallingSupport, ToolDefinition, ToolResult,
14
};
15
+use web_time::Instant;
16
17
use crate::{
18
stream::chunk_error, tool::truncate_output, AgentConfig, AgentError, AgentReply,
crates/ed-agent/src/lib.rs
+5
-2
@@ -24,8 +24,10 @@
24
//! implementations internally, so this crate compiles on every platform `onde`
25
//! supports (macOS, iOS, tvOS, visionOS, watchOS, Windows, Linux, Android) and
26
//! simply surfaces `onde`'s error where it can't run inference. Outside that
27
-//! list `onde` falls back to a stub whose signatures differ, so Ed does not
28
-//! build there either.
27
+//! list (wasm32, for one) `onde` falls back to a stub engine with the same
28
+//! signatures, whose calls all return an "unsupported" error. Releases of
29
+//! `onde` up to 1.3.5 have a stub that drifted from the real engine, so Ed
30
+//! only builds on those targets against a newer release.
31
//!
32
//! Do not add platform gates here, and be careful about adding them in a host:
33
//! a gate narrower than `onde`'s own support list silently disables chat on a
@@ -49,6 +51,7 @@
51
#![forbid(unsafe_code)]
52
53
mod ed;
54
+pub mod maybe;
55
mod reply;
56
mod session;
57
mod sink;
crates/ed-agent/src/maybe.rs
new
+34
@@ -0,0 +1,34 @@
1
+//! Thread-safety bounds that relax on `wasm32`.
2
+//!
3
+//! The host-facing seams ([`ToolExecutor`](crate::ToolExecutor),
4
+//! [`ApprovalHandler`](crate::ApprovalHandler), [`EventSink`](crate::EventSink),
5
+//! [`SessionStore`](crate::SessionStore)) must be `Send + Sync` on native
6
+//! targets, where a turn can hop between runtime threads. In a browser the
7
+//! agent lives on one JS thread and its implementations hold `JsValue`s, which
8
+//! are `!Send`. These aliases resolve to exactly `Send` / `Sync` everywhere
9
+//! except `wasm32`, so native signatures are unchanged. It is the same pattern
10
+//! wgpu uses (`WasmNotSend`).
11
+//!
12
+//! Soundness rests on the single-thread guarantee: nothing holding a `JsValue`
13
+//! may be handed to another thread. Do not paper over a `!Send` type with
14
+//! `unsafe impl Send`.
15
+
16
+#[cfg(not(target_arch = "wasm32"))]
17
+pub trait MaybeSend: Send {}
18
+#[cfg(not(target_arch = "wasm32"))]
19
+impl<T: Send + ?Sized> MaybeSend for T {}
20
+
21
+#[cfg(target_arch = "wasm32")]
22
+pub trait MaybeSend {}
23
+#[cfg(target_arch = "wasm32")]
24
+impl<T: ?Sized> MaybeSend for T {}
25
+
26
+#[cfg(not(target_arch = "wasm32"))]
27
+pub trait MaybeSync: Sync {}
28
+#[cfg(not(target_arch = "wasm32"))]
29
+impl<T: Sync + ?Sized> MaybeSync for T {}
30
+
31
+#[cfg(target_arch = "wasm32")]
32
+pub trait MaybeSync {}
33
+#[cfg(target_arch = "wasm32")]
34
+impl<T: ?Sized> MaybeSync for T {}
crates/ed-agent/src/session.rs
+6
-4
@@ -5,7 +5,7 @@
5
//! and hands the messages to [`Ed::restore_history`](crate::Ed::restore_history)
6
//! when the user switches conversation.
7
8
-use async_trait::async_trait;
8
+use crate::maybe::{MaybeSend, MaybeSync};
9
use onde::inference::types::ChatMessage;
10
use serde::{Deserialize, Serialize};
11
@@ -34,8 +34,9 @@ pub struct SessionError(pub String);
34
/// messages if it does.
35
/// - `delete` of an unknown id succeeds.
36
/// - `list` is ordered most recently updated first.
37
-#[async_trait]
38
-pub trait SessionStore: Send + Sync + 'static {
37
+#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
38
+#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
39
+pub trait SessionStore: MaybeSend + MaybeSync + 'static {
40
async fn list(&self) -> Result<Vec<SessionMeta>, SessionError>;
41
async fn load(&self, id: &str) -> Result<Option<Vec<ChatMessage>>, SessionError>;
42
async fn save(&self, id: &str, messages: &[ChatMessage]) -> Result<(), SessionError>;
@@ -70,7 +71,8 @@ mod tests {
71
}
72
}
73
73
- #[async_trait]
74
+ #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
75
+ #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
76
impl SessionStore for MemoryStore {
77
async fn list(&self) -> Result<Vec<SessionMeta>, SessionError> {
78
let mut metas: Vec<_> = self
crates/ed-agent/src/sink.rs
+3
-2
@@ -2,6 +2,7 @@
2
3
use onde::inference::EngineStatus;
4
5
+use crate::maybe::{MaybeSend, MaybeSync};
6
use crate::{AgentReply, ApprovalRequest, ChatReply, ToolCall, ToolExecutionResult};
7
8
/// Receives lifecycle transitions and completed replies.
@@ -15,9 +16,9 @@ use crate::{AgentReply, ApprovalRequest, ChatReply, ToolCall, ToolExecutionResul
16
///
17
/// Both methods are synchronous and should stay cheap — they are called while
18
/// Ed is mid-operation. Hand off to a channel or an event bus rather than
18
-/// blocking. Implementations must be `Send + Sync` because a load or a send
19
+/// blocking. Implementations must be `Send + Sync` (on `wasm32`, where the agent is single-threaded, that relaxes to nothing; see [`maybe`](crate::maybe)) because a load or a send
20
/// may be driven from any task.
20
-pub trait EventSink: Send + Sync + 'static {
21
+pub trait EventSink: MaybeSend + MaybeSync + 'static {
22
/// The engine moved to a new lifecycle state.
23
///
24
/// `model_name` is the model involved where one is known, and `error`
crates/ed-agent/src/tool.rs
+11
-7
@@ -1,6 +1,6 @@
1
//! Host-defined tools and the policies that guard them.
2
3
-use async_trait::async_trait;
3
+use crate::maybe::{MaybeSend, MaybeSync};
4
use serde::{Deserialize, Serialize};
5
6
/// A tool the model may ask the host application to run.
@@ -105,20 +105,23 @@ pub enum ApprovalDecision {
105
Deny,
106
}
107
108
-#[async_trait]
109
-pub trait ToolExecutor: Send + Sync + 'static {
108
+#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
109
+#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
110
+pub trait ToolExecutor: MaybeSend + MaybeSync + 'static {
111
async fn execute(&self, tool_name: &str, arguments: &str) -> Result<String, String>;
112
}
113
113
-#[async_trait]
114
-pub trait ApprovalHandler: Send + Sync + 'static {
114
+#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
115
+#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
116
+pub trait ApprovalHandler: MaybeSend + MaybeSync + 'static {
117
async fn approve(&self, request: ApprovalRequest) -> ApprovalDecision;
118
}
119
120
#[derive(Debug, Default)]
121
pub struct RejectingExecutor;
122
121
-#[async_trait]
123
+#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
124
+#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
125
impl ToolExecutor for RejectingExecutor {
126
async fn execute(&self, tool_name: &str, _: &str) -> Result<String, String> {
127
Err(format!("no executor is registered for tool {tool_name:?}"))
@@ -128,7 +131,8 @@ impl ToolExecutor for RejectingExecutor {
131
#[derive(Debug, Default)]
132
pub struct DenyApprovals;
133
131
-#[async_trait]
134
+#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
135
+#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
136
impl ApprovalHandler for DenyApprovals {
137
async fn approve(&self, _: ApprovalRequest) -> ApprovalDecision {
138
ApprovalDecision::Deny