BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / conversation / proposal.rs

proposal.rs

proposal.rs 8.4 KiB Download Raw
1use std::collections::HashSet;
2use std::rc::Rc;
3
4use chrono::Utc;
5use gpui_kit::*;
6use mcp::{Outcome, ReactionOutcome};
7
8use super::{ConversationView, ReactionAttempt};
9use crate::agent_api::{self, ProposalEntry};
10use crate::rows::reaction_glyph;
11use crate::views::composer::ComposerEvent;
12use crate::views::proposal_card::{
13 self, CardAction, CardActions, needs_upload, outgoing_for, picture_files,
14};
15
16const QUOTE_CHARS: usize = 80;
17
18impl ConversationView {
19 pub(super) fn render_proposal_card(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
20 if self.draft_active {
21 return None;
22 }
23 let conversation_id = self.conversation_id()?;
24 let entry = agent_api::proposal(&conversation_id, cx)?;
25 let quote = entry
26 .proposal
27 .reply_to_message_id
28 .as_deref()
29 .and_then(|message_id| self.reply_target(message_id, cx))
30 .map(|(_, preview)| {
31 let excerpt: String = preview.excerpt.chars().take(QUOTE_CHARS).collect();
32 format!("{}: {excerpt}", preview.author)
33 });
34 let view = cx.entity();
35 let action = |handler: fn(&mut Self, &mut Window, &mut Context<Self>)| -> CardAction {
36 let view = view.clone();
37 Rc::new(move |window, cx| view.update(cx, |this, cx| handler(this, window, cx)))
38 };
39 let actions = CardActions {
40 send: action(Self::send_proposal),
41 edit: action(Self::edit_proposal),
42 discard: action(Self::discard_proposal),
43 };
44 Some(proposal_card::render(&entry, quote, actions, Utc::now(), cx).into_any_element())
45 }
46
47 pub(super) fn report_composer_activity(&self, event: &ComposerEvent, cx: &App) {
48 let Some(conversation_id) = self.conversation_id().filter(|_| !self.draft_active) else {
49 return;
50 };
51 match event {
52 ComposerEvent::Typing(true) => agent_api::user_typing(&conversation_id, cx),
53 ComposerEvent::Submit(_) => agent_api::user_sent(&conversation_id, cx),
54 _ => {}
55 }
56 }
57
58 fn pending_proposal(&self, cx: &App) -> Option<(String, ProposalEntry)> {
59 let conversation_id = self.conversation_id()?;
60 let entry = agent_api::proposal(&conversation_id, cx)?;
61 Some((conversation_id, entry))
62 }
63
64 fn send_proposal(&mut self, window: &mut Window, cx: &mut Context<Self>) {
65 let Some((conversation_id, entry)) = self.pending_proposal(cx) else {
66 return;
67 };
68 let state = self.app.read(cx);
69 let refusal = if state.mode.read_only || state.mode.demo {
70 Some("read-only mode")
71 } else if state.engine.is_none() {
72 Some("not connected yet")
73 } else {
74 None
75 };
76 if let Some(reason) = refusal {
77 agent_api::fail_proposal(&conversation_id, reason.to_owned(), cx);
78 return;
79 }
80 if needs_upload(&entry) {
81 self.show_notice(
82 "Large images upload as files - check the draft and press Send",
83 cx,
84 );
85 self.edit_proposal(window, cx);
86 return;
87 }
88 let target = entry
89 .proposal
90 .reply_to_message_id
91 .as_deref()
92 .and_then(|message_id| self.reply_target(message_id, cx));
93 let thread_root = target
94 .as_ref()
95 .filter(|_| self.in_feed())
96 .map(|(record, _)| {
97 record
98 .reply_to_id
99 .clone()
100 .unwrap_or_else(|| record.message_id.clone())
101 });
102 let quote = target
103 .filter(|(record, _)| !self.in_feed() || record.reply_to_id.is_some())
104 .map(|(_, preview)| preview);
105 let outgoing = outgoing_for(&entry, quote);
106 match thread_root {
107 Some(root_id) => self.send_in_thread(root_id, outgoing, window, cx),
108 None => self.send(outgoing, window, cx),
109 }
110 agent_api::resolve_proposal(&conversation_id, Outcome::Sent, cx);
111 }
112
113 fn edit_proposal(&mut self, window: &mut Window, cx: &mut Context<Self>) {
114 let Some((conversation_id, entry)) = self.pending_proposal(cx) else {
115 return;
116 };
117 let reply_to = entry.proposal.reply_to_message_id.as_deref();
118 if let Some(message_id) = reply_to {
119 self.begin_reply(message_id, window, cx);
120 } else if self.in_feed() {
121 self.open_new_post(window, cx);
122 }
123 let composer = match self.reply_composer.clone() {
124 Some(reply_composer) if self.in_feed() && reply_to.is_some() => reply_composer,
125 _ => self.composer.clone(),
126 };
127 let mentions = outgoing_for(&entry, None).mentions;
128 let pictures = picture_files(&entry);
129 composer.update(cx, |composer, cx| {
130 composer.load_proposal(&entry.proposal.text, &mentions, pictures, window, cx);
131 composer.focus(window, cx);
132 });
133 agent_api::resolve_proposal(&conversation_id, Outcome::Edited, cx);
134 }
135
136 fn discard_proposal(&mut self, _: &mut Window, cx: &mut Context<Self>) {
137 if let Some(conversation_id) = self.conversation_id() {
138 agent_api::resolve_proposal(&conversation_id, Outcome::Discarded, cx);
139 }
140 }
141
142 pub(super) fn accept_reaction(
143 &mut self,
144 message_id: &str,
145 proposal_id: &str,
146 emoji: &str,
147 cx: &mut Context<Self>,
148 ) {
149 let Some(conversation_id) = self.conversation_id() else {
150 return;
151 };
152 let in_flight = self
153 .pending_reactions
154 .get(message_id)
155 .is_some_and(|entries| {
156 entries
157 .iter()
158 .any(|(glyph, added)| *added && *glyph == reaction_glyph(emoji))
159 });
160 if in_flight
161 || !self.reaction_proposal_is_current(&conversation_id, message_id, proposal_id, cx)
162 {
163 return;
164 }
165 let outcome =
166 match self.change_reaction(message_id, emoji, true, Some(proposal_id.to_owned()), cx) {
167 ReactionAttempt::Started => return,
168 ReactionAttempt::Acted => ReactionOutcome::Accepted,
169 ReactionAttempt::AlreadyThere | ReactionAttempt::MissingMessage => {
170 ReactionOutcome::Stale
171 }
172 ReactionAttempt::Blocked => return,
173 };
174 agent_api::resolve_reaction_proposal(
175 &conversation_id,
176 message_id,
177 proposal_id,
178 outcome,
179 cx,
180 );
181 }
182
183 fn reaction_proposal_is_current(
184 &self,
185 conversation_id: &str,
186 message_id: &str,
187 proposal_id: &str,
188 cx: &App,
189 ) -> bool {
190 agent_api::reaction_proposals(conversation_id, cx)
191 .get(message_id)
192 .is_some_and(|pending| pending.id == proposal_id)
193 }
194
195 pub(super) fn discard_reaction(
196 &mut self,
197 message_id: &str,
198 proposal_id: &str,
199 cx: &mut Context<Self>,
200 ) {
201 if let Some(conversation_id) = self.conversation_id() {
202 agent_api::resolve_reaction_proposal(
203 &conversation_id,
204 message_id,
205 proposal_id,
206 ReactionOutcome::Discarded,
207 cx,
208 );
209 }
210 }
211
212 pub(super) fn refresh_reaction_pills(&mut self, cx: &App) -> HashSet<String> {
213 let Some(conversation_id) = self.conversation_id() else {
214 return HashSet::new();
215 };
216 let latest = agent_api::reaction_proposals(&conversation_id, cx);
217 let mut shown = self.reaction_proposals.borrow_mut();
218 let changed = latest
219 .iter()
220 .filter(|(message_id, proposal)| shown.get(*message_id) != Some(*proposal))
221 .map(|(message_id, _)| message_id.clone())
222 .chain(
223 shown
224 .keys()
225 .filter(|message_id| !latest.contains_key(*message_id))
226 .cloned(),
227 )
228 .collect();
229 *shown = latest;
230 changed
231 }
232
233 pub(super) fn sync_reaction_pills(&mut self, cx: &mut Context<Self>) {
234 let changed = self.refresh_reaction_pills(cx);
235 if changed.is_empty() {
236 return;
237 }
238 self.remeasure_rows_of(&changed, cx);
239 cx.notify();
240 }
241}