Skip to content

The agent that grows with you

feat(desktop): route agent-opened and typed URLs through loopback reach

Brooklyn Nicholson8911e2e0edf7
diff --git a/apps/desktop/src/app/chat/right-rail/preview-pane.test.tsx b/apps/desktop/src/app/chat/right-rail/preview-pane.test.tsx
index be6b2d95216..7050940666d 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-pane.test.tsx
+++ b/apps/desktop/src/app/chat/right-rail/preview-pane.test.tsx
@@ -185,7 +185,9 @@ describe('PreviewPane console state', () => {
fireEvent.keyDown(address, { key: 'Enter' })
// loadURL, not a `src` swap — re-entering the current address must reload.
- expect(loadURL).toHaveBeenCalledWith('http://localhost:4000/app')
+ // Awaited: navigation first asks main whether the address needs a loopback
+ // forward, so the load lands a microtask later.
+ await waitFor(() => expect(loadURL).toHaveBeenCalledWith('http://localhost:4000/app'))
expect(webview.getAttribute('src')).toBe('http://localhost:5174')
})
diff --git a/apps/desktop/src/app/chat/right-rail/preview-pane.tsx b/apps/desktop/src/app/chat/right-rail/preview-pane.tsx
index e8bb6626344..11cfc29340e 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-pane.tsx
+++ b/apps/desktop/src/app/chat/right-rail/preview-pane.tsx
@@ -9,6 +9,7 @@ import { isDesktopFsRemoteMode } from '@/lib/desktop-fs'
import { guardGuestPointers } from '@/lib/guest-pointer-guard'
import { openPreviewTargetInBrowser, remoteHtmlPreviewDocument } from '@/lib/local-preview'
import { isRemoteGateway } from '@/lib/media'
+import { reachablePreviewUrl } from '@/lib/preview-reach'
import { rafCoalesce } from '@/lib/raf-coalesce'
import { cn } from '@/lib/utils'
import { notify, notifyError } from '@/store/notifications'
@@ -350,17 +351,25 @@ export function PreviewPane({ embedded = false, onRestartServer, reloadRequest =
const navigateTo = useCallback(
(url: string) => {
setLoadError(null)
- // loadURL, not a `src` swap: `src` only reloads when the value CHANGES, so
- // re-entering the address you're already on would do nothing. A rejected
- // load is a real navigation failure the user has to see — `did-fail-load`
- // doesn't fire for every rejection (a bad scheme rejects outright).
- void Promise.resolve(webviewRef.current?.loadURL?.(url)).catch((error: unknown) => {
- setLoadError({
- description: error instanceof Error ? error.message : copy.unreachableDescription,
- url
+ // Typed addresses get the same loopback reach as agent-opened ones — on a
+ // remote gateway `localhost:5173` is usually the dev server the user is
+ // there to look at, not something on their own laptop.
+ void reachablePreviewUrl(url)
+ .then(reached =>
+ // loadURL, not a `src` swap: `src` only reloads when the value CHANGES,
+ // so re-entering the address you're already on would do nothing. A
+ // rejected load is a real navigation failure the user has to see —
+ // `did-fail-load` doesn't fire for every rejection (a bad scheme
+ // rejects outright).
+ webviewRef.current?.loadURL?.(reached)
+ )
+ .catch((error: unknown) => {
+ setLoadError({
+ description: error instanceof Error ? error.message : copy.unreachableDescription,
+ url
+ })
+ setLoading(false)
})
- setLoading(false)
- })
},
[copy.unreachableDescription]
)
diff --git a/apps/desktop/src/app/session/hooks/use-preview-routing.ts b/apps/desktop/src/app/session/hooks/use-preview-routing.ts
index aadde0b4898..ede4105b4ce 100644
--- a/apps/desktop/src/app/session/hooks/use-preview-routing.ts
+++ b/apps/desktop/src/app/session/hooks/use-preview-routing.ts
@@ -2,6 +2,7 @@ import { useCallback } from 'react'
import { gatewayEventCompletedFileDiff } from '@/lib/gateway-events'
import { normalizeOrLocalPreviewTarget } from '@/lib/local-preview'
+import { reachablePreviewUrl } from '@/lib/preview-reach'
import {
$previewTabs,
beginPreviewServerRestart,
@@ -81,12 +82,22 @@ export function usePreviewRouting({ baseHandleGatewayEvent, currentCwd, requestG
$sessionTiles.get().some(tile => tile.runtimeId === sid)
if (target && (!event.session_id || onScreen(event.session_id))) {
- void normalizeOrLocalPreviewTarget(target, $currentCwd.get() || currentCwd || undefined).then(resolved => {
- if (resolved) {
+ void normalizeOrLocalPreviewTarget(target, $currentCwd.get() || currentCwd || undefined).then(
+ async resolved => {
+ if (!resolved) {
+ return
+ }
+
const trimmedLabel = typeof label === 'string' ? label.trim() : ''
- openPreview(trimmedLabel ? { ...resolved, label: trimmedLabel } : resolved, 'tool-result')
+ // The agent's loopback is the GATEWAY's loopback. Give the pane a
+ // URL this machine can load, keeping the original as the label so
+ // the user still sees the address the agent named.
+ const url = resolved.kind === 'url' ? await reachablePreviewUrl(resolved.url) : resolved.url
+ const reached = url === resolved.url ? resolved : { ...resolved, label: resolved.label || target, url }
+
+ openPreview(trimmedLabel ? { ...reached, label: trimmedLabel } : reached, 'tool-result')
}
- })
+ )
}
return
diff --git a/apps/desktop/src/lib/preview-reach.test.ts b/apps/desktop/src/lib/preview-reach.test.ts
new file mode 100644
index 00000000000..78693ca7c57
--- /dev/null
+++ b/apps/desktop/src/lib/preview-reach.test.ts
@@ -0,0 +1,78 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { $connection } from '@/store/session'
+
+import { reachablePreviewUrl } from './preview-reach'
+
+const desktopWindow = window as unknown as { hermesDesktop?: Window['hermesDesktop'] }
+
+function installBridge(reachPreviewUrl?: unknown) {
+ desktopWindow.hermesDesktop = { reachPreviewUrl } as unknown as Window['hermesDesktop']
+}
+
+afterEach(() => {
+ $connection.set(null)
+ delete desktopWindow.hermesDesktop
+})
+
+describe('reachablePreviewUrl', () => {
+ // On a local backend the agent's localhost IS this machine's localhost —
+ // rewriting would break a URL that already works.
+ it('leaves a local gateway alone without touching the bridge', async () => {
+ const bridge = vi.fn()
+
+ $connection.set({ mode: 'local' } as never)
+ installBridge(bridge)
+
+ expect(await reachablePreviewUrl('http://localhost:5173/')).toBe('http://localhost:5173/')
+ expect(bridge).not.toHaveBeenCalled()
+ })
+
+ it('asks main to resolve the URL on a remote gateway', async () => {
+ const bridge = vi.fn(async () => 'http://127.0.0.1:45173/')
+
+ $connection.set({ mode: 'remote' } as never)
+ installBridge(bridge)
+
+ expect(await reachablePreviewUrl('http://localhost:5173/')).toBe('http://127.0.0.1:45173/')
+ expect(bridge).toHaveBeenCalledWith('http://localhost:5173/')
+ })
+
+ // A url/cloud remote has no tunnel to borrow, so main hands the URL back
+ // unchanged; the pane's own error explains it from there.
+ it('passes the original through when main cannot reach it', async () => {
+ $connection.set({ mode: 'remote' } as never)
+ installBridge(vi.fn(async (url: string) => url))
+
+ expect(await reachablePreviewUrl('http://localhost:5173/')).toBe('http://localhost:5173/')
+ })
+
+ it('falls back to the original when the bridge throws', async () => {
+ $connection.set({ mode: 'remote' } as never)
+ installBridge(
+ vi.fn(async () => {
+ throw new Error('no handler')
+ })
+ )
+
+ expect(await reachablePreviewUrl('http://localhost:5173/')).toBe('http://localhost:5173/')
+ })
+
+ // An older Electron main predates the handler entirely.
+ it('survives a bridge with no reach method', async () => {
+ $connection.set({ mode: 'remote' } as never)
+ installBridge(undefined)
+
+ expect(await reachablePreviewUrl('http://localhost:5173/')).toBe('http://localhost:5173/')
+ })
+
+ it('ignores an empty url', async () => {
+ $connection.set({ mode: 'remote' } as never)
+ const bridge = vi.fn()
+
+ installBridge(bridge)
+
+ expect(await reachablePreviewUrl('')).toBe('')
+ expect(bridge).not.toHaveBeenCalled()
+ })
+})
diff --git a/apps/desktop/src/lib/preview-reach.ts b/apps/desktop/src/lib/preview-reach.ts
new file mode 100644
index 00000000000..8b82548a8b1
--- /dev/null
+++ b/apps/desktop/src/lib/preview-reach.ts
@@ -0,0 +1,26 @@
+import { isRemoteGateway } from '@/lib/media'
+
+/**
+ * Make a URL loadable from THIS machine before the pane tries it.
+ *
+ * Against a remote gateway the agent's `localhost:5173` names the gateway's
+ * loopback, not ours. Main owns the answer — it knows whether an SSH transport
+ * is behind this connection and can open a forward through it — so the
+ * renderer asks rather than guessing.
+ *
+ * Always resolves to a usable URL. A local gateway, a non-loopback host, a
+ * remote with no tunnel to borrow, or a failed forward all return the original,
+ * and the pane's own load error explains an address it still can't reach.
+ */
+export async function reachablePreviewUrl(url: string): Promise<string> {
+ if (!url || !isRemoteGateway()) {
+ return url
+ }
+
+ try {
+ return (await window.hermesDesktop?.reachPreviewUrl?.(url)) || url
+ } catch {
+ // An older Electron main has no such handler; the URL is no worse for it.
+ return url
+ }
+}