History

added smarter error handling for file_context prompts without provided buffers

KCaverly committed 3 years ago

Commit 587fd701 parent f59f2ec1 file · +26 −23Browse files

Unified · Split

crates/ai/src/templates/file_context.rs+26−23
@@ -1,3 +1,4 @@
1+use anyhow::anyhow;
12 use language::ToOffset;
23
34 use crate::templates::base::PromptArguments;
@@ -12,24 +13,23 @@ impl PromptTemplate for FileContext { · 8 unmodified lines
1213 args: &PromptArguments,
1314 max_token_length: Option<usize>,
1415 ) -> anyhow::Result<(String, usize)> {
15− let mut prompt = String::new();
16−
17− // Add Initial Preamble
18− // TODO: Do we want to add the path in here?
19− writeln!(
20− prompt,
21− "The file you are currently working on has the following content:"
22− )
23− .unwrap();
16+ if let Some(buffer) = &args.buffer {
17+ let mut prompt = String::new();
18+ // Add Initial Preamble
19+ // TODO: Do we want to add the path in here?
20+ writeln!(
21+ prompt,
22+ "The file you are currently working on has the following content:"
23+ )
24+ .unwrap();
2425
25− let language_name = args
26− .language_name
27− .clone()
28− .unwrap_or("".to_string())
29− .to_lowercase();
30− writeln!(prompt, "```{language_name}").unwrap();
26+ let language_name = args
27+ .language_name
28+ .clone()
29+ .unwrap_or("".to_string())
30+ .to_lowercase();
31+ writeln!(prompt, "```{language_name}").unwrap();
3132
32− if let Some(buffer) = &args.buffer {
3333 if let Some(selected_range) = &args.selected_range {
3434 let start = selected_range.start.to_offset(buffer);
3535 let end = selected_range.end.to_offset(buffer);
@@ -74,15 +74,18 @@ impl PromptTemplate for FileContext { · 38 unmodified lines
7474 } else {
7575 // If we dont have a selected range, include entire file.
7676 writeln!(prompt, "{}", &buffer.text()).unwrap();
77+ writeln!(prompt, "```").unwrap();
7778 }
78− }
7979
80− // Really dumb truncation strategy
81− if let Some(max_tokens) = max_token_length {
82− prompt = args.model.truncate(&prompt, max_tokens)?;
83− }
80+ // Really dumb truncation strategy
81+ if let Some(max_tokens) = max_token_length {
82+ prompt = args.model.truncate(&prompt, max_tokens)?;
83+ }
8484
85− let token_count = args.model.count_tokens(&prompt)?;
86− anyhow::Ok((prompt, token_count))
85+ let token_count = args.model.count_tokens(&prompt)?;
86+ anyhow::Ok((prompt, token_count))
87+ } else {
88+ Err(anyhow!("no buffer provided to retrieve file context from"))
89+ }
8790 }
8891 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t