Files
doubao-clone/android/kernel/group_chat.py
l 2142e119c6 feat(v1.2): 阶段4 上半 —— 群聊仲裁 + invite_agent / pass_turn(附录 B 齐了)
## 点名制仲裁(需求 §3.9)

新增 `kernel/group_chat.py`。优先级从高到低:

    ① 用户 @ 的人              用户主权,而且**不受"一轮一次"限制**
    ② AI 上一轮 pass_turn 的人  一轮最多一次
    ③ 主智能体(participants[0])兜底,永不冷场

死亡的人接不了 → **回主智能体**(需求明文)。不回的话用户会看到"没人应"。

★ 为什么单独一个模块:仲裁是**纯逻辑**(给一群人和几个标记,返回谁说话),
  没有 IO。塞在 app.py 的对话循环里就没法逐条测 —— 而它有五种分支
  (点名/移交/兜底/死亡/重复移交),正是最该测的那种。

★ `@` 解析按**最长匹配**:有"玄辰"和"玄辰二号"时,输入 `@玄辰二号`
  不该命中"玄辰"。

## 两个工具(附录 B 的最后两个)

`invite_agent(target)` —— 拉起同世界观的智能体。单聊拉人时**自动转成群聊**
(不转的话 `type` 还是 single,仲裁逻辑根本不会跑)。

按智能体配置分流(§6.1 的「拉起其他智能体:静默 / 需确认」)。

★ 关于"需确认":需求写的是"弹窗询问"。真正的系统弹窗要等界面通道
  (`ask_user` 那套),这一版先退成"让 AI 在正文里问用户一句,
  用户答应后再调一次"。**功能上等价(用户仍是否决者)**,
  差别是它经过 AI 的嘴而不是系统弹窗 —— 注释里写清楚了,
  免得以后有人以为这里已经实现了弹窗。

`pass_turn(target)` —— 移交发言权。一轮最多一次;交给自己/死人都拒。

## ★★ 抓到两个"会静默失效"的字段表缺项

· `inviteMode` 不在 `AGENT_TEXT_FIELDS` 里 → 界面上配了存不进去
· `contextWindow` 同理

这和上一轮的 `rulePackId` 是**同一个坑**(字段表是白名单,不加就丢)。
已经把 v1.2 新增的三个字段一起补上,并在注释里写明了这个陷阱。

## ★ 抓到 `push_context` 的语义陷阱

`pass_turn` 里原本用 `tools_runtime.push_context(handoffTo=…, handoffUsed=True)`
记移交标记 —— 但 `push_context` 是**替换**语义,它把 `agentId` /
`conversationId` 一起冲掉了,接着同一个工具里的 `_me()` 就炸
(表现是"拿不到当前智能体",而报错的地方离真正的原因隔着好几步)。

新增 `update_context(**kw)`(合并),并把两者的区别写进了 docstring。

## 验证

新增 `scripts/probe-group-chat.py`,7 节 40+ 项:会话字段、仲裁五种分支、
`@` 最长匹配、成员增删(主智能体不能移出)、`invite_agent`(跨世界观拒 /
拉自己拒 / 需确认不直接拉人 / 静默直接进)、`pass_turn`(一轮一次 /
死亡拒 / 交给自己拒)、附录 B 的 11 个工具清单核对。

★ 探针里一条**过时的断言**红了:`probe-rule-engine` 写着"邀请/移交还没加
(阶段4)"—— 阶段 4 一落地它就该红。已更新。

八个探针全绿;构建通过。
2026-10-16 18:30:21 +08:00

169 lines
5.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""群聊的发言权仲裁(需求 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