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
| 1 | import * as Dialog from "../components/ui/Dialog"; |
| 2 | import { errorDisplayMessage } from "@t3tools/client-runtime/errors"; |
| 3 | import { msg } from "gt-react-native"; |
| 4 | import { useRef } from "react"; |
| 5 | import { m } from "../i18n/messages"; |
| 6 | import { PROVIDER_SEND_TURN_MAX_ATTACHMENTS, type ServerConfig } from "@t3tools/contracts"; |
| 7 | import { clampFileAttachmentUploadBytes } from "@t3tools/client-runtime/state/attachments"; |
| 8 | import { nextPastedTextFileName } from "@t3tools/client-runtime/text-paste"; |
| 9 | import type { ComposerTextPaste } from "../components/ComposerEditor"; |
| 10 | import { |
| 11 | convertPastedImagesToAttachments, |
| 12 | createPastedTextComposerAttachment, |
| 13 | pickComposerFiles, |
| 14 | pickComposerMedia, |
| 15 | removePersistedComposerAttachmentFile, |
| 16 | type DraftComposerAttachment, |
| 17 | } from "../lib/composerImages"; |
| 18 | import { |
| 19 | appendComposerDraftAttachments, |
| 20 | captureComposerDraftInsertion, |
| 21 | countComposerDraftAttachmentsAfterSelection, |
| 22 | getComposerDraftSnapshot, |
| 23 | type ComposerDraftInsertion, |
| 24 | } from "./use-composer-drafts"; |
| 25 | import { startComposerDraftOperation } from "./composerDraftOperations"; |
| 26 | |
| 27 | export function useComposerAttachmentActions(props: { |
| 28 | readonly draftKey: string | null; |
| 29 | readonly serverConfig: ServerConfig | null | undefined; |
| 30 | readonly locked?: boolean; |
| 31 | }) { |
| 32 | const reservedNames = useRef(new Map<string, Set<string>>()); |
| 33 | const capabilities = props.serverConfig?.environment.capabilities; |
| 34 | const append = async ( |
| 35 | key: string, |
| 36 | signal: AbortSignal, |
| 37 | attachments: ReadonlyArray<DraftComposerAttachment>, |
| 38 | insertion: ComposerDraftInsertion, |
| 39 | ) => { |
| 40 | if (signal.aborted) { |
| 41 | await Promise.all( |
| 42 | attachments.flatMap((item) => |
| 43 | item.fileUri ? [removePersistedComposerAttachmentFile(item.fileUri)] : [], |
| 44 | ), |
| 45 | ); |
| 46 | return 0; |
| 47 | } |
| 48 | return appendComposerDraftAttachments(key, attachments, { appendReference: true, insertion }); |
| 49 | }; |
| 50 | // A picker may finish after its draft was discarded or the composer moved off it. |
| 51 | const withDraftOperation = async <T>(key: string, work: (signal: AbortSignal) => Promise<T>) => { |
| 52 | const operation = startComposerDraftOperation(key); |
| 53 | return work(operation.signal).finally(operation.finish); |
| 54 | }; |
| 55 | const report = (title: string, error: string | null | undefined, rejected: number) => { |
| 56 | const problems = [ |
| 57 | ...(error ? [error] : []), |
| 58 | ...(rejected |
| 59 | ? [ |
| 60 | m( |
| 61 | msg("You can attach up to {max} attachments per message.", { |
| 62 | max: PROVIDER_SEND_TURN_MAX_ATTACHMENTS, |
| 63 | }), |
| 64 | ), |
| 65 | ] |
| 66 | : []), |
| 67 | ]; |
| 68 | if (problems.length) Dialog.alert(title, problems.join("\n\n")); |
| 69 | }; |
| 70 | const onPickDraftMedia = async () => { |
| 71 | const key = props.draftKey; |
| 72 | if (!key || props.locked) return; |
| 73 | const insertion = captureComposerDraftInsertion(key); |
| 74 | await withDraftOperation(key, async (signal) => { |
| 75 | const result = await pickComposerMedia({ |
| 76 | existingCount: countComposerDraftAttachmentsAfterSelection(key, insertion), |
| 77 | maxVideoBytes: capabilities?.fileAttachments?.maxUploadBytes, |
| 78 | }); |
| 79 | report( |
| 80 | m(msg("Could not attach photo or video")), |
| 81 | result.error, |
| 82 | await append(key, signal, result.attachments, insertion), |
| 83 | ); |
| 84 | }); |
| 85 | }; |
| 86 | const onPickDraftFiles = async () => { |
| 87 | const key = props.draftKey; |
| 88 | if (!key || props.locked) return; |
| 89 | const maxBytes = capabilities?.fileAttachments?.maxUploadBytes; |
| 90 | if (maxBytes === undefined) { |
| 91 | Dialog.alert( |
| 92 | m(msg("Could not attach file")), |
| 93 | m(msg("This server does not support file attachments.")), |
| 94 | ); |
| 95 | return; |
| 96 | } |
| 97 | const insertion = captureComposerDraftInsertion(key); |
| 98 | await withDraftOperation(key, async (signal) => { |
| 99 | const result = await pickComposerFiles({ |
| 100 | existingCount: countComposerDraftAttachmentsAfterSelection(key, insertion), |
| 101 | maxBytes, |
| 102 | }); |
| 103 | report( |
| 104 | m(msg("Could not attach file")), |
| 105 | result.error, |
| 106 | await append(key, signal, result.files, insertion), |
| 107 | ); |
| 108 | }); |
| 109 | }; |
| 110 | const onNativePasteImages = async (uris: ReadonlyArray<string>) => { |
| 111 | const key = props.draftKey; |
| 112 | if (!key || props.locked || !uris.length) return false; |
| 113 | const insertion = captureComposerDraftInsertion(key); |
| 114 | return withDraftOperation(key, async (signal) => { |
| 115 | try { |
| 116 | const result = await convertPastedImagesToAttachments({ uris }); |
| 117 | const rejected = await append(key, signal, result.attachments, insertion); |
| 118 | report(m(msg("Could not paste images")), result.error, rejected); |
| 119 | if (signal.aborted) return false; |
| 120 | if (result.error) return false; |
| 121 | if (rejected > 0) return false; |
| 122 | return result.attachments.length > 0; |
| 123 | } catch (error) { |
| 124 | Dialog.alert( |
| 125 | m(msg("Could not paste images")), |
| 126 | errorDisplayMessage(error, m) ?? m(msg("Try again.")), |
| 127 | ); |
| 128 | return false; |
| 129 | } |
| 130 | }); |
| 131 | }; |
| 132 | const onNativePasteText = async (paste: ComposerTextPaste) => { |
| 133 | const key = props.draftKey; |
| 134 | if (!key || props.locked) return; |
| 135 | const maxBytes = capabilities?.fileAttachments?.maxUploadBytes; |
| 136 | if (maxBytes === undefined) return; |
| 137 | const names = reservedNames.current.get(key) ?? new Set<string>(); |
| 138 | reservedNames.current.set(key, names); |
| 139 | for (const attachment of getComposerDraftSnapshot(key).attachments) names.add(attachment.name); |
| 140 | const name = nextPastedTextFileName([...names]); |
| 141 | names.add(name); |
| 142 | await withDraftOperation(key, async (signal) => { |
| 143 | try { |
| 144 | const attachment = await createPastedTextComposerAttachment({ |
| 145 | text: paste.text, |
| 146 | name, |
| 147 | maxBytes: clampFileAttachmentUploadBytes(maxBytes), |
| 148 | }); |
| 149 | const rejected = await append(key, signal, [attachment], { |
| 150 | text: paste.value, |
| 151 | ...paste.selection, |
| 152 | }); |
| 153 | if (rejected) await removePersistedComposerAttachmentFile(attachment.fileUri); |
| 154 | report(m(msg("Could not attach pasted text")), null, rejected); |
| 155 | } catch (error) { |
| 156 | Dialog.alert( |
| 157 | m(msg("Could not attach pasted text")), |
| 158 | errorDisplayMessage(error, m) ?? m(msg("Try again.")), |
| 159 | ); |
| 160 | } |
| 161 | }); |
| 162 | }; |
| 163 | return { onPickDraftMedia, onPickDraftFiles, onNativePasteImages, onNativePasteText }; |
| 164 | } |