Extract user input preparation into codex-user-input (#61949)

## What changed

Move local attachment snapshotting and conversion from `UserInput` to model request content out of `codex-protocol` into a dedicated `codex-user-input` crate. Update core, the TUI, and the queue extension to use the new crate, replacing the `ResponseInputItem` conversion methods with `from_user_input`.

Move the associated attachment and message conversion tests alongside the implementation, preserving coverage for image positions, media labels, serialization, and error placeholders.

GitOrigin-RevId: 77d0f94a4cd4c2edc008b9e2e63a989995aab058
This commit is contained in:
Adam Perry @ OpenAI
2026-11-17 02:43:52 +00:00
committed by copyberry
parent ad161203cb
commit 5b3d2c7fce
24 changed files with 814 additions and 723 deletions
+13
View File
@@ -0,0 +1,13 @@
//! Captures user attachments and renders resolved input for model requests.
mod local_media;
mod message;
pub use local_media::audio_mime_for_path;
pub use local_media::snapshot_local_user_input;
pub use message::LocalImagePreparation;
pub use message::from_user_input;
#[cfg(test)]
#[path = "message_tests.rs"]
mod message_tests;
+93
View File
@@ -0,0 +1,93 @@
//! Reads bounded attachments from the client-facing host.
use std::io;
use std::io::Read;
use std::path::Path;
use codex_utils_image::MAX_PROMPT_IMAGE_INPUT_BYTES;
use codex_utils_image::PromptImageMode;
use codex_utils_image::data_url_from_bytes;
use codex_utils_image::load_for_prompt_bytes;
use codex_protocol::models::AudioReference;
use codex_protocol::models::ImageDetail;
use codex_protocol::models::ImageReference;
use codex_protocol::models::MAX_PROMPT_AUDIO_INPUT_BYTES;
use codex_protocol::user_input::UserInput;
/// Snapshots local image and audio input into portable, validated data URLs.
pub fn snapshot_local_user_input(input: &mut UserInput) -> io::Result<()> {
match input {
UserInput::LocalImage { path, detail } => {
let image_detail = *detail;
let mode = match image_detail {
Some(ImageDetail::Original) => PromptImageMode::Original,
Some(ImageDetail::Auto | ImageDetail::Low | ImageDetail::High) | None => {
PromptImageMode::ResizeToFit
}
};
let file_bytes = read_bounded_local_media(path, MAX_PROMPT_IMAGE_INPUT_BYTES, "image")?;
let image = load_for_prompt_bytes(path, file_bytes, mode).map_err(io::Error::other)?;
*input = UserInput::Image {
image: ImageReference::Inline {
image_url: image.into_data_url(),
},
detail: image_detail,
};
}
UserInput::LocalAudio { path } => {
let mime = audio_mime_for_path(path).ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidData, "unsupported audio format")
})?;
let file_bytes = read_bounded_local_media(path, MAX_PROMPT_AUDIO_INPUT_BYTES, "audio")?;
*input = UserInput::Audio {
audio: AudioReference::Inline {
audio_url: data_url_from_bytes(mime, &file_bytes),
},
};
}
_ => {}
}
Ok(())
}
fn read_bounded_local_media(path: &Path, max_bytes: usize, kind: &str) -> io::Result<Vec<u8>> {
let file = std::fs::File::open(path)?;
if file.metadata()?.len() > max_bytes as u64 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("{kind} input exceeds {max_bytes} bytes"),
));
}
let mut bytes = Vec::new();
file.take(max_bytes as u64 + 1).read_to_end(&mut bytes)?;
if bytes.len() > max_bytes {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("{kind} input exceeds {max_bytes} bytes"),
));
}
Ok(bytes)
}
pub fn audio_mime_for_path(path: &Path) -> Option<&'static str> {
let extension = path.extension()?.to_str()?;
if extension.eq_ignore_ascii_case("wav") {
Some("audio/wav")
} else if extension.eq_ignore_ascii_case("mp3") {
Some("audio/mpeg")
} else if extension.eq_ignore_ascii_case("m4a") {
Some("audio/mp4")
} else if extension.eq_ignore_ascii_case("webm") {
Some("audio/webm")
} else if extension.eq_ignore_ascii_case("ogg") {
Some("audio/ogg")
} else {
None
}
}
#[cfg(test)]
#[path = "local_media_tests.rs"]
mod tests;
@@ -0,0 +1,134 @@
#![cfg(test)]
use super::*;
use anyhow::Result;
use pretty_assertions::assert_eq;
use tempfile::tempdir;
const TINY_PNG_BYTES: &[u8] = &[
137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 1, 0, 0, 0, 1, 8, 6, 0,
0, 0, 31, 21, 196, 137, 0, 0, 0, 11, 73, 68, 65, 84, 120, 156, 99, 96, 0, 2, 0, 0, 5, 0, 1,
122, 94, 171, 63, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130,
];
#[test]
fn snapshots_local_image_user_input_with_requested_detail() -> Result<()> {
let temp_dir = tempdir()?;
let image_path = temp_dir.path().join("sample.png");
std::fs::write(&image_path, TINY_PNG_BYTES)?;
for detail in [
None,
Some(ImageDetail::Auto),
Some(ImageDetail::Low),
Some(ImageDetail::High),
Some(ImageDetail::Original),
] {
let mut input = UserInput::LocalImage {
path: image_path.clone(),
detail,
};
snapshot_local_user_input(&mut input)?;
assert_eq!(
UserInput::Image {
image: codex_protocol::models::ImageReference::Inline {
image_url: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVR4nGNgAAIAAAUAAXpeqz8AAAAASUVORK5CYII="
.to_string(),
},
detail,
},
input
);
}
Ok(())
}
#[test]
fn snapshots_local_audio_user_input_with_supported_mime_types() -> Result<()> {
let temp_dir = tempdir()?;
for (extension, mime) in [
("wav", "audio/wav"),
("MP3", "audio/mpeg"),
("m4a", "audio/mp4"),
("webm", "audio/webm"),
("ogg", "audio/ogg"),
] {
let audio_path = temp_dir.path().join(format!("sample.{extension}"));
std::fs::write(&audio_path, b"audio")?;
let mut input = UserInput::LocalAudio { path: audio_path };
snapshot_local_user_input(&mut input)?;
assert_eq!(
UserInput::Audio {
audio: AudioReference::Inline {
audio_url: format!("data:{mime};base64,YXVkaW8=")
},
},
input
);
}
Ok(())
}
#[test]
fn rejects_invalid_local_media_without_changing_user_input() -> Result<()> {
let temp_dir = tempdir()?;
let invalid_image_path = temp_dir.path().join("invalid.png");
let unsupported_audio_path = temp_dir.path().join("unsupported.flac");
std::fs::write(&invalid_image_path, b"invalid image")?;
std::fs::write(&unsupported_audio_path, b"audio")?;
let mut invalid_image = UserInput::LocalImage {
path: invalid_image_path,
detail: Some(ImageDetail::Original),
};
let original_image = invalid_image.clone();
let image_error = snapshot_local_user_input(&mut invalid_image)
.expect_err("an invalid local image must be rejected");
assert_eq!(io::ErrorKind::Other, image_error.kind());
assert_eq!(original_image, invalid_image);
let mut unsupported_audio = UserInput::LocalAudio {
path: unsupported_audio_path,
};
let original_audio = unsupported_audio.clone();
let audio_error = snapshot_local_user_input(&mut unsupported_audio)
.expect_err("an unsupported local audio format must be rejected");
assert_eq!(io::ErrorKind::InvalidData, audio_error.kind());
assert_eq!(original_audio, unsupported_audio);
Ok(())
}
#[test]
fn rejects_oversized_local_media_without_reading_it() -> Result<()> {
let temp_dir = tempdir()?;
for (name, limit, is_image) in [
("oversized.png", MAX_PROMPT_IMAGE_INPUT_BYTES, true),
("oversized.mp3", MAX_PROMPT_AUDIO_INPUT_BYTES, false),
] {
let path = temp_dir.path().join(name);
std::fs::File::create(&path)?.set_len(limit as u64 + 1)?;
let mut input = if is_image {
UserInput::LocalImage { path, detail: None }
} else {
UserInput::LocalAudio { path }
};
let original = input.clone();
let error = snapshot_local_user_input(&mut input)
.expect_err("oversized local media must be rejected before it is read");
assert_eq!(io::ErrorKind::InvalidData, error.kind());
assert_eq!(original, input);
}
Ok(())
}
+160
View File
@@ -0,0 +1,160 @@
//! Converts user inputs to model content and records image positions for UI events.
use crate::local_media::audio_mime_for_path;
use codex_protocol::models::AudioReference;
use codex_protocol::models::ContentItem;
use codex_protocol::models::DEFAULT_IMAGE_DETAIL;
use codex_protocol::models::ResponseInputItem;
use codex_protocol::models::audio_close_tag_text;
use codex_protocol::models::local_audio_open_tag_text_with_path;
use codex_protocol::models::local_image_content_items;
use codex_protocol::models::local_image_content_items_with_label_number;
use codex_protocol::user_input::UserInput;
use codex_utils_image::data_url_from_bytes;
use std::collections::HashMap;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LocalMediaKind {
Audio,
Image,
}
impl LocalMediaKind {
fn name(self) -> &'static str {
match self {
Self::Audio => "audio",
Self::Image => "image",
}
}
}
fn local_media_error_placeholder(
path: &std::path::Path,
error: impl std::fmt::Display,
media_kind: LocalMediaKind,
) -> ContentItem {
let media_name = media_kind.name();
let path = path.display();
ContentItem::InputText {
text: format!("Codex could not read the local {media_name} at `{path}`: {error}"),
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LocalImagePreparation {
Process,
Defer,
}
fn unsupported_audio_error_placeholder(path: &std::path::Path) -> ContentItem {
ContentItem::InputText {
text: format!(
"Codex cannot attach audio at `{}`: unsupported audio format; use wav, mp3, m4a, webm, or ogg.",
path.display()
),
}
}
fn local_audio_content_items(
path: &std::path::Path,
file_bytes: &[u8],
label_number: usize,
) -> Vec<ContentItem> {
let Some(mime) = audio_mime_for_path(path) else {
return vec![unsupported_audio_error_placeholder(path)];
};
vec![
ContentItem::InputText {
text: local_audio_open_tag_text_with_path(label_number, path),
},
ContentItem::InputAudio {
audio: AudioReference::Inline {
audio_url: data_url_from_bytes(mime, file_bytes),
},
},
ContentItem::InputText {
text: audio_close_tag_text(),
},
]
}
pub fn from_user_input(
items: Vec<UserInput>,
local_image_preparation: LocalImagePreparation,
user_image_content_indices: &mut HashMap<usize, usize>,
) -> ResponseInputItem {
user_image_content_indices.clear();
let mut image_index = 0;
let mut audio_index = 0;
let mut content = Vec::new();
for (input_index, input) in items.into_iter().enumerate() {
let generated = match input {
UserInput::Text { text, .. } => vec![ContentItem::InputText { text }],
UserInput::Image { image, detail, .. } => {
image_index += 1;
let detail = detail.unwrap_or(DEFAULT_IMAGE_DETAIL);
vec![ContentItem::InputImage {
image,
detail: Some(detail),
}]
}
UserInput::LocalImage { path, detail, .. } => {
image_index += 1;
let detail = detail.unwrap_or(DEFAULT_IMAGE_DETAIL);
match std::fs::read(&path) {
Ok(file_bytes) => match local_image_preparation {
LocalImagePreparation::Process => {
local_image_content_items_with_label_number(
&path,
file_bytes,
Some(image_index),
detail,
)
}
LocalImagePreparation::Defer => local_image_content_items(
&path,
data_url_from_bytes("application/octet-stream", &file_bytes),
Some(image_index),
detail,
),
},
Err(err) => vec![local_media_error_placeholder(
&path,
err,
LocalMediaKind::Image,
)],
}
}
UserInput::Audio { audio } => {
audio_index += 1;
vec![ContentItem::InputAudio { audio }]
}
UserInput::LocalAudio { path } => {
audio_index += 1;
match std::fs::read(&path) {
Ok(file_bytes) => local_audio_content_items(&path, &file_bytes, audio_index),
Err(err) => vec![local_media_error_placeholder(
&path,
err,
LocalMediaKind::Audio,
)],
}
}
_ => Vec::new(), // Skills and mentions are injected later in core.
};
// Local images add framing text, while mentions and skills add no content.
// Capture the image's actual slot before preparation replaces its reference.
for (offset, item) in generated.iter().enumerate() {
if matches!(item, ContentItem::InputImage { .. }) {
user_image_content_indices.insert(input_index, content.len() + offset);
}
}
content.extend(generated);
}
ResponseInputItem::Message {
role: "user".to_string(),
content,
phase: None,
}
}
+537
View File
@@ -0,0 +1,537 @@
#![cfg(test)]
use crate::LocalImagePreparation;
use crate::from_user_input;
use anyhow::Result;
use codex_protocol::models::AudioReference;
use codex_protocol::models::ContentItem;
use codex_protocol::models::DEFAULT_IMAGE_DETAIL;
use codex_protocol::models::ImageDetail;
use codex_protocol::models::ImageReference;
use codex_protocol::models::ResponseInputItem;
use codex_protocol::models::audio_close_tag_text;
use codex_protocol::models::image_close_tag_text;
use codex_protocol::models::local_audio_open_tag_text_with_path;
use codex_protocol::models::local_image_open_tag_text_with_path;
use codex_protocol::user_input::UserInput;
use codex_utils_image::data_url_from_bytes;
use pretty_assertions::assert_eq;
use std::collections::HashMap;
use std::path::PathBuf;
use tempfile::tempdir;
// A tiny valid PNG (1x1) so image conversion tests don't depend on cross-crate
// file paths, which break under Bazel sandboxing.
const TINY_PNG_BYTES: &[u8] = &[
137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 1, 0, 0, 0, 1, 8, 6, 0,
0, 0, 31, 21, 196, 137, 0, 0, 0, 11, 73, 68, 65, 84, 120, 156, 99, 96, 0, 2, 0, 0, 5, 0, 1,
122, 94, 171, 63, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130,
];
fn user_message(items: Vec<UserInput>) -> ResponseInputItem {
from_user_input(items, LocalImagePreparation::Process, &mut HashMap::new())
}
#[test]
fn serializes_image_user_input_without_tags() -> Result<()> {
let image_url = "data:image/png;base64,abc".to_string();
let item = user_message(vec![UserInput::Image {
image: ImageReference::Inline {
image_url: image_url.clone(),
},
detail: None,
}]);
match item {
ResponseInputItem::Message { content, .. } => {
let expected = vec![ContentItem::InputImage {
image: ImageReference::Inline { image_url },
detail: Some(DEFAULT_IMAGE_DETAIL),
}];
assert_eq!(content, expected);
}
other => panic!("expected message response but got {other:?}"),
}
Ok(())
}
/// A file-backed user image must reach the Responses API without being resolved by Core.
#[test]
fn file_image_user_input_serializes_file_id() -> Result<()> {
let file_id = "file_123".to_string();
let item = user_message(vec![UserInput::Image {
image: ImageReference::File {
file_id: file_id.clone(),
},
detail: None,
}]);
let expected = ResponseInputItem::Message {
role: "user".to_string(),
content: vec![ContentItem::InputImage {
image: ImageReference::File { file_id },
detail: Some(DEFAULT_IMAGE_DETAIL),
}],
phase: None,
};
assert_eq!(item, expected);
assert_eq!(
serde_json::to_value(item)?,
serde_json::json!({
"type": "message",
"role": "user",
"content": [{
"type": "input_image",
"file_id": "file_123",
"detail": "high"
}]
})
);
Ok(())
}
#[test]
fn serializes_audio_user_input_without_tags() -> Result<()> {
for (audio, wire) in [
(
AudioReference::Inline {
audio_url: "data:audio/wav;base64,abc".into(),
},
serde_json::json!({"type": "input_audio", "audio_url": "data:audio/wav;base64,abc"}),
),
(
AudioReference::File {
file_id: "file_audio".into(),
},
serde_json::json!({"type": "input_audio", "file_id": "file_audio"}),
),
] {
let input = UserInput::Audio {
audio: audio.clone(),
};
assert_eq!(
serde_json::from_value::<UserInput>(serde_json::to_value(&input)?)?,
input
);
let item = user_message(vec![input]);
assert_eq!(
item,
ResponseInputItem::Message {
role: "user".into(),
content: vec![ContentItem::InputAudio { audio }],
phase: None,
}
);
assert_eq!(
serde_json::to_value(&item)?,
serde_json::json!({
"type": "message", "role": "user", "content": [wire],
})
);
assert_eq!(
serde_json::from_value::<ResponseInputItem>(serde_json::to_value(&item)?)?,
item
);
}
Ok(())
}
#[test]
fn serializes_local_audio_user_input_with_label_and_data_url() -> Result<()> {
let temp_dir = tempdir()?;
for (extension, mime) in [
("wav", "audio/wav"),
("mp3", "audio/mpeg"),
("m4a", "audio/mp4"),
("webm", "audio/webm"),
("ogg", "audio/ogg"),
] {
let audio_path = temp_dir.path().join(format!("sample.{extension}"));
std::fs::write(&audio_path, b"audio")?;
let item = user_message(vec![UserInput::LocalAudio {
path: audio_path.clone(),
}]);
assert_eq!(
item,
ResponseInputItem::Message {
role: "user".to_string(),
content: vec![
ContentItem::InputText {
text: local_audio_open_tag_text_with_path(
/*label_number*/ 1,
&audio_path,
),
},
ContentItem::InputAudio {
audio: AudioReference::Inline {
audio_url: format!("data:{mime};base64,YXVkaW8=")
},
},
ContentItem::InputText {
text: audio_close_tag_text(),
},
],
phase: None,
}
);
}
Ok(())
}
#[test]
fn replaces_unsupported_local_audio_format_with_placeholder() -> Result<()> {
let temp_dir = tempdir()?;
let audio_path = temp_dir.path().join("sample.flac");
std::fs::write(&audio_path, b"audio")?;
let item = user_message(vec![UserInput::LocalAudio {
path: audio_path.clone(),
}]);
assert_eq!(
item,
ResponseInputItem::Message {
role: "user".to_string(),
content: vec![ContentItem::InputText {
text: format!(
"Codex cannot attach audio at `{}`: unsupported audio format; use wav, mp3, m4a, webm, or ogg.",
audio_path.display()
),
}],
phase: None,
}
);
Ok(())
}
#[test]
fn replaces_unreadable_local_audio_with_placeholder() {
let audio_path = PathBuf::from("missing.wav");
let item = user_message(vec![UserInput::LocalAudio { path: audio_path }]);
let ResponseInputItem::Message { content, .. } = item else {
panic!("expected message response");
};
let [ContentItem::InputText { text }] = content.as_slice() else {
panic!("expected local audio error placeholder");
};
assert!(
text.starts_with("Codex could not read the local audio at `missing.wav`: "),
"unexpected placeholder: {text}"
);
}
#[test]
fn image_user_input_preserves_requested_detail() -> Result<()> {
let image_url = "data:image/png;base64,abc".to_string();
let item = user_message(vec![UserInput::Image {
image: ImageReference::Inline {
image_url: image_url.clone(),
},
detail: Some(ImageDetail::Original),
}]);
match item {
ResponseInputItem::Message { content, .. } => {
assert_eq!(
content.first(),
Some(&ContentItem::InputImage {
image: ImageReference::Inline { image_url },
detail: Some(ImageDetail::Original),
})
);
}
other => panic!("expected message response but got {other:?}"),
}
Ok(())
}
/// Image associations use original input and expanded content positions, so omitted inputs,
/// local-image framing, failed reads, and duplicate URLs cannot shift a later substitution.
#[test]
fn user_input_image_indices_survive_content_expansion() -> Result<()> {
let dir = tempdir()?;
let local_path = dir.path().join("local.png");
std::fs::write(&local_path, TINY_PNG_BYTES)?;
let inline_image = UserInput::Image {
image: ImageReference::Inline {
image_url: data_url_from_bytes("image/png", TINY_PNG_BYTES),
},
detail: None,
};
let inputs = vec![
UserInput::Text {
text: "compare images".to_string(),
text_elements: Vec::new(),
},
UserInput::Mention {
name: "app".to_string(),
path: "app://example".to_string(),
},
UserInput::Skill {
name: "skill".to_string(),
path: dir.path().join("SKILL.md"),
},
UserInput::LocalImage {
path: dir.path().join("missing.png"),
detail: None,
},
UserInput::LocalImage {
path: local_path,
detail: None,
},
inline_image.clone(),
inline_image,
];
for preparation in [LocalImagePreparation::Defer, LocalImagePreparation::Process] {
let mut indices = HashMap::new();
let item = from_user_input(inputs.clone(), preparation, &mut indices);
assert_eq!(indices, HashMap::from([(4, 3), (5, 5), (6, 6)]));
let ResponseInputItem::Message { content, .. } = item else {
panic!("expected a user message");
};
for content_index in indices.into_values() {
assert!(matches!(
content[content_index],
ContentItem::InputImage { .. }
));
}
}
Ok(())
}
#[test]
fn mixed_remote_and_local_images_share_label_sequence() -> Result<()> {
let image_url = "data:image/png;base64,abc".to_string();
let dir = tempdir()?;
let local_path = dir.path().join("local.png");
std::fs::write(&local_path, TINY_PNG_BYTES)?;
let item = user_message(vec![
UserInput::Image {
image: ImageReference::Inline {
image_url: image_url.clone(),
},
detail: None,
},
UserInput::LocalImage {
path: local_path.clone(),
detail: None,
},
]);
match item {
ResponseInputItem::Message { content, .. } => {
assert_eq!(
content.first(),
Some(&ContentItem::InputImage {
image: ImageReference::Inline { image_url },
detail: Some(DEFAULT_IMAGE_DETAIL),
})
);
assert_eq!(
content.get(1),
Some(&ContentItem::InputText {
text: local_image_open_tag_text_with_path(/*label_number*/ 2, &local_path),
})
);
assert!(matches!(
content.get(2),
Some(ContentItem::InputImage { .. })
));
assert_eq!(
content.get(3),
Some(&ContentItem::InputText {
text: image_close_tag_text(),
})
);
}
other => panic!("expected message response but got {other:?}"),
}
Ok(())
}
#[test]
fn mixed_remote_and_local_audio_share_label_sequence() -> Result<()> {
let audio_url = "data:audio/wav;base64,abc".to_string();
let dir = tempdir()?;
let local_path = dir.path().join("local.mp3");
std::fs::write(&local_path, b"audio")?;
let item = user_message(vec![
UserInput::Audio {
audio: AudioReference::Inline {
audio_url: audio_url.clone(),
},
},
UserInput::LocalAudio {
path: local_path.clone(),
},
]);
assert_eq!(
item,
ResponseInputItem::Message {
role: "user".to_string(),
content: vec![
ContentItem::InputAudio {
audio: AudioReference::Inline { audio_url }
},
ContentItem::InputText {
text: local_audio_open_tag_text_with_path(/*label_number*/ 2, &local_path,),
},
ContentItem::InputAudio {
audio: AudioReference::Inline {
audio_url: "data:audio/mpeg;base64,YXVkaW8=".to_string()
}
},
ContentItem::InputText {
text: audio_close_tag_text(),
},
],
phase: None,
}
);
Ok(())
}
#[test]
fn local_image_user_input_preserves_requested_detail() -> Result<()> {
let dir = tempdir()?;
let local_path = dir.path().join("local.png");
std::fs::write(&local_path, TINY_PNG_BYTES)?;
let item = user_message(vec![UserInput::LocalImage {
path: local_path,
detail: Some(ImageDetail::Original),
}]);
match item {
ResponseInputItem::Message { content, .. } => {
assert!(matches!(
content.get(1),
Some(ContentItem::InputImage {
detail: Some(ImageDetail::Original),
..
})
));
}
other => panic!("expected message response but got {other:?}"),
}
Ok(())
}
#[test]
fn local_image_read_error_adds_placeholder() -> Result<()> {
let dir = tempdir()?;
let missing_path = dir.path().join("missing-image.png");
let item = user_message(vec![UserInput::LocalImage {
path: missing_path.clone(),
detail: None,
}]);
match item {
ResponseInputItem::Message { content, .. } => {
assert_eq!(content.len(), 1);
match &content[0] {
ContentItem::InputText { text } => {
let display_path = missing_path.display().to_string();
assert!(
text.contains(&display_path),
"placeholder should mention missing path: {text}"
);
assert!(
text.contains("could not read"),
"placeholder should mention read issue: {text}"
);
}
other => panic!("expected placeholder text but found {other:?}"),
}
}
other => panic!("expected message response but got {other:?}"),
}
Ok(())
}
#[test]
fn local_image_non_image_adds_placeholder() -> Result<()> {
let dir = tempdir()?;
let json_path = dir.path().join("example.json");
std::fs::write(&json_path, br#"{"hello":"world"}"#)?;
let item = user_message(vec![UserInput::LocalImage {
path: json_path.clone(),
detail: None,
}]);
match item {
ResponseInputItem::Message { content, .. } => {
assert_eq!(content.len(), 1);
match &content[0] {
ContentItem::InputText { text } => {
assert!(
text.contains("unsupported image `application/json`"),
"placeholder should mention unsupported image MIME: {text}"
);
assert!(
text.contains(&json_path.display().to_string()),
"placeholder should mention path: {text}"
);
}
other => panic!("expected placeholder text but found {other:?}"),
}
}
other => panic!("expected message response but got {other:?}"),
}
Ok(())
}
#[test]
fn local_image_unsupported_image_format_adds_placeholder() -> Result<()> {
let dir = tempdir()?;
let svg_path = dir.path().join("example.svg");
std::fs::write(
&svg_path,
br#"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"></svg>"#,
)?;
let item = user_message(vec![UserInput::LocalImage {
path: svg_path.clone(),
detail: None,
}]);
match item {
ResponseInputItem::Message { content, .. } => {
assert_eq!(content.len(), 1);
let expected = format!(
"Codex cannot attach image at `{}`: unsupported image `image/svg+xml`.",
svg_path.display()
);
match &content[0] {
ContentItem::InputText { text } => assert_eq!(text, &expected),
other => panic!("expected placeholder text but found {other:?}"),
}
}
other => panic!("expected message response but got {other:?}"),
}
Ok(())
}