Add opt-in deferred image generation in code mode (#63594)

## What changed

Add `features.defer_image_generation` so code mode can discover the image
generation tool through `ALL_TOOLS` without including its definition in
the initial model request. Direct exposure remains the default and
applies when `image_gen` is excluded from code mode.

## Testing

Update the app-server GIF edit test to discover and invoke the deferred
tool through code mode. Verify that the initial request omits the tool
definition, the discovered definition includes the GIF option, and the
edit preserves animation and API defaults.

GitOrigin-RevId: 899f689bae0b86099a0ca3736d1cd2576bc3a7a9
This commit is contained in:
verma-oai
2026-11-24 09:52:16 +00:00
committed by copyberry
parent d4b84f9bb0
commit 37aeac06b3
5 changed files with 49 additions and 10 deletions
@@ -1130,7 +1130,7 @@ generatedImage(result);
#[cfg(not(windows))]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn gif_edit_from_code_mode_preserves_animation_and_api_defaults() -> Result<()> {
async fn deferred_gif_edit_from_code_mode_preserves_animation_and_api_defaults() -> Result<()> {
let call_id = "code-mode-gif-edit";
let server = responses::start_mock_server().await;
Mock::given(method("POST"))
@@ -1152,13 +1152,15 @@ async fn gif_edit_from_code_mode_preserves_animation_and_api_defaults() -> Resul
call_id,
"exec",
r#"
const result = await tools.image_gen__imagegen({
const imagegen = ALL_TOOLS.find(tool => tool.name === "image_gen__imagegen");
const result = await tools[imagegen.name]({
prompt: "animate the attached image",
gif: {},
num_last_images_to_include: 1,
});
text(result.image_url);
generatedImage(result);
text(imagegen.description);
"#,
),
responses::ev_completed("resp-1"),
@@ -1190,11 +1192,12 @@ generatedImage(result);
start_turn(
&mut mcp,
ThreadStartParams {
model: Some("gpt-5.5".into()),
config: Some(
[(
"features.image_generation_animation".to_string(),
json!(true),
)]
[
("features.image_generation_animation".into(), json!(true)),
("features.defer_image_generation".into(), json!(true)),
]
.into(),
),
..Default::default()
@@ -1274,7 +1277,8 @@ generatedImage(result);
let requests = response_mock.requests();
assert_eq!(requests.len(), 2);
assert!(requests[0].body_contains_text("gif?:"));
assert!(!requests[0].body_contains_text("image_gen__imagegen(args:"));
assert!(requests[1].body_contains_text("gif?:"));
let output = requests[1].custom_tool_call_output(call_id);
assert_eq!(
output["output"][1],
@@ -1295,7 +1299,7 @@ generatedImage(result);
.as_str()
.is_some_and(|hint| hint.contains(&saved_path.display().to_string()))
);
assert_eq!(output["output"].as_array().map(Vec::len), Some(4));
assert_eq!(output["output"].as_array().map(Vec::len), Some(5));
Ok(())
}
+6
View File
@@ -1015,6 +1015,9 @@
"default_mode_request_user_input": {
"type": "boolean"
},
"defer_image_generation": {
"type": "boolean"
},
"defer_mailbox_preemption": {
"type": "boolean"
},
@@ -8425,6 +8428,9 @@
"default_mode_request_user_input": {
"type": "boolean"
},
"defer_image_generation": {
"type": "boolean"
},
"defer_mailbox_preemption": {
"type": "boolean"
},
@@ -14,8 +14,10 @@ use codex_extension_api::ToolExecutor;
use codex_http_client::HttpClientFactory;
use codex_login::AuthManager;
use codex_model_provider_info::ModelProviderInfo;
use codex_tools::ToolExposure;
use codex_utils_absolute_path::AbsolutePathBuf;
use crate::IMAGE_GEN_NAMESPACE;
use crate::backend::CodexImagesBackend;
use crate::tool::ImageGenerationTool;
@@ -31,6 +33,7 @@ type SaveRootResolver = dyn Fn(&Config) -> Option<AbsolutePathBuf> + Send + Sync
struct ImageGenerationExtensionConfig {
available: bool,
gif_enabled: bool,
defer_image_generation: bool,
http_client_factory: HttpClientFactory,
provider: ModelProviderInfo,
save_root: Option<AbsolutePathBuf>,
@@ -44,6 +47,12 @@ impl ImageGenerationExtensionConfig {
|| config.model_provider.requires_openai_auth
|| config.model_provider.uses_openai_actor_authorization(),
gif_enabled: config.features.image_generation_animation_enabled(),
defer_image_generation: config.features.defer_image_generation_enabled()
&& !config
.code_mode
.excluded_tool_namespaces
.iter()
.any(|namespace| namespace == IMAGE_GEN_NAMESPACE),
http_client_factory: config.http_client_factory(),
provider: config.model_provider.clone(),
save_root: resolve_save_root(config),
@@ -106,6 +115,11 @@ impl ToolContributor for ImageGenerationExtension {
})
});
let exposure = if config.defer_image_generation {
ToolExposure::Deferred
} else {
ToolExposure::Direct
};
vec![Arc::new(ImageGenerationTool::new(
CodexImagesBackend::new(
auth.model_provider(config.provider.clone()),
@@ -117,6 +131,7 @@ impl ToolContributor for ImageGenerationExtension {
config.save_root.clone(),
thread_store.level_id().to_string(),
config.gif_enabled,
exposure,
))]
}
}
+4 -2
View File
@@ -81,6 +81,7 @@ pub(crate) struct ImageGenerationTool {
save_root: Option<AbsolutePathBuf>,
thread_id: String,
gif_enabled: bool,
exposure: ToolExposure,
}
impl ImageGenerationTool {
@@ -90,12 +91,14 @@ impl ImageGenerationTool {
save_root: Option<AbsolutePathBuf>,
thread_id: String,
gif_enabled: bool,
exposure: ToolExposure,
) -> Self {
Self {
backend,
save_root,
thread_id,
gif_enabled,
exposure,
}
}
}
@@ -148,9 +151,8 @@ impl<'call> ToolExecutor<ToolCall<'call>> for ImageGenerationTool {
imagegen_tool_spec(self.gif_enabled)
}
/// Exposes image generation directly and through the nested code-mode tool surface.
fn exposure(&self) -> ToolExposure {
ToolExposure::Direct
self.exposure
}
/// Executes the selected image operation and returns the completed image result.
+12
View File
@@ -416,6 +416,8 @@ pub enum Feature {
ExternalMigration,
/// Enable extension-backed image generation.
ImageGeneration,
/// Load the image generation tool definition on demand in code mode.
DeferImageGeneration,
/// Allow the image-generation tool to request a backend-assembled GIF.
ImageGenerationAnimation,
/// Omit inline image and audio content from app-server item notifications.
@@ -684,6 +686,10 @@ impl Features {
self.enabled.contains(&f)
}
pub fn defer_image_generation_enabled(&self) -> bool {
self.enabled(Feature::DeferImageGeneration)
}
/// Whether to advertise GIF output in the image-generation tool.
pub fn image_generation_animation_enabled(&self) -> bool {
self.enabled(Feature::ImageGenerationAnimation)
@@ -2130,6 +2136,12 @@ pub const FEATURES: &[FeatureSpec] = &[
stage: Stage::Stable,
default_enabled: true,
},
FeatureSpec {
id: Feature::DeferImageGeneration,
key: "defer_image_generation",
stage: Stage::UnderDevelopment,
default_enabled: false,
},
FeatureSpec {
id: Feature::ImageGenerationAnimation,
key: "image_generation_animation",