Big push driven by user feedback ("делай имбу") and web research on what
voice assistants need to be the ideal:
TTS backend abstraction (P0.1)
- new module crates/jarvis-core/src/tts/{mod,sapi,piper,silero}.rs
- TtsBackend trait with SapiBackend (current PowerShell), PiperBackend
(rhasspy/piper, neural quality), SileroBackend (python subprocess)
- JARVIS_TTS env var picks (sapi|piper|silero). Auto-detect Piper if
binary + voice present in tools/piper/. Falls back to SAPI on missing.
- SpeakOpts {lang, detached, raw} replaces ad-hoc args. text_utils
sanitiser applied unless raw=true.
- llm_fallback + lua/api/tts both routed through tts::backend().
- tools/piper/install.ps1 downloads piper.exe + ru_RU-irina-medium.onnx
from rhasspy releases + huggingface. Smoke-test included.
- tools/silero/silero_tts.py helper (PyTorch); rust spawns it as subprocess.
IMBA-1 Agentic LLM router
- crates/jarvis-app/src/llm_router.rs
- When fuzzy/intent matcher fails, LLM picks the closest command from the
full registry. Returns JSON {command_id, confidence, reason}.
- Threshold-gated re-dispatch via substitute phrase. JARVIS_LLM_ROUTER=1
enables; JARVIS_LLM_ROUTER_THRESHOLD overrides 0.55 default.
- Inserted in app.rs::execute_command between "no match" and existing
llm_fallback chat fallback.
IMBA-2 Long-term memory
- crates/jarvis-core/src/long_term_memory.rs — JSON store at
APP_CONFIG_DIR/long_term_memory.json. Atomic write-through.
- remember/recall/search/forget/all/build_context API.
- Lua bindings: jarvis.memory.* (5 functions).
- llm_fallback auto-injects relevant facts (substring search of prompt)
into system message before LLM call.
- Pack resources/commands/memory_pack/ with 4 commands: remember, recall,
forget, list.
IMBA-3 Profile switching (work/game/sleep/driving/default)
- crates/jarvis-core/src/profiles.rs — JSON profiles at APP_CONFIG_DIR/profiles/
Auto-seeds 5 defaults on first run with personality + allow/deny lists +
greetings + emoji icons.
- active_profile.txt persists choice across restart.
- Lua bindings: jarvis.profile.{active,set,list,allows,active_name}.
- llm_fallback prepends profile personality to system prompt.
- Pack resources/commands/profile_switch/ with 6 voice triggers.
IMBA-4 Multimodal screenshot + vision LLM
- crates/jarvis-core/src/lua/api/vision.rs — gated on HTTP sandbox.
- jarvis.vision.screenshot() captures via PowerShell System.Drawing.
- jarvis.vision.describe(prompt?) sends base64 PNG to Groq vision model
(default llama-3.2-11b-vision-preview, override via GROQ_VISION_MODEL).
- Pack resources/commands/vision/ with 2 commands: describe + read_error.
P0.2 Continuous conversation grace window
- config::CONVERSATION_GRACE_MS = 30_000.
- app.rs: after command result, if grace_ms > 0 keep listening WITHOUT
re-wake for the grace duration. Existing CMS_WAIT_DELAY back-dated so
the existing timeout fires at start + grace_ms.
Tests: 24/24 jarvis-core unit tests pass (including 5 text_utils).
Build: cargo build --release -p jarvis-app and -p jarvis-gui both succeed
on Windows MSVC (VS 2026 Enterprise vcvars64).
Notes for setup:
- Piper voice install: pwsh tools/piper/install.ps1 (downloads ~90 MB).
- GROQ_TOKEN needed for IMBA-1 (router) and IMBA-4 (vision).
- All features are opt-in via env vars or auto-detect; existing SAPI +
fuzzy match path remains the default.
144 lines
4.3 KiB
Rust
144 lines
4.3 KiB
Rust
use once_cell::sync::OnceCell;
|
|
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::long_term_memory;
|
|
use jarvis_core::profiles;
|
|
use jarvis_core::tts::{self, SpeakOpts};
|
|
use jarvis_core::voices;
|
|
|
|
struct State {
|
|
client: LlmClient,
|
|
history: Mutex<ConversationHistory>,
|
|
max_tokens: u32,
|
|
}
|
|
|
|
static STATE: OnceCell<Option<State>> = OnceCell::new();
|
|
|
|
pub fn init() {
|
|
let _ = STATE.set(build_state());
|
|
}
|
|
|
|
fn build_state() -> Option<State> {
|
|
if !config::LLM_DEFAULT_ENABLED {
|
|
info!("LLM fallback disabled by config.");
|
|
return None;
|
|
}
|
|
|
|
let client = match LlmClient::from_env() {
|
|
Ok(c) => c,
|
|
Err(e) => {
|
|
warn!("LLM fallback disabled: {}. Set GROQ_TOKEN 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 (model: {}).", client.model());
|
|
Some(State {
|
|
client,
|
|
history,
|
|
max_tokens: config::LLM_DEFAULT_MAX_TOKENS,
|
|
})
|
|
}
|
|
|
|
pub fn is_enabled() -> bool {
|
|
STATE.get().and_then(|s| s.as_ref()).is_some()
|
|
}
|
|
|
|
pub fn extract_prompt(text: &str) -> Option<String> {
|
|
let lang = i18n::get_language();
|
|
let triggers = config::get_llm_trigger_phrases(&lang);
|
|
let lowered = text.to_lowercase();
|
|
|
|
for trig in triggers {
|
|
if let Some(rest) = strip_trigger(&lowered, trig) {
|
|
let trimmed = rest.trim();
|
|
if !trimmed.is_empty() {
|
|
return Some(trimmed.to_string());
|
|
}
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
fn strip_trigger(text: &str, trigger: &str) -> Option<String> {
|
|
let t = text.trim_start();
|
|
if let Some(after) = t.strip_prefix(trigger) {
|
|
let next = after.chars().next();
|
|
if next.map_or(true, |c| !c.is_alphanumeric()) {
|
|
return Some(after.to_string());
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
pub fn handle(prompt: &str) {
|
|
let state = match STATE.get().and_then(|s| s.as_ref()) {
|
|
Some(s) => s,
|
|
None => {
|
|
warn!("LLM fallback called while disabled — ignoring.");
|
|
return;
|
|
}
|
|
};
|
|
|
|
info!("LLM prompt: {}", prompt);
|
|
|
|
let snapshot: Vec<ChatMessage> = {
|
|
let mut h = state.history.lock();
|
|
h.push_user(prompt);
|
|
let mut snap = h.snapshot();
|
|
|
|
// Inject (a) profile personality (b) relevant long-term memory as a fresh
|
|
// system message right after the base prompt. Both are optional.
|
|
let profile = profiles::active();
|
|
let mut overlay = String::new();
|
|
if !profile.llm_personality.is_empty() {
|
|
overlay.push_str(&format!("Активный профиль: {} {}\nХарактер для ответа: {}\n",
|
|
profile.icon, profile.name, profile.llm_personality));
|
|
}
|
|
let mem_ctx = long_term_memory::build_context(prompt, 5);
|
|
if !mem_ctx.is_empty() {
|
|
overlay.push_str(&mem_ctx);
|
|
}
|
|
if !overlay.is_empty() {
|
|
// Insert after the base system prompt (index 0 typically), before user msgs.
|
|
let insert_at = if !snap.is_empty() && snap[0].role == "system" { 1 } else { 0 };
|
|
snap.insert(insert_at, ChatMessage::system(overlay));
|
|
}
|
|
|
|
snap
|
|
};
|
|
|
|
match state.client.complete(&snapshot, state.max_tokens) {
|
|
Ok(reply) => {
|
|
let reply = reply.trim().to_string();
|
|
info!("LLM reply: {}", reply);
|
|
state.history.lock().push_assistant(reply.clone());
|
|
ipc::send(IpcEvent::LlmReply { text: reply.clone() });
|
|
voices::play_ok();
|
|
speak_reply(&reply);
|
|
}
|
|
Err(e) => {
|
|
error!("LLM request failed: {}", e);
|
|
state.history.lock().pop_last_user();
|
|
let err_text = config::LLM_FALLBACK_ERROR_RU.to_string();
|
|
ipc::send(IpcEvent::LlmReply { text: err_text.clone() });
|
|
voices::play_error();
|
|
speak_reply(&err_text);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn speak_reply(text: &str) {
|
|
if std::env::var("JARVIS_LLM_TTS").as_deref() == Ok("false") {
|
|
return;
|
|
}
|
|
tts::speak(text, &SpeakOpts::lang("ru"));
|
|
}
|