BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / agents / run_chip.rs

run_chip.rs

run_chip.rs 8.8 KiB Download Raw
1use gpui_kit::assets::IconName;
2use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex};
3use gpui_kit::prelude::FluentBuilder as _;
4use gpui_kit::*;
5
6use super::runs::{AgentRun, AgentRuns, RunState, format_elapsed};
7use super::spinner::spinner;
8use super::{AgentRunsHandle, open_session, retry_run};
9use crate::theme;
10use crate::views::widgets::icon;
11
12const CHIP_HEIGHT: f32 = 28.;
13const CHIP_MAX_WIDTH: f32 = 310.;
14const BUTTON_HEIGHT: f32 = 22.;
15const STATUS_ICON: f32 = 12.;
16
17fn with_runs(cx: &mut App, change: impl FnOnce(&mut AgentRuns, &mut Context<AgentRuns>)) {
18 cx.global::<AgentRunsHandle>().0.clone().update(cx, change);
19}
20
21fn chip_button(id: String, label: &'static str, primary: bool) -> Stateful<Div> {
22 div()
23 .id(ElementId::Name(id.into()))
24 .h(px(BUTTON_HEIGHT))
25 .px(px(8.))
26 .flex()
27 .flex_none()
28 .items_center()
29 .rounded(px(6.))
30 .cursor_pointer()
31 .text_color(theme::accent_text())
32 .font_weight(FontWeight::SEMIBOLD)
33 .when(primary, |button| button.bg(theme::surface_raised()))
34 .hover(move |button| {
35 button.bg(if primary {
36 theme::popup_hover()
37 } else {
38 theme::surface_raised()
39 })
40 })
41 .child(label)
42}
43
44fn dismiss_button(run_id: u64) -> Stateful<Div> {
45 div()
46 .id(("agent-dismiss", run_id as usize))
47 .size(px(BUTTON_HEIGHT))
48 .flex()
49 .flex_none()
50 .items_center()
51 .justify_center()
52 .rounded(px(6.))
53 .cursor_pointer()
54 .hover(|button| button.bg(theme::surface_raised()))
55 .child(icon(IconName::X, STATUS_ICON, theme::text_faint()))
56 .on_click(move |_, _, cx| {
57 cx.stop_propagation();
58 with_runs(cx, |runs, cx| runs.dismiss(run_id, cx));
59 })
60}
61
62fn label(text: String, color: Hsla) -> Div {
63 div().flex_none().text_color(color).child(text)
64}
65
66fn step_text(text: String) -> Div {
67 div()
68 .min_w(px(0.))
69 .truncate()
70 .text_color(theme::text_muted())
71 .child(text)
72}
73
74fn elapsed_text(run: &AgentRun) -> Div {
75 div()
76 .flex_none()
77 .text_color(theme::text_faint())
78 .child(run.elapsed_text())
79}
80
81fn chip_row(run: &AgentRun, fill: Hsla) -> Stateful<Div> {
82 let run_id = run.id;
83 let step = run.current_step.clone();
84 let session_id = run.session_id.clone();
85 let row = h_flex()
86 .id(("agent-chip", run_id as usize))
87 .h(px(CHIP_HEIGHT))
88 .max_w(px(CHIP_MAX_WIDTH))
89 .pl(px(10.))
90 .pr(px(4.))
91 .gap(px(8.))
92 .flex_none()
93 .items_center()
94 .rounded(px(8.))
95 .bg(fill)
96 .cursor_pointer()
97 .text_size(px(12.))
98 .text_color(theme::text_soft())
99 .tooltip(move |window, cx| Tooltip::new(step.clone()).build(window, cx))
100 .on_click(move |_, _, cx| with_runs(cx, |runs, cx| runs.toggle_expanded(run_id, cx)));
101 let action_label = run.action_label.clone();
102 match &run.state {
103 RunState::Running => row
104 .child(spinner(STATUS_ICON))
105 .child(label(action_label, theme::text_soft()))
106 .child(step_text(run.current_step.clone()))
107 .child(elapsed_text(run))
108 .child(
109 div()
110 .size(px(BUTTON_HEIGHT))
111 .flex()
112 .flex_none()
113 .items_center()
114 .justify_center()
115 .child(icon(
116 if run.expanded {
117 IconName::ChevronUp
118 } else {
119 IconName::ChevronDown
120 },
121 STATUS_ICON,
122 theme::text_faint(),
123 )),
124 ),
125 RunState::Done => row
126 .child(icon(IconName::Check, STATUS_ICON, theme::accent_text()))
127 .child(label(action_label, theme::text_soft()))
128 .child(step_text(run.current_step.clone()))
129 .child(elapsed_text(run))
130 .when(!session_id.is_empty(), |row| {
131 row.child(
132 chip_button(format!("agent-open-{run_id}"), "Open session", true).on_click(
133 move |_, _, cx| {
134 cx.stop_propagation();
135 open_session(&session_id, cx);
136 },
137 ),
138 )
139 })
140 .child(dismiss_button(run_id)),
141 RunState::Failed { .. } => {
142 row.child(icon(IconName::X, STATUS_ICON, theme::red_soft()))
143 .child(label("Failed".to_owned(), theme::red_tint()))
144 .child(step_text(run.current_step.clone()))
145 .child(
146 chip_button(format!("agent-log-{run_id}"), "Log", false).on_click(
147 move |_, _, cx| {
148 cx.stop_propagation();
149 with_runs(cx, |runs, cx| runs.toggle_expanded(run_id, cx));
150 },
151 ),
152 )
153 .child(
154 chip_button(format!("agent-retry-{run_id}"), "Retry", false).on_click(
155 move |_, _, cx| {
156 cx.stop_propagation();
157 retry_run(run_id, cx);
158 },
159 ),
160 )
161 .when(!session_id.is_empty(), |row| {
162 row.child(
163 chip_button(format!("agent-open-{run_id}"), "Open session", false)
164 .on_click(move |_, _, cx| {
165 cx.stop_propagation();
166 open_session(&session_id, cx);
167 }),
168 )
169 })
170 .child(dismiss_button(run_id))
171 }
172 RunState::Stopped => row
173 .child(icon(IconName::Square, STATUS_ICON, theme::text_faint()))
174 .child(label(action_label, theme::text_soft()))
175 .child(step_text("Stopped".to_owned()))
176 .child(elapsed_text(run))
177 .child(dismiss_button(run_id)),
178 }
179}
180
181fn step_row(elapsed: String, text: String, current: bool) -> Div {
182 h_flex()
183 .gap(px(8.))
184 .items_center()
185 .overflow_hidden()
186 .whitespace_nowrap()
187 .text_color(if current {
188 theme::text_strong()
189 } else {
190 theme::text_muted()
191 })
192 .child(
193 div()
194 .flex_none()
195 .text_color(theme::text_faint())
196 .child(elapsed),
197 )
198 .child(div().min_w(px(0.)).truncate().child(text))
199}
200
201fn stop_row(run_id: u64) -> Div {
202 h_flex()
203 .mt(px(2.))
204 .items_center()
205 .justify_between()
206 .child(
207 chip_button(
208 format!("agent-stop-open-{run_id}"),
209 "Stop and open session",
210 false,
211 )
212 .on_click(move |_, _, cx| {
213 cx.stop_propagation();
214 with_runs(cx, |runs, cx| runs.stop_and_open_session(run_id, cx));
215 }),
216 )
217 .child(
218 chip_button(format!("agent-stop-{run_id}"), "Stop", false).on_click(move |_, _, cx| {
219 cx.stop_propagation();
220 with_runs(cx, |runs, cx| runs.stop(run_id, cx));
221 }),
222 )
223}
224
225fn detail_panel(run: &AgentRun, fill: Hsla) -> Div {
226 let panel = v_flex()
227 .w(px(CHIP_MAX_WIDTH))
228 .py(px(8.))
229 .px(px(10.))
230 .gap(px(6.))
231 .rounded(px(8.))
232 .bg(fill)
233 .text_size(px(12.))
234 .text_color(theme::text_muted());
235 if matches!(run.state, RunState::Failed { .. }) {
236 let lines = run.log_lines();
237 if lines.is_empty() {
238 return panel.child("No output");
239 }
240 return panel.children(lines.iter().map(|line| {
241 div()
242 .overflow_hidden()
243 .whitespace_nowrap()
244 .text_ellipsis()
245 .child(line.clone())
246 }));
247 }
248 let steps = run.visible_steps();
249 if steps.is_empty() {
250 return panel
251 .child("No steps yet")
252 .when(run.is_running(), |panel| panel.child(stop_row(run.id)));
253 }
254 let last = steps.len() - 1;
255 panel
256 .children(steps.iter().enumerate().map(|(index, (elapsed, text))| {
257 step_row(format_elapsed(*elapsed), text.clone(), index == last)
258 }))
259 .when(run.is_running(), |panel| panel.child(stop_row(run.id)))
260}
261
262pub fn run_chip(run: &AgentRun, fill: Hsla) -> AnyElement {
263 v_flex()
264 .gap(px(4.))
265 .items_start()
266 .child(chip_row(run, fill))
267 .when(run.expanded, |column| column.child(detail_panel(run, fill)))
268 .into_any_element()
269}