Skip to content

Commit ea3db45

Browse files
committed
refactor(web): use destination thread state for paste reuse
1 parent 3e40bc1 commit ea3db45

2 files changed

Lines changed: 12 additions & 9 deletions

File tree

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,13 @@ it.each([
193193
);
194194
});
195195

196-
it.each(["same", "renamed", "other-environment"] as const)(
196+
it.each(["same", "renamed", "other-environment", "different-draft"] as const)(
197197
"preserves thread identity across repeated pastes: %s",
198198
async (variant) => {
199199
const otherEnvironment = variant === "other-environment";
200200
const target = DraftId.make(`thread-collision-${variant}`);
201+
const attachmentTarget =
202+
variant === "different-draft" ? DraftId.make("question-draft") : target;
201203
const store = useComposerDraftStore.getState();
202204
using _draft = { [Symbol.dispose]: () => store.clearComposerContent(target) };
203205
const environmentId = EnvironmentId.make("thread-source");
@@ -224,10 +226,10 @@ it.each(["same", "renamed", "other-environment"] as const)(
224226
activeThread: undefined,
225227
activeThreadId: null,
226228
composerDraftTarget: target,
227-
attachmentDraftTarget: target,
228-
attachmentTargetKey: target,
229-
attachmentTargetKeyRef: { current: target },
230-
questionAttachmentTarget: null,
229+
attachmentDraftTarget: attachmentTarget,
230+
attachmentTargetKey: attachmentTarget,
231+
attachmentTargetKeyRef: { current: attachmentTarget },
232+
questionAttachmentTarget: attachmentTarget === target ? null : attachmentTarget,
231233
pendingUserInputs: [],
232234
composerContextRecords: new Map([[first.contextId, { kind: "thread", record: first }]]),
233235
composerImages: [],
@@ -244,7 +246,7 @@ it.each(["same", "renamed", "other-environment"] as const)(
244246
let importedId: string | undefined;
245247
await act(() => {
246248
importedId = importer
247-
.importContextRecords([pasted], pasted.environmentId)
249+
.importContextRecords([pasted], pasted.environmentId, target)
248250
.get(pasted.contextId);
249251
});
250252
const records = store.getComposerDraft(target)!.threadContexts;
@@ -255,7 +257,7 @@ it.each(["same", "renamed", "other-environment"] as const)(
255257
expect(records).toHaveLength(otherEnvironment ? 2 : 1);
256258
expect(records[0]).toEqual(first);
257259
await act(() => {
258-
const repeated = importer.importContextRecords([pasted], pasted.environmentId);
260+
const repeated = importer.importContextRecords([pasted], pasted.environmentId, target);
259261
expect(repeated.get(pasted.contextId)).toBe(importedId);
260262
});
261263
expect(store.getComposerDraft(target)!.threadContexts).toEqual(records);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ export function useComposerContextImport(input: {
302302
: 0,
303303
);
304304
for (const candidate of orderedRecords) {
305-
// Producer ids fold into context ids, so two different excerpts can collide. Only skip
305+
// Producer ids fold into context ids, so two different excerpts can collide. For non-thread records, only skip
306306
// when the draft already holds the same payload; a colliding but different record is
307307
// re-minted under a fresh id so both survive the paste.
308308
const record = asKnownContextRecord(candidate);
@@ -398,14 +398,15 @@ export function useComposerContextImport(input: {
398398
!linkedEnvironments.has(record.environmentId)
399399
)
400400
break;
401+
// Threads reuse their destination record by identity, reminting only on an ID collision.
401402
const threads = getComposerDraft(threadDraftTarget)?.threadContexts ?? [];
402403
const attached = threads.find(
403404
(thread) =>
404405
thread.environmentId === record.environmentId && thread.threadId === record.threadId,
405406
);
406407
const imported =
407408
attached ??
408-
(conflicts || threads.some((thread) => thread.contextId === record.contextId)
409+
(threads.some((thread) => thread.contextId === record.contextId)
409410
? { ...record, contextId: toKindScopedComposerContextId("thread", randomUUID()) }
410411
: record);
411412
addComposerDraftThreadContexts(threadDraftTarget, [imported], {

0 commit comments

Comments
 (0)