|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use codex_protocol::ThreadId; |
| 4 | +use codex_protocol::protocol::TurnEnvironmentSelection; |
| 5 | +use serde_json::Value; |
| 6 | + |
| 7 | +use crate::ExtensionData; |
| 8 | + |
| 9 | +/// Host state available while an extension contributes one sampling step's World State. |
| 10 | +pub struct WorldStateContributionInput<'a> { |
| 11 | + pub thread_id: ThreadId, |
| 12 | + pub turn_id: &'a str, |
| 13 | + pub environments: &'a [TurnEnvironmentSelection], |
| 14 | + pub session_store: &'a ExtensionData, |
| 15 | + pub thread_store: &'a ExtensionData, |
| 16 | + pub turn_store: &'a ExtensionData, |
| 17 | +} |
| 18 | + |
| 19 | +/// What the harness knows about the previous value of one extension-owned section. |
| 20 | +pub enum PreviousWorldStateSection<'a> { |
| 21 | + Absent, |
| 22 | + Unknown, |
| 23 | + Known(&'a Value), |
| 24 | +} |
| 25 | + |
| 26 | +/// Plain model-visible data rendered by an extension-owned World State section. |
| 27 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 28 | +pub struct RenderedWorldStateFragment { |
| 29 | + role: &'static str, |
| 30 | + markers: (&'static str, &'static str), |
| 31 | + body: String, |
| 32 | +} |
| 33 | + |
| 34 | +impl RenderedWorldStateFragment { |
| 35 | + pub fn new( |
| 36 | + role: &'static str, |
| 37 | + markers: (&'static str, &'static str), |
| 38 | + body: impl Into<String>, |
| 39 | + ) -> Self { |
| 40 | + Self { |
| 41 | + role, |
| 42 | + markers, |
| 43 | + body: body.into(), |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + pub fn role(&self) -> &'static str { |
| 48 | + self.role |
| 49 | + } |
| 50 | + |
| 51 | + pub fn markers(&self) -> (&'static str, &'static str) { |
| 52 | + self.markers |
| 53 | + } |
| 54 | + |
| 55 | + pub fn body(&self) -> &str { |
| 56 | + &self.body |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +type RenderDiff = dyn for<'a> Fn(PreviousWorldStateSection<'a>) -> Option<RenderedWorldStateFragment> |
| 61 | + + Send |
| 62 | + + Sync; |
| 63 | +type LegacyFragmentMatcher = dyn Fn(&str, &str) -> bool + Send + Sync; |
| 64 | + |
| 65 | +/// One extension-owned World State section captured for a sampling step. |
| 66 | +/// |
| 67 | +/// The extension owns the stable ID, comparison snapshot, and diff rendering. The harness owns |
| 68 | +/// persistence and the concrete model-context fragment envelope. |
| 69 | +#[derive(Clone)] |
| 70 | +pub struct WorldStateSectionContribution { |
| 71 | + id: &'static str, |
| 72 | + snapshot: Value, |
| 73 | + render_diff: Arc<RenderDiff>, |
| 74 | + matches_legacy_fragment: Arc<LegacyFragmentMatcher>, |
| 75 | +} |
| 76 | + |
| 77 | +impl WorldStateSectionContribution { |
| 78 | + pub fn new( |
| 79 | + id: &'static str, |
| 80 | + snapshot: Value, |
| 81 | + render_diff: impl for<'a> Fn( |
| 82 | + PreviousWorldStateSection<'a>, |
| 83 | + ) -> Option<RenderedWorldStateFragment> |
| 84 | + + Send |
| 85 | + + Sync |
| 86 | + + 'static, |
| 87 | + ) -> Self { |
| 88 | + Self { |
| 89 | + id, |
| 90 | + snapshot, |
| 91 | + render_diff: Arc::new(render_diff), |
| 92 | + matches_legacy_fragment: Arc::new(|_, _| false), |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + pub fn with_legacy_matcher( |
| 97 | + mut self, |
| 98 | + matcher: impl Fn(&str, &str) -> bool + Send + Sync + 'static, |
| 99 | + ) -> Self { |
| 100 | + self.matches_legacy_fragment = Arc::new(matcher); |
| 101 | + self |
| 102 | + } |
| 103 | + |
| 104 | + pub fn id(&self) -> &'static str { |
| 105 | + self.id |
| 106 | + } |
| 107 | + |
| 108 | + pub fn snapshot(&self) -> &Value { |
| 109 | + &self.snapshot |
| 110 | + } |
| 111 | + |
| 112 | + pub fn render_diff( |
| 113 | + &self, |
| 114 | + previous: PreviousWorldStateSection<'_>, |
| 115 | + ) -> Option<RenderedWorldStateFragment> { |
| 116 | + (self.render_diff)(previous) |
| 117 | + } |
| 118 | + |
| 119 | + pub fn matches_legacy_fragment(&self, role: &str, text: &str) -> bool { |
| 120 | + (self.matches_legacy_fragment)(role, text) |
| 121 | + } |
| 122 | +} |
0 commit comments