Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions crates/chat-cli/src/cli/chat/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ pub const MAX_CURRENT_WORKING_DIRECTORY_LEN: usize = 256;
/// Limit to send the number of messages as part of chat.
pub const MAX_CONVERSATION_STATE_HISTORY_LEN: usize = 250;

pub const MAX_TOOL_RESPONSE_SIZE: usize = 800_000;
/// Actual service limit is 800_000
pub const MAX_TOOL_RESPONSE_SIZE: usize = 600_000;

/// TODO: Use this to gracefully handle user message sizes.
#[allow(dead_code)]
/// Actual service limit is 600_000
pub const MAX_USER_MESSAGE_SIZE: usize = 600_000;

/// In tokens
Expand Down
16 changes: 14 additions & 2 deletions crates/chat-cli/src/cli/chat/conversation_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use super::consts::{
DUMMY_TOOL_NAME,
MAX_CHARS,
MAX_CONVERSATION_STATE_HISTORY_LEN,
MAX_USER_MESSAGE_SIZE,
};
use super::context::ContextManager;
use super::hooks::{
Expand All @@ -50,7 +51,10 @@ use super::tools::{
ToolOrigin,
ToolSpec,
};
use super::util::serde_value_to_document;
use super::util::{
serde_value_to_document,
truncate_safe,
};
use crate::api_client::model::{
AssistantResponseMessage,
ChatMessage,
Expand Down Expand Up @@ -527,6 +531,13 @@ impl ConversationState {
}
}

/// Whether or not it is possible to create a summary out of this conversation state.
///
/// Currently only checks if we have enough messages in the history to create a summary out of.
pub async fn can_create_summary_request(&mut self) -> bool {
self.backend_conversation_state(false, true).await.history.len() >= 2
}

/// Returns a [FigConversationState] capable of replacing the history of the current
/// conversation with a summary generated by the model.
pub async fn create_summary_request(&mut self, custom_prompt: Option<impl AsRef<str>>) -> FigConversationState {
Expand Down Expand Up @@ -633,7 +644,8 @@ impl ConversationState {
// something is set.
tool_content.push_str("<tool result redacted>");
}
user.content = UserMessageContent::Prompt { prompt: tool_content };
let prompt = truncate_safe(&tool_content, MAX_USER_MESSAGE_SIZE).to_string();
user.content = UserMessageContent::Prompt { prompt };
}
}
}
Expand Down
42 changes: 27 additions & 15 deletions crates/chat-cli/src/cli/chat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,26 +955,36 @@ impl ChatContext {
// Errors from attempting to send too large of a conversation history. In
// this case, attempt to automatically compact the history for the user.
crate::api_client::ApiClientError::ContextWindowOverflow => {
let history_too_small = self
.conversation_state
.backend_conversation_state(false, true)
.await
.history
.len()
< 2;
if history_too_small {
print_err!(
"Your conversation is too large - try reducing the size of
the context being passed",
err
);
if !self.conversation_state.can_create_summary_request().await {
execute!(
self.output,
style::SetForegroundColor(Color::Red),
style::Print("Your conversation is too large to continue.\n"),
style::SetForegroundColor(Color::Reset),
style::Print(format!("• Run {} to analyze your context usage\n", "/usage".green())),
style::Print(format!(
"• Run {} to reset your conversation state\n",
"/clear".green()
)),
style::SetAttribute(Attribute::Reset),
style::Print("\n\n"),
)?;
self.conversation_state.reset_next_user_message();
return Ok(ChatState::PromptUser {
tool_uses: None,
pending_tool_index: None,
skip_printing_tools: false,
});
}

execute!(
self.output,
style::SetForegroundColor(Color::Yellow),
style::Print("The context window has overflowed, summarizing the history..."),
style::SetAttribute(Attribute::Reset),
style::Print("\n\n"),
)?;

return Ok(ChatState::CompactHistory {
tool_uses: None,
pending_tool_index: None,
Expand Down Expand Up @@ -1220,8 +1230,10 @@ impl ChatContext {
// Check token usage and display warnings if needed
if pending_tool_index.is_none() {
// Only display warnings when not waiting for tool approval
if let Err(e) = self.display_char_warnings().await {
warn!("Failed to display character limit warnings: {}", e);
if self.conversation_state.can_create_summary_request().await {
if let Err(e) = self.display_char_warnings().await {
warn!("Failed to display character limit warnings: {}", e);
}
}
}

Expand Down