| 1 | use agents::{AgentAction, AgentProfile, Environment, OutputFormat}; |
| 2 | use gpui_kit::assets::IconName; |
| 3 | use gpui_kit::component::{ |
| 4 | button::{Button, ButtonVariants as _}, |
| 5 | h_flex, |
| 6 | input::{InputEvent, InputState, TextareaState}, |
| 7 | v_flex, |
| 8 | }; |
| 9 | use gpui_kit::prelude::FluentBuilder as _; |
| 10 | use gpui_kit::*; |
| 11 | |
| 12 | use super::environment_label; |
| 13 | use super::runs::AgentRuns; |
| 14 | use super::settings_controls::{Choice, MenuEntry, PAGE_PADDING, link_dropdown, section_header}; |
| 15 | use super::settings_forms::resume_toggle; |
| 16 | use crate::runtime; |
| 17 | use crate::theme; |
| 18 | use crate::views::widgets::icon; |
| 19 | |
| 20 | const NOTE: &str = "Agents get the message, its sender and up to 20 earlier messages. Nothing is sent to Teams without you."; |
| 21 | const DEFAULT_DISTRO: &str = "Ubuntu"; |
| 22 | const PROMPT_MIN_ROWS: usize = 4; |
| 23 | const PROMPT_MAX_ROWS: usize = 8; |
| 24 | |
| 25 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 26 | pub(super) enum Page { |
| 27 | List, |
| 28 | Agent, |
| 29 | Action, |
| 30 | } |
| 31 | |
| 32 | #[derive(Clone, PartialEq, Eq)] |
| 33 | pub(super) enum Probe { |
| 34 | Idle, |
| 35 | Running, |
| 36 | Found(String), |
| 37 | Missing(String), |
| 38 | } |
| 39 | |
| 40 | pub(super) struct AgentForm { |
| 41 | pub id: String, |
| 42 | pub name: Entity<InputState>, |
| 43 | pub distro: Entity<InputState>, |
| 44 | pub command: Entity<InputState>, |
| 45 | pub open_session: Entity<InputState>, |
| 46 | pub mcp_flag: Entity<InputState>, |
| 47 | } |
| 48 | |
| 49 | pub(super) struct ActionForm { |
| 50 | pub id: String, |
| 51 | pub label: Entity<InputState>, |
| 52 | pub working_dir: Entity<InputState>, |
| 53 | pub prompt: Entity<TextareaState>, |
| 54 | pub extra_args: Entity<InputState>, |
| 55 | } |
| 56 | |
| 57 | pub struct AgentsSettings { |
| 58 | pub(super) runs: Entity<AgentRuns>, |
| 59 | pub(super) page: Page, |
| 60 | pub(super) agent_form: Option<AgentForm>, |
| 61 | pub(super) action_form: Option<ActionForm>, |
| 62 | pub(super) probe: Probe, |
| 63 | form_subscriptions: Vec<Subscription>, |
| 64 | _runs_subscription: Subscription, |
| 65 | } |
| 66 | |
| 67 | pub fn unique_id(base: &str, taken: impl Fn(&str) -> bool) -> String { |
| 68 | if !taken(base) { |
| 69 | return base.to_owned(); |
| 70 | } |
| 71 | (2..) |
| 72 | .map(|suffix| format!("{base}-{suffix}")) |
| 73 | .find(|candidate| !taken(candidate)) |
| 74 | .unwrap_or_else(|| base.to_owned()) |
| 75 | } |
| 76 | |
| 77 | pub fn move_item<T>(items: &mut [T], index: usize, up: bool) -> bool { |
| 78 | let target = if up { |
| 79 | index.checked_sub(1) |
| 80 | } else { |
| 81 | Some(index + 1).filter(|target| *target < items.len()) |
| 82 | }; |
| 83 | match target { |
| 84 | Some(target) if index < items.len() => { |
| 85 | items.swap(index, target); |
| 86 | true |
| 87 | } |
| 88 | _ => false, |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | impl AgentsSettings { |
| 93 | pub fn new(runs: Entity<AgentRuns>, cx: &mut Context<Self>) -> Self { |
| 94 | let subscription = cx.observe(&runs, |_, _, cx| cx.notify()); |
| 95 | AgentsSettings { |
| 96 | runs, |
| 97 | page: Page::List, |
| 98 | agent_form: None, |
| 99 | action_form: None, |
| 100 | probe: Probe::Idle, |
| 101 | form_subscriptions: Vec::new(), |
| 102 | _runs_subscription: subscription, |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | pub(super) fn go_back(&mut self, cx: &mut Context<Self>) { |
| 107 | self.page = Page::List; |
| 108 | self.agent_form = None; |
| 109 | self.action_form = None; |
| 110 | self.form_subscriptions.clear(); |
| 111 | self.probe = Probe::Idle; |
| 112 | cx.notify(); |
| 113 | } |
| 114 | |
| 115 | fn line_input( |
| 116 | &mut self, |
| 117 | value: &str, |
| 118 | window: &mut Window, |
| 119 | cx: &mut Context<Self>, |
| 120 | on_change: fn(&mut Self, &mut Context<Self>), |
| 121 | ) -> Entity<InputState> { |
| 122 | let value = value.to_owned(); |
| 123 | let input = cx.new(|cx| InputState::new(window, cx).default_value(value)); |
| 124 | self.form_subscriptions.push(cx.subscribe( |
| 125 | &input, |
| 126 | move |this, _, event: &InputEvent, cx| { |
| 127 | if matches!(event, InputEvent::Change) { |
| 128 | on_change(this, cx); |
| 129 | } |
| 130 | }, |
| 131 | )); |
| 132 | input |
| 133 | } |
| 134 | |
| 135 | fn open_agent(&mut self, agent_id: &str, window: &mut Window, cx: &mut Context<Self>) { |
| 136 | let Some(profile) = self |
| 137 | .runs |
| 138 | .read(cx) |
| 139 | .settings() |
| 140 | .agents |
| 141 | .iter() |
| 142 | .find(|profile| profile.id == agent_id) |
| 143 | .cloned() |
| 144 | else { |
| 145 | return; |
| 146 | }; |
| 147 | self.go_back(cx); |
| 148 | let distro = match &profile.environment { |
| 149 | Environment::Wsl { |
| 150 | distro: Some(distro), |
| 151 | } => distro.clone(), |
| 152 | _ => String::new(), |
| 153 | }; |
| 154 | self.agent_form = Some(AgentForm { |
| 155 | id: profile.id.clone(), |
| 156 | name: self.line_input(&profile.name, window, cx, Self::commit_agent), |
| 157 | distro: self.line_input(&distro, window, cx, Self::commit_agent), |
| 158 | command: self.line_input(&profile.command, window, cx, Self::commit_agent), |
| 159 | open_session: self.line_input( |
| 160 | &profile.open_session_command, |
| 161 | window, |
| 162 | cx, |
| 163 | Self::commit_agent, |
| 164 | ), |
| 165 | mcp_flag: self.line_input(&profile.mcp_config_flag, window, cx, Self::commit_agent), |
| 166 | }); |
| 167 | self.page = Page::Agent; |
| 168 | cx.notify(); |
| 169 | } |
| 170 | |
| 171 | fn open_action(&mut self, action_id: &str, window: &mut Window, cx: &mut Context<Self>) { |
| 172 | let Some(action) = self |
| 173 | .runs |
| 174 | .read(cx) |
| 175 | .settings() |
| 176 | .actions |
| 177 | .iter() |
| 178 | .find(|action| action.id == action_id) |
| 179 | .cloned() |
| 180 | else { |
| 181 | return; |
| 182 | }; |
| 183 | self.go_back(cx); |
| 184 | let prompt = cx.new(|cx| { |
| 185 | TextareaState::new(window, cx) |
| 186 | .auto_grow(PROMPT_MIN_ROWS, PROMPT_MAX_ROWS) |
| 187 | .default_value(action.prompt_template.clone()) |
| 188 | }); |
| 189 | self.form_subscriptions |
| 190 | .push(cx.subscribe(&prompt, |this, _, event: &InputEvent, cx| { |
| 191 | if matches!(event, InputEvent::Change) { |
| 192 | this.commit_action(cx); |
| 193 | } |
| 194 | })); |
| 195 | self.action_form = Some(ActionForm { |
| 196 | id: action.id.clone(), |
| 197 | label: self.line_input(&action.label, window, cx, Self::commit_action), |
| 198 | working_dir: self.line_input(&action.working_dir, window, cx, Self::commit_action), |
| 199 | prompt, |
| 200 | extra_args: self.line_input(&action.extra_args, window, cx, Self::commit_action), |
| 201 | }); |
| 202 | self.page = Page::Action; |
| 203 | cx.notify(); |
| 204 | } |
| 205 | |
| 206 | pub(super) fn commit_agent(&mut self, cx: &mut Context<Self>) { |
| 207 | let Some(form) = &self.agent_form else { |
| 208 | return; |
| 209 | }; |
| 210 | let id = form.id.clone(); |
| 211 | let name = form.name.read(cx).value().to_string(); |
| 212 | let distro = form.distro.read(cx).value().trim().to_owned(); |
| 213 | let command = form.command.read(cx).value().to_string(); |
| 214 | let open_session = form.open_session.read(cx).value().to_string(); |
| 215 | let mcp_flag = form.mcp_flag.read(cx).value().trim().to_owned(); |
| 216 | self.runs.update(cx, |runs, cx| { |
| 217 | runs.update_settings( |
| 218 | |settings| { |
| 219 | let Some(agent) = settings.agents.iter_mut().find(|agent| agent.id == id) |
| 220 | else { |
| 221 | return; |
| 222 | }; |
| 223 | agent.name = name; |
| 224 | agent.command = command; |
| 225 | agent.open_session_command = open_session; |
| 226 | agent.mcp_config_flag = mcp_flag; |
| 227 | if let Environment::Wsl { |
| 228 | distro: Some(current), |
| 229 | } = &mut agent.environment |
| 230 | { |
| 231 | *current = distro; |
| 232 | } |
| 233 | }, |
| 234 | cx, |
| 235 | ) |
| 236 | }); |
| 237 | self.probe = Probe::Idle; |
| 238 | } |
| 239 | |
| 240 | pub(super) fn commit_action(&mut self, cx: &mut Context<Self>) { |
| 241 | let Some(form) = &self.action_form else { |
| 242 | return; |
| 243 | }; |
| 244 | let id = form.id.clone(); |
| 245 | let label = form.label.read(cx).value().to_string(); |
| 246 | let working_dir = form.working_dir.read(cx).value().trim().to_owned(); |
| 247 | let prompt = form.prompt.read(cx).value().to_string(); |
| 248 | let extra_args = form.extra_args.read(cx).value().to_string(); |
| 249 | self.runs.update(cx, |runs, cx| { |
| 250 | runs.update_settings( |
| 251 | |settings| { |
| 252 | let Some(action) = settings.actions.iter_mut().find(|action| action.id == id) |
| 253 | else { |
| 254 | return; |
| 255 | }; |
| 256 | action.label = label; |
| 257 | action.working_dir = working_dir; |
| 258 | action.prompt_template = prompt; |
| 259 | action.extra_args = extra_args; |
| 260 | }, |
| 261 | cx, |
| 262 | ) |
| 263 | }); |
| 264 | } |
| 265 | |
| 266 | pub(super) fn set_environment( |
| 267 | &mut self, |
| 268 | environment: Environment, |
| 269 | window: &mut Window, |
| 270 | cx: &mut Context<Self>, |
| 271 | ) { |
| 272 | let Some(form) = &self.agent_form else { |
| 273 | return; |
| 274 | }; |
| 275 | let id = form.id.clone(); |
| 276 | if matches!(environment, Environment::Wsl { distro: Some(_) }) |
| 277 | && form.distro.read(cx).value().trim().is_empty() |
| 278 | { |
| 279 | form.distro |
| 280 | .update(cx, |input, cx| input.set_value(DEFAULT_DISTRO, window, cx)); |
| 281 | } |
| 282 | let distro = form.distro.read(cx).value().trim().to_owned(); |
| 283 | let environment = match environment { |
| 284 | Environment::Wsl { distro: Some(_) } => Environment::Wsl { |
| 285 | distro: Some(distro), |
| 286 | }, |
| 287 | other => other, |
| 288 | }; |
| 289 | self.runs.update(cx, |runs, cx| { |
| 290 | runs.update_settings( |
| 291 | |settings| { |
| 292 | if let Some(agent) = settings.agents.iter_mut().find(|agent| agent.id == id) { |
| 293 | agent.environment = environment; |
| 294 | } |
| 295 | }, |
| 296 | cx, |
| 297 | ) |
| 298 | }); |
| 299 | self.probe = Probe::Idle; |
| 300 | } |
| 301 | |
| 302 | pub(super) fn set_output(&mut self, output: OutputFormat, cx: &mut Context<Self>) { |
| 303 | let Some(id) = self.agent_form.as_ref().map(|form| form.id.clone()) else { |
| 304 | return; |
| 305 | }; |
| 306 | self.runs.update(cx, |runs, cx| { |
| 307 | runs.update_settings( |
| 308 | |settings| { |
| 309 | if let Some(agent) = settings.agents.iter_mut().find(|agent| agent.id == id) { |
| 310 | agent.output = output; |
| 311 | } |
| 312 | }, |
| 313 | cx, |
| 314 | ) |
| 315 | }); |
| 316 | } |
| 317 | |
| 318 | pub(super) fn set_action_agent(&mut self, agent_id: String, cx: &mut Context<Self>) { |
| 319 | self.update_action(cx, move |action| action.agent_id = agent_id); |
| 320 | } |
| 321 | |
| 322 | pub(super) fn set_resume_interrupted_runs(&mut self, resume: bool, cx: &mut Context<Self>) { |
| 323 | self.runs.update(cx, |runs, cx| { |
| 324 | runs.update_settings(|settings| settings.resume_interrupted_runs = resume, cx) |
| 325 | }); |
| 326 | } |
| 327 | |
| 328 | pub(super) fn set_ask_extra(&mut self, ask: bool, cx: &mut Context<Self>) { |
| 329 | self.update_action(cx, move |action| action.ask_extra_instructions = ask); |
| 330 | } |
| 331 | |
| 332 | fn update_action(&mut self, cx: &mut Context<Self>, change: impl FnOnce(&mut AgentAction)) { |
| 333 | let Some(id) = self.action_form.as_ref().map(|form| form.id.clone()) else { |
| 334 | return; |
| 335 | }; |
| 336 | self.runs.update(cx, |runs, cx| { |
| 337 | runs.update_settings( |
| 338 | |settings| { |
| 339 | if let Some(action) = settings.actions.iter_mut().find(|action| action.id == id) |
| 340 | { |
| 341 | change(action); |
| 342 | } |
| 343 | }, |
| 344 | cx, |
| 345 | ) |
| 346 | }); |
| 347 | } |
| 348 | |
| 349 | pub(super) fn insert_placeholder( |
| 350 | &mut self, |
| 351 | placeholder: &str, |
| 352 | window: &mut Window, |
| 353 | cx: &mut Context<Self>, |
| 354 | ) { |
| 355 | let Some(prompt) = self.action_form.as_ref().map(|form| form.prompt.clone()) else { |
| 356 | return; |
| 357 | }; |
| 358 | let text = format!("{{{placeholder}}}"); |
| 359 | prompt.update(cx, |prompt, cx| { |
| 360 | prompt.insert(text, window, cx); |
| 361 | prompt.focus(window, cx); |
| 362 | }); |
| 363 | self.commit_action(cx); |
| 364 | } |
| 365 | |
| 366 | pub(super) fn delete_agent(&mut self, cx: &mut Context<Self>) { |
| 367 | let Some(id) = self.agent_form.as_ref().map(|form| form.id.clone()) else { |
| 368 | return; |
| 369 | }; |
| 370 | self.go_back(cx); |
| 371 | self.runs.update(cx, |runs, cx| { |
| 372 | runs.update_settings( |
| 373 | |settings| settings.agents.retain(|agent| agent.id != id), |
| 374 | cx, |
| 375 | ) |
| 376 | }); |
| 377 | } |
| 378 | |
| 379 | pub(super) fn delete_action(&mut self, cx: &mut Context<Self>) { |
| 380 | let Some(id) = self.action_form.as_ref().map(|form| form.id.clone()) else { |
| 381 | return; |
| 382 | }; |
| 383 | self.go_back(cx); |
| 384 | self.runs.update(cx, |runs, cx| { |
| 385 | runs.update_settings( |
| 386 | |settings| settings.actions.retain(|action| action.id != id), |
| 387 | cx, |
| 388 | ) |
| 389 | }); |
| 390 | } |
| 391 | |
| 392 | pub(super) fn test_agent(&mut self, cx: &mut Context<Self>) { |
| 393 | let Some(profile) = self.current_agent(cx) else { |
| 394 | return; |
| 395 | }; |
| 396 | if profile.command.trim().is_empty() { |
| 397 | self.probe = Probe::Missing("The command is empty".to_owned()); |
| 398 | cx.notify(); |
| 399 | return; |
| 400 | } |
| 401 | self.probe = Probe::Running; |
| 402 | cx.notify(); |
| 403 | let invocation = agents::version_check_invocation(&profile); |
| 404 | let receiver = runtime::spawn(async move { agents::probe(&invocation).await }); |
| 405 | cx.spawn(async move |this, cx| { |
| 406 | let result = receiver |
| 407 | .await |
| 408 | .unwrap_or_else(|_| Err("cancelled".to_owned())); |
| 409 | this.update(cx, |this, cx| { |
| 410 | this.probe = match result { |
| 411 | Ok(line) => Probe::Found(line), |
| 412 | Err(error) => Probe::Missing(error), |
| 413 | }; |
| 414 | cx.notify(); |
| 415 | }) |
| 416 | .ok(); |
| 417 | }) |
| 418 | .detach(); |
| 419 | } |
| 420 | |
| 421 | pub(super) fn current_agent(&self, cx: &App) -> Option<AgentProfile> { |
| 422 | let id = &self.agent_form.as_ref()?.id; |
| 423 | self.runs |
| 424 | .read(cx) |
| 425 | .settings() |
| 426 | .agents |
| 427 | .iter() |
| 428 | .find(|agent| &agent.id == id) |
| 429 | .cloned() |
| 430 | } |
| 431 | |
| 432 | fn add_agent(&mut self, preset: AgentProfile, window: &mut Window, cx: &mut Context<Self>) { |
| 433 | let mut profile = preset; |
| 434 | let existing: Vec<String> = self |
| 435 | .runs |
| 436 | .read(cx) |
| 437 | .settings() |
| 438 | .agents |
| 439 | .iter() |
| 440 | .map(|agent| agent.id.clone()) |
| 441 | .collect(); |
| 442 | profile.id = unique_id(&profile.id, |candidate| { |
| 443 | existing.iter().any(|id| id == candidate) |
| 444 | }); |
| 445 | let id = profile.id.clone(); |
| 446 | self.runs.update(cx, |runs, cx| { |
| 447 | runs.update_settings(|settings| settings.agents.push(profile), cx) |
| 448 | }); |
| 449 | self.open_agent(&id, window, cx); |
| 450 | } |
| 451 | |
| 452 | fn add_action(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 453 | let settings = self.runs.read(cx).settings(); |
| 454 | let agent_id = settings |
| 455 | .agents |
| 456 | .first() |
| 457 | .map(|agent| agent.id.clone()) |
| 458 | .unwrap_or_default(); |
| 459 | let mut action = AgentAction::answer_this(&agent_id); |
| 460 | action.label = "New action".to_owned(); |
| 461 | action.id = unique_id("action", |candidate| { |
| 462 | settings.actions.iter().any(|action| action.id == candidate) |
| 463 | }); |
| 464 | let id = action.id.clone(); |
| 465 | self.runs.update(cx, |runs, cx| { |
| 466 | runs.update_settings(|settings| settings.actions.push(action), cx) |
| 467 | }); |
| 468 | self.open_action(&id, window, cx); |
| 469 | } |
| 470 | |
| 471 | fn move_action(&mut self, index: usize, up: bool, cx: &mut Context<Self>) { |
| 472 | self.runs.update(cx, |runs, cx| { |
| 473 | runs.update_settings( |
| 474 | |settings| { |
| 475 | move_item(&mut settings.actions, index, up); |
| 476 | }, |
| 477 | cx, |
| 478 | ) |
| 479 | }); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | type Preset = (&'static str, fn() -> AgentProfile); |
| 484 | |
| 485 | fn agent_presets() -> [Preset; 4] { |
| 486 | fn custom() -> AgentProfile { |
| 487 | AgentProfile { |
| 488 | id: "custom".to_owned(), |
| 489 | name: "Custom agent".to_owned(), |
| 490 | environment: Environment::Host, |
| 491 | command: String::new(), |
| 492 | output: OutputFormat::Text, |
| 493 | open_session_command: String::new(), |
| 494 | mcp_config_flag: String::new(), |
| 495 | } |
| 496 | } |
| 497 | [ |
| 498 | ("Claude Code (WSL)", AgentProfile::claude_wsl), |
| 499 | ("Claude Code (Windows)", AgentProfile::claude_host), |
| 500 | ("Copilot CLI (WSL)", AgentProfile::copilot_wsl), |
| 501 | ("Custom", custom), |
| 502 | ] |
| 503 | } |
| 504 | |
| 505 | fn list_row( |
| 506 | id: (&'static str, usize), |
| 507 | leading: Option<AnyElement>, |
| 508 | title: String, |
| 509 | detail: String, |
| 510 | mono: Option<String>, |
| 511 | ) -> Stateful<Div> { |
| 512 | h_flex() |
| 513 | .id(id) |
| 514 | .w_full() |
| 515 | .px(px(12.)) |
| 516 | .py(px(8.)) |
| 517 | .gap(px(12.)) |
| 518 | .items_center() |
| 519 | .rounded(px(8.)) |
| 520 | .cursor_pointer() |
| 521 | .hover(|row| row.bg(theme::row_hover())) |
| 522 | .children(leading) |
| 523 | .child( |
| 524 | v_flex() |
| 525 | .flex_1() |
| 526 | .min_w(px(0.)) |
| 527 | .gap(px(2.)) |
| 528 | .child( |
| 529 | div() |
| 530 | .text_size(px(14.)) |
| 531 | .font_weight(FontWeight::SEMIBOLD) |
| 532 | .child(title), |
| 533 | ) |
| 534 | .child( |
| 535 | div() |
| 536 | .truncate() |
| 537 | .text_size(px(12.)) |
| 538 | .text_color(theme::text_muted()) |
| 539 | .child(detail), |
| 540 | ) |
| 541 | .children(mono.map(|command| { |
| 542 | div() |
| 543 | .truncate() |
| 544 | .text_size(px(11.)) |
| 545 | .text_color(theme::text_faint()) |
| 546 | .child(command) |
| 547 | })), |
| 548 | ) |
| 549 | } |
| 550 | |
| 551 | fn small_button(id: (&'static str, usize), name: IconName, enabled: bool) -> Stateful<Div> { |
| 552 | div() |
| 553 | .id(id) |
| 554 | .size(px(28.)) |
| 555 | .flex() |
| 556 | .flex_none() |
| 557 | .items_center() |
| 558 | .justify_center() |
| 559 | .rounded(px(7.)) |
| 560 | .when(enabled, |button| { |
| 561 | button |
| 562 | .cursor_pointer() |
| 563 | .hover(|button| button.bg(theme::surface_raised())) |
| 564 | }) |
| 565 | .when(!enabled, |button| button.opacity(0.4)) |
| 566 | .child(icon(name, 14., theme::text_muted())) |
| 567 | } |
| 568 | |
| 569 | impl AgentsSettings { |
| 570 | fn list_page(&mut self, cx: &mut Context<Self>) -> Div { |
| 571 | let settings = self.runs.read(cx).settings().clone(); |
| 572 | let this = cx.entity(); |
| 573 | let preset_entries: Vec<MenuEntry> = agent_presets() |
| 574 | .into_iter() |
| 575 | .map(|(label, build)| { |
| 576 | let this = this.clone(); |
| 577 | let choose: Choice = std::rc::Rc::new(move |window, cx| { |
| 578 | this.update(cx, |this, cx| this.add_agent(build(), window, cx)) |
| 579 | }); |
| 580 | MenuEntry { |
| 581 | label: label.to_owned(), |
| 582 | selected: false, |
| 583 | choose, |
| 584 | } |
| 585 | }) |
| 586 | .collect(); |
| 587 | let add_action = { |
| 588 | let this = this.clone(); |
| 589 | Button::new("add-action") |
| 590 | .link() |
| 591 | .label("+ Add action") |
| 592 | .on_click(move |_, window, cx| { |
| 593 | this.update(cx, |this, cx| this.add_action(window, cx)) |
| 594 | }) |
| 595 | }; |
| 596 | let action_count = settings.actions.len(); |
| 597 | let agent_rows = settings.agents.iter().enumerate().map(|(index, agent)| { |
| 598 | let (open, id) = (cx.entity(), agent.id.clone()); |
| 599 | list_row( |
| 600 | ("agent-row", index), |
| 601 | Some( |
| 602 | div() |
| 603 | .size(px(28.)) |
| 604 | .flex() |
| 605 | .flex_none() |
| 606 | .items_center() |
| 607 | .justify_center() |
| 608 | .rounded(px(7.)) |
| 609 | .bg(theme::surface_raised()) |
| 610 | .child(icon(IconName::Bot, 14., theme::accent_text())) |
| 611 | .into_any_element(), |
| 612 | ), |
| 613 | agent.name.clone(), |
| 614 | environment_label(&agent.environment), |
| 615 | Some(agent.command.clone()), |
| 616 | ) |
| 617 | .on_click(move |_, window, cx| { |
| 618 | open.update(cx, |this, cx| this.open_agent(&id, window, cx)) |
| 619 | }) |
| 620 | }); |
| 621 | let action_rows = settings.actions.iter().enumerate().map(|(index, action)| { |
| 622 | let (open, id) = (cx.entity(), action.id.clone()); |
| 623 | let (up, down) = (cx.entity(), cx.entity()); |
| 624 | let agent_name = settings |
| 625 | .agents |
| 626 | .iter() |
| 627 | .find(|agent| agent.id == action.agent_id) |
| 628 | .map_or("Unknown agent", |agent| agent.name.as_str()); |
| 629 | list_row( |
| 630 | ("action-row", index), |
| 631 | None, |
| 632 | action.label.clone(), |
| 633 | format!("{agent_name} - {}", action.working_dir), |
| 634 | None, |
| 635 | ) |
| 636 | .on_click(move |_, window, cx| { |
| 637 | open.update(cx, |this, cx| this.open_action(&id, window, cx)) |
| 638 | }) |
| 639 | .child( |
| 640 | h_flex() |
| 641 | .flex_none() |
| 642 | .child( |
| 643 | small_button(("action-up", index), IconName::ArrowUp, index > 0).on_click( |
| 644 | move |_, _, cx| { |
| 645 | cx.stop_propagation(); |
| 646 | up.update(cx, |this, cx| this.move_action(index, true, cx)) |
| 647 | }, |
| 648 | ), |
| 649 | ) |
| 650 | .child( |
| 651 | small_button( |
| 652 | ("action-down", index), |
| 653 | IconName::ArrowDown, |
| 654 | index + 1 < action_count, |
| 655 | ) |
| 656 | .on_click(move |_, _, cx| { |
| 657 | cx.stop_propagation(); |
| 658 | down.update(cx, |this, cx| this.move_action(index, false, cx)) |
| 659 | }), |
| 660 | ), |
| 661 | ) |
| 662 | }); |
| 663 | v_flex() |
| 664 | .w_full() |
| 665 | .child(section_header( |
| 666 | "AGENTS", |
| 667 | Some(link_dropdown("add-agent", "+ Add agent", preset_entries).into_any_element()), |
| 668 | )) |
| 669 | .child(v_flex().w_full().px(px(8.)).children(agent_rows)) |
| 670 | .child(section_header( |
| 671 | "ACTIONS - IN THIS ORDER IN THE MENU", |
| 672 | Some(add_action.into_any_element()), |
| 673 | )) |
| 674 | .child(v_flex().w_full().px(px(8.)).children(action_rows)) |
| 675 | .child(resume_toggle(settings.resume_interrupted_runs, this)) |
| 676 | .child( |
| 677 | div() |
| 678 | .w_full() |
| 679 | .px(px(PAGE_PADDING)) |
| 680 | .pt(px(16.)) |
| 681 | .pb(px(16.)) |
| 682 | .text_size(px(12.)) |
| 683 | .line_height(relative(1.5)) |
| 684 | .text_color(theme::text_faint()) |
| 685 | .child(NOTE), |
| 686 | ) |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | impl Render for AgentsSettings { |
| 691 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 692 | match self.page { |
| 693 | Page::List => self.list_page(cx).into_any_element(), |
| 694 | Page::Agent => self.agent_page(window, cx).into_any_element(), |
| 695 | Page::Action => self.action_page(window, cx).into_any_element(), |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | #[cfg(test)] |
| 701 | mod tests { |
| 702 | use super::{move_item, unique_id}; |
| 703 | |
| 704 | #[test] |
| 705 | fn ids_get_a_numeric_suffix_when_taken() { |
| 706 | let taken = ["claude", "claude-2"]; |
| 707 | let is_taken = |candidate: &str| taken.contains(&candidate); |
| 708 | assert_eq!(unique_id("copilot", is_taken), "copilot"); |
| 709 | assert_eq!(unique_id("claude", is_taken), "claude-3"); |
| 710 | } |
| 711 | |
| 712 | #[test] |
| 713 | fn moving_swaps_neighbours_and_stops_at_the_ends() { |
| 714 | let mut items = vec!["a", "b", "c"]; |
| 715 | assert!(move_item(&mut items, 1, true)); |
| 716 | assert_eq!(items, ["b", "a", "c"]); |
| 717 | assert!(move_item(&mut items, 1, false)); |
| 718 | assert_eq!(items, ["b", "c", "a"]); |
| 719 | assert!(!move_item(&mut items, 0, true)); |
| 720 | assert!(!move_item(&mut items, 2, false)); |
| 721 | assert_eq!(items, ["b", "c", "a"]); |
| 722 | } |
| 723 | } |