1use async_trait::async_trait;
11use js_sys::{Function, Promise, Reflect, JSON};
12use wasm_bindgen::{JsCast, JsValue};
13use wasm_bindgen_futures::JsFuture;
14use yoagent::decision::{
15 parse_systemone_response, Capabilities, DecisionBackend, DecisionError, DecisionModel,
16 Evaluation, QuestionKind, Request,
17};
18use yoagent::retry::RetryConfig;
19
20pub const CLEF: &str = "@cf/cloudflare/clef";
22pub const CLEF_FLASH: &str = "@cf/cloudflare/clef-flash";
24
25const PRICE_PROVIDER: &str = "cloudflare";
27
28const OUT_OF_CAPACITY: u32 = 3040;
34const DAILY_LIMIT: u32 = 3036;
35
36#[derive(Clone, Debug)]
52pub struct AiBackend {
53 ai: JsValue,
54 model_path: String,
55 capabilities: Capabilities,
56 retry: RetryConfig,
57}
58
59impl AiBackend {
60 pub fn new(ai: impl Into<JsValue>, model_path: impl Into<String>) -> Self {
68 Self {
69 ai: ai.into(),
70 model_path: model_path.into(),
71 capabilities: Capabilities::new(QuestionKind::all())
72 .with_token_limits(Some(64_000), Some(32_000)),
73 retry: RetryConfig::default(),
74 }
75 }
76
77 pub fn with_capabilities(mut self, capabilities: Capabilities) -> Self {
79 self.capabilities = capabilities;
80 self
81 }
82
83 pub fn with_retry(mut self, retry: RetryConfig) -> Self {
86 self.retry = retry;
87 self
88 }
89
90 pub fn model_path(&self) -> &str {
92 &self.model_path
93 }
94
95 async fn run_once(&self, input: &JsValue) -> Result<serde_json::Value, DecisionError> {
96 let run = Reflect::get(&self.ai, &JsValue::from_str("run"))
97 .ok()
98 .and_then(|f| f.dyn_into::<Function>().ok())
99 .ok_or_else(|| {
100 DecisionError::Invalid(
101 "the AI binding has no `run` method: pass the Worker's `env.AI`".into(),
102 )
103 })?;
104 let returned = run
106 .call2(&self.ai, &JsValue::from_str(&self.model_path), input)
107 .map_err(thrown)?;
108 let output = JsFuture::from(Promise::resolve(&returned))
109 .await
110 .map_err(thrown)?;
111 let text = JSON::stringify(&output)
112 .ok()
113 .and_then(|s| s.as_string())
114 .ok_or_else(|| {
115 DecisionError::BadResponse(format!(
116 "the AI binding returned no JSON value: {}",
117 describe(&output)
118 ))
119 })?;
120 serde_json::from_str(&text).map_err(|e| {
121 DecisionError::BadResponse(format!("the AI binding's output is not JSON: {e}"))
122 })
123 }
124}
125
126#[async_trait(?Send)]
127impl DecisionBackend for AiBackend {
128 fn capabilities(&self) -> Capabilities {
129 self.capabilities.clone()
130 }
131
132 async fn evaluate(&self, request: &Request) -> Result<Evaluation, DecisionError> {
133 let body = serde_json::to_string(request)
134 .map_err(|e| DecisionError::Invalid(format!("request does not serialize: {e}")))?;
135 let input = JSON::parse(&body).map_err(|e| {
136 DecisionError::Invalid(format!("request is not valid JSON: {}", describe(&e)))
137 })?;
138 let mut attempt = 0usize;
139 let value = loop {
140 match self.run_once(&input).await {
141 Err(e) if e.is_retryable() && attempt < self.retry.max_retries => {
142 attempt += 1;
143 yoagent::rt::sleep(self.retry.delay_for_attempt(attempt)).await;
144 }
145 other => break other?,
146 }
147 };
148 parse_systemone_response(value, request)
149 }
150}
151
152pub fn clef(ai: impl Into<JsValue>) -> DecisionModel {
161 model(ai, CLEF, "clef")
162}
163
164pub fn clef_flash(ai: impl Into<JsValue>) -> DecisionModel {
168 model(ai, CLEF_FLASH, "clef-flash")
169}
170
171fn model(ai: impl Into<JsValue>, path: &str, id: &str) -> DecisionModel {
172 use yoagent::provider::prices::global;
173 let cost = global::resolved().cost(PRICE_PROVIDER, id);
174 if cost.is_none() && global::pricing_enabled() {
177 tracing::warn!("no price for {PRICE_PROVIDER}/{id} in the price table; {id} is unpriced");
178 }
179 DecisionModel::from_backend(AiBackend::new(ai, path), id).with_cost(cost)
180}
181
182fn thrown(e: JsValue) -> DecisionError {
184 let message = format!("Workers AI: {}", describe(&e));
185 match leading_code(&message["Workers AI: ".len()..]) {
186 Some(OUT_OF_CAPACITY) => DecisionError::rate_limited_with_body(429, None, message),
187 Some(DAILY_LIMIT) => DecisionError::http(429, message),
188 _ => DecisionError::backend(message),
189 }
190}
191
192fn leading_code(message: &str) -> Option<u32> {
194 let rest = match message.split_once(": ") {
195 Some((name, rest)) if !name.starts_with(|c: char| c.is_ascii_digit()) => rest,
196 _ => message,
197 };
198 let (code, _) = rest.split_once(':')?;
199 (code.len() == 4).then(|| code.parse().ok()).flatten()
200}
201
202fn describe(v: &JsValue) -> String {
205 if let Some(e) = v.dyn_ref::<js_sys::Error>() {
206 let name = String::from(e.name());
207 let message = String::from(e.message());
208 return if name.is_empty() || name == "Error" {
209 message
210 } else {
211 format!("{name}: {message}")
212 };
213 }
214 if let Some(s) = v.as_string() {
215 return s;
216 }
217 JSON::stringify(v)
218 .ok()
219 .and_then(|s| s.as_string())
220 .unwrap_or_else(|| format!("{v:?}"))
221}