feat: hot-swap LLM backend + media keys + codebase Q&A + scheduler cancel-by-text + TTS pre-warm

Single biggest user-facing change: you can now switch local↔cloud LLM by voice
without restarting. Plus four new packs and a perf nudge.

Shared LLM global (crates/jarvis-core/src/llm/mod.rs)
  - GLOBAL: Lazy<RwLock<Option<Arc<LlmClient>>>>. Single source of truth.
  - init_global() / current() / swap_to(LlmBackend) / current_backend_name() / parse_backend(str).
  - llm_fallback + llm_router migrated: they no longer own a separate LlmClient,
    they read from the global on each request. Hot-swap is now LIVE — change
    backend, next chat call uses the new one.
  - parse_backend accepts both english and russian aliases:
      "groq"/"cloud"/"облако"/"клауд"        → Groq
      "ollama"/"local"/"локал"/"локальный"  → Ollama

Voice + Lua LLM switcher (resources/commands/llm_switch/, 3 commands)
  - "переключись на локальный" / "перейди на оллама"  → swap to Ollama
  - "переключись на облако" / "используй грок"        → swap to Groq
  - "какой у тебя мозг" / "облако или локально"       → status query

  Underlying Lua API (registered globally):
    jarvis.llm({messages}, opts)   -- as before, now uses global client
    jarvis.llm_status()            -> "groq" | "ollama" | "none"
    jarvis.llm_switch(name)        -> name on success, nil on failure

Media keys pack (resources/commands/media_keys/, 4 commands)
  - "пауза" / "плей"           → VK_MEDIA_PLAY_PAUSE
  - "следующий трек"           → VK_MEDIA_NEXT_TRACK
  - "предыдущий трек"          → VK_MEDIA_PREV_TRACK
  - "стоп музыка"              → VK_MEDIA_STOP
  - Helper _media.ps1 uses user32.keybd_event (P/Invoke through Add-Type).
  - Works with anything that listens to global media keys: Spotify, Yandex Music,
    YouTube (focused tab), Foobar2000, Winamp, etc. No OAuth, no API keys.

Codebase Q&A pack (resources/commands/codebase_qa/, 3 commands)
  - "укажи проект <path>"             → stores path in jarvis.memory under codebase.root
  - "какой проект сейчас"             → speaks current path
  - "что делает функция X" / "найди в коде Y" / "объясни код"
    → walks the folder (depth 3, 30 files cap, 4KB per file, 50KB total),
      filters by source extensions (rs/py/ts/lua/go/...), feeds digest to LLM
      with a "senior reviewer" system prompt, speaks 3-5 sentence answer.

Scheduler cancel-by-text (crates/jarvis-core/src/scheduler.rs)
  - new pub fn find_by_text(query, limit) -> Vec<ScheduledTask>
  - new pub fn remove_by_text(query) -> usize (count removed)
  - new pure helper find_by_text_in(tasks, query, limit) for tests
  - Lua: jarvis.scheduler.{find_by_text, remove_by_text}
  - Voice: "отмени напоминание про воду" / "удали задачу про разминку"
  - 3 new unit tests (52 total in jarvis-core).

TTS pre-warm (crates/jarvis-app/src/main.rs)
  - Call jarvis_core::tts::backend() once on startup so the first speak()
    doesn't pay Piper binary discovery + voice loading cost. Cuts first-speak
    latency by ~150-300ms depending on backend.

Build: cargo build --release -p jarvis-app -p jarvis-gui both green.
Tests: 52/52 jarvis-core unit tests pass.
This commit is contained in:
Bossiara13 2026-05-15 16:15:59 +03:00
parent 6225198821
commit 5c7245012e
19 changed files with 709 additions and 34 deletions

View file

