Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions apps/mobile/src/features/threads/ThreadComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
composerContextImportsAtom,
countComposerDraftAttachmentsAfterSelection,
useComposerDraft,
useComposerDraftResetGeneration,
} from "../../state/use-composer-drafts";
import type { ComposerDocumentAttachment } from "../../lib/composerContext";
import { useProject, useVisibleThreadShells } from "../../state/entities";
Expand Down Expand Up @@ -101,6 +102,7 @@ import {
resolveComposerSendPresentation,
type ComposerSendPresentation,
} from "./composerSendPresentation";
import { useSubmitAfterNativeImages } from "./useSubmitAfterNativeImages";
import { ComposerCommandPopover } from "./ComposerCommandPopover";
import { ComposerPopoverAnchor } from "./ComposerPopoverAnchor";
import { ComposerQueuedEditAttachments } from "./ComposerQueuedEdit";
Expand Down Expand Up @@ -173,7 +175,7 @@ export interface ThreadComposerProps {
readonly onChangeDraftMessage: (value: string) => void;
readonly onPickDraftMedia: () => Promise<void>;
readonly onPickDraftFiles: () => Promise<void>;
readonly onNativePasteImages: (uris: ReadonlyArray<string>) => Promise<void>;
readonly onNativePasteImages: (uris: ReadonlyArray<string>) => Promise<boolean>;
readonly onNativePasteText: (paste: ComposerTextPaste) => Promise<void>;
readonly onRemoveDraftImage: (imageId: string) => void;
readonly onStopThread: () => void;
Expand Down Expand Up @@ -293,6 +295,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
const [previewVideo, setPreviewVideo] = useState<VideoPreviewSource | null>(null);
const composerOwnerKey = scopedThreadKey(props.environmentId, props.selectedThread.id);
const composerDraftKey = props.draftKey ?? composerOwnerKey;
const readResetGeneration = useComposerDraftResetGeneration(composerDraftKey);
const {
text: draftMessage,
attachments: draftAttachments,
Expand Down Expand Up @@ -539,13 +542,10 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
onEditorFocusChange?.(false);
};
const handleSend = async (followUp?: ActiveTurnComposerAction) => {
if (
isSubmissionLocked ||
sendBlockedReason !== null ||
pendingPastedTextAttachmentCountRef.current > 0 ||
pendingNativeImagePasteCountRef.current > 0
)
return;
if (isSubmissionLocked || sendBlockedReason !== null) return;
// Sending now would leave a still-converting pasted image out of the turn, so wait for it.
if (nativeImageSubmit.deferSend(followUp)) return;
if (pendingPastedTextAttachmentCountRef.current > 0) return;
if (usageLimitsOffered && isUsageLimitsCommand(draftMessage) && draftAttachments.length === 0) {
if (openUsageLimits()) onChangeDraftMessage("");
return;
Expand Down Expand Up @@ -582,6 +582,14 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
});
};

const nativeImageSubmit = useSubmitAfterNativeImages({
draftKey: composerDraftKey,
readResetGeneration,
blocked: isSubmissionLocked || sendBlockedReason !== null,
onPasteImages: props.onNativePasteImages,
onSubmit: (followUp) => void handleSend(followUp),
});

