Skip to content

Commit 983e071

Browse files
fix: /compact causing validation errors (#1834)
* fix: /compact causing validation errors * adds new line after mcp loading status (#1835) --------- Co-authored-by: Felix Ding <dingfeli@amazon.com>
1 parent 487bc2b commit 983e071

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

crates/chat-cli/src/cli/chat/consts.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ pub const MAX_CURRENT_WORKING_DIRECTORY_LEN: usize = 256;
77
/// Limit to send the number of messages as part of chat.
88
pub const MAX_CONVERSATION_STATE_HISTORY_LEN: usize = 250;
99

10-
pub const MAX_TOOL_RESPONSE_SIZE: usize = 800_000;
10+
/// Actual service limit is 800_000
11+
pub const MAX_TOOL_RESPONSE_SIZE: usize = 600_000;
1112

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

1616
/// In tokens

crates/chat-cli/src/cli/chat/conversation_state.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use super::consts::{
2525
DUMMY_TOOL_NAME,
2626
MAX_CHARS,
2727
MAX_CONVERSATION_STATE_HISTORY_LEN,
28+
MAX_USER_MESSAGE_SIZE,
2829
};
2930
use super::context::ContextManager;
3031
use super::hooks::{
@@ -50,7 +51,10 @@ use super::tools::{
5051
ToolOrigin,
5152
ToolSpec,
5253
};
53-
use super::util::serde_value_to_document;
54+
use super::util::{
55+
serde_value_to_document,
56+
truncate_safe,
57+
};
5458
use crate::api_client::model::{
5559
AssistantResponseMessage,
5660
ChatMessage,
@@ -527,6 +531,13 @@ impl ConversationState {
527531
}
528532
}
529533

534+
/// Whether or not it is possible to create a summary out of this conversation state.
535+
///
536+
/// Currently only checks if we have enough messages in the history to create a summary out of.
537+
pub async fn can_create_summary_request(&mut self) -> bool {
538+
self.backend_conversation_state(false, true).await.history.len() >= 2
539+
}
540+
530541
/// Returns a [FigConversationState] capable of replacing the history of the current
531542
/// conversation with a summary generated by the model.
532543
pub async fn create_summary_request(&mut self, custom_prompt: Option<impl AsRef<str>>) -> FigConversationState {
@@ -633,7 +644,8 @@ impl ConversationState {
633644
// something is set.
634645
tool_content.push_str("<tool result redacted>");
635646
}
636-
user.content = UserMessageContent::Prompt { prompt: tool_content };
647+
let prompt = truncate_safe(&tool_content, MAX_USER_MESSAGE_SIZE).to_string();
648+
user.content = UserMessageContent::Prompt { prompt };
637649
}
638650
}
639651
}

crates/chat-cli/src/cli/chat/mod.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -955,26 +955,36 @@ impl ChatContext {
955955
// Errors from attempting to send too large of a conversation history. In
956956
// this case, attempt to automatically compact the history for the user.
957957
crate::api_client::ApiClientError::ContextWindowOverflow => {
958-
let history_too_small = self
959-
.conversation_state
960-
.backend_conversation_state(false, true)
961-
.await
962-
.history
963-
.len()
964-
< 2;
965-
if history_too_small {
966-
print_err!(
967-
"Your conversation is too large - try reducing the size of
968-
the context being passed",
969-
err
970-
);
958+
if !self.conversation_state.can_create_summary_request().await {
959+
execute!(
960+
self.output,
961+
style::SetForegroundColor(Color::Red),
962+
style::Print("Your conversation is too large to continue.\n"),
963+
style::SetForegroundColor(Color::Reset),
964+
style::Print(format!("• Run {} to analyze your context usage\n", "/usage".green())),
965+
style::Print(format!(
966+
"• Run {} to reset your conversation state\n",
967+
"/clear".green()
968+
)),
969+
style::SetAttribute(Attribute::Reset),
970+
style::Print("\n\n"),
971+
)?;
972+
self.conversation_state.reset_next_user_message();
971973
return Ok(ChatState::PromptUser {
972974
tool_uses: None,
973975
pending_tool_index: None,
974976
skip_printing_tools: false,
975977
});
976978
}
977979

980+
execute!(
981+
self.output,
982+
style::SetForegroundColor(Color::Yellow),
983+
style::Print("The context window has overflowed, summarizing the history..."),
984+
style::SetAttribute(Attribute::Reset),
985+
style::Print("\n\n"),
986+
)?;
987+
978988
return Ok(ChatState::CompactHistory {
979989
tool_uses: None,
980990
pending_tool_index: None,
@@ -1220,8 +1230,10 @@ impl ChatContext {
12201230
// Check token usage and display warnings if needed
12211231
if pending_tool_index.is_none() {
12221232
// Only display warnings when not waiting for tool approval
1223-
if let Err(e) = self.display_char_warnings().await {
1224-
warn!("Failed to display character limit warnings: {}", e);
1233+
if self.conversation_state.can_create_summary_request().await {
1234+
if let Err(e) = self.display_char_warnings().await {
1235+
warn!("Failed to display character limit warnings: {}", e);
1236+
}
12251237
}
12261238
}
12271239

crates/chat-cli/src/cli/chat/tool_manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ impl ToolManagerBuilder {
400400
let msg = eyre::eyre!(msg);
401401
let total = loading_servers.len();
402402
queue_incomplete_load_message(complete, total, &msg, &mut output)?;
403-
output.flush()?;
404403
}
404+
execute!(output, style::Print("\n"),)?;
405405
break;
406406
},
407407
},

0 commit comments

Comments
 (0)