diff --git a/apps/web/src/components/chat/optimisticUserMessages.test.ts b/apps/web/src/components/chat/optimisticUserMessages.test.ts index 0f7ce644e10bd..563d605de1543 100644 --- a/apps/web/src/components/chat/optimisticUserMessages.test.ts +++ b/apps/web/src/components/chat/optimisticUserMessages.test.ts @@ -61,24 +61,36 @@ describe("optimistic user messages", () => { expect(state.handoff).toEqual({}); }); - it("releases the previous thread's messages when the thread changes", () => { + it("a send the server has not acknowledged is still shown after leaving and returning to its thread", () => { let state = applyOptimisticUserMessagesAction(emptyOptimisticUserMessages(threadA), threadA, [ message("a1"), ]); state = reconcileOptimisticUserMessages(state, threadB, ids()); state = reconcileOptimisticUserMessages(state, threadA, ids()); - expect(state).toEqual(emptyOptimisticUserMessages(threadA)); + expect(state.messages.map((m) => m.id)).toEqual([MessageId.make("a1")]); }); - it("drops a late update for a thread the view has left", () => { - const state = reconcileOptimisticUserMessages( + it("a late update for a thread the view has left applies to that thread's pending sends", () => { + let state = reconcileOptimisticUserMessages( applyOptimisticUserMessagesAction(emptyOptimisticUserMessages(threadA), threadA, [ message("a1"), ]), threadB, ids(), ); - expect(applyOptimisticUserMessagesAction(state, threadA, [message("a2")])).toBe(state); + state = applyOptimisticUserMessagesAction(state, threadA, (existing) => [ + ...existing, + message("a2", "blob:a2"), + ]); + // A failed send leaves, even while its thread is not shown. + state = applyOptimisticUserMessagesAction(state, threadA, (existing) => + existing.filter((m) => m.id !== MessageId.make("a1")), + ); + expect(state.messages).toEqual([]); + state = reconcileOptimisticUserMessages(state, threadA, ids()); + expect(state.messages.map((m) => m.id)).toEqual([MessageId.make("a2")]); + // Leaving the thread revoked its blob previews. + expect(state.messages[0]?.attachments?.[0]).not.toHaveProperty("previewUrl"); }); it("revokes a thread's blob previews once, when their use ends or the thread is left", () => { diff --git a/apps/web/src/components/chat/optimisticUserMessages.ts b/apps/web/src/components/chat/optimisticUserMessages.ts index 34a62f2324b40..facbeea072204 100644 --- a/apps/web/src/components/chat/optimisticUserMessages.ts +++ b/apps/web/src/components/chat/optimisticUserMessages.ts @@ -4,34 +4,76 @@ import * as Effect from "effect/Effect"; import * as Schema from "effect/Schema"; import { AsyncResult, Atom } from "effect/reactivity"; import type { SetStateAction } from "react"; -import type { ChatMessage } from "../../types"; +import { type ChatMessage, isImageAttachment } from "../../types"; import { collectUserMessageBlobPreviewUrls } from "../ChatView.logic"; /** * Messages shown before the server echoes them. An acknowledged message leaves `messages` in the * render its ack arrives, so it never reappears even if the server later drops its row. Its blob * previews stay in `handoff` until the server's copies load (see useAttachmentPreviewHandoff). + * Sends still pending in threads the view has left wait in `pendingByThread` until it returns. */ export interface OptimisticUserMessages { readonly threadId: ThreadId; readonly messages: ChatMessage[]; readonly handoff: Readonly>>; + readonly pendingByThread: Readonly>; } export function emptyOptimisticUserMessages(threadId: ThreadId): OptimisticUserMessages { - return { threadId, messages: [], handoff: {} }; + return { threadId, messages: [], handoff: {}, pendingByThread: {} }; +} + +// Leaving a thread revokes its blob previews, so a message kept for later must not point at them. +function withoutBlobPreviews(message: ChatMessage): ChatMessage { + if (collectUserMessageBlobPreviewUrls(message).length === 0) return message; + return { + ...message, + attachments: message.attachments?.map((attachment) => { + if (!isImageAttachment(attachment) || !attachment.previewUrl?.startsWith("blob:")) { + return attachment; + } + const { previewUrl: _revoked, ...rest } = attachment; + return rest; + }), + }; +} + +function withPending( + pendingByThread: OptimisticUserMessages["pendingByThread"], + threadId: ThreadId, + messages: ChatMessage[], +): OptimisticUserMessages["pendingByThread"] { + const { [threadId]: _previous, ...rest } = pendingByThread; + return messages.length === 0 ? rest : { ...rest, [threadId]: messages.map(withoutBlobPreviews) }; } /** - * The view's state for `threadId`: the previous thread's messages are released when the thread - * changes, and acknowledged messages are moved out of `messages`. Returns `state` when nothing changes. + * The view's state for `threadId`: when the thread changes, the previous thread's unacknowledged + * messages are kept for its return and its handoff is released, and acknowledged messages are moved + * out of `messages`. Returns `state` when nothing changes. */ export function reconcileOptimisticUserMessages( state: OptimisticUserMessages, threadId: ThreadId, acknowledgedIds: ReadonlySet, ): OptimisticUserMessages { - if (state.threadId !== threadId) return emptyOptimisticUserMessages(threadId); + if (state.threadId !== threadId) { + return reconcileOptimisticUserMessages( + { + threadId, + messages: state.pendingByThread[threadId] ?? [], + handoff: {}, + pendingByThread: withPending( + withPending(state.pendingByThread, threadId, []), + state.threadId, + state.messages, + ), + }, + threadId, + acknowledgedIds, + ); + } if (!state.messages.some((message) => acknowledgedIds.has(message.id))) return state; const messages: ChatMessage[] = []; const handoff: Record> = { ...state.handoff }; @@ -46,18 +88,21 @@ export function reconcileOptimisticUserMessages( return { ...state, messages, handoff }; } -/** Applies a send-path update. An update for a thread the view has since left is dropped. */ +/** Applies a send-path update. An update for a thread the view has left applies to its pending sends. */ export function applyOptimisticUserMessagesAction( state: OptimisticUserMessages, threadId: ThreadId, action: SetStateAction, ): OptimisticUserMessages { - if (state.threadId !== threadId) return state; - const next = typeof action === "function" ? action(state.messages) : action; + const current = + state.threadId === threadId ? state.messages : (state.pendingByThread[threadId] ?? []); + const next = typeof action === "function" ? action(current) : action; const unchanged = - next.length === state.messages.length && - next.every((message, index) => message === state.messages[index]); - return unchanged ? state : { ...state, messages: next }; + next.length === current.length && next.every((message, index) => message === current[index]); + if (unchanged) return state; + return state.threadId === threadId + ? { ...state, messages: next } + : { ...state, pendingByThread: withPending(state.pendingByThread, threadId, next) }; } export function dropOptimisticHandoff(