@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 04:45 UTC

main
ts 106 lines 3.49 KB
Raw
1 import { MAC_PERMISSION_SETTINGS_URLS } from "../permissions/MacPermission.ts";
2 import {
3 isLocalEditorFileUrl,
4 REMOTE_CAPABLE_EDITOR_IDS,
5 remoteSchemeForEditor,
6 type SystemSettingsPane,
7 } from "@t3tools/contracts";
8 import * as Context from "effect/Context";
9 import * as Effect from "effect/Effect";
10 import * as Layer from "effect/Layer";
11 import * as Option from "effect/Option";
12
13 import * as Electron from "electron";
14
15 // Remote editor deep links and local editor file links must reach the OS handler.
16 const SAFE_WEB_PROTOCOLS = new Set(["http:", "https:"]);
17 const 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.
26 const 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.
36 const ZED_SSH_PATHNAME = /^\/(?:[^/@:]+@)?[^/@:]+\/.*$/;
37
38 const 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
52 export 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 }
68 }
69
70 export 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 */
80 export 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
106 export const layer = Layer.succeed(ElectronShell, make);