From b4dd7d51ee2e30c4445e628056b5cbf93d9d8cbb Mon Sep 17 00:00:00 2001 From: Oxbian Date: Sun, 9 Mar 2025 16:44:26 -0400 Subject: feat: log + error handling --- src/app/init.rs | 20 ++++++++++++++------ src/app/llm.rs | 16 ++++++++-------- 2 files changed, 22 insertions(+), 14 deletions(-) (limited to 'src/app') diff --git a/src/app/init.rs b/src/app/init.rs index f1f917a..dd8d3b7 100644 --- a/src/app/init.rs +++ b/src/app/init.rs @@ -12,7 +12,8 @@ pub struct App { impl App { pub fn new() -> App { - let chat_llm: LLM = LLM::new("config/chat-LLM.json".to_string()).unwrap(); + let chat_llm: LLM = LLM::new("config/chat-LLM.json".to_string()); + App { messages: vec![Message::new( MessageType::SYSTEM, @@ -20,13 +21,16 @@ impl App { )], conv_id: Uuid::new_v4(), chat_llm, - resume_llm: LLM::new("config/resume-LLM.json".to_string()).unwrap(), + resume_llm: LLM::new("config/resume-LLM.json".to_string()), } } fn append_message(&mut self, msg: String, role: MessageType) { let message = Message::new(role, msg); - message.save_message(self.conv_id.to_string()); + + let err = message.save_message(self.conv_id.to_string()); + warn(err.is_err().to_string()); + self.messages.push(message); } @@ -38,9 +42,13 @@ impl App { self.chat_llm.ask_format(&self.messages).await }); - let categorie = result.unwrap()[0]["function"]["arguments"]["category"].clone(); - - self.ask(categorie.to_string().as_str()); + match result { + Ok(msg) => { + let categorie = msg[0]["function"]["arguments"]["category"].clone(); + self.ask(categorie.to_string().as_str()); + }, + Err(e) => self.append_message(e.to_string(), MessageType::ASSISTANT), + } } fn ask(&mut self, mode: &str) { diff --git a/src/app/llm.rs b/src/app/llm.rs index a172855..9c6d222 100644 --- a/src/app/llm.rs +++ b/src/app/llm.rs @@ -15,11 +15,9 @@ pub struct LLM { } impl LLM { - pub fn new(config_file: String) -> Result> { - let contents = fs::read_to_string(config_file)?; - let llm: LLM = serde_json::from_str(&contents)?; - - Ok(llm) + pub fn new(config_file: String) -> LLM { + let contents = fs::read_to_string(config_file).unwrap(); + serde_json::from_str(&contents).unwrap() } pub async fn ask(&self, messages: &Vec) -> Result> { @@ -116,9 +114,9 @@ impl Message { Message { role, content } } - pub fn save_message(&self, conv_id: String) { + pub fn save_message(&self, conv_id: String) -> Result<(), Box> { // Create conv directory if doesn't exist - create_dir_all("conv").unwrap(); + create_dir_all("conv")?; // Save message let mut file = OpenOptions::new() @@ -128,7 +126,9 @@ impl Message { .open("conv/".to_string() + &conv_id) .unwrap(); - writeln!(file, "{}", serde_json::to_string(self).unwrap()).unwrap(); + writeln!(file, "{}", serde_json::to_string(self)?)?; + + Ok(()) } } -- cgit v1.2.3