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