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:
Krish Chainani authored and copyberry committed 2026-09-15 21:19:08 +00:00
1 parent c51cb968e4
commit 7b8b17b97a
95 files changed
+2434 -324

No files matched your search

@@ -11,6 +11,7 @@ use crate::protocol::v2::CollabAgentToolCallStatus;
use crate::protocol::v2::CommandExecutionStatus;
use crate::protocol::v2::DynamicToolCallOutputContentItem;
use crate::protocol::v2::DynamicToolCallStatus;
use crate::protocol::v2::ImageReference;
use crate::protocol::v2::McpToolCallAppContext;
use crate::protocol::v2::McpToolCallError;
use crate::protocol::v2::McpToolCallResult;
@@ -54,6 +55,7 @@ use codex_protocol::protocol::TurnAbortedEvent;
use codex_protocol::protocol::TurnCompleteEvent;
use codex_protocol::protocol::TurnStartedEvent;
use codex_protocol::protocol::UserMessageEvent;
use codex_protocol::protocol::UserMessageImageKind;
use codex_protocol::protocol::ViewImageToolCallEvent;
use codex_protocol::protocol::WebSearchBeginEvent;
use codex_protocol::protocol::WebSearchEndEvent;
@@ -1511,14 +1513,62 @@ impl ThreadHistoryBuilder {
.collect(),
});
}
if let Some(images) = &payload.images {
let has_complete_image_order = payload.has_complete_image_order();
if has_complete_image_order {
let mut inline_index = 0;
let mut file_index = 0;
for image_kind in &payload.image_order {
let (image, detail) = match image_kind {
UserMessageImageKind::Inline => {
let Some(image) = payload
.images
.as_deref()
.and_then(|images| images.get(inline_index))
else {
continue;
};
let detail = payload.image_details.get(inline_index).copied().flatten();
inline_index += 1;
(ImageReference::Inline { url: image.clone() }, detail)
}
UserMessageImageKind::File => {
let Some(file_id) = payload
.file_ids
.as_deref()
.and_then(|file_ids| file_ids.get(file_index))
else {
continue;
};
let detail = payload.file_id_details.get(file_index).copied().flatten();
file_index += 1;
(
ImageReference::File {
file_id: file_id.clone(),
},
detail,
)
}
};
content.push(UserInput::Image { image, detail });
}
} else if let Some(images) = &payload.images {
for (idx, image) in images.iter().enumerate() {
content.push(UserInput::Image {
url: image.clone(),
image: ImageReference::Inline { url: image.clone() },
detail: payload.image_details.get(idx).copied().flatten(),
});
}
}
if !has_complete_image_order && let Some(file_ids) = &payload.file_ids {
for (idx, file_id) in file_ids.iter().enumerate() {
content.push(UserInput::Image {
image: ImageReference::File {
file_id: file_id.clone(),
},
detail: payload.file_id_details.get(idx).copied().flatten(),
});
}
}
for (idx, path) in payload.local_images.iter().enumerate() {
content.push(UserInput::LocalImage {
path: path.clone(),
@@ -1824,7 +1874,9 @@ mod tests {
text_elements: Vec::new(),
},
UserInput::Image {
url: "https://example.com/one.png".into(),
image: ImageReference::Inline {
url: "https://example.com/one.png".into(),
},
detail: None,
}
],
@@ -1999,6 +2051,9 @@ mod tests {
message: "inspect these".into(),
images: Some(vec!["https://example.com/image.png".into()]),
image_details: vec![Some(ImageDetail::Original)],
file_ids: Some(vec!["file_123".into()]),
file_id_details: vec![Some(ImageDetail::High)],
image_order: vec![UserMessageImageKind::File, UserMessageImageKind::Inline],
local_images: vec![local_image_path.clone()],
local_image_details: vec![Some(ImageDetail::Original)],
audio: Some(vec!["https://example.com/audio.mp3".into()]),
@@ -2021,7 +2076,15 @@ mod tests {
text_elements: Vec::new(),
},
UserInput::Image {
url: "https://example.com/image.png".into(),
image: ImageReference::File {
file_id: "file_123".into(),
},
detail: Some(ImageDetail::High),
},
UserInput::Image {
image: ImageReference::Inline {
url: "https://example.com/image.png".into(),
},
detail: Some(ImageDetail::Original),
},
UserInput::LocalImage {
@@ -2039,6 +2102,43 @@ mod tests {
);
}
/// Incomplete ordering metadata must not drop references from the legacy split arrays.
#[test]
fn incomplete_image_order_falls_back_to_legacy_order() {
let events = vec![RolloutItem::EventMsg(EventMsg::UserMessage(
UserMessageEvent {
images: Some(vec!["https://example.com/image.png".into()]),
file_ids: Some(vec!["file_123".into()]),
image_order: vec![UserMessageImageKind::File],
..Default::default()
},
))];
let turns = build_turns_from_rollout_items(&events);
assert_eq!(
turns[0].items[0],
ThreadItem::UserMessage {
id: "item-1".into(),
client_id: None,
content: vec![
UserInput::Image {
image: ImageReference::Inline {
url: "https://example.com/image.png".into(),
},
detail: None,
},
UserInput::Image {
image: ImageReference::File {
file_id: "file_123".into(),
},
detail: None,
},
],
}
);
}
#[test]
fn ignores_user_message_item_lifecycle_events() {
let turn_id = "turn-1";
@@ -29,6 +29,7 @@ use codex_protocol::models::AdditionalPermissionProfile as CoreAdditionalPermiss
use codex_protocol::models::BUILT_IN_PERMISSION_PROFILE_WORKSPACE;
use codex_protocol::models::FileSystemPermissions as CoreFileSystemPermissions;
use codex_protocol::models::ImageDetail;
use codex_protocol::models::ImageReference as CoreImageReference;
use codex_protocol::models::MessagePhase;
use codex_protocol::models::NetworkPermissions as CoreNetworkPermissions;
use codex_protocol::models::WebSearchAction as CoreWebSearchAction;
@@ -3037,7 +3038,9 @@ fn core_turn_item_into_thread_item_converts_supported_variants() {
text_elements: Vec::new(),
},
CoreUserInput::Image {
image_url: "https://example.com/image.png".to_string(),
image: CoreImageReference::Inline {
image_url: "https://example.com/image.png".to_string(),
},
detail: Some(ImageDetail::Original),
},
CoreUserInput::LocalImage {
@@ -3072,7 +3075,9 @@ fn core_turn_item_into_thread_item_converts_supported_variants() {
text_elements: Vec::new(),
},
UserInput::Image {
url: "https://example.com/image.png".to_string(),
image: ImageReference::Inline {
url: "https://example.com/image.png".to_string(),
},
detail: Some(ImageDetail::Original),
},
UserInput::LocalImage {
@@ -3609,16 +3614,84 @@ fn mcp_tool_call_app_context_serializes_missing_mixed_version_fields_as_null() {
);
}
/// Keeps the existing URL form stable while exposing file-backed images as `fileId`.
#[test]
fn user_input_image_references_round_trip_with_stable_wire_shapes() {
let cases = [
(
UserInput::Image {
image: ImageReference::Inline {
url: "data:image/png;base64,AAA".to_string(),
},
detail: Some(ImageDetail::High),
},
json!({
"type": "image",
"url": "data:image/png;base64,AAA",
"detail": "high",
}),
),
(
UserInput::Image {
image: ImageReference::File {
file_id: "file_123".to_string(),
},
detail: Some(ImageDetail::Original),
},
json!({
"type": "image",
"fileId": "file_123",
"detail": "original",
}),
),
];
for (input, wire_value) in cases {
assert_eq!(
serde_json::to_value(&input).expect("user input should serialize"),
wire_value,
);
assert_eq!(
serde_json::from_value::<UserInput>(wire_value).expect("user input should deserialize"),
input,
);
}
}
/// Preserves durable file identity in both directions at the Core boundary.
#[test]
fn file_image_user_input_converts_both_directions() {
let app_server_input = UserInput::Image {
image: ImageReference::File {
file_id: "file_123".to_string(),
},
detail: Some(ImageDetail::High),
};
let core_input = CoreUserInput::Image {
image: CoreImageReference::File {
file_id: "file_123".to_string(),
},
detail: Some(ImageDetail::High),
};
assert_eq!(app_server_input.clone().into_core(), core_input);
assert_eq!(UserInput::from(core_input), app_server_input);
}
#[test]
fn user_input_into_core_preserves_media_fields() {
assert_eq!(
UserInput::Image {
url: "https://example.com/image.png".to_string(),
image: ImageReference::Inline {
url: "https://example.com/image.png".to_string(),
},
detail: Some(ImageDetail::Original),
}
.into_core(),
CoreUserInput::Image {
image_url: "https://example.com/image.png".to_string(),
image: CoreImageReference::Inline {
image_url: "https://example.com/image.png".to_string(),
},
detail: Some(ImageDetail::Original),
}
);
@@ -11,6 +11,7 @@ use codex_protocol::config_types::Personality;
use codex_protocol::config_types::ReasoningSummary;
use codex_protocol::models::FunctionCallOutputBody;
use codex_protocol::models::ImageDetail;
use codex_protocol::models::ImageReference as CoreImageReference;
use codex_protocol::openai_models::ReasoningEffort;
use codex_protocol::plan_tool::PlanItemArg as CorePlanItemArg;
use codex_protocol::plan_tool::StepStatus as CorePlanStepStatus;
@@ -401,6 +402,21 @@ impl From<TextElement> for CoreTextElement {
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(untagged)]
#[ts(untagged)]
#[ts(export_to = "v2/")]
pub enum ImageReference {
Inline {
url: String,
},
File {
#[serde(rename = "fileId")]
#[ts(rename = "fileId")]
file_id: String,
},
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(tag = "type", rename_all = "camelCase")]
#[ts(tag = "type")]
@@ -413,10 +429,11 @@ pub enum UserInput {
text_elements: Vec<TextElement>,
},
Image {
#[serde(flatten)]
image: ImageReference,
#[serde(default)]
#[ts(optional)]
detail: Option<ImageDetail>,
url: String,
},
LocalImage {
#[serde(default)]
@@ -450,8 +467,11 @@ impl UserInput {
text,
text_elements: text_elements.into_iter().map(Into::into).collect(),
},
UserInput::Image { url, detail } => CoreUserInput::Image {
image_url: url,
UserInput::Image { image, detail } => CoreUserInput::Image {
image: match image {
ImageReference::Inline { url } => CoreImageReference::Inline { image_url: url },
ImageReference::File { file_id } => CoreImageReference::File { file_id },
},
detail,
},
UserInput::LocalImage { path, detail } => CoreUserInput::LocalImage { path, detail },
@@ -473,8 +493,13 @@ impl From<CoreUserInput> for UserInput {
text,
text_elements: text_elements.into_iter().map(Into::into).collect(),
},
CoreUserInput::Image { image_url, detail } => UserInput::Image {
url: image_url,
CoreUserInput::Image { image, detail } => UserInput::Image {
image: match image {
CoreImageReference::Inline { image_url } => {
ImageReference::Inline { url: image_url }
}
CoreImageReference::File { file_id } => ImageReference::File { file_id },
},
detail,
},
CoreUserInput::LocalImage { path, detail } => UserInput::LocalImage { path, detail },