pub struct ContextMinimizer {
pub max_turns: usize,
pub strip_system_prompts: bool,
pub strip_prior_tool_results: bool,
pub max_context_chars: usize,
/* private fields */
}Expand description
Strips unnecessary context from LLM request messages to reduce attack surface.
Implements the context-minimization pattern: only the minimum necessary context is forwarded to the LLM, reducing the window for prompt injection attacks embedded in prior conversation turns.
Fields§
§max_turns: usizeMaximum number of conversation turns to keep.
strip_system_prompts: boolWhether to strip system prompts from forwarded tool contexts.
strip_prior_tool_results: boolWhether to strip prior tool results from context.
max_context_chars: usizeMaximum total context characters.
Implementations§
Source§impl ContextMinimizer
impl ContextMinimizer
Sourcepub fn new(
max_turns: usize,
strip_system_prompts: bool,
strip_prior_tool_results: bool,
max_context_chars: usize,
) -> Self
pub fn new( max_turns: usize, strip_system_prompts: bool, strip_prior_tool_results: bool, max_context_chars: usize, ) -> Self
Create a new context minimizer with custom settings.
Sourcepub fn with_strip_pattern(self, pattern: &str) -> Self
pub fn with_strip_pattern(self, pattern: &str) -> Self
Add a custom strip pattern.
Sourcepub fn minimize_context(&self, messages: &[Message]) -> Vec<Message>
pub fn minimize_context(&self, messages: &[Message]) -> Vec<Message>
Minimize a sequence of messages according to the configured policy.
Applies the following transformations in order:
- Strip system prompts (if configured)
- Strip tool results (if configured)
- Keep only the last
max_turnsuser/assistant pairs - Apply strip patterns to all remaining content
- Truncate to
max_context_charstotal
Sourcepub fn minimize_text(&self, text: &str) -> String
pub fn minimize_text(&self, text: &str) -> String
Strip patterns from a single text string.
Trait Implementations§
Source§impl Debug for ContextMinimizer
impl Debug for ContextMinimizer
Auto Trait Implementations§
impl Freeze for ContextMinimizer
impl RefUnwindSafe for ContextMinimizer
impl Send for ContextMinimizer
impl Sync for ContextMinimizer
impl Unpin for ContextMinimizer
impl UnsafeUnpin for ContextMinimizer
impl UnwindSafe for ContextMinimizer
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
Mutably borrows from an owned value. Read more