feat: persist LLM/TTS backend choice + reset/repeat context + quick-search + diagnostics
Closes the UX hole I created in 5c72450: voice-swap to Ollama used to vanish
after restart. Plus P0.3 (long-standing roadmap item) and two new packs.
Persistent backend choice (crates/jarvis-core/src/db/structs.rs)
- Settings struct gains llm_backend + tts_backend fields (both String,
"" / "auto" = follow env/auto-detect).
- set("llm_backend", "groq"|"ollama"|"auto") validates input.
- llm::init_global() reads DB first, then JARVIS_LLM env, then auto-detect.
- llm::swap_to() now persists the choice via db::save_settings.
- Voice swap "переключись на локальный" now survives restart.
Shared conversation history (P0.3, crates/jarvis-core/src/llm/mod.rs)
- HISTORY: Lazy<RwLock<Option<ConversationHistory>>> singleton.
- Helpers: init_history, history_push_user, history_push_assistant,
history_snapshot, history_clear, history_pop_last_user, history_last_assistant.
- llm_fallback migrated off its own Mutex<History> — now reads/writes shared.
- ConversationHistory gains last_assistant() method.
New Lua APIs
- jarvis.llm_reset() → clear conversation turns (keeps system prompt).
- jarvis.llm_last_reply() → string or nil (last assistant message text).
- jarvis.health() → debug table {tts_backend, llm_backend, llm_model,
active_profile, memory_facts, scheduled_tasks,
language, voice, microphone, vosk_model,
noise_suppression}. No secrets included.
New voice packs
- resources/commands/llm_context/ (P0.3)
* "сбрось контекст" / "забудь разговор" → llm.reset
* "повтори последнее" / "повтори ответ" → llm.repeat (uses last_assistant)
- resources/commands/quick_search/ (imba P1 item)
* "найди в гугле <X>" / "загугли <X>"
* Uses DuckDuckGo Instant Answer API (api.duckduckgo.com, no key required).
Pulls AbstractText or RelatedTopics into LLM prompt; falls back to pure
LLM knowledge if DDG returns nothing useful. Speaks 2-4 sentence answer.
- resources/commands/diagnostics/
* "диагностика" / "доложи о себе" / "статус"
* Reads jarvis.health() and speaks a one-line summary. Useful when
debugging — user can read out their current state for a bug report.
Build: cargo build --release -p jarvis-app -p jarvis-gui green.
Tests: 52/52 jarvis-core unit tests pass.
This commit is contained in:
parent
5c7245012e
commit
385bd5c8ce
15 changed files with 413 additions and 37 deletions
|
|
@ -33,6 +33,19 @@ pub struct Settings {
|
|||
pub language: String,
|
||||
|
||||
pub api_keys: ApiKeys,
|
||||
|
||||
/// Preferred LLM backend ("groq" | "ollama" | empty=auto). Empty/auto means
|
||||
/// the JARVIS_LLM env var or auto-detect (Groq if GROQ_TOKEN set else Ollama)
|
||||
/// decides. When the user runs `jarvis.llm_switch(...)` the result is
|
||||
/// persisted here so it survives restart.
|
||||
#[serde(default)]
|
||||
pub llm_backend: String,
|
||||
|
||||
/// Preferred TTS backend ("sapi" | "piper" | "silero" | empty=auto). Same
|
||||
/// rules as `llm_backend`. Empty falls back to JARVIS_TTS env var, then
|
||||
/// auto-detect.
|
||||
#[serde(default)]
|
||||
pub tts_backend: String,
|
||||
}
|
||||
|
||||
fn default_intent_backend() -> String { config::DEFAULT_INTENT_BACKEND.to_string() }
|
||||
|
|
@ -60,6 +73,8 @@ impl Settings {
|
|||
"language" => Some(self.language.clone()),
|
||||
"api_key__picovoice" => Some(self.api_keys.picovoice.clone()),
|
||||
"api_key__openai" => Some(self.api_keys.openai.clone()),
|
||||
"llm_backend" => Some(self.llm_backend.clone()),
|
||||
"tts_backend" => Some(self.tts_backend.clone()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
|
@ -120,6 +135,22 @@ impl Settings {
|
|||
"api_key__openai" => {
|
||||
self.api_keys.openai = val.to_string();
|
||||
}
|
||||
"llm_backend" => {
|
||||
// empty / "auto" / "groq" / "ollama" — anything else rejected
|
||||
match val.trim().to_lowercase().as_str() {
|
||||
"" | "auto" => self.llm_backend = String::new(),
|
||||
"groq" => self.llm_backend = "groq".into(),
|
||||
"ollama" => self.llm_backend = "ollama".into(),
|
||||
other => return Err(format!("unknown llm_backend: '{}'", other)),
|
||||
}
|
||||
}
|
||||
"tts_backend" => {
|
||||
match val.trim().to_lowercase().as_str() {
|
||||
"" | "auto" => self.tts_backend = String::new(),
|
||||
"sapi" | "piper" | "silero" => self.tts_backend = val.to_lowercase(),
|
||||
other => return Err(format!("unknown tts_backend: '{}'", other)),
|
||||
}
|
||||
}
|
||||
_ => return Err(format!("unknown setting: '{}'", key)),
|
||||
}
|
||||
Ok(())
|
||||
|
|
@ -142,6 +173,8 @@ impl Settings {
|
|||
"language",
|
||||
"api_key__picovoice",
|
||||
"api_key__openai",
|
||||
"llm_backend",
|
||||
"tts_backend",
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
@ -173,6 +206,9 @@ impl Default for Settings {
|
|||
picovoice: String::from(""),
|
||||
openai: String::from(""),
|
||||
},
|
||||
|
||||
llm_backend: String::new(),
|
||||
tts_backend: String::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,6 +58,11 @@ impl ConversationHistory {
|
|||
}
|
||||
}
|
||||
|
||||
/// Return the most recent assistant message, or None if none yet.
|
||||
pub fn last_assistant(&self) -> Option<&ChatMessage> {
|
||||
self.turns.iter().rev().find(|m| m.role == "assistant")
|
||||
}
|
||||
|
||||
fn truncate(&mut self) {
|
||||
if self.turns.len() > self.max_turns {
|
||||
let drop = self.turns.len() - self.max_turns;
|
||||
|
|
|
|||
|
|
@ -12,6 +12,52 @@ use once_cell::sync::Lazy;
|
|||
use parking_lot::RwLock;
|
||||
use std::sync::Arc;
|
||||
|
||||
// ─── Shared conversation history ───────────────────────────────────────────
|
||||
//
|
||||
// Single global history so the LLM fallback chat, the Lua reset/repeat helpers,
|
||||
// and any future IPC handler all operate on the same buffer. Initialised on
|
||||
// boot via `init_history(system_prompt, max_turns)`.
|
||||
|
||||
static HISTORY: Lazy<RwLock<Option<ConversationHistory>>> = Lazy::new(|| RwLock::new(None));
|
||||
|
||||
pub fn init_history(system_prompt: impl Into<String>, max_turns: usize) {
|
||||
*HISTORY.write() = Some(ConversationHistory::new(system_prompt, max_turns));
|
||||
}
|
||||
|
||||
pub fn history_push_user(content: impl Into<String>) {
|
||||
if let Some(h) = HISTORY.write().as_mut() {
|
||||
h.push_user(content);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn history_push_assistant(content: impl Into<String>) {
|
||||
if let Some(h) = HISTORY.write().as_mut() {
|
||||
h.push_assistant(content);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn history_snapshot() -> Vec<ChatMessage> {
|
||||
HISTORY.read().as_ref().map(|h| h.snapshot()).unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn history_clear() {
|
||||
if let Some(h) = HISTORY.write().as_mut() {
|
||||
h.clear();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn history_pop_last_user() {
|
||||
if let Some(h) = HISTORY.write().as_mut() {
|
||||
h.pop_last_user();
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the most recent assistant message text, or None.
|
||||
pub fn history_last_assistant() -> Option<String> {
|
||||
HISTORY.read().as_ref()
|
||||
.and_then(|h| h.last_assistant().map(|m| m.content.clone()))
|
||||
}
|
||||
|
||||
/// Shared mutable LLM client. Modules read via `current()`; the user can
|
||||
/// hot-swap backends at runtime via `swap_to(LlmBackend::Ollama)` etc.
|
||||
///
|
||||
|
|
@ -19,12 +65,28 @@ use std::sync::Arc;
|
|||
/// `init_global()` was never called).
|
||||
static GLOBAL: Lazy<RwLock<Option<Arc<LlmClient>>>> = Lazy::new(|| RwLock::new(None));
|
||||
|
||||
/// Initialise the global client from env vars (`JARVIS_LLM`, `GROQ_TOKEN`, ...).
|
||||
/// Initialise the global client. Precedence:
|
||||
/// 1. Settings DB `llm_backend` field (if non-empty)
|
||||
/// 2. `JARVIS_LLM` env var
|
||||
/// 3. Auto-detect (Groq if `GROQ_TOKEN`, else Ollama)
|
||||
///
|
||||
/// Idempotent — replaces any previous global. Returns Ok even when the chosen
|
||||
/// backend can't be probed: an Ollama server may not be running yet but we
|
||||
/// stash the client anyway and let calls fail on first request.
|
||||
pub fn init_global() -> Result<(), ConfigError> {
|
||||
let c = LlmClient::from_env()?;
|
||||
// Try DB-persisted choice first.
|
||||
let persisted = crate::DB.get().and_then(|db| {
|
||||
let s = db.read();
|
||||
let backend = s.llm_backend.trim().to_string();
|
||||
if backend.is_empty() { None } else { parse_backend(&backend) }
|
||||
});
|
||||
|
||||
let c = match persisted {
|
||||
Some(LlmBackend::Groq) => LlmClient::groq()?,
|
||||
Some(LlmBackend::Ollama) => LlmClient::ollama(),
|
||||
None => LlmClient::from_env()?,
|
||||
};
|
||||
|
||||
log::info!("LLM global initialised: backend={}, model={}", c.backend().name(), c.model());
|
||||
*GLOBAL.write() = Some(Arc::new(c));
|
||||
Ok(())
|
||||
|
|
@ -45,6 +107,7 @@ pub fn current_backend_name() -> &'static str {
|
|||
}
|
||||
|
||||
/// Hot-swap to a different backend. Returns the new backend's name on success.
|
||||
/// Persists the choice to the settings DB so it survives restart.
|
||||
pub fn swap_to(backend: LlmBackend) -> Result<&'static str, ConfigError> {
|
||||
let c = match backend {
|
||||
LlmBackend::Groq => LlmClient::groq()?,
|
||||
|
|
@ -53,6 +116,21 @@ pub fn swap_to(backend: LlmBackend) -> Result<&'static str, ConfigError> {
|
|||
let name = c.backend().name();
|
||||
log::info!("LLM swapped → backend={}, model={}", name, c.model());
|
||||
*GLOBAL.write() = Some(Arc::new(c));
|
||||
|
||||
// Persist to settings DB (best-effort — log on failure, don't propagate).
|
||||
if let Some(db) = crate::DB.get() {
|
||||
let snapshot = {
|
||||
let mut s = db.write();
|
||||
s.llm_backend = name.to_string();
|
||||
s.clone()
|
||||
};
|
||||
if let Err(e) = crate::db::save_settings(&snapshot) {
|
||||
log::warn!("LLM swap: failed to persist to settings DB: {}", e);
|
||||
} else {
|
||||
log::info!("LLM choice persisted: {}", name);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(name)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,4 +12,5 @@ pub mod memory;
|
|||
pub mod profile;
|
||||
pub mod vision;
|
||||
pub mod scheduler;
|
||||
pub mod cmd;
|
||||
pub mod cmd;
|
||||
pub mod health;
|
||||
50
crates/jarvis-core/src/lua/api/health.rs
Normal file
50
crates/jarvis-core/src/lua/api/health.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
//! `jarvis.health()` — debug snapshot of active runtime state.
|
||||
//!
|
||||
//! Returns a table that can be JSON-encoded for bug reports. Doesn't include
|
||||
//! secrets (no API keys, no LLM history content).
|
||||
//!
|
||||
//! Example use from Lua:
|
||||
//! local h = jarvis.health()
|
||||
//! for k, v in pairs(h) do print(k, v) end
|
||||
|
||||
use mlua::{Lua, Table};
|
||||
|
||||
pub fn register(lua: &Lua, jarvis: &Table) -> mlua::Result<()> {
|
||||
let health_fn = lua.create_function(|lua, ()| {
|
||||
let t = lua.create_table()?;
|
||||
|
||||
// TTS
|
||||
t.set("tts_backend", crate::tts::backend().name())?;
|
||||
|
||||
// LLM
|
||||
t.set("llm_backend", crate::llm::current_backend_name())?;
|
||||
if let Some(c) = crate::llm::current() {
|
||||
t.set("llm_model", c.model().to_string())?;
|
||||
}
|
||||
|
||||
// Profile
|
||||
t.set("active_profile", crate::profiles::active_name())?;
|
||||
|
||||
// Memory size
|
||||
t.set("memory_facts", crate::long_term_memory::all().len())?;
|
||||
|
||||
// Scheduler size
|
||||
t.set("scheduled_tasks", crate::scheduler::list().len())?;
|
||||
|
||||
// Language
|
||||
t.set("language", crate::i18n::get_language())?;
|
||||
|
||||
// Voice from settings
|
||||
if let Some(db) = crate::DB.get() {
|
||||
let s = db.read();
|
||||
t.set("voice", s.voice.clone())?;
|
||||
t.set("microphone", s.microphone)?;
|
||||
t.set("vosk_model", s.vosk_model.clone())?;
|
||||
t.set("noise_suppression", format!("{:?}", s.noise_suppression))?;
|
||||
}
|
||||
|
||||
Ok(t)
|
||||
})?;
|
||||
jarvis.set("health", health_fn)?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -98,5 +98,25 @@ pub fn register(lua: &Lua, jarvis: &Table) -> mlua::Result<()> {
|
|||
})?;
|
||||
jarvis.set("llm_switch", switch_fn)?;
|
||||
|
||||
// Clear conversation turns (keeps system prompt). Returns true if history was
|
||||
// initialised, false if LLM is not configured.
|
||||
let reset_fn = lua.create_function(|_, ()| {
|
||||
llm::history_clear();
|
||||
Ok(true)
|
||||
})?;
|
||||
jarvis.set("llm_reset", reset_fn)?;
|
||||
|
||||
// Return the most recent assistant message text, or nil.
|
||||
let last_fn = lua.create_function(|lua, ()| {
|
||||
match llm::history_last_assistant() {
|
||||
Some(text) => {
|
||||
let s = lua.create_string(text)?;
|
||||
Ok(Value::String(s))
|
||||
}
|
||||
None => Ok(Value::Nil),
|
||||
}
|
||||
})?;
|
||||
jarvis.set("llm_last_reply", last_fn)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -82,6 +82,7 @@ impl LuaEngine {
|
|||
api::profile::register(&self.lua, &jarvis)?;
|
||||
api::scheduler::register(&self.lua, &jarvis)?;
|
||||
api::cmd::register(&self.lua, &jarvis)?;
|
||||
api::health::register(&self.lua, &jarvis)?;
|
||||
|
||||
// sandbox-controlled APIs
|
||||
if self.sandbox.allows_http() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue