The best way to control Claude Code, Codex and more from any computer or phone
refactor(web): optimistic chat messages leave without a timer (#25710)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 26, 2026 at 19:19 UTC
71ba6b374c0b0181130c0cc3b3d5aaa3387ada67
6 files changed
+489
-276
apps/web/src/components/ChatView.logic.ts
-19
@@ -667,13 +667,6 @@ export function resolveTerminalWorktreePath(input: {
667
return input.threadWorktreePath;
668
}
669
670
-export function revokeBlobPreviewUrl(previewUrl: string | undefined): void {
671
- if (!previewUrl || typeof URL === "undefined" || !previewUrl.startsWith("blob:")) {
672
- return;
673
- }
674
- URL.revokeObjectURL(previewUrl);
675
-}
676
-
670
export async function resolveFileAttachmentUrl(input: {
671
attachment: ChatFileAttachment;
672
environmentId: EnvironmentId;
@@ -733,18 +726,6 @@ export async function prepareRevertedMessageAttachments(input: {
726
);
727
}
728
736
-export function revokeUserMessagePreviewUrls(message: ChatMessage): void {
737
- if (message.role !== "user" || !message.attachments) {
738
- return;
739
- }
740
- for (const attachment of message.attachments) {
741
- if (!isImageAttachment(attachment)) {
742
- continue;
743
- }
744
- revokeBlobPreviewUrl(attachment.previewUrl);
745
- }
746
-}
747
-
729
export function timelineHasEphemeralPreviewUrls(
730
entries: ReadonlyArray<Pick<TimelineEntry, "kind"> & { message?: ChatMessage }>,
731
): boolean {
apps/web/src/components/ChatView.tsx
+41
-57
@@ -13,6 +13,12 @@ import { usePullRequestDraftThread } from "./chat/usePullRequestDraftThread";
13
import { useComposerSelection } from "./chat/useComposerSelection";
14
import { useProjectCloneBanner } from "./chat/useProjectCloneBanner";
15
import { useAttachmentPreviewHandoff } from "./chat/useAttachmentPreviewHandoff";
16
+import {
17
+ applyOptimisticUserMessagesAction,
18
+ createOptimisticBlobPreviews,
19
+ emptyOptimisticUserMessages,
20
+ reconcileOptimisticUserMessages,
21
+} from "./chat/optimisticUserMessages";
22
import { useTimelineRewind } from "./chat/useTimelineRewind";
23
import { useThreadLifecycle } from "./chat/useThreadLifecycle";
24
import { useRightPanelActions } from "./chat/useRightPanelActions";
@@ -321,7 +327,6 @@ import {
327
} from "./chat/draftHeroTransition";
328
import {
329
buildLocalDraftThread,
324
- collectUserMessageBlobPreviewUrls,
330
deriveCommittedServerUserMessageIds,
331
dismissBranchMismatchForSession,
332
shouldShowPlanFollowUpPrompt,
@@ -332,7 +337,6 @@ import {
337
resolveComposerProviderSelection,
338
resolveThreadMetadataUpdateForNextTurn,
339
resolveSendEnvMode,
335
- revokeUserMessagePreviewUrls,
340
startNewThreadForProject,
341
codexArtifactTemplatePromptToAppend,
342
shouldRefocusComposerOnWindowFocus,
@@ -631,7 +635,26 @@ export default function ChatView(props: ChatViewProps) {
635
};
636
const citeAssistantTextStable = useStableCallback(citeAssistantText);
637
const [isWorkspaceFileDragActive, setIsWorkspaceFileDragActive] = useState(false);
634
- const [optimisticUserMessages, setOptimisticUserMessages] = useState<ChatMessage[]>([]);
638
+ const [optimisticBlobPreviews] = useState(createOptimisticBlobPreviews);
639
+ const [heldOptimisticUserMessages, setHeldOptimisticUserMessages] = useState(() =>
640
+ emptyOptimisticUserMessages(threadId),
641
+ );
642
+ // Acknowledged messages leave the timeline in the same render the server's copy enters it.
643
+ const optimisticState = reconcileOptimisticUserMessages(
644
+ heldOptimisticUserMessages,
645
+ threadId,
646
+ serverAcknowledgedUserMessageIds,
647
+ );
648
+ if (optimisticState !== heldOptimisticUserMessages) {
649
+ // Idempotent, and a discarded render only delays revocation to the thread's end.
650
+ optimisticBlobPreviews.handOff(optimisticState);
651
+ setHeldOptimisticUserMessages(optimisticState);
652
+ }
653
+ const optimisticUserMessages = optimisticState.messages;
654
+ const setOptimisticUserMessages = (action: SetStateAction<ChatMessage[]>) =>
655
+ setHeldOptimisticUserMessages((state) =>
656
+ applyOptimisticUserMessagesAction(state, threadId, action),
657
+ );
658
// Draft ids survive promotion and expire on navigation, keeping the pending resend scoped correctly.
659
const [workLocallyResendDraftId, setWorkLocallyResendDraftId] = useState<DraftId | null>(null);
660
const [feedbackSubmissionsByThreadKey, setFeedbackSubmissionsByThreadKey] = useState<
@@ -1400,12 +1423,14 @@ export default function ChatView(props: ChatViewProps) {
1423
}
1424
};
1425
const openFileAttachmentStable = useStableCallback(openFileAttachment);
1403
- const { handoffAttachmentPreviewsStable, timelineAttachmentUrlById } =
1404
- useAttachmentPreviewHandoff({
1405
- environmentId,
1406
- serverVisibleTurnItems,
1407
- optimisticUserMessages,
1408
- });
1426
+ const { timelineAttachmentUrlById } = useAttachmentPreviewHandoff({
1427
+ environmentId,
1428
+ threadId,
1429
+ serverVisibleTurnItems,
1430
+ handoff: optimisticState.handoff,
1431
+ blobPreviews: optimisticBlobPreviews,
1432
+ setOptimisticUserMessages: setHeldOptimisticUserMessages,
1433
+ });
1434
const anchoredTimelineMessages = feedbackSubmissions.flatMap((submission) =>
1435
submission.status === "interrupted"
1436
? []
@@ -2032,57 +2057,12 @@ export default function ChatView(props: ChatViewProps) {
2057
};
2058
}, [activeThread?.id, focusComposerStable, isMobileViewport, terminalUiState.terminalOpen]);
2059
2035
- useEffect(() => {
2036
- if (!activeThread?.id) return;
2037
- if (activeMessageCount === 0) {
2038
- return;
2039
- }
2040
- const removedMessages = optimisticUserMessages.filter((message) =>
2041
- serverAcknowledgedUserMessageIds.has(message.id),
2042
- );
2043
- if (removedMessages.length === 0) {
2044
- return;
2045
- }
2046
- const timer = window.setTimeout(() => {
2047
- setOptimisticUserMessages((existing) =>
2048
- existing.filter((message) => !serverAcknowledgedUserMessageIds.has(message.id)),
2049
- );
2050
- }, 0);
2051
- for (const removedMessage of removedMessages) {
2052
- const previewUrls = collectUserMessageBlobPreviewUrls(removedMessage);
2053
- if (previewUrls.length > 0) {
2054
- handoffAttachmentPreviewsStable(removedMessage.id, previewUrls);
2055
- continue;
2056
- }
2057
- revokeUserMessagePreviewUrls(removedMessage);
2058
- }
2059
- return () => {
2060
- window.clearTimeout(timer);
2061
- };
2062
- }, [
2063
- activeMessageCount,
2064
- activeThread?.id,
2065
- serverAcknowledgedUserMessageIds,
2066
- handoffAttachmentPreviewsStable,
2067
- optimisticUserMessages,
2068
- ]);
2069
-
2060
// Promotion drops draftId while a send may still be dispatching, so key by thread.
2071
- const [optimisticThreadId, setOptimisticThreadId] = useState(threadId);
2072
- const [retiredOptimisticMessages, setRetiredOptimisticMessages] = useState<
2073
- ReadonlyArray<ChatMessage>
2074
- >([]);
2075
- if (optimisticThreadId !== threadId) {
2076
- setOptimisticThreadId(threadId);
2077
- setRetiredOptimisticMessages(optimisticUserMessages);
2078
- setOptimisticUserMessages([]);
2061
+ const [localDispatchThreadId, setLocalDispatchThreadId] = useState(threadId);
2062
+ if (localDispatchThreadId !== threadId) {
2063
+ setLocalDispatchThreadId(threadId);
2064
resetLocalDispatch();
2065
}
2081
- useEffect(() => {
2082
- for (const message of retiredOptimisticMessages) {
2083
- revokeUserMessagePreviewUrls(message);
2084
- }
2085
- }, [retiredOptimisticMessages]);
2066
2067
const activeWorktreePath = activeThread?.worktreePath ?? null;
2068
const derivedEnvMode: DraftThreadEnvMode = resolveEffectiveEnvMode({
@@ -2810,6 +2790,10 @@ export default function ChatView(props: ChatViewProps) {
2790
setDockedDraftHeroThreadKey,
2791
setSetupQueueSendThreadKey,
2792
setOptimisticUserMessages,
2793
+ optimisticBlobPreviews: {
2794
+ track: (message) => optimisticBlobPreviews.track(threadId, message),
2795
+ release: (message) => optimisticBlobPreviews.release(threadId, message),
2796
+ },
2797
setFeedbackSubmissionsByThreadKey,
2798
setThreadError,
2799
setKeepFullHistory,
apps/web/src/components/chat/optimisticUserMessages.test.ts
new
+173
@@ -0,0 +1,173 @@
1
+import { MessageId, ThreadId } from "@t3tools/contracts";
2
+import { AtomRegistry } from "effect/reactivity";
3
+import { describe, expect, it, vi } from "vite-plus/test";
4
+
5
+import type { ChatMessage } from "../../types";
6
+import {
7
+ applyOptimisticUserMessagesAction,
8
+ createOptimisticBlobPreviews,
9
+ dropOptimisticHandoff,
10
+ emptyOptimisticUserMessages,
11
+ loadedHandoffMessageIdsAtom,
12
+ reconcileOptimisticUserMessages,
13
+} from "./optimisticUserMessages";
14
+
15
+const threadA = ThreadId.make("thread-a");
16
+const threadB = ThreadId.make("thread-b");
17
+
18
+function message(id: string, previewUrl?: string): ChatMessage {
19
+ return {
20
+ id: MessageId.make(id),
21
+ role: "user",
22
+ text: id,
23
+ runId: null,
24
+ createdAt: "2026-01-01T00:00:00.000Z",
25
+ updatedAt: "2026-01-01T00:00:00.000Z",
26
+ streaming: false,
27
+ ...(previewUrl
28
+ ? {
29
+ attachments: [
30
+ {
31
+ type: "image",
32
+ id: `${id}-image`,
33
+ name: "image.png",
34
+ mimeType: "image/png",
35
+ sizeBytes: 1,
36
+ previewUrl,
37
+ },
38
+ ],
39
+ }
40
+ : {}),
41
+ } as ChatMessage;
42
+}
43
+
44
+const ids = (...values: string[]) => new Set(values.map((value) => MessageId.make(value)));
45
+
46
+describe("optimistic user messages", () => {
47
+ it("an acknowledged message stays gone after the server drops its row", () => {
48
+ let state = applyOptimisticUserMessagesAction(emptyOptimisticUserMessages(threadA), threadA, [
49
+ message("m1"),
50
+ message("m2", "blob:two"),
51
+ ]);
52
+ state = reconcileOptimisticUserMessages(state, threadA, ids("m1", "m2"));
53
+ expect(state.messages).toEqual([]);
54
+ expect(state.handoff).toEqual({ m2: ["blob:two"] });
55
+
56
+ // A cancelled queued run or a rewind removes the server's copy again.
57
+ state = reconcileOptimisticUserMessages(state, threadA, ids());
58
+ expect(state.messages).toEqual([]);
59
+
60
+ state = dropOptimisticHandoff(state, threadA, MessageId.make("m2"));
61
+ expect(state.handoff).toEqual({});
62
+ });
63
+
64
+ it("releases the previous thread's messages when the thread changes", () => {
65
+ let state = applyOptimisticUserMessagesAction(emptyOptimisticUserMessages(threadA), threadA, [
66
+ message("a1"),
67
+ ]);
68
+ state = reconcileOptimisticUserMessages(state, threadB, ids());
69
+ state = reconcileOptimisticUserMessages(state, threadA, ids());
70
+ expect(state).toEqual(emptyOptimisticUserMessages(threadA));
71
+ });
72
+
73
+ it("drops a late update for a thread the view has left", () => {
74
+ const state = reconcileOptimisticUserMessages(
75
+ applyOptimisticUserMessagesAction(emptyOptimisticUserMessages(threadA), threadA, [
76
+ message("a1"),
77
+ ]),
78
+ threadB,
79
+ ids(),
80
+ );
81
+ expect(applyOptimisticUserMessagesAction(state, threadA, [message("a2")])).toBe(state);
82
+ });
83
+
84
+ it("revokes a thread's blob previews once, when their use ends or the thread is left", () => {
85
+ using revoke = vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => {});
86
+ const previews = createOptimisticBlobPreviews();
87
+ previews.track(threadA, message("a1", "blob:a1"));
88
+ previews.track(threadA, message("a2", "blob:a2"));
89
+ previews.track(threadB, message("b1", "blob:b1"));
90
+
91
+ previews.revoke(threadA, ["blob:a1"]);
92
+ previews.revokeThread(threadA);
93
+ expect(revoke.mock.calls.map(([url]) => url)).toEqual(["blob:a1", "blob:a2"]);
94
+ });
95
+
96
+ it("a send that fails after the server acknowledged it leaves the handed-off previews shown", () => {
97
+ using revoke = vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => {});
98
+ const previews = createOptimisticBlobPreviews();
99
+ const sent = message("m1", "blob:m1");
100
+ previews.track(threadA, sent);
101
+ let state = applyOptimisticUserMessagesAction(emptyOptimisticUserMessages(threadA), threadA, [
102
+ sent,
103
+ ]);
104
+ state = reconcileOptimisticUserMessages(state, threadA, ids("m1"));
105
+ previews.handOff(state);
106
+
107
+ previews.release(threadA, sent);
108
+ expect(revoke).not.toHaveBeenCalled();
109
+
110
+ previews.revokeThread(threadA);
111
+ expect(revoke.mock.calls.map(([url]) => url)).toEqual(["blob:m1"]);
112
+ });
113
+
114
+ it("a send that fails before acknowledgement revokes its previews", () => {
115
+ using revoke = vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => {});
116
+ const previews = createOptimisticBlobPreviews();
117
+ const sent = message("m1", "blob:m1");
118
+ previews.track(threadA, sent);
119
+ const state = applyOptimisticUserMessagesAction(emptyOptimisticUserMessages(threadA), threadA, [
120
+ sent,
121
+ ]);
122
+ previews.handOff(reconcileOptimisticUserMessages(state, threadA, ids()));
123
+
124
+ previews.release(threadA, sent);
125
+ expect(revoke.mock.calls.map(([url]) => url)).toEqual(["blob:m1"]);
126
+ });
127
+});
128
+
129
+class FakeImage {
130
+ static readonly created: FakeImage[] = [];
131
+ src = "";
132
+ private readonly listeners = new Map<string, () => void>();
133
+ constructor() {
134
+ FakeImage.created.push(this);
135
+ }
136
+ addEventListener(type: string, listener: () => void) {
137
+ this.listeners.set(type, listener);
138
+ }
139
+ fire(type: "load" | "error") {
140
+ this.listeners.get(type)?.();
141
+ }
142
+}
143
+
144
+describe("handoff preloads", () => {
145
+ it("reports a handoff loaded, and revokes its blobs, once every server preview loads", async () => {
146
+ vi.stubGlobal("Image", FakeImage);
147
+ try {
148
+ using revoke = vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => undefined);
149
+ FakeImage.created.length = 0;
150
+ const registry = AtomRegistry.make();
151
+ const atom = loadedHandoffMessageIdsAtom(
152
+ JSON.stringify([
153
+ ["m-loaded", ["https://server/a.png", "https://server/b.png"], ["blob:a", "blob:b"]],
154
+ ["m-failed", ["https://server/c.png"], ["blob:c"]],
155
+ ]),
156
+ );
157
+ const release = registry.mount(atom);
158
+ const image = (src: string) => FakeImage.created.find((candidate) => candidate.src === src);
159
+
160
+ image("https://server/a.png")?.fire("load");
161
+ image("https://server/c.png")?.fire("error");
162
+ await Promise.resolve();
163
+ expect(registry.get(atom)).toEqual([]);
164
+
165
+ image("https://server/b.png")?.fire("load");
166
+ await vi.waitFor(() => expect(registry.get(atom)).toEqual(["m-loaded"]));
167
+ expect(revoke.mock.calls.map(([url]) => url)).toEqual(["blob:a", "blob:b"]);
168
+ release();
169
+ } finally {
170
+ vi.unstubAllGlobals();
171
+ }
172
+ });
173
+});
apps/web/src/components/chat/optimisticUserMessages.ts
new
+180
@@ -0,0 +1,180 @@
1
+import type { MessageId, ThreadId } from "@t3tools/contracts";
2
+import * as Effect from "effect/Effect";
3
+import * as Schema from "effect/Schema";
4
+import { AsyncResult, Atom } from "effect/reactivity";
5
+import type { SetStateAction } from "react";
6
+import type { ChatMessage } from "../../types";
7
+import { collectUserMessageBlobPreviewUrls } from "../ChatView.logic";
8
+
9
+/**
10
+ * Messages shown before the server echoes them. An acknowledged message leaves `messages` in the
11
+ * render its ack arrives, so it never reappears even if the server later drops its row. Its blob
12
+ * previews stay in `handoff` until the server's copies load (see useAttachmentPreviewHandoff).
13
+ */
14
+export interface OptimisticUserMessages {
15
+ readonly threadId: ThreadId;
16
+ readonly messages: ChatMessage[];
17
+ readonly handoff: Readonly<Record<string, ReadonlyArray<string>>>;
18
+}
19
+
20
+export function emptyOptimisticUserMessages(threadId: ThreadId): OptimisticUserMessages {
21
+ return { threadId, messages: [], handoff: {} };
22
+}
23
+
24
+/**
25
+ * The view's state for `threadId`: the previous thread's messages are released when the thread
26
+ * changes, and acknowledged messages are moved out of `messages`. Returns `state` when nothing changes.
27
+ */
28
+export function reconcileOptimisticUserMessages(
29
+ state: OptimisticUserMessages,
30
+ threadId: ThreadId,
31
+ acknowledgedIds: ReadonlySet<MessageId>,
32
+): OptimisticUserMessages {
33
+ if (state.threadId !== threadId) return emptyOptimisticUserMessages(threadId);
34
+ if (!state.messages.some((message) => acknowledgedIds.has(message.id))) return state;
35
+ const messages: ChatMessage[] = [];
36
+ const handoff: Record<string, ReadonlyArray<string>> = { ...state.handoff };
37
+ for (const message of state.messages) {
38
+ if (!acknowledgedIds.has(message.id)) {
39
+ messages.push(message);
40
+ continue;
41
+ }
42
+ const previewUrls = collectUserMessageBlobPreviewUrls(message);
43
+ if (previewUrls.length > 0) handoff[message.id] = previewUrls;
44
+ }
45
+ return { ...state, messages, handoff };
46
+}
47
+
48
+/** Applies a send-path update. An update for a thread the view has since left is dropped. */
49
+export function applyOptimisticUserMessagesAction(
50
+ state: OptimisticUserMessages,
51
+ threadId: ThreadId,
52
+ action: SetStateAction<ChatMessage[]>,
53
+): OptimisticUserMessages {
54
+ if (state.threadId !== threadId) return state;
55
+ const next = typeof action === "function" ? action(state.messages) : action;
56
+ const unchanged =
57
+ next.length === state.messages.length &&
58
+ next.every((message, index) => message === state.messages[index]);
59
+ return unchanged ? state : { ...state, messages: next };
60
+}
61
+
62
+export function dropOptimisticHandoff(
63
+ state: OptimisticUserMessages,
64
+ threadId: ThreadId,
65
+ messageId: MessageId,
66
+): OptimisticUserMessages {
67
+ if (state.threadId !== threadId || !(messageId in state.handoff)) return state;
68
+ const { [messageId]: _released, ...handoff } = state.handoff;
69
+ return { ...state, handoff };
70
+}
71
+
72
+/**
73
+ * Blob previews a chat view owns per thread. Revocation cannot run in a state updater, so handlers
74
+ * revoke here and leaving the thread revokes the rest. A handed-off preview still stands in for the
75
+ * server's row, so releasing its message (a send failing after the ack) leaves it to the handoff.
76
+ */
77
+export interface OptimisticBlobPreviews {
78
+ readonly track: (threadId: ThreadId, message: ChatMessage) => void;
79
+ readonly handOff: (state: OptimisticUserMessages) => void;
80
+ readonly revoke: (threadId: ThreadId, previewUrls: ReadonlyArray<string>) => void;
81
+ readonly release: (threadId: ThreadId, message: ChatMessage) => void;
82
+ readonly revokeThread: (threadId: ThreadId) => void;
83
+}
84
+
85
+export function createOptimisticBlobPreviews(): OptimisticBlobPreviews {
86
+ const ownedByThread = new Map<ThreadId, Set<string>>();
87
+ const handedOff = new Set<string>();
88
+ const revoke = (threadId: ThreadId, previewUrls: ReadonlyArray<string>) => {
89
+ const owned = ownedByThread.get(threadId);
90
+ for (const previewUrl of previewUrls) {
91
+ owned?.delete(previewUrl);
92
+ handedOff.delete(previewUrl);
93
+ if (typeof URL !== "undefined") URL.revokeObjectURL(previewUrl);
94
+ }
95
+ };
96
+ return {
97
+ track: (threadId, message) => {
98
+ const previewUrls = collectUserMessageBlobPreviewUrls(message);
99
+ if (previewUrls.length === 0) return;
100
+ const owned = ownedByThread.get(threadId) ?? new Set<string>();
101
+ for (const previewUrl of previewUrls) owned.add(previewUrl);
102
+ ownedByThread.set(threadId, owned);
103
+ },
104
+ handOff: (state) => {
105
+ for (const previewUrls of Object.values(state.handoff)) {
106
+ for (const previewUrl of previewUrls) handedOff.add(previewUrl);
107
+ }
108
+ },
109
+ revoke,
110
+ release: (threadId, message) =>
111
+ revoke(
112
+ threadId,
113
+ collectUserMessageBlobPreviewUrls(message).filter((url) => !handedOff.has(url)),
114
+ ),
115
+ revokeThread: (threadId) => {
116
+ revoke(threadId, [...(ownedByThread.get(threadId) ?? [])]);
117
+ ownedByThread.delete(threadId);
118
+ },
119
+ };
120
+}
121
+
122
+// Server asset URLs carry a signed token, so the error keeps only where the image was loaded from.
123
+class ImagePreloadError extends Schema.TaggedError<ImagePreloadError>()("ImagePreloadError", {
124
+ protocol: Schema.String,
125
+ host: Schema.String,
126
+}) {
127
+ static fromUrl(url: string) {
128
+ const parsed = URL.parse(url, globalThis.location?.href);
129
+ return new ImagePreloadError({ protocol: parsed?.protocol ?? "", host: parsed?.host ?? "" });
130
+ }
131
+}
132
+
133
+const preloadImage = Effect.fn("optimisticUserMessages.preloadImage")(function* (url: string) {
134
+ yield* Effect.callback<void, ImagePreloadError>((resume) => {
135
+ const image = new Image();
136
+ image.addEventListener("load", () => resume(Effect.void), { once: true });
137
+ image.addEventListener("error", () => resume(Effect.fail(ImagePreloadError.fromUrl(url))), {
138
+ once: true,
139
+ });
140
+ image.src = url;
141
+ return Effect.sync(() => {
142
+ image.src = "";
143
+ });
144
+ });
145
+});
146
+
147
+/**
148
+ * Loads one handoff's server previews, keyed by `[serverUrls, blobUrls]`. Once they have all
149
+ * loaded the blobs are no longer shown, so they are revoked here, once per load. A failed load
150
+ * keeps the handoff, and its blobs, until the thread is left.
151
+ */
152
+const handoffPreloadAtom = Atom.family((key: string) => {
153
+ const [serverUrls, blobUrls] = JSON.parse(key) as [string[], string[]];
154
+ return Atom.make(
155
+ typeof Image === "undefined"
156
+ ? Effect.never
157
+ : Effect.forEach(serverUrls, preloadImage, { concurrency: "unbounded", discard: true }).pipe(
158
+ Effect.tap(() =>
159
+ Effect.sync(() => {
160
+ for (const blobUrl of blobUrls) URL.revokeObjectURL(blobUrl);
161
+ }),
162
+ ),
163
+ ),
164
+ );
165
+});
166
+
167
+/**
168
+ * The message ids, among `[messageId, serverUrls, blobUrls]` entries, whose server previews have
169
+ * loaded. Each handoff preloads in its own atom, so a change to the set does not restart the rest.
170
+ */
171
+export const loadedHandoffMessageIdsAtom = Atom.family((key: string) => {
172
+ const preloads = JSON.parse(key) as Array<[MessageId, string[], string[]]>;
173
+ return Atom.make((get) =>
174
+ preloads.flatMap(([messageId, serverUrls, blobUrls]) =>
175
+ AsyncResult.isSuccess(get(handoffPreloadAtom(JSON.stringify([serverUrls, blobUrls]))))
176
+ ? [messageId]
177
+ : [],
178
+ ),
179
+ );
180
+});
apps/web/src/components/chat/useAttachmentPreviewHandoff.ts
+63
-176
@@ -1,106 +1,53 @@
1
-import { useStableCallback } from "../../hooks/useStableCallback";
2
-import { type EnvironmentId, type MessageId } from "@t3tools/contracts";
3
-import { useEffect, useLayoutEffect, useRef, useState } from "react";
1
+import { type EnvironmentId, type MessageId, type ThreadId } from "@t3tools/contracts";
2
+import { useAtomValue } from "@effect/atom-react";
3
+import { type Dispatch, type SetStateAction, useEffect } from "react";
4
import { selectHandoffImageResources } from "../../session-logic";
5
-import { type ChatMessage } from "../../types";
5
import type { useQuietThreadVisibleTurnItems } from "../../state/entities";
7
-import { revokeBlobPreviewUrl, revokeUserMessagePreviewUrls } from "../ChatView.logic";
6
import { useAssetUrls } from "../../assets/assetUrls";
7
+import {
8
+ dropOptimisticHandoff,
9
+ loadedHandoffMessageIdsAtom,
10
+ type OptimisticBlobPreviews,
11
+ type OptimisticUserMessages,
12
+} from "./optimisticUserMessages";
13
+
14
interface AttachmentPreviewHandoffInput {
15
readonly environmentId: EnvironmentId;
16
+ readonly threadId: ThreadId;
17
readonly serverVisibleTurnItems: ReturnType<typeof useQuietThreadVisibleTurnItems>;
12
- readonly optimisticUserMessages: ReadonlyArray<ChatMessage>;
18
+ /** Blob previews of acknowledged messages, by message id; they stand in for the server's copy. */
19
+ readonly handoff: OptimisticUserMessages["handoff"];
20
+ readonly blobPreviews: OptimisticBlobPreviews;
21
+ readonly setOptimisticUserMessages: Dispatch<SetStateAction<OptimisticUserMessages>>;
22
}
23
24
/**
25
* A sent image keeps showing its local blob preview until the server's copy has loaded, so the
17
- * timeline does not flash an empty frame. Returns the attachment URLs the timeline renders and
18
- * the handoff for a message whose optimistic row was replaced by the server's.
26
+ * timeline does not flash an empty frame. The handoff ends, and its blobs are revoked, once the
27
+ * server previews load.
28
*/
29
export function useAttachmentPreviewHandoff(input: AttachmentPreviewHandoffInput) {
21
- const { environmentId, serverVisibleTurnItems, optimisticUserMessages } = input;
30
+ const {
31
+ environmentId,
32
+ threadId,
33
+ serverVisibleTurnItems,
34
+ blobPreviews,
35
+ setOptimisticUserMessages,
36
+ } = input;
37
+ const handoffByMessageId = input.handoff;
38
23
- const optimisticUserMessagesRef = useRef(optimisticUserMessages);
24
- useLayoutEffect(() => {
25
- optimisticUserMessagesRef.current = optimisticUserMessages;
26
- });
27
- const [attachmentPreviewHandoffByMessageId, setAttachmentPreviewHandoffByMessageId] = useState<
28
- Record<string, string[]>
29
- >({});
30
- const attachmentPreviewHandoffByMessageIdRef = useRef<Record<string, string[]>>({});
31
- const attachmentPreviewPromotionInFlightByMessageIdRef = useRef<Record<string, true>>({});
32
- useEffect(() => {
33
- attachmentPreviewHandoffByMessageIdRef.current = attachmentPreviewHandoffByMessageId;
34
- }, [attachmentPreviewHandoffByMessageId]);
35
- const clearAttachmentPreviewHandoff = (
36
- messageId: MessageId,
37
- previewUrls?: ReadonlyArray<string>,
38
- ) => {
39
- delete attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId];
40
- const currentPreviewUrls =
41
- previewUrls ?? attachmentPreviewHandoffByMessageIdRef.current[messageId] ?? [];
42
- setAttachmentPreviewHandoffByMessageId((existing) => {
43
- if (!(messageId in existing)) {
44
- return existing;
45
- }
46
- const next = { ...existing };
47
- delete next[messageId];
48
- attachmentPreviewHandoffByMessageIdRef.current = next;
49
- return next;
50
- });
51
- for (const previewUrl of currentPreviewUrls) {
52
- revokeBlobPreviewUrl(previewUrl);
53
- }
54
- };
55
- const clearAttachmentPreviewHandoffStable = useStableCallback(clearAttachmentPreviewHandoff);
56
- const clearAttachmentPreviewHandoffs = () => {
57
- attachmentPreviewPromotionInFlightByMessageIdRef.current = {};
58
- for (const previewUrls of Object.values(attachmentPreviewHandoffByMessageIdRef.current)) {
59
- for (const previewUrl of previewUrls) {
60
- revokeBlobPreviewUrl(previewUrl);
61
- }
62
- }
63
- attachmentPreviewHandoffByMessageIdRef.current = {};
64
- setAttachmentPreviewHandoffByMessageId({});
65
- };
66
- const clearAttachmentPreviewHandoffsStable = useStableCallback(clearAttachmentPreviewHandoffs);
67
- useEffect(() => {
68
- return () => {
69
- clearAttachmentPreviewHandoffsStable();
70
- for (const message of optimisticUserMessagesRef.current) {
71
- revokeUserMessagePreviewUrls(message);
72
- }
73
- };
74
- }, [clearAttachmentPreviewHandoffsStable]);
75
- const handoffAttachmentPreviews = (messageId: MessageId, previewUrls: string[]) => {
76
- if (previewUrls.length === 0) return;
39
+ // Blob URLs are a browser resource owned per thread; leaving the thread or the view ends them.
40
+ useEffect(() => () => blobPreviews.revokeThread(threadId), [blobPreviews, threadId]);
41
78
- const previousPreviewUrls = attachmentPreviewHandoffByMessageIdRef.current[messageId] ?? [];
79
- const nextPreviewUrlSet = new Set(previewUrls);
80
- for (const previewUrl of previousPreviewUrls) {
81
- if (!nextPreviewUrlSet.has(previewUrl)) {
82
- revokeBlobPreviewUrl(previewUrl);
83
- }
84
- }
85
- setAttachmentPreviewHandoffByMessageId((existing) => {
86
- const next = {
87
- ...existing,
88
- [messageId]: previewUrls,
89
- };
90
- attachmentPreviewHandoffByMessageIdRef.current = next;
91
- return next;
92
- });
93
- };
94
- const handoffAttachmentPreviewsStable = useStableCallback(handoffAttachmentPreviews);
42
const serverAttachmentResources = selectHandoffImageResources(
96
- Object.keys(attachmentPreviewHandoffByMessageId).length === 0
43
+ Object.keys(handoffByMessageId).length === 0
44
? undefined
45
: serverVisibleTurnItems.flatMap(({ item }) =>
46
item.type === "user_message"
47
? [{ id: item.messageId, role: "user" as const, attachments: item.attachments }]
48
: [],
49
),
103
- attachmentPreviewHandoffByMessageId,
50
+ handoffByMessageId,
51
);
52
const serverAttachmentUrls = useAssetUrls(environmentId, serverAttachmentResources);
53
const serverAttachmentUrlById = new Map(
@@ -109,104 +56,47 @@ export function useAttachmentPreviewHandoff(input: AttachmentPreviewHandoffInput
56
return url ? [[resource.attachmentId, url] as const] : [];
57
}),
58
);
112
- useEffect(() => {
113
- if (typeof Image === "undefined" || serverVisibleTurnItems.length === 0) {
114
- return;
115
- }
59
117
- const cleanups: Array<() => void> = [];
118
- const userMessagesById = new Map(
119
- serverVisibleTurnItems.flatMap((row) =>
120
- row.item.type === "user_message" ? [[String(row.item.messageId), row.item] as const] : [],
60
+ // Each handoff whose server previews are all minted, as [messageId, serverUrls, blobUrls].
61
+ const pendingPreloads: Array<readonly [MessageId, ReadonlyArray<string>, ReadonlyArray<string>]> =
62
+ [];
63
+ for (const row of serverVisibleTurnItems) {
64
+ if (row.item.type !== "user_message") continue;
65
+ const handoffUrls = handoffByMessageId[row.item.messageId];
66
+ if (handoffUrls === undefined || row.item.attachments.length === 0) continue;
67
+ const serverPreviewUrls = row.item.attachments.flatMap((attachment) => {
68
+ if (attachment.type !== "image") return [];
69
+ const url = serverAttachmentUrlById.get(attachment.id);
70
+ return url === undefined ? [] : [url];
71
+ });
72
+ if (
73
+ serverPreviewUrls.length === 0 ||
74
+ serverPreviewUrls.length !== handoffUrls.length ||
75
+ serverPreviewUrls.some((previewUrl) => previewUrl.startsWith("blob:"))
76
+ ) {
77
+ continue;
78
+ }
79
+ pendingPreloads.push([row.item.messageId, serverPreviewUrls, handoffUrls]);
80
+ }
81
+ const loadedHandoffMessageIds = useAtomValue(
82
+ loadedHandoffMessageIdsAtom(JSON.stringify(pendingPreloads)),
83
+ );
84
+ // The handoff is state of the rendering ChatView, so dropping a loaded one here is React's
85
+ // adjust-state-while-rendering pattern; the preload atom has already revoked its blobs.
86
+ if (loadedHandoffMessageIds.length > 0) {
87
+ setOptimisticUserMessages((state) =>
88
+ loadedHandoffMessageIds.reduce(
89
+ (next, messageId) => dropOptimisticHandoff(next, threadId, messageId),
90
+ state,
91
),
92
);
93
+ }
94
124
- for (const [messageId, handoffPreviewUrls] of Object.entries(
125
- attachmentPreviewHandoffByMessageId,
126
- )) {
127
- if (attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId]) {
128
- continue;
129
- }
130
-
131
- const serverMessage = userMessagesById.get(messageId);
132
- if (serverMessage === undefined || serverMessage.attachments.length === 0) {
133
- continue;
134
- }
135
-
136
- const serverPreviewUrls = serverMessage.attachments.flatMap((attachment) =>
137
- attachment.type === "image"
138
- ? [serverAttachmentUrlById.get(attachment.id)].filter(
139
- (previewUrl): previewUrl is string => previewUrl !== undefined,
140
- )
141
- : [],
142
- );
143
- if (
144
- serverPreviewUrls.length === 0 ||
145
- serverPreviewUrls.length !== handoffPreviewUrls.length ||
146
- serverPreviewUrls.some((previewUrl) => previewUrl.startsWith("blob:"))
147
- ) {
148
- continue;
149
- }
150
-
151
- attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId] = true;
152
-
153
- let cancelled = false;
154
- const imageInstances: HTMLImageElement[] = [];
155
-
156
- const preloadServerPreviews = Promise.all(
157
- serverPreviewUrls.map(
158
- (previewUrl) =>
159
- new Promise<void>((resolve, reject) => {
160
- const image = new Image();
161
- imageInstances.push(image);
162
- const handleLoad = () => resolve();
163
- const handleError = () =>
164
- reject(new Error(`Failed to load server preview for ${messageId}.`));
165
- image.addEventListener("load", handleLoad, { once: true });
166
- image.addEventListener("error", handleError, { once: true });
167
- image.src = previewUrl;
168
- }),
169
- ),
170
- );
171
-
172
- void preloadServerPreviews
173
- .then(() => {
174
- if (cancelled) {
175
- return;
176
- }
177
- clearAttachmentPreviewHandoffStable(messageId as MessageId, handoffPreviewUrls);
178
- })
179
- .catch(() => {
180
- if (!cancelled) {
181
- delete attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId];
182
- }
183
- });
184
-
185
- cleanups.push(() => {
186
- cancelled = true;
187
- delete attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId];
188
- for (const image of imageInstances) {
189
- image.src = "";
190
- }
191
- });
192
- }
193
-
194
- return () => {
195
- for (const cleanup of cleanups) {
196
- cleanup();
197
- }
198
- };
199
- }, [
200
- attachmentPreviewHandoffByMessageId,
201
- clearAttachmentPreviewHandoffStable,
202
- serverAttachmentUrlById,
203
- serverVisibleTurnItems,
204
- ]);
95
const timelineAttachmentUrlById = (() => {
96
const urls = new Map(serverAttachmentUrlById);
97
for (const row of serverVisibleTurnItems) {
98
if (row.item.type !== "user_message") continue;
209
- const handoffUrls = attachmentPreviewHandoffByMessageId[row.item.messageId];
99
+ const handoffUrls = handoffByMessageId[row.item.messageId];
100
if (handoffUrls === undefined) continue;
101
let imageIndex = 0;
102
for (const attachment of row.item.attachments) {
@@ -218,8 +108,5 @@ export function useAttachmentPreviewHandoff(input: AttachmentPreviewHandoffInput
108
}
109
return urls;
110
})();
221
- return {
222
- handoffAttachmentPreviewsStable,
223
- timelineAttachmentUrlById,
224
- };
111
+ return { timelineAttachmentUrlById };
112
}
apps/web/src/components/chat/useComposerSend.ts
+32
-24
@@ -5,7 +5,6 @@ import {
5
restorePlanFollowUpComposer,
6
buildExpiredTerminalContextToastCopy,
7
deriveComposerSendState,
8
- revokeUserMessagePreviewUrls,
8
boundAttachmentIds,
9
formatOutgoingPrompt,
10
optimisticMessageAttachments,
@@ -224,6 +223,11 @@ interface ComposerSendInput {
223
readonly setDockedDraftHeroThreadKey: Dispatch<SetStateAction<string | null>>;
224
readonly setSetupQueueSendThreadKey: Dispatch<SetStateAction<string | null>>;
225
readonly setOptimisticUserMessages: Dispatch<SetStateAction<ChatMessage[]>>;
226
+ /** Takes ownership of, and later releases, an optimistic message's blob previews. */
227
+ readonly optimisticBlobPreviews: {
228
+ readonly track: (message: ChatMessage) => void;
229
+ readonly release: (message: ChatMessage) => void;
230
+ };
231
readonly setFeedbackSubmissionsByThreadKey: Dispatch<
232
SetStateAction<Record<string, ReadonlyArray<CodexFeedbackSubmission>>>
233
>;
@@ -311,6 +315,7 @@ export function useComposerSend(input: ComposerSendInput) {
315
setDockedDraftHeroThreadKey,
316
setSetupQueueSendThreadKey,
317
setOptimisticUserMessages,
318
+ optimisticBlobPreviews,
319
setFeedbackSubmissionsByThreadKey,
320
setThreadError,
321
setKeepFullHistory,
@@ -1112,25 +1117,30 @@ export function useComposerSend(input: ComposerSendInput) {
1117
messageIdForSend,
1118
);
1119
}
1115
- setOptimisticUserMessages((existing) => [
1116
- ...existing,
1117
- {
1118
- id: messageIdForSend,
1119
- role: "user",
1120
- text: outgoingMessageText,
1121
- ...(optimisticAttachments.length > 0 ? { attachments: optimisticAttachments } : {}),
1122
- ...(outgoingMessageContext !== undefined ? { context: outgoingMessageContext } : {}),
1123
- runId: null,
1124
- createdAt: messageCreatedAt,
1125
- updatedAt: messageCreatedAt,
1126
- streaming: false,
1127
- ...(shouldQueueBehindActiveRun
1128
- ? { inputIntent: "queued_turn" as const }
1129
- : phase === "running" && dispatchMode === "steer"
1130
- ? { inputIntent: "steer" as const }
1131
- : {}),
1132
- },
1133
- ]);
1120
+ const optimisticMessage: ChatMessage = {
1121
+ id: messageIdForSend,
1122
+ role: "user",
1123
+ text: outgoingMessageText,
1124
+ ...(optimisticAttachments.length > 0 ? { attachments: optimisticAttachments } : {}),
1125
+ ...(outgoingMessageContext !== undefined ? { context: outgoingMessageContext } : {}),
1126
+ runId: null,
1127
+ createdAt: messageCreatedAt,
1128
+ updatedAt: messageCreatedAt,
1129
+ streaming: false,
1130
+ ...(shouldQueueBehindActiveRun
1131
+ ? { inputIntent: "queued_turn" as const }
1132
+ : phase === "running" && dispatchMode === "steer"
1133
+ ? { inputIntent: "steer" as const }
1134
+ : {}),
1135
+ };
1136
+ // After leaving the thread during an upload, its previews were already released and the
1137
+ // message is never shown, so tracking them would keep them alive past the thread.
1138
+ if (currentRouteThreadKeyRef.current === routeThreadKey) {
1139
+ optimisticBlobPreviews.track(optimisticMessage);
1140
+ } else {
1141
+ optimisticBlobPreviews.release(optimisticMessage);
1142
+ }
1143
+ setOptimisticUserMessages((existing) => [...existing, optimisticMessage]);
1144
setThreadError(threadIdForSend, null);
1145
if (expiredTerminalContextCount > 0) {
1146
const toastCopy = buildExpiredTerminalContextToastCopy(
@@ -1369,13 +1379,11 @@ export function useComposerSend(input: ComposerSendInput) {
1379
)
1380
) {
1381
setOptimisticUserMessages((existing) => {
1372
- const removed = existing.filter((message) => message.id === messageIdForSend);
1373
- for (const message of removed) {
1374
- revokeUserMessagePreviewUrls(message);
1375
- }
1382
const next = existing.filter((message) => message.id !== messageIdForSend);
1383
return next.length === existing.length ? existing : next;
1384
});
1385
+ // The restored draft clones its images, so the sent copies are no longer used.
1386
+ optimisticBlobPreviews.release(optimisticMessage);
1387
const retryComposerImages = restoreComposerDraft(composerDraftTarget, sendSnapshot);
1388
if (currentRouteThreadKeyRef.current === routeThreadKey) {
1389
promptRef.current = messageTextForSend;