use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompactionConfig {
pub provider: String,
pub model: String,
pub system_prompt: Option<String>,
pub user_prompt_template: Option<String>,
pub max_summary_tokens: usize,
pub temperature: f32,
}
impl Default for CompactionConfig {
fn default() -> Self {
Self {
provider: "anthropic".to_string(),
model: "claude-sonnet-4-6".to_string(),
system_prompt: None,
user_prompt_template: None,
max_summary_tokens: 2000,
temperature: 0.2,
}
}
}
impl CompactionConfig {
pub fn system_prompt(&self) -> &str {
self.system_prompt.as_deref().unwrap_or(
"You are a context compaction assistant. Your job is to summarize content \
concisely while preserving all key information, decisions, and actionable items. \
Never lose critical details.",
)
}
pub fn user_prompt(&self, content: &str, region_name: &str) -> String {
if let Some(template) = &self.user_prompt_template {
template
.replace("{content}", content)
.replace("{region_name}", region_name)
} else {
format!(
"Summarize the following content from the \"{}\" context region. \
Preserve key facts, decisions, code snippets, and actionable items. \
Be concise but thorough.\n\n{}",
region_name, content
)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compaction_config_defaults() {
let config = CompactionConfig::default();
assert_eq!(config.max_summary_tokens, 2000);
assert_eq!(config.temperature, 0.2);
assert!(config.system_prompt().contains("compaction assistant"));
}
#[test]
fn test_compaction_config_user_prompt() {
let config = CompactionConfig::default();
let prompt = config.user_prompt("some content here", "conversation");
assert!(prompt.contains("conversation"));
assert!(prompt.contains("some content here"));
}
#[test]
fn test_compaction_config_custom_template() {
let config = CompactionConfig {
user_prompt_template: Some("Region {region_name}: {content}".to_string()),
..Default::default()
};
let prompt = config.user_prompt("hello", "test");
assert_eq!(prompt, "Region test: hello");
}
}