mirror of
https://github.com/openai/codex.git
synced 2026-12-05 01:00:54 +00:00
Store generated image items as attachment references (#54398)
## What changed - Upload generated images to the attachment store and expose `fileId` in image-generation items when available, with inline `result` fallback on upload or decoding failure. - Preserve file references through legacy events, persisted rollouts, thread history, and task output summaries. Keep model-facing image preparation separate. - Add attachment request tags and mark generated-image uploads with `usage=image_gen`, preserving the thread's ephemeral setting. - Update public schemas and SDK types to accept either image representation, retain the Python `ImageGenerationThreadItem` alias, and prefer `fileId` when reading records containing both fields. ## Testing Add coverage for file-backed generation and rollout persistence, legacy history replay, inline and transitional serialization, task output visibility, and Python SDK alias compatibility. GitOrigin-RevId: f7a2bb5707e2f277348a547a988bc33c8e96d891
This commit is contained in:
committed by
copyberry
parent
1ef30bcbe7
commit
aabf13307a
@@ -29,6 +29,7 @@ tracing = { workspace = true }
|
||||
uuid = { workspace = true, features = ["serde", "v7"] }
|
||||
|
||||
[dev-dependencies]
|
||||
codex-attachment-store = { workspace = true }
|
||||
codex-utils-output-truncation = { workspace = true }
|
||||
pretty_assertions = { workspace = true }
|
||||
tempfile = { workspace = true }
|
||||
|
||||
@@ -949,6 +949,8 @@ fn board_tool_call(name: &str, args: serde_json::Value) -> codex_tools::ToolCall
|
||||
source: codex_tools::ToolCallSource::Direct,
|
||||
conversation_history: codex_tools::ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(codex_tools::NoopTurnItemEmitter),
|
||||
attachment_store: Arc::new(codex_attachment_store::InlineAttachmentStore),
|
||||
ephemeral: false,
|
||||
environments: vec![],
|
||||
payload: codex_tools::ToolPayload::Function {
|
||||
arguments: args.to_string(),
|
||||
|
||||
@@ -1968,6 +1968,8 @@ fn tool_call(tool_name: &str, call_id: &str, arguments: serde_json::Value) -> To
|
||||
source: ToolCallSource::Direct,
|
||||
conversation_history: codex_extension_api::ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
attachment_store: codex_core::passthrough_image_store(),
|
||||
ephemeral: false,
|
||||
environments: Vec::new(),
|
||||
payload: ToolPayload::Function {
|
||||
arguments: arguments.to_string(),
|
||||
|
||||
@@ -462,6 +462,8 @@ fn tool_call(tool_name: ToolName, arguments: serde_json::Value) -> ToolCall<'sta
|
||||
source: ToolCallSource::Direct,
|
||||
conversation_history: ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
attachment_store: codex_core::passthrough_image_store(),
|
||||
ephemeral: false,
|
||||
environments: Vec::new(),
|
||||
payload: ToolPayload::Function {
|
||||
arguments: arguments.to_string(),
|
||||
|
||||
@@ -15,6 +15,7 @@ workspace = true
|
||||
[dependencies]
|
||||
base64 = { workspace = true }
|
||||
codex-api = { workspace = true }
|
||||
codex-attachment-store = { workspace = true }
|
||||
codex-core = { workspace = true }
|
||||
codex-exec-server = { workspace = true }
|
||||
codex-extension-api = { workspace = true }
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::collections::HashMap;
|
||||
use std::io;
|
||||
|
||||
use base64::Engine;
|
||||
@@ -6,6 +7,8 @@ use codex_api::ImageBackground;
|
||||
use codex_api::ImageEditRequest;
|
||||
use codex_api::ImageGenerationRequest;
|
||||
use codex_api::ImageQuality;
|
||||
use codex_attachment_store::UploadRequest;
|
||||
use codex_attachment_store::UploadResult;
|
||||
use codex_exec_server::CreateDirectoryOptions;
|
||||
use codex_exec_server::LOCAL_FS;
|
||||
use codex_extension_api::ExtensionTurnItem;
|
||||
@@ -19,6 +22,7 @@ use codex_extension_api::ToolPayload;
|
||||
use codex_extension_api::ToolSpec;
|
||||
use codex_extension_api::parse_tool_input_schema;
|
||||
use codex_extension_items::ExtensionItem;
|
||||
use codex_extension_items::image_generation::GeneratedImageRef;
|
||||
use codex_extension_items::image_generation::ImageGenerationFailure;
|
||||
use codex_extension_items::image_generation::ImageGenerationItem;
|
||||
use codex_protocol::error::CodexErr;
|
||||
@@ -102,7 +106,7 @@ fn legacy_end_event(item: &ImageGenerationItem) -> EventMsg {
|
||||
call_id: item.id.clone(),
|
||||
status: item.status.clone(),
|
||||
revised_prompt: item.revised_prompt.clone(),
|
||||
result: item.result.clone(),
|
||||
image: item.image.clone(),
|
||||
transparent_background: item.transparent_background,
|
||||
failure: item.failure.clone(),
|
||||
saved_path: item.saved_path.clone(),
|
||||
@@ -156,7 +160,9 @@ impl ImageGenerationTool {
|
||||
id: call.call_id.clone(),
|
||||
status: "in_progress".to_string(),
|
||||
revised_prompt: None,
|
||||
result: String::new(),
|
||||
image: GeneratedImageRef::Inline {
|
||||
result: String::new(),
|
||||
},
|
||||
transparent_background: None,
|
||||
failure: None,
|
||||
saved_path: None,
|
||||
@@ -214,7 +220,9 @@ impl ImageGenerationTool {
|
||||
id: call.call_id.clone(),
|
||||
status: "failed".to_string(),
|
||||
revised_prompt: Some(args.prompt),
|
||||
result: String::new(),
|
||||
image: GeneratedImageRef::Inline {
|
||||
result: String::new(),
|
||||
},
|
||||
transparent_background: None,
|
||||
failure,
|
||||
saved_path: None,
|
||||
@@ -240,11 +248,45 @@ impl ImageGenerationTool {
|
||||
&result,
|
||||
)
|
||||
.await;
|
||||
// The UI item retains its attachment-store reference, while the model-facing output keeps
|
||||
// the original image so Core can prepare and upload it separately.
|
||||
let image = match BASE64_STANDARD.decode(result.trim().as_bytes()) {
|
||||
Ok(data) => {
|
||||
let request = UploadRequest {
|
||||
thread_id: self.thread_id.clone(),
|
||||
ephemeral: call.ephemeral,
|
||||
file_name: None,
|
||||
tags: HashMap::from([("usage".to_string(), "image_gen".to_string())]),
|
||||
data,
|
||||
};
|
||||
match call.attachment_store.upload(request).await {
|
||||
Ok(UploadResult::File { file_id }) => GeneratedImageRef::File { file_id },
|
||||
Ok(UploadResult::Inline { bytes }) => GeneratedImageRef::Inline {
|
||||
result: BASE64_STANDARD.encode(bytes),
|
||||
},
|
||||
Err(error) => {
|
||||
tracing::warn!(
|
||||
kind = ?error.kind(),
|
||||
"failed to upload generated image"
|
||||
);
|
||||
GeneratedImageRef::Inline {
|
||||
result: result.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::warn!("failed to decode generated image for upload: {error}");
|
||||
GeneratedImageRef::Inline {
|
||||
result: result.clone(),
|
||||
}
|
||||
}
|
||||
};
|
||||
let item = ImageGenerationItem {
|
||||
id: call.call_id.clone(),
|
||||
status: "completed".to_string(),
|
||||
revised_prompt: Some(args.prompt),
|
||||
result: result.clone(),
|
||||
image,
|
||||
transparent_background,
|
||||
failure: None,
|
||||
saved_path: saved_path.clone(),
|
||||
|
||||
@@ -29,7 +29,8 @@ pub struct ImageGenerationItem {
|
||||
pub id: String,
|
||||
pub status: String,
|
||||
pub revised_prompt: Option<String>,
|
||||
pub result: String,
|
||||
#[serde(flatten)]
|
||||
pub image: GeneratedImageRef,
|
||||
#[serde(default)]
|
||||
#[ts(optional)]
|
||||
pub transparent_background: Option<bool>,
|
||||
@@ -52,3 +53,21 @@ pub struct ImageGenerationItem {
|
||||
#[ts(skip)]
|
||||
pub generation_id: Option<String>,
|
||||
}
|
||||
|
||||
/// Either inline generated-image data or its durable attachment-store identity.
|
||||
///
|
||||
/// `File` must remain first so rollouts written during the migration, which contain both an empty
|
||||
/// `result` and a `fileId`, retain the file reference when deserialized.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema, PartialEq)]
|
||||
#[serde(untagged)]
|
||||
#[ts(untagged)]
|
||||
pub enum GeneratedImageRef {
|
||||
File {
|
||||
#[serde(rename = "fileId")]
|
||||
#[ts(rename = "fileId")]
|
||||
file_id: String,
|
||||
},
|
||||
Inline {
|
||||
result: String,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ use serde_json::json;
|
||||
use ts_rs::TS;
|
||||
|
||||
use super::ExtensionItem;
|
||||
use super::image_generation::GeneratedImageRef;
|
||||
use super::image_generation::ImageGenerationItem;
|
||||
use super::sleep::SleepItem;
|
||||
use super::web_search::WebSearchAction;
|
||||
@@ -13,7 +14,9 @@ fn completed_image_generation_item() -> ExtensionItem {
|
||||
id: "image-1".to_string(),
|
||||
status: "completed".to_string(),
|
||||
revised_prompt: Some("A blue square".to_string()),
|
||||
result: "cG5n".to_string(),
|
||||
image: GeneratedImageRef::Inline {
|
||||
result: "cG5n".to_string(),
|
||||
},
|
||||
transparent_background: None,
|
||||
failure: None,
|
||||
saved_path: None,
|
||||
@@ -54,6 +57,73 @@ fn image_generation_item_preserves_stable_wire_shape() {
|
||||
.expect("deserialize legacy image-generation item without transparency metadata"),
|
||||
item
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::from_value::<ExtensionItem>(json!({
|
||||
"kind": "image_gen.generation",
|
||||
"id": "image-1",
|
||||
"status": "completed",
|
||||
"revisedPrompt": "A blue square",
|
||||
"result": "cG5n",
|
||||
"fileId": null,
|
||||
}))
|
||||
.expect("deserialize transitional inline image-generation item"),
|
||||
item
|
||||
);
|
||||
}
|
||||
|
||||
/// Transitional rollouts containing both fields must retain the durable file reference.
|
||||
#[test]
|
||||
fn image_generation_item_reads_transitional_file_wire_shape() {
|
||||
let item = ExtensionItem::ImageGeneration(ImageGenerationItem {
|
||||
id: "image-1".to_string(),
|
||||
status: "completed".to_string(),
|
||||
revised_prompt: Some("A blue square".to_string()),
|
||||
image: GeneratedImageRef::File {
|
||||
file_id: "file-1".to_string(),
|
||||
},
|
||||
transparent_background: None,
|
||||
failure: None,
|
||||
saved_path: None,
|
||||
imagegen_request_id: None,
|
||||
generation_id: None,
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_value::<ExtensionItem>(json!({
|
||||
"kind": "image_gen.generation",
|
||||
"id": "image-1",
|
||||
"status": "completed",
|
||||
"revisedPrompt": "A blue square",
|
||||
"result": "",
|
||||
"fileId": "file-1",
|
||||
}))
|
||||
.expect("deserialize transitional file-backed image-generation item"),
|
||||
item
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::from_value::<ExtensionItem>(json!({
|
||||
"kind": "image_gen.generation",
|
||||
"id": "image-1",
|
||||
"status": "completed",
|
||||
"revisedPrompt": "A blue square",
|
||||
"result": "stale-inline-bytes",
|
||||
"fileId": "file-1",
|
||||
}))
|
||||
.expect("prefer file ID when both image representations are populated"),
|
||||
item
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::to_value(&item).expect("serialize file-backed extension item"),
|
||||
json!({
|
||||
"kind": "image_gen.generation",
|
||||
"id": "image-1",
|
||||
"status": "completed",
|
||||
"revisedPrompt": "A blue square",
|
||||
"fileId": "file-1",
|
||||
"transparentBackground": null,
|
||||
"failure": null,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -84,7 +154,12 @@ fn image_generation_item_preserves_authoritative_transparency() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_generation_transparency_is_optional_in_typescript() {
|
||||
fn image_generation_typescript_fields_preserve_wire_contracts() {
|
||||
assert!(
|
||||
ImageGenerationItem::inline().contains("{ fileId: string, } | { result: string, }")
|
||||
|| ImageGenerationItem::inline().contains("{ result: string, } | { fileId: string, }"),
|
||||
"v2 image-generation items must require exactly one image reference"
|
||||
);
|
||||
assert!(
|
||||
ImageGenerationItem::inline().contains("transparentBackground?: boolean"),
|
||||
"image-generation transparency must remain optional for existing TypeScript clients"
|
||||
|
||||
@@ -230,6 +230,8 @@ async fn add_ad_hoc_note_tool_creates_note_file() {
|
||||
source: ToolCallSource::Direct,
|
||||
conversation_history: codex_extension_api::ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
attachment_store: codex_core::passthrough_image_store(),
|
||||
ephemeral: false,
|
||||
environments: Vec::new(),
|
||||
payload: payload.clone(),
|
||||
})
|
||||
@@ -276,6 +278,8 @@ async fn add_ad_hoc_note_tool_rejects_paths_as_filenames() {
|
||||
source: ToolCallSource::Direct,
|
||||
conversation_history: codex_extension_api::ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
attachment_store: codex_core::passthrough_image_store(),
|
||||
ephemeral: false,
|
||||
environments: Vec::new(),
|
||||
payload,
|
||||
})
|
||||
@@ -323,6 +327,8 @@ async fn read_tool_reads_memory_file() {
|
||||
source: ToolCallSource::Direct,
|
||||
conversation_history: codex_extension_api::ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
attachment_store: codex_core::passthrough_image_store(),
|
||||
ephemeral: false,
|
||||
environments: Vec::new(),
|
||||
payload: payload.clone(),
|
||||
})
|
||||
@@ -440,6 +446,8 @@ async fn search_tool_accepts_multiple_queries() {
|
||||
source: ToolCallSource::Direct,
|
||||
conversation_history: codex_extension_api::ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
attachment_store: codex_core::passthrough_image_store(),
|
||||
ephemeral: false,
|
||||
environments: Vec::new(),
|
||||
payload: payload.clone(),
|
||||
})
|
||||
@@ -516,6 +524,8 @@ async fn search_tool_accepts_windowed_all_match_mode() {
|
||||
source: ToolCallSource::Direct,
|
||||
conversation_history: codex_extension_api::ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
attachment_store: codex_core::passthrough_image_store(),
|
||||
ephemeral: false,
|
||||
environments: Vec::new(),
|
||||
payload: payload.clone(),
|
||||
})
|
||||
@@ -572,6 +582,8 @@ async fn search_tool_rejects_legacy_single_query() {
|
||||
source: ToolCallSource::Direct,
|
||||
conversation_history: codex_extension_api::ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
attachment_store: codex_core::passthrough_image_store(),
|
||||
ephemeral: false,
|
||||
environments: Vec::new(),
|
||||
payload,
|
||||
})
|
||||
|
||||
@@ -38,6 +38,7 @@ tracing = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
assert_matches = { workspace = true }
|
||||
codex-attachment-store = { workspace = true }
|
||||
codex-models-manager = { workspace = true }
|
||||
codex-utils-cargo-bin = { workspace = true }
|
||||
dunce = { workspace = true }
|
||||
|
||||
@@ -5,6 +5,7 @@ use std::sync::atomic::AtomicUsize;
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
use assert_matches::assert_matches;
|
||||
use codex_attachment_store::InlineAttachmentStore;
|
||||
use codex_config::ConfigLayerEntry;
|
||||
use codex_config::ConfigLayerSource;
|
||||
use codex_config::ConfigLayerStack;
|
||||
@@ -2085,6 +2086,8 @@ async fn skills_list_only_returns_model_visible_bounded_metadata() -> TestResult
|
||||
source: ToolCallSource::Direct,
|
||||
conversation_history: ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
attachment_store: Arc::new(InlineAttachmentStore),
|
||||
ephemeral: false,
|
||||
environments: Vec::new(),
|
||||
payload: payload.clone(),
|
||||
};
|
||||
@@ -2245,6 +2248,8 @@ async fn skills_list_only_returns_model_visible_bounded_metadata() -> TestResult
|
||||
source: ToolCallSource::Direct,
|
||||
conversation_history: ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
attachment_store: Arc::new(InlineAttachmentStore),
|
||||
ephemeral: false,
|
||||
environments: Vec::new(),
|
||||
payload: insufficient_budget_payload,
|
||||
})
|
||||
|
||||
@@ -226,6 +226,8 @@ async fn cloud_preference_preserves_aliases_reads_and_executor_fallback() -> Tes
|
||||
source: ToolCallSource::Direct,
|
||||
conversation_history: ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
attachment_store: Arc::new(InlineAttachmentStore),
|
||||
ephemeral: false,
|
||||
environments: vec![ToolEnvironment::new(
|
||||
"local".to_string(),
|
||||
PathUri::parse("file:///skills")?,
|
||||
|
||||
Reference in New Issue
Block a user