diff options
author | Oxbian <oxbian@mailbox.org> | 2025-03-02 21:32:20 -0500 |
---|---|---|
committer | Oxbian <oxbian@mailbox.org> | 2025-03-02 21:32:20 -0500 |
commit | e4eaecc8ce7fb3e84977c41597eff80edd4d73c7 (patch) | |
tree | fd33e7d93a680b2e0e93cb0eacb66137bd77ecab | |
parent | 25cf2d92f3198ba7541dad979eca1f9c1238ff04 (diff) | |
download | NAI-e4eaecc8ce7fb3e84977c41597eff80edd4d73c7.tar.gz NAI-e4eaecc8ce7fb3e84977c41597eff80edd4d73c7.zip |
feat: conversation saved in file
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/app/init.rs | 6 | ||||
-rw-r--r-- | src/app/llm.rs | 24 | ||||
-rw-r--r-- | src/helper/init.rs | 14 |
5 files changed, 34 insertions, 14 deletions
@@ -1,2 +1,4 @@ /target +log.txt debug.txt +conv/* @@ -4,9 +4,11 @@ version = "0.1.0" edition = "2021" [dependencies] +chrono = "0.4.40" color-eyre = "0.6.3" ratatui = "0.29.0" reqwest = { version = "0.12.12", features = ["blocking", "json"] } serde = { version = "1.0.217", features = ["derive"] } serde_json = "1.0.138" tokio = "1.43.0" +uuid = { version = "1.15.1", features = ["v4"] } diff --git a/src/app/init.rs b/src/app/init.rs index aa74ae5..f62b2d0 100644 --- a/src/app/init.rs +++ b/src/app/init.rs @@ -1,9 +1,11 @@ use crate::app::llm::{Message, MessageType, LLM}; -use crate::helper::init::print_in_file; +use crate::helper::init::warn; +use uuid::Uuid; use tokio; pub struct App { pub messages: Vec<Message>, // History of recorded message + conv_id: Uuid, chat_llm: LLM, resume_llm: LLM, } @@ -16,6 +18,7 @@ impl App { MessageType::SYSTEM, chat_llm.system_prompt.clone(), )], + conv_id: Uuid::new_v4(), chat_llm, resume_llm: LLM::new("config/resume-LLM.json".to_string()).unwrap(), } @@ -23,6 +26,7 @@ impl App { fn append_message(&mut self, msg: String, role: MessageType) { let message = Message::new(role, msg); + message.save_message(self.conv_id.to_string()); self.messages.push(message); } diff --git a/src/app/llm.rs b/src/app/llm.rs index 8603395..59e045b 100644 --- a/src/app/llm.rs +++ b/src/app/llm.rs @@ -1,9 +1,10 @@ -use crate::helper::init::print_in_file; +use crate::helper::init::warn; use reqwest::{header::CONTENT_TYPE, Client}; use serde::{Deserialize, Serialize}; use serde_json::Value; use std::fmt; -use std::fs; +use std::fs::{OpenOptions, create_dir_all}; +use std::io::Write; #[derive(Deserialize, Debug)] pub struct LLM { @@ -40,7 +41,7 @@ impl LLM { while let Some(chunk) = res.chunk().await? { let answer: Value = serde_json::from_slice(chunk.as_ref())?; - print_in_file(answer.to_string()); + warn(answer.to_string()); if answer["done"].as_bool().unwrap_or(false) { break; } @@ -53,7 +54,7 @@ impl LLM { Err(e) => return Err(Box::new(e)), } - print_in_file(full_message.clone()); + warn(full_message.clone()); Ok(full_message) } } @@ -85,6 +86,21 @@ impl Message { pub fn new(role: MessageType, content: String) -> Message { Message { role, content } } + + pub fn save_message(&self, conv_id: String) { + // Create conv directory if doesn't exist + create_dir_all("conv").unwrap(); + + // Save message + let mut file = OpenOptions::new() + .write(true) + .append(true) + .create(true) + .open("conv/".to_string() + &conv_id) + .unwrap(); + + writeln!(file, "{}", serde_json::to_string(self).unwrap()).unwrap(); + } } impl fmt::Display for Message { diff --git a/src/helper/init.rs b/src/helper/init.rs index 084caf5..abee146 100644 --- a/src/helper/init.rs +++ b/src/helper/init.rs @@ -1,18 +1,14 @@ +use chrono::prelude::*; use std::fs::OpenOptions; use std::io::{self, Write}; -pub fn print_in_file(content: String) -> io::Result<()> { - // Open the file (create it if it doesn't exist, or truncate it if it does) +pub fn warn(content: String) { let mut file = OpenOptions::new() .write(true) .append(true) .create(true) - .open("debug.txt") + .open("log.txt") .unwrap(); - - if let Err(e) = writeln!(file, "{}", content) { - eprintln!("Couldn't write to file: {}", e); - } - - Ok(()) + let utc: DateTime<Local> = Local::now(); + writeln!(file, "[{}] {}", utc, content); } |