feat(ui): 上下文注入行改成折叠卡片,摘要卡片统一同一套视觉

画像快照、渠道说明、开场记忆这些「只发给模型看」的注入内容,之前要么完全
不可见(画像快照存在 sessions 表里,不在 messages),要么假装成用户发言——
channel_note / memory_note 会被 userMsgHtml 渲染成一条用户气泡,打开微信
会话时那两段 # [CTX] 大文本就挂在你名下。

现在统一成可折叠卡片:画像快照是每轮请求拼在最前的上下文,历史又是懒加载
的,所以等翻到最早(has_more=false)再补到流顶,不会插在首屏那几条前面被
后续分页顶到中间;渠道/记忆按 type 直接渲染成卡片。内容都走 markdown,和
摘要卡片一致。

压缩摘要卡片与它们共用一套:淡蓝底、蓝边蓝字、三角箭头、虚线分隔,折叠与
展开同底色,去掉渐变/阴影/数据库图标,默认全部收起(流式收尾也不再自动
展开),标题旁的「后续对话以此为起点」一并去掉。摘要卡片补 width:100%,
否则在 align-items:flex-start 的 .msg 里折叠后只剩标题那点宽。

顺带:errAck(工具参数非法被整轮移除)在气泡上方标一行,原来只显示模型
当时的内容,看不出这一轮其实没进上下文。
This commit is contained in:
2026-11-13 14:52:24 +08:00
parent b9fad470ee
commit 793be29331
4 changed files with 70 additions and 24 deletions
+30
View File
@@ -225,6 +225,36 @@ main { flex: 1; min-height: 0; display: flex; flex-direction: column; }
/* 流式阶段给一点视觉提示:边色稍亮,收尾(assistant_message)后去除 */
.msg-think.streaming { border-color: #d8e0f5; }
/* 上下文卡片:只发给模型看的注入行(画像快照 / 渠道说明 / 开场记忆)折叠展示,
视觉与压缩摘要卡片一致:淡蓝底、蓝边蓝字、三角箭头、虚线分隔,不用渐变/阴影 */
.msg-ctx {
margin: 0 0 12px; border: 1px solid #dbe4fb; border-radius: 12px; overflow: hidden;
background: #f7f9ff;
}
.msg-ctx > summary {
list-style: none; cursor: pointer; user-select: none;
display: flex; align-items: center; gap: 7px;
padding: 9px 12px; font-size: 13px; font-weight: 600; color: var(--active);
}
.msg-ctx > summary::-webkit-details-marker { display: none; }
.msg-ctx > summary::before {
content: ''; flex: none; width: 0; height: 0; margin-left: 1px;
border-left: 5px solid var(--active); border-top: 4px solid transparent; border-bottom: 4px solid transparent;
transition: transform .15s ease;
}
.msg-ctx[open] > summary::before { transform: rotate(90deg); }
.msg-ctx-body {
/* 比标题区(#f7f9ff)再淡一档,分隔开又不跳色 */
border-top: 1px dashed #dbe4fb; background: #fbfcff;
padding: 10px 13px 12px;
}
.msg-ctx-body .bubble-ai { margin: 0; background: transparent; font-size: 14px; line-height: 1.7; }
/* errAck:这一轮工具参数非法、已从上下文移除,气泡上方标一行 */
.msg-note {
margin: 0 0 6px; padding: 3px 9px; border-radius: 6px;
background: #fff8c5; color: #9a6700; font-size: 12px;
}
/* 用户气泡:右侧,浅色背景,圆角矩形 */
.bubble-user {
background: var(--active-soft); color: var(--ink);
+7 -9
View File
@@ -17,13 +17,12 @@
<link rel="icon" type="image/png" sizes="192x192" href="/icons/icon-192.png">
<link rel="stylesheet" href="/css/style.css">
<style>
/* 「压缩」摘要卡片:与 .msg-think 同族的折叠交互,但做成更醒目的蓝色调卡片;
流式收尾(live)和历史重渲染(type='summary' 消息)共用同一套样式。
live 阶段 open=true 展开阅读;历史重渲染默认收起。 */
/* 「压缩」摘要卡片:与 .msg-ctx(画像快照/渠道/记忆)同一套视觉——淡蓝底、蓝边蓝字、
三角箭头、虚线分隔。纯色不用渐变/阴影,折叠与展开同一底色。默认收起 */
.msg-summary {
width: 100%; /* .msg.other 是 align-items:flex-start 的列 flex,不写就只撑标题那点宽 */
margin: 0 0 8px; border: 1px solid #dbe4fb; border-radius: 12px; overflow: hidden;
background: linear-gradient(180deg, #f7f9ff 0%, #ffffff 56%);
box-shadow: 0 1px 3px rgba(57, 100, 254, 0.08);
background: #f7f9ff;
}
.msg-summary > summary {
list-style: none; cursor: pointer; user-select: none;
@@ -37,13 +36,12 @@
transition: transform .15s ease;
}
.msg-summary[open] > summary::before { transform: rotate(90deg); }
.msg-summary .sum-ic { flex: none; width: 15px; height: 15px; }
.msg-summary .sum-hint { margin-left: auto; font-size: 11px; font-weight: 400; color: var(--gray); }
.msg-summary-body {
border-top: 1px dashed #dbe4fb; background: var(--page);
/* 比标题区(#f7f9ff)再淡一档,分隔开又不跳色 */
border-top: 1px dashed #dbe4fb; background: #fbfcff;
padding: 10px 13px 12px;
}
.msg-summary-body .bubble-ai { margin: 0; font-size: 14px; line-height: 1.7; }
.msg-summary-body .bubble-ai { margin: 0; background: transparent; font-size: 14px; line-height: 1.7; }
/* 「仅摘要」后摘要之前的旧历史:UI 保留但变淡,尾部附存档说明(不再发给 AI) */
.msg-archived { opacity: .55; }
.archived-note {
+32 -13
View File
@@ -56,6 +56,21 @@
'</div>';
}
// 上下文卡片:画像快照 / 渠道说明 / 开场记忆这类「只发给模型看的注入行」,
// 统一渲染成可折叠的系统卡片,而不是伪装成用户发言。
function ctxCardHtml(title, text, id) {
const r = mdHtml(text || ''); // 与摘要卡片同样走 markdown,保持四张卡片观感一致
return '<details class="msg-ctx"' + (id ? ' id="' + id + '"' : '') + '><summary>' + esc(title) + '</summary>'
+ '<div class="msg-ctx-body"><div class="bubble-ai' + (r.fmt ? ' md' : '') + '">' + r.html + '</div></div></details>';
}
// 画像快照是每轮请求拼在最前的上下文(不在 messages 表里)。历史是懒加载的,首屏只拉最近几条,
// 所以不能一打开就插,得等翻到最早、确认没有更早的消息了再放到最顶。
function ensureProfileCard(box) {
if (!state.profileSnapshot || !box || box.querySelector('#ctx-profile')) return;
box.insertAdjacentHTML('afterbegin', ctxCardHtml('画像快照', state.profileSnapshot, 'ctx-profile'));
}
function userMsgHtml(m) {
const p = parseUploadNotice(m.content);
const attach = p.files && p.files.length
@@ -72,10 +87,14 @@
function aiMsgHtml(m) {
const text = (m.content || '').trim();
const reason = (m.reasoning || '').trim();
if (!text && !reason) return null; // 纯工具调用的应答:不渲染气泡,工具卡由 appendToolCardsFromAssistant 铺
const errAck = m.type === 'errAck';
// 纯工具调用的应答不渲染气泡(工具卡由 appendToolCardsFromAssistant 铺),但 errAck 要留个标记
if (!text && !reason && !errAck) return null;
const note = errAck
? '<div class="msg-note">工具调用参数无效,本轮已从上下文移除,模型会重新发起</div>' : '';
const think = reason ? thinkPanelHtml(m.reasoning) : '';
const bubble = text ? (() => { const r = mdHtml(m.content); return '<div class="bubble-ai' + (r.fmt ? ' md' : '') + '">' + r.html + '</div>'; })() : '';
return '<div class="msg other">' + think + bubble + actionsHtml(m.created_at, m.content || '', m.id, m.usage) + '</div>';
return '<div class="msg other">' + note + think + bubble + actionsHtml(m.created_at, m.content || '', m.id, m.usage) + '</div>';
}
// 思考过程折叠面板:summary 固定名「思考过程」+ 右侧复制按钮,body 展示推理全文。
// 历史重渲染默认收起(与原设计一致),流式阶段的展开行为由 ensureStreamThinkPanel 显式控制。
@@ -118,15 +137,7 @@
// 「压缩」摘要卡片:流式收尾与历史重渲染共用的头部(图标 + 标题 + 右侧说明)。
function summaryHeaderHtml() {
return '<summary>' +
'<svg class="sum-ic" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round">' +
'<ellipse cx="12" cy="5" rx="8" ry="2.5"></ellipse>' +
'<path d="M4 5v6c0 1.4 3.6 2.5 8 2.5s8-1.1 8-2.5V5"></path>' +
'<path d="M4 11v6c0 1.4 3.6 2.5 8 2.5s8-1.1 8-2.5v-6"></path>' +
'</svg>' +
'<span>上下文总结</span>' +
'<span class="sum-hint">后续对话以此为起点</span>' +
'</summary>';
return '<summary><span>上下文总结</span></summary>';
}
// 历史重渲染:被标记 type='summary' 的摘要消息渲染成摘要卡片(默认收起,点开可重现)。
@@ -322,7 +333,11 @@
}
function msgNode(m) {
if (m.role === 'user') return userMsgHtml(m);
if (m.role === 'user') {
if (m.type === 'channel_note') return ctxCardHtml('渠道上下文', m.content);
if (m.type === 'memory_note') return ctxCardHtml('开场记忆', m.content);
return userMsgHtml(m);
}
// 被「压缩总结」标记的摘要消息:重渲染成摘要卡片,而不是普通 AI 气泡
if (m.role === 'assistant') return m.type === 'summary' ? summaryMsgHtml(m) : aiMsgHtml(m);
return null; // tool 结果不单独展示
@@ -431,9 +446,12 @@
const prevTop = keepView ? box.scrollTop : 0;
const followBottom = !keepView || !box.firstElementChild || isNearBottom();
box.innerHTML = '';
state.profileSnapshot = (s && s.profile_snapshot) || '';
appendMessagesDom(detail.messages || [], box).closeArc();
// 首屏就把历史拉完了(条数少):直接上画像卡片;否则等上滑翻到最早再插
if (!detail.has_more) ensureProfileCard(box);
if (!(detail.messages || []).length) {
box.innerHTML = '<div class="drawer-empty">描述你的需求,AI 会和你讨论并直接干活。</div>';
box.insertAdjacentHTML('beforeend', '<div class="drawer-empty">描述你的需求,AI 会和你讨论并直接干活。</div>');
}
state.chatHasMore = !!detail.has_more;
state.chatOldestId = detail.oldest_id || null;
@@ -501,6 +519,7 @@
while (frag.firstChild) box.insertBefore(frag.firstChild, anchor);
box.scrollTop = box.scrollHeight - prevH + prevTop;
state.chatHasMore = !!detail.has_more;
if (!detail.has_more) ensureProfileCard(box); // 翻到最早了:画像卡片补到流最顶
if (detail.oldest_id) state.chatOldestId = detail.oldest_id;
state.chatLoadedCount += (detail.messages || []).length;
// 旧页太短导致补完后仍贴在触发区:继续拉下一页,避免卡在顶部要再动一下才加载
+1 -2
View File
@@ -308,13 +308,12 @@
// 「压缩」总结回合的最终答复(后端已保证只有压缩回合里首条无工具调用的收尾回复才带 is_summary,
// 中间 AI 回复已按普通消息渲染):把已流式输出的气泡装进摘要卡片(与历史重渲染同款样式),
// 并标记 wrap 为 msg-summary-live:archiveDomBeforeSummary 据此定位摘要卡片,之前的旧消息收进存档容器。
// open=true 让用户立刻读到;后续轮次不自动折起(卡片可手动点 summary 收起)。
// 摘要卡片默认收起(与画像快照/渠道/记忆同一种交互),用户想读时点开。
// 防重:同一 streamWrap 已有卡片则不再重复插入(保护 SSE 重连重发等场景)。
if (d.is_summary && hasText && state.streamWrap && !state.streamWrap.querySelector('.msg-summary')) {
const wrap0 = state.streamWrap;
const det = document.createElement('details');
det.className = 'msg-summary';
det.open = true;
det.innerHTML = summaryHeaderHtml() + '<div class="msg-summary-body"></div>';
const body = det.querySelector('.msg-summary-body');
const bubble = wrap0.querySelector('.bubble-ai');