Skip to content

The agent that grows with you

fix(desktop): share URL host validation

Denis H5c5138337972
diff --git a/apps/desktop/src/app/chat/composer/url-refs.ts b/apps/desktop/src/app/chat/composer/url-refs.ts
index a292412bafe..bff92a98a2b 100644
--- a/apps/desktop/src/app/chat/composer/url-refs.ts
+++ b/apps/desktop/src/app/chat/composer/url-refs.ts
@@ -134,8 +134,8 @@ function splitUrlTail(raw: string) {
return { trailing: raw.slice(url.length), url }
}
-/** A URL needs a host past the scheme to be worth chipping. */
-const hasHost = (url: string) => /^https?:\/\/[^/\s]/i.test(url)
+/** A URL needs a host past the scheme to be worth treating as a URL reference. */
+export const hasHttpUrlHost = (url: string) => /^https?:\/\/[^/\s]/i.test(splitUrlTail(url).url)
/** Rewrite bare links in `text` as `@url:` directives, leaving links that are
* already part of a directive alone. Returns `text` unchanged when there are
@@ -161,7 +161,7 @@ export function linkifyUrls(text: string) {
const start = match.index ?? 0
const { url } = splitUrlTail(match[0])
- if (!hasHost(url) || containsIndex(protectedRanges, start)) {
+ if (!hasHttpUrlHost(url) || containsIndex(protectedRanges, start)) {
continue
}
@@ -188,7 +188,7 @@ export function resolveExactLinkPaste(raw: string): string | null {
const { trailing, url } = splitUrlTail(unwrapped)
// Trailing sentence punctuation means the user copied prose, not a link.
- if (trailing || !hasHost(url)) {
+ if (trailing || !hasHttpUrlHost(url)) {
return null
}
@@ -270,7 +270,7 @@ export function chipTypedUrlOnSpace(event: KeyboardEvent<HTMLDivElement>) {
const { trailing, url } = splitUrlTail(token)
- if (!hasHost(url)) {
+ if (!hasHttpUrlHost(url)) {
return false
}
diff --git a/apps/desktop/src/components/chat/shiki-highlighter.test.ts b/apps/desktop/src/components/chat/shiki-highlighter.test.ts
index 1bd91e5261e..81198cbb8c6 100644
--- a/apps/desktop/src/components/chat/shiki-highlighter.test.ts
+++ b/apps/desktop/src/components/chat/shiki-highlighter.test.ts
@@ -53,8 +53,8 @@ second @url:"https://two.example" @file:\`src/app.ts\``
expect(copyableCodeText(code)).toBe('first https://one.example\nsecond https://two.example @file:`src/app.ts`')
})
- it('keeps hostless and malformed-host HTTP(S) URL directives raw', () => {
- for (const code of ['@url:http://', '@url:https://', '@url:http://?', '@url:https://#fragment', '@url:http://:80']) {
+ it('keeps hostless HTTP(S) URL directives raw', () => {
+ for (const code of ['@url:http://', '@url:https://', '@url:http://?', '@url:https://?']) {
expect(copyableCodeText(code)).toBe(code)
}
})
diff --git a/apps/desktop/src/components/chat/shiki-highlighter.tsx b/apps/desktop/src/components/chat/shiki-highlighter.tsx
index bbca7c0bd00..a5a45661203 100644
--- a/apps/desktop/src/components/chat/shiki-highlighter.tsx
+++ b/apps/desktop/src/components/chat/shiki-highlighter.tsx
@@ -3,6 +3,7 @@
import type { SyntaxHighlighterProps } from '@assistant-ui/react-streamdown'
import { type FC, lazy, Suspense, useMemo } from 'react'
+import { hasHttpUrlHost } from '@/app/chat/composer/url-refs'
import { referenceRe, unquoteReferenceValue } from '@/components/assistant-ui/reference-kinds'
import { CodeCard, CodeCardBody } from '@/components/chat/code-card'
import { ExpandableBlock } from '@/components/chat/expandable-block'
@@ -126,23 +127,12 @@ const PlainCode: FC<{ code: string }> = ({ code }) => {
)
}
-/** Match the composer's contract: only HTTP(S) URLs with a parsed host become URL references. */
-function isHttpUrlWithHost(value: string): boolean {
- try {
- const url = new URL(value)
-
- return (url.protocol === 'http:' || url.protocol === 'https:') && Boolean(url.hostname)
- } catch {
- return false
- }
-}
-
/** Convert URL references back to literal URLs for code-block clipboard text. */
export function copyableCodeText(code: string): string {
return code.replace(referenceRe(), (directive, kind: string, value: string) => {
const url = unquoteReferenceValue(value)
- return kind === 'url' && isHttpUrlWithHost(url) ? url : directive
+ return kind === 'url' && hasHttpUrlHost(url) ? url : directive
})
}