use crate::provider::{ChatMessage, ModelRequest};
#[derive(Debug, Clone)]
pub struct Condensed {
pub request: ModelRequest,
pub dropped: usize,
}
#[must_use]
pub fn condense(req: &ModelRequest, keep_head: usize, keep_tail: usize) -> Option<Condensed> {
let n = req.messages.len();
let kept = keep_head.saturating_add(keep_tail);
if n <= kept.saturating_add(1) {
return None;
}
let dropped = n - kept;
let mut messages: Vec<ChatMessage> = Vec::with_capacity(kept + 1);
messages.extend(req.messages.iter().take(keep_head).cloned());
messages.push(ChatMessage::text(
"user",
format!(
"[{dropped} earlier turns omitted: this conversation exceeded the context window of \
every available model. The opening and the most recent turns are shown. If answering \
depends on the omitted portion, say so rather than guessing.]"
),
));
messages.extend(req.messages.iter().skip(n - keep_tail).cloned());
let mut out = req.clone();
out.messages = messages;
out.raw = serde_json::Value::Null;
Some(Condensed {
request: out,
dropped,
})
}
#[cfg(test)]
mod tests {
use super::*;
fn req(n: usize) -> ModelRequest {
ModelRequest {
model: "anthropic/claude-haiku-4-5".to_owned(),
system: Some("sys".to_owned()),
messages: (0..n)
.map(|i| ChatMessage::text("user", format!("turn {i}")))
.collect(),
max_tokens: 1024,
tools: serde_json::Value::Null,
raw: serde_json::json!({ "messages": "the full original" }),
cache_prefix: false,
}
}
#[test]
fn the_head_and_tail_survive_and_the_middle_goes() {
let c = condense(&req(20), 2, 3).expect("20 turns can be condensed");
assert_eq!(c.dropped, 15);
assert_eq!(c.request.messages.len(), 6);
assert_eq!(c.request.messages[0].text_view(), "turn 0");
assert_eq!(c.request.messages[1].text_view(), "turn 1");
assert!(
c.request.messages[2]
.text_view()
.contains("15 earlier turns omitted")
);
assert_eq!(c.request.messages[5].text_view(), "turn 19");
}
#[test]
fn the_model_is_told_the_history_is_incomplete() {
let c = condense(&req(30), 1, 1).expect("condensable");
let marker = c.request.messages[1].text_view();
assert!(marker.contains("omitted"), "{marker}");
assert!(
marker.contains("say so rather than guessing"),
"the model needs an instruction, not just a notice: {marker}"
);
}
#[test]
fn the_raw_body_is_cleared_or_the_retry_overflows_again() {
let c = condense(&req(20), 2, 2).expect("condensable");
assert!(
c.request.raw.is_null(),
"raw must not survive condensing: {:?}",
c.request.raw
);
}
#[test]
fn a_conversation_with_no_droppable_middle_returns_none() {
assert!(
condense(&req(5), 2, 3).is_none(),
"nothing between head and tail"
);
assert!(
condense(&req(6), 2, 3).is_none(),
"one turn is not worth a retry"
);
assert!(condense(&req(1), 2, 3).is_none());
assert!(condense(&req(7), 2, 3).is_some(), "two spare turns is");
}
#[test]
fn the_system_prompt_and_limits_are_untouched() {
let c = condense(&req(20), 2, 2).expect("condensable");
assert_eq!(c.request.system.as_deref(), Some("sys"));
assert_eq!(c.request.max_tokens, 1024);
assert_eq!(c.request.model, "anthropic/claude-haiku-4-5");
}
}