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

Commit 9fe3efc

Browse files
committed
adding some error handling
1 parent 639aa16 commit 9fe3efc

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

server/bleep/src/agent/symbol.rs

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::llm_gateway;
77
use crate::llm_gateway::api::{Function, FunctionCall};
88
use crate::webserver::hoverable::{inner_handle, HoverableRequest, HoverableResponse};
99
use crate::webserver::intelligence::{inner_handle as token_info, TokenInfoRequest};
10-
use crate::webserver::Error;
10+
use tracing::log::warn;
1111

1212
pub struct ChunkRefDef {
1313
pub chunk: CodeChunk,
@@ -151,7 +151,7 @@ impl Agent {
151151
&self,
152152
query: &str,
153153
chunks_with_symbols: Vec<ChunkRefDef>,
154-
) -> Result<RefDefMetadata, Error> {
154+
) -> Result<RefDefMetadata, SymbolError> {
155155
let mut i: i32 = -1;
156156
// we have multiples chunks and each chunk may have multiple symbols
157157
// unique alias (i) per symbol
@@ -171,6 +171,9 @@ impl Agent {
171171
)
172172
})
173173
.collect::<Vec<_>>();
174+
if i == -1 {
175+
return Err(SymbolError::SymbolListEmptyError);
176+
}
174177

175178
// Classifier
176179

@@ -220,24 +223,36 @@ impl Agent {
220223
let llm_response = self
221224
.llm_with_function_call(prompt, filter_function)
222225
.await
223-
.unwrap_or(FunctionCall {
224-
name: Some("filter".to_string()),
225-
arguments: "{\"symbol\": 0}".to_string(),
226+
.unwrap_or({
227+
warn!("Symbol classfier llm call failed, picking the first symbol.");
228+
FunctionCall {
229+
name: Some("filter".to_string()),
230+
arguments: "{\"symbol\": 0}".to_string(),
231+
}
226232
});
227233

228234
let filter_argument: Filter =
229-
serde_json::from_str(llm_response.arguments.as_str()).unwrap();
235+
match serde_json::from_str(llm_response.clone().arguments.as_str()) {
236+
Ok(argument) => argument,
237+
Err(_e) => {
238+
warn!("Cannot deserialize: {:?}", llm_response);
239+
return Err(SymbolError::DeserializeFilterError);
240+
}
241+
};
230242

231243
let selected_symbol = filter_argument.symbol;
232244

233245
// finding symbol metadata
234-
let output = symbols
246+
let output = match symbols
235247
.into_iter()
236248
.flat_map(|(_, symbol_with_alias)| symbol_with_alias)
237249
.find(|(alias, _)| alias.clone() == selected_symbol as i32)
238-
.unwrap();
250+
{
251+
Some((_alias, symbol_metadata)) => Ok(symbol_metadata),
252+
_ => Err(SymbolError::SymbolOutOfBoundsError)
253+
};
239254

240-
Ok(output.1)
255+
output
241256
}
242257

243258
pub async fn get_ref_def_extra_chunks(&mut self, chunks: Vec<CodeChunk>) -> Vec<CodeChunk> {
@@ -256,10 +271,13 @@ impl Agent {
256271
let user_query = self.last_exchange().query.target().unwrap();
257272

258273
// select one symbol
259-
let selected_symbol = self
260-
.filter_symbols(&user_query, chunks_with_symbols)
261-
.await
262-
.unwrap();
274+
let selected_symbol = match self.filter_symbols(&user_query, chunks_with_symbols).await {
275+
Ok(selected_symbol) => selected_symbol,
276+
Err(e) => {
277+
warn!("Returning no extra chunks: {}", e);
278+
return Vec::new();
279+
}
280+
};
263281

264282
// get expanded chunks for selected symbol
265283
let extra_chunks = self.expand_symbol_into_chunks(selected_symbol).await;
@@ -333,3 +351,13 @@ pub struct RefDefMetadata {
333351
struct Filter {
334352
symbol: usize,
335353
}
354+
355+
#[derive(thiserror::Error, Debug)]
356+
pub enum SymbolError {
357+
#[error("No symbol retrieved in the provided chunks")]
358+
SymbolListEmptyError,
359+
#[error("Cannot deserialize llm function call arguments")]
360+
DeserializeFilterError,
361+
#[error("Selected symbol out of bounds")]
362+
SymbolOutOfBoundsError,
363+
}

0 commit comments

Comments
 (0)