BlueGit

ict rusty-teams

Files

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

settings_forms.rs

settings_forms.rs 22.5 KiB Download Raw
1use std::rc::Rc;
2
3use agents::{AgentBackend, 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::settings::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 host_label() -> &'static str {
31 if cfg!(windows) {
32 "Windows"
33 } else {
34 "This computer"
35 }
36}
37
38fn environment_choices() -> [(&'static str, Environment); 3] {
39 [
40 (host_label(), Environment::Host),
41 ("WSL - default distro", Environment::Wsl { distro: None }),
42 (
43 "WSL - named distro",
44 Environment::Wsl {
45 distro: Some(String::new()),
46 },
47 ),
48 ]
49}
50
51fn environment_kind(environment: &Environment) -> u8 {
52 match environment {
53 Environment::Host => 0,
54 Environment::Wsl { distro: None } => 1,
55 Environment::Wsl { distro: Some(_) } => 2,
56 }
57}
58
59fn same_environment_kind(left: &Environment, right: &Environment) -> bool {
60 environment_kind(left) == environment_kind(right)
61}
62
63fn output_choices() -> [(&'static str, OutputFormat); 3] {
64 [
65 (
66 "Claude stream-json - live steps",
67 OutputFormat::ClaudeStreamJson,
68 ),
69 ("Copilot JSON - live steps", OutputFormat::CopilotJsonl),
70 ("Plain text - time and last line", OutputFormat::Text),
71 ]
72}
73
74fn back_row(title: String, cx: &mut Context<AgentsSettings>) -> Stateful<Div> {
75 h_flex()
76 .id("agents-back")
77 .w_full()
78 .px(px(PAGE_PADDING))
79 .pt(px(12.))
80 .pb(px(8.))
81 .gap(px(6.))
82 .items_center()
83 .cursor_pointer()
84 .text_size(px(15.))
85 .font_weight(FontWeight::BOLD)
86 .child(icon(IconName::ArrowLeft, 16., theme::text()))
87 .child(div().truncate().child(title))
88 .on_click(cx.listener(|this, _, _, cx| this.go_back(cx)))
89}
90
91fn form(children: impl IntoIterator<Item = AnyElement>) -> Div {
92 v_flex()
93 .w_full()
94 .px(px(PAGE_PADDING))
95 .pb(px(16.))
96 .gap(px(12.))
97 .children(children)
98}
99
100impl AgentsSettings {
101 pub(super) fn agent_page(&mut self, _: &mut Window, cx: &mut Context<Self>) -> Div {
102 let (Some(form_state), Some(profile)) = (&self.agent_form, self.current_agent(cx)) else {
103 return div();
104 };
105 let problems = self.runs.read(cx).settings().validate();
106 let this = cx.entity();
107 let environment_entries: Vec<MenuEntry> = environment_choices()
108 .into_iter()
109 .map(|(label, environment)| {
110 let this = this.clone();
111 let selected = same_environment_kind(&environment, &profile.environment);
112 let choose: Choice = Rc::new(move |window, cx| {
113 let environment = environment.clone();
114 this.update(cx, |this, cx| this.set_environment(environment, window, cx))
115 });
116 MenuEntry {
117 label: label.to_owned(),
118 selected,
119 choose,
120 }
121 })
122 .collect();
123 let environment_label = environment_choices()
124 .into_iter()
125 .find(|(_, environment)| same_environment_kind(environment, &profile.environment))
126 .map_or(host_label(), |(label, _)| label);
127 let output_entries: Vec<MenuEntry> = output_choices()
128 .into_iter()
129 .map(|(label, output)| {
130 let this = this.clone();
131 let choose: Choice =
132 Rc::new(move |_, cx| this.update(cx, |this, cx| this.set_output(output, cx)));
133 MenuEntry {
134 label: label.to_owned(),
135 selected: output == profile.output,
136 choose,
137 }
138 })
139 .collect();
140 let output_label = output_choices()
141 .into_iter()
142 .find(|(_, output)| *output == profile.output)
143 .map_or("", |(label, _)| label);
144 let command_problem = command_problem(&problems, &profile.id, &profile.command);
145 let open_problem =
146 placeholder_problem(&problems, &profile.id, &profile.open_session_command);
147 let named_distro = matches!(profile.environment, Environment::Wsl { distro: Some(_) });
148 let chat = profile.is_chat();
149 let status = match &self.probe {
150 Probe::Idle => None,
151 Probe::Running => Some((
152 if chat {
153 "Loading models...".to_owned()
154 } else {
155 "Testing...".to_owned()
156 },
157 theme::text_muted(),
158 )),
159 Probe::Found(line) => Some((
160 if chat {
161 line.clone()
162 } else {
163 format!("Found: {line}")
164 },
165 theme::green(),
166 )),
167 Probe::Missing(error) => Some((error.clone(), theme::red_soft())),
168 };
169 let shows_base = matches!(profile.backend, AgentBackend::OpenAi { .. });
170 let key_saved = match &profile.backend {
171 AgentBackend::OpenAi { api_key, .. } | AgentBackend::Anthropic { api_key, .. } => {
172 !api_key.is_empty()
173 }
174 AgentBackend::Command => false,
175 };
176 let model_problem = problems.iter().find_map(|problem| match problem {
177 agents::SettingsProblem::EmptyModel { agent_id } if agent_id == &profile.id => {
178 Some("Choose a model".to_owned())
179 }
180 _ => None,
181 });
182 let current_model = form_state.model.read(cx).value().trim().to_owned();
183 let mut model_names = self.models.clone();
184 if !current_model.is_empty() && !model_names.iter().any(|name| name == &current_model) {
185 model_names.insert(0, current_model.clone());
186 }
187 let model_entries: Vec<MenuEntry> = model_names
188 .iter()
189 .map(|name| {
190 let this = this.clone();
191 let label = name.clone();
192 let chosen = label.clone();
193 let selected = label == current_model;
194 let choose: Choice = Rc::new(move |window, cx| {
195 let name = chosen.clone();
196 this.update(cx, |this, cx| this.set_model(name, window, cx))
197 });
198 MenuEntry {
199 label,
200 selected,
201 choose,
202 }
203 })
204 .collect();
205 let model_label = if current_model.is_empty() {
206 "Choose a model".to_owned()
207 } else {
208 current_model
209 };
210 let test_this = this.clone();
211 let delete_this = this.clone();
212 v_flex()
213 .w_full()
214 .child(back_row(profile.name.clone(), cx))
215 .child(form([
216 field(
217 "Name",
218 text_field(&form_state.name, false, false, cx),
219 None,
220 None,
221 )
222 .into_any_element(),
223 div()
224 .w_full()
225 .when(chat && shows_base, |slot| {
226 slot.child(field(
227 "Base URL",
228 text_field(&form_state.base_url, true, false, cx),
229 Some(hint("OpenAI-compatible, including /v1.")),
230 None,
231 ))
232 })
233 .into_any_element(),
234 div()
235 .w_full()
236 .when(chat && !model_entries.is_empty(), |slot| {
237 slot.child(field(
238 "Installed models",
239 dropdown("agent-model", model_label, model_entries),
240 Some(hint("Loaded from the endpoint. The choice fills Model.")),
241 None,
242 ))
243 })
244 .into_any_element(),
245 div()
246 .w_full()
247 .when(chat, |slot| {
248 slot.child(field(
249 "Model",
250 text_field(&form_state.model, true, model_problem.is_some(), cx),
251 Some(hint(
252 "Pick a loaded model, or type an id. Leave the URL or key field to reload.",
253 )),
254 model_problem.clone(),
255 ))
256 })
257 .into_any_element(),
258 div()
259 .w_full()
260 .when(chat, |slot| {
261 slot.child(field(
262 "API key",
263 text_field(&form_state.api_key, true, false, cx),
264 Some(hint(if key_saved {
265 "A key is saved. Type a new one to replace it. Ollama can stay empty."
266 } else {
267 "Stored in the local app database. Ollama can stay empty."
268 })),
269 None,
270 ))
271 })
272 .into_any_element(),
273 div()
274 .w_full()
275 .when(!chat, |slot| {
276 slot.child(field(
277 "Runs in",
278 dropdown(
279 "agent-environment",
280 environment_label.to_owned(),
281 environment_entries,
282 ),
283 Some(hint(
284 "This computer or a WSL distro. Paths below are in that system.",
285 )),
286 None,
287 ))
288 })
289 .into_any_element(),
290 div()
291 .w_full()
292 .when(!chat && named_distro, |slot| {
293 slot.child(field(
294 "Distro name",
295 text_field(&form_state.distro, false, false, cx),
296 None,
297 None,
298 ))
299 })
300 .into_any_element(),
301 div()
302 .w_full()
303 .when(!chat, |slot| {
304 slot.child(field(
305 "Command",
306 text_field(&form_state.command, true, command_problem.is_some(), cx),
307 Some(hint(
308 "The prompt goes to stdin unless the command has {prompt}.",
309 )),
310 command_problem,
311 ))
312 })
313 .into_any_element(),
314 div()
315 .w_full()
316 .when(!chat, |slot| {
317 slot.child(field(
318 "Output",
319 dropdown("agent-output", output_label.to_owned(), output_entries),
320 Some(hint("Plain text shows only the time and the last line.")),
321 None,
322 ))
323 })
324 .into_any_element(),
325 div()
326 .w_full()
327 .when(!chat, |slot| {
328 slot.child(field(
329 "MCP config flag",
330 text_field(&form_state.mcp_flag, true, false, cx),
331 Some(hint(
332 "Passes the Teams MCP endpoint to the agent. Empty = none.",
333 )),
334 None,
335 ))
336 })
337 .into_any_element(),
338 div()
339 .w_full()
340 .when(!chat, |slot| {
341 slot.child(field(
342 "Open session",
343 text_field(&form_state.open_session, true, open_problem.is_some(), cx),
344 Some(hint("Available: {cwd} and {session_id}.")),
345 open_problem,
346 ))
347 })
348 .into_any_element(),
349 h_flex()
350 .w_full()
351 .gap(px(8.))
352 .items_center()
353 .child(
354 raised_button("agent-test", "Test").on_click(move |_, _, cx| {
355 test_this.update(cx, |this, cx| this.test_agent(cx))
356 }),
357 )
358 .children(status.map(|(text, color)| {
359 div()
360 .flex_1()
361 .min_w(px(0.))
362 .truncate()
363 .text_size(px(12.))
364 .text_color(color)
365 .child(text)
366 }))
367 .child(div().flex_1())
368 .child(
369 div()
370 .id("agent-delete")
371 .cursor_pointer()
372 .text_size(px(13.))
373 .text_color(theme::red_soft())
374 .child("Delete")
375 .on_click(move |_, _, cx| {
376 delete_this.update(cx, |this, cx| this.delete_agent(cx))
377 }),
378 )
379 .into_any_element(),
380 ]))
381 }
382
383 pub(super) fn action_page(&mut self, _: &mut Window, cx: &mut Context<Self>) -> Div {
384 let Some(form_state) = &self.action_form else {
385 return div();
386 };
387 let settings = self.runs.read(cx).settings().clone();
388 let Some(action) = settings
389 .actions
390 .iter()
391 .find(|action| action.id == form_state.id)
392 .cloned()
393 else {
394 return div();
395 };
396 let problems = settings.validate();
397 let this = cx.entity();
398 let agent_entries: Vec<MenuEntry> = settings
399 .agents
400 .iter()
401 .map(|agent| {
402 let (this, agent_id) = (this.clone(), agent.id.clone());
403 let selected = agent.id == action.agent_id;
404 let choose: Choice = Rc::new(move |_, cx| {
405 let agent_id = agent_id.clone();
406 this.update(cx, |this, cx| this.set_action_agent(agent_id, cx))
407 });
408 MenuEntry {
409 label: agent.name.clone(),
410 selected,
411 choose,
412 }
413 })
414 .collect();
415 let agent_label = settings
416 .agents
417 .iter()
418 .find(|agent| agent.id == action.agent_id)
419 .map_or("Choose an agent".to_owned(), |agent| agent.name.clone());
420 let agent_problem = agent_problem(&problems, &action.id);
421 let prompt_problem = placeholder_problem(&problems, &action.id, &action.prompt_template);
422 let args_problem = placeholder_problem(&problems, &action.id, &action.extra_args);
423 let chips =
424 h_flex()
425 .w_full()
426 .flex_wrap()
427 .gap(px(4.))
428 .children(PLACEHOLDER_CHIPS.iter().map(|placeholder| {
429 let this = this.clone();
430 div()
431 .id(*placeholder)
432 .px(px(6.))
433 .py(px(2.))
434 .rounded(px(6.))
435 .cursor_pointer()
436 .bg(theme::row_hover())
437 .hover(|chip| chip.bg(theme::surface_raised()))
438 .font_family(cx.theme().mono_font_family.clone())
439 .text_size(px(11.))
440 .text_color(theme::accent_text())
441 .child(format!("{{{placeholder}}}"))
442 .on_click(move |_, window, cx| {
443 this.update(cx, |this, cx| {
444 this.insert_placeholder(placeholder, window, cx)
445 })
446 })
447 }));
448 let ask = action.ask_extra_instructions;
449 let (ask_this, delete_this) = (this.clone(), this.clone());
450 v_flex()
451 .w_full()
452 .child(back_row(action.label.clone(), cx))
453 .child(form([
454 h_flex()
455 .w_full()
456 .gap(px(8.))
457 .items_start()
458 .child(
459 field(
460 "Label",
461 text_field(&form_state.label, false, false, cx),
462 None,
463 None,
464 )
465 .flex_1()
466 .min_w(px(0.)),
467 )
468 .child(
469 field(
470 "Agent",
471 dropdown("action-agent", agent_label, agent_entries),
472 None,
473 agent_problem,
474 )
475 .flex_1()
476 .min_w(px(0.)),
477 )
478 .into_any_element(),
479 field(
480 "Working dir",
481 text_field(&form_state.working_dir, true, false, cx),
482 Some(hint("A path inside the system the agent runs in.")),
483 None,
484 )
485 .into_any_element(),
486 field(
487 "Prompt",
488 boxed(
489 Textarea::new(&form_state.prompt)
490 .appearance(false)
491 .bordered(false),
492 prompt_problem.is_some(),
493 cx,
494 ),
495 Some(chips.into_any_element()),
496 prompt_problem,
497 )
498 .into_any_element(),
499 h_flex()
500 .id("action-ask-extra")
501 .w_full()
502 .gap(px(12.))
503 .items_center()
504 .cursor_pointer()
505 .child(
506 v_flex()
507 .flex_1()
508 .child(
509 div()
510 .text_size(px(14.))
511 .font_weight(FontWeight::SEMIBOLD)
512 .child("Ask for extra instructions"),
513 )
514 .child(
515 div()
516 .text_size(px(12.))
517 .text_color(theme::text_muted())
518 .child("Dialog before each run"),
519 ),
520 )
521 .child(switch(ask))
522 .on_click(move |_, _, cx| {
523 ask_this.update(cx, |this, cx| this.set_ask_extra(!ask, cx))
524 })
525 .into_any_element(),
526 field(
527 "Extra args",
528 text_field(&form_state.extra_args, true, args_problem.is_some(), cx),
529 Some(hint("Added after the agent command.")),
530 args_problem,
531 )
532 .into_any_element(),
533 h_flex()
534 .w_full()
535 .justify_end()
536 .child(
537 div()
538 .id("action-delete")
539 .cursor_pointer()
540 .text_size(px(13.))
541 .text_color(theme::red_soft())
542 .child("Delete")
543 .on_click(move |_, _, cx| {
544 delete_this.update(cx, |this, cx| this.delete_action(cx))
545 }),
546 )
547 .into_any_element(),
548 ]))
549 }
550}
551
552pub(super) fn resume_toggle(resume: bool, settings: Entity<AgentsSettings>) -> Stateful<Div> {
553 h_flex()
554 .id("resume-interrupted-runs")
555 .w_full()
556 .px(px(PAGE_PADDING))
557 .pt(px(16.))
558 .gap(px(12.))
559 .items_center()
560 .cursor_pointer()
561 .child(
562 v_flex()
563 .flex_1()
564 .child(
565 div()
566 .text_size(px(14.))
567 .font_weight(FontWeight::SEMIBOLD)
568 .child("Resume interrupted runs"),
569 )
570 .child(
571 div()
572 .text_size(px(12.))
573 .text_color(theme::text_muted())
574 .child("Continue running agents after the app restarts"),
575 ),
576 )
577 .child(switch(resume))
578 .on_click(move |_, _, cx| {
579 settings.update(cx, |this, cx| this.set_resume_interrupted_runs(!resume, cx))
580 })
581}
582
583#[cfg(test)]
584mod tests {
585 use agents::Environment;
586
587 use super::same_environment_kind;
588
589 #[test]
590 fn environments_compare_by_kind_not_distro_name() {
591 let named = |name: &str| Environment::Wsl {
592 distro: Some(name.to_owned()),
593 };
594 assert!(same_environment_kind(&named("Ubuntu"), &named("Debian")));
595 assert!(!same_environment_kind(
596 &named("Ubuntu"),
597 &Environment::Wsl { distro: None }
598 ));
599 assert!(!same_environment_kind(&Environment::Host, &named("Ubuntu")));
600 assert!(same_environment_kind(
601 &Environment::Host,
602 &Environment::Host
603 ));
604 }
605}