use crate::prompt::{load_prompt, substitute};
use crate::retry::RetryExhausted;
use crate::util::INJECTED_IMAGE_TAG;
use crate::{ChatMessage, ChatRole};
const INPUT_INSPECTION_CODE: &str = "data_inspection_failed";
const INAPPROPRIATE_CONTENT_FRAGMENT: &str = "inappropriate content";
const REASON_FALLBACK: &str = "no specific reason was provided";
const REASON_CAP: usize = 500;
const PHRASE_ASSET: &str = "context/image_rejected.md";
#[must_use]
pub(crate) fn detect_input_image_rejection(
exhausted: &RetryExhausted,
history: &[ChatMessage],
) -> Option<usize> {
let trail: Vec<String> = exhausted
.failures
.iter()
.map(|f| f.error_chain.to_lowercase())
.collect();
let has_code = trail.iter().any(|t| t.contains(INPUT_INSPECTION_CODE));
let has_image_fragment = trail
.iter()
.any(|t| t.contains(INAPPROPRIATE_CONTENT_FRAGMENT) && t.contains("image"));
if !has_code || !has_image_fragment {
return None;
}
let idx = history.iter().rposition(|m| m.role == ChatRole::User)?;
has_image_marker(&history[idx].content).then_some(idx)
}
fn has_image_marker(content: &str) -> bool {
crate::util::MEDIA_MARKER_RE
.captures_iter(content)
.any(|caps| crate::util::parse_media_marker(&caps).0 == crate::util::MediaMarkerKind::Image)
}
#[must_use]
pub(crate) fn extract_provider_reason(exhausted: &RetryExhausted) -> Option<String> {
exhausted
.failures
.iter()
.find_map(|f| reason_from_chain(&f.error_chain))
}
fn reason_from_chain(chain: &str) -> Option<String> {
let sep = chain.find("): ")?;
let body = &chain[sep + 3..];
let body_json = serde_json::from_str::<serde_json::Value>(body).ok()?;
crate::util::extract_provider_error_detail(&body_json)
}
#[must_use]
pub(crate) fn strip_image_markers(content: &str, reason: Option<&str>) -> String {
let reason = reason.unwrap_or(REASON_FALLBACK);
let sanitized = crate::util::truncate(reason, REASON_CAP);
let sanitized = sanitized
.trim_end_matches(|c: char| c == '.' || c.is_whitespace())
.to_string();
let phrase = substitute(
&load_prompt(PHRASE_ASSET),
&[("{{reason}}", sanitized.as_str())],
);
let mut out = String::with_capacity(content.len() + phrase.len());
let mut first = true;
let mut last_end = 0usize;
for caps in crate::util::MEDIA_MARKER_RE.captures_iter(content) {
let (kind, _) = crate::util::parse_media_marker(&caps);
if kind != crate::util::MediaMarkerKind::Image {
continue;
}
let whole = caps.get_match();
if first {
let seg = &content[last_end..whole.start()];
if seg.contains(INJECTED_IMAGE_TAG) {
let cleaned = seg.replace(INJECTED_IMAGE_TAG, "").trim().to_string();
out.push_str(&cleaned);
} else {
out.push_str(seg);
}
out.push_str(&phrase);
first = false;
} else {
out.push_str(&content[last_end..whole.start()]);
}
last_end = whole.end();
}
out.push_str(&content[last_end..]);
out
}
#[cfg(test)]
mod tests {
use super::*;
use crate::retry::{FailureClass, RetryFailureRecord};
fn trail(error_chain: &str) -> RetryExhausted {
RetryExhausted::with_last_raw(
vec![RetryFailureRecord::new_simple(
FailureClass::InvalidRequest,
&anyhow::anyhow!("{error_chain}"),
None,
)],
FailureClass::InvalidRequest,
None,
)
}
const REJECTION_CHAIN: &str = r#"OpenRouter API error (400): {"error":{"message":"Input image data may contain inappropriate content.","code":"data_inspection_failed","type":"invalid_request_error"}}"#;
fn history_with_image() -> Vec<ChatMessage> {
vec![
ChatMessage::system("role description"),
ChatMessage::user("earlier text without image"),
ChatMessage::user("[IMAGE:/tmp/photo.png]\n\ndescribe this"),
]
}
#[test]
fn image_rejection_detected_on_most_recent_user_message() {
let idx = detect_input_image_rejection(&trail(REJECTION_CHAIN), &history_with_image());
assert_eq!(
idx,
Some(2),
"the image-bearing user message must be targeted"
);
}
#[test]
fn detection_gate_keys_on_the_image_inspection_code_and_wording() {
let cases: &[(&str, Option<usize>)] = &[
(
r#"OpenRouter API error (400): {"error":{"message":"Input data may contain inappropriate content.","code":"data_inspection_failed","type":"invalid_request_error"}}"#,
None,
),
(
r#"OpenRouter API error (400): {"error":{"message":"Input image data may contain inappropriate content.","code":"other_code","type":"invalid_request_error"}}"#,
None,
),
(
r#"OpenRouter API error (400): {"error":{"message":"INPUT IMAGE DATA MAY CONTAIN INAPPROPRIATE CONTENT.","code":"DATA_INSPECTION_FAILED"}}"#,
Some(2),
),
];
for (chain, expected) in cases {
assert_eq!(
detect_input_image_rejection(&trail(chain), &history_with_image()),
*expected,
"unexpected decision for: {chain}"
);
}
}
#[test]
fn strip_targets_only_the_most_recent_user_message() {
let history = vec![
ChatMessage::system("role description"),
ChatMessage::user("[IMAGE:/tmp/old.png] old image"),
ChatMessage::user("a later text-only message"),
];
assert_eq!(
detect_input_image_rejection(&trail(REJECTION_CHAIN), &history),
None
);
let history = vec![
ChatMessage::user("[IMAGE:/tmp/old.png] old"),
ChatMessage::user("[IMAGE:/tmp/new.png] new"),
];
let idx = detect_input_image_rejection(&trail(REJECTION_CHAIN), &history);
assert_eq!(idx, Some(1));
}
#[test]
fn malformed_or_empty_marker_does_not_trigger_strip() {
for content in [
"hello [IMAGE:] world",
"unclosed [IMAGE:foo",
"trailing [IMAGE:",
"[AUDIO:/tmp/sound.mp3] audio-only",
] {
let history = vec![ChatMessage::system("role"), ChatMessage::user(content)];
assert_eq!(
detect_input_image_rejection(&trail(REJECTION_CHAIN), &history),
None,
"content with no strippable IMAGE marker must not trigger: {content:?}"
);
}
}
#[test]
fn malformed_latest_marker_with_earlier_image_is_conservative_noop() {
let history = vec![
ChatMessage::user("[IMAGE:/tmp/old.png] earlier image"),
ChatMessage::user("see [IMAGE: here"),
];
assert_eq!(
detect_input_image_rejection(&trail(REJECTION_CHAIN), &history),
None
);
}
#[test]
fn strip_rewrites_image_markers_and_keeps_the_rest() {
type Case<'a> = (&'a str, Option<&'a str>, &'a [&'a str], &'a [&'a str]);
let phrase = "rejected by the provider's content-inspection check";
let cases: &[Case] = &[
(
"see [IMAGE:/tmp/a.png] and [IMAGE:/tmp/b.png] here",
Some("the image is blocked"),
&["see ", "and ", "here", phrase, "the image is blocked"],
&["[IMAGE:"],
),
(
"[AUDIO:/tmp/sound.mp3] listen and [IMAGE:/tmp/img.png] look",
None,
&["[AUDIO:/tmp/sound.mp3]", "listen and", "look", phrase],
&["[IMAGE:"],
),
(
"<injected-tool-result-image>\n[IMAGE:data:image/jpeg;base64,abc]",
Some("blocked"),
&[phrase],
&["[IMAGE:", "<injected-tool-result-image>", "\n"],
),
];
for (content, reason, present, absent) in cases {
let out = strip_image_markers(content, *reason);
for needle in *present {
assert!(out.contains(needle), "missing {needle:?}: {out}");
}
for needle in *absent {
assert!(!out.contains(needle), "unexpected {needle:?}: {out}");
}
assert!(
!out.starts_with([' ', '\n']),
"no dangling whitespace before the phrase: {out:?}"
);
}
}
#[test]
fn reason_extracted_from_http_error_body() {
let reason = extract_provider_reason(&trail(REJECTION_CHAIN));
assert_eq!(
reason.as_deref(),
Some("Input image data may contain inappropriate content.")
);
}
}