BlueGit

ict rusty-teams

Files

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

settings_forms.rs

settings_forms.rs 17.1 KiB Download Raw
1use std::rc::Rc;
2
3use agents::{Environment, OutputFormat};
4use gpui_kit::assets::IconName;
5use gpui_kit::component::{ActiveTheme as _, h_flex, input::Textarea, v_flex};
6use gpui_kit::prelude::FluentBuilder as _;
7use gpui_kit::*;
8
9use super::settings_controls::{
10 Choice, MenuEntry, PAGE_PADDING, boxed, dropdown, field, hint, raised_button, text_field,
11};
12use super::settings_problems::{agent_problem, command_problem, placeholder_problem};
13use super::settings_section::{AgentsSettings, Probe};
14use crate::notify::switch;
15use crate::theme;
16use crate::views::widgets::icon;
17
18const PLACEHOLDER_CHIPS: [&str; 9] = [
19 "message",
20 "sender",
21 "chat",
22 "thread",
23 "extra",
24 "conversation_id",
25 "message_id",
26 "session_id",
27 "agent",
28];
29
30fn environment_choices() -> [(&'static str, Environment); 3] {
31 [
32 ("Windows", Environment::Host),
33 ("WSL - default distro", Environment::Wsl { distro: None }),
34 (
35 "WSL - named distro",
36 Environment::Wsl {
37 distro: Some(String::new()),
38 },
39 ),
40 ]
41}
42
43fn environment_kind(environment: &Environment) -> u8 {
44 match environment {
45 Environment::Host => 0,
46 Environment::Wsl { distro: None } => 1,
47 Environment::Wsl { distro: Some(_) } => 2,
48 }
49}
50
51fn same_environment_kind(left: &Environment, right: &Environment) -> bool {
52 environment_kind(left) == environment_kind(right)
53}
54
55fn output_choices() -> [(&'static str, OutputFormat); 3] {
56 [
57 (
58 "Claude stream-json - live steps",
59 OutputFormat::ClaudeStreamJson,
60 ),
61 ("Copilot JSON - live steps", OutputFormat::CopilotJsonl),
62 ("Plain text - time and last line", OutputFormat::Text),
63 ]
64}
65
66fn back_row(title: String, cx: &mut Context<AgentsSettings>) -> Stateful<Div> {
67 h_flex()
68 .id("agents-back")
69 .w_full()
70 .px(px(PAGE_PADDING))
71 .pt(px(12.))
72 .pb(px(8.))
73 .gap(px(6.))
74 .items_center()
75 .cursor_pointer()
76 .text_size(px(15.))
77 .font_weight(FontWeight::BOLD)
78 .child(icon(IconName::ArrowLeft, 16., theme::text()))
79 .child(div().truncate().child(title))
80 .on_click(cx.listener(|this, _, _, cx| this.go_back(cx)))
81}
82
83fn form(children: impl IntoIterator<Item = AnyElement>) -> Div {
84 v_flex()
85 .w_full()
86 .px(px(PAGE_PADDING))
87 .pb(px(16.))
88 .gap(px(12.))
89 .children(children)
90}
91
92impl AgentsSettings {
93 pub(super) fn agent_page(&mut self, _: &mut Window, cx: &mut Context<Self>) -> Div {
94 let (Some(form_state), Some(profile)) = (&self.agent_form, self.current_agent(cx)) else {
95 return div();
96 };
97 let problems = self.runs.read(cx).settings().validate();
98 let this = cx.entity();
99 let environment_entries: Vec<MenuEntry> = environment_choices()
100 .into_iter()
101 .map(|(label, environment)| {
102 let this = this.clone();
103 let selected = same_environment_kind(&environment, &profile.environment);
104 let choose: Choice = Rc::new(move |window, cx| {
105 let environment = environment.clone();
106 this.update(cx, |this, cx| this.set_environment(environment, window, cx))
107 });
108 MenuEntry {
109 label: label.to_owned(),
110 selected,
111 choose,
112 }
113 })
114 .collect();
115 let environment_label = environment_choices()
116 .into_iter()
117 .find(|(_, environment)| same_environment_kind(environment, &profile.environment))
118 .map_or("Windows", |(label, _)| label);
119 let output_entries: Vec<MenuEntry> = output_choices()
120 .into_iter()
121 .map(|(label, output)| {
122 let this = this.clone();
123 let choose: Choice =
124 Rc::new(move |_, cx| this.update(cx, |this, cx| this.set_output(output, cx)));
125 MenuEntry {
126 label: label.to_owned(),
127 selected: output == profile.output,
128 choose,
129 }
130 })
131 .collect();
132 let output_label = output_choices()
133 .into_iter()
134 .find(|(_, output)| *output == profile.output)
135 .map_or("", |(label, _)| label);
136 let command_problem = command_problem(&problems, &profile.id, &profile.command);
137 let open_problem =
138 placeholder_problem(&problems, &profile.id, &profile.open_session_command);
139 let named_distro = matches!(profile.environment, Environment::Wsl { distro: Some(_) });
140 let status = match &self.probe {
141 Probe::Idle => None,
142 Probe::Running => Some(("Testing...".to_owned(), theme::text_muted())),
143 Probe::Found(line) => Some((format!("Found: {line}"), theme::green())),
144 Probe::Missing(error) => Some((error.clone(), theme::red_soft())),
145 };
146 let test_this = this.clone();
147 let delete_this = this.clone();
148 v_flex()
149 .w_full()
150 .child(back_row(profile.name.clone(), cx))
151 .child(form([
152 field(
153 "Name",
154 text_field(&form_state.name, false, false, cx),
155 None,
156 None,
157 )
158 .into_any_element(),
159 field(
160 "Runs in",
161 dropdown(
162 "agent-environment",
163 environment_label.to_owned(),
164 environment_entries,
165 ),
166 Some(hint(
167 "Windows or a WSL distro. Paths below are in that system.",
168 )),
169 None,
170 )
171 .into_any_element(),
172 div()
173 .w_full()
174 .when(named_distro, |slot| {
175 slot.child(field(
176 "Distro name",
177 text_field(&form_state.distro, false, false, cx),
178 None,
179 None,
180 ))
181 })
182 .into_any_element(),
183 field(
184 "Command",
185 text_field(&form_state.command, true, command_problem.is_some(), cx),
186 Some(hint(
187 "The prompt goes to stdin unless the command has {prompt}.",
188 )),
189 command_problem,
190 )
191 .into_any_element(),
192 field(
193 "Output",
194 dropdown("agent-output", output_label.to_owned(), output_entries),
195 Some(hint("Plain text shows only the time and the last line.")),
196 None,
197 )
198 .into_any_element(),
199 field(
200 "MCP config flag",
201 text_field(&form_state.mcp_flag, true, false, cx),
202 Some(hint(
203 "Passes the Teams MCP endpoint to the agent. Empty = none.",
204 )),
205 None,
206 )
207 .into_any_element(),
208 field(
209 "Open session",
210 text_field(&form_state.open_session, true, open_problem.is_some(), cx),
211 Some(hint("Available: {cwd} and {session_id}.")),
212 open_problem,
213 )
214 .into_any_element(),
215 h_flex()
216 .w_full()
217 .gap(px(8.))
218 .items_center()
219 .child(
220 raised_button("agent-test", "Test").on_click(move |_, _, cx| {
221 test_this.update(cx, |this, cx| this.test_agent(cx))
222 }),
223 )
224 .children(status.map(|(text, color)| {
225 div()
226 .flex_1()
227 .min_w(px(0.))
228 .truncate()
229 .text_size(px(12.))
230 .text_color(color)
231 .child(text)
232 }))
233 .child(div().flex_1())
234 .child(
235 div()
236 .id("agent-delete")
237 .cursor_pointer()
238 .text_size(px(13.))
239 .text_color(theme::red_soft())
240 .child("Delete")
241 .on_click(move |_, _, cx| {
242 delete_this.update(cx, |this, cx| this.delete_agent(cx))
243 }),
244 )
245 .into_any_element(),
246 ]))
247 }
248
249 pub(super) fn action_page(&mut self, _: &mut Window, cx: &mut Context<Self>) -> Div {
250 let Some(form_state) = &self.action_form else {
251 return div();
252 };
253 let settings = self.runs.read(cx).settings().clone();
254 let Some(action) = settings
255 .actions
256 .iter()
257 .find(|action| action.id == form_state.id)
258 .cloned()
259 else {
260 return div();
261 };
262 let problems = settings.validate();
263 let this = cx.entity();
264 let agent_entries: Vec<MenuEntry> = settings
265 .agents
266 .iter()
267 .map(|agent| {
268 let (this, agent_id) = (this.clone(), agent.id.clone());
269 let selected = agent.id == action.agent_id;
270 let choose: Choice = Rc::new(move |_, cx| {
271 let agent_id = agent_id.clone();
272 this.update(cx, |this, cx| this.set_action_agent(agent_id, cx))
273 });
274 MenuEntry {
275 label: agent.name.clone(),
276 selected,
277 choose,
278 }
279 })
280 .collect();
281 let agent_label = settings
282 .agents
283 .iter()
284 .find(|agent| agent.id == action.agent_id)
285 .map_or("Choose an agent".to_owned(), |agent| agent.name.clone());
286 let agent_problem = agent_problem(&problems, &action.id);
287 let prompt_problem = placeholder_problem(&problems, &action.id, &action.prompt_template);
288 let args_problem = placeholder_problem(&problems, &action.id, &action.extra_args);
289 let chips =
290 h_flex()
291 .w_full()
292 .flex_wrap()
293 .gap(px(4.))
294 .children(PLACEHOLDER_CHIPS.iter().map(|placeholder| {
295 let this = this.clone();
296 div()
297 .id(*placeholder)
298 .px(px(6.))
299 .py(px(2.))
300 .rounded(px(6.))
301 .cursor_pointer()
302 .bg(theme::row_hover())
303 .hover(|chip| chip.bg(theme::surface_raised()))
304 .font_family(cx.theme().mono_font_family.clone())
305 .text_size(px(11.))
306 .text_color(theme::accent_text())
307 .child(format!("{{{placeholder}}}"))
308 .on_click(move |_, window, cx| {
309 this.update(cx, |this, cx| {
310 this.insert_placeholder(placeholder, window, cx)
311 })
312 })
313 }));
314 let ask = action.ask_extra_instructions;
315 let (ask_this, delete_this) = (this.clone(), this.clone());
316 v_flex()
317 .w_full()
318 .child(back_row(action.label.clone(), cx))
319 .child(form([
320 h_flex()
321 .w_full()
322 .gap(px(8.))
323 .items_start()
324 .child(
325 field(
326 "Label",
327 text_field(&form_state.label, false, false, cx),
328 None,
329 None,
330 )
331 .flex_1()
332 .min_w(px(0.)),
333 )
334 .child(
335 field(
336 "Agent",
337 dropdown("action-agent", agent_label, agent_entries),
338 None,
339 agent_problem,
340 )
341 .flex_1()
342 .min_w(px(0.)),
343 )
344 .into_any_element(),
345 field(
346 "Working dir",
347 text_field(&form_state.working_dir, true, false, cx),
348 Some(hint("A path inside the system the agent runs in.")),
349 None,
350 )
351 .into_any_element(),
352 field(
353 "Prompt",
354 boxed(
355 Textarea::new(&form_state.prompt)
356 .appearance(false)
357 .bordered(false),
358 prompt_problem.is_some(),
359 cx,
360 ),
361 Some(chips.into_any_element()),
362 prompt_problem,
363 )
364 .into_any_element(),
365 h_flex()
366 .id("action-ask-extra")
367 .w_full()
368 .gap(px(12.))
369 .items_center()
370 .cursor_pointer()
371 .child(
372 v_flex()
373 .flex_1()
374 .child(
375 div()
376 .text_size(px(14.))
377 .font_weight(FontWeight::SEMIBOLD)
378 .child("Ask for extra instructions"),
379 )
380 .child(
381 div()
382 .text_size(px(12.))
383 .text_color(theme::text_muted())
384 .child("Dialog before each run"),
385 ),
386 )
387 .child(switch(ask))
388 .on_click(move |_, _, cx| {
389 ask_this.update(cx, |this, cx| this.set_ask_extra(!ask, cx))
390 })
391 .into_any_element(),
392 field(
393 "Extra args",
394 text_field(&form_state.extra_args, true, args_problem.is_some(), cx),
395 Some(hint("Added after the agent command.")),
396 args_problem,
397 )
398 .into_any_element(),
399 h_flex()
400 .w_full()
401 .justify_end()
402 .child(
403 div()
404 .id("action-delete")
405 .cursor_pointer()
406 .text_size(px(13.))
407 .text_color(theme::red_soft())
408 .child("Delete")
409 .on_click(move |_, _, cx| {
410 delete_this.update(cx, |this, cx| this.delete_action(cx))
411 }),
412 )
413 .into_any_element(),
414 ]))
415 }
416}
417
418pub(super) fn resume_toggle(resume: bool, settings: Entity<AgentsSettings>) -> Stateful<Div> {
419 h_flex()
420 .id("resume-interrupted-runs")
421 .w_full()
422 .px(px(PAGE_PADDING))
423 .pt(px(16.))
424 .gap(px(12.))
425 .items_center()
426 .cursor_pointer()
427 .child(
428 v_flex()
429 .flex_1()
430 .child(
431 div()
432 .text_size(px(14.))
433 .font_weight(FontWeight::SEMIBOLD)
434 .child("Resume interrupted runs"),
435 )
436 .child(
437 div()
438 .text_size(px(12.))
439 .text_color(theme::text_muted())
440 .child("Continue running agents after the app restarts"),
441 ),
442 )
443 .child(switch(resume))
444 .on_click(move |_, _, cx| {
445 settings.update(cx, |this, cx| this.set_resume_interrupted_runs(!resume, cx))
446 })
447}
448
449#[cfg(test)]
450mod tests {
451 use agents::Environment;
452
453 use super::same_environment_kind;
454
455 #[test]
456 fn environments_compare_by_kind_not_distro_name() {
457 let named = |name: &str| Environment::Wsl {
458 distro: Some(name.to_owned()),
459 };
460 assert!(same_environment_kind(&named("Ubuntu"), &named("Debian")));
461 assert!(!same_environment_kind(
462 &named("Ubuntu"),
463 &Environment::Wsl { distro: None }
464 ));
465 assert!(!same_environment_kind(&Environment::Host, &named("Ubuntu")));
466 assert!(same_environment_kind(
467 &Environment::Host,
468 &Environment::Host
469 ));
470 }
471}