diff --git a/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx b/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx index c6fa629d18b4..70770d681c89 100644 --- a/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx +++ b/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx @@ -42,7 +42,7 @@ import { } from "@t3tools/contracts"; import { ComposerEditor, type ComposerEditorHandle } from "../../components/ComposerEditor"; -import { composerContextImportsAtom } from "../../state/use-composer-drafts"; +import { composerContextImportsAtom, composerDraftBusy } from "../../state/use-composer-drafts"; import { abortComposerDraftOperations } from "../../state/composerDraftOperations"; import { composerContextSendBlockReason } from "../../lib/composerContext"; import { ComposerInlineControl } from "../../components/ComposerToolbar"; @@ -601,7 +601,7 @@ export function NewTaskDraftScreen(props: { return null; } const draft = getComposerDraftSnapshot(draftKey); - if (appAtomRegistry.get(composerContextImportsAtom)[draftKey]) return null; + if (composerDraftBusy(draftKey)) return null; const modelSelection = resolveSelectableModelSelection( selectedEnvironmentServerConfig, diff --git a/apps/mobile/src/features/threads/ThreadComposer.tsx b/apps/mobile/src/features/threads/ThreadComposer.tsx index b2ebef9a4252..e5015760a57f 100644 --- a/apps/mobile/src/features/threads/ThreadComposer.tsx +++ b/apps/mobile/src/features/threads/ThreadComposer.tsx @@ -46,6 +46,7 @@ import { getComposerDraftSnapshot, setComposerDraftText, composerContextImportsAtom, + composerPendingMediaAtom, countComposerDraftAttachmentsAfterSelection, useComposerDraft, useComposerDraftResetGeneration, @@ -305,9 +306,11 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps context: draftContext, } = useComposerDraft(composerDraftKey); const queuedEdit = props.queuedEdit ?? null; + const pendingMedia = useAtomValue(composerPendingMediaAtom)[composerDraftKey]; const hasContent = draftMessage.trim().length > 0 || draftAttachments.length > 0 || + pendingMedia !== undefined || (queuedEdit?.existingAttachments.length ?? 0) > 0; const showStopAction = !hasContent && props.canStopThread && queuedEdit === null; const onResumeQueue = props.onResumeQueue; @@ -326,10 +329,16 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps AuthOrchestrationOperateScope, ); const uploadStates = useAtomValue(composerAttachmentUploadsAtom); - const stripAttachments = composerStripAttachments( - draftAttachments, - (attachment) => uploadStates[composerAttachmentUploadKey(props.environmentId, attachment.id)], - ); + // A finished attachment keeps its placeholder's id; skip the placeholder once it lands. + const stripAttachments = [ + ...composerStripAttachments( + draftAttachments, + (attachment) => uploadStates[composerAttachmentUploadKey(props.environmentId, attachment.id)], + ), + ...(pendingMedia ?? []).filter( + (placeholder) => !draftAttachments.some(({ id }) => id === placeholder.id), + ), + ]; const attachmentsUploading = props.connectionState === "connected" && composerAttachmentsStillUploading({ @@ -501,6 +510,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps (props.canOperateThread || props.connectionState !== "connected") && hasContent && !contextImports[composerDraftKey] && + pendingMedia === undefined && !isSubmissionLocked && sendBlockedReason === null && !modelUnavailable; @@ -516,7 +526,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps (current) => current ?? { ...source, - gallery: filePreviewGallery(source, composerImageGallery(draftAttachments)), + gallery: filePreviewGallery(source, composerImageGallery(stripAttachments)), }, ); }; diff --git a/apps/mobile/src/lib/composerFiles.test.ts b/apps/mobile/src/lib/composerFiles.test.ts index 9a584b7a333a..5004ec5914a7 100644 --- a/apps/mobile/src/lib/composerFiles.test.ts +++ b/apps/mobile/src/lib/composerFiles.test.ts @@ -385,8 +385,17 @@ describe("composer file attachments", () => { uri.endsWith("clip.mov") ? video.fileSize : 3, ); - const result = await pickComposerMedia({ existingCount: 0, maxVideoBytes: 50 * 1024 * 1024 }); + const onPicked = vi.fn(); + const result = await pickComposerMedia({ + existingCount: 0, + maxVideoBytes: 50 * 1024 * 1024, + onPicked, + }); + expect(onPicked).toHaveBeenCalledWith([ + expect.objectContaining({ id: "attachment-id", type: "image", previewUri: image.uri }), + expect.objectContaining({ id: "attachment-id", type: "file", fileUri: video.uri }), + ]); expect(mocks.pickMedia).toHaveBeenCalledWith( expect.objectContaining({ mediaTypes: ["images", "videos"], diff --git a/apps/mobile/src/lib/composerImages.ts b/apps/mobile/src/lib/composerImages.ts index fe671928ffdf..2d823a7c38b5 100644 --- a/apps/mobile/src/lib/composerImages.ts +++ b/apps/mobile/src/lib/composerImages.ts @@ -433,6 +433,8 @@ export async function pickComposerImages(input: { readonly existingCount: number export async function pickComposerMedia(input: { readonly existingCount: number; readonly maxVideoBytes?: number; + /** Receives placeholders over the picker's local copies before processing; each keeps its attachment's id. */ + readonly onPicked?: (placeholders: ReadonlyArray) => void; readonly acceptSelection?: () => boolean; }): Promise<{ readonly attachments: ReadonlyArray; @@ -488,11 +490,28 @@ export async function pickComposerMedia(input: { error: null, }; } + const ids = result.assets.map(() => uuidv4()); + input.onPicked?.( + result.assets.flatMap((asset, index): DraftComposerAttachment[] => { + const common = { + id: ids[index]!, + name: asset.fileName ?? "", + mimeType: asset.mimeType ?? "", + sizeBytes: asset.fileSize ?? 0, + }; + if (asset.type === "image" || asset.mimeType?.startsWith("image/")) { + return [{ ...common, type: "image", previewUri: asset.uri }]; + } + return input.maxVideoBytes === undefined + ? [] + : [{ ...common, type: "file", fileUri: asset.uri }]; + }), + ); const attachments: DraftComposerAttachment[] = []; let error: string | null = null; - for (const asset of result.assets) { + for (const [index, asset] of result.assets.entries()) { if (attachments.length >= remainingSlots) { error = m( msg("You can attach up to {max} attachments per message.", { @@ -510,15 +529,16 @@ export async function pickComposerMedia(input: { try { const { File } = await import("expo-file-system"); const file = new File(asset.uri); - attachments.push( - await createComposerFileAttachment({ + attachments.push({ + ...(await createComposerFileAttachment({ uri: asset.uri, name: asset.fileName?.trim() || file.name || "video", mimeType: mimeType || file.type || "application/octet-stream", sizeBytes: asset.fileSize ?? null, maxBytes: clampFileAttachmentUploadBytes(input.maxVideoBytes), - }), - ); + })), + id: ids[index]!, + }); } catch (cause) { error = errorDisplayMessage(cause, m) ?? @@ -592,9 +612,14 @@ export async function pickComposerMedia(input: { image.name, PROVIDER_SEND_TURN_MAX_IMAGE_BYTES, ); - attachments.push( - await ownedComposerImageAttachment({ fileUri, name: image.name, mimeType: image.mimeType }), - ); + attachments.push({ + ...(await ownedComposerImageAttachment({ + fileUri, + name: image.name, + mimeType: image.mimeType, + })), + id: ids[index]!, + }); } catch (cause) { error = errorDisplayMessage(cause, m) ?? m(msg("Failed to read ''{name}''.", { name })); } diff --git a/apps/mobile/src/state/thread-task-permissions.test.ts b/apps/mobile/src/state/thread-task-permissions.test.ts index 0be50e8c2280..944a00e31b68 100644 --- a/apps/mobile/src/state/thread-task-permissions.test.ts +++ b/apps/mobile/src/state/thread-task-permissions.test.ts @@ -206,6 +206,7 @@ vi.mock("./use-composer-drafts", async () => { return { composerDraftsAtom: Atom.make({}), composerContextImportsAtom: Atom.make({}), + composerDraftBusy: () => false, getComposerDraftSnapshot: () => state.draft, isFreshSessionPending: () => state.freshPending, setFreshSessionPending: state.setFreshPending, diff --git a/apps/mobile/src/state/use-composer-attachment-actions.ts b/apps/mobile/src/state/use-composer-attachment-actions.ts index 3e9c3439d416..ec13b98a53e4 100644 --- a/apps/mobile/src/state/use-composer-attachment-actions.ts +++ b/apps/mobile/src/state/use-composer-attachment-actions.ts @@ -20,6 +20,9 @@ import { captureComposerDraftInsertion, countComposerDraftAttachmentsAfterSelection, getComposerDraftSnapshot, + addComposerPendingMedia, + keepPendingComposerMedia, + removeComposerPendingMedia, type ComposerDraftInsertion, } from "./use-composer-drafts"; import { startComposerDraftOperation } from "./composerDraftOperations"; @@ -72,15 +75,24 @@ export function useComposerAttachmentActions(props: { if (!key || props.locked) return; const insertion = captureComposerDraftInsertion(key); await withDraftOperation(key, async (signal) => { - const result = await pickComposerMedia({ + let placeholderIds: ReadonlyArray = []; + await pickComposerMedia({ existingCount: countComposerDraftAttachmentsAfterSelection(key, insertion), maxVideoBytes: capabilities?.fileAttachments?.maxUploadBytes, - }); - report( - m(msg("Could not attach photo or video")), - result.error, - await append(key, signal, result.attachments, insertion), - ); + onPicked: (placeholders) => { + placeholderIds = placeholders.map(({ id }) => id); + addComposerPendingMedia(key, placeholders); + }, + }) + .then(async (result) => { + // append writes the draft before its first await, so no removal lands in between. + report( + m(msg("Could not attach photo or video")), + result.error, + await append(key, signal, keepPendingComposerMedia(key, result.attachments), insertion), + ); + }) + .finally(() => removeComposerPendingMedia(key, placeholderIds)); }); }; const onPickDraftFiles = async () => { diff --git a/apps/mobile/src/state/use-composer-drafts.test.ts b/apps/mobile/src/state/use-composer-drafts.test.ts index a951c13839ee..2c99d3727c37 100644 --- a/apps/mobile/src/state/use-composer-drafts.test.ts +++ b/apps/mobile/src/state/use-composer-drafts.test.ts @@ -214,6 +214,9 @@ import { findNewTaskDraftKeys, flushComposerDrafts, getComposerDraftSnapshot, + addComposerPendingMedia, + keepPendingComposerMedia, + removeComposerDraftAttachment, mergeComposerDraftContentState, migrateLegacyNewTaskDraft, modelOptionMemoryAtom, @@ -1170,6 +1173,36 @@ describe("mobile composer drafts", () => { expect(appAtomRegistry.get(composerDraftsAtom)[draftKey]?.attachments).toHaveLength(101); }); + it("releases picked media whose placeholder was removed while it processed", async () => { + const outboxLoad = vi.spyOn(threadOutboxManager, "load").mockResolvedValue(true); + onTestFinished(() => outboxLoad.mockRestore()); + const cleanup = Promise.withResolvers(); + composerAttachmentCleanupMocks.remove.mockImplementationOnce(async () => { + cleanup.resolve(); + }); + const video = (id: string) => ({ + id, + type: "file" as const, + name: `${id}.mov`, + mimeType: "video/quicktime", + sizeBytes: 42, + fileUri: `file:///documents/t3-composer-attachments/${id}.mov`, + }); + const draftKey = "thread-pending-media"; + appAtomRegistry.set(composerDraftsAtom, { [draftKey]: { text: "", attachments: [] } }); + addComposerPendingMedia(draftKey, [video("kept"), video("removed")]); + + removeComposerDraftAttachment(draftKey, "removed"); + + expect(keepPendingComposerMedia(draftKey, [video("kept"), video("removed")])).toEqual([ + video("kept"), + ]); + await cleanup.promise; + expect(composerAttachmentCleanupMocks.remove).toHaveBeenCalledExactlyOnceWith( + video("removed").fileUri, + ); + }); + it("drops duplicate attachments, releases their files, and keeps them on restore", async () => { const outboxLoad = vi.spyOn(threadOutboxManager, "load").mockResolvedValue(true); onTestFinished(() => outboxLoad.mockRestore()); diff --git a/apps/mobile/src/state/use-composer-drafts.ts b/apps/mobile/src/state/use-composer-drafts.ts index 8bf91eaf923d..841c986666aa 100644 --- a/apps/mobile/src/state/use-composer-drafts.ts +++ b/apps/mobile/src/state/use-composer-drafts.ts @@ -91,6 +91,59 @@ export function setComposerContextImporting(draftKey: string, importing: boolean appAtomRegistry.set(composerContextImportsAtom, next); } +/** Placeholders for picked media still being processed, shown in the strip until their attachments land. */ +export const composerPendingMediaAtom = Atom.make< + Record> +>({}).pipe(Atom.keepAlive); + +function updateComposerPendingMedia( + draftKey: string, + update: (current: ReadonlyArray) => DraftComposerAttachment[], +): void { + const all = appAtomRegistry.get(composerPendingMediaAtom); + const current = all[draftKey] ?? []; + const placeholders = update(current); + if (placeholders.length === current.length) return; + const next = { ...all }; + if (placeholders.length > 0) next[draftKey] = placeholders; + else delete next[draftKey]; + appAtomRegistry.set(composerPendingMediaAtom, next); +} + +export function addComposerPendingMedia( + draftKey: string, + placeholders: ReadonlyArray, +): void { + updateComposerPendingMedia(draftKey, (current) => [...current, ...placeholders]); +} + +export function removeComposerPendingMedia(draftKey: string, ids: ReadonlyArray): void { + updateComposerPendingMedia(draftKey, (current) => + current.filter((placeholder) => !ids.includes(placeholder.id)), + ); +} + +/** Picked attachments whose placeholder the user removed while processing are released, not added. */ +export function keepPendingComposerMedia( + draftKey: string, + attachments: ReadonlyArray, +): ReadonlyArray { + const pending = appAtomRegistry.get(composerPendingMediaAtom)[draftKey] ?? []; + const kept = attachments.filter(({ id }) => pending.some((placeholder) => placeholder.id === id)); + scheduleUnusedComposerAttachmentCleanup( + attachments.filter((attachment) => !kept.includes(attachment)), + ); + return kept; +} + +/** A draft can't be sent while context is importing or picked media is still processing. */ +export function composerDraftBusy(draftKey: string): boolean { + return ( + appAtomRegistry.get(composerContextImportsAtom)[draftKey] === true || + appAtomRegistry.get(composerPendingMediaAtom)[draftKey] !== undefined + ); +} + interface ComposerDraftSelection { readonly text: string; readonly start: number; @@ -1548,6 +1601,7 @@ export function replaceComposerDraftAttachments( } export function removeComposerDraftAttachment(draftKey: string, imageId: string): void { + removeComposerPendingMedia(draftKey, [imageId]); const previousAttachments = getComposerDraftSnapshot(draftKey).attachments; updateComposerDrafts((current) => { const existing = normalizeDraft(current[draftKey]); diff --git a/apps/mobile/src/state/use-thread-composer-state.ts b/apps/mobile/src/state/use-thread-composer-state.ts index a26af21009c0..fdd02fd8b021 100644 --- a/apps/mobile/src/state/use-thread-composer-state.ts +++ b/apps/mobile/src/state/use-thread-composer-state.ts @@ -92,7 +92,7 @@ import { insertComposerDraftContext, clearComposerDraft, clearComposerDraftContent, - composerContextImportsAtom, + composerDraftBusy, getComposerDraftSnapshot, isFreshSessionPending, mergeComposerDraftContent, @@ -508,7 +508,9 @@ export function useThreadComposerState(input: { const threadKey = scopedThreadKey(thread.environmentId, thread.id); const edit = getQueuedRunEdit(threadKey); if (edit === null) return; - const draft = getComposerDraftSnapshot(queuedEditDraftKey(threadKey, edit.runId)); + const editDraftKey = queuedEditDraftKey(threadKey, edit.runId); + if (composerDraftBusy(editDraftKey)) return; + const draft = getComposerDraftSnapshot(editDraftKey); const text = draft.text.trim(); if ( text.length === 0 && @@ -649,7 +651,7 @@ export function useThreadComposerState(input: { if (startingSideChatRef.current) return null; const threadKey = scopedThreadKey(selectedThreadShell.environmentId, selectedThreadShell.id); const draft = getComposerDraftSnapshot(threadKey); - if (appAtomRegistry.get(composerContextImportsAtom)[threadKey]) return null; + if (composerDraftBusy(threadKey)) return null; const thread = selectedThreadShell; const text = draft.text.trim(); const attachments = draft.attachments; @@ -851,7 +853,7 @@ export function useThreadComposerState(input: { if (!response) return null; // A context paste still landing, or any draft or settings change while approvals were // declined, means this snapshot is no longer what the user sent. - if (appAtomRegistry.get(composerContextImportsAtom)[threadKey]) return null; + if (composerDraftBusy(threadKey)) return null; const current = getComposerDraftSnapshot(threadKey); if ( current.text !== draft.text ||