The best way to control Claude Code, Codex and more from any computer or phone

main
ts 111 lines 4.51 KB
Raw
1 import { DesktopWslStateSchema, type DesktopWslState } from "@t3tools/contracts";
2 import * as Effect from "effect/Effect";
3 import * as Option from "effect/Option";
4 import * as Schema from "effect/Schema";
5
6 import * as DesktopLifecycle from "../../app/DesktopLifecycle.ts";
7 import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts";
8 import * as DesktopWslBackend from "../../wsl/DesktopWslBackend.ts";
9 import * as DesktopWslEnvironment from "../../wsl/DesktopWslEnvironment.ts";
10 import * as IpcChannels from "../channels.ts";
11 import { makeIpcMethod } from "../DesktopIpc.ts";
12
13 const readWslState: Effect.Effect<
14 DesktopWslState,
15 never,
16 | DesktopAppSettings.DesktopAppSettings
17 | DesktopWslEnvironment.DesktopWslEnvironment
18 | DesktopWslBackend.DesktopWslBackend
19 > = Effect.gen(function* () {
20 const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
21 const wslEnvironment = yield* DesktopWslEnvironment.DesktopWslEnvironment;
22 const wslBackend = yield* DesktopWslBackend.DesktopWslBackend;
23 const settings = yield* appSettings.get;
24 const available = settings.localEnvironmentEnabled && (yield* wslEnvironment.isAvailable);
25 // Listing distros on non-WSL hosts spawns wsl.exe only to time out.
26 const distros = available ? yield* wslEnvironment.listDistros : [];
27 const preflightError = yield* wslBackend.lastPreflightError;
28 return {
29 enabled: settings.wslBackendEnabled,
30 distro: settings.wslDistro,
31 available,
32 wslOnly: settings.wslOnly,
33 distros,
34 preflightError: settings.wslOnly ? null : Option.getOrNull(preflightError),
35 };
36 });
37
38 export const getWslState = makeIpcMethod({
39 channel: IpcChannels.GET_WSL_STATE_CHANNEL,
40 payload: Schema.Void,
41 result: DesktopWslStateSchema,
42 handler: Effect.fn("desktop.ipc.wsl.getState")(function* () {
43 return yield* readWslState;
44 }),
45 });
46
47 export const setWslBackendEnabled = makeIpcMethod({
48 channel: IpcChannels.SET_WSL_BACKEND_ENABLED_CHANNEL,
49 payload: Schema.Boolean,
50 result: DesktopWslStateSchema,
51 handler: Effect.fn("desktop.ipc.wsl.setEnabled")(function* (enabled) {
52 const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
53 const wslBackend = yield* DesktopWslBackend.DesktopWslBackend;
54 const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
55 const previousSettings = yield* appSettings.get;
56 const updateSettings = enabled
57 ? appSettings.setWslBackendEnabled(true)
58 : appSettings.applyWslWindowsFallback;
59 const change = yield* updateSettings;
60 const settings = yield* appSettings.get;
61 const changedWslOnlyPrimary = enabled
62 ? settings.wslOnly
63 : previousSettings.wslBackendEnabled && previousSettings.wslOnly;
64 if (changedWslOnlyPrimary && change.changed) {
65 const state = yield* readWslState;
66 yield* lifecycle.relaunch(`wslBackendEnabled=${enabled}`);
67 return state;
68 }
69 // Reconcile never fails, so no rollback is needed.
70 yield* wslBackend.reconcile;
71 return yield* readWslState;
72 }),
73 });
74
75 export const setWslDistro = makeIpcMethod({
76 channel: IpcChannels.SET_WSL_DISTRO_CHANNEL,
77 payload: Schema.NullOr(Schema.String),
78 result: DesktopWslStateSchema,
79 handler: Effect.fn("desktop.ipc.wsl.setDistro")(function* (distro) {
80 const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
81 const wslBackend = yield* DesktopWslBackend.DesktopWslBackend;
82 const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
83 const change = yield* appSettings.setWslDistro(distro);
84 const settings = yield* appSettings.get;
85 // WSL-only primary backends capture their distro at startup; relaunch to change it.
86 if (settings.wslBackendEnabled && settings.wslOnly && change.changed) {
87 const state = yield* readWslState;
88 yield* lifecycle.relaunch(`wslDistro=${distro ?? "default"}`);
89 return state;
90 }
91 yield* wslBackend.reconcile;
92 return yield* readWslState;
93 }),
94 });
95
96 export const setWslOnly = makeIpcMethod({
97 channel: IpcChannels.SET_WSL_ONLY_CHANNEL,
98 payload: Schema.Boolean,
99 result: DesktopWslStateSchema,
100 handler: Effect.fn("desktop.ipc.wsl.setOnly")(function* (enabled) {
101 // The pool captures its primary at initialization; disabled WSL can stage mode changes until enable.
102 const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
103 const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
104 const change = yield* appSettings.setWslOnly(enabled);
105 const state = yield* readWslState;
106 if (state.enabled && change.changed) {
107 yield* lifecycle.relaunch(`wslOnly=${enabled}`);
108 }
109 return state;
110 }),
111 });