J.A.R.V.I.S-rust/crates/jarvis-core/src/lua/api/scheduler.rs
Bossiara13 5c7245012e 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.
2026-05-15 16:15:59 +03:00

161 lines
5.5 KiB
Rust

//! Lua bindings for the proactive scheduler.
//!
//! Usage:
//! jarvis.scheduler.add({
//! name = "Daily briefing",
//! schedule = "daily 09:00",
//! action = { type = "speak", text = "Доброе утро. Готов к работе." }
//! })
//!
//! jarvis.scheduler.add({
//! name = "Reminder",
//! schedule = "in 5 minutes",
//! action = { type = "speak", text = "Выключи кофеварку." }
//! })
//!
//! for _, t in ipairs(jarvis.scheduler.list()) do
//! print(t.id, t.name, t.schedule_human)
//! end
//!
//! jarvis.scheduler.remove(id)
//! jarvis.scheduler.clear()
//!
//! Schedule string syntax (see `scheduler::Schedule::parse`):
//! "daily HH:MM" | "at HH:MM" | "every N minutes" | "every N hours" | "in N minutes" | "in N hours"
use mlua::{Lua, Table, Value};
use crate::scheduler::{self, Action, Schedule, ScheduledTask};
pub fn register(lua: &Lua, jarvis: &Table) -> mlua::Result<()> {
let sched = lua.create_table()?;
let add_fn = lua.create_function(|_, t: Table| {
let name: String = t.get::<Option<String>>("name")?.unwrap_or_else(|| "task".to_string());
let schedule_str: String = t.get::<String>("schedule")?;
let id: String = t.get::<Option<String>>("id")?.unwrap_or_default();
let action_tbl: Table = t.get::<Table>("action")?;
let action_type: String = action_tbl.get::<String>("type")?;
let action = match action_type.as_str() {
"speak" => Action::Speak {
text: action_tbl.get::<String>("text").unwrap_or_default(),
},
"lua" => Action::Lua {
script_path: action_tbl.get::<String>("script_path").unwrap_or_default(),
},
other => return Err(mlua::Error::external(format!("unknown action type: {}", other))),
};
let schedule = Schedule::parse(&schedule_str)
.map_err(|e| mlua::Error::external(format!("bad schedule '{}': {}", schedule_str, e)))?;
let task = ScheduledTask {
id,
name,
schedule,
action,
last_fired: None,
enabled: true,
created_at: 0,
};
scheduler::add(task).map_err(mlua::Error::external)
})?;
sched.set("add", add_fn)?;
let remove_fn = lua.create_function(|_, id: String| {
Ok(scheduler::remove(&id))
})?;
sched.set("remove", remove_fn)?;
let clear_fn = lua.create_function(|_, ()| {
Ok(scheduler::clear())
})?;
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() {
arr.set(i + 1, task_to_lua(lua, t)?)?;
}
Ok(arr)
})?;
sched.set("list", list_fn)?;
let count_fn = lua.create_function(|_, ()| {
Ok(scheduler::list().len())
})?;
sched.set("count", count_fn)?;
jarvis.set("scheduler", sched)?;
Ok(())
}
fn task_to_lua(lua: &Lua, t: &ScheduledTask) -> mlua::Result<Table> {
let tbl = lua.create_table()?;
tbl.set("id", t.id.clone())?;
tbl.set("name", t.name.clone())?;
tbl.set("enabled", t.enabled)?;
tbl.set("created_at", t.created_at)?;
tbl.set("schedule_human", schedule_human(&t.schedule))?;
let last_fired: Value = match t.last_fired {
Some(ts) => Value::Integer(ts),
None => Value::Nil,
};
tbl.set("last_fired", last_fired)?;
let action_tbl = lua.create_table()?;
match &t.action {
Action::Speak { text } => {
action_tbl.set("type", "speak")?;
action_tbl.set("text", text.clone())?;
}
Action::Lua { script_path } => {
action_tbl.set("type", "lua")?;
action_tbl.set("script_path", script_path.clone())?;
}
}
tbl.set("action", action_tbl)?;
Ok(tbl)
}
fn schedule_human(s: &Schedule) -> String {
match s {
Schedule::Daily { hour, minute } => format!("каждый день в {:02}:{:02}", hour, minute),
Schedule::Interval { seconds } => {
if *seconds % 3600 == 0 {
format!("каждые {} часов", seconds / 3600)
} else if *seconds % 60 == 0 {
format!("каждые {} минут", seconds / 60)
} else {
format!("каждые {} секунд", seconds)
}
}
Schedule::Once { at } => {
chrono::DateTime::from_timestamp(*at, 0)
.map(|dt: chrono::DateTime<chrono::Utc>| {
dt.with_timezone(&chrono::Local).format("один раз в %H:%M %d.%m").to_string()
})
.unwrap_or_else(|| format!("один раз в {}", at))
}
}
}