Fix optimistic user message reconciliation

This commit is contained in:
Mohamed Boudra
2026-05-26 11:37:13 +07:00
parent 483790d8c9
commit a3071b46a4
4 changed files with 37 additions and 22 deletions
@@ -326,7 +326,7 @@ describe("processTimelineResponse", () => {
...makeTimelineEntry(1, "Analyze this", "user_message"),
item: {
type: "user_message",
text: "Analyze this",
text: "server-rendered attachment text",
messageId: "canonical-create-user",
},
},
@@ -339,6 +339,7 @@ describe("processTimelineResponse", () => {
expect(userMessages[0]).toMatchObject({
id: "canonical-create-user",
text: "Analyze this",
timestamp: new Date(1000),
images: [image],
attachments: [attachment],
});
@@ -295,16 +295,15 @@ function mergeCanonicalUserWithOptimistic(
canonical: UserMessageItem,
optimistic: UserMessageItem,
): UserMessageItem {
const shouldPreserveImages =
(!canonical.images || canonical.images.length === 0) &&
Boolean(optimistic.images && optimistic.images.length > 0);
const shouldPreserveAttachments =
(!canonical.attachments || canonical.attachments.length === 0) &&
Boolean(optimistic.attachments && optimistic.attachments.length > 0);
return {
...canonical,
...(shouldPreserveImages ? { images: optimistic.images } : {}),
...(shouldPreserveAttachments ? { attachments: optimistic.attachments } : {}),
kind: "user_message",
id: canonical.id,
text: optimistic.text,
timestamp: optimistic.timestamp,
...(optimistic.images && optimistic.images.length > 0 ? { images: optimistic.images } : {}),
...(optimistic.attachments && optimistic.attachments.length > 0
? { attachments: optimistic.attachments }
: {}),
};
}