修复:SSE 断档页面假死——可见心跳 + 静默看门狗自动重同步
根因:/messages 已进服务端但页面不刷新——流断档窗口内的事件(assistant_message/done 等)在 sse.js 无客户端即丢弃、不重放;EventSource 多半已自动重连,缺的是「重新拉库」。手动切会话再回来能恢复,正是 renderDetail 重拉的效果。 - server: 心跳从注释帧 `: ping` 改为具名事件 event: ping(注释帧不触发前端 JS 事件,无法做静默检测;对代理保活等价) - 前端: connectStream 记录 state.lastSseAt + ping 监听刷新;watchdog 每 5s 判定,会话页且静默 >45s(3 个心跳周期)→ resyncSession():closeStream + GET limit + renderDetail + connectStream,即自动化「切走再切回」 - visibilitychange 回前台立即补判(后台 interval 被浏览器限频,覆盖休眠/长切走场景) - 防抖:sseResyncing 互斥 + 每次重同步重置计时,失败按 45s 节奏重试;草稿/输入框不受影响(只重绘消息区)
This commit is contained in:
@@ -510,6 +510,7 @@
|
||||
attachments: [], // 待发送附件 {file, name, size}
|
||||
lastImgBlobUrl: null, // 任务文件图片预览的 blob URL,下次预览或开新图时回收
|
||||
es: null, // EventSource
|
||||
lastSseAt: 0, // 最近一次收到 SSE 心跳时刻(看门狗静默判死用)
|
||||
streamText: '', // 当前流式累积文本
|
||||
streamEl: null, // 当前流式 AI 气泡
|
||||
streamWrap: null, // 当前流式消息容器(.msg.other);思考面板与气泡都挂它上面
|
||||
@@ -1132,6 +1133,33 @@
|
||||
state.pendingSummary = false;
|
||||
}
|
||||
|
||||
// ---------- SSE 静默看门狗 ----------
|
||||
// 流断档期的 assistant_message/tool_* 等事件不会重放(sse.js 无客户端即丢弃),页面会停在“无回应”假死;
|
||||
// 而 EventSource 本身多半已自动重连——真正缺的是重新拉一次库。服务端每 15s 发可见心跳(event: ping),
|
||||
// 静默超 45s 即判死,自动做一次“切走再切回”式重同步(resyncSession);重连失败则 45s 后再试,不刷屏。
|
||||
let sseResyncing = false;
|
||||
function resyncSession() {
|
||||
const id = state.sessionId;
|
||||
if (!id || state.view !== 'chat' || sseResyncing) return;
|
||||
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);
|
||||
}).catch(() => {}).then(() => {
|
||||
sseResyncing = false;
|
||||
if (state.sessionId === id && state.view === 'chat' && !state.es) connectStream();
|
||||
});
|
||||
}
|
||||
setInterval(() => {
|
||||
if (state.sessionId && state.view === 'chat' && state.es && Date.now() - state.lastSseAt > 45000) resyncSession();
|
||||
}, 5000);
|
||||
// 后台标签页里 interval 被限频;切回前台立刻补一次判定,自动覆盖“休眠/切走醒来后页面停在半截”场景
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (!document.hidden && state.sessionId && state.view === 'chat' && state.es
|
||||
&& Date.now() - state.lastSseAt > 45000) resyncSession();
|
||||
});
|
||||
|
||||
async function openSession(id) {
|
||||
closeStream();
|
||||
removeTyping();
|
||||
@@ -1574,6 +1602,8 @@
|
||||
if (!state.sessionId) return;
|
||||
const es = new EventSource('/api/sessions/' + state.sessionId + '/stream');
|
||||
state.es = es;
|
||||
state.lastSseAt = Date.now(); // 建连即重置静默计时
|
||||
es.addEventListener('ping', () => { state.lastSseAt = Date.now(); }); // 服务端 15s 可见心跳
|
||||
es.addEventListener('status', ev => {
|
||||
const d = JSON.parse(ev.data);
|
||||
state.status = d.status;
|
||||
|
||||
Reference in New Issue
Block a user