// Bot 設定 › 個人檔案: who the bot is (its soul and 指示 — one text —, what it is good at, what // it leaves to others), what it may do (能力), the skills it has and the computers it may use; // and the same from a chat: a bot looks at its own profile (my_profile) and changes it when the // person asks (update_my_profile — shown to the person as a card first). 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 與指示 (one text), 擅長, 不找它做的事. 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.instructions).hint_text(t("例如:我是細心的研究員,只相信官方來源;先講結論、再給證據,最多三段;不確定就說不確定,不硬猜。")).desired_rows(4).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| { let mut on: Vec = b.abilities.clone().unwrap_or_else(|| crate::profile::ABILITIES.iter().map(|a| a.0.to_string()).collect()); let summary = format!("{} / {} {}", crate::profile::ABILITIES.iter().filter(|a| on.iter().any(|o| o == a.0)).count(), crate::profile::ABILITIES.len(), t("類")); if !fold_title(ui, p, ("abilities", b.id), "能力", &summary, "這個 Bot 可以用哪幾類工具。沒勾的不會提供給它,也不會寫進它的說明:每一步都比較省 token,也比較安全。記憶、技能、問你問題、傳檔案到聊天每個 Bot 都有。") { return; } 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| { let mut mine: Vec = b.skills.clone().unwrap_or_else(|| all.iter().map(|k| k.0.clone()).collect()); let summary = format!("{} {} / {}", t("會"), all.iter().filter(|k| mine.contains(&k.0)).count(), all.len()); if !fold_title(ui, p, ("skills", b.id), "技能", &summary, "從技能庫勾選這個 Bot 會的技能。它的說明裡只列這些,用到時才讀完整做法。它自己學會(存成)的技能會自動加進來。") { return; } 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 = 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. pub(super) 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_SM).color(p.text2)); let mut on: Vec = 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 = all.iter().skip(1).map(|c| c.0.clone()).collect(); let kept: Vec = 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); } } /// One line of a setting's text for the approval card. fn brief(text: &str) -> String { let t: String = text.split_whitespace().collect::>().join(" "); if t.is_empty() { "(空)".into() } else if t.chars().count() > 60 { format!("「{}…」", t.chars().take(60).collect::()) } else { format!("「{t}」") } } impl App { /// Who is talking in a conversation: the bot itself, or in a group the member speaking now. pub(super) fn speaking_bot(&self, conv: u64) -> u64 { self.state .bot(conv) .filter(|b| !b.members.is_empty()) .and_then(|b| b.messages.iter().rev().find(|m| m.role == Role::Assistant).map(|m| m.author)) .filter(|a| *a != 0) .unwrap_or(conv) } /// my_profile: everything about the bot's own settings, in words. pub(super) fn my_profile_text(&self, conv: u64) -> String { let me = self.speaking_bot(conv); let Some(b) = self.state.bot(me) else { return "找不到這個 Bot".into() }; let or_none = |t: &str| if t.trim().is_empty() { "(沒寫)".to_string() } else { t.trim().to_string() }; let mut out = vec![format!("你的個人檔案(「{}」):", b.name)]; out.push(format!("- 描述(其他 Bot 看得到):{}", or_none(&b.description))); out.push(format!("- Soul 與指示(只有你自己讀):{}", or_none(&b.instructions))); out.push(format!("- 擅長(其他 Bot 看得到):{}", or_none(&b.strengths))); out.push(format!("- 不找你做的事(其他 Bot 看得到):{}", or_none(&b.not_for))); type Ability = (&'static str, &'static str, &'static str); let (on, off): (Vec<&Ability>, Vec<&Ability>) = crate::profile::ABILITIES.iter().partition(|a| crate::profile::has(b, a.0)); out.push(format!("- 能力(有):{}", on.iter().map(|a| format!("{}〔{}〕", a.1, a.0)).collect::>().join("、"))); if !off.is_empty() { out.push(format!("- 能力(沒有):{}", off.iter().map(|a| format!("{}〔{}〕", a.1, a.0)).collect::>().join("、"))); } let library: Vec<&str> = self.state.skills.iter().filter(|k| k.enabled).map(|k| k.name.as_str()).collect(); let (mine, rest): (Vec<&str>, Vec<&str>) = library.iter().partition(|n| crate::profile::has_skill(b, n)); out.push(format!("- 技能(會 {} 個):{}", mine.len(), if mine.is_empty() { "(沒有)".to_string() } else { mine.join("、") })); if !rest.is_empty() { out.push(format!("- 技能庫裡你還不會的:{}", rest.join("、"))); } let every: Vec = self.every_computer(b).into_iter().map(|c| c.0).collect(); let may: Vec = self.targets_for(b).iter().map(|t| t.name.clone()).collect(); let roles = self.roles_for(b, &self.targets_for(b)); out.push(format!("- 可以用的電腦:{}(預設:{})", may.join("、"), Self::chosen_computer(b, &self.targets_for(b), &roles).0)); let not_yet: Vec<&String> = every.iter().filter(|c| !may.contains(c)).collect(); if !not_yet.is_empty() { out.push(format!("- 沒開放給你的電腦:{}", not_yet.iter().map(|c| c.as_str()).collect::>().join("、"))); } let thinking = providers::EFFORTS.iter().find(|e| e.0 == b.thinking).map(|e| e.1).unwrap_or("自動"); let step = match b.step_model.trim() { "" => "自動".to_string(), "off" => "不用(每一步都用主模型)".to_string(), m => m.to_string(), }; out.push(format!("- 模型:{} · {};思考模式:{thinking};執行步驟用的模型:{step}", b.provider.label(), b.model)); out.push(format!("- 頭像:{} · {}", b.shape, b.color)); out.push("要改就用 update_my_profile(只帶要改的欄位);使用者會先看到確認卡片,按允許才生效。".into()); out.join("\n") } /// update_my_profile, worked out against the bot as it is now: the bot as it would be and the /// changes in words (for the approval card) — or what is wrong with the request. pub(super) fn profile_change(&self, conv: u64, args: &str) -> Result<(u64, Bot, Vec), String> { let me = self.speaking_bot(conv); if self.state.bot(me).is_some_and(|b| b.branch_of != 0) { return Err(appctl::BRANCH_NO_SETTINGS.into()); } // 同事之間: what another bot asked for isn't the person's word. let started_by = self.state.bot(conv).and_then(|b| b.messages.iter().rev().find(|m| m.role == Role::User && !m.steer && !m.interrupted)).map(|m| m.from_bot).unwrap_or(0); if started_by != 0 { let who = self.state.bot(started_by).map(|b| b.name.clone()).unwrap_or_else(|| "另一個 Bot".into()); return Err(format!("沒有改:這一輪是「{who}」叫你做的,不是使用者本人。要改設定,請使用者自己跟你說。")); } let old = self.state.bot(me).cloned().ok_or("找不到這個 Bot")?; let v: serde_json::Value = serde_json::from_str(if args.trim().is_empty() { "{}" } else { args }).map_err(|_| "設定的格式看不懂".to_string())?; let text = |k: &str| v[k].as_str().map(|s| s.trim().to_string()); let list = |k: &str| -> Vec { v[k].as_array().into_iter().flatten().filter_map(|x| x.as_str()).map(|x| x.trim().to_string()).filter(|x| !x.is_empty()).collect() }; let mut b = old.clone(); let mut lines: Vec = vec![]; let mut set = |label: &str, slot: &mut String, new: Option| { if let Some(n) = new.filter(|n| n != slot.trim()) { lines.push(format!("{label}:{} → {}", brief(slot), brief(&n))); *slot = n; } }; if let Some(n) = text("name").filter(|n| !n.is_empty()) { if self.state.bots.iter().any(|x| x.id != me && x.name == n) { return Err(format!("已經有叫「{n}」的 Bot 了")); } set("名稱", &mut b.name, Some(n)); } set("描述", &mut b.description, text("description")); set("Soul 與指示", &mut b.instructions, text("soul").or_else(|| text("instructions"))); set("擅長", &mut b.strengths, text("strengths")); set("不找它做的事", &mut b.not_for, text("not_for")); // 能力 let (on, off) = (list("abilities_on"), list("abilities_off")); if !on.is_empty() || !off.is_empty() { let mut keys: Vec = crate::profile::ABILITIES.iter().map(|a| a.0).filter(|k| crate::profile::has(&old, k)).map(String::from).collect(); for w in &on { let k = crate::profile::ability_key(w).ok_or_else(|| format!("沒有「{w}」這個能力(可以用:{})", crate::profile::ABILITIES.iter().map(|a| a.0).collect::>().join("、")))?; if !keys.iter().any(|x| x == k) { keys.push(k.to_string()); lines.push(format!("能力:打開「{}」", crate::profile::label(k))); } } for w in &off { let k = crate::profile::ability_key(w).ok_or_else(|| format!("沒有「{w}」這個能力"))?; if keys.iter().any(|x| x == k) { keys.retain(|x| x != k); lines.push(format!("能力:關掉「{}」", crate::profile::label(k))); } } b.abilities = if crate::profile::ABILITIES.iter().all(|a| keys.iter().any(|x| x == a.0)) { None } else { Some(crate::profile::ABILITIES.iter().map(|a| a.0.to_string()).filter(|k| keys.contains(k)).collect()) }; } // 技能 let (add, remove) = (list("skills_add"), list("skills_remove")); if !add.is_empty() || !remove.is_empty() { let library: Vec = self.state.skills.iter().filter(|k| k.enabled).map(|k| k.name.clone()).collect(); let mut mine: Vec = library.iter().filter(|n| crate::profile::has_skill(&old, n)).cloned().collect(); for n in &add { let k = library.iter().find(|x| x.eq_ignore_ascii_case(n)).ok_or_else(|| format!("技能庫裡沒有「{n}」"))?; if !mine.contains(k) { mine.push(k.clone()); lines.push(format!("技能:學會「{k}」")); } } for n in &remove { if let Some(k) = mine.iter().find(|x| x.eq_ignore_ascii_case(n)).cloned() { mine.retain(|x| *x != k); lines.push(format!("技能:不再用「{k}」")); } } let ordered: Vec = library.iter().filter(|n| mine.contains(n)).cloned().collect(); b.skills = if ordered.len() == library.len() { None } else { Some(ordered) }; } // 可以用的電腦 let own = crate::model::own_computer_name(&old.name); let (add, remove) = (list("computers_add"), list("computers_remove")); if !add.is_empty() || !remove.is_empty() { let every: Vec = self.every_computer(&old).into_iter().map(|c| c.0).filter(|n| *n != own).collect(); let mut may: Vec = every.iter().filter(|n| old.computers.as_ref().is_none_or(|c| c.contains(n))).cloned().collect(); for n in &add { let c = every.iter().find(|x| x.eq_ignore_ascii_case(n)).ok_or_else(|| format!("沒有「{n}」這台電腦(有:{})", every.join("、")))?; if !may.contains(c) { may.push(c.clone()); lines.push(format!("電腦:可以用「{c}」")); } } for n in &remove { if n == &own { return Err("你自己的電腦一定可以用,不能拿掉".into()); } if let Some(c) = may.iter().find(|x| x.eq_ignore_ascii_case(n)).cloned() { may.retain(|x| *x != c); lines.push(format!("電腦:不再用「{c}」")); } } b.computers = if every.iter().all(|c| may.contains(c)) { None } else { Some(every.iter().filter(|c| may.contains(c)).cloned().collect()) }; } if let Some(c) = text("default_computer").filter(|c| !c.is_empty()) { let may: Vec = self.targets_for(&b).iter().map(|t| t.name.clone()).collect(); let c = may.iter().find(|x| x.eq_ignore_ascii_case(&c)).cloned().ok_or_else(|| format!("「{c}」不是你可以用的電腦(可以用:{})", may.join("、")))?; let keep = Self::computer_to_keep(&b, &c); if keep != b.default_computer { lines.push(format!("預設電腦:→「{c}」")); b.default_computer = keep; } } if let Some(c) = &b.computers { // A default it may no longer use goes back to its own computer. let d = b.default_computer.trim().to_string(); if !d.is_empty() && d != crate::model::BOT_COMPUTER_NAME && d != own && !c.contains(&d) { b.default_computer = crate::model::BOT_COMPUTER_NAME.to_string(); lines.push("預設電腦:→ 它自己的電腦".into()); } } // 模型 if let Some(p) = text("provider").filter(|p| !p.is_empty()) { let k = crate::appctl::provider_of(&p).or_else(|| crate::providers::ALL_PROVIDERS.iter().copied().find(|x| x.label() == p)).ok_or_else(|| format!("沒有「{p}」這個供應商"))?; if k != b.provider { lines.push(format!("AI 供應商:{} → {}", b.provider.label(), k.label())); b.provider = k; if text("model").is_none() { b.model = k.suggested_model().into(); } } } if let Some(m) = text("model").filter(|m| !m.is_empty() && *m != b.model) { lines.push(format!("模型:{} → {m}", b.model)); b.model = m; } if let Some(t) = text("thinking") { let t = if matches!(t.as_str(), "auto" | "自動") { String::new() } else { t }; if !providers::EFFORTS.iter().any(|e| e.0 == t) { return Err("思考模式只有:\"\"(自動)、low、medium、high".into()); } if t != b.thinking { let name = |x: &str| providers::EFFORTS.iter().find(|e| e.0 == x).map(|e| e.1).unwrap_or("自動").to_string(); lines.push(format!("思考模式:{} → {}", name(&b.thinking), name(&t))); b.thinking = t; } } if let Some(m) = text("step_model").filter(|m| *m != b.step_model) { // In the editor's words: "" is 自動, "off" is 不分開 (the card read 「(空) → 「off」」). let name = |x: &str| match x.trim() { "" => "自動".to_string(), "off" => "不分開".to_string(), m => m.to_string(), }; lines.push(format!("執行步驟用的模型:{} → {}", name(&b.step_model), name(&m))); b.step_model = m; } if let Some(c) = text("color").filter(|c| *c != b.color) { let ok = crate::avatar::COLORS.iter().any(|x| x.0 == c) || (c.starts_with('#') && c.len() == 7 && c[1..].chars().all(|ch| ch.is_ascii_hexdigit())); if !ok { return Err(format!("沒有「{c}」這個顏色")); } lines.push(format!("頭像顏色:{} → {c}", b.color)); b.color = c; } if let Some(sh) = text("shape").filter(|sh| *sh != b.shape) { if !crate::avatar::SHAPES.contains(&sh.as_str()) { return Err(format!("沒有「{sh}」這個頭像形狀")); } lines.push(format!("頭像形狀:{} → {sh}", b.shape)); b.shape = sh; } if lines.is_empty() { return Err("沒有要改的設定(和現在一樣)。先用 my_profile 看現在的設定。".into()); } Ok((me, b, lines)) } /// update_my_profile after the person said yes: worked out again against the bot as it is now /// (it kept talking meanwhile) and only its settings taken over. pub(super) fn apply_profile_change(&mut self, conv: u64, args: &str) -> appctl::Outcome { let done = |text: String, ok: bool| appctl::Outcome { text, ok, wake: vec![], theme_changed: false, page: None }; match self.profile_change(conv, args) { Err(e) => done(e, false), Ok((me, new, lines)) => { if let Some(b) = self.state.bot_mut(me) { b.name = new.name; b.description = new.description; b.instructions = new.instructions; b.strengths = new.strengths; b.not_for = new.not_for; b.abilities = new.abilities; b.skills = new.skills; b.computers = new.computers; b.default_computer = new.default_computer; b.provider = new.provider; b.model = new.model; b.thinking = new.thinking; b.step_model = new.step_model; b.color = new.color; b.shape = new.shape; } self.mark(); done(format!("已更新你的設定(能力、技能、模型從下一則訊息起生效):\n- {}", lines.join("\n- ")), true) } } } }