BlueGit

ict rusty-teams

Files

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

agents_panel.rs

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