Skip to content

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

fix(agent): preserve pasted images across the native-provider message hop

Nocturnalc56207239569
diff --git a/src/openhuman/agent/message_convert.rs b/src/openhuman/agent/message_convert.rs
index f40467cd67..da1e84386d 100644
--- a/src/openhuman/agent/message_convert.rs
+++ b/src/openhuman/agent/message_convert.rs
@@ -308,6 +308,49 @@ pub(crate) fn messages_to_history(messages: &[Message]) -> Vec<ChatMessage> {
messages.iter().map(message_to_chat_message).collect()
}
+/// Serialize a user [`Message`]'s content blocks back into a single string for a
+/// native provider request, **preserving image attachments** as inline
+/// `[IMAGE:<url>]` markers. This is the inverse of [`user_content_blocks`]: the
+/// forward hop lifts `[IMAGE:…]` markers into typed [`ContentBlock::Image`]
+/// blocks, so the reverse hop must re-emit them, or a native-tool provider that
+/// round-trips through a string-content [`ChatMessage`] (claude-code, and any
+/// other `supports_native_tools` provider) silently loses every pasted image —
+/// [`Message::text`] concatenates only [`ContentBlock::Text`] and drops the rest.
+/// The marker-aware provider input builders reinflate `[IMAGE:data:…]` back into
+/// real image content blocks. Text-only turns are byte-for-byte identical to
+/// `msg.text()` (fast path), so non-image traffic is unaffected.
+fn native_user_content(msg: &Message) -> String {
+ let Message::User(user) = msg else {
+ return msg.text();
+ };
+ // Fast path: no image blocks → identical to `msg.text()`.
+ if !user
+ .content
+ .iter()
+ .any(|b| matches!(b, ContentBlock::Image(_)))
+ {
+ return msg.text();
+ }
+ let mut out = String::new();
+ for block in &user.content {
+ let piece = match block {
+ ContentBlock::Text(text) => text.clone(),
+ ContentBlock::Image(image) => format!("[IMAGE:{}]", image.url),
+ // Json / ProviderExtension carry no user-visible text — `msg.text()`
+ // drops them too, so skip them to preserve that behaviour.
+ _ => continue,
+ };
+ if piece.is_empty() {
+ continue;
+ }
+ if !out.is_empty() {
+ out.push('\n');
+ }
+ out.push_str(&piece);
+ }
+ out
+}
+
/// Convert one harness [`Message`] into a [`ChatMessage`] for a **native**
/// tool-calling provider request, preserving the structure the provider needs to
/// round-trip a tool round: an assistant turn that made tool calls is encoded as
@@ -320,7 +363,7 @@ pub(crate) fn messages_to_history(messages: &[Message]) -> Vec<ChatMessage> {
pub(crate) fn message_to_native_chat_message(msg: &Message) -> ChatMessage {
match msg {
Message::System(_) => ChatMessage::system(msg.text()),
- Message::User(_) => ChatMessage::user(msg.text()),
+ Message::User(_) => ChatMessage::user(native_user_content(msg)),
Message::Assistant(a) if !a.tool_calls.is_empty() => {
let tool_calls: Vec<_> = a.tool_calls.iter().map(ta_call_to_oh_call).collect();
let payload = serde_json::json!({
@@ -536,6 +579,47 @@ mod tests {
}
}
+ // Regression: the native-provider reverse hop must round-trip a pasted image.
+ // The forward hop lifts `[IMAGE:data:…]` into a `ContentBlock::Image`; the
+ // reverse hop (`message_to_native_chat_message`, feeding claude-code and any
+ // other `supports_native_tools` provider) must re-emit it as an `[IMAGE:…]`
+ // marker. Before the fix it called `msg.text()`, which drops image blocks, so
+ // pasted images reached the model as text-only — the certificate never arrived.
+ #[test]
+ fn native_reverse_hop_preserves_pasted_image_marker() {
+ let jpeg = "data:image/jpeg;base64,/9j/4AAQSkZJRg==";
+ let harness_msg = chat_message_to_message(&ChatMessage::user(format!(
+ "here is my adhd certificate [IMAGE:{jpeg}]"
+ )));
+ // Sanity: the forward hop split prose + image (two blocks).
+ assert!(
+ matches!(&harness_msg, Message::User(u) if u.content.len() == 2),
+ "forward hop should lift the image into its own block"
+ );
+
+ let native = message_to_native_chat_message(&harness_msg);
+ assert_eq!(native.role, "user");
+ assert!(
+ native.content.contains(&format!("[IMAGE:{jpeg}]")),
+ "the image marker must survive the reverse hop: {}",
+ native.content
+ );
+ assert!(
+ native.content.contains("here is my adhd certificate"),
+ "prose must be preserved alongside the image: {}",
+ native.content
+ );
+ }
+
+ // Text-only native turns must be byte-for-byte identical to `msg.text()` —
+ // the image fix must not perturb the overwhelming majority of traffic.
+ #[test]
+ fn native_reverse_hop_text_only_is_unchanged() {
+ let harness_msg = chat_message_to_message(&ChatMessage::user("just some text".to_string()));
+ let native = message_to_native_chat_message(&harness_msg);
+ assert_eq!(native.content, "just some text");
+ }
+
// 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 462c0d670c..6195ac45d0 100644
--- a/src/openhuman/inference/provider/claude_code/input_builder.rs
+++ b/src/openhuman/inference/provider/claude_code/input_builder.rs
@@ -11,48 +11,162 @@
//! - On a `--resume` of an existing CC session: claude already has prior
//! turns server-side; we only send the last user turn.
+use base64::Engine as _;
use serde_json::{json, Value};
use crate::openhuman::agent::messages::ChatMessage;
+use crate::openhuman::agent::multimodal::{parse_image_markers, rehydrate_image_placeholders};
/// Build the bytes to write to claude's stdin. Returns an empty `Vec`
/// when there is nothing to send (caller should abort).
pub fn build_stdin(messages: &[ChatMessage], is_new_session: bool) -> Vec<u8> {
- let mut out = String::new();
- let to_emit: Vec<&ChatMessage> = if is_new_session {
- messages.iter().filter(|m| m.role != "system").collect()
- } else {
- // Resume: only the trailing user turn matters.
- messages
- .iter()
- .rev()
- .find(|m| m.role == "user")
- .into_iter()
- .collect()
+ // Resolve any `[Image: … #att:<id>]` placeholders to on-disk `[IMAGE:<path>]`
+ // markers so pasted images can be inlined below. No-op for messages that
+ // carry no image placeholder, so plain-text turns are unaffected.
+ let rehydrated = rehydrate_image_placeholders(messages);
+ let messages: &[ChatMessage] = &rehydrated;
+
+ // `--input-format stream-json` accepts ONLY user-role turns — replaying an
+ // assistant turn fails with `Expected message role 'user', got 'assistant'`.
+ // So we always emit exactly one user message: the trailing user turn. On a
+ // *new* session (including a recreated one — see the session_store recovery
+ // path) any prior turns are folded into a text preamble so the session keeps
+ // its context instead of erroring; on resume the CLI already holds them.
+ let non_system: Vec<&ChatMessage> = messages.iter().filter(|m| m.role != "system").collect();
+ let Some(last_user_pos) = non_system.iter().rposition(|m| m.role == "user") else {
+ return Vec::new();
};
- for msg in to_emit {
- let role = match msg.role.as_str() {
- "user" => "user",
- "assistant" => "assistant",
- // CC stdin doesn't accept `system` or `tool` rows. The system
- // prompt is plumbed via `--append-system-prompt`; tool roles
- // belong to the harness, not the CLI's input format.
- _ => continue,
- };
- let line = json!({
- "type": "user",
- "message": {
- "role": role,
- "content": [{"type": "text", "text": msg.content}],
- },
- });
- push_json_line(&mut out, &line);
+ let mut content: Vec<Value> = Vec::new();
+ if is_new_session {
+ if let Some(preamble) = prior_conversation_preamble(&non_system, last_user_pos) {
+ content.push(json!({"type": "text", "text": preamble}));
+ }
+ }
+ // Pasted images arrive as inline `[IMAGE:data:…]` markers in the user turn's
+ // content (the multimodal pipeline rehydrates them, and the native-provider
+ // message bridge re-emits them from its typed image blocks — see
+ // `message_convert::message_to_native_chat_message`). `content_blocks` splits
+ // each marker into a real Anthropic `image` block below.
+ content.extend(content_blocks(&non_system[last_user_pos].content));
+ if content.is_empty() {
+ return Vec::new();
}
+ let line = json!({
+ "type": "user",
+ "message": { "role": "user", "content": content },
+ });
+ let mut out = String::new();
+ push_json_line(&mut out, &line);
out.into_bytes()
}
+/// Render the turns before `end` (the latest user turn) as a plain-text preamble
+/// so a newly-created CC session inherits the thread's context — the CLI can't
+/// accept assistant-role input, so this is the only way to seed prior turns.
+///
+/// A user turn with no assistant reply after it is skipped: that is a failed or
+/// pending attempt (e.g. a message that errored and was then re-asked), not part
+/// of the conversation. Replaying it would duplicate the re-asked current turn
+/// and make the message look like it "came through twice". Image markers are
+/// stripped (only the latest turn re-sends its images). Returns `None` when
+/// there is nothing to carry over (a genuinely fresh conversation).
+fn prior_conversation_preamble(non_system: &[&ChatMessage], end: usize) -> Option<String> {
+ let mut turns = Vec::new();
+ for (i, m) in non_system.iter().take(end).enumerate() {
+ match m.role.as_str() {
+ "assistant" => {
+ let (text, _images) = parse_image_markers(&m.content);
+ if !text.is_empty() {
+ turns.push(format!("Assistant: {text}"));
+ }
+ }
+ "user" => {
+ // Only fold an *answered* user turn (an assistant reply follows).
+ let answered = non_system
+ .get(i + 1)
+ .is_some_and(|next| next.role == "assistant");
+ if answered {
+ let (text, _images) = parse_image_markers(&m.content);
+ if !text.is_empty() {
+ turns.push(format!("User: {text}"));
+ }
+ }
+ }
+ _ => {}
+ }
+ }
+ if turns.is_empty() {
+ return None;
+ }
+ Some(format!(
+ "[Earlier in this conversation]\n{}\n[End of earlier conversation]\n",
+ turns.join("\n")
+ ))
+}
+
+/// Split a message's text into stream-json content blocks: the prose as a
+/// `text` block, plus one native `image` block per `[IMAGE:<ref>]` marker (the
+/// `claude` CLI + Opus are vision-capable). An image that cannot be read
+/// degrades to a short text note rather than being silently dropped.
+fn content_blocks(raw: &str) -> Vec<Value> {
+ let (text, image_refs) = parse_image_markers(raw);
+ let mut blocks: Vec<Value> = Vec::new();
+ if !text.is_empty() {
+ blocks.push(json!({"type": "text", "text": text}));
+ }
+ for reference in &image_refs {
+ match image_block(reference) {
+ Some(block) => blocks.push(block),
+ None => blocks.push(json!({
+ "type": "text",
+ "text": "[an attached image could not be read]"
+ })),
+ }
+ }
+ if blocks.is_empty() {
+ // Preserve prior behaviour for a genuinely empty message.
+ blocks.push(json!({"type": "text", "text": raw}));
+ }
+ blocks
+}
+
+/// Build an Anthropic `image` content block from an `[IMAGE:<ref>]` reference.
+/// `<ref>` is either a `data:` URI (inline base64) or an on-disk file path (a
+/// rehydrated attachment). Returns `None` when the ref cannot be resolved.
+fn image_block(reference: &str) -> Option<Value> {
+ let (media_type, data_b64) = if let Some(rest) = reference.strip_prefix("data:") {
+ let (mime, data) = rest.split_once(";base64,")?;
+ (mime.to_string(), data.to_string())
+ } else {
+ let bytes = std::fs::read(reference).ok()?;
+ (
+ media_type_from_path(reference),
+ base64::engine::general_purpose::STANDARD.encode(bytes),
+ )
+ };
+ Some(json!({
+ "type": "image",
+ "source": {"type": "base64", "media_type": media_type, "data": data_b64},
+ }))
+}
+
+/// Best-effort media type from a file extension. Claude accepts jpeg/png/gif/webp.
+fn media_type_from_path(path: &str) -> String {
+ let lower = path.to_ascii_lowercase();
+ if lower.ends_with(".png") {
+ "image/png"
+ } else if lower.ends_with(".gif") {
+ "image/gif"
+ } else if lower.ends_with(".webp") {
+ "image/webp"
+ } else {
+ "image/jpeg"
+ }
+ .to_string()
+}
+
fn push_json_line(buf: &mut String, v: &Value) {
buf.push_str(&serde_json::to_string(v).unwrap_or_default());
buf.push('\n');
@@ -72,20 +186,50 @@ mod tests {
}
#[test]
- fn new_session_pipes_full_user_history() {
+ fn new_session_folds_prior_turns_into_one_user_message() {
+ // The CLI accepts only user-role input, so a new/recreated session emits
+ // exactly ONE user message: the latest turn, with prior turns folded in
+ // as a text preamble. Regression for "Expected message role 'user', got
+ // 'assistant'" when a session is recreated for a thread with history.
let history = vec![
msg("system", "you are helpful"),
msg("user", "hi"),
msg("assistant", "hello"),
msg("user", "how are you?"),
];
- let bytes = build_stdin(&history, true);
- let s = String::from_utf8(bytes).unwrap();
+ let s = String::from_utf8(build_stdin(&history, true)).unwrap();
let lines: Vec<_> = s.lines().collect();
- assert_eq!(lines.len(), 3); // system filtered out
- assert!(lines[0].contains("\"hi\""));
- assert!(lines[1].contains("\"hello\""));
- assert!(lines[2].contains("how are you"));
+ assert_eq!(lines.len(), 1, "exactly one user message: {s}");
+ assert!(
+ !s.contains("\"role\":\"assistant\""),
+ "must never emit an assistant-role input turn: {s}"
+ );
+ assert!(s.contains("how are you"), "latest user turn present: {s}");
+ assert!(
+ s.contains("hi") && s.contains("hello"),
+ "prior turns folded in as context: {s}"
+ );
+ }
+
+ #[test]
+ fn preamble_skips_an_unanswered_prior_user_turn() {
+ // A prior user turn with no assistant reply (a failed attempt that got
+ // re-asked) must NOT be folded in — otherwise the re-ask looks like it
+ // "came through twice". General fix, not tied to any one setup.
+ let history = vec![
+ msg("user", "what is my address?"), // errored: no assistant reply
+ msg("user", "what is my address?"), // re-asked (current turn)
+ ];
+ let s = String::from_utf8(build_stdin(&history, true)).unwrap();
+ assert_eq!(
+ s.matches("what is my address").count(),
+ 1,
+ "an unanswered prior turn must not be folded in: {s}"
+ );
+ assert!(
+ !s.contains("[Earlier in this conversation]"),
+ "no preamble when the only prior turn was unanswered: {s}"
+ );
}
#[test]
@@ -107,4 +251,27 @@ mod tests {
let bytes = build_stdin(&[], true);
assert!(bytes.is_empty());
}
+
+ #[test]
+ fn user_message_with_image_marker_emits_native_image_block() {
+ // A rehydrated / inline data-URI marker becomes a real image block, and
+ // the surrounding prose stays a text block. Regression for pasted images
+ // being dropped on the way to the claude-code brain.
+ let m = ChatMessage::user("look at this [IMAGE:data:image/png;base64,QUJD]");
+ let s = String::from_utf8(build_stdin(&[m], true)).unwrap();
+ assert!(s.contains("\"type\":\"image\""), "image block emitted: {s}");
+ assert!(s.contains("\"media_type\":\"image/png\""), "{s}");
+ assert!(s.contains("\"data\":\"QUJD\""), "base64 payload preserved: {s}");
+ assert!(s.contains("\"text\":\"look at this\""), "prose kept: {s}");
+ assert!(!s.contains("[IMAGE:"), "raw marker stripped: {s}");
+ }
+
+ #[test]
+ fn plain_text_still_single_text_block() {
+ // serde_json sorts object keys, so the block serializes as
+ // {"text":"hi","type":"text"} — a single text block, no image blocks.
+ let s = String::from_utf8(build_stdin(&[ChatMessage::user("hi")], true)).unwrap();
+ assert!(s.contains("\"content\":[{\"text\":\"hi\",\"type\":\"text\"}]"), "{s}");
+ assert!(!s.contains("\"type\":\"image\""), "no image block for plain text: {s}");
+ }
}