agent_works/compression/
config.rs1use tracing;
4
5#[derive(Clone, Debug)]
21pub struct CompressionConfig {
22 pub enabled: bool,
24 pub trigger_tokens: usize,
28 pub keep_recent_messages: usize,
31 pub max_summary_chars: usize,
34 pub max_transcript_chars: usize,
36}
37
38impl Default for CompressionConfig {
39 fn default() -> Self {
40 Self {
41 enabled: true,
42 trigger_tokens: 256_000,
43 keep_recent_messages: 16,
44 max_summary_chars: 10 * 1024, max_transcript_chars: 20 * 1024, }
47 }
48}
49
50impl CompressionConfig {
53 pub fn builder() -> Self {
55 Self::default()
56 }
57
58 pub fn with_enabled(mut self, enabled: bool) -> Self {
59 self.enabled = enabled;
60 self
61 }
62
63 pub fn with_trigger_tokens(mut self, tokens: usize) -> Self {
64 self.trigger_tokens = tokens;
65 self
66 }
67
68 pub fn with_keep_recent_messages(mut self, n: usize) -> Self {
69 self.keep_recent_messages = n;
70 self
71 }
72
73 pub fn with_max_summary_chars(mut self, chars: usize) -> Self {
74 self.max_summary_chars = chars;
75 self
76 }
77
78 pub fn with_max_transcript_chars(mut self, chars: usize) -> Self {
79 self.max_transcript_chars = chars;
80 self
81 }
82
83 pub fn validate_context_window(mut self, context_window: usize) -> Self {
86 if self.trigger_tokens >= context_window {
87 tracing::warn!(
88 trigger_tokens = self.trigger_tokens,
89 context_window,
90 "trigger_tokens >= context_window, clamping to 50% of context_window"
91 );
92 self.trigger_tokens = context_window / 2;
93 }
94 self
95 }
96}
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101
102 #[test]
103 fn test_default_values() {
104 let cfg = CompressionConfig::default();
105 assert!(cfg.enabled);
106 assert_eq!(cfg.trigger_tokens, 256_000);
107 assert_eq!(cfg.keep_recent_messages, 16);
108 assert_eq!(cfg.max_summary_chars, 10 * 1024);
109 assert_eq!(cfg.max_transcript_chars, 20 * 1024);
110 }
111
112 #[test]
113 fn test_builder_chaining() {
114 let cfg = CompressionConfig::builder()
115 .with_enabled(false)
116 .with_trigger_tokens(50_000)
117 .with_keep_recent_messages(20)
118 .with_max_summary_chars(8_000)
119 .with_max_transcript_chars(16_000);
120
121 assert!(!cfg.enabled);
122 assert_eq!(cfg.trigger_tokens, 50_000);
123 assert_eq!(cfg.keep_recent_messages, 20);
124 assert_eq!(cfg.max_summary_chars, 8_000);
125 assert_eq!(cfg.max_transcript_chars, 16_000);
126 }
127}