fix: preserve chat order and published page scripts

This commit is contained in:
john
2026-07-15 14:05:09 +08:00
parent b3337c9643
commit 933466c8ab
5 changed files with 97 additions and 8 deletions
+35 -2
View File
@@ -30,8 +30,41 @@ export function mergeConversationSnapshot(current, incoming) {
}
const base = Array.isArray(current) ? current : [];
const incomingIds = new Set(incoming.map((message) => message?.id).filter(Boolean));
const localOnly = base.filter((message) => message?.id && !incomingIds.has(message.id));
return localOnly.length ? [...incoming, ...localOnly] : incoming;
const localOnlyByNextAnchor = new Map();
// Keep the regression-guard behaviour of retaining local streamed messages,
// but put them back between the same server messages instead of appending the
// whole local tail. Finish/UpdateConversation snapshots can temporarily omit
// a middle message while Goose is still persisting it.
for (let index = 0; index < base.length; index += 1) {
const message = base[index];
if (!message?.id || incomingIds.has(message.id)) continue;
let nextAnchor = null;
for (let cursor = index + 1; cursor < base.length; cursor += 1) {
const candidateId = base[cursor]?.id;
if (candidateId && incomingIds.has(candidateId)) {
nextAnchor = candidateId;
break;
}
}
const bucket = localOnlyByNextAnchor.get(nextAnchor) ?? [];
bucket.push(message);
localOnlyByNextAnchor.set(nextAnchor, bucket);
}
if (localOnlyByNextAnchor.size === 0) return incoming;
const merged = [];
for (const message of incoming) {
const before = localOnlyByNextAnchor.get(message?.id);
if (before?.length) merged.push(...before);
merged.push(message);
}
const trailing = localOnlyByNextAnchor.get(null);
if (trailing?.length) merged.push(...trailing);
return merged;
}
/**