feat(app): the bot's own Chrome in the data folder, reaching only public addresses
- install step browser: Chrome for Testing 157 (pinned SHA-256) into runtime/chrome - services start it headless with its own profile when features.browser is on; CDP on a free loopback port passed to the worker as JARVIS_BROWSER_CDP; a failure leaves the services running - every Chrome connection goes through the app's loopback egress proxy: one lookup, public addresses only (403 otherwise, no host logged), QUIC off, WebRTC only through the proxy - stop: Browser.close, then kill after 5 s; dashboard shows the browser's state - the worker's runtime environment follows the settings step, not every step being installed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -22,6 +22,34 @@ pub struct Services {
|
||||
/// The conversation's missed requests, for the dashboard.
|
||||
pub missed: conversation::MissedLog,
|
||||
pub llm: Arc<Llm>,
|
||||
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<String>),
|
||||
Failed(String),
|
||||
Running(Arc<crate::browser::BotBrowser>),
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -30,10 +58,31 @@ impl Services {
|
||||
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<Llm>) -> Result<Services, Box<dyn std::error::Error + Send + Sync>> {
|
||||
@@ -60,24 +109,28 @@ pub async fn start_services(data_dir: &Path, llm: Arc<Llm>) -> Result<Services,
|
||||
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()).all_done() {
|
||||
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_llm_stop(&llm, format!("could not write the collection settings: {e}")).await.into()),
|
||||
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_llm_stop(&llm, e.to_string()).await.into()),
|
||||
Err(e) => return Err(after_stop(&llm, &browser, e.to_string()).await.into()),
|
||||
};
|
||||
let mut process_events = worker_process.subscribe();
|
||||
worker_process.start();
|
||||
@@ -111,10 +164,10 @@ pub async fn start_services(data_dir: &Path, llm: Arc<Llm>) -> Result<Services,
|
||||
if let Err(e) = core.start().await {
|
||||
worker.close();
|
||||
worker_process.stop().await;
|
||||
return Err(after_llm_stop(&llm, e.to_string()).await.into());
|
||||
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 })
|
||||
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.
|
||||
@@ -126,6 +179,12 @@ pub fn secrets_in_use(token: &str, mcp_env: &Value) -> Vec<String> {
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user