Merge branch 'main' into web-modal-focus
This commit is contained in:
@@ -576,8 +576,31 @@ function onChatEvent(frame) {
|
||||
if (!sessionId || !state.chatThreads.has(sessionId)) return;
|
||||
markBackgroundChatUnseen(sessionId);
|
||||
const thread = state.chatThreads.get(sessionId);
|
||||
const messageId = String(frame?.message_id ?? "");
|
||||
// The user turn of a prompt sent from another client (the OpenCode UI, not this
|
||||
// page) never reaches the broker, so no `user` frame exists for it: only the
|
||||
// assistant's frames arrive. When the first frame of an unknown turn arrives,
|
||||
// read the newest history once so the user message (and anything else the page
|
||||
// missed) appears without a manual refresh.
|
||||
const knownTurn = messageId !== "" && (thread.messages ?? []).some((m) => m?.id === messageId);
|
||||
state.chatThreads.set(sessionId, applyChatEvent(thread, frame, Date.now()));
|
||||
renderContent();
|
||||
if (!knownTurn) scheduleHistorySync(sessionId, messageId);
|
||||
}
|
||||
|
||||
/** At most one history read per unseen turn, and only for the open conversation. */
|
||||
const historySyncTimers = new Map();
|
||||
const historySyncedFor = new Map();
|
||||
function scheduleHistorySync(sessionId, messageId) {
|
||||
if (!messageId || sessionId !== state.chatActiveId) return;
|
||||
if (historySyncedFor.get(sessionId) === messageId) return;
|
||||
historySyncedFor.set(sessionId, messageId);
|
||||
if (historySyncTimers.has(sessionId)) return;
|
||||
const timer = setTimeout(() => {
|
||||
historySyncTimers.delete(sessionId);
|
||||
loadChatHistory(sessionId).catch(() => {});
|
||||
}, 250);
|
||||
historySyncTimers.set(sessionId, timer);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user