feat: Enhance token usage tracking and improve chat UI with emojis
This commit is contained in:
@@ -1125,9 +1125,22 @@ fn run_agent_turn(
|
||||
Remember: Cycle 0 MUST be investigation-only (access: read). Cycle 1 MUST be planning-only (access: read). Only subsequent cycles can perform modifications (access: write/full)."
|
||||
));
|
||||
|
||||
let planner_prompt_chars = system_msg.content.as_deref().map_or(0, str::len)
|
||||
+ user_msg.content.as_deref().map_or(0, str::len);
|
||||
let planner_result = tc.client.chat_with_tools_non_streaming(&[system_msg, user_msg], None);
|
||||
let pipeline_result = match planner_result {
|
||||
Ok((reply, _)) => {
|
||||
Ok((reply, usage_opt)) => {
|
||||
let (mut tok_in, mut tok_out) = usage_opt.unwrap_or((0, 0));
|
||||
if tok_in == 0 {
|
||||
tok_in = (planner_prompt_chars / 4).max(1) as u64;
|
||||
}
|
||||
if tok_out == 0 {
|
||||
let response_chars = reply.content.as_deref().map_or(0, str::len);
|
||||
tok_out = (response_chars / 4).max(1) as u64;
|
||||
}
|
||||
if let Ok(mut q) = events_q.lock() {
|
||||
q.push_back(TurnEvent::Usage { tokens_in: tok_in, tokens_out: tok_out });
|
||||
}
|
||||
let reply_text = reply.content.as_deref().unwrap_or("").trim();
|
||||
let clean_json = if reply_text.starts_with("```") {
|
||||
let mut lines = reply_text.lines();
|
||||
@@ -1350,7 +1363,18 @@ fn run_agent_turn(
|
||||
}
|
||||
};
|
||||
|
||||
let (tok_in, tok_out) = final_usage.unwrap_or((0, 0));
|
||||
let (mut tok_in, mut tok_out) = final_usage.unwrap_or((0, 0));
|
||||
if tok_in == 0 {
|
||||
let total_chars: usize = wire_msgs.iter()
|
||||
.filter_map(|m| m.content.as_deref())
|
||||
.map(str::len)
|
||||
.sum();
|
||||
tok_in = (total_chars / 4).max(1) as u64;
|
||||
}
|
||||
if tok_out == 0 {
|
||||
let response_chars = response.content.as_deref().map_or(0, str::len);
|
||||
tok_out = (response_chars / 4).max(1) as u64;
|
||||
}
|
||||
if let Ok(mut q) = events_q.lock() {
|
||||
q.push_back(TurnEvent::Usage { tokens_in: tok_in, tokens_out: tok_out });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user