Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ElectronShell.ts

106 lines3.5 KB

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). Match
25// each exact link shape the wizard sends, never a whole scheme.
26const isAppLaunchUrl = (url: URL) =>
27 url.protocol === "grokbot:" &&
28 url.host === "app" &&
29 url.pathname === "/v1/prompt" &&
30 url.username.length === 0 &&
31 url.password.length === 0 &&
32 url.hash.length === 0 &&
33 [...url.searchParams.keys()].join() === "text";
34​
35// Zed embeds [user@]host in the path; reject passwords and ports there.
36const ZED_SSH_PATHNAME = /^\/(?:[^/@:]+@)?[^/@:]+\/.*$/;
37​
38const isRemoteEditorUrl = (url: URL) =>
39 REMOTE_EDITOR_PROTOCOLS.has(url.protocol) &&
40 url.username.length === 0 &&
41 url.password.length === 0 &&
42 (url.protocol === "zed:"
43 ? url.host === "ssh" && ZED_SSH_PATHNAME.test(url.pathname)
44 : url.protocol === "jetbrains:"
45 ? url.host === "gateway" &&
46 url.pathname === "/ssh/environment" &&
47 (url.searchParams.get("h") ?? "").length > 0
48 : url.host === "vscode-remote" &&
49 url.pathname.startsWith("/ssh-remote+") &&
50 url.pathname.length > "/ssh-remote+".length);
51​
52export function parseSafeExternalUrl(rawUrl: unknown): Option.Option<string> {
53 if (typeof rawUrl !== "string") {
54 return Option.none();
55 }
56​
57 try {
58 const url = new URL(rawUrl);
59 return SAFE_WEB_PROTOCOLS.has(url.protocol) ||
60 isAppLaunchUrl(url) ||
61 isRemoteEditorUrl(url) ||
62 isLocalEditorFileUrl(url)
63 ? Option.some(url.href)
64 : Option.none();
65 } catch {
66 return Option.none();
67 }
69​
70export class ElectronShell extends Context.Service<
71 ElectronShell,
72 {
73 readonly openExternal: (rawUrl: unknown) => Effect.Effect<boolean>;
74 readonly openSystemSettings: (pane: SystemSettingsPane) => Effect.Effect<boolean>;
75 readonly copyText: (text: string) => Effect.Effect<void>;
76 }
77>()("@t3tools/desktop/electron/ElectronShell") {}
78​
79/** @public */
80export const make = ElectronShell.of({
81 openExternal: Effect.fn("ElectronShell.openExternal")(function (rawUrl) {
82 return Option.match(parseSafeExternalUrl(rawUrl), {
83 onNone: () => Effect.succeed(false),
84 onSome: (externalUrl) =>
85 Effect.promise(() =>
86 Electron.shell.openExternal(externalUrl).then(
87 () => true,
88 () => false,
89 ),
90 ),
91 });
92 }),
93 openSystemSettings: Effect.fn("ElectronShell.openSystemSettings")(function (pane) {
94 return Effect.promise(() =>
95 Electron.shell.openExternal(MAC_PERMISSION_SETTINGS_URLS[pane]).then(
96 () => true,
97 () => false,
98 ),
99 );
100 }),
101 copyText: Effect.fn("ElectronShell.copyText")(function (text) {
102 return Effect.promise(() => Electron.clipboard.writeText(text).catch(() => undefined));
103 }),
104});
105​
106export const layer = Layer.succeed(ElectronShell, make);