Skip to content

Commit 4ac5950

Browse files
only21milcodex
andcommitted
fix(web): keep deleted clipboard attachments out of the draft
Read the live reference after downloading so deleting a chip cannot stage its bytes or recreate its reference. Preserve referenced annotation screenshots and undo before completion. Co-Authored-By: GPT-6.1 Sol <[email protected]>
1 parent 50bcba0 commit 4ac5950

3 files changed

Lines changed: 38 additions & 8 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
14341434
composerContextRecords,
14351435
composerImages,
14361436
composerFilesRef,
1437+
promptRef,
14371438
pendingImageCompressionsRef,
14381439
pendingThreadImportsRef,
14391440
addComposerFilesToDraft,

‎apps/web/src/components/chat/useComposerContextImport.browser.test.tsx‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ function Harness({ input, ready }: { input: ImportInput; ready: (value: Importer
3131
return null;
3232
}
3333

34+
function deferred<T>() {
35+
let resolve!: (value: T) => void;
36+
const promise = new Promise<T>((fulfill) => {
37+
resolve = fulfill;
38+
});
39+
return { promise, resolve };
40+
}
41+
3442
it.each([
3543
{ kind: "file", edit: "delete" },
3644
{ kind: "image", edit: "delete" },
@@ -55,9 +63,9 @@ it.each([
5563
[Symbol.dispose]: () => rememberCopiedComposerAttachments(sourceEnvironmentId, new Map()),
5664
};
5765
const promptRef = { current: "" };
58-
const downloaded = Promise.withResolvers<Response>();
59-
const requested = Promise.withResolvers<void>();
60-
const settled = Promise.withResolvers<void>();
66+
const downloaded = deferred<Response>();
67+
const requested = deferred<void>();
68+
const settled = deferred<void>();
6169
const end = pendingDraftWork.end.bind(pendingDraftWork);
6270
using _settled = vi.spyOn(pendingDraftWork, "end").mockImplementation((key) => {
6371
end(key);
@@ -69,7 +77,7 @@ it.each([
6977
});
7078
const input = {
7179
environmentId: EnvironmentId.make("clipboard-target"),
72-
activeThread: null,
80+
activeThread: undefined,
7381
activeThreadId: null,
7482
composerDraftTarget: target,
7583
attachmentDraftTarget: target,
@@ -96,16 +104,20 @@ it.each([
96104
/>,
97105
);
98106
const contextId = ComposerContextId.make(`${kind}_source`);
99-
const attachment = {
100-
version: 1,
101-
kind,
107+
const attachmentFields = {
108+
version: 1 as const,
102109
contextId,
103110
label: "notes.txt",
104111
attachmentId: "source-attachment",
105112
name: "notes.txt",
106113
mimeType: kind === "file" ? "text/plain" : "image/png",
107114
sizeBytes: 5,
108-
} satisfies ComposerContextRecord;
115+
};
116+
const attachment = (
117+
kind === "file"
118+
? { ...attachmentFields, kind: "file" as const }
119+
: { ...attachmentFields, kind: "image" as const }
120+
) satisfies ComposerContextRecord;
109121
const annotation = {
110122
version: 1,
111123
kind: "preview-annotation",

‎apps/web/src/components/chat/useComposerContextImport.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
import { assetEnvironment } from "~/state/assets";
3333
import { copiedComposerAttachmentFile } from "~/lib/copiedComposerAttachments";
3434
import { countQuestionAttachments, questionAttachmentDraftId } from "../../questionAttachments";
35+
import { collectInlineContextIds } from "../../lib/composerContextReferences";
3536
import { elementContextToPreviewAnnotation } from "../../lib/elementContext";
3637
import { orchestrationEnvironment } from "~/state/orchestration";
3738
import { pendingDraftWork } from "./pendingDraftWork";
@@ -74,6 +75,7 @@ export function useComposerContextImport(input: {
7475
readonly composerContextRecords: ComposerDraftContextRecords;
7576
readonly composerImages: ReadonlyArray<ComposerImageAttachment>;
7677
readonly composerFilesRef: ChatComposerProps["composerFilesRef"];
78+
readonly promptRef: RefObject<string>;
7779
readonly pendingImageCompressionsRef: RefObject<Map<string, number>>;
7880
readonly pendingThreadImportsRef: RefObject<Map<string, number>>;
7981
readonly addComposerFilesToDraft: (files: ComposerFileAttachment[]) => string[];
@@ -91,6 +93,7 @@ export function useComposerContextImport(input: {
9193
composerContextRecords,
9294
composerImages,
9395
composerFilesRef,
96+
promptRef,
9497
pendingImageCompressionsRef,
9598
pendingThreadImportsRef,
9699
addComposerFilesToDraft,
@@ -171,6 +174,20 @@ export function useComposerContextImport(input: {
171174
fail("Downloading it from the source failed.");
172175
return;
173176
}
177+
// A deleted chip must not return with its download. Undo can restore it before
178+
// completion, and an annotation can still own its screenshot without an image chip.
179+
const references = new Set(collectInlineContextIds(promptRef.current));
180+
if (
181+
!references.has(toKindScopedComposerContextId(record.kind, localId)) &&
182+
!(
183+
record.kind === "image" &&
184+
references.has(previewAnnotationContextId(localId)) &&
185+
getComposerDraft(composerDraftTarget)?.previewAnnotations.some(
186+
(annotation) => annotation.id === localId,
187+
)
188+
)
189+
)
190+
return;
174191
file = new File([blob], record.name, { type: record.mimeType || blob.type });
175192
}
176193
// The draft these bytes belong to may have been sent or switched away from while they

0 commit comments

Comments
 (0)