1use std::{
2 ops::Range,
3 sync::Arc,
4 time::{Duration, Instant},
5};
6
7use cloud_llm_client::EditPredictionRejectReason;
8use edit_prediction_types::{PredictedCursorPosition, interpolate_edits};
9use gpui::{AsyncApp, Entity, SharedString};
10use language::{Anchor, Buffer, BufferSnapshot, EditPreview, TextBufferSnapshot};
11use zeta_prompt::ZetaPromptInput;
12
13#[derive(Clone, Default, Debug, PartialEq, Eq, Hash)]
14pub struct EditPredictionId(pub SharedString);
15
16impl From<EditPredictionId> for gpui::ElementId {
17 fn from(value: EditPredictionId) -> Self {
18 gpui::ElementId::Name(value.0)
19 }
20}
21
22impl std::fmt::Display for EditPredictionId {
23 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24 write!(f, "{}", self.0)
25 }
26}
27
28/// A prediction response that was returned from the provider, whether it was ultimately valid or not.
29pub struct EditPredictionResult {
30 pub id: EditPredictionId,
31 pub prediction: Result<EditPrediction, EditPredictionRejectReason>,
32}
33
34impl EditPredictionResult {
35 pub async fn new(
36 id: EditPredictionId,
37 edited_buffer: &Entity<Buffer>,
38 edited_buffer_snapshot: &BufferSnapshot,
39 edits: Arc<[(Range<Anchor>, Arc<str>)]>,
40 cursor_position: Option<PredictedCursorPosition>,
41 buffer_snapshotted_at: Instant,
42 response_received_at: Instant,
43 inputs: ZetaPromptInput,
44 model_version: Option<String>,
45 cx: &mut AsyncApp,
46 ) -> Self {
47 if edits.is_empty() {
48 return Self {
49 id,
50 prediction: Err(EditPredictionRejectReason::Empty),
51 };
52 }
53
54 let Some((edits, snapshot, edit_preview_task)) =
55 edited_buffer.read_with(cx, |buffer, cx| {
56 let new_snapshot = buffer.snapshot();
57 let edits: Arc<[_]> =
58 interpolate_edits(&edited_buffer_snapshot, &new_snapshot, &edits)?.into();
59
60 Some((edits.clone(), new_snapshot, buffer.preview_edits(edits, cx)))
61 })
62 else {
63 return Self {
64 id,
65 prediction: Err(EditPredictionRejectReason::InterpolatedEmpty),
66 };
67 };
68
69 let edit_preview = edit_preview_task.await;
70
71 Self {
72 id: id.clone(),
73 prediction: Ok(EditPrediction {
74 id,
75 edits,
76 cursor_position,
77 snapshot,
78 edit_preview,
79 inputs,
80 buffer: edited_buffer.clone(),
81 buffer_snapshotted_at,
82 response_received_at,
83 model_version,
84 }),
85 }
86 }
87}
88
89#[derive(Clone)]
90pub struct EditPrediction {
91 pub id: EditPredictionId,
92 pub edits: Arc<[(Range<Anchor>, Arc<str>)]>,
93 pub cursor_position: Option<PredictedCursorPosition>,
94 pub snapshot: BufferSnapshot,
95 pub edit_preview: EditPreview,
96 pub buffer: Entity<Buffer>,
97 pub buffer_snapshotted_at: Instant,
98 pub response_received_at: Instant,
99 pub inputs: zeta_prompt::ZetaPromptInput,
100 pub model_version: Option<String>,
101}
102
103impl EditPrediction {
104 pub fn interpolate(
105 &self,
106 new_snapshot: &TextBufferSnapshot,
107 ) -> Option<Vec<(Range<Anchor>, Arc<str>)>> {
108 interpolate_edits(&self.snapshot, new_snapshot, &self.edits)
109 }
110
111 pub fn targets_buffer(&self, buffer: &Buffer) -> bool {
112 self.snapshot.remote_id() == buffer.remote_id()
113 }
114
115 pub fn latency(&self) -> Duration {
116 self.response_received_at - self.buffer_snapshotted_at
117 }
118}
119
120impl std::fmt::Debug for EditPrediction {
121 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
122 f.debug_struct("EditPrediction")
123 .field("id", &self.id)
124 .field("edits", &self.edits)
125 .finish()
126 }
127}
128
129#[cfg(test)]
130mod tests {
131 use std::path::Path;
132
133 use super::*;
134 use gpui::{App, Entity, TestAppContext, prelude::*};
135 use language::{Buffer, ToOffset as _};
136 use zeta_prompt::ZetaPromptInput;
137
138 #[gpui::test]
139 async fn test_edit_prediction_basic_interpolation(cx: &mut TestAppContext) {
140 let buffer = cx.new(|cx| Buffer::local("Lorem ipsum dolor", cx));
141 let edits: Arc<[(Range<Anchor>, Arc<str>)]> = cx.update(|cx| {
142 to_prediction_edits([(2..5, "REM".into()), (9..11, "".into())], &buffer, cx).into()
143 });
144
145 let edit_preview = cx
146 .read(|cx| buffer.read(cx).preview_edits(edits.clone(), cx))
147 .await;
148
149 let prediction = EditPrediction {
150 id: EditPredictionId("prediction-1".into()),
151 edits,
152 cursor_position: None,
153 snapshot: cx.read(|cx| buffer.read(cx).snapshot()),
154 buffer: buffer.clone(),
155 edit_preview,
156 model_version: None,
157 inputs: ZetaPromptInput {
158 events: vec![],
159 related_files: vec![],
160 cursor_path: Path::new("path.txt").into(),
161 cursor_offset_in_excerpt: 0,
162 cursor_excerpt: "".into(),
163 excerpt_start_row: None,
164 excerpt_ranges: Default::default(),
165 experiment: None,
166 in_open_source_repo: false,
167 can_collect_data: false,
168 },
169 buffer_snapshotted_at: Instant::now(),
170 response_received_at: Instant::now(),
171 };
172
173 cx.update(|cx| {
174 assert_eq!(
175 from_prediction_edits(
176 &prediction.interpolate(&buffer.read(cx).snapshot()).unwrap(),
177 &buffer,
178 cx
179 ),
180 vec![(2..5, "REM".into()), (9..11, "".into())]
181 );
182
183 buffer.update(cx, |buffer, cx| buffer.edit([(2..5, "")], None, cx));
184 assert_eq!(
185 from_prediction_edits(
186 &prediction.interpolate(&buffer.read(cx).snapshot()).unwrap(),
187 &buffer,
188 cx
189 ),
190 vec![(2..2, "REM".into()), (6..8, "".into())]
191 );
192
193 buffer.update(cx, |buffer, cx| buffer.undo(cx));
194 assert_eq!(
195 from_prediction_edits(
196 &prediction.interpolate(&buffer.read(cx).snapshot()).unwrap(),
197 &buffer,
198 cx
199 ),
200 vec![(2..5, "REM".into()), (9..11, "".into())]
201 );
202
203 buffer.update(cx, |buffer, cx| buffer.edit([(2..5, "R")], None, cx));
204 assert_eq!(
205 from_prediction_edits(
206 &prediction.interpolate(&buffer.read(cx).snapshot()).unwrap(),
207 &buffer,
208 cx
209 ),
210 vec![(3..3, "EM".into()), (7..9, "".into())]
211 );
212
213 buffer.update(cx, |buffer, cx| buffer.edit([(3..3, "E")], None, cx));
214 assert_eq!(
215 from_prediction_edits(
216 &prediction.interpolate(&buffer.read(cx).snapshot()).unwrap(),
217 &buffer,
218 cx
219 ),
220 vec![(4..4, "M".into()), (8..10, "".into())]
221 );
222
223 buffer.update(cx, |buffer, cx| buffer.edit([(4..4, "M")], None, cx));
224 assert_eq!(
225 from_prediction_edits(
226 &prediction.interpolate(&buffer.read(cx).snapshot()).unwrap(),
227 &buffer,
228 cx
229 ),
230 vec![(9..11, "".into())]
231 );
232
233 buffer.update(cx, |buffer, cx| buffer.edit([(4..5, "")], None, cx));
234 assert_eq!(
235 from_prediction_edits(
236 &prediction.interpolate(&buffer.read(cx).snapshot()).unwrap(),
237 &buffer,
238 cx
239 ),
240 vec![(4..4, "M".into()), (8..10, "".into())]
241 );
242
243 buffer.update(cx, |buffer, cx| buffer.edit([(8..10, "")], None, cx));
244 assert_eq!(
245 from_prediction_edits(
246 &prediction.interpolate(&buffer.read(cx).snapshot()).unwrap(),
247 &buffer,
248 cx
249 ),
250 vec![(4..4, "M".into())]
251 );
252
253 buffer.update(cx, |buffer, cx| buffer.edit([(4..6, "")], None, cx));
254 assert_eq!(prediction.interpolate(&buffer.read(cx).snapshot()), None);
255 })
256 }
257
258 fn to_prediction_edits(
259 iterator: impl IntoIterator<Item = (Range<usize>, Arc<str>)>,
260 buffer: &Entity<Buffer>,
261 cx: &App,
262 ) -> Vec<(Range<Anchor>, Arc<str>)> {
263 let buffer = buffer.read(cx);
264 iterator
265 .into_iter()
266 .map(|(range, text)| {
267 (
268 buffer.anchor_after(range.start)..buffer.anchor_before(range.end),
269 text,
270 )
271 })
272 .collect()
273 }
274
275 fn from_prediction_edits(
276 editor_edits: &[(Range<Anchor>, Arc<str>)],
277 buffer: &Entity<Buffer>,
278 cx: &App,
279 ) -> Vec<(Range<usize>, Arc<str>)> {
280 let buffer = buffer.read(cx);
281 editor_edits
282 .iter()
283 .map(|(range, text)| {
284 (
285 range.start.to_offset(buffer)..range.end.to_offset(buffer),
286 text.clone(),
287 )
288 })
289 .collect()
290 }
291}