Skip to content

Commit ae15bf2

Browse files
authored
fix: /compact validation error: missing tool specs (#224)
* add mcp token in usage * fix validation e & use cs.tools calculate token * ci * revise token cal * change the ratio * change the ratio * ut
1 parent dc99852 commit ae15bf2

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,9 +600,20 @@ impl ConversationState {
600600
flatten_history(conv_state.history.take(history_len.saturating_sub(1)))
601601
};
602602

603+
let user_input_message_context = UserInputMessageContext {
604+
env_state: Some(build_env_state()),
605+
git_state: None,
606+
tool_results: None,
607+
tools: if self.tools.is_empty() {
608+
None
609+
} else {
610+
Some(self.tools.values().flatten().cloned().collect::<Vec<Tool>>())
611+
},
612+
};
613+
603614
let mut summary_message = UserInputMessage {
604615
content: summary_content,
605-
user_input_message_context: None,
616+
user_input_message_context: Some(user_input_message_context),
606617
user_intent: None,
607618
images: None,
608619
model_id: self.model.clone(),

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ use crate::api_client::{
161161
};
162162
use crate::auth::AuthError;
163163
use crate::auth::builder_id::is_idc_user;
164+
use crate::cli::chat::token_counter::CharCount;
164165
use crate::database::Database;
165166
use crate::database::settings::Setting;
166167
use crate::mcp_client::{
@@ -2988,13 +2989,19 @@ impl ChatContext {
29882989
}
29892990

29902991
let data = state.calculate_conversation_size();
2992+
let tool_specs_json: String = state
2993+
.tools
2994+
.values()
2995+
.filter_map(|s| serde_json::to_string(s).ok())
2996+
.collect();
29912997

29922998
let context_token_count: TokenCount = data.context_messages.into();
29932999
let assistant_token_count: TokenCount = data.assistant_messages.into();
29943000
let user_token_count: TokenCount = data.user_messages.into();
3001+
let tools_char_count: CharCount = tool_specs_json.len().into(); // usize → CharCount
3002+
let tools_token_count: TokenCount = tools_char_count.into(); // CharCount → TokenCount
29953003
let total_token_used: TokenCount =
2996-
(data.context_messages + data.user_messages + data.assistant_messages).into();
2997-
3004+
(data.context_messages + data.user_messages + data.assistant_messages + tools_char_count).into();
29983005
let window_width = self.terminal_width();
29993006
// set a max width for the progress bar for better aesthetic
30003007
let progress_bar_width = std::cmp::min(window_width, 80);
@@ -3003,13 +3010,18 @@ impl ChatContext {
30033010
* progress_bar_width as f64) as usize;
30043011
let assistant_width = ((assistant_token_count.value() as f64 / CONTEXT_WINDOW_SIZE as f64)
30053012
* progress_bar_width as f64) as usize;
3013+
let tools_width = ((tools_token_count.value() as f64 / CONTEXT_WINDOW_SIZE as f64)
3014+
* progress_bar_width as f64) as usize;
30063015
let user_width = ((user_token_count.value() as f64 / CONTEXT_WINDOW_SIZE as f64)
30073016
* progress_bar_width as f64) as usize;
30083017

30093018
let left_over_width = progress_bar_width
3010-
- std::cmp::min(context_width + assistant_width + user_width, progress_bar_width);
3019+
- std::cmp::min(
3020+
context_width + assistant_width + user_width + tools_width,
3021+
progress_bar_width,
3022+
);
30113023

3012-
let is_overflow = (context_width + assistant_width + user_width) > progress_bar_width;
3024+
let is_overflow = (context_width + assistant_width + user_width + tools_width) > progress_bar_width;
30133025

30143026
if is_overflow {
30153027
queue!(
@@ -3036,6 +3048,7 @@ impl ChatContext {
30363048
total_token_used,
30373049
CONTEXT_WINDOW_SIZE / 1000
30383050
)),
3051+
// Context files
30393052
style::SetForegroundColor(Color::DarkCyan),
30403053
// add a nice visual to mimic "tiny" progress, so the overral progress bar doesn't look too
30413054
// empty
@@ -3045,13 +3058,23 @@ impl ChatContext {
30453058
0
30463059
})),
30473060
style::Print("█".repeat(context_width)),
3061+
// Tools
3062+
style::SetForegroundColor(Color::DarkRed),
3063+
style::Print("|".repeat(if tools_width == 0 && *tools_token_count > 0 {
3064+
1
3065+
} else {
3066+
0
3067+
})),
3068+
style::Print("█".repeat(tools_width)),
3069+
// Assistant responses
30483070
style::SetForegroundColor(Color::Blue),
30493071
style::Print("|".repeat(if assistant_width == 0 && *assistant_token_count > 0 {
30503072
1
30513073
} else {
30523074
0
30533075
})),
30543076
style::Print("█".repeat(assistant_width)),
3077+
// User prompts
30553078
style::SetForegroundColor(Color::Magenta),
30563079
style::Print("|".repeat(if user_width == 0 && *user_token_count > 0 { 1 } else { 0 })),
30573080
style::Print("█".repeat(user_width)),
@@ -3079,6 +3102,14 @@ impl ChatContext {
30793102
context_token_count,
30803103
(context_token_count.value() as f32 / CONTEXT_WINDOW_SIZE as f32) * 100.0
30813104
)),
3105+
style::SetForegroundColor(Color::DarkRed),
3106+
style::Print("█ Tools: "),
3107+
style::SetForegroundColor(Color::Reset),
3108+
style::Print(format!(
3109+
" ~{} tokens ({:.2}%)\n",
3110+
tools_token_count,
3111+
(tools_token_count.value() as f32 / CONTEXT_WINDOW_SIZE as f32) * 100.0
3112+
)),
30823113
style::SetForegroundColor(Color::Blue),
30833114
style::Print("█ Q responses: "),
30843115
style::SetForegroundColor(Color::Reset),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl std::fmt::Display for TokenCount {
7575
pub struct TokenCounter;
7676

7777
impl TokenCounter {
78-
pub const TOKEN_TO_CHAR_RATIO: usize = 3;
78+
pub const TOKEN_TO_CHAR_RATIO: usize = 4;
7979

8080
/// Estimates the number of tokens in the input content.
8181
/// Currently uses a simple heuristic: content length / TOKEN_TO_CHAR_RATIO

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ mod tests {
189189
"Yet another test file that's has the largest context file".to_string(),
190190
),
191191
];
192-
let limit = 10;
192+
let limit = 9;
193193

194194
let dropped_files = drop_matched_context_files(&mut files, limit).unwrap();
195195
assert_eq!(dropped_files.len(), 1);

0 commit comments

Comments
 (0)