BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / notify / settings_mcp.rs

settings_mcp.rs

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