Skip to content

Commit 7f3d61b

Browse files
only21milcodex
andcommitted
perf(composer): reuse persisted image bytes when attachments change
Co-Authored-By: GPT-6.1 Sol <[email protected]>
1 parent 67c0fc0 commit 7f3d61b

3 files changed

Lines changed: 139 additions & 25 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ import { listContinuationForEnter, listIndentForTab } from "../../composer-list-
100100
import {
101101
deriveComposerSendState,
102102
getAntigravitySendBlockReason,
103-
readFileAsDataUrl,
104103
resolveComposerInteractionMode,
105104
resolveComposerProviderSelection,
106105
threadShellHasStarted,
@@ -109,6 +108,7 @@ import {
109108
dataTransferHasComposerMention,
110109
makeComposerMentionDragHandlers,
111110
} from "./composerMentionDrag";
111+
import { serializeComposerImages } from "./composerImagePersistence";
112112
import {
113113
composerFloatingLayerProps,
114114
useComposerMenuProps,
@@ -3519,31 +3519,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
35193519
getComposerDraft(attachmentDraftTarget)?.persistedAttachments ?? [];
35203520
try {
35213521
const currentPersistedAttachments = getPersistedAttachmentsForThread();
3522-
const existingPersistedById = new Map(
3523-
currentPersistedAttachments.map((attachment) => [attachment.id, attachment]),
3522+
const serialized = await serializeComposerImages(
3523+
composerImages,
3524+
currentPersistedAttachments,
35243525
);
3525-
const stagedAttachmentById = new Map<string, PersistedComposerImageAttachment>();
3526-
await Promise.all(
3527-
composerImages.map(async (image) => {
3528-
try {
3529-
const dataUrl = await readFileAsDataUrl(image.file);
3530-
stagedAttachmentById.set(image.id, {
3531-
id: image.id,
3532-
name: image.name,
3533-
mimeType: image.mimeType,
3534-
sizeBytes: image.sizeBytes,
3535-
dataUrl,
3536-
...(image.source ? { source: image.source } : {}),
3537-
});
3538-
} catch {
3539-
const existingPersisted = existingPersistedById.get(image.id);
3540-
if (existingPersisted) {
3541-
stagedAttachmentById.set(image.id, existingPersisted);
3542-
}
3543-
}
3544-
}),
3545-
);
3546-
const serialized = Array.from(stagedAttachmentById.values());
35473526
if (cancelled) return;
35483527
syncComposerDraftPersistedAttachments(attachmentDraftTarget, serialized);
35493528
} catch {
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)