Skip to content

Commit 6396651

Browse files
committed
fix(mobile): resolve bare thread links in messages
1 parent 5211acf commit 6396651

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ describe("thread links", () => {
100100
title,
101101
),
102102
).toBe("[There](t3-thread://v2/there/t1) and [Here](t3-thread://v2/local/t1)");
103+
const bare =
104+
"One agent is working: t3-thread://v2/there/thread:delegated-task:command%3Amcp%3A1";
105+
expect(threadLinkRefs(bare, local)).toEqual([
106+
ref("there", "thread:delegated-task:command%3Amcp%3A1"),
107+
ref("there", "thread:delegated-task:command:mcp:1"),
108+
]);
109+
expect(relabelThreadLinks(bare, local, () => "Convert settings to TypeScript")).toBe(
110+
"One agent is working: [Convert settings to TypeScript](t3-thread://v2/there/thread:delegated-task:command%3Amcp%3A1)",
111+
);
112+
expect(
113+
relabelThreadLinks("See (t3-thread://v1/t1), then t3-thread://v2/there/t1.", local, title),
114+
).toBe("See ([Here](t3-thread://v1/t1)), then [There](t3-thread://v2/there/t1).");
103115
});
104116

105117
it("turns a v2 link it cannot resolve into plain text, never the raw link", () => {
@@ -113,6 +125,9 @@ describe("thread links", () => {
113125
expect(relabelThreadLinks("[Old](t3-thread://v1/t1)", local, () => undefined)).toBe(
114126
"[Old](t3-thread://v1/t1)",
115127
);
128+
expect(relabelThreadLinks("t3-thread://v2/there/t1", local, () => undefined)).toBe(
129+
"Thread on another machine",
130+
);
116131
});
117132

118133
it("leaves links inside code spans and fences as written", () => {
@@ -122,6 +137,7 @@ describe("thread links", () => {
122137
"<t3-thread://v2/local/t1>",
123138
"```",
124139
"After <t3-thread://v2/local/t1>",
140+
"Literal `t3-thread://v2/local/t1`",
125141
].join("\n");
126142
expect(relabelThreadLinks(markdown, local, () => "New")).toBe(
127143
[
@@ -130,6 +146,7 @@ describe("thread links", () => {
130146
"<t3-thread://v2/local/t1>",
131147
"```",
132148
"After [New](t3-thread://v2/local/t1)",
149+
"Literal `t3-thread://v2/local/t1`",
133150
].join("\n"),
134151
);
135152
});

‎packages/shared/src/threadLinks.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const V1_PREFIX = `${THREAD_LINK_PROTOCOL}://v1/`;
77
const V2_PREFIX = `${THREAD_LINK_PROTOCOL}://v2/`;
88
// Code comes first in the alternation so a link written inside a code span or fence is skipped.
99
const THREAD_LINK_OUTSIDE_CODE =
10-
/(?<fence>(`{3,}|~{3,})[\s\S]*?(?:\2|$))|(?<span>(`+)[^\n]*?\4)|\[(?<label>[^\]\n]*)\]\((?<href>t3-thread:\/\/v[12]\/[^\s)]+)\)|<(?<autolink>t3-thread:\/\/v[12]\/[^\s<>]+)>/g;
10+
/(?<fence>(`{3,}|~{3,})[\s\S]*?(?:\2|$))|(?<span>(`+)[^\n]*?\4)|\[(?<label>[^\]\n]*)\]\((?<href>t3-thread:\/\/v[12]\/[^\s)]+)\)|<(?<autolink>t3-thread:\/\/v[12]\/[^\s<>]+)>|(?<![\w/])(?<bare>t3-thread:\/\/v[12]\/[^\s<>`\])]*[^\s<>`\]).,!?;:])/g;
1111

1212
const decodeThreadId = Schema.decodeUnknownOption(ThreadId);
1313
const decodeEnvironmentId = Schema.decodeUnknownOption(EnvironmentId);
@@ -98,7 +98,7 @@ export function threadLinkRefs(
9898
): ReadonlyArray<ScopedThreadRef> {
9999
const refs = new Map<string, ScopedThreadRef>();
100100
for (const match of markdown.matchAll(THREAD_LINK_OUTSIDE_CODE)) {
101-
const href = match.groups?.href ?? match.groups?.autolink;
101+
const href = match.groups?.href ?? match.groups?.autolink ?? match.groups?.bare;
102102
const link = href === undefined ? null : parseThreadLinkHref(href);
103103
if (link === null) continue;
104104
for (const ref of threadLinkCandidates(link, messageEnvironmentId)) {
@@ -128,8 +128,13 @@ export function relabelThreadLinks(
128128
): string {
129129
if (!hasThreadLinks(markdown)) return markdown;
130130
return markdown.replace(THREAD_LINK_OUTSIDE_CODE, (source, ...args) => {
131-
const groups = args.at(-1) as { href?: string; autolink?: string; label?: string };
132-
const href = groups.href ?? groups.autolink;
131+
const groups = args.at(-1) as {
132+
href?: string;
133+
autolink?: string;
134+
bare?: string;
135+
label?: string;
136+
};
137+
const href = groups.href ?? groups.autolink ?? groups.bare;
133138
if (href === undefined) return source;
134139
const written = parseThreadLinkHref(href);
135140
if (written === null) return source;

0 commit comments

Comments
 (0)