| 1 | use std::rc::Rc; |
| 2 | |
| 3 | use chrono::Utc; |
| 4 | use gpui_kit::*; |
| 5 | use mcp::Outcome; |
| 6 | use teams_core::Draft; |
| 7 | |
| 8 | use super::ConversationView; |
| 9 | use crate::agent_api::{self, ProposalEntry}; |
| 10 | use crate::views::composer::{ComposerEvent, Outgoing}; |
| 11 | use crate::views::proposal_card::{self, CardAction, CardActions}; |
| 12 | |
| 13 | const QUOTE_CHARS: usize = 80; |
| 14 | |
| 15 | impl ConversationView { |
| 16 | pub(super) fn render_proposal_card(&self, cx: &mut Context<Self>) -> Option<AnyElement> { |
| 17 | if self.draft_active { |
| 18 | return None; |
| 19 | } |
| 20 | let conversation_id = self.conversation_id()?; |
| 21 | let entry = agent_api::proposal(&conversation_id, cx)?; |
| 22 | let quote = entry |
| 23 | .proposal |
| 24 | .reply_to_message_id |
| 25 | .as_deref() |
| 26 | .and_then(|message_id| self.reply_target(message_id, cx)) |
| 27 | .map(|(_, preview)| { |
| 28 | let excerpt: String = preview.excerpt.chars().take(QUOTE_CHARS).collect(); |
| 29 | format!("{}: {excerpt}", preview.author) |
| 30 | }); |
| 31 | let view = cx.entity(); |
| 32 | let action = |handler: fn(&mut Self, &mut Window, &mut Context<Self>)| -> CardAction { |
| 33 | let view = view.clone(); |
| 34 | Rc::new(move |window, cx| view.update(cx, |this, cx| handler(this, window, cx))) |
| 35 | }; |
| 36 | let actions = CardActions { |
| 37 | send: action(Self::send_proposal), |
| 38 | edit: action(Self::edit_proposal), |
| 39 | discard: action(Self::discard_proposal), |
| 40 | }; |
| 41 | Some(proposal_card::render(&entry, quote, actions, Utc::now(), cx).into_any_element()) |
| 42 | } |
| 43 | |
| 44 | pub(super) fn report_composer_activity(&self, event: &ComposerEvent, cx: &App) { |
| 45 | let Some(conversation_id) = self.conversation_id().filter(|_| !self.draft_active) else { |
| 46 | return; |
| 47 | }; |
| 48 | match event { |
| 49 | ComposerEvent::Typing(true) => agent_api::user_typing(&conversation_id, cx), |
| 50 | ComposerEvent::Submit(_) => agent_api::user_sent(&conversation_id, cx), |
| 51 | _ => {} |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | fn pending_proposal(&self, cx: &App) -> Option<(String, ProposalEntry)> { |
| 56 | let conversation_id = self.conversation_id()?; |
| 57 | let entry = agent_api::proposal(&conversation_id, cx)?; |
| 58 | Some((conversation_id, entry)) |
| 59 | } |
| 60 | |
| 61 | fn send_proposal(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 62 | let Some((conversation_id, entry)) = self.pending_proposal(cx) else { |
| 63 | return; |
| 64 | }; |
| 65 | let state = self.app.read(cx); |
| 66 | let refusal = if state.mode.read_only || state.mode.demo { |
| 67 | Some("read-only mode") |
| 68 | } else if state.engine.is_none() { |
| 69 | Some("not connected yet") |
| 70 | } else { |
| 71 | None |
| 72 | }; |
| 73 | if let Some(reason) = refusal { |
| 74 | agent_api::fail_proposal(&conversation_id, reason.to_owned(), cx); |
| 75 | return; |
| 76 | } |
| 77 | let target = entry |
| 78 | .proposal |
| 79 | .reply_to_message_id |
| 80 | .as_deref() |
| 81 | .and_then(|message_id| self.reply_target(message_id, cx)); |
| 82 | let thread_root = target |
| 83 | .as_ref() |
| 84 | .filter(|_| self.in_feed()) |
| 85 | .map(|(record, _)| { |
| 86 | record |
| 87 | .reply_to_id |
| 88 | .clone() |
| 89 | .unwrap_or_else(|| record.message_id.clone()) |
| 90 | }); |
| 91 | let quote = target |
| 92 | .filter(|(record, _)| !self.in_feed() || record.reply_to_id.is_some()) |
| 93 | .map(|(_, preview)| preview); |
| 94 | let outgoing = Outgoing { |
| 95 | draft: Draft::from_markdown(entry.proposal.text.trim()), |
| 96 | subject: None, |
| 97 | mentions: Vec::new(), |
| 98 | reply: quote, |
| 99 | edit: None, |
| 100 | images: Vec::new(), |
| 101 | files: Vec::new(), |
| 102 | link_preview: None, |
| 103 | }; |
| 104 | match thread_root { |
| 105 | Some(root_id) => self.send_in_thread(root_id, outgoing, window, cx), |
| 106 | None => self.send(outgoing, window, cx), |
| 107 | } |
| 108 | agent_api::resolve_proposal(&conversation_id, Outcome::Sent, cx); |
| 109 | } |
| 110 | |
| 111 | fn edit_proposal(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 112 | let Some((conversation_id, entry)) = self.pending_proposal(cx) else { |
| 113 | return; |
| 114 | }; |
| 115 | let reply_to = entry.proposal.reply_to_message_id.as_deref(); |
| 116 | if let Some(message_id) = reply_to { |
| 117 | self.begin_reply(message_id, window, cx); |
| 118 | } else if self.in_feed() { |
| 119 | self.open_new_post(window, cx); |
| 120 | } |
| 121 | let composer = match self.reply_composer.clone() { |
| 122 | Some(reply_composer) if self.in_feed() && reply_to.is_some() => reply_composer, |
| 123 | _ => self.composer.clone(), |
| 124 | }; |
| 125 | composer.update(cx, |composer, cx| { |
| 126 | composer.set_text(&entry.proposal.text, window, cx); |
| 127 | composer.focus(window, cx); |
| 128 | }); |
| 129 | agent_api::resolve_proposal(&conversation_id, Outcome::Edited, cx); |
| 130 | } |
| 131 | |
| 132 | fn discard_proposal(&mut self, _: &mut Window, cx: &mut Context<Self>) { |
| 133 | if let Some(conversation_id) = self.conversation_id() { |
| 134 | agent_api::resolve_proposal(&conversation_id, Outcome::Discarded, cx); |
| 135 | } |
| 136 | } |
| 137 | } |