Skip to content

Commit 1844a26

Browse files
committed
fix(web): preserve unlinked targets before duplicate detection
1 parent 60ce7e5 commit 1844a26

2 files changed

Lines changed: 67 additions & 60 deletions

File tree

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

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -198,54 +198,62 @@ it.each([
198198
);
199199
});
200200

201-
it("keeps a pasted thread the agent can't read as a link to its machine", async () => {
202-
const target = DraftId.make("clipboard-threads");
203-
const store = useComposerDraftStore.getState();
204-
using _draft = { [Symbol.dispose]: () => store.clearComposerContent(target) };
205-
const thread = (environmentId: string, threadId: string) => ({
206-
version: 1 as const,
207-
kind: "thread" as const,
208-
contextId: ComposerContextId.make(`thread_${threadId}`),
209-
label: `Thread ${threadId}`,
210-
environmentId: EnvironmentId.make(environmentId),
211-
threadId: ThreadId.make(threadId),
212-
title: `Thread ${threadId}`,
213-
});
214-
const unlinked = thread("unlinked-source", "remote");
215-
const linked = thread("linked-source", "linked");
216-
let importer!: Importer;
217-
await using _screen = await render(
218-
<Harness
219-
input={{
220-
environmentId: EnvironmentId.make("clipboard-target"),
221-
linkedEnvironments: new Map([[linked.environmentId, "Linked"]]),
222-
activeThread: undefined,
223-
activeThreadId: null,
224-
composerDraftTarget: target,
225-
attachmentDraftTarget: target,
226-
attachmentTargetKey: target,
227-
attachmentTargetKeyRef: { current: target },
228-
questionAttachmentTarget: null,
229-
pendingUserInputs: [],
230-
composerContextRecords: new Map(),
231-
composerImages: [],
232-
composerFilesRef: { current: [] },
233-
pendingThreadImportsRef: { current: new Map() },
234-
promptRef: { current: "" },
235-
addComposerFilesToDraft: (files) => store.addFiles(target, files),
236-
}}
237-
ready={(value) => {
238-
importer = value;
239-
}}
240-
/>,
241-
);
242-
let remapped!: ReturnType<Importer["importContextRecords"]>;
243-
await act(async () => {
244-
remapped = importer.importContextRecords([unlinked, linked], unlinked.environmentId);
245-
});
246-
expect(Object.fromEntries(remapped)).toEqual({
247-
[unlinked.contextId]: { markdown: "[Thread remote](t3-thread://v2/unlinked-source/remote)" },
248-
[linked.contextId]: linked.contextId,
249-
});
250-
expect(store.getComposerDraft(target)?.threadContexts).toEqual([linked]);
251-
});
201+
it.each([false, true])(
202+
"keeps an unreadable thread as a link (already attached: %s)",
203+
async (alreadyAttached) => {
204+
const target = DraftId.make("clipboard-threads");
205+
const store = useComposerDraftStore.getState();
206+
using _draft = { [Symbol.dispose]: () => store.clearComposerContent(target) };
207+
const thread = (environmentId: string, threadId: string) => ({
208+
version: 1 as const,
209+
kind: "thread" as const,
210+
contextId: ComposerContextId.make(`thread_${threadId}`),
211+
label: `Thread ${threadId}`,
212+
environmentId: EnvironmentId.make(environmentId),
213+
threadId: ThreadId.make(threadId),
214+
title: `Thread ${threadId}`,
215+
});
216+
const unlinked = thread("unlinked-source", "remote");
217+
const linked = thread("linked-source", "linked");
218+
if (alreadyAttached) store.addThreadContexts(target, [unlinked], { appendReference: false });
219+
let importer!: Importer;
220+
await using _screen = await render(
221+
<Harness
222+
input={{
223+
environmentId: EnvironmentId.make("clipboard-target"),
224+
linkedEnvironments: new Map([[linked.environmentId, "Linked"]]),
225+
activeThread: undefined,
226+
activeThreadId: null,
227+
composerDraftTarget: target,
228+
attachmentDraftTarget: target,
229+
attachmentTargetKey: target,
230+
attachmentTargetKeyRef: { current: target },
231+
questionAttachmentTarget: null,
232+
pendingUserInputs: [],
233+
composerContextRecords: new Map(
234+
alreadyAttached ? [[unlinked.contextId, { kind: "thread", record: unlinked }]] : [],
235+
),
236+
composerImages: [],
237+
composerFilesRef: { current: [] },
238+
pendingThreadImportsRef: { current: new Map() },
239+
promptRef: { current: "" },
240+
addComposerFilesToDraft: (files) => store.addFiles(target, files),
241+
}}
242+
ready={(value) => {
243+
importer = value;
244+
}}
245+
/>,
246+
);
247+
let remapped!: ReturnType<Importer["importContextRecords"]>;
248+
await act(async () => {
249+
remapped = importer.importContextRecords([unlinked, linked], unlinked.environmentId);
250+
});
251+
expect(Object.fromEntries(remapped)).toEqual({
252+
[unlinked.contextId]: { markdown: "[Thread remote](t3-thread://v2/unlinked-source/remote)" },
253+
[linked.contextId]: linked.contextId,
254+
});
255+
expect(store.getComposerDraft(target)?.threadContexts).toEqual(
256+
alreadyAttached ? [unlinked, linked] : [linked],
257+
);
258+
},
259+
);

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,14 @@ export function useComposerContextImport(input: {
313313
// re-minted under a fresh id so both survive the paste.
314314
const record = asKnownContextRecord(candidate);
315315
if (!record) continue;
316+
if (
317+
record.kind === "thread" &&
318+
record.environmentId !== environmentId &&
319+
!linkedEnvironments.has(record.environmentId)
320+
) {
321+
rewritten.set(record.contextId, { markdown: unreadableThreadContextLink(record) });
322+
continue;
323+
}
316324
// A reminted annotation needs its screenshot under the new annotation id.
317325
const existing = dependentAttachmentLocalIds.has(record.contextId)
318326
? undefined
@@ -397,15 +405,6 @@ export function useComposerContextImport(input: {
397405
break;
398406
}
399407
case "thread": {
400-
// The agent reads threads on its own server and the machines it links to; a pasted
401-
// thread from any other machine stays a link instead of an attached chip.
402-
if (
403-
record.environmentId !== environmentId &&
404-
!linkedEnvironments.has(record.environmentId)
405-
) {
406-
rewritten.set(record.contextId, { markdown: unreadableThreadContextLink(record) });
407-
break;
408-
}
409408
addComposerDraftThreadContexts(threadDraftTarget, [record], {
410409
appendReference: false,
411410
});

0 commit comments

Comments
 (0)