keep 2 most recent images, age out the rest
age_out_images now keeps 1 existing image + 1 about to be added = 2 live images for motion/comparison. Previously aged all to 1. Reduces image bloat in conversation log and context. Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
parent
3f3db9ce26
commit
41b3f50c91
1 changed files with 19 additions and 18 deletions
|
|
@ -897,33 +897,34 @@ impl Agent {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Replace base64 image data in older messages with text placeholders.
|
/// Replace base64 image data in older messages with text placeholders.
|
||||||
/// Only the most recent image stays live — each new image ages out
|
/// Keeps the 2 most recent images live (enough for motion/comparison).
|
||||||
/// all previous ones. The tool result message (right before each image
|
/// The tool result message before each image records what was loaded.
|
||||||
/// message) already records what was loaded, so no info is lost.
|
|
||||||
fn age_out_images(&mut self) {
|
fn age_out_images(&mut self) {
|
||||||
for entry in &mut self.context.entries {
|
// Find image entries newest-first, skip 1 (caller is about to add another)
|
||||||
let msg = entry.message_mut();
|
let to_age: Vec<usize> = self.context.entries.iter().enumerate()
|
||||||
|
.rev()
|
||||||
|
.filter(|(_, e)| {
|
||||||
|
if let Some(MessageContent::Parts(parts)) = &e.message().content {
|
||||||
|
parts.iter().any(|p| matches!(p, ContentPart::ImageUrl { .. }))
|
||||||
|
} else { false }
|
||||||
|
})
|
||||||
|
.map(|(i, _)| i)
|
||||||
|
.skip(1) // keep 1 existing + 1 about to be added = 2 live
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
for i in to_age {
|
||||||
|
let msg = self.context.entries[i].message_mut();
|
||||||
if let Some(MessageContent::Parts(parts)) = &msg.content {
|
if let Some(MessageContent::Parts(parts)) = &msg.content {
|
||||||
let has_images = parts.iter().any(|p| matches!(p, ContentPart::ImageUrl { .. }));
|
|
||||||
if !has_images {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let mut replacement = String::new();
|
let mut replacement = String::new();
|
||||||
for part in parts {
|
for part in parts {
|
||||||
match part {
|
match part {
|
||||||
ContentPart::Text { text } => {
|
ContentPart::Text { text } => {
|
||||||
if !replacement.is_empty() {
|
if !replacement.is_empty() { replacement.push('\n'); }
|
||||||
replacement.push('\n');
|
|
||||||
}
|
|
||||||
replacement.push_str(text);
|
replacement.push_str(text);
|
||||||
}
|
}
|
||||||
ContentPart::ImageUrl { .. } => {
|
ContentPart::ImageUrl { .. } => {
|
||||||
if !replacement.is_empty() {
|
if !replacement.is_empty() { replacement.push('\n'); }
|
||||||
replacement.push('\n');
|
replacement.push_str("[image aged out]");
|
||||||
}
|
|
||||||
replacement.push_str(
|
|
||||||
"[image aged out — see tool result above for details]",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue