Skip to main content

lean_ctx/core/
compressor.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Generic text compressors.
4//!
5//! The compressors live in the `lean-ctx-compress` crate and are re-exported
6//! here unchanged. This facade adds what needs the engine: compression steered
7//! by the active task intent.
8
9pub use lean_ctx_compress::compressor::*;
10
11/// Compresses content using the active task intent to preserve task-relevant sections.
12pub fn task_aware_compress(
13    content: &str,
14    ext: Option<&str>,
15    intent: &crate::core::intent_engine::StructuredIntent,
16) -> String {
17    use crate::core::intent_engine::{IntentScope, TaskType};
18
19    let budget_ratio = match intent.scope {
20        IntentScope::SingleFile => 0.7,
21        IntentScope::MultiFile => 0.5,
22        IntentScope::CrossModule => 0.35,
23        IntentScope::ProjectWide => 0.25,
24    };
25
26    match intent.task_type {
27        TaskType::FixBug | TaskType::Debug => {
28            let filtered = crate::core::task_relevance::information_bottleneck_filter_typed(
29                content,
30                &intent.keywords,
31                budget_ratio,
32                Some(intent.task_type),
33                &[],
34            );
35            safeguard_ratio(content, &filtered)
36        }
37        TaskType::Refactor | TaskType::Review => {
38            let cleaned = lightweight_cleanup(content);
39            let filtered = crate::core::task_relevance::information_bottleneck_filter_typed(
40                &cleaned,
41                &intent.keywords,
42                budget_ratio.max(0.5),
43                Some(intent.task_type),
44                &[],
45            );
46            safeguard_ratio(content, &filtered)
47        }
48        TaskType::Generate | TaskType::Test => {
49            let compressed = aggressive_compress(content, ext);
50            safeguard_ratio(content, &compressed)
51        }
52        TaskType::Explore | TaskType::Config | TaskType::Deploy => {
53            let cleaned = lightweight_cleanup(content);
54            safeguard_ratio(content, &cleaned)
55        }
56    }
57}