Files
codex/codex-rs/ext/extension-api/examples/enabled_extensions/shared_state_extension.rs
jif-oai 68e045a631 Make context contributors async (#22491)
## Summary
- make ContextContributor return a boxed Send future
- await context contributors during initial context assembly
- update existing contributors and extension-api examples for the async
contract

## Testing
- cargo test -p codex-extension-api --examples
- cargo test -p codex-git-attribution
- cargo test -p codex-core
build_initial_context_includes_git_attribution_from_extensions --
--nocapture
- cargo test -p codex-core
build_initial_context_omits_git_attribution_when_feature_is_disabled --
--nocapture
- cargo test -p codex-core (fails in unrelated
agent::control::tests::spawn_agent_fork_last_n_turns_keeps_only_recent_turns
stack overflow)
- just fix -p codex-extension-api
- just fix -p codex-git-attribution
- just fix -p codex-core
- cargo clippy -p codex-extension-api --examples
2026-05-13 16:43:28 +02:00

99 lines
2.8 KiB
Rust

use std::sync::Arc;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;
use codex_extension_api::ContextContributor;
use codex_extension_api::ExtensionData;
use codex_extension_api::ExtensionRegistryBuilder;
use codex_extension_api::PromptFragment;
/// Installs the tutorial contributors used by the example host.
pub fn install(registry: &mut ExtensionRegistryBuilder<()>) {
registry.prompt_contributor(Arc::new(StyleContributor));
registry.prompt_contributor(Arc::new(UsageContributor));
}
#[derive(Debug)]
struct StyleContributor;
impl ContextContributor for StyleContributor {
fn contribute<'a>(
&'a self,
session_store: &'a ExtensionData,
thread_store: &'a ExtensionData,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<PromptFragment>> + Send + 'a>> {
Box::pin(async move {
contribution_counts(session_store).record_style();
contribution_counts(thread_store).record_style();
vec![PromptFragment::developer_policy(
"Prefer short answers unless the user asks for detail.",
)]
})
}
}
#[derive(Debug)]
struct UsageContributor;
impl ContextContributor for UsageContributor {
fn contribute<'a>(
&'a self,
session_store: &'a ExtensionData,
thread_store: &'a ExtensionData,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<PromptFragment>> + Send + 'a>> {
Box::pin(async move {
contribution_counts(session_store).record_usage();
contribution_counts(thread_store).record_usage();
vec![PromptFragment::developer_capability(
"This extension can contribute more than one prompt fragment.",
)]
})
}
}
/// Returns how many style contributions were recorded in `store`.
pub fn recorded_style_contributions(store: &ExtensionData) -> u64 {
store
.get::<ContributionCounts>()
.map(|counts| counts.style())
.unwrap_or_default()
}
/// Returns how many usage contributions were recorded in `store`.
pub fn recorded_usage_contributions(store: &ExtensionData) -> u64 {
store
.get::<ContributionCounts>()
.map(|counts| counts.usage())
.unwrap_or_default()
}
#[derive(Debug, Default)]
struct ContributionCounts {
style: AtomicU64,
usage: AtomicU64,
}
impl ContributionCounts {
fn record_style(&self) {
self.style.fetch_add(1, Ordering::Relaxed);
}
fn record_usage(&self) {
self.usage.fetch_add(1, Ordering::Relaxed);
}
fn style(&self) -> u64 {
self.style.load(Ordering::Relaxed)
}
fn usage(&self) -> u64 {
self.usage.load(Ordering::Relaxed)
}
}
fn contribution_counts(store: &ExtensionData) -> Arc<ContributionCounts> {
store.get_or_init::<ContributionCounts>(Default::default)
}