From f45d6f830fa94d6faf36fe9cfe6c2be213859d1e Mon Sep 17 00:00:00 2001 From: kicer Date: Mon, 31 Aug 2026 15:29:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E9=80=81=E5=90=8E=E5=8D=B3=E6=97=B6?= =?UTF-8?q?=E5=9B=9E=E9=A6=88=EF=BC=9Asend=20=E6=88=90=E5=8A=9F=E5=8D=B3?= =?UTF-8?q?=E6=8C=82=E3=80=8C=E6=80=9D=E8=80=83=E4=B8=AD=E2=80=A6=E3=80=8D?= =?UTF-8?q?=E5=8D=A0=E4=BD=8D=E6=B0=94=E6=B3=A1=20+=20=E7=A7=92=E8=AE=A1?= =?UTF-8?q?=E6=97=B6=EF=BC=880s=20=E8=B5=B7=EF=BC=8C500ms=20=E5=88=B7?= =?UTF-8?q?=E6=96=B0=EF=BC=89=EF=BC=8C=E9=A6=96=E4=B8=AA=20delta/assistant?= =?UTF-8?q?=5Fmessage/tool=5Fcall/done/error=20=E5=88=B0=E8=BE=BE=E6=97=B6?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=B9=B6=E6=B5=81=E5=BC=8F=E5=A1=AB=E5=85=85?= =?UTF-8?q?=E6=AD=A3=E6=96=87=EF=BC=9B=E6=8E=92=E9=98=9F=E9=80=81=E8=BE=BE?= =?UTF-8?q?=E4=B8=8D=E6=8C=82=E5=8D=A0=E4=BD=8D=EF=BC=9B=E5=88=87=E9=A1=B5?= =?UTF-8?q?/=E8=BF=94=E5=9B=9E=E5=88=97=E8=A1=A8=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/server/public/css/style.css | 10 ++++++++ app/server/public/js/app.js | 42 ++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/app/server/public/css/style.css b/app/server/public/css/style.css index 0c75887..70fa529 100644 --- a/app/server/public/css/style.css +++ b/app/server/public/css/style.css @@ -123,6 +123,16 @@ body { .msg-bubble table { border-collapse: collapse; margin: 6px 0; display: block; overflow-x: auto; } .msg-bubble th, .msg-bubble td { border: 1px solid #dde; padding: 4px 8px; font-size: 13px; } .msg-bubble blockquote { border-left: 3px solid #ccd; padding-left: 8px; color: #667; margin: 4px 0; } +.msg-bubble.pending-bubble { color: #889; } +.pending-dots { display: inline-flex; gap: 3px; margin-left: 6px; vertical-align: middle; } +.pending-dots i { + width: 4px; height: 4px; border-radius: 50%; background: #9ab; + display: inline-block; animation: blink 1.2s infinite; +} +.pending-dots i:nth-child(2) { animation-delay: 0.2s; } +.pending-dots i:nth-child(3) { animation-delay: 0.4s; } +@keyframes blink { 0%, 60%, 100% { opacity: 0.25; } 30% { opacity: 1; } } +.pending-timer { margin-left: 8px; font-size: 11px; color: #aab; font-variant-numeric: tabular-nums; } .msg-bubble ul, .msg-bubble ol { padding-left: 1.5em; margin: 6px 0; } .msg-bubble li { margin: 2px 0; } .msg-time { font-size: 10px; color: #aab; margin-top: 3px; } diff --git a/app/server/public/js/app.js b/app/server/public/js/app.js index a70a5d4..10e74e3 100644 --- a/app/server/public/js/app.js +++ b/app/server/public/js/app.js @@ -15,6 +15,9 @@ const state = { toolCards: {}, filePanelOpen: false, pendingDeleteId: null, + pendingEl: null, + pendingTimer: null, + pendingStart: 0, }; // ---------- API ---------- @@ -212,6 +215,7 @@ function appendToolCardsFromAssistant(m, container) { async function renderDetail(detail) { const box = $('messages'); box.innerHTML = ''; + removePendingBubble(); state.streamEl = null; state.streamText = ''; state.toolCards = {}; @@ -236,6 +240,34 @@ function scrollBottom() { box.scrollTop = box.scrollHeight; } +// ---------- AI 处理中占位 ---------- +// 发送后立即挂一个「思考中 + 秒计时」占位气泡,首个增量/回复/工具卡到达时移除, +// 避免上游慢时长时间无反馈像卡死。 +function addPendingBubble() { + removePendingBubble(); + const wrap = document.createElement('div'); + wrap.className = 'msg other'; + const box = $('messages'); + const tip = box.querySelector('.empty-tip'); + if (tip) tip.remove(); + wrap.innerHTML = '
AI
' + + '
思考中' + + '0s
'; + box.appendChild(wrap); + state.pendingEl = wrap; + state.pendingStart = Date.now(); + state.pendingTimer = setInterval(() => { + const t = state.pendingEl && state.pendingEl.querySelector('.pending-timer'); + if (t) t.textContent = Math.floor((Date.now() - state.pendingStart) / 1000) + 's'; + }, 500); + scrollBottom(); +} + +function removePendingBubble() { + if (state.pendingTimer) { clearInterval(state.pendingTimer); state.pendingTimer = null; } + if (state.pendingEl) { state.pendingEl.remove(); state.pendingEl = null; } +} + // ---------- SSE ---------- function connectStream() { if (state.es) { state.es.close(); state.es = null; } @@ -248,6 +280,7 @@ function connectStream() { }); es.addEventListener('assistant_delta', ev => { const d = JSON.parse(ev.data); + removePendingBubble(); if (!state.streamEl) { const wrap = document.createElement('div'); wrap.className = 'msg other'; @@ -263,6 +296,7 @@ function connectStream() { }); es.addEventListener('assistant_message', ev => { const d = JSON.parse(ev.data); + removePendingBubble(); if (state.streamEl) { const bubble = state.streamEl; bubble.innerHTML = md(d.content || ''); @@ -283,6 +317,7 @@ function connectStream() { }); es.addEventListener('tool_call', ev => { const d = JSON.parse(ev.data); + removePendingBubble(); // 工具卡出现也算即时反馈 if (state.toolCards[d.id]) return; // assistant_message 已建卡(含完整参数),不重复创建 const card = renderToolCard(d.id, d.name, d.arguments); $('messages').appendChild(card); @@ -295,10 +330,12 @@ function connectStream() { scrollBottom(); }); es.addEventListener('done', ev => { + removePendingBubble(); // 本轮结束,占位兼终止(如模型报错无回复) loadSessions(); loadModels(); }); es.addEventListener('error', ev => { + removePendingBubble(); try { const d = JSON.parse(ev.data); if (d && d.message) toast('错误:' + d.message); @@ -367,7 +404,7 @@ const ui = { }, async confirmDelete() { const id = state.pendingDeleteId; - if (!id) return; + if (!id) { this.closeDelete(); return; } const delDir = $('del-dir-check').checked; this.closeDelete(); try { @@ -387,6 +424,7 @@ const ui = { }, backToList() { if (state.es) { state.es.close(); state.es = null; } + removePendingBubble(); state.sessionId = null; showList(); loadSessions(); @@ -408,6 +446,8 @@ const ui = { wrap.innerHTML = '
我' + (r.delivered === 'queued' ? '(排队中,暂存)' : '') + '
' + '
' + esc(content) + '
'; box.appendChild(wrap); + // 立即给出回馈:占位气泡 + 等待计时,首个增量/回复到达时移除并填充内容 + if (r.delivered !== 'queued') addPendingBubble(); scrollBottom(); } catch (e) { toast('发送失败:' + e.message); } },