Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Simplify contextual user fragment registration (#36742)

jif7dd2f689e97c
diff --git a/codex-rs/context-fragments/src/fragment.rs b/codex-rs/context-fragments/src/fragment.rs
index b5c4500917..f7fb6821f6 100644
--- a/codex-rs/context-fragments/src/fragment.rs
+++ b/codex-rs/context-fragments/src/fragment.rs
@@ -2,38 +2,6 @@ use codex_protocol::models::ContentItem;
use codex_protocol::models::ResponseInputItem;
use codex_protocol::models::ResponseItem;
-/// Type-erased registration for a contextual user fragment.
-///
-/// Implementations are used by context filtering code to recognize injected
-/// fragments without constructing the concrete context payload.
-pub trait FragmentRegistration: Sync {
- fn matches_text(&self, text: &str) -> bool;
-}
-
-pub struct FragmentRegistrationProxy<T> {
- _marker: std::marker::PhantomData<fn() -> T>,
-}
-
-impl<T> FragmentRegistrationProxy<T> {
- pub const fn new() -> Self {
- Self {
- _marker: std::marker::PhantomData,
- }
- }
-}
-
-impl<T> Default for FragmentRegistrationProxy<T> {
- fn default() -> Self {
- Self::new()
- }
-}
-
-impl<T: ContextualUserFragment> FragmentRegistration for FragmentRegistrationProxy<T> {
- fn matches_text(&self, text: &str) -> bool {
- T::matches_text(text)
- }
-}
-
/// Context payload that is injected as a message fragment.
///
/// Implementations own the response role and provide the exact fragment body.
diff --git a/codex-rs/context-fragments/src/lib.rs b/codex-rs/context-fragments/src/lib.rs
index c95f3001d3..0c189fbfb5 100644
--- a/codex-rs/context-fragments/src/lib.rs
+++ b/codex-rs/context-fragments/src/lib.rs
@@ -4,5 +4,3 @@ mod fragment;
pub use additional_context::AdditionalContextDeveloperFragment;
pub use additional_context::AdditionalContextUserFragment;
pub use fragment::ContextualUserFragment;
-pub use fragment::FragmentRegistration;
-pub use fragment::FragmentRegistrationProxy;
diff --git a/codex-rs/core/src/context/contextual_user_message.rs b/codex-rs/core/src/context/contextual_user_message.rs
index a978a6f129..9a1b5d85ff 100644
--- a/codex-rs/core/src/context/contextual_user_message.rs
+++ b/codex-rs/core/src/context/contextual_user_message.rs
@@ -3,8 +3,7 @@ use codex_protocol::items::parse_hook_prompt_fragment;
use codex_protocol::models::ContentItem;
use super::AdditionalContextUserFragment;
-use super::FragmentRegistration;
-use super::FragmentRegistrationProxy;
+use super::ContextualUserFragment;
use super::InternalModelContextFragment;
use super::LegacyApplyPatchExecCommandWarning;
use super::LegacyModelMismatchWarning;
@@ -17,54 +16,25 @@ use super::UserInstructions;
use super::UserShellCommand;
use super::world_state::EnvironmentsState;
-static USER_INSTRUCTIONS_REGISTRATION: FragmentRegistrationProxy<UserInstructions> =
- FragmentRegistrationProxy::new();
-static ENVIRONMENT_CONTEXT_REGISTRATION: FragmentRegistrationProxy<EnvironmentsState> =
- FragmentRegistrationProxy::new();
-static ADDITIONAL_CONTEXT_REGISTRATION: FragmentRegistrationProxy<AdditionalContextUserFragment> =
- FragmentRegistrationProxy::new();
-static SKILL_INSTRUCTIONS_REGISTRATION: FragmentRegistrationProxy<SkillInstructions> =
- FragmentRegistrationProxy::new();
-static USER_SHELL_COMMAND_REGISTRATION: FragmentRegistrationProxy<UserShellCommand> =
- FragmentRegistrationProxy::new();
-static TURN_ABORTED_REGISTRATION: FragmentRegistrationProxy<TurnAborted> =
- FragmentRegistrationProxy::new();
-static SUBAGENT_NOTIFICATION_REGISTRATION: FragmentRegistrationProxy<SubagentNotification> =
- FragmentRegistrationProxy::new();
-static INTERNAL_MODEL_CONTEXT_REGISTRATION: FragmentRegistrationProxy<
- InternalModelContextFragment,
-> = FragmentRegistrationProxy::new();
-static RECOMMENDED_PLUGINS_REGISTRATION: FragmentRegistrationProxy<RecommendedPluginsInstructions> =
- FragmentRegistrationProxy::new();
-static LEGACY_UNIFIED_EXEC_PROCESS_LIMIT_WARNING_REGISTRATION: FragmentRegistrationProxy<
- LegacyUnifiedExecProcessLimitWarning,
-> = FragmentRegistrationProxy::new();
-static LEGACY_APPLY_PATCH_EXEC_COMMAND_WARNING_REGISTRATION: FragmentRegistrationProxy<
- LegacyApplyPatchExecCommandWarning,
-> = FragmentRegistrationProxy::new();
-static LEGACY_MODEL_MISMATCH_WARNING_REGISTRATION: FragmentRegistrationProxy<
- LegacyModelMismatchWarning,
-> = FragmentRegistrationProxy::new();
-
-static CONTEXTUAL_USER_FRAGMENTS: &[&dyn FragmentRegistration] = &[
- &USER_INSTRUCTIONS_REGISTRATION,
- &ENVIRONMENT_CONTEXT_REGISTRATION,
- &ADDITIONAL_CONTEXT_REGISTRATION,
- &SKILL_INSTRUCTIONS_REGISTRATION,
- &USER_SHELL_COMMAND_REGISTRATION,
- &TURN_ABORTED_REGISTRATION,
- &SUBAGENT_NOTIFICATION_REGISTRATION,
- &INTERNAL_MODEL_CONTEXT_REGISTRATION,
- &RECOMMENDED_PLUGINS_REGISTRATION,
- &LEGACY_UNIFIED_EXEC_PROCESS_LIMIT_WARNING_REGISTRATION,
- &LEGACY_APPLY_PATCH_EXEC_COMMAND_WARNING_REGISTRATION,
- &LEGACY_MODEL_MISMATCH_WARNING_REGISTRATION,
+const CONTEXTUAL_USER_FRAGMENT_MATCHERS: &[fn(&str) -> bool] = &[
+ UserInstructions::matches_text,
+ EnvironmentsState::matches_text,
+ AdditionalContextUserFragment::matches_text,
+ SkillInstructions::matches_text,
+ UserShellCommand::matches_text,
+ TurnAborted::matches_text,
+ SubagentNotification::matches_text,
+ InternalModelContextFragment::matches_text,
+ RecommendedPluginsInstructions::matches_text,
+ LegacyUnifiedExecProcessLimitWarning::matches_text,
+ LegacyApplyPatchExecCommandWarning::matches_text,
+ LegacyModelMismatchWarning::matches_text,
];
fn is_standard_contextual_user_text(text: &str) -> bool {
- CONTEXTUAL_USER_FRAGMENTS
+ CONTEXTUAL_USER_FRAGMENT_MATCHERS
.iter()
- .any(|fragment| fragment.matches_text(text))
+ .any(|matches_text| matches_text(text))
}
pub(crate) fn is_contextual_user_fragment(content_item: &ContentItem) -> bool {
diff --git a/codex-rs/core/src/context/mod.rs b/codex-rs/core/src/context/mod.rs
index d8ef85e3e5..fc874cd6d2 100644
--- a/codex-rs/core/src/context/mod.rs
+++ b/codex-rs/core/src/context/mod.rs
@@ -40,8 +40,6 @@ pub(crate) use available_plugins_instructions::AvailablePluginsInstructions;
pub(crate) use codex_context_fragments::AdditionalContextDeveloperFragment;
pub(crate) use codex_context_fragments::AdditionalContextUserFragment;
pub use codex_context_fragments::ContextualUserFragment;
-pub(crate) use codex_context_fragments::FragmentRegistration;
-pub(crate) use codex_context_fragments::FragmentRegistrationProxy;
pub(crate) use codex_core_skills::SkillInstructions;
pub(crate) use contextual_user_message::is_contextual_user_fragment;
pub(crate) use contextual_user_message::parse_visible_hook_prompt_message;