Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions apps/web/src/components/chat/optimisticUserMessages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
67 changes: 56 additions & 11 deletions apps/web/src/components/chat/optimisticUserMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, ReadonlyArray<string>>>;
readonly pendingByThread: Readonly<Record<ThreadId, ChatMessage[]>>;
}

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<MessageId>,
): 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<string, ReadonlyArray<string>> = { ...state.handoff };
Expand All @@ -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<ChatMessage[]>,
): 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(
Expand Down
Loading