AnyBot/src/always.rs

95 lines
4.7 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! 一律放行 (/always-approve, also /yolo): one switch for "don't ask me". Typed by the person, it
//! turns on 直接操作 (this computer, every bot's tools) and from then on answers every request
//! that would wait for 允許 by itself — the ones 直接操作 still asked about too (the 「一律詢問」
//! rules, a click on 付款/傳送/刪除, sending mail or messages, calls, texts, posts on X, a card
//! from the vault). Like Claude Code's bypassPermissions or Codex's --yolo.
//!
//! What it never changes: what the safety rules refuse outright (sudo, rm -rf /, the private key
//! files, AnyBot's own data, cloud metadata addresses) — those are refusals, not questions; a
//! question card or 需要處理 (they need the person's words or hands, not a yes); and a turn
//! started by someone else's message (a channel, a teammate): theirs still waits for the person.
//! Only the person turns it on: the command is read from what they type (the window, a screen of
//! the brain, the phone as the owner), never from a bot, a routine or a channel.
use crate::providers::{ApprovalAnswer, ApprovalKind};
/// What 「/always-approve …」 asks for.
#[derive(Debug, PartialEq)]
pub enum Command {
On,
Off,
Status,
}
/// 「/always-approve」 (or 「/yolo」), with 「off」 or 「status」; None for anything else.
pub fn command(text: &str) -> Option<Command> {
let t = text.trim();
let rest = t.strip_prefix("/always-approve").or_else(|| t.strip_prefix("/yolo"))?;
if rest.chars().next().is_some_and(|c| !c.is_whitespace()) {
return None;
}
Some(match rest.trim().to_lowercase().as_str() {
"" | "on" | "開" | "打開" | "開啟" => Command::On,
"off" | "stop" | "關" | "關閉" | "關掉" | "取消" | "停止" => Command::Off,
_ => Command::Status,
})
}
/// The answer it gives in the person's place (None: it still waits for them).
pub fn answer_for(kind: ApprovalKind) -> Option<ApprovalAnswer> {
match kind {
ApprovalKind::Action | ApprovalKind::EnableTools | ApprovalKind::EnableThisComputer => Some(ApprovalAnswer::Once),
ApprovalKind::Handoff | ApprovalKind::Question => None,
}
}
pub const ON_NOTE: &str = "已一律放行:所有 Bot 做事都不再一個個問你(例行任務也一樣),連「一律詢問」規則、付款/傳送/刪除的點擊、寄信、傳訊息、打電話、發文都直接做。\n仍然會擋:安全規則一律拒絕的(sudo、rm -rf /、金鑰和密碼檔、AnyBot 自己的資料);別人傳來的訊息(訊息管道、團隊成員)照樣等你按;問題卡和「需要處理」照樣等你。\n關掉:/always-approve off,或 設定 › 權限。";
pub const LOCKED_NOTE: &str = "沒辦法一律放行:「直接操作」或「在這台電腦上執行」被你的管理員鎖住了。";
/// What 「/always-approve off」 says (whether 直接操作 stayed on: it was on before).
pub fn off_note(direct_stays: bool) -> String {
if direct_stays {
"已關閉一律放行:要核准的事又會問你。「直接操作」還開著(在這之前就開了;設定 › 權限 可以關)。".into()
} else {
"已關閉一律放行:要核准的事又會問你,「直接操作」也關了。".into()
}
}
pub fn status_note(on: bool) -> String {
if on {
"現在是一律放行:Bot 做事不會問你(安全規則擋的、別人傳來的訊息除外)。關掉用 /always-approve off。".into()
} else {
"現在沒有一律放行:要核准的事會問你。打開用 /always-approve。".into()
}
}
/// The line above the input while it is on.
pub const BAR: &str = "一律放行 · Bot 做事不再問你(安全規則擋的照樣擋)";
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_command_and_its_words() {
assert_eq!(command("/always-approve"), Some(Command::On));
assert_eq!(command(" /yolo "), Some(Command::On));
assert_eq!(command("/always-approve off"), Some(Command::Off));
assert_eq!(command("/yolo 關閉"), Some(Command::Off));
assert_eq!(command("/always-approve status"), Some(Command::Status));
assert_eq!(command("/always-approver"), None);
assert_eq!(command("/yolox"), None);
assert_eq!(command("請 /yolo"), None);
}
#[test]
fn it_answers_requests_but_not_questions_or_handoffs() {
assert_eq!(answer_for(ApprovalKind::Action), Some(ApprovalAnswer::Once));
assert_eq!(answer_for(ApprovalKind::EnableTools), Some(ApprovalAnswer::Once));
assert_eq!(answer_for(ApprovalKind::EnableThisComputer), Some(ApprovalAnswer::Once));
assert_eq!(answer_for(ApprovalKind::Question), None);
assert_eq!(answer_for(ApprovalKind::Handoff), None);
}
}