fix(group): @ 谁就该谁答 + 背景跟对话走 + talk_to 工具(v2.0.74)
用户连着报了五条,其实根子集中在两处: 1. 「@ 的时候立即切换」「背景和智能体对不上」「声音对不上」 —— 三个症状,一个根因:**仲裁在前、落库在后**。 群聊仲裁要读「用户这一轮 @ 了谁」,但本次这条消息还没写进库, 于是拿 raw_history 里最后一条 user(= **上一次**说的话)去匹配, 没匹配到就兜底给主智能体 → 我 @ 寐眠,回答的却是小黑。 消息的 agentId 是小黑 → 名字标签是小黑、朗读音色也是小黑, 而 AI 自述是寐眠、背景又跟着 @ 切了 → 三处全对不上。 修:仲裁用**本条**消息(autoContinue 那几轮没有新消息,才回退历史)。 2. 「背景是根据当前对话来的,不是让你 @ 谁就换哪个背景」 背景层原来跟 currentSpeakerId 走,是我理解反了。 背景表达的是「这个场景是谁的」(和顶栏同一个来源), 改成 activeAgent —— 和顶栏天然一致。 「谁在说话」是**每条消息**自己的事(msg.agentId),两者不是一回事。 3. 新增 talk_to 工具(用户要求:智能体主动调用工具和别的智能体聊, 参数就是「智能体 + 内容」) pass_turn 只交发言权、**不带内容** —— 对方接到一片空白,聊不起来。 talk_to 把话落进对话(发言者是本人)再把发言权交出去, 界面据此自动续一轮让对方回应。单聊/空内容/一轮两次都会被拒。 顺带:_handle_chat 给自动续轮放行空消息(原来每一轮都被打回 400, 界面上是一屏「出错了:消息为空」)。 探针:group-chat 加了第 14~17 节共 20 条断言。八个探针全绿。
This commit is contained in:
@@ -187,10 +187,12 @@ def main():
|
||||
print("\n=== 7. 附录 B 的工具清单齐了吗 ===")
|
||||
for want in ('memory_add', 'memory_update', 'memory_forget', 'memory_search',
|
||||
'emotion_set', 'get_token_usage', 'change_stat', 'grant_status',
|
||||
'invite_agent', 'pass_turn'):
|
||||
'invite_agent', 'pass_turn', 'talk_to'):
|
||||
check(f"有 {want}", want in agent_tools.AGENT_TOOLS)
|
||||
# 附录 B 列了 10 个工具,外加"建议机制"(memory_suggest_summary)= 11
|
||||
check("一共 11 个(附录 B 的 10 个 + 建议机制)", len(agent_tools.AGENT_TOOLS) == 11,
|
||||
# 附录 B 列了 10 个,外加"建议机制"(memory_suggest_summary)
|
||||
# 和新增的 talk_to(用户要求:智能体主动找另一个智能体说话)= 12
|
||||
check("一共 12 个(附录 B 的 10 个 + 建议机制 + talk_to)",
|
||||
len(agent_tools.AGENT_TOOLS) == 12,
|
||||
str(sorted(agent_tools.AGENT_TOOLS)))
|
||||
|
||||
print("\n=== 8. ★★ 走真实路径测 pass_turn(旧探针是假通过)===")
|
||||
@@ -373,6 +375,133 @@ def main():
|
||||
"if not auto_continue:" in _app,
|
||||
"append_messages 没被 auto_continue 包住")
|
||||
|
||||
print("\n=== 15. 轮中移交要告诉界面(否则两个 AI 永远聊不起来)===")
|
||||
#
|
||||
# ★★ 真机反馈第二版:「还是不能互相对话」。
|
||||
#
|
||||
# 第一版是靠 `speaker` 事件的 `reason == 'handoff'` 判断的 —— 但那个
|
||||
# 事件是**轮前**发的,而 `pass_turn` 是**轮中**才调的。最常见的剧本是:
|
||||
# ① 用户没点名(或点名的人不在场)→ 兜底给主智能体,reason = "host"
|
||||
# ② 主智能体说完话,轮中调 `pass_turn` 让给别人
|
||||
# 界面看到的 reason 是 "host",就以为没人移交,**不再续轮** ——
|
||||
# 被让的那个人永远开不了口。
|
||||
#
|
||||
# 所以轮末必须补一个 `handoff` 事件。这条同样对源码断言(要真跑模型)。
|
||||
check("轮末会 emit `handoff`(判据是**轮中**的移交状态,不是轮前的 reason)",
|
||||
"'type': 'handoff'" in _app,
|
||||
"缺这个事件 → 界面不知道 AI 中途交了话 → 两个 AI 永远聊不起来")
|
||||
check("判据取自 tools_runtime 的轮次状态(不是线程局部变量)",
|
||||
"tools_runtime.handoff_state()" in _app,
|
||||
"handoff_state 没被读 —— pass_turn 那条链路是断的")
|
||||
|
||||
print("\n=== 16. 「让他们聊」:界面直接指定发言者 ===")
|
||||
#
|
||||
# ★★ 真机反馈第三版:还是聊不起来。
|
||||
#
|
||||
# 原因很实在:**AI 并不总会调 `pass_turn`** —— 它更愿意自己把话说完。
|
||||
# 所以"等它自觉移交"这条路靠不住。用户要的是一个能按下去的入口:
|
||||
# 点「让他们聊」,界面就一轮一轮把发言权在两个人之间递过去
|
||||
# (`forceSpeaker`),完全不依赖 AI 配合。
|
||||
gconv = store.create_conversation(
|
||||
title='对聊测试', agent_id=a['id'], kind='group', participants=[a['id'], b['id']],
|
||||
)
|
||||
r = G.resolve(gconv, forced=b['id'])
|
||||
check("forced 直接生效(用户没点名时)", r['speaker'] == b['id'] and r['reason'] == 'forced', str(r))
|
||||
r = G.resolve(gconv, want=b['id'], forced=a['id'])
|
||||
check("**用户 @ 压过 forced**(「@ 的时候立即切换」——自动对聊必须让位)",
|
||||
r['speaker'] == b['id'] and r['reason'] == 'mention', str(r))
|
||||
r = G.resolve(gconv, forced='不存在的人')
|
||||
check("forced 指定的人不在场 → 回主智能体(不抛异常)",
|
||||
r['speaker'] == a['id'], str(r))
|
||||
r = G.resolve(gconv, want=b['id'])
|
||||
check("不传 forced 时用户 @ 仍然优先(原有行为没被改坏)",
|
||||
r['speaker'] == b['id'] and r['reason'] == 'mention', str(r))
|
||||
r = G.resolve(gconv)
|
||||
check("什么都不传 → 兜底主智能体(永不冷场)",
|
||||
r['speaker'] == a['id'] and r['reason'] == 'host', str(r))
|
||||
|
||||
# ★★ 自动续轮**没有用户消息**,所以 `_handle_chat` 开头那条"消息为空"
|
||||
# 的校验必须给它放行 —— 否则每一轮都被打回 400,
|
||||
# 界面上是一屏「出错了:消息为空」,用户看到的就是"还是不能互相对话"。
|
||||
# (真机现场截图就是这么六条错误。)
|
||||
check("`_handle_chat` 给自动续轮放行空消息",
|
||||
"not auto_continue" in _app and "bool(body.get('forceSpeaker'))" in _app,
|
||||
"空消息校验没放行 autoContinue/forceSpeaker")
|
||||
# 两边判据必须一致,不然一边放行一边往里塞空用户消息
|
||||
check("两处判据一致(校验处 与 追加消息处 用同一个条件)",
|
||||
_app.count("bool(body.get('autoContinue')) or bool(body.get('forceSpeaker'))") >= 2,
|
||||
"两处条件不一致 —— 对话里会冒出一堆空条目")
|
||||
|
||||
print("\n=== 17. talk_to:智能体主动找另一个智能体说话 ===")
|
||||
#
|
||||
# ★★ 用户的原话:「不是这样聊是智能体主动调用工具和别的智能体聊」
|
||||
# 「工具定义 / 智能体加上内容」。
|
||||
#
|
||||
# 所以这个工具的**参数就是那两样**:跟谁说 + 说什么。
|
||||
# 它和 `pass_turn` 的分工是关键:那个只交发言权、**不带内容** ——
|
||||
# 对方接到的是一片空白,只能凭上下文猜,聊不起来。
|
||||
entry = agent_tools.AGENT_TOOLS.get('talk_to')
|
||||
check("工具注册了(AGENT_TOOLS 里能找到)", entry is not None,
|
||||
str(sorted(agent_tools.AGENT_TOOLS)))
|
||||
if entry:
|
||||
props = entry['spec'].get('parameters', {}).get('properties', {})
|
||||
check("参数就是「智能体 + 内容」", set(props) == {'agent', 'content'}, str(list(props)))
|
||||
check("两个都是必填",
|
||||
set(entry['spec']['parameters'].get('required') or []) == {'agent', 'content'},
|
||||
str(entry['spec']['parameters'].get('required')))
|
||||
# ★ 模型要能看见它 —— AGENT_TOOLS 会被合并进 BUILTIN_TOOLS 才是真注册
|
||||
import builtin_tools
|
||||
check("模型真的能看到它(合并进了 BUILTIN_TOOLS)",
|
||||
'talk_to' in builtin_tools.BUILTIN_TOOLS, "没合并进工具表")
|
||||
|
||||
# 单聊里没有"别人"可聊。
|
||||
# ★ 断言看**返回值**,不是等它抛 —— `real_call` 走 `execute_tool`,
|
||||
# 那一层会把工具抛的异常捕获成 `{'ok': False, 'error': ...}`。
|
||||
# (直接调 `agent_tools.talk_to` 才会抛,但那样测的就不是真实路径了。)
|
||||
#
|
||||
# ★★ 必须**新建**一条:上面第 5 节测 `invite_agent` 时把原来的
|
||||
# `single` 拉人拉成了群聊(`invite_agent` 会把 type 改成 group)——
|
||||
# 拿它当"单聊"用,断言必然失败。这种"用例被前面的测试改脏了"
|
||||
# 很隐蔽:断言的错看着像代码的错。
|
||||
lonely = store.create_conversation('真的单聊', agent_id=a['id'])
|
||||
tools_runtime.push_context(agentId=a['id'], conversationId=lonely['id'], toolName='talk_to')
|
||||
try:
|
||||
r = tools_runtime.execute_tool('talk_to', {'agent': '仙辰', 'content': '在吗'},
|
||||
store.read_settings())
|
||||
finally:
|
||||
tools_runtime.clear_context()
|
||||
check("单聊里会被拒(没有别人可以聊)",
|
||||
r.get('ok') is False and '不是群聊' in str(r.get('error')), str(r))
|
||||
|
||||
# 群聊里:话要落进对话 + 发言权交给对方
|
||||
tools_runtime.begin_round()
|
||||
before = len((store.get_conversation(c2['id']) or {}).get('messages') or [])
|
||||
r = real_call('talk_to', {'agent': '仙辰', 'content': '你刚才那句话是什么意思?'})
|
||||
check("群聊里调用成功", r.get('ok') is True, str(r))
|
||||
msgs = (store.get_conversation(c2['id']) or {}).get('messages') or []
|
||||
check("**说的话真的进了对话**(`pass_turn` 就没有这一步)",
|
||||
len(msgs) == before + 1 and '什么意思' in (msgs[-1].get('content') or ''),
|
||||
str([m.get('content') for m in msgs[-2:]]))
|
||||
check("那条消息的发言者**是我**", msgs[-1].get('agentId') == a['id'],
|
||||
str(msgs[-1].get('agentId')))
|
||||
check("**发言权交给了对方**(界面据此自动续一轮让他回应)",
|
||||
tools_runtime.handoff_state().get('to') == b['id'],
|
||||
str(tools_runtime.handoff_state()))
|
||||
|
||||
# 一轮最多一次:既 pass_turn 又 talk_to 要被拦住
|
||||
tools_runtime.begin_round()
|
||||
real_call('pass_turn', {'target': '仙辰'})
|
||||
r = real_call('talk_to', {'agent': '仙辰', 'content': '再说一句'})
|
||||
check("一轮里不能又移交又找人聊",
|
||||
r.get('ok') is False and '已经' in str(r.get('error')), str(r))
|
||||
|
||||
# 内容不能为空
|
||||
tools_runtime.begin_round()
|
||||
r = real_call('talk_to', {'agent': '仙辰', 'content': ' '})
|
||||
check("空内容会被拒(内容是必填的)",
|
||||
r.get('ok') is False and ('content' in str(r.get('error')) or '什么' in str(r.get('error'))),
|
||||
str(r))
|
||||
|
||||
print()
|
||||
if fails:
|
||||
print(f"❌ {len(fails)} 项不合格:")
|
||||
|
||||
Reference in New Issue
Block a user