Skip to content
This repository was archived by the owner on Oct 10, 2026. It is now read-only.

Commit cbe8052

Browse files
t3dotggclaude
andauthored
feat(web): pasting a huge screenshot now compresses it instead of erroring (pingdotgg#4967)
Co-authored-by: Claude Fable 5 <[email protected]>
1 parent 69a18ce commit cbe8052

3 files changed

Lines changed: 282 additions & 82 deletions

File tree

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

Lines changed: 89 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ import {
6464
} from "../../promptStashStore";
6565
import { ComposerStashBadge } from "./ComposerStashBadge";
6666
import { ComposerStashMenu } from "./ComposerStashMenu";
67-
import { compressImageForStash } from "../../lib/stashImageCompression";
67+
import { compressImageForStash, compressImageToByteLimit } from "../../lib/imageCompression";
6868
import { isCommandPaletteOpen } from "../../commandPaletteBus";
6969
import { getTerminalFocusOwner } from "../../lib/terminalFocus";
7070
import { resolveShortcutCommand } from "../../keybindings";
@@ -206,8 +206,6 @@ import { searchProviderSkills } from "../../providerSkillSearch";
206206
import { useMediaQuery } from "../../hooks/useMediaQuery";
207207
import type { ReviewCommentContext } from "../../reviewCommentContext";
208208

209-
const IMAGE_SIZE_LIMIT_LABEL = `${Math.round(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES / (1024 * 1024))}MB`;
210-
211209
const runtimeModeConfig: Record<
212210
RuntimeMode,
213211
{ label: string; description: string; icon: LucideIcon }
@@ -1006,6 +1004,13 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
10061004
* thread) can still be stashed while an earlier encode is running.
10071005
*/
10081006
const stashInFlightRef = useRef<Set<string>>(new Set());
1007+
/**
1008+
* Count of pasted images still being compressed, per thread. Reserved
1009+
* against the attachment limit so concurrent pastes can't overshoot it,
1010+
* and checked by `submitComposer` so a send can't race an image into the
1011+
* next draft.
1012+
*/
1013+
const pendingImageCompressionsRef = useRef<Map<ThreadId, number>>(new Map());
10091014

