Skip to content

Commit 17222bc

Browse files
committed
fix(web): preserve pasted thread identity across environments
1 parent a740b7e commit 17222bc

2 files changed

Lines changed: 73 additions & 3 deletions

File tree

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
import { threadContextRecord } from "@t3tools/shared/composerContextReferences";
12
import { useEffect } from "react";
2-
import { type ComposerContextRecord, ComposerContextId, EnvironmentId } from "@t3tools/contracts";
3+
import {
4+
type ComposerContextRecord,
5+
ComposerContextId,
6+
EnvironmentId,
7+
ThreadId,
8+
} from "@t3tools/contracts";
39
import { expect, it, vi } from "vite-plus/test";
410

511
import { act, render, stubGlobal } from "../../../test/render";
@@ -186,3 +192,64 @@ it.each([
186192
: [],
187193
);
188194
});
195+
196+
it.each([false, true])(
197+
"preserves thread identity when pasting a colliding context (other environment: %s)",
198+
async (otherEnvironment) => {
199+
const target = DraftId.make(`thread-collision-${otherEnvironment}`);
200+
const store = useComposerDraftStore.getState();
201+
using _draft = { [Symbol.dispose]: () => store.clearComposerContent(target) };
202+
const environmentId = EnvironmentId.make("thread-source");
203+
const otherEnvironmentId = EnvironmentId.make("other-thread-source");
204+
const first = threadContextRecord(
205+
{ environmentId, threadId: ThreadId.make("same-id") },
206+
"First",
207+
);
208+
const pasted = otherEnvironment
209+
? threadContextRecord(
210+
{ environmentId: otherEnvironmentId, threadId: first.threadId },
211+
"Second",
212+
)
213+
: first;
214+
store.addThreadContexts(target, [first], { appendReference: false });
215+
let importer!: Importer;
216+
await using _screen = await render(
217+
<Harness
218+
input={{
219+
environmentId,
220+
linkedEnvironments: new Map([[otherEnvironmentId, "Other machine"]]),
221+
activeThread: undefined,
222+
activeThreadId: null,
223+
composerDraftTarget: target,
224+
attachmentDraftTarget: target,
225+
attachmentTargetKey: target,
226+
attachmentTargetKeyRef: { current: target },
227+
questionAttachmentTarget: null,
228+
pendingUserInputs: [],
229+
composerContextRecords: new Map([[first.contextId, { kind: "thread", record: first }]]),
230+
composerImages: [],
231+
composerFilesRef: { current: [] },
232+
pendingThreadImportsRef: { current: new Map() },
233+
promptRef: { current: "" },
234+
addComposerFilesToDraft: () => [],
235+
}}
236+
ready={(value) => {
237+
importer = value;
238+
}}
239+
/>,
240+
);
241+
let importedId: string | undefined;
242+
await act(() => {
243+
importedId = importer
244+
.importContextRecords([pasted], pasted.environmentId)
245+
.get(pasted.contextId);
246+
});
247+
const records = store.getComposerDraft(target)!.threadContexts;
248+
expect(records.find((record) => record.contextId === importedId)).toEqual({
249+
...pasted,
250+
contextId: importedId,
251+
});
252+
expect(records).toHaveLength(otherEnvironment ? 2 : 1);
253+
expect(records[0]).toEqual(first);
254+
},
255+
);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,13 @@ export function useComposerContextImport(input: {
399399
!linkedEnvironments.has(record.environmentId)
400400
)
401401
break;
402-
addComposerDraftThreadContexts(threadDraftTarget, [record], {
402+
const imported = conflicts
403+
? { ...record, contextId: toKindScopedComposerContextId("thread", randomUUID()) }
404+
: record;
405+
addComposerDraftThreadContexts(threadDraftTarget, [imported], {
403406
appendReference: false,
404407
});
405-
rewritten.set(record.contextId, record.contextId);
408+
rewritten.set(record.contextId, imported.contextId);
406409
break;
407410
}
408411
case "environment": {

0 commit comments

Comments
 (0)