mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Refactor image content to use a shared ImageReference type (#45543)
## What changed Represent images in `ContentItem` and `FunctionCallOutputContentItem` with `ImageReference::Inline`, flattened to preserve the existing `image_url` wire format. Update image producers and consumers and regenerate app-server schemas and SDK artifacts. Preserve the Python SDK's `InputImageContentItem` and `InputImageFunctionCallOutputContentItem` class names during generation. ## Testing Add a regression test for stable Python image class names and adapt existing image tests to the shared representation. GitOrigin-RevId: c38a780ac3314c2ac2deb3afc1b93b94b6f93fec
This commit is contained in:
1 parent
6ae5e71458
commit
5a66d460d3
79 files changed
+994
-312
No files matched your search
@@ -5,6 +5,7 @@
|
||||
use std::io::Write;
|
||||
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::ImageReference;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::protocol::TruncationPolicy;
|
||||
|
||||
@@ -97,7 +98,10 @@ impl SectionCost {
|
||||
ContentItem::InputText { text } | ContentItem::OutputText { text } => {
|
||||
self.text_bytes = self.text_bytes.saturating_add(text.len());
|
||||
}
|
||||
ContentItem::InputImage { image_url, .. } => {
|
||||
ContentItem::InputImage {
|
||||
image: ImageReference::Inline { image_url },
|
||||
..
|
||||
} => {
|
||||
self.image_bytes = self.image_bytes.saturating_add(image_url.len());
|
||||
self.image_count = self.image_count.saturating_add(1);
|
||||
}
|
||||
@@ -206,7 +210,11 @@ pub(super) fn content_framing_tokens(item_count: usize) -> usize {
|
||||
|
||||
fn adjusted_tokens(mut bytes: usize, content: &[ContentItem]) -> usize {
|
||||
for item in content {
|
||||
if let ContentItem::InputImage { image_url, .. } = item {
|
||||
if let ContentItem::InputImage {
|
||||
image: ImageReference::Inline { image_url },
|
||||
..
|
||||
} = item
|
||||
{
|
||||
let payload = ByteCount::measure(|counter| serde_json::to_writer(counter, image_url));
|
||||
if payload == usize::MAX {
|
||||
return usize::MAX;
|
||||
|
||||
@@ -4,6 +4,7 @@ use super::*;
|
||||
use crate::Budgeted;
|
||||
use crate::composition::SectionOutput;
|
||||
use crate::composition::user_message as message;
|
||||
use codex_protocol::models::ImageReference;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
@@ -17,7 +18,9 @@ fn section_costs_keep_multimodal_payloads_separate() {
|
||||
text: "évidence".to_owned(),
|
||||
}),
|
||||
Budgeted::required(ContentItem::InputImage {
|
||||
image_url: "data:image/png;base64,AAAA".to_owned(),
|
||||
image: ImageReference::Inline {
|
||||
image_url: "data:image/png;base64,AAAA".to_owned(),
|
||||
},
|
||||
detail: None,
|
||||
}),
|
||||
]),
|
||||
@@ -59,7 +62,9 @@ fn section_costs_keep_multimodal_payloads_separate() {
|
||||
fn request_estimate_reserves_images_independently_of_encoded_size() {
|
||||
let image = |payload: &str| {
|
||||
message(vec![ContentItem::InputImage {
|
||||
image_url: format!("data:image/png;base64,{payload}"),
|
||||
image: ImageReference::Inline {
|
||||
image_url: format!("data:image/png;base64,{payload}"),
|
||||
},
|
||||
detail: None,
|
||||
}])
|
||||
};
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
use codex_context_fragments::ContextualUserFragment;
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::ImageReference;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::protocol::TruncationPolicy;
|
||||
use codex_protocol::user_input::UserInput;
|
||||
@@ -214,7 +215,10 @@ impl CollectedContext {
|
||||
Ok(Budgeted::required(ContentItem::InputText { text }))
|
||||
}
|
||||
UserInput::Image { image_url, detail } => Ok(Budgeted::optional(
|
||||
ContentItem::InputImage { image_url, detail },
|
||||
ContentItem::InputImage {
|
||||
image: ImageReference::Inline { image_url },
|
||||
detail,
|
||||
},
|
||||
BudgetPriority::Image,
|
||||
)),
|
||||
_ => Err(SectionError::UnsupportedDelivery {
|
||||
@@ -281,9 +285,10 @@ impl ComposedContext {
|
||||
}));
|
||||
continue;
|
||||
}
|
||||
ContentItem::InputImage { image_url, detail } => {
|
||||
UserInput::Image { image_url, detail }
|
||||
}
|
||||
ContentItem::InputImage {
|
||||
image: ImageReference::Inline { image_url },
|
||||
detail,
|
||||
} => UserInput::Image { image_url, detail },
|
||||
ContentItem::InputAudio { .. } | ContentItem::OutputText { .. } => {
|
||||
return Err(SectionError::UnsupportedDelivery {
|
||||
section: section.id,
|
||||
|
||||
@@ -7,6 +7,7 @@ use std::collections::HashSet;
|
||||
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::ImageDetail;
|
||||
use codex_protocol::models::ImageReference;
|
||||
use codex_protocol::protocol::TruncationPolicy;
|
||||
|
||||
use crate::ComposedContext;
|
||||
@@ -90,7 +91,10 @@ impl ComposedContext {
|
||||
pub fn retain_images(&mut self, mut admit: impl FnMut(&str, &mut Option<ImageDetail>) -> bool) {
|
||||
for section in &mut self.sections {
|
||||
retain_content(section, &mut self.truncations, |_, item| match item {
|
||||
ContentItem::InputImage { image_url, detail } => admit(image_url, detail),
|
||||
ContentItem::InputImage {
|
||||
image: ImageReference::Inline { image_url },
|
||||
detail,
|
||||
} => admit(image_url, detail),
|
||||
_ => true,
|
||||
});
|
||||
}
|
||||
@@ -273,7 +277,10 @@ fn retain_content(
|
||||
if !keep {
|
||||
let original_bytes = match &item.content {
|
||||
ContentItem::InputText { text } | ContentItem::OutputText { text } => text.len(),
|
||||
ContentItem::InputImage { image_url, .. } => image_url.len(),
|
||||
ContentItem::InputImage {
|
||||
image: ImageReference::Inline { image_url },
|
||||
..
|
||||
} => image_url.len(),
|
||||
ContentItem::InputAudio { audio_url } => audio_url.len(),
|
||||
};
|
||||
truncations.push(TruncationObservation {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
use super::*;
|
||||
use crate::budget::section_tokens;
|
||||
use crate::composition::user_message;
|
||||
use codex_protocol::models::ImageReference;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
@@ -146,7 +147,9 @@ fn budget_reserves_existing_context_and_preserves_required_messages() {
|
||||
.unwrap()
|
||||
.into_message();
|
||||
let image = ContentItem::InputImage {
|
||||
image_url: "data:image/png;base64,AAAA".to_owned(),
|
||||
image: ImageReference::Inline {
|
||||
image_url: "data:image/png;base64,AAAA".to_owned(),
|
||||
},
|
||||
detail: None,
|
||||
};
|
||||
let make_context = || ComposedContext {
|
||||
@@ -241,7 +244,9 @@ fn image_omission_preserves_text_and_later_eviction_policy() {
|
||||
delivery: SectionDelivery::UserContent(vec![
|
||||
Budgeted::optional(
|
||||
ContentItem::InputImage {
|
||||
image_url: "rejected-image".to_owned(),
|
||||
image: ImageReference::Inline {
|
||||
image_url: "rejected-image".to_owned(),
|
||||
},
|
||||
detail: None,
|
||||
},
|
||||
BudgetPriority::Image,
|
||||
@@ -307,11 +312,15 @@ fn image_omission_preserves_text_and_later_eviction_policy() {
|
||||
);
|
||||
|
||||
let older = ContentItem::InputImage {
|
||||
image_url: "older-image".to_owned(),
|
||||
image: ImageReference::Inline {
|
||||
image_url: "older-image".to_owned(),
|
||||
},
|
||||
detail: None,
|
||||
};
|
||||
let newer = ContentItem::InputImage {
|
||||
image_url: "newer-image".to_owned(),
|
||||
image: ImageReference::Inline {
|
||||
image_url: "newer-image".to_owned(),
|
||||
},
|
||||
detail: None,
|
||||
};
|
||||
let image_section = |images: Vec<ContentItem>| SectionOutput {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use codex_protocol::models::ExecutedToolCall;
|
||||
use codex_protocol::models::ImageReference;
|
||||
use codex_protocol::models::ReasoningItemContent;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::json;
|
||||
@@ -147,7 +148,9 @@ fn oversized_user_images_preserve_text_and_metadata_in_order() {
|
||||
content.insert(
|
||||
/*index*/ 1,
|
||||
ContentItem::InputImage {
|
||||
image_url: format!("data:image/png;base64,{}", "A".repeat(image_bytes)),
|
||||
image: ImageReference::Inline {
|
||||
image_url: format!("data:image/png;base64,{}", "A".repeat(image_bytes)),
|
||||
},
|
||||
detail: Some(codex_protocol::models::ImageDetail::Original),
|
||||
},
|
||||
);
|
||||
|
||||
@@ -10,6 +10,7 @@ use crate::SectionScope;
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::FunctionCallOutputContentItem;
|
||||
use codex_protocol::models::ImageDetail;
|
||||
use codex_protocol::models::ImageReference;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
@@ -65,7 +66,11 @@ impl TranscriptImages {
|
||||
while images.len() >= MAX_TRANSCRIPT_IMAGES
|
||||
|| image_bytes + image_url.len() > MAX_TRANSCRIPT_IMAGE_BYTES
|
||||
{
|
||||
let Some(ContentItem::InputImage { image_url, .. }) = images.pop_front() else {
|
||||
let Some(ContentItem::InputImage {
|
||||
image: ImageReference::Inline { image_url },
|
||||
..
|
||||
}) = images.pop_front()
|
||||
else {
|
||||
break;
|
||||
};
|
||||
image_bytes -= image_url.len();
|
||||
@@ -73,7 +78,9 @@ impl TranscriptImages {
|
||||
}
|
||||
image_bytes += image_url.len();
|
||||
images.push_back(ContentItem::InputImage {
|
||||
image_url: image_url.to_owned(),
|
||||
image: ImageReference::Inline {
|
||||
image_url: image_url.to_owned(),
|
||||
},
|
||||
detail,
|
||||
});
|
||||
};
|
||||
@@ -84,7 +91,11 @@ impl TranscriptImages {
|
||||
if matches!(role.as_str(), "user" | "assistant") =>
|
||||
{
|
||||
for item in content {
|
||||
if let ContentItem::InputImage { image_url, detail } = item {
|
||||
if let ContentItem::InputImage {
|
||||
image: ImageReference::Inline { image_url },
|
||||
detail,
|
||||
} = item
|
||||
{
|
||||
include_image(image_url, *detail);
|
||||
}
|
||||
}
|
||||
@@ -95,8 +106,10 @@ impl TranscriptImages {
|
||||
{
|
||||
if let Some(content) = output.content_items() {
|
||||
for item in content {
|
||||
if let FunctionCallOutputContentItem::InputImage { image_url, detail } =
|
||||
item
|
||||
if let FunctionCallOutputContentItem::InputImage {
|
||||
image: ImageReference::Inline { image_url },
|
||||
detail,
|
||||
} = item
|
||||
{
|
||||
include_image(image_url, *detail);
|
||||
}
|
||||
@@ -108,7 +121,11 @@ impl TranscriptImages {
|
||||
}
|
||||
if input.include_tool_outputs {
|
||||
for image in input.node_repl_images {
|
||||
if let ContentItem::InputImage { image_url, detail } = image {
|
||||
if let ContentItem::InputImage {
|
||||
image: ImageReference::Inline { image_url },
|
||||
detail,
|
||||
} = image
|
||||
{
|
||||
include_image(image_url, *detail);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,12 +5,15 @@ use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::FunctionCallOutputContentItem;
|
||||
use codex_protocol::models::FunctionCallOutputPayload;
|
||||
use codex_protocol::models::ImageDetail;
|
||||
use codex_protocol::models::ImageReference;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
fn image(url: &str) -> ContentItem {
|
||||
ContentItem::InputImage {
|
||||
image_url: url.into(),
|
||||
image: ImageReference::Inline {
|
||||
image_url: url.into(),
|
||||
},
|
||||
detail: Some(ImageDetail::High),
|
||||
}
|
||||
}
|
||||
@@ -32,7 +35,9 @@ fn image_selection_preserves_source_policy_order_and_both_limits() {
|
||||
namespace: None,
|
||||
output: FunctionCallOutputPayload::from_content_items(vec![
|
||||
FunctionCallOutputContentItem::InputImage {
|
||||
image_url: "tool".into(),
|
||||
image: ImageReference::Inline {
|
||||
image_url: "tool".into(),
|
||||
},
|
||||
detail: Some(ImageDetail::High),
|
||||
},
|
||||
]),
|
||||
|
||||
Reference in new issue
Block a user