Skip to content

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

feat(claude-code): limit image blocks to 16 per message

Steven Enamakel3aa88f2a3a5c
diff --git a/src/openhuman/agent/multimodal.rs b/src/openhuman/agent/multimodal.rs
index d28d7e7c31..1d31a7fc2b 100644
--- a/src/openhuman/agent/multimodal.rs
+++ b/src/openhuman/agent/multimodal.rs
@@ -599,6 +599,11 @@ pub fn is_managed_attachment_path(path: &str) -> bool {
candidate.starts_with(root)
}
+#[cfg(test)]
+pub(crate) fn managed_attachments_dir_for_tests() -> PathBuf {
+ attachments_dir()
+}
+
/// Per-user fallback attachments dir used only when [`init_attachments_dir`]
/// was never called. Uses the OS user cache dir (e.g. `~/Library/Caches/…`,
/// `~/.cache/…`) so persisted image bytes aren't dropped into a world-readable
diff --git a/src/openhuman/inference/provider/claude_code/input_builder.rs b/src/openhuman/inference/provider/claude_code/input_builder.rs
index c6d8e59e0a..1bcc04a2e8 100644
--- a/src/openhuman/inference/provider/claude_code/input_builder.rs
+++ b/src/openhuman/inference/provider/claude_code/input_builder.rs
@@ -80,8 +80,10 @@ fn user_row(text: &str) -> Value {
}
fn content_blocks(raw: &str) -> Vec<Value> {
+ const MAX_IMAGES_PER_MESSAGE: usize = 16;
let mut blocks = Vec::new();
let mut cursor = 0;
+ let mut image_count = 0;
while let Some(relative) = raw[cursor..].find("[IMAGE:") {
let start = cursor + relative;
if start > cursor {
@@ -94,7 +96,13 @@ fn content_blocks(raw: &str) -> Vec<Value> {
};
let end = start + end_relative + 1;
let reference = &raw[start + 7..end - 1];
- blocks.push(image_block(reference).unwrap_or_else(|| {
+ let block = if image_count < MAX_IMAGES_PER_MESSAGE {
+ image_count += 1;
+ image_block(reference)
+ } else {
+ None
+ };
+ blocks.push(block.unwrap_or_else(|| {
json!({
"type":"text", "text":"[an attached image could not be read]"
})
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 2171697090..b1734c81e7 100644
--- a/src/openhuman/inference/provider/claude_code/input_builder_tests.rs
+++ b/src/openhuman/inference/provider/claude_code/input_builder_tests.rs
@@ -164,9 +164,9 @@ fn percent_encoded_data_uri_emits_an_image_block() {
#[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");
+ let dir = crate::openhuman::agent::multimodal::managed_attachments_dir_for_tests();
+ std::fs::create_dir_all(&dir).unwrap();
+ let path = dir.join(format!("input-builder-test-{}.png", std::process::id()));
std::fs::write(&path, b"PNG").unwrap();
let s = String::from_utf8(build_stdin(
&[msg("user", &format!("file [IMAGE:{}]", path.display()))],
@@ -178,6 +178,7 @@ fn readable_managed_image_file_emits_an_image_block() {
assert!(content.len() >= 2, "expected text and image blocks: {s}");
assert_eq!(content[1]["type"], "image");
assert_eq!(content[1]["source"]["data"], "UE5H");
+ let _ = std::fs::remove_file(path);
}
#[test]