use async_trait::async_trait;
use js_sys::{Function, Promise, Reflect, JSON};
use wasm_bindgen::{JsCast, JsValue};
use wasm_bindgen_futures::JsFuture;
use yoagent::decision::{
parse_systemone_response, Capabilities, DecisionBackend, DecisionError, DecisionModel,
Evaluation, QuestionKind, Request,
};
use yoagent::retry::RetryConfig;
pub const CLEF: &str = "@cf/cloudflare/clef";
pub const CLEF_FLASH: &str = "@cf/cloudflare/clef-flash";
const PRICE_PROVIDER: &str = "cloudflare";
const OUT_OF_CAPACITY: u32 = 3040;
const DAILY_LIMIT: u32 = 3036;
#[derive(Clone, Debug)]
pub struct AiBackend {
ai: JsValue,
model_path: String,
capabilities: Capabilities,
retry: RetryConfig,
}
impl AiBackend {
pub fn new(ai: impl Into<JsValue>, model_path: impl Into<String>) -> Self {
Self {
ai: ai.into(),
model_path: model_path.into(),
capabilities: Capabilities::new(QuestionKind::all())
.with_token_limits(Some(64_000), Some(32_000)),
retry: RetryConfig::default(),
}
}
pub fn with_capabilities(mut self, capabilities: Capabilities) -> Self {
self.capabilities = capabilities;
self
}
pub fn with_retry(mut self, retry: RetryConfig) -> Self {
self.retry = retry;
self
}
pub fn model_path(&self) -> &str {
&self.model_path
}
async fn run_once(&self, input: &JsValue) -> Result<serde_json::Value, DecisionError> {
let run = Reflect::get(&self.ai, &JsValue::from_str("run"))
.ok()
.and_then(|f| f.dyn_into::<Function>().ok())
.ok_or_else(|| {
DecisionError::Invalid(
"the AI binding has no `run` method: pass the Worker's `env.AI`".into(),
)
})?;
let returned = run
.call2(&self.ai, &JsValue::from_str(&self.model_path), input)
.map_err(thrown)?;
let output = JsFuture::from(Promise::resolve(&returned))
.await
.map_err(thrown)?;
let text = JSON::stringify(&output)
.ok()
.and_then(|s| s.as_string())
.ok_or_else(|| {
DecisionError::BadResponse(format!(
"the AI binding returned no JSON value: {}",
describe(&output)
))
})?;
serde_json::from_str(&text).map_err(|e| {
DecisionError::BadResponse(format!("the AI binding's output is not JSON: {e}"))
})
}
}
#[async_trait(?Send)]
impl DecisionBackend for AiBackend {
fn capabilities(&self) -> Capabilities {
self.capabilities.clone()
}
async fn evaluate(&self, request: &Request) -> Result<Evaluation, DecisionError> {
let body = serde_json::to_string(request)
.map_err(|e| DecisionError::Invalid(format!("request does not serialize: {e}")))?;
let input = JSON::parse(&body).map_err(|e| {
DecisionError::Invalid(format!("request is not valid JSON: {}", describe(&e)))
})?;
let mut attempt = 0usize;
let value = loop {
match self.run_once(&input).await {
Err(e) if e.is_retryable() && attempt < self.retry.max_retries => {
attempt += 1;
yoagent::rt::sleep(self.retry.delay_for_attempt(attempt)).await;
}
other => break other?,
}
};
parse_systemone_response(value, request)
}
}
pub fn clef(ai: impl Into<JsValue>) -> DecisionModel {
model(ai, CLEF, "clef")
}
pub fn clef_flash(ai: impl Into<JsValue>) -> DecisionModel {
model(ai, CLEF_FLASH, "clef-flash")
}
fn model(ai: impl Into<JsValue>, path: &str, id: &str) -> DecisionModel {
use yoagent::provider::prices::global;
let cost = global::resolved().cost(PRICE_PROVIDER, id);
if cost.is_none() && global::pricing_enabled() {
tracing::warn!("no price for {PRICE_PROVIDER}/{id} in the price table; {id} is unpriced");
}
DecisionModel::from_backend(AiBackend::new(ai, path), id).with_cost(cost)
}
fn thrown(e: JsValue) -> DecisionError {
let message = format!("Workers AI: {}", describe(&e));
match leading_code(&message["Workers AI: ".len()..]) {
Some(OUT_OF_CAPACITY) => DecisionError::rate_limited_with_body(429, None, message),
Some(DAILY_LIMIT) => DecisionError::http(429, message),
_ => DecisionError::backend(message),
}
}
fn leading_code(message: &str) -> Option<u32> {
let rest = match message.split_once(": ") {
Some((name, rest)) if !name.starts_with(|c: char| c.is_ascii_digit()) => rest,
_ => message,
};
let (code, _) = rest.split_once(':')?;
(code.len() == 4).then(|| code.parse().ok()).flatten()
}
fn describe(v: &JsValue) -> String {
if let Some(e) = v.dyn_ref::<js_sys::Error>() {
let name = String::from(e.name());
let message = String::from(e.message());
return if name.is_empty() || name == "Error" {
message
} else {
format!("{name}: {message}")
};
}
if let Some(s) = v.as_string() {
return s;
}
JSON::stringify(v)
.ok()
.and_then(|s| s.as_string())
.unwrap_or_else(|| format!("{v:?}"))
}