agent_sdk/context/
config.rs

1//! Configuration for context compaction.
2
3use serde::{Deserialize, Serialize};
4
5/// Configuration for context compaction.
6///
7/// Controls when and how context compaction occurs.
8///
9/// # Example
10///
11/// ```
12/// use agent_sdk::context::CompactionConfig;
13///
14/// let config = CompactionConfig::default()
15///     .with_threshold_tokens(100_000)
16///     .with_retain_recent(20);
17/// ```
18#[derive(Clone, Debug, Serialize, Deserialize)]
19pub struct CompactionConfig {
20    /// Token threshold to trigger compaction.
21    /// When estimated tokens exceed this, compaction is triggered.
22    /// Default: 80,000 (conservative for 128K context models)
23    pub threshold_tokens: usize,
24
25    /// Number of recent messages to keep intact (not summarized).
26    /// These messages remain in full to preserve immediate context.
27    /// Default: 10
28    pub retain_recent: usize,
29
30    /// Minimum messages before compaction is considered.
31    /// Prevents compaction when conversation is still short.
32    /// Default: 20
33    pub min_messages_for_compaction: usize,
34
35    /// Whether to automatically compact when threshold is reached.
36    /// If false, compaction only occurs on explicit request.
37    /// Default: true
38    pub auto_compact: bool,
39}
40
41impl Default for CompactionConfig {
42    fn default() -> Self {
43        Self {
44            threshold_tokens: 80_000,
45            retain_recent: 10,
46            min_messages_for_compaction: 20,
47            auto_compact: true,
48        }
49    }
50}
51
52impl CompactionConfig {
53    /// Create a new configuration with default values.
54    #[must_use]
55    pub fn new() -> Self {
56        Self::default()
57    }
58
59    /// Set the token threshold for compaction.
60    #[must_use]
61    pub const fn with_threshold_tokens(mut self, threshold: usize) -> Self {
62        self.threshold_tokens = threshold;
63        self
64    }
65
66    /// Set the number of recent messages to retain.
67    #[must_use]
68    pub const fn with_retain_recent(mut self, count: usize) -> Self {
69        self.retain_recent = count;
70        self
71    }
72
73    /// Set the minimum messages for compaction.
74    #[must_use]
75    pub const fn with_min_messages(mut self, count: usize) -> Self {
76        self.min_messages_for_compaction = count;
77        self
78    }
79
80    /// Set whether to auto-compact.
81    #[must_use]
82    pub const fn with_auto_compact(mut self, auto: bool) -> Self {
83        self.auto_compact = auto;
84        self
85    }
86}
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91
92    #[test]
93    fn test_default_config() {
94        let config = CompactionConfig::default();
95        assert_eq!(config.threshold_tokens, 80_000);
96        assert_eq!(config.retain_recent, 10);
97        assert_eq!(config.min_messages_for_compaction, 20);
98        assert!(config.auto_compact);
99    }
100
101    #[test]
102    fn test_builder_pattern() {
103        let config = CompactionConfig::new()
104            .with_threshold_tokens(50_000)
105            .with_retain_recent(5)
106            .with_min_messages(10)
107            .with_auto_compact(false);
108
109        assert_eq!(config.threshold_tokens, 50_000);
110        assert_eq!(config.retain_recent, 5);
111        assert_eq!(config.min_messages_for_compaction, 10);
112        assert!(!config.auto_compact);
113    }
114}