Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(agent-tools): don't render a screenshot through Chrome when the desktop webview fails

James Dam0ada76e13f06
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/commands.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/commands.rs
index bb8544c74..5a0bb35a7 100644
--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/commands.rs
+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/commands.rs
@@ -807,7 +807,7 @@ pub async fn execute_tool<R: tauri::Runtime>(
}
/// The webview-backed `screenshot` renderer on the desktop (Linux, macOS,
-/// Windows); `None` everywhere else, keeping the Chrome fallback.
+/// Windows); `None` everywhere else, which renders through headless Chrome.
#[cfg(all(
feature = "tauri",
any(target_os = "linux", target_os = "macos", target_os = "windows")
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/handlers.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/handlers.rs
index 9c3286724..d890ff61e 100644
--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/handlers.rs
+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/handlers.rs
@@ -1495,9 +1495,11 @@ fn chrome_binary() -> Option<PathBuf> {
/// Render a local HTML/SVG file to PNG bytes.
///
/// `backend`, when present, renders through the desktop's own webview (no Chrome
-/// needed); on any backend failure this falls back to headless Chrome so nothing
-/// regresses. `None` goes straight to Chrome. Either way the viewport clamping
-/// and output cap are identical.
+/// needed), and its result is final: the webview checks every frame the page
+/// loads against the screenshot's own folder, and headless Chrome has no such
+/// check, so a page that makes the webview fail must not get a second render
+/// through Chrome. `None` (no desktop webview, e.g. the CLI) goes to Chrome.
+/// Either way the viewport clamping and output cap are identical.
///
/// `width`/`height` are the viewport in CSS pixels; `scale` is the device pixel
/// ratio, so the PNG comes out `width*scale` pixels wide with the layout
@@ -1532,14 +1534,11 @@ pub async fn render_html_png(
1.0
};
- // Try the injected backend (the desktop's webview capture) first, so the tool
- // works without Chrome; on any failure fall through to Chrome so nothing
- // regresses.
+ // The desktop's webview capture, when injected, is the only renderer: see
+ // the doc comment for why a failure here does not fall through to Chrome.
if let Some(backend) = backend {
- match backend(target.to_path_buf(), width, height, scale, scope).await {
- Ok(png) => return finalize_screenshot_png(png),
- Err(_) => { /* fall through to Chrome */ }
- }
+ let png = backend(target.to_path_buf(), width, height, scale, scope).await?;
+ return finalize_screenshot_png(png);
}
let Some(chrome) = chrome_binary() else {
@@ -4256,6 +4255,23 @@ mod tests {
let _ = std::fs::remove_dir_all(&outside);
}
+ /// A failing desktop renderer is the answer: the page is not rendered a
+ /// second time through Chrome, which would not check its frames.
+ #[tokio::test]
+ async fn a_failing_screenshot_backend_does_not_fall_back_to_chrome() {
+ let root = unique_root();
+ let page = root.join("page.html");
+ std::fs::write(&page, b"<h1>page</h1>").unwrap();
+ let backend: ScreenshotBackend = std::sync::Arc::new(|_, _, _, _, _| {
+ Box::pin(async { Err("webview capture timed out".to_string()) })
+ });
+
+ let result =
+ render_html_png(&page, 800, 600, 1.0, PageScope::Directory, Some(&backend)).await;
+ assert_eq!(result, Err("webview capture timed out".to_string()));
+ let _ = std::fs::remove_dir_all(&root);
+ }
+
/// Renders for real when a browser is present, and returns an image part
/// rather than a data URL buried in the text.
#[cfg(feature = "tauri")]
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/mod.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/mod.rs
index 330f6c67e..1efb8110e 100644
--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/mod.rs
+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/mod.rs
@@ -188,8 +188,9 @@ pub struct ToolContext<'a> {
/// `[tools].env_set`. Applied after `env_passthrough` and winning per key;
/// the one way to inject a secret-named variable on purpose.
pub env_set: &'a [(String, String)],
- /// Renders HTML/SVG to PNG for the `screenshot` tool. `None` falls back to
- /// headless Chrome; the desktop injects a webview-backed renderer. See
+ /// Renders HTML/SVG to PNG for the `screenshot` tool. `None` renders through
+ /// headless Chrome; the desktop injects a webview-backed renderer, whose
+ /// failures are returned as-is rather than retried in Chrome. See
/// [`ScreenshotBackend`].
pub screenshot_backend: Option<ScreenshotBackend>,
/// The run's lifecycle hooks. Carried on the context rather than passed to