//! Starts everything the app runs: the LLM (`strata.rs`), the voice worker process, the connection //! to it, and the Discord core. Used by the app binary (and the Tauri shell from M5). use std::path::Path; use std::sync::Arc; use serde_json::Value; use tokio::sync::mpsc; use crate::conversation::{self, Conversation}; use crate::discord::{CoreEvent, DiscordCore}; use crate::settings::load_settings; use crate::strata::Llm; use crate::worker_client::{WorkerClient, WorkerClientOptions}; use crate::worker_process::{ProcessEvent, WorkerProcess, WorkerProcessOptions}; pub struct Services { pub core: DiscordCore, pub events: mpsc::UnboundedReceiver, pub worker: WorkerClient, pub worker_process: WorkerProcess, /// The conversation's missed requests, for the dashboard. pub missed: conversation::MissedLog, pub llm: Arc, pub browser: BrowserRun, } /// The bot browser of a run (admin.spec.md, "Bot browser"). pub enum BrowserRun { /// Not started: the feature is off, or (with a note) it is not installed. Off(Option), Failed(String), Running(Arc), } impl BrowserRun { /// `{state, note}` for the dashboard. pub fn view(&self) -> Value { let (state, note) = match self { BrowserRun::Off(note) => ("off", note.clone()), BrowserRun::Failed(e) => ("failed", Some(e.clone())), BrowserRun::Running(b) if b.running() => ("ready", None), BrowserRun::Running(_) => ("exited", None), }; serde_json::json!({"state": state, "note": note}) } pub async fn stop(&self) { if let BrowserRun::Running(b) = self { b.stop().await; } } } impl Services { /// Stop everything; an error means the LLM's server may still run. pub async fn stop(&self) -> Result<(), String> { self.core.close().await; self.worker.close(); self.worker_process.stop().await; self.browser.stop().await; self.llm.stop().await } } /// Start the bot browser when its feature is on and it is installed; a failure leaves the services /// running without it. async fn start_browser(data_dir: &Path, settings: &crate::settings::Settings) -> BrowserRun { if !settings.features.browser { return BrowserRun::Off(None); } let layout = crate::install::Layout { data: data_dir.to_path_buf(), src: std::path::PathBuf::new() }; if !crate::install::Installer::new(layout.clone(), crate::install::steps()).done("browser") { crate::warn_log!("browser", "the bot browser is not installed"); return BrowserRun::Off(Some("설치되지 않음".into())); } match crate::browser::BotBrowser::start(&layout.chrome_exe(), &crate::browser::profile_dir(data_dir)).await { Ok(b) => BrowserRun::Running(Arc::new(b)), Err(e) => { crate::warn_log!("browser", "Warning: the bot browser did not start: {e}"); BrowserRun::Failed(e) } } } /// Start the services; the LLM first (its first start downloads the model), so nothing answers /// before the brain can. A failure stops what already started. pub async fn start_services(data_dir: &Path, llm: Arc) -> Result> { let settings = load_settings(&data_dir.join("config").join("settings.json"))?; // The stored token (the setup wizard keeps it encrypted, admin.spec.md), else the environment. let secrets = crate::secrets::Secrets::new(&data_dir.join("config").join("secrets.dat")); let stored = secrets.get(crate::secrets::DISCORD_TOKEN)?; // The MCP servers' environment values stay encrypted on disk: they reach the brain only in the // worker's environment (admin.spec.md, "Features and MCP servers"). let mcp_env = crate::features::mcp_env(&secrets, &settings.mcp_servers)?; let token = match stored { Some(t) => t, None => std::env::var("JARVIS_DISCORD_TOKEN").map_err(|_| "no bot token: set it in the app or JARVIS_DISCORD_TOKEN")?, }; // What these services are given stays masked in the logs until the next start, also if it is // replaced or removed in the store meanwhile (admin.spec.md, "Logs"). if let Some(hub) = crate::logs::installed() { hub.set_secrets_in_use(secrets_in_use(&token, &mcp_env)); } let (base_url, model) = llm.start(&settings).await?; let brain_config = match crate::strata::write_brain_config(data_dir, &base_url, &model, &settings) { Ok(f) => f, Err(e) => return Err(after_llm_stop(&llm, format!("could not write the brain settings: {e}")).await.into()), }; let browser = start_browser(data_dir, &settings).await; let w = &settings.voice_worker; let mut wopts = WorkerProcessOptions::new(&w.python, &w.cwd, settings.worker_args()); // With the installed runtime, the worker's models, caches and temp files stay in the data folder. let layout = crate::install::Layout { data: data_dir.to_path_buf(), src: w.cwd.clone() }; if crate::install::Installer::new(layout.clone(), crate::install::steps()).done("settings") { wopts.env.extend(layout.env()); } wopts.env.insert("JARVIS_CONFIG_PATH".into(), brain_config.to_string_lossy().into_owned()); wopts.env.insert(crate::features::MCP_ENV_VAR.into(), mcp_env.to_string()); if let BrowserRun::Running(b) = &browser { wopts.env.insert("JARVIS_BROWSER_CDP".into(), b.cdp_url()); } // Background collection (admin.spec.md): the enabled targets, and the keep days in any case. match crate::collect::write_config(data_dir, &settings) { Ok(f) => { wopts.env.insert(crate::collect::COLLECT_ENV_VAR.into(), f.to_string_lossy().into_owned()); } Err(e) => return Err(after_stop(&llm, &browser, format!("could not write the collection settings: {e}")).await.into()), } let worker_process = match WorkerProcess::new(wopts) { Ok(p) => p, Err(e) => return Err(after_stop(&llm, &browser, e.to_string()).await.into()), }; let mut process_events = worker_process.subscribe(); worker_process.start(); let mut opts = WorkerClientOptions::new(worker_process.url(), worker_process.token.clone()); opts.stt = settings.voice.stt.clone(); opts.stt.insert("hotwords".into(), Value::from(settings.voice.wake_words.clone())); opts.memory = settings.memory.clone(); let (worker, worker_events) = WorkerClient::new(opts); // The worker loads its models before it listens: connect once it says READY (after restarts the // client keeps retrying on its own). let w2 = worker.clone(); tokio::spawn(async move { loop { match process_events.recv().await { Ok(ProcessEvent::Ready) => return w2.start(), Ok(_) => {} Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {} Err(_) => return, } } }); let (calls, response) = (settings.call.clone(), settings.response.clone()); let chat_calls = conversation::ChatCalls::from_settings(&settings); let (core, core_events) = DiscordCore::new(settings, token, worker.clone(), worker_events); // Call detection and the response policy answer what is heard; every event still reaches `events`. let (out, events) = mpsc::unbounded_channel(); let conv = Conversation::new(core.clone(), calls, response).with_chat(chat_calls); let missed = conv.missed_handle(); tokio::spawn(conversation::run(conv, core_events, out)); if let Err(e) = core.start().await { worker.close(); worker_process.stop().await; return Err(after_stop(&llm, &browser, e.to_string()).await.into()); } crate::info_log!("app", "services started"); Ok(Services { core, events, worker, worker_process, missed, llm, browser }) } /// The secret values the services get: the bot token and the MCP servers' environment values. pub fn secrets_in_use(token: &str, mcp_env: &Value) -> Vec { let mut v = vec![token.to_owned()]; for vars in mcp_env.as_object().into_iter().flat_map(|m| m.values()) { v.extend(vars.as_object().into_iter().flat_map(|m| m.values()).filter_map(Value::as_str).map(str::to_owned)); } v } /// Stop the bot browser and the LLM after a failed start. async fn after_stop(llm: &Llm, browser: &BrowserRun, error: String) -> String { browser.stop().await; after_llm_stop(llm, error).await } /// Stop the LLM after a failed start; the error also says if its server may still run. async fn after_llm_stop(llm: &Llm, error: String) -> String { match llm.stop().await { Ok(()) => error, Err(stop) => format!("{error} ({stop})"), } } #[cfg(test)] mod tests { use super::*; #[test] fn the_services_secrets_are_the_token_and_every_mcp_value() { let env = serde_json::json!({"brave": {"BRAVE_API_KEY": "k1"}, "other": {"A": "k2", "B": "k3"}}); let mut v = secrets_in_use("tok", &env); v.sort(); assert_eq!(v, vec!["k1", "k2", "k3", "tok"]); } }