BlueGit

ict rusty-teams

Files

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

start_dialog.rs

start_dialog.rs 12.2 KiB Download Raw
1use agents::AgentAction;
2use gpui_kit::assets::IconName;
3use gpui_kit::component::{
4 ActiveTheme as _, h_flex,
5 input::{Textarea, TextareaState},
6 v_flex,
7};
8use gpui_kit::prelude::FluentBuilder as _;
9use gpui_kit::*;
10
11use super::{mcp_bridge, message_context, start_run, start_run_when_mcp_ready};
12use crate::app_state::AppState;
13use crate::theme;
14use crate::views::widgets::{icon, symbol};
15
16const CARD_WIDTH: f32 = 520.;
17const CARD_RADIUS: f32 = 10.;
18const CARD_PADDING: f32 = 16.;
19const CARD_MARGIN: f32 = 32.;
20const SECTION_GAP: f32 = 12.;
21const BACKDROP_OPACITY: f32 = 0.55;
22const CLOSE_SIZE: f32 = 28.;
23const BUTTON_HEIGHT: f32 = 32.;
24const QUOTE_LINES: usize = 3;
25const FIELD_MIN_ROWS: usize = 3;
26const FIELD_MAX_ROWS: usize = 6;
27const PROMPT_SIZE: f32 = 12.;
28
29#[derive(Clone, Debug, PartialEq, Eq)]
30pub struct StartRequest {
31 pub action_id: String,
32 pub conversation_id: String,
33 pub message_id: String,
34}
35
36pub enum StartDialogEvent {
37 Close,
38}
39
40pub struct StartDialog {
41 app: Entity<AppState>,
42 request: StartRequest,
43 action: AgentAction,
44 subtitle: String,
45 sender: String,
46 quote: String,
47 needs_allow: bool,
48 input: Entity<TextareaState>,
49 show_prompt: bool,
50}
51
52impl EventEmitter<StartDialogEvent> for StartDialog {}
53
54pub fn needs_dialog(action: &AgentAction, chat_allowed: bool) -> bool {
55 action.ask_extra_instructions || !chat_allowed
56}
57
58impl StartDialog {
59 pub fn new(
60 app: Entity<AppState>,
61 request: StartRequest,
62 action: AgentAction,
63 agent_name: &str,
64 window: &mut Window,
65 cx: &mut Context<Self>,
66 ) -> Self {
67 let context =
68 message_context::build(app.read(cx), &request.conversation_id, &request.message_id);
69 let (sender, quote) = context
70 .map(|context| (context.sender, context.message))
71 .unwrap_or_default();
72 let input = cx.new(|cx| {
73 TextareaState::new(window, cx)
74 .auto_grow(FIELD_MIN_ROWS, FIELD_MAX_ROWS)
75 .placeholder("e.g. only the backend, open a PR when done")
76 });
77 input.update(cx, |input, cx| input.focus(window, cx));
78 StartDialog {
79 app,
80 subtitle: format!("{agent_name} - {}", action.working_dir),
81 needs_allow: !mcp_bridge::chat_allowed(&request.conversation_id, cx),
82 request,
83 action,
84 sender,
85 quote,
86 input,
87 show_prompt: false,
88 }
89 }
90
91 fn start(&mut self, cx: &mut Context<Self>) {
92 let extra = self.input.read(cx).value().trim().to_owned();
93 if self.needs_allow {
94 mcp_bridge::allow_chat(&self.request.conversation_id, cx);
95 }
96 if self.needs_allow && mcp_bridge::endpoint(cx).is_none() {
97 start_run_when_mcp_ready(
98 self.request.action_id.clone(),
99 self.request.conversation_id.clone(),
100 self.request.message_id.clone(),
101 extra,
102 cx,
103 );
104 } else {
105 start_run(
106 &self.request.action_id,
107 &self.request.conversation_id,
108 &self.request.message_id,
109 &extra,
110 cx,
111 );
112 }
113 cx.emit(StartDialogEvent::Close);
114 }
115
116 fn on_key_down(&mut self, event: &KeyDownEvent, _: &mut Window, cx: &mut Context<Self>) {
117 match event.keystroke.key.as_str() {
118 "escape" => cx.emit(StartDialogEvent::Close),
119 "enter" if event.keystroke.modifiers.control => self.start(cx),
120 _ => return,
121 }
122 cx.stop_propagation();
123 }
124
125 fn header(&self, cx: &mut Context<Self>) -> Div {
126 h_flex()
127 .w_full()
128 .items_start()
129 .gap(px(SECTION_GAP))
130 .child(
131 v_flex()
132 .flex_1()
133 .min_w(px(0.))
134 .child(
135 div()
136 .text_size(px(15.))
137 .font_weight(FontWeight::BOLD)
138 .child(self.action.label.clone()),
139 )
140 .child(
141 div()
142 .truncate()
143 .text_size(px(12.))
144 .text_color(theme::text_muted())
145 .child(self.subtitle.clone()),
146 ),
147 )
148 .child(
149 div()
150 .id("agent-dialog-close")
151 .size(px(CLOSE_SIZE))
152 .flex_none()
153 .flex()
154 .items_center()
155 .justify_center()
156 .rounded_full()
157 .cursor_pointer()
158 .hover(|button| button.bg(theme::popup_hover()))
159 .on_click(cx.listener(|_, _, _, cx| cx.emit(StartDialogEvent::Close)))
160 .child(symbol("close", 16., theme::text_muted())),
161 )
162 }
163
164 fn quote(&self) -> impl IntoElement {
165 let text = format!("{} - {}", self.sender, self.quote);
166 let sender_style = HighlightStyle {
167 color: Some(theme::text()),
168 font_weight: Some(FontWeight::BOLD),
169 ..Default::default()
170 };
171 let sender_range = 0..self.sender.len();
172 div()
173 .w_full()
174 .px(px(12.))
175 .py(px(8.))
176 .rounded(px(8.))
177 .bg(theme::reply_surface())
178 .text_size(px(12.))
179 .line_height(relative(1.4))
180 .text_color(theme::text_soft())
181 .line_clamp(QUOTE_LINES)
182 .child(StyledText::new(text).with_highlights([(sender_range, sender_style)]))
183 }
184
185 fn field(&self, window: &Window, cx: &App) -> Div {
186 let focused = self.input.focus_handle(cx).contains_focused(window, cx);
187 v_flex()
188 .w_full()
189 .gap(px(6.))
190 .child(
191 div()
192 .text_size(px(11.))
193 .font_weight(FontWeight::SEMIBOLD)
194 .text_color(theme::text_faint())
195 .child("Extra instructions (optional)"),
196 )
197 .child(
198 div()
199 .w_full()
200 .px(px(12.))
201 .py(px(8.))
202 .rounded(px(8.))
203 .bg(theme::surface_raised())
204 .border_1()
205 .border_color(if focused {
206 theme::accent()
207 } else {
208 theme::surface_raised()
209 })
210 .child(Textarea::new(&self.input).appearance(false).bordered(false)),
211 )
212 }
213
214 fn prompt_toggle(&self, cx: &mut Context<Self>) -> Stateful<Div> {
215 h_flex()
216 .id("agent-dialog-prompt-toggle")
217 .gap(px(4.))
218 .items_center()
219 .cursor_pointer()
220 .text_size(px(12.))
221 .text_color(theme::text_muted())
222 .hover(|toggle| toggle.text_color(theme::text_soft()))
223 .child(icon(
224 if self.show_prompt {
225 IconName::ChevronDown
226 } else {
227 IconName::ChevronRight
228 },
229 12.,
230 theme::text_muted(),
231 ))
232 .child("Show prompt")
233 .on_click(cx.listener(|this, _, _, cx| {
234 this.show_prompt = !this.show_prompt;
235 cx.notify();
236 }))
237 }
238
239 fn prompt_box(&self, cx: &mut Context<Self>) -> Div {
240 let extra = self.input.read(cx).value().trim().to_owned();
241 let prompt = message_context::preview_prompt(
242 self.app.read(cx),
243 &self.action,
244 &self.request.conversation_id,
245 &self.request.message_id,
246 &extra,
247 )
248 .unwrap_or_default();
249 div()
250 .w_full()
251 .max_h(px(160.))
252 .px(px(12.))
253 .py(px(8.))
254 .rounded(px(8.))
255 .bg(theme::reply_surface())
256 .overflow_hidden()
257 .font_family(cx.theme().mono_font_family.clone())
258 .text_size(px(PROMPT_SIZE))
259 .line_height(relative(1.5))
260 .text_color(theme::text_muted())
261 .child(prompt)
262 }
263
264 fn buttons(&self, cx: &mut Context<Self>) -> Div {
265 let start_label = if self.needs_allow {
266 "Allow and start"
267 } else {
268 "Start"
269 };
270 h_flex()
271 .w_full()
272 .items_center()
273 .gap(px(8.))
274 .child(self.prompt_toggle(cx))
275 .child(div().flex_1())
276 .child(
277 h_flex()
278 .id("agent-dialog-cancel")
279 .h(px(BUTTON_HEIGHT))
280 .px(px(12.))
281 .items_center()
282 .rounded(px(8.))
283 .cursor_pointer()
284 .bg(theme::surface_raised())
285 .hover(|button| button.bg(theme::popup_hover()))
286 .text_size(px(13.))
287 .child("Cancel")
288 .on_click(cx.listener(|_, _, _, cx| cx.emit(StartDialogEvent::Close))),
289 )
290 .child(
291 h_flex()
292 .id("agent-dialog-start")
293 .h(px(BUTTON_HEIGHT))
294 .px(px(12.))
295 .items_center()
296 .rounded(px(8.))
297 .cursor_pointer()
298 .bg(theme::accent())
299 .hover(|button| button.bg(theme::accent_text()))
300 .text_color(theme::on_accent())
301 .text_size(px(13.))
302 .font_weight(FontWeight::SEMIBOLD)
303 .child(start_label)
304 .on_click(cx.listener(|this, _, _, cx| this.start(cx))),
305 )
306 }
307}
308
309impl Render for StartDialog {
310 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
311 div()
312 .id("agent-dialog-layer")
313 .absolute()
314 .top_0()
315 .left_0()
316 .size_full()
317 .flex()
318 .items_center()
319 .justify_center()
320 .child(
321 div()
322 .id("agent-dialog-backdrop")
323 .absolute()
324 .top_0()
325 .left_0()
326 .size_full()
327 .bg(black().opacity(BACKDROP_OPACITY))
328 .occlude()
329 .on_click(cx.listener(|_, _, _, cx| cx.emit(StartDialogEvent::Close))),
330 )
331 .child(
332 v_flex()
333 .id("agent-dialog-card")
334 .w(px(CARD_WIDTH))
335 .max_w(relative(1.))
336 .max_h(relative(1.))
337 .m(px(CARD_MARGIN))
338 .p(px(CARD_PADDING))
339 .gap(px(SECTION_GAP))
340 .rounded(px(CARD_RADIUS))
341 .bg(theme::popup())
342 .text_color(theme::text())
343 .occlude()
344 .capture_key_down(cx.listener(Self::on_key_down))
345 .child(self.header(cx))
346 .child(self.quote())
347 .when(self.needs_allow, |card| {
348 card.child(
349 div()
350 .text_size(px(12.))
351 .text_color(theme::text_soft())
352 .child("Agents can't see this chat yet."),
353 )
354 })
355 .child(self.field(window, cx))
356 .when(self.show_prompt, |card| card.child(self.prompt_box(cx)))
357 .child(self.buttons(cx)),
358 )
359 }
360}
361
362#[cfg(test)]
363mod tests {
364 use agents::AgentAction;
365
366 use super::needs_dialog;
367
368 #[test]
369 fn the_dialog_shows_for_extra_instructions_or_a_chat_agents_cannot_see() {
370 let quiet = AgentAction::answer_this("claude");
371 let asking = AgentAction::build_this("claude");
372 assert!(!needs_dialog(&quiet, true));
373 assert!(needs_dialog(&quiet, false));
374 assert!(needs_dialog(&asking, true));
375 }
376}