From 87b396d2dad1fd222e718d4ebdfad1b082c5dde5 Mon Sep 17 00:00:00 2001 From: "opencode-agent[bot]" <219766164+opencode-agent[bot]@users.noreply.github.com> Date: Tue, 20 Oct 2026 00:28:21 +0200 Subject: [PATCH] fix(app): make links in user messages clickable (#57211) Co-authored-by: neriousy <34747899+neriousy@users.noreply.github.com> --- .../session-message-projection.spec.ts | 14 ++++++ .../src/components/message-part.css | 11 +++++ .../src/message/message-content.tsx | 46 +++++++++++++++---- .../src/timeline/timeline-row.stories.tsx | 38 ++++++++++++++- 4 files changed, 99 insertions(+), 10 deletions(-) diff --git a/packages/session-ui/component-tests/session-message-projection.spec.ts b/packages/session-ui/component-tests/session-message-projection.spec.ts index 111281fe8aee..deadea368ea4 100644 --- a/packages/session-ui/component-tests/session-message-projection.spec.ts +++ b/packages/session-ui/component-tests/session-message-projection.spec.ts @@ -87,3 +87,17 @@ story("renders user image, file attachment, file reference, and agent reference" expect(bodyBox).not.toBeNull() expect(bodyBox!.y - (attachmentsBox!.y + attachmentsBox!.height)).toBe(8) }) + +story("links URLs in user messages without swallowing punctuation or mentions", async ({ mount }) => { + const timeline = await mount("current-session-timeline-rows--conversation", { args: { scenario: "links" } }) + const links = timeline.locator('[data-slot="user-message-text"] a[data-slot="user-message-link"]') + await expect(links).toHaveCount(2) + await expect(links.nth(0)).toHaveText("https://github.com/Hona/mesh") + await expect(links.nth(0)).toHaveAttribute("href", "https://github.com/Hona/mesh") + await expect(links.nth(0)).toHaveAttribute("target", "_blank") + await expect(links.nth(1)).toHaveText("https://opencode.ai/docs") + await expect(timeline.getByText("@src/a.ts", { exact: true })).toBeVisible() + await expect(timeline.locator('[data-slot="user-message-text"]')).toHaveText( + "set it up on this pc https://github.com/Hona/mesh, then compare @src/a.ts with (https://opencode.ai/docs).", + ) +}) diff --git a/packages/session-ui/src/components/message-part.css b/packages/session-ui/src/components/message-part.css index bc25361b052c..c80c4af962b0 100644 --- a/packages/session-ui/src/components/message-part.css +++ b/packages/session-ui/src/components/message-part.css @@ -195,6 +195,17 @@ color: var(--v2-blue-500); } + [data-slot="user-message-link"] { + color: inherit; + text-decoration: underline; + text-decoration-color: color-mix(in oklch, currentColor 40%, transparent); + text-underline-offset: 2px; + + &:hover { + text-decoration-color: currentColor; + } + } + max-width: 100%; } diff --git a/packages/session-ui/src/message/message-content.tsx b/packages/session-ui/src/message/message-content.tsx index bb0bc8698154..fdb4d5a99bff 100644 --- a/packages/session-ui/src/message/message-content.tsx +++ b/packages/session-ui/src/message/message-content.tsx @@ -533,16 +533,33 @@ function CurrentHighlightedText(props: { ].sort((a, b) => a.start - b.start) const result: HighlightSegment[] = [] + // Plain text between references may contain URLs, which become links. + const pushText = (text: string) => { + let last = 0 + + for (const match of text.matchAll(urlPattern)) { + const href = match[0].replace(/[),.;:!?\]]+$/, "") + + if (!URL.canParse(href)) continue + + if (match.index > last) result.push({ text: text.slice(last, match.index) }) + result.push({ text: href, type: "link" }) + last = match.index + href.length + } + + if (last < text.length) result.push({ text: text.slice(last) }) + } + let last = 0 references.forEach((reference) => { if (reference.start < last) return - if (reference.start > last) result.push({ text: props.text.slice(last, reference.start) }) + if (reference.start > last) pushText(props.text.slice(last, reference.start)) result.push({ text: props.text.slice(reference.start, reference.end), type: reference.type }) last = reference.end }) - if (last < props.text.length) result.push({ text: props.text.slice(last) }) + if (last < props.text.length) pushText(props.text.slice(last)) return result }) @@ -550,18 +567,29 @@ function CurrentHighlightedText(props: { return ( {(segment) => ( - - - @ - {segment.text.slice(1)} - - + + + @ + {segment.text.slice(1)} + + + } + > + + {segment.text} + + )} ) } -type HighlightSegment = { text: string; type?: "file" | "agent" } +const urlPattern = /https?:\/\/[^\s<>"'`]+/g + +type HighlightSegment = { text: string; type?: "file" | "agent" | "link" } /** A compaction the server admitted but has not started, drawn as the divider a started one opens with. */ export function SessionCompactionQueued() { diff --git a/packages/session-ui/src/timeline/timeline-row.stories.tsx b/packages/session-ui/src/timeline/timeline-row.stories.tsx index 2fc3d2572045..83e6a81f4437 100644 --- a/packages/session-ui/src/timeline/timeline-row.stories.tsx +++ b/packages/session-ui/src/timeline/timeline-row.stories.tsx @@ -207,7 +207,10 @@ function RetryAndRecoverStory() { ? { attempt: 2, at: 1_900_000_000_000, - error: { type: "ProviderRateLimitError" as const, message: "Rate limit reached. Retrying with backoff." }, + error: { + type: "ProviderRateLimitError" as const, + message: "Rate limit reached. Retrying with backoff.", + }, } : undefined, time: { created: STORY_TIME, completed: finished ? STORY_TIME + 300 : undefined }, @@ -617,6 +620,38 @@ const RichUserAttachments = { ), } +const UserMessageLinks = { + render: () => ( + + ), +} + const conversationScenarios = { reasoning: AgentReasoning, hidden: WorkingWithoutReasoningDetails, @@ -627,6 +662,7 @@ const conversationScenarios = { interruption: InterruptedTurn, models: AliasedModelNotices, attachments: RichUserAttachments, + links: UserMessageLinks, } export const Conversation = {