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

@ -4,14 +4,16 @@ use parking_lot::Mutex;
use jarvis_core::config;
use jarvis_core::i18n;
use jarvis_core::ipc::{self, IpcEvent};
use jarvis_core::llm::{ChatMessage, ConversationHistory, LlmClient};
use jarvis_core::llm::{self, ChatMessage, ConversationHistory};
use jarvis_core::long_term_memory;
use jarvis_core::profiles;
use jarvis_core::tts::{self, SpeakOpts};
use jarvis_core::voices;
// State now only holds conversation history — the actual LLM client lives in
// `jarvis_core::llm::GLOBAL` so that voice/Lua commands can hot-swap backends
// (Ollama ↔ Groq) without rebuilding this module's state.
struct State {
client: LlmClient,
history: Mutex<ConversationHistory>,
max_tokens: u32,
}
@ -28,21 +30,18 @@ fn build_state() -> Option<State> {
return None;
}
let client = match LlmClient::from_env() {
Ok(c) => c,
Err(e) => {
warn!("LLM fallback disabled: {}. Set GROQ_TOKEN or run Ollama to enable.", e);
return None;
}
};
// Initialise the shared global client (idempotent — fine if init was already called).
if let Err(e) = llm::init_global() {
warn!("LLM fallback disabled: {}. Set GROQ_TOKEN or run Ollama to enable.", e);
return None;
}
let lang = i18n::get_language();
let prompt = config::get_llm_system_prompt(&lang);
let history = Mutex::new(ConversationHistory::new(prompt, config::LLM_DEFAULT_MAX_HISTORY));
info!("LLM fallback enabled (backend: {}, model: {}).", client.backend().name(), client.model());
info!("LLM fallback enabled (backend: {}).", llm::current_backend_name());
Some(State {
client,
history,
max_tokens: config::LLM_DEFAULT_MAX_TOKENS,
})
@ -116,7 +115,15 @@ pub fn handle(prompt: &str) {
snap
};
match state.client.complete(&snapshot, state.max_tokens) {
let client = match llm::current() {
Some(c) => c,
None => {
warn!("LLM call but global client missing — ignoring.");
return;
}
};
match client.complete(&snapshot, state.max_tokens) {
Ok(reply) => {
let reply = reply.trim().to_string();
info!("LLM reply: {}", reply);

View file

@ -13,7 +13,7 @@ use serde::Deserialize;
use jarvis_core::config;
use jarvis_core::i18n;
use jarvis_core::llm::{ChatMessage, LlmClient};
use jarvis_core::llm::{self, ChatMessage};
use jarvis_core::{JCommandsList, COMMANDS_LIST};
const SYSTEM_PROMPT_RU: &str = "Ты — диспетчер голосовых команд J.A.R.V.I.S. \
@ -38,7 +38,6 @@ const TOP_P: f32 = 0.9;
const MAX_PHRASES_PER_CMD: usize = 4;
struct State {
client: LlmClient,
threshold: f32,
}
@ -53,18 +52,18 @@ fn build_state() -> Option<State> {
info!("LLM router disabled (set JARVIS_LLM_ROUTER=1 to enable).");
return None;
}
let client = match LlmClient::from_env() {
Ok(c) => c,
Err(e) => {
// The actual LlmClient lives in the shared global; just make sure it's initialised.
if llm::current().is_none() {
if let Err(e) = llm::init_global() {
warn!("LLM router disabled: {}. Set GROQ_TOKEN or run Ollama to enable.", e);
return None;
}
};
}
let threshold = jarvis_core::runtime_config::llm_router_threshold();
let _ = DEFAULT_THRESHOLD; // kept for backward visibility in source
let _ = DEFAULT_THRESHOLD; // kept for source-doc visibility
info!("LLM router enabled (backend: {}, threshold {:.2}).", client.backend().name(), threshold);
Some(State { client, threshold })
info!("LLM router enabled (backend: {}, threshold {:.2}).", llm::current_backend_name(), threshold);
Some(State { threshold })
}
fn router_enabled() -> bool {
@ -110,7 +109,8 @@ pub fn try_route(text: &str) -> Option<RouteResult> {
ChatMessage::user(prompt),
];
let raw = match state.client.complete_with(&messages, MAX_TOKENS, TEMPERATURE, TOP_P) {
let client = llm::current()?;
let raw = match client.complete_with(&messages, MAX_TOKENS, TEMPERATURE, TOP_P) {
Ok(r) => r,
Err(e) => {
warn!("LLM router request failed: {}", e);

View file

@ -60,6 +60,11 @@ fn main() -> Result<(), String> {
eprintln!("[jarvis-app] step: runtime_config::log_effective_config");
jarvis_core::runtime_config::log_effective_config();
eprintln!("[jarvis-app] step: tts pre-warm");
// Touch the TTS backend so first speech doesn't pay init cost (Piper binary
// discovery, voice loading). All subsequent speak() calls reuse the same Arc.
let _ = jarvis_core::tts::backend();
eprintln!("[jarvis-app] step: llm_fallback::init");
llm_fallback::init();