Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/cloud_llm_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ workspace-hack.workspace = true

[dev-dependencies]
pretty_assertions.workspace = true
indoc.workspace = true
112 changes: 112 additions & 0 deletions crates/cloud_llm_client/src/predict_edits_v3.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use chrono::Duration;
use serde::{Deserialize, Serialize};
use std::{
fmt::Display,
ops::{Add, Range, Sub},
path::{Path, PathBuf},
sync::Arc,
Expand Down Expand Up @@ -91,6 +92,38 @@ pub enum Event {
},
}

impl Display for Event {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Event::BufferChange {
path,
old_path,
diff,
predicted,
} => {
let new_path = path.as_deref().unwrap_or(Path::new("untitled"));
let old_path = old_path.as_deref().unwrap_or(new_path);

if *predicted {
write!(
f,
"// User accepted prediction:\n--- a/{}\n+++ b/{}\n{diff}",
old_path.display(),
new_path.display()
)
} else {
write!(
f,
"--- a/{}\n+++ b/{}\n{diff}",
old_path.display(),
new_path.display()
)
}
}
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Signature {
pub text: String,
Expand Down Expand Up @@ -204,3 +237,82 @@ impl Sub for Line {
Self(self.0 - rhs.0)
}
}

#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
use pretty_assertions::assert_eq;

#[test]
fn test_event_display() {
let ev = Event::BufferChange {
path: None,
old_path: None,
diff: "@@ -1,2 +1,2 @@\n-a\n-b\n".into(),
predicted: false,
};
assert_eq!(
ev.to_string(),
indoc! {"
--- a/untitled
+++ b/untitled
@@ -1,2 +1,2 @@
-a
-b
"}
);

let ev = Event::BufferChange {
path: Some(PathBuf::from("foo/bar.txt")),
old_path: Some(PathBuf::from("foo/bar.txt")),
diff: "@@ -1,2 +1,2 @@\n-a\n-b\n".into(),
predicted: false,
};
assert_eq!(
ev.to_string(),
indoc! {"
--- a/foo/bar.txt
+++ b/foo/bar.txt
@@ -1,2 +1,2 @@
-a
-b
"}
);

let ev = Event::BufferChange {
path: Some(PathBuf::from("abc.txt")),
old_path: Some(PathBuf::from("123.txt")),
diff: "@@ -1,2 +1,2 @@\n-a\n-b\n".into(),
predicted: false,
};
assert_eq!(
ev.to_string(),
indoc! {"
--- a/123.txt
+++ b/abc.txt
@@ -1,2 +1,2 @@
-a
-b
"}
);

let ev = Event::BufferChange {
path: Some(PathBuf::from("abc.txt")),
old_path: Some(PathBuf::from("123.txt")),
diff: "@@ -1,2 +1,2 @@\n-a\n-b\n".into(),
predicted: true,
};
assert_eq!(
ev.to_string(),
indoc! {"
// User accepted prediction:
--- a/123.txt
+++ b/abc.txt
@@ -1,2 +1,2 @@
-a
-b
"}
);
}
}
55 changes: 9 additions & 46 deletions crates/cloud_zeta2_prompt/src/cloud_zeta2_prompt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//! Zeta2 prompt planning and generation code shared with cloud.

use anyhow::{Context as _, Result, anyhow};
use cloud_llm_client::predict_edits_v3::{
self, Event, Line, Point, PromptFormat, ReferencedDeclaration,
};
use cloud_llm_client::predict_edits_v3::{self, Line, Point, PromptFormat, ReferencedDeclaration};
use indoc::indoc;
use ordered_float::OrderedFloat;
use rustc_hash::{FxHashMap, FxHashSet};
Expand Down Expand Up @@ -419,7 +417,7 @@ impl<'a> PlannedPrompt<'a> {
};

if self.request.events.is_empty() {
prompt.push_str("No edits yet.\n\n");
prompt.push_str("(No edit history)\n\n");
} else {
prompt.push_str(
"The following are the latest edits made by the user, from earlier to later.\n\n",
Expand Down Expand Up @@ -465,50 +463,15 @@ impl<'a> PlannedPrompt<'a> {
}

fn push_events(output: &mut String, events: &[predict_edits_v3::Event]) {
for event in events {
match event {
Event::BufferChange {
path,
old_path,
diff,
predicted,
} => {
if let Some(old_path) = &old_path
&& let Some(new_path) = &path
{
if old_path != new_path {
writeln!(
output,
"User renamed {} to {}\n\n",
old_path.display(),
new_path.display()
)
.unwrap();
}
}
if events.is_empty() {
return;
};

let path = path
.as_ref()
.map_or_else(|| "untitled".to_string(), |path| path.display().to_string());

if *predicted {
writeln!(
output,
"User accepted prediction {:?}:\n`````diff\n{}\n`````\n",
path, diff
)
.unwrap();
} else {
writeln!(
output,
"User edited {:?}:\n`````diff\n{}\n`````\n",
path, diff
)
.unwrap();
}
}
}
writeln!(output, "`````diff").unwrap();
for event in events {
writeln!(output, "{}", event).unwrap();
}
writeln!(output, "`````\n").unwrap();
}

fn push_file_snippets(
Expand Down
Loading