会议模式:内核数据模型 + 执行引擎 + SSE 路由

用户需求 C 的第一部分(引擎侧)。界面编辑器下一轮。

【数据模型】
meeting = { id, name, nodes[], edges[] }
节点四类:
  start  初始输入(文本可写死,留空则用用户发的那句)
  agent  智能体:agentId + **本节点专属**的附加提示词 + 输出控制
  judge  判断:按条件分 true / false 两条出边
  end    最终输出

★ 新增会议时给的是"输入 → 一个智能体 → 输出"的**最小可跑闭环**,
  不是空白画布 —— 对着空白画布干瞪眼是最差的新手体验,
  而且有个能跑的例子,节点和连线怎么连一眼就懂。

【安全决定:判断节点的规则不用 eval】
判断节点要支持"包含某词""字数多于 N"这类条件。最省事是让用户写表达式然后
eval —— **绝对不行**:配置能被任意改写(界面上可编辑、文件可手改),
eval 就等于把任意代码执行权交出去。
改用**结构化规则**(7 种 kind + value):能表达的就那几种,
多一种就多一个分支,而不是多一分风险。AI 判断也支持(让模型回 YES/NO)。

【执行引擎】
- 拓扑序 + 访问集合 + 步数上限,而不是递归:
  用户画错画出环时递归会直接爆栈,这样最多"少跑几个节点"
- 一个节点的输入 = 所有上游输出拼接(多条入边不用额外的合并节点)
- 单个节点出错**不让整场会议停摆**:上报 node_error,继续跑下游
- 边和节点在保存时做消毒:丢掉类型不认识的节点、指向不存在节点的悬空连线

【测试抓到的一个真设计缺陷】
判断节点原先把输出**覆盖成了字符串 "true"/"false"**,
于是下游智能体收到的输入就是 "true",完全没法用 ——
验证脚本报出来的是"真 收到[true]"。
修成**透传上游内容**,判断结论单独走 verdict 字段。

这个错误要是等界面做完再发现,会以为是"连线没连对",白查半天。

【验证】
- scripts/verify-meeting.mjs:引擎逻辑,7 张图(直链 / true 分支 / false 分支 /
  多入边拼接 / 环 / 节点报错 / 无入口),用**假智能体**不打真实模型。
  Node 与 Python 逐用例对拍一致;判断规则 6/6。20/20。
- scripts/verify-meeting-http.mjs:HTTP 端到端(起内核 + mock LLM),
  验路由、SSE 事件流、真调模型、判断分支、透传。两份内核各 12/12。

回归:语音 41/41 ×2、Markdown 19/19。

【还剩】会议模式的可视化编辑器(Compose 画布:拖节点、拉连线、点节点配参数)。
This commit is contained in:
l
2026-10-10 11:29:56 +08:00
parent 4bf46e7eb3
commit 376a143a37
6 changed files with 1476 additions and 0 deletions

View File

