修复刷新闪烁的问题
This commit is contained in:
@@ -319,11 +319,15 @@
|
||||
if (m.role === 'assistant') return m.type === 'summary' ? summaryMsgHtml(m) : aiMsgHtml(m);
|
||||
return null; // tool 结果不单独展示
|
||||
}
|
||||
// 已渲染消息的轻量签名:role/type 变化(存档、摘要)意味着 DOM 结构要重排,增量路径必须放弃
|
||||
function msgSig(m) { return (m.role || '') + '|' + (m.type || ''); }
|
||||
|
||||
// 把一段消息渲染进目标容器(全量渲染时传 chat-view;懒加载更早页时传离屏 div 再前插进盒子)。
|
||||
// tool 行由后端过滤不返回,工具卡内容展开时由 hookToolCardLazyLoad 按 tool_call_msg_ids 拉取。
|
||||
// 返回 closeArc:若目标是分页前插且页尾存档段还要与现有顶部合并,调用方可不调它(不补「已存档」尾注)。
|
||||
function appendMessagesDom(messages, target) {
|
||||
// 只有真正渲染进主消息流时才记增量跟踪(离屏分页容器不参与,避免切会话时污染新会话的跟踪状态)
|
||||
const track = target === $('chat-view');
|
||||
// 存档段(type='archived',「仅摘要」时摘要之前的旧历史):连成一截淡色容器,
|
||||
// 段尾插一条「已存档、不再发给 AI」说明,紧接摘要卡片,一眼能看出上下文从哪里算起。
|
||||
let arcWrap = null;
|
||||
@@ -343,6 +347,11 @@
|
||||
}
|
||||
const node = msgNode(m);
|
||||
if (node) t.insertAdjacentHTML('beforeend', node);
|
||||
// 记录已渲染消息签名:增量重渲染(trySoftAppend)据此判断能否只追加尾部
|
||||
if (m.id && track) {
|
||||
state.renderedSigs[m.id] = msgSig(m);
|
||||
if (m.id > state.lastRenderedMsgId) state.lastRenderedMsgId = m.id;
|
||||
}
|
||||
if (m.role === 'assistant' && m.tool_call_msg_ids) Object.assign(state.toolCallMsgIds, m.tool_call_msg_ids);
|
||||
if (m.role === 'assistant') appendToolCardsFromAssistant(m, t);
|
||||
}
|
||||
@@ -351,21 +360,66 @@
|
||||
return { closeArc };
|
||||
}
|
||||
|
||||
function renderDetail(detail) {
|
||||
// 增量重渲染:仅当“已渲染的消息仍是新窗口里的连续前缀”时才只追加尾部。
|
||||
// 任何结构变化(存档/摘要改 type、删除、新增过多导致窗口覆盖不到已渲染末条)都返回 false 走全量重建。
|
||||
function trySoftAppend(detail) {
|
||||
const msgs = detail.messages || [];
|
||||
const lastId = state.lastRenderedMsgId;
|
||||
const box = $('chat-view');
|
||||
if (!lastId || !box || !box.firstElementChild || !msgs.length) return false;
|
||||
if (!msgs.some(m => m.id === lastId)) return false; // 新增过多/被删:窗口已不含已渲染末条
|
||||
for (const m of msgs) {
|
||||
if (m.id > lastId) continue;
|
||||
const sig = state.renderedSigs[m.id];
|
||||
// 只比对“确实渲染过”的行(窗口里可能带出未加载过的更早历史)
|
||||
if (sig !== undefined && sig !== msgSig(m)) return false; // 存档/摘要等改 type,必须整树重排
|
||||
}
|
||||
const tail = msgs.filter(m => m.id > lastId);
|
||||
if (tail.length && tail[0].type === 'archived') return false; // 存档段跨增量:交给全量重排
|
||||
// 补齐工具卡懒加载映射(后台期间落库的 tool 行,已渲染的卡片展开时才能取到内容)
|
||||
for (const m of msgs) {
|
||||
if (m.role === 'assistant' && m.tool_call_msg_ids) Object.assign(state.toolCallMsgIds, m.tool_call_msg_ids);
|
||||
}
|
||||
if (tail.length || state.status !== 'active') removeTyping(); // 后台期间本轮已结束但没赶上 done:收掉打字点
|
||||
// 后台期间本轮已收尾(库里有新的 assistant 行):丢弃半截流式气泡,避免与新行重复
|
||||
if (tail.some(m => m.role === 'assistant') && state.streamWrap) {
|
||||
try { state.streamWrap.remove(); } catch (_) {}
|
||||
state.streamWrap = null; state.streamEl = null; state.streamText = '';
|
||||
state.streamReasoning = ''; state.streamThinkEl = null; state.streaming = false;
|
||||
}
|
||||
if (tail.length) {
|
||||
const atBottom = isNearBottom();
|
||||
appendMessagesDom(tail, box).closeArc();
|
||||
state.chatLoadedCount += tail.length;
|
||||
if (atBottom) { userPaused = false; scrollBottom(); }
|
||||
}
|
||||
updateScrollBottomBtn();
|
||||
return true;
|
||||
}
|
||||
|
||||
function renderDetail(detail, opts) {
|
||||
const s = detail.session;
|
||||
state.sessionTitle = (s && s.title) || '';
|
||||
state.sessionType = (s && s.assistant_type) || 'secretary'; // 顶部 icon 跟随会话类型
|
||||
state.status = (s && s.status) || '';
|
||||
state.modelId = (s && s.model_id) || ''; // 顶部动作区展示当前模型
|
||||
state.schedule = detail.schedule || null; // 定时任务数据(顶部动作区副标题)
|
||||
renderHeader();
|
||||
updateHeaderActions();
|
||||
|
||||
// 非破坏性重渲染(回前台补拉/看门狗):只追加新增消息,已有节点、展开态、滚动位置一律不动
|
||||
if (opts && opts.soft && state.sessionId === (s && s.id) && trySoftAppend(detail)) return;
|
||||
|
||||
state.toolCards = {};
|
||||
state.toolCallStreaming = {}; // 流式参数早卡映射随会话/回合重置
|
||||
state.toolCallMsgIds = {}; // tool_call.id → tool 消息行 id(展开时懒加载用)
|
||||
userPaused = false;
|
||||
state.renderedSigs = {};
|
||||
state.lastRenderedMsgId = 0;
|
||||
removeTyping();
|
||||
renderHeader();
|
||||
updateHeaderActions();
|
||||
const box = $('chat-view');
|
||||
const keepView = !!(opts && opts.keepView);
|
||||
const prevTop = keepView ? box.scrollTop : 0;
|
||||
const followBottom = !keepView || !box.firstElementChild || isNearBottom();
|
||||
box.innerHTML = '';
|
||||
appendMessagesDom(detail.messages || [], box).closeArc();
|
||||
if (!(detail.messages || []).length) {
|
||||
@@ -375,7 +429,13 @@
|
||||
state.chatOldestId = detail.oldest_id || null;
|
||||
state.chatLoadedCount = (detail.messages || []).length;
|
||||
hookToolCardLazyLoad();
|
||||
scrollBottom();
|
||||
if (followBottom) {
|
||||
userPaused = false;
|
||||
scrollBottom();
|
||||
} else {
|
||||
userPaused = true; // 用户原本在翻历史:保持暂停跟随与原阅读位置
|
||||
box.scrollTop = prevTop;
|
||||
}
|
||||
updateScrollBottomBtn();
|
||||
// 首屏条数太少撑不满滚动区时不会产生 scroll 事件,上滑永远触发不了加载:自动补一页
|
||||
if (state.chatHasMore && box.scrollHeight <= box.clientHeight + CHAT_TOP_TRIGGER) loadOlderMessages();
|
||||
@@ -456,6 +516,11 @@
|
||||
actionsHtml(Date.now(), content, msgId);
|
||||
decorateCodePre(wrap); // 用户消息里也可能有围栏代码块
|
||||
$('chat-view').appendChild(wrap);
|
||||
// 已落库的行记入增量跟踪:避免回前台补拉时把这条再追加一遍
|
||||
if (msgId) {
|
||||
state.renderedSigs[msgId] = 'user|';
|
||||
if (msgId > state.lastRenderedMsgId) state.lastRenderedMsgId = msgId;
|
||||
}
|
||||
scrollBottom();
|
||||
}
|
||||
|
||||
|
||||
@@ -188,6 +188,8 @@
|
||||
chatHasMore: false, // 当前会话是否还有更早的历史未加载(消息懒加载分页)
|
||||
chatOldestId: null, // 已加载最早一条消息的行 id(拉更早页的 before 游标)
|
||||
chatLoadedCount: 0, // 已加载消息条数(重拉详情时按同量分页,避免退回全量渲染)
|
||||
lastRenderedMsgId: 0, // 已渲染的最新一条消息行 id(增量重渲染判断“只追加尾部”)
|
||||
renderedSigs: {}, // 行 id -> role|type 签名(检测存档/摘要等结构变化,决定是否全量重建)
|
||||
};
|
||||
|
||||
// ---------- 视图切换 ----------
|
||||
|
||||
@@ -83,6 +83,8 @@
|
||||
state.streamText = '';
|
||||
state.toolCards = {};
|
||||
state.toolCallStreaming = {}; // 流式参数早卡映射随会话/回合重置
|
||||
state.lastRenderedMsgId = 0;
|
||||
state.renderedSigs = {};
|
||||
state.streaming = false;
|
||||
setView('welcome'); // 复用 newChat 同样的视图切换路径
|
||||
}
|
||||
@@ -109,8 +111,8 @@
|
||||
sseResyncing = true;
|
||||
state.lastSseAt = Date.now(); // 计时从现在开始,失败也等下个周期再试
|
||||
closeStream();
|
||||
req('GET', '/api/sessions/' + id + '?limit=' + CHAT_INIT_PAGE_SIZE).then(detail => {
|
||||
if (state.sessionId === id && state.view === 'chat') renderDetail(detail);
|
||||
req('GET', '/api/sessions/' + id + '?limit=' + Math.max(CHAT_PAGE_SIZE, state.chatLoadedCount + 20)).then(detail => {
|
||||
if (state.sessionId === id && state.view === 'chat') renderDetail(detail, { soft: true, keepView: true });
|
||||
}).catch(() => {}).then(() => {
|
||||
sseResyncing = false;
|
||||
if (state.sessionId === id && state.view === 'chat' && !state.es) connectStream();
|
||||
@@ -137,17 +139,45 @@
|
||||
sseSuspended = false;
|
||||
resyncSession();
|
||||
}
|
||||
// 回前台宽限:后台里事件只是被暂停时,恢复后积压的 SSE 事件会立刻补达;
|
||||
// 先给现有连接一个自证存活的窗口,收到任何事件就不再做整库补拉(避免把“后台没收到 ping”误判为断线)。
|
||||
const SSE_RESUME_GRACE_MS = 1500;
|
||||
let sseResumeProbeTimer = 0;
|
||||
let sseResumeSawEvent = false;
|
||||
function markSseAlive(evName) {
|
||||
state.lastSseAt = Date.now();
|
||||
// status 只会在建连/重连时到来:它证不了断点期没丢事件,不能当作宽限期存活证据
|
||||
if (evName !== 'status') sseResumeSawEvent = true;
|
||||
}
|
||||
function probeSseAlive() {
|
||||
const id = state.sessionId;
|
||||
sseResumeSawEvent = false;
|
||||
clearTimeout(sseResumeProbeTimer);
|
||||
sseResumeProbeTimer = setTimeout(() => {
|
||||
sseResumeProbeTimer = 0;
|
||||
if (sseResumeSawEvent) return; // 宽限期内有事件:连接活着,无需补拉
|
||||
if (state.sessionId === id && state.view === 'chat' && state.es) resyncSession();
|
||||
}, SSE_RESUME_GRACE_MS);
|
||||
}
|
||||
// 后台标签页里 interval 被限频;切回前台立刻补一次判定,自动覆盖“休眠/切走醒来后页面停在半截”场景
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (document.hidden) {
|
||||
clearTimeout(sseSuspendTimer);
|
||||
clearTimeout(sseResumeProbeTimer);
|
||||
sseResumeProbeTimer = 0;
|
||||
sseSuspendTimer = setTimeout(suspendStream, SSE_SUSPEND_HIDDEN_MS);
|
||||
return;
|
||||
}
|
||||
clearTimeout(sseSuspendTimer);
|
||||
resumeStream();
|
||||
if (state.sessionId && state.view === 'chat' && state.es
|
||||
&& Date.now() - state.lastSseAt > 45000) resyncSession();
|
||||
clearTimeout(sseResumeProbeTimer);
|
||||
sseResumeProbeTimer = 0;
|
||||
// 后台已挂起:客户端不在线期间的 SSE 事件确定丢弃,必须补拉(增量渲染,不闪)
|
||||
if (sseSuspended) { resumeStream(); return; }
|
||||
if (!state.sessionId || state.view !== 'chat' || !state.es) return;
|
||||
// 连接已不是 OPEN(曾掉线/正在重连):断点期间的事件可能已丢,直接补拉
|
||||
if (state.es.readyState !== EventSource.OPEN) { resyncSession(); return; }
|
||||
// 仍 OPEN 但心跳变旧:先给连接一个宽限自证,真静默才补拉
|
||||
if (Date.now() - state.lastSseAt > 45000) probeSseAlive();
|
||||
});
|
||||
|
||||
async function openSession(id) {
|
||||
@@ -174,6 +204,8 @@
|
||||
state.chatHasMore = false; // 分页状态先复位:防详情返回前用旧会话的游标/标志拉到错会话的消息
|
||||
state.chatOldestId = null;
|
||||
state.chatLoadedCount = 0;
|
||||
state.lastRenderedMsgId = 0;
|
||||
state.renderedSigs = {};
|
||||
renderAttachChips();
|
||||
clearUploadCards(); // 切会话清掉旧会话的上传卡片
|
||||
renderDrawerList(); // 会话切换后立刻重画抽屉高亮,避免下次打开才看到更新
|
||||
@@ -221,6 +253,8 @@
|
||||
state.streamText = '';
|
||||
state.toolCards = {};
|
||||
state.toolCallStreaming = {}; // 流式参数早卡映射随会话/回合重置
|
||||
state.lastRenderedMsgId = 0;
|
||||
state.renderedSigs = {};
|
||||
state.streaming = false;
|
||||
userPaused = false;
|
||||
renderAttachChips();
|
||||
|
||||
@@ -134,7 +134,10 @@
|
||||
const es = new EventSource('/api/sessions/' + state.sessionId + '/stream');
|
||||
state.es = es;
|
||||
state.lastSseAt = Date.now(); // 建连即重置静默计时
|
||||
es.addEventListener('ping', () => { state.lastSseAt = Date.now(); }); // 服务端 15s 可见心跳
|
||||
// 任何具名事件都算连接存活:静默看门狗与回前台宽限共用(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']
|
||||
.forEach(function (n) { es.addEventListener(n, function () { markSseAlive(n); }); });
|
||||
es.addEventListener('status', ev => {
|
||||
const d = JSON.parse(ev.data);
|
||||
state.status = d.status;
|
||||
@@ -170,6 +173,11 @@
|
||||
// 防重:同 id 的 assistant_message 只处理一次(防御 EventSource 自动重连、服务端重发等情况)
|
||||
if (d.id && state.lastAssistantMsgId === d.id) return;
|
||||
if (d.id) state.lastAssistantMsgId = d.id;
|
||||
// 已落库的回合记入增量跟踪(否则回前台补拉会把同一条 assistant 再追加一遍)
|
||||
if (d.id) {
|
||||
state.renderedSigs[d.id] = 'assistant|' + (d.is_summary ? 'summary' : '');
|
||||
if (d.id > state.lastRenderedMsgId) state.lastRenderedMsgId = d.id;
|
||||
}
|
||||
removeTyping();
|
||||
const hasText = !!(d.content && d.content.trim());
|
||||
const r = hasText ? mdHtml(d.content) : null;
|
||||
|
||||
Reference in New Issue
Block a user