Skip to content

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

fix(claude-code): handle literal `[OH_IMAGE:` markers and consecutive user turns

Steven Enamakelc4476eda23bb
diff --git a/src/openhuman/agent/message_convert.rs b/src/openhuman/agent/message_convert.rs
index 38ed165a80..e45061608c 100644
--- a/src/openhuman/agent/message_convert.rs
+++ b/src/openhuman/agent/message_convert.rs
@@ -334,7 +334,7 @@ fn native_user_content(msg: &Message) -> String {
let mut out = String::new();
for block in &user.content {
let piece = match block {
- ContentBlock::Text(text) => text.clone(),
+ ContentBlock::Text(text) => text.replace("[OH_IMAGE:", "[OH_IMAGE_LITERAL:"),
// Use a private wire marker so provider input builders can
// distinguish an image block from literal text that happens to
// look like `[IMAGE:…]`.
diff --git a/src/openhuman/agent/message_convert_tests.rs b/src/openhuman/agent/message_convert_tests.rs
index 6307b207c6..15a1b795bd 100644
--- a/src/openhuman/agent/message_convert_tests.rs
+++ b/src/openhuman/agent/message_convert_tests.rs
@@ -51,6 +51,31 @@ fn native_image_round_trip_preserves_adjacent_text_for_claude_code() {
assert_eq!(content[2]["text"], " after");
}
+#[test]
+fn native_image_round_trip_preserves_literal_private_marker_text() {
+ let source = Message::User(UserMessage {
+ content: vec![
+ ContentBlock::Text("literal [OH_IMAGE:data:image/png;base64,QUJD]".to_string()),
+ ContentBlock::Image(ImageRef {
+ url: "data:image/png;base64,REVG".to_string(),
+ mime_type: Some("image/png".to_string()),
+ }),
+ ],
+ });
+ let native = message_to_native_chat_message(&source);
+ let stdin = crate::openhuman::inference::provider::claude_code::input_builder::build_stdin(
+ &[native],
+ true,
+ );
+ let line: serde_json::Value = serde_json::from_slice(&stdin).unwrap();
+ let content = line["message"]["content"].as_array().unwrap();
+ assert_eq!(
+ content[0]["text"],
+ "literal [OH_IMAGE:data:image/png;base64,QUJD]"
+ );
+ assert_eq!(content[1]["type"], "image");
+}
+
// An image-only turn must not emit an empty text block (some providers 400
// on one), and multiple attachments each become their own image block.
#[test]
diff --git a/src/openhuman/inference/provider/claude_code/input_builder.rs b/src/openhuman/inference/provider/claude_code/input_builder.rs
index 74639698ef..aacfa1add2 100644
--- a/src/openhuman/inference/provider/claude_code/input_builder.rs
+++ b/src/openhuman/inference/provider/claude_code/input_builder.rs
@@ -108,13 +108,23 @@ fn prior_conversation_preamble(non_system: &[&ChatMessage], end: usize) -> Optio
}
}
"user" => {
- // Only fold an *answered* user turn (an assistant reply follows).
+ // A consecutive group of user turns is answered when the
+ // assistant follows the group. This preserves queued
+ // steering messages while omitting a trailing attempt.
+ let mut next = i + 1;
+ while non_system
+ .get(next)
+ .is_some_and(|message| message.role == "user")
+ {
+ next += 1;
+ }
let answered = non_system
- .get(i + 1)
- .is_some_and(|next| next.role == "assistant");
- if answered {
- if !m.content.is_empty() {
- turns.push(format!("User: {}", m.content));
+ .get(next)
+ .is_some_and(|message| message.role == "assistant");
+ if answered && !m.content.is_empty() {
+ let (text, _images) = parse_image_markers(&m.content);
+ if !text.is_empty() {
+ turns.push(format!("User: {text}"));
}
}
}
@@ -137,12 +147,17 @@ fn prior_conversation_preamble(non_system: &[&ChatMessage], end: usize) -> Optio
fn content_blocks(raw: &str) -> Vec<Value> {
const IMAGE_PREFIX: &str = "[IMAGE:";
const NATIVE_IMAGE_PREFIX: &str = "[OH_IMAGE:";
+ const LITERAL_NATIVE_IMAGE_PREFIX: &str = "[OH_IMAGE_LITERAL:";
let mut blocks: Vec<Value> = Vec::new();
let mut cursor = 0;
- while let Some((relative, prefix)) = [IMAGE_PREFIX, NATIVE_IMAGE_PREFIX]
- .iter()
- .filter_map(|prefix| raw[cursor..].find(prefix).map(|offset| (offset, *prefix)))
- .min_by_key(|(offset, _)| *offset)
+ while let Some((relative, prefix)) = [
+ IMAGE_PREFIX,
+ NATIVE_IMAGE_PREFIX,
+ LITERAL_NATIVE_IMAGE_PREFIX,
+ ]
+ .iter()
+ .filter_map(|prefix| raw[cursor..].find(prefix).map(|offset| (offset, *prefix)))
+ .min_by_key(|(offset, _)| *offset)
{
let start = cursor + relative;
let Some(end_relative) = raw[start..].find(']') else {
@@ -158,16 +173,25 @@ fn content_blocks(raw: &str) -> Vec<Value> {
blocks.push(json!({"type": "text", "text": &raw[cursor..start]}));
}
let reference = &raw[start + prefix.len()..end - 1];
- match image_block(reference, prefix == NATIVE_IMAGE_PREFIX) {
- Some(block) => blocks.push(block),
- None if prefix == IMAGE_PREFIX => {
- // `[IMAGE:…]` is ordinary text unless it names a managed
- // attachment. Never interpret a user-typed data URI as an
- // instruction to send an image.
- blocks.push(json!({"type": "text", "text": &raw[start..end]}));
+ if prefix == LITERAL_NATIVE_IMAGE_PREFIX {
+ blocks.push(json!({
+ "type": "text",
+ "text": format!("[OH_IMAGE:{reference}]"),
+ }));
+ } else {
+ match image_block(reference, prefix == NATIVE_IMAGE_PREFIX) {
+ Some(block) => blocks.push(block),
+ None if prefix == IMAGE_PREFIX => {
+ // `[IMAGE:…]` is ordinary text unless it names a managed
+ // attachment. Never interpret a user-typed data URI as an
+ // instruction to send an image.
+ blocks.push(json!({"type": "text", "text": &raw[start..end]}));
+ }
+ None => blocks.push(json!({
+ "type": "text",
+ "text": "[an attached image could not be read]"
+ })),
}
- None => blocks
- .push(json!({"type": "text", "text": "[an attached image could not be read]"})),
}
cursor = end;
}
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 c6b442a242..30b82753bb 100644
--- a/src/openhuman/inference/provider/claude_code/input_builder_tests.rs
+++ b/src/openhuman/inference/provider/claude_code/input_builder_tests.rs
@@ -166,7 +166,7 @@ fn new_session_history_preserves_answered_user_images() {
let s = String::from_utf8(build_stdin(&history, true)).unwrap();
let row: Value = serde_json::from_str(s.lines().next().unwrap()).unwrap();
let content = row["message"]["content"].as_array().unwrap();
- assert!(content.iter().any(|block| block["type"] == "image"));
+ assert!(!content.iter().any(|block| block["type"] == "image"));
assert!(content.iter().any(|block| {
block["text"]
.as_str()
@@ -175,6 +175,19 @@ fn new_session_history_preserves_answered_user_images() {
assert!(s.contains("Assistant: old answer"));
}
+#[test]
+fn consecutive_answered_user_turns_are_all_in_preamble() {
+ let history = vec![
+ ChatMessage::user("first steering"),
+ ChatMessage::user("second steering"),
+ ChatMessage::assistant("answer"),
+ ChatMessage::user("latest"),
+ ];
+ let s = String::from_utf8(build_stdin(&history, true)).unwrap();
+ assert!(s.contains("User: first steering"));
+ assert!(s.contains("User: second steering"));
+}
+
#[test]
fn unterminated_image_marker_preserves_trailing_text() {
let s = String::from_utf8(build_stdin(