Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
76 changes: 76 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
59 changes: 54 additions & 5 deletions src/plugins/ai_chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<serde_json::Value, String> {
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());
Expand Down