Lets the desktop app launch the local backend inside a WSL distro instead of natively on Windows. Adds: - Backend plumbing (apps/desktop/src/wsl): pure path parsing utilities, a DesktopWslEnvironment Effect service wrapping wsl.exe operations (listDistros, preWarm, windowsToWslPath, ensureNodePty, isAvailable), and an explicit preflight that checks for missing node / build tools before spawning so the failure message names the actual problem. - Spawn path: DesktopBackendConfiguration branches on the new wslMode setting and assembles "wsl.exe -d <distro> -- node <linux-entry> --bootstrap-fd 0" with the bootstrap envelope on stdin (wsl.exe drops additional file descriptors). Sensitive env vars forward via WSLENV; --dev-url is passed as a CLI flag so the WSL dev backend lands in dev/ instead of userdata/ deterministi- cally. The Windows-side T3CODE_HOME is scrubbed and extendEnv is disabled for WSL so the WSL backend cannot accidentally share a baseDir with the local backend via /mnt/c/... - Settings: wslMode + wslDistro on DesktopAppSettings, with validation that drops distro names containing control or shell meta characters. Contracts get DesktopWslMode / DesktopWslDistro / DesktopWslState schemas. - IPC: getWslState and setWslBackend on the desktop bridge. The setter pre-warms the WSL VM, persists settings, then drives an in-process backend stop + start with a 2-minute readiness wait and a rollback path that reverts to the previous mode if the new backend never reports ready. pickFolder defaults to the WSL home UNC path when wslMode is "wsl". - Web UI: backend-runtime selector in Connection Settings with a three-stage swap modal (restarting / re-establishing session / syncing) that suppresses the WS reconnect toast for the duration of the swap, waits for the new backend's welcome event before closing, and clears the previous env's store state so the side- bar does not render stale threads. New suppressReconnect helper on the connection-status atom plus exports for the descriptor refresh and reauth used by the swap flow.
130 lines
6.1 KiB
TypeScript
130 lines
6.1 KiB
TypeScript
import type { DesktopBridge } from "@t3tools/contracts";
|
|
import { contextBridge, ipcRenderer } from "electron";
|
|
|
|
import * as IpcChannels from "./ipc/channels.ts";
|
|
|
|
function unwrapEnsureSshEnvironmentResult(result: unknown) {
|
|
if (
|
|
typeof result === "object" &&
|
|
result !== null &&
|
|
"type" in result &&
|
|
result.type === IpcChannels.SSH_PASSWORD_PROMPT_CANCELLED_RESULT
|
|
) {
|
|
const message =
|
|
"message" in result && typeof result.message === "string"
|
|
? result.message
|
|
: "SSH authentication cancelled.";
|
|
throw new Error(message);
|
|
}
|
|
return result as Awaited<ReturnType<DesktopBridge["ensureSshEnvironment"]>>;
|
|
}
|
|
|
|
contextBridge.exposeInMainWorld("desktopBridge", {
|
|
getAppBranding: () => {
|
|
const result = ipcRenderer.sendSync(IpcChannels.GET_APP_BRANDING_CHANNEL);
|
|
if (typeof result !== "object" || result === null) {
|
|
return null;
|
|
}
|
|
return result as ReturnType<DesktopBridge["getAppBranding"]>;
|
|
},
|
|
getLocalEnvironmentBootstrap: () => {
|
|
const result = ipcRenderer.sendSync(IpcChannels.GET_LOCAL_ENVIRONMENT_BOOTSTRAP_CHANNEL);
|
|
if (typeof result !== "object" || result === null) {
|
|
return null;
|
|
}
|
|
return result as ReturnType<DesktopBridge["getLocalEnvironmentBootstrap"]>;
|
|
},
|
|
getClientSettings: () => ipcRenderer.invoke(IpcChannels.GET_CLIENT_SETTINGS_CHANNEL),
|
|
setClientSettings: (settings) =>
|
|
ipcRenderer.invoke(IpcChannels.SET_CLIENT_SETTINGS_CHANNEL, settings),
|
|
getSavedEnvironmentRegistry: () =>
|
|
ipcRenderer.invoke(IpcChannels.GET_SAVED_ENVIRONMENT_REGISTRY_CHANNEL),
|
|
setSavedEnvironmentRegistry: (records) =>
|
|
ipcRenderer.invoke(IpcChannels.SET_SAVED_ENVIRONMENT_REGISTRY_CHANNEL, records),
|
|
getSavedEnvironmentSecret: (environmentId) =>
|
|
ipcRenderer.invoke(IpcChannels.GET_SAVED_ENVIRONMENT_SECRET_CHANNEL, environmentId),
|
|
setSavedEnvironmentSecret: (environmentId, secret) =>
|
|
ipcRenderer.invoke(IpcChannels.SET_SAVED_ENVIRONMENT_SECRET_CHANNEL, { environmentId, secret }),
|
|
removeSavedEnvironmentSecret: (environmentId) =>
|
|
ipcRenderer.invoke(IpcChannels.REMOVE_SAVED_ENVIRONMENT_SECRET_CHANNEL, environmentId),
|
|
discoverSshHosts: () => ipcRenderer.invoke(IpcChannels.DISCOVER_SSH_HOSTS_CHANNEL),
|
|
ensureSshEnvironment: async (target, options) =>
|
|
unwrapEnsureSshEnvironmentResult(
|
|
await ipcRenderer.invoke(IpcChannels.ENSURE_SSH_ENVIRONMENT_CHANNEL, {
|
|
target,
|
|
...(options === undefined ? {} : { options }),
|
|
}),
|
|
),
|
|
disconnectSshEnvironment: (target) =>
|
|
ipcRenderer.invoke(IpcChannels.DISCONNECT_SSH_ENVIRONMENT_CHANNEL, target),
|
|
fetchSshEnvironmentDescriptor: (httpBaseUrl) =>
|
|
ipcRenderer.invoke(IpcChannels.FETCH_SSH_ENVIRONMENT_DESCRIPTOR_CHANNEL, { httpBaseUrl }),
|
|
bootstrapSshBearerSession: (httpBaseUrl, credential) =>
|
|
ipcRenderer.invoke(IpcChannels.BOOTSTRAP_SSH_BEARER_SESSION_CHANNEL, {
|
|
httpBaseUrl,
|
|
credential,
|
|
}),
|
|
fetchSshSessionState: (httpBaseUrl, bearerToken) =>
|
|
ipcRenderer.invoke(IpcChannels.FETCH_SSH_SESSION_STATE_CHANNEL, { httpBaseUrl, bearerToken }),
|
|
issueSshWebSocketToken: (httpBaseUrl, bearerToken) =>
|
|
ipcRenderer.invoke(IpcChannels.ISSUE_SSH_WEBSOCKET_TOKEN_CHANNEL, { httpBaseUrl, bearerToken }),
|
|
onSshPasswordPrompt: (listener) => {
|
|
const wrappedListener = (_event: Electron.IpcRendererEvent, request: unknown) => {
|
|
if (typeof request !== "object" || request === null) return;
|
|
listener(request as Parameters<typeof listener>[0]);
|
|
};
|
|
|
|
ipcRenderer.on(IpcChannels.SSH_PASSWORD_PROMPT_CHANNEL, wrappedListener);
|
|
return () => {
|
|
ipcRenderer.removeListener(IpcChannels.SSH_PASSWORD_PROMPT_CHANNEL, wrappedListener);
|
|
};
|
|
},
|
|
resolveSshPasswordPrompt: (requestId, password) =>
|
|
ipcRenderer.invoke(IpcChannels.RESOLVE_SSH_PASSWORD_PROMPT_CHANNEL, { requestId, password }),
|
|
getServerExposureState: () => ipcRenderer.invoke(IpcChannels.GET_SERVER_EXPOSURE_STATE_CHANNEL),
|
|
setServerExposureMode: (mode) =>
|
|
ipcRenderer.invoke(IpcChannels.SET_SERVER_EXPOSURE_MODE_CHANNEL, mode),
|
|
setTailscaleServeEnabled: (input) =>
|
|
ipcRenderer.invoke(IpcChannels.SET_TAILSCALE_SERVE_ENABLED_CHANNEL, input),
|
|
getAdvertisedEndpoints: () => ipcRenderer.invoke(IpcChannels.GET_ADVERTISED_ENDPOINTS_CHANNEL),
|
|
getWslState: () => ipcRenderer.invoke(IpcChannels.GET_WSL_STATE_CHANNEL),
|
|
setWslBackend: (input) => ipcRenderer.invoke(IpcChannels.SET_WSL_BACKEND_CHANNEL, input),
|
|
pickFolder: (options) => ipcRenderer.invoke(IpcChannels.PICK_FOLDER_CHANNEL, options),
|
|
confirm: (message) => ipcRenderer.invoke(IpcChannels.CONFIRM_CHANNEL, message),
|
|
setTheme: (theme) => ipcRenderer.invoke(IpcChannels.SET_THEME_CHANNEL, theme),
|
|
showContextMenu: (items, position) =>
|
|
ipcRenderer.invoke(IpcChannels.CONTEXT_MENU_CHANNEL, {
|
|
items,
|
|
...(position === undefined ? {} : { position }),
|
|
}),
|
|
openExternal: (url: string) => ipcRenderer.invoke(IpcChannels.OPEN_EXTERNAL_CHANNEL, url),
|
|
onMenuAction: (listener) => {
|
|
const wrappedListener = (_event: Electron.IpcRendererEvent, action: unknown) => {
|
|
if (typeof action !== "string") return;
|
|
listener(action);
|
|
};
|
|
|
|
ipcRenderer.on(IpcChannels.MENU_ACTION_CHANNEL, wrappedListener);
|
|
return () => {
|
|
ipcRenderer.removeListener(IpcChannels.MENU_ACTION_CHANNEL, wrappedListener);
|
|
};
|
|
},
|
|
getUpdateState: () => ipcRenderer.invoke(IpcChannels.UPDATE_GET_STATE_CHANNEL),
|
|
setUpdateChannel: (channel) =>
|
|
ipcRenderer.invoke(IpcChannels.UPDATE_SET_CHANNEL_CHANNEL, channel),
|
|
checkForUpdate: () => ipcRenderer.invoke(IpcChannels.UPDATE_CHECK_CHANNEL),
|
|
downloadUpdate: () => ipcRenderer.invoke(IpcChannels.UPDATE_DOWNLOAD_CHANNEL),
|
|
installUpdate: () => ipcRenderer.invoke(IpcChannels.UPDATE_INSTALL_CHANNEL),
|
|
onUpdateState: (listener) => {
|
|
const wrappedListener = (_event: Electron.IpcRendererEvent, state: unknown) => {
|
|
if (typeof state !== "object" || state === null) return;
|
|
listener(state as Parameters<typeof listener>[0]);
|
|
};
|
|
|
|
ipcRenderer.on(IpcChannels.UPDATE_STATE_CHANNEL, wrappedListener);
|
|
return () => {
|
|
ipcRenderer.removeListener(IpcChannels.UPDATE_STATE_CHANNEL, wrappedListener);
|
|
};
|
|
},
|
|
} satisfies DesktopBridge);
|