BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / settings / mcp.rs

mcp.rs

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