pub struct EnhancedContextManager { /* private fields */ }Expand description
Enhanced context manager with compression, summarization, and statistics.
Manages conversation history with automatic compression when token limits are approached, supports AI-powered summarization, and provides detailed statistics about context usage.
Implementations§
Source§impl EnhancedContextManager
impl EnhancedContextManager
Sourcepub fn new(config: ContextConfig) -> Self
pub fn new(config: ContextConfig) -> Self
Sourcepub fn with_default_config() -> Self
pub fn with_default_config() -> Self
Create a new EnhancedContextManager with default configuration.
Sourcepub fn set_system_prompt(&mut self, prompt: impl Into<String>)
pub fn set_system_prompt(&mut self, prompt: impl Into<String>)
Sourcepub fn system_prompt(&self) -> &str
pub fn system_prompt(&self) -> &str
Get the current system prompt.
Sourcepub fn set_summarizer_client(&mut self, client: Arc<dyn SummarizerClient>)
pub fn set_summarizer_client(&mut self, client: Arc<dyn SummarizerClient>)
Set the summarizer client for AI-powered summarization.
§Arguments
client- The summarizer client implementation
Sourcepub fn has_summarizer_client(&self) -> bool
pub fn has_summarizer_client(&self) -> bool
Check if AI summarization is available.
Sourcepub fn add_turn(
&mut self,
user: Message,
assistant: Message,
api_usage: Option<TokenUsage>,
)
pub fn add_turn( &mut self, user: Message, assistant: Message, api_usage: Option<TokenUsage>, )
Add a conversation turn (user message + assistant response).
Estimates tokens for the turn and optionally applies incremental compression if enabled in the configuration.
§Arguments
user- The user’s messageassistant- The assistant’s responseapi_usage- Optional token usage from the API call
Sourcepub fn turn_count(&self) -> usize
pub fn turn_count(&self) -> usize
Get the number of conversation turns.
Sourcepub fn turns(&self) -> &[ConversationTurn]
pub fn turns(&self) -> &[ConversationTurn]
Get a reference to all conversation turns.
Sourcepub fn turns_mut(&mut self) -> &mut Vec<ConversationTurn>
pub fn turns_mut(&mut self) -> &mut Vec<ConversationTurn>
Get a mutable reference to all conversation turns.
Sourcepub fn get_messages(&self) -> Vec<Message>
pub fn get_messages(&self) -> Vec<Message>
Get all messages for an API call.
Returns messages in the correct order for sending to an LLM:
- System prompt (if set)
- Summary of old turns (if any are summarized)
- All conversation turns (user/assistant pairs)
§Returns
A vector of messages ready for an API call.
Sourcepub fn get_messages_collapsed(&self) -> Vec<Message>
pub fn get_messages_collapsed(&self) -> Vec<Message>
Get messages with tool references collapsed.
Similar to get_messages() but collapses tool_reference blocks
to placeholder text to save tokens.
Sourcepub fn get_used_tokens(&self) -> usize
pub fn get_used_tokens(&self) -> usize
Get the number of tokens currently used in context.
Includes system prompt tokens and all turn tokens.
Sourcepub fn get_available_tokens(&self) -> usize
pub fn get_available_tokens(&self) -> usize
Get the number of available tokens (max - used).
Sourcepub async fn maybe_compress(&mut self) -> Result<(), ContextError>
pub async fn maybe_compress(&mut self) -> Result<(), ContextError>
Check and perform compression if threshold is exceeded.
This method is called automatically after adding turns if auto-compression is needed.
§Returns
Ok(()) if compression was successful or not needed.
Sourcepub async fn compact(&mut self) -> Result<(), ContextError>
pub async fn compact(&mut self) -> Result<(), ContextError>
Force compression of old conversation turns.
Summarizes older turns while keeping recent messages intact. Uses AI summarization if available, otherwise falls back to simple text extraction.
§Returns
Ok(()) if compression was successful.
Sourcepub fn export(&self) -> ContextExport
pub fn export(&self) -> ContextExport
Sourcepub fn import(&mut self, data: ContextExport)
pub fn import(&mut self, data: ContextExport)
Import context state from an export.
Replaces the current state with the imported data.
§Arguments
data- The exported context data to import
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear all conversation history.
Resets turns and statistics but preserves configuration and system prompt.
Sourcepub fn get_stats(&self) -> ContextStats
pub fn get_stats(&self) -> ContextStats
Get statistics about the current context state.
Sourcepub fn get_compression_details(&self) -> CompressionDetails
pub fn get_compression_details(&self) -> CompressionDetails
Get detailed compression information.
Sourcepub fn get_context_usage(&self) -> ContextUsage
pub fn get_context_usage(&self) -> ContextUsage
Get current context usage information.
Sourcepub fn is_near_limit(&self) -> bool
pub fn is_near_limit(&self) -> bool
Check if context is near the limit.
Returns true if usage exceeds the summarize threshold.
Sourcepub fn get_formatted_report(&self) -> String
pub fn get_formatted_report(&self) -> String
Get a formatted statistics report.
Sourcepub fn analyze_compression(&self) -> CompressionResult
pub fn analyze_compression(&self) -> CompressionResult
Analyze compression effectiveness.
Sourcepub fn collapse_tool_references(message: &Message) -> Message
pub fn collapse_tool_references(message: &Message) -> Message
Sourcepub fn config(&self) -> &ContextConfig
pub fn config(&self) -> &ContextConfig
Get a reference to the current configuration.
Sourcepub fn config_mut(&mut self) -> &mut ContextConfig
pub fn config_mut(&mut self) -> &mut ContextConfig
Get a mutable reference to the configuration.
Sourcepub fn set_config(&mut self, config: ContextConfig)
pub fn set_config(&mut self, config: ContextConfig)
Update the configuration.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for EnhancedContextManager
impl !RefUnwindSafe for EnhancedContextManager
impl Send for EnhancedContextManager
impl Sync for EnhancedContextManager
impl Unpin for EnhancedContextManager
impl UnsafeUnpin for EnhancedContextManager
impl !UnwindSafe for EnhancedContextManager
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<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.