Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(claude_code): support percent-encoded data URIs in image blocks

Steven Enamakel5ba0a7063be1
diff --git a/src/openhuman/inference/provider/claude_code/input_builder.rs b/src/openhuman/inference/provider/claude_code/input_builder.rs
index 22a8f7de44..5366e93231 100644
--- a/src/openhuman/inference/provider/claude_code/input_builder.rs
+++ b/src/openhuman/inference/provider/claude_code/input_builder.rs
@@ -112,14 +112,23 @@ fn content_blocks(raw: &str) -> Vec<Value> {
fn image_block(reference: &str) -> Option<Value> {
let (media_type, data) = if let Some(rest) = reference.strip_prefix("data:") {
- let (mime, encoded) = rest.split_once(";base64,")?;
- let bytes = base64::engine::general_purpose::STANDARD
- .decode(encoded)
- .ok()?;
+ let (metadata, payload) = rest.split_once(',')?;
+ let mime = metadata.split(';').next()?.to_string();
+ let (bytes, encoded) = if metadata
+ .split(';')
+ .any(|flag| flag.eq_ignore_ascii_case("base64"))
+ {
+ let bytes = base64::engine::general_purpose::STANDARD.decode(payload).ok()?;
+ (bytes, payload.to_string())
+ } else {
+ let bytes = percent_decode_bytes(payload)?;
+ let encoded = base64::engine::general_purpose::STANDARD.encode(&bytes);
+ (bytes, encoded)
+ };
if bytes.len() > 20 * 1024 * 1024 {
return None;
}
- (mime.to_string(), encoded.to_string())
+ (mime, encoded)
} else {
if !is_managed_attachment_path(reference) {
return None;
@@ -147,6 +156,24 @@ fn image_block(reference: &str) -> Option<Value> {
Some(json!({"type":"image", "source":{"type":"base64", "media_type":media_type, "data":data}}))
}
+fn percent_decode_bytes(input: &str) -> Option<Vec<u8>> {
+ let bytes = input.as_bytes();
+ let mut decoded = Vec::with_capacity(bytes.len());
+ let mut index = 0;
+ while index < bytes.len() {
+ if bytes[index] == b'%' {
+ let high = bytes.get(index + 1).and_then(|b| (*b as char).to_digit(16))?;
+ let low = bytes.get(index + 2).and_then(|b| (*b as char).to_digit(16))?;
+ decoded.push((high * 16 + low) as u8);
+ index += 3;
+ } else {
+ decoded.push(bytes[index]);
+ index += 1;
+ }
+ }
+ Some(decoded)
+}
+
/// Fold prior turns into a single labelled transcript, or `None` when there
/// is nothing to carry.
///
diff --git a/src/openhuman/inference/provider/claude_code/input_builder_tests.rs b/src/openhuman/inference/provider/claude_code/input_builder_tests.rs
index d6084bb35c..c6dd505918 100644
--- a/src/openhuman/inference/provider/claude_code/input_builder_tests.rs
+++ b/src/openhuman/inference/provider/claude_code/input_builder_tests.rs
@@ -143,6 +143,36 @@ fn interleaved_text_and_images_keep_source_order() {
assert_eq!(content[4]["text"], " after");
}
+#[test]
+fn percent_encoded_data_uri_emits_an_image_block() {
+ let s = String::from_utf8(build_stdin(
+ &[msg("user", "see [IMAGE:data:image/png,%89PNG%0D%0A]")],
+ true,
+ ))
+ .unwrap();
+ let row: Value = serde_json::from_str(s.lines().next().unwrap()).unwrap();
+ let block = &row["message"]["content"][1];
+ assert_eq!(block["type"], "image");
+ assert_eq!(block["source"]["media_type"], "image/png");
+ assert_eq!(block["source"]["data"], "iVBORw0K");
+}
+
+#[test]
+fn readable_managed_image_file_emits_an_image_block() {
+ let dir = tempfile::tempdir().unwrap();
+ crate::openhuman::agent::multimodal::init_attachments_dir(dir.path().to_path_buf());
+ let path = dir.path().join("sample.png");
+ std::fs::write(&path, b"PNG").unwrap();
+ let s = String::from_utf8(build_stdin(
+ &[msg("user", &format!("file [IMAGE:{}]", path.display()))],
+ true,
+ ))
+ .unwrap();
+ let row: Value = serde_json::from_str(s.lines().next().unwrap()).unwrap();
+ assert_eq!(row["message"]["content"][1]["type"], "image");
+ assert_eq!(row["message"]["content"][1]["source"]["data"], "UE5H");
+}
+
#[test]
fn unmanaged_and_unreadable_images_degrade_without_reading_paths() {
let s = String::from_utf8(build_stdin(