diff --git a/docs/feature-map/safety.md b/docs/feature-map/safety.md index d9b7000..379fc1f 100644 --- a/docs/feature-map/safety.md +++ b/docs/feature-map/safety.md @@ -4,7 +4,7 @@ Bot 要動到你的電腦、對外送出或做收不回來的事時,先問你 ## 子功能 -- 允許請求:黃色卡片停在輸入框上方,「允許 {Bot} {動作}嗎?」加細節框。按鈕:「允許」、「一律允許{類別}」(有能記住的類別才有)、「拒絕」。電腦工具沒開、或「在這台電腦上執行」沒開時,一律允許的按鈕就是「一律允許」,會打開什麼寫在滑過的說明裡。輸入框空著時 Enter 允許、Esc 拒絕——有任何對話框開著(設定、Bot 設定、記憶、檔案或頁面…)、或游標在別的輸入框時不算,那裡的 Enter/Esc 不會替你回答卡片(Esc 照樣關對話框)。 +- 允許請求:黃色卡片停在輸入框上方,「允許 {Bot} {動作}嗎?」加細節框。按鈕:「允許」、「一律允許{類別}」(有能記住的類別才有)、「拒絕」。電腦工具沒開、或「在這台電腦上執行」沒開時,一律允許的按鈕就是「一律允許」,會打開什麼寫在滑過的說明裡。輸入框空著時 Enter 允許、Esc 拒絕——有任何對話框或選單開著(設定、Bot 設定、記憶、檔案或頁面、右鍵選單…)、或游標在別的輸入框時不算,那裡的 Enter/Esc 不會替你回答卡片(Esc 照樣關對話框和選單);這些都沒了之後要再等一下(約 0.6 秒)卡片才接按鍵。 - 允許卡片:一句「它想在你的電腦上執行:」+指令,三個按鈕「允許」「一律允許」(滑過說明它會打開什麼)「拒絕」;下面一行灰字「之後都直接操作,不再一個個問」:允許這一筆並打開直接操作(還沒開、管理員沒鎖時才有)。等你的卡片(允許、問題、交給你處理)都是同一種黃:淡黃底、細黃框。 - 一律允許記在那個 Bot 身上(例如「修改檔案」、「npm install」、某個連接器),Bot 設定 ›「電腦操作」的「已設定「一律允許」」看得到,按 × 移除。 - 拒絕後停下:同一輪裡 Bot 再提一模一樣的操作(同一個工具、同樣的參數),直接替你拒絕、不再問,直到你再傳一則訊息(工作中插話也算)。按停止、關視窗不算拒絕。 diff --git a/src/app.rs b/src/app.rs index c03730a..67cd361 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3984,12 +3984,22 @@ impl App { // with the caret in another field (a connector's name, a search). // (Where the caret was as this frame began: a field that takes the Enter first lets go // of the caret before this card looks, so the last frame's is what counts.) + // A menu too: one closed by this very Esc is already marked closed (egui doesn't use the key + // up), so whether one was open as the frame began counts as well — that Esc closed a menu, + // it didn't say no. let composer = egui::Id::unique(("composer", bot.id)); let seen = egui::Id::unique("approval-dock-caret"); - let before: Option = ctx.data(|d| d.get_temp(seen)).flatten(); - let elsewhere = before.or_else(|| ctx.memory(|m| m.focused())).is_some_and(|f| f != composer); + let (caret_before, menu_before): (Option, bool) = ctx.data(|d| d.get_temp(seen)).unwrap_or((None, false)); + let elsewhere = caret_before.or_else(|| ctx.memory(|m| m.focused())).is_some_and(|f| f != composer); let dialog_open = self.show_settings || ctx.memory(|m| m.top_modal_layer()).is_some(); - if answer.is_none() && draft_empty && !typing && !elsewhere && !dialog_open && shown > 0.6 && !egui::Popup::is_any_open(ctx) { + let menu_now = egui::Popup::is_any_open(ctx); + let blocked = typing || elsewhere || dialog_open || menu_now || menu_before; + // While anything else has the keys, the wait starts over: a card that comes into view + // (a dialog closed, a field let go) takes keys only after it has been there a moment. + if blocked { + self.ask_shown.insert(bot.id, Instant::now()); + } + if answer.is_none() && draft_empty && !blocked && shown > 0.6 { let (pressed, _) = ui.input_mut(chat_ui::take_enter); if enter || pressed { answer = Some(ApprovalAnswer::Once); @@ -3998,7 +4008,7 @@ impl App { } } // (Read, then kept: one lock at a time — a ctx call inside another freezes the window.) - let now = ctx.memory(|m| m.focused()); + let now = (ctx.memory(|m| m.focused()), egui::Popup::is_any_open(ctx)); ctx.data_mut(|d| d.insert_temp(seen, now)); if let Some(ans) = answer { self.ask_shown.remove(&bot.id); @@ -7933,6 +7943,42 @@ mod tests { assert!(got.try_recv().is_err(), "keys in another dialog don't answer the card"); } + /// Esc that closes a menu closes the menu: it isn't a no to the step waiting in the chat. + /// And a card that comes into view (a field let go) takes keys only after a moment. + #[test] + fn esc_closing_a_menu_does_not_turn_down_the_waiting_step() { + let ctx = egui::Context::default(); + let mut app = super::App::new(&ctx, Some(std::env::temp_dir().join("anybot-esc-menu.png"))); + let id = app.state.bots[0].id; + let (reply, got) = std::sync::mpsc::channel(); + app.approvals.insert(id, Approval { call_id: "c1".into(), tool: "shell".into(), reason: "執行:ls".into(), grant: String::new(), reply, kind: ApprovalKind::Action }); + app.ask_shown.insert(id, Instant::now() - std::time::Duration::from_secs(2)); + let bot = app.state.bot(id).unwrap().clone(); + let esc = egui::Event::Key { key: Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE }; + let menu = egui::Id::unique("a-menu"); + // A menu drawn before the chat's input, open; then Esc (which closes it first). + let frame = |app: &mut super::App, events: Vec, open: bool| { + let mut out = ctx.run_ui(egui::RawInput { events, ..Default::default() }, |ui| { + let r = ui.button("⋯"); + if open { + egui::Popup::open_id(ui.ctx(), menu); + } + egui::Popup::menu(&r).id(menu).show(|ui| ui.label("釘選")); + app.composer(ui, &ctx, &bot); + }); + out.textures_delta.clear(); + }; + frame(&mut app, vec![], true); + frame(&mut app, vec![], false); + // (Even with the card up long enough by the clock: the menu's own Esc is the menu's.) + app.ask_shown.insert(id, Instant::now() - std::time::Duration::from_secs(2)); + frame(&mut app, vec![esc.clone()], false); + assert!(got.try_recv().is_err(), "the Esc closed the menu, it didn't say no"); + // Right after (the menu just gone): still a moment before the card takes keys. + frame(&mut app, vec![esc], false); + assert!(got.try_recv().is_err(), "not at once after the menu"); + } + /// Enter in an empty box with a step waiting for a yes is the yes. (The box took the Enter /// first, sent nothing — it was empty — and the waiting step never heard it.) #[test]