From a464a24fde19e2625a35139343eb3564986645cb Mon Sep 17 00:00:00 2001 From: kicer Date: Sat, 5 Sep 2026 11:22:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E3=80=8C=E4=BB=85=E6=91=98=E8=A6=81?= =?UTF-8?q?=E3=80=8D=E9=87=8D=E5=A4=8D=E6=B8=B2=E6=9F=93=EF=BC=9A=E6=8C=89?= =?UTF-8?q?=E6=B6=88=E6=81=AF=20id=20=E5=8E=BB=E9=87=8D=20+=20=E6=80=BB?= =?UTF-8?q?=E7=BB=93=E6=94=B9=E6=88=90=E6=8A=98=E5=8F=A0=E9=9D=A2=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 加 state.lastAssistantMsgId 防重:SSE assistant_message 同 id 只处理一次(防御 EventSource 自动重连/服务端重发导致 handler 被二次触发)。 - 「上下文总结」UI 改造:去掉 emoji 小标,改成 details/summary 折叠面板(复用 .msg-think 视觉风格,summary 文字「上下文总结」),刚收到时默认 open 便于阅读, 收尾后随同思考面板一起折起,避免占住后续气泡的空间。 - 防重插入:同 streamWrap 里已存在 .msg-summary 时不重复插入。 --- app/server/public/index.html | 83 ++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 32 deletions(-) diff --git a/app/server/public/index.html b/app/server/public/index.html index a77c624..ef3a0d8 100644 --- a/app/server/public/index.html +++ b/app/server/public/index.html @@ -19,8 +19,14 @@ 放在 link 之后以保证同优先级下能压住 style.css 里同优先级的规则。 */ body .msg-actions .act-del { display: none; } body.log-debug .msg-actions .act-del { display: flex; } - /* 「压缩」总结气泡上的小标。点不出任何效果,纯状态。 */ - .summary-tag { font-size: 11px; color: #6c757d; padding: 2px 0 6px 4px; letter-spacing: .5px; } + /* 「压缩」总结折叠面板:复用 .msg-think 的折叠交互,但 body 是常规字体(不是代码块)。 + summary 文字「上下文总结」;默认 open。收尾后改为非 streaming。 */ + .msg-summary { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; margin: 0 0 6px; } + .msg-summary > summary { list-style: none; cursor: pointer; user-select: none; padding: 6px 10px; display: flex; align-items: center; gap: 6px; color: var(--gray); font-size: 12px; } + .msg-summary > summary::-webkit-details-marker { display: none; } + .msg-summary > summary::before { content: '▸'; font-size: 10px; color: var(--gray-light); transition: transform .15s; display: inline-block; } + .msg-summary[open] > summary::before { transform: rotate(90deg); } + .msg-summary .bubble-ai { margin: 0 10px 10px; } /* 状态栏图标按钮(压缩):仅显示 SVG、去掉文字 padding;保持与文字按钮同高。 */ .stop-link.icon-only { padding: 0 8px; line-height: 0; } .stop-link.icon-only .ic { width: 18px; height: 18px; vertical-align: middle; } @@ -61,7 +67,7 @@ @@ -430,6 +436,7 @@ streaming: false, pendingSummary: false, // 是否在等待「压缩」总结 AI 回复(避免重复弹窗;同时用于按钮 disabled 防重入) pendingSummaryMode: null, // pendingSummary 期间记录用户选的模式:'generate_only' 或 'compress_and_clear' + lastAssistantMsgId: null, // 最近处理过的 SSE assistant_message id:重复 id 直接丢,防重渲染(补「2 遍」问题) }; // ---------- 视图切换 ---------- @@ -828,16 +835,18 @@ } async function openSession(id) { - closeStream(); - removeTyping(); - setFilesPanel(false); // 会话切换,收起文件面板防旧列表残留 - state.sessionId = id; - state.status = ''; - // 先从抽屉列表缓存取会话类型,让顶部 icon 立即正确;详情回来后由 renderDetail 纠正 - const cached = (state.sessions || []).find(x => x.id === id); - state.sessionType = (cached && cached.assistant_type) || 'secretary'; - state.streamEl = null; - state.streamText = ''; + closeStream(); + removeTyping(); + setFilesPanel(false); // 会话切换,收起文件面板防旧列表残留 + state.sessionId = id; + state.status = ''; + // 先从抽屉列表缓存取会话类型,让顶部 icon 立即正确;详情回来后由 renderDetail 纠正 + const cached = (state.sessions || []).find(x => x.id === id); + state.sessionType = (cached && cached.assistant_type) || 'secretary'; + state.streamEl = null; + state.streamText = ''; + state.streamWrap = null; + state.lastAssistantMsgId = null; state.toolCards = {}; state.streaming = false; state.attachments = []; @@ -1196,8 +1205,11 @@ updateScrollBottomBtn(); }); es.addEventListener('assistant_message', ev => { - const d = JSON.parse(ev.data); - removeTyping(); + const d = JSON.parse(ev.data); + // 防重:同 id 的 assistant_message 只处理一次(防御 EventSource 自动重连、服务端重发等情况) + if (d.id && state.lastAssistantMsgId === d.id) return; + if (d.id) state.lastAssistantMsgId = d.id; + removeTyping(); const hasText = !!(d.content && d.content.trim()); const r = hasText ? mdHtml(d.content) : null; // 思考面板:本轮结束时锁定为后端返回的完整文本(不清空 streamReasoning 以防与历史回填不一致) @@ -1224,26 +1236,33 @@ bubble.innerHTML = r.html; state.streamWrap.appendChild(bubble); } - // 「压缩」总结标签:后端 SSE 事件 is_summary=true 表示本轮回复是压缩总结, - // 在 AI 气泡顶部加个“📋 上下文总结”小标,让 UI 能一眼区分。 - if (d.is_summary && state.streamWrap) { - const tag = document.createElement('div'); - tag.className = 'summary-tag'; - tag.textContent = '📋 上下文总结'; - // 插到气泡顶部、但在思考面板之后(避免被思考面板遮住) - const bubble = state.streamWrap.querySelector('.bubble-ai'); - const think = state.streamWrap.querySelector('.msg-think'); - if (bubble) state.streamWrap.insertBefore(tag, bubble); - else if (think) think.insertAdjacentElement('afterend', tag); - else state.streamWrap.appendChild(tag); - } + // 「压缩」总结面板:复用 .msg-think 的折叠样式(参考思考过程),summary 文字“上下文总结”。 + // 默认 open=true(刚收到总结时展开,方便用户立刻读),后续随同思考面板一起折起。 + // 防重:同一 streamWrap 里已存在 summary 面板则不再重复插入(保护二次重入)。 + if (d.is_summary && state.streamWrap && !state.streamWrap.querySelector('.msg-summary')) { + const det = document.createElement('details'); + det.className = 'msg-summary streaming'; + det.open = true; + const sum = document.createElement('summary'); + sum.textContent = '上下文总结'; + det.appendChild(sum); + // 把总结气泡收进 details 里:点击 summary 折叠后可一起隐藏默认占空间的大段总结。 + const oldBubble = state.streamWrap.querySelector('.bubble-ai'); + if (oldBubble) det.appendChild(oldBubble); + state.streamWrap.appendChild(det); + } if (d.tool_calls) appendToolCardsFromAssistant(d, $('chat-view')); state.streaming = false; // 思考面板默认折叠,避免挡着后续气泡;点击展开 - if (state.streamWrap) { - const t = state.streamWrap.querySelector('.msg-think'); - if (t && t.open) t.open = false; - } + if (state.streamWrap) { + const t = state.streamWrap.querySelector('.msg-think'); + if (t && t.open) t.open = false; + } + // 上下文总结面板同步折起:避免总结泡在后续气泡中占住大面积空间;用户点开可以重现。 + if (state.streamWrap) { + const sm = state.streamWrap.querySelector('.msg-summary'); + if (sm && sm.open) sm.open = false; + } state.streamWrap = null; state.streamReasoning = ''; state.streamThinkEl = null;