Skip to content

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

fix(message-convert): stop inserting newlines between user content pieces

Steven Enamakel388896a1df6b
diff --git a/src/openhuman/agent/message_convert.rs b/src/openhuman/agent/message_convert.rs
index 80e99d4ef1..ad1d98cf98 100644
--- a/src/openhuman/agent/message_convert.rs
+++ b/src/openhuman/agent/message_convert.rs
@@ -343,9 +343,6 @@ fn native_user_content(msg: &Message) -> String {
if piece.is_empty() {
continue;
}
- if !out.is_empty() {
- out.push('\n');
- }
out.push_str(&piece);
}
out
diff --git a/src/openhuman/agent/message_convert_tests.rs b/src/openhuman/agent/message_convert_tests.rs
index 039817ff03..45d5be982d 100644
--- a/src/openhuman/agent/message_convert_tests.rs
+++ b/src/openhuman/agent/message_convert_tests.rs
@@ -26,6 +26,34 @@ fn user_image_marker_becomes_an_image_content_block() {
}
}
+#[test]
+fn native_image_round_trip_preserves_adjacent_text_for_claude_code() {
+ let png = "data:image/png;base64,QUJD";
+ let source = Message::User(UserMessage {
+ id: None,
+ content: vec![
+ ContentBlock::Text("before ".to_string()),
+ ContentBlock::Image(ImageRef {
+ url: png.to_string(),
+ mime_type: Some("image/png".to_string()),
+ }),
+ ContentBlock::Text(" after".to_string()),
+ ],
+ tool_calls: Vec::new(),
+ usage: None,
+ });
+ 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"], "before ");
+ assert_eq!(content[1]["type"], "image");
+ assert_eq!(content[2]["text"], " after");
+}
+
// 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 b9117d3827..da62ade51b 100644
--- a/src/openhuman/inference/provider/claude_code/input_builder.rs
+++ b/src/openhuman/inference/provider/claude_code/input_builder.rs
@@ -35,16 +35,32 @@ pub fn build_stdin(messages: &[ChatMessage], is_new_session: bool) -> Vec<u8> {
// 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();
- };
+ // A prompt is active only when the final non-system turn is from the
+ // user. If history ends on an assistant/tool turn, replay all of it as
+ // context on a new session; never resubmit an earlier answered prompt.
+ let last_user_pos = non_system.iter().rposition(|m| m.role == "user");
+ let active_user_pos = last_user_pos.filter(|&pos| pos == non_system.len() - 1);
let mut content: Vec<Value> = Vec::new();
if is_new_session {
- if let Some(preamble) = prior_conversation_preamble(&non_system, last_user_pos) {
+ let context_end = active_user_pos.unwrap_or(non_system.len());
+ if let Some(preamble) = prior_conversation_preamble(&non_system, context_end) {
content.push(json!({"type": "text", "text": preamble}));
}
}
+ let Some(last_user_pos) = active_user_pos else {
+ return if is_new_session && !content.is_empty() {
+ let line = json!({
+ "type": "user",
+ "message": { "role": "user", "content": content },
+ });
+ let mut out = String::new();
+ push_json_line(&mut out, &line);
+ out.into_bytes()
+ } else {
+ Vec::new()
+ };
+ };
// 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