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::{MAX_IMPORT_DEPTH, MemoryContent, MemoryLoader};
18pub use orchestrator::PromptOrchestrator;
19pub use provider::{FileMemoryProvider, InMemoryProvider, MemoryProvider};
20pub use routing::RoutingStrategy;
21pub use rule_index::RuleIndex;
22pub use static_context::{McpToolMeta, StaticContext};
23
24// Re-export SkillIndex from skills module for convenience
25pub use crate::skills::SkillIndex;
26
27use thiserror::Error;
28
29#[derive(Error, Debug)]
30pub enum ContextError {
31    #[error("Source error: {message}")]
32    Source { message: String },
33
34    #[error("Token budget exceeded: {current} > {limit}")]
35    TokenBudgetExceeded { current: u64, limit: u64 },
36
37    #[error("Skill not found: {name}")]
38    SkillNotFound { name: String },
39
40    #[error("Rule not found: {name}")]
41    RuleNotFound { name: String },
42
43    #[error("Parse error: {message}")]
44    Parse { message: String },
45
46    #[error("IO error: {0}")]
47    Io(#[from] std::io::Error),
48}
49
50pub type ContextResult<T> = std::result::Result<T, ContextError>;
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_context_error_display() {
58        let err = ContextError::SkillNotFound {
59            name: "test-skill".to_string(),
60        };
61        assert!(err.to_string().contains("test-skill"));
62    }
63
64    #[test]
65    fn test_token_budget_error() {
66        let err = ContextError::TokenBudgetExceeded {
67            current: 250_000,
68            limit: 200_000,
69        };
70        assert!(err.to_string().contains("250000"));
71        assert!(err.to_string().contains("200000"));
72    }
73}