| 1 | use std::collections::{BTreeSet, HashMap}; |
| 2 | use std::time::Duration; |
| 3 | |
| 4 | use gpui_kit::assets::IconName; |
| 5 | use gpui_kit::component::scroll::ScrollableElement as _; |
| 6 | use gpui_kit::component::{ |
| 7 | ActiveTheme as _, h_flex, |
| 8 | input::{Input, InputEvent, InputState}, |
| 9 | v_flex, |
| 10 | }; |
| 11 | use gpui_kit::prelude::FluentBuilder as _; |
| 12 | use gpui_kit::*; |
| 13 | use mcp::install::{InstallReport, InstallState, Location, Target}; |
| 14 | use mcp::{Access, AgentPolicy}; |
| 15 | |
| 16 | use super::rows::{ROW_INSET, caption, control_row, toggle_row}; |
| 17 | use crate::agent_api::{self, ServerStatus}; |
| 18 | use crate::app_state::AppState; |
| 19 | use crate::data::is_one_on_one; |
| 20 | use crate::fuzzy; |
| 21 | use crate::theme; |
| 22 | use crate::views::switcher::{Candidate, candidate_avatar, candidates_from}; |
| 23 | use crate::views::widgets::icon; |
| 24 | |
| 25 | const COPIED_SHOWN: Duration = Duration::from_millis(1500); |
| 26 | const PORT_WIDTH: f32 = 72.; |
| 27 | const FIELD_HEIGHT: f32 = 28.; |
| 28 | const FIELD_LABEL_WIDTH: f32 = 60.; |
| 29 | const SMALL_BUTTON_HEIGHT: f32 = 28.; |
| 30 | const PICKER_ROW_HEIGHT: f32 = 30.; |
| 31 | const PICKER_MAX_ROWS: usize = 6; |
| 32 | const PICKER_RESULTS: usize = 40; |
| 33 | const TOKEN_TAIL: usize = 4; |
| 34 | const CHIP_MAX_WIDTH: f32 = 180.; |
| 35 | const WSL_WARNING: &str = "WSL agents cannot reach 127.0.0.1 on Windows. Add networkingMode=mirrored to .wslconfig and run wsl --shutdown."; |
| 36 | |
| 37 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 38 | enum Scope { |
| 39 | Chats, |
| 40 | Channels, |
| 41 | } |
| 42 | |
| 43 | impl Scope { |
| 44 | fn access(self, policy: &AgentPolicy) -> &Access { |
| 45 | match self { |
| 46 | Scope::Chats => &policy.chats, |
| 47 | Scope::Channels => &policy.channels, |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | fn access_mut(self, policy: &mut AgentPolicy) -> &mut Access { |
| 52 | match self { |
| 53 | Scope::Chats => &mut policy.chats, |
| 54 | Scope::Channels => &mut policy.channels, |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | fn title(self) -> &'static str { |
| 59 | match self { |
| 60 | Scope::Chats => "Chats", |
| 61 | Scope::Channels => "Channels", |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | fn add_label(self) -> &'static str { |
| 66 | match self { |
| 67 | Scope::Chats => "+ Add chat", |
| 68 | Scope::Channels => "+ Add channel", |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | fn contains(self, candidate: &Candidate) -> bool { |
| 73 | use crate::app_state::Selection; |
| 74 | matches!( |
| 75 | (self, &candidate.selection), |
| 76 | (Scope::Chats, Selection::Chat(_)) | (Scope::Channels, Selection::Channel(_)) |
| 77 | ) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 82 | enum Copied { |
| 83 | Url, |
| 84 | Token, |
| 85 | } |
| 86 | |
| 87 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 88 | enum InstallAction { |
| 89 | Install, |
| 90 | Update, |
| 91 | Remove, |
| 92 | } |
| 93 | |
| 94 | impl InstallAction { |
| 95 | fn label(self) -> &'static str { |
| 96 | match self { |
| 97 | InstallAction::Install => "Install", |
| 98 | InstallAction::Update => "Update", |
| 99 | InstallAction::Remove => "Remove", |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | fn busy_label(self) -> &'static str { |
| 104 | match self { |
| 105 | InstallAction::Install => "Installing...", |
| 106 | InstallAction::Update => "Updating...", |
| 107 | InstallAction::Remove => "Removing...", |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | fn target_logo(target: Target) -> &'static str { |
| 113 | match target { |
| 114 | Target::ClaudeCode => "CC", |
| 115 | Target::Codex => "CX", |
| 116 | Target::CopilotCli => "GH", |
| 117 | Target::VsCode => "VS", |
| 118 | Target::Cursor => "CU", |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | fn target_action(state: &InstallState) -> Option<InstallAction> { |
| 123 | if state.stale { |
| 124 | Some(InstallAction::Update) |
| 125 | } else if !state.installed_at.is_empty() { |
| 126 | Some(InstallAction::Remove) |
| 127 | } else if state.available { |
| 128 | Some(InstallAction::Install) |
| 129 | } else { |
| 130 | None |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | fn locations_label(locations: &[Location]) -> &'static str { |
| 135 | let windows = locations.contains(&Location::Windows); |
| 136 | let wsl = locations |
| 137 | .iter() |
| 138 | .any(|location| matches!(location, Location::Wsl { .. })); |
| 139 | match (windows, wsl) { |
| 140 | (true, true) => "Windows + WSL", |
| 141 | (true, false) => "Windows", |
| 142 | (false, true) => "WSL", |
| 143 | (false, false) => "Installed", |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | fn install_detail(target: Target, state: Option<&InstallState>) -> AnyElement { |
| 148 | let muted = |text: &'static str| { |
| 149 | div() |
| 150 | .text_size(px(12.)) |
| 151 | .text_color(theme::text_muted()) |
| 152 | .child(text) |
| 153 | .into_any_element() |
| 154 | }; |
| 155 | let Some(state) = state else { |
| 156 | return muted(target.config_hint()); |
| 157 | }; |
| 158 | if state.stale { |
| 159 | return div() |
| 160 | .text_size(px(12.)) |
| 161 | .text_color(theme::amber()) |
| 162 | .child("Out of date - Update") |
| 163 | .into_any_element(); |
| 164 | } |
| 165 | if !state.installed_at.is_empty() { |
| 166 | return h_flex() |
| 167 | .gap(px(6.)) |
| 168 | .items_center() |
| 169 | .text_size(px(12.)) |
| 170 | .text_color(theme::green()) |
| 171 | .child(div().size(px(6.)).rounded_full().bg(theme::green())) |
| 172 | .child(locations_label(&state.installed_at)) |
| 173 | .into_any_element(); |
| 174 | } |
| 175 | if state.available { |
| 176 | muted(target.config_hint()) |
| 177 | } else { |
| 178 | muted("Not installed") |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | fn wsl_warning() -> Div { |
| 183 | note_box( |
| 184 | h_flex() |
| 185 | .gap(px(8.)) |
| 186 | .items_start() |
| 187 | .child( |
| 188 | div() |
| 189 | .font_weight(FontWeight::BOLD) |
| 190 | .text_color(theme::amber()) |
| 191 | .child("!"), |
| 192 | ) |
| 193 | .child(div().flex_1().child(WSL_WARNING)), |
| 194 | 4., |
| 195 | ) |
| 196 | } |
| 197 | |
| 198 | pub struct McpView { |
| 199 | app: Entity<AppState>, |
| 200 | picker: Option<Scope>, |
| 201 | port_input: Entity<InputState>, |
| 202 | port_invalid: bool, |
| 203 | search_input: Entity<InputState>, |
| 204 | copied: Option<Copied>, |
| 205 | install_states: Option<Vec<InstallState>>, |
| 206 | install_busy: Option<(Target, InstallAction)>, |
| 207 | install_errors: HashMap<Target, String>, |
| 208 | wsl_unreachable: bool, |
| 209 | _subscriptions: Vec<Subscription>, |
| 210 | } |
| 211 | |
| 212 | impl McpView { |
| 213 | pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self { |
| 214 | let port = agent_api::policy(cx).port; |
| 215 | let port_input = cx.new(|cx| InputState::new(window, cx)); |
| 216 | port_input.update(cx, |input, cx| { |
| 217 | input.set_value(port.to_string(), window, cx) |
| 218 | }); |
| 219 | let search_input = cx.new(|cx| InputState::new(window, cx).placeholder("Search")); |
| 220 | let mut subscriptions = vec![ |
| 221 | cx.subscribe_in(&port_input, window, Self::on_port_event), |
| 222 | cx.subscribe_in(&search_input, window, |_, _, event, _, cx| { |
| 223 | if matches!(event, InputEvent::Change) { |
| 224 | cx.notify(); |
| 225 | } |
| 226 | }), |
| 227 | ]; |
| 228 | if let Some(api) = agent_api::handle(cx) { |
| 229 | subscriptions.push(cx.observe(&api, |_, _, cx| cx.notify())); |
| 230 | } |
| 231 | let mut view = McpView { |
| 232 | app, |
| 233 | picker: None, |
| 234 | port_input, |
| 235 | port_invalid: false, |
| 236 | search_input, |
| 237 | copied: None, |
| 238 | install_states: None, |
| 239 | install_busy: None, |
| 240 | install_errors: HashMap::new(), |
| 241 | wsl_unreachable: false, |
| 242 | _subscriptions: subscriptions, |
| 243 | }; |
| 244 | view.refresh_wsl(cx); |
| 245 | view.refresh_installs(cx); |
| 246 | view |
| 247 | } |
| 248 | |
| 249 | fn on_port_event( |
| 250 | &mut self, |
| 251 | input: &Entity<InputState>, |
| 252 | event: &InputEvent, |
| 253 | _window: &mut Window, |
| 254 | cx: &mut Context<Self>, |
| 255 | ) { |
| 256 | if !matches!(event, InputEvent::Blur | InputEvent::PressEnter { .. }) { |
| 257 | return; |
| 258 | } |
| 259 | let current = agent_api::policy(cx).port; |
| 260 | let typed = input |
| 261 | .read(cx) |
| 262 | .value() |
| 263 | .trim() |
| 264 | .parse::<u16>() |
| 265 | .ok() |
| 266 | .filter(|port| *port != 0); |
| 267 | self.port_invalid = typed.is_none(); |
| 268 | if let Some(port) = typed |
| 269 | && port != current |
| 270 | { |
| 271 | agent_api::update_policy(cx, |policy| policy.port = port); |
| 272 | self.refresh_installs(cx); |
| 273 | } |
| 274 | self.refresh_wsl(cx); |
| 275 | cx.notify(); |
| 276 | } |
| 277 | |
| 278 | fn refresh_wsl(&mut self, cx: &mut Context<Self>) { |
| 279 | let port = agent_api::policy(cx).port; |
| 280 | let task = cx.background_spawn(async move { mcp::install::wsl_reachable(port) }); |
| 281 | cx.spawn(async move |this, cx| { |
| 282 | let reachable = task.await; |
| 283 | this.update(cx, |this, cx| { |
| 284 | this.wsl_unreachable = reachable == Some(false); |
| 285 | cx.notify(); |
| 286 | }) |
| 287 | .ok(); |
| 288 | }) |
| 289 | .detach(); |
| 290 | } |
| 291 | |
| 292 | fn refresh_installs(&mut self, cx: &mut Context<Self>) { |
| 293 | let Some(endpoint) = agent_api::configured_endpoint(cx) else { |
| 294 | return; |
| 295 | }; |
| 296 | let demo = agent_api::is_demo(cx); |
| 297 | let task = cx.background_spawn(async move { |
| 298 | let homes = agent_api::agent_homes(demo); |
| 299 | Target::ALL |
| 300 | .map(|target| mcp::install::status(target, &endpoint, &homes)) |
| 301 | .to_vec() |
| 302 | }); |
| 303 | cx.spawn(async move |this, cx| { |
| 304 | let states = task.await; |
| 305 | this.update(cx, |this, cx| { |
| 306 | this.install_states = Some(states); |
| 307 | cx.notify(); |
| 308 | }) |
| 309 | .ok(); |
| 310 | }) |
| 311 | .detach(); |
| 312 | } |
| 313 | |
| 314 | fn run_install_action( |
| 315 | &mut self, |
| 316 | target: Target, |
| 317 | action: InstallAction, |
| 318 | cx: &mut Context<Self>, |
| 319 | ) { |
| 320 | let Some(endpoint) = agent_api::configured_endpoint(cx) else { |
| 321 | return; |
| 322 | }; |
| 323 | self.install_busy = Some((target, action)); |
| 324 | self.install_errors.remove(&target); |
| 325 | let demo = agent_api::is_demo(cx); |
| 326 | let task = cx.background_spawn(async move { |
| 327 | let homes = agent_api::agent_homes(demo); |
| 328 | match action { |
| 329 | InstallAction::Remove => mcp::install::uninstall(target, &homes).map(|_| None), |
| 330 | _ => mcp::install::install(target, &endpoint, &homes).map(Some), |
| 331 | } |
| 332 | }); |
| 333 | cx.spawn(async move |this, cx| { |
| 334 | let result = task.await; |
| 335 | this.update(cx, |this, cx| { |
| 336 | this.install_busy = None; |
| 337 | match result { |
| 338 | Ok(Some(InstallReport::OpenUrl(url))) => cx.open_url(&url), |
| 339 | Ok(_) => {} |
| 340 | Err(error) => { |
| 341 | this.install_errors.insert(target, error.to_string()); |
| 342 | } |
| 343 | } |
| 344 | this.refresh_installs(cx); |
| 345 | cx.notify(); |
| 346 | }) |
| 347 | .ok(); |
| 348 | }) |
| 349 | .detach(); |
| 350 | cx.notify(); |
| 351 | } |
| 352 | |
| 353 | fn copy(&mut self, target: Copied, text: String, cx: &mut Context<Self>) { |
| 354 | cx.write_to_clipboard(ClipboardItem::new_string(text)); |
| 355 | self.copied = Some(target); |
| 356 | cx.spawn(async move |this, cx| { |
| 357 | cx.background_executor().timer(COPIED_SHOWN).await; |
| 358 | this.update(cx, |this, cx| { |
| 359 | this.copied = None; |
| 360 | cx.notify(); |
| 361 | }) |
| 362 | .ok(); |
| 363 | }) |
| 364 | .detach(); |
| 365 | cx.notify(); |
| 366 | } |
| 367 | |
| 368 | fn open_picker(&mut self, scope: Scope, window: &mut Window, cx: &mut Context<Self>) { |
| 369 | self.picker = if self.picker == Some(scope) { |
| 370 | None |
| 371 | } else { |
| 372 | Some(scope) |
| 373 | }; |
| 374 | self.search_input |
| 375 | .update(cx, |input, cx| input.set_value("", window, cx)); |
| 376 | if self.picker.is_some() { |
| 377 | self.search_input |
| 378 | .update(cx, |input, cx| input.focus(window, cx)); |
| 379 | } |
| 380 | cx.notify(); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | fn small_button(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Stateful<Div> { |
| 385 | inert_button(id, label) |
| 386 | .cursor_pointer() |
| 387 | .text_color(theme::text()) |
| 388 | .hover(|button| button.bg(theme::popup_hover())) |
| 389 | } |
| 390 | |
| 391 | fn inert_button(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Stateful<Div> { |
| 392 | div() |
| 393 | .id(id) |
| 394 | .h(px(SMALL_BUTTON_HEIGHT)) |
| 395 | .px(px(12.)) |
| 396 | .flex_none() |
| 397 | .flex() |
| 398 | .items_center() |
| 399 | .rounded(px(7.)) |
| 400 | .bg(theme::surface_raised()) |
| 401 | .text_size(px(12.)) |
| 402 | .font_weight(FontWeight::SEMIBOLD) |
| 403 | .text_color(theme::text_faint()) |
| 404 | .child(label.into()) |
| 405 | } |
| 406 | |
| 407 | fn note_box(content: impl IntoElement, margin_top: f32) -> Div { |
| 408 | h_flex() |
| 409 | .mx(px(ROW_INSET)) |
| 410 | .mt(px(margin_top)) |
| 411 | .mb(px(12.)) |
| 412 | .px(px(12.)) |
| 413 | .py(px(10.)) |
| 414 | .gap(px(8.)) |
| 415 | .rounded(px(8.)) |
| 416 | .bg(theme::surface_raised()) |
| 417 | .text_size(px(12.)) |
| 418 | .text_color(theme::text_muted()) |
| 419 | .child(content) |
| 420 | } |
| 421 | |
| 422 | fn masked_token(token: &str) -> String { |
| 423 | let tail: String = token |
| 424 | .chars() |
| 425 | .rev() |
| 426 | .take(TOKEN_TAIL) |
| 427 | .collect::<Vec<_>>() |
| 428 | .into_iter() |
| 429 | .rev() |
| 430 | .collect(); |
| 431 | format!("****...{tail}") |
| 432 | } |
| 433 | |
| 434 | fn chat_kind_label(state: &AppState, conversation_id: &str) -> Option<&'static str> { |
| 435 | let chat = state |
| 436 | .sidebar |
| 437 | .chats |
| 438 | .iter() |
| 439 | .find(|chat| chat.id == conversation_id)?; |
| 440 | Some(if chat.kind.eq_ignore_ascii_case("meeting") { |
| 441 | "Meeting" |
| 442 | } else if is_one_on_one(chat) { |
| 443 | "Chat" |
| 444 | } else { |
| 445 | "Group" |
| 446 | }) |
| 447 | } |
| 448 | |
| 449 | impl Render for McpView { |
| 450 | fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 451 | v_flex() |
| 452 | .w_full() |
| 453 | .child(caption("SERVER")) |
| 454 | .child(self.server_tab(cx)) |
| 455 | .child(caption("ACCESS")) |
| 456 | .child(self.access_tab(cx)) |
| 457 | .child(caption("INSTALL")) |
| 458 | .child(self.install_tab(cx)) |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | impl McpView { |
| 463 | fn field_row( |
| 464 | &self, |
| 465 | label: &'static str, |
| 466 | value: String, |
| 467 | buttons: Vec<AnyElement>, |
| 468 | cx: &App, |
| 469 | ) -> Div { |
| 470 | h_flex() |
| 471 | .w_full() |
| 472 | .px(px(ROW_INSET)) |
| 473 | .py(px(6.)) |
| 474 | .gap(px(8.)) |
| 475 | .items_center() |
| 476 | .child( |
| 477 | div() |
| 478 | .w(px(FIELD_LABEL_WIDTH)) |
| 479 | .flex_none() |
| 480 | .text_size(px(12.)) |
| 481 | .text_color(theme::text_faint()) |
| 482 | .child(label), |
| 483 | ) |
| 484 | .child( |
| 485 | div() |
| 486 | .flex_1() |
| 487 | .min_w_0() |
| 488 | .h(px(FIELD_HEIGHT)) |
| 489 | .px(px(10.)) |
| 490 | .flex() |
| 491 | .items_center() |
| 492 | .rounded(px(7.)) |
| 493 | .bg(theme::surface_raised()) |
| 494 | .font_family(cx.theme().mono_font_family.clone()) |
| 495 | .text_size(px(12.)) |
| 496 | .text_color(theme::text_soft()) |
| 497 | .child(div().truncate().child(value)), |
| 498 | ) |
| 499 | .children(buttons) |
| 500 | } |
| 501 | |
| 502 | fn copy_button( |
| 503 | &self, |
| 504 | id: &'static str, |
| 505 | target: Copied, |
| 506 | text: String, |
| 507 | cx: &mut Context<Self>, |
| 508 | ) -> AnyElement { |
| 509 | let label = if self.copied == Some(target) { |
| 510 | "Copied" |
| 511 | } else { |
| 512 | "Copy" |
| 513 | }; |
| 514 | small_button(id, label) |
| 515 | .on_click(cx.listener(move |this, _, _, cx| this.copy(target, text.clone(), cx))) |
| 516 | .into_any_element() |
| 517 | } |
| 518 | |
| 519 | fn server_tab(&self, cx: &mut Context<Self>) -> Div { |
| 520 | let policy = agent_api::policy(cx); |
| 521 | let (status, token) = match agent_api::handle(cx) { |
| 522 | Some(api) => { |
| 523 | let api = api.read(cx); |
| 524 | (api.status().clone(), api.token().to_owned()) |
| 525 | } |
| 526 | None => (ServerStatus::Off, String::new()), |
| 527 | }; |
| 528 | let failed = matches!(status, ServerStatus::PortInUse(_) | ServerStatus::Failed(_)); |
| 529 | let (detail, detail_color) = match (policy.enabled, failed) { |
| 530 | (false, _) => ("Off - agents cannot reach Rusty Teams", theme::text_muted()), |
| 531 | (true, true) => ("Could not start", theme::red_soft()), |
| 532 | (true, false) => ( |
| 533 | "Agents on this computer can read and write", |
| 534 | theme::text_muted(), |
| 535 | ), |
| 536 | }; |
| 537 | let enabled = policy.enabled; |
| 538 | let running = matches!(status, ServerStatus::Running(_)); |
| 539 | let reinstall_note = |
| 540 | agent_api::handle(cx).and_then(|api| api.read(cx).reinstall_note().map(str::to_owned)); |
| 541 | let warning = match &status { |
| 542 | ServerStatus::PortInUse(port) => Some(format!( |
| 543 | "Port {port} is in use by another program. Pick another port, then press Install again for each agent." |
| 544 | )), |
| 545 | ServerStatus::Failed(reason) => Some(reason.clone()), |
| 546 | _ => None, |
| 547 | } |
| 548 | .or(reinstall_note); |
| 549 | let url = agent_api::endpoint_url(policy.port); |
| 550 | let regenerate = small_button("mcp-token-new", "New") |
| 551 | .on_click(cx.listener(|this, _, _, cx| { |
| 552 | if let Some(api) = agent_api::handle(cx) { |
| 553 | api.update(cx, |api, cx| api.regenerate_token(cx)); |
| 554 | this.refresh_installs(cx); |
| 555 | } |
| 556 | })) |
| 557 | .into_any_element(); |
| 558 | v_flex() |
| 559 | .w_full() |
| 560 | .child( |
| 561 | toggle_row("mcp-enabled", "MCP server", detail, detail_color, enabled).on_click( |
| 562 | cx.listener(move |_, _, _, cx| { |
| 563 | agent_api::update_policy(cx, |policy| policy.enabled = !enabled) |
| 564 | }), |
| 565 | ), |
| 566 | ) |
| 567 | .children(warning.map(|text| { |
| 568 | note_box( |
| 569 | h_flex() |
| 570 | .gap(px(8.)) |
| 571 | .items_start() |
| 572 | .child( |
| 573 | div() |
| 574 | .font_weight(FontWeight::BOLD) |
| 575 | .text_color(theme::red_soft()) |
| 576 | .child("!"), |
| 577 | ) |
| 578 | .child(div().flex_1().child(text)), |
| 579 | 4., |
| 580 | ) |
| 581 | })) |
| 582 | .child(control_row( |
| 583 | "Port", |
| 584 | "Only this computer can connect", |
| 585 | div() |
| 586 | .w(px(PORT_WIDTH)) |
| 587 | .flex_none() |
| 588 | .child(Input::new(&self.port_input)), |
| 589 | )) |
| 590 | .when(self.port_invalid, |tab| { |
| 591 | tab.child( |
| 592 | div() |
| 593 | .px(px(ROW_INSET)) |
| 594 | .pb(px(4.)) |
| 595 | .text_size(px(12.)) |
| 596 | .text_color(theme::red_soft()) |
| 597 | .child("Enter a port from 1 to 65535. Nothing was saved."), |
| 598 | ) |
| 599 | }) |
| 600 | .when(running && self.wsl_unreachable, |tab| { |
| 601 | tab.child(wsl_warning()) |
| 602 | }) |
| 603 | .child(self.field_row( |
| 604 | "URL", |
| 605 | url.clone(), |
| 606 | vec![self.copy_button("mcp-url-copy", Copied::Url, url, cx)], |
| 607 | cx, |
| 608 | )) |
| 609 | .child(self.field_row( |
| 610 | "Token", |
| 611 | masked_token(&token), |
| 612 | vec![ |
| 613 | self.copy_button("mcp-token-copy", Copied::Token, token, cx), |
| 614 | regenerate, |
| 615 | ], |
| 616 | cx, |
| 617 | )) |
| 618 | } |
| 619 | |
| 620 | fn segmented(&self, scope: Scope, access: &Access, cx: &mut Context<Self>) -> Div { |
| 621 | let options: [(&'static str, bool, fn() -> Access); 3] = [ |
| 622 | ("All", matches!(access, Access::All), || Access::All), |
| 623 | ("Selected", matches!(access, Access::Selected(_)), || { |
| 624 | Access::Selected(BTreeSet::new()) |
| 625 | }), |
| 626 | ("None", matches!(access, Access::None), || Access::None), |
| 627 | ]; |
| 628 | h_flex() |
| 629 | .flex_none() |
| 630 | .p(px(2.)) |
| 631 | .rounded(px(8.)) |
| 632 | .bg(theme::surface_raised()) |
| 633 | .children(options.map(|(label, active, make)| { |
| 634 | div() |
| 635 | .id(SharedString::from(format!("{}-{label}", scope.title()))) |
| 636 | .h(px(24.)) |
| 637 | .px(px(10.)) |
| 638 | .flex() |
| 639 | .items_center() |
| 640 | .rounded(px(6.)) |
| 641 | .cursor_pointer() |
| 642 | .text_size(px(12.)) |
| 643 | .font_weight(FontWeight::SEMIBOLD) |
| 644 | .text_color(if active { |
| 645 | theme::text() |
| 646 | } else { |
| 647 | theme::text_muted() |
| 648 | }) |
| 649 | .when(active, |item| item.bg(theme::popup_hover())) |
| 650 | .child(label) |
| 651 | .on_click(cx.listener(move |this, _, _, cx| { |
| 652 | if !active { |
| 653 | this.picker = None; |
| 654 | agent_api::update_policy(cx, |policy| { |
| 655 | *scope.access_mut(policy) = make() |
| 656 | }); |
| 657 | } |
| 658 | })) |
| 659 | })) |
| 660 | } |
| 661 | |
| 662 | fn scope_detail(scope: Scope, access: &Access) -> String { |
| 663 | match (scope, access) { |
| 664 | (Scope::Chats, Access::All) => "All chats".to_owned(), |
| 665 | (Scope::Channels, Access::All) => "All channels".to_owned(), |
| 666 | (Scope::Chats, Access::None) => "No chat visible".to_owned(), |
| 667 | (Scope::Channels, Access::None) => "No channel visible".to_owned(), |
| 668 | (Scope::Chats, Access::Selected(ids)) => { |
| 669 | format!("{} selected, incl. meeting chats", ids.len()) |
| 670 | } |
| 671 | (Scope::Channels, Access::Selected(ids)) => format!("{} selected", ids.len()), |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | fn scope_block( |
| 676 | &self, |
| 677 | scope: Scope, |
| 678 | policy: &AgentPolicy, |
| 679 | titles: &HashMap<String, String>, |
| 680 | cx: &mut Context<Self>, |
| 681 | ) -> Div { |
| 682 | let access = scope.access(policy); |
| 683 | let header = control_row( |
| 684 | scope.title(), |
| 685 | Self::scope_detail(scope, access), |
| 686 | self.segmented(scope, access, cx), |
| 687 | ); |
| 688 | let Access::Selected(ids) = access else { |
| 689 | return v_flex().w_full().child(header); |
| 690 | }; |
| 691 | let chips = ids.iter().map(|conversation_id| { |
| 692 | let title = titles |
| 693 | .get(conversation_id) |
| 694 | .cloned() |
| 695 | .unwrap_or_else(|| "Unknown".to_owned()); |
| 696 | let conversation_id = conversation_id.clone(); |
| 697 | h_flex() |
| 698 | .h(px(24.)) |
| 699 | .pl(px(8.)) |
| 700 | .gap(px(4.)) |
| 701 | .items_center() |
| 702 | .rounded(px(6.)) |
| 703 | .bg(theme::surface_raised()) |
| 704 | .text_size(px(12.)) |
| 705 | .text_color(theme::text_strong()) |
| 706 | .child(div().max_w(px(CHIP_MAX_WIDTH)).truncate().child(title)) |
| 707 | .child( |
| 708 | div() |
| 709 | .id(SharedString::from(format!("remove-{conversation_id}"))) |
| 710 | .w(px(16.)) |
| 711 | .h_full() |
| 712 | .flex() |
| 713 | .items_center() |
| 714 | .justify_center() |
| 715 | .cursor_pointer() |
| 716 | .child(icon(IconName::X, 10., theme::text_faint())) |
| 717 | .on_click(cx.listener(move |_, _, _, cx| { |
| 718 | let conversation_id = conversation_id.clone(); |
| 719 | agent_api::update_policy(cx, |policy| { |
| 720 | if let Access::Selected(ids) = scope.access_mut(policy) { |
| 721 | ids.remove(&conversation_id); |
| 722 | } |
| 723 | }) |
| 724 | })), |
| 725 | ) |
| 726 | }); |
| 727 | let add = div() |
| 728 | .id(SharedString::from(format!("add-{}", scope.title()))) |
| 729 | .h(px(24.)) |
| 730 | .px(px(8.)) |
| 731 | .flex() |
| 732 | .items_center() |
| 733 | .rounded(px(6.)) |
| 734 | .border_1() |
| 735 | .border_color(theme::border_strong()) |
| 736 | .cursor_pointer() |
| 737 | .text_size(px(12.)) |
| 738 | .text_color(theme::accent_text()) |
| 739 | .child(scope.add_label()) |
| 740 | .on_click(cx.listener(move |this, _, window, cx| this.open_picker(scope, window, cx))); |
| 741 | v_flex() |
| 742 | .w_full() |
| 743 | .child(header) |
| 744 | .child( |
| 745 | h_flex() |
| 746 | .w_full() |
| 747 | .flex_wrap() |
| 748 | .gap(px(6.)) |
| 749 | .px(px(ROW_INSET)) |
| 750 | .pb(px(8.)) |
| 751 | .children(chips) |
| 752 | .child(add), |
| 753 | ) |
| 754 | .children( |
| 755 | self.picker |
| 756 | .filter(|open| *open == scope) |
| 757 | .map(|_| self.picker_panel(scope, ids, cx)), |
| 758 | ) |
| 759 | } |
| 760 | |
| 761 | fn picker_panel( |
| 762 | &self, |
| 763 | scope: Scope, |
| 764 | selected: &BTreeSet<String>, |
| 765 | cx: &mut Context<Self>, |
| 766 | ) -> Div { |
| 767 | let query = self.search_input.read(cx).value().to_string(); |
| 768 | let state = self.app.read(cx); |
| 769 | let candidates: Vec<(String, Candidate)> = candidates_from(state) |
| 770 | .into_iter() |
| 771 | .filter(|(_, candidate)| scope.contains(candidate)) |
| 772 | .collect(); |
| 773 | let results = fuzzy::rank(&query, &candidates, PICKER_RESULTS); |
| 774 | let rows: Vec<AnyElement> = results |
| 775 | .into_iter() |
| 776 | .map(|candidate| { |
| 777 | let conversation_id = candidate.selection.conversation_id().to_owned(); |
| 778 | let added = selected.contains(&conversation_id); |
| 779 | let kind = match scope { |
| 780 | Scope::Chats => chat_kind_label(state, &conversation_id) |
| 781 | .unwrap_or("Chat") |
| 782 | .to_owned(), |
| 783 | Scope::Channels => candidate.subtitle.clone(), |
| 784 | }; |
| 785 | h_flex() |
| 786 | .id(SharedString::from(format!("pick-{conversation_id}"))) |
| 787 | .h(px(PICKER_ROW_HEIGHT)) |
| 788 | .flex_none() |
| 789 | .px(px(6.)) |
| 790 | .gap(px(8.)) |
| 791 | .items_center() |
| 792 | .rounded(px(6.)) |
| 793 | .cursor_pointer() |
| 794 | .hover(|row| row.bg(theme::popup_hover())) |
| 795 | .child(candidate_avatar( |
| 796 | &state.directory, |
| 797 | &candidate.avatar, |
| 798 | 20., |
| 799 | theme::popup(), |
| 800 | )) |
| 801 | .child( |
| 802 | div() |
| 803 | .flex_1() |
| 804 | .min_w_0() |
| 805 | .truncate() |
| 806 | .text_size(px(13.)) |
| 807 | .text_color(theme::text()) |
| 808 | .child(candidate.title.clone()), |
| 809 | ) |
| 810 | .child( |
| 811 | div() |
| 812 | .flex_none() |
| 813 | .text_size(px(11.)) |
| 814 | .text_color(if added { |
| 815 | theme::accent_text() |
| 816 | } else { |
| 817 | theme::text_faint() |
| 818 | }) |
| 819 | .child(if added { "Added".to_owned() } else { kind }), |
| 820 | ) |
| 821 | .on_click(cx.listener(move |this, _, _, cx| { |
| 822 | let conversation_id = conversation_id.clone(); |
| 823 | this.picker = None; |
| 824 | agent_api::update_policy(cx, |policy| { |
| 825 | if let Access::Selected(ids) = scope.access_mut(policy) { |
| 826 | ids.insert(conversation_id); |
| 827 | } |
| 828 | }); |
| 829 | })) |
| 830 | .into_any_element() |
| 831 | }) |
| 832 | .collect(); |
| 833 | v_flex() |
| 834 | .mx(px(ROW_INSET)) |
| 835 | .mb(px(8.)) |
| 836 | .p(px(6.)) |
| 837 | .gap(px(4.)) |
| 838 | .rounded(px(8.)) |
| 839 | .bg(theme::surface_raised()) |
| 840 | .child( |
| 841 | h_flex() |
| 842 | .h(px(30.)) |
| 843 | .px(px(6.)) |
| 844 | .gap(px(6.)) |
| 845 | .items_center() |
| 846 | .child(icon(IconName::Search, 14., theme::text_muted())) |
| 847 | .child( |
| 848 | div().flex_1().child( |
| 849 | Input::new(&self.search_input) |
| 850 | .appearance(false) |
| 851 | .bordered(false), |
| 852 | ), |
| 853 | ), |
| 854 | ) |
| 855 | .child( |
| 856 | v_flex() |
| 857 | .id("mcp-picker-list") |
| 858 | .w_full() |
| 859 | .max_h(px(PICKER_ROW_HEIGHT * PICKER_MAX_ROWS as f32)) |
| 860 | .overflow_y_scrollbar() |
| 861 | .children(rows), |
| 862 | ) |
| 863 | } |
| 864 | |
| 865 | fn access_tab(&self, cx: &mut Context<Self>) -> Div { |
| 866 | let policy = agent_api::policy(cx); |
| 867 | let titles: HashMap<String, String> = candidates_from(self.app.read(cx)) |
| 868 | .into_iter() |
| 869 | .map(|(label, candidate)| (candidate.selection.conversation_id().to_owned(), label)) |
| 870 | .collect(); |
| 871 | let toggles: [( |
| 872 | &'static str, |
| 873 | &'static str, |
| 874 | &'static str, |
| 875 | Hsla, |
| 876 | bool, |
| 877 | fn(&mut AgentPolicy), |
| 878 | ); 5] = [ |
| 879 | ( |
| 880 | "mcp-write", |
| 881 | "Write messages and reactions", |
| 882 | "Agents can answer in the chats and channels above", |
| 883 | theme::text_muted(), |
| 884 | policy.write, |
| 885 | |policy| policy.write = !policy.write, |
| 886 | ), |
| 887 | ( |
| 888 | "mcp-review", |
| 889 | "Review before sending", |
| 890 | if policy.review { |
| 891 | "Card above the composer, note under a message. You decide" |
| 892 | } else { |
| 893 | "Off: agents send and react right away, as you" |
| 894 | }, |
| 895 | if policy.review { |
| 896 | theme::text_muted() |
| 897 | } else { |
| 898 | theme::amber() |
| 899 | }, |
| 900 | policy.review, |
| 901 | |policy| policy.review = !policy.review, |
| 902 | ), |
| 903 | ( |
| 904 | "mcp-calendar", |
| 905 | "Calendar and meetings", |
| 906 | "Times, titles, organizers, join links", |
| 907 | theme::text_muted(), |
| 908 | policy.calendar, |
| 909 | |policy| policy.calendar = !policy.calendar, |
| 910 | ), |
| 911 | ( |
| 912 | "mcp-meetings", |
| 913 | "Organize meetings", |
| 914 | if policy.review { |
| 915 | "Free/busy of people, meeting card in Calendar. You create it" |
| 916 | } else { |
| 917 | "Without review: agents create meetings and invite people right away" |
| 918 | }, |
| 919 | if policy.review { |
| 920 | theme::text_muted() |
| 921 | } else { |
| 922 | theme::amber() |
| 923 | }, |
| 924 | policy.meetings, |
| 925 | |policy| policy.meetings = !policy.meetings, |
| 926 | ), |
| 927 | ( |
| 928 | "mcp-transcripts", |
| 929 | "Meeting transcripts", |
| 930 | "Spoken text goes to the agent's provider", |
| 931 | theme::amber(), |
| 932 | policy.transcripts, |
| 933 | |policy| policy.transcripts = !policy.transcripts, |
| 934 | ), |
| 935 | ]; |
| 936 | v_flex() |
| 937 | .w_full() |
| 938 | .child(self.scope_block(Scope::Chats, &policy, &titles, cx)) |
| 939 | .child(self.scope_block(Scope::Channels, &policy, &titles, cx)) |
| 940 | .children(toggles.map(|(id, title, detail, color, on, flip)| { |
| 941 | toggle_row(id, title, detail, color, on) |
| 942 | .on_click(cx.listener(move |_, _, _, cx| agent_api::update_policy(cx, flip))) |
| 943 | })) |
| 944 | .child(note_box( |
| 945 | if policy.review { |
| 946 | "Agents only see what is allowed here. They never send - you do." |
| 947 | } else { |
| 948 | "Agents only see what is allowed here. Without review they send as you." |
| 949 | }, |
| 950 | 4., |
| 951 | )) |
| 952 | } |
| 953 | |
| 954 | fn install_row(&self, index: usize, target: Target, cx: &mut Context<Self>) -> Div { |
| 955 | let state = self |
| 956 | .install_states |
| 957 | .as_ref() |
| 958 | .and_then(|states| states.get(index)); |
| 959 | let busy = self.install_busy; |
| 960 | let action = state.and_then(target_action); |
| 961 | let button = match (busy, action) { |
| 962 | (Some((busy_target, busy_action)), _) if busy_target == target => { |
| 963 | inert_button(target.label(), busy_action.busy_label()).into_any_element() |
| 964 | } |
| 965 | (None, Some(action)) => small_button(target.label(), action.label()) |
| 966 | .on_click( |
| 967 | cx.listener(move |this, _, _, cx| this.run_install_action(target, action, cx)), |
| 968 | ) |
| 969 | .into_any_element(), |
| 970 | _ => inert_button(target.label(), InstallAction::Install.label()).into_any_element(), |
| 971 | }; |
| 972 | v_flex() |
| 973 | .w_full() |
| 974 | .child( |
| 975 | h_flex() |
| 976 | .w_full() |
| 977 | .px(px(ROW_INSET)) |
| 978 | .py(px(8.)) |
| 979 | .gap(px(12.)) |
| 980 | .items_center() |
| 981 | .child( |
| 982 | div() |
| 983 | .size(px(28.)) |
| 984 | .flex_none() |
| 985 | .flex() |
| 986 | .items_center() |
| 987 | .justify_center() |
| 988 | .rounded(px(7.)) |
| 989 | .bg(theme::surface_raised()) |
| 990 | .text_size(px(11.)) |
| 991 | .font_weight(FontWeight::BOLD) |
| 992 | .text_color(theme::text_soft()) |
| 993 | .child(target_logo(target)), |
| 994 | ) |
| 995 | .child( |
| 996 | v_flex() |
| 997 | .flex_1() |
| 998 | .child( |
| 999 | div() |
| 1000 | .text_size(px(14.)) |
| 1001 | .font_weight(FontWeight::SEMIBOLD) |
| 1002 | .text_color(theme::text()) |
| 1003 | .child(target.label()), |
| 1004 | ) |
| 1005 | .child(install_detail(target, state)), |
| 1006 | ) |
| 1007 | .child(button), |
| 1008 | ) |
| 1009 | .children(self.install_errors.get(&target).map(|message| { |
| 1010 | div() |
| 1011 | .px(px(ROW_INSET)) |
| 1012 | .pb(px(4.)) |
| 1013 | .pl(px(60.)) |
| 1014 | .text_size(px(12.)) |
| 1015 | .text_color(theme::red_soft()) |
| 1016 | .child(message.clone()) |
| 1017 | })) |
| 1018 | } |
| 1019 | |
| 1020 | fn install_tab(&self, cx: &mut Context<Self>) -> Div { |
| 1021 | let rows: Vec<Div> = Target::ALL |
| 1022 | .into_iter() |
| 1023 | .enumerate() |
| 1024 | .map(|(index, target)| self.install_row(index, target, cx)) |
| 1025 | .collect(); |
| 1026 | v_flex().w_full().children(rows) |
| 1027 | } |
| 1028 | } |