Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions codex-rs/protocol/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,19 @@ fn local_image_error_placeholder(
}
}

fn invalid_image_error_placeholder(
path: &std::path::Path,
error: impl std::fmt::Display,
) -> ContentItem {
ContentItem::InputText {
text: format!(
"Image located at `{}` is invalid: {}",
path.display(),
error
),
}
}

impl From<ResponseInputItem> for ResponseItem {
fn from(item: ResponseInputItem) -> Self {
match item {
Expand Down Expand Up @@ -247,9 +260,10 @@ impl From<Vec<UserInput>> for ResponseInputItem {
image_url: image.into_data_url(),
},
Err(err) => {
tracing::warn!("Failed to resize image {}: {}", path.display(), err);
if matches!(&err, ImageProcessingError::Read { .. }) {
local_image_error_placeholder(&path, &err)
} else if err.is_invalid_image() {
invalid_image_error_placeholder(&path, &err)
} else {
match std::fs::read(&path) {
Ok(bytes) => {
Expand Down Expand Up @@ -365,6 +379,7 @@ impl Serialize for FunctionCallOutputPayload {
where
S: Serializer,
{
tracing::error!("Payload: {:?}", self);
if let Some(items) = &self.content_items {
items.serialize(serializer)
} else {
Expand Down Expand Up @@ -452,7 +467,7 @@ fn convert_content_blocks_to_items(
) -> Option<Vec<FunctionCallOutputContentItem>> {
let mut saw_image = false;
let mut items = Vec::with_capacity(blocks.len());

tracing::warn!("Blocks: {:?}", blocks);
for block in blocks {
match block {
ContentBlock::TextContent(text) => {
Expand Down
13 changes: 13 additions & 0 deletions codex-rs/utils/image/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use image::ImageError;
use image::ImageFormat;
use std::path::PathBuf;
use thiserror::Error;
Expand All @@ -23,3 +24,15 @@ pub enum ImageProcessingError {
source: image::ImageError,
},
}

impl ImageProcessingError {
pub fn is_invalid_image(&self) -> bool {
matches!(
self,
ImageProcessingError::Decode {
source: ImageError::Decoding(_),
..
}
)
}
}
Loading