From fdef2b9d615dfa4b3eedf876c105a3813b564193 Mon Sep 17 00:00:00 2001 From: Musa Date: Tue, 10 Mar 2026 11:35:49 -0700 Subject: [PATCH] message extraction logic in ResponsesAPIRequest --- crates/hermesllm/src/apis/openai_responses.rs | 85 +++++++++++++------ 1 file changed, 60 insertions(+), 25 deletions(-) diff --git a/crates/hermesllm/src/apis/openai_responses.rs b/crates/hermesllm/src/apis/openai_responses.rs index 09999586..424430b6 100644 --- a/crates/hermesllm/src/apis/openai_responses.rs +++ b/crates/hermesllm/src/apis/openai_responses.rs @@ -1050,39 +1050,74 @@ impl ProviderRequest for ResponsesAPIRequest { } fn extract_messages_text(&self) -> String { - fn content_items_to_text(content_items: &[InputContent]) -> String { - content_items.iter().fold(String::new(), |acc, content| { - acc + " " - + &match content { - InputContent::InputText { text } => text.clone(), - InputContent::InputImage { .. } => "[Image]".to_string(), - InputContent::InputFile { .. } => "[File]".to_string(), - InputContent::InputAudio { .. } => "[Audio]".to_string(), - } - }) - } - - fn message_content_to_text(content: &MessageContent) -> String { - match content { - MessageContent::Text(text) => text.clone(), - MessageContent::Items(content_items) => content_items_to_text(content_items), - } - } - match &self.input { InputParam::Text(text) => text.clone(), InputParam::SingleItem(item) => { // Normalize single-item input for extraction behavior parity. match item { - InputItem::Message(msg) => message_content_to_text(&msg.content), + InputItem::Message(msg) => { + let mut extracted = String::new(); + match &msg.content { + MessageContent::Text(text) => extracted.push_str(text), + MessageContent::Items(content_items) => { + for content in content_items { + // Preserve existing behavior: each content item is prefixed with a space. + extracted.push(' '); + match content { + InputContent::InputText { text } => { + extracted.push_str(text) + } + InputContent::InputImage { .. } => { + extracted.push_str("[Image]") + } + InputContent::InputFile { .. } => { + extracted.push_str("[File]") + } + InputContent::InputAudio { .. } => { + extracted.push_str("[Audio]") + } + } + } + } + } + extracted + } _ => String::new(), } } - InputParam::Items(items) => items.iter().fold(String::new(), |acc, item| match item { - InputItem::Message(msg) => acc + " " + &message_content_to_text(&msg.content), - // Skip non-message items (references, outputs, etc.) - _ => acc, - }), + InputParam::Items(items) => { + let mut extracted = String::new(); + for item in items { + if let InputItem::Message(msg) = item { + // Preserve existing behavior: each message is prefixed with a space. + extracted.push(' '); + match &msg.content { + MessageContent::Text(text) => extracted.push_str(text), + MessageContent::Items(content_items) => { + for content in content_items { + // Preserve existing behavior: each content item is prefixed with a space. + extracted.push(' '); + match content { + InputContent::InputText { text } => { + extracted.push_str(text) + } + InputContent::InputImage { .. } => { + extracted.push_str("[Image]") + } + InputContent::InputFile { .. } => { + extracted.push_str("[File]") + } + InputContent::InputAudio { .. } => { + extracted.push_str("[Audio]") + } + } + } + } + } + } + } + extracted + } } }