pub struct ContextWindow {
pub regions: Vec<Region>,
pub current_tokens: usize,
pub max_tokens: usize,
pub region_scripts: HashMap<String, Arc<RegionScript>>,
}Expand description
Context window component storing the agent’s memory regions.
Fields§
§regions: Vec<Region>All regions in this context window
current_tokens: usizeCurrent total token usage
max_tokens: usizeMaximum token budget
region_scripts: HashMap<String, Arc<RegionScript>>Compiled custom-region scripts, keyed by the script path each
RegionKind::Custom carries. Populated once at spawn by the CLI
(which resolves blueprint-dir-relative paths and compile-checks the
files); a stage-layout swap rebuilds regions but leaves this table
untouched, so per-stage custom regions keep working. Empty when no
custom regions exist - every hook lookup then misses and the region
renders its fallback shape.
Implementations§
Source§impl ContextWindow
impl ContextWindow
Sourcepub fn get_region(&self, name: &str) -> Option<&Region>
pub fn get_region(&self, name: &str) -> Option<&Region>
Get a region by name.
Sourcepub fn get_region_mut(&mut self, name: &str) -> Option<&mut Region>
pub fn get_region_mut(&mut self, name: &str) -> Option<&mut Region>
Get a mutable reference to a region by name.
Sourcepub fn add_region(&mut self, region: Region)
pub fn add_region(&mut self, region: Region)
Add a region to this context window.
Sourcepub fn add_to_region(
&mut self,
region_name: &str,
content: String,
tokens: usize,
) -> Result<()>
pub fn add_to_region( &mut self, region_name: &str, content: String, tokens: usize, ) -> Result<()>
Add content to a specific region.
Sourcepub fn replace_region(
&mut self,
region_name: &str,
content: String,
tokens: usize,
) -> bool
pub fn replace_region( &mut self, region_name: &str, content: String, tokens: usize, ) -> bool
Replace a region’s entire content with a single entry (clear, then add).
Returns false (no-op) if the region does not exist. Used to keep an
authoritative document region (e.g. the plan) holding only its current
version, so revisions build on it instead of accumulating stale copies.
Sourcepub fn add_typed_entry(
&mut self,
region_name: &str,
kind: EntryKind,
content: String,
tokens: usize,
) -> Result<()>
pub fn add_typed_entry( &mut self, region_name: &str, kind: EntryKind, content: String, tokens: usize, ) -> Result<()>
Add a typed entry to a specific region.
Like add_to_region but the entry carries an
EntryKind so message roles are determined by type, not text-prefix
parsing.
Sourcepub fn calculate_tokens(&self) -> usize
pub fn calculate_tokens(&self) -> usize
Calculate current token usage across all regions.
Sourcepub fn needs_eviction(&self, threshold: f32) -> bool
pub fn needs_eviction(&self, threshold: f32) -> bool
Check if the context window needs eviction.
Sourcepub fn try_evict(&mut self, target_free_tokens: usize) -> Result<EvictionResult>
pub fn try_evict(&mut self, target_free_tokens: usize) -> Result<EvictionResult>
Execute eviction cascade to free up space.
Returns an EvictionResult with tokens freed and any regions that need
LLM-based compaction. The caller is responsible for performing compaction
on the listed regions (since it requires async LLM access).
Sourcepub fn assemble(&self) -> AssembledContext
pub fn assemble(&self) -> AssembledContext
Result of assembling the context window into system blocks + messages.
System-bound regions become system_blocks; the messages region
becomes messages with proper typed entries (no text-prefix parsing).
Thin wrapper over assemble_with_meta with
no stage metadata - custom-region scripts see empty stage fields.
Sourcepub fn assemble_with_meta(&self, meta: &AssembleMeta) -> AssembledContext
pub fn assemble_with_meta(&self, meta: &AssembleMeta) -> AssembledContext
assemble with stage metadata for custom-region
render(ctx) hooks (stage name, per-stage iteration count, model).
The inference path (build_request) threads real values; other
callers use the default.
Sourcepub fn enable_taint_tracking(&mut self)
pub fn enable_taint_tracking(&mut self)
Enable taint tracking on all regions in this context window.
Sourcepub fn add_tainted_to_region(
&mut self,
region_name: &str,
content: String,
tokens: usize,
taint_level: TaintLevel,
) -> Result<()>
pub fn add_tainted_to_region( &mut self, region_name: &str, content: String, tokens: usize, taint_level: TaintLevel, ) -> Result<()>
Add tainted content to a specific region.
Sourcepub fn add_typed_tainted_to_region(
&mut self,
region_name: &str,
kind: EntryKind,
content: String,
tokens: usize,
taint_level: TaintLevel,
) -> Result<()>
pub fn add_typed_tainted_to_region( &mut self, region_name: &str, kind: EntryKind, content: String, tokens: usize, taint_level: TaintLevel, ) -> Result<()>
Add a typed entry to a region with a specific taint level.
The typed+tainted counterpart of add_typed_entry
and add_tainted_to_region: the entry keeps
its EntryKind (so turn-group eviction stays intact) while contributing
the given taint level (so the taint gate sees sensitive tool output).
Sourcepub fn overall_taint(&self) -> Option<TaintLevel>
pub fn overall_taint(&self) -> Option<TaintLevel>
Get the overall taint level (max across all regions). Returns None if no region has taint tracking enabled.
Sourcepub fn taint_summary(&self) -> Vec<(String, TaintLevel)>
pub fn taint_summary(&self) -> Vec<(String, TaintLevel)>
Get a summary of taint levels across all regions (for dashboard/audit).
Trait Implementations§
Source§impl Clone for ContextWindow
impl Clone for ContextWindow
Source§fn clone(&self) -> ContextWindow
fn clone(&self) -> ContextWindow
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Component for ContextWindow
impl Component for ContextWindow
Source§const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::Table
const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::Table
Source§type Mutability = Mutable
type Mutability = Mutable
Component<Mutability = Mutable>,
while immutable components will instead have Component<Mutability = Immutable>. Read moreSource§fn register_required_components(
_requiree: ComponentId,
required_components: &mut RequiredComponentsRegistrator<'_, '_>,
)
fn register_required_components( _requiree: ComponentId, required_components: &mut RequiredComponentsRegistrator<'_, '_>, )
Source§fn clone_behavior() -> ComponentCloneBehavior
fn clone_behavior() -> ComponentCloneBehavior
Source§fn relationship_accessor() -> Option<ComponentRelationshipAccessor<Self>>
fn relationship_accessor() -> Option<ComponentRelationshipAccessor<Self>>
ComponentRelationshipAccessor required for working with relationships in dynamic contexts. Read moreSource§fn on_add() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
fn on_add() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
Source§fn on_insert() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
fn on_insert() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
Source§fn on_discard() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
fn on_discard() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
Source§fn on_remove() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
fn on_remove() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
Source§fn on_despawn() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
fn on_despawn() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
Source§fn map_entities<E>(_this: &mut Self, _mapper: &mut E)where
E: EntityMapper,
fn map_entities<E>(_this: &mut Self, _mapper: &mut E)where
E: EntityMapper,
EntityMapper. This is used to remap entities in contexts like scenes and entity cloning.
When deriving Component, this is populated by annotating fields containing entities with #[entities] Read moreAuto Trait Implementations§
impl !RefUnwindSafe for ContextWindow
impl !UnwindSafe for ContextWindow
impl Freeze for ContextWindow
impl Send for ContextWindow
impl Sync for ContextWindow
impl Unpin for ContextWindow
impl UnsafeUnpin for ContextWindow
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<C> Bundle for Cwhere
C: Component,
impl<C> Bundle for Cwhere
C: Component,
fn component_ids( components: &mut ComponentsRegistrator<'_>, ) -> impl Iterator<Item = ComponentId> + use<C>
Source§fn get_component_ids(
components: &Components,
) -> impl Iterator<Item = Option<ComponentId>>
fn get_component_ids( components: &Components, ) -> impl Iterator<Item = Option<ComponentId>>
Source§impl<C> BundleFromComponents for Cwhere
C: Component,
impl<C> BundleFromComponents for Cwhere
C: Component,
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ConditionalSend for Twhere
T: Send,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.