// 群組聊天 @點名: 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 = 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 = 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![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()); } }