Skip to content

The agent that grows with you

fix(tui): bare URLs render verbatim instead of a derived site label (#106843)

Siddharth Balyanae43fd6df6c1
diff --git a/ui-tui/src/__tests__/markdown.test.ts b/ui-tui/src/__tests__/markdown.test.ts
index 9c4f0eaed6d..89b611c9fda 100644
--- a/ui-tui/src/__tests__/markdown.test.ts
+++ b/ui-tui/src/__tests__/markdown.test.ts
@@ -37,7 +37,9 @@ const ESC = String.fromCharCode(27)
const CSI_RE = new RegExp(`${ESC}\\[[0-?]*[ -/]*[@-~]`, 'g')
const OSC_RE = new RegExp(`${ESC}\\][\\s\\S]*?(?:${BEL}|${ESC}\\\\)`, 'g')
-const renderPlain = (node: React.ReactNode) => {
+// The escape stream exactly as it reaches the terminal, OSC sequences and
+// all — the only view that can prove an OSC 8 hyperlink was emitted.
+const renderAnsi = (node: React.ReactNode) => {
const stdout = new PassThrough()
const stdin = new PassThrough()
const stderr = new PassThrough()
@@ -61,10 +63,13 @@ const renderPlain = (node: React.ReactNode) => {
instance.cleanup()
return output
+}
+
+const renderPlain = (node: React.ReactNode) =>
+ renderAnsi(node)
.replace(OSC_RE, '')
.split('\n')
.map(line => stripAnsi(line).replace(CSI_RE, '').trimEnd())
-}
describe('INLINE_RE emphasis', () => {
it('matches word-boundary italic/bold', () => {
@@ -271,55 +276,97 @@ describe('Md wrapping', () => {
})
describe('Md link labels', () => {
- it('renders bare URLs with readable slug labels', () => {
- const lines = renderPlain(
- React.createElement(
- Box,
- { width: 120 },
- React.createElement(Md, {
- t: DEFAULT_THEME,
- text: 'see https://www.expedia.com/things-to-do/puerto-rico-el-yunque-rainforest-adventure for details'
- })
- )
- )
+ const md = (text: string, width = 200) =>
+ React.createElement(Box, { width }, React.createElement(Md, { cols: width, t: DEFAULT_THEME, text }))
- const rendered = lines.join('\n')
+ // The link target has to survive as literal text, not just as OSC 8
+ // metadata: a bare URL that renders as a site name leaves nothing to read,
+ // copy or retype on any terminal that strips the escape.
+ it('renders a bare URL verbatim instead of a derived label', () => {
+ const url = 'https://connect.example.com/link/lk_9f2c1d7e'
+ const rendered = renderPlain(md(`see ${url} for details`)).join('\n')
- expect(rendered).toContain('Puerto Rico El Yunque Rainforest Adventure')
- expect(rendered).not.toContain('https://www.expedia.com/things-to-do/puerto-rico-el-yunque-rainforest-adventure')
+ expect(rendered).toContain(url)
+ // `urlSlugTitleLabel` used to turn the last path segment into this.
+ expect(rendered).not.toContain('Lk 9f2c1d7e')
})
- it('keeps the authored markdown label even when a page title resolves', async () => {
- const url = 'https://www.expedia.com/things-to-do/puerto-rico-el-yunque-rainforest-adventure'
+ // Regression for the connect-link handoff: the stored assistant text is
+ // `Connect link: <url>` and the TUI showed `Connect link: Composio`, so the
+ // OAuth step could not be completed from the TUI at all.
+ it('keeps the URL visible in a "Connect link:" message', () => {
+ const url = 'https://connect.example.com/link/lk_a1b2c3d4e5'
+ const rendered = renderPlain(md(`Connect link: ${url}`)).join('\n')
- // Warm the shared cache so `useLinkTitle` would have a title to render
- // synchronously — the label must still win.
- await stubFetchedTitle(url, 'El Yunque Rainforest Adventure | Expedia')
+ expect(rendered).toContain(`Connect link: ${url}`)
+ })
- const lines = renderPlain(
- React.createElement(
- Box,
- { width: 80 },
- React.createElement(Md, { t: DEFAULT_THEME, text: `[Trip details](${url})` })
- )
- )
+ it('wraps a bare URL in an OSC 8 hyperlink pointing at the same target', () => {
+ const url = 'https://connect.example.com/link/lk_9f2c1d7e'
+ const ansi = renderAnsi(md(`Connect link: ${url}`))
- const rendered = lines.join('\n')
+ expect(ansi).toContain(`;${url}${BEL}`)
+ expect(ansi).toContain(`${ESC}]8;`)
+ })
- expect(rendered).toContain('Trip details')
- expect(rendered).not.toContain('El Yunque Rainforest Adventure | Expedia')
+ it('leaves trailing prose punctuation outside the visible URL', () => {
+ const url = 'https://docs.example.com/guide/auth'
+ const rendered = renderPlain(md(`open ${url}, then retry`)).join('\n')
+
+ expect(rendered).toContain(`open ${url}, then retry`)
})
- it('still resolves titles for links whose label is just the URL', async () => {
- const url = 'https://www.expedia.com/things-to-do/puerto-rico-el-yunque-rainforest-adventure'
+ it('renders an autolink verbatim', () => {
+ const url = 'https://docs.example.com/guide/auth'
+ const rendered = renderPlain(md(`see <${url}>`)).join('\n')
- await stubFetchedTitle(url, 'Rainforest Adventure Tour')
+ expect(rendered).toContain(url)
+ })
- const lines = renderPlain(
- React.createElement(Box, { width: 120 }, React.createElement(Md, { t: DEFAULT_THEME, text: `[${url}](${url})` }))
- )
+ it('keeps an authored markdown label and carries the target in OSC 8', () => {
+ const url = 'https://docs.example.com/guide/auth'
+ const ansi = renderAnsi(md(`[Trip details](${url})`))
+
+ expect(stripAnsi(ansi.replace(OSC_RE, ''))).toContain('Trip details')
+ expect(ansi).toContain(`;${url}${BEL}`)
+ })
+
+ it('never lets a fetched page title replace the URL', async () => {
+ const url = 'https://connect.example.com/link/lk_9f2c1d7e'
+
+ // Warm the shared title cache, then prove the renderer ignores it. This
+ // is the exact shape of the live defect: the fetched title was the only
+ // thing on screen.
+ await stubFetchedTitle(url, 'Connect your account')
+
+ const rendered = renderPlain(md(`Connect link: ${url}`)).join('\n')
+
+ expect(rendered).toContain(url)
+ expect(rendered).not.toContain('Connect your account')
+ })
+
+ it('renders a URL-labelled markdown link as the URL', async () => {
+ const url = 'https://docs.example.com/guide/auth'
+
+ await stubFetchedTitle(url, 'Auth Guide')
+
+ const rendered = renderPlain(md(`[${url}](${url})`)).join('\n')
+
+ expect(rendered).toContain(url)
+ expect(rendered).not.toContain('Auth Guide')
+ })
+
+ it('falls back to the URL when the markdown label is blank', () => {
+ const url = 'https://docs.example.com/guide/auth'
+ const rendered = renderPlain(md(`[ ](${url})`)).join('\n')
+
+ expect(rendered).toContain(url)
+ })
+
+ it('renders a mailto autolink as the address', () => {
+ const rendered = renderPlain(md('write <[email protected]> today')).join('\n')
- expect(lines.join('\n')).toContain('Rainforest Adventure Tour')
+ expect(rendered).toContain('write [email protected] today')
})
})
diff --git a/ui-tui/src/components/markdown.tsx b/ui-tui/src/components/markdown.tsx
index 2aa421822b4..1912bca2511 100644
--- a/ui-tui/src/components/markdown.tsx
+++ b/ui-tui/src/components/markdown.tsx
@@ -2,7 +2,7 @@ import { Box, Link, stringWidth, Text } from '@hermes/ink'
import { Fragment, memo, type ReactNode, useMemo } from 'react'
import { ensureEmojiPresentation } from '../lib/emoji.js'
-import { normalizeExternalUrl, urlSlugTitleLabel, useLinkTitle } from '../lib/externalLink.js'
+import { normalizeExternalUrl } from '../lib/externalLink.js'
import { BOX_CLOSE, BOX_OPEN, texToUnicode } from '../lib/mathUnicode.js'
import { highlightLine, isHighlightable } from '../lib/syntax.js'
import type { Theme } from '../theme.js'
@@ -150,46 +150,34 @@ const isTableDivider = (row: string) => {
const autolinkUrl = (raw: string) =>
raw.startsWith('mailto:') || raw.startsWith('http') || !raw.includes('@') ? raw : `mailto:${raw}`
-const defaultLinkLabel = (url: string) =>
- url.startsWith('mailto:') ? url.replace(/^mailto:/, '') : /^https?:\/\//i.test(url) ? urlSlugTitleLabel(url) : url
-
-// A label only counts as authored if it says something the URL doesn't:
-// `[https://example.com](https://example.com)` and `<https://example.com>`
-// are bare links wearing markdown syntax, so they still want a page title.
-const pickAuthoredLabel = (label: string | undefined, target: string): string | undefined => {
- const trimmed = label?.trim()
-
- return trimmed && normalizeExternalUrl(trimmed) !== target ? trimmed : undefined
-}
-
-interface ResolvedLinkProps {
- authoredLabel?: string
- t: Theme
- url: string
-}
-
-// Title resolution is a fallback for links with no text of their own, not an
-// override — replacing `[Read the RFC](url)` with the page title throws away
-// better wording than we can derive, and mangles labels like `#71706`.
-function ResolvedLink({ authoredLabel, t, url }: ResolvedLinkProps) {
- const fetched = useLinkTitle(authoredLabel ? null : url)
- const display = authoredLabel || fetched || defaultLinkLabel(url)
+// A bare URL renders as itself. The target IS the message for connect links,
+// one-time tokens and signed URLs, so a derived slug label or a fetched page
+// title ("Composio") hides the only string the reader has to copy — and a
+// terminal that ignores OSC 8 leaves nothing behind at all. `mailto:` is the
+// one scheme whose useful text is the address, not the URL.
+const urlAsText = (url: string) => (url.startsWith('mailto:') ? url.replace(/^mailto:/, '') : url)
+
+// An authored markdown label may stand in for the URL, because the OSC 8
+// wrapper below keeps the target reachable by click. A blank label
+// (`[](url)`) says nothing, so the URL itself becomes the text.
+const authoredLabel = (label: string | undefined): string | undefined => label?.trim() || undefined
+
+// `Link` emits the OSC 8 hyperlink unconditionally and the renderer also
+// records it per cell, so a label never strands its target: terminals that
+// speak OSC 8 make it clickable, and the in-process click dispatcher covers
+// the ones that don't.
+const renderLink = (k: number, t: Theme, rawUrl: string, label?: string) => {
+ const target = normalizeExternalUrl(rawUrl)
return (
- <Link url={url}>
+ <Link key={k} url={target}>
<Text color={t.color.accent} underline>
- {display}
+ {authoredLabel(label) ?? urlAsText(target)}
</Text>
</Link>
)
}
-const renderResolvedLink = (k: number, t: Theme, rawUrl: string, label?: string) => {
- const target = normalizeExternalUrl(rawUrl)
-
- return <ResolvedLink authoredLabel={pickAuthoredLabel(label, target)} key={k} t={t} url={target} />
-}
-
export const stripInlineMarkup = (v: string) =>
v
.replace(/!\[(.*?)\]\(((?:[^\s()]|\([^\s()]*\))+?)\)/g, '[image: $1] $2')
@@ -568,9 +556,9 @@ function MdInline({ color, t, text }: { color?: string; t: Theme; text: string }
</Text>
)
} else if (m[3] && m[4]) {
- parts.push(renderResolvedLink(parts.length, t, m[4], m[3]))
+ parts.push(renderLink(parts.length, t, m[4], m[3]))
} else if (m[5]) {
- parts.push(renderResolvedLink(parts.length, t, autolinkUrl(m[5]), m[5].replace(/^mailto:/, '')))
+ parts.push(renderLink(parts.length, t, autolinkUrl(m[5]), m[5].replace(/^mailto:/, '')))
} else if (m[6]) {
parts.push(
<Text key={parts.length} strikethrough>
@@ -632,7 +620,7 @@ function MdInline({ color, t, text }: { color?: string; t: Theme; text: string }
// so `see https://x.com/, which…` keeps the comma outside the link.
const url = m[16].replace(/[),.;:!?]+$/g, '')
- parts.push(renderResolvedLink(parts.length, t, url))
+ parts.push(renderLink(parts.length, t, url))
if (url.length < m[16].length) {
parts.push(<Text key={parts.length}>{m[16].slice(url.length)}</Text>)