TypeScript
feat(mobile): queue a message while its attachment is still uploading (#10404)
Julius Marminge66a24d6c1a00
diff --git a/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx b/apps/mobile/src/features/threads/NewTaskDraftScreen.tsxindex e2afdbb498a..7067f05fd01 100644--- a/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx+++ b/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx@@ -42,6 +42,7 @@ import { ComposerAttachmentStrip } from "../../components/ComposerAttachmentStriimport { EnvironmentMachineSymbol } from "../../components/EnvironmentMachineSymbol";import {composerAttachmentUploadBlockReason,+ composerAttachmentsStillUploading,composerAttachmentUploadsAtom,} from "../../state/composer-attachment-uploads";import { FilePreviewModal, type FilePreviewSource } from "../../components/FilePreviewModal";@@ -192,6 +193,18 @@ export function NewTaskDraftScreen(props: {states: uploadStates,}): null;+ // A connected composer with uploads still in flight queues the task rather+ // than making the user wait: the outbox drain finishes the upload and sends.+ const attachmentsUploading =+ environmentConnected &&+ selectedProject !== null &&+ composerAttachmentsStillUploading({+ environmentId: selectedProject.environmentId,+ attachments: flow.attachments,+ serverConfig: selectedEnvironmentServerConfig,+ states: uploadStates,+ });+ const queuesInsteadOfStarting = !environmentConnected || attachmentsUploading;const promptInputRef = useRef<ComposerEditorHandle>(null);const loadedBranchesProjectKeyRef = useRef<string | null>(null);const [isComposerFocused, setIsComposerFocused] = useState(false);@@ -987,10 +1000,11 @@ export function NewTaskDraftScreen(props: {const editingPendingTask = flow.editingPendingTask;- if (!environmentConnected) {- // Offline: park the task in the outbox; the drain sends it when the- // environment reconnects. Editing an existing pending task re-queues it- // under its original identifiers.+ if (queuesInsteadOfStarting) {+ // Offline, or an attachment is still uploading: park the task in the+ // outbox and let the drain send it once the environment is reachable+ // and the bytes are on the server. Editing an existing pending task+ // re-queues it under its original identifiers.const metadata = editingPendingTask? {threadId: editingPendingTask.threadId,@@ -1255,7 +1269,7 @@ export function NewTaskDraftScreen(props: {const workspaceControls = (<View className="flex-row items-center gap-1 px-2">- {flow.submitting && environmentConnected && flow.workspaceMode === "worktree" ? (+ {flow.submitting && !queuesInsteadOfStarting && flow.workspaceMode === "worktree" ? (<ViewaccessibleaccessibilityLabel="Setting up worktree…"@@ -1444,12 +1458,14 @@ export function NewTaskDraftScreen(props: {attachmentBlockReason ??(flow.submitting? "Starting task"- : environmentConnected- ? "Start task"- : "Queue task")+ : attachmentsUploading+ ? "Queue task, sends when uploads finish"+ : environmentConnected+ ? "Start task"+ : "Queue task")}disabled={!canStart}- icon={environmentConnected ? "arrow.up" : "tray.and.arrow.up"}+ icon={queuesInsteadOfStarting ? "tray.and.arrow.up" : "arrow.up"}onPress={() => void handleStart()}variant="primary"/>diff --git a/apps/mobile/src/features/threads/ThreadComposer.tsx b/apps/mobile/src/features/threads/ThreadComposer.tsxindex d61d7b92d0f..d015b2aa95b 100644--- a/apps/mobile/src/features/threads/ThreadComposer.tsx+++ b/apps/mobile/src/features/threads/ThreadComposer.tsx@@ -30,6 +30,7 @@ import { ActivityIndicator, Alert, Platform, Pressable, View, type ViewStyle } fimport { FilePreviewModal, type FilePreviewSource } from "../../components/FilePreviewModal";import {composerAttachmentUploadBlockReason,+ composerAttachmentsStillUploading,composerAttachmentUploadsAtom,} from "../../state/composer-attachment-uploads";import Animated, {@@ -323,8 +324,21 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer(props.selectedThread.session?.status === "running" ||props.selectedThread.session?.status === "starting");+ const uploadStates = useAtomValue(composerAttachmentUploadsAtom);+ const attachmentsUploading =+ props.connectionState === "connected" &&+ composerAttachmentsStillUploading({+ environmentId: props.environmentId,+ attachments: props.draftAttachments,+ serverConfig: props.serverConfig,+ states: uploadStates,+ });+ // Every send goes through the outbox; the label says whether it leaves now+ // or waits (for the connection, an earlier queued message, or an upload).const sendLabel =- props.connectionState !== "connected" || props.queueCount > 0 ? "Queue" : "Send";+ props.connectionState !== "connected" || props.queueCount > 0 || attachmentsUploading+ ? "Queue"+ : "Send";const currentModelSelection = props.selectedThread.modelSelection;const currentRuntimeMode = props.selectedThread.runtimeMode;const modelUnavailable =@@ -403,7 +417,6 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposerconst isExpanded = isFocused || settingsSheetPresentation.isActive;const showsCompactDictation = isVoiceInputPresented && !isExpanded;const isToolbarVisible = isExpanded || isVoiceInputPresented;- const uploadStates = useAtomValue(composerAttachmentUploadsAtom);const attachmentBlockReason = composerAttachmentUploadBlockReason({environmentId: props.environmentId,attachments: props.draftAttachments,diff --git a/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts b/apps/mobile/src/lib/composerAttachmentUploadQueue.test.tsindex 2ebacc74089..ba70a28456b 100644--- a/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts+++ b/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts@@ -3,6 +3,7 @@ import { describe, expect, it, vi } from "vite-plus/test";import {composerAttachmentUploadBlockReason,+ composerAttachmentsStillUploading,composerAttachmentUploadKey,composerDraftEnvironmentId,createComposerAttachmentUploadQueue,@@ -248,7 +249,7 @@ describe("draft upload scope and offline submission", () => {);});- it("allows offline queuing while a connected composer waits for upload or retry", () => {+ it("only a failed upload blocks sending; an in-flight one queues instead", () => {const key = composerAttachmentUploadKey(environmentId, "file");const input = {environmentId,@@ -261,7 +262,16 @@ describe("draft upload scope and offline submission", () => {},states: {},};- expect(composerAttachmentUploadBlockReason(input)).toBe("Attachment still uploading");+ // Not started yet and mid-transfer both let the send through as a queued+ // message; the outbox drain finishes (or redoes) the upload.+ expect(composerAttachmentUploadBlockReason(input)).toBeNull();+ expect(composerAttachmentsStillUploading(input)).toBe(true);+ expect(+ composerAttachmentsStillUploading({+ ...input,+ states: { [key]: { status: "uploading", progress: 0.6 } },+ }),+ ).toBe(true);expect(composerAttachmentUploadBlockReason({ ...input, connected: false })).toBeNull();expect(composerAttachmentUploadBlockReason({@@ -269,8 +279,19 @@ describe("draft upload scope and offline submission", () => {states: { [key]: { status: "failed", reason: "Offline" } },}),).toBe("Retry or remove the failed attachment");+ expect(+ composerAttachmentsStillUploading({+ ...input,+ states: { [key]: { status: "failed", reason: "Offline" } },+ }),+ ).toBe(false);expect(composerAttachmentUploadBlockReason({ ...input, states: { [key]: { status: "ready" } } }),).toBeNull();+ expect(+ composerAttachmentsStillUploading({ ...input, states: { [key]: { status: "ready" } } }),+ ).toBe(false);+ // Attachments the environment cannot accept never count as uploading.+ expect(composerAttachmentsStillUploading({ ...input, serverConfig: null })).toBe(false);});});diff --git a/apps/mobile/src/lib/composerAttachmentUploadQueue.ts b/apps/mobile/src/lib/composerAttachmentUploadQueue.tsindex 538b343abeb..3ee30ce8b16 100644--- a/apps/mobile/src/lib/composerAttachmentUploadQueue.ts+++ b/apps/mobile/src/lib/composerAttachmentUploadQueue.ts@@ -75,6 +75,12 @@ export function canUploadComposerAttachment();}+/**+ * Only a failed upload blocks sending: the outbox drain would hit the same+ * failure, so the user has to retry or remove the file first. An upload still+ * in flight does not block; the message queues and the drain reuses the+ * finished upload (or re-sends the local bytes) when it delivers.+ */export function composerAttachmentUploadBlockReason(input: {readonly environmentId: EnvironmentId;readonly attachments: ReadonlyArray<DraftComposerAttachment>;@@ -87,11 +93,24 @@ export function composerAttachmentUploadBlockReason(input: {if (!canUploadComposerAttachment(attachment, input.serverConfig)) continue;const state = input.states[composerAttachmentUploadKey(input.environmentId, attachment.id)];if (state?.status === "failed") return "Retry or remove the failed attachment";- if (state?.status !== "ready") return "Attachment still uploading";}return null;}+/** Whether any attachment the environment accepts is still being uploaded. */+export function composerAttachmentsStillUploading(input: {+ readonly environmentId: EnvironmentId;+ readonly attachments: ReadonlyArray<DraftComposerAttachment>;+ readonly serverConfig: UploadServerConfig | null;+ readonly states: Readonly<Record<string, ComposerAttachmentUploadState>>;+}): boolean {+ return input.attachments.some((attachment) => {+ if (!canUploadComposerAttachment(attachment, input.serverConfig)) return false;+ const state = input.states[composerAttachmentUploadKey(input.environmentId, attachment.id)];+ return state?.status !== "ready" && state?.status !== "failed";+ });+}+/** Bounds transfers across environments; disconnected or discarded drafts keep their local bytes. */export function createComposerAttachmentUploadQueue(options: {readonly upload: (diff --git a/apps/mobile/src/state/composer-attachment-uploads.ts b/apps/mobile/src/state/composer-attachment-uploads.tsindex d454133f1a3..352367c09a1 100644--- a/apps/mobile/src/state/composer-attachment-uploads.ts+++ b/apps/mobile/src/state/composer-attachment-uploads.ts@@ -25,7 +25,10 @@ import {} from "./use-composer-drafts";import { useRemoteConnectionStatus } from "./use-remote-environment-registry";-export { composerAttachmentUploadBlockReason } from "../lib/composerAttachmentUploadQueue";+export {+ composerAttachmentUploadBlockReason,+ composerAttachmentsStillUploading,+} from "../lib/composerAttachmentUploadQueue";export const composerAttachmentUploadsAtom = Atom.make<Readonly<Record<string, ComposerAttachmentUploadState>>