mirror of
https://github.com/openai/codex.git
synced 2026-11-05 20:10:33 +00:00
Expose local image paths to models
This commit is contained in:
@@ -161,7 +161,9 @@ async fn copy_paste_local_image_persists_rollout_request_shape() -> anyhow::Resu
|
||||
role: "user".to_string(),
|
||||
content: vec![
|
||||
ContentItem::InputText {
|
||||
text: codex_protocol::models::local_image_open_tag_text(/*label_number*/ 1),
|
||||
text: codex_protocol::models::local_image_open_tag_text_with_path(
|
||||
/*label_number*/ 1, &abs_path,
|
||||
),
|
||||
},
|
||||
ContentItem::InputImage {
|
||||
image_url,
|
||||
|
||||
@@ -1031,6 +1031,18 @@ pub fn local_image_open_tag_text(label_number: usize) -> String {
|
||||
format!("{LOCAL_IMAGE_OPEN_TAG_PREFIX}{label}{LOCAL_IMAGE_OPEN_TAG_SUFFIX}")
|
||||
}
|
||||
|
||||
pub fn local_image_open_tag_text_with_path(label_number: usize, path: &std::path::Path) -> String {
|
||||
let label = local_image_label_text(label_number);
|
||||
let path = path
|
||||
.display()
|
||||
.to_string()
|
||||
.replace('&', "&")
|
||||
.replace('"', """)
|
||||
.replace('<', "<")
|
||||
.replace('>', ">");
|
||||
format!("{LOCAL_IMAGE_OPEN_TAG_PREFIX}{label} path=\"{path}\"{LOCAL_IMAGE_OPEN_TAG_SUFFIX}")
|
||||
}
|
||||
|
||||
pub fn is_local_image_open_tag_text(text: &str) -> bool {
|
||||
text.strip_prefix(LOCAL_IMAGE_OPEN_TAG_PREFIX)
|
||||
.is_some_and(|rest| rest.ends_with(LOCAL_IMAGE_OPEN_TAG_SUFFIX))
|
||||
@@ -1087,7 +1099,7 @@ pub fn local_image_content_items_with_label_number(
|
||||
let mut items = Vec::with_capacity(3);
|
||||
if let Some(label_number) = label_number {
|
||||
items.push(ContentItem::InputText {
|
||||
text: local_image_open_tag_text(label_number),
|
||||
text: local_image_open_tag_text_with_path(label_number, path),
|
||||
});
|
||||
}
|
||||
items.push(ContentItem::InputImage {
|
||||
@@ -2873,7 +2885,7 @@ mod tests {
|
||||
detail: None,
|
||||
},
|
||||
UserInput::LocalImage {
|
||||
path: local_path,
|
||||
path: local_path.clone(),
|
||||
detail: None,
|
||||
},
|
||||
]);
|
||||
@@ -2890,7 +2902,10 @@ mod tests {
|
||||
assert_eq!(
|
||||
content.get(1),
|
||||
Some(&ContentItem::InputText {
|
||||
text: local_image_open_tag_text(/*label_number*/ 2),
|
||||
text: local_image_open_tag_text_with_path(
|
||||
/*label_number*/ 2,
|
||||
&local_path
|
||||
),
|
||||
})
|
||||
);
|
||||
assert!(matches!(
|
||||
@@ -2910,6 +2925,14 @@ mod tests {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_image_open_tag_escapes_path() {
|
||||
assert_eq!(
|
||||
local_image_open_tag_text_with_path(1, std::path::Path::new(r#"/tmp/a&"<b>.png"#)),
|
||||
r#"<image name=[Image #1] path="/tmp/a&"<b>.png">"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_image_user_input_preserves_requested_detail() -> Result<()> {
|
||||
let dir = tempdir()?;
|
||||
|
||||
Reference in New Issue
Block a user