fix: persist LLM config to prefs store so Settings survive restarts
set_llm_config only mutated an in-memory Mutex; lib.rs always started from LlmConfig::default(). Config now round-trips through the same dm-pal-prefs.json store as dataDir, with a defaults fallback so a future shape change can't brick startup. Prefs consts now live in lib.rs (pub) instead of being mirrored per-module.
This commit is contained in:
@@ -3,11 +3,8 @@ use serde::{Deserialize, Serialize};
|
|||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use tauri::Manager;
|
use tauri::Manager;
|
||||||
use tauri_plugin_store::StoreExt;
|
use tauri_plugin_store::StoreExt;
|
||||||
|
use crate::{DATA_DIR_KEY, PREFS_STORE};
|
||||||
// ponytail: the data-dir pref store + key are defined in lib.rs; mirror them
|
// PREFS_STORE / DATA_DIR_KEY live in lib.rs (pub) — single source of truth.
|
||||||
// here so this module is self-contained for reads/writes.
|
|
||||||
const PREFS_STORE: &str = "dm-pal-prefs.json";
|
|
||||||
const DATA_DIR_KEY: &str = "dataDir";
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct DataDirInfo {
|
pub struct DataDirInfo {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ use crate::llm::{self, AppState, ChatMessage, ChatResponse, GenerateRequest, Llm
|
|||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use tauri::ipc::Channel;
|
use tauri::ipc::Channel;
|
||||||
use tauri::AppHandle;
|
use tauri::AppHandle;
|
||||||
|
use tauri_plugin_store::StoreExt;
|
||||||
|
|
||||||
// ─── Simple (non-streaming) generate ─────────────────────────
|
// ─── Simple (non-streaming) generate ─────────────────────────
|
||||||
|
|
||||||
@@ -95,9 +96,18 @@ pub fn get_llm_config(state: tauri::State<'_, AppState>) -> Result<crate::llm::L
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn set_llm_config(state: tauri::State<'_, AppState>, config: crate::llm::LlmConfig) -> Result<(), String> {
|
pub fn set_llm_config(
|
||||||
let mut lock = state.config.lock().map_err(|e| e.to_string())?;
|
app: AppHandle,
|
||||||
*lock = config;
|
state: tauri::State<'_, AppState>,
|
||||||
|
config: crate::llm::LlmConfig,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
let stored = serde_json::to_value(&config).map_err(|e| e.to_string())?;
|
||||||
|
*state.config.lock().map_err(|e| e.to_string())? = config;
|
||||||
|
// Persist so Settings survive restart. Same prefs store as dataDir;
|
||||||
|
// load_llm_config in lib.rs falls back to defaults if this ever unparses.
|
||||||
|
let store = app.store(crate::PREFS_STORE).map_err(|e| e.to_string())?;
|
||||||
|
store.set(crate::CONFIG_KEY, stored);
|
||||||
|
store.save().map_err(|e| e.to_string())?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+15
-3
@@ -12,8 +12,11 @@ use tauri_plugin_store::StoreExt;
|
|||||||
// ponytail: the data-dir preference lives in a tiny store at the OS default
|
// ponytail: the data-dir preference lives in a tiny store at the OS default
|
||||||
// app_data_dir so it's always discoverable at startup, even before we know the
|
// app_data_dir so it's always discoverable at startup, even before we know the
|
||||||
// custom location. Key: `dataDir` (absolute path). Empty/missing → default.
|
// custom location. Key: `dataDir` (absolute path). Empty/missing → default.
|
||||||
const PREFS_STORE: &str = "dm-pal-prefs.json";
|
// The persisted LLM config lives here too (key `llmConfig`) so Settings
|
||||||
const DATA_DIR_KEY: &str = "dataDir";
|
// survive restarts. pub: commands modules read/write through crate::.
|
||||||
|
pub const PREFS_STORE: &str = "dm-pal-prefs.json";
|
||||||
|
pub const DATA_DIR_KEY: &str = "dataDir";
|
||||||
|
pub const CONFIG_KEY: &str = "llmConfig";
|
||||||
|
|
||||||
/// Resolve the data dir: the configured one if set and usable, else the
|
/// Resolve the data dir: the configured one if set and usable, else the
|
||||||
/// default `$APPDATA/dm-toolkit`.
|
/// default `$APPDATA/dm-toolkit`.
|
||||||
@@ -34,6 +37,15 @@ fn resolve_data_dir(app: &tauri::AppHandle) -> std::path::PathBuf {
|
|||||||
default
|
default
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Load the persisted LLM config (Settings). Falls back to defaults when
|
||||||
|
/// missing or unparsable — a future config-shape change never bricks startup;
|
||||||
|
/// the DM just re-enters Settings once.
|
||||||
|
fn load_llm_config(app: &tauri::AppHandle) -> llm::LlmConfig {
|
||||||
|
let Ok(store) = app.store(PREFS_STORE) else { return llm::LlmConfig::default() };
|
||||||
|
let Some(v) = store.get(CONFIG_KEY) else { return llm::LlmConfig::default() };
|
||||||
|
serde_json::from_value(v).unwrap_or_default()
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||||
pub fn run() {
|
pub fn run() {
|
||||||
tauri::Builder::default()
|
tauri::Builder::default()
|
||||||
@@ -48,7 +60,7 @@ pub fn run() {
|
|||||||
let gen = generations::GenerationStore::open(&data_dir)
|
let gen = generations::GenerationStore::open(&data_dir)
|
||||||
.expect("open generations db");
|
.expect("open generations db");
|
||||||
app.manage(AppState {
|
app.manage(AppState {
|
||||||
config: Mutex::new(llm::LlmConfig::default()),
|
config: Mutex::new(load_llm_config(&app.handle())),
|
||||||
rag,
|
rag,
|
||||||
gen,
|
gen,
|
||||||
data_dir,
|
data_dir,
|
||||||
|
|||||||
Reference in New Issue
Block a user