Skip to content

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

fix(agent): handle empty message content in conversion

Steven Enamakel98a0a5640739
diff --git a/crates/openhuman-core/src/agent/message_convert.rs b/crates/openhuman-core/src/agent/message_convert.rs
index b8131a8a03..91e82d9a16 100644
--- a/crates/openhuman-core/src/agent/message_convert.rs
+++ b/crates/openhuman-core/src/agent/message_convert.rs
@@ -135,18 +135,16 @@ fn reasoning_extra_metadata(content: &[ContentBlock]) -> Option<serde_json::Valu
.map(|reasoning| serde_json::json!({ REASONING_EXT_KEY: reasoning }))
}
-/// Convert one openhuman [`TranscriptMessage`] into a harness [`Message`].
+/// Convert one transcript row into a harness [`Message`].
///
-/// Role strings map onto the typed arms. A seeded **native** tool round is
-/// serialized by [`NativeDialect::to_provider_messages`] as a
-/// `{ "content", "tool_calls" }` assistant envelope followed by
-/// `{ "tool_call_id", "content" }` tool envelopes; we unwrap those back into the
-/// structured [`AssistantMessage::tool_calls`] / [`ToolMessage::tool_call_id`]
-/// the harness needs. Without this, the seeded assistant loses its tool calls
-/// while the following tool rows survive, so the harness re-sends orphan `tool`
-/// messages and native providers reject the request (`assistant message with
-/// 'tool_calls' must be followed by tool messages`). A plain assistant/tool
-/// message that isn't an envelope maps straight through as text.
+/// Role strings map onto the typed arms: an assistant row's `tool_calls` become
+/// [`AssistantMessage::tool_calls`], a tool row's `tool_call_id` becomes
+/// [`ToolMessage::tool_call_id`] (falling back to the row id, then an empty id
+/// for a bare tool message), and a user row's `parts` become text and
+/// [`ContentBlock::Image`] blocks in order. Keeping the calls on the assistant
+/// message is what stops the harness re-sending orphan `tool` messages, which
+/// native providers reject (`assistant message with 'tool_calls' must be
+/// followed by tool messages`).
pub(crate) fn chat_message_to_message(msg: &TranscriptMessage) -> Message {
let text = msg.content.clone();
match msg.role.as_str() {
@@ -165,49 +163,92 @@ pub(crate) fn chat_message_to_message(msg: &TranscriptMessage) -> Message {
.as_ref()
.and_then(|meta| meta.get(REASONING_EXT_KEY))
.and_then(serde_json::Value::as_str);
- if let Some(envelope) = parse_assistant_envelope(&text) {
- let tool_calls = envelope.tool_calls.iter().map(oh_call_to_ta_call).collect();
- let mut content = vec![ContentBlock::Text(envelope.content)];
- content.extend(reasoning_content_block(reasoning));
- Message::Assistant(AssistantMessage {
- id: msg.id.clone(),
- content,
- tool_calls,
- usage: None,
- origin: None,
- })
- } else {
- let mut content = vec![ContentBlock::Text(text)];
- content.extend(reasoning_content_block(reasoning));
- Message::Assistant(AssistantMessage {
- id: msg.id.clone(),
- content,
- tool_calls: Vec::new(),
- usage: None,
- origin: None,
- })
- }
- }
- "tool" => {
- // Prefer the envelope's `tool_call_id` (the native seed shape); fall
- // back to the message id, then an empty id for a bare tool message.
- let (tool_call_id, content) = parse_tool_envelope(&text)
- .unwrap_or_else(|| (msg.id.clone().unwrap_or_default(), text.clone()));
- Message::Tool(ToolMessage {
- tool_call_id,
- content: vec![ContentBlock::Text(content)],
- trusted_verbatim: false,
- artifact: None,
+ let mut content = vec![ContentBlock::Text(text)];
+ content.extend(reasoning_content_block(reasoning));
+ Message::Assistant(AssistantMessage {
+ id: msg.id.clone(),
+ content,
+ tool_calls: msg.tool_calls.iter().map(row_call_to_ta_call).collect(),
+ usage: None,
+ origin: None,
})
}
+ "tool" => Message::Tool(ToolMessage {
+ tool_call_id: msg
+ .tool_call_id
+ .clone()
+ .or_else(|| msg.id.clone())
+ .unwrap_or_default(),
+ content: vec![ContentBlock::Text(text)],
+ trusted_verbatim: false,
+ artifact: None,
+ }),
// "user" and any unrecognized role default to a user turn — the safest
// mapping for a free-form inbound message.
_ => Message::User(UserMessage {
- content: user_content_blocks(text),
+ content: match msg.parts.as_deref() {
+ Some(parts) => user_blocks_from_parts(parts),
+ None => user_content_blocks(text),
+ },
}),
}
}
+/// The content blocks of a user row's typed `parts`, in source order.
+fn user_blocks_from_parts(parts: &[TranscriptPart]) -> Vec<ContentBlock> {
+ let blocks: Vec<ContentBlock> = parts
+ .iter()
+ .map(|part| match part {
+ TranscriptPart::Text { text } => ContentBlock::Text(text.clone()),
+ TranscriptPart::Image { url } => ContentBlock::Image(ImageRef {
+ url: url.clone(),
+ mime_type: data_uri_mime(url),
+ }),
+ })
+ .collect();
+ if blocks.is_empty() {
+ vec![ContentBlock::Text(String::new())]
+ } else {
+ blocks
+ }
+}
+
+/// A user [`Message`] from the text a person (or a channel) sent: any
+/// `[IMAGE:<ref>]` marker whose payload is a provider-ready reference becomes a
+/// typed [`ContentBlock::Image`], so the turn is stored and replayed with real
+/// image parts instead of a text marker. Text without such a marker is a plain
+/// text message, byte for byte.
+pub(crate) fn user_message_from_text(text: &str) -> Message {
+ Message::User(UserMessage {
+ content: user_content_blocks(text.to_string()),
+ })
+}
+
+/// The inverse of [`user_message_from_text`]: a user message's text with each
+/// image block rendered back as an `[IMAGE:<url>]` marker. A text-only message
+/// is exactly [`Message::text`].
+pub(crate) fn user_text_with_markers(msg: &Message) -> String {
+ let Message::User(user) = msg else {
+ return msg.text();
+ };
+ if !user
+ .content
+ .iter()
+ .any(|block| matches!(block, ContentBlock::Image(_)))
+ {
+ return msg.text();
+ }
+ user.content
+ .iter()
+ .filter_map(|block| match block {
+ ContentBlock::Text(text) => Some(text.clone()),
+ ContentBlock::Image(image) => Some(format!("[IMAGE:{}]", image.url)),
+ _ => None,
+ })
+ .collect::<Vec<_>>()
+ .join(" ")
+}
+
/// Build the content blocks for a user turn, lifting any `[IMAGE:…]` markers out
/// of the text into typed [`ContentBlock::Image`] blocks.
///
@@ -307,17 +348,27 @@ fn data_uri_mime(reference: &str) -> Option<String> {
(!mime.is_empty()).then(|| mime.to_string())
}
-/// Inverse of [`ta_call_to_oh_call`]: rebuild a harness [`TaToolCall`] from an
-/// openhuman [`NativeToolCall`] (whose `arguments` is a serialized JSON string).
-fn oh_call_to_ta_call(oh: &tinytools_agent::dialect::NativeToolCall) -> TaToolCall {
+/// Rebuild a harness [`TaToolCall`] from a row's [`TranscriptToolCall`] (whose
+/// `arguments` is the serialized JSON string the provider emitted).
+fn row_call_to_ta_call(call: &TranscriptToolCall) -> TaToolCall {
TaToolCall {
- id: oh.id.clone(),
- name: oh.name.clone(),
- arguments: serde_json::from_str(&oh.arguments).unwrap_or(serde_json::Value::Null),
+ id: call.id.clone(),
+ name: call.name.clone(),
+ arguments: serde_json::from_str(&call.arguments).unwrap_or(serde_json::Value::Null),
invalid: None,
}
}
+/// A harness [`TaToolCall`] as a row's [`TranscriptToolCall`].
+fn ta_call_to_row_call(call: &TaToolCall) -> TranscriptToolCall {
+ TranscriptToolCall {
+ id: call.id.clone(),
+ name: call.name.clone(),
+ arguments: call.arguments.to_string(),
+ extra_content: None,
+ }
+}
+
/// Convert a seed history into the harness `input` transcript.
pub(crate) fn history_to_messages(history: &[TranscriptMessage]) -> Vec<Message> {
history.iter().map(chat_message_to_message).collect()
@@ -364,58 +415,15 @@ pub(crate) fn messages_to_history(messages: &[Message]) -> Vec<TranscriptMessage
.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 [`TranscriptMessage`] (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.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:…]`.
- ContentBlock::Image(image) => format!("[OH_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;
- }
- out.push_str(&piece);
- }
- out
-}
-
-/// Convert one harness [`Message`] into a [`TranscriptMessage`] 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
-/// the `{ "content", "tool_calls" }` JSON envelope (matching the dispatcher's
-/// native `to_provider_messages`), and a tool result as `{ "tool_call_id",
-/// "content" }`. Without this the provider sees an assistant with no `tool_calls`
-/// followed by an orphan tool message and drops the round — breaking multi-turn
-/// native tool calling (e.g. the orchestrator's `spawn_parallel_agents` →
-/// synthesis hop).
+/// Convert one harness [`Message`] into a typed row 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 keeps
+/// them in `tool_calls`, a tool result keeps its `tool_call_id`, and a user turn
+/// with pictures keeps its text and image blocks as ordered `parts`. Without
+/// this the provider sees an assistant with no `tool_calls` followed by an
+/// orphan tool message and drops the round — breaking multi-turn native tool
+/// calling (e.g. the orchestrator's `spawn_parallel_agents` → synthesis hop) —
+/// and a native-tool provider silently loses every pasted image.
///
/// Returns `None` for [`Message::Custom`]: that variant is a host-side
/// out-of-band record (compaction marker, label, audit note) that the harness
@@ -423,25 +431,17 @@ fn native_user_content(msg: &Message) -> String {
pub(crate) fn message_to_native_chat_message(msg: &Message) -> Option<TranscriptMessage> {
Some(match msg {
Message::System(_) => TranscriptMessage::system(msg.text()),
- Message::User(_) => TranscriptMessage::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 mut cm = TranscriptMessage::assistant(encode_assistant_envelope(
- Some(&msg.text()),
- &tool_calls,
- None,
- ));
- cm.extra_metadata = reasoning_extra_metadata(&a.content);
- cm
- }
+ Message::User(user) => user_row(user),
Message::Assistant(a) => {
- let mut cm = TranscriptMessage::assistant(msg.text());
+ let mut cm = TranscriptMessage::assistant_with_calls(
+ msg.text(),
+ a.tool_calls.iter().map(ta_call_to_row_call).collect(),
+ );
cm.extra_metadata = reasoning_extra_metadata(&a.content);
cm
}
Message::Tool(t) => {
- let mut cm =
- TranscriptMessage::tool(encode_tool_envelope(&t.tool_call_id, &msg.text()));
+ let mut cm = TranscriptMessage::tool_result(t.tool_call_id.clone(), msg.text());
cm.id = Some(t.tool_call_id.clone());
cm
}
@@ -452,6 +452,33 @@ pub(crate) fn message_to_native_chat_message(msg: &Message) -> Option<Transcript
})
}
+/// The row of a user message: its text, plus ordered parts when it carries an
+/// image. Json / provider-extension blocks carry no user-visible text, so they
+/// are dropped, as [`Message::text`] drops them.
+fn user_row(user: &UserMessage) -> TranscriptMessage {
+ let has_image = user
+ .content
+ .iter()
+ .any(|block| matches!(block, ContentBlock::Image(_)));
+ if !has_image {
+ return TranscriptMessage::user(Message::User(user.clone()).text());
+ }
+ TranscriptMessage::user_with_parts(
+ user.content
+ .iter()
+ .filter_map(|block| match block {
+ ContentBlock::Text(text) if !text.is_empty() => {
+ Some(TranscriptPart::Text { text: text.clone() })
+ }
+ ContentBlock::Image(image) => Some(TranscriptPart::Image {
+ url: image.url.clone(),
+ }),
+ _ => None,
+ })
+ .collect(),
+ )
+}
+
/// Convert a harness transcript into typed dialect entries, preserving
/// assistant tool-call structure (`AssistantToolCalls`) and tool results
/// (`ToolResults`) instead of flattening tool calls to text.