From 9dbd89707ccdafe2d944e862ec1f895a33a947e4 Mon Sep 17 00:00:00 2001 From: kicer Date: Thu, 10 Sep 2026 21:22:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E5=BF=AB=E9=80=9F?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E6=A8=A1=E5=9E=8B=E4=B8=8B=E6=B5=81=E5=BC=8F?= =?UTF-8?q?=E6=B8=B2=E6=9F=93=E5=8D=A1=E9=A1=BF=E2=80=94=E2=80=94assistant?= =?UTF-8?q?/reasoning=20delta=20=E5=90=88=E5=B8=A7=E5=88=B0=E6=AF=8F?= =?UTF-8?q?=E4=B8=AA=E5=8A=A8=E7=94=BB=E5=B8=A7=E5=88=B7=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=20DOM=EF=BC=8C=E6=96=87=E6=9C=AC=E8=8A=82=E7=82=B9=E5=B0=BE?= =?UTF-8?q?=E9=83=A8=E5=A2=9E=E9=95=BF=E8=B5=B0=20appendData=20=E5=A2=9E?= =?UTF-8?q?=E9=87=8F=E8=BF=BD=E5=8A=A0=EF=BC=8C=E6=BB=9A=E5=8A=A8/?= =?UTF-8?q?=E8=BD=AC=E5=9C=88=E6=8C=89=E9=92=AE=E5=90=8C=E6=AD=A5=E5=90=88?= =?UTF-8?q?=E5=B9=B6=EF=BC=8C=E4=B8=8D=E5=86=8D=E9=80=90=20token=20?= =?UTF-8?q?=E5=85=A8=E9=87=8F=E9=87=8D=E5=86=99+=E5=BC=BA=E5=88=B6?= =?UTF-8?q?=E9=87=8D=E6=8E=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/server/public/index.html | 38 +++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/app/server/public/index.html b/app/server/public/index.html index 24c596d..ed7e836 100644 --- a/app/server/public/index.html +++ b/app/server/public/index.html @@ -1597,6 +1597,34 @@ if (det) { det.open = false; det.classList.remove('streaming'); } } + // ---------- 流式 delta 合帧渲染 ---------- + // 快速输出模型(如 DeepSeek)的 delta 频率远超屏幕刷新率:逐条全量重写文本节点 + + // 读布局滚动(scrollHeight/scrollTop)会把主线程挤满,页面与右下角转圈肉眼可见地卡。 + // 改为 delta 只累积进缓冲,每个动画帧统一刷一次 DOM;文本节点内容只在尾部增长时 + // 走 appendData 增量追加,避免整段文本节点重建重排。 + let streamPaintQueued = false; + function paintStreamingText(el, full) { + if (!el) return; + const tn = el.firstChild; + if (tn && tn.nodeType === 3) { + if (tn.data === full) return; // 无变化不碰 DOM,不触发布局 + if (full.startsWith(tn.data)) { tn.appendData(full.slice(tn.data.length)); return; } + } + el.textContent = full; // 结构不符(收尾后重建/罕见回退)才整段重写 + } + + function scheduleStreamPaint() { + if (streamPaintQueued) return; + streamPaintQueued = true; + requestAnimationFrame(() => { + streamPaintQueued = false; + paintStreamingText(state.streamEl, state.streamText); + paintStreamingText(state.streamThinkEl, state.streamReasoning); + maybeAutoScroll(); + updateScrollBottomBtn(); + }); + } + function connectStream() { closeStream(); if (!state.sessionId) return; @@ -1619,12 +1647,10 @@ if (!state.streamText.trim()) return; // 忽略前导空白(纯工具调用回合无文本) ensureStreamWrap(); state.streamText = d.text; // 上面 if 已 trim 为非空 - collapseStreamThink(); // 首个正文 delta:思考已结束,折叠思考面板 + collapseStreamThink(); // 首个正文 delta:思考已结束,折叠思考面板 } - state.streamEl.textContent = state.streamText; state.streaming = true; - maybeAutoScroll(); - updateScrollBottomBtn(); + scheduleStreamPaint(); // DOM 刷新合到每个动画帧一次:快速输出模型逐 token 重排会挤爆主线程 }); es.addEventListener('reasoning_delta', ev => { const d = JSON.parse(ev.data); @@ -1632,10 +1658,8 @@ state.streamReasoning += d.text; if (!state.streamWrap) ensureStreamWrap(); ensureStreamThinkPanel(); - state.streamThinkEl.textContent = state.streamReasoning; state.streaming = true; - maybeAutoScroll(); - updateScrollBottomBtn(); + scheduleStreamPaint(); }); es.addEventListener('assistant_message', ev => { const d = JSON.parse(ev.data);