mirror of
https://github.com/openai/codex.git
synced 2026-04-24 14:45:27 +00:00
feat: placeholder for image that can't be decoded to prevent 400 (#6773)
This commit is contained in:
@@ -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 {
|
||||
@@ -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) => {
|
||||
@@ -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 {
|
||||
@@ -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) => {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use image::ImageError;
|
||||
use image::ImageFormat;
|
||||
use std::path::PathBuf;
|
||||
use thiserror::Error;
|
||||
@@ -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(_),
|
||||
..
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user