The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 11:00 UTC
fix(web): open terminal file links naming the server hostname (#37133)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 15, 2026 at 11:03 UTC
46f3ab39f227467d62eeda595f043765816ef1fd
7 files changed
+111
-12
apps/server/src/ws.ts
+2
@@ -1,6 +1,7 @@
1
import { OrchestrationDispatchCommandError } from "@t3tools/contracts";
2
// @effect-diagnostics-next-line nodeBuiltinImport:off -- A synchronous digest inside the pure provider mapping.
3
import * as NodeCrypto from "node:crypto";
4
+import * as HostProcess from "@t3tools/shared/HostProcess";
5
import * as Context from "effect/Context";
6
import * as NodeHttpServerRequest from "@effect/platform-node/NodeHttpServerRequest";
7
import * as SecureRandom from "effect/crypto/SecureRandom";
@@ -1836,6 +1837,7 @@ export const layerWsRpc = (
1837
environment,
1838
auth,
1839
cwd: config.cwd,
1840
+ hostname: yield* HostProcess.Hostname,
1841
keybindingsConfigPath: config.keybindingsConfigPath,
1842
keybindings: keybindingsConfig.keybindings,
1843
issues: keybindingsConfig.issues,
apps/web/src/components/ThreadTerminalDrawer.tsx
+6
-4
@@ -527,11 +527,14 @@ function TerminalSessionViewport({
527
const canReadFilesystem = useEnvironmentScope(environmentId, AuthFilesystemReadScope);
528
const [linkPath, setLinkPath] = useState<string | null>(null);
529
const metadataThreadRef = useContext(FileMetadataThreadContext);
530
+ const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId));
531
+ const hostname = serverConfig?.hostname;
532
+ const readHostname = useEffectEvent(() => hostname);
533
const linkMetadataQuery =
534
linkPath !== null && canOpenHostEditor && canReadFilesystem
535
? getFileMetadataQueryAtom(
536
environmentId,
534
- resolveTerminalLinkTarget(linkPath, cwd),
537
+ resolveTerminalLinkTarget(linkPath, cwd, hostname),
538
cwd,
539
metadataThreadRef,
540
)
@@ -552,7 +555,7 @@ function TerminalSessionViewport({
555
return false;
556
const query = getFileMetadataQueryAtom(
557
environmentId,
555
- resolveTerminalLinkTarget(text, cwd),
558
+ resolveTerminalLinkTarget(text, cwd, hostname),
559
cwd,
560
metadataThreadRef,
561
);
@@ -563,7 +566,6 @@ function TerminalSessionViewport({
566
(result.value?.kind === "file" || result.value?.kind === "directory")
567
);
568
});
566
- const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId));
569
const openInPreferredEditor = useOpenInPreferredEditor(
570
environmentId,
571
serverConfig?.availableEditors ?? [],
@@ -1069,7 +1071,7 @@ function TerminalSessionViewport({
1071
);
1072
return;
1073
}
1072
- const target = resolveTerminalLinkTarget(text, cwd);
1074
+ const target = resolveTerminalLinkTarget(text, cwd, readHostname());
1075
void (async () => {
1076
const result = await openTerminalPath(target);
1077
if (result._tag === "Success" || isAtomCommandInterrupted(result)) {
apps/web/src/terminal-links.ts
+6
-3
@@ -162,9 +162,12 @@ export function isTerminalUrl(value: string): boolean {
162
return /^https?:\/\//iu.test(value);
163
}
164
165
-/** Explicit OSC 8 targets may be `file://` URIs; everything else is a path relative to `cwd`. */
166
-export function resolveTerminalLinkTarget(text: string, cwd: string): string {
167
- const fileUrl = /^file:\/\//iu.test(text) ? parseFileUrlHref(text) : null;
165
+/**
166
+ * Explicit OSC 8 targets may be `file://` URIs; everything else is a path relative to `cwd`.
167
+ * `hostname` is the terminal's environment's, which producers such as GNU `ls` put in the URI.
168
+ */
169
+export function resolveTerminalLinkTarget(text: string, cwd: string, hostname?: string): string {
170
+ const fileUrl = /^file:\/\//iu.test(text) ? parseFileUrlHref(text, hostname) : null;
171
if (!fileUrl) return resolvePathLinkTarget(text, cwd);
172
return formatFilePathPosition(
173
splitFilePathPosition(
packages/contracts/src/server.ts
+2
@@ -685,6 +685,8 @@ export const ServerConfig = Schema.Struct({
685
environment: ExecutionEnvironmentDescriptor,
686
auth: ServerAuthDescriptor,
687
cwd: TrimmedNonEmptyString,
688
+ /** The machine's own name, which file URIs from its shells may carry. Older servers omit it. */
689
+ hostname: Schema.optionalKey(Schema.String),
690
keybindingsConfigPath: TrimmedNonEmptyString,
691
keybindings: ResolvedKeybindingsConfig,
692
issues: ServerConfigIssues,
packages/shared/src/fileLinks.test.ts
+14
-3
@@ -87,10 +87,21 @@ describe("parseFileUrlHref", () => {
87
"D:/Programme/t3code/OpenInPicker.tsx",
88
"#L69",
89
],
90
- ["file://server/share/workspace-image.svg", "\\\\server\\share\\workspace-image.svg", ""],
90
+ [
91
+ "file://server/share/workspace-image.svg",
92
+ "\\\\server\\share\\workspace-image.svg",
93
+ "",
94
+ "box-node",
95
+ ],
96
["file://localhost/home/me/notes.md", "/home/me/notes.md", ""],
92
- ])("parses %s", (href, path, hash) => {
93
- expect(parseFileUrlHref(href)).toEqual({ path, hash });
97
+ // GNU `ls --hyperlink` names the machine's own hostname.
98
+ ["file://box-node/var/tmp/report.txt", "/var/tmp/report.txt", "", "Box-Node"],
99
+ ["file://1234/var/tmp/report.txt", "/var/tmp/report.txt", "", "1234"],
100
+ ["file://böx/var/tmp/report.txt", "/var/tmp/report.txt", "", "böx"],
101
+ ["file://server/share/report.txt", "\\\\server\\share\\report.txt", "", "09"],
102
+ ["file://a/share/report.txt", "\\\\a\\share\\report.txt", "", "a#b"],
103
+ ])("parses %s", (href, path, hash, localHostname?: string) => {
104
+ expect(parseFileUrlHref(href, localHostname)).toEqual({ path, hash });
105
});
106
107
it("keeps percent-encoding so the caller decodes once", () => {
packages/shared/src/fileLinks.ts
+15
-2
@@ -9,9 +9,13 @@ const POSITION_SUFFIX_CAPTURE_PATTERN = /:(\d+)(?::(\d+)|-(\d+))?$/;
9
10
const POSITION_HASH_PATTERN = /^#L(\d+)(?:C(\d+)|-L?(\d+))?$/i;
11
12
-/** Keeps paths percent-encoded to avoid double decoding; non-localhost authorities become UNC shares. */
12
+/**
13
+ * Keeps paths percent-encoded to avoid double decoding. `localhost` and `localHostname` (the
14
+ * machine the path lives on) are local authorities; any other becomes a UNC share.
15
+ */
16
export function parseFileUrlHref(
17
href: string,
18
+ localHostname?: string,
19
): { readonly path: string; readonly hash: string } | null {
20
// Chromium on Windows parses any one-letter scheme as a drive letter
21
// (`new URL("d:x")` is `file:///D:/x`), so the protocol check alone would turn
@@ -21,7 +25,16 @@ export function parseFileUrlHref(
25
const parsed = new URL(href);
26
if (parsed.protocol.toLowerCase() !== "file:") return null;
27
24
- const uncHostname = parsed.hostname.toLowerCase() === "localhost" ? "" : parsed.hostname;
28
+ const host = parsed.hostname.toLowerCase();
29
+ let localHost = "";
30
+ if (host && localHostname) {
31
+ try {
32
+ localHost = new URL(`file://${encodeURIComponent(localHostname)}/`).hostname;
33
+ } catch {
34
+ // System hostnames need not be valid URL authorities; they cannot match a file URI.
35
+ }
36
+ }
37
+ const uncHostname = host === "localhost" || host === localHost ? "" : parsed.hostname;
38
const path = uncHostname
39
? `\\\\${uncHostname}${parsed.pathname.replaceAll("/", "\\")}`
40
: parsed.pathname;
scripts/scenarios/terminal-hostname-file-link-opens-its-file.e2e.ts
new
+66
@@ -0,0 +1,66 @@
1
+/// <reference lib="dom" />
2
+// The DOM lib types the function this scenario runs in the page.
3
+import { expect } from "e2e";
4
+import * as Effect from "effect/Effect";
5
+import * as FileSystem from "effect/FileSystem";
6
+
7
+import { T3_HOME } from "../e2e.config.ts";
8
+import { runPromise, test, WEB } from "./t3.ts";
9
+
10
+const EDITOR_ARGS = `${T3_HOME}/editor-args.txt`;
11
+
12
+/** The file's lines, or none while it does not exist. */
13
+const readLines = (file: string) =>
14
+ runPromise(
15
+ Effect.gen(function* () {
16
+ const fs = yield* FileSystem.FileSystem;
17
+ if (!(yield* fs.exists(file))) return [];
18
+ return (yield* fs.readFileString(file)).trim().split("\n");
19
+ }),
20
+ );
21
+
22
+test(
23
+ "Ctrl+click on a GNU ls --hyperlink filename, whose file URI names this machine, opens that file",
24
+ WEB,
25
+ async ({ t3, app, browser, screen }) => {
26
+ await runPromise(
27
+ Effect.flatMap(FileSystem.FileSystem, (fs) => fs.remove(EDITOR_ARGS, { force: true })),
28
+ );
29
+ const project = await t3.project("demo", { "one.txt": "hello\n" });
30
+ const thread = await t3.thread({ project, title: "List", messages: ["List it"] });
31
+ await t3.open(thread);
32
+
33
+ await screen.getByRole("button", "Toggle terminal drawer").tap();
34
+ const input = screen.getByRole("textbox", "Terminal input");
35
+ // The input mounts before fonts and WASM finish loading and its handlers are installed.
36
+ await expect(input).toHaveAttribute("data-workspace-focus-default", "");
37
+ await input.focus();
38
+ // GNU ls writes `file://<hostname>/<path>`. Enough rows to scroll, so every row is the link.
39
+ await browser.keyboard.type(
40
+ "for i in $(seq 1 100); do command ls --hyperlink=always --color=never -1 one.txt; done; touch ls.done\n",
41
+ );
42
+ await expect.poll(() => readLines(`${project}/ls.done`), { timeout: 30_000 }).toEqual([""]);
43
+ await browser.setViewport({ width: 1000, height: 700 });
44
+ await app.screenshot("listed-and-resized");
45
+
46
+ // The terminal draws on a canvas, so point at the first cells of its middle row.
47
+ const canvas = await browser.evaluate(() => {
48
+ const box = document
49
+ .querySelector('[aria-label="Terminal input"]')
50
+ ?.parentElement?.querySelector("canvas")
51
+ ?.getBoundingClientRect();
52
+ return box ? { x: box.x, y: box.y, width: box.width, height: box.height } : null;
53
+ });
54
+ if (!canvas) throw new Error("The terminal canvas is not on the page.");
55
+ await browser.mouse.move(canvas.x + 16, canvas.y + canvas.height / 2);
56
+ await browser.keyboard.down("Control");
57
+ await browser.mouse.down();
58
+ await browser.mouse.up();
59
+ await browser.keyboard.up("Control");
60
+ await app.screenshot("after-ctrl-click");
61
+
62
+ await expect
63
+ .poll(() => readLines(EDITOR_ARGS), { timeout: 15_000 })
64
+ .toEqual(["--classic", `${project}/one.txt`]);
65
+ },
66
+);