capture_example.rs

  1use crate::{
  2    StoredEvent,
  3    cursor_excerpt::editable_and_context_ranges_for_cursor_position,
  4    example_spec::{
  5        CapturedEvent, CapturedPromptInput, CapturedRelatedExcerpt, CapturedRelatedFile,
  6        ExampleSpec, MAX_CURSOR_FILE_SIZE,
  7    },
  8};
  9use anyhow::Result;
 10use buffer_diff::BufferDiffSnapshot;
 11use collections::HashMap;
 12use gpui::{App, Entity, Task};
 13use language::{Buffer, ToPoint as _};
 14use project::{Project, WorktreeId};
 15use std::{collections::hash_map, fmt::Write as _, ops::Range, path::Path, sync::Arc};
 16use text::{BufferSnapshot as TextBufferSnapshot, Point, ToOffset as _};
 17
 18pub fn capture_example(
 19    project: Entity<Project>,
 20    buffer: Entity<Buffer>,
 21    cursor_anchor: language::Anchor,
 22    mut events: Vec<StoredEvent>,
 23    related_files: Vec<zeta_prompt::RelatedFile>,
 24    populate_expected_patch: bool,
 25    cx: &mut App,
 26) -> Option<Task<Result<ExampleSpec>>> {
 27    let snapshot = buffer.read(cx).snapshot();
 28    let file = snapshot.file()?;
 29    let worktree_id = file.worktree_id(cx);
 30    let repository = project.read(cx).active_repository(cx)?;
 31    let repository_snapshot = repository.read(cx).snapshot();
 32    let worktree = project.read(cx).worktree_for_id(worktree_id, cx)?;
 33    let root_name = worktree.read(cx).root_name_str().to_owned();
 34    let cursor_path: Arc<Path> = file.path().as_std_path().into();
 35    if worktree.read(cx).abs_path() != repository_snapshot.work_directory_abs_path {
 36        return None;
 37    }
 38
 39    let repository_url = repository_snapshot
 40        .remote_origin_url
 41        .clone()
 42        .or_else(|| repository_snapshot.remote_upstream_url.clone())?;
 43    let revision = repository_snapshot.head_commit.as_ref()?.sha.to_string();
 44
 45    let git_store = project.read(cx).git_store().clone();
 46
 47    Some(cx.spawn(async move |mut cx| {
 48        let snapshots_by_path =
 49            collect_snapshots(&project, &git_store, worktree_id, &events, &mut cx).await?;
 50
 51        events.retain(|stored_event| {
 52            let zeta_prompt::Event::BufferChange { path, .. } = stored_event.event.as_ref();
 53            let relative_path = strip_root_name(path, &root_name);
 54            snapshots_by_path.contains_key(relative_path)
 55        });
 56
 57        let line_comment_prefix = snapshot
 58            .language()
 59            .and_then(|lang| lang.config().line_comments.first())
 60            .map(|s| s.to_string())
 61            .unwrap_or_default();
 62
 63        let full_cursor_offset = cursor_anchor.to_offset(&snapshot);
 64        let cursor_point = cursor_anchor.to_point(&snapshot);
 65        let cursor_file_content = if snapshot.len() <= MAX_CURSOR_FILE_SIZE {
 66            Some(snapshot.text())
 67        } else {
 68            None
 69        };
 70
 71        let (cursor_excerpt, cursor_offset_in_excerpt, cursor_excerpt_range) = cx
 72            .background_executor()
 73            .spawn(async move { compute_cursor_excerpt(&snapshot, cursor_anchor) })
 74            .await;
 75        let uncommitted_diff = cx
 76            .background_executor()
 77            .spawn(async move { compute_uncommitted_diff(snapshots_by_path) })
 78            .await;
 79
 80        let mut edit_history = String::new();
 81        for stored_event in &events {
 82            write_event_with_relative_paths(&mut edit_history, &stored_event.event, &root_name);
 83            if !edit_history.ends_with('\n') {
 84                edit_history.push('\n');
 85            }
 86        }
 87
 88        // Initialize an empty patch with context lines, to make it easy
 89        // to write the expected patch by hand.
 90        let mut expected_patches = Vec::new();
 91        let mut rejected_patch = None;
 92        if populate_expected_patch {
 93            let mut empty_patch = String::new();
 94            let start_row = cursor_excerpt_range.start.row + 1;
 95            let row_count = cursor_excerpt_range.end.row - cursor_excerpt_range.start.row + 1;
 96            writeln!(&mut empty_patch, "--- a/{}", cursor_path.display()).ok();
 97            writeln!(&mut empty_patch, "+++ b/{}", cursor_path.display()).ok();
 98            writeln!(
 99                &mut empty_patch,
100                "@@ -{},{} +{},{} @@",
101                start_row, row_count, start_row, row_count,
102            )
103            .ok();
104            for line in cursor_excerpt.lines() {
105                writeln!(&mut empty_patch, " {}", line).ok();
106            }
107
108            expected_patches.push(empty_patch.clone());
109            rejected_patch = Some(empty_patch);
110        }
111
112        let prompt_input = cursor_file_content.map(|content| {
113            let captured_events: Vec<CapturedEvent> = events
114                .iter()
115                .map(|stored_event| {
116                    let zeta_prompt::Event::BufferChange {
117                        path,
118                        old_path,
119                        diff,
120                        predicted,
121                        in_open_source_repo,
122                    } = stored_event.event.as_ref();
123                    CapturedEvent {
124                        path: strip_root_name(path, &root_name).into(),
125                        old_path: strip_root_name(old_path, &root_name).into(),
126                        diff: diff.clone(),
127                        predicted: *predicted,
128                        in_open_source_repo: *in_open_source_repo,
129                    }
130                })
131                .collect();
132
133            let captured_related_files: Vec<CapturedRelatedFile> = related_files
134                .iter()
135                .map(|rf| CapturedRelatedFile {
136                    path: strip_root_name(&rf.path, &root_name).into(),
137                    max_row: rf.max_row,
138                    excerpts: rf
139                        .excerpts
140                        .iter()
141                        .map(|e| CapturedRelatedExcerpt {
142                            row_range: e.row_range.clone(),
143                            text: e.text.to_string(),
144                        })
145                        .collect(),
146                })
147                .collect();
148
149            CapturedPromptInput {
150                cursor_file_content: content,
151                cursor_offset: full_cursor_offset,
152                cursor_row: cursor_point.row,
153                cursor_column: cursor_point.column,
154                excerpt_start_row: Some(0),
155                events: captured_events,
156                related_files: captured_related_files,
157                in_open_source_repo: false,
158            }
159        });
160
161        let mut spec = ExampleSpec {
162            name: generate_timestamp_name(),
163            repository_url,
164            revision,
165            tags: Vec::new(),
166            reasoning: None,
167            uncommitted_diff,
168            cursor_path,
169            cursor_position: String::new(),
170            edit_history,
171            expected_patches,
172            rejected_patch,
173            captured_prompt_input: prompt_input,
174            telemetry: None,
175            human_feedback: Vec::new(),
176            rating: None,
177        };
178        spec.set_cursor_excerpt(
179            &cursor_excerpt,
180            cursor_offset_in_excerpt,
181            &line_comment_prefix,
182        );
183        Ok(spec)
184    }))
185}
186
187fn strip_root_name<'a>(path: &'a Path, root_name: &str) -> &'a Path {
188    path.strip_prefix(root_name).unwrap_or(path)
189}
190
191fn write_event_with_relative_paths(
192    output: &mut String,
193    event: &zeta_prompt::Event,
194    root_name: &str,
195) {
196    fn write_relative_path(output: &mut String, path: &Path, root_name: &str) {
197        for component in strip_root_name(path, root_name).components() {
198            output.push('/');
199            write!(output, "{}", component.as_os_str().to_string_lossy()).ok();
200        }
201    }
202
203    let zeta_prompt::Event::BufferChange {
204        path,
205        old_path,
206        diff,
207        ..
208    } = event;
209
210    output.push_str("--- a");
211    write_relative_path(output, old_path.as_ref(), root_name);
212    output.push_str("\n+++ b");
213    write_relative_path(output, path.as_ref(), root_name);
214    output.push('\n');
215    output.push_str(diff);
216}
217
218fn compute_cursor_excerpt(
219    snapshot: &language::BufferSnapshot,
220    cursor_anchor: language::Anchor,
221) -> (String, usize, Range<Point>) {
222    use text::ToOffset as _;
223
224    let cursor_point = cursor_anchor.to_point(snapshot);
225    let (_editable_range, context_range) =
226        editable_and_context_ranges_for_cursor_position(cursor_point, snapshot, 100, 50);
227    let context_start_offset = context_range.start.to_offset(snapshot);
228    let cursor_offset = cursor_anchor.to_offset(snapshot);
229    let cursor_offset_in_excerpt = cursor_offset.saturating_sub(context_start_offset);
230    let excerpt = snapshot
231        .text_for_range(context_range.clone())
232        .collect::<String>();
233    (excerpt, cursor_offset_in_excerpt, context_range)
234}
235
236async fn collect_snapshots(
237    project: &Entity<Project>,
238    git_store: &Entity<project::git_store::GitStore>,
239    worktree_id: WorktreeId,
240    events: &[StoredEvent],
241    cx: &mut gpui::AsyncApp,
242) -> Result<HashMap<Arc<Path>, (TextBufferSnapshot, BufferDiffSnapshot)>> {
243    let mut snapshots_by_path = HashMap::default();
244    for stored_event in events {
245        let zeta_prompt::Event::BufferChange { path, .. } = stored_event.event.as_ref();
246        if let Some((project_path, relative_path)) = project.read_with(cx, |project, cx| {
247            let project_path = project
248                .find_project_path(path, cx)
249                .filter(|path| path.worktree_id == worktree_id)?;
250            let relative_path: Arc<Path> = project_path.path.as_std_path().into();
251            Some((project_path, relative_path))
252        }) {
253            if let hash_map::Entry::Vacant(entry) = snapshots_by_path.entry(relative_path) {
254                let buffer = project
255                    .update(cx, |project, cx| {
256                        project.open_buffer(project_path.clone(), cx)
257                    })
258                    .await?;
259                let diff = git_store
260                    .update(cx, |git_store, cx| {
261                        git_store.open_uncommitted_diff(buffer.clone(), cx)
262                    })
263                    .await?;
264                let diff_snapshot = diff.update(cx, |diff, cx| diff.snapshot(cx));
265                entry.insert((stored_event.old_snapshot.clone(), diff_snapshot));
266            }
267        }
268    }
269    Ok(snapshots_by_path)
270}
271
272fn compute_uncommitted_diff(
273    snapshots_by_path: HashMap<Arc<Path>, (TextBufferSnapshot, BufferDiffSnapshot)>,
274) -> String {
275    let mut uncommitted_diff = String::new();
276    for (relative_path, (before_text, diff_snapshot)) in snapshots_by_path {
277        if let Some(head_text) = &diff_snapshot.base_text_string() {
278            let file_diff = language::unified_diff(head_text, &before_text.text());
279            if !file_diff.is_empty() {
280                let path_str = relative_path.to_string_lossy();
281                writeln!(uncommitted_diff, "--- a/{path_str}").ok();
282                writeln!(uncommitted_diff, "+++ b/{path_str}").ok();
283                uncommitted_diff.push_str(&file_diff);
284                if !uncommitted_diff.ends_with('\n') {
285                    uncommitted_diff.push('\n');
286                }
287            }
288        }
289    }
290    uncommitted_diff
291}
292
293fn generate_timestamp_name() -> String {
294    let format = time::format_description::parse("[year]-[month]-[day] [hour]:[minute]:[second]");
295    match format {
296        Ok(format) => {
297            let now = time::OffsetDateTime::now_local()
298                .unwrap_or_else(|_| time::OffsetDateTime::now_utc());
299            now.format(&format)
300                .unwrap_or_else(|_| "unknown-time".to_string())
301        }
302        Err(_) => "unknown-time".to_string(),
303    }
304}
305
306#[cfg(test)]
307mod tests {
308    use super::*;
309    use crate::EditPredictionStore;
310    use client::{Client, UserStore};
311    use clock::FakeSystemClock;
312    use gpui::{AppContext as _, TestAppContext, http_client::FakeHttpClient};
313    use indoc::indoc;
314    use language::{Anchor, Point};
315    use project::{FakeFs, Project};
316    use serde_json::json;
317    use settings::SettingsStore;
318    use std::path::Path;
319
320    #[gpui::test]
321    async fn test_capture_example(cx: &mut TestAppContext) {
322        init_test(cx);
323        let fs = FakeFs::new(cx.executor());
324
325        let committed_contents = indoc! {"
326            fn main() {
327                one();
328                two();
329                three();
330                four();
331                five();
332                six();
333                seven();
334                eight();
335                nine();
336            }
337        "};
338
339        let disk_contents = indoc! {"
340            fn main() {
341                // comment 1
342                one();
343                two();
344                three();
345                four();
346                five();
347                six();
348                seven();
349                eight();
350                // comment 2
351                nine();
352            }
353        "};
354
355        fs.insert_tree(
356            "/project",
357            json!({
358                ".git": {},
359                "src": {
360                    "main.rs": disk_contents,
361                }
362            }),
363        )
364        .await;
365
366        // Create an external file outside the main project
367        fs.insert_tree(
368            "/external",
369            json!({
370                "external.rs": "fn external() {}\n",
371            }),
372        )
373        .await;
374
375        fs.set_head_for_repo(
376            Path::new("/project/.git"),
377            &[("src/main.rs", committed_contents.to_string())],
378            "abc123def456",
379        );
380        fs.set_remote_for_repo(
381            Path::new("/project/.git"),
382            "origin",
383            "https://github.com/test/repo.git",
384        );
385
386        let project = Project::test(fs.clone(), ["/project".as_ref()], cx).await;
387
388        let buffer = project
389            .update(cx, |project, cx| {
390                project.open_local_buffer("/project/src/main.rs", cx)
391            })
392            .await
393            .unwrap();
394
395        let ep_store = cx.read(|cx| EditPredictionStore::try_global(cx).unwrap());
396        ep_store.update(cx, |ep_store, cx| {
397            ep_store.register_buffer(&buffer, &project, cx)
398        });
399        cx.run_until_parked();
400
401        buffer.update(cx, |buffer, cx| {
402            let point = Point::new(6, 0);
403            buffer.edit([(point..point, "    // comment 3\n")], None, cx);
404            let point = Point::new(4, 0);
405            buffer.edit([(point..point, "    // comment 4\n")], None, cx);
406
407            pretty_assertions::assert_eq!(
408                buffer.text(),
409                indoc! {"
410                    fn main() {
411                        // comment 1
412                        one();
413                        two();
414                        // comment 4
415                        three();
416                        four();
417                        // comment 3
418                        five();
419                        six();
420                        seven();
421                        eight();
422                        // comment 2
423                        nine();
424                    }
425                "}
426            );
427        });
428        cx.run_until_parked();
429
430        // Open and edit an external file (outside the main project's worktree)
431        let external_buffer = project
432            .update(cx, |project, cx| {
433                project.open_local_buffer("/external/external.rs", cx)
434            })
435            .await
436            .unwrap();
437        ep_store.update(cx, |ep_store, cx| {
438            ep_store.register_buffer(&external_buffer, &project, cx)
439        });
440        cx.run_until_parked();
441        external_buffer.update(cx, |buffer, cx| {
442            let point = Point::new(0, 0);
443            buffer.edit([(point..point, "// external edit\n")], None, cx);
444        });
445        cx.run_until_parked();
446
447        // Verify the external edit was recorded in events
448        let events = ep_store.update(cx, |store, cx| store.edit_history_for_project(&project, cx));
449        assert!(
450            matches!(
451                events
452                    .last()
453                    .unwrap()
454                    .event
455                    .as_ref(),
456                zeta_prompt::Event::BufferChange { path, .. } if path.as_ref() == "/external/external.rs"
457            ),
458            "external file edit should be in events"
459        );
460
461        let mut example = cx
462            .update(|cx| {
463                capture_example(
464                    project.clone(),
465                    buffer.clone(),
466                    Anchor::MIN,
467                    events,
468                    Vec::new(),
469                    true,
470                    cx,
471                )
472                .unwrap()
473            })
474            .await
475            .unwrap();
476        example.name = "test".to_string();
477
478        pretty_assertions::assert_eq!(
479            example,
480            ExampleSpec {
481                name: "test".to_string(),
482                repository_url: "https://github.com/test/repo.git".to_string(),
483                revision: "abc123def456".to_string(),
484                tags: Vec::new(),
485                reasoning: None,
486                uncommitted_diff: indoc! {"
487                    --- a/src/main.rs
488                    +++ b/src/main.rs
489                    @@ -1,4 +1,5 @@
490                     fn main() {
491                    +    // comment 1
492                         one();
493                         two();
494                         three();
495                    @@ -7,5 +8,6 @@
496                         six();
497                         seven();
498                         eight();
499                    +    // comment 2
500                         nine();
501                     }
502                "}
503                .to_string(),
504                cursor_path: Path::new("src/main.rs").into(),
505                cursor_position: indoc! {"
506                    fn main() {
507                    ^[CURSOR_POSITION]
508                        // comment 1
509                        one();
510                        two();
511                        // comment 4
512                        three();
513                        four();
514                        // comment 3
515                        five();
516                        six();
517                        seven();
518                        eight();
519                        // comment 2
520                        nine();
521                    }
522                "}
523                .to_string(),
524                edit_history: indoc! {"
525                    --- a/src/main.rs
526                    +++ b/src/main.rs
527                    @@ -2,8 +2,10 @@
528                         // comment 1
529                         one();
530                         two();
531                    +    // comment 4
532                         three();
533                         four();
534                    +    // comment 3
535                         five();
536                         six();
537                         seven();
538                "}
539                .to_string(),
540                expected_patches: vec![
541                    indoc! {"
542                        --- a/src/main.rs
543                        +++ b/src/main.rs
544                        @@ -1,16 +1,16 @@
545                         fn main() {
546                             // comment 1
547                             one();
548                             two();
549                             // comment 4
550                             three();
551                             four();
552                             // comment 3
553                             five();
554                             six();
555                             seven();
556                             eight();
557                             // comment 2
558                             nine();
559                         }
560                    "}
561                    .to_string()
562                ],
563                rejected_patch: Some(
564                    indoc! {"
565                        --- a/src/main.rs
566                        +++ b/src/main.rs
567                        @@ -1,16 +1,16 @@
568                         fn main() {
569                             // comment 1
570                             one();
571                             two();
572                             // comment 4
573                             three();
574                             four();
575                             // comment 3
576                             five();
577                             six();
578                             seven();
579                             eight();
580                             // comment 2
581                             nine();
582                         }
583                    "}
584                    .to_string()
585                ),
586                captured_prompt_input: example.captured_prompt_input.clone(),
587                telemetry: None,
588                human_feedback: Vec::new(),
589                rating: None,
590            }
591        );
592
593        let prompt_input = example
594            .captured_prompt_input
595            .expect("should have captured prompt input");
596        assert!(
597            prompt_input.cursor_file_content.contains("fn main()"),
598            "cursor_file_content should contain file content"
599        );
600        assert_eq!(
601            prompt_input.cursor_offset, 0,
602            "cursor at Anchor::MIN should be offset 0"
603        );
604        assert_eq!(
605            prompt_input.cursor_row, 0,
606            "cursor at Anchor::MIN should be row 0"
607        );
608        assert_eq!(
609            prompt_input.cursor_column, 0,
610            "cursor at Anchor::MIN should be column 0"
611        );
612        assert!(prompt_input.events.len() > 0, "should have captured events");
613        assert_eq!(
614            prompt_input.related_files.len(),
615            0,
616            "should have no related files (none passed)"
617        );
618    }
619
620    fn init_test(cx: &mut TestAppContext) {
621        cx.update(|cx| {
622            let settings_store = SettingsStore::test(cx);
623            cx.set_global(settings_store);
624            zlog::init_test();
625            let http_client = FakeHttpClient::with_404_response();
626            let client = Client::new(Arc::new(FakeSystemClock::new()), http_client, cx);
627            language_model::init(client.clone(), cx);
628            let user_store = cx.new(|cx| UserStore::new(client.clone(), cx));
629            EditPredictionStore::global(&client, &user_store, cx);
630        })
631    }
632}