Remove the image generation extension's dependency on codex-core (#63727)

## Why

The extension depended on the full core configuration to resolve a small
set of per-thread settings. Accepting those settings from the host lets
the extension work with other host configuration types.

## What changed

Make the extension generic over any host configuration that can provide
`ImageGenerationSettings`. Resolve the settings and artifact save root
at thread startup and refresh them when the thread configuration changes.

Share the `image_gen` namespace through `codex-config` and the
`x-codex-turn-metadata` header through `codex-api`.

GitOrigin-RevId: 3f10fd0a45c910e87bb6beea7f7c2e6c3f7eb707
This commit is contained in:
jif
2026-11-24 19:54:23 +00:00
committed by copyberry
parent ae21235afb
commit 35ca204afa
11 changed files with 75 additions and 77 deletions
+1 -2
View File
@@ -4059,14 +4059,13 @@ dependencies = [
"base64 0.22.1",
"codex-api",
"codex-attachment-store",
"codex-core",
"codex-config",
"codex-exec-server",
"codex-extension-api",
"codex-extension-items",
"codex-http-client",
"codex-login",
"codex-model-provider",
"codex-model-provider-info",
"codex-protocol",
"codex-tools",
"codex-utils-absolute-path",
+1
View File
@@ -18,6 +18,7 @@ pub(crate) mod telemetry;
/// Path of the Responses endpoint, relative to a provider's base URL.
pub const RESPONSES_PATH: &str = "/responses";
pub use crate::requests::headers::X_CODEX_TURN_METADATA_HEADER;
pub use crate::requests::headers::build_realtime_thread_headers;
pub use crate::requests::headers::build_session_headers;
pub use codex_client::RequestTelemetry;
+3 -1
View File
@@ -4,6 +4,8 @@ use codex_utils_string::to_ascii_json_string;
use http::HeaderMap;
use http::HeaderValue;
pub const X_CODEX_TURN_METADATA_HEADER: &str = "x-codex-turn-metadata";
/// Correlate both the media call and its observer with the same thread and session tree.
pub fn build_realtime_thread_headers(
session_id: String,
@@ -17,7 +19,7 @@ pub fn build_realtime_thread_headers(
&& let Ok(metadata) = to_ascii_json_string(&serde_json::json!({ "thread_source": source }))
&& let Ok(value) = HeaderValue::from_str(&metadata)
{
headers.insert("x-codex-turn-metadata", value);
headers.insert(X_CODEX_TURN_METADATA_HEADER, value);
}
headers
}
+12
View File
@@ -70,6 +70,18 @@ const fn default_enabled() -> bool {
true
}
/// Resolved per-thread settings for the standalone image generation tool.
pub struct ImageGenerationSettings {
pub provider: codex_model_provider_info::ModelProviderInfo,
pub http_client_factory: codex_http_client::HttpClientFactory,
pub gif_enabled: bool,
pub deferred: bool,
}
impl ImageGenerationSettings {
pub const NAMESPACE: &str = "image_gen";
}
/// Last selected grouping in Agent Command Center.
#[derive(Serialize, Deserialize, Debug, Default, Copy, Clone, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "kebab-case")]
+1 -1
View File
@@ -71,6 +71,7 @@ use codex_api::SseTelemetry;
use codex_api::StreamOptions;
use codex_api::TransportError;
use codex_api::WebsocketTelemetry;
pub use codex_api::X_CODEX_TURN_METADATA_HEADER;
use codex_api::auth_header_telemetry;
use codex_api::build_session_headers;
use codex_api::create_text_param_for_request;
@@ -171,7 +172,6 @@ pub const OPENAI_BETA_HEADER: &str = "OpenAI-Beta";
pub const X_CODEX_INSTALLATION_ID_HEADER: &str = "x-codex-installation-id";
pub const X_CODEX_ROUTING_HINT_HEADER: &str = "x-codex-routing-hint";
pub const X_CODEX_TURN_STATE_HEADER: &str = "x-codex-turn-state";
pub const X_CODEX_TURN_METADATA_HEADER: &str = "x-codex-turn-metadata";
pub const X_CODEX_PARENT_THREAD_ID_HEADER: &str = "x-codex-parent-thread-id";
pub const X_CODEX_WINDOW_ID_HEADER: &str = "x-codex-window-id";
pub const X_OPENAI_MEMGEN_REQUEST_HEADER: &str = "x-openai-memgen-request";
+18 -1
View File
@@ -47,6 +47,7 @@ use codex_config::types::ApprovalsReviewer;
use codex_config::types::AuthCredentialsStoreMode;
use codex_config::types::AuthKeyringBackendKind;
use codex_config::types::History;
use codex_config::types::ImageGenerationSettings;
use codex_config::types::McpServerConfig;
use codex_config::types::McpServerTransportConfig;
use codex_config::types::MemoriesConfig;
@@ -1539,6 +1540,22 @@ pub struct TerminalResizeReflowConfig {
pub max_rows: TerminalResizeReflowMaxRows,
}
impl From<&Config> for ImageGenerationSettings {
fn from(config: &Config) -> Self {
Self {
provider: config.model_provider.clone(),
http_client_factory: config.http_client_factory(),
gif_enabled: config.features.image_generation_animation_enabled(),
deferred: config.features.defer_image_generation_enabled()
&& !config
.code_mode
.excluded_tool_namespaces
.iter()
.any(|namespace| namespace == Self::NAMESPACE),
}
}
}
impl AuthManagerConfig for Config {
fn codex_home(&self) -> PathBuf {
self.codex_home.to_path_buf()
@@ -3411,7 +3428,7 @@ fn validate_multi_agent_v2_tool_namespace(namespace: Option<&str>) -> std::io::R
"container",
"file_search",
"functions",
"image_gen",
ImageGenerationSettings::NAMESPACE,
"multi_tool_use",
"python",
"python_user_visible",
+2 -2
View File
@@ -60,6 +60,7 @@ use crate::tools::registry::ToolExposure;
use crate::tools::registry::ToolRegistry;
use crate::tools::router::ToolRouter;
use crate::tools::tool_namespaces_info::collect_tool_namespaces_info;
use codex_config::types::ImageGenerationSettings;
use codex_connectors::apps_config_from_layer_stack;
use codex_extension_api::ExtensionData;
use codex_features::Feature;
@@ -101,7 +102,6 @@ use std::collections::btree_map::Entry;
use std::sync::Arc;
use tracing::instrument;
const IMAGE_GEN_NAMESPACE: &str = "image_gen";
const IMAGEGEN_TOOL_NAME: &str = "imagegen";
#[derive(Clone, Copy)]
@@ -1640,7 +1640,7 @@ fn append_extension_tool_executors(
if is_standalone_web_search && (!standalone_web_search_enabled || !web_search_mode_on) {
continue;
}
if tool_name == ToolName::namespaced(IMAGE_GEN_NAMESPACE, IMAGEGEN_TOOL_NAME)
if tool_name == ToolName::namespaced(ImageGenerationSettings::NAMESPACE, IMAGEGEN_TOOL_NAME)
&& !image_generation_available(turn_context, model_info)
{
continue;
+1 -2
View File
@@ -16,14 +16,13 @@ workspace = true
base64 = { workspace = true }
codex-api = { workspace = true }
codex-attachment-store = { workspace = true }
codex-core = { workspace = true }
codex-config = { workspace = true }
codex-exec-server = { workspace = true }
codex-extension-api = { workspace = true }
codex-extension-items = { workspace = true }
codex-http-client = { workspace = true }
codex-login = { workspace = true }
codex-model-provider = { workspace = true }
codex-model-provider-info = { workspace = true }
codex-protocol = { workspace = true }
codex-tools = { workspace = true }
codex-utils-absolute-path = { workspace = true }
+1 -1
View File
@@ -4,8 +4,8 @@ use codex_api::ImageRequestError;
use codex_api::ImageResponse;
use codex_api::ImagesClient;
use codex_api::ReqwestTransport;
use codex_api::X_CODEX_TURN_METADATA_HEADER;
use codex_api::map_api_error;
use codex_core::X_CODEX_TURN_METADATA_HEADER;
use codex_http_client::ClientRouteClass;
use codex_http_client::HttpClientFactory;
use codex_login::default_client::add_originator_header;
+33 -66
View File
@@ -1,6 +1,6 @@
use std::sync::Arc;
use codex_core::config::Config;
use codex_config::types::ImageGenerationSettings;
use codex_extension_api::ConfigContributor;
use codex_extension_api::ExtensionData;
use codex_extension_api::ExtensionFuture;
@@ -11,89 +11,46 @@ use codex_extension_api::ThreadStartInput;
use codex_extension_api::ToolCall;
use codex_extension_api::ToolContributor;
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;
#[derive(Clone)]
struct ImageGenerationExtension {
struct ImageGenerationExtension<C> {
auth_manager: Arc<AuthManager>,
resolve_save_root: Arc<SaveRootResolver>,
config_from_host: Box<dyn Fn(&C) -> ImageGenerationExtensionConfig + Send + Sync>,
}
type SaveRootResolver = dyn Fn(&Config) -> Option<AbsolutePathBuf> + Send + Sync;
#[derive(Clone)]
struct ImageGenerationExtensionConfig {
available: bool,
gif_enabled: bool,
defer_image_generation: bool,
http_client_factory: HttpClientFactory,
provider: ModelProviderInfo,
settings: ImageGenerationSettings,
save_root: Option<AbsolutePathBuf>,
}
impl ImageGenerationExtensionConfig {
/// Resolves the image provider and save root for a thread.
fn from_config(config: &Config, resolve_save_root: &SaveRootResolver) -> Self {
Self {
available: config.model_provider.is_openai()
|| 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),
}
}
}
impl ThreadLifecycleContributor<Config> for ImageGenerationExtension {
/// Seeds image-generation configuration when a thread begins.
fn on_thread_start<'a>(
&'a self,
input: ThreadStartInput<'a, Config>,
) -> ExtensionFuture<'a, ()> {
impl<C: Sync> ThreadLifecycleContributor<C> for ImageGenerationExtension<C> {
fn on_thread_start<'a>(&'a self, input: ThreadStartInput<'a, C>) -> ExtensionFuture<'a, ()> {
Box::pin(async move {
input
.thread_store
.insert(ImageGenerationExtensionConfig::from_config(
input.config,
self.resolve_save_root.as_ref(),
));
.insert((self.config_from_host)(input.config));
})
}
}
impl ConfigContributor<Config> for ImageGenerationExtension {
/// Refreshes image-generation configuration after thread configuration changes.
impl<C> ConfigContributor<C> for ImageGenerationExtension<C> {
fn on_config_changed(
&self,
_session_store: &ExtensionData,
thread_store: &ExtensionData,
_previous_config: &Config,
new_config: &Config,
_previous_config: &C,
new_config: &C,
) {
thread_store.insert(ImageGenerationExtensionConfig::from_config(
new_config,
self.resolve_save_root.as_ref(),
));
thread_store.insert((self.config_from_host)(new_config));
}
}
impl ToolContributor for ImageGenerationExtension {
impl<C> ToolContributor for ImageGenerationExtension<C> {
/// Creates the image-generation tool exposed by this installed extension.
fn tools(
&self,
@@ -103,7 +60,12 @@ impl ToolContributor for ImageGenerationExtension {
let Some(config) = thread_store.get::<ImageGenerationExtensionConfig>() else {
return Vec::new();
};
if !config.available {
let settings = &config.settings;
let provider = &settings.provider;
if !provider.is_openai()
&& !provider.requires_openai_auth
&& !provider.uses_openai_actor_authorization()
{
return Vec::new();
}
let auth = thread_store
@@ -115,36 +77,41 @@ impl ToolContributor for ImageGenerationExtension {
})
});
let exposure = if config.defer_image_generation {
let exposure = if settings.deferred {
ToolExposure::Deferred
} else {
ToolExposure::Direct
};
vec![Arc::new(ImageGenerationTool::new(
CodexImagesBackend::new(
auth.model_provider(config.provider.clone()),
config.http_client_factory.clone(),
auth.model_provider(provider.clone()),
settings.http_client_factory.clone(),
thread_store
.get::<ThreadOriginator>()
.map(|originator| originator.0.clone()),
),
config.save_root.clone(),
thread_store.level_id().to_string(),
config.gif_enabled,
settings.gif_enabled,
exposure,
))]
}
}
/// Installs the standalone image-generation extension contributors.
pub fn install(
registry: &mut ExtensionRegistryBuilder<Config>,
/// Installs image generation using the host's settings and artifact storage policy.
pub fn install<C: Sync + 'static>(
registry: &mut ExtensionRegistryBuilder<C>,
auth_manager: Arc<AuthManager>,
resolve_save_root: impl Fn(&Config) -> Option<AbsolutePathBuf> + Send + Sync + 'static,
) {
resolve_save_root: impl Fn(&C) -> Option<AbsolutePathBuf> + Send + Sync + 'static,
) where
for<'a> ImageGenerationSettings: From<&'a C>,
{
let extension = Arc::new(ImageGenerationExtension {
auth_manager,
resolve_save_root: Arc::new(resolve_save_root),
config_from_host: Box::new(move |config: &C| ImageGenerationExtensionConfig {
settings: ImageGenerationSettings::from(config),
save_root: resolve_save_root(config),
}),
});
registry.thread_lifecycle_contributor(extension.clone());
registry.config_contributor(extension.clone());
+2 -1
View File
@@ -5,5 +5,6 @@ mod tool;
pub use extension::install;
pub(crate) const IMAGE_GEN_NAMESPACE: &str = "image_gen";
pub(crate) const IMAGE_GEN_NAMESPACE: &str =
codex_config::types::ImageGenerationSettings::NAMESPACE;
pub(crate) const IMAGEGEN_TOOL_NAME: &str = "imagegen";