const lockedProviderInstanceId = props.canSwitchProvider
? undefined
: currentModelSelection.instanceId;
Expand Down Expand Up @@ -867,7 +875,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
onPasteImages={(uris) => {
if (isSubmissionLocked) return;
pendingNativeImagePasteCountRef.current += 1;
void props.onNativePasteImages(uris).finally(() => {
void nativeImageSubmit.pasteImages(uris).finally(() => {
pendingNativeImagePasteCountRef.current -= 1;
});
}}
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/src/features/threads/ThreadDetailScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export interface ThreadDetailScreenProps {
readonly onChangeDraftMessage: (value: string) => void;
readonly onPickDraftMedia: () => Promise<void>;
readonly onPickDraftFiles: () => Promise<void>;
readonly onNativePasteImages: (uris: ReadonlyArray<string>) => Promise<void>;
readonly onNativePasteImages: (uris: ReadonlyArray<string>) => Promise<boolean>;
readonly onNativePasteText: (paste: ComposerTextPaste) => Promise<void>;
readonly onRemoveDraftImage: (imageId: string) => void;
readonly onStopThread: () => void;
Expand Down
114 changes: 114 additions & 0 deletions apps/mobile/src/features/threads/useSubmitAfterNativeImages.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { act, useLayoutEffect } from "react";
import { createRoot } from "react-dom/client";
import { expect, it, vi } from "vite-plus/test";
import { useSubmitAfterNativeImages } from "./useSubmitAfterNativeImages";

function setup() {
const root = createRoot(document.createElement("div"));
let queue: ReturnType<typeof useSubmitAfterNativeImages>;
let generation = 0;
let blocked = false;
const onSubmit = vi.fn();
const preparations: Array<(success: boolean) => void> = [];
const onPasteImages = () => new Promise<boolean>((resolve) => preparations.push(resolve));
function Probe() {
const current = useSubmitAfterNativeImages({
draftKey: "draft",
readResetGeneration: () => generation,
blocked,
onPasteImages,
onSubmit: (action) => {
if (!blocked) onSubmit(action);
},
});
useLayoutEffect(() => {
queue = current;
});
return null;
}
const render = () => act(() => root.render(<Probe />));
render();
return {
get queue() {
return queue!;
},
onSubmit,
setBlocked(value: boolean) {
blocked = value;
render();
},
reset() {
generation += 1;
render();
},
async finish(success: boolean) {
await act(async () => {
preparations.shift()!(success);
});
},
[Symbol.dispose]() {
act(() => root.unmount());
},
};
}

it("does not send when conversion finishes without adding an image", async () => {
using state = setup();
const paste = state.queue.pasteImages(["image"]);
expect(state.queue.deferSend()).toBe(true);
await state.finish(false);
await paste;
expect(state.onSubmit).not.toHaveBeenCalled();

const first = state.queue.pasteImages(["failed"]);
const second = state.queue.pasteImages(["successful"]);
state.queue.deferSend();
await state.finish(false);
await state.finish(true);
await Promise.all([first, second]);
expect(state.onSubmit).not.toHaveBeenCalled();

const retry = state.queue.pasteImages(["retry"]);
state.queue.deferSend();
await state.finish(true);
await retry;
expect(state.onSubmit).toHaveBeenCalledOnce();
});

it("does not send a replacement draft after the original draft was discarded", async () => {
using state = setup();
const paste = state.queue.pasteImages(["image"]);
state.queue.deferSend();
state.reset();
await state.finish(true);
await paste;
expect(state.onSubmit).not.toHaveBeenCalled();

const next = state.queue.pasteImages(["next"]);
state.queue.deferSend();
state.setBlocked(true);
await state.finish(true);
await next;
state.reset();
state.setBlocked(false);
expect(state.onSubmit).not.toHaveBeenCalled();
});

it("waits for other attachment preparation and sends once after all images join the draft", async () => {
using state = setup();
const first = state.queue.pasteImages(["first"]);
const second = state.queue.pasteImages(["second"]);
state.queue.deferSend("queue");
state.queue.deferSend("steer");
await state.finish(true);
expect(state.onSubmit).not.toHaveBeenCalled();
state.setBlocked(true);
await state.finish(true);
await Promise.all([first, second]);
expect(state.onSubmit).not.toHaveBeenCalled();
state.setBlocked(false);
expect(state.onSubmit).toHaveBeenCalledExactlyOnceWith("steer");
state.setBlocked(true);
state.setBlocked(false);
expect(state.onSubmit).toHaveBeenCalledOnce();
});
60 changes: 60 additions & 0 deletions apps/mobile/src/features/threads/useSubmitAfterNativeImages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { useEffect, useEffectEvent, useRef, useState } from "react";
import type { ActiveTurnComposerAction } from "@t3tools/client-runtime/state/composer-dispatch";

export function useSubmitAfterNativeImages(props: {
readonly draftKey: string;
readonly readResetGeneration: () => number;
readonly blocked: boolean;
readonly onPasteImages: (uris: ReadonlyArray<string>) => Promise<boolean>;
readonly onSubmit: (followUp?: ActiveTurnComposerAction) => void;
}) {
type HeldSend = {
readonly draftKey: string;
readonly generation: number;
readonly followUp?: ActiveTurnComposerAction;
};
const pendingCount = useRef(0);
const failed = useRef(false);
const heldSend = useRef<HeldSend | null>(null);
const [releasedSend, setReleasedSend] = useState<HeldSend | null>(null);
const consumedSend = useRef<HeldSend | null>(null);
const sendReleased = useEffectEvent((held: HeldSend) => {
if (held.draftKey === props.draftKey && held.generation === props.readResetGeneration()) {
props.onSubmit(held.followUp);
}
});
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- submits from the render where the native images joined the draft
useEffect(() => {
if (!releasedSend || props.blocked || consumedSend.current === releasedSend) return;
consumedSend.current = releasedSend;
sendReleased(releasedSend);
}, [releasedSend, props.blocked]);
return {
deferSend: (followUp?: ActiveTurnComposerAction) => {
if (pendingCount.current === 0) return false;
heldSend.current = {
draftKey: props.draftKey,
generation: props.readResetGeneration(),
followUp,
};
return true;
},
pasteImages: (uris: ReadonlyArray<string>) => {
if (pendingCount.current === 0) failed.current = false;
pendingCount.current += 1;
const finish = (success: boolean) => {
if (!success) failed.current = true;
pendingCount.current -= 1;
if (pendingCount.current === 0) {
const held = heldSend.current;
heldSend.current = null;
if (held && !failed.current) setReleasedSend(held);
}
};
return props.onPasteImages(uris).then(finish, (error: unknown) => {
finish(false);
throw error;
});
},
};
}
20 changes: 11 additions & 9 deletions apps/mobile/src/state/use-composer-attachment-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export function useComposerAttachmentActions(props: {
return appendComposerDraftAttachments(key, attachments, { appendReference: true, insertion });
};
// A picker may finish after its draft was discarded or the composer moved off it.
const withDraftOperation = async (key: string, work: (signal: AbortSignal) => Promise<void>) => {
const withDraftOperation = async <T>(key: string, work: (signal: AbortSignal) => Promise<T>) => {
const operation = startComposerDraftOperation(key);
await work(operation.signal).finally(operation.finish);
return work(operation.signal).finally(operation.finish);
};
const report = (title: string, error: string | null | undefined, rejected: number) => {
const problems = [
Expand Down Expand Up @@ -109,21 +109,23 @@ export function useComposerAttachmentActions(props: {
};
const onNativePasteImages = async (uris: ReadonlyArray<string>) => {
const key = props.draftKey;
if (!key || props.locked || !uris.length) return;
if (!key || props.locked || !uris.length) return false;
const insertion = captureComposerDraftInsertion(key);
await withDraftOperation(key, async (signal) => {
return withDraftOperation(key, async (signal) => {
try {
const result = await convertPastedImagesToAttachments({ uris });
report(
m(msg("Could not paste images")),
result.error,
await append(key, signal, result.attachments, insertion),
);
const rejected = await append(key, signal, result.attachments, insertion);
report(m(msg("Could not paste images")), result.error, rejected);
if (signal.aborted) return false;
if (result.error) return false;
if (rejected > 0) return false;
return result.attachments.length > 0;
} catch (error) {
Dialog.alert(
m(msg("Could not paste images")),
errorDisplayMessage(error, m) ?? m(msg("Try again.")),
);
return false;
}
});
};
Expand Down
1 change: 1 addition & 0 deletions apps/mobile/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { defineConfig, normalizePath } from "vite-plus";
import "vite-plus/test/config";

const reactTests = [
"src/features/threads/useSubmitAfterNativeImages.test.tsx",
"src/features/threads/git/GitBranchesSheet.test.tsx",
"src/features/threads/new-task-flow-provider.test.tsx",
"src/features/layout/workspace/workspace-tabs-state.test.ts",
Expand Down
19 changes: 10 additions & 9 deletions apps/web/src/components/chat/ChatComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ import { useComposerAttachmentIntake } from "./useComposerAttachmentIntake";
import {
countPreparingComposerImages,
usePreparingComposerImages,
useSubmitAfterPreparingImages,
} from "./preparingComposerImages";
import { ComposerControlsBar } from "./ComposerControlsBar";
import { composerCommandKeyHandler } from "./composerCommandKey";
Expand Down Expand Up @@ -1918,6 +1919,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
attachmentBlockReason,
environmentUnavailable: environmentUnavailable !== null,
});
const submitAfterPreparingImages = useSubmitAfterPreparingImages(
attachmentTargetKey,
submissionBlocked,
);
// The pending Submit button follows the answer's own blockers, matching submitComposer.
const primaryActionSendDisabledReason =
(activePendingProgress ? attachmentBlockReason : sendDisabledReason) ??
Expand Down Expand Up @@ -2196,16 +2201,12 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
event?.preventDefault();
return false;
}
// A send while a pasted image is still compressing would strand that image:
// the turn snapshot wouldn't include it, and it would surface in the *next*
// draft instead.
if (activeThreadId && countPreparingComposerImages(attachmentTargetKey) > 0) {
// Sending now would leave a still-compressing image out of the turn, so wait for it.
if (
activeThreadId &&
submitAfterPreparingImages(() => submitComposerRef.current(dispatchMode, submissionIntent))
) {
event?.preventDefault();
toastManager.add({
type: "info",
title: gt("Still compressing a pasted image."),
description: gt("Send again once its thumbnail appears."),
});
return false;
}
if (pendingDraftWork.has(attachmentTargetKey)) {
Expand Down
Loading
Loading