10101015
// ------------------------------------------------------------------
10111016
// Derived: composer send state
@@ -1821,12 +1826,26 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
18211826
event?.preventDefault();
18221827
return;
18231828
}
1829+
// A send while a pasted image is still compressing would strand that
1830+
// image: the turn snapshot wouldn't include it, and it would surface
1831+
// in the *next* draft instead. Only oversized images hit this — small
1832+
// files clear the pending counter within a microtask.
1833+
if (activeThreadId && (pendingImageCompressionsRef.current.get(activeThreadId) ?? 0) > 0) {
1834+
event?.preventDefault();
1835+
toastManager.add({
1836+
type: "info",
1837+
title: "Still compressing a pasted image.",
1838+
description: "Send again once its thumbnail appears.",
1839+
});
1840+
return;
1841+
}
18241842
onSend(event);
18251843
if (shouldBlurMobileComposerOnSubmit()) {
18261844
blurMobileComposerAfterSend();
18271845
}
18281846
},
18291847
[
1848+
activeThreadId,
18301849
blurMobileComposerAfterSend,
18311850
isSendDisabled,
18321851
noProviderAvailable,
@@ -2273,7 +2292,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
22732292
// ------------------------------------------------------------------
22742293
// Callbacks: images
22752294
// ------------------------------------------------------------------
2276-
const addComposerImages = (files: File[]) => {
2295+
const addComposerImages = async (files: File[]) => {
22772296
if (!activeThreadId || files.length === 0) return;
22782297
if (pendingUserInputs.length > 0) {
22792298
toastManager.add({
@@ -2282,40 +2301,81 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
22822301
});
22832302
return;
22842303
}
2285-
const nextImages: ComposerImageAttachment[] = [];
2286-
let nextImageCount = composerImagesRef.current.length;
2304+
// Captured before the awaits below: the user may switch threads while a
2305+
// large image is being compressed, and the attachments and errors belong
2306+
// to the thread the paste happened in.
2307+
const threadId = activeThreadId;
2308+
2309+
// Validation happens synchronously so concurrent pastes see each other:
2310+
// accepted files reserve their attachment slots (via the pending counter)
2311+
// before the first await, keeping the total under the limit.
2312+
const pendingCount = pendingImageCompressionsRef.current.get(threadId) ?? 0;
2313+
let reservedCount = composerImagesRef.current.length + pendingCount;
2314+
const acceptedFiles: File[] = [];
22872315
let error: string | null = null;
22882316
for (const file of files) {
22892317
if (!file.type.startsWith("image/")) {
22902318
error = `Unsupported file type for '${file.name}'. Please attach image files only.`;
22912319
continue;
22922320
}
2293-
if (file.size > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES) {
2294-
error = `'${file.name}' exceeds the ${IMAGE_SIZE_LIMIT_LABEL} attachment limit.`;
2295-
continue;
2296-
}
2297-
if (nextImageCount >= PROVIDER_SEND_TURN_MAX_ATTACHMENTS) {
2321+
if (reservedCount >= PROVIDER_SEND_TURN_MAX_ATTACHMENTS) {
22982322
error = `You can attach up to ${PROVIDER_SEND_TURN_MAX_ATTACHMENTS} images per message.`;
22992323
break;
23002324
}
2301-
const previewUrl = URL.createObjectURL(file);
2302-
nextImages.push({
2303-
type: "image",
2304-
id: randomUUID(),
2305-
name: file.name || "image",
2306-
mimeType: file.type,
2307-
sizeBytes: file.size,
2308-
previewUrl,
2309-
file,
2310-
});
2311-
nextImageCount += 1;
2325+
acceptedFiles.push(file);
2326+
reservedCount += 1;
23122327
}
2313-
if (nextImages.length === 1 && nextImages[0]) {
2314-
addComposerImage(nextImages[0]);
2315-
} else if (nextImages.length > 1) {
2316-
addComposerImagesToDraft(nextImages);
2328+
setThreadError(threadId, error);
2329+
if (acceptedFiles.length === 0) return;
2330+
2331+
pendingImageCompressionsRef.current.set(threadId, pendingCount + acceptedFiles.length);
2332+
try {
2333+
const nextImages: ComposerImageAttachment[] = [];
2334+
let compressionError: string | null = null;
2335+
for (const file of acceptedFiles) {
2336+
// Images over the wire cap are downscaled to fit rather than
2337+
// refused; files already within it pass through byte-for-byte.
2338+
const compressed = await compressImageToByteLimit(file, PROVIDER_SEND_TURN_MAX_IMAGE_BYTES);
2339+
if (!compressed.ok) {
2340+
compressionError =
2341+
compressed.reason === "unreadable"
2342+
? `'${file.name}' could not be read as an image.`
2343+
: `'${file.name}' is too large to attach, even after compression.`;
2344+
continue;
2345+
}
2346+
const attachmentFile = compressed.file;
2347+
const previewUrl = URL.createObjectURL(attachmentFile);
2348+
nextImages.push({
2349+
type: "image",
2350+
id: randomUUID(),
2351+
name: attachmentFile.name || "image",
2352+
mimeType: attachmentFile.type,
2353+
sizeBytes: attachmentFile.size,
2354+
previewUrl,
2355+
file: attachmentFile,
2356+
});
2357+
}
2358+
if (nextImages.length === 1 && nextImages[0]) {
2359+
addComposerImage(nextImages[0]);
2360+
} else if (nextImages.length > 1) {
2361+
addComposerImagesToDraft(nextImages);
2362+
}
2363+
// Only failures are reported here. Success must not pass `null`: by
2364+
// now other work (a failed send, an overlapping paste) may have set a
2365+
// thread error this call knows nothing about, and clearing it would
2366+
// swallow that message.
2367+
if (compressionError !== null) {
2368+
setThreadError(threadId, compressionError);
2369+
}
2370+
} finally {
2371+
const remaining =
2372+
(pendingImageCompressionsRef.current.get(threadId) ?? 0) - acceptedFiles.length;
2373+
if (remaining > 0) {
2374+
pendingImageCompressionsRef.current.set(threadId, remaining);
2375+
} else {
2376+
pendingImageCompressionsRef.current.delete(threadId);
2377+
}
23172378
}
2318-
setThreadError(activeThreadId, error);
23192379
};
23202380

23212381
const removeComposerImage = (imageId: string) => {
@@ -2331,7 +2391,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
23312391
const imageFiles = files.filter((file) => file.type.startsWith("image/"));
23322392
if (imageFiles.length === 0) return;
23332393
event.preventDefault();
2334-
addComposerImages(imageFiles);
2394+
void addComposerImages(imageFiles);
23352395
};
23362396

23372397
const onComposerDragEnter = (event: React.DragEvent<HTMLDivElement>) => {
@@ -2365,7 +2425,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
23652425
dragDepthRef.current = 0;
23662426
setIsDragOverComposer(false);
23672427
const files = Array.from(event.dataTransfer.files);
2368-
addComposerImages(files);
2428+
void addComposerImages(files);
23692429
focusComposer();
23702430
};
23712431

apps/web/src/lib/stashImageCompression.test.ts renamed to apps/web/src/lib/imageCompression.test.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
22

3-
import { compressImageForStash, MAX_STASH_IMAGE_DATA_URL_CHARS } from "./stashImageCompression";
3+
import {
4+
compressImageForStash,
5+
compressImageToByteLimit,
6+
MAX_COMPRESSIBLE_SOURCE_BYTES,
7+
MAX_STASH_IMAGE_DATA_URL_CHARS,
8+
} from "./imageCompression";
49

510
/**
611
* jsdom has no real canvas/codec, so the re-encode path is exercised with
@@ -159,6 +164,56 @@ describe("compressImageForStash", () => {
159164
});
160165
});
161166

167+
it("compressImageToByteLimit passes small files through byte-for-byte", async () => {
168+
const bitmapSpy = vi.fn();
169+
vi.stubGlobal("createImageBitmap", bitmapSpy);
170+
171+
const original = makeFile(1024);
172+
const result = await compressImageToByteLimit(original, 10 * 1024 * 1024);
173+
174+
expect(result.ok).toBe(true);
175+
expect(result.ok && result.recompressed).toBe(false);
176+
// Pass-through must be the same File object, not a copy.
177+
expect(result.ok && result.file).toBe(original);
178+
expect(bitmapSpy).not.toHaveBeenCalled();
179+
});
180+
181+
it("compressImageToByteLimit re-encodes an oversized file under the byte cap", async () => {
182+
stubCanvasPipeline(() => 200_000);
183+
184+
const result = await compressImageToByteLimit(makeFile(2_000_000), 1_000_000);
185+
186+
expect(result.ok).toBe(true);
187+
expect(result.ok && result.recompressed).toBe(true);
188+
expect(result.ok && result.file.type).toBe("image/webp");
189+
// The re-encoded name must match the new container format.
190+
expect(result.ok && result.file.name).toBe("shot.webp");
191+
expect(result.ok && result.file.size).toBeLessThanOrEqual(1_000_000);
192+
});
193+
194+
it("compressImageToByteLimit refuses sources above the decode-safety ceiling", async () => {
195+
const bitmapSpy = vi.fn();
196+
vi.stubGlobal("createImageBitmap", bitmapSpy);
197+
198+
const result = await compressImageToByteLimit(
199+
makeFile(MAX_COMPRESSIBLE_SOURCE_BYTES + 1),
200+
10 * 1024 * 1024,
201+
);
202+
203+
expect(result).toEqual({ ok: false, reason: "too-large" });
204+
// The whole point of the ceiling is to never decode such a file.
205+
expect(bitmapSpy).not.toHaveBeenCalled();
206+
});
207+
208+
it("compressImageToByteLimit reports too-large when no encoding fits", async () => {
209+
const { close } = stubCanvasPipeline(() => 3_000_000);
210+
211+
const result = await compressImageToByteLimit(makeFile(2_000_000), 1_000_000);
212+
213+
expect(result).toEqual({ ok: false, reason: "too-large" });
214+
expect(close).toHaveBeenCalled();
215+
});
216+
162217
it("shrinks below the source size when the image is already under MAX_DIMENSION", async () => {
163218
// A small-but-heavy source (e.g. a dense PNG): only a real downscale can
164219
// get it under budget, since quality alone is stubbed to never suffice.

0 commit comments

Comments
 (0)