AnyBot/src/app/group.rs

86 lines
4.1 KiB
Rust
Raw Normal View History

Bot 之間溝通:ask_bot 問同事並等回答、群組 @點名、共用頁面/「關於你」記憶/教技能;修正插話丟附件、Enter 核准失效等 問同事(ask_bot) - A 問 B 並等它回答:問題放進 B 自己的聊天,B 用自己的模型、工具、記憶和電腦做,最後的回覆交回 A - 最多轉 3 手、不能互等(避免卡死)、對方忙就排隊(10 分鐘逾時)、停止 A 時 B 也停 - message_bot 也加轉手上限;別的 Bot 叫的不算使用者的話(改不了自己的設定) 群組 @點名 - 「@名字」只叫被點到的成員回答;成員在回覆裡 @ 別人就接著說(一輪最多 8 則) - 電腦和手機的輸入框打 @ 會列出成員 - 修正:每個成員帶自己的記憶;成員回覆的每一步都記得是誰說的 共用資源 - Bot 可以讀其他 Bot 的頁面(只能讀,改要請作者改) - 共用的「關於你」記憶:每個 Bot 都帶著,記憶頁可切換查看、編輯;外人的訊息不帶 - teach_skill:把自己會的技能教給同事(先問你);同事名冊列出每個 Bot 會的技能 修正 - 插話(Bot 工作中傳訊息)會帶附件:聊天有檔案卡片,模型看得到圖片;當機復原也帶著 - 輸入框有焦點、框是空的時 Enter 允許待核准的步驟(原本被當成空白送出吃掉) - 大腦的螢幕連不到大腦時,引用、附件、記憶不會先被用掉 - 「還原上一個答案」存在 Bot 上:重開還在,連大腦的畫面和手機都能還原;停止的回合也留著 - Bot 忙時被叫醒(例行任務、message_bot、觸發器)改成排隊開自己的一輪,不再壓成純文字插話 - 停止時結果已回來的步驟標成完成;輸入法剛選字的那一格不送出 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha3a7riWESpv471gDgC
2026-11-13 13:34:03 +00:00
// 群組聊天 @點名: the person's 「@名字」 picks who answers (start_reply); a member that writes
// 「@名字」 in its answer hands that member the word next. So that members can't keep handing it
// round, a round stops at MAX_GROUP_ANSWERS answers, and the word goes back to the person.
use super::App;
use crate::model::{Bot, Role};
/// The most answers members give between two of the person's messages.
pub(super) const MAX_GROUP_ANSWERS: usize = 8;
impl App {
/// A group's members, (id, name), as @ finds them.
pub(super) fn member_names(&self, group: &Bot) -> Vec<(u64, String)> {
group.members.iter().filter_map(|m| self.state.bot(*m)).filter(|b| b.members.is_empty()).map(|b| (b.id, b.name.clone())).collect()
}
/// A member just answered in `conv`: the members it @named answer next, in the order named,
/// ahead of the rest of the round — while the round has room.
pub(super) fn pass_the_word(&mut self, conv: u64) {
let Some(g) = self.state.bot(conv).filter(|g| !g.members.is_empty()) else { return };
let Some(last) = g.messages.iter().rev().find(|m| !m.note && m.role != Role::Tool) else { return };
if last.role != Role::Assistant || last.author == 0 {
return;
}
let author = last.author;
let named: Vec<u64> = crate::mention::mentioned(&last.text, &self.member_names(g)).into_iter().filter(|m| *m != author).collect();
if named.is_empty() {
return;
}
let spoken = self.group_spoken.get(&conv).copied().unwrap_or(0);
let q = self.group_queue.entry(conv).or_default();
let room = MAX_GROUP_ANSWERS.saturating_sub(spoken + q.len());
let next: Vec<u64> = named.into_iter().filter(|m| !q.contains(m)).take(room).collect();
for m in next.into_iter().rev() {
q.push_front(m);
}
}
}
#[cfg(test)]
mod tests {
use crate::model::Message;
/// A group of the first three bots, with the window's side ready (no model needed).
fn group() -> (super::App, u64, [u64; 3]) {
let ctx = eframe::egui::Context::default();
let mut app = super::App::new(&ctx, Some(std::env::temp_dir().join("anybot-group-at.png")));
let ids = [app.state.bots[0].id, app.state.bots[1].id, app.state.bots[2].id];
let mut g = app.state.bots[0].clone();
g.id = 9_001;
g.name = "小組".into();
g.members = ids.to_vec();
g.messages.clear();
app.state.bots.push(g);
(app, 9_001, ids)
}
#[test]
fn a_member_hands_the_word_to_the_ones_it_names() {
let (mut app, gid, [a, b, c]) = group();
let (bn, cn) = (app.state.bot(b).unwrap().name.clone(), app.state.bot(c).unwrap().name.clone());
app.group_queue.insert(gid, [c].into());
app.group_spoken.insert(gid, 1);
let say = |app: &mut super::App, who: u64, text: &str| app.state.bot_mut(gid).unwrap().messages.push(Message { author: who, ..Message::assistant(text, "") });
// A names B: B answers next, ahead of C (who was already in the round).
say(&mut app, a, &format!("我覺得可以,@{bn} 你查一下法規?"));
app.pass_the_word(gid);
assert_eq!(app.group_queue[&gid].iter().copied().collect::<Vec<_>>(), vec![b, c]);
// Naming itself or one already waiting adds nobody.
say(&mut app, b, &format!("@{bn} 我自己…@{cn}"));
app.pass_the_word(gid);
assert_eq!(app.group_queue[&gid].len(), 2);
// The round is full: the word goes back to the person.
app.group_queue.insert(gid, Default::default());
app.group_spoken.insert(gid, super::MAX_GROUP_ANSWERS);
say(&mut app, c, &format!("@{bn}"));
app.pass_the_word(gid);
assert!(app.group_queue[&gid].is_empty());
// The person's own words don't pass anything (start_reply reads those).
app.group_spoken.insert(gid, 0);
app.state.bot_mut(gid).unwrap().messages.push(Message::user(&format!("@{bn}")));
app.pass_the_word(gid);
assert!(app.group_queue[&gid].is_empty());
}
}