Skip to content

Commit 3cdc098

Browse files
fix(chat): trim padding around internal thread links
1 parent 9e09f60 commit 3cdc098

4 files changed

Lines changed: 60 additions & 5 deletions

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ import {
6262
import type { ToolCallVisibility } from "@t3tools/contracts/settings";
6363
import { renderAssistantCitationsAsText } from "@t3tools/shared/assistantCitations";
6464
import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard";
65-
import { hostedThreadLink, parseThreadLinkHref } from "@t3tools/shared/threadLinks";
65+
import {
66+
hostedThreadLink,
67+
parseThreadLinkHref,
68+
trimThreadAutolinkPadding,
69+
} from "@t3tools/shared/threadLinks";
6670
import { PROMPT_LINK_MAX_PROMPT_LENGTH } from "@t3tools/shared/promptLinks";
6771
import {
6872
parseComposerContextHref,
@@ -1202,7 +1206,10 @@ const AssistantMarkdownContent = memo(function AssistantMarkdownContent(props: {
12021206
readonly skills?: ReadonlyArray<SelectableMarkdownSkill> | undefined;
12031207
}) {
12041208
const models = useModelMentions(props.environmentId, props.markdown);
1205-
const liveMarkdown = useLiveThreadLinkLabels(props.markdown, props.environmentId);
1209+
const liveMarkdown = useLiveThreadLinkLabels(
1210+
trimThreadAutolinkPadding(props.markdown),
1211+
props.environmentId,
1212+
);
12061213
const threadIcons = useThreadChipIcons(props.environmentId, liveMarkdown);
12071214
const linkHandlers: MarkdownLinkHandlers = {
12081215
...props.linkHandlers,

‎apps/web/src/components/chat/TimelineAssistantRows.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { shouldPreserveAssistantLineBreaks } from "@t3tools/shared/markdownPipeline";
2+
import { trimThreadAutolinkPadding } from "@t3tools/shared/threadLinks";
23
import { type RunId } from "@t3tools/contracts";
34
import { canForkProjectedAssistantItem } from "@t3tools/client-runtime/state/thread-workflows";
45
import { memo, use, useState } from "react";
@@ -33,7 +34,8 @@ export function AssistantTimelineRow({ row }: { row: Extract<TimelineRow, { kind
3334
row.projectedItem?.item.id,
3435
);
3536
const text = liveText ?? row.message.text;
36-
const messageText = text || (row.message.streaming ? "" : "(empty response)");
37+
const messageText =
38+
trimThreadAutolinkPadding(text) || (row.message.streaming ? "" : "(empty response)");
3739

3840
return (
3941
<>

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
relabelThreadLinks,
1111
threadLinkCandidates,
1212
threadLinkRefs,
13+
trimThreadAutolinkPadding,
1314
} from "./threadLinks.ts";
1415

1516
const ref = (environmentId: string, threadId = "chat-1") => ({
@@ -151,6 +152,35 @@ describe("thread links", () => {
151152
);
152153
});
153154

155+
it("trims the whitespace a model writes inside a thread autolink's brackets, and nothing else", () => {
156+
const delegated =
157+
"t3-thread://v2/ffa70b7f-bbd5-47ff-9f01-bdad03fba727/thread:delegated-task:command%3Amcp%3A14ece488-0be2-4b67-bb57-7c2ee9190309%3Adelegate-task%3Azack-settings-ui-opus-20261107-2144";
158+
expect(trimThreadAutolinkPadding(`Opened < ${delegated}>.`)).toBe(`Opened <${delegated}>.`);
159+
expect(
160+
trimThreadAutolinkPadding("<\tt3-thread://v1/t1 \t> and <\n t3-thread://v2/local/t2\n>"),
161+
).toBe("<t3-thread://v1/t1> and <t3-thread://v2/local/t2>");
162+
expect(
163+
trimThreadAutolinkPadding("[Settings](< t3-thread://v1/t1 >)\r\n<\r\nt3-thread://v1/t2\r\n>"),
164+
).toBe("[Settings](<t3-thread://v1/t1>)\r\n<t3-thread://v1/t2>");
165+
const untouched = [
166+
"<t3-thread://v2/local/t1> [a](<t3-thread://v2/local/t1>)",
167+
"< t3 > < t3-thread://v2/env > < t3-thread://v2/<env>/<thread> > < t3-thread://v1/ >",
168+
"< t3-thread://v2/local/thr ead > <\n\nt3-thread://v1/t1>",
169+
'< https://example.com/t3-thread > <a href="t3-thread://v2/local/t1">x</a>',
170+
"Literal `< t3-thread://v2/local/t1 >`",
171+
"Literal `code\n< t3-thread://v2/local/t1 >\ncode`",
172+
" < t3-thread://v2/local/t1 >",
173+
"\t< t3-thread://v2/local/t1 >",
174+
"```md",
175+
"< t3-thread://v2/local/t1 >",
176+
"```",
177+
].join("\n");
178+
expect(trimThreadAutolinkPadding(untouched)).toBe(untouched);
179+
expect(
180+
relabelThreadLinks(trimThreadAutolinkPadding(`< ${delegated} >`), local, () => "Settings UI"),
181+
).toBe(`[Settings UI](${delegated})`);
182+
});
183+
154184
it("lists the threads a message's links can name, outside code", () => {
155185
expect(
156186
threadLinkRefs(

‎packages/shared/src/threadLinks.ts‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ const V2_PREFIX = `${THREAD_LINK_PROTOCOL}://v2/`;
1010
const HOSTED_PREFIX = `${DEFAULT_HOSTED_APP_URL}/threads/`;
1111
const HOSTED_SEGMENT = "[A-Za-z0-9_%-]+";
1212
const HOSTED_LINK = `${HOSTED_PREFIX.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&")}${HOSTED_SEGMENT}/${HOSTED_SEGMENT}`;
13-
// Code comes first in the alternation so a link written inside a code span or fence is skipped.
13+
// Code comes first in an alternation so a link written inside a code span or fence is skipped.
14+
const CODE = String.raw`(?<fence>(\`{3,}|~{3,})[\s\S]*?(?:\2|(?![\s\S])))|(?<span>(\`+)[\s\S]*?\4)`;
1415
const THREAD_LINK_OUTSIDE_CODE = new RegExp(
15-
String.raw`(?<fence>(\`{3,}|~{3,})[\s\S]*?(?:\2|$))|(?<span>(\`+)[^\n]*?\4)|\[(?<label>[^\]\n]*)\]\(<?(?<href>t3-thread:\/\/v[12]\/[^\s>)]+|${HOSTED_LINK})>?\)|<(?<autolink>t3-thread:\/\/v[12]\/[^\s<>]+|${HOSTED_LINK})>|(?<bare>${HOSTED_LINK})(?![\w%/?#-])`,
16+
String.raw`${CODE}|\[(?<label>[^\]\n]*)\]\(<?(?<href>t3-thread:\/\/v[12]\/[^\s>)]+|${HOSTED_LINK})>?\)|<(?<autolink>t3-thread:\/\/v[12]\/[^\s<>]+|${HOSTED_LINK})>|(?<bare>${HOSTED_LINK})(?![\w%/?#-])`,
1617
"g",
1718
);
1819

@@ -139,6 +140,21 @@ export function formatHostedThreadLink(
139140
return cleaned ? `[${cleaned}](${href})` : href;
140141
}
141142

143+
// At most one line break on a side, so a blank line never joins two paragraphs.
144+
const PADDED_THREAD_AUTOLINK = new RegExp(
145+
String.raw`${CODE}|^(?: {4}|\t)[^\n]*|<[ \t]*\r?\n?[ \t]*(?<autolink>t3-thread:\/\/v[12]\/[^\s<>]+)[ \t]*\r?\n?[ \t]*>`,
146+
"gm",
147+
);
148+
149+
/** Models sometimes pad an autolink, as in `< t3-thread://… >`, which Markdown leaves as text. */
150+
export function trimThreadAutolinkPadding(markdown: string): string {
151+
if (!markdown.includes(`${THREAD_LINK_PROTOCOL}://v`)) return markdown;
152+
return markdown.replace(PADDED_THREAD_AUTOLINK, (source, ...args) => {
153+
const href = (args.at(-1) as { autolink?: string }).autolink;
154+
return href !== undefined && parseThreadLinkHref(href) ? `<${href}>` : source;
155+
});
156+
}
157+
142158
export function hasThreadLinks(markdown: string): boolean {
143159
return markdown.includes(`${THREAD_LINK_PROTOCOL}://v`) || markdown.includes(HOSTED_PREFIX);
144160
}

0 commit comments

Comments
 (0)