The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { resolveQueuedEditExit } from "@t3tools/client-runtime/queued-run-edit"; |
| 2 | import type { ChatAttachment } from "@t3tools/contracts"; |
| 3 | import { |
| 4 | composerDraftHasUserContent, |
| 5 | useComposerDraftStore, |
| 6 | type ComposerFileAttachment, |
| 7 | type ComposerImageAttachment, |
| 8 | type ComposerThreadTarget, |
| 9 | } from "../../composerDraftStore"; |
| 10 | import { msg } from "gt-react"; |
| 11 | import { m } from "../../i18n/messages"; |
| 12 | import { releaseDraftAttachments } from "../../lib/attachmentUploadQueue"; |
| 13 | |
| 14 | /** Staged attachments upload as soon as they are added, so a dropped edit must release them. */ |
| 15 | export function discardQueuedEditDraft(target: ComposerThreadTarget): void { |
| 16 | const store = useComposerDraftStore.getState(); |
| 17 | const draft = store.getComposerDraft(target); |
| 18 | if (draft) { |
| 19 | releaseDraftAttachments([...draft.images, ...draft.files]); |
| 20 | for (const image of draft.images) { |
| 21 | if (image.previewUrl.startsWith("blob:")) URL.revokeObjectURL(image.previewUrl); |
| 22 | } |
| 23 | } |
| 24 | store.clearComposerContent(target); |
| 25 | } |
| 26 | |
| 27 | export function recoverQueuedMessageEdit(input: { |
| 28 | readonly editTarget: ComposerThreadTarget; |
| 29 | readonly threadTarget: ComposerThreadTarget; |
| 30 | readonly originalText: string; |
| 31 | readonly removedExistingAttachment?: boolean; |
| 32 | }): "kept" | "discarded" | "clean" { |
| 33 | const store = useComposerDraftStore.getState(); |
| 34 | const draft = store.getComposerDraft(input.editTarget); |
| 35 | const recovery = resolveQueuedEditExit({ |
| 36 | editChanged: |
| 37 | input.removedExistingAttachment === true || |
| 38 | (draft !== null && |
| 39 | (draft.prompt !== input.originalText || draft.images.length > 0 || draft.files.length > 0)), |
| 40 | // A cleared edit has nothing to put back, so it can only be discarded. |
| 41 | threadDraftEmpty: |
| 42 | composerDraftHasUserContent(draft) && |
| 43 | !composerDraftHasUserContent(store.getComposerDraft(input.threadTarget)), |
| 44 | }); |
| 45 | if (recovery === "kept") { |
| 46 | store.moveComposerContent(input.editTarget, input.threadTarget); |
| 47 | store.clearComposerContent(input.editTarget); |
| 48 | } else { |
| 49 | discardQueuedEditDraft(input.editTarget); |
| 50 | } |
| 51 | return recovery; |
| 52 | } |
| 53 | |
| 54 | export async function prepareQueuedEditAttachments(input: { |
| 55 | readonly existingAttachments: ReadonlyArray<ChatAttachment>; |
| 56 | readonly images: ReadonlyArray<ComposerImageAttachment>; |
| 57 | readonly files: ReadonlyArray<ComposerFileAttachment>; |
| 58 | readonly uploadFiles: ( |
| 59 | files: ReadonlyArray<ComposerFileAttachment>, |
| 60 | ) => Promise<ReadonlyArray<ChatAttachment>>; |
| 61 | readonly readImage: (file: File) => Promise<string>; |
| 62 | }) { |
| 63 | const files = input.files.length === 0 ? [] : await input.uploadFiles(input.files); |
| 64 | if (files.length !== input.files.length) |
| 65 | throw new Error(m(msg("Retry or remove failed uploads before saving."))); |
| 66 | const images = await Promise.all( |
| 67 | input.images.map(async (image) => ({ |
| 68 | type: "image" as const, |
| 69 | id: image.id, |
| 70 | name: image.name, |
| 71 | mimeType: image.mimeType, |
| 72 | sizeBytes: image.sizeBytes, |
| 73 | dataUrl: await input.readImage(image.file), |
| 74 | ...(image.source ? { source: image.source } : {}), |
| 75 | })), |
| 76 | ); |
| 77 | return [...input.existingAttachments, ...images, ...files]; |
| 78 | } |