The best way to control Claude Code, Codex and more from any computer or phone
fix(composer): keep screenshots preparing after an unanswered question closes (#39569)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 20, 2026 at 09:11 UTC
019e55bad5f9d5bea0ad69fe4b6a44f2c2ba52a9
14 files changed
+765
-126
apps/mobile/src/features/threads/QuestionAttachments.tsx
+26
-43
@@ -12,7 +12,6 @@ import { View, type TextInputInstance } from "react-native";
12
import { useEffect, useEffectEvent, useRef, useState } from "react";
13
import { ComposerAttachmentButton } from "./ComposerAttachmentButton";
14
import { ComposerAttachmentStrip } from "../../components/ComposerAttachmentStrip";
15
-import { pickComposerFiles, pickComposerMedia } from "../../lib/composerImages";
15
import { useThreadSelection } from "../../state/use-thread-selection";
16
import { useNavigation } from "@react-navigation/native";
17
import { useGT } from "gt-react-native";
@@ -25,13 +24,12 @@ import {
24
appendComposerDraftAttachments,
25
composerDraftsAtom,
26
removeComposerDraftAttachment,
28
- releaseUnusedComposerAttachmentFiles,
27
} from "../../state/use-composer-drafts";
28
+import { questionAttachmentDraftKey } from "../../state/question-attachments";
29
import {
31
- changeQuestionAttachmentPreparation,
32
- questionAttachmentDraftKey,
33
- questionAttachmentPreparationAtom,
34
-} from "../../state/question-attachments";
30
+ pickQuestionAttachments,
31
+ startQuestionImagePreparation,
32
+} from "../../state/question-attachment-preparation";
33
34
export function QuestionAttachments(props: {
35
requestId: string;
@@ -108,19 +106,20 @@ export function QuestionAttachments(props: {
106
props.requestId,
107
props.question.id,
108
);
111
- changeQuestionAttachmentPreparation(key, 1);
109
+ const preparation = startQuestionImagePreparation(key);
110
void convertPastedImagesToAttachments({ uris })
113
- .then(async (result) => {
114
- if (
115
- scope.active &&
116
- (appAtomRegistry.get(questionAttachmentPreparationAtom)[key] ?? 0) > 0
117
- ) {
118
- const problems = [
119
- ...(result.error ? [result.error] : []),
120
- ...(append(key, result.attachments) > 0 ? [gt("Too many attachments.")] : []),
121
- ];
122
- if (problems.length) Dialog.alert(gt("Could not paste image"), problems.join("\n\n"));
123
- } else await releaseUnusedComposerAttachmentFiles(result.attachments);
111
+ .then((result) =>
112
+ preparation.complete(result, {
113
+ isActive: () => scope.active,
114
+ append: (attachments) => append(key, attachments),
115
+ }),
116
+ )
117
+ .then(({ error, rejected }) => {
118
+ const problems = [
119
+ ...(error ? [error] : []),
120
+ ...(rejected > 0 ? [gt("Too many attachments.")] : []),
121
+ ];
122
+ if (problems.length) Dialog.alert(gt("Could not paste image"), problems.join("\n\n"));
123
})
124
.catch((error) =>
125
Dialog.alert(
@@ -128,7 +127,7 @@ export function QuestionAttachments(props: {
127
errorDisplayMessage(error, m) ?? gt("Try again."),
128
),
129
)
131
- .finally(() => changeQuestionAttachmentPreparation(key, -1));
130
+ .finally(preparation.finish);
131
});
132
if (!selectedThread || props.question.allowCustomAnswer === false) return null;
133
const { environmentId, id: threadId } = selectedThread;
@@ -143,33 +142,17 @@ export function QuestionAttachments(props: {
142
const attachments = drafts[key]?.attachments ?? [];
143
const pick = async (kind: "media" | "files") => {
144
const scope = pickerScope.current;
146
- changeQuestionAttachmentPreparation(key, 1);
145
const picked = await settlePromise(async () => {
148
- const existingCount = appAtomRegistry.get(composerDraftsAtom)[key]?.attachments.length ?? 0;
149
- const result =
150
- kind === "files"
151
- ? await pickComposerFiles({
152
- existingCount,
153
- maxBytes: capabilities?.fileAttachments?.maxUploadBytes,
154
- })
155
- : await pickComposerMedia({
156
- existingCount,
157
- maxVideoBytes: capabilities?.fileAttachments?.maxUploadBytes,
158
- });
159
- const picked = "files" in result ? result.files : result.attachments;
160
- // Resolution on another client clears the reservation while the picker is open.
161
- if (
162
- !scope.active ||
163
- (appAtomRegistry.get(questionAttachmentPreparationAtom)[key] ?? 0) === 0
164
- ) {
165
- await releaseUnusedComposerAttachmentFiles(picked);
166
- return;
167
- }
168
- const rejected = append(key, picked);
169
- if (result.error || rejected > 0)
146
+ const result = await pickQuestionAttachments({
147
+ key,
148
+ kind,
149
+ maxUploadBytes: capabilities?.fileAttachments?.maxUploadBytes,
150
+ isActive: () => scope.active,
151
+ append: (attachments) => append(key, attachments),
152
+ });
153
+ if (result.error || result.rejected > 0)
154
Dialog.alert(gt("Could not attach file"), result.error ?? gt("Too many attachments."));
155
});
172
- changeQuestionAttachmentPreparation(key, -1);
156
if (AsyncResult.isFailure(picked)) {
157
const error = squashAtomCommandFailure(picked);
158
Dialog.alert(gt("Could not attach file"), errorDisplayMessage(error, m) ?? gt("Try again."));
apps/mobile/src/lib/composerImages.ts
+4
-2
@@ -271,6 +271,7 @@ export async function createComposerFileAttachment(input: {
271
export async function pickComposerFiles(input: {
272
readonly existingCount: number;
273
readonly maxBytes?: number;
274
+ readonly acceptSelection?: () => boolean;
275
}): Promise<{
276
readonly files: ReadonlyArray<DraftComposerAttachment>;
277
readonly error: string | null;
@@ -301,7 +302,7 @@ export async function pickComposerFiles(input: {
302
} finally {
303
endHandoff();
304
}
304
- if (result.canceled) {
305
+ if (result.canceled || input.acceptSelection?.() === false) {
306
return { files: [], error: null };
307
}
308
@@ -399,6 +400,7 @@ export async function pickComposerImages(input: { readonly existingCount: number
400
export async function pickComposerMedia(input: {
401
readonly existingCount: number;
402
readonly maxVideoBytes?: number;
403
+ readonly acceptSelection?: () => boolean;
404
}): Promise<{
405
readonly attachments: ReadonlyArray<DraftComposerAttachment>;
406
readonly error: string | null;
@@ -447,7 +449,7 @@ export async function pickComposerMedia(input: {
449
endHandoff();
450
}
451
450
- if (result.canceled) {
452
+ if (result.canceled || input.acceptSelection?.() === false) {
453
return {
454
attachments: [],
455
error: null,
apps/mobile/src/state/question-attachment-preparation.ts
new
+111
@@ -0,0 +1,111 @@
1
+import {
2
+ pickComposerFiles,
3
+ pickComposerMedia,
4
+ type DraftComposerAttachment,
5
+} from "../lib/composerImages";
6
+import { startComposerDraftOperation } from "./composerDraftOperations";
7
+import { changeQuestionAttachmentPreparation } from "./question-attachments";
8
+import {
9
+ appendComposerDraftAttachments,
10
+ getComposerDraftSnapshot,
11
+ releaseUnusedComposerAttachmentFiles,
12
+} from "./use-composer-drafts";
13
+
14
+const imagePreparations = new Map<string, Set<(threadKey: string | null) => void>>();
15
+
16
+/** A paste or accepted picker selection can finish in the thread draft. */
17
+export function startQuestionImagePreparation(key: string, waitingForSelection = false) {
18
+ let destination: string | null = key;
19
+ let threadOperation: ReturnType<typeof startComposerDraftOperation> | undefined;
20
+ const resolve = (threadKey: string | null) => {
21
+ destination = waitingForSelection ? null : threadKey;
22
+ if (destination !== null) threadOperation = startComposerDraftOperation(destination);
23
+ };
24
+ const pending = imagePreparations.get(key) ?? new Set();
25
+ pending.add(resolve);
26
+ imagePreparations.set(key, pending);
27
+ changeQuestionAttachmentPreparation(key, 1);
28
+ const currentDestination = () => (threadOperation?.signal.aborted ? null : destination);
29
+ return {
30
+ acceptSelection: () => {
31
+ if (destination !== key) return false;
32
+ waitingForSelection = false;
33
+ return true;
34
+ },
35
+ destination: currentDestination,
36
+ complete: async (
37
+ result: { attachments: ReadonlyArray<DraftComposerAttachment>; error: string | null },
38
+ scope: QuestionAttachmentScope,
39
+ ) => {
40
+ const target = currentDestination();
41
+ if (target === null || (target === key && !scope.isActive())) {
42
+ await releaseUnusedComposerAttachmentFiles(result.attachments);
43
+ return { error: null, rejected: 0 };
44
+ }
45
+ const rejected =
46
+ target === key
47
+ ? scope.append(result.attachments)
48
+ : appendUnsentQuestionAttachments(target, result.attachments);
49
+ return { error: result.error, rejected };
50
+ },
51
+ finish: () => {
52
+ threadOperation?.finish();
53
+ if (imagePreparations.get(key) !== pending || !pending.delete(resolve)) return;
54
+ if (pending.size === 0) imagePreparations.delete(key);
55
+ changeQuestionAttachmentPreparation(key, -1);
56
+ },
57
+ };
58
+}
59
+
60
+export function resolveQuestionImagePreparations(key: string, threadKey: string | null): void {
61
+ const pending = imagePreparations.get(key);
62
+ imagePreparations.delete(key);
63
+ for (const resolve of pending ?? []) resolve(threadKey);
64
+}
65
+
66
+export function appendUnsentQuestionAttachments(
67
+ threadKey: string,
68
+ attachments: Parameters<typeof appendComposerDraftAttachments>[1],
69
+): number {
70
+ const { text } = getComposerDraftSnapshot(threadKey);
71
+ // Chips go after the draft's text, never over a selection the user left in it.
72
+ return appendComposerDraftAttachments(threadKey, attachments, {
73
+ appendReference: true,
74
+ insertion: { text, start: text.length, end: text.length },
75
+ });
76
+}
77
+
78
+interface QuestionAttachmentScope {
79
+ readonly isActive: () => boolean;
80
+ readonly append: (attachments: ReadonlyArray<DraftComposerAttachment>) => number;
81
+}
82
+
83
+export async function pickQuestionAttachments(
84
+ input: QuestionAttachmentScope & {
85
+ readonly key: string;
86
+ readonly kind: "media" | "files";
87
+ readonly maxUploadBytes?: number | undefined;
88
+ },
89
+): Promise<{ error: string | null; rejected: number }> {
90
+ const preparation = startQuestionImagePreparation(input.key, true);
91
+ try {
92
+ const existingCount = getComposerDraftSnapshot(input.key).attachments.length;
93
+ const acceptSelection = () => input.isActive() && preparation.acceptSelection();
94
+ const result =
95
+ input.kind === "files"
96
+ ? await pickComposerFiles({
97
+ existingCount,
98
+ maxBytes: input.maxUploadBytes,
99
+ acceptSelection,
100
+ })
101
+ : await pickComposerMedia({
102
+ existingCount,
103
+ maxVideoBytes: input.maxUploadBytes,
104
+ acceptSelection,
105
+ });
106
+ const attachments = "files" in result ? result.files : result.attachments;
107
+ return await preparation.complete({ attachments, error: result.error }, input);
108
+ } finally {
109
+ preparation.finish();
110
+ }
111
+}
apps/mobile/src/state/thread-task-permissions.test.ts
+24
-2
@@ -247,6 +247,8 @@ import { Alert } from "react-native";
247
import { useThreadComposerState } from "./use-thread-composer-state";
248
import { useSelectedThreadRequests } from "./use-selected-thread-requests";
249
import { questionAttachmentDraftKey } from "./question-attachments";
250
+import { startQuestionImagePreparation } from "./question-attachment-preparation";
251
+import { abortComposerDraftOperations } from "./composerDraftOperations";
252
253
const NO_SIDE_CHATS = { startSideChat: async () => false };
254
@@ -644,6 +646,19 @@ it.each(["cancelled", "expired"] as const)(
646
(status) => {
647
const environmentId = EnvironmentId.make("secondary");
648
const threadId = ThreadId.make("thread");
649
+ const unanswered = RuntimeRequestId.make("input");
650
+ const answered = RuntimeRequestId.make("input-other");
651
+ const questionKey = questionAttachmentDraftKey(environmentId, threadId, unanswered, "language");
652
+ const preparing = startQuestionImagePreparation(questionKey);
653
+ const preparingAnswered = startQuestionImagePreparation(
654
+ questionAttachmentDraftKey(environmentId, threadId, answered, "language"),
655
+ );
656
+ using _preparations = {
657
+ [Symbol.dispose]: () => {
658
+ preparing.finish();
659
+ preparingAnswered.finish();
660
+ },
661
+ };
662
const notes = {
663
id: "notes",
664
type: "file",
@@ -652,7 +667,7 @@ it.each(["cancelled", "expired"] as const)(
667
sizeBytes: 5,
668
} as unknown as DraftComposerImageAttachment;
669
state.questionDrafts = {
655
- [questionAttachmentDraftKey(environmentId, threadId, "input", "language")]: {
670
+ [questionKey]: {
671
text: "",
672
attachments: [notes],
673
},
@@ -660,12 +675,19 @@ it.each(["cancelled", "expired"] as const)(
675
makeThreadRequestsResolvedAnnouncer(
676
environmentId,
677
threadId,
663
- )([{ id: RuntimeRequestId.make("input"), status }]);
678
+ )([
679
+ { id: unanswered, status },
680
+ { id: answered, status: "resolved" },
681
+ ]);
682
expect(state.appendAttachments).toHaveBeenCalledWith(
683
"secondary:thread",
684
[notes],
685
expect.objectContaining({ appendReference: true }),
686
);
687
+ expect(preparing.destination()).toBe("secondary:thread");
688
+ expect(preparingAnswered.destination()).toBeNull();
689
+ abortComposerDraftOperations("secondary:thread");
690
+ expect(preparing.destination()).toBeNull();
691
},
692
);
693
apps/mobile/src/state/use-composer-drafts.test.ts
+75
@@ -23,6 +23,7 @@ import {
23
MessageId,
24
ProjectId,
25
ProviderInstanceId,
26
+ RuntimeRequestId,
27
ThreadId,
28
type OrchestrationV2ThreadShell,
29
type OrchestrationV2ShellSnapshot,
@@ -88,6 +89,7 @@ const composerDraftFileMocks = vi.hoisted(() => {
89
},
90
File: class {
91
exists = true;
92
+ size = 3;
93
parentDirectory = null;
94
95
create() {}
@@ -130,6 +132,9 @@ const composerAttachmentCleanupMocks = vi.hoisted(() => ({
132
releaseUploads: vi.fn(async () => undefined),
133
}));
134
135
+const pickMedia = vi.hoisted(() => vi.fn());
136
+vi.mock("expo-image-picker", () => ({ launchImageLibraryAsync: pickMedia }));
137
+
138
const incomingShareStorageMocks = vi.hoisted(() => ({
139
load: vi.fn<typeof import("../features/sharing/incoming-share-storage").loadIncomingShareDrafts>(
140
async () => [],
@@ -177,6 +182,11 @@ import {
182
} from "@t3tools/shared/composerContextReferences";
183
import { appAtomRegistry } from "./atom-registry";
184
import { startComposerDraftOperation } from "./composerDraftOperations";
185
+import { questionAttachmentDraftKey } from "./question-attachments";
186
+import {
187
+ pickQuestionAttachments,
188
+ resolveQuestionImagePreparations,
189
+} from "./question-attachment-preparation";
190
import { threadOutboxManager } from "./thread-outbox";
191
import {
192
appendComposerDraftAttachments,
@@ -229,6 +239,71 @@ const DRAFT: ComposerDraft = {
239
attachments: [],
240
};
241
242
+it.each(["picker open", "reading image"] as const)(
243
+ "recovers only accepted screenshots when a question is cancelled while %s",
244
+ async (stage) => {
245
+ const questionKey = questionAttachmentDraftKey(
246
+ EnvironmentId.make("environment"),
247
+ ThreadId.make("thread"),
248
+ RuntimeRequestId.make("input"),
249
+ "language",
250
+ );
251
+ const threadKey = "environment:thread";
252
+ const screenshot = {
253
+ canceled: false,
254
+ assets: [
255
+ {
256
+ uri: "file:///picker/screenshot.png",
257
+ fileName: "screenshot.png",
258
+ type: "image",
259
+ mimeType: "image/png",
260
+ },
261
+ ],
262
+ } as const;
263
+ const opened = Promise.withResolvers<void>();
264
+ const selection = Promise.withResolvers<typeof screenshot>();
265
+ const reading = Promise.withResolvers<void>();
266
+ const bytes = Promise.withResolvers<string>();
267
+ composerDraftFileMocks.readImage.mockImplementationOnce(() => {
268
+ reading.resolve();
269
+ return bytes.promise;
270
+ });
271
+ pickMedia.mockImplementationOnce(() => {
272
+ opened.resolve();
273
+ return selection.promise;
274
+ });
275
+ const picking = pickQuestionAttachments({
276
+ key: questionKey,
277
+ kind: "media",
278
+ isActive: () => true,
279
+ append: (attachments) => appendComposerDraftAttachments(questionKey, attachments),
280
+ });
281
+ if (stage === "reading image") {
282
+ selection.resolve(screenshot);
283
+ await Promise.race([reading.promise, picking]);
284
+ } else {
285
+ await Promise.race([opened.promise, picking]);
286
+ }
287
+ resolveQuestionImagePreparations(questionKey, threadKey);
288
+ selection.resolve(screenshot);
289
+ bytes.resolve("YWJj");
290
+ await picking;
291
+
292
+ expect(getComposerDraftSnapshot(questionKey).attachments).toEqual([]);
293
+ expect(getComposerDraftSnapshot(threadKey).attachments).toEqual(
294
+ stage === "picker open"
295
+ ? []
296
+ : [
297
+ expect.objectContaining({
298
+ type: "image",
299
+ name: "screenshot.png",
300
+ dataUrl: "data:image/png;base64,YWJj",
301
+ }),
302
+ ],
303
+ );
304
+ },
305
+);
306
+
307
afterEach(() => {
308
vi.useRealTimers();
309
resetComposerDraftsLoadState();
apps/mobile/src/state/use-selected-thread-requests.ts
+13
-12
@@ -7,12 +7,14 @@ import {
7
questionAttachmentDraftPrefix,
8
questionAttachmentPreparationAtom,
9
} from "./question-attachments";
10
+import {
11
+ appendUnsentQuestionAttachments,
12
+ resolveQuestionImagePreparations,
13
+} from "./question-attachment-preparation";
14
import { scopedThreadKey } from "../lib/scopedEntities";
15
import {
12
- appendComposerDraftAttachments,
16
clearComposerDraft,
17
composerDraftsAtom,
15
- getComposerDraftSnapshot,
18
questionAttachmentDraftsAtom,
19
} from "./use-composer-drafts";
20
import {
@@ -111,6 +113,7 @@ function pruneResolvedRequestDrafts(
113
environmentId: EnvironmentId,
114
threadId: ThreadId,
115
pendingRequestIds: ReadonlyArray<RuntimeRequestId>,
116
+ unansweredRequestIds: ReadonlyArray<RuntimeRequestId> = [],
117
): void {
118
const drafts = appAtomRegistry.get(userInputDraftsByRequestKeyAtom);
119
const prunedDrafts = pruneResolvedUserInputDrafts(
@@ -125,11 +128,15 @@ function pruneResolvedRequestDrafts(
128
const attachmentDrafts = appAtomRegistry.get(questionAttachmentDraftsAtom(prefix));
129
const counts = { ...appAtomRegistry.get(questionAttachmentPreparationAtom) };
130
let changed = false;
128
- for (const key of resolvedQuestionDraftKeys(
131
+ const resolved = resolvedQuestionDraftKeys(
132
new Set([...Object.keys(attachmentDrafts), ...Object.keys(counts)]),
133
prefix,
134
pendingRequestIds,
132
- )) {
135
+ );
136
+ const discard = new Set(resolvedQuestionDraftKeys(resolved, prefix, unansweredRequestIds));
137
+ const threadKey = scopedThreadKey(environmentId, threadId);
138
+ for (const key of resolved) {
139
+ resolveQuestionImagePreparations(key, discard.has(key) ? null : threadKey);
140
if (attachmentDrafts[key]) clearComposerDraft(key);
141
if (key in counts) {
142
delete counts[key];
@@ -157,20 +164,14 @@ function returnUnsentAttachmentsToThreadDraft(
164
const attachments = Object.entries(drafts).flatMap(([key, draft]) =>
165
otherRequestKeys.has(key) ? [] : draft.attachments,
166
);
160
- const threadKey = scopedThreadKey(environmentId, threadId);
161
- const { text } = getComposerDraftSnapshot(threadKey);
162
- // Chips go after the draft's text, never over a selection the user left in it.
163
- appendComposerDraftAttachments(threadKey, attachments, {
164
- appendReference: true,
165
- insertion: { text, start: text.length, end: text.length },
166
- });
167
+ appendUnsentQuestionAttachments(scopedThreadKey(environmentId, threadId), attachments);
168
}
169
170
// Resolution can come from anywhere: another device, an interrupt, the turn
171
// ending, or a reload over drafts for requests that resolved meanwhile.
172
onThreadRequestsResolved(({ environmentId, threadId, pendingRequestIds, unansweredRequestIds }) => {
173
returnUnsentAttachmentsToThreadDraft(environmentId, threadId, unansweredRequestIds);
173
- pruneResolvedRequestDrafts(environmentId, threadId, pendingRequestIds);
174
+ pruneResolvedRequestDrafts(environmentId, threadId, pendingRequestIds, unansweredRequestIds);
175
});
176
177
/**
apps/web/src/components/chat/preparingComposerImages.ts
+26
@@ -1,3 +1,5 @@
1
+import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2
+import type { ScopedThreadRef } from "@t3tools/contracts";
3
import { useEffect, useRef } from "react";
4
import { create } from "zustand";
5
@@ -23,6 +25,30 @@ const usePreparingComposerImageStore = create<{
25
readonly byTargetKey: Readonly<Record<string, ReadonlyArray<PreparingComposerImage>>>;
26
}>(() => ({ byTargetKey: {} }));
27
28
+const movedImageThreads = new WeakMap<PreparingComposerImage, ScopedThreadRef>();
29
+
30
+export function movedPreparingImageThread(image: PreparingComposerImage) {
31
+ return movedImageThreads.get(image);
32
+}
33
+
34
+/** Carries the live tiles to the thread, where they can still be removed or awaited. */
35
+export function movePreparingComposerImagesToThread(
36
+ sourceKey: string,
37
+ thread: ScopedThreadRef,
38
+): void {
39
+ const images = getPreparingComposerImages(sourceKey);
40
+ if (images.length === 0) return;
41
+ const threadKey = scopedThreadKey(thread);
42
+ for (const image of images) movedImageThreads.set(image, thread);
43
+ invalidatePendingSubmit(sourceKey);
44
+ usePreparingComposerImageStore.setState(({ byTargetKey }) => {
45
+ const next = { ...byTargetKey };
46
+ delete next[sourceKey];
47
+ next[threadKey] = [...(next[threadKey] ?? NO_PREPARING_IMAGES), ...images];
48
+ return { byTargetKey: next };
49
+ });
50
+}
51
+
52
export function usePreparingComposerImages(targetKey: string) {
53
return usePreparingComposerImageStore(
54
(state) => state.byTargetKey[targetKey] ?? NO_PREPARING_IMAGES,
apps/web/src/components/chat/useComposerAttachmentIntake.browser.test.tsx
+183
-2
@@ -1,6 +1,13 @@
1
import { useEffect } from "react";
2
-import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
2
+import {
3
+ EnvironmentId,
4
+ ProjectId,
5
+ PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
6
+ RuntimeRequestId,
7
+ ThreadId,
8
+} from "@t3tools/contracts";
9
import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
10
+import { makeThreadRequestsResolvedAnnouncer } from "@t3tools/client-runtime/state/thread-requests";
11
import { expect, it, vi } from "vite-plus/test";
12
import { AsyncResult } from "effect/reactivity";
13
@@ -9,11 +16,17 @@ import {
16
type ComposerImageAttachment,
17
DraftId,
18
composerTargetKey,
19
+ hydrateImagesFromPersisted,
20
useComposerDraftStore,
21
} from "../../composerDraftStore";
22
import { showThreadUndoNotice } from "../../hooks/showThreadUndoNotice";
23
import { discardComposerDraft } from "../../lib/discardComposerDraft";
16
-import { getPreparingComposerImages } from "./preparingComposerImages";
24
+import { questionAttachmentDraftId } from "../../questionAttachments";
25
+import { readComposerImageBytes } from "./composerAttachmentFiles";
26
+import {
27
+ getPreparingComposerImages,
28
+ useSubmitAfterPreparingImages,
29
+} from "./preparingComposerImages";
30
import { useComposerAttachmentIntake } from "./useComposerAttachmentIntake";
31
32
const compression = vi.hoisted(() => ({ prepare: vi.fn() }));
@@ -32,6 +45,15 @@ function Harness({ input, ready }: { input: IntakeInput; ready: (value: Intake)
45
return null;
46
}
47
48
+function SendProbe(props: {
49
+ target: string;
50
+ ready: (holdSend: ReturnType<typeof useSubmitAfterPreparingImages>) => void;
51
+}) {
52
+ const holdSend = useSubmitAfterPreparingImages(props.target);
53
+ useEffect(() => props.ready(holdSend));
54
+ return null;
55
+}
56
+
57
function deferred<T>() {
58
let resolve!: (value: T) => void;
59
const promise = new Promise<T>((fulfill) => {
@@ -56,6 +78,165 @@ const compress = async (source: File) => {
78
const visiblePreparing = (targetKey: string) =>
79
getPreparingComposerImages(targetKey).filter((image) => !image.hidden);
80
81
+it.each(["available", "full", "duplicate"] as const)(
82
+ "finishes a cancelled question's screenshot and handles a waiting send (%s)",
83
+ async (capacity) => {
84
+ const store = useComposerDraftStore.getState();
85
+ const environmentId = EnvironmentId.make(`intake-recovery-${capacity}`);
86
+ const threadId = ThreadId.make("thread");
87
+ const requestId = RuntimeRequestId.make("question");
88
+ const target = scopeThreadRef(environmentId, threadId);
89
+ const question = questionAttachmentDraftId(environmentId, threadId, requestId, "image");
90
+ using _draft = { [Symbol.dispose]: () => store.clearComposerContent(target) };
91
+ if (capacity !== "available") {
92
+ store.addFiles(
93
+ target,
94
+ Array.from(
95
+ { length: PROVIDER_SEND_TURN_MAX_ATTACHMENTS - (capacity === "duplicate" ? 1 : 0) },
96
+ (_, index) => {
97
+ const name = `notes-${index}.txt`;
98
+ return {
99
+ type: "file" as const,
100
+ id: name,
101
+ name,
102
+ mimeType: "text/plain",
103
+ sizeBytes: 1,
104
+ file: new File(["a"], name, { type: "text/plain" }),
105
+ };
106
+ },
107
+ ),
108
+ );
109
+ }
110
+ if (capacity === "duplicate") {
111
+ const { file } = await compress(pastedImage());
112
+ await readComposerImageBytes(file);
113
+ store.addImages(target, [
114
+ {
115
+ type: "image",
116
+ id: "existing-image",
117
+ name: file.name,
118
+ mimeType: file.type,
119
+ sizeBytes: file.size,
120
+ file,
121
+ previewUrl: URL.createObjectURL(file),
122
+ },
123
+ ]);
124
+ }
125
+ const resumeCompression = deferred<void>();
126
+ compression.prepare.mockReset().mockImplementation(async (file: File) => {
127
+ await resumeCompression.promise;
128
+ return compress(file);
129
+ });
130
+ const setThreadError = vi.fn();
131
+ let intake!: Intake;
132
+ let holdSend!: ReturnType<typeof useSubmitAfterPreparingImages>;
133
+ const send = vi.fn();
134
+ await using _screen = await render(
135
+ <>
136
+ <Harness
137
+ input={{
138
+ activeThreadId: threadId,
139
+ activePendingProgress: null,
140
+ activePendingIsResponding: false,
141
+ pendingUserInputs: [],
142
+ supportsQuestionAttachments: true,
143
+ isLiteralPendingAnswer: true,
144
+ projectSelectionRequired: false,
145
+ questionAttachmentTarget: question,
146
+ attachmentTargetKey: question,
147
+ fileStagingLimit: null,
148
+ promptRef: { current: "" },
149
+ composerImagesRef: { current: [] },
150
+ composerFilesRef: { current: [] },
151
+ composerEditorRef: { current: null },
152
+ isRevertingCheckpointRef: { current: false },
153
+ setThreadError,
154
+ countReservedAttachments: () => 0,
155
+ addComposerImage: (image) => store.addImages(question, [image]),
156
+ addComposerImagesToDraft: (images) => store.addImages(question, images),
157
+ addComposerFilesToDraft: () => [],
158
+ removeComposerImageFromDraft: () => {},
159
+ insertAttachmentReferences: () => false,
160
+ }}
161
+ ready={(value) => {
162
+ intake = value;
163
+ }}
164
+ />
165
+ {capacity !== "available" && (
166
+ <SendProbe
167
+ target={composerTargetKey(target)}
168
+ ready={(value) => {
169
+ holdSend = value;
170
+ }}
171
+ />
172
+ )}
173
+ </>,
174
+ );
175
+ const attaching = intake.addComposerAttachments([pastedImage()]);
176
+ makeThreadRequestsResolvedAnnouncer(
177
+ environmentId,
178
+ threadId,
179
+ )([{ id: requestId, status: "cancelled" }]);
180
+ if (capacity !== "available") {
181
+ expect(holdSend(send)).toBe(true);
182
+ }
183
+ await act(async () => {
184
+ resumeCompression.resolve();
185
+ await attaching;
186
+ });
187
+
188
+ if (capacity === "full") {
189
+ expect(send).not.toHaveBeenCalled();
190
+ expect(store.getComposerDraft(target)?.images).toEqual([]);
191
+ expect(setThreadError).toHaveBeenLastCalledWith(
192
+ threadId,
193
+ expect.stringContaining("image.png could not be restored"),
194
+ );
195
+ } else if (capacity === "duplicate") {
196
+ expect(send).toHaveBeenCalledOnce();
197
+ expect(store.getComposerDraft(target)?.images.map(({ id }) => id)).toEqual([
198
+ "existing-image",
199
+ ]);
200
+ expect(setThreadError).toHaveBeenLastCalledWith(threadId, null);
201
+ } else {
202
+ expect(store.getComposerDraft(target)?.persistedAttachments).toEqual([
203
+ expect.objectContaining({
204
+ name: "image.png",
205
+ dataUrl: expect.stringContaining("data:image/png;base64,"),
206
+ }),
207
+ ]);
208
+ expect(setThreadError).toHaveBeenLastCalledWith(threadId, null);
209
+ }
210
+ },
211
+);
212
+
213
+it("saves recovered image records alongside existing records and drops stale results", async () => {
214
+ const target = DraftId.make("intake-recovery-persisted-records");
215
+ const store = useComposerDraftStore.getState();
216
+ using _draft = { [Symbol.dispose]: () => store.clearComposerContent(target) };
217
+ const saved = {
218
+ id: "saved",
219
+ name: "saved.png",
220
+ mimeType: "image/png",
221
+ sizeBytes: 1,
222
+ dataUrl: "data:image/png;base64,AQ==",
223
+ };
224
+ const recovered = { ...saved, id: "recovered", dataUrl: "data:image/png;base64,Ag==" };
225
+ const stale = { ...saved, id: "replaced", dataUrl: "data:image/png;base64,Aw==" };
226
+ const images = hydrateImagesFromPersisted([saved, recovered, stale]);
227
+ store.addImages(target, images);
228
+ await store.syncPersistedAttachments(target, [saved]);
229
+ store.removeImage(target, stale.id);
230
+ const replacement = hydrateImagesFromPersisted([
231
+ { ...stale, dataUrl: "data:image/png;base64,BA==" },
232
+ ]);
233
+ expect(store.addImages(target, replacement)).toEqual([stale.id]);
234
+
235
+ await store.appendPersistedAttachments(target, images, [recovered, stale]);
236
+
237
+ expect(store.getComposerDraft(target)?.persistedAttachments).toEqual([saved, recovered]);
238
+});
239
+
240
it("prepares a repeated paste without a tile, and still keeps a different image", async () => {
241
const target = DraftId.make("intake-repeat-paste");
242
const store = useComposerDraftStore.getState();
apps/web/src/components/chat/useComposerAttachmentIntake.ts
+57
-30
@@ -18,15 +18,14 @@ import {
18
type DraftId,
19
composerFileMatchesReattachMarker,
20
composerFileNeedsReattach,
21
+ composerTargetKey,
22
+ useComposerDraftStore,
23
} from "../../composerDraftStore";
24
import { type ComposerPromptEditorHandle } from "../ComposerPromptEditorTiptap";
25
import type { PendingUserInputProgress } from "../../pendingUserInput";
26
import type { ComposerAttachmentInsertion } from "../composerAttachmentInsertion";
27
import { type PendingUserInput } from "../../session-logic";
26
-import {
27
- changeQuestionAttachmentPreparation,
28
- useQuestionAttachmentPreparation,
29
-} from "../../questionAttachments";
28
+import { changeQuestionAttachmentPreparation } from "../../questionAttachments";
29
import {
30
classifyComposerAttachmentFile,
31
composerFileAttachmentMimeType,
@@ -53,10 +52,12 @@ import {
52
import { requestConfirmDialog } from "~/confirmDialog";
53
import { toastManager } from "../ui/toast";
54
import { type RefObject, useRef } from "react";
55
+import { serializeComposerImages } from "./composerImagePersistence";
56
import {
57
addPreparingComposerImages,
58
getPreparingComposerImages,
59
isPreparingComposerImage,
60
+ movedPreparingImageThread,
61
removePreparingComposerImages,
62
} from "./preparingComposerImages";
63
@@ -286,9 +287,17 @@ export function useComposerAttachmentIntake(input: {
287
const pendingInsertion = imageAttachmentsGetChips
288
? composerEditorRef.current?.captureAttachmentInsertion()
289
: undefined;
289
- for (const { id, file } of preparingImages) {
290
+ for (const image of preparingImages) {
291
+ const { id, file } = image;
292
+ const movedThread = movedPreparingImageThread(image);
293
// Removed from the strip while an earlier image was compressing.
291
- if (!isPreparingComposerImage(attachmentTargetKey, id)) continue;
294
+ if (
295
+ !isPreparingComposerImage(
296
+ movedThread ? composerTargetKey(movedThread) : attachmentTargetKey,
297
+ id,
298
+ )
299
+ )
300
+ continue;
301
// A rejection must still reach the cleanup below: a leaked
302
// placeholder would block sending from this draft for good.
303
const result = await prepareImageForAttachment(
@@ -314,21 +323,10 @@ export function useComposerAttachmentIntake(input: {
323
compressed.push({ id, file: result.file });
324
}
325
pendingInsertion?.dispose();
317
- const questionDiscarded =
318
- questionAttachmentTarget !== null &&
319
- !useQuestionAttachmentPreparation.getState().counts[questionAttachmentTarget];
320
- // Same tick as the draft insert below, so the strip swaps placeholders for
321
- // real tiles in one render. A placeholder removed meanwhile stays removed.
322
- const stillPreparingIds = removePreparingComposerImages(
323
- attachmentTargetKey,
324
- new Set(preparingImages.map((image) => image.id)),
325
- { cancelSubmission: compressionErrors.size > 0 || questionDiscarded },
326
- );
327
- if (questionAttachmentTarget)
328
- changeQuestionAttachmentPreparation(questionAttachmentTarget, -stillPreparingIds.size);
329
- if (questionDiscarded) return false;
326
+ const movedThread = preparingImages.map(movedPreparingImageThread).find(Boolean);
327
+ const completionTargetKey = movedThread ? composerTargetKey(movedThread) : attachmentTargetKey;
328
const nextImages: ComposerImageAttachment[] = compressed
331
- .filter(({ id }) => stillPreparingIds.has(id))
329
+ .filter(({ id }) => isPreparingComposerImage(completionTargetKey, id))
330
.map(({ id, file }) => ({
331
type: "image",
332
id,
@@ -338,19 +336,34 @@ export function useComposerAttachmentIntake(input: {
336
previewUrl: URL.createObjectURL(file),
337
file,
338
}));
339
+ const unrestoredImageNames: string[] = [];
340
+ const draftStore = useComposerDraftStore.getState();
341
const storedImageIds = new Set(
342
- nextImages.length === 1 && nextImages[0]
343
- ? addComposerImage(nextImages[0])
344
- : nextImages.length > 1
345
- ? addComposerImagesToDraft(nextImages)
346
- : [],
342
+ movedThread
343
+ ? draftStore.addImages(movedThread, nextImages, {
344
+ onCapacityExceeded: (image) => unrestoredImageNames.push(image.name),
345
+ })
346
+ : nextImages.length === 1 && nextImages[0]
347
+ ? addComposerImage(nextImages[0])
348
+ : nextImages.length > 1
349
+ ? addComposerImagesToDraft(nextImages)
350
+ : [],
351
);
352
+ // Keep the placeholders until the draft decides whether the recovered images fit,
353
+ // so a held send can be cancelled before the last placeholder disappears.
354
+ const stillPreparingIds = removePreparingComposerImages(
355
+ completionTargetKey,
356
+ new Set(preparingImages.map((image) => image.id)),
357
+ { cancelSubmission: compressionErrors.size > 0 || unrestoredImageNames.length > 0 },
358
+ );
359
+ if (questionAttachmentTarget && !movedThread)
360
+ changeQuestionAttachmentPreparation(questionAttachmentTarget, -stillPreparingIds.size);
361
const storedImages = nextImages.filter((image) => storedImageIds.has(image.id));
362
// The draft may be gone by now; revoking a URL the store already revoked is harmless.
363
for (const image of nextImages) {
364
if (!storedImageIds.has(image.id)) URL.revokeObjectURL(image.previewUrl);
365
}
353
- if (storedImages.length > 0 && imageAttachmentsGetChips) {
366
+ if (storedImages.length > 0 && imageAttachmentsGetChips && !movedThread) {
367
insertedAny =
368
insertAttachmentReferences(
369
storedImages.map(imageContextReference),
@@ -362,12 +375,26 @@ export function useComposerAttachmentIntake(input: {
375
// now other work (a failed send, an overlapping paste) may have set a
376
// thread error this call knows nothing about, and clearing it would
377
// swallow that message.
365
- const compressionError = [...compressionErrors]
378
+ const completionErrors = [...compressionErrors]
379
.filter(([id]) => stillPreparingIds.has(id))
380
.map(([, message]) => message)
368
- .at(-1);
369
- if (compressionError !== undefined) {
370
- setThreadError(threadId, compressionError);
381
+ .slice(-1);
382
+ if (unrestoredImageNames.length > 0) {
383
+ completionErrors.push(
384
+ m(
385
+ msg("{names} could not be restored: the composer is at its {limit}-attachment limit.", {
386
+ names: unrestoredImageNames.join(", "),
387
+ limit: PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
388
+ }),
389
+ ),
390
+ );
391
+ }
392
+ if (completionErrors.length > 0) {
393
+ setThreadError(threadId, completionErrors.join("\n"));
394
+ }
395
+ if (movedThread && storedImages.length > 0) {
396
+ const serialized = await serializeComposerImages(storedImages, []);
397
+ await draftStore.appendPersistedAttachments(movedThread, storedImages, serialized);
398
}
399
return insertedAny;
400
};
apps/web/src/components/desktop/SnapShotCoordinator.tsx
+14
-18
@@ -13,6 +13,7 @@ import { useEffect, useEffectEvent, useRef, useState } from "react";
13
import { m } from "../../i18n/messages";
14
15
import {
16
+ type ComposerImageAttachment,
17
type DraftId,
18
type PersistedComposerImageAttachment,
19
useComposerDraftStore,
@@ -182,21 +183,20 @@ export async function deliverSnapShot(
183
const source = resizeSnapShotSource(capture.source, compressed.imageSize);
184
const dataUrl = compressed.recompressed ? await readFileAsDataUrl(file) : capture.dataUrl;
185
const target = resolveTarget();
185
- const alreadyAttached =
186
- store.getComposerDraft(target)?.images.some(({ id }) => id === capture.id) ?? false;
187
- if (!alreadyAttached) {
186
+ const existingImage = store.getComposerDraft(target)?.images.find(({ id }) => id === capture.id);
187
+ const image: ComposerImageAttachment = existingImage ?? {
188
+ type: "image",
189
+ id: capture.id,
190
+ name: file.name,
191
+ mimeType: file.type,
192
+ sizeBytes: file.size,
193
+ previewUrl: dataUrl,
194
+ file,
195
+ source,
196
+ };
197
+ if (!existingImage) {
198
const questionAttachmentCount =
199
typeof target === "string" ? countQuestionRequestAttachments(target) : null;
190
- const image = {
191
- type: "image" as const,
192
- id: capture.id,
193
- name: file.name,
194
- mimeType: file.type,
195
- sizeBytes: file.size,
196
- previewUrl: dataUrl,
197
- file,
198
- source,
199
- };
200
// Only question drafts may bypass addImage's thread-session gate.
201
const added =
202
questionAttachmentCount === null
@@ -213,11 +213,7 @@ export async function deliverSnapShot(
213
dataUrl,
214
source,
215
};
216
- const persistedAttachments =
217
- store
218
- .getComposerDraft(target)
219
- ?.persistedAttachments.filter((attachment) => attachment.id !== capture.id) ?? [];
220
- await store.syncPersistedAttachments(target, [...persistedAttachments, persisted]);
216
+ await store.appendPersistedAttachments(target, [image], [persisted]);
217
if (!store.getComposerDraft(target)?.persistedAttachments.some(({ id }) => id === capture.id)) {
218
throw new Error(m(msg("The captured window could not be saved to the draft.")));
219
}
apps/web/src/composerDraftStore.ts
+32
-4
@@ -671,7 +671,10 @@ interface ComposerDraftStoreState {
671
addImages: (
672
threadRef: ComposerThreadTarget,
673
images: ComposerImageAttachment[],
674
- options?: { allowDuplicates?: boolean },
674
+ options?: {
675
+ allowDuplicates?: boolean;
676
+ onCapacityExceeded?: (image: ComposerImageAttachment) => void;
677
+ },
678
) => string[];
679
removeImage: (threadRef: ComposerThreadTarget, imageId: string) => void;
680
/** Returns the ids of files appended; a re-pick that replaces a marker is not listed. */
@@ -755,6 +758,12 @@ interface ComposerDraftStoreState {
758
threadRef: ComposerThreadTarget,
759
attachments: PersistedComposerImageAttachment[],
760
) => Promise<void>;
761
+ /** Saves records for the supplied images that the draft still owns after serialization. */
762
+ appendPersistedAttachments: (
763
+ threadRef: ComposerThreadTarget,
764
+ images: ReadonlyArray<ComposerImageAttachment>,
765
+ attachments: ReadonlyArray<PersistedComposerImageAttachment>,
766
+ ) => Promise<void>;
767
clearComposerContent: (threadRef: ComposerThreadTarget) => void;
768
/** Clears text and attachments, preserving terminal/element contexts, preview annotations, and review comments. */
769
clearComposerPromptAndImages: (threadRef: ComposerThreadTarget) => void;
@@ -2585,7 +2594,6 @@ function readPersistedAttachmentIdsFromStorage(threadKey: string): string[] {
2594
2595
function verifyPersistedAttachments(
2596
threadKey: string,
2588
- attachments: PersistedComposerImageAttachment[],
2597
set: (
2598
partial:
2599
| ComposerDraftStoreState
@@ -2609,7 +2617,7 @@ function verifyPersistedAttachments(
2617
return state;
2618
}
2619
const imageIdSet = new Set(current.images.map((image) => image.id));
2612
- const persistedAttachments = attachments.filter(
2620
+ const persistedAttachments = current.persistedAttachments.filter(
2621
(attachment) => imageIdSet.has(attachment.id) && persistedIdSet.has(attachment.id),
2622
);
2623
const nonPersistedImageIds: string[] = [];
@@ -3563,6 +3571,7 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
3571
return [];
3572
}
3573
let acceptedIds: string[] = [];
3574
+ const capacityRejections: ComposerImageAttachment[] = [];
3575
set((state) => {
3576
const existing = state.draftsByThreadKey[threadKey] ?? createEmptyThreadDraft();
3577
const existingIds = new Set(existing.images.map((image) => image.id));
@@ -3587,6 +3596,7 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
3596
existing.images.length + existing.files.length + dedupedIncoming.length >=
3597
PROVIDER_SEND_TURN_MAX_ATTACHMENTS
3598
) {
3599
+ capacityRejections.push(image);
3600
if (!acceptedPreviewUrls.has(image.previewUrl)) {
3601
revokeObjectPreviewUrl(image.previewUrl);
3602
}
@@ -3611,6 +3621,7 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
3621
},
3622
};
3623
});
3624
+ capacityRejections.forEach((image) => options?.onCapacityExceeded?.(image));
3625
return acceptedIds;
3626
},
3627
removeImage: (threadRef, imageId) => {
@@ -4364,7 +4375,24 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
4375
return { draftsByThreadKey: nextDraftsByThreadKey };
4376
});
4377
await Promise.resolve();
4367
- verifyPersistedAttachments(threadKey, attachments, set);
4378
+ verifyPersistedAttachments(threadKey, set);
4379
+ },
4380
+ appendPersistedAttachments: async (threadRef, images, attachments) => {
4381
+ const current = get().getComposerDraft(threadRef);
4382
+ if (!current || attachments.length === 0) return;
4383
+ const ownedImages = new Set(current.images);
4384
+ const ownedIds = new Set(
4385
+ images.filter((image) => ownedImages.has(image)).map(({ id }) => id),
4386
+ );
4387
+ const ownedRecords = attachments.filter(({ id }) => ownedIds.has(id));
4388
+ if (ownedRecords.length === 0) return;
4389
+ const persistedById = new Map(
4390
+ current.persistedAttachments.map((record) => [record.id, record]),
4391
+ );
4392
+ for (const record of ownedRecords) {
4393
+ if (!persistedById.has(record.id)) persistedById.set(record.id, record);
4394
+ }
4395
+ await get().syncPersistedAttachments(threadRef, [...persistedById.values()]);
4396
},
4397
clearComposerContent: (threadRef) => {
4398
const threadKey = resolveComposerDraftKey(get(), threadRef) ?? "";
apps/web/src/questionAttachments.test.ts
+49
-1
@@ -5,7 +5,12 @@ import {
5
import { ApprovalRequestId, EnvironmentId, RuntimeRequestId, ThreadId } from "@t3tools/contracts";
6
import { threadContextRecord } from "@t3tools/shared/composerContextReferences";
7
import { beforeEach, expect, it, vi } from "vite-plus/test";
8
-import { useComposerDraftStore } from "./composerDraftStore";
8
+import { composerTargetKey, useComposerDraftStore } from "./composerDraftStore";
9
+import {
10
+ addPreparingComposerImages,
11
+ cancelPreparingComposerImages,
12
+ getPreparingComposerImages,
13
+} from "./components/chat/preparingComposerImages";
14
import { formatInlineContextReference } from "./lib/composerContextReferences";
15
import { threadContextReference } from "./lib/composerContextRecords";
16
import {
@@ -219,3 +224,46 @@ it.each(["cancelled", "expired"] as const)(
224
expect(usePendingUserInputAnswers.getState()).toEqual({});
225
},
226
);
227
+
228
+it.each(["cancelled", "expired"] as const)(
229
+ "returns preparing images to the thread draft when a question is %s",
230
+ (status) => {
231
+ const unanswered = RuntimeRequestId.make("request");
232
+ const answered = RuntimeRequestId.make("request-other");
233
+ const target = composerTargetKey({ environmentId, threadId });
234
+ const preparing = [
235
+ [threadId, unanswered, "first", "move-first"],
236
+ [threadId, unanswered, "second", "move-second"],
237
+ [threadId, answered, "first", "discard"],
238
+ [ThreadId.make("other-thread"), unanswered, "first", "stay"],
239
+ ] as const;
240
+ const keys = preparing.map(([thread, request, question, id]) => {
241
+ const key = questionAttachmentDraftId(environmentId, thread, request, question);
242
+ addPreparingComposerImages(key, [
243
+ { id, name: "screenshot.png", file: new File(["image"], "screenshot.png"), hidden: false },
244
+ ]);
245
+ changeQuestionAttachmentPreparation(key, 1);
246
+ return key;
247
+ });
248
+ using _cleanup = {
249
+ [Symbol.dispose]: () => {
250
+ for (const key of [...keys, target]) cancelPreparingComposerImages(key);
251
+ },
252
+ };
253
+
254
+ makeThreadRequestsResolvedAnnouncer(
255
+ environmentId,
256
+ threadId,
257
+ )([
258
+ { id: unanswered, status },
259
+ { id: answered, status: "resolved" },
260
+ ]);
261
+
262
+ expect(getPreparingComposerImages(target).map(({ id }) => id)).toEqual([
263
+ "move-first",
264
+ "move-second",
265
+ ]);
266
+ expect(keys.slice(0, 3).flatMap(getPreparingComposerImages)).toEqual([]);
267
+ expect(getPreparingComposerImages(keys[3]!).map(({ id }) => id)).toEqual(["stay"]);
268
+ },
269
+);
apps/web/src/questionAttachments.ts
+23
-12
@@ -15,6 +15,11 @@ import type {
15
} from "@t3tools/contracts";
16
import { create } from "zustand";
17
import { DraftId, useComposerDraftStore } from "./composerDraftStore";
18
+import {
19
+ cancelPreparingComposerImages,
20
+ countPreparingComposerImages,
21
+ movePreparingComposerImagesToThread,
22
+} from "./components/chat/preparingComposerImages";
23
import { releaseDraftAttachments } from "./lib/attachmentUploadQueue";
24
import {
25
carryDisplacedCustomAnswerIntoPrompt,
@@ -115,6 +120,7 @@ export function clearQuestionAttachmentDraft(
120
}
121
}
122
store.clearComposerContent(key);
123
+ if (countPreparingComposerImages(key) > 0) cancelPreparingComposerImages(key);
124
useQuestionAttachmentPreparation.setState(({ counts }) => {
125
const next = { ...counts };
126
delete next[key];
@@ -178,12 +184,20 @@ function returnUnsentAnswersToThreadDraft(
184
}
185
186
const threadPrefix = questionAttachmentDraftPrefix(environmentId, threadId);
181
- const requestPrefixes = unansweredRequestIds.map(
182
- (requestId) =>
183
- `${threadPrefix}${encodeURIComponent(`${JSON.stringify([requestId]).slice(0, -1)},`)}`,
187
+ const preparingKeys = Object.keys(useQuestionAttachmentPreparation.getState().counts);
188
+ const otherRequestKeys = new Set(
189
+ resolvedQuestionDraftKeys(
190
+ [...preparingKeys, ...Object.keys(store.draftsByThreadKey)],
191
+ threadPrefix,
192
+ unansweredRequestIds,
193
+ ),
194
);
195
+ const wasUnanswered = (key: string) => key.startsWith(threadPrefix) && !otherRequestKeys.has(key);
196
+ for (const key of preparingKeys.filter(wasUnanswered)) {
197
+ movePreparingComposerImagesToThread(key, target);
198
+ }
199
const questionDrafts = Object.entries(store.draftsByThreadKey).filter(([key]) =>
186
- requestPrefixes.some((requestPrefix) => key.startsWith(requestPrefix)),
200
+ wasUnanswered(key),
201
);
202
const images = questionDrafts.flatMap(([, draft]) => draft.images);
203
const files = questionDrafts.flatMap(([, draft]) => draft.files);
@@ -193,14 +207,11 @@ function returnUnsentAnswersToThreadDraft(
207
const moved = useComposerDraftStore.getState().getComposerDraft(target);
208
const movedIds = new Set([...(moved?.images ?? []), ...(moved?.files ?? [])].map(({ id }) => id));
209
// Only a mounted composer re-serializes images, so carry their saved copies for a reload.
196
- const persisted = moved?.persistedAttachments ?? [];
197
- const persistedIds = new Set(persisted.map(({ id }) => id));
198
- const movedPersisted = questionDrafts
199
- .flatMap(([, draft]) => draft.persistedAttachments)
200
- .filter(({ id }) => movedIds.has(id) && !persistedIds.has(id));
201
- if (movedPersisted.length > 0) {
202
- void store.syncPersistedAttachments(target, [...persisted, ...movedPersisted]);
203
- }
210
+ void store.appendPersistedAttachments(
211
+ target,
212
+ images,
213
+ questionDrafts.flatMap(([, draft]) => draft.persistedAttachments),
214
+ );
215
for (const [key] of questionDrafts) clearQuestionAttachmentDraft(DraftId.make(key), movedIds);
216
}
217
scripts/scenarios/stopped-question-keeps-preparing-image.e2e.ts
new
+128
@@ -0,0 +1,128 @@
1
+// @effect-diagnostics nodeBuiltinImport:off -- The scenario starts a local ACP fixture.
2
+import * as NodePath from "node:path";
3
+
4
+import { ProviderDriverKind, ProviderInstanceId } from "@t3tools/contracts";
5
+import { expect } from "e2e";
6
+import * as Effect from "effect/Effect";
7
+
8
+import { T3_HOME } from "../e2e.config.ts";
9
+import {
10
+ expectHeldImageCompressions,
11
+ finishImageCompression,
12
+ holdImageCompression,
13
+ pasteWideScreenshot,
14
+} from "./image-compression-hold.ts";
15
+import { makeState, rpcClient, runPromise, test, WEB } from "./t3.ts";
16
+
17
+const ACP = `
18
+import { createInterface } from "node:readline";
19
+
20
+const sessionId = "image-question-session";
21
+const send = (message) => process.stdout.write(JSON.stringify({ jsonrpc: "2.0", ...message }) + "\\n");
22
+let promptId;
23
+for await (const line of createInterface({ input: process.stdin })) {
24
+ const { id, method } = JSON.parse(line);
25
+ if (method === "session/cancel" && promptId !== undefined) {
26
+ send({ id: promptId, result: { stopReason: "cancelled" } });
27
+ promptId = undefined;
28
+ }
29
+ if (id === undefined || method === undefined) continue;
30
+ if (method === "initialize") {
31
+ send({ id, result: { protocolVersion: 1, agentCapabilities: { promptCapabilities: { image: true } }, authMethods: [],
32
+ agentInfo: { name: "Question fixture", version: "1.0.0" } } });
33
+ } else if (method === "session/new") {
34
+ send({ id, result: { sessionId } });
35
+ } else if (method === "session/prompt") {
36
+ promptId = id;
37
+ send({
38
+ id: "ask-image-question",
39
+ method: "elicitation/create",
40
+ params: {
41
+ sessionId,
42
+ mode: "form",
43
+ message: "What should we look at?",
44
+ requestedSchema: {
45
+ type: "object",
46
+ properties: { answer: { type: "string", title: "Screenshot question" } },
47
+ required: ["answer"],
48
+ },
49
+ },
50
+ });
51
+ } else {
52
+ send({ id, error: { code: -32601, message: "Method not found" } });
53
+ }
54
+}
55
+`;
56
+
57
+test(
58
+ "Stopping an unanswered question keeps a screenshot that is still preparing",
59
+ WEB,
60
+ async ({ t3, app, browser }) => {
61
+ await holdImageCompression(browser);
62
+ const project = await t3.project("demo", { "question-agent.mjs": ACP });
63
+ const instanceId = ProviderInstanceId.make("scenario-question-agent");
64
+ await runPromise(
65
+ Effect.gen(function* () {
66
+ const client = yield* rpcClient(T3_HOME, app.baseUrl ?? "");
67
+ yield* client["server.updateSettings"]({
68
+ patch: {},
69
+ providerInstanceMutation: {
70
+ operation: "upsert",
71
+ instanceId,
72
+ instance: {
73
+ driver: ProviderDriverKind.make("acpRegistry"),
74
+ displayName: "Question fixture",
75
+ enabled: true,
76
+ config: {
77
+ source: "local",
78
+ commandPath: process.execPath,
79
+ commandArgs: [NodePath.join(project, "question-agent.mjs")],
80
+ },
81
+ },
82
+ },
83
+ });
84
+ }).pipe(Effect.scoped),
85
+ );
86
+ const { cli } = makeState(T3_HOME);
87
+ const started = await runPromise(
88
+ cli(
89
+ [
90
+ "thread",
91
+ "start",
92
+ project,
93
+ "Ask for a screenshot.",
94
+ "--title",
95
+ "Screenshot question thread",
96
+ "--provider",
97
+ instanceId,
98
+ "--model",
99
+ "default",
100
+ "--wait",
101
+ "--json",
102
+ ],
103
+ [2],
104
+ ),
105
+ );
106
+ const { threadId } = JSON.parse(started.stdout) as { threadId: string };
107
+ await t3.open({ threadId });
108
+
109
+ const composer = browser.locator('[data-chat-composer-form="true"]');
110
+ const question = browser.locator(
111
+ '[data-chat-composer-form="true"] [data-pending-user-input-toggle]',
112
+ );
113
+ await question.waitFor({ timeout: 60_000 });
114
+ await composer.getByRole("textbox").fill("Here is the screenshot.");
115
+ await pasteWideScreenshot(browser);
116
+ await expectHeldImageCompressions(browser, 1);
117
+ await composer.getByRole("button", "Stop generation").tap();
118
+ await question.waitFor({ state: "hidden" });
119
+
120
+ await finishImageCompression(browser);
121
+ await app.screenshot("draft-after-stopping-question");
122
+ await expect(
123
+ browser.locator(
124
+ '[data-chat-composer-form="true"] button[aria-label="Preview screenshot.png"]:enabled',
125
+ ),
126
+ ).toBeVisible();
127
+ },
128
+);