@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 527 lines 20.2 KB
Raw
1 import { msg } from "gt-react";
2 import { m } from "~/i18n/messages";
3 import type { ChatComposerProps } from "./ChatComposer";
4 import {
5 type ChatFileAttachment,
6 PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
7 PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
8 PROVIDER_SEND_TURN_MAX_INPUT_CHARS,
9 } from "@t3tools/contracts";
10 import {
11 type ComposerContextReference,
12 collectInlineContextIds,
13 stripInlineContextReferences,
14 } from "~/lib/composerContextReferences";
15 import {
16 type ComposerFileAttachment,
17 type ComposerImageAttachment,
18 type DraftId,
19 composerFileMatchesReattachMarker,
20 composerFileNeedsReattach,
21 composerTargetKey,
22 useComposerDraftStore,
23 } from "../../composerDraftStore";
24 import { type ComposerPromptEditorHandle } from "../ComposerPromptEditorTiptap";
25 import type { PendingUserInputProgress } from "../../pendingUserInput";
26 import type { ComposerAttachmentInsertion } from "../composerAttachmentInsertion";
27 import { type PendingUserInput } from "../../session-logic";
28 import { changeQuestionAttachmentPreparation } from "../../questionAttachments";
29 import {
30 classifyComposerAttachmentFile,
31 composerFileAttachmentMimeType,
32 composerImageSourceKey,
33 readComposerImageBytes,
34 normalizeComposerImageFileMimeType,
35 rememberComposerImageSource,
36 } from "./composerAttachmentFiles";
37 import {
38 fileAttachmentTooLargeMessage,
39 formatAttachmentSize,
40 } from "@t3tools/client-runtime/state/attachments";
41 import { fileContextReference, imageContextReference } from "~/lib/composerContextRecords";
42 import { isMacPlatform, randomUUID } from "~/lib/utils";
43 import {
44 nextPastedTextFileName,
45 pastedTextDisposition,
46 wouldTextPasteExceedLimit,
47 } from "@t3tools/client-runtime/text-paste";
48 import {
49 inheritComposerImageThumbnail,
50 prepareImageForAttachment,
51 } from "../../lib/imageCompression";
52 import { requestConfirmDialog } from "~/confirmDialog";
53 import { toastManager } from "../ui/toast";
54 import { type RefObject, useRef } from "react";
55 import { serializeComposerImages } from "./composerImagePersistence";
56 import {
57 addPreparingComposerImages,
58 getPreparingComposerImages,
59 isPreparingComposerImage,
60 movedPreparingImageThread,
61 removePreparingComposerImages,
62 } from "./preparingComposerImages";
63
64 /**
65 * Takes files into the draft: validates and reserves slots, compresses images, inserts
66 * their inline chips, folds large pastes into text attachments, and confirms removing a referenced image.
67 */
68 export function useComposerAttachmentIntake(input: {
69 readonly activeThreadId: ChatComposerProps["activeThreadId"];
70 readonly activePendingProgress: PendingUserInputProgress | null;
71 readonly activePendingIsResponding: boolean;
72 readonly pendingUserInputs: ReadonlyArray<PendingUserInput>;
73 readonly supportsQuestionAttachments: boolean;
74 readonly isLiteralPendingAnswer: boolean;
75 readonly projectSelectionRequired: boolean;
76 readonly questionAttachmentTarget: DraftId | null;
77 readonly attachmentTargetKey: string;
78 readonly fileStagingLimit: number | null;
79 readonly promptRef: ChatComposerProps["promptRef"];
80 readonly composerImagesRef: ChatComposerProps["composerImagesRef"];
81 readonly composerFilesRef: ChatComposerProps["composerFilesRef"];
82 readonly composerEditorRef: RefObject<ComposerPromptEditorHandle | null>;
83 readonly isRevertingCheckpointRef: RefObject<boolean>;
84 readonly setThreadError: ChatComposerProps["setThreadError"];
85 readonly countReservedAttachments: () => number;
86 readonly addComposerImage: (image: ComposerImageAttachment) => string[];
87 readonly addComposerImagesToDraft: (images: ComposerImageAttachment[]) => string[];
88 readonly addComposerFilesToDraft: (files: ComposerFileAttachment[]) => string[];
89 readonly removeComposerImageFromDraft: (imageId: string) => void;
90 readonly insertAttachmentReferences: (
91 references: ReadonlyArray<ComposerContextReference>,
92 selection?: { start: number; end: number },
93 pendingInsertion?: ComposerAttachmentInsertion,
94 ) => boolean;
95 }) {
96 const {
97 activeThreadId,
98 activePendingProgress,
99 activePendingIsResponding,
100 pendingUserInputs,
101 supportsQuestionAttachments,
102 isLiteralPendingAnswer,
103 projectSelectionRequired,
104 questionAttachmentTarget,
105 attachmentTargetKey,
106 fileStagingLimit,
107 promptRef,
108 composerImagesRef,
109 composerFilesRef,
110 composerEditorRef,
111 isRevertingCheckpointRef,
112 setThreadError,
113 countReservedAttachments,
114 addComposerImage,
115 addComposerImagesToDraft,
116 addComposerFilesToDraft,
117 removeComposerImageFromDraft,
118 insertAttachmentReferences,
119 } = input;
120 const pastedTextFileNamesRef = useRef<{ targetKey: string; names: Set<string> }>({
121 targetKey: "",
122 names: new Set(),
123 });
124
125 const addComposerAttachments = async (
126 files: File[],
127 options?: {
128 readonly source?: ChatFileAttachment["source"];
129 readonly selection?: { start: number; end: number };
130 readonly skipImageInlineChip?: boolean;
131 },
132 ): Promise<boolean> => {
133 if (!activeThreadId || files.length === 0 || isRevertingCheckpointRef.current) return false;
134 if (
135 pendingUserInputs.length > 0 &&
136 (!supportsQuestionAttachments ||
137 activePendingProgress?.activeQuestion?.allowCustomAnswer === false ||
138 activePendingIsResponding)
139 ) {
140 toastManager.add({
141 type: "error",
142 title: m(msg("This question cannot accept attachments.")),
143 });
144 return false;
145 }
146 // Captured before the awaits below: the user may switch threads while a
147 // large image is being compressed, and the attachments and errors belong
148 // to the thread the paste happened in.
149 const threadId = activeThreadId;
150 const imageAttachmentsGetChips =
151 !options?.skipImageInlineChip &&
152 (options?.selection !== undefined ||
153 pendingUserInputs.length > 0 ||
154 projectSelectionRequired ||
155 stripInlineContextReferences(promptRef.current).trim().length > 0);
156
157 // Validation happens synchronously so concurrent pastes see each other:
158 // accepted files reserve their attachment slots (as preparing images)
159 // before the first await, keeping the total under the limit.
160 let reservedCount = countReservedAttachments();
161 // A pick that matches a needs-reattach marker replaces it in the draft, so
162 // it must not consume a slot; a draft full of markers would otherwise hit
163 // the capacity error before the replacement path could run.
164 const reattachMarkers = composerFilesRef.current.filter(composerFileNeedsReattach);
165 const replacedReattachMarkerIds = new Set<string>();
166 const acceptedImages: File[] = [];
167 const acceptedFiles: ComposerFileAttachment[] = [];
168 let error: string | null = null;
169 for (const file of files) {
170 const attachmentKind = classifyComposerAttachmentFile(file);
171 const fileMimeType =
172 attachmentKind === "file" ? composerFileAttachmentMimeType(file) : file.type;
173 const matchingReattachMarker =
174 attachmentKind === "file"
175 ? reattachMarkers.find(
176 (marker) =>
177 !replacedReattachMarkerIds.has(marker.id) &&
178 composerFileMatchesReattachMarker(marker, {
179 name: file.name || "file",
180 mimeType: fileMimeType,
181 sizeBytes: file.size,
182 }),
183 )
184 : undefined;
185 if (matchingReattachMarker) {
186 replacedReattachMarkerIds.add(matchingReattachMarker.id);
187 }
188 if (!matchingReattachMarker && reservedCount >= PROVIDER_SEND_TURN_MAX_ATTACHMENTS) {
189 error = m(
190 msg("You can attach up to {max} files per message.", {
191 max: PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
192 }),
193 );
194 continue;
195 }
196 if (attachmentKind === "unsupported-image") {
197 error = m(
198 msg(
199 "''{name}'' is not a supported image type. Attach GIF, HEIC, HEIF, JPEG, PNG, or WebP images.",
200 { name: file.name },
201 ),
202 );
203 continue;
204 }
205 if (attachmentKind === "image") {
206 acceptedImages.push(normalizeComposerImageFileMimeType(file));
207 } else {
208 if (fileStagingLimit === null) {
209 error = m(msg("This server does not support file attachments."));
210 continue;
211 }
212 if (file.size <= 0) {
213 error = m(msg("''{name}'' is empty or could not be read.", { name: file.name }));
214 continue;
215 }
216 if (file.size > fileStagingLimit) {
217 error = fileAttachmentTooLargeMessage(file.name, fileStagingLimit, m);
218 continue;
219 }
220 const attachmentFile =
221 file.type === fileMimeType
222 ? file
223 : new File([file], file.name, { type: fileMimeType, lastModified: file.lastModified });
224 acceptedFiles.push({
225 type: "file",
226 id: randomUUID(),
227 name: attachmentFile.name || "file",
228 mimeType: fileMimeType,
229 sizeBytes: attachmentFile.size,
230 file: attachmentFile,
231 ...(options?.source ? { source: options.source } : {}),
232 });
233 }
234 if (!matchingReattachMarker) {
235 reservedCount += 1;
236 }
237 }
238 setThreadError(threadId, error);
239 let insertedAny = false;
240 if (acceptedFiles.length > 0) {
241 const storedIds = new Set(addComposerFilesToDraft(acceptedFiles));
242 const storedFiles = acceptedFiles.filter((file) => storedIds.has(file.id));
243 if (storedFiles.length > 0) {
244 insertedAny = insertAttachmentReferences(
245 storedFiles.map(fileContextReference),
246 options?.selection,
247 );
248 }
249 if (options?.source?._tag === "pasted-text" && storedFiles.length > 0) {
250 const attached = storedFiles[0]!;
251 toastManager.add({
252 type: "info",
253 title: m(msg("Large paste attached as {name}", { name: attached.name })),
254 description: m(
255 msg("{size} · Use {shortcut} to keep a large paste inline.", {
256 size: formatAttachmentSize(attached.sizeBytes, m),
257 shortcut: isMacPlatform(navigator.platform) ? "⌘⇧V" : "Ctrl+Shift+V",
258 }),
259 ),
260 data: { hideCopyButton: true },
261 });
262 }
263 }
264 if (acceptedImages.length === 0) return insertedAny;
265
266 // The store drops a repeated image only after compressing it, so a likely repeat prepares
267 // without a tile instead of flashing one. Only the store decides it is a duplicate.
268 const imageSourceKeys = new Set(
269 [...composerImagesRef.current, ...getPreparingComposerImages(attachmentTargetKey)].map(
270 (image) => composerImageSourceKey(image.file),
271 ),
272 );
273 // The draft image reuses its placeholder's id and thumbnail so the strip tile stays exactly
274 // as it is when the compressed file replaces it.
275 const preparingImages = acceptedImages.map((file) => {
276 const sourceKey = composerImageSourceKey(file);
277 const hidden = imageSourceKeys.has(sourceKey);
278 imageSourceKeys.add(sourceKey);
279 return { id: randomUUID(), name: file.name || "image", file, hidden };
280 });
281 addPreparingComposerImages(attachmentTargetKey, preparingImages);
282 if (questionAttachmentTarget)
283 changeQuestionAttachmentPreparation(questionAttachmentTarget, acceptedImages.length);
284 const compressed: { id: string; file: File }[] = [];
285 // Keyed by image so a failure whose tile was removed (even mid-compression) is not reported.
286 const compressionErrors = new Map<string, string>();
287 const pendingInsertion = imageAttachmentsGetChips
288 ? composerEditorRef.current?.captureAttachmentInsertion()
289 : undefined;
290 for (const image of preparingImages) {
291 const { id, file } = image;
292 const movedThread = movedPreparingImageThread(image);
293 // Removed from the strip while an earlier image was compressing.
294 if (
295 !isPreparingComposerImage(
296 movedThread ? composerTargetKey(movedThread) : attachmentTargetKey,
297 id,
298 )
299 )
300 continue;
301 // A rejection must still reach the cleanup below: a leaked
302 // placeholder would block sending from this draft for good.
303 const result = await prepareImageForAttachment(
304 file,
305 PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
306 ).catch(() => ({ ok: false, reason: "unreadable" }) as const);
307 if (!result.ok) {
308 compressionErrors.set(
309 id,
310 result.reason === "unreadable"
311 ? m(msg("''{name}'' could not be read as an image.", { name: file.name }))
312 : m(
313 msg("''{name}'' is too large to attach, even after compression.", {
314 name: file.name,
315 }),
316 ),
317 );
318 continue;
319 }
320 await inheritComposerImageThumbnail(file, result.file);
321 await readComposerImageBytes(result.file);
322 rememberComposerImageSource(file, result.file);
323 compressed.push({ id, file: result.file });
324 }
325 pendingInsertion?.dispose();
326 const movedThread = preparingImages.map(movedPreparingImageThread).find(Boolean);
327 const completionTargetKey = movedThread ? composerTargetKey(movedThread) : attachmentTargetKey;
328 const nextImages: ComposerImageAttachment[] = compressed
329 .filter(({ id }) => isPreparingComposerImage(completionTargetKey, id))
330 .map(({ id, file }) => ({
331 type: "image",
332 id,
333 name: file.name || "image",
334 mimeType: file.type,
335 sizeBytes: file.size,
336 previewUrl: URL.createObjectURL(file),
337 file,
338 }));
339 const unrestoredImageNames: string[] = [];
340 const draftStore = useComposerDraftStore.getState();
341 const storedImageIds = new Set(
342 movedThread
343 ? draftStore.addImages(movedThread, nextImages, {
344 onCapacityExceeded: (image) => unrestoredImageNames.push(image.name),
345 })
346 : nextImages.length === 1 && nextImages[0]
347 ? addComposerImage(nextImages[0])
348 : nextImages.length > 1
349 ? addComposerImagesToDraft(nextImages)
350 : [],
351 );
352 // Keep the placeholders until the draft decides whether the recovered images fit,
353 // so a held send can be cancelled before the last placeholder disappears.
354 const stillPreparingIds = removePreparingComposerImages(
355 completionTargetKey,
356 new Set(preparingImages.map((image) => image.id)),
357 { cancelSubmission: compressionErrors.size > 0 || unrestoredImageNames.length > 0 },
358 );
359 if (questionAttachmentTarget && !movedThread)
360 changeQuestionAttachmentPreparation(questionAttachmentTarget, -stillPreparingIds.size);
361 const storedImages = nextImages.filter((image) => storedImageIds.has(image.id));
362 // The draft may be gone by now; revoking a URL the store already revoked is harmless.
363 for (const image of nextImages) {
364 if (!storedImageIds.has(image.id)) URL.revokeObjectURL(image.previewUrl);
365 }
366 if (storedImages.length > 0 && imageAttachmentsGetChips && !movedThread) {
367 insertedAny =
368 insertAttachmentReferences(
369 storedImages.map(imageContextReference),
370 undefined,
371 pendingInsertion,
372 ) || insertedAny;
373 }
374 // Only failures are reported here. Success must not pass `null`: by
375 // now other work (a failed send, an overlapping paste) may have set a
376 // thread error this call knows nothing about, and clearing it would
377 // swallow that message.
378 const completionErrors = [...compressionErrors]
379 .filter(([id]) => stillPreparingIds.has(id))
380 .map(([, message]) => message)
381 .slice(-1);
382 if (unrestoredImageNames.length > 0) {
383 completionErrors.push(
384 m(
385 msg("{names} could not be restored: the composer is at its {limit}-attachment limit.", {
386 names: unrestoredImageNames.join(", "),
387 limit: PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
388 }),
389 ),
390 );
391 }
392 if (completionErrors.length > 0) {
393 setThreadError(threadId, completionErrors.join("\n"));
394 }
395 if (movedThread && storedImages.length > 0) {
396 const serialized = await serializeComposerImages(storedImages, []);
397 await draftStore.appendPersistedAttachments(movedThread, storedImages, serialized);
398 }
399 return insertedAny;
400 };
401
402 const removeComposerImage = (imageId: string) => {
403 if (
404 removePreparingComposerImages(attachmentTargetKey, new Set([imageId]), {
405 cancelSubmission: true,
406 }).size > 0
407 ) {
408 // Its compression finishes in the background and is dropped.
409 if (questionAttachmentTarget)
410 changeQuestionAttachmentPreparation(questionAttachmentTarget, -1);
411 return;
412 }
413 const image = composerImagesRef.current.find((candidate) => candidate.id === imageId);
414 const referenced = collectInlineContextIds(promptRef.current).includes(
415 image ? imageContextReference(image).contextId : "",
416 );
417 if (!referenced) {
418 removeComposerImageFromDraft(imageId);
419 return;
420 }
421 const confirmation = requestConfirmDialog(
422 image?.name != null
423 ? m(
424 msg(
425 "Remove {name} from the message?\nIt is referenced in your text; removing it also removes every reference.",
426 { name: image.name },
427 ),
428 )
429 : m(
430 msg(
431 "Remove this image from the message?\nIt is referenced in your text; removing it also removes every reference.",
432 ),
433 ),
434 { variant: "destructive" },
435 );
436 if (!confirmation) {
437 removeComposerImageFromDraft(imageId);
438 return;
439 }
440 void confirmation.then((confirmed) => {
441 if (confirmed) removeComposerImageFromDraft(imageId);
442 });
443 };
444
445 const foldPastedText = (
446 plainText: string,
447 bypassAutoAttachment: boolean,
448 selectionOverride?: { start: number; end: number },
449 ): boolean => {
450 if (isLiteralPendingAnswer) return false;
451 const questionCanAttach =
452 pendingUserInputs.length === 0 ||
453 (supportsQuestionAttachments &&
454 activePendingProgress?.activeQuestion?.allowCustomAnswer !== false &&
455 !activePendingIsResponding);
456 const hasAttachmentSlot = countReservedAttachments() < PROVIDER_SEND_TURN_MAX_ATTACHMENTS;
457 const selection = selectionOverride ?? composerEditorRef.current?.readSelectionRange();
458 const wouldExceedInputLimit = wouldTextPasteExceedLimit({
459 valueLength: promptRef.current.length,
460 selection: selection ?? { start: 0, end: 0 },
461 textLength: plainText.length,
462 maxLength: PROVIDER_SEND_TURN_MAX_INPUT_CHARS,
463 });
464 const shouldFold =
465 pastedTextDisposition({
466 text: plainText,
467 bypassAutoAttachment,
468 wouldExceedInputLimit,
469 canAttach: true,
470 }) === "attachment";
471 if (!shouldFold) {
472 return false;
473 }
474
475 const canStageAttachment =
476 Boolean(activeThreadId) &&
477 !isRevertingCheckpointRef.current &&
478 questionCanAttach &&
479 hasAttachmentSlot;
480 if (!canStageAttachment || fileStagingLimit === null) {
481 if (!wouldExceedInputLimit) {
482 return false;
483 }
484 toastManager.add({
485 type: "error",
486 title: m(msg("Pasted text is too large for this message")),
487 description: m(msg("Remove some text or an attachment, then paste again.")),
488 data: { hideCopyButton: true },
489 });
490 return true;
491 }
492
493 if (pastedTextFileNamesRef.current.targetKey !== attachmentTargetKey) {
494 pastedTextFileNamesRef.current = { targetKey: attachmentTargetKey, names: new Set() };
495 }
496 const reservedNames = pastedTextFileNamesRef.current.names;
497 for (const file of composerFilesRef.current) reservedNames.add(file.name);
498 const foldedFileName = nextPastedTextFileName([...reservedNames]);
499 reservedNames.add(foldedFileName);
500 const foldedFile = new File([plainText], foldedFileName, {
501 type: "text/plain;charset=utf-8",
502 });
503 if (foldedFile.size > fileStagingLimit) {
504 reservedNames.delete(foldedFileName);
505 if (!wouldExceedInputLimit) return false;
506 toastManager.add({
507 type: "error",
508 title: m(msg("Pasted text is too large to attach")),
509 description: m(msg("Reduce the clipboard contents or save a smaller excerpt as a file.")),
510 data: { hideCopyButton: true },
511 });
512 return true;
513 }
514
515 void addComposerAttachments([foldedFile], {
516 source: { _tag: "pasted-text" },
517 ...(selection ? { selection } : {}),
518 });
519 return true;
520 };
521
522 return {
523 addComposerAttachments,
524 removeComposerImage,
525 foldPastedText,
526 };
527 }