diff --git a/CHANGELOG.md b/CHANGELOG.md index f41249d..3301e0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file. - **GitHub Pages Deployment** — added a GitHub Actions workflow that builds the WASM release, generates `wasm-bindgen` web bindings, uploads the static bundle, and deploys it to GitHub Pages. - **Browser File Picker** — image attachments now use `rfd` file handles in the browser and read selected files directly from the Web File API. - **Browser AI Chat** — the WASM build can now send AI chat requests from the browser using direct HTTP calls for Anthropic, OpenAI-compatible providers, Gemini, Cohere, and Ollama, while reusing existing code application, verification, and error recovery. +- **Browser AI Retries** — browser AI requests now retry rate-limited (429), overloaded (529) and transient 5xx responses for up to three attempts, waiting out the provider's `Retry-After` (up to 60 s) or a jittered backoff from the `llm-wasm` retry policy instead of failing on the first response. - **Compiler Benchmark** — added a reusable benchmark for the default scene with per-phase timing, selectable scenes and iteration counts, plus optional Hyper dispatch tracing for investigating compiler performance. ### Changed diff --git a/Cargo.lock b/Cargo.lock index 9772de6..dc6c9cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1582,6 +1582,15 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" +[[package]] +name = "block-buffer" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa" +dependencies = [ + "hybrid-array", +] + [[package]] name = "block2" version = "0.5.1" @@ -2070,6 +2079,15 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" +[[package]] +name = "crypto-common" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" +dependencies = [ + "hybrid-array", +] + [[package]] name = "csgrs" version = "0.23.0" @@ -2217,6 +2235,16 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "digest" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "dirs" version = "6.0.0" @@ -3270,6 +3298,15 @@ version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" +[[package]] +name = "hybrid-array" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27f864f10dfb56725ce5ce5472bc52252c8f93a4ab86327122cebf62c5f59a17" +dependencies = [ + "typenum", +] + [[package]] name = "hyper" version = "1.10.1" @@ -3980,6 +4017,19 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "11d3d7f243d5c5a8b9bb5d6dd2b1602c0cb0b9db1621bafc7ed66e35ff9fe092" +[[package]] +name = "llm-wasm" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f9369a4b863665544fdf5f3b84b6b8933af3a88b261bdee6d84d2f3eead237f" +dependencies = [ + "lru", + "serde", + "serde_json", + "sha2", + "thiserror 2.0.18", +] + [[package]] name = "lock_api" version = "0.4.14" @@ -4038,6 +4088,12 @@ dependencies = [ "imgref", ] +[[package]] +name = "lru" +version = "0.18.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef9ac18847474e638e3702b76c65d4eb93428471a74778ef0f1be711717f89b5" + [[package]] name = "lru-slab" version = "0.1.2" @@ -6006,6 +6062,17 @@ dependencies = [ "syn", ] +[[package]] +name = "sha2" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "sharded-slab" version = "0.1.7" @@ -6291,7 +6358,9 @@ dependencies = [ "hypermesh", "hyperreal", "image", + "js-sys", "lib3mf", + "llm-wasm", "open", "openscad-rs", "reqwest", @@ -6302,6 +6371,7 @@ dependencies = [ "ttf-parser 0.25.1", "url", "wasm-bindgen-futures", + "web-sys", "web-time", "winresource", ] @@ -6880,6 +6950,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc7d623258602320d5c55d1bc22793b57daff0ec7efc270ea7d55ce1d5f5471c" +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + [[package]] name = "typewit" version = "1.15.2" diff --git a/Cargo.toml b/Cargo.toml index 78007ba..35c3c2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,7 +49,10 @@ tokio = { version = "1", features = ["rt-multi-thread", "macros", "sync", "proce [target.'cfg(target_arch = "wasm32")'.dependencies] getrandom = { version = "0.3", features = ["wasm_js"] } +js-sys = "0.3" +llm-wasm = "0.3" wasm-bindgen-futures = "0.4" +web-sys = { version = "0.3", features = ["Window"] } [target.'cfg(windows)'.build-dependencies] winresource = "0.1" diff --git a/src/plugins/ai_chat.rs b/src/plugins/ai_chat.rs index aae89ac..c0fcfc7 100644 --- a/src/plugins/ai_chat.rs +++ b/src/plugins/ai_chat.rs @@ -793,15 +793,64 @@ async fn fetch_model_names( } } +/// Longest server-requested `Retry-After` the browser build waits out before +/// reporting the error instead. +#[cfg(target_arch = "wasm32")] +const MAX_RETRY_AFTER_MS: u32 = 60_000; + +#[cfg(target_arch = "wasm32")] +async fn sleep_ms(ms: u32) { + let promise = js_sys::Promise::new(&mut |resolve, _reject| { + let scheduled = web_sys::window().and_then(|window| { + window + .set_timeout_with_callback_and_timeout_and_arguments_0( + &resolve, + i32::try_from(ms).unwrap_or(i32::MAX), + ) + .ok() + }); + if scheduled.is_none() { + let _ = resolve.call0(&js_sys::global()); + } + }); + let _ = wasm_bindgen_futures::JsFuture::from(promise).await; +} + #[cfg(target_arch = "wasm32")] async fn fetch_json( - request: reqwest::RequestBuilder, + mut request: reqwest::RequestBuilder, label: &str, ) -> Result { - let response = request - .send() - .await - .map_err(|e| format!("{label} request failed. Browser CORS policy may require a proxy or CORS-enabled custom endpoint. Details: {e}"))?; + // Rate limits (429), Anthropic overload (529) and transient 5xx responses + // are retried with jittered backoff, honouring the server's Retry-After. + let policy = llm_wasm::retry::RetryPolicy::new(3, 500, 8_000) + .unwrap_or_else(|_| llm_wasm::retry::RetryPolicy::exponential()); + let mut attempt = 1; + let response = loop { + let retry_request = request.try_clone(); + let response = request + .send() + .await + .map_err(|e| format!("{label} request failed. Browser CORS policy may require a proxy or CORS-enabled custom endpoint. Details: {e}"))?; + + let status = response.status().as_u16(); + let retryable = policy.should_retry(attempt, status) + || (status == 529 && attempt < policy.max_attempts()); + let Some(next_request) = retry_request.filter(|_| retryable) else { + break response; + }; + let retry_after = response + .headers() + .get(reqwest::header::RETRY_AFTER) + .and_then(|value| value.to_str().ok()); + let delay = policy.delay_for_response(attempt, retry_after, js_sys::Math::random()); + if delay > MAX_RETRY_AFTER_MS { + break response; + } + sleep_ms(delay).await; + request = next_request; + attempt += 1; + }; if response.status() == reqwest::StatusCode::UNAUTHORIZED { return Err("Unauthorized (401). Please check your API key.".into());