use std::sync::Arc; use codex_core::config::Config; use codex_extension_api::CodexExtension; use codex_extension_api::ContextContributor; use codex_extension_api::ExtensionData; use codex_extension_api::ExtensionRegistryBuilder; use codex_extension_api::PromptFragment; use codex_extension_api::ThreadStartContributor; use codex_features::Feature; const DEFAULT_ATTRIBUTION_VALUE: &str = "Codex "; /// Prompt-only extension that contributes the configured git-attribution instruction. #[derive(Clone, Copy, Debug, Default)] pub struct GitAttributionExtension; impl GitAttributionExtension { /// Creates an extension instance. pub fn new() -> Self { Self } } impl ContextContributor for GitAttributionExtension { fn contribute( &self, _session_store: &ExtensionData, thread_store: &ExtensionData, ) -> Vec { let Some(config_store) = thread_store.get::() else { return Vec::new(); }; if !config_store.enabled { return Vec::new(); } build_instruction(config_store.prompt.as_deref()) .map(PromptFragment::developer_policy) .into_iter() .collect() } } #[derive(Clone, Debug, Default)] struct GitAttributionConfig { enabled: bool, prompt: Option, } impl ThreadStartContributor for GitAttributionExtension { fn contribute( &self, config: &Config, _session_store: &ExtensionData, thread_store: &ExtensionData, ) { thread_store.insert(GitAttributionConfig { enabled: config.features.enabled(Feature::CodexGitCommit), prompt: config.commit_attribution.clone(), }); } } impl CodexExtension for GitAttributionExtension { fn install(self: Arc, registry: &mut ExtensionRegistryBuilder) { registry.thread_start_contributor(self.clone()); registry.prompt_contributor(self); } } /// Creates a shared git-attribution extension instance. pub fn extension() -> Arc { Arc::new(GitAttributionExtension::new()) } fn build_commit_message_trailer(config_attribution: Option<&str>) -> Option { let value = resolve_attribution_value(config_attribution)?; Some(format!("Co-authored-by: {value}")) } fn build_instruction(config_attribution: Option<&str>) -> Option { let trailer = build_commit_message_trailer(config_attribution)?; Some(format!( "When you write or edit a git commit message, ensure the message ends with this trailer exactly once:\n{trailer}\n\nRules:\n- Keep existing trailers and append this trailer at the end if missing.\n- Do not duplicate this trailer if it already exists.\n- Keep one blank line between the commit body and trailer block." )) } fn resolve_attribution_value(config_attribution: Option<&str>) -> Option { match config_attribution { Some(value) => { let trimmed = value.trim(); if trimmed.is_empty() { None } else { Some(trimmed.to_string()) } } None => Some(DEFAULT_ATTRIBUTION_VALUE.to_string()), } }