Files
codex/codex-rs/ext/image-generation/src/extension.rs
T
jackz 1d87af5faa Honor network policy in history notes and image generation extensions (#47742)
## Why

History notes and image generation requests used default HTTP clients that did not carry the configured application network policy.

## What changed

Pass `Config::http_client_factory()` into both extension backends and create transports with `ClientRouteClass::Api`, so their requests honor application network restrictions and configured proxy behavior.

## Testing

Add regression coverage verifying that denied image requests never reach the server and that history notes context requests stop after the network policy becomes restrictive. Retain coverage for image file access restrictions.

GitOrigin-RevId: 7ba61fdc60fa1afe1842d869da9ccd744ab7d82f
2026-09-24 04:16:35 +00:00

128 lines
4.3 KiB
Rust

use std::sync::Arc;
use codex_core::config::Config;
use codex_extension_api::ConfigContributor;
use codex_extension_api::ExtensionData;
use codex_extension_api::ExtensionFuture;
use codex_extension_api::ExtensionRegistryBuilder;
use codex_extension_api::ThreadLifecycleContributor;
use codex_extension_api::ThreadOriginator;
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::create_model_provider;
use codex_model_provider_info::ModelProviderInfo;
use codex_utils_absolute_path::AbsolutePathBuf;
use crate::backend::CodexImagesBackend;
use crate::tool::ImageGenerationTool;
#[derive(Clone)]
struct ImageGenerationExtension {
auth_manager: Arc<AuthManager>,
resolve_save_root: Arc<SaveRootResolver>,
}
type SaveRootResolver = dyn Fn(&Config) -> Option<AbsolutePathBuf> + Send + Sync;
#[derive(Clone)]
struct ImageGenerationExtensionConfig {
available: bool,
http_client_factory: HttpClientFactory,
provider: ModelProviderInfo,
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(),
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, ()> {
Box::pin(async move {
input
.thread_store
.insert(ImageGenerationExtensionConfig::from_config(
input.config,
self.resolve_save_root.as_ref(),
));
})
}
}
impl ConfigContributor<Config> for ImageGenerationExtension {
/// Refreshes image-generation configuration after thread configuration changes.
fn on_config_changed(
&self,
_session_store: &ExtensionData,
thread_store: &ExtensionData,
_previous_config: &Config,
new_config: &Config,
) {
thread_store.insert(ImageGenerationExtensionConfig::from_config(
new_config,
self.resolve_save_root.as_ref(),
));
}
}
impl ToolContributor for ImageGenerationExtension {
/// Creates the image-generation tool exposed by this installed extension.
fn tools(
&self,
_session_store: &ExtensionData,
thread_store: &ExtensionData,
) -> Vec<Arc<dyn for<'call> ToolExecutor<ToolCall<'call>>>> {
let Some(config) = thread_store.get::<ImageGenerationExtensionConfig>() else {
return Vec::new();
};
if !config.available {
return Vec::new();
}
vec![Arc::new(ImageGenerationTool::new(
CodexImagesBackend::new(
create_model_provider(config.provider.clone(), Some(self.auth_manager.clone())),
config.http_client_factory.clone(),
thread_store
.get::<ThreadOriginator>()
.map(|originator| originator.0.clone()),
),
config.save_root.clone(),
thread_store.level_id().to_string(),
))]
}
}
/// Installs the standalone image-generation extension contributors.
pub fn install(
registry: &mut ExtensionRegistryBuilder<Config>,
auth_manager: Arc<AuthManager>,
resolve_save_root: impl Fn(&Config) -> Option<AbsolutePathBuf> + Send + Sync + 'static,
) {
let extension = Arc::new(ImageGenerationExtension {
auth_manager,
resolve_save_root: Arc::new(resolve_save_root),
});
registry.thread_lifecycle_contributor(extension.clone());
registry.config_contributor(extension.clone());
registry.tool_contributor(extension);
}