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