BlueGit

ict rusty-teams

Files

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

agents_panel.rs

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