Skip to main content

aster/context/
mod.rs

1//! Context Management Module
2//!
3//! This module provides comprehensive context management functionality aligned with
4//!
5//! - Token estimation for different content types
6//! - Dynamic context window management
7//! - Intelligent message summarization
8//! - Message compression
9//! - Prompt caching support
10//! - Message priority sorting
11//! - File mention resolution
12//! - AGENTS.md parsing
13//!
14//! # Architecture
15//!
16//! The module is organized into the following components:
17//!
18//! - `types`: Core type definitions (TokenUsage, ContextConfig, ConversationTurn, etc.)
19//! - `token_estimator`: Token estimation for different content types
20//! - `window_manager`: Dynamic context window management
21//! - `summarizer`: Intelligent message summarization
22//! - `compressor`: Message compression
23//! - `cache_controller`: Prompt caching support
24//! - `priority_sorter`: Message priority sorting
25//! - `file_mention`: File mention resolution
26//! - `agents_md_parser`: AGENTS.md parsing
27//! - `manager`: Enhanced context manager
28//!
29//! # Quick Start
30//!
31//! ```rust,ignore
32//! use aster::context::{EnhancedContextManager, ContextConfig, TokenEstimator};
33//!
34//! // Create a context manager with default configuration
35//! let mut manager = EnhancedContextManager::new(ContextConfig::default());
36//! manager.set_system_prompt("You are a helpful assistant.");
37//!
38//! // Add conversation turns
39//! manager.add_turn(user_message, assistant_message, Some(usage));
40//!
41//! // Get messages for API call
42//! let messages = manager.get_messages();
43//!
44//! // Check context usage
45//! let usage = manager.get_context_usage();
46//! println!("Context usage: {:.1}%", usage.percentage);
47//! ```
48//!
49//! # Token Estimation
50//!
51//! ```rust,ignore
52//! use aster::context::TokenEstimator;
53//!
54//! let tokens = TokenEstimator::estimate_tokens("Hello, world!");
55//! let message_tokens = TokenEstimator::estimate_message_tokens(&message);
56//! ```
57//!
58//! # Message Compression
59//!
60//! ```rust,ignore
61//! use aster::context::{MessageCompressor, CompressionConfig};
62//!
63//! let compressed = MessageCompressor::compress_code_block(&code, 50);
64//! let compressed_msg = MessageCompressor::compress_message(&message, &config);
65//! ```
66
67// ============================================================================
68// Module Declarations
69// ============================================================================
70
71pub mod agents_md_parser;
72pub mod cache_controller;
73pub mod compressor;
74pub mod file_mention;
75pub mod manager;
76pub mod priority_sorter;
77pub mod pruner;
78pub mod summarizer;
79pub mod token_estimator;
80pub mod types;
81pub mod window_manager;
82
83#[cfg(test)]
84mod token_estimator_property_tests;
85
86#[cfg(test)]
87mod compressor_property_tests;
88
89#[cfg(test)]
90mod summarizer_property_tests;
91
92// ============================================================================
93// Re-exports: Core Components
94// ============================================================================
95
96/// Token estimation for different content types (Asian, code, English text)
97pub use token_estimator::TokenEstimator;
98
99/// Dynamic context window management for different LLM models
100pub use window_manager::{ContextWindowManager, MODEL_CONTEXT_WINDOWS};
101
102/// Message compression (code blocks, tool output, file content)
103pub use compressor::{
104    MessageCompressor,
105    // Compression constants
106    DEFAULT_CODE_BLOCK_MAX_LINES,
107    DEFAULT_FILE_CONTENT_MAX_CHARS,
108    DEFAULT_TOOL_OUTPUT_MAX_CHARS,
109};
110
111/// Progressive pruning for Tool output management
112pub use pruner::ProgressivePruner;
113
114/// Intelligent message summarization (AI-powered and simple)
115pub use summarizer::{
116    Summarizer,
117    SummarizerClient,
118    SummarizerResponse,
119    // Summarizer constants
120    DEFAULT_SUMMARY_BUDGET,
121    MAX_SUMMARY_LENGTH,
122    SUMMARY_SYSTEM_PROMPT,
123};
124
125/// Prompt caching support for reducing API costs
126pub use cache_controller::{CacheController, CacheEligibility};
127
128/// Message priority sorting for intelligent compression decisions
129pub use priority_sorter::PrioritySorter;
130
131/// File mention resolution (@filename syntax)
132pub use file_mention::{FileMentionResolver, COMMON_EXTENSIONS};
133
134/// AGENTS.md parsing for project-specific instructions
135pub use agents_md_parser::AgentsMdParser;
136
137/// Enhanced context manager with compression, summarization, and statistics
138pub use manager::EnhancedContextManager;
139
140// ============================================================================
141// Re-exports: Types
142// ============================================================================
143
144pub use types::{
145    // File mention types
146    AgentsMdConfig,
147    // Cache types
148    CacheConfig,
149    CacheControl,
150    CacheSavings,
151    CacheStats,
152    CacheType,
153    // Compression types
154    CodeBlock,
155    CompressionConfig,
156    CompressionDetails,
157    CompressionResult,
158    // Core types
159    ContextConfig,
160    ContextError,
161    ContextExport,
162    ContextStats,
163    ContextUsage,
164    // Window types
165    ContextWindowStats,
166    ConversationTurn,
167    FileMentionResult,
168    // Priority types
169    MessagePriority,
170    PrioritizedMessage,
171    // Progressive pruning types
172    PruningConfig,
173    PruningLevel,
174    ResolvedFile,
175    TokenUsage,
176    // Constants from types module
177    CHARS_PER_TOKEN_ASIAN,
178    CHARS_PER_TOKEN_CODE,
179    CHARS_PER_TOKEN_DEFAULT,
180    CODE_BLOCK_MAX_LINES,
181    FILE_CONTENT_MAX_CHARS,
182    TOOL_OUTPUT_MAX_CHARS,
183};
184
185// ============================================================================
186// Convenience Type Aliases
187// ============================================================================
188
189/// Result type for context operations
190pub type ContextResult<T> = std::result::Result<T, ContextError>;