/** * 精确定位"轮询会不会重复投递事件"(不走 adb,直接 HTTP)。 * 用法: node scripts/probe-poll-cursor.mjs [base] */ const BASE = process.argv[2] || 'http://127.0.0.1:5403'; const post = async (p, body) => (await fetch(BASE + p, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), })).json(); const get = async (p) => (await fetch(BASE + p)).json(); console.log('起任务…'); const started = await post('/api/chat', { conversationId: 'c_main', message: '用一句话解释什么是流式输出', history: [], transport: 'poll', }); console.log(' runId =', started.runId); let cursor = 0; let text = ''; let reasoning = ''; let done = false; const perPoll = []; let seq = 0; while (!done && seq < 300) { const r = await get(`/api/chat/poll?runId=${started.runId}&cursor=${cursor}`); if (!r.ok) { console.log('轮询失败', r); break; } let addedText = 0; let addedEvents = 0; for (const e of r.events || []) { addedEvents += 1; if (e.type === 'delta') { text += e.text; addedText += e.text.length; } if (e.type === 'reasoning') reasoning += e.text; } perPoll.push({ seq, from: cursor, to: r.cursor, events: addedEvents, addedText }); // 关键:游标必须由服务端给的 cursor 推进,且不能回退 if (r.cursor < cursor) { console.log('❌ 服务端 cursor 回退了!', cursor, '→', r.cursor); break; } cursor = r.cursor; done = Boolean(r.done); seq += 1; if (!done) await new Promise((x) => setTimeout(x, 120)); } console.log(`\n轮询 ${perPoll.length} 次,正文累计 ${text.length} 字,思维链 ${reasoning.length} 字`); console.log('每次拉到的增量:'); for (const p of perPoll.slice(0, 20)) { console.log(` #${p.seq} cursor ${p.from}→${p.to} 事件 ${p.events} 正文 +${p.addedText}`); } // 和落库内容对比:这才是"真值" const conv = await get('/api/conversations/c_main'); const stored = (conv.conversation.messages || []).filter((m) => m.role === 'assistant').pop(); const storedText = stored?.content || ''; console.log(`\n落库正文 ${storedText.length} 字`); console.log(`轮询累计 ${text.length} 字`); console.log(storedText.length === text.length ? '✅ 完全一致:轮询没有重复投递' : `❌ 对不上:差 ${text.length - storedText.length} 字`); if (storedText.length !== text.length) { console.log('\n落库:', storedText.slice(0, 100)); console.log('累计:', text.slice(0, 100)); }