Skip to main content

enact_context/
lib.rs

1//! # enact-context
2//!
3//! Context window management and compaction for long-running agentic executions.
4//!
5//! ## Key Components
6//!
7//! - **ContextWindow**: Manages the context segments within token limits
8//! - **ContextBudget**: Token allocation across different segment types
9//! - **Compactor**: Strategies for reducing context size when approaching limits
10//! - **TokenCounter**: Token counting using tiktoken
11//! - **PromptCalibrator**: Constructs calibrated prompts for spawned callables
12//! - **StepContextBuilder**: Extracts learnings when steps are discovered
13//! - **ResultCondenser**: Condenses child traces to 1-2k token summaries
14//!
15//! ## Architecture
16//!
17//! ```text
18//! ┌─────────────────────────────────────────────────────────────┐
19//! │                     ContextWindow                            │
20//! ├─────────────────────────────────────────────────────────────┤
21//! │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐         │
22//! │  │   System    │  │   History   │  │   Tools     │  ...    │
23//! │  │  (critical) │  │   (high)    │  │  (medium)   │         │
24//! │  └─────────────┘  └─────────────┘  └─────────────┘         │
25//! │                                                              │
26//! │  ContextBudget: total=128K, used=45K, available=83K         │
27//! ├─────────────────────────────────────────────────────────────┤
28//! │  Compactor: summarize | truncate | sliding_window           │
29//! └─────────────────────────────────────────────────────────────┘
30//!
31//! ┌─────────────────────────────────────────────────────────────┐
32//! │                   Callable Spawning Flow                     │
33//! ├─────────────────────────────────────────────────────────────┤
34//! │  Parent Context ──► PromptCalibrator ──► Child Prompt       │
35//! │       │                                       │              │
36//! │       │          StepContextBuilder           │              │
37//! │       ▼                 │                     ▼              │
38//! │  Step Discovered ──────►│◄───── Child Execution             │
39//! │       │                 │              │                     │
40//! │       ▼                 ▼              ▼                     │
41//! │  Learnings ◄──── ResultCondenser ◄── Child Trace            │
42//! └─────────────────────────────────────────────────────────────┘
43//! ```
44//!
45//! ## Usage
46//!
47//! ```rust,ignore
48//! use enact_context::{ContextWindow, ContextBudget, Compactor};
49//!
50//! // Create a context window with GPT-4 128K budget
51//! let budget = ContextBudget::preset_gpt4_128k();
52//! let mut window = ContextWindow::new(budget);
53//!
54//! // Add segments
55//! window.add_segment(ContextSegment::system("You are a helpful assistant..."))?;
56//! window.add_segment(ContextSegment::user_input("Hello!"))?;
57//!
58//! // Check if compaction is needed
59//! if window.needs_compaction() {
60//!     let compactor = Compactor::summarize();
61//!     window.compact(&compactor).await?;
62//! }
63//! ```
64
65pub mod budget;
66pub mod calibrator;
67pub mod compactor;
68pub mod condenser;
69pub mod config;
70pub mod segment;
71pub mod step_context;
72pub mod token_counter;
73pub mod window;
74
75// Re-exports
76pub use budget::{ContextBudget, SegmentBudget};
77pub use calibrator::{CalibratedPrompt, CalibrationConfig, PromptCalibrator};
78pub use compactor::{CompactionResult, CompactionStrategy, CompactionStrategyType, Compactor};
79pub use condenser::{CondensedResult, CondenserConfig, ExecutionTrace, ResultCondenser};
80pub use segment::{ContextPriority, ContextSegment, ContextSegmentType};
81pub use step_context::{
82    StepContextBuilder, StepContextConfig, StepContextResult, StepLearning, ToolCallInfo,
83};
84pub use token_counter::TokenCounter;
85pub use window::ContextWindow;
86
87/// Prelude for common imports
88pub mod prelude {
89    pub use crate::budget::{ContextBudget, SegmentBudget};
90    pub use crate::calibrator::{CalibratedPrompt, CalibrationConfig, PromptCalibrator};
91    pub use crate::compactor::{
92        CompactionResult, CompactionStrategy, CompactionStrategyType, Compactor,
93    };
94    pub use crate::condenser::{CondensedResult, CondenserConfig, ExecutionTrace, ResultCondenser};
95    pub use crate::segment::{ContextPriority, ContextSegment, ContextSegmentType};
96    pub use crate::step_context::{
97        StepContextBuilder, StepContextConfig, StepContextResult, StepLearning,
98    };
99    pub use crate::token_counter::TokenCounter;
100    pub use crate::window::ContextWindow;
101}