Files
doubao-clone/scripts/verify-meeting.mjs
l 24181cd6b6 会议模式:一手势分工、节点可重复/无限循环、停止按钮、结果单开一屏
用户反馈的四条。

【1. 节点拖不动 → 改成"一指拖节点 / 两指移画布"】
复现出来是"拖节点变成了平移画布"。查的过程比较绕,值得记:

  先后试过两种写法都不行:
    a) 节点自己 detectDragGestures + 父级平移  → 父级总是赢
    b) 节点按下就 consume 抢手势              → 还是父级赢
  最后按用户提的**手指数分工**来做,才彻底没有争抢:
    一指 → 拖手指底下那个节点(空白处则什么都不做)
    两指 → 平移画布(按在哪都行)

  ★ 中途我一度以为"拖拽根本没生效",其实是**验证方法错了**:
    测试点 (240,648) 落在空白处 —— 那个会议的节点早被拖到画布角落
    (784,712)(784=CANVAS_W-NODE_W、712=CANVAS_H-NODE_H,正是四角限位),
    跑出屏幕了。日志一打就清楚了。
    所以测试脚本现在**先删掉旧会议、新建一个**,坐标才可预测。

【2. 停止按钮】
跑起来时"运行"变红色"停止"。一场会议可能跑好几分钟(每个智能体都是一次
完整模型调用),发现配错了却停不下来最让人抓狂。
点了停止会把还在"跑"的节点标成中断,不让界面上一直转圈。

【3. 节点可重复 / 无限循环】
原来引擎用 visited 集合(跑过就不再跑),环只转一圈 —— 用户要的是
"n1 → n2 → n1 只有一轮,但**能重复,可以无限循环**"。
改成**每个节点一个「最多执行次数」**:
    1(默认)  只跑一次,有环也只转一圈 —— 和以前一样
    2~20      环来回跑对应轮数
    **0        无限循环**,靠「停止」收尾(两个智能体互相点评的用法)
节点卡片上会标 ×N / ∞,不然跑起来发现它转了三轮会很意外。

★ 敢开无限是有前提的:引擎每一轮都检查 should_stop(客户端断开或用户点停止),
  所以"无限"实际是"跑到你喊停为止"。另外兜了个很大的硬上限,
  防止用户忘了停或客户端异常断开时把额度烧光。

【4. 结果单开一屏】
节点卡片就那么点大,长回答只能看两行。新增「结果」:最终输出放最上面
(那才是用户要的),下面按**节点在图上从左到右的顺序**列各自的产出,
带「复制」按钮。按 x 坐标排而不是按事件到达顺序 —— 用户脑子里的顺序是
图上那个顺序。

【测试抓到的】
- 引擎改完漏了 `final_output = ''` 的初始化 → UnboundLocalError
- Kotlin `awaitPointerEvent` 是 AwaitPointerEventScope 的方法,不用 import
- 环用例的测试图**没有入口节点**(n1、n2 都有入边)→ 引擎直接报"没有入口节点",
  跑不起来。真实的环一定是从某个起点进去的,测试图也得有

【验证】
- verify-meeting.mjs:新增 4 个环用例(1 轮 / 3 轮 / 无限+停止 / 第二轮拿到上一轮输出),
  Node 与 Python **11/11 逐用例一致**,总共 29/29
- 模拟器实测:一指拖节点 → 节点移动、**连线跟着走**;一指拖空白 → 不动
- 全部回归:Markdown 19/19、语音 41/41 ×2、会议引擎 29/29、会议 HTTP 12/12 ×2
2026-10-10 13:58:51 +08:00

419 lines
14 KiB
JavaScript
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.

