mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Support image references by file ID in inputs and tool outputs (#45794)
## What changed - Accept `fileId` alongside the existing `url` form for app-server image inputs, and forward file references to the Responses API as `file_id`. Update generated schemas and client types. - Preserve file references, image detail hints, and mixed inline/file image ordering through user-message events, thread history, and rollout migration. Retain file images when truncating tool output. - Pass file references through image preparation without resolving them, while keeping resize-notice numbering correct. Omit them from unsupported TUI display and Guardian image context. - Reject image-edit requests whose recent-image window includes a file reference, preventing selection of an older inline image instead. ## Testing Add coverage for serialization, request and rollout preservation, mixed-image history ordering, incomplete ordering metadata, tool-output truncation, and rejection of unsupported image-edit selections. GitOrigin-RevId: 6ca20a8577155cc934b720803c3b7b3bffdf972a
This commit is contained in:
1 parent
c51cb968e4
commit
7b8b17b97a
95 files changed
+2434
-324
No files matched your search
@@ -25,6 +25,10 @@ pub(super) fn content_item_token_count(item: &ContentItem) -> usize {
|
||||
image_url, *detail,
|
||||
)))
|
||||
.unwrap_or(usize::MAX),
|
||||
ContentItem::InputImage {
|
||||
image: ImageReference::File { .. },
|
||||
..
|
||||
} => 0,
|
||||
ContentItem::InputAudio { .. } => 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +63,14 @@ impl NodeReplReviewResponse {
|
||||
.saturating_add(self.items.iter().fold(0_usize, |bytes, item| {
|
||||
bytes.saturating_add(match item {
|
||||
Text { text, .. } => text.len(),
|
||||
Image { image_url, .. } => image_url.len(),
|
||||
Image {
|
||||
image: ImageReference::Inline { image_url },
|
||||
..
|
||||
} => image_url.len(),
|
||||
Image {
|
||||
image: ImageReference::File { file_id },
|
||||
..
|
||||
} => file_id.len(),
|
||||
_ => 0,
|
||||
})
|
||||
}))
|
||||
@@ -116,14 +123,15 @@ impl NodeReplReviewEvidence {
|
||||
.iter()
|
||||
.flat_map(|response| &response.items)
|
||||
.filter_map(|item| match item {
|
||||
Image { image_url, detail } if seen_images.insert(image_url) => {
|
||||
Some(ContentItem::InputImage {
|
||||
image: ImageReference::Inline {
|
||||
image_url: image_url.clone(),
|
||||
},
|
||||
detail: *detail,
|
||||
})
|
||||
}
|
||||
Image {
|
||||
image: ImageReference::Inline { image_url },
|
||||
detail,
|
||||
} if seen_images.insert(image_url) => Some(ContentItem::InputImage {
|
||||
image: ImageReference::Inline {
|
||||
image_url: image_url.clone(),
|
||||
},
|
||||
detail: *detail,
|
||||
}),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use codex_protocol::models::ImageReference;
|
||||
use codex_protocol::user_input::UserInput;
|
||||
use codex_utils_output_truncation::approx_bytes_for_tokens;
|
||||
use pretty_assertions::assert_eq;
|
||||
@@ -15,7 +16,18 @@ fn text_input(text: &str) -> UserInput {
|
||||
|
||||
fn image_input(image_url: &str) -> UserInput {
|
||||
UserInput::Image {
|
||||
image_url: image_url.to_string(),
|
||||
image: ImageReference::Inline {
|
||||
image_url: image_url.to_string(),
|
||||
},
|
||||
detail: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn file_image_input(file_id: &str) -> UserInput {
|
||||
UserInput::Image {
|
||||
image: ImageReference::File {
|
||||
file_id: file_id.to_string(),
|
||||
},
|
||||
detail: None,
|
||||
}
|
||||
}
|
||||
@@ -121,6 +133,36 @@ fn evidence_preserves_tail_text_after_oversized_image_response() {
|
||||
assert!(rendered_text(&inputs).contains("FINAL IMPORTANT"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_backed_images_are_retained_but_omitted_from_guardian_views() {
|
||||
let evidence = NodeReplReviewEvidence::default();
|
||||
let file_id = format!("file_{}", "x".repeat(4_096));
|
||||
let items = vec![text_input("visible"), file_image_input(&file_id)];
|
||||
|
||||
evidence.record("browser", "cell", "file", items.clone());
|
||||
|
||||
let snapshot = evidence.snapshot_since(/*reviewed_sequence*/ 0).unwrap();
|
||||
assert_eq!(snapshot.responses.len(), 1);
|
||||
assert_eq!(snapshot.responses[0].items, items);
|
||||
assert!(snapshot.responses[0].has_images());
|
||||
assert!(snapshot.responses[0].retained_bytes() >= file_id.len());
|
||||
assert_eq!(evidence.images(), Vec::new());
|
||||
|
||||
let rendered = snapshot
|
||||
.context(NodeReplReviewEvidenceMode::Multimodal)
|
||||
.render_inputs();
|
||||
assert!(rendered_text(&rendered).contains("visible"));
|
||||
assert!(!rendered.iter().any(|item| {
|
||||
matches!(
|
||||
item,
|
||||
UserInput::Image {
|
||||
image: ImageReference::File { .. },
|
||||
..
|
||||
}
|
||||
)
|
||||
}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn evidence_evicts_complete_oldest_responses_and_rejects_oversized_items() {
|
||||
let evidence = NodeReplReviewEvidence::default();
|
||||
|
||||
@@ -930,6 +930,7 @@ static ORIGINAL_IMAGE_ESTIMATE_CACHE: LazyLock<BlockingLruCache<[u8; 20], Option
|
||||
});
|
||||
|
||||
fn estimate_response_item_model_visible_bytes(item: &ResponseItem) -> i64 {
|
||||
// TODO(kc) Account for file-backed image size after its token-cost contract is defined.
|
||||
match item {
|
||||
ResponseItem::Message { content, .. } => content
|
||||
.iter()
|
||||
@@ -941,6 +942,10 @@ fn estimate_response_item_model_visible_bytes(item: &ResponseItem) -> i64 {
|
||||
image: ImageReference::Inline { image_url },
|
||||
detail,
|
||||
} => estimate_image_bytes(image_url, *detail),
|
||||
ContentItem::InputImage {
|
||||
image: ImageReference::File { .. },
|
||||
..
|
||||
} => 0,
|
||||
ContentItem::InputAudio { audio_url } => estimate_audio_bytes(audio_url),
|
||||
})
|
||||
.fold(0i64, i64::saturating_add),
|
||||
@@ -1147,6 +1152,10 @@ fn estimate_function_output_bytes(output: &FunctionCallOutputBody) -> i64 {
|
||||
image: ImageReference::Inline { image_url },
|
||||
detail,
|
||||
} => estimate_image_bytes(image_url, *detail),
|
||||
FunctionCallOutputContentItem::InputImage {
|
||||
image: ImageReference::File { .. },
|
||||
..
|
||||
} => 0,
|
||||
FunctionCallOutputContentItem::InputAudio { audio_url } => {
|
||||
estimate_audio_bytes(audio_url)
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ use codex_protocol::items::TurnItem;
|
||||
use codex_protocol::items::UserMessageItem;
|
||||
use codex_protocol::items::WebSearchItem;
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::ImageReference;
|
||||
use codex_protocol::models::MessagePhase;
|
||||
use codex_protocol::models::ReasoningItemContent;
|
||||
use codex_protocol::models::ReasoningItemReasoningSummary;
|
||||
@@ -127,12 +126,9 @@ fn parse_user_message(message: &[ContentItem]) -> Option<UserMessageItem> {
|
||||
text_elements: Vec::new(),
|
||||
});
|
||||
}
|
||||
ContentItem::InputImage {
|
||||
image: ImageReference::Inline { image_url },
|
||||
detail,
|
||||
} => {
|
||||
ContentItem::InputImage { image, detail } => {
|
||||
content.push(UserInput::Image {
|
||||
image_url: image_url.clone(),
|
||||
image: image.clone(),
|
||||
detail: *detail,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -110,11 +110,11 @@ fn parses_user_message_with_text_and_two_images() {
|
||||
text_elements: Vec::new(),
|
||||
},
|
||||
UserInput::Image {
|
||||
image_url: img1,
|
||||
image: ImageReference::Inline { image_url: img1 },
|
||||
detail: Some(DEFAULT_IMAGE_DETAIL),
|
||||
},
|
||||
UserInput::Image {
|
||||
image_url: img2,
|
||||
image: ImageReference::Inline { image_url: img2 },
|
||||
detail: Some(DEFAULT_IMAGE_DETAIL),
|
||||
},
|
||||
];
|
||||
@@ -124,6 +124,49 @@ fn parses_user_message_with_text_and_two_images() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Canonical user-message events must retain opaque file IDs for durable thread history.
|
||||
#[test]
|
||||
fn parses_user_message_with_file_image() {
|
||||
let item = ResponseItem::Message {
|
||||
id: None,
|
||||
role: "user".to_string(),
|
||||
content: vec![
|
||||
ContentItem::InputImage {
|
||||
image: ImageReference::File {
|
||||
file_id: "file_123".to_string(),
|
||||
},
|
||||
detail: Some(DEFAULT_IMAGE_DETAIL),
|
||||
},
|
||||
ContentItem::InputText {
|
||||
text: "describe it".to_string(),
|
||||
},
|
||||
],
|
||||
phase: None,
|
||||
internal_chat_message_metadata_passthrough: None,
|
||||
};
|
||||
|
||||
let turn_item = parse_turn_item(&item).expect("expected user message turn item");
|
||||
|
||||
let TurnItem::UserMessage(user) = turn_item else {
|
||||
panic!("expected TurnItem::UserMessage");
|
||||
};
|
||||
assert_eq!(
|
||||
user.content,
|
||||
vec![
|
||||
UserInput::Image {
|
||||
image: ImageReference::File {
|
||||
file_id: "file_123".to_string(),
|
||||
},
|
||||
detail: Some(DEFAULT_IMAGE_DETAIL),
|
||||
},
|
||||
UserInput::Text {
|
||||
text: "describe it".to_string(),
|
||||
text_elements: Vec::new(),
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skips_local_image_label_text() {
|
||||
let image_url = "data:image/png;base64,abc".to_string();
|
||||
@@ -158,7 +201,7 @@ fn skips_local_image_label_text() {
|
||||
TurnItem::UserMessage(user) => {
|
||||
let expected_content = vec![
|
||||
UserInput::Image {
|
||||
image_url,
|
||||
image: ImageReference::Inline { image_url },
|
||||
detail: Some(DEFAULT_IMAGE_DETAIL),
|
||||
},
|
||||
UserInput::Text {
|
||||
@@ -287,7 +330,7 @@ fn skips_unnamed_image_label_text() {
|
||||
TurnItem::UserMessage(user) => {
|
||||
let expected_content = vec![
|
||||
UserInput::Image {
|
||||
image_url,
|
||||
image: ImageReference::Inline { image_url },
|
||||
detail: Some(DEFAULT_IMAGE_DETAIL),
|
||||
},
|
||||
UserInput::Text {
|
||||
|
||||
@@ -534,7 +534,9 @@ async fn run_review_on_session(
|
||||
};
|
||||
let mut prepared = vec![
|
||||
ResponseInputItem::from(vec![UserInput::Image {
|
||||
image_url: image_url.to_owned(),
|
||||
image: ImageReference::Inline {
|
||||
image_url: image_url.to_owned(),
|
||||
},
|
||||
detail: *detail,
|
||||
}])
|
||||
.into(),
|
||||
|
||||
@@ -179,6 +179,8 @@ fn prepare_message_content(
|
||||
metadata: &mut Vec<ImagePreparationMetadata>,
|
||||
mode: ImagePreparationMode,
|
||||
) -> Vec<ResizedImage> {
|
||||
// TODO(kc) Prepare file-backed images once Core has an explicit resolution contract. Until
|
||||
// then they pass through while retaining their positions in resize notices.
|
||||
let image_count = items
|
||||
.iter()
|
||||
.filter(|item| matches!(item.content(), ContentItem::InputImage { .. }))
|
||||
@@ -186,12 +188,14 @@ fn prepare_message_content(
|
||||
let mut image_number = 0;
|
||||
let mut resized_images = Vec::new();
|
||||
for item in items {
|
||||
if matches!(item.content(), ContentItem::InputImage { .. }) {
|
||||
image_number += 1;
|
||||
}
|
||||
if let ContentItem::InputImage {
|
||||
image: ImageReference::Inline { image_url },
|
||||
detail,
|
||||
} = item.content_mut()
|
||||
{
|
||||
image_number += 1;
|
||||
match prepare_image(image_url, detail, origin, metadata, mode) {
|
||||
Ok(Some(resize)) if resize_notice_mode == ImageResizeNoticeMode::Enabled => {
|
||||
resized_images.push(ResizedImage {
|
||||
@@ -231,12 +235,14 @@ fn prepare_tool_output_content(
|
||||
let mut image_number = 0;
|
||||
let mut resized_images = Vec::new();
|
||||
for item in items {
|
||||
if matches!(item, FunctionCallOutputContentItem::InputImage { .. }) {
|
||||
image_number += 1;
|
||||
}
|
||||
if let FunctionCallOutputContentItem::InputImage {
|
||||
image: ImageReference::Inline { image_url },
|
||||
detail,
|
||||
} = item
|
||||
{
|
||||
image_number += 1;
|
||||
match prepare_image(image_url, detail, origin, metadata, mode) {
|
||||
Ok(Some(resize)) if resize_notice_mode == ImageResizeNoticeMode::Enabled => {
|
||||
resized_images.push(ResizedImage {
|
||||
|
||||
@@ -224,8 +224,9 @@ fn preparation_reports_tool_output_item_id() {
|
||||
);
|
||||
}
|
||||
|
||||
/// File-backed images retain their sequence positions even though preparation skips them.
|
||||
#[test]
|
||||
fn resize_notices_preserve_original_image_positions_and_skip_failed_images() {
|
||||
fn resize_notices_count_file_backed_images_and_skip_failed_images() {
|
||||
let (large_image_url, _) = png_data_url(/*width*/ 2048, /*height*/ 2048);
|
||||
let (small_image_url, _) = png_data_url(/*width*/ 64, /*height*/ 32);
|
||||
let mut items = vec![
|
||||
@@ -233,6 +234,12 @@ fn resize_notices_preserve_original_image_positions_and_skip_failed_images() {
|
||||
id: None,
|
||||
role: "user".to_string(),
|
||||
content: vec![
|
||||
ContentItem::InputImage {
|
||||
image: ImageReference::File {
|
||||
file_id: "file_message".to_string(),
|
||||
},
|
||||
detail: Some(ImageDetail::High),
|
||||
},
|
||||
ContentItem::InputImage {
|
||||
image: ImageReference::Inline {
|
||||
image_url: small_image_url,
|
||||
@@ -259,6 +266,7 @@ fn resize_notices_preserve_original_image_positions_and_skip_failed_images() {
|
||||
ContentItemKind("user.image".to_string()),
|
||||
ContentItemKind("user.image".to_string()),
|
||||
ContentItemKind("user.image".to_string()),
|
||||
ContentItemKind("user.image".to_string()),
|
||||
]),
|
||||
..Default::default()
|
||||
},
|
||||
@@ -270,6 +278,12 @@ fn resize_notices_preserve_original_image_positions_and_skip_failed_images() {
|
||||
name: None,
|
||||
namespace: None,
|
||||
output: FunctionCallOutputPayload::from_content_items(vec![
|
||||
FunctionCallOutputContentItem::InputImage {
|
||||
image: ImageReference::File {
|
||||
file_id: "file_tool".to_string(),
|
||||
},
|
||||
detail: Some(ImageDetail::High),
|
||||
},
|
||||
FunctionCallOutputContentItem::InputImage {
|
||||
image: ImageReference::Inline {
|
||||
image_url: "data:image/png;base64,%%%".to_string(),
|
||||
@@ -294,7 +308,7 @@ fn resize_notices_preserve_original_image_positions_and_skip_failed_images() {
|
||||
);
|
||||
let expected_user_notice = concat!(
|
||||
"<image_resize_notice>\n",
|
||||
"Image 3 of 3 in the preceding user message was resized from 2048x2048 to 1600x1600 pixels.\n",
|
||||
"Image 4 of 4 in the preceding user message was resized from 2048x2048 to 1600x1600 pixels.\n",
|
||||
"</image_resize_notice>"
|
||||
);
|
||||
|
||||
@@ -310,6 +324,7 @@ fn resize_notices_preserve_original_image_positions_and_skip_failed_images() {
|
||||
internal_chat_message_metadata_passthrough,
|
||||
&Some(InternalChatMessageMetadataPassthrough {
|
||||
content_item_kinds: Some(vec![
|
||||
ContentItemKind("user.image".to_string()),
|
||||
ContentItemKind("user.image".to_string()),
|
||||
ContentItemKind("images.preparation_error".to_string()),
|
||||
ContentItemKind("user.image".to_string()),
|
||||
@@ -318,6 +333,10 @@ fn resize_notices_preserve_original_image_positions_and_skip_failed_images() {
|
||||
}),
|
||||
);
|
||||
let [
|
||||
ContentItem::InputImage {
|
||||
image: ImageReference::File { file_id },
|
||||
..
|
||||
},
|
||||
ContentItem::InputImage {
|
||||
image:
|
||||
ImageReference::Inline {
|
||||
@@ -337,8 +356,9 @@ fn resize_notices_preserve_original_image_positions_and_skip_failed_images() {
|
||||
},
|
||||
] = content.as_slice()
|
||||
else {
|
||||
panic!("expected unchanged image, failed image placeholder, and resized image");
|
||||
panic!("expected file image, unchanged image, failed image placeholder, and resized image");
|
||||
};
|
||||
assert_eq!(file_id, "file_message");
|
||||
assert_eq!(
|
||||
decoded_image(small_message_image_url).1.dimensions(),
|
||||
(64, 32)
|
||||
@@ -373,6 +393,10 @@ fn resize_notices_preserve_original_image_positions_and_skip_failed_images() {
|
||||
panic!("expected function call output");
|
||||
};
|
||||
let [
|
||||
FunctionCallOutputContentItem::InputImage {
|
||||
image: ImageReference::File { file_id },
|
||||
..
|
||||
},
|
||||
FunctionCallOutputContentItem::InputText {
|
||||
text: failed_tool_image,
|
||||
},
|
||||
@@ -385,8 +409,9 @@ fn resize_notices_preserve_original_image_positions_and_skip_failed_images() {
|
||||
},
|
||||
] = output.content_items().expect("tool output content items")
|
||||
else {
|
||||
panic!("expected failed image placeholder and resized image in the tool output");
|
||||
panic!("expected file image, failed image placeholder, and resized image in tool output");
|
||||
};
|
||||
assert_eq!(file_id, "file_tool");
|
||||
assert_eq!(failed_tool_image, IMAGE_PROCESSING_ERROR_PLACEHOLDER);
|
||||
assert_eq!(
|
||||
decoded_image(resized_tool_image_url).1.dimensions(),
|
||||
@@ -400,7 +425,7 @@ fn resize_notices_preserve_original_image_positions_and_skip_failed_images() {
|
||||
content: vec![ContentItem::InputText {
|
||||
text: concat!(
|
||||
"<image_resize_notice>\n",
|
||||
"Image 2 of 2 in the preceding tool output was resized from 2048x2048 to 1600x1600 pixels.\n",
|
||||
"Image 3 of 3 in the preceding tool output was resized from 2048x2048 to 1600x1600 pixels.\n",
|
||||
"</image_resize_notice>"
|
||||
)
|
||||
.to_string(),
|
||||
|
||||
@@ -11306,7 +11306,9 @@ async fn task_finish_emits_turn_item_lifecycle_for_leftover_pending_user_input()
|
||||
text_elements: vec![text_element.clone()],
|
||||
},
|
||||
UserInput::Image {
|
||||
image_url: image_url.clone(),
|
||||
image: ImageReference::Inline {
|
||||
image_url: image_url.clone(),
|
||||
},
|
||||
detail: Some(ImageDetail::High),
|
||||
},
|
||||
];
|
||||
|
||||
@@ -40,6 +40,7 @@ use codex_protocol::config_types::ApprovalsReviewer;
|
||||
use codex_protocol::error::SandboxErr;
|
||||
use codex_protocol::models::AdditionalPermissionProfile as PermissionProfile;
|
||||
use codex_protocol::models::ContentItemKind;
|
||||
use codex_protocol::models::ImageReference;
|
||||
use codex_protocol::models::NetworkPermissions;
|
||||
use codex_protocol::models::ResponseInputItem;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
@@ -235,7 +236,9 @@ async fn request_permissions_routes_to_guardian_when_reviewer_is_enabled() {
|
||||
.thread_extension_data
|
||||
.get_or_init(crate::context::NodeReplReviewEvidence::default);
|
||||
let image = UserInput::Image {
|
||||
image_url: image_url.to_string(),
|
||||
image: ImageReference::Inline {
|
||||
image_url: image_url.to_string(),
|
||||
},
|
||||
detail: None,
|
||||
};
|
||||
evidence.record("js", "cell", "image", vec![image]);
|
||||
|
||||
@@ -367,7 +367,10 @@ impl CoreToolRuntime for McpHandler {
|
||||
load_data_url_for_prompt_uncached(&image_url, PromptImageMode::Original)
|
||||
.ok()?;
|
||||
captured_image_bytes = next_image_bytes;
|
||||
Some(UserInput::Image { image_url, detail })
|
||||
Some(UserInput::Image {
|
||||
image: codex_protocol::models::ImageReference::Inline { image_url },
|
||||
detail,
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user