Skip to content
Source
Sign inSign up
Open History to see the last commit

imageAttachmentSize.ts

28 lines1.2 KB

1/**
2 * Anthropic rejects an image over 2000px on a side once a request holds more than 20 images, and
3 * Claude Code forwards images sent into a running turn unchanged, so one large paste breaks
4 * every later request in that thread.
5 */
6export const IMAGE_ATTACHMENT_MAX_EDGE = 2000;
7​
8export type DownscalableImageMimeType = "image/png" | "image/jpeg";
9​
10export function downscalableImageMimeType(mimeType: string): DownscalableImageMimeType | null {
11 const normalized = mimeType.toLowerCase();
12 if (normalized === "image/png") return "image/png";
13 if (normalized === "image/jpeg" || normalized === "image/jpg") return "image/jpeg";
14 return null;
16​
17export function fitImageAttachmentSize(size: {
18 readonly width: number;
19 readonly height: number;
20}): { width: number; height: number } | null {
21 const longestEdge = Math.max(size.width, size.height);
22 if (longestEdge <= IMAGE_ATTACHMENT_MAX_EDGE) return null;
23 const scale = IMAGE_ATTACHMENT_MAX_EDGE / longestEdge;
24 return {
25 width: Math.max(1, Math.min(IMAGE_ATTACHMENT_MAX_EDGE, Math.round(size.width * scale))),
26 height: Math.max(1, Math.min(IMAGE_ATTACHMENT_MAX_EDGE, Math.round(size.height * scale))),
27 };