feat: terminal 执行中增量输出走 SSE(tool_output),工具卡实时滚动
This commit is contained in:
@@ -274,6 +274,19 @@
|
||||
pre.insertAdjacentElement('afterend', el);
|
||||
}
|
||||
}
|
||||
// 工具执行中的增量输出(terminal 长命令):边跑边填;只留尾部 2000 行,避免长跑命令撑爆 DOM
|
||||
function appendToolOutput(id, chunk) {
|
||||
const card = document.getElementById('tool-' + id);
|
||||
if (!card) return;
|
||||
const pre = card.querySelector('.tool-result');
|
||||
if (!pre) return;
|
||||
if (!pre.dataset.streaming) { pre.textContent = ''; pre.dataset.streaming = '1'; } // 首块到达:清掉“执行中…”占位
|
||||
const follow = pre.scrollHeight - pre.scrollTop - pre.clientHeight < 40; // 用户往上翻时不抢滚动
|
||||
pre.appendChild(document.createTextNode(chunk));
|
||||
const lines = pre.textContent.split('\n');
|
||||
if (lines.length > 2000) pre.textContent = '…(更早输出已折叠)\n' + lines.slice(-2000).join('\n');
|
||||
if (follow) pre.scrollTop = pre.scrollHeight;
|
||||
}
|
||||
// 把 assistant 消息里的 tool_calls 建成工具卡片(历史渲染与 assistant_message 事件共用)
|
||||
function appendToolCardsFromAssistant(m, container) {
|
||||
if (!m.tool_calls) return;
|
||||
|
||||
@@ -243,7 +243,7 @@
|
||||
state.lastSseAt = Date.now(); // 建连即重置静默计时
|
||||
// 任何具名事件都算连接存活:静默看门狗与回前台宽限共用(error 不列,原生连接错误也走 error)
|
||||
['ping', 'status', 'assistant_delta', 'reasoning_delta', 'assistant_message', 'tool_call_start',
|
||||
'tool_call_args', 'tool_call', 'tool_result', 'ui_card', 'auto_compress', 'done', 'title', 'think_auto',
|
||||
'tool_call_args', 'tool_call', 'tool_output', 'tool_result', 'ui_card', 'auto_compress', 'done', 'title', 'think_auto',
|
||||
'pending_added', 'pending_removed', 'user_message']
|
||||
.forEach(function (n) { es.addEventListener(n, function () { markSseAlive(n); }); });
|
||||
es.addEventListener('status', ev => {
|
||||
@@ -424,6 +424,10 @@
|
||||
state.toolCards[d.id] = card;
|
||||
maybeAutoScroll();
|
||||
});
|
||||
es.addEventListener('tool_output', ev => {
|
||||
const d = JSON.parse(ev.data);
|
||||
appendToolOutput(d.id, d.chunk);
|
||||
});
|
||||
es.addEventListener('tool_result', ev => {
|
||||
const d = JSON.parse(ev.data);
|
||||
fillToolResult(d.tool_call_id, d.content, d.is_error, d.image_url, d.msg_id);
|
||||
|
||||
Reference in New Issue
Block a user