Skip to content

The agent that grows with you

feat(desktop): open web links in the in-app browser, Cmd-click for the system one

Brooklyn Nicholsond48b44150615
diff --git a/apps/desktop/src/app/chat/composer/directive-actions.test.tsx b/apps/desktop/src/app/chat/composer/directive-actions.test.tsx
index 4c827fe9a16..fcaa4424f5c 100644
--- a/apps/desktop/src/app/chat/composer/directive-actions.test.tsx
+++ b/apps/desktop/src/app/chat/composer/directive-actions.test.tsx
@@ -2,6 +2,7 @@ import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { I18nProvider } from '@/i18n'
+import { $previewTabs, closeRightRail } from '@/store/preview'
import { ComposerDirectiveActions } from './directive-actions'
import { refChipElement } from './rich-editor'
@@ -46,6 +47,7 @@ function pillValue() {
afterEach(() => {
cleanup()
document.body.replaceChildren()
+ closeRightRail()
delete desktopWindow.hermesDesktop
openSession.mockReset()
vi.useRealTimers()
@@ -62,7 +64,7 @@ describe('ComposerDirectiveActions', () => {
expect(pillValue()).toBe('https://example.com/docs')
})
- it('opens a url externally rather than navigating the app', () => {
+ it('opens a url in the in-app browser rather than navigating the app', async () => {
const openExternal = vi.fn().mockResolvedValue(undefined)
desktopWindow.hermesDesktop = { openExternal } as unknown as Window['hermesDesktop']
@@ -72,7 +74,8 @@ describe('ComposerDirectiveActions', () => {
hover(chips(editor, 'url')[0]!)
fireEvent.click(screen.getByRole('button'))
- expect(openExternal).toHaveBeenCalledWith('https://example.com/docs')
+ expect(openExternal).not.toHaveBeenCalled()
+ await vi.waitFor(() => expect($previewTabs.get().at(-1)?.target.url).toBe('https://example.com/docs'))
expect(pillValue()).toBeNull()
})
diff --git a/apps/desktop/src/app/right-sidebar/terminal/links.test.ts b/apps/desktop/src/app/right-sidebar/terminal/links.test.ts
index fcbc45853f2..76b5e4b9ced 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/links.test.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/links.test.ts
@@ -2,6 +2,8 @@ import type { ILink, Terminal as TerminalType } from '@xterm/xterm'
import { Terminal } from '@xterm/xterm'
import { beforeEach, describe, expect, it, vi } from 'vitest'
+import { $previewTabs, closeRightRail } from '@/store/preview'
+
import { isTerminalLinkActivation, terminalLinkHandler, terminalWebLinksAddon } from './links'
const openExternal = vi.fn()
@@ -9,6 +11,7 @@ const click = (init: Partial<MouseEvent> = {}) => ({ ctrlKey: false, metaKey: fa
beforeEach(() => {
openExternal.mockClear()
+ closeRightRail()
Object.defineProperty(window, 'hermesDesktop', { configurable: true, value: { openExternal } })
// jsdom reports a non-mac platform, so the activation modifier resolves to
// Ctrl unless we say otherwise.
@@ -40,11 +43,23 @@ async function clickLinkIn(text: string, event: MouseEvent) {
}
describe('terminal links', () => {
- it('opens a ⌘-clicked URL through the desktop bridge, not the window.open Electron denies', async () => {
+ it('opens a ⌘-clicked URL in the in-app browser, not the window.open Electron denies', async () => {
const uri = 'https://example.com/path'
expect(await clickLinkIn(uri, new MouseEvent('click', { metaKey: true }))).toBe(uri)
+ expect(openExternal).not.toHaveBeenCalled()
+ await vi.waitFor(() => expect($previewTabs.get().at(-1)?.target.url).toBe(uri))
+ })
+
+ // The terminal spends ⌘/Ctrl on activation itself, so ⇧ is what escapes to
+ // the OS browser here.
+ it('escapes to the OS browser on ⇧⌘-click', async () => {
+ const uri = 'https://example.com/path'
+
+ await clickLinkIn(uri, new MouseEvent('click', { metaKey: true, shiftKey: true }))
+
expect(openExternal).toHaveBeenCalledWith(uri)
+ expect($previewTabs.get()).toHaveLength(0)
})
it('leaves a bare click to the selection, so a misclick never launches a browser', async () => {
@@ -53,13 +68,13 @@ describe('terminal links', () => {
expect(openExternal).not.toHaveBeenCalled()
})
- it('routes OSC 8 hyperlinks the same way, instead of xterm\u2019s confirm() dialog', () => {
+ it('routes OSC 8 hyperlinks the same way, instead of xterm’s confirm() dialog', async () => {
terminalLinkHandler.activate(new MouseEvent('click', { metaKey: true }), 'https://example.com/osc8', {
end: { x: 10, y: 1 },
start: { x: 1, y: 1 }
})
- expect(openExternal).toHaveBeenCalledWith('https://example.com/osc8')
+ await vi.waitFor(() => expect($previewTabs.get().at(-1)?.target.url).toBe('https://example.com/osc8'))
})
})
diff --git a/apps/desktop/src/app/right-sidebar/terminal/links.ts b/apps/desktop/src/app/right-sidebar/terminal/links.ts
index 41024407134..cba690fd547 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/links.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/links.ts
@@ -1,7 +1,7 @@
import { WebLinksAddon } from '@xterm/addon-web-links'
import type { ILinkHandler } from '@xterm/xterm'
-import { openExternalLink } from '@/lib/external-link'
+import { openLink } from '@/lib/external-link'
import { isMacPlatform } from './selection'
@@ -10,8 +10,8 @@ import { isMacPlatform } from './selection'
// activate through `window.open()`, which the window's setWindowOpenHandler
// denies: a click did nothing but log "Opening link blocked as opener could not
// be cleared", and OSC 8 fronted that dead end with a raw confirm() dialog.
-// Route both through the desktop bridge, the path every other external link in
-// the app takes.
+// Route both through the shared link router, the path every other link in the
+// app takes.
//
// ⌘-click on macOS, Ctrl-click elsewhere — VS Code's integrated terminal,
// Terminal.app, and iTerm2 all agree. A bare click belongs to the selection, so
@@ -24,9 +24,14 @@ export function isTerminalLinkActivation(
return isMac ? event.metaKey : event.ctrlKey
}
+// The terminal spends the ⌘/Ctrl modifier on activation itself, so it has no
+// second chord left to mean "native browser" — ⇧ takes that job here. Elsewhere
+// a bare click opens in-app and ⌘ escapes to the OS; here it's ⌘ to open and
+// ⇧⌘ to escape, which keeps both gestures reachable without stealing the
+// selection back from a plain click.
const activate = (event: MouseEvent, uri: string) => {
if (isTerminalLinkActivation(event)) {
- openExternalLink(uri)
+ openLink(uri, { native: event.shiftKey })
}
}
diff --git a/apps/desktop/src/components/assistant-ui/directive-text.tsx b/apps/desktop/src/components/assistant-ui/directive-text.tsx
index b976230247d..3e21698a6df 100644
--- a/apps/desktop/src/components/assistant-ui/directive-text.tsx
+++ b/apps/desktop/src/components/assistant-ui/directive-text.tsx
@@ -8,7 +8,7 @@ import { Fragment, useEffect, useMemo, useState } from 'react'
import { ZoomableImage } from '@/components/chat/zoomable-image'
import type { I18nContextValue } from '@/i18n'
import { extractEmbeddedImages } from '@/lib/embedded-images'
-import { openExternalLink } from '@/lib/external-link'
+import { openLink } from '@/lib/external-link'
import { triggerHaptic } from '@/lib/haptics'
import { gatewayMediaDataUrl, isRemoteGateway } from '@/lib/media'
import { useSessionLinkTitle } from '@/lib/session-link-title'
@@ -488,7 +488,7 @@ export const DIRECTIVE_ACTIONS: Record<string, DirectiveAction> = {
url: {
icon: 'link-external',
label: t => t.composer.openDirective,
- run: openExternalLink
+ run: openLink
}
}
diff --git a/apps/desktop/src/components/assistant-ui/session-ref-open.test.tsx b/apps/desktop/src/components/assistant-ui/session-ref-open.test.tsx
index f10d729d88f..f8f3c5648ca 100644
--- a/apps/desktop/src/components/assistant-ui/session-ref-open.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/session-ref-open.test.tsx
@@ -2,6 +2,7 @@ import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { __resetSessionLinkTitleCache } from '@/lib/session-link-title'
+import { $previewTabs, closeRightRail } from '@/store/preview'
import { DirectiveContent } from './directive-text'
import { MarkdownTextContent } from './markdown-text'
@@ -16,6 +17,7 @@ const desktopWindow = window as unknown as { hermesDesktop?: Window['hermesDeskt
afterEach(() => {
cleanup()
+ closeRightRail()
openSession.mockClear()
delete desktopWindow.hermesDesktop
__resetSessionLinkTitleCache()
@@ -45,11 +47,11 @@ describe('session refs open the session', () => {
})
})
-// A url the user sent renders as a chip too, and it opens in the browser — the
-// same door the composer's hover pill uses, so a link behaves the same before
-// and after send.
-describe('url refs open externally', () => {
- it('opens a url chip in the user transcript', () => {
+// A url the user sent renders as a chip too, and it opens in the IN-APP
+// browser — the same door the composer's hover pill uses, so a link behaves
+// the same before and after send.
+describe('url refs open in the browser pane', () => {
+ it('opens a url chip in the user transcript', async () => {
const openExternal = vi.fn().mockResolvedValue(undefined)
desktopWindow.hermesDesktop = { openExternal } as unknown as Window['hermesDesktop']
@@ -61,6 +63,7 @@ describe('url refs open externally', () => {
expect(chip.tagName).toBe('BUTTON')
fireEvent.click(chip)
- expect(openExternal).toHaveBeenCalledWith('https://example.com/docs')
+ expect(openExternal).not.toHaveBeenCalled()
+ await vi.waitFor(() => expect($previewTabs.get().at(-1)?.target.url).toBe('https://example.com/docs'))
})
})
diff --git a/apps/desktop/src/lib/external-link.test.tsx b/apps/desktop/src/lib/external-link.test.tsx
index 5407639199c..3f33f45680d 100644
--- a/apps/desktop/src/lib/external-link.test.tsx
+++ b/apps/desktop/src/lib/external-link.test.tsx
@@ -1,6 +1,9 @@
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import { afterEach, describe, expect, it, vi } from 'vitest'
+import { IS_MAC } from '@/lib/keybinds/combo'
+import { $previewTabs, closeRightRail } from '@/store/preview'
+
import {
__resetLinkTitleCache,
ExternalLink,
@@ -35,6 +38,7 @@ function installTitleBridge(title: string) {
afterEach(() => {
__resetLinkTitleCache()
+ closeRightRail()
vi.restoreAllMocks()
cleanup()
@@ -102,14 +106,44 @@ describe('external link helpers', () => {
expect(bridge).toHaveBeenCalledTimes(1)
})
- it('opens links via the desktop bridge', () => {
+ // A web link belongs in the in-app browser now; the OS browser is the
+ // ⌘/Ctrl-click escape hatch.
+ it('opens a web link in the in-app browser', async () => {
const openExternal = vi.fn().mockResolvedValue(undefined)
installDesktopBridge({ openExternal: openExternal as unknown as Window['hermesDesktop']['openExternal'] })
render(<ExternalLink href="https://example.com/path/to/resource">Example link</ExternalLink>)
fireEvent.click(screen.getByRole('link', { name: 'Example link' }))
+
+ expect(openExternal).not.toHaveBeenCalled()
+ await waitFor(() => expect($previewTabs.get().at(-1)?.target.url).toBe('https://example.com/path/to/resource'))
+ })
+
+ // Platform-specific on purpose (same rule as terminal links / middle-click):
+ // ⌘ on macOS, Ctrl elsewhere. The suite runs as non-mac.
+ it('escapes to the OS browser on the platform open-elsewhere modifier', () => {
+ const openExternal = vi.fn().mockResolvedValue(undefined)
+ installDesktopBridge({ openExternal: openExternal as unknown as Window['hermesDesktop']['openExternal'] })
+
+ render(<ExternalLink href="https://example.com/path/to/resource">Example link</ExternalLink>)
+
+ fireEvent.click(screen.getByRole('link', { name: 'Example link' }), IS_MAC ? { metaKey: true } : { ctrlKey: true })
+
expect(openExternal).toHaveBeenCalledWith('https://example.com/path/to/resource')
+ expect($previewTabs.get()).toHaveLength(0)
+ })
+
+ // A webview can't do anything useful with these, so they always hand off.
+ it('hands a non-web scheme to the OS', () => {
+ const openExternal = vi.fn().mockResolvedValue(undefined)
+ installDesktopBridge({ openExternal: openExternal as unknown as Window['hermesDesktop']['openExternal'] })
+
+ render(<ExternalLink href="mailto:[email protected]">Mail</ExternalLink>)
+
+ fireEvent.click(screen.getByRole('link', { name: 'Mail' }))
+
+ expect(openExternal).toHaveBeenCalledWith('mailto:[email protected]')
})
it('hides the trailing external-link icon by default', () => {
diff --git a/apps/desktop/src/lib/external-link.tsx b/apps/desktop/src/lib/external-link.tsx
index 31f17c49c4d..9a284f672b5 100644
--- a/apps/desktop/src/lib/external-link.tsx
+++ b/apps/desktop/src/lib/external-link.tsx
@@ -2,6 +2,7 @@ import type { ComponentProps, ReactNode } from 'react'
import { useEffect, useMemo, useState } from 'react'
import { ArrowUpRight } from '@/lib/icons'
+import { IS_MAC } from '@/lib/keybinds/combo'
import { resolveBrandIcon } from './brand-icon'
import { cn } from './utils'
@@ -201,6 +202,48 @@ export function openExternalLink(href: string): void {
}
}
+/**
+ * True when a click asked for the SYSTEM browser — ⌘ on macOS, Ctrl elsewhere,
+ * the modifier every app uses for "open this somewhere else". Middle-click
+ * counts too: it is the other half of the same convention.
+ */
+export function wantsNativeBrowser(event: Pick<MouseEvent, 'button' | 'ctrlKey' | 'metaKey'>): boolean {
+ return event.button === 1 || (IS_MAC ? event.metaKey : event.ctrlKey)
+}
+
+/**
+ * Where a link the user clicked should open.
+ *
+ * A web page opens in the in-app browser — that pane exists so reading a doc
+ * doesn't cost a context switch out of Hermes, and it is the surface the agent
+ * can see. ⌘/Ctrl-click (or middle-click) escapes to the real browser, which is
+ * where you go for anything needing your logged-in session or a password.
+ *
+ * Everything that ISN'T a web page — `mailto:`, `file:`, a custom scheme — has
+ * no business in the webview and always hands off to the OS.
+ */
+export function openLink(href: string, options: { native?: boolean } = {}): void {
+ const target = normalizeExternalUrl(href)
+
+ if (!target) {
+ return
+ }
+
+ if (options.native || !/^https?:$/i.test(parseUrl(target)?.protocol ?? '')) {
+ openExternalLink(target)
+
+ return
+ }
+
+ // Lazy: this module is a leaf every surface imports, and the preview store
+ // pulls the layout/session graph behind it. A static edge would make one
+ // link helper drag that whole tree into anything that renders a link. The
+ // tab lands a microtask later, which is invisible.
+ void import('@/store/preview').then(({ openPreview }) =>
+ openPreview({ kind: 'url', label: hostPathLabel(target), source: target, url: target }, 'explicit-link')
+ )
+}
+
interface ExternalLinkProps extends Omit<ComponentProps<'a'>, 'href' | 'target'> {
href: string
children?: ReactNode
@@ -250,6 +293,17 @@ export function ExternalLink({
}
event.preventDefault()
+ openLink(target, { native: wantsNativeBrowser(event.nativeEvent) })
+ }}
+ // Middle-click never fires `click`; it's the other half of the
+ // open-elsewhere convention, so it has to be caught on its own.
+ onAuxClick={event => {
+ if (event.button !== 1) {
+ return
+ }
+
+ event.preventDefault()
+ event.stopPropagation()
openExternalLink(target)
}}
rel="noopener noreferrer"