// 個人檔案: each bot is an individual of its own — its soul, what it is good at and what not, the // skills it has, what it may do (能力) and where (可以用的電腦) — and the 同事名冊 every bot reads, // so it knows whom to hand what. // // It also keeps every request small: a bot is told only about the tools, the rules and the skills // it has. (Measured before this: every bot carried all 59 tools, ~12 500 tokens of their // descriptions, and the rules for all of them, on every step of every turn — a bot that only // patrols Threads carried the shell, the screen and the files.) What a bot lacks it is not // offered, and a call to it anyway is refused with where to turn instead. use crate::model::{Bot, State}; /// The tool families a bot may have: (key, label, what it covers). A bot's `abilities` lists /// keys; None is all of them (a bot from before profiles keeps everything it had). pub const ABILITIES: [(&str, &str, &str); 11] = [ ("web", "上網", "讀公開網頁、搜尋"), ("browser", "瀏覽器", "AnyBot 瀏覽器:要登入的網站、保險箱填入"), ("screen", "看螢幕與操作", "截圖、點擊、打字,重播技能"), ("files", "檔案與指令", "讀寫檔案、執行指令、打開給你看、發布網站、傳到其他裝置"), ("mac_apps", "Mac App", "提醒事項、行事曆、聯絡人、備忘錄、郵件、訊息"), ("connectors", "連接器", "你接上的 MCP 連接器"), ("phone", "電話、簡訊、X", "打電話、傳簡訊、在 X 發文"), ("routines", "例行任務", "設定和管理定時要做的事"), ("pages", "頁面", "寫和維護追蹤、報告頁面"), ("team", "和其他 Bot 合作", "把工作交給其他 Bot"), ("admin", "管理 App", "建立和修改 Bot、群組、技能,改 App 設定"), ]; /// Which family a tool belongs to. None: every bot has it — remembering and looking things up /// in its memory, its skills, asking the person (a question card, taking over a screen), a card /// or a file in the chat, an emoji. pub fn ability_of(tool: &str) -> Option<&'static str> { Some(match tool { "web_fetch" => "web", crate::browser::TOOL => "browser", t if t.starts_with("browser") || t.starts_with("vault_") => "browser", crate::computer::TOOL | crate::agent::REPLAY_TOOL | "view_demo_step" => "screen", t if t.starts_with("computer_use") => "screen", "shell" | "read_file" | "edit_file" | "list_dir" | "search" | "read_terminal" | "write_terminal" | "open_for_person" | "publish_site" | "send_to_device" => "files", t if crate::macapps::is_mac_tool(t) => "mac_apps", t if crate::mcp::is_mcp_tool(t) || t == crate::mcp::FIND_TOOL || t == crate::mcp::USE_TOOL => "connectors", "place_call" | "send_sms" | "post_to_x" => "phone", "create_routine" | "update_routine" | "delete_routine" | "list_routines" | "run_routine_now" => "routines", "write_page" | "read_page" | "list_pages" => "pages", "message_bot" | "list_bots" => "team", "create_bot" | "update_bot" | "update_settings" | "create_group" | "move_to_group" | "rename_group" | "delete_group" | "delete_skill" | "skill_install" => "admin", _ => return None, }) } pub fn label(key: &str) -> &'static str { ABILITIES.iter().find(|a| a.0 == key).map(|a| a.1).unwrap_or("這個能力") } /// Whether the bot has this family. pub fn has(b: &Bot, key: &str) -> bool { b.abilities.as_ref().is_none_or(|a| a.iter().any(|x| x == key)) } /// Whether it works on computers at all (files, screens or a browser): the rules about /// computers, folders and choosing one are for those bots only. pub fn works_on_computers(b: &Bot) -> bool { has(b, "files") || has(b, "screen") || has(b, "browser") } /// The families it lacks (what its turns refuse). pub fn lacks(b: &Bot) -> Vec { ABILITIES.iter().map(|a| a.0).filter(|k| !has(b, k)).map(String::from).collect() } /// Keep only the tools the bot has. pub fn trim_tools(b: &Bot, specs: &mut Vec) { specs.retain(|t| ability_of(t["name"].as_str().unwrap_or("")).is_none_or(|k| has(b, k))); } /// What a refused call says. pub fn lacking_text(key: &str) -> String { format!("「{}」不是你的能力(Bot 設定 › 能力沒有開)。不要換別的工具硬做:告訴使用者,或交給同事名冊裡會這件事的 Bot。", label(key)) } /// The skills this bot has (by name; all of them when it never chose). pub fn skills_of(b: &Bot, all: &[crate::memory::Skill]) -> Vec { match &b.skills { None => all.to_vec(), Some(names) => all.iter().filter(|k| names.iter().any(|n| n == &k.name)).cloned().collect(), } } /// Whether it has this skill. pub fn has_skill(b: &Bot, name: &str) -> bool { b.skills.as_ref().is_none_or(|names| names.iter().any(|n| n == name)) } /// Who it is, for its own instructions: the soul first, then the role, what it is good at and /// what it leaves to others. pub fn identity(b: &Bot) -> String { let mut s = String::new(); let soul = b.soul.trim(); if !soul.is_empty() { s.push_str("\n\n你是怎樣的存在(你的 soul:個性、價值觀、說話和做事的方式;照著它做自己):\n"); s.push_str(soul); s.push('\n'); } let strengths = b.strengths.trim(); let not_for = b.not_for.trim(); if !strengths.is_empty() { s.push_str(&format!("\n你擅長:{strengths}。")); } if !not_for.is_empty() { s.push_str(&format!("\n不找你做的事:{not_for}。遇到這類事不要硬做,交給同事名冊裡適合的 Bot,或告訴使用者該找誰。")); } s } /// One line, at most `max` characters (the roster stays short whatever is written). fn short(text: &str, max: usize) -> String { let t: String = text.split_whitespace().collect::>().join(" "); let t = t.trim_end_matches(['。', '.', ';', ';']).to_string(); if t.chars().count() <= max { t } else { format!("{}…", t.chars().take(max).collect::()) } } /// 同事名冊: the other bots, one line each — who it is, what it is good at, what not, where it /// works. What they keep for themselves (their soul, their memories) is not here. In the order of /// the bots, so it reads the same every turn (the model's cache holds). pub fn roster(me: &Bot, st: &State) -> String { let others: Vec<&Bot> = st.bots.iter().filter(|b| b.id != me.id && b.members.is_empty()).collect(); if others.is_empty() { return String::new(); } let how = if has(me, "team") { "要別人的專長時用 message_bot 交給它,把前因後果交代清楚" } else { "要別人的專長時,告訴使用者該找誰" }; let mut s = format!("\n\n同事名冊(其他 Bot,各有自己的記憶、技能和電腦;{how}):"); for b in others { s.push_str(&format!("\n- {}", b.name)); if b.id == st.primary_bot { s.push_str("(主要聯絡窗口)"); } let about = b.description.trim(); if !about.is_empty() { s.push_str(&format!(":{}", short(about, 50))); } if !b.strengths.trim().is_empty() { s.push_str(&format!(";擅長 {}", short(&b.strengths, 60))); } if !b.not_for.trim().is_empty() { s.push_str(&format!(";不找它 {}", short(&b.not_for, 40))); } if let Some(c) = &b.computers { let own = crate::model::own_computer_name(&b.name); let mut names = vec![own.clone()]; names.extend(c.iter().filter(|n| **n != own).cloned()); s.push_str(&format!(";在 {} 做事", short(&names.join("、"), 60))); } } s } #[cfg(test)] mod tests { use super::*; fn bot(name: &str) -> Bot { let mut b = State::demo().bots[0].clone(); b.name = name.into(); b } #[test] fn every_tool_has_its_family() { assert_eq!(ability_of("web_fetch"), Some("web")); assert_eq!(ability_of(crate::browser::TOOL), Some("browser")); assert_eq!(ability_of(crate::vault::TOOL), Some("browser")); assert_eq!(ability_of(crate::vault::LIST_TOOL), Some("browser")); assert_eq!(ability_of(crate::computer::TOOL), Some("screen")); assert_eq!(ability_of("shell"), Some("files")); assert_eq!(ability_of("mcp__github__get_issue"), Some("connectors")); assert_eq!(ability_of(crate::mcp::USE_TOOL), Some("connectors")); assert_eq!(ability_of("create_routine"), Some("routines")); assert_eq!(ability_of("write_page"), Some("pages")); assert_eq!(ability_of("message_bot"), Some("team")); assert_eq!(ability_of("update_bot"), Some("admin")); // What every bot keeps. for t in ["remember", "forget", "search_memory", "use_skill", "save_skill", crate::ask::TOOL, crate::tools::HANDOFF_TOOL, "send_file", "show_card", "react", "search_documents", "list_computers"] { assert_eq!(ability_of(t), None, "{t}"); } } #[test] fn a_bot_is_offered_only_what_it_has() { let mut b = bot("巡邏"); let specs = || vec![serde_json::json!({"name": "web_fetch"}), serde_json::json!({"name": "browser"}), serde_json::json!({"name": "shell"}), serde_json::json!({"name": "remember"})]; // From before profiles: everything. let mut all = specs(); trim_tools(&b, &mut all); assert_eq!(all.len(), 4); assert!(lacks(&b).is_empty()); b.abilities = Some(vec!["browser".into()]); let mut some = specs(); trim_tools(&b, &mut some); let names: Vec<&str> = some.iter().filter_map(|t| t["name"].as_str()).collect(); assert_eq!(names, vec!["browser", "remember"]); assert!(lacks(&b).contains(&"files".to_string()) && !lacks(&b).contains(&"browser".to_string())); assert!(works_on_computers(&b)); b.abilities = Some(vec!["web".into(), "pages".into()]); assert!(!works_on_computers(&b)); } #[test] fn skills_are_the_ones_it_chose() { let skill = |n: &str| crate::memory::Skill { name: n.into(), ..Default::default() }; let all = vec![skill("pptx"), skill("週報"), skill("xlsx")]; let mut b = bot("寫手"); assert_eq!(skills_of(&b, &all).len(), 3); b.skills = Some(vec!["週報".into(), "不存在".into()]); let mine = skills_of(&b, &all); assert_eq!(mine.iter().map(|k| k.name.as_str()).collect::>(), vec!["週報"]); assert!(has_skill(&b, "週報") && !has_skill(&b, "pptx")); } #[test] fn the_roster_says_who_does_what_and_keeps_the_soul_private() { let mut st = State::demo(); st.bots.truncate(3); let me = st.bots[0].clone(); st.bots[1].strengths = "FSD 新聞、法規進度".into(); st.bots[1].not_for = "寫程式".into(); st.bots[1].soul = "我很嚴謹,只引用官方來源".into(); st.bots[1].computers = Some(vec!["Mac mini".into()]); let r = roster(&me, &st); assert!(r.contains(&st.bots[1].name) && r.contains("擅長 FSD 新聞、法規進度") && r.contains("不找它 寫程式") && r.contains("Mac mini"), "{r}"); assert!(!r.contains("嚴謹") && !r.contains(&format!("- {}", me.name)), "no soul, not itself: {r}"); assert_eq!(r, roster(&me, &st), "the same every turn"); // Its own: the soul first. let id = identity(&st.bots[1]); assert!(id.contains("我很嚴謹") && id.contains("你擅長:FSD 新聞") && id.contains("不找你做的事:寫程式")); // Long words stay short. st.bots[2].strengths = "很".repeat(200); assert!(roster(&me, &st).chars().count() < 400); } }