188 lines
9.9 KiB
Rust
188 lines
9.9 KiB
Rust
// 功能地圖 (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!("plan", "計畫(/plan)"),
|
||
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.extend([crate::plan::SUBMIT_TOOL, crate::plan::UPDATE_TOOL].map(String::from));
|
||
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());
|
||
}
|
||
}
|