AnyBot/src/featuremap.rs

186 lines
9.8 KiB
Rust
Raw Normal View History

feat: AnyBot 功能地圖——Bot 用 anybot_guide 認識 AnyBot 本身;修好地圖整理時找到的七個問題 功能地圖(照 Lauren Tan 的 feature map,poteto/verification-skill-example) - docs/feature-map/:20 區加索引,每區四段(子功能/使用者怎麼到/怎麼操作(Bot:、驗證:)/陷阱),內容逐條對照程式;索引寫共同前提、證據與略過的回報、全面檢查順序 - Bot 的系統提示多一行區塊目錄;新工具 anybot_guide 一次讀一區(唯讀) - cargo test featuremap:: 擋漏寫:Bot 能拿到的每個工具、browser/computer 的每個動作、設定視窗每個分頁和區塊、update_settings 的鍵;每區要有四段、不能太長 - 自動學技能:AnyBot 本身怎麼用不抄進技能,地圖錯漏記成待改進;園丁在重複的待改進旁提醒補地圖 - CLAUDE.md:改功能時同一個 commit 一起改地圖 - 側邊欄「分區」工具的說明寫清楚:不是群組聊天 修正 1. 別人傳來的訊息(訊息管道、團隊成員)那一輪不給 AnyBot 瀏覽器、保險庫和已送紀錄(瀏覽器帶著使用者的登入);硬叫也擋下,公開網頁仍可 web_fetch 2. 自動學會的技能加進那個 Bot 自己的技能清單(支線的算主 Bot 的) 3. 按停止一併停掉這個 Bot 的互動終端機(一般背景指令照舊) 4. 設定 › 整合 › 技能、頁面的「刪除」要再按一次確定 5. update_bot 先跳確認卡片,列出每一項要改成什麼 6. Bot 範本的頭像形狀改成存在的形狀,並加測試 7. 文字:直接操作的提示改「設定 → 權限」、金鑰錯誤改「設定 → 模型」、等核准逾時照實說是什麼在等 Mac 490 個測試通過。 Refs: cl3xk7 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 03:15:09 +00:00
// 功能地圖 (Lauren Tan's feature map, poteto/verification-skill-example): AnyBot as behaviour,
// one file per area under docs/feature-map/, each answering the same four questions — what is
// there, how a person gets to it, how an agent drives it, what usually lies. Two readers:
// - the bots: anybot_guide reads one area. AnyBot is a product the bots work inside, and they
// used to learn it by trial and error (a contact fill used 107 times as a typing tool) and
// write what they found into their task skills;
// - agents changing AnyBot: they open the file for the change and check it the way it says.
// Kept as code: the tests below fail when a tool a bot can be given, a browser or screen action,
// or a settings tab or section isn't in the map — a new feature comes with its entry.
use serde_json::{json, Value};
/// One area of the map.
pub struct Area {
pub slug: &'static str,
pub title: &'static str,
pub text: &'static str,
}
/// The map's index: who reads it, the shared conventions, the order of a full sweep (for people
/// and agents changing AnyBot; a bot gets `index_line` instead).
#[cfg(test)]
pub const INDEX: &str = include_str!("../docs/feature-map/README.md");
macro_rules! area {
($slug:literal, $title:literal) => {
Area { slug: $slug, title: $title, text: include_str!(concat!("../docs/feature-map/", $slug, ".md")) }
};
}
/// The areas, in the order of a full sweep.
pub const AREAS: &[Area] = &[
area!("chat", "聊天"),
area!("bots", "Bot 與個人檔案"),
area!("team", "同事、群組與支線"),
area!("memory", "記憶"),
area!("skills", "技能與重播"),
area!("pages", "頁面"),
area!("routines", "例行任務"),
area!("goal", "目標(/goal)"),
area!("computers", "電腦:大腦與手腳"),
area!("files", "檔案與指令"),
area!("browser", "瀏覽器"),
area!("screen", "螢幕操作"),
area!("vault", "保險庫"),
area!("connectors", "連接器(MCP)"),
area!("apps", "Mac App 與電話"),
area!("channels", "訊息管道"),
area!("phone", "手機與帳號"),
area!("safety", "安全與核准"),
area!("settings", "設定總覽"),
area!("garden", "園丁、待改進與更新"),
];
/// The tool a bot reads the map with.
pub const TOOL: &str = "anybot_guide";
pub fn spec() -> Value {
let topics: Vec<&str> = AREAS.iter().map(|a| a.slug).collect();
json!({"name": TOOL,
"description": "Read one area of AnyBot's own feature map: what AnyBot can do there, where the person finds it, which of your tools drive it, and what usually goes wrong. Read it when the person asks about AnyBot itself (can you…, where do I set…, why did…), before working around something AnyBot may already do, and when one of your tools behaves unexpectedly. Don't guess AnyBot's features or settings from memory.",
"parameters": {"type": "object", "properties": {
"topic": {"type": "string", "enum": topics, "description": "The area (the list is in your instructions)."}},
"required": ["topic"]}})
}
/// One line for the instructions: the areas there are (the same every turn, so it caches).
pub fn index_line() -> String {
let list: Vec<String> = AREAS.iter().map(|a| format!("{} {}", a.slug, a.title)).collect();
format!("\n\nAnyBot 功能地圖({TOOL} 讀一區;問到 AnyBot 本身、要繞路之前、工具反應怪怪的,先讀,不要憑印象):{}。", list.join("、"))
}
/// The area a bot asked for (by slug or title).
pub fn read(topic: &str) -> Result<String, String> {
let t = topic.trim();
AREAS
.iter()
.find(|a| a.slug.eq_ignore_ascii_case(t) || a.title == t || a.title.starts_with(t) && !t.is_empty())
.map(|a| a.text.trim().to_string())
.ok_or_else(|| format!("沒有「{t}」這一區。可以讀:{}", AREAS.iter().map(|a| a.slug).collect::<Vec<_>>().join("、")))
}
#[cfg(test)]
mod tests {
use super::*;
/// The whole map, as one text (the areas and the index).
fn map_text() -> String {
std::iter::once(INDEX).chain(AREAS.iter().map(|a| a.text)).collect::<Vec<_>>().join("\n")
}
fn names(specs: &[Value]) -> Vec<String> {
specs.iter().filter_map(|t| t["name"].as_str().map(String::from)).collect()
}
fn actions(specs: &[Value], tool: &str) -> Vec<String> {
specs.iter().filter(|t| t["name"] == tool).flat_map(|t| t["parameters"]["properties"]["action"]["enum"].as_array().cloned().unwrap_or_default()).filter_map(|a| a.as_str().map(String::from)).collect()
}
/// Every tool a bot can be given, from where the window puts them together (app.rs).
fn every_tool() -> Vec<String> {
let one = vec!["x".to_string()];
let mut all = names(&crate::appctl::specs());
all.extend(names(&crate::tools::specs_for(&one, "x")));
all.extend(names(&crate::web::specs_for(&one, "x")));
all.extend(names(&crate::computer::specs_for(&one, "x", true)));
all.extend(names(&crate::macapps::specs()));
all.extend(names(&crate::phone::specs()));
all.extend(names(&crate::vault::specs_for(&crate::vault::Offer { login: true, card: true, contact: true })));
all.push(crate::ask::spec()["name"].as_str().unwrap().to_string());
all.push(crate::teach::view_step_spec()["name"].as_str().unwrap().to_string());
all.extend([crate::mcp::FIND_TOOL, crate::mcp::USE_TOOL, crate::agent::REPLAY_TOOL, crate::tools::HANDOFF_TOOL, "show_card", "react", "send_to_device", "send_file", "open_for_person", "publish_site", TOOL].map(String::from));
all.sort();
all.dedup();
all
}
#[test]
fn every_tool_a_bot_can_be_given_is_on_the_map() {
let map = map_text();
let missing: Vec<String> = every_tool().into_iter().filter(|n| !map.contains(&format!("`{n}`"))).collect();
assert!(missing.is_empty(), "these tools aren't on the feature map (docs/feature-map/, written as `name`): {missing:?}");
}
#[test]
fn every_browser_and_screen_action_is_in_its_area() {
let one = vec!["x".to_string()];
let area = |slug: &str| AREAS.iter().find(|a| a.slug == slug).unwrap().text;
for (slug, tool, specs) in [("browser", crate::browser::TOOL, crate::web::specs_for(&one, "x")), ("screen", crate::computer::TOOL, crate::computer::specs_for(&one, "x", true))] {
let missing: Vec<String> = actions(&specs, tool).into_iter().filter(|a| !area(slug).contains(&format!("`{a}`"))).collect();
assert!(missing.is_empty(), "{slug}.md doesn't name these `{tool}` actions: {missing:?}");
}
}
#[test]
fn every_settings_tab_and_section_is_in_the_settings_area() {
let settings = AREAS.iter().find(|a| a.slug == "settings").unwrap().text;
let sources = [include_str!("app.rs"), include_str!("app/remote_ui.rs"), include_str!("app/vault_ui.rs"), include_str!("app/voice_ui.rs"), include_str!("app/channels_ui.rs"), include_str!("app/computers_ui.rs"), include_str!("app/mirror_ui.rs")];
let mut wanted: Vec<String> = vec![];
for src in sources {
for pat in ["setting_title(ui, \"", "setting_row(ui, p, \""] {
for part in src.split(pat).skip(1) {
if let Some(name) = part.split('"').next().filter(|n| !n.is_empty()) {
wanted.push(name.to_string());
}
}
}
}
// The tabs of the settings window.
let tabs = include_str!("app.rs").split("(SettingsTab::General, \"").nth(1).and_then(|r| r.split(']').next()).unwrap_or("");
wanted.extend(tabs.split("\", Icon::").filter_map(|p| p.rsplit('"').next()).filter(|n| !n.is_empty() && !n.contains(')')).map(String::from));
wanted.push("一般".into());
// What a bot may change itself.
let keys = crate::appctl::specs().into_iter().find(|t| t["name"] == "update_settings").map(|t| t["parameters"]["properties"].as_object().map(|o| o.keys().cloned().collect::<Vec<_>>()).unwrap_or_default()).unwrap_or_default();
wanted.extend(keys.into_iter().map(|k| format!("`{k}`")));
wanted.sort();
wanted.dedup();
assert!(wanted.len() > 15, "the settings scan found too little: {wanted:?}");
let missing: Vec<&String> = wanted.iter().filter(|w| !settings.contains(w.as_str())).collect();
assert!(missing.is_empty(), "settings.md doesn't mention: {missing:?}");
}
#[test]
fn every_area_answers_the_four_questions_and_the_index_lists_it() {
for a in AREAS {
for h in ["## 子功能", "## 使用者怎麼到", "## 怎麼操作", "## 陷阱"] {
assert!(a.text.contains(h), "{}.md has no 「{h}」", a.slug);
}
assert!(a.text.contains("- Bot:") && a.text.contains("- 驗證:"), "{}.md: 怎麼操作 needs a 「- Bot:」 and a 「- 驗證:」 line", a.slug);
assert!(a.text.starts_with(&format!("# {}", a.title)), "{}.md should start with 「# {}」", a.slug, a.title);
assert!(INDEX.contains(&format!("({}.md)", a.slug)), "README.md doesn't list {}.md", a.slug);
// Short enough to read in one go (her rule: behaviour-level, no source needed).
assert!(a.text.chars().count() < 6000, "{}.md is too long ({} chars): split it", a.slug, a.text.chars().count());
}
}
#[test]
fn a_bot_reads_one_area_by_its_name() {
assert!(read("routines").unwrap().starts_with("# 例行任務"));
assert!(read("例行任務").unwrap().starts_with("# 例行任務"));
assert!(read("nope").unwrap_err().contains("routines"));
assert!(index_line().contains("routines 例行任務") && index_line().contains(TOOL));
assert_eq!(spec()["parameters"]["properties"]["topic"]["enum"].as_array().unwrap().len(), AREAS.len());
}
}