Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

attachmentUpload.ts

427 lines15.4 KB

1import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets";
2import {
3 isAssetAttachmentNotFoundFailure,
4 runAttachmentUploadCycle,
5 verifyPersistedAttachmentUpload,
6} from "@t3tools/client-runtime/state/attachments";
7import { runAtomCommand, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
8import { msg } from "gt-react-native";
9import { m } from "../i18n/messages";
10import type {
11 ChatFileAttachment,
12 ChatImageAttachment,
13 EnvironmentId,
14 UploadChatImageAttachment,
15} from "@t3tools/contracts";
16import {
17 AuthOrchestrationOperateScope,
18 PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES,
19} from "@t3tools/contracts";
20import * as Effect from "effect/Effect";
21import * as Option from "effect/Option";
22​
23import { appAtomRegistry } from "../state/atom-registry";
24import { assetEnvironment } from "../state/assets";
25import { attachmentEnvironment } from "../state/attachments";
26import { environmentSession, readEnvironmentScope } from "../state/session";
27import { resolveOwnedComposerAttachmentFileUri } from "./composerAttachmentFiles";
28import { retainComposerAttachmentFileForPreview } from "./composerAttachmentPreviewRetention";
29import {
30 isComposerImageAttachment,
31 isFileBackedComposerAttachment,
32 type DraftComposerAttachment,
33 type DraftComposerImageAttachment,
34} from "./composerImages";
35import { imageMimeType } from "@t3tools/shared/image";
36import { uuidv4 } from "./uuid";
37import {
38 attachmentUploadError,
39 type AttachmentUploadError,
40} from "@t3tools/client-runtime/state/attachment-uploads";
41​
42export type UploadedMobileAttachment =
43 | UploadChatImageAttachment
44 | ChatImageAttachment
45 | ChatFileAttachment;
46​
47export function withUploadedMobileAttachmentReferences(input: {
48 readonly environmentId: EnvironmentId;
49 readonly attachments: ReadonlyArray<DraftComposerAttachment>;
50 readonly uploadedAttachments: ReadonlyArray<UploadedMobileAttachment>;
51}): ReadonlyArray<DraftComposerAttachment> {
52 return input.attachments.map((attachment, index) => {
53 const uploaded = input.uploadedAttachments[index];
54 // Files-picked images upload as images; compare wire types to retain uploaded ids and avoid re-uploading.
55 const uploadedAs = isComposerImageAttachment(attachment) ? "image" : attachment.type;
56 if (
57 !uploaded ||
58 !("id" in uploaded) ||
59 uploadedAs !== uploaded.type ||
60 (attachment.uploadedAttachmentId === uploaded.id &&
61 attachment.uploadEnvironmentId === input.environmentId)
62 ) {
63 return attachment;
64 }
65 return {
66 ...attachment,
67 uploadedAttachmentId: uploaded.id,
68 uploadEnvironmentId: input.environmentId,
69 };
70 });
72​
73export async function releasePendingAttachmentUploads(
74 environmentId: EnvironmentId,
75 attachmentIds: ReadonlyArray<string>,
76): Promise<void> {
77 const deleteOnce = async (attachmentId: string): Promise<boolean> => {
78 if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) {
79 throw new Error("This connection cannot delete pending attachments.");
80 }
81 const result = await runAtomCommand(
82 appAtomRegistry,
83 attachmentEnvironment.remove,
84 { environmentId, input: { attachmentId } },
85 { reportFailure: false, reportDefect: false },
86 );
87 return (
88 result._tag === "Success" ||
89 isAssetAttachmentNotFoundFailure(squashAtomCommandFailure(result))
90 );
91 };
92​
93 const failedAttachmentIds: string[] = [];
94 for (const attachmentId of attachmentIds) {
95 if (!(await deleteOnce(attachmentId)) && !(await deleteOnce(attachmentId))) {
96 failedAttachmentIds.push(attachmentId);
97 }
98 }
99 if (failedAttachmentIds.length > 0) {
100 throw new Error(
101 `Could not delete ${failedAttachmentIds.length} pending attachment upload(s): ${failedAttachmentIds.join(", ")}.`,
102 );
103 }
105​
106async function releaseCreatedUploadsQuietly(
107 environmentId: EnvironmentId,
108 attachmentIds: ReadonlyArray<string>,
109): Promise<void> {
110 try {
111 await releasePendingAttachmentUploads(environmentId, attachmentIds);
112 } catch (error) {
113 console.warn("[attachments] could not delete abandoned pending uploads", error);
114 }
116​
117export interface PreparedTurnAttachments {
118 readonly status: "ready";
119 readonly attachments: ReadonlyArray<UploadedMobileAttachment>;
120 readonly draftAttachments: ReadonlyArray<DraftComposerAttachment>;
121 readonly pendingAttachmentIds: ReadonlyArray<string>;
123​
124export type PrepareTurnAttachmentsResult =
125 | PreparedTurnAttachments
126 | { readonly status: "abandoned" };
127​
128/** Promote Files-picked images consistently in headers, payloads and references or ChatImageAttachment rejects them. */
129function composerAttachmentWireMimeType(attachment: DraftComposerAttachment): string {
130 if (!isComposerImageAttachment(attachment)) return attachment.mimeType;
131 return supportedImageWireMimeType(attachment);
133​
134function supportedImageWireMimeType(
135 attachment: DraftComposerAttachment,
136): (typeof PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES)[number] {
137 const inferred = imageMimeType(attachment);
138 const mimeType = PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES.find(
139 (type) => type === attachment.mimeType.toLowerCase() || type === inferred,
140 );
141 if (!mimeType) {
142 throw new Error(m(msg("Unsupported image type for ''{name}''.", { name: attachment.name })));
143 }
144 return mimeType;
146​
147function uploadedReference(
148 attachment: DraftComposerAttachment,
149 id: string,
150): ChatImageAttachment | ChatFileAttachment {
151 const fields = {
152 id,
153 name: attachment.name,
154 mimeType: composerAttachmentWireMimeType(attachment),
155 sizeBytes: attachment.sizeBytes,
156 };
157 return isComposerImageAttachment(attachment)
158 ? { type: "image", ...fields }
159 : {
160 type: "file",
161 ...fields,
162 ...(attachment.source ? { source: attachment.source } : {}),
163 };
165​
166function attachmentUploadInput(attachment: DraftComposerAttachment) {
167 const fields = { name: attachment.name, sizeBytes: attachment.sizeBytes };
168 return isComposerImageAttachment(attachment)
169 ? { ...fields, mimeType: supportedImageWireMimeType(attachment) }
170 : { type: "file" as const, ...fields, mimeType: attachment.mimeType };
172​
173async function toUploadChatImageAttachments(
174 attachments: ReadonlyArray<DraftComposerImageAttachment>,
175): Promise<ReadonlyArray<UploadChatImageAttachment>> {
176 return Promise.all(
177 attachments.map(async (attachment) => ({
178 type: attachment.type,
179 name: attachment.name,
180 mimeType: attachment.mimeType,
181 sizeBytes: attachment.sizeBytes,
182 dataUrl: await composerImageAttachmentDataUrl(attachment),
183 })),
184 );
186​
187async function composerImageAttachmentDataUrl(
188 attachment: DraftComposerImageAttachment,
189): Promise<string> {
190 if (attachment.dataUrl !== undefined) {
191 return attachment.dataUrl;
192 }
193 if (!isFileBackedComposerAttachment(attachment)) {
194 throw new Error(`'${attachment.name}' is no longer available. Attach the image again.`);
195 }
196 const release = retainComposerAttachmentFileForPreview(attachment);
197 try {
198 const { File, Paths } = await import("expo-file-system");
199 const uri =
200 resolveOwnedComposerAttachmentFileUri(attachment.fileUri, Paths.document.uri) ??
201 attachment.fileUri;
202 const base64 = await new File(uri).base64();
203 return `data:${attachment.mimeType};base64,${base64}`;
204 } finally {
205 release();
206 }
208​
209async function uploadFileBytes(
210 environmentId: EnvironmentId,
211 attachment: DraftComposerAttachment,
212 url: string,
213 signal: AbortSignal,
214 onProgress?: (progress: number) => void,
215): Promise<void> {
216 const { File, Paths, UploadType } = await import("expo-file-system");
217 if (signal.aborted) throw new Error("Upload cancelled.");
218 if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) {
219 throw new Error(m(msg("This connection cannot upload attachments.")));
220 }
221 const fileUri = attachment.fileUri;
222 const inlineDataUrl = attachment.type === "image" ? attachment.dataUrl : undefined;
223 if (fileUri === undefined && inlineDataUrl === undefined) {
224 throw new Error(`'${attachment.name}' is no longer available. Attach the image again.`);
225 }
226 const file =
227 fileUri === undefined
228 ? new File(Paths.cache, `t3-upload-${uuidv4()}`)
229 : new File(resolveOwnedComposerAttachmentFileUri(fileUri, Paths.document.uri) ?? fileUri);
230 try {
231 if (fileUri === undefined && inlineDataUrl !== undefined) {
232 file.create();
233 await file.write(inlineDataUrl.slice(inlineDataUrl.indexOf(",") + 1), {
234 encoding: "base64",
235 });
236 }
237 const result = await file.upload(url, {
238 httpMethod: "POST",
239 uploadType: UploadType.BINARY_CONTENT,
240 headers: { "Content-Type": composerAttachmentWireMimeType(attachment) },
241 signal,
242 ...(onProgress
243 ? {
244 // Native events carry Int64 counts, which expo-modules-jsi delivers as BigInt despite the `number` type.
245 onProgress: ({ bytesSent, totalBytes }) => {
246 const total = Number(totalBytes);
247 if (total > 0) onProgress(Number(bytesSent) / total);
248 },
249 }
250 : {}),
251 });
252 if (result.status < 200 || result.status >= 300) {
253 throw new Error(
254 m(
255 msg("Upload failed for ''{name}'' ({status}).", {
256 name: attachment.name,
257 status: String(result.status),
258 }),
259 ),
260 );
261 }
262 } finally {
263 if (fileUri === undefined && file.exists) file.delete();
264 }
266​
267function requireUploadAccess(environmentId: EnvironmentId): void {
268 if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) {
269 throw new Error(m(msg("This connection cannot upload attachments.")));
270 }
272​
273/**
274 * Uploads one attachment and returns its uploaded id, reusing an earlier upload the server
275 * still has. Interrupting it aborts the native transfer and deletes the pending upload it minted;
276 * a failed transfer deletes it too.
277 */
278export const uploadComposerAttachment = Effect.fn("uploadComposerAttachment")(function* (
279 environmentId: EnvironmentId,
280 attachment: DraftComposerAttachment,
281 onProgress?: (progress: number) => void,
282) {
283 yield* Effect.try({
284 try: () => requireUploadAccess(environmentId),
285 catch: attachmentUploadError,
286 });
287 if (
288 attachment.uploadEnvironmentId === environmentId &&
289 attachment.uploadedAttachmentId !== undefined
290 ) {
291 const verification = yield* verifyPersistedAttachmentUpload({
292 registry: appAtomRegistry,
293 createAssetUrl: assetEnvironment.createUrl,
294 environmentId,
295 attachmentId: attachment.uploadedAttachmentId,
296 });
297 if (verification.status === "failed") {
298 return yield* attachmentUploadError(verification.error);
299 }
300 if (verification.status === "verified") return attachment.uploadedAttachmentId;
301 }
302 yield* Effect.try({
303 try: () => requireUploadAccess(environmentId),
304 catch: attachmentUploadError,
305 });
306 if (
307 Option.isNone(
308 appAtomRegistry.get(environmentSession.preparedConnectionValueAtom(environmentId)),
309 )
310 ) {
311 return yield* attachmentUploadError(new Error(m(msg("The environment is not connected."))));
312 }
313 const result = yield* runAttachmentUploadCycle({
314 registry: appAtomRegistry,
315 createUploadUrl: attachmentEnvironment.createUploadUrl,
316 remove: attachmentEnvironment.remove,
317 environmentId,
318 upload: attachmentUploadInput(attachment),
319 // Resolve at transfer time; reconnects may change the base URL.
320 resolveUploadUrl: (relativeUrl) => {
321 if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return null;
322 const connection = appAtomRegistry.get(
323 environmentSession.preparedConnectionValueAtom(environmentId),
324 );
325 return Option.isNone(connection)
326 ? null
327 : resolveAssetUrl(connection.value.httpBaseUrl, relativeUrl);
328 },
329 transport: (url) =>
330 Effect.tryPromise({
331 try: (signal) => uploadFileBytes(environmentId, attachment, url, signal, onProgress),
332 catch: attachmentUploadError,
333 }),
334 });
335 if (result.status === "uploaded") return result.attachmentId;
336 if (result.attachmentId !== null) {
337 yield* Effect.promise(() =>
338 releaseCreatedUploadsQuietly(environmentId, [result.attachmentId!]),
339 );
340 }
341 return yield* attachmentUploadError(
342 result.step === "resolve-url" &&
343 !readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)
344 ? new Error(m(msg("This connection cannot upload attachments.")))
345 : result.error instanceof Error
346 ? result.error
347 : new Error(m(msg("Upload failed for ''{name}''.", { name: attachment.name }))),
348 );
349});
350​
351/**
352 * Turns a message's attachments into wire references. `uploadAttachment` is the shared upload
353 * registry, so a send joins an upload the composer already started; interrupting this stops
354 * waiting without cancelling that shared upload. persistUploadedReferences runs only for new
355 * uploaded ids; save them to their owner so crash retries reuse bytes.
356 */
357export const prepareTurnAttachments = Effect.fn("prepareTurnAttachments")(function* (input: {
358 readonly environmentId: EnvironmentId;
359 readonly attachments: ReadonlyArray<DraftComposerAttachment>;
360 readonly supportsImageUploads?: boolean;
361 readonly uploadAttachment: (
362 attachment: DraftComposerAttachment,
363 ) => Effect.Effect<string, AttachmentUploadError>;
364 readonly persistUploadedReferences?: (
365 draftAttachments: ReadonlyArray<DraftComposerAttachment>,
366 ) => Effect.Effect<"persisted" | "abandon">;
367}) {
368 const { environmentId } = input;
369 const inlineImages = (attachments: ReadonlyArray<DraftComposerAttachment>) =>
370 Effect.tryPromise({
371 try: () =>
372 toUploadChatImageAttachments(
373 attachments.filter(
374 (attachment): attachment is DraftComposerImageAttachment => attachment.type === "image",
375 ),
376 ),
377 catch: attachmentUploadError,
378 });
379 const files = input.attachments.filter((attachment) => attachment.type === "file");
380 if (input.attachments.length === 0 || (files.length === 0 && !input.supportsImageUploads)) {
381 const imageAttachments = yield* inlineImages(input.attachments);
382 return {
383 status: "ready",
384 attachments: imageAttachments,
385 draftAttachments: input.attachments,
386 pendingAttachmentIds: [],
387 } satisfies PreparedTurnAttachments as PrepareTurnAttachmentsResult;
388 }
389​
390 yield* Effect.try({
391 try: () => requireUploadAccess(environmentId),
392 catch: attachmentUploadError,
393 });
394 const uploadedAttachments: UploadedMobileAttachment[] = [];
395 const pendingAttachmentIds: string[] = [];
396 for (const attachment of input.attachments) {
397 if (attachment.type === "image" && !input.supportsImageUploads) {
398 uploadedAttachments.push(...(yield* inlineImages([attachment])));
399 continue;
400 }
401 const uploadedId = yield* input.uploadAttachment(attachment);
402 pendingAttachmentIds.push(uploadedId);
403 uploadedAttachments.push(uploadedReference(attachment, uploadedId));
404 }
405​
406 const draftAttachments = withUploadedMobileAttachmentReferences({
407 environmentId,
408 attachments: input.attachments,
409 uploadedAttachments,
410 });
411 const referencesChanged = draftAttachments.some(
412 (attachment, index) => attachment !== input.attachments[index],
413 );
414 if (
415 referencesChanged &&
416 input.persistUploadedReferences &&
417 (yield* input.persistUploadedReferences(draftAttachments)) === "abandon"
418 ) {
419 return { status: "abandoned" } as PrepareTurnAttachmentsResult;
420 }
421 return {
422 status: "ready",
423 attachments: uploadedAttachments,
424 draftAttachments,
425 pendingAttachmentIds,
426 } satisfies PreparedTurnAttachments as PrepareTurnAttachmentsResult;
427});