enact-context 0.0.2

Context window management and compaction for Enact
Documentation
//! # enact-context
//!
//! Context window management and compaction for long-running agentic executions.
//!
//! ## Key Components
//!
//! - **ContextWindow**: Manages the context segments within token limits
//! - **ContextBudget**: Token allocation across different segment types
//! - **Compactor**: Strategies for reducing context size when approaching limits
//! - **TokenCounter**: Token counting using tiktoken
//! - **PromptCalibrator**: Constructs calibrated prompts for spawned callables
//! - **StepContextBuilder**: Extracts learnings when steps are discovered
//! - **ResultCondenser**: Condenses child traces to 1-2k token summaries
//!
//! ## Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │                     ContextWindow                            │
//! ├─────────────────────────────────────────────────────────────┤
//! │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐         │
//! │  │   System    │  │   History   │  │   Tools     │  ...    │
//! │  │  (critical) │  │   (high)    │  │  (medium)   │         │
//! │  └─────────────┘  └─────────────┘  └─────────────┘         │
//! │                                                              │
//! │  ContextBudget: total=128K, used=45K, available=83K         │
//! ├─────────────────────────────────────────────────────────────┤
//! │  Compactor: summarize | truncate | sliding_window           │
//! └─────────────────────────────────────────────────────────────┘
//!
//! ┌─────────────────────────────────────────────────────────────┐
//! │                   Callable Spawning Flow                     │
//! ├─────────────────────────────────────────────────────────────┤
//! │  Parent Context ──► PromptCalibrator ──► Child Prompt       │
//! │       │                                       │              │
//! │       │          StepContextBuilder           │              │
//! │       ▼                 │                     ▼              │
//! │  Step Discovered ──────►│◄───── Child Execution             │
//! │       │                 │              │                     │
//! │       ▼                 ▼              ▼                     │
//! │  Learnings ◄──── ResultCondenser ◄── Child Trace            │
//! └─────────────────────────────────────────────────────────────┘
//! ```
//!
//! ## Usage
//!
//! ```rust,ignore
//! use enact_context::{ContextWindow, ContextBudget, Compactor};
//!
//! // Create a context window with GPT-4 128K budget
//! let budget = ContextBudget::preset_gpt4_128k();
//! let mut window = ContextWindow::new(budget);
//!
//! // Add segments
//! window.add_segment(ContextSegment::system("You are a helpful assistant..."))?;
//! window.add_segment(ContextSegment::user_input("Hello!"))?;
//!
//! // Check if compaction is needed
//! if window.needs_compaction() {
//!     let compactor = Compactor::summarize();
//!     window.compact(&compactor).await?;
//! }
//! ```

pub mod budget;
pub mod calibrator;
pub mod compactor;
pub mod condenser;
pub mod config;
pub mod segment;
pub mod step_context;
pub mod token_counter;
pub mod window;

// Re-exports
pub use budget::{ContextBudget, SegmentBudget};
pub use calibrator::{CalibratedPrompt, CalibrationConfig, PromptCalibrator};
pub use compactor::{CompactionResult, CompactionStrategy, CompactionStrategyType, Compactor};
pub use condenser::{CondensedResult, CondenserConfig, ExecutionTrace, ResultCondenser};
pub use segment::{ContextPriority, ContextSegment, ContextSegmentType};
pub use step_context::{
    StepContextBuilder, StepContextConfig, StepContextResult, StepLearning, ToolCallInfo,
};
pub use token_counter::TokenCounter;
pub use window::ContextWindow;

/// Prelude for common imports
pub mod prelude {
    pub use crate::budget::{ContextBudget, SegmentBudget};
    pub use crate::calibrator::{CalibratedPrompt, CalibrationConfig, PromptCalibrator};
    pub use crate::compactor::{
        CompactionResult, CompactionStrategy, CompactionStrategyType, Compactor,
    };
    pub use crate::condenser::{CondensedResult, CondenserConfig, ExecutionTrace, ResultCondenser};
    pub use crate::segment::{ContextPriority, ContextSegment, ContextSegmentType};
    pub use crate::step_context::{
        StepContextBuilder, StepContextConfig, StepContextResult, StepLearning,
    };
    pub use crate::token_counter::TokenCounter;
    pub use crate::window::ContextWindow;
}