#![recursion_limit = "512"] // the one `computer` tool's schema is one deep json! literal mod agent; mod followup; mod shared; mod ask; mod app; mod appctl; mod attach; mod axread; mod axwrite; mod avatar; mod bidi; mod botbrowser; mod browser; mod cloudbox; mod channels; mod checkpoint; mod computer; mod desktop; mod diagram; mod platform; mod mic; mod pages; #[cfg(target_os = "linux")] mod x11; #[cfg(target_os = "linux")] mod wl; #[cfg(target_os = "linux")] mod portal; #[cfg(target_os = "linux")] mod desk; mod egress; mod facts; mod find; mod httpd; mod intents; mod i18n; mod i18n_table; mod icons; mod landlock; mod lantls; mod lock; mod macapps; mod marks; mod mcp; mod mcp_oauth; mod chatgpt; mod memory; mod memstore; mod mention; mod pty; mod m2v; mod model; mod notify; mod oplog; mod phone; mod policy; mod preview; mod profile; mod providers; mod publish; mod remote; mod routines; mod setup; mod skills; mod teach; mod share; mod speech; mod teams; mod theme; mod tools; mod triggers; mod embed; mod evidence; mod featuremap; mod garden; mod goal; mod plan; mod always; mod smart; mod secrets; mod logfile; mod net; mod doctor; mod outputs; mod gate; mod hooks; mod usage; #[cfg(test)] mod testkit; mod store; mod docindex; mod drops; mod vault; mod vault_store; mod version; mod vnc; mod voice; #[cfg(target_os = "windows")] mod win; mod tunnel; mod web; mod wire; use eframe::egui; use std::path::PathBuf; /// PingFang ships as a downloadable system asset on macOS; its folder name varies per Mac. fn pingfang_path() -> Option { let root = std::path::Path::new("/System/Library/AssetsV2"); for set in std::fs::read_dir(root).ok()?.flatten() { if !set.file_name().to_string_lossy().starts_with("com_apple_MobileAsset_Font") { continue; } for asset in std::fs::read_dir(set.path()).into_iter().flatten().flatten() { let p = asset.path().join("AssetData/PingFang.ttc"); if p.exists() { return Some(p); } } } None } /// PingFang TC Regular inside PingFang.ttc. const PINGFANG_TC_INDEX: u32 = 2; /// The window's first size (screenshot mode can ask for another, e.g. ANYBOT_SHOT_SIZE=760x560). fn shot_size() -> [f32; 2] { std::env::var("ANYBOT_SHOT_SIZE").ok().and_then(|v| { let (w, h) = v.split_once('x')?; Some([w.parse().ok()?, h.parse().ok()?]) }).unwrap_or([1180.0, 780.0]) } /// 思源黑體 (Noto Sans TC Regular, SIL OFL 1.1, fonts/OFL.txt): the interface's one font — /// plain, clear, the same on every computer. pub const NOTO_TC: &[u8] = include_bytes!("../fonts/NotoSansTC-Regular.otf"); /// 思源黑體's bold weight, for titles, bold words and the main button of a choice. pub const NOTO_TC_BOLD: &[u8] = include_bytes!("../fonts/NotoSansTC-Bold.otf"); /// 思源宋體 (Noto Serif TC Regular, SIL OFL 1.1): what is read at length — a bot's answer, a /// page's text — is set in a book face, the way Claude sets its answers. pub const NOTO_SERIF_TC: &[u8] = include_bytes!("../fonts/NotoSerifTC-Regular.otf"); /// 思源宋體's bold weight: headings and bold words in reading text. pub const NOTO_SERIF_TC_BOLD: &[u8] = include_bytes!("../fonts/NotoSerifTC-Bold.otf"); /// The family bold interface text is set in (`RichText::family(bold())`, `FontId::new(size, bold())`). pub fn bold() -> egui::FontFamily { egui::FontFamily::Name("bold".into()) } /// The reading family (思源宋體): answers and pages. pub fn read() -> egui::FontFamily { egui::FontFamily::Name("read".into()) } /// The reading family's bold (思源宋體 Bold): headings and bold words in answers and pages. pub fn read_bold() -> egui::FontFamily { egui::FontFamily::Name("read-bold".into()) } /// The fonts, one set for every computer: 思源黑體 for the interface, 思源宋體 for reading, the /// system's mono for code. The system's CJK font only fills in what the bundled ones lack /// (symbols, a rare character); egui's own fonts stay last for emoji. pub fn load_fonts(ctx: &egui::Context) { let mut fonts = egui::FontDefinitions::default(); // One font for Chinese *and* English: mixing egui's built-in Latin font with a Chinese // fallback put the two on different baselines and sizes ("Bot 設定" looked broken). let mut candidates: Vec<(PathBuf, u32)> = pingfang_path().map(|p| vec![(p, PINGFANG_TC_INDEX)]).unwrap_or_default(); candidates.extend( [ ("/System/Library/Fonts/Hiragino Sans GB.ttc", 0), ("/System/Library/Fonts/STHeiti Medium.ttc", 0), ("/System/Library/Fonts/Supplemental/Arial Unicode.ttf", 0), ("C:\\Windows\\Fonts\\msjh.ttc", 0), ("C:\\Windows\\Fonts\\msyh.ttc", 0), // Linux: Noto Sans CJK (face 3 = Traditional Chinese) on Debian/Ubuntu, Arch, Fedora; then WenQuanYi. ("/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc", 3), ("/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc", 3), ("/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc", 3), ("/usr/share/fonts/truetype/wqy/wqy-microhei.ttc", 0), ] .into_iter() .map(|(p, i)| (PathBuf::from(p), i)), ); // Code: the original's `--cursor-font-family-mono` list is ui-monospace, SFMono-Regular, Menlo… for path in ["/System/Library/Fonts/SFNSMono.ttf", "/System/Library/Fonts/Menlo.ttc"] { if let Ok(bytes) = std::fs::read(path) { fonts.font_data.insert("mono".into(), egui::FontData::from_owned(bytes).into()); fonts.families.entry(egui::FontFamily::Monospace).or_default().insert(0, "mono".into()); break; } } for (path, index) in candidates { if let Ok(bytes) = std::fs::read(&path) { let mut data = egui::FontData::from_owned(bytes); data.index = index; fonts.font_data.insert("cjk".into(), data.into()); fonts.families.entry(egui::FontFamily::Proportional).or_default().insert(0, "cjk".into()); // Code stays in the monospace font; Chinese inside code falls back to the same font. fonts.families.entry(egui::FontFamily::Monospace).or_default().push("cjk".into()); break; } } for (name, bytes) in [("noto", NOTO_TC), ("noto-bold", NOTO_TC_BOLD), ("serif", NOTO_SERIF_TC), ("serif-bold", NOTO_SERIF_TC_BOLD)] { fonts.font_data.insert(name.into(), egui::FontData::from_static(bytes).into()); } // 思源黑體 first everywhere it has the letter (code: after the mono font, before the system's). let base = { let p = fonts.families.entry(egui::FontFamily::Proportional).or_default(); p.insert(0, "noto".into()); p.clone() }; let mono = fonts.families.entry(egui::FontFamily::Monospace).or_default(); let at = mono.iter().position(|f| f == "cjk").unwrap_or(mono.len()); mono.insert(at, "noto".into()); // Each other family: its face in front of the same list (what it lacks comes from 思源黑體). let with = |first: &[&str]| first.iter().map(|f| f.to_string()).chain(base.iter().cloned()).collect::>(); fonts.families.insert(bold(), with(&["noto-bold"])); fonts.families.insert(read(), with(&["serif"])); fonts.families.insert(read_bold(), with(&["serif-bold", "noto-bold"])); ctx.set_fonts(fonts); } /// The window icon (Linux taskbars and window switchers; macOS uses the app bundle's). fn app_icon() -> egui::IconData { svg_icon(include_str!("../icons/anybot.svg"), 256) } /// AnyBot's icon (the cat on yellow) as straight RGBA, `px` square (notification banners). pub fn app_icon_rgba(px: u32) -> Vec { svg_icon(include_str!("../icons/anybot.svg"), px).rgba } /// An SVG drawn to a square RGBA picture of `px` pixels. fn svg_icon(svg: &str, px: u32) -> egui::IconData { let tree = resvg::usvg::Tree::from_str(svg, &resvg::usvg::Options::default()).expect("icon svg"); let mut pm = resvg::tiny_skia::Pixmap::new(px, px).expect("icon pixmap"); let s = px as f32 / tree.size().width(); resvg::render(&tree, resvg::tiny_skia::Transform::from_scale(s, s), &mut pm.as_mut()); // tiny-skia is premultiplied; IconData wants straight alpha. let mut rgba = pm.take(); for c in rgba.as_chunks_mut::<4>().0 { let a = c[3] as u32; if a > 0 && a < 255 { for v in &mut c[..3] { *v = ((*v as u32 * 255 + a / 2) / a).min(255) as u8; } } } egui::IconData { rgba, width: px, height: px } } /// `anybot --write-icon out.png [size] [file.svg]`: the app icon (or that SVG) as a PNG, drawn at /// that size (for the Mac bundle, Linux packages and the web icons). fn write_icon(out: &str, px: u32, svg: Option<&str>) -> Result<(), String> { let icon = match svg { Some(path) => svg_icon(&std::fs::read_to_string(path).map_err(|e| format!("{path}: {e}"))?, px), None => svg_icon(include_str!("../icons/anybot.svg"), px), }; let img = image::RgbaImage::from_raw(icon.width, icon.height, icon.rgba).ok_or("icon")?; img.save(out).map_err(|e| e.to_string()) } /// This data folder for this copy (`--serve`, `--ask`), or a clear word and exit when another /// AnyBot is using it. fn take_folder_or_exit() -> lock::DataLock { match lock::take(lock::WAIT) { Ok(l) => l, Err(msg) => { eprintln!("AnyBot:{msg}"); std::process::exit(1); } } } /// What a second window shows instead of opening: another AnyBot is using this data folder. fn busy_window(msg: String) -> eframe::Result<()> { eprintln!("AnyBot:{msg}"); let viewport = egui::ViewportBuilder::default().with_title("AnyBot").with_inner_size([460.0, 240.0]).with_resizable(false).with_app_id("anybot").with_icon(app_icon()); eframe::run_native( "AnyBot", eframe::NativeOptions { viewport, ..Default::default() }, Box::new(move |cc| { load_fonts(&cc.egui_ctx); Ok(Box::new(Busy(msg))) }), ) } struct Busy(String); impl eframe::App for Busy { fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) { egui::CentralPanel::default().show(ui, |ui| { ui.add_space(10.0); ui.label(egui::RichText::new("AnyBot 已經在執行").size(17.0).strong()); ui.add_space(8.0); ui.label(&self.0); ui.add_space(14.0); if ui.button("關閉").clicked() { ui.ctx().send_viewport_cmd(egui::ViewportCommand::Close); } }); } } const USAGE: &str = "用法: anybot 打開 AnyBot 視窗 anybot --serve 不開視窗,讓 Bot 繼續工作、接手機 anybot --ask 問題… 問一個 Bot 一句,印出這一輪(給人看) --json 一行一個 JSON(給腳本:message、approval、learned、done) --bot 名字 問哪個 Bot(預設主要 Bot) --data-dir 資料夾 用另一個資料夾(像 ANYBOT_DATA_DIR) --approve once 要核准的自動允許一次(預設自動拒絕) 結束碼:0 這一輪順利、1 失敗或時間到、130 按了 Ctrl-C、2 指令寫錯 anybot setup … 在沒有螢幕的機器上設定 anybot --install-skill 來源 安裝技能(GitHub 網址、owner/repo/path、SKILL.md 網址或本機資料夾) anybot --update-box 更新 Bot 的電腦(同 設定 › 電腦 › 更新;AnyBot 開著就請它做) anybot --screenshot 檔名 把視窗畫成一張圖 anybot --banner 標題 內文 送一則通知,看它怎麼送出(Mac:AnyBot.app 自己的通知) anybot --version 印出版本(v0.0.1-36bc2be · 建置時間) 資料夾:ANYBOT_DATA_DIR(沒設就用這個使用者的 AnyBot 資料夾)"; fn main() -> eframe::Result<()> { // Linux 的指令沙盒 (landlock.rs): AnyBot started as the sandbox around one command — nothing // of the app, and before reading the arguments as text (a path in them may not be). if std::env::args_os().nth(1).is_some_and(|a| a == landlock::ARG) { landlock::run_helper(&std::env::args_os().skip(2).collect::>()); } let args: Vec = std::env::args().collect(); // Before anything else: no data folder, no window. if matches!(args.get(1).map(String::as_str), Some("--version" | "-V")) { println!("AnyBot {}", version::full(version::exe_modified())); return Ok(()); } // `anybot --ask … --data-dir X`: that folder for everything (set before anything reads it). let ask = (args.get(1).map(String::as_str) == Some("--ask")).then(|| app::AskOpts::parse(&args[2..])); if let Some(Err(e)) = &ask { eprintln!("{e}"); std::process::exit(2); } if let Some(Ok(app::AskOpts { data_dir: Some(d), .. })) = &ask { std::env::set_var("ANYBOT_DATA_DIR", d); } // 紀錄檔與當掉報告 (logfile.rs): logs/ in the data folder, and a report if anything panics. logfile::init(); // 完整結果 older than a week go. outputs::prune(); // 檢查點 kept content no change has used for a week. checkpoint::prune(); // `anybot --update-box` (make update-box): 更新 Bot 的電腦 from a terminal, by this AnyBot — // or the one running on this data folder (cloudbox::update_box_cli). if args.get(1).map(String::as_str) == Some("--update-box") { std::process::exit(cloudbox::update_box_cli()); } // P7: install a skill package (a GitHub folder/URL, SKILL.md URL, or local folder/file). if let Some(i) = args.iter().position(|a| a == "--install-skill") { let src = args.get(i + 1).cloned().unwrap_or_default(); if src.is_empty() { eprintln!("用法:anybot --install-skill "); std::process::exit(2); } match crate::skills::install_from_source(&src) { Ok(name) => { let _folder = take_folder_or_exit(); let mut st = model::State::load(); crate::memory::merge_skill_files(&mut st.skills); st.save(); println!("已安裝技能「{name}」(在 設定 › 整合 › 技能 可看)"); } Err(e) => { eprintln!("{e}"); std::process::exit(1); } } return Ok(()); } let screenshot = args.iter().position(|a| a == "--screenshot").and_then(|i| args.get(i + 1)).map(PathBuf::from); // For tests that start AnyBots of their own: a whole settings file (the sample bots) to edit. if args.get(1).map(String::as_str) == Some("--sample-state") { let out = args.get(2).cloned().unwrap_or_else(|| "state.json".into()); let mut st = model::State::samples(); st.remote = Default::default(); if let Err(e) = std::fs::write(&out, serde_json::to_vec_pretty(&st).unwrap_or_default()) { eprintln!("{e}"); std::process::exit(1); } return Ok(()); } // `anybot --banner 標題 內文`: send one banner the way a bot's would (checks that AnyBot.app's // own notifications work: its icon, allowed in 系統設定 › 通知). if args.get(1).map(String::as_str) == Some("--banner") { notify::prepare(); println!("{}", notify::banner_now(args.get(2).map(String::as_str).unwrap_or("AnyBot"), args.get(3).map(String::as_str).unwrap_or("通知測試"))); return Ok(()); } if args.get(1).map(String::as_str) == Some("--write-icon") { let out = args.get(2).cloned().unwrap_or_else(|| "anybot.png".into()); let px = args.get(3).and_then(|v| v.parse().ok()).unwrap_or(256); if let Err(e) = write_icon(&out, px, args.get(4).map(String::as_str)) { eprintln!("{e}"); std::process::exit(1); } return Ok(()); } // For testing on a new system: `anybot --tool computer_use_screenshot '{}' computer_use_click // '{"ref":"7"}' …` runs computer tools in order (one process, so a picture's numbers carry // over), prints what the bot would hear, and saves the numbered pictures that come back as // anybot-tool-N.jpg in the temp folder. if args.get(1).map(String::as_str) == Some("--tool") { let mut all_ok = true; for (i, pair) in args[2..].chunks(2).enumerate() { let json = pair.get(1).cloned().unwrap_or_else(|| "{}".into()); let (text, ok, img) = computer::run(&pair[0], &json); println!("{} {}: {text}", pair[0], if ok { "ok" } else { "failed" }); if let Some(b64) = img { use base64::Engine; let out = std::env::temp_dir().join(format!("anybot-tool-{i}.jpg")); let _ = std::fs::write(&out, base64::engine::general_purpose::STANDARD.decode(b64).unwrap_or_default()); println!(" saved {}", out.display()); } all_ok &= ok; std::thread::sleep(std::time::Duration::from_millis(300)); } std::process::exit(if all_ok { 0 } else { 1 }); } // For trying agent-desktop on a new system: `anybot --desktop snapshot '{"app":"Finder"}'`. if args.get(1).map(String::as_str) == Some("--desktop") { let cmd = args.get(2).cloned().unwrap_or_else(|| "apps".into()); let v: serde_json::Value = serde_json::from_str(args.get(3).map(String::as_str).unwrap_or("{}")).unwrap_or_default(); match desktop::run(&cmd, &v) { Ok(out) => println!("{}", serde_json::to_string_pretty(&out).unwrap_or_default()), Err(e) => { eprintln!("{e}"); std::process::exit(1); } } std::process::exit(0); } // `anybot setup`: make this computer a brain, or hands for a brain (in a terminal). It takes // the data folder only while it writes the settings (see setup.rs). if args.get(1).map(String::as_str) == Some("setup") { std::process::exit(setup::run(&args[2..])); } // Everything below uses the data folder (the chats, the work log): one AnyBot at a time. if let Some(Ok(opts)) = ask { let ctx = egui::Context::default(); let _folder = take_folder_or_exit(); app::App::ask_cli(&ctx, opts); } // No window: keep the bots working and answer the phone (always-on Mac or server). if args.iter().any(|a| a == "--serve") { // The first line of the log says which build is running (make server-logs, journalctl). log::info!("AnyBot {}", version::full(version::exe_modified())); let ctx = egui::Context::default(); let _folder = take_folder_or_exit(); app::App::serve(&ctx); } // `--help` or a flag this build doesn't know: say what there is and stop. (It used to open the // window on the real data folder, next to the AnyBot already using it.) Single-dash arguments // are macOS's own (-psn_…, -NSDocumentRevisionsDebugMode) and are left alone. if let Some(bad) = args.iter().skip(1).find(|a| a.as_str() == "-h" || (a.starts_with("--") && !matches!(a.as_str(), "--screenshot" | "--serve"))) { if bad != "--help" && bad != "-h" { eprintln!("AnyBot:不認得「{bad}」。"); } eprintln!("{USAGE}"); std::process::exit(if bad == "--help" || bad == "-h" { 0 } else { 2 }); } // (A screenshot run is a tool next to the AnyBot that owns the folder; it doesn't take it.) let _folder = if screenshot.is_some() { None } else { match lock::take(lock::WAIT) { Ok(l) => Some(l), Err(msg) => return busy_window(msg), } }; let mut viewport = egui::ViewportBuilder::default() .with_title("AnyBot") .with_inner_size(shot_size()) .with_min_inner_size([app::SIDEBAR_MIN_WIDTH + app::CHAT_MIN_WIDTH, 480.0]) .with_app_id("anybot") .with_icon(app_icon()); // On a Mac the window is one surface to the very top (the grey system title bar didn't match // a dark window); the traffic lights sit over the sidebar's empty corner. if cfg!(target_os = "macos") { viewport = viewport.with_fullsize_content_view(true).with_titlebar_shown(false).with_title_shown(false); } let mut options = eframe::NativeOptions { viewport, ..Default::default() }; // The built-in graphics, not the separate card: egui-wgpu asks for HighPerformance, which on // a two-GPU Intel Mac keeps the discrete card awake (heat, fans, battery) for a chat window. // WGPU_POWER_PREF still wins when set. if std::env::var_os("WGPU_POWER_PREF").is_none() { if let eframe::egui_wgpu::WgpuSetup::CreateNew(c) = &mut options.wgpu_options.wgpu_setup { c.power_preference = eframe::wgpu::PowerPreference::LowPower; } } eframe::run_native( "AnyBot", options, Box::new(move |cc| { load_fonts(&cc.egui_ctx); Ok(Box::new(app::App::new(&cc.egui_ctx, screenshot))) }), ) }