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