@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, 03:37 UTC

main
ts 347 lines 10.9 KB
Raw
1 import * as Schema from "effect/Schema";
2 import { PositiveInt, TrimmedNonEmptyString } from "./baseSchemas.ts";
3
4 export const EditorLaunchStyle = Schema.Literals(["direct-path", "goto", "line-column"]);
5 export type EditorLaunchStyle = typeof EditorLaunchStyle.Type;
6
7 type EditorDefinition = {
8 readonly id: string;
9 readonly label: string;
10 readonly commands: readonly [string, ...string[]] | null;
11 readonly baseArgs?: readonly string[];
12 readonly launchStyle: EditorLaunchStyle;
13 readonly remoteScheme?: string;
14 readonly jetbrainsProductCode?: string;
15 };
16
17 export const EDITORS = [
18 {
19 id: "cursor",
20 label: "Cursor",
21 commands: ["cursor"],
22 // File and workspace opens must target the IDE even when the Agents Window is active.
23 baseArgs: ["--classic"],
24 launchStyle: "goto",
25 remoteScheme: "cursor",
26 },
27 { id: "trae", label: "Trae", commands: ["trae"], launchStyle: "goto" },
28 { id: "kiro", label: "Kiro", commands: ["kiro"], baseArgs: ["ide"], launchStyle: "goto" },
29 {
30 id: "vscode",
31 label: "VS Code",
32 commands: ["code"],
33 launchStyle: "goto",
34 remoteScheme: "vscode",
35 },
36 {
37 id: "vscode-insiders",
38 label: "VS Code Insiders",
39 commands: ["code-insiders"],
40 launchStyle: "goto",
41 remoteScheme: "vscode-insiders",
42 },
43 {
44 id: "vscodium",
45 label: "VSCodium",
46 commands: ["codium"],
47 launchStyle: "goto",
48 remoteScheme: "vscodium",
49 },
50 {
51 id: "zed",
52 label: "Zed",
53 commands: ["zed", "zeditor"],
54 launchStyle: "direct-path",
55 remoteScheme: "zed",
56 },
57 {
58 id: "antigravity",
59 label: "Antigravity",
60 // The IDE ships antigravity-ide; agy is the standalone CLI.
61 commands: ["antigravity-ide", "agy-ide"],
62 launchStyle: "goto",
63 },
64 {
65 id: "idea",
66 label: "IntelliJ IDEA",
67 commands: ["idea"],
68 launchStyle: "line-column",
69 jetbrainsProductCode: "IU",
70 },
71 {
72 id: "aqua",
73 label: "Aqua",
74 commands: ["aqua"],
75 launchStyle: "line-column",
76 jetbrainsProductCode: "QA",
77 },
78 {
79 id: "clion",
80 label: "CLion",
81 commands: ["clion"],
82 launchStyle: "line-column",
83 jetbrainsProductCode: "CL",
84 },
85 {
86 id: "datagrip",
87 label: "DataGrip",
88 commands: ["datagrip"],
89 launchStyle: "line-column",
90 jetbrainsProductCode: "DB",
91 },
92 {
93 id: "dataspell",
94 label: "DataSpell",
95 commands: ["dataspell"],
96 launchStyle: "line-column",
97 jetbrainsProductCode: "DS",
98 },
99 {
100 id: "goland",
101 label: "GoLand",
102 commands: ["goland"],
103 launchStyle: "line-column",
104 jetbrainsProductCode: "GO",
105 },
106 {
107 id: "phpstorm",
108 label: "PhpStorm",
109 commands: ["phpstorm"],
110 launchStyle: "line-column",
111 jetbrainsProductCode: "PS",
112 },
113 {
114 id: "pycharm",
115 label: "PyCharm",
116 commands: ["pycharm"],
117 launchStyle: "line-column",
118 jetbrainsProductCode: "PY",
119 },
120 {
121 id: "rider",
122 label: "Rider",
123 commands: ["rider"],
124 launchStyle: "line-column",
125 jetbrainsProductCode: "RD",
126 },
127 {
128 id: "rubymine",
129 label: "RubyMine",
130 commands: ["rubymine"],
131 launchStyle: "line-column",
132 jetbrainsProductCode: "RM",
133 },
134 {
135 id: "rustrover",
136 label: "RustRover",
137 commands: ["rustrover"],
138 launchStyle: "line-column",
139 jetbrainsProductCode: "RR",
140 },
141 {
142 id: "webstorm",
143 label: "WebStorm",
144 commands: ["webstorm"],
145 launchStyle: "line-column",
146 jetbrainsProductCode: "WS",
147 },
148 { id: "file-manager", label: "File Manager", commands: null, launchStyle: "direct-path" },
149 ] as const satisfies ReadonlyArray<EditorDefinition>;
150
151 export const EditorId = Schema.Literals(EDITORS.map((e) => e.id));
152 export type EditorId = typeof EditorId.Type;
153
154 export const FileManagerRevealKind = Schema.Literals(["finder", "file-explorer", "files"]);
155 export type FileManagerRevealKind = typeof FileManagerRevealKind.Type;
156
157 export const LaunchEditorInput = Schema.Struct({
158 cwd: TrimmedNonEmptyString,
159 editor: EditorId,
160 /** When present, cwd is an exact path; {} opens it without a position. Omit for diagnostic suffix parsing. */
161 position: Schema.optional(
162 Schema.Struct({
163 line: Schema.optional(PositiveInt),
164 column: Schema.optional(PositiveInt),
165 }),
166 ),
167 /** Workspace the target belongs to; JetBrains IDEs open positioned files in this project. */
168 projectRoot: Schema.optional(TrimmedNonEmptyString),
169 /** Only for file-manager; requires the server's shellRevealInFileManager capability. */
170 reveal: Schema.optional(Schema.Boolean),
171 });
172 export type LaunchEditorInput = typeof LaunchEditorInput.Type;
173
174 const remoteSchemeOf = (editor: EditorDefinition): string | undefined =>
175 editor.remoteScheme ?? (editor.jetbrainsProductCode === undefined ? undefined : "jetbrains");
176
177 export const REMOTE_CAPABLE_EDITOR_IDS: ReadonlyArray<EditorId> = EDITORS.flatMap((editor) =>
178 remoteSchemeOf(editor) !== undefined ? [editor.id] : [],
179 );
180
181 export const remoteSchemeForEditor = (id: EditorId): string | undefined => {
182 const editor = EDITORS.find((candidate) => candidate.id === id);
183 return editor === undefined ? undefined : remoteSchemeOf(editor);
184 };
185
186 // `goto` editors share VS Code's `file:line:col` addressing, so their remote schemes also open
187 // a local file as `<scheme>://file/<absolute path>[:line[:col]]`. Zed and JetBrains differ.
188 export const LOCAL_FILE_LINK_SCHEMES: ReadonlyArray<string> = EDITORS.flatMap((editor) =>
189 editor.launchStyle === "goto" && "remoteScheme" in editor ? [editor.remoteScheme] : [],
190 );
191
192 export const isLocalEditorFileUrl = (url: URL): boolean =>
193 LOCAL_FILE_LINK_SCHEMES.includes(url.protocol.slice(0, -1)) &&
194 url.host === "file" &&
195 url.username.length === 0 &&
196 url.password.length === 0 &&
197 url.pathname.length > 1;
198
199 export const buildRemoteOpenUrl = (input: {
200 readonly editor: EditorId;
201 readonly host: string;
202 readonly username?: string;
203 readonly absolutePath: string;
204 readonly isFile?: boolean;
205 }): string | undefined => {
206 const scheme = remoteSchemeForEditor(input.editor);
207 if (scheme === undefined) {
208 return undefined;
209 }
210 const editor = EDITORS.find((candidate) => candidate.id === input.editor);
211 if (editor !== undefined && "jetbrainsProductCode" in editor) {
212 const params = new URLSearchParams({
213 h: input.host,
214 launchIde: "true",
215 ideHint: editor.jetbrainsProductCode,
216 projectHint: input.absolutePath.replaceAll("\\", "/"),
217 });
218 if (input.username !== undefined) {
219 params.set("u", input.username);
220 }
221 return `${scheme}://gateway/ssh/environment?${params.toString()}`;
222 }
223 // Windows server paths (`C:\...`) appear as `/C:/...` in vscode-remote URIs.
224 const posixPath = input.absolutePath.replaceAll("\\", "/");
225 const rootedPath = posixPath.startsWith("/") ? posixPath : `/${posixPath}`;
226 const encodedHost = encodeURIComponent(input.host);
227 if (input.editor === "zed") {
228 // Zed roots paths on the system drive, so omit the Windows drive prefix.
229 const zedPath = /^[Cc]:[\\/]/.test(input.absolutePath) ? rootedPath.slice(3) : rootedPath;
230 const encodedZedPath = zedPath.split("/").map(encodeURIComponent).join("/");
231 // Zed reads this segment as ssh URL userinfo, so the `@` stays literal.
232 const zedDestination =
233 input.username === undefined
234 ? encodedHost
235 : `${encodeURIComponent(input.username)}@${encodedHost}`;
236 return `${scheme}://ssh/${zedDestination}${encodedZedPath}`;
237 }
238 const encodedPath = rootedPath.split("/").map(encodeURIComponent).join("/");
239 const plainDestination =
240 input.username === undefined ? input.host : `${input.username}@${input.host}`;
241 // Remote-SSH splits the decoded authority on "+", so a destination containing one must use its
242 // hex-encoded JSON form instead.
243 const destination = plainDestination.includes("+")
244 ? Array.from(
245 new TextEncoder().encode(JSON.stringify({ hostName: input.host, user: input.username })),
246 (byte) => byte.toString(16).padStart(2, "0"),
247 ).join("")
248 : plainDestination;
249 // VS Code opens a bare vscode-remote path as a folder; a trailing position makes it open the file.
250 return `${scheme}://vscode-remote/ssh-remote+${encodeURIComponent(destination)}${encodedPath}${input.isFile ? ":1:1" : ""}`;
251 };
252
253 export const RemoteOpenTargetKind = Schema.Literals(["tailscale", "mdns"]);
254 export type RemoteOpenTargetKind = typeof RemoteOpenTargetKind.Type;
255
256 export const RemoteOpenTarget = Schema.Struct({
257 kind: RemoteOpenTargetKind,
258 host: TrimmedNonEmptyString,
259 username: Schema.optionalKey(TrimmedNonEmptyString),
260 });
261 export type RemoteOpenTarget = typeof RemoteOpenTarget.Type;
262
263 export class ExternalLauncherUnknownEditorError extends Schema.TaggedError<ExternalLauncherUnknownEditorError>()(
264 "ExternalLauncherUnknownEditorError",
265 {
266 editor: Schema.String,
267 },
268 ) {
269 override get message(): string {
270 return `Unknown editor: ${this.editor}`;
271 }
272 }
273
274 export class ExternalLauncherUnsupportedEditorError extends Schema.TaggedError<ExternalLauncherUnsupportedEditorError>()(
275 "ExternalLauncherUnsupportedEditorError",
276 {
277 editor: EditorId,
278 },
279 ) {
280 override get message(): string {
281 return `Unsupported editor: ${this.editor}`;
282 }
283 }
284
285 export class ExternalLauncherCommandNotFoundError extends Schema.TaggedError<ExternalLauncherCommandNotFoundError>()(
286 "ExternalLauncherCommandNotFoundError",
287 {
288 editor: EditorId,
289 command: Schema.String,
290 },
291 ) {
292 override get message(): string {
293 return `Editor command not found: ${this.command}`;
294 }
295 }
296
297 export class ExternalLauncherUnsupportedTargetError extends Schema.TaggedError<ExternalLauncherUnsupportedTargetError>()(
298 "ExternalLauncherUnsupportedTargetError",
299 {
300 editor: EditorId,
301 },
302 ) {
303 override get message(): string {
304 return `${this.editor} cannot open a path containing line breaks or double quotes`;
305 }
306 }
307
308 const ExternalLauncherSpawnFields = {
309 command: Schema.String,
310 args: Schema.Array(Schema.String),
311 cause: Schema.Defect(),
312 };
313
314 export class ExternalLauncherBrowserSpawnError extends Schema.TaggedError<ExternalLauncherBrowserSpawnError>()(
315 "ExternalLauncherBrowserSpawnError",
316 {
317 ...ExternalLauncherSpawnFields,
318 target: Schema.String,
319 },
320 ) {
321 override get message(): string {
322 return `Failed to launch browser target '${this.target}' with '${[this.command, ...this.args].join(" ")}'`;
323 }
324 }
325
326 export class ExternalLauncherEditorSpawnError extends Schema.TaggedError<ExternalLauncherEditorSpawnError>()(
327 "ExternalLauncherEditorSpawnError",
328 {
329 ...ExternalLauncherSpawnFields,
330 editor: EditorId,
331 target: Schema.String,
332 },
333 ) {
334 override get message(): string {
335 return `Failed to launch '${this.target}' in ${this.editor} with '${[this.command, ...this.args].join(" ")}'`;
336 }
337 }
338
339 export const ExternalLauncherError = Schema.Union([
340 ExternalLauncherUnknownEditorError,
341 ExternalLauncherUnsupportedEditorError,
342 ExternalLauncherCommandNotFoundError,
343 ExternalLauncherUnsupportedTargetError,
344 ExternalLauncherBrowserSpawnError,
345 ExternalLauncherEditorSpawnError,
346 ]);
347 export type ExternalLauncherError = typeof ExternalLauncherError.Type;