From 1af25d7da4319ad38263b23811377fc800a8dfe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=A7=E9=A9=8A?= Date: Wed, 11 Nov 2026 01:01:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E7=A4=BA=E9=9F=B3:=20a=20chime=20when?= =?UTF-8?q?ever=20a=20bot=20needs=20the=20person?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- FEATURES.md | 1 + src/app.rs | 24 +++++++++++++++-- src/i18n_table.rs | 2 ++ src/model.rs | 5 ++++ src/notify.rs | 26 ++++++++++++++++-- src/voice.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 121 insertions(+), 4 deletions(-) diff --git a/FEATURES.md b/FEATURES.md index b9c1288..0d8fed8 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -346,6 +346,7 @@ AnyBot 是一個桌面聊天 App,介面和運作方式照 Grok Bot 重做。 - 「前往聊天」:只有請求來自別的 Bot 時才有,按下會切過去。 - 「稍後再決定」:提示「先關掉這個視窗;聊天裡的卡片還在,Bot 會一直等你」。 - 有新請求時,Dock 圖示會跳動。你不在 App 前面而且通知有開時,還會跳 macOS 通知,標題「(Bot) 需要你允許」。 +- 有新請求時(要你允許某件事,或要你親自處理一個步驟)會響一聲提示音,就算你正在看 AnyBot 也會響:上行的兩聲「叮—咚」,約 0.8 秒、音量不大,Mac、Windows、Linux 都一樣(程式自己合成,不用音檔)。好幾個請求同時進來只響一次;跳 macOS 通知時,通知本身就不再另外響。連到別台大腦的電腦也會響(請求來自大腦時一樣);伺服器上的大腦不響。在「設定 › 一般 › 通知 › 需要你時發出提示音」可以關掉,打開時會先響一次讓你聽。 --- diff --git a/src/app.rs b/src/app.rs index d9b296f..daf88cf 100644 --- a/src/app.rs +++ b/src/app.rs @@ -445,6 +445,10 @@ impl App { app.settings_tab = SettingsTab::Providers; app.settings_provider = ProviderKind::Anthropic; } + Ok("general") => { + app.show_settings = true; + app.settings_tab = SettingsTab::General; + } // 設定 › 語音, set up (voicesettings) or not (voiceoff), and a chat with voice on (voicechat). Ok(v @ ("voicesettings" | "voiceoff" | "voicechat")) => { if v != "voiceoff" { @@ -2523,9 +2527,14 @@ impl App { } /// A new request: show it as a pop-up (even over another bot's chat) and get the person's - /// attention (the Dock icon bounces when AnyBot isn't in front). + /// attention (the Dock icon bounces when AnyBot isn't in front, and the 提示音 rings). fn on_new_approval(&mut self, ctx: &egui::Context, bot_id: u64) { self.popup_hidden.remove(&bot_id); + // (A brain on a server has nobody to hear it.) + let chime = !self.demo && !self.headless && self.state.alert_sound; + if chime { + crate::voice::chime(); + } if !self.demo { ctx.send_viewport_cmd(egui::ViewportCommand::RequestUserAttention(egui::UserAttentionType::Critical)); // Away from AnyBot (other app in front, or idle 90 s+): also a macOS banner. @@ -2535,7 +2544,8 @@ impl App { // 「{callee} 需要你」 for a step only the person can do. let title = if a.kind == ApprovalKind::Handoff { format!("{}:需要你", b.name) } else { format!("{} 需要你允許", b.name) }; if !self.headless { - notify::banner(&title, &what); + // The banner doesn't ring a second time when the 提示音 just did. + notify::banner_with(&title, &what, !chime); } crate::remote::notify(&title, &what, bot_id); } @@ -3431,6 +3441,16 @@ impl App { } }); hint(ui, "視窗不在最前面,或 90 秒沒有動鍵盤滑鼠,就會通知。登入帳號後也會推播到手機。", p); + setting_row(ui, p, "需要你時發出提示音", "Bot 要你允許某件事、或要你親自處理一個步驟時,響一聲(就算你正在看 AnyBot)", |ui| { + let before = self.state.alert_sound; + if ui.add(egui::Checkbox::without_text(&mut self.state.alert_sound)).changed() { + self.dirty = true; + // Turned on: let the person hear what it sounds like. + if !before { + crate::voice::chime(); + } + } + }); section(ui, "Bot", p); setting_row(ui, p, "時區", "設定 Bot 用於排程和提醒的時區", |ui| { if ui.add(egui::TextEdit::singleline(&mut self.state.timezone).desired_width(150.0)).changed() { diff --git a/src/i18n_table.rs b/src/i18n_table.rs index e989986..9ee823e 100644 --- a/src/i18n_table.rs +++ b/src/i18n_table.rs @@ -83,6 +83,8 @@ pub const ROWS: &[(&str, &str, &str)] = &[ ("有更新的版本:", "A newer version is available: ", "新しいバージョンがあります:"), ("已是最新版本", "Up to date", "最新です"), ("回到最新訊息", "Jump to latest", "最新のメッセージへ"), + ("需要你時發出提示音", "Chime when a bot needs you", "あなたが必要なときに通知音"), + ("Bot 要你允許某件事、或要你親自處理一個步驟時,響一聲(就算你正在看 AnyBot)", "A chime when a bot asks you to allow something or to do a step yourself (even while you're looking at AnyBot)", "Bot が許可を求めたり、あなた自身の操作が必要なときに音で知らせます(AnyBot を見ているときも)"), ("聽", "Listen", "聞く"), ("說", "Speak", "話す"), ("服務", "Service", "サービス"), diff --git a/src/model.rs b/src/model.rs index 969b5af..a908e21 100644 --- a/src/model.rs +++ b/src/model.rs @@ -447,6 +447,10 @@ pub struct State { /// 「Bot 完成任務或等你處理時通知你」(ttdOeI): banners when away. #[serde(default = "yes")] pub notify: bool, + /// 「需要你時發出提示音」: a chime whenever a bot waits for the person (to allow something, or + /// a step only they can do), even while they're looking at AnyBot. + #[serde(default = "yes")] + pub alert_sound: bool, /// 直接操作: one switch like the reference assistant's local-execution + auto-review; only safety checks and /// "always ask" rules stop the bot. #[serde(default)] @@ -824,6 +828,7 @@ impl State { timezone: crate::routines::local_timezone(), next_routine_id: 1, notify: true, + alert_sound: true, direct_mode: false, auto_update_when_away: true, channels: vec![], diff --git a/src/notify.rs b/src/notify.rs index 05129cc..c09285e 100644 --- a/src/notify.rs +++ b/src/notify.rs @@ -103,21 +103,36 @@ fn applescript_quote(s: &str) -> String { /// The AppleScript line for a banner (title + message), with text safely quoted. pub fn banner_script(title: &str, body: &str) -> String { + 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 { let body: String = body.chars().take(180).collect(); - format!("display notification {} with title {} sound name \"Glass\"", applescript_quote(&body), applescript_quote(title)) + let line = format!("display notification {} with title {}", applescript_quote(&body), applescript_quote(title)); + if sound { + format!("{line} sound name \"Glass\"") + } else { + line + } } /// Show a macOS banner. AnyBot isn't a signed .app bundle when run from a build folder, so it can't /// ask the notification center itself (the original's module needs a bundle); macOS's own /// `display notification` is used instead. Doesn't wait. pub fn banner(title: &str, body: &str) { + banner_with(title, body, true) +} + +/// A banner, with or without the system's sound. +pub fn banner_with(title: &str, body: &str, sound: bool) { if !cfg!(target_os = "macos") { // The desktop's notification service (GNOME, KDE, XFCE…) through notify-send. let body: String = body.chars().take(180).collect(); let _ = crate::computer::spawn_reaped(Command::new("notify-send").args(["-a", "AnyBot", title, &body]).stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null())); return; } - let _ = crate::computer::spawn_reaped(Command::new("osascript").arg("-e").arg(banner_script(title, body)).stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null())); + 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())); } #[cfg(test)] @@ -152,4 +167,11 @@ mod tests { let s = banner_script("電腦助手 需要你允許", "執行 \"rm\" \\ 指令\n下一行"); assert_eq!(s, "display notification \"執行 \\\"rm\\\" \\\\ 指令下一行\" with title \"電腦助手 需要你允許\" sound name \"Glass\""); } + + #[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\"")); + } } diff --git a/src/voice.rs b/src/voice.rs index caa81ec..8a1ef1f 100644 --- a/src/voice.rs +++ b/src/voice.rs @@ -249,6 +249,52 @@ fn play(mp3: Vec, gen: u64) -> Result<(), String> { Ok(()) } +// ---------- 提示音 ---------- + +/// 提示音: two soft bell notes going up (E6, then A6 0.16 s later), about 0.8 s, quiet. Made +/// here instead of shipped as a file, so it sounds the same on macOS, Windows and Linux. +pub fn chime_samples(rate: u32) -> Vec { + let r = rate as f32; + let len = (r * 0.8) as usize; + let mut out = vec![0.0f32; len]; + let tau = std::f32::consts::TAU; + for (freq, start) in [(1318.51f32, 0.0f32), (1760.0, 0.16)] { + let s0 = (start * r) as usize; + for (i, o) in out.iter_mut().enumerate().skip(s0) { + let t = (i - s0) as f32 / r; + // 4 ms to full, then a bell's fall; a softer octave above gives it its ring. + let env = (t / 0.004).min(1.0) * (-t * 7.0).exp(); + *o += 0.16 * env * ((tau * freq * t).sin() + 0.3 * (-t * 5.0).exp() * (tau * 2.0 * freq * t).sin()); + } + } + // The last 20 ms down to nothing (no click when the sound stops). + let tail = (r * 0.02) as usize; + for k in 0..tail { + out[len - 1 - k] *= k as f32 / tail as f32; + } + out +} + +/// Ring the 提示音 once, without waiting. Several requests at once ring once (2 s apart at most). +pub fn chime() { + static LAST: std::sync::Mutex> = std::sync::Mutex::new(None); + { + let mut last = LAST.lock().unwrap(); + if last.is_some_and(|t| t.elapsed() < Duration::from_secs(2)) { + return; + } + *last = Some(Instant::now()); + } + std::thread::spawn(|| { + let Ok(mut sink) = rodio::DeviceSinkBuilder::open_default_sink() else { return }; + sink.log_on_drop(false); + const RATE: u32 = 44_100; + let player = rodio::Player::connect_new(sink.mixer()); + player.append(rodio::buffer::SamplesBuffer::new(std::num::NonZero::new(1).unwrap(), std::num::NonZero::new(RATE).unwrap(), chime_samples(RATE))); + player.sleep_until_end(); + }); +} + pub fn stop_speaking() { use std::sync::atomic::Ordering::SeqCst; PLAY_GEN.fetch_add(1, SeqCst); @@ -287,4 +333,25 @@ mod tests { assert!(!speaking() && quiet.lock().unwrap().is_none()); assert_eq!(length_words(7_200), "2 小時"); } + + /// The 提示音 as a file to listen to: ANYBOT_TEST_OUT=chime.wav cargo test --bin anybot chime_to_file -- --ignored + #[test] + #[ignore] + fn chime_to_file() { + std::fs::write(std::env::var("ANYBOT_TEST_OUT").unwrap(), crate::mic::wav(&chime_samples(44_100), 44_100)).unwrap(); + } + + #[test] + fn the_chime_is_short_quiet_and_ends_in_silence() { + let rate = 44_100; + let s = chime_samples(rate); + let secs = s.len() as f32 / rate as f32; + assert!((0.6..=1.0).contains(&secs), "{secs} s"); + let peak = s.iter().fold(0.0f32, |m, x| m.max(x.abs())); + assert!(peak > 0.1 && peak < 0.5, "peak {peak}"); + // No click at the end: the last 10 ms are next to nothing. + assert!(s[s.len() - rate as usize / 100..].iter().all(|x| x.abs() < 0.005)); + // Two notes: the second starts later and higher, so the start is the first note alone. + assert!(s[..rate as usize / 10].iter().any(|x| x.abs() > 0.05)); + } }