"""群聊的发言权仲裁(需求 v1.2 §3.9)。 ## 点名制 需求原话: > **发言权仲裁:点名制**。被 @ 者发言;AI 可通过 `pass_turn` 指定下一位 > 发言者,**一轮最多一次移交**;无人被点名时由主智能体(会话发起者)兜底, > **永不冷场**。 翻译成优先级(从高到低): ① 用户 @ 的人 用户主权,最高 ② AI 上一轮 pass_turn 的人 一轮最多一次 ③ 主智能体(participants[0])兜底,永不冷场 ## 死亡的人不能接 > 死亡智能体被点名时移交无效,发言权回主智能体。 这条单独写出来,是因为"移交无效"有三种可能的处置: 报错、随机选一个、回主智能体 —— 需求选了第三种。 **回主智能体**也对:它会接着把话头圆下去,用户不会看到"没人应"。 ## 为什么单独一个模块 仲裁是**纯逻辑**(给一群人和几个标记,返回谁说话),没有 IO。 放在 `app.py` 的对话循环里就没法逐条测 —— 而它的分支(点名/移交/ 兜底/死亡/不在场)有五种,正是最该测的那种。 """ #: 一轮最多移交一次。需求明文,所以做成一个常量而不是散在代码里的布尔。 MAX_HANDOFF_PER_ROUND = 1 def member_ids(conv: dict) -> list: """群聊的成员 id 列表(顺序就是加入顺序,**第 0 个是主智能体**)""" conv = conv or {} ids = conv.get('participants') if not isinstance(ids, list): return [] return [str(x) for x in ids if str(x or '').strip()] def is_group(conv: dict) -> bool: return (conv or {}).get('type') == 'group' def host_id(conv: dict) -> str: """主智能体 = 会话发起者 = 成员列表的第一个(需求:"发起者")""" ids = member_ids(conv) return ids[0] if ids else (conv or {}).get('agentId') or '' def mention_in(text: str, names: list) -> str: """ 从用户输入里找出被 @ 的人(返回 id,没有就空串)。 ★ 按**名字**匹配而不是 id:界面上的"成员快捷条"插进去的就是 `@玄辰` (用户看到的是名字)。重名时取**最长的那个**匹配 —— 有"玄辰"和"玄辰二号"时,输入 `@玄辰二号` 不该命中"玄辰"。 """ t = str(text or '') hit = '' best = '' for aid, name in (names or []): n = str(name or '').strip() if not n or f'@{n}' not in t: continue if len(n) > len(best): best, hit = n, aid return hit def resolve(conv: dict, want: str = '', passed: str = '', alive: dict = None, handoffs: int = 0) -> dict: """ 决定这一轮谁发言。 [want] 用户 @ 出来的人(`mention_in` 的结果) [passed] AI 上一轮 `pass_turn` 指定的人 [alive] `{agent_id: bool}`,缺省视为活着 [handoffs] 这一轮已经移交过几次(用于"一轮最多一次") 返回 `{'speaker': id, 'reason': 'mention'|'handoff'|'host'|'handoff_denied', 'note': ''}`。 """ conv = conv or {} alive = alive or {} ids = member_ids(conv) host = host_id(conv) def ok(aid: str) -> bool: return bool(aid) and aid in ids and alive.get(aid, True) # ① 用户 @ 的人(用户主权,不受"一轮一次"约束 —— 那条只管 AI 的移交) if want: if ok(want): return {'speaker': want, 'reason': 'mention', 'note': ''} # 被 @ 的人死了或不在场:回主智能体,而不是报错 return {'speaker': host, 'reason': 'host', 'note': '被点的人不在场或已死亡,发言权回主智能体'} # ② AI 的移交(一轮最多一次) if passed: if handoffs >= MAX_HANDOFF_PER_ROUND: return {'speaker': host, 'reason': 'handoff_denied', 'note': '这一轮已经移交过了,不再移交'} if ok(passed): return {'speaker': passed, 'reason': 'handoff', 'note': ''} # ★ 死亡的人被点名时**移交无效**,发言权回主智能体(需求明文) return {'speaker': host, 'reason': 'host', 'note': f'「{passed}」已死亡或不在场,移交无效,发言权回主智能体'} # ③ 兜底:永不冷场 return {'speaker': host, 'reason': 'host', 'note': ''} def add_member(conv: dict, agent_id: str) -> bool: """ 加一个成员(用户手动增删**无条件允许** —— 用户主权)。 ★ 只有群聊才谈得上"加成员"。单聊要拉人 → 那个会话应该**转成群聊** (`promote`),而不是偷偷塞进 participants 里 —— 不转的话 `type` 还是 single,仲裁逻辑根本不会跑。 """ c = conv or {} aid = str(agent_id or '').strip() if not aid: return False ids = member_ids(c) if aid in ids: return False ids.append(aid) c['participants'] = ids return True def remove_member(conv: dict, agent_id: str) -> bool: """ 移出一个成员。**主智能体不能被移出**(需求说发言权兜底靠它 —— 移掉的话这个群聊就没有兜底了,会冷场)。 """ c = conv or {} aid = str(agent_id or '').strip() ids = member_ids(c) if aid not in ids or aid == host_id(c): return False ids.remove(aid) c['participants'] = ids return True def promote(conv: dict, agent_id: str) -> None: """把单聊转成群聊(拉第二个人进来时用)""" c = conv or {} if c.get('type') != 'group': first = (c.get('agentId') or '').strip() c['type'] = 'group' c['participants'] = [x for x in [first] if x] add_member(c, agent_id) def describe(conv: dict) -> str: """给界面/日志的一行话(不要各处自己拼)""" c = conv or {} n = len(member_ids(c)) kind = {'group': '群聊', 'private': '私聊记录'}.get(c.get('type') or '', '单聊') return f'{kind}({n} 人)' if n > 1 else kind