fix: parse flat content arrays in Responses API input

When input is [{type: 'input_image', ...}, {type: 'input_text', text: '...'}],
the code was looking for items with role: 'user' which don't exist in flat
content arrays. Now extracts text from input_text items directly first,
falling back to role-based messages only if no flat text found.

Also adds debug header dump for MITM request forwarding.
This commit is contained in:
Nikketryhard
2026-02-15 18:10:03 -06:00
parent 1a6bfa5b53
commit 371c57bab0
2 changed files with 51 additions and 25 deletions

View File

@@ -80,33 +80,49 @@ fn extract_responses_input(
text_items
}
} else {
// Normal input extraction (existing logic)
items
// Normal input extraction
// First try: flat content parts (input_text / input_image)
let flat_text: String = items
.iter()
.rev()
.find(|item| item["role"].as_str() == Some("user"))
.and_then(|item| {
// Also scan content array for images
if image.is_none() {
image = super::util::extract_first_image(&item["content"]);
}
match &item["content"] {
serde_json::Value::String(s) => Some(s.clone()),
serde_json::Value::Array(parts) => Some(
parts
.iter()
.filter(|p| {
let t = p["type"].as_str().unwrap_or("");
t == "input_text" || t == "text"
})
.filter_map(|p| p["text"].as_str())
.collect::<Vec<_>>()
.join(" "),
),
_ => None,
}
.filter(|item| {
let t = item["type"].as_str().unwrap_or("");
t == "input_text" || t == "text"
})
.unwrap_or_default()
.filter_map(|p| p["text"].as_str())
.collect::<Vec<_>>()
.join(" ");
if !flat_text.is_empty() {
flat_text
} else {
// Fallback: conversation-style with role: "user"
items
.iter()
.rev()
.find(|item| item["role"].as_str() == Some("user"))
.and_then(|item| {
// Also scan content array for images
if image.is_none() {
image = super::util::extract_first_image(&item["content"]);
}
match &item["content"] {
serde_json::Value::String(s) => Some(s.clone()),
serde_json::Value::Array(parts) => Some(
parts
.iter()
.filter(|p| {
let t = p["type"].as_str().unwrap_or("");
t == "input_text" || t == "text"
})
.filter_map(|p| p["text"].as_str())
.collect::<Vec<_>>()
.join(" "),
),
_ => None,
}
})
.unwrap_or_default()
}
}
}
_ => String::new(),