The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | type EnvironmentId, |
| 3 | isProviderSendTurnSupportedImageMimeType, |
| 4 | PROVIDER_SEND_TURN_MAX_FILE_BYTES, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import { |
| 7 | clampFileAttachmentUploadBytes, |
| 8 | fileAttachmentTooLargeMessage, |
| 9 | } from "@t3tools/client-runtime/state/attachments"; |
| 10 | import { msg } from "gt-react"; |
| 11 | |
| 12 | import type { ComposerFileAttachment, ComposerImageAttachment } from "../../composerDraftStore"; |
| 13 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 14 | import { isHeicImageFile } from "../../lib/imageCompression"; |
| 15 | import { isVideoAttachment, videoMimeType } from "../../types"; |
| 16 | import { isSvgAttachment } from "@t3tools/shared/image"; |
| 17 | import { isWorkspaceImagePreviewPath } from "@t3tools/shared/mediaPreview"; |
| 18 | |
| 19 | type ComposerAttachmentFileKind = "image" | "file" | "unsupported-image"; |
| 20 | |
| 21 | interface FileAttachmentCapabilityState { |
| 22 | readonly attachmentUploadsCapabilityKnown: boolean; |
| 23 | readonly supportsAttachmentUploads: boolean; |
| 24 | readonly maxFileAttachmentBytes: number | null; |
| 25 | } |
| 26 | |
| 27 | const IMAGE_MIME_TYPE_BY_EXTENSION: Readonly<Record<string, string>> = { |
| 28 | gif: "image/gif", |
| 29 | jpeg: "image/jpeg", |
| 30 | jpg: "image/jpeg", |
| 31 | png: "image/png", |
| 32 | webp: "image/webp", |
| 33 | }; |
| 34 | |
| 35 | /** |
| 36 | * Some sources (drags from other apps, files piped through a shell) hand over a |
| 37 | * `File` with an empty or generic MIME type. |
| 38 | */ |
| 39 | function inferImageMimeTypeFromName(name: string): string | null { |
| 40 | const dotIndex = name.lastIndexOf("."); |
| 41 | if (dotIndex <= 0) { |
| 42 | return null; |
| 43 | } |
| 44 | return IMAGE_MIME_TYPE_BY_EXTENSION[name.slice(dotIndex + 1).toLowerCase()] ?? null; |
| 45 | } |
| 46 | |
| 47 | function inferImageMimeTypeForUnknownFile(file: Pick<File, "name" | "type">): string | null { |
| 48 | const mimeType = file.type.toLowerCase(); |
| 49 | if (mimeType !== "" && mimeType !== "application/octet-stream") { |
| 50 | return null; |
| 51 | } |
| 52 | return inferImageMimeTypeFromName(file.name); |
| 53 | } |
| 54 | |
| 55 | export function normalizeComposerImageFileMimeType(file: File): File { |
| 56 | const inferredMimeType = inferImageMimeTypeForUnknownFile(file); |
| 57 | if (!inferredMimeType) { |
| 58 | return file; |
| 59 | } |
| 60 | return new File([file], file.name, { |
| 61 | type: inferredMimeType, |
| 62 | lastModified: file.lastModified, |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | function composerImageSignature( |
| 67 | image: Pick<ComposerImageAttachment, "mimeType" | "sizeBytes" | "name">, |
| 68 | ): string { |
| 69 | // Hydrated images get fresh lastModified values, so exclude it from dedupe. |
| 70 | return `${image.mimeType}\u0000${image.sizeBytes}\u0000${image.name}`; |
| 71 | } |
| 72 | |
| 73 | // Byte hashes by file; a file whose bytes were never read gets a key that matches only itself. |
| 74 | const composerImageFingerprints = new WeakMap<File, string>(); |
| 75 | let unreadComposerImageCount = 0; |
| 76 | |
| 77 | function composerImageFingerprint(file: File): string { |
| 78 | let fingerprint = composerImageFingerprints.get(file); |
| 79 | if (fingerprint === undefined) { |
| 80 | fingerprint = `unread:${unreadComposerImageCount++}`; |
| 81 | composerImageFingerprints.set(file, fingerprint); |
| 82 | } |
| 83 | return fingerprint; |
| 84 | } |
| 85 | |
| 86 | /** Records the bytes behind an image file, so a draft drops it only as a byte-for-byte repeat. */ |
| 87 | export function rememberComposerImageBytes(file: File, bytes: Uint8Array): void { |
| 88 | // cyrb53, synchronous: crypto.subtle is missing on plain-http remote origins. |
| 89 | let first = 0xdeadbeef; |
| 90 | let second = 0x41c6ce57; |
| 91 | for (let index = 0; index < bytes.length; index++) { |
| 92 | const byte = bytes[index]!; |
| 93 | first = Math.imul(first ^ byte, 2654435761); |
| 94 | second = Math.imul(second ^ byte, 1597334677); |
| 95 | } |
| 96 | first = Math.imul(first ^ (first >>> 16), 2246822507); |
| 97 | first ^= Math.imul(second ^ (second >>> 13), 3266489909); |
| 98 | second = Math.imul(second ^ (second >>> 16), 2246822507); |
| 99 | second ^= Math.imul(first ^ (first >>> 13), 3266489909); |
| 100 | composerImageFingerprints.set(file, `${second >>> 0}:${first >>> 0}`); |
| 101 | } |
| 102 | |
| 103 | export async function readComposerImageBytes(file: File): Promise<void> { |
| 104 | // An unreadable file keeps matching only itself. |
| 105 | const bytes = await file.arrayBuffer().catch(() => null); |
| 106 | if (bytes) rememberComposerImageBytes(file, new Uint8Array(bytes)); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Matches name, type, size and bytes; unread files match only themselves. |
| 111 | * Producers must await readComposerImageBytes before publishing images to the draft. |
| 112 | */ |
| 113 | export function composerImageDedupKey( |
| 114 | image: Pick<ComposerImageAttachment, "mimeType" | "sizeBytes" | "name" | "file">, |
| 115 | ): string { |
| 116 | return `${composerImageSignature(image)}\u0000${composerImageFingerprint(image.file)}`; |
| 117 | } |
| 118 | |
| 119 | // Compression changes an image's bytes, so a compressed image answers for the file it came from. |
| 120 | const composerImageSourceKeys = new WeakMap<File, string>(); |
| 121 | |
| 122 | /** A pasted image's name, type and size: a guess, before its bytes are read, that the draft has it. */ |
| 123 | export function composerImageSourceKey(file: File): string { |
| 124 | return ( |
| 125 | composerImageSourceKeys.get(file) ?? |
| 126 | composerImageSignature({ |
| 127 | mimeType: file.type, |
| 128 | sizeBytes: file.size, |
| 129 | name: file.name || "image", |
| 130 | }) |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | export function rememberComposerImageSource(source: File, prepared: File): void { |
| 135 | if (prepared !== source) composerImageSourceKeys.set(prepared, composerImageSourceKey(source)); |
| 136 | } |
| 137 | |
| 138 | function isDngFile(file: Pick<File, "name" | "type">): boolean { |
| 139 | const mimeType = file.type.split(";", 1)[0]?.trim().toLowerCase(); |
| 140 | return ( |
| 141 | file.name.toLowerCase().endsWith(".dng") || |
| 142 | mimeType === "image/dng" || |
| 143 | mimeType === "image/x-dng" || |
| 144 | mimeType === "image/x-adobe-dng" |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | export function composerFileAttachmentMimeType(file: Pick<File, "name" | "type">): string { |
| 149 | // Downstream image detection uses MIME, even for attachments staged as files. |
| 150 | if (isDngFile(file)) return "image/x-adobe-dng"; |
| 151 | if (isSvgAttachment({ name: file.name, mimeType: file.type })) return "image/svg+xml"; |
| 152 | return ( |
| 153 | videoMimeType({ name: file.name, mimeType: file.type }) ?? |
| 154 | (file.type || "application/octet-stream") |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | export function classifyComposerAttachmentFile( |
| 159 | file: Pick<File, "name" | "type">, |
| 160 | ): ComposerAttachmentFileKind { |
| 161 | if (isSvgAttachment({ name: file.name, mimeType: file.type })) return "file"; |
| 162 | if (isDngFile(file)) return "file"; |
| 163 | if (isHeicImageFile(file)) { |
| 164 | return "image"; |
| 165 | } |
| 166 | if (inferImageMimeTypeForUnknownFile(file)) { |
| 167 | return "image"; |
| 168 | } |
| 169 | if (!file.type.toLowerCase().startsWith("image/")) { |
| 170 | return "file"; |
| 171 | } |
| 172 | if (isProviderSendTurnSupportedImageMimeType(file.type)) return "image"; |
| 173 | // These can be previewed as files, but providers must not receive them as images. |
| 174 | return isWorkspaceImagePreviewPath(file.name) ? "file" : "unsupported-image"; |
| 175 | } |
| 176 | |
| 177 | export function isPreviewableComposerVideo( |
| 178 | file: ComposerFileAttachment, |
| 179 | environmentId: EnvironmentId, |
| 180 | ): boolean { |
| 181 | return ( |
| 182 | isVideoAttachment(file) && |
| 183 | (file.file !== null || |
| 184 | (file.uploadedAttachmentId !== undefined && file.uploadEnvironmentId === environmentId)) |
| 185 | ); |
| 186 | } |
| 187 | |
| 188 | export function composerOtherFilesForPresentation( |
| 189 | files: ReadonlyArray<ComposerFileAttachment>, |
| 190 | environmentId: EnvironmentId, |
| 191 | inlineFileIds: ReadonlySet<string>, |
| 192 | ): ComposerFileAttachment[] { |
| 193 | return files.filter( |
| 194 | (file) => !isPreviewableComposerVideo(file, environmentId) && !inlineFileIds.has(file.id), |
| 195 | ); |
| 196 | } |
| 197 | |
| 198 | export function fileAttachmentStagingLimit(input: FileAttachmentCapabilityState): number | null { |
| 199 | if (!input.attachmentUploadsCapabilityKnown) { |
| 200 | return PROVIDER_SEND_TURN_MAX_FILE_BYTES; |
| 201 | } |
| 202 | if (!input.supportsAttachmentUploads || input.maxFileAttachmentBytes === null) { |
| 203 | return null; |
| 204 | } |
| 205 | return clampFileAttachmentUploadBytes(input.maxFileAttachmentBytes); |
| 206 | } |
| 207 | |
| 208 | export function fileAttachmentCapabilityBlockReason( |
| 209 | input: FileAttachmentCapabilityState & { |
| 210 | readonly files: ReadonlyArray<{ readonly name: string; readonly sizeBytes: number }>; |
| 211 | }, |
| 212 | resolve: MessageResolver = m, |
| 213 | ): string | null { |
| 214 | if (input.files.length === 0) { |
| 215 | return null; |
| 216 | } |
| 217 | if (!input.attachmentUploadsCapabilityKnown) { |
| 218 | return resolve(msg("Waiting for the server before file attachments can send")); |
| 219 | } |
| 220 | const maxFileAttachmentBytes = fileAttachmentStagingLimit(input); |
| 221 | if (maxFileAttachmentBytes === null) { |
| 222 | return resolve( |
| 223 | msg("This server does not accept file attachments right now. Remove the files to send."), |
| 224 | ); |
| 225 | } |
| 226 | const oversizedFile = input.files.find((file) => file.sizeBytes > maxFileAttachmentBytes); |
| 227 | if (oversizedFile) { |
| 228 | return fileAttachmentTooLargeMessage(oversizedFile.name, maxFileAttachmentBytes, resolve); |
| 229 | } |
| 230 | return null; |
| 231 | } |
| 232 | |
| 233 | /** Release only unpersisted uploads; hydrated files still need their server copies. */ |
| 234 | export function attachmentsToReleaseOnUploadCapabilityLoss( |
| 235 | attachments: ReadonlyArray<ComposerImageAttachment | ComposerFileAttachment>, |
| 236 | ): Array<ComposerImageAttachment | ComposerFileAttachment> { |
| 237 | return attachments.filter( |
| 238 | (attachment) => !(attachment.type === "file" && attachment.uploadedAttachmentId !== undefined), |
| 239 | ); |
| 240 | } |
| 241 | |
| 242 | export function shouldHandleComposerAttachmentPaste(input: { |
| 243 | readonly files: ReadonlyArray<File>; |
| 244 | readonly plainText: string; |
| 245 | readonly bypassAutoAttachment?: boolean; |
| 246 | }): boolean { |
| 247 | // Paste as Text keeps the text even when the source also put an image of it on the clipboard. |
| 248 | if (input.bypassAutoAttachment && input.plainText.length > 0) return false; |
| 249 | if ( |
| 250 | input.files.some((file) => { |
| 251 | if (isDngFile(file)) return true; |
| 252 | const classification = classifyComposerAttachmentFile(file); |
| 253 | return ( |
| 254 | classification === "image" || |
| 255 | classification === "unsupported-image" || |
| 256 | isSvgAttachment({ name: file.name, mimeType: file.type }) |
| 257 | ); |
| 258 | }) |
| 259 | ) { |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | if (input.plainText.length > 0) { |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | return input.files.some((file) => classifyComposerAttachmentFile(file) === "file"); |
| 268 | } |