@@ -51,6 +51,15 @@ import {
writeMcpConfigText,
} from './mcp.mjs';
import { listModels } from './providers.mjs';
import {
listMeetings,
getMeeting,
createMeeting,
saveMeeting,
deleteMeeting,
runMeeting,
JUDGE_KINDS,
} from './meeting.mjs';
import {
synthesize,
synthesizeStream,
@@ -102,6 +111,94 @@ function cors(res) {
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS');
}
/**
* 跑一整张会议图,事件走 SSE。
*
* ★ 节点的附加提示词和输出控制都是**拼在系统提示词后面**,而不是替换它 ——
* 智能体本身的人设不能被节点的输出要求顶掉,两者是
* "人设 + 这一轮的额外要求"的关系。
*/
async function streamMeetingRun(req, res, meetingId) {
const body = await readBody(req).catch(() => ({}));
const settings = readSettings();
const item = getMeeting(meetingId);
if (!item) {
sendJson(res, 404, { error: '会议不存在' });
return;
}
const userInput = String(body.input || '').trim();
cors(res);
res.writeHead(200, {
'Content-Type': 'text/event-stream; charset=utf-8',
'Cache-Control': 'no-cache, no-transform',
Connection: 'keep-alive',
'X-Accel-Buffering': 'no',
});
const send = (event) => {
try {
res.write(`data: ${JSON.stringify(event)}\n\n`);
} catch { /* 客户端断了就算了 */ }
};
const runAgentFn = async (node, incoming, cfg, emit, nodeId) => {
const agent = node.agentId ? getAgent(node.agentId) : null;
const extra = String(node.extraPrompt || '').trim();
const control = String(node.outputControl || '').trim();
let mergedAgent;
if (node._judgeOnly) {
// AI 判断:只要一个是/否,别的都不要
mergedAgent = {
id: '',
name: '判断器',
systemPrompt: '你是一个判断器。根据下面给出的内容回答 YES 或 NO,'
+ '不要解释、不要加别的字。\n\n判断标准:' + (extra || '内容是否合格'),
};
} else {
const base = agent?.systemPrompt || cfg.systemPrompt || '';
const parts = base.trim() ? [base] : [];
if (extra) parts.push(`【本次任务的额外要求】\n${extra}`);
if (control) parts.push(`【输出要求】\n${control}`);
mergedAgent = { ...(agent || {}), systemPrompt: parts.join('\n\n') };
}
// 上游内容作为这一轮的用户消息;没有上游就给个占位,免得空消息报错
const message = incoming.trim() || '(上游没有内容,请根据你的角色直接开始)';
const result = await runAgent({
settings: cfg,
messages: [{ role: 'user', content: message }],
agent: mergedAgent,
emit: (event) => {
// 只把正文增量转成 node_delta,其它事件原样转发但带上 nodeId,
// 界面上按节点分组显示
if (event.type === 'text') {
emit({ type: 'node_delta', nodeId, text: event.text || '' });
} else if (event.type !== 'done') {
emit({ ...event, nodeId });
}
},
});
return result?.content || '';
};
try {
await runMeeting({
meeting: item,
settings,
userInput,
emit: send,
runAgentFn,
});
} catch (err) {
// 引擎自己炸了也要让界面知道,不能一声不响地断掉
send({ type: 'meeting_done', output: '', error: `${err.name}: ${err.message}` });
} finally {
res.end();
}
}
/**
* 边合成边下发音频。
*
@@ -532,6 +629,57 @@ async function handleApi(req, res, url) {
}
}
// 会议模式
if (pathname === '/api/meetings' && method === 'GET') {
sendJson(res, 200, { meetings: listMeetings() });
return true;
}
if (pathname === '/api/meetings' && method === 'POST') {
const body = await readBody(req).catch(() => ({}));
sendJson(res, 200, { meeting: createMeeting(body) });
return true;
}
if (pathname === '/api/meeting/meta' && method === 'GET') {
// 编辑器要用:节点类型、判断条件、可用智能体
sendJson(res, 200, {
nodeTypes: [
{ key: 'start', label: '初始输入', desc: '整张图的入口' },
{ key: 'agent', label: '智能体', desc: '一个智能体处理一轮' },
{ key: 'judge', label: '判断', desc: '按条件分 true / false 两条路' },
{ key: 'end', label: '最终输出', desc: '汇总结果' },
],
judgeKinds: JUDGE_KINDS.map(([key, label]) => ({ key, label })),
agents: listAgents().map((a) => ({ id: a.id, name: a.name, avatar: a.avatar })),
});
return true;
}
// /api/meetings/<id>/run 走 SSE;必须在通用 id 匹配之前判掉(那条正则不接受斜杠)
if (pathname.startsWith('/api/meetings/') && pathname.endsWith('/run') && method === 'POST') {
const id = pathname.slice('/api/meetings/'.length, -'/run'.length);
await streamMeetingRun(req, res, id);
return true;
}
const meetMatch = pathname.match(/^\/api\/meetings\/([^/]+)$/);
if (meetMatch) {
const id = meetMatch[1];
if (method === 'GET') {
const item = getMeeting(id);
sendJson(res, item ? 200 : 404, item ? { meeting: item } : { error: '会议不存在' });
return true;
}
if (method === 'PUT' || method === 'PATCH') {
const body = await readBody(req).catch(() => ({}));
const item = saveMeeting(id, body);
sendJson(res, item ? 200 : 404, item ? { meeting: item } : { error: '会议不存在' });
return true;
}
if (method === 'DELETE') {
const ok = deleteMeeting(id);
sendJson(res, ok ? 200 : 404, { ok });
return true;
}
}
// 工具
if (pathname === '/api/tools' && method === 'GET') {
const settings = readSettings();