diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsx index 550e64aa63b69..053fd09b3e5f1 100644 --- a/apps/mobile/src/features/threads/ThreadFeed.tsx +++ b/apps/mobile/src/features/threads/ThreadFeed.tsx @@ -62,7 +62,11 @@ import { import type { ToolCallVisibility } from "@t3tools/contracts/settings"; import { renderAssistantCitationsAsText } from "@t3tools/shared/assistantCitations"; import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard"; -import { hostedThreadLink, parseThreadLinkHref } from "@t3tools/shared/threadLinks"; +import { + hostedThreadLink, + parseThreadLinkHref, + trimThreadAutolinkPadding, +} from "@t3tools/shared/threadLinks"; import { PROMPT_LINK_MAX_PROMPT_LENGTH } from "@t3tools/shared/promptLinks"; import { parseComposerContextHref, @@ -1202,7 +1206,10 @@ const AssistantMarkdownContent = memo(function AssistantMarkdownContent(props: { readonly skills?: ReadonlyArray | undefined; }) { const models = useModelMentions(props.environmentId, props.markdown); - const liveMarkdown = useLiveThreadLinkLabels(props.markdown, props.environmentId); + const liveMarkdown = useLiveThreadLinkLabels( + trimThreadAutolinkPadding(props.markdown), + props.environmentId, + ); const threadIcons = useThreadChipIcons(props.environmentId, liveMarkdown); const linkHandlers: MarkdownLinkHandlers = { ...props.linkHandlers, diff --git a/apps/web/src/components/chat/TimelineAssistantRows.tsx b/apps/web/src/components/chat/TimelineAssistantRows.tsx index 7928960dcb402..69a8240d066b5 100644 --- a/apps/web/src/components/chat/TimelineAssistantRows.tsx +++ b/apps/web/src/components/chat/TimelineAssistantRows.tsx @@ -1,4 +1,5 @@ import { shouldPreserveAssistantLineBreaks } from "@t3tools/shared/markdownPipeline"; +import { trimThreadAutolinkPadding } from "@t3tools/shared/threadLinks"; import { type RunId } from "@t3tools/contracts"; import { canForkProjectedAssistantItem } from "@t3tools/client-runtime/state/thread-workflows"; import { memo, use, useState } from "react"; @@ -33,7 +34,8 @@ export function AssistantTimelineRow({ row }: { row: Extract diff --git a/packages/shared/src/threadLinks.test.ts b/packages/shared/src/threadLinks.test.ts index c935c379a06be..5285214b8656d 100644 --- a/packages/shared/src/threadLinks.test.ts +++ b/packages/shared/src/threadLinks.test.ts @@ -10,6 +10,7 @@ import { relabelThreadLinks, threadLinkCandidates, threadLinkRefs, + trimThreadAutolinkPadding, } from "./threadLinks.ts"; const ref = (environmentId: string, threadId = "chat-1") => ({ @@ -151,6 +152,35 @@ describe("thread links", () => { ); }); + it("trims the whitespace a model writes inside a thread autolink's brackets, and nothing else", () => { + const delegated = + "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"; + expect(trimThreadAutolinkPadding(`Opened < ${delegated}>.`)).toBe(`Opened <${delegated}>.`); + expect( + trimThreadAutolinkPadding("<\tt3-thread://v1/t1 \t> and <\n t3-thread://v2/local/t2\n>"), + ).toBe(" and "); + expect( + trimThreadAutolinkPadding("[Settings](< t3-thread://v1/t1 >)\r\n<\r\nt3-thread://v1/t2\r\n>"), + ).toBe("[Settings]()\r\n"); + const untouched = [ + " [a]()", + "< t3 > < t3-thread://v2/env > < t3-thread://v2// > < t3-thread://v1/ >", + "< t3-thread://v2/local/thr ead > <\n\nt3-thread://v1/t1>", + '< https://example.com/t3-thread > x', + "Literal `< t3-thread://v2/local/t1 >`", + "Literal `code\n< t3-thread://v2/local/t1 >\ncode`", + " < t3-thread://v2/local/t1 >", + "\t< t3-thread://v2/local/t1 >", + "```md", + "< t3-thread://v2/local/t1 >", + "```", + ].join("\n"); + expect(trimThreadAutolinkPadding(untouched)).toBe(untouched); + expect( + relabelThreadLinks(trimThreadAutolinkPadding(`< ${delegated} >`), local, () => "Settings UI"), + ).toBe(`[Settings UI](${delegated})`); + }); + it("lists the threads a message's links can name, outside code", () => { expect( threadLinkRefs( diff --git a/packages/shared/src/threadLinks.ts b/packages/shared/src/threadLinks.ts index 275fa079bae74..e3a5c5eb1ac03 100644 --- a/packages/shared/src/threadLinks.ts +++ b/packages/shared/src/threadLinks.ts @@ -10,9 +10,10 @@ const V2_PREFIX = `${THREAD_LINK_PROTOCOL}://v2/`; const HOSTED_PREFIX = `${DEFAULT_HOSTED_APP_URL}/threads/`; const HOSTED_SEGMENT = "[A-Za-z0-9_%-]+"; const HOSTED_LINK = `${HOSTED_PREFIX.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&")}${HOSTED_SEGMENT}/${HOSTED_SEGMENT}`; -// Code comes first in the alternation so a link written inside a code span or fence is skipped. +// Code comes first in an alternation so a link written inside a code span or fence is skipped. +const CODE = String.raw`(?(\`{3,}|~{3,})[\s\S]*?(?:\2|(?![\s\S])))|(?(\`+)[\s\S]*?\4)`; const THREAD_LINK_OUTSIDE_CODE = new RegExp( - String.raw`(?(\`{3,}|~{3,})[\s\S]*?(?:\2|$))|(?(\`+)[^\n]*?\4)|\[(?