The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | pickComposerFiles, |
| 3 | pickComposerMedia, |
| 4 | type DraftComposerAttachment, |
| 5 | } from "../lib/composerImages"; |
| 6 | import { startComposerDraftOperation } from "./composerDraftOperations"; |
| 7 | import { changeQuestionAttachmentPreparation } from "./question-attachments"; |
| 8 | import { |
| 9 | appendComposerDraftAttachments, |
| 10 | getComposerDraftSnapshot, |
| 11 | releaseUnusedComposerAttachmentFiles, |
| 12 | } from "./use-composer-drafts"; |
| 13 | |
| 14 | const imagePreparations = new Map<string, Set<(threadKey: string | null) => void>>(); |
| 15 | |
| 16 | /** A paste or accepted picker selection can finish in the thread draft. */ |
| 17 | export function startQuestionImagePreparation(key: string, waitingForSelection = false) { |
| 18 | let destination: string | null = key; |
| 19 | let threadOperation: ReturnType<typeof startComposerDraftOperation> | undefined; |
| 20 | const resolve = (threadKey: string | null) => { |
| 21 | destination = waitingForSelection ? null : threadKey; |
| 22 | if (destination !== null) threadOperation = startComposerDraftOperation(destination); |
| 23 | }; |
| 24 | const pending = imagePreparations.get(key) ?? new Set(); |
| 25 | pending.add(resolve); |
| 26 | imagePreparations.set(key, pending); |
| 27 | changeQuestionAttachmentPreparation(key, 1); |
| 28 | const currentDestination = () => (threadOperation?.signal.aborted ? null : destination); |
| 29 | return { |
| 30 | acceptSelection: () => { |
| 31 | if (destination !== key) return false; |
| 32 | waitingForSelection = false; |
| 33 | return true; |
| 34 | }, |
| 35 | destination: currentDestination, |
| 36 | complete: async ( |
| 37 | result: { attachments: ReadonlyArray<DraftComposerAttachment>; error: string | null }, |
| 38 | scope: QuestionAttachmentScope, |
| 39 | ) => { |
| 40 | const target = currentDestination(); |
| 41 | if (target === null || (target === key && !scope.isActive())) { |
| 42 | await releaseUnusedComposerAttachmentFiles(result.attachments); |
| 43 | return { error: null, rejected: 0 }; |
| 44 | } |
| 45 | const rejected = |
| 46 | target === key |
| 47 | ? scope.append(result.attachments) |
| 48 | : appendUnsentQuestionAttachments(target, result.attachments); |
| 49 | return { error: result.error, rejected }; |
| 50 | }, |
| 51 | finish: () => { |
| 52 | threadOperation?.finish(); |
| 53 | if (imagePreparations.get(key) !== pending || !pending.delete(resolve)) return; |
| 54 | if (pending.size === 0) imagePreparations.delete(key); |
| 55 | changeQuestionAttachmentPreparation(key, -1); |
| 56 | }, |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | export function resolveQuestionImagePreparations(key: string, threadKey: string | null): void { |
| 61 | const pending = imagePreparations.get(key); |
| 62 | imagePreparations.delete(key); |
| 63 | for (const resolve of pending ?? []) resolve(threadKey); |
| 64 | } |
| 65 | |
| 66 | export function appendUnsentQuestionAttachments( |
| 67 | threadKey: string, |
| 68 | attachments: Parameters<typeof appendComposerDraftAttachments>[1], |
| 69 | ): number { |
| 70 | const { text } = getComposerDraftSnapshot(threadKey); |
| 71 | // Chips go after the draft's text, never over a selection the user left in it. |
| 72 | return appendComposerDraftAttachments(threadKey, attachments, { |
| 73 | appendReference: true, |
| 74 | insertion: { text, start: text.length, end: text.length }, |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | interface QuestionAttachmentScope { |
| 79 | readonly isActive: () => boolean; |
| 80 | readonly append: (attachments: ReadonlyArray<DraftComposerAttachment>) => number; |
| 81 | } |
| 82 | |
| 83 | export async function pickQuestionAttachments( |
| 84 | input: QuestionAttachmentScope & { |
| 85 | readonly key: string; |
| 86 | readonly kind: "media" | "files"; |
| 87 | readonly maxUploadBytes?: number | undefined; |
| 88 | }, |
| 89 | ): Promise<{ error: string | null; rejected: number }> { |
| 90 | const preparation = startQuestionImagePreparation(input.key, true); |
| 91 | try { |
| 92 | const existingCount = getComposerDraftSnapshot(input.key).attachments.length; |
| 93 | const acceptSelection = () => input.isActive() && preparation.acceptSelection(); |
| 94 | const result = |
| 95 | input.kind === "files" |
| 96 | ? await pickComposerFiles({ |
| 97 | existingCount, |
| 98 | maxBytes: input.maxUploadBytes, |
| 99 | acceptSelection, |
| 100 | }) |
| 101 | : await pickComposerMedia({ |
| 102 | existingCount, |
| 103 | maxVideoBytes: input.maxUploadBytes, |
| 104 | acceptSelection, |
| 105 | }); |
| 106 | const attachments = "files" in result ? result.files : result.attachments; |
| 107 | return await preparation.complete({ attachments, error: result.error }, input); |
| 108 | } finally { |
| 109 | preparation.finish(); |
| 110 | } |
| 111 | } |