Open History to see the last commit
1import { MAC_PERMISSION_SETTINGS_URLS } from "../permissions/MacPermission.ts";
2import {
3 isLocalEditorFileUrl,
4 REMOTE_CAPABLE_EDITOR_IDS,
5 remoteSchemeForEditor,
6 type SystemSettingsPane,
7} from "@t3tools/contracts";
8import * as Context from "effect/Context";
9import * as Effect from "effect/Effect";
10import * as Layer from "effect/Layer";
11import * as Option from "effect/Option";
12
13import * as Electron from "electron";
14
15// Remote editor deep links and local editor file links must reach the OS handler.
16const SAFE_WEB_PROTOCOLS = new Set(["http:", "https:"]);
17const REMOTE_EDITOR_PROTOCOLS = new Set(
18 REMOTE_CAPABLE_EDITOR_IDS.flatMap((id) => {
19 const scheme = remoteSchemeForEditor(id);
20 return scheme === undefined ? [] : [`${scheme}:`];
21 }),
22);
23
24// "Use T3 Code in another app" opens these apps (apps/web/src/components/mcp/mcpApps.ts). List
25// each exact link shape the wizard sends, never a whole scheme.
26const APP_LAUNCH_URLS = new Set(["grokbot://app"]);
27
28// Zed embeds [user@]host in the path; reject passwords and ports there.
29const ZED_SSH_PATHNAME = /^\/(?:[^/@:]+@)?[^/@:]+\/.*$/;
30
31const isRemoteEditorUrl = (url: URL) =>
32 REMOTE_EDITOR_PROTOCOLS.has(url.protocol) &&
33 url.username.length === 0 &&
34 url.password.length === 0 &&
35 (url.protocol === "zed:"
36 ? url.host === "ssh" && ZED_SSH_PATHNAME.test(url.pathname)
37 : url.protocol === "jetbrains:"
38 ? url.host === "gateway" &&
39 url.pathname === "/ssh/environment" &&
40 (url.searchParams.get("h") ?? "").length > 0
41 : url.host === "vscode-remote" &&
42 url.pathname.startsWith("/ssh-remote+") &&
43 url.pathname.length > "/ssh-remote+".length);
44
45export function parseSafeExternalUrl(rawUrl: unknown): Option.Option<string> {
46 if (typeof rawUrl !== "string") {
47 return Option.none();
48 }
49
50 try {
51 const url = new URL(rawUrl);
52 return SAFE_WEB_PROTOCOLS.has(url.protocol) ||
53 APP_LAUNCH_URLS.has(url.href) ||
54 isRemoteEditorUrl(url) ||
55 isLocalEditorFileUrl(url)
56 ? Option.some(url.href)
57 : Option.none();
58 } catch {
59 return Option.none();
60 }
61}
62
63export class ElectronShell extends Context.Service<
64 ElectronShell,
65 {
66 readonly openExternal: (rawUrl: unknown) => Effect.Effect<boolean>;
67 readonly openSystemSettings: (pane: SystemSettingsPane) => Effect.Effect<boolean>;
68 readonly copyText: (text: string) => Effect.Effect<void>;
69 }
70>()("@t3tools/desktop/electron/ElectronShell") {}
71
72/** @public */
73export const make = ElectronShell.of({
74 openExternal: Effect.fn("ElectronShell.openExternal")(function (rawUrl) {
75 return Option.match(parseSafeExternalUrl(rawUrl), {
76 onNone: () => Effect.succeed(false),
77 onSome: (externalUrl) =>
78 Effect.promise(() =>
79 Electron.shell.openExternal(externalUrl).then(
80 () => true,
81 () => false,
82 ),
83 ),
84 });
85 }),
86 openSystemSettings: Effect.fn("ElectronShell.openSystemSettings")(function (pane) {
87 return Effect.promise(() =>
88 Electron.shell.openExternal(MAC_PERMISSION_SETTINGS_URLS[pane]).then(
89 () => true,
90 () => false,
91 ),
92 );
93 }),
94 copyText: Effect.fn("ElectronShell.copyText")(function (text) {
95 return Effect.promise(() => Electron.clipboard.writeText(text).catch(() => undefined));
96 }),
97});
98
99export const layer = Layer.succeed(ElectronShell, make);