Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(terminal): Cmd/Ctrl+click links inside tmux mouse-reporting mode

enes6a5c444cde32
diff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsx
index 2f1eb46cf..4c40732d2 100644
--- a/src/renderer/nodes/TerminalNode.tsx
+++ b/src/renderer/nodes/TerminalNode.tsx
@@ -18,7 +18,11 @@ import { LocalTransport } from '../terminal/local-transport'
import type { TerminalTransport } from '../terminal/transport'
import { patchTerminalScale } from '../terminal/scale-fix'
import { parseOsc52 } from '../terminal/osc52'
-import { createFileLinkProvider, makeDirListingLookup } from '../terminal/file-links'
+import {
+ createFileLinkProvider,
+ installLinkClickFallback,
+ makeDirListingLookup
+} from '../terminal/file-links'
import { sshFs } from '../terminal/ssh-fs'
import type { FsApi } from '@shared/types'
import {
@@ -698,20 +702,35 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
const project = st.projects.find((p) => p.id === st.activeProjectId)
return project?.ssh ? { fs: sshFs(project.id), ssh: true } : { fs: api.fs, ssh: false }
}
+ // Relay-remote nodes have no client fs, so file-path links are skipped (URL-only) — mirrors
+ // the CLAUDE.md note. A relay project carries the runtime-only `remote` flag.
+ const isRelayProject = (): boolean => {
+ const st = useProjects.getState()
+ return !!st.projects.find((p) => p.id === st.activeProjectId)?.remote
+ }
const lookup = makeDirListingLookup(async (dir) => projectFs().fs.list(dir))
- term.registerLinkProvider(
- createFileLinkProvider(term, {
- getCwd: () => (data.cwd as string | undefined) || undefined,
+ const getCwd = (): string | undefined => (data.cwd as string | undefined) || undefined
+ const openFile = (abs: string, isDir: boolean): void => {
+ if (isDir) window.dispatchEvent(new CustomEvent('nodeterm:reveal-file', { detail: { path: abs } }))
+ else
+ window.dispatchEvent(
+ new CustomEvent('nodeterm:open-file', { detail: { path: abs, ssh: projectFs().ssh } })
+ )
+ }
+ term.registerLinkProvider(createFileLinkProvider(term, { getCwd, lookup, activate: openFile }))
+ // The provider above (and the WebLinksAddon) rely on xterm's own click handling, which
+ // tmux/agent mouse-reporting swallows. This capture-phase mouse-up fallback restores
+ // Cmd/Ctrl+click for both URLs and file paths in that mode. Attached to `term.element` so
+ // it travels with the terminal across park/adopt; it dies with the terminal on dispose.
+ if (term.element) {
+ installLinkClickFallback(term, term.element, {
+ getCwd,
lookup,
- activate: (abs, isDir) => {
- if (isDir) window.dispatchEvent(new CustomEvent('nodeterm:reveal-file', { detail: { path: abs } }))
- else
- window.dispatchEvent(
- new CustomEvent('nodeterm:open-file', { detail: { path: abs, ssh: projectFs().ssh } })
- )
- }
+ activateFile: openFile,
+ openUrl: (uri) => window.nodeTerminal.shell.openExternal(uri),
+ fileEnabled: () => !isRelayProject()
})
- )
+ }
}
// Cmd+C (mac) / Ctrl+Shift+C (Linux, Windows) / Ctrl+Insert copy the terminal selection — xterm
diff --git a/src/renderer/terminal/file-links.test.ts b/src/renderer/terminal/file-links.test.ts
index ea5b40c68..b4663f0c4 100644
--- a/src/renderer/terminal/file-links.test.ts
+++ b/src/renderer/terminal/file-links.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
-import { matchFileTokens, resolveFileToken } from './file-links'
+import { matchFileTokens, matchUrlTokens, resolveFileToken } from './file-links'
describe('matchFileTokens', () => {
it('finds absolute, dot-relative and bare relative paths', () => {
@@ -52,3 +52,24 @@ describe('resolveFileToken', () => {
expect(resolveFileToken('/abs/x.ts', '~/proj')).toBe('/abs/x.ts') // absolutes unaffected
})
})
+
+describe('matchUrlTokens', () => {
+ it('finds http(s) URLs with their start index', () => {
+ const t = matchUrlTokens('open https://example.com/a/b and http://x.io/p here')
+ expect(t.map((x) => x.url)).toEqual(['https://example.com/a/b', 'http://x.io/p'])
+ expect(t[0].startIndex).toBe(5)
+ })
+
+ it('strips trailing sentence punctuation', () => {
+ expect(matchUrlTokens('see https://example.com/a.').map((x) => x.url)).toEqual([
+ 'https://example.com/a'
+ ])
+ expect(matchUrlTokens('(https://example.com/a)').map((x) => x.url)).toEqual([
+ 'https://example.com/a'
+ ])
+ })
+
+ it('ignores non-http schemes and bare paths', () => {
+ expect(matchUrlTokens('ftp://x.io file:///etc/hosts /usr/local plain')).toEqual([])
+ })
+})
diff --git a/src/renderer/terminal/file-links.ts b/src/renderer/terminal/file-links.ts
index 202c52e79..bfe58b716 100644
--- a/src/renderer/terminal/file-links.ts
+++ b/src/renderer/terminal/file-links.ts
@@ -56,6 +56,33 @@ export function matchFileTokens(lineText: string): FileToken[] {
return out
}
+// http(s) URLs, matched the way the WebLinksAddon's click path would see them. Kept here so
+// the mouse-up click fallback (below) can hit-test URLs and file paths in one pass — under
+// tmux/agent mouse-reporting the addon's own click never fires (see installLinkClickFallback).
+const URL_RE = /\bhttps?:\/\/[^\s"'`<>()[\]{}|\\^]+/gi
+
+export interface UrlToken {
+ text: string
+ startIndex: number
+ url: string
+}
+
+export function matchUrlTokens(lineText: string): UrlToken[] {
+ const out: UrlToken[] = []
+ for (const m of lineText.matchAll(URL_RE)) {
+ const text = m[0].replace(TRAILING_PUNCT, '')
+ if (text.length < 8) continue // "http://x" is the shortest sane URL
+ try {
+ const u = new URL(text)
+ if (u.protocol !== 'http:' && u.protocol !== 'https:') continue
+ } catch {
+ continue
+ }
+ out.push({ text, startIndex: m.index, url: text })
+ }
+ return out
+}
+
/** Absolute path for a token: absolutes pass through, relatives resolve against cwd,
* `.`/`..` segments normalized. Null when unresolvable or when `..` escapes the root.
* A home-relative cwd (`~` or `~/proj`, the SSH-project default) keeps its leading `~` as
@@ -166,3 +193,104 @@ export function makeDirListingLookup(
return { exists: !!e, dir: !!e?.dir }
}
}
+
+// Walk up from an arbitrary buffer row to the first non-wrapped row, then join the whole
+// soft-wrapped logical line from there. Lets a click on ANY wrapped row of a long path resolve
+// to the same logical string the hover provider builds from the line's first row.
+function logicalLineContaining(term: Terminal, row: number): { text: string; startRow: number } | null {
+ const buf = term.buffer.active
+ let start = row
+ while (start > 0 && buf.getLine(start)?.isWrapped) start -= 1
+ const l = logicalLine(term, start)
+ return l ? { text: l.text, startRow: start } : null
+}
+
+// Cell (0-based col, 0-based buffer row) under a mouse event. The canvas applies zoom as a CSS
+// transform, so getBoundingClientRect() is already the on-screen (scaled) size — dividing the
+// scaled offset by the scaled cell size cancels the zoom, keeping cols/rows constant.
+function bufferPosFromEvent(term: Terminal, ev: MouseEvent): { col: number; row: number } | null {
+ const screen = term.element?.querySelector('.xterm-screen') as HTMLElement | null
+ if (!screen || term.cols <= 0 || term.rows <= 0) return null
+ const rect = screen.getBoundingClientRect()
+ const x = ev.clientX - rect.left
+ const y = ev.clientY - rect.top
+ if (x < 0 || y < 0 || x >= rect.width || y >= rect.height) return null
+ const cw = rect.width / term.cols
+ const ch = rect.height / term.rows
+ if (cw <= 0 || ch <= 0) return null
+ return {
+ col: Math.floor(x / cw),
+ row: Math.floor(y / ch) + term.buffer.active.viewportY
+ }
+}
+
+export interface LinkClickDeps {
+ getCwd(): string | undefined
+ lookup(abs: string): Promise<{ exists: boolean; dir: boolean }>
+ activateFile(abs: string, dir: boolean): void
+ openUrl(url: string): void
+ /** false for relay-remote nodes (no client fs) — they stay URL-only. */
+ fileEnabled(): boolean
+}
+
+/**
+ * Cmd/Ctrl+click link opening that works INSIDE tmux / an agent's fullscreen TUI. There, the
+ * app has mouse-reporting on, so xterm consumes a click as a mouse escape and never runs the
+ * registered link provider's `activate` (xterm: `areMouseEventsActive && !shouldForceSelection`
+ * ⇒ early return). This capture-phase `mouseup` listener runs BEFORE xterm's mouse handler:
+ * gated on the modifier, it hit-tests the buffer itself, opens the link, and stops propagation
+ * so the mouse report is never sent. Non-modifier clicks/drags fall through untouched, so tmux
+ * copy-mode selection and scrolling are unaffected. Attach to `term.element` so the listener
+ * travels with the terminal across park/adopt. Returns a disposer.
+ */
+export function installLinkClickFallback(
+ term: Terminal,
+ host: HTMLElement,
+ deps: LinkClickDeps
+): { dispose(): void } {
+ const onMouseUp = (ev: MouseEvent): void => {
+ if (ev.button !== 0 || !(ev.metaKey || ev.ctrlKey)) return
+ // Only take over when the app has mouse-reporting on (tmux mouse / agent TUI) — that is the
+ // exact case where xterm's own link `activate` never fires. With reporting OFF (a plain shell
+ // when tmux is unavailable) xterm's provider + WebLinksAddon handle the click, so stepping in
+ // here would open the link twice.
+ if (term.modes.mouseTrackingMode === 'none') return
+ const pos = bufferPosFromEvent(term, ev)
+ if (!pos) return
+ const logical = logicalLineContaining(term, pos.row)
+ if (!logical) return
+ const idx = (pos.row - logical.startRow) * term.cols + pos.col
+ const inRange = (startIndex: number, len: number): boolean =>
+ idx >= startIndex && idx < startIndex + len
+
+ for (const u of matchUrlTokens(logical.text)) {
+ if (inRange(u.startIndex, u.text.length)) {
+ ev.preventDefault()
+ ev.stopPropagation()
+ term.clearSelection()
+ deps.openUrl(u.url)
+ return
+ }
+ }
+ if (!deps.fileEnabled()) return
+ for (const t of matchFileTokens(logical.text)) {
+ if (inRange(t.startIndex, t.text.length)) {
+ const abs = resolveFileToken(t.path, deps.getCwd())
+ if (!abs) return
+ // Swallow the click NOW so tmux never gets the mouse report; existence is async and a
+ // Cmd/Ctrl+click on a path-shaped token is a deliberate open regardless of the outcome.
+ ev.preventDefault()
+ ev.stopPropagation()
+ term.clearSelection()
+ void deps.lookup(abs).then((f) => {
+ if (f.exists) deps.activateFile(abs, f.dir)
+ })
+ return
+ }
+ }
+ }
+ host.addEventListener('mouseup', onMouseUp, { capture: true })
+ return {
+ dispose: () => host.removeEventListener('mouseup', onMouseUp, { capture: true })
+ }
+}