feat: native folder/file picker, persistent config, and model management

This commit is contained in:
Evandro Fonseca Junior committed 2026-03-26 21:44:39 -03:00
1 parent d1d77b83ef
commit 2cac674cba
5 files changed
+506 -195

No files matched your search

+1
View File
@@ -21,6 +21,7 @@ tauri-build = { version = "2", features = [] }
[dependencies]
tauri = { version = "2", features = [] }
tauri-plugin-shell = "2"
tauri-plugin-dialog = "2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
@@ -5,6 +5,7 @@
"windows": ["main"],
"permissions": [
"core:default",
"shell:allow-open"
"shell:allow-open",
"dialog:allow-open"
]
}
+145 -54
View File
@@ -1,18 +1,24 @@
use std::path::Path;
use std::process::{Child, Command};
use std::sync::Mutex;
use tauri::{Manager, State};
use tauri::{AppHandle, Manager, State};
use tauri_plugin_dialog::DialogExt;
pub struct ServerState(pub Mutex<Option<Child>>);
/// Query GPU info via nvidia-smi.
// ─── GPU ──────────────────────────────────────────────────────────────────────
#[derive(serde::Serialize)]
pub struct GpuInfo {
pub name: String,
pub vram_mib: u32,
pub recommended_layers: i32,
}
#[tauri::command]
fn query_gpu() -> Result<GpuInfo, String> {
let output = Command::new("nvidia-smi")
.args([
"--query-gpu=name,memory.total",
"--format=csv,noheader,nounits",
])
.args(["--query-gpu=name,memory.total", "--format=csv,noheader,nounits"])
.output()
.map_err(|_| "nvidia-smi not found — no NVIDIA GPU detected".to_string())?;
@@ -36,21 +42,11 @@ fn query_gpu() -> Result<GpuInfo, String> {
let usable = vram_mib.saturating_sub(1024);
let recommended_layers = (usable / 80).min(200) as i32;
Ok(GpuInfo {
name,
vram_mib,
recommended_layers,
})
Ok(GpuInfo { name, vram_mib, recommended_layers })
}
#[derive(serde::Serialize)]
pub struct GpuInfo {
pub name: String,
pub vram_mib: u32,
pub recommended_layers: i32,
}
// ─── Server lifecycle ─────────────────────────────────────────────────────────
/// Start the inference server with full configuration.
#[tauri::command]
fn start_server(
state: State<ServerState>,
@@ -60,16 +56,11 @@ fn start_server(
ctx_size: u32,
) -> Result<String, String> {
let mut guard = state.0.lock().map_err(|e| e.to_string())?;
if guard.is_some() {
return Err("Server is already running".into());
}
let binary = if cfg!(target_os = "windows") {
"localllm.exe"
} else {
"localllm"
};
let binary = if cfg!(target_os = "windows") { "localllm.exe" } else { "localllm" };
let child = Command::new(binary)
.env("LOCALLLM_MODEL", &model_path)
@@ -81,20 +72,16 @@ fn start_server(
let pid = child.id();
*guard = Some(child);
Ok(format!("Server started (pid={pid}) on {addr}"))
}
#[tauri::command]
fn stop_server(state: State<ServerState>) -> Result<String, String> {
let mut guard = state.0.lock().map_err(|e| e.to_string())?;
match guard.as_mut() {
None => Err("Server is not running".into()),
Some(child) => {
child
.kill()
.map_err(|e| format!("Failed to kill server: {e}"))?;
child.kill().map_err(|e| format!("Failed to kill server: {e}"))?;
let _ = child.wait();
*guard = None;
Ok("Server stopped".into())
@@ -105,11 +92,15 @@ fn stop_server(state: State<ServerState>) -> Result<String, String> {
#[tauri::command]
fn server_status(state: State<ServerState>) -> String {
let guard = state.0.lock().unwrap();
if guard.is_some() {
"running".into()
} else {
"stopped".into()
}
if guard.is_some() { "running".into() } else { "stopped".into() }
}
// ─── Model files ──────────────────────────────────────────────────────────────
#[derive(serde::Serialize, serde::Deserialize, Clone)]
pub struct ModelFile {
pub name: String,
pub path: String,
}
/// Scan a directory for .gguf model files.
@@ -131,16 +122,9 @@ fn scan_models(dir: String) -> Result<Vec<ModelFile>, String> {
if p.is_file() {
if let Some(ext) = p.extension() {
if ext.eq_ignore_ascii_case("gguf") {
let name = p
.file_name()
.unwrap_or_default()
.to_string_lossy()
.to_string();
let name = p.file_name().unwrap_or_default().to_string_lossy().to_string();
let full_path = p.to_string_lossy().to_string();
models.push(ModelFile {
name,
path: full_path,
});
models.push(ModelFile { name, path: full_path });
}
}
}
@@ -150,13 +134,67 @@ fn scan_models(dir: String) -> Result<Vec<ModelFile>, String> {
Ok(models)
}
/// Open a native folder picker and scan the chosen directory for .gguf files.
/// Returns (chosen_dir, models[]).
#[derive(serde::Serialize)]
pub struct PickDirResult {
pub dir: String,
pub models: Vec<ModelFile>,
}
#[tauri::command]
async fn pick_models_dir(app: AppHandle) -> Result<PickDirResult, String> {
let folder = app
.dialog()
.file()
.set_title("Select models folder")
.blocking_pick_folder();
match folder {
Some(path) => {
let dir = path.to_string();
let models = scan_models(dir.clone())?;
Ok(PickDirResult { dir, models })
}
None => Err("cancelled".into()),
}
}
/// Open a native multi-file picker filtered to .gguf files.
/// Returns the selected ModelFile list (files in their original locations — no copy).
#[tauri::command]
async fn pick_model_files(app: AppHandle) -> Result<Vec<ModelFile>, String> {
let files = app
.dialog()
.file()
.set_title("Select model files")
.add_filter("GGUF models", &["gguf"])
.blocking_pick_files();
match files {
Some(paths) => {
let models: Vec<ModelFile> = paths
.iter()
.map(|p| {
let path_str = p.to_string();
let name = Path::new(&path_str)
.file_name()
.unwrap_or_default()
.to_string_lossy()
.to_string();
ModelFile { name, path: path_str }
})
.collect();
Ok(models)
}
None => Err("cancelled".into()),
}
}
/// Copy a .gguf file into the models directory.
/// Returns the new ModelFile on success.
#[tauri::command]
fn import_model(src_path: String, dest_dir: String) -> Result<ModelFile, String> {
let src = Path::new(&src_path);
// Validate source
if !src.exists() {
return Err(format!("Source file not found: {src_path}"));
}
@@ -168,7 +206,6 @@ fn import_model(src_path: String, dest_dir: String) -> Result<ModelFile, String>
return Err("Only .gguf files are supported".into());
}
// Ensure destination directory exists
let dest_dir_path = Path::new(&dest_dir);
if !dest_dir_path.exists() {
std::fs::create_dir_all(dest_dir_path)
@@ -178,7 +215,6 @@ fn import_model(src_path: String, dest_dir: String) -> Result<ModelFile, String>
let file_name = src.file_name().unwrap_or_default();
let dest_path = dest_dir_path.join(file_name);
// If already in the same directory, just return it
if src == dest_path {
return Ok(ModelFile {
name: file_name.to_string_lossy().to_string(),
@@ -186,7 +222,6 @@ fn import_model(src_path: String, dest_dir: String) -> Result<ModelFile, String>
});
}
// Copy the file
std::fs::copy(src, &dest_path).map_err(|e| format!("Failed to copy model: {e}"))?;
Ok(ModelFile {
@@ -195,24 +230,80 @@ fn import_model(src_path: String, dest_dir: String) -> Result<ModelFile, String>
})
}
#[derive(serde::Serialize, Clone)]
pub struct ModelFile {
pub name: String,
pub path: String,
// ─── Persistent config ────────────────────────────────────────────────────────
/// All user-configurable settings, persisted to app data dir as config.json.
#[derive(serde::Serialize, serde::Deserialize, Clone, Default)]
pub struct AppConfig {
pub models_dir: Option<String>,
pub model_path: Option<String>,
pub addr: Option<String>,
pub gpu_layers: Option<i32>,
pub ctx_size: Option<u32>,
pub api_key: Option<String>,
pub server_url: Option<String>,
/// Extra model paths added via file picker (not necessarily in models_dir)
pub extra_model_paths: Option<Vec<String>>,
}
fn config_path(app: &AppHandle) -> Result<std::path::PathBuf, String> {
let data_dir = app
.path()
.app_data_dir()
.map_err(|e| format!("Cannot resolve app data dir: {e}"))?;
std::fs::create_dir_all(&data_dir)
.map_err(|e| format!("Cannot create app data dir: {e}"))?;
Ok(data_dir.join("config.json"))
}
#[tauri::command]
fn save_config(app: AppHandle, config: AppConfig) -> Result<(), String> {
let path = config_path(&app)?;
let json = serde_json::to_string_pretty(&config)
.map_err(|e| format!("Serialization error: {e}"))?;
std::fs::write(&path, json).map_err(|e| format!("Write error: {e}"))?;
Ok(())
}
#[tauri::command]
fn load_config(app: AppHandle) -> Result<AppConfig, String> {
let path = config_path(&app)?;
if !path.exists() {
return Ok(AppConfig::default());
}
let json = std::fs::read_to_string(&path).map_err(|e| format!("Read error: {e}"))?;
serde_json::from_str(&json).map_err(|e| format!("Parse error: {e}"))
}
/// Returns the app data directory path (useful for debugging).
#[tauri::command]
fn get_app_data_dir(app: AppHandle) -> Result<String, String> {
app.path()
.app_data_dir()
.map(|p| p.to_string_lossy().to_string())
.map_err(|e| format!("Cannot resolve app data dir: {e}"))
}
// ─── App entry ────────────────────────────────────────────────────────────────
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_dialog::init())
.manage(ServerState(Mutex::new(None)))
.invoke_handler(tauri::generate_handler![
query_gpu,
scan_models,
pick_models_dir,
pick_model_files,
import_model,
start_server,
stop_server,
server_status
server_status,
save_config,
load_config,
get_app_data_dir,
])
.setup(|app| {
#[cfg(debug_assertions)]