mirror of
https://github.com/openai/codex.git
synced 2026-11-18 22:20:48 +00:00
add image generation content model
This commit is contained in:
@@ -585,6 +585,13 @@ impl ThreadHistoryBuilder {
|
||||
id: payload.call_id.clone(),
|
||||
status: String::new(),
|
||||
revised_prompt: None,
|
||||
content: crate::protocol::v2::ImageGenerationContent::Inline {
|
||||
mime_type: "image/png".to_string(),
|
||||
data_base64: String::new(),
|
||||
byte_length: 0,
|
||||
width: None,
|
||||
height: None,
|
||||
},
|
||||
result: String::new(),
|
||||
saved_path: None,
|
||||
};
|
||||
@@ -596,6 +603,13 @@ impl ThreadHistoryBuilder {
|
||||
id: payload.call_id.clone(),
|
||||
status: payload.status.clone(),
|
||||
revised_prompt: payload.revised_prompt.clone(),
|
||||
content: crate::protocol::v2::ImageGenerationContent::Inline {
|
||||
mime_type: "image/png".to_string(),
|
||||
data_base64: payload.result.clone(),
|
||||
byte_length: crate::protocol::v2::image_generation_byte_length(&payload.result),
|
||||
width: None,
|
||||
height: None,
|
||||
},
|
||||
result: payload.result.clone(),
|
||||
saved_path: payload.saved_path.clone(),
|
||||
};
|
||||
@@ -1465,6 +1479,13 @@ mod tests {
|
||||
id: "ig_123".into(),
|
||||
status: "completed".into(),
|
||||
revised_prompt: Some("final prompt".into()),
|
||||
content: crate::protocol::v2::ImageGenerationContent::Inline {
|
||||
mime_type: "image/png".into(),
|
||||
data_base64: "Zm9v".into(),
|
||||
byte_length: 3,
|
||||
width: None,
|
||||
height: None,
|
||||
},
|
||||
result: "Zm9v".into(),
|
||||
saved_path: Some(test_path_buf("/tmp/ig_123.png").abs()),
|
||||
},
|
||||
|
||||
@@ -6215,6 +6215,8 @@ pub enum ThreadItem {
|
||||
id: String,
|
||||
status: String,
|
||||
revised_prompt: Option<String>,
|
||||
#[serde(default)]
|
||||
content: ImageGenerationContent,
|
||||
result: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
#[ts(optional)]
|
||||
@@ -6231,6 +6233,52 @@ pub enum ThreadItem {
|
||||
ContextCompaction { id: String },
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
|
||||
#[serde(tag = "type", rename_all = "camelCase")]
|
||||
#[ts(tag = "type", export_to = "v2/")]
|
||||
pub enum ImageGenerationContent {
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[ts(rename_all = "camelCase")]
|
||||
Inline {
|
||||
mime_type: String,
|
||||
data_base64: String,
|
||||
byte_length: u64,
|
||||
width: Option<u32>,
|
||||
height: Option<u32>,
|
||||
},
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[ts(rename_all = "camelCase")]
|
||||
Deferred {
|
||||
content_id: String,
|
||||
mime_type: String,
|
||||
byte_length: u64,
|
||||
width: Option<u32>,
|
||||
height: Option<u32>,
|
||||
},
|
||||
}
|
||||
|
||||
impl Default for ImageGenerationContent {
|
||||
fn default() -> Self {
|
||||
Self::Inline {
|
||||
mime_type: "image/png".to_string(),
|
||||
data_base64: String::new(),
|
||||
byte_length: 0,
|
||||
width: None,
|
||||
height: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn image_generation_byte_length(data_base64: &str) -> u64 {
|
||||
let padding_len = data_base64
|
||||
.as_bytes()
|
||||
.iter()
|
||||
.rev()
|
||||
.take_while(|byte| **byte == b'=')
|
||||
.count();
|
||||
((data_base64.len() / 4) * 3).saturating_sub(padding_len) as u64
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[ts(rename_all = "camelCase", export_to = "v2/")]
|
||||
@@ -6692,6 +6740,13 @@ impl From<CoreTurnItem> for ThreadItem {
|
||||
id: image.id,
|
||||
status: image.status,
|
||||
revised_prompt: image.revised_prompt,
|
||||
content: ImageGenerationContent::Inline {
|
||||
mime_type: "image/png".to_string(),
|
||||
data_base64: image.result.clone(),
|
||||
byte_length: image_generation_byte_length(&image.result),
|
||||
width: None,
|
||||
height: None,
|
||||
},
|
||||
result: image.result,
|
||||
saved_path: image.saved_path,
|
||||
},
|
||||
@@ -11883,4 +11938,29 @@ mod tests {
|
||||
"unexpected error: {err}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_generation_defaults_missing_content_for_legacy_payloads() {
|
||||
let item: ThreadItem = serde_json::from_value(json!({
|
||||
"type": "imageGeneration",
|
||||
"id": "ig_123",
|
||||
"status": "completed",
|
||||
"revisedPrompt": null,
|
||||
"result": "Zm9v",
|
||||
"savedPath": null
|
||||
}))
|
||||
.expect("legacy image generation item should deserialize");
|
||||
|
||||
assert_eq!(
|
||||
item,
|
||||
ThreadItem::ImageGeneration {
|
||||
id: "ig_123".to_string(),
|
||||
status: "completed".to_string(),
|
||||
revised_prompt: None,
|
||||
content: ImageGenerationContent::default(),
|
||||
result: "Zm9v".to_string(),
|
||||
saved_path: None,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user