agent2: Token count (#36496)

Release Notes:

- N/A

---------

Co-authored-by: Agus Zubiaga <agus@zed.dev>
This commit is contained in:
Bennet Bo Fenner 2025-08-19 22:40:31 +02:00 committed by GitHub
parent 6825715503
commit 5fb68cb8be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 321 additions and 26 deletions

View file

@ -816,7 +816,7 @@ impl AcpThreadView {
self.thread_retry_status.take();
self.thread_state = ThreadState::ServerExited { status: *status };
}
AcpThreadEvent::TitleUpdated => {}
AcpThreadEvent::TitleUpdated | AcpThreadEvent::TokenUsageUpdated => {}
}
cx.notify();
}
@ -2794,6 +2794,7 @@ impl AcpThreadView {
.child(
h_flex()
.gap_1()
.children(self.render_token_usage(cx))
.children(self.profile_selector.clone())
.children(self.model_selector.clone())
.child(self.render_send_button(cx)),
@ -2816,6 +2817,44 @@ impl AcpThreadView {
.thread(acp_thread.session_id(), cx)
}
fn render_token_usage(&self, cx: &mut Context<Self>) -> Option<Div> {
let thread = self.thread()?.read(cx);
let usage = thread.token_usage()?;
let is_generating = thread.status() != ThreadStatus::Idle;
let used = crate::text_thread_editor::humanize_token_count(usage.used_tokens);
let max = crate::text_thread_editor::humanize_token_count(usage.max_tokens);
Some(
h_flex()
.flex_shrink_0()
.gap_0p5()
.mr_1()
.child(
Label::new(used)
.size(LabelSize::Small)
.color(Color::Muted)
.map(|label| {
if is_generating {
label
.with_animation(
"used-tokens-label",
Animation::new(Duration::from_secs(2))
.repeat()
.with_easing(pulsating_between(0.6, 1.)),
|label, delta| label.alpha(delta),
)
.into_any()
} else {
label.into_any_element()
}
}),
)
.child(Label::new("/").size(LabelSize::Small).color(Color::Muted))
.child(Label::new(max).size(LabelSize::Small).color(Color::Muted)),
)
}
fn toggle_burn_mode(
&mut self,
_: &ToggleBurnMode,

View file

@ -1526,6 +1526,7 @@ impl AgentDiff {
self.update_reviewing_editors(workspace, window, cx);
}
AcpThreadEvent::TitleUpdated
| AcpThreadEvent::TokenUsageUpdated
| AcpThreadEvent::EntriesRemoved(_)
| AcpThreadEvent::ToolAuthorizationRequired
| AcpThreadEvent::Retry(_) => {}