| 1 | use gpui_kit::assets::IconName; |
| 2 | use gpui_kit::component::button::{Button, ButtonVariants as _}; |
| 3 | use gpui_kit::component::input::{Input, InputEvent, InputState}; |
| 4 | use gpui_kit::component::scroll::ScrollableElement as _; |
| 5 | use gpui_kit::component::{Selectable as _, h_flex, v_flex}; |
| 6 | use gpui_kit::prelude::FluentBuilder as _; |
| 7 | use gpui_kit::*; |
| 8 | |
| 9 | use super::ConversationView; |
| 10 | use crate::app_state::Selection; |
| 11 | use crate::render::{layout_card_blocks, render_blocks}; |
| 12 | use crate::runtime; |
| 13 | use crate::theme; |
| 14 | use crate::views::scroll_edge::with_top_edge; |
| 15 | |
| 16 | pub const COPILOT_PANE_TAG: &str = "shell/islands/copilot"; |
| 17 | const COPILOT_SCROLL_DIVIDER: &str = "shell/islands/copilot/divider-scroll"; |
| 18 | const TITLE: &str = "Copilot"; |
| 19 | const OPEN_LABEL: &str = "Open Copilot"; |
| 20 | const CLOSE_LABEL: &str = "Close Copilot"; |
| 21 | const PLACEHOLDER: &str = "Message Copilot"; |
| 22 | const WORKING: &str = "Copilot is reading the chat ..."; |
| 23 | const UNAVAILABLE: &str = "Copilot needs a signed-in account."; |
| 24 | const FAILED: &str = "Copilot could not answer:"; |
| 25 | const GREETING: &str = "Hi, how can I help?"; |
| 26 | const LOGO_ASSET: &str = "symbols/copilot_color.svg"; |
| 27 | const LOGO_BUTTON: f32 = 18.; |
| 28 | const LOGO_TITLE: f32 = 20.; |
| 29 | const INPUT_MIN_HEIGHT: f32 = 72.; |
| 30 | const INPUT_RADIUS: f32 = 12.; |
| 31 | const CHIP_RADIUS: f32 = 8.; |
| 32 | const SUGGESTIONS: [&str; 3] = [ |
| 33 | "Summarize this chat", |
| 34 | "What are open items?", |
| 35 | "What decisions were made?", |
| 36 | ]; |
| 37 | const PANE_WIDTH: f32 = 400.; |
| 38 | const ISLAND_RADIUS: f32 = 10.; |
| 39 | const PANE_PADDING: f32 = 16.; |
| 40 | const HEADER_HEIGHT: f32 = 48.; |
| 41 | const BUTTON_SIZE: f32 = 32.; |
| 42 | const BUBBLE_RADIUS: f32 = 8.; |
| 43 | |
| 44 | #[derive(Default)] |
| 45 | pub(super) struct CopilotPane { |
| 46 | open: bool, |
| 47 | turns: Vec<CopilotTurn>, |
| 48 | input: Option<Entity<InputState>>, |
| 49 | scroll: ScrollHandle, |
| 50 | generation: u64, |
| 51 | _input_events: Option<Subscription>, |
| 52 | } |
| 53 | |
| 54 | struct CopilotTurn { |
| 55 | prompt: String, |
| 56 | answer: CopilotAnswer, |
| 57 | } |
| 58 | |
| 59 | enum CopilotAnswer { |
| 60 | Waiting, |
| 61 | Ready(String), |
| 62 | Failed(String), |
| 63 | } |
| 64 | |
| 65 | impl ConversationView { |
| 66 | fn copilot_chat_id(&self) -> Option<String> { |
| 67 | match &self.current.as_ref()?.selection { |
| 68 | Selection::Chat(chat_id) => Some(chat_id.clone()), |
| 69 | Selection::Channel(_) => None, |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | pub(super) fn copilot_label(&self) -> &'static str { |
| 74 | if self.copilot.open { CLOSE_LABEL } else { OPEN_LABEL } |
| 75 | } |
| 76 | |
| 77 | pub(super) fn copilot_button(&self, cx: &mut Context<Self>) -> Option<AnyElement> { |
| 78 | self.copilot_chat_id()?; |
| 79 | let open = self.copilot.open; |
| 80 | Some( |
| 81 | Button::new("copilot-toggle") |
| 82 | .child(copilot_logo(LOGO_BUTTON)) |
| 83 | .ghost() |
| 84 | .size(px(BUTTON_SIZE)) |
| 85 | .rounded(px(8.)) |
| 86 | .when(open, |button| button.selected(true)) |
| 87 | .tooltip(if open { CLOSE_LABEL } else { OPEN_LABEL }) |
| 88 | .on_click(cx.listener(|this, _, window, cx| this.toggle_copilot(window, cx))) |
| 89 | .into_any_element(), |
| 90 | ) |
| 91 | } |
| 92 | |
| 93 | pub(super) fn toggle_copilot(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 94 | self.copilot.open = !self.copilot.open; |
| 95 | if self.copilot.open && self.copilot.input.is_none() { |
| 96 | let input = cx.new(|cx| InputState::new(window, cx).placeholder(PLACEHOLDER)); |
| 97 | let subscription = cx.subscribe_in(&input, window, |this, input, event: &InputEvent, window, cx| { |
| 98 | if matches!(event, InputEvent::PressEnter { .. }) { |
| 99 | let prompt = input.read(cx).value().trim().to_owned(); |
| 100 | if !prompt.is_empty() { |
| 101 | input.update(cx, |input, cx| input.set_value("", window, cx)); |
| 102 | this.ask_copilot(prompt, cx); |
| 103 | } |
| 104 | } |
| 105 | }); |
| 106 | self.copilot.input = Some(input); |
| 107 | self.copilot._input_events = Some(subscription); |
| 108 | } |
| 109 | if self.copilot.open { |
| 110 | self.channel_details_open = false; |
| 111 | self.find_panel = None; |
| 112 | self.find_subscription = None; |
| 113 | if let Some(input) = &self.copilot.input { |
| 114 | input.update(cx, |input, cx| input.focus(window, cx)); |
| 115 | } |
| 116 | } |
| 117 | cx.notify(); |
| 118 | } |
| 119 | |
| 120 | pub(super) fn close_copilot(&mut self) { |
| 121 | self.copilot.open = false; |
| 122 | } |
| 123 | |
| 124 | pub(super) fn reset_copilot(&mut self) { |
| 125 | let input = self.copilot.input.take(); |
| 126 | let events = self.copilot._input_events.take(); |
| 127 | self.copilot = CopilotPane { |
| 128 | input, |
| 129 | generation: self.copilot.generation + 1, |
| 130 | _input_events: events, |
| 131 | ..CopilotPane::default() |
| 132 | }; |
| 133 | } |
| 134 | |
| 135 | fn ask_copilot(&mut self, prompt: String, cx: &mut Context<Self>) { |
| 136 | let Some(chat_id) = self.copilot_chat_id() else { |
| 137 | return; |
| 138 | }; |
| 139 | let index = self.copilot.turns.len(); |
| 140 | let generation = self.copilot.generation; |
| 141 | self.copilot.turns.push(CopilotTurn { |
| 142 | prompt: prompt.clone(), |
| 143 | answer: CopilotAnswer::Waiting, |
| 144 | }); |
| 145 | self.copilot.scroll.scroll_to_bottom(); |
| 146 | cx.notify(); |
| 147 | let Some(engine) = self.app.read(cx).engine.clone() else { |
| 148 | self.settle_copilot(generation, index, Err(UNAVAILABLE.to_owned()), cx); |
| 149 | return; |
| 150 | }; |
| 151 | let receiver = runtime::spawn(async move { engine.ask_copilot(&chat_id, &prompt).await }); |
| 152 | cx.spawn(async move |this, cx| { |
| 153 | let answer = match receiver.await { |
| 154 | Ok(Ok(text)) => Ok(text), |
| 155 | Ok(Err(error)) => Err(error.to_string()), |
| 156 | Err(error) => Err(error.to_string()), |
| 157 | }; |
| 158 | this.update(cx, |this, cx| this.settle_copilot(generation, index, answer, cx)) |
| 159 | .ok(); |
| 160 | }) |
| 161 | .detach(); |
| 162 | } |
| 163 | |
| 164 | fn settle_copilot( |
| 165 | &mut self, |
| 166 | generation: u64, |
| 167 | index: usize, |
| 168 | answer: Result<String, String>, |
| 169 | cx: &mut Context<Self>, |
| 170 | ) { |
| 171 | if generation != self.copilot.generation { |
| 172 | return; |
| 173 | } |
| 174 | let Some(turn) = self.copilot.turns.get_mut(index) else { |
| 175 | return; |
| 176 | }; |
| 177 | turn.answer = match answer { |
| 178 | Ok(text) => CopilotAnswer::Ready(text), |
| 179 | Err(error) => CopilotAnswer::Failed(format!( |
| 180 | "{FAILED} {}", |
| 181 | error.split(" (https://").next().unwrap_or_default() |
| 182 | )), |
| 183 | }; |
| 184 | self.copilot.scroll.scroll_to_bottom(); |
| 185 | cx.notify(); |
| 186 | } |
| 187 | |
| 188 | pub(super) fn render_copilot(&self, cx: &mut Context<Self>) -> Option<AnyElement> { |
| 189 | if !self.copilot.open { |
| 190 | return None; |
| 191 | } |
| 192 | self.copilot_chat_id()?; |
| 193 | let busy = self |
| 194 | .copilot |
| 195 | .turns |
| 196 | .last() |
| 197 | .is_some_and(|turn| matches!(turn.answer, CopilotAnswer::Waiting)); |
| 198 | let input_box = self.copilot.input.as_ref().map(|input| { |
| 199 | div() |
| 200 | .w_full() |
| 201 | .min_h(px(INPUT_MIN_HEIGHT)) |
| 202 | .px(px(12.)) |
| 203 | .py(px(10.)) |
| 204 | .rounded(px(INPUT_RADIUS)) |
| 205 | .bg(theme::surface_raised()) |
| 206 | .child(Input::new(input).appearance(false).bordered(false)) |
| 207 | }); |
| 208 | let content = if self.copilot.turns.is_empty() { |
| 209 | let suggestions = v_flex().w_full().gap(px(8.)).children( |
| 210 | SUGGESTIONS.iter().enumerate().map(|(index, prompt)| { |
| 211 | div() |
| 212 | .id(("copilot-suggestion", index)) |
| 213 | .w_full() |
| 214 | .px(px(12.)) |
| 215 | .py(px(10.)) |
| 216 | .rounded(px(CHIP_RADIUS)) |
| 217 | .bg(theme::surface_raised()) |
| 218 | .text_size(px(12.)) |
| 219 | .font_weight(FontWeight::SEMIBOLD) |
| 220 | .text_color(theme::text()) |
| 221 | .cursor_pointer() |
| 222 | .hover(|chip| chip.bg(theme::row_hover())) |
| 223 | .child(*prompt) |
| 224 | .on_click(cx.listener(move |this, _, _, cx| { |
| 225 | this.ask_copilot((*prompt).to_owned(), cx) |
| 226 | })) |
| 227 | }), |
| 228 | ); |
| 229 | v_flex() |
| 230 | .flex_1() |
| 231 | .min_h_0() |
| 232 | .px(px(PANE_PADDING)) |
| 233 | .pb(px(PANE_PADDING)) |
| 234 | .justify_center() |
| 235 | .gap(px(16.)) |
| 236 | .child( |
| 237 | div() |
| 238 | .px(px(4.)) |
| 239 | .text_size(px(24.)) |
| 240 | .text_color(theme::text()) |
| 241 | .child(GREETING), |
| 242 | ) |
| 243 | .children(input_box) |
| 244 | .child(suggestions) |
| 245 | .into_any_element() |
| 246 | } else { |
| 247 | let mut body = v_flex() |
| 248 | .w_full() |
| 249 | .px(px(PANE_PADDING)) |
| 250 | .pb(px(PANE_PADDING)) |
| 251 | .gap(px(12.)); |
| 252 | for (index, turn) in self.copilot.turns.iter().enumerate() { |
| 253 | body = body |
| 254 | .child(prompt_bubble(&turn.prompt)) |
| 255 | .child(answer_view(index, &turn.answer, cx)); |
| 256 | } |
| 257 | v_flex() |
| 258 | .flex_1() |
| 259 | .min_h_0() |
| 260 | .child(with_top_edge( |
| 261 | div() |
| 262 | .id("copilot-body") |
| 263 | .flex_1() |
| 264 | .min_h_0() |
| 265 | .overflow_y_scrollbar() |
| 266 | .track_scroll(&self.copilot.scroll) |
| 267 | .child(body), |
| 268 | &self.copilot.scroll, |
| 269 | COPILOT_SCROLL_DIVIDER, |
| 270 | )) |
| 271 | .child( |
| 272 | div() |
| 273 | .flex_none() |
| 274 | .px(px(PANE_PADDING)) |
| 275 | .pb(px(PANE_PADDING)) |
| 276 | .when(busy, |footer| footer.opacity(0.6)) |
| 277 | .children(input_box), |
| 278 | ) |
| 279 | .into_any_element() |
| 280 | }; |
| 281 | Some( |
| 282 | h_flex() |
| 283 | .h_full() |
| 284 | .flex_none() |
| 285 | .child( |
| 286 | v_flex() |
| 287 | .debug_selector(|| COPILOT_PANE_TAG.into()) |
| 288 | .w(px(PANE_WIDTH)) |
| 289 | .h_full() |
| 290 | .rounded(px(ISLAND_RADIUS)) |
| 291 | .overflow_hidden() |
| 292 | .bg(theme::surface()) |
| 293 | .child( |
| 294 | h_flex() |
| 295 | .h(px(HEADER_HEIGHT)) |
| 296 | .flex_none() |
| 297 | .px(px(PANE_PADDING)) |
| 298 | .items_center() |
| 299 | .justify_between() |
| 300 | .child( |
| 301 | h_flex() |
| 302 | .gap(px(8.)) |
| 303 | .items_center() |
| 304 | .child(copilot_logo(LOGO_TITLE)) |
| 305 | .child( |
| 306 | div() |
| 307 | .text_size(px(15.)) |
| 308 | .font_weight(FontWeight::SEMIBOLD) |
| 309 | .text_color(theme::text()) |
| 310 | .child(TITLE), |
| 311 | ), |
| 312 | ) |
| 313 | .child( |
| 314 | Button::new("copilot-close") |
| 315 | .icon(IconName::X) |
| 316 | .ghost() |
| 317 | .size(px(BUTTON_SIZE)) |
| 318 | .rounded(px(8.)) |
| 319 | .tooltip(CLOSE_LABEL) |
| 320 | .on_click(cx.listener(|this, _, _, cx| { |
| 321 | this.copilot.open = false; |
| 322 | cx.notify(); |
| 323 | })), |
| 324 | ), |
| 325 | ) |
| 326 | .child(content), |
| 327 | ) |
| 328 | .into_any_element(), |
| 329 | ) |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | fn copilot_logo(size: f32) -> Img { |
| 334 | img(LOGO_ASSET).size(px(size)).flex_none() |
| 335 | } |
| 336 | |
| 337 | fn prompt_bubble(prompt: &str) -> Div { |
| 338 | h_flex().w_full().justify_end().child( |
| 339 | div() |
| 340 | .max_w(px(PANE_WIDTH * 0.8)) |
| 341 | .px(px(10.)) |
| 342 | .py(px(6.)) |
| 343 | .rounded(px(BUBBLE_RADIUS)) |
| 344 | .bg(theme::row_hover()) |
| 345 | .text_size(px(13.)) |
| 346 | .text_color(theme::text()) |
| 347 | .child(prompt.to_owned()), |
| 348 | ) |
| 349 | } |
| 350 | |
| 351 | fn answer_view(index: usize, answer: &CopilotAnswer, cx: &App) -> AnyElement { |
| 352 | match answer { |
| 353 | CopilotAnswer::Waiting => div() |
| 354 | .text_size(px(13.)) |
| 355 | .text_color(theme::text_muted()) |
| 356 | .child(WORKING) |
| 357 | .into_any_element(), |
| 358 | CopilotAnswer::Failed(error) => div() |
| 359 | .text_size(px(13.)) |
| 360 | .text_color(theme::red()) |
| 361 | .child(error.clone()) |
| 362 | .into_any_element(), |
| 363 | CopilotAnswer::Ready(text) => { |
| 364 | let spans = teams_core::html_to_spans(&teams_core::markdown_to_html(text)); |
| 365 | div() |
| 366 | .w_full() |
| 367 | .text_size(px(13.)) |
| 368 | .child(render_blocks( |
| 369 | &layout_card_blocks(&spans), |
| 370 | &format!("copilot-answer-{index}"), |
| 371 | false, |
| 372 | cx, |
| 373 | )) |
| 374 | .into_any_element() |
| 375 | } |
| 376 | } |
| 377 | } |