Displays the tool calls an agent makes. Each call is a trace row that runs, then opens on its output; a failed call turns destructive and shows its error.
Loading GPUI…
use std::time::Duration;
use gpui::{div, prelude::*, px, relative, Context, Task, Window};
use crate::icons::TERMINAL;
use crate::theme::ActiveTheme;
use crate::ui::code_block::{CodeBlock, CodeBlockSize};
use crate::ui::bubble::{Bubble, BubbleAlign, BubbleVariant};
use crate::ui::button::{Button, ButtonSize, ButtonVariant};
use crate::ui::message::{Message, MessageAlign};
use crate::ui::trace::{Trace, TraceCollapsible, TraceLine, TraceStatus};
/// The mock transport sends one answer word every 20ms; each command takes 900ms.
const CHUNK: Duration = Duration::from_millis(20);
const RUN: Duration = Duration::from_millis(900);
const QUESTION: &str = "Which package managers are installed?";
const ANSWER: &str = "Node 24 and npm 11 are installed. pnpm is not.";
/// Each command and what it printed; `Err` is the error the tool returned.
const COMMANDS: [(&str, Result<&str, &str>); 3] = [
("node --version", Ok("v24.18.0")),
("npm --version", Ok("11.6.2")),
("pnpm --version", Err("command not found: pnpm")),
];
#[derive(Clone, Copy)]
struct Call {
finished: bool,
/// Set once the reader toggles the row; until then it stays closed.
open: Option<bool>,
}
pub struct ToolPattern {
calls: Vec<Call>,
/// Words of the answer shown so far.
words: usize,
busy: bool,
stream: Option<Task<()>>,
}
impl Default for ToolPattern {
/// Starts on the finished transcript. Like shadcn's `defaultOpen`, only the failed call opens,
/// and only here: rows that finish during a replay stay closed.
fn default() -> Self {
let calls = COMMANDS
.iter()
.map(|(_, result)| Call { finished: true, open: result.is_err().then_some(true) })
.collect();
Self { calls, words: ANSWER.split_inclusive(' ').count(), busy: false, stream: None }
}
}
impl ToolPattern {
fn replay(&mut self, cx: &mut Context<Self>) {
self.calls.clear();
self.words = 0;
self.busy = true;
self.stream = Some(cx.spawn(async move |this, cx| {
for ix in 0..COMMANDS.len() {
let started = this.update(cx, |this, cx| {
this.calls.push(Call { finished: false, open: None });
cx.notify();
});
if started.is_err() {
return;
}
cx.background_executor().timer(RUN).await;
let _ = this.update(cx, |this, cx| {
this.calls[ix].finished = true;
cx.notify();
});
}
for _ in 0..ANSWER.split_inclusive(' ').count() {
cx.background_executor().timer(CHUNK).await;
if this.update(cx, |this, cx| { this.words += 1; cx.notify(); }).is_err() {
return;
}
}
let _ = this.update(cx, |this, cx| { this.busy = false; cx.notify(); });
}));
cx.notify();
}
}
impl Render for ToolPattern {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = cx.theme();
let fg = theme.foreground;
let rows = self.calls.iter().enumerate().map(|(ix, call)| {
let (command, result) = COMMANDS[ix];
if !call.finished {
// A running call has nothing to show yet, so it is a plain line with no chevron.
return TraceLine::new(("call", ix), format!("Running {command}"))
.icon(TERMINAL)
.status(TraceStatus::Running)
.into_any_element();
}
let (title, status, output) = match result {
Ok(stdout) => (format!("Ran {command}"), TraceStatus::Done, stdout),
Err(error) => (format!("Failed to run {command}"), TraceStatus::Error, error),
};
let line = TraceLine::new(("call", ix), title).icon(TERMINAL).status(status);
TraceCollapsible::new(("row", ix), call.open.unwrap_or(false), line)
.on_open_change(cx.listener(move |this, open: &bool, _, cx| {
if let Some(call) = this.calls.get_mut(ix) {
call.open = Some(*open);
}
cx.notify();
}))
.child(
// `max-h-64`, as shadcn's demo sets on the content.
div().max_h(px(256.0)).overflow_hidden().child(
CodeBlock::new(("output", ix), output)
.size(CodeBlockSize::Sm)
.wrap(true)
.copy_button(false),
),
)
.into_any_element()
});
let answer: String = ANSWER.split_inclusive(' ').take(self.words).collect();
div()
.w(px(384.0))
.min_h(px(288.0))
.flex()
.flex_col()
.gap_4()
.py_6()
.text_sm()
.child(
Message::new()
.align(MessageAlign::End)
.child(Bubble::new("question").align(BubbleAlign::End).variant(BubbleVariant::Muted).child(QUESTION)),
)
.when(!self.calls.is_empty(), |this| {
this.child(
Message::new().child(
div()
.flex()
.flex_col()
.gap_4()
.w_full()
.child(Trace::new().children(rows))
.when(!answer.is_empty(), |this| {
this.child(div().line_height(relative(1.4286)).text_color(fg).child(answer))
}),
),
)
})
.when(!self.busy, |this| {
this.child(
div().mt_auto().flex().child(
Button::new("replay", "Replay")
.variant(ButtonVariant::Outline)
.size(ButtonSize::Compact)
.rounded_full()
.on_click(cx.listener(|this, _, _, cx| this.replay(cx))),
),
)
})
}
}Installation
Add the components
$ cargo hyuskui add bubble button code-block message trace iconsUses Bubble, Button, Code Block, Message and Trace.
Copy the pattern
Paste the code into
src/ai/tool.rsand declare the module. The demo plays scripted data on a timer; drive it from your agent's events instead.src/ai/tool.rs use std::time::Duration; use gpui::{div, prelude::*, px, relative, Context, Task, Window}; use crate::icons::TERMINAL; use crate::theme::ActiveTheme; use crate::ui::code_block::{CodeBlock, CodeBlockSize}; use crate::ui::bubble::{Bubble, BubbleAlign, BubbleVariant}; use crate::ui::button::{Button, ButtonSize, ButtonVariant}; use crate::ui::message::{Message, MessageAlign}; use crate::ui::trace::{Trace, TraceCollapsible, TraceLine, TraceStatus}; /// The mock transport sends one answer word every 20ms; each command takes 900ms. const CHUNK: Duration = Duration::from_millis(20); const RUN: Duration = Duration::from_millis(900); const QUESTION: &str = "Which package managers are installed?"; const ANSWER: &str = "Node 24 and npm 11 are installed. pnpm is not."; /// Each command and what it printed; `Err` is the error the tool returned. const COMMANDS: [(&str, Result<&str, &str>); 3] = [ ("node --version", Ok("v24.18.0")), ("npm --version", Ok("11.6.2")), ("pnpm --version", Err("command not found: pnpm")), ]; #[derive(Clone, Copy)] struct Call { finished: bool, /// Set once the reader toggles the row; until then it stays closed. open: Option<bool>, } pub struct ToolPattern { calls: Vec<Call>, /// Words of the answer shown so far. words: usize, busy: bool, stream: Option<Task<()>>, } impl Default for ToolPattern { /// Starts on the finished transcript. Like shadcn's `defaultOpen`, only the failed call opens, /// and only here: rows that finish during a replay stay closed. fn default() -> Self { let calls = COMMANDS .iter() .map(|(_, result)| Call { finished: true, open: result.is_err().then_some(true) }) .collect(); Self { calls, words: ANSWER.split_inclusive(' ').count(), busy: false, stream: None } } } impl ToolPattern { fn replay(&mut self, cx: &mut Context<Self>) { self.calls.clear(); self.words = 0; self.busy = true; self.stream = Some(cx.spawn(async move |this, cx| { for ix in 0..COMMANDS.len() { let started = this.update(cx, |this, cx| { this.calls.push(Call { finished: false, open: None }); cx.notify(); }); if started.is_err() { return; } cx.background_executor().timer(RUN).await; let _ = this.update(cx, |this, cx| { this.calls[ix].finished = true; cx.notify(); }); } for _ in 0..ANSWER.split_inclusive(' ').count() { cx.background_executor().timer(CHUNK).await; if this.update(cx, |this, cx| { this.words += 1; cx.notify(); }).is_err() { return; } } let _ = this.update(cx, |this, cx| { this.busy = false; cx.notify(); }); })); cx.notify(); } } impl Render for ToolPattern { fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { let theme = cx.theme(); let fg = theme.foreground; let rows = self.calls.iter().enumerate().map(|(ix, call)| { let (command, result) = COMMANDS[ix]; if !call.finished { // A running call has nothing to show yet, so it is a plain line with no chevron. return TraceLine::new(("call", ix), format!("Running {command}")) .icon(TERMINAL) .status(TraceStatus::Running) .into_any_element(); } let (title, status, output) = match result { Ok(stdout) => (format!("Ran {command}"), TraceStatus::Done, stdout), Err(error) => (format!("Failed to run {command}"), TraceStatus::Error, error), }; let line = TraceLine::new(("call", ix), title).icon(TERMINAL).status(status); TraceCollapsible::new(("row", ix), call.open.unwrap_or(false), line) .on_open_change(cx.listener(move |this, open: &bool, _, cx| { if let Some(call) = this.calls.get_mut(ix) { call.open = Some(*open); } cx.notify(); })) .child( // `max-h-64`, as shadcn's demo sets on the content. div().max_h(px(256.0)).overflow_hidden().child( CodeBlock::new(("output", ix), output) .size(CodeBlockSize::Sm) .wrap(true) .copy_button(false), ), ) .into_any_element() }); let answer: String = ANSWER.split_inclusive(' ').take(self.words).collect(); div() .w(px(384.0)) .min_h(px(288.0)) .flex() .flex_col() .gap_4() .py_6() .text_sm() .child( Message::new() .align(MessageAlign::End) .child(Bubble::new("question").align(BubbleAlign::End).variant(BubbleVariant::Muted).child(QUESTION)), ) .when(!self.calls.is_empty(), |this| { this.child( Message::new().child( div() .flex() .flex_col() .gap_4() .w_full() .child(Trace::new().children(rows)) .when(!answer.is_empty(), |this| { this.child(div().line_height(relative(1.4286)).text_color(fg).child(answer)) }), ), ) }) .when(!self.busy, |this| { this.child( div().mt_auto().flex().child( Button::new("replay", "Replay") .variant(ButtonVariant::Outline) .size(ButtonSize::Compact) .rounded_full() .on_click(cx.listener(|this, _, _, cx| this.replay(cx))), ), ) }) } }