use serde::{Deserialize, Serialize};
use crate::message::{Message, Part, Role};
#[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(default)]
pub struct ContextManager {
pub max_input_tokens: u64,
pub preserve_recent_turns: usize,
pub max_images: usize,
pub preserve_recent_images: usize,
}
impl Default for ContextManager {
fn default() -> Self {
Self {
max_input_tokens: 0,
preserve_recent_turns: 3,
max_images: 30,
preserve_recent_images: 10,
}
}
}
pub(super) const IMAGE_PLACEHOLDER: &str = "[image from an earlier turn removed]";
impl ContextManager {
pub(super) fn truncate_history(&self, history: &mut [Message]) {
if history.is_empty() {
return;
}
let preserve_from = find_preserve_boundary(history, self.preserve_recent_turns);
let start_idx = if history[0].role == Role::System {
1
} else {
0
};
debug_assert!(
preserve_from == 0 || start_idx <= preserve_from,
"start_idx ({start_idx}) > preserve_from ({preserve_from}): \
truncation window would overlap the system message"
);
for msg in history.iter_mut().take(preserve_from).skip(start_idx) {
if msg.role == Role::Tool {
let original_id = msg.id.clone();
let placeholder =
Message::new(Role::Tool).with_contents([Part::text("[context truncated]")]);
*msg = if let Some(id) = original_id {
placeholder.with_id(id)
} else {
placeholder
};
}
}
}
pub(super) fn cap_images(&self, history: &mut [Message]) {
if self.max_images == 0 {
return;
}
let images: Vec<(usize, usize)> = history
.iter()
.enumerate()
.filter(|(_, m)| m.role == Role::Tool)
.flat_map(|(i, m)| {
let parts = m.contents.iter().enumerate();
parts
.filter(|(_, p)| p.is_image())
.map(move |(j, _)| (i, j))
})
.collect();
if images.len() <= self.max_images {
return;
}
let replaced = images.len().saturating_sub(self.preserve_recent_images);
for (i, j) in images.into_iter().take(replaced) {
history[i].contents[j] = Part::text(IMAGE_PLACEHOLDER);
}
}
}
fn find_preserve_boundary(history: &[Message], preserve_recent_turns: usize) -> usize {
if preserve_recent_turns == 0 {
return history.len();
}
let mut turns_found = 0usize;
let mut i = history.len();
while i > 0 {
i -= 1;
if history[i].role == Role::System {
continue;
}
if history[i].role == Role::User {
turns_found += 1;
if turns_found >= preserve_recent_turns {
return i;
}
}
}
0
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
datatype::Value,
message::{Message, Part, Role},
};
fn sys() -> Message {
Message::new(Role::System).with_contents([Part::text("system")])
}
fn user(text: &str) -> Message {
Message::new(Role::User).with_contents([Part::text(text)])
}
fn asst(text: &str) -> Message {
Message::new(Role::Assistant).with_contents([Part::text(text)])
}
fn tool_result(id: &str) -> Message {
Message::new(Role::Tool)
.with_id(id)
.with_contents([Part::value(Value::string("ok".to_string()))])
}
fn tool_call_asst(call_id: &str, tool_name: &str) -> Message {
Message::new(Role::Assistant).with_tool_calls([Part::function(
call_id,
tool_name,
Value::null(),
)])
}
fn image() -> Part {
Part::image_embedded("image/png", vec![0u8; 4].into()).unwrap()
}
fn shot(id: &str) -> Message {
Message::new(Role::Tool)
.with_id(id)
.with_contents([Part::text("the page"), image()])
}
fn user_with_image(text: &str) -> Message {
Message::new(Role::User).with_contents([Part::text(text), image()])
}
fn images_in(history: &[Message]) -> usize {
history
.iter()
.flat_map(|m| &m.contents)
.filter(|p| p.is_image())
.count()
}
fn with_images(max_images: usize, preserve_recent_images: usize) -> ContextManager {
ContextManager {
max_images,
preserve_recent_images,
..Default::default()
}
}
#[test]
fn test_preserve_recent_turns_boundary() {
let history = vec![
sys(),
user("u1"),
asst("a1"),
user("u2"),
asst("a2"),
user("u3"),
asst("a3"),
];
let boundary = find_preserve_boundary(&history, 2);
assert_eq!(boundary, 3, "preserve boundary should be at u2 (index 3)");
}
#[test]
fn test_no_change_when_all_within_preserve_window() {
let mut history = vec![sys(), user("u1"), asst("a1"), user("u2"), asst("a2")];
let cm = ContextManager {
max_input_tokens: 30_000,
preserve_recent_turns: 10,
..Default::default()
};
let original_len = history.len();
cm.truncate_history(&mut history);
assert_eq!(history.len(), original_len, "nothing should change");
}
#[test]
fn test_tool_placeholder_replacement() {
let mut history = vec![
sys(),
user("u1"),
tool_call_asst("call_1", "my_tool"),
tool_result("call_1"),
user("u2"),
asst("a2"),
];
let cm = ContextManager {
max_input_tokens: 30_000,
preserve_recent_turns: 1,
..Default::default()
};
cm.truncate_history(&mut history);
let tool_msg = history.iter().find(|m| m.role == Role::Tool);
assert!(
tool_msg.is_some(),
"Tool message must still exist as placeholder"
);
let tool_msg = tool_msg.unwrap();
assert_eq!(
tool_msg.id.as_deref(),
Some("call_1"),
"tool_use_id must be preserved to avoid Anthropic 400 errors"
);
let content = tool_msg
.contents
.first()
.expect("placeholder must have content");
let val = content
.as_text()
.expect("placeholder content must be a Value part");
assert_eq!(
val, "[context truncated]",
"placeholder content must be '[context truncated]'"
);
}
#[test]
fn test_system_message_never_dropped() {
let mut history = vec![
sys(),
user("u1"),
asst("a1"),
user("u2"),
asst("a2"),
user("u3"),
asst("a3"),
];
let cm = ContextManager {
max_input_tokens: 30_000,
preserve_recent_turns: 1,
..Default::default()
};
cm.truncate_history(&mut history);
assert_eq!(
history[0].role,
Role::System,
"System message must always remain at index 0"
);
assert_eq!(history.len(), 7, "no messages should be dropped");
}
#[test]
fn test_preserve_counts_user_messages_not_assistant() {
let history = vec![
sys(),
user("u1"),
asst("a1"),
user("u2"),
tool_call_asst("call_1", "my_tool"),
tool_result("call_1"),
asst("a2"),
];
let boundary = find_preserve_boundary(&history, 2);
assert_eq!(
boundary, 1,
"preserve_recent_turns=2 must keep 2 user turns, landing at u1 (index 1)"
);
}
#[test]
fn test_preserved_messages_untouched() {
let mut history = vec![
sys(),
user("u1"),
asst("a1"),
user("u2"),
tool_call_asst("call_2", "tool_b"),
tool_result("call_2"),
asst("a2"),
];
let cm = ContextManager {
max_input_tokens: 30_000,
preserve_recent_turns: 1,
..Default::default()
};
cm.truncate_history(&mut history);
let tool_msg = history
.iter()
.find(|m| m.role == Role::Tool)
.expect("tool result must still be present");
let val = tool_msg
.contents
.first()
.and_then(|p| p.as_value())
.expect("tool result content must be a Value part");
assert_eq!(
val.as_str(),
Some("ok"),
"tool result inside preserve window must not be replaced"
);
}
#[test]
fn test_over_the_cap_the_oldest_screenshots_go_until_the_preserved_remain() {
let mut history = vec![sys(), user("u1")];
history.extend((1..=5).map(|n| shot(&format!("c{n}"))));
history.push(asst("a1"));
with_images(4, 2).cap_images(&mut history);
assert_eq!(history[2].contents[1].as_text(), Some(IMAGE_PLACEHOLDER));
assert!(history[2..=4].iter().all(|m| m.contents[1].is_text()));
assert!(history[5..=6].iter().all(|m| m.contents[1].is_image()));
assert_eq!(history[2].contents[0].as_text(), Some("the page"));
assert_eq!(history[2].id.as_deref(), Some("c1"));
}
#[test]
fn test_at_the_cap_nothing_goes() {
let mut history = vec![sys(), user("u1")];
history.extend((1..=4).map(|n| shot(&format!("c{n}"))));
with_images(4, 2).cap_images(&mut history);
assert_eq!(images_in(&history), 4);
}
#[test]
fn test_a_persons_images_are_neither_counted_nor_replaced() {
let mut history = vec![
sys(),
user_with_image("u1"),
user_with_image("u2"),
shot("c1"),
user_with_image("u3"),
shot("c2"),
asst("a1"),
];
with_images(2, 1).cap_images(&mut history);
assert_eq!(images_in(&history), 5);
history.push(shot("c3"));
with_images(2, 1).cap_images(&mut history);
assert_eq!(images_in(&history), 4);
assert!(history[3].contents[1].is_text());
assert!(history[5].contents[1].is_text());
assert!(history[7].contents[1].is_image());
assert!(
history
.iter()
.filter(|m| m.role == Role::User)
.all(|m| m.contents[1].is_image())
);
}
#[test]
fn test_a_cap_of_zero_is_off_and_keeping_none_clears_them() {
let mut history = vec![sys(), user("u1")];
history.extend((1..=12).map(|n| shot(&format!("c{n}"))));
with_images(0, 0).cap_images(&mut history);
assert_eq!(images_in(&history), 12);
with_images(3, 0).cap_images(&mut history);
assert_eq!(images_in(&history), 0);
}
}