AnyBot: first commit of the working desktop app
Rust/egui rebuild of Grok Bot as of 2026-11-04: chat with streaming
providers (xAI, OpenAI, Claude, Gemini, OpenRouter, OpenCode Go, Ollama,
custom), tools and approvals, the bot's Docker computer, computer use on
macOS / Linux X11 / Wayland, AnyBot's browser with a full DevTools-based
toolset, the encrypted vault, pinned grid and sidebar sections, Markdown
with Mermaid diagrams, phone access through the relay, and the Linux
build and test scripts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha3oEtWeHh58zA7ErHa
2026-11-04 08:51:51 +00:00
|
|
|
|
// Getting the person's attention when a bot needs them (or finished) while they're away.
|
|
|
|
|
|
//
|
2026-11-09 08:37:33 +00:00
|
|
|
|
// Behaviour spec:
|
|
|
|
|
|
// - macOS 10.14+ and a real .app bundle: ask UNUserNotificationCenter, waiting at most 3 s.
|
AnyBot: first commit of the working desktop app
Rust/egui rebuild of Grok Bot as of 2026-11-04: chat with streaming
providers (xAI, OpenAI, Claude, Gemini, OpenRouter, OpenCode Go, Ollama,
custom), tools and approvals, the bot's Docker computer, computer use on
macOS / Linux X11 / Wayland, AnyBot's browser with a full DevTools-based
toolset, the encrypted vault, pinned grid and sidebar sections, Markdown
with Mermaid diagrams, phone access through the relay, and the Linux
build and test scripts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha3oEtWeHh58zA7ErHa
2026-11-04 08:51:51 +00:00
|
|
|
|
// authorizationStatus 0..4 = notDetermined/denied/authorized/provisional/ephemeral (else "unknown");
|
|
|
|
|
|
// alertSetting 0..2 = notSupported/disabled/enabled (else "unknown").
|
2026-11-09 08:37:33 +00:00
|
|
|
|
// - authorized/provisional/ephemeral → granted, denied → denied, notDetermined → undetermined.
|
|
|
|
|
|
// Banners only when authorized and alerts enabled.
|
|
|
|
|
|
// - The desktop counts as present only when not locked, not idle for 90 s and the window is
|
|
|
|
|
|
// alive; re-checked every 10 s.
|
AnyBot: first commit of the working desktop app
Rust/egui rebuild of Grok Bot as of 2026-11-04: chat with streaming
providers (xAI, OpenAI, Claude, Gemini, OpenRouter, OpenCode Go, Ollama,
custom), tools and approvals, the bot's Docker computer, computer use on
macOS / Linux X11 / Wayland, AnyBot's browser with a full DevTools-based
toolset, the encrypted vault, pinned grid and sidebar sections, Markdown
with Mermaid diagrams, phone access through the relay, and the Linux
build and test scripts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha3oEtWeHh58zA7ErHa
2026-11-04 08:51:51 +00:00
|
|
|
|
|
|
|
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
|
|
|
2026-11-09 08:37:33 +00:00
|
|
|
|
/// Away after this much keyboard/mouse silence `Z_e=9e4`).
|
AnyBot: first commit of the working desktop app
Rust/egui rebuild of Grok Bot as of 2026-11-04: chat with streaming
providers (xAI, OpenAI, Claude, Gemini, OpenRouter, OpenCode Go, Ollama,
custom), tools and approvals, the bot's Docker computer, computer use on
macOS / Linux X11 / Wayland, AnyBot's browser with a full DevTools-based
toolset, the encrypted vault, pinned grid and sidebar sections, Markdown
with Mermaid diagrams, phone access through the relay, and the Linux
build and test scripts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha3oEtWeHh58zA7ErHa
2026-11-04 08:51:51 +00:00
|
|
|
|
pub const IDLE_THRESHOLD_SECS: f64 = 90.0;
|
2026-11-09 08:37:33 +00:00
|
|
|
|
/// How often presence is re-read `uBt=1e4`).
|
AnyBot: first commit of the working desktop app
Rust/egui rebuild of Grok Bot as of 2026-11-04: chat with streaming
providers (xAI, OpenAI, Claude, Gemini, OpenRouter, OpenCode Go, Ollama,
custom), tools and approvals, the bot's Docker computer, computer use on
macOS / Linux X11 / Wayland, AnyBot's browser with a full DevTools-based
toolset, the encrypted vault, pinned grid and sidebar sections, Markdown
with Mermaid diagrams, phone access through the relay, and the Linux
build and test scripts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha3oEtWeHh58zA7ErHa
2026-11-04 08:51:51 +00:00
|
|
|
|
pub const PRESENCE_POLL_SECS: f64 = 10.0;
|
|
|
|
|
|
|
|
|
|
|
|
/// Seconds since the last keyboard or mouse input (macOS `HIDIdleTime`, in nanoseconds).
|
|
|
|
|
|
pub fn parse_hid_idle(ioreg: &str) -> Option<f64> {
|
|
|
|
|
|
let line = ioreg.lines().find(|l| l.contains("\"HIDIdleTime\""))?;
|
|
|
|
|
|
let n: f64 = line.rsplit('=').next()?.trim().parse().ok()?;
|
|
|
|
|
|
Some(n / 1e9)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn system_idle_secs() -> Option<f64> {
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
|
return crate::desk::idle_secs();
|
|
|
|
|
|
#[allow(unreachable_code)]
|
|
|
|
|
|
if !cfg!(target_os = "macos") {
|
|
|
|
|
|
return None;
|
|
|
|
|
|
}
|
|
|
|
|
|
let out = Command::new("ioreg").args(["-c", "IOHIDSystem", "-d", "4"]).stderr(Stdio::null()).output().ok()?;
|
|
|
|
|
|
parse_hid_idle(&String::from_utf8_lossy(&out.stdout))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-19 14:28:31 +00:00
|
|
|
|
/// system_idle_secs read on a thread of its own, for the window: `ioreg` is a new process each
|
|
|
|
|
|
/// time (30–80 ms on an older Mac — a hitch every 10 s on the window's thread). Returns the last
|
|
|
|
|
|
/// reading (at most one poll old) and starts the next one unless one is still running.
|
|
|
|
|
|
pub fn idle_secs_in_background() -> Option<f64> {
|
|
|
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
|
static LAST: Mutex<Option<f64>> = Mutex::new(None);
|
|
|
|
|
|
static READING: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
|
if !READING.swap(true, Ordering::AcqRel) {
|
|
|
|
|
|
std::thread::spawn(|| {
|
|
|
|
|
|
let v = system_idle_secs();
|
|
|
|
|
|
*LAST.lock().unwrap_or_else(|e| e.into_inner()) = v;
|
|
|
|
|
|
READING.store(false, Ordering::Release);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
*LAST.lock().unwrap_or_else(|e| e.into_inner())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
AnyBot: first commit of the working desktop app
Rust/egui rebuild of Grok Bot as of 2026-11-04: chat with streaming
providers (xAI, OpenAI, Claude, Gemini, OpenRouter, OpenCode Go, Ollama,
custom), tools and approvals, the bot's Docker computer, computer use on
macOS / Linux X11 / Wayland, AnyBot's browser with a full DevTools-based
toolset, the encrypted vault, pinned grid and sidebar sections, Markdown
with Mermaid diagrams, phone access through the relay, and the Linux
build and test scripts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha3oEtWeHh58zA7ErHa
2026-11-04 08:51:51 +00:00
|
|
|
|
/// Notify unless the person is looking at AnyBot right now: the window is in front and they
|
|
|
|
|
|
/// were active within the last 90 s.
|
|
|
|
|
|
pub fn should_notify(window_focused: bool, idle_secs: Option<f64>) -> bool {
|
|
|
|
|
|
!window_focused || idle_secs.map(|s| s >= IDLE_THRESHOLD_SECS).unwrap_or(false)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------- applying a new version while you're away ----------
|
2026-11-09 08:37:33 +00:00
|
|
|
|
// Behaviour spec: apply when 自動更新 is on (default) and an update is staged, and the machine
|
|
|
|
|
|
// is idle: (screen locked || screensaver on) and idle for >= 600 s, and no bot work running.
|
|
|
|
|
|
// Checks: first 30 s after launch, then every 3600 s plus up to 300 s of random jitter.
|
AnyBot: first commit of the working desktop app
Rust/egui rebuild of Grok Bot as of 2026-11-04: chat with streaming
providers (xAI, OpenAI, Claude, Gemini, OpenRouter, OpenCode Go, Ollama,
custom), tools and approvals, the bot's Docker computer, computer use on
macOS / Linux X11 / Wayland, AnyBot's browser with a full DevTools-based
toolset, the encrypted vault, pinned grid and sidebar sections, Markdown
with Mermaid diagrams, phone access through the relay, and the Linux
build and test scripts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha3oEtWeHh58zA7ErHa
2026-11-04 08:51:51 +00:00
|
|
|
|
|
|
|
|
|
|
pub const AUTO_UPDATE_IDLE_SECS: f64 = 600.0;
|
|
|
|
|
|
pub const UPDATE_FIRST_CHECK_SECS: u64 = 30;
|
|
|
|
|
|
pub const UPDATE_CHECK_INTERVAL_SECS: u64 = 3600;
|
|
|
|
|
|
pub const UPDATE_CHECK_JITTER_SECS: u64 = 300;
|
|
|
|
|
|
|
2026-11-09 08:37:33 +00:00
|
|
|
|
/// Every input of the idle-update rule, in one place.
|
AnyBot: first commit of the working desktop app
Rust/egui rebuild of Grok Bot as of 2026-11-04: chat with streaming
providers (xAI, OpenAI, Claude, Gemini, OpenRouter, OpenCode Go, Ollama,
custom), tools and approvals, the bot's Docker computer, computer use on
macOS / Linux X11 / Wayland, AnyBot's browser with a full DevTools-based
toolset, the encrypted vault, pinned grid and sidebar sections, Markdown
with Mermaid diagrams, phone access through the relay, and the Linux
build and test scripts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha3oEtWeHh58zA7ErHa
2026-11-04 08:51:51 +00:00
|
|
|
|
pub fn should_apply_update_now(enabled: bool, update_ready: bool, screen_locked: bool, screensaver: bool, idle_secs: Option<f64>, bots_idle: bool) -> bool {
|
|
|
|
|
|
enabled && update_ready && (screen_locked || screensaver) && idle_secs.map(|s| s >= AUTO_UPDATE_IDLE_SECS).unwrap_or(false) && bots_idle
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// The screen is locked (Electron's `getSystemIdleState` "locked"; here the session flag).
|
|
|
|
|
|
pub fn screen_locked() -> bool {
|
|
|
|
|
|
if !cfg!(target_os = "macos") {
|
|
|
|
|
|
// systemd-logind knows when the session is locked.
|
|
|
|
|
|
let id = std::env::var("XDG_SESSION_ID").unwrap_or_default();
|
|
|
|
|
|
let mut c = Command::new("loginctl");
|
|
|
|
|
|
c.arg("show-session");
|
|
|
|
|
|
if !id.is_empty() {
|
|
|
|
|
|
c.arg(&id);
|
|
|
|
|
|
}
|
|
|
|
|
|
return c.args(["-p", "LockedHint"]).stderr(Stdio::null()).output().map(|o| String::from_utf8_lossy(&o.stdout).trim() == "LockedHint=yes").unwrap_or(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
Command::new("ioreg").args(["-n", "Root", "-d1"]).stderr(Stdio::null()).output().map(|o| String::from_utf8_lossy(&o.stdout).contains("\"CGSSessionScreenIsLocked\" = Yes")).unwrap_or(false)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// The screensaver is running: the original's `NRn` runs `pgrep -x ScreenSaverEngine` (2 s timeout).
|
|
|
|
|
|
pub fn screensaver_active() -> bool {
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
|
return crate::desk::screensaver_on();
|
|
|
|
|
|
#[allow(unreachable_code)]
|
|
|
|
|
|
if !cfg!(target_os = "macos") {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
let Ok(mut c) = Command::new("pgrep").args(["-x", "ScreenSaverEngine"]).stdout(Stdio::null()).stderr(Stdio::null()).spawn() else { return false };
|
|
|
|
|
|
let start = std::time::Instant::now();
|
|
|
|
|
|
while start.elapsed() < std::time::Duration::from_secs(2) {
|
|
|
|
|
|
if let Ok(Some(st)) = c.try_wait() {
|
|
|
|
|
|
return st.success();
|
|
|
|
|
|
}
|
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(20));
|
|
|
|
|
|
}
|
|
|
|
|
|
let _ = c.kill();
|
2026-11-05 07:48:22 +00:00
|
|
|
|
let _ = c.wait();
|
AnyBot: first commit of the working desktop app
Rust/egui rebuild of Grok Bot as of 2026-11-04: chat with streaming
providers (xAI, OpenAI, Claude, Gemini, OpenRouter, OpenCode Go, Ollama,
custom), tools and approvals, the bot's Docker computer, computer use on
macOS / Linux X11 / Wayland, AnyBot's browser with a full DevTools-based
toolset, the encrypted vault, pinned grid and sidebar sections, Markdown
with Mermaid diagrams, phone access through the relay, and the Linux
build and test scripts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha3oEtWeHh58zA7ErHa
2026-11-04 08:51:51 +00:00
|
|
|
|
false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Next periodic check: one hour plus a random part of five minutes.
|
|
|
|
|
|
pub fn next_update_check_secs(random_0_1: f64) -> u64 {
|
|
|
|
|
|
UPDATE_CHECK_INTERVAL_SECS + (random_0_1.clamp(0.0, 1.0) * UPDATE_CHECK_JITTER_SECS as f64).floor() as u64
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn applescript_quote(s: &str) -> String {
|
|
|
|
|
|
let clean: String = s.chars().filter(|c| !c.is_control() || *c == ' ').collect();
|
|
|
|
|
|
format!("\"{}\"", clean.replace('\\', "\\\\").replace('"', "\\\""))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// The AppleScript line for a banner (title + message), with text safely quoted.
|
安全、修正: harden sandboxes, approval gates, vault, relay roles, and fix protocol/UI bugs
- macOS 沙盒與工具 (tools.rs, policy.rs, docindex.rs): block AppleScript raw events and
concatenation outside the sandbox; stop exposing ~/.ssh or localhost sockets in Sealed
mode; deny writes to shell profiles, LaunchAgents and .git/hooks; expand secret-file
ignores and enforce symlink/policy checks across SSH, device and document search.
- 電腦操作與遮罩 (computer.rs, agent.rs, marks.rs, desktop.rs, cloudbox.rs, portal.rs):
forbid clicking or typing into AnyBot's own windows; extend the sensitive-action hard
gate to raw coordinates, Return/Delete submits and remote devices; stop batch steps if
the front app changes; black out secrets on zoom, show, >600-node trees, adjacent OTP
labels, box screenshots and control text lists; preserve non-text Wayland clipboards.
- 保險庫與瀏覽器 (vault.rs, vault_store.rs, browser.rs, botbrowser.rs, web.rs, box/browser):
verify tab host and HTTPS atomically at fill time; taint filled tabs against browser_eval
and PDF export while masking card/CVC values and screenshots; remember sites only on
always-allow; drop --remote-allow-origins=*; gate private URLs and block metadata/Docker
ports, OrbStack 198.18/15 and IPv6 transition ranges.
- 帳號伺服器、同步與傳送 (relay/, remote.rs, mirror_ui.rs, share.rs, drops.rs): isolate
paired hands hosts from brain sync, tickets, pairing and backup overwrites; redact
routine trigger secrets and clear REDACTED when endpoints change; isolate channel and
team-member turns from owner memories, appctl and grants; scrub shared bot exports and
reset grants/routines on import; validate drop IDs and serialize chunk writes.
- 網路與協定穩定性 (httpd.rs, tunnel.rs, vnc.rs, Caddyfile*): fix chunked integer overflow
and CRLF header injection; enforce Origin checks, connection/job caps, 200 MB backup
reads, unique atomic temp files and ASCII hex guards; bound deflate unzip and VNC sniff
buffers; move VNC stdin/text off the tunnel loop.
- 介面、排程、語言與其他修正 (app.rs, i18n.rs, providers.rs, skills.rs, routines.rs,
setup.rs, store.rs, preview.rs, attach.rs, speech.rs, teams.rs, mcp.rs, voice.rs):
wire up the delete-routine confirmation dialog and mirror run-now; preserve unsaved
mirror edits across brief disconnects; give Chinese skills distinct folders and approval
gates; convert parsed tool JSON fields without corrupting code fences, URLs or paths;
fix Hourly DST offsets and time out unattended routine approvals; lock --install-skill,
cap docx/xlsx unzip sizes, isolate test env keys, and tighten local file permissions.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-12 06:34:10 +00:00
|
|
|
|
#[cfg(test)]
|
AnyBot: first commit of the working desktop app
Rust/egui rebuild of Grok Bot as of 2026-11-04: chat with streaming
providers (xAI, OpenAI, Claude, Gemini, OpenRouter, OpenCode Go, Ollama,
custom), tools and approvals, the bot's Docker computer, computer use on
macOS / Linux X11 / Wayland, AnyBot's browser with a full DevTools-based
toolset, the encrypted vault, pinned grid and sidebar sections, Markdown
with Mermaid diagrams, phone access through the relay, and the Linux
build and test scripts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha3oEtWeHh58zA7ErHa
2026-11-04 08:51:51 +00:00
|
|
|
|
pub fn banner_script(title: &str, body: &str) -> String {
|
提示音: a chime whenever a bot needs the person
A request to allow something, or a step only the person can do, only
bounced the Dock icon; a banner (with macOS's Glass sound) came only
when they were away, and on Linux notify-send made no sound at all. So
someone looking at another window on the same screen, or at AnyBot
itself without watching it, missed that a bot was waiting.
Now on_new_approval rings a chime every time, focused or not: two soft
bell notes going up (E6, then A6), 0.8 s, quiet, synthesized
(voice::chime_samples) so it is the same on macOS, Windows and Linux.
Several requests at once ring once; the banner that comes with it when
away stays silent so it doesn't ring twice (the 「完成了」 banner keeps
its sound). A screen of another brain rings for the brain's requests
too (they come through the same on_new_approval); a brain on a server
doesn't. 設定 › 一般 › 通知 › 「需要你時發出提示音」 turns it off (on
by default; turning it on plays it once).
2026-11-10 17:01:05 +00:00
|
|
|
|
banner_script_with(title, body, true)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Same, with or without macOS's own sound (none when AnyBot rings its 提示音 itself).
|
|
|
|
|
|
pub fn banner_script_with(title: &str, body: &str, sound: bool) -> String {
|
AnyBot: first commit of the working desktop app
Rust/egui rebuild of Grok Bot as of 2026-11-04: chat with streaming
providers (xAI, OpenAI, Claude, Gemini, OpenRouter, OpenCode Go, Ollama,
custom), tools and approvals, the bot's Docker computer, computer use on
macOS / Linux X11 / Wayland, AnyBot's browser with a full DevTools-based
toolset, the encrypted vault, pinned grid and sidebar sections, Markdown
with Mermaid diagrams, phone access through the relay, and the Linux
build and test scripts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha3oEtWeHh58zA7ErHa
2026-11-04 08:51:51 +00:00
|
|
|
|
let body: String = body.chars().take(180).collect();
|
提示音: a chime whenever a bot needs the person
A request to allow something, or a step only the person can do, only
bounced the Dock icon; a banner (with macOS's Glass sound) came only
when they were away, and on Linux notify-send made no sound at all. So
someone looking at another window on the same screen, or at AnyBot
itself without watching it, missed that a bot was waiting.
Now on_new_approval rings a chime every time, focused or not: two soft
bell notes going up (E6, then A6), 0.8 s, quiet, synthesized
(voice::chime_samples) so it is the same on macOS, Windows and Linux.
Several requests at once ring once; the banner that comes with it when
away stays silent so it doesn't ring twice (the 「完成了」 banner keeps
its sound). A screen of another brain rings for the brain's requests
too (they come through the same on_new_approval); a brain on a server
doesn't. 設定 › 一般 › 通知 › 「需要你時發出提示音」 turns it off (on
by default; turning it on plays it once).
2026-11-10 17:01:05 +00:00
|
|
|
|
let line = format!("display notification {} with title {}", applescript_quote(&body), applescript_quote(title));
|
|
|
|
|
|
if sound {
|
|
|
|
|
|
format!("{line} sound name \"Glass\"")
|
|
|
|
|
|
} else {
|
|
|
|
|
|
line
|
|
|
|
|
|
}
|
AnyBot: first commit of the working desktop app
Rust/egui rebuild of Grok Bot as of 2026-11-04: chat with streaming
providers (xAI, OpenAI, Claude, Gemini, OpenRouter, OpenCode Go, Ollama,
custom), tools and approvals, the bot's Docker computer, computer use on
macOS / Linux X11 / Wayland, AnyBot's browser with a full DevTools-based
toolset, the encrypted vault, pinned grid and sidebar sections, Markdown
with Mermaid diagrams, phone access through the relay, and the Linux
build and test scripts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha3oEtWeHh58zA7ErHa
2026-11-04 08:51:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-15 16:06:12 +00:00
|
|
|
|
/// Show a banner, with the system's sound. Doesn't wait.
|
AnyBot: first commit of the working desktop app
Rust/egui rebuild of Grok Bot as of 2026-11-04: chat with streaming
providers (xAI, OpenAI, Claude, Gemini, OpenRouter, OpenCode Go, Ollama,
custom), tools and approvals, the bot's Docker computer, computer use on
macOS / Linux X11 / Wayland, AnyBot's browser with a full DevTools-based
toolset, the encrypted vault, pinned grid and sidebar sections, Markdown
with Mermaid diagrams, phone access through the relay, and the Linux
build and test scripts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha3oEtWeHh58zA7ErHa
2026-11-04 08:51:51 +00:00
|
|
|
|
pub fn banner(title: &str, body: &str) {
|
2026-11-15 16:06:12 +00:00
|
|
|
|
banner_for(title, body, true, None)
|
提示音: a chime whenever a bot needs the person
A request to allow something, or a step only the person can do, only
bounced the Dock icon; a banner (with macOS's Glass sound) came only
when they were away, and on Linux notify-send made no sound at all. So
someone looking at another window on the same screen, or at AnyBot
itself without watching it, missed that a bot was waiting.
Now on_new_approval rings a chime every time, focused or not: two soft
bell notes going up (E6, then A6), 0.8 s, quiet, synthesized
(voice::chime_samples) so it is the same on macOS, Windows and Linux.
Several requests at once ring once; the banner that comes with it when
away stays silent so it doesn't ring twice (the 「完成了」 banner keeps
its sound). A screen of another brain rings for the brain's requests
too (they come through the same on_new_approval); a brain on a server
doesn't. 設定 › 一般 › 通知 › 「需要你時發出提示音」 turns it off (on
by default; turning it on plays it once).
2026-11-10 17:01:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-15 16:06:12 +00:00
|
|
|
|
/// A banner about one bot: a click on it opens that bot's chat (`take_clicked`).
|
|
|
|
|
|
///
|
|
|
|
|
|
/// - macOS, inside AnyBot.app: the app's own banner (`center`), with AnyBot's icon (the cat on
|
|
|
|
|
|
/// yellow); a click brings AnyBot to the front on that bot. Run from a build folder (no bundle),
|
|
|
|
|
|
/// macOS's `display notification` instead (that banner shows Script Editor's icon).
|
|
|
|
|
|
/// - Linux: the desktop's notification service through notify-send, with AnyBot's icon.
|
|
|
|
|
|
pub fn banner_for(title: &str, body: &str, sound: bool, bot: Option<u64>) {
|
|
|
|
|
|
let body: String = body.chars().take(180).collect();
|
AnyBot: first commit of the working desktop app
Rust/egui rebuild of Grok Bot as of 2026-11-04: chat with streaming
providers (xAI, OpenAI, Claude, Gemini, OpenRouter, OpenCode Go, Ollama,
custom), tools and approvals, the bot's Docker computer, computer use on
macOS / Linux X11 / Wayland, AnyBot's browser with a full DevTools-based
toolset, the encrypted vault, pinned grid and sidebar sections, Markdown
with Mermaid diagrams, phone access through the relay, and the Linux
build and test scripts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha3oEtWeHh58zA7ErHa
2026-11-04 08:51:51 +00:00
|
|
|
|
if !cfg!(target_os = "macos") {
|
|
|
|
|
|
// The desktop's notification service (GNOME, KDE, XFCE…) through notify-send.
|
2026-11-15 16:06:12 +00:00
|
|
|
|
let mut cmd = Command::new("notify-send");
|
|
|
|
|
|
cmd.args(["-a", "AnyBot"]);
|
|
|
|
|
|
if let Some(icon) = icon_png() {
|
|
|
|
|
|
cmd.arg("-i").arg(icon);
|
|
|
|
|
|
}
|
|
|
|
|
|
let _ = crate::computer::spawn_reaped(cmd.args([title, &body]).stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
if center::post(title, &body, sound, bot) {
|
AnyBot: first commit of the working desktop app
Rust/egui rebuild of Grok Bot as of 2026-11-04: chat with streaming
providers (xAI, OpenAI, Claude, Gemini, OpenRouter, OpenCode Go, Ollama,
custom), tools and approvals, the bot's Docker computer, computer use on
macOS / Linux X11 / Wayland, AnyBot's browser with a full DevTools-based
toolset, the encrypted vault, pinned grid and sidebar sections, Markdown
with Mermaid diagrams, phone access through the relay, and the Linux
build and test scripts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha3oEtWeHh58zA7ErHa
2026-11-04 08:51:51 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2026-11-15 16:06:12 +00:00
|
|
|
|
let _ = bot;
|
|
|
|
|
|
let _ = crate::computer::spawn_reaped(Command::new("osascript").arg("-e").arg(banner_script_with(title, &body, sound)).stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// One banner from a command (`anybot --banner`): sends it and says which way it went.
|
|
|
|
|
|
pub fn banner_now(title: &str, body: &str) -> String {
|
|
|
|
|
|
banner_for(title, body, true, None);
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
{
|
|
|
|
|
|
if !center::usable() {
|
|
|
|
|
|
return "不在 AnyBot.app 裡:用 display notification 送出(圖示會是「工序指令編寫程式」)".into();
|
|
|
|
|
|
}
|
|
|
|
|
|
// (A moment for the system to take it before the program ends.)
|
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
|
|
|
|
|
format!("AnyBot 自己的通知,通知中心裡有 {} 則 AnyBot 的通知", center::delivered())
|
|
|
|
|
|
}
|
|
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
|
|
|
|
{
|
|
|
|
|
|
"notify-send".into()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Get ready to show banners (macOS: hear clicks on them, and show them while AnyBot is in front
|
|
|
|
|
|
/// but the person is idle). Call once at start, on the main thread.
|
|
|
|
|
|
pub fn prepare() {
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
center::prepare();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Bots whose banner the person clicked since the last call (the app opens that chat).
|
|
|
|
|
|
pub fn take_clicked() -> Vec<u64> {
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
{
|
|
|
|
|
|
center::take_clicked()
|
|
|
|
|
|
}
|
|
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
|
|
|
|
{
|
|
|
|
|
|
vec![]
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// The identifier a banner gets: which bot it is about, and a number so each one is its own.
|
|
|
|
|
|
pub fn banner_id(bot: Option<u64>, n: u64) -> String {
|
|
|
|
|
|
format!("anybot-{}-{n}", bot.map(|b| b.to_string()).unwrap_or_else(|| "-".into()))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// The bot a banner identifier is about.
|
|
|
|
|
|
pub fn bot_of_banner(id: &str) -> Option<u64> {
|
|
|
|
|
|
id.strip_prefix("anybot-")?.split('-').next()?.parse().ok()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// AnyBot's icon as a PNG file for notify-send (drawn once into the data folder).
|
|
|
|
|
|
#[cfg_attr(target_os = "macos", allow(dead_code))]
|
|
|
|
|
|
fn icon_png() -> Option<std::path::PathBuf> {
|
|
|
|
|
|
static PATH: std::sync::OnceLock<Option<std::path::PathBuf>> = std::sync::OnceLock::new();
|
|
|
|
|
|
PATH.get_or_init(|| {
|
|
|
|
|
|
let path = crate::model::State::path().parent()?.join("anybot-icon.png");
|
|
|
|
|
|
if path.exists() {
|
|
|
|
|
|
return Some(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
let icon = crate::app_icon_rgba(128);
|
|
|
|
|
|
image::RgbaImage::from_raw(128, 128, icon)?.save(&path).ok()?;
|
|
|
|
|
|
Some(path)
|
|
|
|
|
|
})
|
|
|
|
|
|
.clone()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// AnyBot.app's own banners: NSUserNotificationCenter. The newer UNUserNotificationCenter only
|
|
|
|
|
|
/// lets apps signed by an Apple developer team send notifications (it answers "Notifications are
|
|
|
|
|
|
/// not allowed for this application"); AnyBot signs itself on each Mac (deploy/mac/sign.sh), and
|
|
|
|
|
|
/// the older center takes its banners, with the app's icon, without asking first. 系統設定 › 通知
|
|
|
|
|
|
/// › AnyBot still decides whether and how they show.
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
mod center {
|
|
|
|
|
|
use objc2::rc::Retained;
|
|
|
|
|
|
use objc2::runtime::{AnyClass, NSObject, NSObjectProtocol, ProtocolObject};
|
|
|
|
|
|
use objc2::{define_class, msg_send, ClassType};
|
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
|
use objc2_foundation::{NSBundle, NSString, NSUserNotification, NSUserNotificationCenter, NSUserNotificationCenterDelegate};
|
|
|
|
|
|
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
|
|
|
|
|
use std::sync::{Mutex, Once};
|
|
|
|
|
|
|
|
|
|
|
|
static PREPARED: Once = Once::new();
|
|
|
|
|
|
static USABLE: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
|
static NEXT: AtomicU64 = AtomicU64::new(1);
|
|
|
|
|
|
static CLICKED: Mutex<Vec<u64>> = Mutex::new(Vec::new());
|
|
|
|
|
|
|
|
|
|
|
|
define_class!(
|
|
|
|
|
|
// Shows AnyBot's banners while AnyBot is in front too (they're only sent when the person
|
|
|
|
|
|
// is away, and idle in front of it counts), and hears which banner was clicked.
|
|
|
|
|
|
#[unsafe(super(NSObject))]
|
|
|
|
|
|
#[name = "AnyBotBannerDelegate"]
|
|
|
|
|
|
struct Delegate;
|
|
|
|
|
|
|
|
|
|
|
|
unsafe impl NSObjectProtocol for Delegate {}
|
|
|
|
|
|
|
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
|
unsafe impl NSUserNotificationCenterDelegate for Delegate {
|
|
|
|
|
|
#[unsafe(method(userNotificationCenter:shouldPresentNotification:))]
|
|
|
|
|
|
fn should_present(&self, _center: &NSUserNotificationCenter, _n: &NSUserNotification) -> bool {
|
|
|
|
|
|
true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[unsafe(method(userNotificationCenter:didActivateNotification:))]
|
|
|
|
|
|
fn did_activate(&self, _center: &NSUserNotificationCenter, n: &NSUserNotification) {
|
|
|
|
|
|
if let Some(bot) = n.identifier().and_then(|id| super::bot_of_banner(&id.to_string())) {
|
|
|
|
|
|
if let Ok(mut c) = CLICKED.lock() {
|
|
|
|
|
|
c.push(bot);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
/// Inside a real .app with an identifier (the center is nil for a bare program), on a macOS
|
|
|
|
|
|
/// that still has the center.
|
|
|
|
|
|
fn in_bundle() -> bool {
|
|
|
|
|
|
let b = NSBundle::mainBundle();
|
|
|
|
|
|
AnyClass::get(c"NSUserNotificationCenter").is_some() && b.bundleIdentifier().is_some() && b.bundlePath().to_string().ends_with(".app")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn prepare() {
|
|
|
|
|
|
PREPARED.call_once(|| {
|
|
|
|
|
|
if !in_bundle() {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
USABLE.store(true, Ordering::SeqCst);
|
|
|
|
|
|
let delegate: Retained<Delegate> = unsafe { msg_send![Delegate::class(), new] };
|
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
|
unsafe {
|
|
|
|
|
|
NSUserNotificationCenter::defaultUserNotificationCenter().setDelegate(Some(ProtocolObject::from_ref(&*delegate)));
|
|
|
|
|
|
}
|
|
|
|
|
|
// The center keeps only a weak reference: the delegate lives as long as AnyBot.
|
|
|
|
|
|
std::mem::forget(delegate);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn usable() -> bool {
|
|
|
|
|
|
prepare();
|
|
|
|
|
|
USABLE.load(Ordering::SeqCst)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Show the banner. False: not inside AnyBot.app (the caller uses display notification).
|
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
|
pub fn post(title: &str, body: &str, sound: bool, bot: Option<u64>) -> bool {
|
|
|
|
|
|
if !usable() {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
let n = NSUserNotification::new();
|
|
|
|
|
|
n.setTitle(Some(&NSString::from_str(title)));
|
|
|
|
|
|
n.setInformativeText(Some(&NSString::from_str(body)));
|
|
|
|
|
|
n.setIdentifier(Some(&NSString::from_str(&super::banner_id(bot, NEXT.fetch_add(1, Ordering::SeqCst)))));
|
|
|
|
|
|
if sound {
|
|
|
|
|
|
n.setSoundName(Some(&NSString::from_str("Glass")));
|
|
|
|
|
|
}
|
|
|
|
|
|
NSUserNotificationCenter::defaultUserNotificationCenter().deliverNotification(&n);
|
|
|
|
|
|
true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// How many of AnyBot's banners the system is keeping (`anybot --banner`'s check).
|
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
|
pub fn delivered() -> usize {
|
|
|
|
|
|
if !usable() {
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
NSUserNotificationCenter::defaultUserNotificationCenter().deliveredNotifications().count()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn take_clicked() -> Vec<u64> {
|
|
|
|
|
|
CLICKED.lock().map(|mut c| std::mem::take(&mut *c)).unwrap_or_default()
|
|
|
|
|
|
}
|
AnyBot: first commit of the working desktop app
Rust/egui rebuild of Grok Bot as of 2026-11-04: chat with streaming
providers (xAI, OpenAI, Claude, Gemini, OpenRouter, OpenCode Go, Ollama,
custom), tools and approvals, the bot's Docker computer, computer use on
macOS / Linux X11 / Wayland, AnyBot's browser with a full DevTools-based
toolset, the encrypted vault, pinned grid and sidebar sections, Markdown
with Mermaid diagrams, phone access through the relay, and the Linux
build and test scripts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha3oEtWeHh58zA7ErHa
2026-11-04 08:51:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
|
mod tests {
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn presence_rule() {
|
|
|
|
|
|
assert!(!should_notify(true, Some(5.0)));
|
|
|
|
|
|
assert!(should_notify(true, Some(90.0)));
|
|
|
|
|
|
assert!(should_notify(false, Some(1.0)));
|
|
|
|
|
|
assert!(!should_notify(true, None));
|
|
|
|
|
|
let sample = " | | \"HIDIdleTime\" = 123000000000\n";
|
|
|
|
|
|
assert_eq!(parse_hid_idle(sample), Some(123.0));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn update_applies_only_when_truly_away() {
|
|
|
|
|
|
assert!(should_apply_update_now(true, true, true, false, Some(600.0), true));
|
|
|
|
|
|
assert!(should_apply_update_now(true, true, false, true, Some(900.0), true));
|
|
|
|
|
|
assert!(!should_apply_update_now(true, true, false, false, Some(900.0), true), "screen in use");
|
|
|
|
|
|
assert!(!should_apply_update_now(true, true, true, false, Some(599.0), true), "not idle long enough");
|
|
|
|
|
|
assert!(!should_apply_update_now(true, true, true, false, Some(900.0), false), "a bot is working");
|
|
|
|
|
|
assert!(!should_apply_update_now(false, true, true, false, Some(900.0), true), "turned off");
|
|
|
|
|
|
assert!(!should_apply_update_now(true, false, true, false, Some(900.0), true), "nothing to apply");
|
|
|
|
|
|
assert_eq!(next_update_check_secs(0.0), 3600);
|
|
|
|
|
|
assert_eq!(next_update_check_secs(0.999), 3899);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn banner_text_is_quoted() {
|
|
|
|
|
|
let s = banner_script("電腦助手 需要你允許", "執行 \"rm\" \\ 指令\n下一行");
|
|
|
|
|
|
assert_eq!(s, "display notification \"執行 \\\"rm\\\" \\\\ 指令下一行\" with title \"電腦助手 需要你允許\" sound name \"Glass\"");
|
|
|
|
|
|
}
|
提示音: a chime whenever a bot needs the person
A request to allow something, or a step only the person can do, only
bounced the Dock icon; a banner (with macOS's Glass sound) came only
when they were away, and on Linux notify-send made no sound at all. So
someone looking at another window on the same screen, or at AnyBot
itself without watching it, missed that a bot was waiting.
Now on_new_approval rings a chime every time, focused or not: two soft
bell notes going up (E6, then A6), 0.8 s, quiet, synthesized
(voice::chime_samples) so it is the same on macOS, Windows and Linux.
Several requests at once ring once; the banner that comes with it when
away stays silent so it doesn't ring twice (the 「完成了」 banner keeps
its sound). A screen of another brain rings for the brain's requests
too (they come through the same on_new_approval); a brain on a server
doesn't. 設定 › 一般 › 通知 › 「需要你時發出提示音」 turns it off (on
by default; turning it on plays it once).
2026-11-10 17:01:05 +00:00
|
|
|
|
|
2026-11-15 16:06:12 +00:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn a_banner_click_finds_its_bot() {
|
|
|
|
|
|
assert_eq!(bot_of_banner(&banner_id(Some(42), 7)), Some(42));
|
|
|
|
|
|
assert_eq!(bot_of_banner(&banner_id(None, 7)), None);
|
|
|
|
|
|
assert_eq!(bot_of_banner("something-else"), None);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
提示音: a chime whenever a bot needs the person
A request to allow something, or a step only the person can do, only
bounced the Dock icon; a banner (with macOS's Glass sound) came only
when they were away, and on Linux notify-send made no sound at all. So
someone looking at another window on the same screen, or at AnyBot
itself without watching it, missed that a bot was waiting.
Now on_new_approval rings a chime every time, focused or not: two soft
bell notes going up (E6, then A6), 0.8 s, quiet, synthesized
(voice::chime_samples) so it is the same on macOS, Windows and Linux.
Several requests at once ring once; the banner that comes with it when
away stays silent so it doesn't ring twice (the 「完成了」 banner keeps
its sound). A screen of another brain rings for the brain's requests
too (they come through the same on_new_approval); a brain on a server
doesn't. 設定 › 一般 › 通知 › 「需要你時發出提示音」 turns it off (on
by default; turning it on plays it once).
2026-11-10 17:01:05 +00:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn a_banner_can_be_silent() {
|
|
|
|
|
|
// (When AnyBot plays its own 提示音, the banner doesn't add a second sound.)
|
|
|
|
|
|
assert_eq!(banner_script_with("標題", "內文", false), "display notification \"內文\" with title \"標題\"");
|
|
|
|
|
|
assert!(banner_script_with("標題", "內文", true).ends_with("sound name \"Glass\""));
|
|
|
|
|
|
}
|
AnyBot: first commit of the working desktop app
Rust/egui rebuild of Grok Bot as of 2026-11-04: chat with streaming
providers (xAI, OpenAI, Claude, Gemini, OpenRouter, OpenCode Go, Ollama,
custom), tools and approvals, the bot's Docker computer, computer use on
macOS / Linux X11 / Wayland, AnyBot's browser with a full DevTools-based
toolset, the encrypted vault, pinned grid and sidebar sections, Markdown
with Mermaid diagrams, phone access through the relay, and the Linux
build and test scripts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha3oEtWeHh58zA7ErHa
2026-11-04 08:51:51 +00:00
|
|
|
|
}
|