| 1 | use gpui_kit::assets::IconName; |
| 2 | use gpui_kit::component::scroll::ScrollableElement as _; |
| 3 | use gpui_kit::component::{h_flex, v_flex}; |
| 4 | use gpui_kit::prelude::FluentBuilder as _; |
| 5 | use gpui_kit::*; |
| 6 | |
| 7 | use super::runs::{AgentRun, AgentRuns, RunState}; |
| 8 | use super::spinner::spinner; |
| 9 | use crate::theme; |
| 10 | use crate::views::scroll_edge::with_top_edge; |
| 11 | use crate::views::widgets::icon; |
| 12 | |
| 13 | const AGENTS_SCROLL_DIVIDER: &str = "shell/islands/sidebar/agents/divider-scroll"; |
| 14 | const DONE_LIMIT: usize = 50; |
| 15 | const HISTORY_WINDOW: usize = 60; |
| 16 | const EMPTY_TEXT: &str = "No agent runs yet. Hover a message and click the robot button."; |
| 17 | const STATUS_ICON: f32 = 12.; |
| 18 | |
| 19 | pub enum AgentsPanelEvent { |
| 20 | Open { |
| 21 | conversation_id: String, |
| 22 | message_id: String, |
| 23 | }, |
| 24 | } |
| 25 | |
| 26 | pub struct AgentsPanel { |
| 27 | runs: Entity<AgentRuns>, |
| 28 | scroll: ScrollHandle, |
| 29 | _subscription: Subscription, |
| 30 | } |
| 31 | |
| 32 | impl EventEmitter<AgentsPanelEvent> for AgentsPanel {} |
| 33 | |
| 34 | impl AgentsPanel { |
| 35 | pub fn new(runs: Entity<AgentRuns>, cx: &mut Context<Self>) -> Self { |
| 36 | let subscription = cx.observe(&runs, |_, _, cx| cx.notify()); |
| 37 | AgentsPanel { |
| 38 | runs, |
| 39 | scroll: ScrollHandle::new(), |
| 40 | _subscription: subscription, |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | fn show(&mut self, run: &AgentRun, cx: &mut Context<Self>) { |
| 45 | cx.emit(AgentsPanelEvent::Open { |
| 46 | conversation_id: run.conversation_id.clone(), |
| 47 | message_id: run.message_id.clone(), |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | fn row(&self, run: &AgentRun, cx: &mut Context<Self>) -> Stateful<Div> { |
| 52 | let shown = run.clone(); |
| 53 | let status = match &run.state { |
| 54 | RunState::Running => div().mt(px(2.)).child(spinner(STATUS_ICON)), |
| 55 | RunState::Done => { |
| 56 | div() |
| 57 | .mt(px(2.)) |
| 58 | .child(icon(IconName::Check, STATUS_ICON, theme::accent_text())) |
| 59 | } |
| 60 | RunState::Failed { .. } => { |
| 61 | div() |
| 62 | .mt(px(2.)) |
| 63 | .child(icon(IconName::X, STATUS_ICON, theme::red_soft())) |
| 64 | } |
| 65 | RunState::Stopped => { |
| 66 | div() |
| 67 | .mt(px(2.)) |
| 68 | .child(icon(IconName::Square, STATUS_ICON, theme::text_faint())) |
| 69 | } |
| 70 | }; |
| 71 | let trailing = if run.is_running() { |
| 72 | div() |
| 73 | .text_size(px(11.)) |
| 74 | .text_color(theme::text_faint()) |
| 75 | .child(run.elapsed_text()) |
| 76 | } else { |
| 77 | div() |
| 78 | .text_size(px(12.)) |
| 79 | .font_weight(FontWeight::SEMIBOLD) |
| 80 | .text_color(theme::accent_text()) |
| 81 | .child("Show") |
| 82 | }; |
| 83 | h_flex() |
| 84 | .id(("agent-run-row", run.id as usize)) |
| 85 | .mx(px(6.)) |
| 86 | .px(px(10.)) |
| 87 | .py(px(8.)) |
| 88 | .gap(px(12.)) |
| 89 | .items_start() |
| 90 | .rounded(px(8.)) |
| 91 | .cursor_pointer() |
| 92 | .hover(|row| row.bg(theme::row_hover())) |
| 93 | .child(status.flex_none()) |
| 94 | .child( |
| 95 | v_flex() |
| 96 | .flex_1() |
| 97 | .min_w(px(0.)) |
| 98 | .gap(px(2.)) |
| 99 | .child( |
| 100 | div() |
| 101 | .truncate() |
| 102 | .text_size(px(13.)) |
| 103 | .text_color(theme::text()) |
| 104 | .child(format!("{} - {}", run.action_label, run.chat_title)), |
| 105 | ) |
| 106 | .child( |
| 107 | div() |
| 108 | .truncate() |
| 109 | .text_size(px(12.)) |
| 110 | .text_color(theme::text_muted()) |
| 111 | .child(run.current_step.clone()), |
| 112 | ), |
| 113 | ) |
| 114 | .child(trailing.flex_none()) |
| 115 | .on_click(cx.listener(move |this, _, _, cx| this.show(&shown, cx))) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | fn section_header(title: &'static str) -> Div { |
| 120 | div() |
| 121 | .px(px(16.)) |
| 122 | .pt(px(8.)) |
| 123 | .pb(px(4.)) |
| 124 | .text_size(px(11.)) |
| 125 | .font_weight(FontWeight::SEMIBOLD) |
| 126 | .text_color(theme::text_faint()) |
| 127 | .child(title) |
| 128 | } |
| 129 | |
| 130 | pub fn split_runs(runs: Vec<&AgentRun>) -> (Vec<&AgentRun>, Vec<&AgentRun>) { |
| 131 | let (running, finished): (Vec<&AgentRun>, Vec<&AgentRun>) = |
| 132 | runs.into_iter().partition(|run| run.is_running()); |
| 133 | (running, finished.into_iter().take(DONE_LIMIT).collect()) |
| 134 | } |
| 135 | |
| 136 | impl Render for AgentsPanel { |
| 137 | fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 138 | let runs = self.runs.read(cx); |
| 139 | let (running, finished) = split_runs(runs.recent(HISTORY_WINDOW)); |
| 140 | let (running, finished): (Vec<AgentRun>, Vec<AgentRun>) = ( |
| 141 | running.into_iter().cloned().collect(), |
| 142 | finished.into_iter().cloned().collect(), |
| 143 | ); |
| 144 | let empty = running.is_empty() && finished.is_empty(); |
| 145 | let list = v_flex() |
| 146 | .id("agents-list") |
| 147 | .debug_selector(|| "shell/islands/sidebar/agents".into()) |
| 148 | .flex_1() |
| 149 | .min_h_0() |
| 150 | .pb(px(12.)) |
| 151 | .overflow_y_scrollbar() |
| 152 | .track_scroll(&self.scroll) |
| 153 | .when(!running.is_empty(), |list| { |
| 154 | list.child(section_header("Running")) |
| 155 | .children(running.iter().map(|run| self.row(run, cx))) |
| 156 | }) |
| 157 | .when(!finished.is_empty(), |list| { |
| 158 | list.child(section_header("Done")) |
| 159 | .children(finished.iter().map(|run| self.row(run, cx))) |
| 160 | }) |
| 161 | .when(empty, |list| { |
| 162 | list.child( |
| 163 | div() |
| 164 | .py(px(32.)) |
| 165 | .px(px(24.)) |
| 166 | .flex() |
| 167 | .justify_center() |
| 168 | .text_center() |
| 169 | .text_size(px(13.)) |
| 170 | .text_color(theme::text_muted()) |
| 171 | .child(EMPTY_TEXT), |
| 172 | ) |
| 173 | }); |
| 174 | with_top_edge(list, &self.scroll, AGENTS_SCROLL_DIVIDER) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | #[cfg(test)] |
| 179 | mod tests { |
| 180 | use super::split_runs; |
| 181 | use crate::agents::fixtures::finished_run; |
| 182 | use crate::agents::runs::RunState; |
| 183 | |
| 184 | #[test] |
| 185 | fn running_runs_go_first_and_done_is_capped_at_fifty() { |
| 186 | let mut runs: Vec<_> = (0..60).map(finished_run).collect(); |
| 187 | runs[3].state = RunState::Running; |
| 188 | let newest_first: Vec<_> = runs.iter().rev().collect(); |
| 189 | let (running, finished) = split_runs(newest_first); |
| 190 | assert_eq!(running.len(), 1); |
| 191 | assert_eq!(running[0].id, 3); |
| 192 | assert_eq!(finished.len(), 50); |
| 193 | assert_eq!(finished[0].id, 59); |
| 194 | } |
| 195 | } |