mirror of
https://github.com/storytold/effectcraft.git
synced 2026-12-05 08:21:21 -05:00
fix(ui-egui): reply-size check only where the agents channel is (web build)
craft-automation is a desktop-only dependency of ui-egui, so the wasm gate could not resolve MAX_RESPONSE_BYTES. The web hands replies to the page, which has no line limit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -25,7 +25,6 @@
|
||||
use std::sync::Arc;
|
||||
use std::sync::mpsc::Sender;
|
||||
|
||||
use craft_automation::protocol::MAX_RESPONSE_BYTES;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::EffectcraftApp;
|
||||
@@ -1002,8 +1001,15 @@ pub fn save_screenshot(image: &egui::ColorImage, path: Option<&str>, crop: Optio
|
||||
}
|
||||
}
|
||||
|
||||
/// A compressed PNG, for a reply: a reply line has a limit ([`MAX_RESPONSE_BYTES`] on the agents
|
||||
/// channel), which an uncompressed window or frame passes.
|
||||
/// The longest reply line: the agents channel's on desktop. The web hands replies to the page,
|
||||
/// which has no such limit.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
const REPLY_LIMIT: Option<usize> = Some(craft_automation::protocol::MAX_RESPONSE_BYTES);
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
const REPLY_LIMIT: Option<usize> = None;
|
||||
|
||||
/// A compressed PNG, for a reply: a reply line has a limit ([`REPLY_LIMIT`]), which an
|
||||
/// uncompressed window or frame passes.
|
||||
fn compressed_png(rgba: &[u8], w: u32, h: u32) -> Result<Vec<u8>, String> {
|
||||
use image::ImageEncoder;
|
||||
use image::codecs::png::{CompressionType, FilterType, PngEncoder};
|
||||
@@ -1020,15 +1026,17 @@ const FRAME_TOO_LARGE: &str = "pass `path` to write it to a file, or `max_side`
|
||||
const SCREENSHOT_TOO_LARGE: &str = "pass `path` to write it to a file, or `panel` or `id` to capture part of the window";
|
||||
|
||||
/// `png` in base64 for a reply, or an error that says what to do (`instead`) when it wouldn't fit
|
||||
/// in one ([`MAX_RESPONSE_BYTES`], with room for the rest of the reply).
|
||||
/// in one ([`REPLY_LIMIT`], with room for the rest of the reply).
|
||||
fn inline_base64(png: &[u8], instead: &str) -> Result<String, String> {
|
||||
const REST_OF_REPLY: usize = 4 << 10;
|
||||
let encoded = base64(png);
|
||||
if encoded.len().saturating_add(REST_OF_REPLY) > MAX_RESPONSE_BYTES {
|
||||
if let Some(limit) = REPLY_LIMIT
|
||||
&& encoded.len().saturating_add(REST_OF_REPLY) > limit
|
||||
{
|
||||
return Err(format!(
|
||||
"the image is too large to return inline ({} MiB in base64, a reply holds at most {} MiB): {instead}",
|
||||
encoded.len().div_ceil(1 << 20),
|
||||
MAX_RESPONSE_BYTES >> 20
|
||||
limit >> 20
|
||||
));
|
||||
}
|
||||
Ok(encoded)
|
||||
|
||||
Reference in New Issue
Block a user