Skip to content

Commit a3e964d

Browse files
ahalekellyclaude
andcommitted
fix(mobile): image attachments no longer overfill the draft or block on full context
Chips replace the selected text, which frees the slots of any chips in the selection. A mixed pick counted those slots even when only images were accepted, so the selection stayed and the draft could pass the attachment limit. A full context also rejected images that need no context record. Files now take chips first, against the draft with the selection replaced; images then fill the slots that remain. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e721a43 commit a3e964d

2 files changed

Lines changed: 98 additions & 47 deletions

File tree

‎apps/mobile/src/state/use-composer-drafts.test.ts‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ describe("mobile composer drafts", () => {
716716
},
717717
);
718718

719-
it("rejects attachments atomically when no context slots remain", async () => {
719+
it("rejects files but keeps images when no context slots remain", async () => {
720720
const outboxLoad = vi.spyOn(threadOutboxManager, "load").mockResolvedValue(true);
721721
onTestFinished(() => outboxLoad.mockRestore());
722722
const cleanup = Promise.withResolvers<void>();
@@ -759,6 +759,55 @@ describe("mobile composer drafts", () => {
759759
).toBe(1);
760760
expect(getComposerDraftSnapshot(key)).toEqual(before);
761761
await cleanup.promise;
762+
const image = {
763+
id: "photo",
764+
type: "image" as const,
765+
name: "photo.png",
766+
mimeType: "image/png",
767+
sizeBytes: 4,
768+
previewUri: "file:///photo.png",
769+
};
770+
const video = {
771+
id: "video",
772+
type: "file" as const,
773+
name: "clip.mp4",
774+
mimeType: "video/mp4",
775+
sizeBytes: 4,
776+
fileUri: "file:///clip.mp4",
777+
};
778+
expect(appendComposerDraftAttachments(key, [image, video], { appendReference: true })).toBe(1);
779+
expect(getComposerDraftSnapshot(key)).toEqual({ ...before, attachments: [image] });
780+
});
781+
782+
it("fills images only into slots left after file chips replace the selection", () => {
783+
const key = "environment-1:replace-at-limit";
784+
const file = (id: string) => ({
785+
id,
786+
type: "file" as const,
787+
name: `${id}.txt`,
788+
mimeType: "text/plain",
789+
sizeBytes: 4,
790+
fileUri: `file:///${id}.txt`,
791+
});
792+
const image = {
793+
id: "photo",
794+
type: "image" as const,
795+
name: "photo.png",
796+
mimeType: "image/png",
797+
sizeBytes: 4,
798+
previewUri: "file:///photo.png",
799+
};
800+
appendComposerDraftAttachments(key, [file("old")], { appendReference: true });
801+
const { text } = getComposerDraftSnapshot(key);
802+
const insertion = captureComposerDraftInsertion(key, { start: 0, end: text.length });
803+
expect(
804+
appendComposerDraftAttachments(key, [image, file("new")], {
805+
appendReference: true,
806+
insertion,
807+
maxAttachments: 1,
808+
}),
809+
).toBe(1);
810+
expect(getComposerDraftSnapshot(key).attachments).toEqual([file("new")]);
762811
});
763812
it.each(["environment-1:thread", "environment-1:new-task:draft"])(
764813
"preserves the captured paste selection across async writes in %s",

‎apps/mobile/src/state/use-composer-drafts.ts‎

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,56 +1342,58 @@ export function appendComposerDraftAttachments(
13421342
let removed: ReadonlyArray<DraftComposerAttachment> = [];
13431343
updateComposerDrafts((current) => {
13441344
const existing = normalizeDraft(current[draftKey]);
1345-
const needsReference = (attachment: DraftComposerAttachment) =>
1346-
options?.appendReference === true && attachment.type !== "image";
1347-
const referencing = attachments.some(needsReference);
1348-
const retained = referencing
1349-
? draftWithoutInsertionSelection(draftKey, existing, options?.insertion)
1350-
: existing;
1351-
const remaining = options?.allowOverflow
1352-
? attachments.length
1353-
: Math.max(
1345+
const limit = Math.min(
1346+
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
1347+
options?.maxAttachments ?? PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
1348+
);
1349+
const capacity = (draft: ComposerDraft, wanted: number) =>
1350+
options?.allowOverflow ? wanted : Math.max(0, limit - draft.attachments.length);
1351+
// Chips replace the selection, so files are placed first and images fill the slots left after.
1352+
const chipped = options?.appendReference
1353+
? attachments.filter((attachment) => attachment.type !== "image")
1354+
: [];
1355+
let draft = existing;
1356+
if (chipped.length > 0) {
1357+
const retained = draftWithoutInsertionSelection(draftKey, existing, options?.insertion);
1358+
const records = chipped
1359+
.slice(
13541360
0,
1355-
Math.min(
1356-
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
1357-
options?.maxAttachments ?? PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
1358-
) - retained.attachments.length,
1361+
Math.max(
1362+
0,
1363+
Math.min(
1364+
capacity(retained, chipped.length),
1365+
COMPOSER_CONTEXT_MAX_RECORDS - (retained.context?.records.length ?? 0),
1366+
),
1367+
),
1368+
)
1369+
.map((attachment) => attachmentContextRecord(attachment));
1370+
const inserted =
1371+
records.length > 0 &&
1372+
draftWithInsertedContext(
1373+
draftKey,
1374+
{
1375+
...existing,
1376+
attachments: [...existing.attachments, ...chipped.slice(0, records.length)],
1377+
},
1378+
{
1379+
text: records.map(formatComposerContextReference).join(" "),
1380+
context: { version: 1, records },
1381+
},
1382+
options?.insertion,
13591383
);
1360-
const contextCapacity = referencing
1361-
? Math.max(0, COMPOSER_CONTEXT_MAX_RECORDS - (retained.context?.records.length ?? 0))
1362-
: attachments.length;
1363-
const accepted = attachments.slice(0, Math.min(remaining, contextCapacity));
1364-
rejected = attachments.slice(accepted.length);
1365-
if (accepted.length === 0) {
1366-
return current;
1384+
if (inserted) draft = { ...inserted, attachments: [...inserted.attachments] };
13671385
}
1368-
let draft = { ...existing, attachments: [...existing.attachments, ...accepted] };
1369-
const records = accepted
1370-
.filter(needsReference)
1371-
.map((attachment) => attachmentContextRecord(attachment));
1372-
if (records.length > 0) {
1373-
const inserted = draftWithInsertedContext(
1374-
draftKey,
1375-
draft,
1376-
{
1377-
text: records.map(formatComposerContextReference).join(" "),
1378-
context: { version: 1, records },
1379-
},
1380-
options?.insertion,
1381-
);
1382-
if (!inserted) {
1383-
rejected = attachments;
1384-
return current;
1385-
}
1386-
draft = { ...inserted, attachments: [...inserted.attachments] };
1387-
removed = existing.attachments.filter(
1388-
(attachment) => !draft.attachments.includes(attachment),
1389-
);
1390-
}
1391-
return {
1392-
...current,
1393-
[draftKey]: draft,
1386+
const plain = options?.appendReference
1387+
? attachments.filter((attachment) => attachment.type === "image")
1388+
: attachments;
1389+
draft = {
1390+
...draft,
1391+
attachments: [...draft.attachments, ...plain.slice(0, capacity(draft, plain.length))],
13941392
};
1393+
rejected = attachments.filter((attachment) => !draft.attachments.includes(attachment));
1394+
if (rejected.length === attachments.length) return current;
1395+
removed = existing.attachments.filter((attachment) => !draft.attachments.includes(attachment));
1396+
return { ...current, [draftKey]: draft };
13951397
});
13961398
scheduleUnusedComposerAttachmentCleanup([...rejected, ...removed]);
13971399
return rejected.length;

0 commit comments

Comments
 (0)