The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
fix(agent): handle empty message content in agent messages
Steven Enamakel888e8aab2480
diff --git a/crates/openhuman-core/src/agent/messages.rs b/crates/openhuman-core/src/agent/messages.rsindex 1ce3da0182..0f1bcb3094 100644--- a/crates/openhuman-core/src/agent/messages.rs+++ b/crates/openhuman-core/src/agent/messages.rs@@ -1,269 +1,83 @@-//! Durable OpenHuman transcript records.+//! Wire adapters for the message rows OpenHuman persists itself.//!-//! Runtime model calls use [`tinyinference_llm::message::Message`]. These-//! compact records preserve the stable JSONL/thread storage contract used by-//! existing installations and carry OpenHuman-only message metadata.--use serde::{Deserialize, Serialize};--use tinytools_agent::dialect::NativeToolCall;--const TURN_USAGE_METADATA_KEY: &str = "openhuman_turn_usage";-const TOOL_FAILURE_METADATA_KEY: &str = "openhuman_tool_failure";-const REPLAYED_METADATA_KEY: &str = "openhuman_replayed";-const WRAPPED_VALUE_KEY: &str = "openhuman_wrapped_value";-const WRAPPED_FLAG: &str = "wrapped";--fn would_wrap(message: &ChatMessage) -> bool {- matches!(&message.extra_metadata, Some(value) if !value.is_object())-}--fn insert_host_metadata(message: &mut ChatMessage, key: &str, value: serde_json::Value) {- let mut map = match message.extra_metadata.take() {- Some(serde_json::Value::Object(map)) => map,- Some(existing) => {- let mut map = serde_json::Map::new();- map.insert(WRAPPED_VALUE_KEY.to_string(), existing);- map- }- None => serde_json::Map::new(),- };- map.insert(key.to_string(), value);- message.extra_metadata = Some(serde_json::Value::Object(map));-}--fn take_host_metadata(- extra: &mut Option<serde_json::Value>,- key: &str,-) -> Option<serde_json::Value> {- let serde_json::Value::Object(map) = extra.as_mut()? else {- return None;- };- let value = map.remove(key)?;- let wrapped = value- .get(WRAPPED_FLAG)- .and_then(serde_json::Value::as_bool)- .unwrap_or(false);- if map.is_empty() {- *extra = None;- } else if wrapped {- match [TOOL_FAILURE_METADATA_KEY, REPLAYED_METADATA_KEY]- .into_iter()- .find(|remaining| map.contains_key(*remaining))- {- Some(carrier) => {- if let Some(serde_json::Value::Object(marker)) = map.get_mut(carrier) {- marker.insert(WRAPPED_FLAG.to_string(), serde_json::Value::Bool(true));- }- }- None if map.len() == 1 => {- if let Some(original) = map.remove(WRAPPED_VALUE_KEY) {- *extra = Some(original);- }- }- None => {}- }- }- Some(value)-}--/// Host-only compatibility marker for an execution failure that the provider-/// message dialect cannot represent. The durable session format itself uses-/// `TranscriptMessage::tool_failure`; this marker exists only while a row is-/// adapted through OpenHuman's `ChatMessage` runtime shape.-pub(crate) fn attach_chat_tool_failure_metadata(message: &mut ChatMessage, detail: Option<&str>) {- let mut payload = serde_json::Map::new();- payload.insert("failure".to_string(), serde_json::Value::Bool(true));- if let Some(detail) = detail.map(str::trim).filter(|detail| !detail.is_empty()) {- payload.insert(- "detail".to_string(),- serde_json::Value::String(detail.to_string()),- );- }- if would_wrap(message) {- payload.insert(WRAPPED_FLAG.to_string(), serde_json::Value::Bool(true));- }- insert_host_metadata(- message,- TOOL_FAILURE_METADATA_KEY,- serde_json::Value::Object(payload),- );-}--pub(crate) fn attach_chat_turn_usage_metadata(- message: &mut ChatMessage,- usage: &tinyagents_session::transcript::TurnUsage,-) {- if let Ok(payload) = serde_json::to_value(usage) {- insert_host_metadata(message, TURN_USAGE_METADATA_KEY, payload);- }-}--#[derive(Debug, Clone, Serialize, Deserialize)]-pub struct ChatMessage {- #[serde(default, skip_serializing)]- pub id: Option<String>,- pub role: String,- pub content: String,- #[serde(default, skip_serializing)]- pub extra_metadata: Option<serde_json::Value>,- /// Ascending byte offsets into [`Self::content`] at which the provider may- /// place a prompt-cache breakpoint. Only meaningful on the system message.- ///- /// `skip_serializing` applies only to this host runtime shape: these are a- /// property of a provider call, not this type's wire representation. The- /// explicit adapter persists them in `TranscriptMessage` so a transcript- /// round trip remains lossless; a later host may still discard or rebuild- /// them when assembling its next prompt. `serde(default)` keeps existing- /// host-serialized records loadable.- #[serde(default, skip_serializing)]- pub cache_breakpoints: Vec<usize>,+//! The runtime, the transcript writer and the host all speak the one+//! `tinyagents_session::transcript::TranscriptMessage` row; there is no+//! host-owned message type. What remains here is the compatibility contract for+//! the two host-owned files that embed rows:+//!+//! - the durable sub-agent session store (`subagent_sessions.json`,+//! `latestHistory`), and+//! - the sub-agent pause checkpoint (`history`).+//!+//! Both were written as `{ "role", "content" }` pairs only: the old host type+//! never serialized its `id`, `extra_metadata` or `cache_breakpoints`.+//! [`history_wire`] keeps writing exactly that shape, so a binary of either+//! generation reads what the other wrote, and reads any row shape (the+//! vendor row is a superset whose extra fields default).++use serde::ser::SerializeSeq;+use serde::{Deserialize, Deserializer, Serialize, Serializer};+use tinyagents_session::transcript::TranscriptMessage;++#[derive(Serialize)]+struct WireRow<'a> {+ role: &'a str,+ content: &'a str,}-/// Convert the host's provider-facing record into the neutral durable-/// transcript record. This is intentionally a field-for-field conversion at-/// the host boundary: `tinyagents-session` must not know OpenHuman's runtime-/// message type, while no provider metadata may be discarded before durable-/// persistence.-pub(crate) fn transcript_message_from_chat(- message: &ChatMessage,-) -> tinyagents_session::transcript::TranscriptMessage {- let mut extra_metadata = message.extra_metadata.clone();- let turn_usage = take_host_metadata(&mut extra_metadata, TURN_USAGE_METADATA_KEY)- .and_then(|value| serde_json::from_value(value).ok());- let tool_failure =- take_host_metadata(&mut extra_metadata, TOOL_FAILURE_METADATA_KEY).map(|value| {- tinyagents_session::transcript::ToolFailure {- failed: true,- detail: value- .get("detail")- .and_then(serde_json::Value::as_str)- .map(str::to_string),- }- });- let replayed = take_host_metadata(&mut extra_metadata, REPLAYED_METADATA_KEY);- let request_id = replayed.as_ref().and_then(|value| {- value- .get("request_id")- .and_then(serde_json::Value::as_str)- .map(str::to_string)- });- tinyagents_session::transcript::TranscriptMessage {- id: message.id.clone(),- role: message.role.clone(),- content: message.content.clone(),- extra_metadata,- cache_breakpoints: message.cache_breakpoints.clone(),- turn_usage,- request_id,- preserve_request_id: replayed.is_some(),- interrupted: false,- tool_failure,+fn serialize_rows<S: Serializer>(+ rows: &[TranscriptMessage],+ serializer: S,+) -> Result<S::Ok, S::Error> {+ let mut seq = serializer.serialize_seq(Some(rows.len()))?;+ for row in rows {+ seq.serialize_element(&WireRow {+ role: &row.role,+ content: &row.content,+ })?;}+ seq.end()}-/// Convert a neutral durable transcript row back into the host's provider-/// message shape after the lossless transcript reader has completed replay.-pub(crate) fn chat_message_from_transcript(- message: tinyagents_session::transcript::TranscriptMessage,-) -> ChatMessage {- let mut chat = ChatMessage {- id: message.id,- role: message.role,- content: message.content,- extra_metadata: message.extra_metadata,- cache_breakpoints: message.cache_breakpoints,- };- if let Some(usage) = message.turn_usage.as_ref() {- attach_chat_turn_usage_metadata(&mut chat, usage);- }- if let Some(failure) = message- .tool_failure- .as_ref()- .filter(|failure| failure.failed)- {- attach_chat_tool_failure_metadata(&mut chat, failure.detail.as_deref());- }- if message.preserve_request_id {- let mut payload = serde_json::Map::new();- payload.insert(- "request_id".to_string(),- message- .request_id- .map(serde_json::Value::String)- .unwrap_or(serde_json::Value::Null),- );- if would_wrap(&chat) {- payload.insert(WRAPPED_FLAG.to_string(), serde_json::Value::Bool(true));- }- insert_host_metadata(- &mut chat,- REPLAYED_METADATA_KEY,- serde_json::Value::Object(payload),- );- }- chat-}+/// `#[serde(with = "…::history_wire")]` for a `Vec<TranscriptMessage>` field.+pub mod history_wire {+ use super::*;-impl ChatMessage {- pub fn system(content: impl Into<String>) -> Self {- Self {- id: None,- role: "system".into(),- content: content.into(),- extra_metadata: None,- cache_breakpoints: Vec::new(),- }+ pub fn serialize<S: Serializer>(+ rows: &[TranscriptMessage],+ serializer: S,+ ) -> Result<S::Ok, S::Error> {+ serialize_rows(rows, serializer)}- pub fn user(content: impl Into<String>) -> Self {- Self {- id: None,- role: "user".into(),- content: content.into(),- extra_metadata: None,- cache_breakpoints: Vec::new(),- }+ pub fn deserialize<'de, D: Deserializer<'de>>(+ deserializer: D,+ ) -> Result<Vec<TranscriptMessage>, D::Error> {+ Vec::<TranscriptMessage>::deserialize(deserializer)}- pub fn assistant(content: impl Into<String>) -> Self {- Self {- id: None,- role: "assistant".into(),- content: content.into(),- extra_metadata: None,- cache_breakpoints: Vec::new(),+ /// The same contract for an `Option<Vec<TranscriptMessage>>` field.+ pub mod option {+ use super::*;++ pub fn serialize<S: Serializer>(+ rows: &Option<Vec<TranscriptMessage>>,+ serializer: S,+ ) -> Result<S::Ok, S::Error> {+ match rows {+ Some(rows) => serialize_rows(rows, serializer),+ None => serializer.serialize_none(),+ }}- }- pub fn tool(content: impl Into<String>) -> Self {- Self {- id: None,- role: "tool".into(),- content: content.into(),- extra_metadata: None,- cache_breakpoints: Vec::new(),+ pub fn deserialize<'de, D: Deserializer<'de>>(+ deserializer: D,+ ) -> Result<Option<Vec<TranscriptMessage>>, D::Error> {+ Option::<Vec<TranscriptMessage>>::deserialize(deserializer)}}}-#[derive(Debug, Clone, Serialize, Deserialize)]-pub struct ToolResultMessage {- pub tool_call_id: String,- pub content: String,-}--#[derive(Debug, Clone, Serialize, Deserialize)]-#[serde(tag = "type", content = "data")]-pub enum ConversationMessage {- Chat(ChatMessage),- AssistantToolCalls {- text: Option<String>,- tool_calls: Vec<NativeToolCall>,- #[serde(default, skip_serializing_if = "Option::is_none")]- reasoning_content: Option<String>,- #[serde(default, skip_serializing_if = "Option::is_none")]- extra_metadata: Option<serde_json::Value>,- },- ToolResults(Vec<ToolResultMessage>),-}+#[cfg(test)]+#[path = "messages_tests.rs"]+mod tests;