fix(pr-view): Update mergeMessages to main

This commit is contained in:
cmanu
2026-08-10 08:11:27 -07:00
parent 0a6b39f618
commit d074e0eb4b
@@ -1,7 +1,9 @@
import type { Message, MessageLoadMode, Part } from "../types/messages"
import { sameParts } from "./session-parts"
import { withoutResolvedSessionErrors } from "./session-errors"
export function mergeMessages(current: Message[], incoming: Message[], mode: Exclude<MessageLoadMode, "focus">) {
const kept = withoutResolvedSessionErrors(current, incoming)
if (mode === "reconcile") {
// Tail reconcile: incoming is the authoritative newest-N snapshot.
// Local state may already hold some of those IDs and may also hold
@@ -10,12 +12,12 @@ export function mergeMessages(current: Message[], incoming: Message[], mode: Exc
// server messages land in the right position and optimistic tail
// entries stay at the end.
const byId = new Map<string, Message>()
for (const msg of current) byId.set(msg.id, msg)
for (const msg of kept) byId.set(msg.id, msg)
for (const msg of incoming) byId.set(msg.id, msg)
return [...byId.values()].sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime())
}
const seen = new Set<string>()
const source = mode === "prepend" ? [...incoming, ...current] : incoming
const source = mode === "prepend" ? [...incoming, ...kept] : incoming
return source.filter((msg) => {
if (seen.has(msg.id)) return false
seen.add(msg.id)