claude_agent/context/
mod.rs

1//! Context management with progressive disclosure for optimal token usage.
2
3pub mod builder;
4pub mod import_extractor;
5pub mod level;
6pub mod memory_loader;
7pub mod orchestrator;
8pub mod provider;
9pub mod routing;
10pub mod rule_index;
11pub mod static_context;
12
13pub use crate::types::TokenUsage;
14pub use builder::ContextBuilder;
15pub use import_extractor::ImportExtractor;
16pub use level::{LeveledMemoryProvider, enterprise_base_path, user_base_path};
17pub use memory_loader::{
18    DEFAULT_IMPORT_DEPTH, MAX_IMPORT_DEPTH, MemoryContent, MemoryLoader, MemoryLoaderConfig,
19};
20pub use orchestrator::PromptOrchestrator;
21pub use provider::{FileMemoryProvider, InMemoryProvider, MemoryProvider};
22pub use routing::RoutingStrategy;
23pub use rule_index::RuleIndex;
24pub use static_context::{McpToolMeta, StaticContext};
25
26// Re-export SkillIndex from skills module for convenience
27pub use crate::skills::SkillIndex;
28
29use thiserror::Error;
30
31#[derive(Error, Debug)]
32pub enum ContextError {
33    #[error("Source error: {message}")]
34    Source { message: String },
35
36    #[error("Token budget exceeded: {current} > {limit}")]
37    TokenBudgetExceeded { current: u64, limit: u64 },
38
39    #[error("Skill not found: {name}")]
40    SkillNotFound { name: String },
41
42    #[error("Rule not found: {name}")]
43    RuleNotFound { name: String },
44
45    #[error("Parse error: {message}")]
46    Parse { message: String },
47
48    #[error("IO error: {0}")]
49    Io(#[from] std::io::Error),
50}
51
52pub type ContextResult<T> = std::result::Result<T, ContextError>;
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_context_error_display() {
60        let err = ContextError::SkillNotFound {
61            name: "test-skill".to_string(),
62        };
63        assert!(err.to_string().contains("test-skill"));
64    }
65
66    #[test]
67    fn test_token_budget_error() {
68        let err = ContextError::TokenBudgetExceeded {
69            current: 250_000,
70            limit: 200_000,
71        };
72        assert!(err.to_string().contains("250000"));
73        assert!(err.to_string().contains("200000"));
74    }
75}