From f601e18364546587ec94ac141370d0988ac6f4b4 Mon Sep 17 00:00:00 2001 From: kicer Date: Thu, 10 Sep 2026 10:04:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=EF=BC=9Amock=20=E4=B8=8A?= =?UTF-8?q?=E6=B8=B8=E6=96=B0=E5=A2=9E=E3=80=8C=E6=85=A2=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E3=80=8D=E5=9C=BA=E6=99=AF=EF=BC=88write=5Ffile=20=E5=8F=82?= =?UTF-8?q?=E6=95=B0=2040=20=E5=AD=97/=E7=89=87=C3=9790ms=20=E6=BB=B4?= =?UTF-8?q?=E7=81=8C=E7=BA=A6=207s=EF=BC=8Cusage=20=E5=B8=A6=20cached=5Fto?= =?UTF-8?q?kens=EF=BC=89=EF=BC=9Bsmoke=20=E7=A7=BB=E9=99=A4=E5=B7=B2?= =?UTF-8?q?=E5=88=A0=20/start=20=E8=B7=AF=E7=94=B1=E7=9A=84=E6=AE=8B?= =?UTF-8?q?=E7=95=99=E8=B0=83=E7=94=A8=E4=B8=8E=E6=96=AD=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/test/mock-upstream.js | 27 +++++++++++++++++++++++++++ app/test/smoke.js | 4 ---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/app/test/mock-upstream.js b/app/test/mock-upstream.js index 785c31c..e1529fa 100644 --- a/app/test/mock-upstream.js +++ b/app/test/mock-upstream.js @@ -2,6 +2,7 @@ // 规则: // - 历史里已有 tool 结果 -> 回纯文本收尾 // - 最后一条 user 消息含 "写文件" -> 返回 write_file 工具调用 +// - 含 "慢工具" -> write_file 参数碎片化慢速下传(验证 tool_call 流式早反馈) // - 含 "读图" -> 返回 read_file(pic.png) 工具调用(验证 vision 图片回传) // - 含 "慢" -> 慢速输出(用于终止测试) // - 其他 -> 纯文本 "已收到: <最后一条user消息>" @@ -41,6 +42,30 @@ function streamToolCall(res) { res.end(); } +// 慢速工具调用场景:参数拆 40 字符/片、90ms 一片共约 7 秒,验证前端 tool_call 参数流式早反馈 +// (tool_call_start 即时建卡 + tool_call_args 实时填充;usage 帧带 cached_tokens 顺带验证命中率展示) +function streamSlowToolCall(res) { + res.writeHead(200, { 'Content-Type': 'text/event-stream' }); + const id = 'call-slow-' + Date.now(); + const args = JSON.stringify({ path: 'slow.txt', content: 'slow stream line. '.repeat(190) }); // ≈3.4k 字符 + sse(res, { choices: [{ delta: { role: 'assistant', tool_calls: [{ index: 0, id, function: { name: 'write_file' } }] } }] }); + let pos = 0; + const timer = setInterval(() => { + if (pos >= args.length) { + clearInterval(timer); + sse(res, { choices: [{ delta: {}, finish_reason: 'tool_calls' }] }); + sse(res, { usage: { prompt_tokens: 3000, completion_tokens: 1200, total_tokens: 4200, prompt_tokens_details: { cached_tokens: 2850 } } }); + res.write('data: [DONE]\n\n'); + res.end(); + return; + } + const piece = args.slice(pos, pos + 40); + pos += 40; + sse(res, { choices: [{ delta: { tool_calls: [{ index: 0, function: { arguments: piece } }] } }] }); + }, 90); + res.on('close', () => clearInterval(timer)); +} + function streamReadFileCall(res) { res.writeHead(200, { 'Content-Type': 'text/event-stream' }); const id = 'call-img-' + Date.now(); @@ -76,6 +101,8 @@ const server = http.createServer((req, res) => { streamReadFileCall(res); } else if (userText.includes('写文件')) { streamToolCall(res); + } else if (userText.includes('慢工具')) { + streamSlowToolCall(res); } else if (userText.includes('慢')) { streamText(res, '这是一段很长的慢速输出,用来测试终止功能是否生效。', 200); } else { diff --git a/app/test/smoke.js b/app/test/smoke.js index 436ca0d..41b337d 100644 --- a/app/test/smoke.js +++ b/app/test/smoke.js @@ -103,7 +103,6 @@ async function main() { console.log('\n[2] 工具调用'); s = await api('POST', '/api/sessions', { title: '冒烟-工具' }); await waitSession(s.id, x => x.session.status === 'held' && !!x.session.model_id, 5000, '绑定(held)'); - await api('POST', '/api/sessions/' + s.id + '/start'); await api('POST', '/api/sessions/' + s.id + '/messages', { content: '请写文件' }); d = await waitSession(s.id, x => x.session.status === 'held' && x.messages.some(m => m.role === 'tool'), 15000, '工具回合完成(held)'); const toolMsgs = d.messages.filter(m => m.role === 'tool'); @@ -114,9 +113,6 @@ async function main() { const dl = await fetch(BASE + '/api/sessions/' + s.id + '/files/download?path=hello.txt'); const dlText = await dl.text(); ok(dlText === 'hello from agent', '下载内容正确', dlText); - const taskMd = d.messages.find(m => m.role === 'user' && m.content.includes('task.md')); - ok(!!taskMd, '开始执行自动追加模板消息'); - ok(d.session.phase === 'execution', '阶段切换为 execution'); // ---- 2.5 read_file 读图回传(vision)---- console.log('\n[2.5] 读图回传');