mirror of
https://github.com/openai/codex.git
synced 2026-10-28 18:58:46 +00:00
97 lines
3.3 KiB
Rust
97 lines
3.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::ExtensionRegistryBuilder;
|
|
use codex_extension_api::ThreadLifecycleContributor;
|
|
use codex_extension_api::ThreadStartInput;
|
|
use codex_extension_api::ToolCall;
|
|
use codex_extension_api::ToolContributor;
|
|
use codex_extension_api::ToolExecutor;
|
|
use codex_features::Feature;
|
|
use codex_http_state::HttpStateContext;
|
|
use codex_http_state::HttpStateSurface;
|
|
use codex_login::AuthManager;
|
|
use codex_model_provider::create_model_provider;
|
|
use codex_model_provider_info::ModelProviderInfo;
|
|
|
|
use crate::backend::CodexImagesBackend;
|
|
use crate::tool::ImageGenerationTool;
|
|
|
|
#[derive(Clone)]
|
|
struct ImageGenerationExtension {
|
|
auth_manager: Arc<AuthManager>,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
struct ImageGenerationExtensionConfig {
|
|
enabled: bool,
|
|
provider: ModelProviderInfo,
|
|
}
|
|
|
|
impl From<&Config> for ImageGenerationExtensionConfig {
|
|
/// Resolves whether standalone image generation should be available for a thread.
|
|
fn from(config: &Config) -> Self {
|
|
Self {
|
|
enabled: config.features.enabled(Feature::ImageGenExt)
|
|
&& config.model_provider.is_openai(),
|
|
provider: config.model_provider.clone(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[async_trait::async_trait]
|
|
impl ThreadLifecycleContributor<Config> for ImageGenerationExtension {
|
|
/// Seeds image-generation availability when a thread begins.
|
|
async fn on_thread_start(&self, input: ThreadStartInput<'_, Config>) {
|
|
input
|
|
.thread_store
|
|
.insert(ImageGenerationExtensionConfig::from(input.config));
|
|
}
|
|
}
|
|
|
|
impl ConfigContributor<Config> for ImageGenerationExtension {
|
|
/// Refreshes image-generation availability 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(new_config));
|
|
}
|
|
}
|
|
|
|
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 ToolExecutor<ToolCall>>> {
|
|
let Some(config) = thread_store.get::<ImageGenerationExtensionConfig>() else {
|
|
return Vec::new();
|
|
};
|
|
if !config.enabled || !self.auth_manager.current_auth_uses_codex_backend() {
|
|
return Vec::new();
|
|
}
|
|
|
|
vec![Arc::new(ImageGenerationTool::new(CodexImagesBackend::new(
|
|
create_model_provider(config.provider.clone(), Some(self.auth_manager.clone())),
|
|
thread_store.get::<HttpStateSurface>().map(|surface| {
|
|
HttpStateContext::new(self.auth_manager.codex_home().to_path_buf(), *surface)
|
|
}),
|
|
)))]
|
|
}
|
|
}
|
|
|
|
/// Installs the feature-gated standalone image-generation extension contributors.
|
|
pub fn install(registry: &mut ExtensionRegistryBuilder<Config>, auth_manager: Arc<AuthManager>) {
|
|
let extension = Arc::new(ImageGenerationExtension { auth_manager });
|
|
registry.thread_lifecycle_contributor(extension.clone());
|
|
registry.config_contributor(extension.clone());
|
|
registry.tool_contributor(extension);
|
|
}
|