The best way to control Claude Code, Codex and more from any computer or phone
perf(composer): reuse persisted image bytes when attachments change (#22340)
Co-authored-by: GPT-6.1 Sol <noreply@openai.com> Co-authored-by: maria-rcks <254055478+maria-rcks@users.noreply.github.com>
Victor Vogel committed
Oct 30, 2026 at 16:25 UTC
e9485df15271da8a74a1caf9b434dded929a47dc
3 files changed
+139
-26
apps/web/src/components/chat/ChatComposer.tsx
+4
-26
@@ -79,7 +79,6 @@ import { DEFAULT_COMPOSER_PLACEHOLDER } from "../../composerPlaceholder";
79
import {
80
deriveComposerSendState,
81
getAntigravitySendBlockReason,
82
- readFileAsDataUrl,
82
resolveComposerInteractionMode,
83
threadShellHasStarted,
84
} from "../ChatView.logic";
@@ -87,6 +86,7 @@ import {
86
dataTransferHasComposerMention,
87
makeComposerMentionDragHandlers,
88
} from "./composerMentionDrag";
89
+import { serializeComposerImages } from "./composerImagePersistence";
90
import {
91
isInsideCollapsedComposerControls,
92
isInsideRestingComposerControlScope,
@@ -96,7 +96,6 @@ import {
96
type ComposerFileAttachment,
97
type ComposerImageAttachment,
98
type DraftId,
99
- type PersistedComposerImageAttachment,
99
composerFileNeedsReattach,
100
composerDraftHasUserContent,
101
composerTargetKey,
@@ -1522,31 +1521,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
1521
getComposerDraft(attachmentDraftTarget)?.persistedAttachments ?? [];
1522
try {
1523
const currentPersistedAttachments = getPersistedAttachmentsForThread();
1525
- const existingPersistedById = new Map(
1526
- currentPersistedAttachments.map((attachment) => [attachment.id, attachment]),
1524
+ const serialized = await serializeComposerImages(
1525
+ composerImages,
1526
+ currentPersistedAttachments,
1527
);
1528
- const stagedAttachmentById = new Map<string, PersistedComposerImageAttachment>();
1529
- await Promise.all(
1530
- composerImages.map(async (image) => {
1531
- try {
1532
- const dataUrl = await readFileAsDataUrl(image.file);
1533
- stagedAttachmentById.set(image.id, {
1534
- id: image.id,
1535
- name: image.name,
1536
- mimeType: image.mimeType,
1537
- sizeBytes: image.sizeBytes,
1538
- dataUrl,
1539
- ...(image.source ? { source: image.source } : {}),
1540
- });
1541
- } catch {
1542
- const existingPersisted = existingPersistedById.get(image.id);
1543
- if (existingPersisted) {
1544
- stagedAttachmentById.set(image.id, existingPersisted);
1545
- }
1546
- }
1547
- }),
1548
- );
1549
- const serialized = Array.from(stagedAttachmentById.values());
1528
if (cancelled) return;
1529
syncComposerDraftPersistedAttachments(attachmentDraftTarget, serialized);
1530
} catch {
apps/web/src/components/chat/composerImagePersistence.test.ts
new
+98
@@ -0,0 +1,98 @@
1
+import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
2
+
3
+import type { ComposerImageAttachment } from "../../composerDraftStore";
4
+import { readFileAsDataUrl } from "../../lib/imageCompression";
5
+import { serializeComposerImages } from "./composerImagePersistence";
6
+
7
+vi.mock("../../lib/imageCompression", () => ({ readFileAsDataUrl: vi.fn() }));
8
+
9
+function image(id: string): ComposerImageAttachment {
10
+ const file = new File([id], `${id}.png`, { type: "image/png" });
11
+ return {
12
+ type: "image",
13
+ id,
14
+ name: file.name,
15
+ mimeType: file.type,
16
+ sizeBytes: file.size,
17
+ previewUrl: `blob:${id}`,
18
+ file,
19
+ };
20
+}
21
+
22
+describe("serializeComposerImages", () => {
23
+ beforeEach(() => {
24
+ vi.mocked(readFileAsDataUrl).mockReset();
25
+ });
26
+
27
+ it("reads only new images and drops removed images without rereading retained bytes", async () => {
28
+ const a = image("a");
29
+ const b = image("b");
30
+ vi.mocked(readFileAsDataUrl).mockImplementation(async (file) =>
31
+ file === a.file ? "data:image/png;base64,YQ==" : "data:image/png;base64,Yg==",
32
+ );
33
+
34
+ const first = await serializeComposerImages([a], []);
35
+ expect(first).toEqual([
36
+ {
37
+ id: "a",
38
+ name: "a.png",
39
+ mimeType: "image/png",
40
+ sizeBytes: 1,
41
+ dataUrl: "data:image/png;base64,YQ==",
42
+ },
43
+ ]);
44
+ expect(readFileAsDataUrl).toHaveBeenCalledTimes(1);
45
+
46
+ const second = await serializeComposerImages([a, b], first);
47
+ expect(second).toEqual([
48
+ first[0],
49
+ {
50
+ id: "b",
51
+ name: "b.png",
52
+ mimeType: "image/png",
53
+ sizeBytes: 1,
54
+ dataUrl: "data:image/png;base64,Yg==",
55
+ },
56
+ ]);
57
+ expect(readFileAsDataUrl).toHaveBeenCalledTimes(2);
58
+ expect(readFileAsDataUrl).toHaveBeenNthCalledWith(2, b.file);
59
+
60
+ expect(await serializeComposerImages([a], second)).toEqual(first);
61
+ expect(await serializeComposerImages([], second)).toEqual([]);
62
+ expect(readFileAsDataUrl).toHaveBeenCalledTimes(2);
63
+ });
64
+
65
+ it("preserves cached bytes without trying to read an unavailable file", async () => {
66
+ const a = image("a");
67
+ const persisted = {
68
+ id: a.id,
69
+ name: a.name,
70
+ mimeType: a.mimeType,
71
+ sizeBytes: a.sizeBytes,
72
+ dataUrl: "data:image/png;base64,YQ==",
73
+ };
74
+ vi.mocked(readFileAsDataUrl).mockRejectedValue(new Error("File is unavailable"));
75
+
76
+ expect(await serializeComposerImages([a], [persisted])).toEqual([persisted]);
77
+ expect(readFileAsDataUrl).not.toHaveBeenCalled();
78
+ });
79
+
80
+ it("omits failed uncached reads while keeping successfully serialized images", async () => {
81
+ const a = image("a");
82
+ const b = image("b");
83
+ vi.mocked(readFileAsDataUrl)
84
+ .mockRejectedValueOnce(new Error("File is unavailable"))
85
+ .mockResolvedValueOnce("data:image/png;base64,Yg==");
86
+
87
+ expect(await serializeComposerImages([a, b], [])).toEqual([
88
+ {
89
+ id: "b",
90
+ name: "b.png",
91
+ mimeType: "image/png",
92
+ sizeBytes: 1,
93
+ dataUrl: "data:image/png;base64,Yg==",
94
+ },
95
+ ]);
96
+ expect(readFileAsDataUrl).toHaveBeenCalledTimes(2);
97
+ });
98
+});
apps/web/src/components/chat/composerImagePersistence.ts
new
+37
@@ -0,0 +1,37 @@
1
+import type {
2
+ ComposerImageAttachment,
3
+ PersistedComposerImageAttachment,
4
+} from "../../composerDraftStore";
5
+import { readFileAsDataUrl } from "../../lib/imageCompression";
6
+
7
+export async function serializeComposerImages(
8
+ images: ReadonlyArray<ComposerImageAttachment>,
9
+ persistedAttachments: ReadonlyArray<PersistedComposerImageAttachment>,
10
+): Promise<PersistedComposerImageAttachment[]> {
11
+ const existingPersistedById = new Map(
12
+ persistedAttachments.map((attachment) => [attachment.id, attachment]),
13
+ );
14
+ const stagedAttachmentById = new Map<string, PersistedComposerImageAttachment>();
15
+ await Promise.all(
16
+ images.map(async (image) => {
17
+ try {
18
+ const dataUrl =
19
+ existingPersistedById.get(image.id)?.dataUrl ?? (await readFileAsDataUrl(image.file));
20
+ stagedAttachmentById.set(image.id, {
21
+ id: image.id,
22
+ name: image.name,
23
+ mimeType: image.mimeType,
24
+ sizeBytes: image.sizeBytes,
25
+ dataUrl,
26
+ ...(image.source ? { source: image.source } : {}),
27
+ });
28
+ } catch {
29
+ const existingPersisted = existingPersistedById.get(image.id);
30
+ if (existingPersisted) {
31
+ stagedAttachmentById.set(image.id, existingPersisted);
32
+ }
33
+ }
34
+ }),
35
+ );
36
+ return Array.from(stagedAttachmentById.values());
37
+}