Something went wrong. Try again.
A rust frontend for LLM's using the openai api.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107#[cfg(not(any(target_os = "android", target_os = "ios")))]use anyhow::Context;#[cfg(target_os = "android")]use dioxus::core::bail;use dioxus::logger::tracing::info;
use crate::{ appstate::{settings::Settings, state::State, storage::Storage}, components::settings::SettingsTab,};
use std::{ collections::HashMap, fs::{self, File}, path::PathBuf,};
#[derive(PartialEq, Clone, Debug)]pub struct App { pub settings: Settings, pub storage: Storage, pub state: State,}
impl App { pub fn load() -> anyhow::Result<App> { let (storage_path, settings_path) = Self::get_paths()?;
let storage = if !storage_path.exists() { Storage::default() } else { let storage_file = File::open(storage_path)?; // if (storage_file.read) let storage = Storage::load(&storage_file)?;
info!("Loaded storage file from storage"); storage };
let settings = if !settings_path.exists() { Settings::default() } else { let settings_file = File::open(settings_path)?; let settings = Settings::load(&settings_file)?;
info!("Loaded settings file from storage"); settings };
Ok(App { settings, storage, state: State { selected_chat: None, models: HashMap::new(), settings_shown: false, selected_settings: SettingsTab::default(), }, }) }
pub fn save(&self) -> anyhow::Result<()> { info!("Saving data..."); let (storage_path, settings_path) = Self::get_paths()?;
let mut storage_file = File::create(storage_path)?; let mut settings_file = File::create(settings_path)?;
self.storage.save(&mut storage_file)?; self.settings.save(&mut settings_file)?;
Ok(()) }
fn get_paths() -> anyhow::Result<(PathBuf, PathBuf)> { #[cfg(not(any(target_os = "android", target_os = "ios")))] let mut data_dir = dirs::data_dir().context("Couldn't find this system's data dir!")?; let mut config_dir = dirs::config_dir().context("Couldn't find this system's config dir!")?;
#[cfg(target_os = "android")] bail!("Unable to handle this OS.. for now");
data_dir.push("llm-river"); config_dir.push("llm-river");
fs::create_dir_all(&data_dir)?; fs::create_dir_all(&config_dir)?;
let storage_path = { let mut settings_file = data_dir.clone(); settings_file.push("storage.json");
settings_file };
let settings_path = { let mut settings_file = config_dir.clone(); settings_file.push("settings.toml");
settings_file };
Ok((storage_path, settings_path)) }}