/** * 会议模式 HTTP 端到端:起内核 + mock LLM,真跑一张图,看 SSE 事件。 * * 前面 verify-meeting.mjs 验的是引擎逻辑(用假智能体、不联网); * 这个验的是"路由 + SSE + 真的调模型"这一整条链路。 * * 跑法: node scripts/verify-meeting-http.mjs py|node */ import { spawn, spawnSync } from 'node:child_process'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const KIND = process.argv[2] === 'node' ? 'node' : 'py'; const PY = process.env.PYTHON || 'C:\\Python313\\python.exe'; const KPORT = KIND === 'py' ? 5451 : 5452; const MPORT = 5453; const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), `doubao-meet-${KIND}-`)); fs.writeFileSync(path.join(dataDir, 'settings.json'), JSON.stringify({ provider: 'custom', baseUrl: `http://127.0.0.1:${MPORT}/v1`, apiKey: 'mock-key', model: 'mock-gpt', speech: { ttsEngine: 'system' }, }), 'utf8'); const procs = []; const cleanup = () => { for (const p of procs) { try { p.kill(); } catch { /* 忽略 */ } } }; process.on('exit', cleanup); const results = []; const check = (name, ok, detail = '') => { results.push({ name, ok }); console.log(` ${ok ? '✅' : '❌'} ${name}${detail ? ` → ${detail}` : ''}`); }; // mock LLM:固定回一段短文本,不打真实模型 const mock = spawn(process.execPath, [path.join(ROOT, 'scripts', 'mock-llm.mjs')], { env: { ...process.env, MOCK_PORT: String(MPORT), MOCK_REPLY_CHARS: '0' }, stdio: ['ignore', 'pipe', 'pipe'], }); mock.stdout.on('data', () => {}); mock.stderr.on('data', () => {}); procs.push(mock); const env = { ...process.env, PYTHONIOENCODING: 'utf-8', DOUBAO_DATA: dataDir }; const kern = KIND === 'py' ? spawn(PY, [path.join(ROOT, 'android', 'kernel', 'main.py')], { env: { ...env, DOUBAO_PORT: String(KPORT) }, stdio: ['ignore', 'pipe', 'pipe'] }) : spawn(process.execPath, [path.join(ROOT, 'server', 'src', 'index.mjs')], { env: { ...env, PORT: String(KPORT) }, stdio: ['ignore', 'pipe', 'pipe'] }); kern.stdout.on('data', () => {}); kern.stderr.on('data', () => {}); procs.push(kern); const base = `http://127.0.0.1:${KPORT}`; const api = async (p, opt) => { const r = await fetch(base + p, opt); const t = await r.text(); try { return JSON.parse(t); } catch { return { _raw: t.slice(0, 300), _status: r.status }; } }; // 等内核就绪 let ready = false; for (let i = 0; i < 60; i += 1) { await new Promise((r) => setTimeout(r, 500)); try { const r = await fetch(`${base}/api/health`, { signal: AbortSignal.timeout(1500) }); if (r.ok) { ready = true; break; } } catch { /* 还没起来 */ } } console.log(`=== 会议 HTTP 端到端(${KIND})===`); if (!ready) { console.log(' 内核起不来'); process.exit(1); } check('内核就绪', true); // 元信息 const meta = await api('/api/meeting/meta'); const kindKeys = (meta.nodeTypes || []).map((t) => t.key); check('元信息给了节点类型/判断条件/智能体', kindKeys.length >= 6 && (meta.judgeKinds || []).length >= 6 && Array.isArray(meta.agents), `节点类型 ${kindKeys.join('/')} · 判断条件 ${(meta.judgeKinds || []).length} · 智能体 ${(meta.agents || []).length}`); // 变量节点是后加的,单独断言一下,免得哪天又漏了 check('节点类型里有写变量/读变量', kindKeys.includes('var') && kindKeys.includes('readvar'), kindKeys.join(',')); // 建会议 const made = await api('/api/meetings', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: '端到端测试会议' }), }); const meetingId = made.meeting?.id; check('建会议成功', !!meetingId, meetingId || JSON.stringify(made).slice(0, 120)); check('默认图可以直接跑(3 节点 2 连线)', made.meeting?.nodes?.length === 3 && made.meeting?.edges?.length === 2, `${made.meeting?.nodes?.length} 节点 / ${made.meeting?.edges?.length} 连线`); // 跑一张带判断节点的图:输入 → 判断 → 两个智能体分支 → 输出 const graph = { nodes: [ { id: 's', type: 'start', x: 0, y: 0, text: '给我讲讲苹果' }, { id: 'j', type: 'judge', x: 100, y: 0, judgeKind: 'contains', judgeValue: '苹果', extraPrompt: '内容里有没有提到水果' }, { id: 't', type: 'agent', x: 200, y: -50, title: '水果专家', agentId: '', outputControl: '一句话' }, { id: 'f', type: 'agent', x: 200, y: 50, title: '别的专家', agentId: '', outputControl: '一句话' }, { id: 'e', type: 'end', x: 300, y: 0 }, ], // ★ exec 排顺序、str 数据线传值 —— 判断节点要拿到内容必须有数据线。 // 老写法"一根线既排顺序又传值"在类型化之后不成立了。 edges: [ { from: 's', to: 'j', kind: 'exec' }, { from: 's', to: 'j', kind: 'str' }, { from: 'j', to: 't', kind: 'exec', branch: 'true' }, { from: 'j', to: 'f', kind: 'exec', branch: 'false' }, { from: 'j', to: 't', kind: 'str' }, { from: 'j', to: 'f', kind: 'str' }, { from: 't', to: 'e', kind: 'exec' }, { from: 'f', to: 'e', kind: 'exec' }, { from: 't', to: 'e', kind: 'str' }, { from: 'f', to: 'e', kind: 'str' }, ], }; const saved = await api(`/api/meetings/${meetingId}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ nodes: graph.nodes, edges: graph.edges }), }); check('保存图成功', (saved.meeting?.nodes || []).length === 5, `${saved.meeting?.nodes?.length} 节点`); check('悬空连线会被清掉', true, '(这条图没有悬空连线,只确认保存没报错)'); // 跑 const res = await fetch(`${base}/api/meetings/${meetingId}/run`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ input: '' }), signal: AbortSignal.timeout(60000), }); check('run 返回 SSE', (res.headers.get('content-type') || '').includes('event-stream'), res.headers.get('content-type') || ''); const text = await res.text(); const events = text.split('\n') .filter((l) => l.startsWith('data: ')) .map((l) => { try { return JSON.parse(l.slice(6)); } catch { return null; } }) .filter(Boolean); const types = events.map((e) => e.type); const starts = events.filter((e) => e.type === 'node_start').map((e) => e.nodeId); check('事件流完整', types.includes('meeting_start') && types.includes('meeting_done'), types.filter((t, i) => types.indexOf(t) === i).join(', ')); check('走的是 true 分支(输入含"苹果")', starts.join('→') === 's→j→t→e', starts.join('→')); const jDone = events.find((e) => e.type === 'node_done' && e.nodeId === 'j'); check('判断节点透传上游内容(不是 "true")', jDone?.output === '给我讲讲苹果', JSON.stringify(jDone?.output)); check('判断节点带 verdict 字段', jDone?.verdict === true, String(jDone?.verdict)); const done = events.find((e) => e.type === 'meeting_done'); check('最终输出非空', !!done?.output, JSON.stringify(done?.output || '').slice(0, 80)); // 清掉测试数据 await api(`/api/meetings/${meetingId}`, { method: 'DELETE' }); cleanup(); try { fs.rmSync(dataDir, { recursive: true, force: true }); } catch { /* 忽略 */ } const pass = results.filter((r) => r.ok).length; console.log(`\n=== 结果:${pass} 通过 / ${results.length - pass} 失败 ===`); process.exit(pass === results.length ? 0 : 1);