Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 11 additions & 22 deletions crates/goose/src/agents/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,40 +963,29 @@ impl Agent {
session: Option<SessionConfig>,
cancel_token: Option<CancellationToken>,
) -> Result<BoxStream<'_, Result<AgentEvent>>> {
// Handle auto-compaction before processing
let (conversation, compaction_msg, _summarization_usage) = match self
let compaction_result = self
.handle_auto_compaction(unfixed_conversation.messages(), &session)
.await?
{
Some((compacted_messages, msg, usage)) => (compacted_messages, Some(msg), usage),
None => {
let context = self
.prepare_reply_context(unfixed_conversation, &session)
.await?;
(context.conversation, None, None)
}
};
.await?;

// If we compacted, yield the compaction message and history replacement event
if let Some(compaction_msg) = compaction_msg {
return Ok(Box::pin(async_stream::try_stream! {
yield AgentEvent::Message(Message::assistant().with_summarization_requested(compaction_msg));
if let Some((conversation, compaction_message, _summarization_usage)) = compaction_result {
Ok(Box::pin(async_stream::try_stream! {
yield AgentEvent::Message(
Message::assistant().with_summarization_requested(compaction_message)
);
yield AgentEvent::HistoryReplaced(conversation.messages().clone());
if let Some(session_to_store) = &session {
SessionManager::replace_conversation(&session_to_store.id, &conversation).await?
}

// Continue with normal reply processing using compacted messages
let mut reply_stream = self.reply_internal(conversation, session, cancel_token).await?;
while let Some(event) = reply_stream.next().await {
yield event?;
}
}));
}))
} else {
self.reply_internal(unfixed_conversation, session, cancel_token)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we flush this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you mean? why is there an .await?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I misclicked my comment. both arms now end on reply_internal, but one arm flushes the stream, the other returns

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the first branch creates a new stream, with two compaction-related items followed by the contents of the reply_internal stream, and the second just returns the reply_internal stream

.await
}

// No compaction needed, proceed with normal processing
self.reply_internal(conversation, session, cancel_token)
.await
}

/// Main reply method that handles the actual agent processing
Expand Down
Loading