Skip to content

Commit 4c4c913

Browse files
shivamhwpclaude
andcommitted
fix(clients): fall back to the title when a thread link can't be encoded
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 52b7265 commit 4c4c913

4 files changed

Lines changed: 70 additions & 74 deletions

File tree

‎apps/mobile/src/lib/composerContextClipboard.test.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,6 @@ describe("mobile context clipboard imports", () => {
235235
expect(result!.failures).toEqual([]);
236236
expect(result!.skippedThreads).toEqual([foreignThread.title]);
237237
const references = collectComposerContextReferences(result!.text);
238-
expect(references.map((reference) => reference.label)).toEqual(
239-
[localThread, terminal, image].map((record) => record.label),
240-
);
241238
expect(result!.context.records.map((record) => record.contextId)).toEqual(
242239
references.map((reference) => reference.contextId),
243240
);

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

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

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]: {
253-
markdown: "[Thread remote](t3-thread://v2/unlinked-source/remote)",
254-
},
255-
[linked.contextId]: linked.contextId,
256-
});
257-
expect(store.getComposerDraft(target)?.threadContexts).toEqual(
258-
alreadyAttached ? [unlinked, linked] : [linked],
259-
);
260-
},
261-
);
201+
it("keeps an unreadable thread as a link even when the draft already attaches it", 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+
store.addThreadContexts(target, [unlinked], { appendReference: false });
217+
let importer!: Importer;
218+
await using _screen = await render(
219+
<Harness
220+
input={{
221+
environmentId: EnvironmentId.make("clipboard-target"),
222+
linkedEnvironments: new Map([[linked.environmentId, "Linked"]]),
223+
activeThread: undefined,
224+
activeThreadId: null,
225+
composerDraftTarget: target,
226+
attachmentDraftTarget: target,
227+
attachmentTargetKey: target,
228+
attachmentTargetKeyRef: { current: target },
229+
questionAttachmentTarget: null,
230+
pendingUserInputs: [],
231+
composerContextRecords: new Map([
232+
[unlinked.contextId, { kind: "thread", record: unlinked }],
233+
]),
234+
composerImages: [],
235+
composerFilesRef: { current: [] },
236+
pendingThreadImportsRef: { current: new Map() },
237+
promptRef: { current: "" },
238+
addComposerFilesToDraft: (files) => store.addFiles(target, files),
239+
}}
240+
ready={(value) => {
241+
importer = value;
242+
}}
243+
/>,
244+
);
245+
let remapped!: ReturnType<Importer["importContextRecords"]>;
246+
await act(async () => {
247+
remapped = importer.importContextRecords([unlinked, linked], unlinked.environmentId);
248+
});
249+
expect(Object.fromEntries(remapped)).toEqual({
250+
[unlinked.contextId]: {
251+
markdown: "[Thread remote](t3-thread://v2/unlinked-source/remote)",
252+
},
253+
[linked.contextId]: linked.contextId,
254+
});
255+
expect(store.getComposerDraft(target)?.threadContexts).toEqual([unlinked, linked]);
256+
});

‎packages/shared/src/composerContextReferences.test.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -526,17 +526,17 @@ describe("threadLinkContextReferences", () => {
526526
["env-1", "a b", true],
527527
["env-1", "a>b", true],
528528
["env-1", "a%29b", true],
529-
["env-1", ".", true],
530529
["env-1", "..", true],
531530
["source/machine", "t1", true],
532531
// No link form carries both: a hosted URL drops the dot segment.
533532
[".", "a b", false],
534533
] as const) {
535534
const record = threadContextRecord(thread(environmentId, threadId), "Copied title");
536-
expect(
537-
threadLinkContextReferences(unreadableThreadContextLink(record), [], () => undefined)
538-
.records,
539-
).toEqual(carried ? [record] : []);
535+
const link = unreadableThreadContextLink(record);
536+
if (!carried) expect(link).toBe("Copied title");
537+
expect(threadLinkContextReferences(link, [], () => undefined).records).toEqual(
538+
carried ? [record] : [],
539+
);
540540
}
541541
expect(
542542
threadLinkContextReferences("https://app.t3.codes/threads/env-9/t9", [], () => undefined)

‎packages/shared/src/composerContextReferences.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,15 @@ export function unreadableThreadContextLink(record: ThreadContextRecord): string
445445
});
446446
return links === 1 && exact;
447447
};
448-
return (
449-
[formatThreadLink(record, record.title), formatHostedThreadLink(record, record.title)].find(
450-
namesRecord,
451-
) ?? record.title
452-
);
448+
const link = formatThreadLink(record, record.title);
449+
if (namesRecord(link)) return link;
450+
try {
451+
const hosted = formatHostedThreadLink(record, record.title);
452+
if (namesRecord(hosted)) return hosted;
453+
} catch {
454+
// A lone surrogate can't be URI-encoded.
455+
}
456+
return record.title;
453457
}
454458

455459
export function remapComposerContextAttachments(

0 commit comments

Comments
 (0)