Skip to content

Commit 19a2c66

Browse files
committed
fix(web): preserve distinct video draft files
1 parent 21aea47 commit 19a2c66

3 files changed

Lines changed: 103 additions & 50 deletions

File tree

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

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import {
5757
type PersistedComposerFileAttachment,
5858
type PersistedComposerImageAttachment,
5959
composerFileDedupKey,
60+
composerFileMatchesReattachMarker,
6061
composerFileNeedsReattach,
6162
composerTargetKey,
6263
hydrateImagesFromPersisted,
@@ -2413,9 +2414,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
24132414
),
24142415
);
24152416
const existingFileKeys = new Set(composerFilesNow.map(composerFileDedupKey));
2416-
const reattachMarkerKeys = new Set(
2417-
composerFilesNow.filter(composerFileNeedsReattach).map(composerFileDedupKey),
2418-
);
2417+
const reattachMarkers = composerFilesNow.filter(composerFileNeedsReattach);
2418+
const restoredMarkerIds = new Set<string>();
24192419
const duplicateFiles: PersistedComposerFileAttachment[] = [];
24202420
const markerReplacements: ComposerFileAttachment[] = [];
24212421
const appendedFiles: ComposerFileAttachment[] = [];
@@ -2441,23 +2441,23 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
24412441
}
24422442
continue;
24432443
}
2444-
if (existingFileKeys.has(key)) {
2445-
if (reattachMarkerKeys.has(key)) {
2446-
// The draft row with this identity is a needs-reattach marker,
2447-
// not a real duplicate. Replace it (addFiles swaps a matching
2448-
// marker in place) instead of deleting the only uploaded copy.
2449-
reattachMarkerKeys.delete(key);
2450-
existingFileIds.add(file.id);
2451-
if (expired) {
2452-
// The draft's marker already says "attach again"; nothing to
2453-
// restore or release, but say why the stash copy is gone.
2454-
expiredFileNames.push(file.name);
2455-
} else {
2456-
retainedUploadIds.add(file.attachmentId);
2457-
markerReplacements.push(restored);
2458-
}
2459-
continue;
2444+
const reattachMarker = reattachMarkers.find(
2445+
(marker) =>
2446+
!restoredMarkerIds.has(marker.id) && composerFileMatchesReattachMarker(marker, file),
2447+
);
2448+
if (reattachMarker) {
2449+
restoredMarkerIds.add(reattachMarker.id);
2450+
existingFileIds.add(file.id);
2451+
existingFileKeys.add(key);
2452+
if (expired) {
2453+
expiredFileNames.push(file.name);
2454+
} else {
2455+
retainedUploadIds.add(file.attachmentId);
2456+
markerReplacements.push(restored);
24602457
}
2458+
continue;
2459+
}
2460+
if (existingFileKeys.has(key)) {
24612461
if (!expired && !retainedUploadIds.has(file.attachmentId)) {
24622462
duplicateFiles.push(file);
24632463
}
@@ -2993,9 +2993,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
29932993
// A pick that matches a needs-reattach marker replaces it in the draft, so
29942994
// it must not consume a slot; a draft full of markers would otherwise hit
29952995
// the capacity error before the replacement path could run.
2996-
const reattachKeys = new Set(
2997-
composerFilesRef.current.filter(composerFileNeedsReattach).map(composerFileDedupKey),
2998-
);
2996+
const reattachMarkers = composerFilesRef.current.filter(composerFileNeedsReattach);
2997+
const replacedReattachMarkerIds = new Set<string>();
29992998
const acceptedImages: File[] = [];
30002999
const acceptedFiles: ComposerFileAttachment[] = [];
30013000
let error: string | null = null;
@@ -3006,16 +3005,22 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
30063005
? (videoMimeType({ name: file.name, mimeType: file.type }) ??
30073006
(file.type || "application/octet-stream"))
30083007
: file.type;
3009-
const replacesReattachMarker =
3010-
attachmentKind === "file" &&
3011-
reattachKeys.delete(
3012-
composerFileDedupKey({
3013-
name: file.name || "file",
3014-
mimeType: fileMimeType,
3015-
sizeBytes: file.size,
3016-
}),
3017-
);
3018-
if (!replacesReattachMarker && reservedCount >= PROVIDER_SEND_TURN_MAX_ATTACHMENTS) {
3008+
const matchingReattachMarker =
3009+
attachmentKind === "file"
3010+
? reattachMarkers.find(
3011+
(marker) =>
3012+
!replacedReattachMarkerIds.has(marker.id) &&
3013+
composerFileMatchesReattachMarker(marker, {
3014+
name: file.name || "file",
3015+
mimeType: fileMimeType,
3016+
sizeBytes: file.size,
3017+
}),
3018+
)
3019+
: undefined;
3020+
if (matchingReattachMarker) {
3021+
replacedReattachMarkerIds.add(matchingReattachMarker.id);
3022+
}
3023+
if (!matchingReattachMarker && reservedCount >= PROVIDER_SEND_TURN_MAX_ATTACHMENTS) {
30193024
error = `You can attach up to ${PROVIDER_SEND_TURN_MAX_ATTACHMENTS} files per message.`;
30203025
// Keep scanning: a later file in this batch can still replace a
30213026
// needs-reattach marker without needing a free slot.
@@ -3053,7 +3058,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
30533058
file: attachmentFile,
30543059
});
30553060
}
3056-
if (!replacesReattachMarker) {
3061+
if (!matchingReattachMarker) {
30573062
reservedCount += 1;
30583063
}
30593064
}

‎apps/web/src/composerDraftStore.test.ts‎

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -530,16 +530,15 @@ describe("composerDraftStore file attachments", () => {
530530
store.addFiles(threadRef, [marker]);
531531

532532
const file = new File(["report"], marker.name, { type: "video/x-matroska" });
533-
store.addFiles(threadRef, [
534-
{
535-
type: "file",
536-
id: "file-repicked",
537-
name: file.name,
538-
mimeType: file.type,
539-
sizeBytes: file.size,
540-
file,
541-
},
542-
]);
533+
const repicked: ComposerFileAttachment = {
534+
type: "file",
535+
id: "file-repicked",
536+
name: file.name,
537+
mimeType: file.type,
538+
sizeBytes: file.size,
539+
file,
540+
};
541+
store.addFiles(threadRef, [repicked, { ...repicked, id: "file-repicked-duplicate" }]);
543542

544543
const files = store.getComposerDraft(threadRef)?.files;
545544
expect(files?.map((entry) => entry.id)).toEqual(["file-repicked"]);
@@ -582,6 +581,36 @@ describe("composerDraftStore file attachments", () => {
582581
]);
583582
});
584583

584+
it("keeps same-name videos with different MIME types", () => {
585+
const store = useComposerDraftStore.getState();
586+
const mp4 = new File(["report"], "clip", { type: "video/mp4" });
587+
const webm = new File(["report"], "clip", { type: "video/webm" });
588+
589+
store.addFiles(threadRef, [
590+
{
591+
type: "file",
592+
id: "video-mp4",
593+
name: mp4.name,
594+
mimeType: mp4.type,
595+
sizeBytes: mp4.size,
596+
file: mp4,
597+
},
598+
{
599+
type: "file",
600+
id: "video-webm",
601+
name: webm.name,
602+
mimeType: webm.type,
603+
sizeBytes: webm.size,
604+
file: webm,
605+
},
606+
]);
607+
608+
expect(store.getComposerDraft(threadRef)?.files.map((file) => file.id)).toEqual([
609+
"video-mp4",
610+
"video-webm",
611+
]);
612+
});
613+
585614
it("keeps the remaining file slot available after a duplicate is skipped", () => {
586615
const store = useComposerDraftStore.getState();
587616
store.addImages(

‎apps/web/src/composerDraftStore.ts‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,21 @@ function composerImageDedupKey(image: ComposerImageAttachment): string {
749749
export function composerFileDedupKey(
750750
file: Pick<ComposerFileAttachment, "mimeType" | "sizeBytes" | "name">,
751751
): string {
752-
const mimeType = videoMimeType(file) === null ? file.mimeType : "video";
753-
return `${mimeType}\u0000${file.sizeBytes}\u0000${file.name}`;
752+
return `${file.mimeType}\u0000${file.sizeBytes}\u0000${file.name}`;
753+
}
754+
755+
export function composerFileMatchesReattachMarker(
756+
marker: Pick<ComposerFileAttachment, "mimeType" | "sizeBytes" | "name">,
757+
file: Pick<ComposerFileAttachment, "mimeType" | "sizeBytes" | "name">,
758+
): boolean {
759+
if (marker.name !== file.name || marker.sizeBytes !== file.sizeBytes) return false;
760+
if (marker.mimeType === file.mimeType) return true;
761+
const markerMimeType = marker.mimeType.toLowerCase();
762+
return (
763+
(markerMimeType === "" || markerMimeType === "application/octet-stream") &&
764+
videoMimeType(marker) !== null &&
765+
videoMimeType(file) !== null
766+
);
754767
}
755768

756769
function terminalContextDedupKey(context: TerminalContextDraft): string {
@@ -3208,15 +3221,21 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
32083221
if (knownIds.has(file.id)) {
32093222
continue;
32103223
}
3211-
const duplicate = knownFiles.get(key);
3224+
const duplicate =
3225+
knownFiles.get(key) ??
3226+
existing.files.find(
3227+
(candidate) =>
3228+
composerFileNeedsReattach(candidate) &&
3229+
!replacements.has(candidate.id) &&
3230+
composerFileMatchesReattachMarker(candidate, file),
3231+
);
32123232
if (duplicate) {
3213-
// A needs-reattach marker persists exactly the metadata this
3214-
// key hashes, so re-picking the same file matches its marker.
3215-
// Dropping the pick as a duplicate would leave the draft
3216-
// blocked forever; replace the marker so the upload restarts.
3233+
// A needs-reattach marker is not a usable duplicate. Replace
3234+
// it so the upload restarts.
32173235
if (composerFileNeedsReattach(duplicate) && !replacements.has(duplicate.id)) {
32183236
replacements.set(duplicate.id, file);
32193237
knownIds.add(file.id);
3238+
knownFiles.set(key, file);
32203239
}
32213240
continue;
32223241
}

0 commit comments

Comments
 (0)