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:
parent
6225198821
commit
5c7245012e
19 changed files with 709 additions and 34 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,65 @@ mod client;
|
|||
mod history;
|
||||
|
||||
pub use client::{
|
||||
ChatMessage, ConfigError, LlmClient, LlmError,
|
||||
ChatMessage, ConfigError, LlmBackend, LlmClient, LlmError,
|
||||
DEFAULT_BASE_URL, DEFAULT_MODEL, ENV_BASE_URL, ENV_MODEL, ENV_TOKEN,
|
||||
OLLAMA_DEFAULT_BASE_URL, OLLAMA_DEFAULT_MODEL,
|
||||
};
|
||||
pub use history::ConversationHistory;
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
use parking_lot::RwLock;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Shared mutable LLM client. Modules read via `current()`; the user can
|
||||
/// hot-swap backends at runtime via `swap_to(LlmBackend::Ollama)` etc.
|
||||
///
|
||||
/// `None` means LLM is disabled (no GROQ_TOKEN and no Ollama reachable, or
|
||||
/// `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`, ...).
|
||||
/// 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()?;
|
||||
log::info!("LLM global initialised: backend={}, model={}", c.backend().name(), c.model());
|
||||
*GLOBAL.write() = Some(Arc::new(c));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Return a clone of the active client. Cheap (Arc).
|
||||
pub fn current() -> Option<Arc<LlmClient>> {
|
||||
GLOBAL.read().clone()
|
||||
}
|
||||
|
||||
/// Name of the currently active backend, or "none" if not initialised.
|
||||
pub fn current_backend_name() -> &'static str {
|
||||
match GLOBAL.read().as_ref().map(|c| c.backend()) {
|
||||
Some(LlmBackend::Groq) => "groq",
|
||||
Some(LlmBackend::Ollama) => "ollama",
|
||||
None => "none",
|
||||
}
|
||||
}
|
||||
|
||||
/// Hot-swap to a different backend. Returns the new backend's name on success.
|
||||
pub fn swap_to(backend: LlmBackend) -> Result<&'static str, ConfigError> {
|
||||
let c = match backend {
|
||||
LlmBackend::Groq => LlmClient::groq()?,
|
||||
LlmBackend::Ollama => LlmClient::ollama(),
|
||||
};
|
||||
let name = c.backend().name();
|
||||
log::info!("LLM swapped → backend={}, model={}", name, c.model());
|
||||
*GLOBAL.write() = Some(Arc::new(c));
|
||||
Ok(name)
|
||||
}
|
||||
|
||||
/// Parse a backend name (case-insensitive) into the enum.
|
||||
pub fn parse_backend(name: &str) -> Option<LlmBackend> {
|
||||
match name.trim().to_lowercase().as_str() {
|
||||
"groq" | "cloud" | "облако" | "клауд" => Some(LlmBackend::Groq),
|
||||
"ollama" | "local" | "локал" | "локальн" | "локальный" => Some(LlmBackend::Ollama),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -293,6 +293,54 @@ pub fn list() -> Vec<ScheduledTask> {
|
|||
state.store.read().tasks.clone()
|
||||
}
|
||||
|
||||
/// Case-insensitive substring search across `name` + any `Speak` action text.
|
||||
/// Returns matched tasks (clones) up to `limit`.
|
||||
pub fn find_by_text(query: &str, limit: usize) -> Vec<ScheduledTask> {
|
||||
find_by_text_in(&list(), query, limit)
|
||||
}
|
||||
|
||||
/// Pure logic split out for testing.
|
||||
pub(crate) fn find_by_text_in(tasks: &[ScheduledTask], query: &str, limit: usize) -> Vec<ScheduledTask> {
|
||||
let nq = query.trim().to_lowercase();
|
||||
if nq.is_empty() { return Vec::new(); }
|
||||
let mut hits: Vec<ScheduledTask> = tasks.iter()
|
||||
.filter(|t| {
|
||||
if t.name.to_lowercase().contains(&nq) { return true; }
|
||||
if let Action::Speak { text } = &t.action {
|
||||
if text.to_lowercase().contains(&nq) { return true; }
|
||||
}
|
||||
false
|
||||
})
|
||||
.cloned()
|
||||
.collect();
|
||||
hits.truncate(limit.max(1));
|
||||
hits
|
||||
}
|
||||
|
||||
/// Remove all tasks whose name or speak text matches `query`. Returns count.
|
||||
pub fn remove_by_text(query: &str) -> usize {
|
||||
let Some(state) = STATE.get() else { return 0; };
|
||||
let nq = query.trim().to_lowercase();
|
||||
if nq.is_empty() { return 0; }
|
||||
let removed = {
|
||||
let mut store = state.store.write();
|
||||
let before = store.tasks.len();
|
||||
store.tasks.retain(|t| {
|
||||
let name_match = t.name.to_lowercase().contains(&nq);
|
||||
let text_match = if let Action::Speak { text } = &t.action {
|
||||
text.to_lowercase().contains(&nq)
|
||||
} else { false };
|
||||
!(name_match || text_match)
|
||||
});
|
||||
before - store.tasks.len()
|
||||
};
|
||||
if removed > 0 {
|
||||
persist(state);
|
||||
info!("Scheduler: removed {} tasks matching '{}'", removed, query);
|
||||
}
|
||||
removed
|
||||
}
|
||||
|
||||
pub fn find(id: &str) -> Option<ScheduledTask> {
|
||||
let state = STATE.get()?;
|
||||
state.store.read().tasks.iter().find(|t| t.id == id).cloned()
|
||||
|
|
@ -446,4 +494,45 @@ mod tests {
|
|||
assert_eq!(s.next_fire(None, 1000), Some(500));
|
||||
assert_eq!(s.next_fire(Some(500), 1000), None);
|
||||
}
|
||||
|
||||
fn speak_task(id: &str, name: &str, text: &str) -> ScheduledTask {
|
||||
ScheduledTask {
|
||||
id: id.into(),
|
||||
name: name.into(),
|
||||
schedule: Schedule::Interval { seconds: 60 },
|
||||
action: Action::Speak { text: text.into() },
|
||||
last_fired: None,
|
||||
enabled: true,
|
||||
created_at: 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_by_text_matches_name() {
|
||||
let tasks = vec![
|
||||
speak_task("a", "Drink water", "Попей воды."),
|
||||
speak_task("b", "Stretch", "Разомнись."),
|
||||
];
|
||||
let hits = find_by_text_in(&tasks, "water", 5);
|
||||
assert_eq!(hits.len(), 1);
|
||||
assert_eq!(hits[0].id, "a");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_by_text_matches_speak_text_case_insensitive() {
|
||||
let tasks = vec![
|
||||
speak_task("a", "X", "Попей воды."),
|
||||
speak_task("b", "Y", "Разомнись."),
|
||||
];
|
||||
let hits = find_by_text_in(&tasks, "ВОД", 5);
|
||||
assert_eq!(hits.len(), 1);
|
||||
assert_eq!(hits[0].id, "a");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_by_text_empty_query_returns_nothing() {
|
||||
let tasks = vec![speak_task("a", "X", "Y")];
|
||||
assert!(find_by_text_in(&tasks, "", 5).is_empty());
|
||||
assert!(find_by_text_in(&tasks, " ", 5).is_empty());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
98
resources/commands/codebase_qa/ask.lua
Normal file
98
resources/commands/codebase_qa/ask.lua
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
-- "что делает функция X" / "найди в коде Y"
|
||||
-- Reads a digest of source files in the configured codebase root, sends to LLM.
|
||||
local root = jarvis.memory.recall("codebase.root")
|
||||
if not root or root == "" then
|
||||
return jarvis.cmd.not_found("Сначала укажите проект.")
|
||||
end
|
||||
|
||||
local phrase = (jarvis.context.phrase or "")
|
||||
local question = jarvis.text.strip_trigger(phrase:lower(), {
|
||||
"что делает функция",
|
||||
"вопрос по коду",
|
||||
"спроси код",
|
||||
"найди в проекте",
|
||||
"найди в коде",
|
||||
"что в коде",
|
||||
"объясни код",
|
||||
"ask the codebase",
|
||||
"ask the code",
|
||||
"what does the function",
|
||||
"explain the code",
|
||||
})
|
||||
question = question:gsub("^[%s,:%.]+", ""):gsub("%s+$", "")
|
||||
if question == "" then
|
||||
return jarvis.cmd.error("Сформулируйте вопрос.")
|
||||
end
|
||||
|
||||
-- Build a digest: walk depth-2, pick source files by extension, cap per-file size + total size.
|
||||
local EXT_OK = {
|
||||
rs=true, lua=true, py=true, ts=true, tsx=true, js=true, jsx=true, svelte=true,
|
||||
go=true, java=true, kt=true, c=true, h=true, cpp=true, hpp=true, cs=true,
|
||||
rb=true, php=true, sh=true, ps1=true, sql=true, toml=true, yaml=true, yml=true,
|
||||
json=true, md=true,
|
||||
}
|
||||
local SKIP_DIR = {
|
||||
[".git"]=true, ["target"]=true, ["node_modules"]=true, ["dist"]=true, ["build"]=true,
|
||||
["__pycache__"]=true, [".venv"]=true, ["venv"]=true, [".idea"]=true, [".vscode"]=true,
|
||||
}
|
||||
|
||||
local MAX_FILE_BYTES = 4096 -- ~1k tokens per file
|
||||
local MAX_TOTAL_BYTES = 50000 -- ~12k tokens total for digest
|
||||
local MAX_FILES = 30
|
||||
|
||||
local function walk(dir, depth, acc)
|
||||
if depth > 3 then return end
|
||||
local entries = jarvis.fs.list(dir)
|
||||
if not entries then return end
|
||||
for _, ent in ipairs(entries) do
|
||||
if #acc.files >= MAX_FILES or acc.bytes >= MAX_TOTAL_BYTES then
|
||||
return
|
||||
end
|
||||
local name = ent.name
|
||||
local full = dir .. "\\" .. name
|
||||
if ent.is_dir then
|
||||
if not SKIP_DIR[name] and not name:match("^%.") then
|
||||
walk(full, depth + 1, acc)
|
||||
end
|
||||
else
|
||||
local ext = name:match("%.([%w]+)$")
|
||||
if ext and EXT_OK[ext:lower()] then
|
||||
local content = jarvis.fs.read(full)
|
||||
if content then
|
||||
if #content > MAX_FILE_BYTES then
|
||||
content = content:sub(1, MAX_FILE_BYTES) .. "\n... [truncated]"
|
||||
end
|
||||
-- relative path for readability
|
||||
local rel = full:sub(#root + 2)
|
||||
table.insert(acc.files, "--- " .. rel .. " ---\n" .. content)
|
||||
acc.bytes = acc.bytes + #content
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local acc = { files = {}, bytes = 0 }
|
||||
walk(root, 1, acc)
|
||||
|
||||
if #acc.files == 0 then
|
||||
return jarvis.cmd.not_found("Не нашёл исходников в проекте.")
|
||||
end
|
||||
|
||||
local digest = table.concat(acc.files, "\n\n")
|
||||
local prompt = string.format(
|
||||
"Ты — старший разработчик. По digest проекта ответь на вопрос пользователя кратко (3-5 предложений) на русском. Указывай файлы где уместно.\n\n=== ВОПРОС ===\n%s\n\n=== КОД ===\n%s",
|
||||
question, digest
|
||||
)
|
||||
|
||||
local reply = jarvis.llm({
|
||||
{ role = "system", content = "Ты — внимательный код-ревьюер. Отвечай по существу, без воды." },
|
||||
{ role = "user", content = prompt }
|
||||
}, { max_tokens = 400, temperature = 0.2 })
|
||||
|
||||
if not reply or reply == "" then
|
||||
return jarvis.cmd.error("Не получилось получить ответ.")
|
||||
end
|
||||
|
||||
jarvis.system.notify("Codebase Q&A", reply:sub(1, 250))
|
||||
return jarvis.cmd.ok(reply)
|
||||
61
resources/commands/codebase_qa/command.toml
Normal file
61
resources/commands/codebase_qa/command.toml
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
# Codebase Q&A — point J.A.R.V.I.S. at a folder, ask questions about the code.
|
||||
# Active folder stored via jarvis.memory (key="codebase.root"). Voice commands let
|
||||
# you swap the folder, list it, and ask questions. The script reads files lazily
|
||||
# (depth-limited, size-capped) and feeds a digest to the LLM.
|
||||
|
||||
[[commands]]
|
||||
id = "codebase.set"
|
||||
type = "lua"
|
||||
script = "set.lua"
|
||||
sandbox = "full"
|
||||
timeout = 5000
|
||||
|
||||
[commands.phrases]
|
||||
ru = [
|
||||
"укажи проект",
|
||||
"укажи папку проекта",
|
||||
"укажи кодовую базу",
|
||||
"выбери проект",
|
||||
"проект сейчас",
|
||||
]
|
||||
en = ["set codebase", "set project folder", "use project"]
|
||||
ua = ["вкажи проект", "вкажи папку проекту"]
|
||||
|
||||
|
||||
[[commands]]
|
||||
id = "codebase.ask"
|
||||
type = "lua"
|
||||
script = "ask.lua"
|
||||
sandbox = "full"
|
||||
timeout = 60000
|
||||
|
||||
[commands.phrases]
|
||||
ru = [
|
||||
"спроси код",
|
||||
"вопрос по коду",
|
||||
"что в коде",
|
||||
"что делает функция",
|
||||
"найди в коде",
|
||||
"найди в проекте",
|
||||
"объясни код",
|
||||
]
|
||||
en = ["ask the code", "ask the codebase", "what does the function", "explain the code"]
|
||||
ua = ["що в коді", "що робить функція"]
|
||||
|
||||
|
||||
[[commands]]
|
||||
id = "codebase.where"
|
||||
type = "lua"
|
||||
script = "where.lua"
|
||||
sandbox = "full"
|
||||
timeout = 3000
|
||||
|
||||
[commands.phrases]
|
||||
ru = [
|
||||
"какой проект сейчас",
|
||||
"где проект",
|
||||
"какая папка проекта",
|
||||
"какая кодовая база",
|
||||
]
|
||||
en = ["which project", "what's the codebase"]
|
||||
ua = ["який проект"]
|
||||
29
resources/commands/codebase_qa/set.lua
Normal file
29
resources/commands/codebase_qa/set.lua
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
-- "Укажи проект C:\Jarvis\rust" — stores path in memory under key "codebase.root"
|
||||
local phrase = (jarvis.context.phrase or "")
|
||||
|
||||
local body = jarvis.text.strip_trigger(phrase:lower(), {
|
||||
"укажи папку проекта",
|
||||
"укажи кодовую базу",
|
||||
"укажи проект",
|
||||
"выбери проект",
|
||||
"проект сейчас",
|
||||
"set codebase",
|
||||
"set project folder",
|
||||
"use project",
|
||||
})
|
||||
body = body:gsub("^[%s,:%.]+", ""):gsub("%s+$", "")
|
||||
|
||||
-- Path can have backslashes/spaces — preserve original casing by picking from raw phrase
|
||||
local raw = phrase:sub(#phrase - #body + 1):gsub("^[%s,:%.]+", ""):gsub("%s+$", "")
|
||||
local path = raw ~= "" and raw or body
|
||||
|
||||
if path == "" then
|
||||
return jarvis.cmd.error("Укажите путь к папке проекта.")
|
||||
end
|
||||
|
||||
if not jarvis.fs.is_dir(path) then
|
||||
return jarvis.cmd.error("Папка не найдена: " .. path)
|
||||
end
|
||||
|
||||
jarvis.memory.remember("codebase.root", path)
|
||||
return jarvis.cmd.ok("Проект установлен.")
|
||||
5
resources/commands/codebase_qa/where.lua
Normal file
5
resources/commands/codebase_qa/where.lua
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
local root = jarvis.memory.recall("codebase.root")
|
||||
if not root or root == "" then
|
||||
return jarvis.cmd.not_found("Проект не выбран. Скажите: укажи проект.")
|
||||
end
|
||||
return jarvis.cmd.ok("Сейчас работаю с проектом " .. root .. ".")
|
||||
62
resources/commands/llm_switch/command.toml
Normal file
62
resources/commands/llm_switch/command.toml
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
# Switch between local (Ollama) and cloud (Groq) LLM at runtime.
|
||||
|
||||
[[commands]]
|
||||
id = "llm.switch.local"
|
||||
type = "lua"
|
||||
script = "switch.lua"
|
||||
sandbox = "standard"
|
||||
timeout = 3000
|
||||
|
||||
[commands.phrases]
|
||||
ru = [
|
||||
"переключись на локальный",
|
||||
"переключись на локальный мозг",
|
||||
"включи локальный режим",
|
||||
"перейди на локальный",
|
||||
"используй локальный",
|
||||
"перейди на оллама",
|
||||
"используй оллама",
|
||||
]
|
||||
en = ["switch to local", "use local llm", "use ollama"]
|
||||
ua = ["перейди на локальний", "використовуй локальний"]
|
||||
|
||||
|
||||
[[commands]]
|
||||
id = "llm.switch.cloud"
|
||||
type = "lua"
|
||||
script = "switch.lua"
|
||||
sandbox = "standard"
|
||||
timeout = 3000
|
||||
|
||||
[commands.phrases]
|
||||
ru = [
|
||||
"переключись на облако",
|
||||
"переключись на облачный",
|
||||
"переключись на грок",
|
||||
"переключись на гроку",
|
||||
"используй облако",
|
||||
"используй грок",
|
||||
"перейди на облако",
|
||||
]
|
||||
en = ["switch to cloud", "use cloud llm", "use groq"]
|
||||
ua = ["перейди на хмару", "використовуй хмару"]
|
||||
|
||||
|
||||
[[commands]]
|
||||
id = "llm.status"
|
||||
type = "lua"
|
||||
script = "status.lua"
|
||||
sandbox = "minimal"
|
||||
timeout = 2000
|
||||
|
||||
[commands.phrases]
|
||||
ru = [
|
||||
"какой у тебя мозг",
|
||||
"какой ллм",
|
||||
"какой у тебя llm",
|
||||
"облако или локально",
|
||||
"ты сейчас где работаешь",
|
||||
"что за модель",
|
||||
]
|
||||
en = ["which llm", "are you local or cloud", "what model"]
|
||||
ua = ["який ллм", "хмара чи локально"]
|
||||
8
resources/commands/llm_switch/status.lua
Normal file
8
resources/commands/llm_switch/status.lua
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
local backend = jarvis.llm_status()
|
||||
if backend == "groq" then
|
||||
return jarvis.cmd.ok("Сейчас работаю на облаке.")
|
||||
elseif backend == "ollama" then
|
||||
return jarvis.cmd.ok("Сейчас работаю локально.")
|
||||
else
|
||||
return jarvis.cmd.not_found("LLM не настроен.")
|
||||
end
|
||||
22
resources/commands/llm_switch/switch.lua
Normal file
22
resources/commands/llm_switch/switch.lua
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
-- Map command id → backend name (see command.toml).
|
||||
local cmd_id = jarvis.context.command_id or ""
|
||||
local target
|
||||
if cmd_id:match("local") then
|
||||
target = "ollama"
|
||||
elseif cmd_id:match("cloud") then
|
||||
target = "groq"
|
||||
else
|
||||
return jarvis.cmd.error("Не понял на что переключать.")
|
||||
end
|
||||
|
||||
local result = jarvis.llm_switch(target)
|
||||
if not result then
|
||||
local human = (target == "ollama") and "локальный" or "облачный"
|
||||
return jarvis.cmd.error("Не получилось переключиться на " .. human .. ".")
|
||||
end
|
||||
|
||||
if result == "groq" then
|
||||
return jarvis.cmd.ok("Переключился на облачный мозг.")
|
||||
else
|
||||
return jarvis.cmd.ok("Переключился на локальный мозг.")
|
||||
end
|
||||
18
resources/commands/media_keys/_media.ps1
Normal file
18
resources/commands/media_keys/_media.ps1
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Helper: send a Windows media key via user32.keybd_event.
|
||||
# Called by key.lua with one argument = decimal VK code.
|
||||
param([int]$Vk)
|
||||
|
||||
if (-not $Vk) { exit 1 }
|
||||
|
||||
Add-Type -TypeDefinition @"
|
||||
using System.Runtime.InteropServices;
|
||||
public class MediaKey {
|
||||
[DllImport("user32.dll")]
|
||||
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
|
||||
}
|
||||
"@
|
||||
|
||||
# down + up
|
||||
[MediaKey]::keybd_event([byte]$Vk, 0, 0, 0)
|
||||
[MediaKey]::keybd_event([byte]$Vk, 0, 2, 0)
|
||||
exit 0
|
||||
80
resources/commands/media_keys/command.toml
Normal file
80
resources/commands/media_keys/command.toml
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
# Media playback control via Windows virtual media keys.
|
||||
# Works with anything that listens to global media keys: Spotify, Yandex Music,
|
||||
# YouTube in browser (when tab is focused), Foobar2000, Winamp, etc.
|
||||
# No OAuth, no API keys — just sends VK_MEDIA_* via PowerShell SendKeys.
|
||||
|
||||
[[commands]]
|
||||
id = "media.play_pause"
|
||||
type = "lua"
|
||||
script = "key.lua"
|
||||
sandbox = "full"
|
||||
timeout = 2000
|
||||
|
||||
[commands.phrases]
|
||||
ru = [
|
||||
"пауза",
|
||||
"продолжи",
|
||||
"плей",
|
||||
"включи музыку",
|
||||
"выключи музыку",
|
||||
"приостанови музыку",
|
||||
"снова музыку",
|
||||
]
|
||||
en = ["pause", "play", "resume music", "stop music"]
|
||||
ua = ["пауза", "продовжи"]
|
||||
|
||||
|
||||
[[commands]]
|
||||
id = "media.next"
|
||||
type = "lua"
|
||||
script = "key.lua"
|
||||
sandbox = "full"
|
||||
timeout = 2000
|
||||
|
||||
[commands.phrases]
|
||||
ru = [
|
||||
"следующий трек",
|
||||
"следующую песню",
|
||||
"перемотай вперёд",
|
||||
"вперёд трек",
|
||||
"переключи на следующую",
|
||||
"next",
|
||||
]
|
||||
en = ["next track", "next song", "skip"]
|
||||
ua = ["наступний трек"]
|
||||
|
||||
|
||||
[[commands]]
|
||||
id = "media.prev"
|
||||
type = "lua"
|
||||
script = "key.lua"
|
||||
sandbox = "full"
|
||||
timeout = 2000
|
||||
|
||||
[commands.phrases]
|
||||
ru = [
|
||||
"предыдущий трек",
|
||||
"предыдущую песню",
|
||||
"перемотай назад",
|
||||
"назад трек",
|
||||
"переключи на предыдущую",
|
||||
"previous",
|
||||
]
|
||||
en = ["previous track", "previous song", "back"]
|
||||
ua = ["попередній трек"]
|
||||
|
||||
|
||||
[[commands]]
|
||||
id = "media.stop"
|
||||
type = "lua"
|
||||
script = "key.lua"
|
||||
sandbox = "full"
|
||||
timeout = 2000
|
||||
|
||||
[commands.phrases]
|
||||
ru = [
|
||||
"стоп музыка",
|
||||
"останови плеер",
|
||||
]
|
||||
en = ["stop", "stop player"]
|
||||
ua = ["зупини плеєр"]
|
||||
26
resources/commands/media_keys/key.lua
Normal file
26
resources/commands/media_keys/key.lua
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
-- Map command id → VK_MEDIA_* virtual key code.
|
||||
-- See https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
|
||||
local cmd_id = jarvis.context.command_id or ""
|
||||
|
||||
local VK = {
|
||||
["media.play_pause"] = 179, -- VK_MEDIA_PLAY_PAUSE (0xB3)
|
||||
["media.next"] = 176, -- VK_MEDIA_NEXT_TRACK (0xB0)
|
||||
["media.prev"] = 177, -- VK_MEDIA_PREV_TRACK (0xB1)
|
||||
["media.stop"] = 178, -- VK_MEDIA_STOP (0xB2)
|
||||
}
|
||||
|
||||
local code = VK[cmd_id]
|
||||
if not code then
|
||||
return jarvis.cmd.error("Неизвестная медиа-команда.")
|
||||
end
|
||||
|
||||
local helper = jarvis.context.command_path .. "\\_media.ps1"
|
||||
local cmd_str = string.format(
|
||||
'powershell -NoProfile -ExecutionPolicy Bypass -File "%s" %d',
|
||||
helper, code
|
||||
)
|
||||
|
||||
jarvis.system.exec(cmd_str)
|
||||
|
||||
-- silent — feedback would be annoying for media-key spam
|
||||
return jarvis.cmd.silent_ok()
|
||||
27
resources/commands/scheduler/cancel_by_text.lua
Normal file
27
resources/commands/scheduler/cancel_by_text.lua
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
-- "Отмени напоминание про воду" / "удали задачу про разминку"
|
||||
local phrase = (jarvis.context.phrase or ""):lower()
|
||||
|
||||
local query = jarvis.text.strip_trigger(phrase, {
|
||||
"отмени напоминание про",
|
||||
"отмени напоминание о",
|
||||
"удали задачу про",
|
||||
"удали задачу о",
|
||||
"отмени про",
|
||||
"забудь напоминание про",
|
||||
"cancel reminder about",
|
||||
})
|
||||
query = query:gsub("^[%s,:%.]+", ""):gsub("%s+$", "")
|
||||
|
||||
if query == "" then
|
||||
return jarvis.cmd.error("Что именно отменить?")
|
||||
end
|
||||
|
||||
local n = jarvis.scheduler.remove_by_text(query)
|
||||
if n == 0 then
|
||||
return jarvis.cmd.not_found("Не нашёл напоминаний про " .. query .. ".")
|
||||
end
|
||||
|
||||
if n == 1 then
|
||||
return jarvis.cmd.ok("Отменил напоминание.")
|
||||
end
|
||||
return jarvis.cmd.ok(string.format("Отменил %d напоминаний про %s.", n, query))
|
||||
|
|
@ -107,3 +107,28 @@ ru = [
|
|||
]
|
||||
en = ["clear schedule", "cancel all reminders"]
|
||||
ua = ["очисти розклад", "скасуй всі нагадування"]
|
||||
|
||||
|
||||
[[commands]]
|
||||
id = "scheduler.cancel_by_text"
|
||||
type = "lua"
|
||||
script = "cancel_by_text.lua"
|
||||
sandbox = "minimal"
|
||||
timeout = 3000
|
||||
|
||||
[commands.phrases]
|
||||
ru = [
|
||||
"отмени напоминание про",
|
||||
"отмени напоминание о",
|
||||
"удали задачу про",
|
||||
"удали задачу о",
|
||||
"отмени про",
|
||||
"забудь напоминание про",
|
||||
]
|
||||
en = [
|
||||
"cancel reminder about",
|
||||
"remove task about",
|
||||
]
|
||||
ua = [
|
||||
"скасуй нагадування про",
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue