-
Notifications
You must be signed in to change notification settings - Fork 15.5k
Let extensions contribute World State sections #30100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
codex-rs/ext/extension-api/src/contributors/world_state.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use codex_protocol::ThreadId; | ||
| use codex_protocol::protocol::TurnEnvironmentSelection; | ||
| use serde_json::Value; | ||
|
|
||
| use crate::ExtensionData; | ||
|
|
||
| /// Host state available while an extension contributes one sampling step's World State. | ||
| pub struct WorldStateContributionInput<'a> { | ||
| pub thread_id: ThreadId, | ||
| pub turn_id: &'a str, | ||
| pub environments: &'a [TurnEnvironmentSelection], | ||
| pub session_store: &'a ExtensionData, | ||
| pub thread_store: &'a ExtensionData, | ||
| pub turn_store: &'a ExtensionData, | ||
| } | ||
|
|
||
| /// What the harness knows about the previous value of one extension-owned section. | ||
| pub enum PreviousWorldStateSection<'a> { | ||
| Absent, | ||
| Unknown, | ||
| Known(&'a Value), | ||
| } | ||
|
|
||
| /// Plain model-visible data rendered by an extension-owned World State section. | ||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| pub struct RenderedWorldStateFragment { | ||
| role: &'static str, | ||
| markers: (&'static str, &'static str), | ||
| body: String, | ||
| } | ||
|
|
||
| impl RenderedWorldStateFragment { | ||
| pub fn new( | ||
| role: &'static str, | ||
|
jif-oai marked this conversation as resolved.
|
||
| markers: (&'static str, &'static str), | ||
| body: impl Into<String>, | ||
| ) -> Self { | ||
| Self { | ||
| role, | ||
| markers, | ||
| body: body.into(), | ||
| } | ||
| } | ||
|
|
||
| pub fn role(&self) -> &'static str { | ||
| self.role | ||
| } | ||
|
|
||
| pub fn markers(&self) -> (&'static str, &'static str) { | ||
| self.markers | ||
| } | ||
|
|
||
| pub fn body(&self) -> &str { | ||
| &self.body | ||
| } | ||
| } | ||
|
|
||
| type RenderDiff = dyn for<'a> Fn(PreviousWorldStateSection<'a>) -> Option<RenderedWorldStateFragment> | ||
| + Send | ||
| + Sync; | ||
| type LegacyFragmentMatcher = dyn Fn(&str, &str) -> bool + Send + Sync; | ||
|
|
||
| /// One extension-owned World State section captured for a sampling step. | ||
| /// | ||
| /// The extension owns the stable ID, comparison snapshot, and diff rendering. The harness owns | ||
| /// persistence and the concrete model-context fragment envelope. | ||
| #[derive(Clone)] | ||
| pub struct WorldStateSectionContribution { | ||
| id: &'static str, | ||
| snapshot: Value, | ||
| render_diff: Arc<RenderDiff>, | ||
| matches_legacy_fragment: Arc<LegacyFragmentMatcher>, | ||
| } | ||
|
|
||
| impl WorldStateSectionContribution { | ||
| pub fn new( | ||
| id: &'static str, | ||
| snapshot: Value, | ||
| render_diff: impl for<'a> Fn( | ||
| PreviousWorldStateSection<'a>, | ||
| ) -> Option<RenderedWorldStateFragment> | ||
| + Send | ||
| + Sync | ||
| + 'static, | ||
| ) -> Self { | ||
| Self { | ||
| id, | ||
| snapshot, | ||
| render_diff: Arc::new(render_diff), | ||
| matches_legacy_fragment: Arc::new(|_, _| false), | ||
| } | ||
| } | ||
|
|
||
| pub fn with_legacy_matcher( | ||
| mut self, | ||
| matcher: impl Fn(&str, &str) -> bool + Send + Sync + 'static, | ||
| ) -> Self { | ||
| self.matches_legacy_fragment = Arc::new(matcher); | ||
| self | ||
| } | ||
|
|
||
| pub fn id(&self) -> &'static str { | ||
| self.id | ||
| } | ||
|
|
||
| pub fn snapshot(&self) -> &Value { | ||
| &self.snapshot | ||
| } | ||
|
|
||
| pub fn render_diff( | ||
| &self, | ||
| previous: PreviousWorldStateSection<'_>, | ||
| ) -> Option<RenderedWorldStateFragment> { | ||
| (self.render_diff)(previous) | ||
| } | ||
|
|
||
| pub fn matches_legacy_fragment(&self, role: &str, text: &str) -> bool { | ||
| (self.matches_legacy_fragment)(role, text) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.