fix: preserve chat order and published page scripts
This commit is contained in:
+35
-2
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user