use std::sync::Arc; use akode_protocol::ThreadId; use akode_protocol::models::ContentItem; use akode_protocol::models::ContentItemMetadata; use akode_protocol::openai_models::ModelInfo; use akode_protocol::protocol::TurnEnvironmentSelection; use serde_json::Value; use crate::ExtensionData; use crate::ExtensionMetrics; /// 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, /// Resolved model metadata captured for this sampling step, retained across discovery. pub model_info: &'a ModelInfo, pub environments: &'a [TurnEnvironmentSelection], /// Metrics bound to the captured model for this sampling step. pub extension_metrics: Option>, pub session_store: &'a ExtensionData, pub thread_store: &'a ExtensionData, pub turn_store: &'a ExtensionData, /// The same captured extension inputs that were used to build this step's tools. pub step_store: &'a ExtensionData, /// Persisted comparison state from the last recorded step. Its rendered text may have /// left model history; use a retained-fragment matcher before referring back to it. /// After compaction, sections may contain only retained extension metadata. pub previous_world_state: Option<&'a serde_json::Map>, } /// What the harness knows about the previous value of one extension-owned section. pub enum PreviousWorldStateSection<'a> { Absent, Unknown, Known(&'a Value), } /// Model-visible content rendered by an extension-owned World State section. /// Markers apply only to `InputText` bodies. /// The producer owns size bounds for the content; rendering does not truncate it. /// Non-text user-role bodies are currently classified as ordinary user media. #[derive(Clone, Debug, PartialEq)] pub struct RenderedWorldStateFragment { role: &'static str, markers: (&'static str, &'static str), body: ContentItem, metadata: ContentItemMetadata, } impl RenderedWorldStateFragment { pub fn new( role: &'static str, markers: (&'static str, &'static str), body: ContentItem, ) -> Self { Self { role, markers, body, metadata: ContentItemMetadata::harness(), } } /// Attach the producer's source before the harness assembles the context message. pub fn with_metadata(mut self, metadata: ContentItemMetadata) -> Self { self.metadata = metadata; self } pub fn metadata(&self) -> &ContentItemMetadata { &self.metadata } pub fn role(&self) -> &'static str { self.role } pub fn markers(&self) -> (&'static str, &'static str) { self.markers } pub fn body(&self) -> &ContentItem { &self.body } } type RenderDiff = dyn for<'a> Fn(PreviousWorldStateSection<'a>) -> (Option, Option) + 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, include_in_full_context: bool, window_prefix: bool, render_diff: Arc, matches_legacy_fragment: Arc, matches_retained_fragment: Option>, } impl WorldStateSectionContribution { /// Return a replacement snapshot and/or fragment. A missing snapshot retains the /// previous section state; a missing fragment emits no model-visible update. A null /// snapshot removes the section from persisted state. pub fn new( id: &'static str, render_diff: impl for<'a> Fn( PreviousWorldStateSection<'a>, ) -> (Option, Option) + Send + Sync + 'static, ) -> Self { Self { id, include_in_full_context: true, window_prefix: false, render_diff: Arc::new(render_diff), matches_legacy_fragment: Arc::new(|_, _| false), matches_retained_fragment: None, } } /// Recompute this section during ordinary step context updates instead of replaying /// its captured value when initial or compacted context is rebuilt. Full rendering /// omits both its fragment and comparison snapshot so a fresh contribution can emit. /// After prefix compaction, the next diff receives [`PreviousWorldStateSection::Unknown`] /// if the section's legacy matcher recognizes text in the retained suffix. pub fn without_full_context(mut self) -> Self { self.include_in_full_context = false; self } pub fn include_in_full_context(&self) -> bool { self.include_in_full_context } 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 } /// Render this section's developer fragments as separate messages before retained /// history when the harness builds a window (including after compaction). Changes /// within an existing window are still appended as ordinary history updates. /// Fragments with any other role are not rendered, including in history updates. /// The producer still owns content size bounds; prefix placement does not truncate. pub fn with_window_prefix(mut self) -> Self { self.window_prefix = true; self } pub fn is_window_prefix(&self) -> bool { self.window_prefix } /// Requires a matching model-visible fragment whenever a persisted snapshot is reused. pub fn with_retained_fragment_matcher( mut self, matcher: impl Fn(&str, &str) -> bool + Send + Sync + 'static, ) -> Self { self.matches_retained_fragment = Some(Arc::new(matcher)); self } pub fn id(&self) -> &'static str { self.id } pub fn render_diff( &self, previous: PreviousWorldStateSection<'_>, ) -> (Option, Option) { (self.render_diff)(previous) } pub fn matches_legacy_fragment(&self, role: &str, text: &str) -> bool { (self.matches_legacy_fragment)(role, text) } pub fn has_retained_fragment_matcher(&self) -> bool { self.matches_retained_fragment.is_some() } pub fn matches_retained_fragment(&self, role: &str, text: &str) -> bool { self.matches_retained_fragment .as_ref() .is_some_and(|matcher| matcher(role, text)) } }