@ -1,15 +1,18 @@
use mlua::{Lua, Table, Value};
use crate::llm::{ChatMessage, LlmClient};
use crate::llm::{self, ChatMessage};
// Lua-callable wrapper around jarvis_core::llm::LlmClient.
// Centralises the Groq plumbing so command scripts can just say:
// Lua-callable wrapper around jarvis_core::llm.
// Command scripts call:
// local reply = jarvis.llm(
// { {role='system', content='...'}, {role='user', content='...'} },
// { temperature = 0.0, max_tokens = 64 }
// )
// Returns the assistant text (string) or nil on any failure (missing token,
// network error, empty response, etc.) — callers should always check `if reply`.
// Returns assistant text (string) or nil on any failure.
//
// Backend selection is global (see `jarvis_core::llm::swap_to`). Lua scripts can:
// jarvis.llm_status() -> "groq" | "ollama" | "none"
// jarvis.llm_switch("ollama") -> name string on success, nil on failure
pub fn register(lua: &Lua, jarvis: &Table) -> mlua::Result<()> {
let chat_fn = lua.create_function(|lua, (messages, opts): (Table, Option<Table>)| {
let mut chat_messages = Vec::new();
@ -39,11 +42,18 @@ pub fn register(lua: &Lua, jarvis: &Table) -> mlua::Result<()> {
if let Ok(v) = o.get::<f32>("top_p") { top_p = v; }
}
let client = match LlmClient::from_env() {
Ok(c) => c,
Err(e) => {
log::warn!("[Lua llm] no client: {}", e);
return Ok(Value::Nil);
// Prefer the live global client (so runtime backend swap takes effect).
// Fall back to a one-shot client from env if global is not initialised.
let client = match llm::current() {
Some(c) => c,
None => {
match llm::LlmClient::from_env() {
Ok(c) => std::sync::Arc::new(c),
Err(e) => {
log::warn!("[Lua llm] no client: {}", e);
return Ok(Value::Nil);
}
}
}
};
@ -58,7 +68,35 @@ pub fn register(lua: &Lua, jarvis: &Table) -> mlua::Result<()> {
}
}
})?;
jarvis.set("llm", chat_fn)?;
// Current backend name ("groq" | "ollama" | "none").
let status_fn = lua.create_function(|_, ()| {
Ok(llm::current_backend_name().to_string())
})?;
jarvis.set("llm_status", status_fn)?;
// Hot-swap backend. Accepts "groq"/"ollama"/"local"/"cloud"/"локальный"/"облако".
// Returns the new backend name on success, nil on failure.
let switch_fn = lua.create_function(|lua, name: String| {
match llm::parse_backend(&name) {
Some(b) => match llm::swap_to(b) {
Ok(actual) => {
let s = lua.create_string(actual)?;
Ok(Value::String(s))
}
Err(e) => {
log::warn!("[Lua llm_switch] swap failed: {}", e);
Ok(Value::Nil)
}
},
None => {
log::warn!("[Lua llm_switch] unknown backend name: {}", name);
Ok(Value::Nil)
}
}
})?;
jarvis.set("llm_switch", switch_fn)?;
Ok(())
}

View file

@ -73,6 +73,23 @@ pub fn register(lua: &Lua, jarvis: &Table) -> mlua::Result<()> {
})?;
sched.set("clear", clear_fn)?;
// Remove all tasks whose name OR speak text matches `query` (substring,
// case-insensitive). Returns count removed.
let remove_by_text_fn = lua.create_function(|_, query: String| {
Ok(scheduler::remove_by_text(&query))
})?;
sched.set("remove_by_text", remove_by_text_fn)?;
// Find (don't remove) tasks matching query. Returns array of tasks.
let find_by_text_fn = lua.create_function(|lua, (query, n): (String, Option<usize>)| {
let arr = lua.create_table()?;
for (i, t) in scheduler::find_by_text(&query, n.unwrap_or(5)).iter().enumerate() {
arr.set(i + 1, task_to_lua(lua, t)?)?;
}
Ok(arr)
})?;
sched.set("find_by_text", find_by_text_fn)?;
let list_fn = lua.create_function(|lua, ()| {
let arr = lua.create_table()?;
for (i, t) in scheduler::list().iter().enumerate() {