BlueGit

ict rusty-teams

Files

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

proposal.rs

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