diff --git a/Cargo.lock b/Cargo.lock index 888e718d1e84..97f9f660d162 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3127,6 +3127,7 @@ dependencies = [ "tabby-scheduler", "tantivy", "textdistance", + "thiserror", "tokio", "tower", "tower-http 0.4.0", @@ -3400,18 +3401,18 @@ checksum = "d321c8576c2b47e43953e9cce236550d4cd6af0a6ce518fe084340082ca6037b" [[package]] name = "thiserror" -version = "1.0.45" +version = "1.0.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dedd246497092a89beedfe2c9f176d44c1b672ea6090edc20544ade01fbb7ea0" +checksum = "1177e8c6d7ede7afde3585fd2513e611227efd6481bd78d2e82ba1ce16557ed4" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.45" +version = "1.0.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d7b1fadccbbc7e19ea64708629f9d8dccd007c260d66485f20a6d41bc1cf4b3" +checksum = "10712f02019e9288794769fba95cd6847df9874d49d871d062172f9dd41bc4cc" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index bb913941b7f3..f0d25cd78b48 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,3 +37,4 @@ tokenizers = "0.13.4-rc3" futures = "0.3.28" async-stream = "0.3.5" regex = "1.10.0" +thiserror = "1.0.49" diff --git a/crates/tabby/Cargo.toml b/crates/tabby/Cargo.toml index 6f75c887bfdc..2076f73b67c2 100644 --- a/crates/tabby/Cargo.toml +++ b/crates/tabby/Cargo.toml @@ -42,6 +42,7 @@ axum-streams = { version = "0.9.1", features = ["json"] } minijinja = { version = "1.0.8", features = ["loader"] } textdistance = "1.0.2" regex.workspace = true +thiserror.workspace = true [target.'cfg(all(target_os="macos", target_arch="aarch64"))'.dependencies] llama-cpp-bindings = { path = "../llama-cpp-bindings" } diff --git a/crates/tabby/src/serve/completions/prompt.rs b/crates/tabby/src/serve/completions/prompt.rs index 1825f4b2b60d..276e15ab2f9c 100644 --- a/crates/tabby/src/serve/completions/prompt.rs +++ b/crates/tabby/src/serve/completions/prompt.rs @@ -7,7 +7,10 @@ use textdistance::Algorithm; use tracing::warn; use super::{Segments, Snippet}; -use crate::serve::{completions::languages::get_language, search::IndexServer}; +use crate::serve::{ + completions::languages::get_language, + search::{IndexServer, IndexServerError}, +}; static MAX_SNIPPETS_TO_FETCH: usize = 20; static MAX_SNIPPET_CHARS_IN_PROMPT: usize = 768; @@ -109,7 +112,11 @@ fn collect_snippets(index_server: &IndexServer, language: &str, text: &str) -> V let serp = match index_server.search(&query_text, MAX_SNIPPETS_TO_FETCH, 0) { Ok(serp) => serp, - Err(err) => { + Err(IndexServerError::NotReady) => { + // Ignore. + return vec![]; + } + Err(IndexServerError::TantivyError(err)) => { warn!("Failed to search query: {}", err); return ret; } diff --git a/crates/tabby/src/serve/search.rs b/crates/tabby/src/serve/search.rs index 87a37d758cda..f8249a20d024 100644 --- a/crates/tabby/src/serve/search.rs +++ b/crates/tabby/src/serve/search.rs @@ -14,6 +14,7 @@ use tantivy::{ schema::Field, DocAddress, Document, Index, IndexReader, }; +use thiserror::Error; use tokio::{sync::OnceCell, task, time::sleep}; use tracing::{debug, instrument, log::info}; use utoipa::{IntoParams, ToSchema}; @@ -198,11 +199,25 @@ impl IndexServer { } } - pub fn search(&self, q: &str, limit: usize, offset: usize) -> tantivy::Result { + pub fn search( + &self, + q: &str, + limit: usize, + offset: usize, + ) -> Result { if let Some(imp) = self.get_cell() { - imp.search(q, limit, offset) + Ok(imp.search(q, limit, offset)?) } else { - Err(tantivy::TantivyError::InternalError("Not Ready".to_owned())) + Err(IndexServerError::NotReady) } } } + +#[derive(Error, Debug)] +pub enum IndexServerError { + #[error("index not ready")] + NotReady, + + #[error("underlying tantivy error")] + TantivyError(#[from] tantivy::TantivyError), +}