Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

Commit a39011a

Browse files
authored
Warn on empty result for single file answer contexts (#704)
1 parent 6b7869c commit a39011a

File tree

5 files changed

+49
-39
lines changed

5 files changed

+49
-39
lines changed

server/bleep/src/indexes/file.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl Indexable for File {
156156
}
157157

158158
impl Indexer<File> {
159-
pub async fn file_body(&self, file_disk_path: &str) -> Result<ContentDocument> {
159+
pub async fn file_body(&self, file_disk_path: &str) -> Result<Option<ContentDocument>> {
160160
// Mostly taken from `by_path`, below.
161161
//
162162
// TODO: This can be unified with `by_path` below, but we first need to decide on a unified
@@ -274,7 +274,7 @@ impl Indexer<File> {
274274
&self,
275275
repo_ref: &RepoRef,
276276
relative_path: &str,
277-
) -> Result<ContentDocument> {
277+
) -> Result<Option<ContentDocument>> {
278278
let reader = self.reader.read().await;
279279
let searcher = reader.searcher();
280280

@@ -300,7 +300,7 @@ impl Indexer<File> {
300300
&self,
301301
query: Box<dyn tantivy::query::Query>,
302302
searcher: tantivy::Searcher,
303-
) -> Result<ContentDocument> {
303+
) -> Result<Option<ContentDocument>> {
304304
let file_source = &self.source;
305305

306306
let collector = TopDocs::with_limit(1);
@@ -310,14 +310,16 @@ impl Indexer<File> {
310310

311311
match search_results.as_slice() {
312312
// no paths matched, the input path was not well formed
313-
[] => Err(anyhow::Error::msg("no path found")),
313+
[] => Ok(None),
314314

315315
// exactly one path, good
316316
[(_, doc_addr)] => {
317317
let retrieved_doc = searcher
318318
.doc(*doc_addr)
319319
.expect("failed to get document by address");
320-
Ok(ContentReader.read_document(file_source, retrieved_doc))
320+
Ok(Some(
321+
ContentReader.read_document(file_source, retrieved_doc),
322+
))
321323
}
322324

323325
// more than one path matched, this can occur when top docs is no

server/bleep/src/webserver/answer.rs

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -475,20 +475,6 @@ impl Conversation {
475475
paths = semantic_paths;
476476
}
477477

478-
let prompt = Some("§alias, path".to_owned())
479-
.into_iter()
480-
.chain(paths.iter().map(|p| format!("{}, {p}", self.path_alias(p))))
481-
.collect::<Vec<_>>()
482-
.join("\n");
483-
484-
ctx.track_query(
485-
EventData::input_stage("path search")
486-
.with_payload("query", query)
487-
.with_payload("is_semantic", is_semantic)
488-
.with_payload("results", &paths)
489-
.with_payload("raw_prompt", prompt),
490-
);
491-
492478
let formatted_paths = paths
493479
.iter()
494480
.map(|p| SeenPath {
@@ -497,7 +483,17 @@ impl Conversation {
497483
})
498484
.collect::<Vec<_>>();
499485

500-
serde_json::to_string(&formatted_paths).unwrap()
486+
let prompt = serde_json::to_string(&formatted_paths).unwrap();
487+
488+
ctx.track_query(
489+
EventData::input_stage("path search")
490+
.with_payload("query", query)
491+
.with_payload("is_semantic", is_semantic)
492+
.with_payload("results", &paths)
493+
.with_payload("raw_prompt", &prompt),
494+
);
495+
496+
prompt
501497
}
502498

503499
Action::Code { query } => {
@@ -665,6 +661,7 @@ impl Conversation {
665661
.by_path(repo_ref, &path)
666662
.await
667663
.with_context(|| format!("failed to read path: {path}"))?
664+
.with_context(|| format!("path does not exist in the index: {path}"))?
668665
.content
669666
.lines()
670667
.enumerate()
@@ -878,27 +875,34 @@ impl Conversation {
878875
let alias = path_aliases[0];
879876
let path = self.paths[alias].clone();
880877

881-
let file_contents = ctx
878+
let doc = ctx
882879
.app
883880
.indexes
884881
.file
885882
.by_path(&self.repo_ref, &path)
886883
.await
887-
.with_context(|| format!("failed to read path: {}", path))?
888-
.content;
889-
890-
let bpe =
891-
tiktoken_rs::get_bpe_from_model("gpt-4").context("invalid model requested")?;
892-
893-
let trimmed_file_contents = limit_tokens(&file_contents, bpe, 4000);
894-
895-
vec![CodeChunk {
896-
alias: alias as u32,
897-
path,
898-
start_line: 1,
899-
end_line: trimmed_file_contents.lines().count() as u32 + 1,
900-
snippet: trimmed_file_contents.to_owned(),
901-
}]
884+
.with_context(|| format!("failed to read path: {}", path))?;
885+
886+
match doc {
887+
Some(doc) => {
888+
let bpe = tiktoken_rs::get_bpe_from_model("gpt-4")
889+
.context("invalid model requested")?;
890+
891+
let trimmed_file_contents = limit_tokens(&doc.content, bpe, 4000);
892+
893+
vec![CodeChunk {
894+
alias: alias as u32,
895+
path,
896+
start_line: 1,
897+
end_line: trimmed_file_contents.lines().count() as u32 + 1,
898+
snippet: trimmed_file_contents.to_owned(),
899+
}]
900+
}
901+
None => {
902+
warn!("only path alias did not return any results");
903+
vec![]
904+
}
905+
}
902906
} else {
903907
self.code_chunks
904908
.iter()
@@ -1291,6 +1295,7 @@ impl Conversation {
12911295
.by_path(repo_ref, &path)
12921296
.await
12931297
.unwrap()
1298+
.unwrap_or_else(|| panic!("path did not exist in the index: {path}"))
12941299
.content;
12951300

12961301
chunks

server/bleep/src/webserver/file.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ pub(super) async fn handle<'a>(
3838
params.path.to_str().context("invalid file path")?,
3939
)
4040
.await
41-
.map_err(Error::internal)?;
41+
.map_err(Error::internal)?
42+
.ok_or_else(|| Error::user("file not found").with_status(StatusCode::NOT_FOUND))?;
4243

4344
Ok(json(FileResponse {
4445
contents: split_by_lines(&doc.content, &doc.line_end_indices, &params)?.to_string(),

server/bleep/src/webserver/hoverable.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ pub(super) async fn handle(
3131
let repo_ref = &payload.repo_ref.parse::<RepoRef>().map_err(Error::user)?;
3232

3333
let document = match indexes.file.by_path(repo_ref, &payload.relative_path).await {
34-
Ok(doc) => doc,
34+
Ok(Some(doc)) => doc,
35+
Ok(None) => return Err(Error::user("file not found").with_status(StatusCode::NOT_FOUND)),
3536
Err(e) => return Err(Error::user(e)),
3637
};
3738

server/bleep/src/webserver/intelligence.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ pub(super) async fn handle(
5959
.file
6060
.by_path(&repo_ref, &payload.relative_path)
6161
.await
62-
.map_err(Error::user)?;
62+
.map_err(Error::user)?
63+
.ok_or_else(|| Error::user("path not found").with_status(StatusCode::NOT_FOUND))?;
6364
let lang = source_document.lang.as_deref();
6465
let all_docs = {
6566
let associated_langs = match lang.map(TSLanguage::from_id) {

0 commit comments

Comments
 (0)