Skip to content

Commit bc0ddde

Browse files
committed
fix(tui): open local file links with the OS handler
Transcript clicks always went through the HTTP-only browser opener, so absolute paths and file:// links showed an error toast. Route those targets through openHref while keeping openUrl strict for auth and docs.
1 parent e41e7e9 commit bc0ddde

4 files changed

Lines changed: 56 additions & 8 deletions

File tree

‎packages/tui/src/app.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ import { Toast, ToastProvider, useToast } from "./ui/toast"
8484
import { isFallbackTitle } from "@opencode/util/session-title-fallback"
8585
import * as Model from "./util/model"
8686
import { ArgsProvider, useArgs, type Args } from "./context/args"
87-
import { openUrl } from "@opencode/util/open"
87+
import { openHref, openUrl } from "@opencode/util/open"
8888
import { PromptRefProvider, usePromptRef } from "./context/prompt"
8989
import { Config, ConfigProvider, useConfig } from "./config"
9090
import { newSessionLocation } from "./config/new-session-location"
@@ -1334,7 +1334,7 @@ function App() {
13341334
}}
13351335
onMouseUp={(event) => {
13361336
const url = clickedLink(renderer, event)
1337-
if (url) return void openUrl(url).catch(toast.error)
1337+
if (url) return void openHref(url).catch(toast.error)
13381338
if (copyOnSelectEnabled()) Selection.copyOnSelectRelease(event, renderer, toast, clipboard)
13391339
}}
13401340
>

‎packages/tui/src/ui/link.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { JSX } from "solid-js"
22
import type { CliRenderer, MouseEvent, RGBA } from "@opentui/core"
3-
import { openUrl } from "@opencode/util/open"
3+
import { openHref } from "@opencode/util/open"
44

55
export interface LinkProps {
66
href: string
@@ -13,7 +13,7 @@ export interface LinkProps {
1313

1414
/**
1515
* Link component that renders clickable hyperlinks.
16-
* Clicking anywhere on the link text opens the URL in the default browser.
16+
* Clicking anywhere on the link text opens http(s) URLs in the browser, or local file paths with the OS handler.
1717
*/
1818
export function Link(props: LinkProps) {
1919
const displayText = props.children ?? props.href
@@ -25,7 +25,7 @@ export function Link(props: LinkProps) {
2525
width={props.width}
2626
wrapMode={props.wrapMode}
2727
onMouseUp={() => {
28-
openUrl(props.href).catch(() => {})
28+
openHref(props.href).catch(() => {})
2929
}}
3030
>
3131
<a href={props.href}>{displayText}</a>

‎packages/util/src/open.test.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { describe, expect, test } from "bun:test"
2-
import { openUrl } from "./open.js"
2+
import path from "node:path"
3+
import { pathToFileURL } from "node:url"
4+
import { openUrl, resolveOpenTarget } from "./open.js"
35

46
describe("openUrl", () => {
57
test("rejects values that are not URLs", async () => {
@@ -14,3 +16,23 @@ describe("openUrl", () => {
1416
await expect(openUrl("\\\\server\\share\\file.html")).rejects.toThrow("Only http and https links")
1517
})
1618
})
19+
20+
describe("resolveOpenTarget", () => {
21+
test("keeps http and https URLs for the browser", () => {
22+
expect(resolveOpenTarget("https://example.com/docs")).toBe("https://example.com/docs")
23+
expect(resolveOpenTarget("http://127.0.0.1:4096")).toBe("http://127.0.0.1:4096/")
24+
})
25+
26+
test("maps local file URLs and absolute paths to filesystem paths", () => {
27+
const file = path.join(path.resolve("/tmp"), "example.md")
28+
expect(resolveOpenTarget(pathToFileURL(file).href)).toBe(file)
29+
expect(resolveOpenTarget(file)).toBe(file)
30+
})
31+
32+
test("rejects remote file URLs and unsafe schemes", () => {
33+
expect(resolveOpenTarget("file://example.com/share/index.html")).toBeUndefined()
34+
expect(resolveOpenTarget("javascript:alert(1)")).toBeUndefined()
35+
expect(resolveOpenTarget("packages/tui/src/ui/link.tsx")).toBeUndefined()
36+
expect(resolveOpenTarget("not a url")).toBeUndefined()
37+
})
38+
})

‎packages/util/src/open.ts‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import open from "open"
2+
import path from "node:path"
3+
import { fileURLToPath } from "node:url"
24

35
export function openUrl(input: string) {
46
const url = URL.canParse(input) ? new URL(input) : undefined
@@ -7,6 +9,30 @@ export function openUrl(input: string) {
79
return open(url.href)
810
}
911

10-
export function openPath(path: string) {
11-
return open(path)
12+
export function openPath(target: string) {
13+
return open(target)
14+
}
15+
16+
/** Open an http(s) URL in the browser, or a local file path / file:// link with the OS handler. */
17+
export function openHref(input: string) {
18+
const target = resolveOpenTarget(input)
19+
if (!target) return Promise.reject(new Error(`Only http, https, and local file links can be opened: ${input}`))
20+
return open(target)
21+
}
22+
23+
/** @internal */
24+
export function resolveOpenTarget(input: string) {
25+
if (URL.canParse(input)) {
26+
const url = new URL(input)
27+
if (url.protocol === "http:" || url.protocol === "https:") return url.href
28+
// Match desktop: only local file URLs (no host) become filesystem paths.
29+
if (url.protocol === "file:" && !url.hostname) {
30+
try {
31+
return fileURLToPath(url.href)
32+
} catch {
33+
return undefined
34+
}
35+
}
36+
}
37+
if (path.isAbsolute(input)) return input
1238
}

0 commit comments

Comments
 (0)