/**
* 会议模式执行引擎的验收 —— Node 与 Python 两份实现跑同一组图,结果必须一致。
*
* ★ 用**假的智能体函数**(echo / 固定回话),不打任何真实模型。
* 要验的是"图怎么走、分支怎么选、错误怎么隔离"这些引擎行为,
* 模型回答什么跟这些无关,没必要花额度也没必要联网。
*
* 跑法: node scripts/verify-meeting.mjs
*/
import { spawnSync } from 'node:child_process';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { runMeeting, judgeRule } from '../server/src/meeting.mjs';
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const PY = process.env.PYTHON || 'C:\\Python313\\python.exe';
const results = [];
const check = (name, ok, detail = '') => {
results.push({ name, ok });
console.log(` ${ok ? '✅' : '❌'} ${name}${detail ? ` → ${detail}` : ''}`);
};
/* ------------------------------------------------------------------ 图用例 */
/** 拿一个固定回话的假智能体:按节点标题回不同内容,方便验证数据怎么流 */
const echoAgent = async (node, input) => {
const tag = node.title || node.id;
return `${tag} 收到[${input}]`;
};
const cases = [
{
name: '最简单的直链:输入 → 智能体 → 输出',
meeting: {
nodes: [
{ id: 's', type: 'start', text: '原始输入' },
{ id: 'a', type: 'agent', title: '甲', agentId: '' },
{ id: 'e', type: 'end' },
],
edges: [{ from: 's', to: 'a' }, { from: 'a', to: 'e' }],
},
wantOrder: ['s', 'a', 'e'],
wantOutput: '甲 收到[原始输入]',
},
{
name: '判断走 true 分支',
meeting: {
nodes: [
{ id: 's', type: 'start', text: '里面有苹果' },
{ id: 'j', type: 'judge', judgeKind: 'contains', judgeValue: '苹果' },
{ id: 't', type: 'agent', title: '真', agentId: '' },
{ id: 'f', type: 'agent', title: '假', agentId: '' },
{ id: 'e', type: 'end' },
],
edges: [
{ from: 's', to: 'j' },
{ from: 'j', to: 't', branch: 'true' },
{ from: 'j', to: 'f', branch: 'false' },
{ from: 't', to: 'e' },
{ from: 'f', to: 'e' },
],
},
wantOrder: ['s', 'j', 't', 'e'],
wantOutput: '真 收到[里面有苹果]',
},
{
name: '判断走 false 分支',
meeting: {
nodes: [
{ id: 's', type: 'start', text: '里面有香蕉' },
{ id: 'j', type: 'judge', judgeKind: 'contains', judgeValue: '苹果' },
{ id: 't', type: 'agent', title: '真', agentId: '' },
{ id: 'f', type: 'agent', title: '假', agentId: '' },
{ id: 'e', type: 'end' },
],
edges: [
{ from: 's', to: 'j' },
{ from: 'j', to: 't', branch: 'true' },
{ from: 'j', to: 'f', branch: 'false' },
{ from: 't', to: 'e' },
{ from: 'f', to: 'e' },
],
},
wantOrder: ['s', 'j', 'f', 'e'],
wantOutput: '假 收到[里面有香蕉]',
},
{
name: '两个上游汇到一个下游(多入边拼接)',
meeting: {
nodes: [
{ id: 's', type: 'start', text: '起点' },
{ id: 'a', type: 'agent', title: '甲', agentId: '' },
{ id: 'b', type: 'agent', title: '乙', agentId: '' },
{ id: 'e', type: 'end' },
],
edges: [
{ from: 's', to: 'a' },
{ from: 's', to: 'b' },
{ from: 'a', to: 'e' },
{ from: 'b', to: 'e' },
],
},
wantHas: ['甲 收到[起点]', '乙 收到[起点]'],
},
{
name: '环不会把引擎转死(默认每节点只跑一次)',
meeting: {
nodes: [
{ id: 's', type: 'start', text: 'x' },
{ id: 'a', type: 'agent', title: '甲', agentId: '' },
{ id: 'b', type: 'agent', title: '乙', agentId: '' },
],
edges: [
{ from: 's', to: 'a' },
{ from: 'a', to: 'b' },
{ from: 'b', to: 'a' }, // 环
],
},
wantRunCount: 2, // 甲、乙各一次,不会无限循环
},
{
name: 'n1 ↔ n2 环:maxRuns=1 只走一轮',
meeting: {
nodes: [
{ id: 's', type: 'start', text: '起点' },
{ id: 'n1', type: 'agent', title: '甲', agentId: '', maxRuns: 1 },
{ id: 'n2', type: 'agent', title: '乙', agentId: '', maxRuns: 1 },
],
edges: [
{ from: 's', to: 'n1' },
{ from: 'n1', to: 'n2' },
{ from: 'n2', to: 'n1' },
],
},
wantEachRuns: { n1: 1, n2: 1 },
},
{
name: 'n1 ↔ n2 环:maxRuns=3 能来回跑三轮',
meeting: {
nodes: [
{ id: 's', type: 'start', text: '起点' },
{ id: 'n1', type: 'agent', title: '甲', agentId: '', maxRuns: 3 },
{ id: 'n2', type: 'agent', title: '乙', agentId: '', maxRuns: 3 },
],
edges: [
{ from: 's', to: 'n1' },
{ from: 'n1', to: 'n2' },
{ from: 'n2', to: 'n1' },
],
},
wantEachRuns: { n1: 3, n2: 3 },
},
{
name: 'n1 ↔ n2 环:maxRuns=0 无限循环(靠 shouldStop 收尾)',
meeting: {
nodes: [
{ id: 's', type: 'start', text: '起点' },
{ id: 'n1', type: 'agent', title: '甲', agentId: '', maxRuns: 0 },
{ id: 'n2', type: 'agent', title: '乙', agentId: '', maxRuns: 0 },
],
edges: [
{ from: 's', to: 'n1' },
{ from: 'n1', to: 'n2' },
{ from: 'n2', to: 'n1' },
],
},
// 提示跑 6 次之后停下来,模拟用户按停止
stopAfter: 6,
wantEachRuns: { n1: 3, n2: 3 },
wantAborted: true,
},
{
name: '重复时后一轮能拿到前一轮的输出',
meeting: {
nodes: [
{ id: 's', type: 'start', text: '起点' },
{ id: 'n1', type: 'agent', title: '甲', agentId: '', maxRuns: 2 },
{ id: 'n2', type: 'agent', title: '乙', agentId: '', maxRuns: 2 },
],
edges: [
{ from: 's', to: 'n1' },
{ from: 'n1', to: 'n2' },
{ from: 'n2', to: 'n1' },
],
},
// 第二轮甲收到的必须**包含乙第一轮的产出**,不能是空的 ——
// 否则"重复"就只是把同一件事做两遍,没有意义。
// 注意 n1 现在有两条入边(s 和 n2),按设计会拼接,所以还带着起点那句。
wantSecondRoundHas: '乙 收到[甲 收到[起点]]',
},
{
name: '单个节点出错不影响下游继续跑',
meeting: {
nodes: [
{ id: 's', type: 'start', text: 'x' },
{ id: 'boom', type: 'agent', title: '会炸的', agentId: '' },
{ id: 'after', type: 'agent', title: '后面', agentId: '' },
{ id: 'e', type: 'end' },
],
edges: [
{ from: 's', to: 'boom' },
{ from: 'boom', to: 'after' },
{ from: 'after', to: 'e' },
],
},
wantError: true,
wantOrder: ['s', 'boom', 'after', 'e'],
},
{
name: '没有入口节点时给明确错误而不是崩',
meeting: {
nodes: [{ id: 'a', type: 'agent', title: '甲', agentId: '' }],
edges: [{ from: 'a', to: 'a' }],
},
wantErrorText: '没有入口节点',
},
];
/* ------------------------------------------------- 跑一份实现,收集事件 */
async function runOne(impl, cs) {
const order = [];
const events = [];
let runCount = 0;
// 每个节点跑了几次 / 每次收到的输入(验"重复"用)
const perNode = {};
const inputsByNode = {};
const agentFn = async (node, input, settings, emit, nodeId) => {
runCount += 1;
perNode[nodeId] = (perNode[nodeId] || 0) + 1;
(inputsByNode[nodeId] = inputsByNode[nodeId] || []).push(input);
if (node.title === '会炸的') throw new Error('故意炸一下');
return impl === 'node'
? echoAgent(node, input)
: `${node.title || node.id} 收到[${input}]`;
};
const out = await runMeeting({
meeting: cs.meeting,
settings: {},
userInput: '',
emit: (e) => {
events.push(e);
if (e.type === 'node_start') order.push(e.nodeId);
},
runAgentFn: agentFn,
// 无限循环的用例靠这个模拟"用户按了停止"
shouldStop: cs.stopAfter ? () => runCount >= cs.stopAfter : undefined,
});
return {
order, events, runCount, perNode, inputsByNode,
output: out.output || '', error: out.error || '', aborted: !!out.aborted,
};
}
/* --------------------------------------------- Python 侧:起内核跑同一组 */
async function runPython(impl, cases) {
const script = `
import json, sys, os
sys.path.insert(0, os.path.join(${JSON.stringify(ROOT)}, 'android', 'kernel'))
import meeting as M
cases = json.loads(sys.stdin.read())
out = []
for cs in cases:
order = []
events = []
state = {'runs': 0}
per_node = {}
inputs_by_node = {}
def emit(e):
events.append(e)
if e.get('type') == 'node_start':
order.append(e.get('nodeId'))
def agent_fn(node, incoming, settings, emit_fn, node_id):
state['runs'] += 1
per_node[node_id] = per_node.get(node_id, 0) + 1
inputs_by_node.setdefault(node_id, []).append(incoming)
if node.get('title') == '会炸的':
raise RuntimeError('故意炸一下')
title = node.get('title') or node.get('id')
return f"{title} 收到[{incoming}]"
stop_after = cs.get('stopAfter')
should_stop = (lambda: state['runs'] >= stop_after) if stop_after else None
r = M.run_meeting(meeting=cs['meeting'], settings={}, user_input='',
emit=emit, run_agent_fn=agent_fn, should_stop=should_stop)
out.append({'order': order, 'events': events, 'runCount': state['runs'],
'perNode': per_node, 'inputsByNode': inputs_by_node,
'output': r.get('output') or '', 'error': r.get('error') or '',
'aborted': bool(r.get('aborted'))})
print(json.dumps(out, ensure_ascii=False))
`;
const res = spawnSync(PY, ['-c', script], {
input: JSON.stringify(cases.map((c) => ({
meeting: c.meeting,
stopAfter: c.stopAfter || null,
}))),
encoding: 'utf8',
maxBuffer: 32 * 1024 * 1024,
env: { ...process.env, PYTHONIOENCODING: 'utf-8' },
});
if (res.status !== 0) {
console.log(' Python 侧执行失败:', (res.stderr || '').slice(0, 600));
return null;
}
try {
return JSON.parse(res.stdout.trim().split('\n').pop());
} catch (e) {
console.log(' Python 返回解析失败:', e.message, (res.stdout || '').slice(0, 300));
return null;
}
}
/* ------------------------------------------------------------------ 主流程 */
console.log('=== 会议执行引擎:Node 与 Python 对拍 ===\n');
for (const cs of cases) {
console.log(`【${cs.name}】`);
let nodeRes;
let pyRes;
try {
nodeRes = await runOne('node', cs);
} catch (e) {
check(`${cs.name} · Node 跑通`, false, e.message);
continue;
}
check(`${cs.name} · Node 跑通`, true);
if (cs.wantOrder) {
check(' 节点顺序正确', JSON.stringify(nodeRes.order) === JSON.stringify(cs.wantOrder),
nodeRes.order.join(' → '));
}
if (cs.wantOutput !== undefined) {
check(' 最终输出正确', nodeRes.output === cs.wantOutput, JSON.stringify(nodeRes.output));
}
if (cs.wantHas) {
const ok = cs.wantHas.every((s) => nodeRes.output.includes(s));
check(' 多入边内容都进来了', ok, JSON.stringify(nodeRes.output).slice(0, 90));
}
if (cs.wantRunCount !== undefined) {
check(' 环没有把引擎转死', nodeRes.runCount === cs.wantRunCount, `跑了 ${nodeRes.runCount} 次`);
}
if (cs.wantEachRuns) {
const got = JSON.stringify(nodeRes.perNode);
const want = JSON.stringify(cs.wantEachRuns);
check(' 每个节点的执行次数正确', got === want, got);
}
if (cs.wantSecondRoundHas !== undefined) {
// 第二轮拿到的输入必须包含第一轮的产出,不能是空的 ——
// 否则"重复"就只是把同一件事做两遍,没有意义
const second = nodeRes.inputsByNode.n1?.[1] || '';
check(' 第二轮能拿到上一轮的输出', second.includes(cs.wantSecondRoundHas),
JSON.stringify(second));
}
if (cs.wantAborted) {
check(' 被停止后正确报告 aborted', nodeRes.aborted === true, String(nodeRes.aborted));
}
if (cs.wantError) {
const has = nodeRes.events.some((e) => e.type === 'node_error');
check(' 出错上报了 node_error', has);
}
if (cs.wantErrorText) {
check(' 给出明确错误', nodeRes.error.includes(cs.wantErrorText), nodeRes.error);
}
}
/* ---------------------------------------- 两份实现逐字段对拍(最关键的一条) */
console.log('\n=== 两份实现对拍 ===');
const pyAll = await runPython('py', cases);
if (!pyAll) {
check('Python 侧可执行', false);
} else {
let same = 0;
for (let i = 0; i < cases.length; i += 1) {
const cs = cases[i];
let nodeRes;
try {
nodeRes = await runOne('node', cs);
} catch {
continue;
}
const py = pyAll[i];
const ok = JSON.stringify(nodeRes.order) === JSON.stringify(py.order)
&& nodeRes.output === py.output
&& nodeRes.runCount === py.runCount;
if (ok) same += 1;
else {
console.log(` ❌ 「${cs.name}」两边不一致`);
console.log(` Node: 顺序=${JSON.stringify(nodeRes.order)} 输出=${JSON.stringify(nodeRes.output)}`);
console.log(` Py : 顺序=${JSON.stringify(py.order)} 输出=${JSON.stringify(py.output)}`);
}
}
check(`逐用例一致(${same}/${cases.length})`, same === cases.length);
}
/* ------------------------------------------------------ 判断规则单测 */
console.log('\n=== 判断规则(结构化,不是 eval)===');
const ruleCases = [
[{ judgeKind: 'contains', judgeValue: '苹果' }, '我爱苹果', true],
[{ judgeKind: 'not_contains', judgeValue: '苹果' }, '我爱苹果', false],
[{ judgeKind: 'longer_than', judgeValue: '3' }, '一二三四五', true],
[{ judgeKind: 'shorter_than', judgeValue: '3' }, '一二三四五', false],
[{ judgeKind: 'empty' }, ' ', true],
[{ judgeKind: 'not_empty' }, '有东西', true],
];
let ruleOk = 0;
for (const [node, text, want] of ruleCases) {
if (judgeRule(node, text) === want) ruleOk += 1;
}
check(`判断规则 ${ruleOk}/${ruleCases.length}`, ruleOk === ruleCases.length);
const pass = results.filter((r) => r.ok).length;
console.log(`\n=== 结果:${pass} 通过 / ${results.length - pass} 失败 ===`);
process.exit(pass === results.length ? 0 : 1);