Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/experiment #59

Merged
merged 13 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"react-hook-form": "^7.49.3",
"react-hotkeys-hook": "^4.5.0",
"tailwind-merge": "^2.2.0",
"tailwind-scrollbar": "^3.1.0",
"tailwindcss": "^3.4.1",
"tailwindcss-animate": "^1.0.7",
"uuid": "^9.0.1",
Expand Down
3 changes: 2 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ edition = "2021"
tauri-build = { version = "1.5", features = [] }

[dependencies]
tauri = { version = "1.6", features = [ "fs-all", "dialog-all", "shell-open"] }
tauri = { version = "1.6", features = [ "path-all", "fs-all", "dialog-all", "shell-open"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
anyhow = "1.0.79"
Expand All @@ -27,6 +27,7 @@ ollama-rs = { version = "0.2.0", default-features = false, features = ["rustls"]
chrono = "0.4.38"
reqwest = {version = "0.12.4", features = ["blocking", "json", "rustls-tls"], default-features = false }
sqlx = { version = "0.8.1", features = ["runtime-tokio", "sqlite", "chrono"] }
eff-wordlist = "1.0.3"

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
Expand Down
117 changes: 33 additions & 84 deletions src-tauri/src/commands/experiment.rs
Original file line number Diff line number Diff line change
@@ -1,93 +1,42 @@
use grid_search_desktop::{Error, ExperimentFile};
use std::fs;

#[tauri::command]
pub fn get_experiments(app_handle: tauri::AppHandle) -> Result<Vec<ExperimentFile>, Error> {
let binding = app_handle.path_resolver().app_data_dir().unwrap();
let app_data_dir = binding.to_str().unwrap();
let mut files: Vec<ExperimentFile> = fs::read_dir(app_data_dir)?
.filter_map(Result::ok)
.filter_map(|entry| {
let path = entry.path();
if path.extension().unwrap_or_default() != "json" {
return None;
}
let metadata = fs::metadata(&path).ok()?;
let created = metadata.created().ok()?;
let contents = fs::read_to_string(&path).ok()?;
Some(ExperimentFile {
name: path.file_name()?.to_string_lossy().into_owned(),
created,
contents,
})
})
.collect();
use crate::db::DatabaseState;

files.sort_by_key(|file| file.created);
files.reverse();
Ok(files)
#[tauri::command]
pub async fn get_experiments(
state: tauri::State<'_, DatabaseState>,
) -> Result<Vec<ExperimentFile>, Error> {
let stmt = r#"
SELECT
name,
created,
contents
FROM experiments
ORDER BY id DESC
"#;

let query = sqlx::query_as::<_, ExperimentFile>(stmt);
let pool = &state.0;
let experiments = query.fetch_all(pool).await?;

println!("\nRetrieved {} experiments:", experiments.len());
Ok(experiments)
}

// ---
// Example below connects to the database
// ---

// #[tauri::command]
// pub async fn get_experiments(
// state: tauri::State<'_, DatabaseState>,
// ) -> Result<Vec<ExperimentFile>, Error> {
// // Access the database pool
// let pool = &state.0;

// // Example query
// let experiments =
// sqlx::query!("SELECT name, created, contents FROM experiments ORDER BY created DESC")
// .fetch_all(pool)
// .await
// .map_err(|e| Error::StringError(e.to_string()))?;

// // Convert to your ExperimentFile struct
// let files: Vec<ExperimentFile> = experiments
// .into_iter()
// .map(|row| ExperimentFile {
// name: row.name,
// created: SystemTime::from(chrono::DateTime::parse_from_rfc3339(&row.created).unwrap()),
// contents: row.contents,
// })
// .collect();
#[tauri::command]
pub async fn delete_experiments(
state: tauri::State<'_, DatabaseState>,
uuid: String,
) -> Result<(), Error> {
let pool = &state.0;

// Ok(files)
// }
let stmt: &str = match uuid.as_str() {
"*" => "DELETE FROM experiments",
_ => "DELETE FROM experiments WHERE experiment_uuid = $1",
};

#[tauri::command]
pub fn delete_experiment_files(
app_handle: tauri::AppHandle,
file_name: String,
) -> Result<(), String> {
let app_data_dir = app_handle
.path_resolver()
.app_data_dir()
.ok_or_else(|| "Failed to get application data directory".to_string())?;
let _ = sqlx::query(stmt).bind(&uuid).execute(pool).await?;
print!("Deleted experiment with UUID: {}", uuid);

if file_name == "*" {
// Delete all JSON files in the directory
for entry in fs::read_dir(&app_data_dir).map_err(|e| e.to_string())? {
let entry = entry.map_err(|e| e.to_string())?;
let path = entry.path();
if path.is_file() && path.extension().map(|e| e == "json").unwrap_or(false) {
fs::remove_file(path).map_err(|e| e.to_string())?;
}
}
Ok(())
} else {
// Delete a specific file
let file_path = app_data_dir.join(file_name);
if file_path.exists() && file_path.is_file() {
fs::remove_file(file_path).map_err(|e| e.to_string())?;
Ok(())
} else {
// File doesn't exist, do nothing
Ok(())
}
}
Ok(())
}
14 changes: 4 additions & 10 deletions src-tauri/src/commands/llm.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::db::DatabaseState;
use reqwest::Client;
use serde_json::json;
use tokio::time::{self, Duration};
Expand Down Expand Up @@ -62,9 +63,9 @@ pub async fn get_ollama_version(config: IDefaultConfigs) -> Result<String, Error

#[tauri::command]
pub async fn get_inference(
state: tauri::State<'_, DatabaseState>,
config: IDefaultConfigs,
params: TParamIteration,
app_handle: tauri::AppHandle,
) -> Result<GenerationResponse, Error> {
// println!("----------------------------------------------------------");
// println!("Config and Params");
Expand Down Expand Up @@ -180,18 +181,11 @@ pub async fn get_inference(
dbg!(&res);
println!("---------------------------------------------");

// sets the base path for storing logs
// see https://github.com/tauri-apps/tauri/discussions/5557
let app_data_dir = app_handle
.path_resolver()
.app_data_dir()
.unwrap_or_else(|| panic!("Failed to get application data directory"));
let app_data_dir_str = app_data_dir.to_string_lossy();

// Log the experiment if it's successful
let pool = &state.0;
match res {
Ok(generation_response) => {
log_experiment(&config, &params, &generation_response, &app_data_dir_str).await?;
log_experiment(pool, &config, &params, &generation_response).await?;
Ok(generation_response)
}
Err(err) => Err(Error::StringError(err.to_string())),
Expand Down
Loading
Loading