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