Skip to main content

stynx_code_compact/
auto_compact.rs

1use stynx_code_errors::AppResult;
2use stynx_code_types::{Conversation, Provider};
3
4use crate::full_compact::FullCompactor;
5
6pub struct AutoCompactor {
7
8    pub threshold: f64,
9    full: FullCompactor,
10}
11
12impl Default for AutoCompactor {
13    fn default() -> Self {
14        Self {
15            threshold: 0.80,
16            full: FullCompactor::new(),
17        }
18    }
19}
20
21impl AutoCompactor {
22    pub fn new(threshold: f64) -> Self {
23        Self {
24            threshold,
25            full: FullCompactor::new(),
26        }
27    }
28
29    pub fn should_compact(&self, current_tokens: u64, limit: u64) -> bool {
30        if limit == 0 {
31            return false;
32        }
33        (current_tokens as f64 / limit as f64) >= self.threshold
34    }
35
36    pub async fn compact(
37        &self,
38        conversation: &Conversation,
39        provider: &dyn Provider,
40    ) -> AppResult<Conversation> {
41        self.full.compact(conversation, provider).await
42    }
43}