Skip to content

Commit 6649318

Browse files
committed
Show tool duration alongside token counts
1 parent 1a544fc commit 6649318

3 files changed

Lines changed: 116 additions & 2 deletions

File tree

‎crates/jcode-tui/src/tui/ui_messages.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4109,7 +4109,9 @@ pub(crate) fn render_tool_message(
41094109
}
41104110

41114111
let centered = markdown::center_code_blocks();
4112-
let token_badge = tool_output_token_badge(&msg.content);
4112+
let mut token_badge = tool_output_token_badge(&msg.content);
4113+
token_badge.label =
4114+
tools_ui::tool_usage_label(&token_badge.label, msg.duration_secs, &msg.content);
41134115

41144116
// A restored or remotely mirrored transcript can occasionally retain the
41154117
// todo result while losing its paired ToolCall metadata. Recognize the

‎crates/jcode-tui/src/tui/ui_messages/tests.rs‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3070,6 +3070,96 @@ fn discovery_cards_wrap_within_narrow_transcript_width() {
30703070
);
30713071
}
30723072

3073+
#[test]
3074+
fn render_tool_message_groups_duration_and_tokens() {
3075+
for name in [
3076+
"bash",
3077+
"read",
3078+
"edit",
3079+
"webfetch",
3080+
"debug_socket",
3081+
"unknown_tool",
3082+
] {
3083+
for content in ["ok", "Error: failed"] {
3084+
let mut msg = DisplayMessage::tool(
3085+
content,
3086+
crate::message::ToolCall {
3087+
id: "timed".into(),
3088+
name: name.into(),
3089+
input: serde_json::json!({}),
3090+
intent: Some("A completed tool call".into()),
3091+
thought_signature: None,
3092+
},
3093+
);
3094+
msg.duration_secs = Some(2.4);
3095+
let tokens =
3096+
crate::util::format_approx_token_count(crate::util::estimate_tokens(content));
3097+
for width in [32, 60, 120] {
3098+
let lines = render_tool_message(&msg, width, crate::config::DiffDisplayMode::Off);
3099+
let row = extract_line_text(&lines[0]);
3100+
assert!(row.contains(&format!("2.4s, {tokens}")), "{name}: {row}");
3101+
assert!(!row.contains("2.4s ·"), "{row}");
3102+
assert!(lines[0].width() < width as usize, "{row}");
3103+
}
3104+
}
3105+
}
3106+
}
3107+
3108+
#[test]
3109+
fn batch_subcall_groups_duration_and_tokens() {
3110+
let tool = crate::message::ToolCall {
3111+
id: "timed-child".into(),
3112+
name: "read".into(),
3113+
input: serde_json::json!({}),
3114+
intent: Some("Read configuration".into()),
3115+
thought_signature: None,
3116+
};
3117+
let line = crate::tui::ui::tools_ui::render_batch_subcall_line(
3118+
&tool,
3119+
"✓",
3120+
rgb(100, 180, 100),
3121+
50,
3122+
Some(80),
3123+
Some("[tool timing: duration=73ms] config content"),
3124+
);
3125+
let text = extract_line_text(&line);
3126+
assert!(text.contains(" · 73ms, "), "{text}");
3127+
assert!(text.ends_with(" tok"), "{text}");
3128+
assert!(line.width() <= 80);
3129+
}
3130+
3131+
#[test]
3132+
fn tool_usage_label_handles_missing_and_legacy_duration() {
3133+
use crate::tui::ui::tools_ui::tool_usage_label;
3134+
assert_eq!(tool_usage_label("217 tok", None, "ok"), "217 tok");
3135+
assert_eq!(tool_usage_label("217 tok", Some(0.0), "ok"), "0ms, 217 tok");
3136+
assert_eq!(tool_usage_label("217 tok", Some(f32::NAN), "ok"), "217 tok");
3137+
assert_eq!(
3138+
tool_usage_label(
3139+
"217 tok",
3140+
None,
3141+
"[tool timing: start=x finish=y duration=2.4s] ok"
3142+
),
3143+
"2.4s, 217 tok"
3144+
);
3145+
assert_eq!(
3146+
tool_usage_label("217 tok", None, "[tool timing: duration=1m 4s] ok"),
3147+
"1m 4s, 217 tok"
3148+
);
3149+
assert_eq!(
3150+
tool_usage_label("217 tok", None, "[tool timing: duration=73ms] ok"),
3151+
"73ms, 217 tok"
3152+
);
3153+
assert_eq!(
3154+
tool_usage_label("217 tok", None, "[tool timing: duration=NaNs] ok"),
3155+
"217 tok"
3156+
);
3157+
assert_eq!(
3158+
tool_usage_label("217 tok", None, "[tool timing: duration=invalid] ok"),
3159+
"217 tok"
3160+
);
3161+
}
3162+
30733163
#[test]
30743164
fn render_tool_message_colors_high_token_badge() {
30753165
let msg = DisplayMessage {

‎crates/jcode-tui/src/tui/ui_tools.rs‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,21 @@ pub(super) fn get_tool_summary_with_budget(
15721572
}
15731573
}
15741574

1575+
/// Keep timing and token usage together as one compact metadata group.
1576+
/// Older transcript entries can lack timing; never invent a duration for them.
1577+
pub(super) fn tool_usage_label(tokens: &str, seconds: Option<f32>, output: &str) -> String {
1578+
let duration = seconds
1579+
.filter(|seconds| seconds.is_finite() && *seconds >= 0.0)
1580+
.or_else(|| jcode_tui_messages::DisplayMessage::tool_duration_from_output(output))
1581+
.map(|seconds| {
1582+
crate::message::Message::format_duration((seconds as f64 * 1000.0).round() as u64)
1583+
});
1584+
match duration {
1585+
Some(duration) => format!("{duration}, {tokens}"),
1586+
None => tokens.to_owned(),
1587+
}
1588+
}
1589+
15751590
pub(super) fn render_batch_subcall_line(
15761591
tool: &ToolCall,
15771592
icon: &str,
@@ -1588,7 +1603,14 @@ pub(super) fn render_batch_subcall_line(
15881603
crate::util::ApproxTokenSeverity::Warning => rgb(214, 184, 92),
15891604
crate::util::ApproxTokenSeverity::Danger => rgb(224, 118, 118),
15901605
};
1591-
(crate::util::format_approx_token_count(tokens), color)
1606+
(
1607+
tool_usage_label(
1608+
&crate::util::format_approx_token_count(tokens),
1609+
None,
1610+
content,
1611+
),
1612+
color,
1613+
)
15921614
});
15931615
let intent = tool
15941616
.intent

0 commit comments

Comments
 (0)