fix(ai): serialize image edits with images array

This commit is contained in:
zjm54321
2026-08-11 00:30:00 +08:00
parent 06f5d3c8c0
commit 8d918d0459
@@ -542,6 +542,20 @@ pub fn build_openai_image_api_provider_request_body(
request: &NormalizedOpenAiImageRequest, request: &NormalizedOpenAiImageRequest,
mapped_model: Option<&str>, mapped_model: Option<&str>,
upstream_is_stream: bool, upstream_is_stream: bool,
) -> Option<Value> {
build_openai_image_api_provider_request_body_with_edit_images(
request,
mapped_model,
upstream_is_stream,
true,
)
}
fn build_openai_image_api_provider_request_body_with_edit_images(
request: &NormalizedOpenAiImageRequest,
mapped_model: Option<&str>,
upstream_is_stream: bool,
use_plural_edit_images: bool,
) -> Option<Value> { ) -> Option<Value> {
let model = mapped_model let model = mapped_model
.map(str::trim) .map(str::trim)
@@ -578,12 +592,16 @@ pub fn build_openai_image_api_provider_request_body(
.or_insert_with(|| response_format.clone()); .or_insert_with(|| response_format.clone());
} }
insert_standard_openai_image_inputs(&mut body, request.images.clone()); insert_standard_openai_image_inputs(&mut body, request.images.clone());
project_openai_image_api_request_body( let mut body = project_openai_image_api_request_body(
&Value::Object(body), &Value::Object(body),
model, model,
request.operation, request.operation,
request.max_generation_count, request.max_generation_count,
) )?;
if use_plural_edit_images && request.operation == OpenAiImageOperation::Edit {
insert_standard_openai_images_edit_inputs(body.as_object_mut()?, request.images.clone())?;
}
Some(body)
} }
pub fn build_codex_openai_image_api_provider_request_body( pub fn build_codex_openai_image_api_provider_request_body(
@@ -591,8 +609,12 @@ pub fn build_codex_openai_image_api_provider_request_body(
mapped_model: Option<&str>, mapped_model: Option<&str>,
upstream_is_stream: bool, upstream_is_stream: bool,
) -> Option<Value> { ) -> Option<Value> {
let body = let body = build_openai_image_api_provider_request_body_with_edit_images(
build_openai_image_api_provider_request_body(request, mapped_model, upstream_is_stream)?; request,
mapped_model,
upstream_is_stream,
false,
)?;
project_codex_openai_image_api_request_body(&body, request.operation) project_codex_openai_image_api_request_body(&body, request.operation)
} }
@@ -898,6 +920,31 @@ pub(crate) fn insert_standard_openai_image_inputs(
object.insert("image".to_string(), image); object.insert("image".to_string(), image);
} }
fn insert_standard_openai_images_edit_inputs(
object: &mut Map<String, Value>,
images: Vec<Value>,
) -> Option<()> {
let images = images
.into_iter()
.map(|image| {
let image = image.as_object()?;
image
.get("image_url")
.cloned()
.map(|image_url| json!({ "image_url": image_url }))
.or_else(|| {
image
.get("file_id")
.cloned()
.map(|file_id| json!({ "file_id": file_id }))
})
})
.collect::<Option<Vec<_>>>()?;
object.remove("image");
object.insert("images".to_string(), Value::Array(images));
Some(())
}
fn openai_image_api_inputs(object: &Map<String, Value>) -> Option<Vec<&Value>> { fn openai_image_api_inputs(object: &Map<String, Value>) -> Option<Vec<&Value>> {
match (object.get("image"), object.get("images")) { match (object.get("image"), object.get("images")) {
(Some(_), Some(_)) => None, (Some(_), Some(_)) => None,
@@ -2316,9 +2363,10 @@ mod tests {
assert_eq!(provider_request_body["response_format"], "url"); assert_eq!(provider_request_body["response_format"], "url");
assert_eq!(provider_request_body["user"], "user-123"); assert_eq!(provider_request_body["user"], "user-123");
assert_eq!( assert_eq!(
provider_request_body["image"]["image_url"], provider_request_body["images"],
"data:image/png;base64,aW1hZ2U=" json!([{"image_url": "data:image/png;base64,aW1hZ2U="}])
); );
assert!(provider_request_body.get("image").is_none());
assert_eq!( assert_eq!(
provider_request_body["mask"]["image_url"], provider_request_body["mask"]["image_url"],
"data:image/png;base64,bWFzaw==" "data:image/png;base64,bWFzaw=="
@@ -2329,7 +2377,7 @@ mod tests {
} }
#[test] #[test]
fn build_image_api_provider_edit_request_uses_one_standard_image_field() { fn build_image_api_provider_edit_request_uses_plural_images_field() {
let parts = request_parts("/v1/images/edits", Some("application/json")); let parts = request_parts("/v1/images/edits", Some("application/json"));
let request = normalize_openai_image_request( let request = normalize_openai_image_request(
&parts, &parts,
@@ -2350,10 +2398,13 @@ mod tests {
.expect("standard Images edit body should project"); .expect("standard Images edit body should project");
assert_eq!( assert_eq!(
provider_request_body["image"].as_array().map(Vec::len), provider_request_body["images"],
Some(2) json!([
{"image_url": "data:image/png;base64,Zm9v"},
{"image_url": "https://example.test/reference.png"}
])
); );
assert!(provider_request_body.get("images").is_none()); assert!(provider_request_body.get("image").is_none());
} }
#[test] #[test]