mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Preserve uploaded image file IDs in user message display history (#46258)
## Why Image preparation replaces uploaded images with file references in model history, but user message events still retain the original local paths or inline image URLs. Live and persisted display history should carry the prepared file IDs as well. ## What changed - Prepare images before emitting user message events, and copy uploaded file IDs into the corresponding user inputs. - Track original input positions through content expansion so failed image reads, omitted inputs, and duplicate URLs do not misassociate images. - Preserve text spans, image detail settings, and inputs without a prepared file reference. ## Testing Add a protocol test for image position mapping and an integration test covering file IDs in model requests, live user message events, and persisted display history, including failed images and duplicate URLs. GitOrigin-RevId: 55e7ab97925b5db049c5c2403d90349635eee740
This commit is contained in:
1 parent
fcf05456bb
commit
96aca987f7
4 files changed
+377
-73
No files matched your search
@@ -15,6 +15,7 @@ use codex_history::RolloutItem;
|
||||
use codex_protocol::config_types::CollaborationMode;
|
||||
use codex_protocol::config_types::ModeKind;
|
||||
use codex_protocol::config_types::Settings;
|
||||
use codex_protocol::items::TurnItem;
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::DEFAULT_IMAGE_DETAIL;
|
||||
use codex_protocol::models::ImageDetail;
|
||||
@@ -25,7 +26,10 @@ use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
use codex_protocol::protocol::Op;
|
||||
use codex_protocol::protocol::ThreadHistoryMode;
|
||||
use codex_protocol::protocol::ThreadSettingsOverrides;
|
||||
use codex_protocol::user_input::ByteRange;
|
||||
use codex_protocol::user_input::TextElement;
|
||||
use codex_protocol::user_input::UserInput;
|
||||
use codex_utils_image::data_url_from_bytes;
|
||||
use core_test_support::TempDirExt;
|
||||
@@ -43,6 +47,7 @@ use core_test_support::test_codex::local_selections;
|
||||
use core_test_support::test_codex::test_codex;
|
||||
use core_test_support::test_codex::turn_permission_fields;
|
||||
use core_test_support::wait_for_event;
|
||||
use core_test_support::wait_for_event_match;
|
||||
use image::GenericImageView;
|
||||
use image::ImageBuffer;
|
||||
use image::Rgba;
|
||||
@@ -406,6 +411,166 @@ async fn file_image_passes_through_request_and_rollout() -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Uploaded images keep their original input positions and text spans in live and durable display
|
||||
/// history, even when earlier images fail preparation or expand into multiple model content items.
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn uploaded_images_preserve_user_message_display_history() -> anyhow::Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
let server = start_mock_server().await;
|
||||
let test = test_codex()
|
||||
.with_image_store(Arc::new(RecordingFileAttachmentStore::default()))
|
||||
.with_history_mode(ThreadHistoryMode::Paginated)
|
||||
.build_with_auto_env(&server)
|
||||
.await?;
|
||||
let image_path = test.cwd.path().join("display-image.png");
|
||||
write_test_png(&image_path, [12, 34, 56, 255])?;
|
||||
let image_url = data_url_from_bytes("image/png", &fs::read(&image_path)?);
|
||||
let input = vec![
|
||||
UserInput::Text {
|
||||
text: "inspect these images".to_string(),
|
||||
text_elements: vec![TextElement::new(
|
||||
ByteRange { start: 0, end: 7 },
|
||||
Some("<file>".to_string()),
|
||||
)],
|
||||
},
|
||||
UserInput::LocalImage {
|
||||
path: test.cwd.path().join("missing-image.png"),
|
||||
detail: None,
|
||||
},
|
||||
UserInput::Image {
|
||||
image: ImageReference::Inline {
|
||||
image_url: "data:image/png;base64,bm90IGFuIGltYWdl".to_string(),
|
||||
},
|
||||
detail: None,
|
||||
},
|
||||
UserInput::LocalImage {
|
||||
path: image_path,
|
||||
detail: Some(ImageDetail::High),
|
||||
},
|
||||
UserInput::Image {
|
||||
image: ImageReference::Inline {
|
||||
image_url: image_url.clone(),
|
||||
},
|
||||
detail: None,
|
||||
},
|
||||
UserInput::Text {
|
||||
text: "and this copy".to_string(),
|
||||
text_elements: Vec::new(),
|
||||
},
|
||||
UserInput::Image {
|
||||
image: ImageReference::File {
|
||||
file_id: "file_existing".to_string(),
|
||||
},
|
||||
detail: Some(ImageDetail::Original),
|
||||
},
|
||||
UserInput::Image {
|
||||
image: ImageReference::Inline { image_url },
|
||||
detail: None,
|
||||
},
|
||||
];
|
||||
let mut expected_input = input.clone();
|
||||
for index in [3, 4, 7] {
|
||||
let detail = match &input[index] {
|
||||
UserInput::Image { detail, .. } | UserInput::LocalImage { detail, .. } => *detail,
|
||||
_ => unreachable!("these inputs are images"),
|
||||
};
|
||||
expected_input[index] = UserInput::Image {
|
||||
image: ImageReference::File {
|
||||
file_id: UPLOADED_FILE_ID.to_string(),
|
||||
},
|
||||
detail,
|
||||
};
|
||||
}
|
||||
let response_mock = responses::mount_sse_once(
|
||||
&server,
|
||||
sse(vec![
|
||||
ev_response_created("resp-display"),
|
||||
ev_completed("resp-display"),
|
||||
]),
|
||||
)
|
||||
.await;
|
||||
test.codex
|
||||
.start_or_steer_turn(TurnInputRequest::user_input(input))
|
||||
.await?;
|
||||
|
||||
let started = wait_for_event_match(&test.codex, |event| match event {
|
||||
EventMsg::ItemStarted(event) => match &event.item {
|
||||
TurnItem::UserMessage(item) => Some(item.clone()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
let completed = wait_for_event_match(&test.codex, |event| match event {
|
||||
EventMsg::ItemCompleted(event) => match &event.item {
|
||||
TurnItem::UserMessage(item) => Some(item.clone()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
assert_eq!(started.content, expected_input);
|
||||
assert_eq!(completed.id, started.id);
|
||||
assert_eq!(completed.client_id, started.client_id);
|
||||
assert_eq!(completed.content, started.content);
|
||||
wait_for_event(&test.codex, |event| {
|
||||
matches!(event, EventMsg::TurnComplete(_))
|
||||
})
|
||||
.await;
|
||||
|
||||
let request = response_mock.single_request();
|
||||
let mut model_file_ids = Vec::new();
|
||||
for item in request.input() {
|
||||
if item.get("role").and_then(Value::as_str) != Some("user") {
|
||||
continue;
|
||||
}
|
||||
if let Some(content) = item.get("content").and_then(Value::as_array) {
|
||||
for content_item in content {
|
||||
if content_item.get("type").and_then(Value::as_str) == Some("input_image") {
|
||||
model_file_ids.push(
|
||||
content_item
|
||||
.get("file_id")
|
||||
.and_then(Value::as_str)
|
||||
.context("prepared image file ID")?
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(
|
||||
model_file_ids,
|
||||
vec![
|
||||
UPLOADED_FILE_ID,
|
||||
UPLOADED_FILE_ID,
|
||||
"file_existing",
|
||||
UPLOADED_FILE_ID,
|
||||
]
|
||||
);
|
||||
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
let rollout_path = test.codex.rollout_path().context("rollout path")?;
|
||||
let rollout_text = read_rollout_text(&rollout_path).await?;
|
||||
let mut persisted_user_items = Vec::new();
|
||||
for line in rollout_text.lines() {
|
||||
if let RolloutItem::EventMsg(EventMsg::ItemCompleted(event)) =
|
||||
codex_rollout::parse_rollout_line(line)?.item
|
||||
&& let TurnItem::UserMessage(item) = event.item
|
||||
{
|
||||
persisted_user_items.push(item);
|
||||
}
|
||||
}
|
||||
let [persisted] = persisted_user_items.as_slice() else {
|
||||
panic!("expected exactly one persisted user message");
|
||||
};
|
||||
assert_eq!(persisted.id, completed.id);
|
||||
assert_eq!(persisted.client_id, completed.client_id);
|
||||
assert_eq!(persisted.content, completed.content);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn resumed_history_only_emits_resize_notices_for_new_images() -> anyhow::Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
Reference in new issue
Block a user