diff --git a/docs/feature-map/browser.md b/docs/feature-map/browser.md index d195d02..93e16d1 100644 --- a/docs/feature-map/browser.md +++ b/docs/feature-map/browser.md @@ -41,7 +41,7 @@ Bot 用 AnyBot 自己的瀏覽器上網、登入、點按、打字、送出。 - 有些模型經閘道叫 `browser` 時參數送到是空的(`{}`;OpenCode Go · deepseek-v4.1-flash 一天 395 次,思考裡每次都有寫動作):AnyBot 說「參數是空的——不是你沒寫,是沒有送到」,再叫一次還是空的就改用單一動作的工具(`browser_open {url}`、`browser_click {ref|text}`…,AnyBot 照收),紀錄檔記一行「叫 browser 卻沒帶參數;串流裡這個呼叫的原文:…」。對話裡只要出現過一次空的 `browser`(或 `computer`),下一步起工具清單就不再給這個大工具,改給一個動作一個工具(`browser_open`、`browser_click`、`browser_tabs {action}`…,各自只帶自己的欄位;`fill` 和 `computer` 的 `batch` 不拆),紀錄檔記「模型送過空參數的 browser,這段對話改給單一動作的工具」(看的是模型讀得到的對話:那一步被收起進摘要後,下一輪又先給回大工具,再空一次才換)——那個模型在 jenny-Host 叫 `browser` 3719 次有 3452 次空的,叫單一動作的工具卻每次都帶得到參數。參數不是合法 JSON 時說「不是合法的 JSON」(字串裡的換行要寫 \n),不再說成沒給 action。 - 別人傳來的訊息那一輪不能用 AnyBot 瀏覽器、保險庫和 `sent`(帶著使用者的登入)。公開網頁用 `web_fetch`。 -- 不同電腦、不同設定檔的登入不共用。一輪裡沒帶 `profile` 的瀏覽器步驟用這一輪上一次指名的那個(步驟的參數會補上它),一輪還沒指名過才用使用中的;指名別的就從那一步起換過去(`profiles` 不補)。以前沒帶就用使用中的,別的工作剛換過就落到別的帳號(jenny-Host 11/26:拾日的 9 則回覆從 Ghost 送出)。 +- 不同電腦、不同設定檔的登入不共用。一輪裡沒帶 `profile` 的瀏覽器步驟用這一輪在同一台電腦上一次指名的那個(模型一送出就補進參數,聊天裡看得到),一輪還沒指名過才用使用中的;指名別的就從那一步起換過去。不補:`profiles`、`sent`(沒帶照舊列全部帳號);「你的瀏覽器」(和「我的瀏覽器」這類說法)只用那一步,不往下帶。沒寫電腦和寫預設那台的名字算同一台。補上的設定檔會跟著步驟存進聊天,從這輪學到的技能也會記下它。以前沒帶就用使用中的,別的工作剛換過就落到別的帳號(jenny-Host 11/26:拾日的 9 則回覆從 Ghost 送出)。 - 「做事的電腦」開不了瀏覽器(沒裝 Chrome/Brave…、SSH)時,步驟回「使用者選的電腦「…」現在開不了 AnyBot 的瀏覽器」,不改開在這台 Mac。公開網頁用 `web_fetch`,其他先問。 - 使用者的 Firefox 若已用平常方式開著,要先請他關掉,AnyBot 才開得起來。 - 一模一樣的按鈕(每篇的「回覆」)清單只列一次;要點第二篇,先 `open` 那篇網址。 diff --git a/src/agent.rs b/src/agent.rs index ccdb0bb..efd5fa4 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -459,8 +459,10 @@ struct UsedBrowsers { close: bool, /// This job's name in the browsers (its tabs are kept apart by it). job: String, - /// The browser profile this turn last named (see follow_profile). - profile: String, + /// The browser profile this turn last named on each computer (see follow_profile). + profile: std::collections::HashMap, + /// The computer a step that names none runs on (its name, so both ways of saying it match). + here: String, } /// Names the jobs of this run (a bot's job on another computer's browser carries it along). @@ -489,7 +491,7 @@ fn with_job(args: &str, job: &str) -> String { impl UsedBrowsers { fn new(via_name: &str, bot_id: u64, close: bool) -> Self { let n = JOB_SEQ.fetch_add(1, Ordering::Relaxed); - UsedBrowsers { list: vec![], screen: String::new(), via_name: via_name.to_string(), close, job: format!("{bot_id}-{}-{n}", crate::model::now_ms()), profile: String::new() } + UsedBrowsers { list: vec![], screen: String::new(), via_name: via_name.to_string(), close, job: format!("{bot_id}-{}-{n}", crate::model::now_ms()), profile: Default::default(), here: String::new() } } /// A browser step that names no profile runs in the one this turn last named, not in the @@ -498,18 +500,27 @@ impl UsedBrowsers { /// 「拾日」 named no profile, ran in 「Ghost」 (the current one) and nine replies went out /// from the wrong Threads account. fn follow_profile(&mut self, c: &mut ToolCall) { - if !c.name.starts_with("browser_") || c.name == "browser_profiles" { + // (Managing profiles names one on purpose; the sent log without one lists every account.) + if !c.name.starts_with("browser_") || matches!(c.name.as_str(), "browser_profiles" | crate::browser::SENT_LOG) { return; } let mut v = tools::args_of(&c.args); let Some(o) = v.as_object_mut() else { return }; + // Per computer: a profile named on another one may not be there, or be another login. + let on = o.get("computer").and_then(|x| x.as_str()).map(str::trim).filter(|x| !x.is_empty()).unwrap_or(&self.here).to_string(); match o.get("profile").and_then(|p| p.as_str()).map(str::trim).filter(|p| !p.is_empty()) { - Some(p) => self.profile = p.to_string(), - None if !self.profile.is_empty() => { - o.insert("profile".into(), serde_json::json!(self.profile)); - c.args = v.to_string(); + // The person's own browser is read for a step, never carried on: AnyBot doesn't open + // or type in it, and a later step meant AnyBot's own. + Some(p) if crate::botbrowser::means_persons_browser(p) => {} + Some(p) => { + self.profile.insert(on, p.to_string()); + } + None => { + if let Some(p) = self.profile.get(&on) { + o.insert("profile".into(), serde_json::json!(p)); + c.args = v.to_string(); + } } - None => {} } } @@ -2112,8 +2123,6 @@ struct CallTurn<'a> { fn run_call(t: CallTurn<'_>, asked: ToolCall) -> Flow { #[allow(unused_mut)] let CallTurn { job, bot_id, send, cancel, mut history, mut grants, said_no, refused, trouble, mut tools_on, mut local_on, mut tools_once, mut local_once, mut browsers, sent_count, hooks, hook_base, runs, mut tried, step_sent_at, step_sent, step_note, skill_limit, mut previous, mut plan_changes, last_sent, runs_with, mut replay, mut plan_go, moved } = t; - let mut asked = asked; - browsers.follow_profile(&mut asked); // The same click twice in one step opens a menu and closes it again (a Wi‑Fi // menu did exactly that): the second one isn't done. let twice = matches!(asked.name.as_str(), "computer_use_click" | "browser_click") && previous.as_ref() == Some(&(asked.name.clone(), asked.args.clone())); @@ -2337,6 +2346,7 @@ pub fn spawn(job: Job, bot_id: u64, tx: Sender<(u64, StreamEvent)>, cancel: Arc< let mut local_once = false; let mut browsers = UsedBrowsers::new(&job.via_name, bot_id, job.close_browsers); browsers.screen = box_screen_of(&job, bot_id); + browsers.here = job.default_computer.clone(); // 電腦現況: what someone at each computer already knows (system, sudo, network, apps…). // Added ONCE here, before the step loop: the turn's system prefix (instructions + facts) // and its tool list (and the deterministic `quick_tools` map of it) never change between @@ -2623,6 +2633,9 @@ pub fn spawn(job: Job, bot_id: u64, tx: Sender<(u64, StreamEvent)>, cancel: Arc< } computer::expand_call(c); crate::browser::expand_call(c); + // (Here, where the call is first written down: the chat, the repeat checks and a + // step moved to the chosen computer all see the profile it runs in.) + browsers.follow_profile(c); } let quick = if plan.quick_is_off() { None } else { tried_quick }; let via = if quick.is_none() && scripted.is_none() { job.via.clone() } else { via }; @@ -4401,7 +4414,7 @@ echo 'done送出了' > done.html; (setsid pyt assert_eq!(profile(&c), None); let mut c = call("browser_open", r#"{"url":"https://www.threads.com/@shiri.days__","profile":"拾日(Firefox)"}"#); b.follow_profile(&mut c); - for (name, args) in [("browser_click", r#"{"ref":"20"}"#), ("browser_type", r#"{"ref":"20","value":"嗨"}"#), ("browser_press", r#"{"key":"ctrl+Return","profile":""}"#), ("browser_sent_log", "")] { + for (name, args) in [("browser_click", r#"{"ref":"20"}"#), ("browser_type", r#"{"ref":"20","value":"嗨"}"#), ("browser_press", r#"{"key":"ctrl+Return","profile":""}"#), ("browser_read", "")] { let mut c = call(name, args); b.follow_profile(&mut c); assert_eq!(profile(&c).as_deref(), Some("拾日(Firefox)"), "{name}"); @@ -4413,14 +4426,63 @@ echo 'done送出了' > done.html; (setsid pyt let mut c = call("browser_press", r#"{"key":"Enter"}"#); b.follow_profile(&mut c); assert_eq!(profile(&c).as_deref(), Some("Ghost(Firefox)")); - // Not a browser step, or managing the profiles: untouched. - for (name, args) in [("browser_profiles", r#"{"name":"新的"}"#), ("shell", r#"{"command":"ls"}"#)] { + // The person's own browser read for a step, however it is called: not carried on. + for p in ["你的瀏覽器", "我的瀏覽器", "My Chrome"] { + let mut c = call("browser_read", &format!(r#"{{"profile":"{p}"}}"#)); + b.follow_profile(&mut c); + } + let mut c = call("browser_open", r#"{"url":"https://x"}"#); + b.follow_profile(&mut c); + assert_eq!(profile(&c).as_deref(), Some("Ghost(Firefox)")); + // Per computer: one named on the Mac isn't carried here, and the reverse. + let mut c = call("browser_open", r#"{"url":"https://x","computer":"Mac","profile":"工作"}"#); + b.follow_profile(&mut c); + let mut c = call("browser_click", r#"{"ref":"1","computer":"Mac"}"#); + b.follow_profile(&mut c); + assert_eq!(profile(&c).as_deref(), Some("工作")); + let mut c = call("browser_click", r#"{"ref":"1"}"#); + b.follow_profile(&mut c); + assert_eq!(profile(&c).as_deref(), Some("Ghost(Firefox)")); + let mut c = call("browser_click", r#"{"ref":"1","computer":"筆電"}"#); + b.follow_profile(&mut c); + assert_eq!(profile(&c), None); + // The default computer named or not: the same computer. + let mut b = UsedBrowsers::new("t", 1, false); + b.here = "jenny-Host".into(); + let mut c = call("browser_open", r#"{"url":"https://x","computer":"jenny-Host","profile":"拾日(Firefox)"}"#); + b.follow_profile(&mut c); + let mut c = call("browser_click", r#"{"ref":"20"}"#); + b.follow_profile(&mut c); + assert_eq!(profile(&c).as_deref(), Some("拾日(Firefox)")); + let mut c = call("browser_open", r#"{"url":"https://x","profile":"Sunny(Firefox)"}"#); + b.follow_profile(&mut c); + let mut c = call("browser_click", r#"{"ref":"20","computer":"jenny-Host"}"#); + b.follow_profile(&mut c); + assert_eq!(profile(&c).as_deref(), Some("Sunny(Firefox)")); + // Not a browser step, managing the profiles, or the sent log (every account without one): untouched. + for (name, args) in [("browser_profiles", r#"{"name":"新的"}"#), ("shell", r#"{"command":"ls"}"#), ("browser_sent_log", r#"{"days":1}"#)] { let mut c = call(name, args); b.follow_profile(&mut c); assert_eq!(c.args, args, "{name}"); } } + /// Through a real turn: the call written down (what the chat keeps and the repeat checks + /// read) already carries the turn's profile. + #[test] + fn the_chat_keeps_the_profile_a_browser_step_ran_in() { + let replies = vec![ + sse_call("c1", "browser", serde_json::json!({"action": "read", "profile": "拾日(Firefox)"})), + sse_call("c2", "browser_press", serde_json::json!({"key": "ctrl+Return"})), + ]; + let (_, _, seen) = browser_turn(replies, false, None, &[], ApprovalAnswer::Reject); + let last = seen.last().unwrap(); + let body: serde_json::Value = serde_json::from_str(&last[last.find('{').unwrap()..]).unwrap(); + let args = body["messages"].as_array().unwrap().iter().flat_map(|m| m["tool_calls"].as_array().cloned().unwrap_or_default()).find(|c| c["id"] == "c2").map(|c| c["function"]["arguments"].as_str().unwrap_or("").to_string()).unwrap(); + let v = tools::args_of(&args); + assert_eq!((v["key"].as_str(), v["profile"].as_str()), (Some("ctrl+Return"), Some("拾日(Firefox)")), "{args}"); + } + #[test] fn a_model_that_sent_the_one_tool_empty_gets_the_one_action_tools() { let start = || { diff --git a/src/app.rs b/src/app.rs index 7692ddc..bc9fec0 100644 --- a/src/app.rs +++ b/src/app.rs @@ -7299,7 +7299,7 @@ impl App { // that bot's whole chat, megabytes on a long one, and the work log already keeps what // happened in between.) let longest = self.streams.keys().filter_map(|id| self.state.bot(*id)).map(|b| b.messages.len()).max().unwrap_or(0); - let every = (longest as u64 / 2_000).clamp(5, 30); + let every = (5 + longest as u64 / 2_000).min(30); if self.dirty && !self.demo && (self.streams.is_empty() || self.save_now || self.last_save.elapsed().as_secs() >= every) { if self.mir.on { self.mirror_save(); diff --git a/src/botbrowser.rs b/src/botbrowser.rs index 8e15276..ad64a1a 100644 --- a/src/botbrowser.rs +++ b/src/botbrowser.rs @@ -31,6 +31,11 @@ pub const DEFAULT_PROFILE: &str = "預設"; pub const PERSON_PROFILE: &str = "你的瀏覽器"; /// Other ways people name their own browser. const PERSON_ALIASES: [&str; 6] = ["你的 chrome", "我的 chrome", "我的瀏覽器", "你的chrome", "我的chrome", "my chrome"]; +/// Whether a profile name means the person's own browser (「你的瀏覽器」 or another way of saying it). +pub fn means_persons_browser(name: &str) -> bool { + let n = name.trim(); + n == PERSON_PROFILE || PERSON_ALIASES.contains(&n.to_lowercase().as_str()) +} /// Longest profile name (estimated). pub const PROFILE_NAME_MAX_CHARS: usize = 30; /// File Chrome writes into its folder with the port it chose (`--remote-debugging-port=0`).