mirror of
https://github.com/openai/codex.git
synced 2026-11-02 19:43:24 +00:00
## What changed - Remove the step-scoped `ExtensionData` argument from context, turn-input, and tool contributors. - Pass the host's optional `McpResourceClient` through `ThreadStartInput` so extensions can retain session capabilities explicitly. - Keep the MCP resource client in skills-owned session state for catalog loading and skill tools. GitOrigin-RevId: bafa77bcd998aff408d6a396c5fd9ac268c4cce4
124 lines
4.1 KiB
Rust
124 lines
4.1 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_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,
|
|
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(),
|
|
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 ToolExecutor<ToolCall>>> {
|
|
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())),
|
|
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);
|
|
}
|