Autocompact + One Shot Summarization algorithm (#3559)

Co-authored-by: Michael Neale <michael.neale@gmail.com>
This commit is contained in:
David Katz
2025-07-31 14:23:06 -04:00
committed by GitHub
parent b5e73c4c89
commit 6f36c1f81e
13 changed files with 1076 additions and 171 deletions
+30 -3
View File
@@ -448,8 +448,8 @@ async fn process_message_streaming(
// Create a user message
let user_message = GooseMessage::user().with_text(content.clone());
// Get existing messages from session and add the new user message
let mut messages = {
// Messages will be auto-compacted in agent.reply() if needed
let messages = {
let mut session_msgs = session_messages.lock().await;
session_msgs.push(user_message.clone());
session_msgs.clone()
@@ -618,7 +618,10 @@ async fn process_message_streaming(
// TODO: Implement proper UI for context handling
let (summarized_messages, _) =
agent.summarize_context(&messages).await?;
messages = summarized_messages;
{
let mut session_msgs = session_messages.lock().await;
*session_msgs = summarized_messages;
}
}
_ => {
// Handle other message types as needed
@@ -626,6 +629,30 @@ async fn process_message_streaming(
}
}
}
Ok(AgentEvent::HistoryReplaced(new_messages)) => {
// Replace the session's message history with the compacted messages
{
let mut session_msgs = session_messages.lock().await;
*session_msgs = new_messages;
}
// Persist the updated messages to the JSONL file
let current_messages = {
let session_msgs = session_messages.lock().await;
session_msgs.clone()
};
if let Err(e) = session::persist_messages(
&session_file,
&current_messages,
None, // No provider needed for persisting
working_dir.clone(),
)
.await
{
error!("Failed to persist compacted messages: {}", e);
}
}
Ok(AgentEvent::McpNotification(_notification)) => {
// Handle MCP notifications if needed
// For now, we'll just log them
+20
View File
@@ -846,6 +846,7 @@ impl Session {
}
async fn process_agent_response(&mut self, interactive: bool) -> Result<()> {
// Messages will be auto-compacted in agent.reply() if needed
let cancel_token = CancellationToken::new();
let cancel_token_clone = cancel_token.clone();
@@ -1140,6 +1141,25 @@ impl Session {
_ => (),
}
}
Some(Ok(AgentEvent::HistoryReplaced(new_messages))) => {
// Replace the session's message history with the compacted messages
self.messages = new_messages;
// Persist the updated messages to the session file
if let Some(session_file) = &self.session_file {
let provider = self.agent.provider().await.ok();
let working_dir = std::env::current_dir().ok();
if let Err(e) = session::persist_messages_with_schedule_id(
session_file,
&self.messages,
provider,
self.scheduled_job_id.clone(),
working_dir,
).await {
eprintln!("Failed to persist compacted messages: {}", e);
}
}
}
Some(Ok(AgentEvent::ModelChange { model, mode })) => {
// Log model change if in debug mode
if self.debug {