AnyBot/src/app/profile_ui.rs

158 lines
8.8 KiB
Rust
Raw Normal View History

Bot 個人檔案:soul、擅長、技能、能力、可用電腦、同事名冊;例行任務每輪開新對話(省 token) 每個 Bot 是獨立的個體(src/profile.rs、src/app/profile_ui.rs) - 新欄位:soul(只有自己讀,放在提示最前面)、擅長、不找它做的事、會的技能、能力、可以用的電腦 - 同事名冊:每個 Bot 的提示裡一行一個其他 Bot(簡介、擅長、不找它、在哪幾台電腦),不含別人的 soul 和記憶;順序固定,吃得到快取 - 能力 11 類(上網、瀏覽器、螢幕、檔案與指令、Mac App、連接器、電話簡訊 X、例行任務、頁面、 合作、管理 App):沒勾的工具不提供、規則不寫進提示;硬叫會被 agent 擋下並說該找誰 - 技能:說明只列自己會的,use_skill 只讀得到自己的;自己學會的自動算它的 - 可以用的電腦:自己的電腦一定可以;預設電腦不在名單時回到自己的電腦,不誤報「沒有連線」; 其他畫面也拿得到完整電腦清單來勾;匯出 Bot 不帶電腦名單和草稿 - 舊 Bot 全部勾選(None = 全部),行為不變 - 例行任務:每輪只從那則訊息讀起(Message::fresh),提醒先讀頁面/查記憶;聊天畫面不變, 使用者自己的下一則照樣看整段 - 順手修:角色說明結尾的「。」不再變成「。。」 - 實測(照建議勾能力):明日之星固定內容 ~21.7k → ~11.5k token、FSD ~25.5k → ~13.2k; 例行任務每輪歷史最多 ~70k → 幾百 連接器(MCP) - 實測三個連接器都能用(本機用 AnyBot 自己的 MCP client):time(get_current_time)、 fetch(讀 example.com)、SccMCP(82 個工具,唯讀的 system_status 查詢成功;超過 20 個走 find / use,搜尋得到) - 測試:有「連接器」能力的 Bot 拿得到(大的走 find/use、提示有說明),沒有的拿不到,硬叫三種 方式都被擋 - 測試用的假模型伺服器改成讀完整個請求再回,平行跑測試時不再偶發斷線 驗證:macOS cargo test 389 通過(連跑三次);Windows/Linux cargo check 通過 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 09:14:51 +00:00
// Bot 設定 › 個人檔案: who the bot is (its soul, what it is good at, what it leaves to others),
// what it may do (能力), the skills it has and the computers it may use. The rules and the
// roster that go with them are in profile.rs.
use super::*;
/// The editor's skill list scrolls past this height.
const SKILL_LIST_MAX_H: f32 = 220.0;
impl App {
/// 個人檔案: soul, 擅長, 不找它做的事.
pub(super) fn profile_identity_card(&mut self, ui: &mut egui::Ui, b: &mut Bot) {
let p = self.pal;
setting_card(ui, p, |ui| {
setting_title(ui, "個人檔案", "Soul 只有這個 Bot 自己讀,放在它每次回答的最前面。「擅長」和「不找它做的事」會出現在其他 Bot 的同事名冊裡,讓它們知道什麼事該交給誰。", p);
field_label(ui, "Soul(個性、價值觀、說話和做事的方式)", p);
ui.add(egui::TextEdit::multiline(&mut b.soul).hint_text(t("例如:我是細心的研究員,只相信官方來源;先講結論、再給證據;不確定就說不確定,不硬猜。")).desired_rows(3).desired_width(f32::INFINITY));
field_label(ui, "擅長(什麼事找它)", p);
ui.add(egui::TextEdit::singleline(&mut b.strengths).hint_text(t("例如:FSD 新聞和法規進度、維護追蹤頁")).desired_width(f32::INFINITY));
field_label(ui, "不找它做的事", p);
ui.add(egui::TextEdit::singleline(&mut b.not_for).hint_text(t("例如:寫程式、操作別人的電腦")).desired_width(f32::INFINITY));
});
}
/// 能力: the tool families it has. Fewer is safer and cheaper: what it lacks isn't offered
/// (nor described) on any step.
pub(super) fn profile_abilities_card(&mut self, ui: &mut egui::Ui, b: &mut Bot) {
let p = self.pal;
setting_card(ui, p, |ui| {
setting_title(ui, "能力", "這個 Bot 可以用哪幾類工具。沒勾的不會提供給它,也不會寫進它的說明:每一步都比較省 token,也比較安全。記憶、技能、問你問題、傳檔案到聊天每個 Bot 都有。", p);
let mut on: Vec<String> = b.abilities.clone().unwrap_or_else(|| crate::profile::ABILITIES.iter().map(|a| a.0.to_string()).collect());
let mut changed = false;
egui::Grid::new(("abilities", b.id)).num_columns(2).spacing([16.0, 4.0]).show(ui, |ui| {
for (i, (key, label, what)) in crate::profile::ABILITIES.iter().enumerate() {
let mut has = on.iter().any(|x| x == key);
if ui.checkbox(&mut has, t(label)).on_hover_text(t(what)).changed() {
changed = true;
if has {
on.push(key.to_string());
} else {
on.retain(|x| x != key);
}
}
if i % 2 == 1 {
ui.end_row();
}
}
});
if changed {
// All of them again: back to "everything", so families added later come too.
b.abilities = if crate::profile::ABILITIES.iter().all(|a| on.iter().any(|x| x == a.0)) { None } else { Some(on) };
}
let n = crate::profile::ABILITIES.iter().filter(|a| crate::profile::has(b, a.0)).count();
hint(ui, format!("{n} / {} 類", crate::profile::ABILITIES.len()), p);
});
}
/// 技能: the ones it has, out of the library (installed once, for all).
pub(super) fn profile_skills_card(&mut self, ui: &mut egui::Ui, b: &mut Bot) {
let p = self.pal;
let all: Vec<(String, String)> = self.state.skills.iter().filter(|k| k.enabled).map(|k| (k.name.clone(), k.description.clone())).collect();
if all.is_empty() {
return;
}
setting_card(ui, p, |ui| {
setting_title(ui, "技能", "從技能庫勾選這個 Bot 會的技能。它的說明裡只列這些,用到時才讀完整做法。它自己學會(存成)的技能會自動加進來。", p);
let mut mine: Vec<String> = b.skills.clone().unwrap_or_else(|| all.iter().map(|k| k.0.clone()).collect());
let mut changed = false;
ui.horizontal(|ui| {
let n = all.iter().filter(|k| mine.contains(&k.0)).count();
ui.label(RichText::new(format!("{} {n} / {}", t("會"), all.len())).size(FONT_SM).color(p.text2));
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
if ui.small_button(t("全不選")).clicked() {
mine.clear();
changed = true;
}
if ui.small_button(t("全選")).clicked() {
mine = all.iter().map(|k| k.0.clone()).collect();
changed = true;
}
});
});
egui::ScrollArea::vertical().id_salt(("skills", b.id)).max_height(SKILL_LIST_MAX_H).auto_shrink([false, true]).show(ui, |ui| {
for (name, about) in &all {
let mut has = mine.contains(name);
let r = ui.checkbox(&mut has, name.as_str());
let r = if about.trim().is_empty() { r } else { r.on_hover_text(about.as_str()) };
if r.changed() {
changed = true;
if has {
mine.push(name.clone());
} else {
mine.retain(|x| x != name);
}
}
}
});
if changed {
// In the library's order, so its instructions read the same every turn.
let ordered: Vec<String> = all.iter().filter(|k| mine.contains(&k.0)).map(|k| k.0.clone()).collect();
b.skills = if ordered.len() == all.len() { None } else { Some(ordered) };
}
});
}
/// Every computer there is for this bot (name, what it is): a screen of a brain asks the brain.
fn every_computer(&self, b: &Bot) -> Vec<(String, String)> {
if self.mir.on {
return self.mir.computers.get(&b.id).and_then(|c| c["all"].as_array().or(c["names"].as_array()).cloned()).unwrap_or_default().iter().filter_map(|x| Some((x["name"].as_str()?.to_string(), x["kind"].as_str().unwrap_or("").to_string()))).collect();
}
let t = self.all_targets_for(b);
let r = self.roles_for(b, &t);
t.iter().map(|x| (x.name.clone(), mirror_ui::computer_kind(x, &r).to_string())).collect()
}
/// 可以用的電腦: its own always; of the others, the ticked ones.
pub(super) fn profile_computers(&mut self, ui: &mut egui::Ui, b: &mut Bot) {
let p = self.pal;
let own = crate::model::own_computer_name(&b.name);
let all = self.every_computer(b);
if all.len() <= 1 {
return;
}
ui.add_space(6.0);
ui.label(RichText::new(t("可以用的電腦")).size(FONT_XS).color(p.text2));
let mut on: Vec<String> = b.computers.clone().unwrap_or_else(|| all.iter().map(|c| c.0.clone()).collect());
let mut changed = false;
ui.horizontal_wrapped(|ui| {
for (name, _) in &all {
let is_own = *name == own || all.first().is_some_and(|f| f.0 == *name);
let mut has = is_own || on.contains(name);
let r = ui.add_enabled(!is_own, egui::Checkbox::new(&mut has, name.as_str()));
let r = if is_own { r.on_disabled_hover_text(t("它自己的電腦一定可以用")) } else { r };
if r.changed() {
changed = true;
if has {
on.push(name.clone());
} else {
on.retain(|x| x != name);
}
}
}
});
if changed {
let others: Vec<String> = all.iter().skip(1).map(|c| c.0.clone()).collect();
let kept: Vec<String> = others.iter().filter(|n| on.contains(n)).cloned().collect();
b.computers = if kept.len() == others.len() { None } else { Some(kept) };
// The computer it works on by default must be one of them.
let chosen = b.default_computer.trim().to_string();
let allowed = chosen.is_empty() || chosen == crate::model::BOT_COMPUTER_NAME || chosen == own || b.computers.as_ref().is_none_or(|c| c.contains(&chosen));
if !allowed {
b.default_computer = crate::model::BOT_COMPUTER_NAME.to_string();
}
}
hint(ui, "沒勾的電腦,這個 Bot 看不到也不能用;同事名冊會寫它在哪幾台做事,交辦時才知道該找誰。", p);
}
}