BlueGit

ict rusty-teams

Files

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

proposal.rs

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