Skip to main content

yoagent_workers/
lib.rs

1//! Run [yoagent](https://docs.rs/yoagent) on Cloudflare Workers through the
2//! Worker's own bindings.
3//!
4//! yoagent itself builds for `wasm32-unknown-unknown` with
5//! `default-features = false` and reaches every provider over the host's
6//! `fetch`. This crate adds what only a Worker has: the objects the Workers
7//! runtime hands it in `env`, already authenticated as the account, so no API
8//! token is needed (and, per Cloudflare, faster and less restricted than the
9//! REST API).
10//!
11//! - `ai`: the Workers AI binding (`env.AI`) as a yoagent `DecisionBackend`,
12//!   with presets for Cloudflare's Clef decision models.
13//!
14//! ```toml
15//! # wrangler.toml
16//! [ai]
17//! binding = "AI"
18//! ```
19//!
20//! ```ignore
21//! // In a workers-rs fetch handler (a raw wasm-bindgen Worker passes its
22//! // `env.AI` JsValue the same way). `worker::Error` has no conversion from
23//! // `DecisionError`, hence the `map_err`.
24//! let clef = yoagent_workers::ai::clef(env.ai("AI")?);
25//! let urgent = clef
26//!     .noul("Checkout fails for every customer.", "Is this urgent?")
27//!     .await
28//!     .map_err(|e| worker::Error::RustError(e.to_string()))?;
29//! ```
30//!
31//! A binding belongs to one request: build the decision model (and the agent
32//! that uses it) inside the handler, from that request's `env`.
33//!
34//! Everything here exists only on `wasm32`; on other targets the crate is
35//! empty. yoagent's own core never depends on this crate.
36
37#[cfg(target_arch = "wasm32")]
38pub mod ai;