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