From a76bbdeaee9d736985f5630b3804bdeb37e16d6d Mon Sep 17 00:00:00 2001 From: kyugao Date: Mon, 3 Aug 2026 12:06:21 +0800 Subject: [PATCH] fix: drain initial message sync before polling --- tauri/src/main.ts | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/tauri/src/main.ts b/tauri/src/main.ts index eaeba85..15176e7 100644 --- a/tauri/src/main.ts +++ b/tauri/src/main.ts @@ -152,12 +152,36 @@ async function syncLoop(): Promise { polling = true; while (session.active) { try { - const response = await fetch(api("/messages/sync"), { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ channel_id: session.channelID, channel_type: session.channelType, login_uid: session.uid, start_message_seq: session.lastSeq === 0 ? 0 : session.lastSeq + 1, limit: 50 }) }); - const body = await response.json().catch(() => ({})) as { error?: string; messages?: SyncedMessage[] }; - if (!response.ok) throw new Error(body.error || "同步失败"); - for (const message of body.messages ?? []) { - session.lastSeq = Math.max(session.lastSeq, message.message_seq); - if (message.from_uid !== session.uid) render(decodeConversationItem(decodeBase64(message.payload))); + // WuKongIM's legacy sync endpoint can return a single retained message + // per request even when `more` is 0. Drain until it is empty before + // switching to the paced live-polling loop; otherwise a reopened chat + // with history needs minutes to reach the latest Agent response. + while (session.active) { + const startMessageSeq = session.lastSeq === 0 ? 0 : session.lastSeq + 1; + const response = await fetch(api("/messages/sync"), { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ channel_id: session.channelID, channel_type: session.channelType, login_uid: session.uid, start_message_seq: startMessageSeq, limit: 50 }) }); + const body = await response.json().catch(() => ({})) as { error?: string; messages?: SyncedMessage[] }; + if (!response.ok) throw new Error(body.error || "同步失败"); + + const synced = body.messages ?? []; + if (synced.length === 0) { + setPresence("已同步,等待消息"); + break; + } + + let advanced = false; + for (const message of synced) { + const previousSeq = session.lastSeq; + session.lastSeq = Math.max(session.lastSeq, message.message_seq); + advanced ||= session.lastSeq > previousSeq; + if (message.from_uid !== session.uid) render(decodeConversationItem(decodeBase64(message.payload))); + } + + // Avoid a hot loop if an upstream response repeats a sequence that + // cannot advance the local cursor. + if (!advanced) { + setPresence("已同步,等待消息"); + break; + } } } catch { if (session.active) setPresence("同步中断,正在重试…");