дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 01:31:41 +02:00
Docker commands moved from the desktop crate into the ide-host project_host domain, so a remote window reaches the Docker daemon on the remote host through openidea-server instead of the local one. The SSH tunnel reuses the existing resolve logic, docker_executable only caches a found path, and the Services store drops event streams, timers and in-flight requests when the host changes. Co-authored-by: dengliu <[email protected]>
499 рядки
19 KiB
TypeScript
499 рядки
19 KiB
TypeScript
import { confirmProjectTrust } from "./projectTrust";
|
||
/**
|
||
* Remote development: in a window whose project lives on another machine, the
|
||
* commands that machine's `openidea-server` answers are routed to it (through
|
||
* the `remote_invoke` command) instead of running locally. Routing happens below
|
||
* the generated bindings, so features call `commands.*` as usual.
|
||
*/
|
||
import { Channel, type InvokeArgs, type InvokeOptions } from "@tauri-apps/api/core";
|
||
import { invokeLocal, setInvokeRoute, type InvokeRoute } from "./tauriCore";
|
||
import { create } from "zustand";
|
||
import {
|
||
call,
|
||
commands,
|
||
IpcError,
|
||
isTauri,
|
||
type ChannelFrame,
|
||
type RemoteProgress,
|
||
type RemoteProject,
|
||
type RemoteSession,
|
||
type RemoteTarget,
|
||
} from "./ipc";
|
||
import { isClientSetting, overlayClientSettings } from "./clientSettings";
|
||
import type { Settings } from "./ipc";
|
||
import { usePlatform } from "./platformInfo";
|
||
import { useProject } from "./project";
|
||
|
||
interface RemoteState {
|
||
session: RemoteSession | null;
|
||
/** The host the connection was lost to (the window's project is unreachable), else null. */
|
||
lost: string | null;
|
||
takenOver: boolean;
|
||
/** A reconnection to the lost host is in progress. */
|
||
reconnecting: boolean;
|
||
}
|
||
|
||
export const useRemote = create<RemoteState>(() => ({
|
||
session: null,
|
||
lost: null,
|
||
takenOver: false,
|
||
reconnecting: false,
|
||
}));
|
||
|
||
let routed = new Set<string>();
|
||
let nextChannel = 1;
|
||
const BUILD_MODEL_COMMANDS = new Set(["build_model", "build_sync", "gradle_link_projects", "gradle_unlink_projects"]);
|
||
|
||
/**
|
||
* The arguments with each `Channel` replaced by `{ $channel: id }`, and the
|
||
* channels by id (their values come back tagged with the id).
|
||
*/
|
||
export function encodeArgs(args: unknown): { json: string; channels: Map<number, Channel<unknown>> } {
|
||
const channels = new Map<number, Channel<unknown>>();
|
||
const plain: Record<string, unknown> = {};
|
||
if (args && typeof args === "object") {
|
||
for (const [k, v] of Object.entries(args as Record<string, unknown>)) {
|
||
if (v instanceof Channel) {
|
||
const id = nextChannel++;
|
||
channels.set(id, v as Channel<unknown>);
|
||
plain[k] = { $channel: id };
|
||
} else {
|
||
plain[k] = v;
|
||
}
|
||
}
|
||
}
|
||
return { json: JSON.stringify(plain), channels };
|
||
}
|
||
|
||
async function remoteInvoke(cmd: string, args?: InvokeArgs, options?: InvokeOptions): Promise<unknown> {
|
||
const { json, channels } = encodeArgs(args);
|
||
if (cmd === "db_execute_stream" || cmd === "vcs_watch_commit_messages" || cmd.startsWith("docker_")) {
|
||
// These callbacks are reached through onChannel; their own Tauri registrations are unused.
|
||
const runtime = "__TAURI_INTERNALS__" in window ? window.__TAURI_INTERNALS__ : undefined;
|
||
if (
|
||
runtime &&
|
||
typeof runtime === "object" &&
|
||
"unregisterCallback" in runtime &&
|
||
typeof runtime.unregisterCallback === "function"
|
||
) {
|
||
for (const channel of channels.values()) runtime.unregisterCallback(channel.id);
|
||
}
|
||
}
|
||
const onChannel = new Channel<ChannelFrame>();
|
||
onChannel.onmessage = (frame) => channels.get(frame.id)?.onmessage(JSON.parse(frame.data));
|
||
const result = await invokeLocal<string>("remote_invoke", { cmd, args: json, onChannel }, options);
|
||
const value = JSON.parse(result);
|
||
// Released servers predating independent Gradle builds return a single build.
|
||
if (BUILD_MODEL_COMMANDS.has(cmd) && value?.gradle && !Array.isArray(value.gradle)) {
|
||
value.gradle = [value.gradle];
|
||
}
|
||
return value;
|
||
}
|
||
|
||
function isMcpServerSetting(key: string): boolean {
|
||
return key === "mcpServer" || key.startsWith("mcpServer.");
|
||
}
|
||
|
||
/**
|
||
* Where a command of a remote window runs. Local always: opening a project (a
|
||
* local folder; leaving the remote one) and closing it (that drops the
|
||
* connection, not the backend). Client UI settings stay local.
|
||
* Host-specific service settings apply to the remote host only;
|
||
* other global tooling settings apply on both sides.
|
||
*/
|
||
export function remoteRouting(cmd: string, args: InvokeArgs | undefined, served: ReadonlySet<string>) {
|
||
const key = (args as { key?: string } | undefined)?.key ?? "";
|
||
if (cmd === "settings_set" && (key === "remoteDevelopment" || key.startsWith("remoteDevelopment."))) return "remote";
|
||
if (cmd === "system_font_families") return "local";
|
||
if (cmd === "settings_set" && isClientSetting(key)) return "local";
|
||
if (!served.has(cmd) || cmd === "open_project" || cmd === "close_project") return "local";
|
||
if (cmd === "settings_set" && isMcpServerSetting(key)) {
|
||
return served.has("mcp_server_status") ? "remote" : "local";
|
||
}
|
||
if (cmd === "settings_set" && (args as { scope?: string } | undefined)?.scope === "global") return "both";
|
||
return "remote";
|
||
}
|
||
|
||
/** This machine's own global settings (for desktop SSH configurations even in a remote window). */
|
||
export function localGlobalSettings(): Promise<Settings> {
|
||
return isTauri() ? invokeLocal<Settings>("settings_get_global") : commands.settingsGetGlobal();
|
||
}
|
||
|
||
/** All effective settings responses use the same client overlay, including writes and project snapshots. */
|
||
async function clientSettings<T extends Settings>(settings: T): Promise<T> {
|
||
return overlayClientSettings(settings, await invokeLocal<Settings>("settings_get_global"));
|
||
}
|
||
|
||
const route: InvokeRoute = (cmd, args, options) => {
|
||
if (cmd === "open_project" || cmd === "light_edit_open") {
|
||
return invokeLocal(cmd, args, options).then((result) => {
|
||
disableRemote();
|
||
return result;
|
||
});
|
||
}
|
||
const destination = remoteRouting(cmd, args, routed);
|
||
if (cmd === "settings_set") {
|
||
const setting = args as { scope: string; key: string; value: string };
|
||
return (async () => {
|
||
if (isClientSetting(setting.key)) {
|
||
await invokeLocal(cmd, { ...setting, scope: "global" }, options);
|
||
return clientSettings((await remoteInvoke("settings_get")) as Settings);
|
||
}
|
||
if (destination === "local") return invokeLocal(cmd, args, options);
|
||
if (destination === "both") await invokeLocal(cmd, args, options);
|
||
return clientSettings((await remoteInvoke(cmd, args, options)) as Settings);
|
||
})();
|
||
}
|
||
if (destination === "local") return null;
|
||
return remoteInvoke(cmd, args, options).then(async (result) => {
|
||
if (cmd === "settings_get" || cmd === "settings_get_global") return clientSettings(result as Settings);
|
||
if (cmd === "project_current" && result) {
|
||
const project = result as { settings: Settings };
|
||
return { ...project, settings: await clientSettings(project.settings) };
|
||
}
|
||
return result;
|
||
});
|
||
};
|
||
|
||
/**
|
||
* Routes `session.commands` to the server from now on: the commands this build's
|
||
* server answers, as the desktop backend knows them (not as the server reports them).
|
||
*/
|
||
export function enableRemote(session: RemoteSession) {
|
||
routed = new Set(session.commands);
|
||
const takenOver = session.takenOver === true;
|
||
setInvokeRoute(route);
|
||
useRemote.setState({ session, lost: takenOver ? session.host : null, takenOver, reconnecting: false });
|
||
}
|
||
|
||
export function disableRemote() {
|
||
routed = new Set();
|
||
setInvokeRoute(null);
|
||
useRemote.setState({ session: null, lost: null, takenOver: false, reconnecting: false });
|
||
}
|
||
|
||
export function isRemoteWindow(): boolean {
|
||
return useRemote.getState().session !== null;
|
||
}
|
||
|
||
/** Whether `path` is the project folder of this window's remote session. */
|
||
export function isRemoteRoot(path: string): boolean {
|
||
return useRemote.getState().session?.root === path;
|
||
}
|
||
|
||
/** The OS this window's commands run on: the remote server's, else this desktop's (`usePlatform`). */
|
||
export function hostOs(): string {
|
||
return useRemote.getState().session?.os ?? usePlatform.getState().os;
|
||
}
|
||
|
||
/** Re-attaches after a reload (the backend keeps the connection per window). */
|
||
export async function restoreRemote(): Promise<RemoteSession | null> {
|
||
if (!isTauri()) return null;
|
||
const session = await commands.remoteCurrent().catch(() => null);
|
||
if (session) enableRemote(session);
|
||
return session;
|
||
}
|
||
|
||
/** Where the connection in progress is, and to which location (`null`: none). */
|
||
export const useRemoteProgress = create<{ progress: RemoteProgress | null; location: string | null }>(() => ({
|
||
progress: null,
|
||
location: null,
|
||
}));
|
||
|
||
/** Routes this window to the server `remoteConnect` answered with and shows its project. */
|
||
async function attachRemote(r: RemoteProject, path: string): Promise<void> {
|
||
enableRemote({
|
||
target: r.target,
|
||
root: r.root,
|
||
host: r.host,
|
||
os: r.os,
|
||
commands: r.commands,
|
||
idlePolicy: r.idlePolicy,
|
||
});
|
||
if (takeLegacyRemoteTrust(r.target, path, r.host)) {
|
||
const status = await call(commands.projectTrustStatus(r.root));
|
||
if (!status.decided) await call(commands.projectTrustSet(r.root, true, false));
|
||
if (!(await call(commands.remoteTrustStatus())).decided) await call(commands.remoteTrustSet(true));
|
||
}
|
||
try {
|
||
await confirmProjectTrust(r.root);
|
||
} catch (error) {
|
||
await commands.closeProject();
|
||
disableRemote();
|
||
throw error;
|
||
}
|
||
const current = await commands.projectCurrent();
|
||
if (current) r.project = current;
|
||
r.project.settings = await clientSettings(r.project.settings);
|
||
useProject.setState({ project: r.project, settings: r.project.settings });
|
||
rememberRemote({
|
||
target: r.target,
|
||
path: r.root,
|
||
name: r.project.displayName || r.project.name,
|
||
host: r.host,
|
||
openedAtMs: Date.now(),
|
||
});
|
||
}
|
||
|
||
let connectAttempt = 0;
|
||
|
||
let backendBusyPrompt: ((target: RemoteTarget) => Promise<boolean>) | null = null;
|
||
|
||
/** Asks whether to stop another build's backend that still holds the project (#6119). */
|
||
export function provideBackendBusyPrompt(value: (target: RemoteTarget) => Promise<boolean>): () => void {
|
||
backendBusyPrompt = value;
|
||
return () => {
|
||
if (backendBusyPrompt === value) backendBusyPrompt = null;
|
||
};
|
||
}
|
||
|
||
/** Connects; when another build's backend still holds the project, stops it once the user agrees. */
|
||
async function connectOrReplace(target: RemoteTarget, path: string, onProgress: Channel<RemoteProgress>) {
|
||
try {
|
||
return await call(commands.remoteConnect(target, path, false, onProgress));
|
||
} catch (e) {
|
||
const busy = e instanceof IpcError && e.data?.type === "remoteBackendBusy";
|
||
if (!busy || !backendBusyPrompt || !(await backendBusyPrompt(target))) throw e;
|
||
return await call(commands.remoteConnect(target, path, true, onProgress));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Opens `path` of `target` (an SSH host or a WSL distribution) in this window. A dev container's
|
||
* project folder is the one in the container: `path` is then only a label.
|
||
*/
|
||
export async function connectRemote(target: RemoteTarget, path: string) {
|
||
useProject.setState({ opening: true });
|
||
const location = remoteLocation(target, path);
|
||
// Two connects to one location (a double click) share `location`; only the latest attaches.
|
||
const attempt = ++connectAttempt;
|
||
const current = () => attempt === connectAttempt && useRemoteProgress.getState().location === location;
|
||
const onProgress = new Channel<RemoteProgress>();
|
||
onProgress.onmessage = (progress) => {
|
||
if (current()) useRemoteProgress.setState({ progress });
|
||
};
|
||
useRemoteProgress.setState({
|
||
progress: { stage: "connecting", fraction: null },
|
||
location,
|
||
});
|
||
try {
|
||
const r = await connectOrReplace(target, path, onProgress);
|
||
// Another connect replaced this one, or the user cancelled. Disconnecting here
|
||
// would drop the connection that replacement published. Cancel already disconnects.
|
||
if (!current()) {
|
||
throw new IpcError({ kind: "cancelled", message: "Remote connection cancelled" });
|
||
}
|
||
await attachRemote(r, path);
|
||
return r;
|
||
} finally {
|
||
if (current()) {
|
||
useProject.setState({ opening: false });
|
||
useRemoteProgress.setState({ progress: null, location: null });
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Opens the project again, reattaching to its existing backend if it is still running.
|
||
*/
|
||
export async function reconnectRemote(): Promise<void> {
|
||
const session = useRemote.getState().session;
|
||
if (!session) return;
|
||
useRemote.setState({ reconnecting: true });
|
||
try {
|
||
const r = await connectOrReplace(session.target, session.root, new Channel<RemoteProgress>());
|
||
useProject.setState({ project: null });
|
||
await attachRemote(r, session.root);
|
||
} finally {
|
||
useRemote.setState({ reconnecting: false });
|
||
}
|
||
}
|
||
|
||
export async function disconnectRemote() {
|
||
if (!isRemoteWindow()) return;
|
||
await commands.closeProject();
|
||
disableRemote();
|
||
useProject.setState({ project: null });
|
||
}
|
||
|
||
/** Shuts down the remote `openidea-server` daemon and its processes, then closes the window's project. */
|
||
export async function stopRemoteBackend() {
|
||
if (!isRemoteWindow()) return;
|
||
await shutdownRemoteBackend();
|
||
await disconnectRemote();
|
||
}
|
||
|
||
/** Stops the backend without deciding what to do with the desktop window. */
|
||
export async function shutdownRemoteBackend() {
|
||
await call(commands.remoteStopBackend());
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Remote project locations: `ssh://<configuration id>/<absolute path>`,
|
||
// `wsl://<distribution>/<absolute path>` or `devcontainer://<local folder>[:<config>]/<container path>`,
|
||
// what the session and the recent projects remember for a remote window.
|
||
|
||
const SCHEMES = { ssh: "ssh://", wsl: "wsl://", devContainer: "devcontainer://" } as const;
|
||
|
||
/** The SSH configuration id, the WSL distribution name or the dev container's local folder. */
|
||
export function targetName(target: RemoteTarget): string {
|
||
switch (target.kind) {
|
||
case "ssh":
|
||
return target.config;
|
||
case "wsl":
|
||
return target.distro;
|
||
case "devContainer":
|
||
return target.folder;
|
||
}
|
||
}
|
||
|
||
export function sameTarget(a: RemoteTarget, b: RemoteTarget): boolean {
|
||
return (
|
||
a.kind === b.kind &&
|
||
targetName(a) === targetName(b) &&
|
||
(a.kind !== "devContainer" || b.kind !== "devContainer" || a.config === b.config)
|
||
);
|
||
}
|
||
|
||
export function remoteLocation(target: RemoteTarget, path: string): string {
|
||
const config = target.kind === "devContainer" && target.config ? `:${encodeURIComponent(target.config)}` : "";
|
||
return `${SCHEMES[target.kind]}${encodeURIComponent(targetName(target))}${config}${path.startsWith("/") ? "" : "/"}${path}`;
|
||
}
|
||
|
||
export function parseRemoteLocation(location: string): { target: RemoteTarget; path: string } | null {
|
||
const kind = (Object.keys(SCHEMES) as (keyof typeof SCHEMES)[]).find((k) => location.startsWith(SCHEMES[k]));
|
||
if (!kind) return null;
|
||
const rest = location.slice(SCHEMES[kind].length);
|
||
const slash = rest.indexOf("/");
|
||
if (slash <= 0) return null;
|
||
const authority = rest.slice(0, slash);
|
||
const colon = kind === "devContainer" ? authority.indexOf(":") : -1;
|
||
const name = decodeURIComponent(colon < 0 ? authority : authority.slice(0, colon));
|
||
const config = colon < 0 ? "" : decodeURIComponent(authority.slice(colon + 1));
|
||
const target: RemoteTarget =
|
||
kind === "ssh" ? { kind, config: name } : kind === "wsl" ? { kind, distro: name } : { kind, folder: name, config };
|
||
return { target, path: rest.slice(slash) };
|
||
}
|
||
|
||
/**
|
||
* The remote project a saved location stands for: a `ssh://` / `wsl://` location, or a
|
||
* `\\wsl.localhost\<distro>\…` folder remembered before WSL projects existed (it is a WSL
|
||
* project, not a network folder). `null` for a plain local path.
|
||
*/
|
||
export async function resolveRemoteLocation(location: string): Promise<{ target: RemoteTarget; path: string } | null> {
|
||
return parseRemoteLocation(location) ?? (await commands.wslResolve(location).catch(() => null));
|
||
}
|
||
|
||
/** The location of this window's project: remote, else its local root. */
|
||
export function projectLocation(root: string | null | undefined): string | null {
|
||
if (!root) return null;
|
||
const session = useRemote.getState().session;
|
||
return session && session.root === root ? remoteLocation(session.target, root) : root;
|
||
}
|
||
|
||
export interface RecentRemote {
|
||
target: RemoteTarget;
|
||
path: string;
|
||
name: string;
|
||
/** `user@host:port` when it was opened. */
|
||
host: string;
|
||
openedAtMs: number;
|
||
}
|
||
|
||
const RECENT_KEY = "openidea.remote.recent";
|
||
const RECENT_MAX = 20;
|
||
const TRUST_MIGRATION_KEY = "openidea.remote.trustMigration";
|
||
|
||
/** Snapshot existing user-owned recents before any new connection is remembered. */
|
||
export function initializeRemoteTrustMigration() {
|
||
try {
|
||
if (localStorage.getItem(TRUST_MIGRATION_KEY) === null) {
|
||
localStorage.setItem(TRUST_MIGRATION_KEY, JSON.stringify(recentRemotes()));
|
||
}
|
||
} catch {
|
||
/* Unavailable storage never grants trust. */
|
||
}
|
||
}
|
||
|
||
/** An entry as stored: before WSL it carried `sshConfig` instead of `target`. */
|
||
function normalize(entry: RecentRemote & { sshConfig?: string }): RecentRemote | null {
|
||
if (!entry || !entry.path) return null;
|
||
const target = entry.target ?? (entry.sshConfig ? { kind: "ssh" as const, config: entry.sshConfig } : null);
|
||
return target ? { ...entry, target } : null;
|
||
}
|
||
|
||
/** Whether `path` on SSH `target` was remembered before the trust prompt existed (WSL never was). */
|
||
export function takeLegacyRemoteTrust(target: RemoteTarget, path: string, host: string): boolean {
|
||
if (target.kind !== "ssh") return false;
|
||
try {
|
||
const value: unknown = JSON.parse(localStorage.getItem(TRUST_MIGRATION_KEY) ?? "[]");
|
||
if (!Array.isArray(value)) return false;
|
||
const matches = (stored: RecentRemote) => {
|
||
const entry = normalize(stored);
|
||
return entry !== null && sameTarget(entry.target, target) && entry.path === path && entry.host === host;
|
||
};
|
||
const found = value.some(matches);
|
||
localStorage.setItem(TRUST_MIGRATION_KEY, JSON.stringify(value.filter((entry) => !matches(entry))));
|
||
return found;
|
||
} catch {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
export function recentRemotes(): RecentRemote[] {
|
||
try {
|
||
const list = JSON.parse(localStorage.getItem(RECENT_KEY) ?? "[]") as unknown;
|
||
return Array.isArray(list) ? list.map(normalize).filter((r): r is RecentRemote => r !== null) : [];
|
||
} catch {
|
||
return [];
|
||
}
|
||
}
|
||
|
||
function saveRemotes(list: RecentRemote[]) {
|
||
try {
|
||
localStorage.setItem(RECENT_KEY, JSON.stringify(list.slice(0, RECENT_MAX)));
|
||
} catch {
|
||
/* storage unavailable */
|
||
}
|
||
syncRemoteRecentMenu();
|
||
}
|
||
|
||
/** Hands the recent remote projects to File › Open Recent, which lists them among the local ones. */
|
||
export function syncRemoteRecentMenu() {
|
||
if (!isTauri()) return;
|
||
const projects = recentRemotes().map((r) => ({
|
||
location: remoteLocation(r.target, r.path),
|
||
name: r.name,
|
||
detail: `${r.host || targetName(r.target)}:${r.path}`,
|
||
openedAtMs: r.openedAtMs,
|
||
}));
|
||
void Promise.resolve()
|
||
.then(() => commands.menuSetRemoteRecent(projects))
|
||
.catch(() => {});
|
||
}
|
||
|
||
const sameRemote = (a: Pick<RecentRemote, "target" | "path">, b: Pick<RecentRemote, "target" | "path">) =>
|
||
sameTarget(a.target, b.target) && a.path === b.path;
|
||
|
||
export function rememberRemote(entry: RecentRemote) {
|
||
saveRemotes([entry, ...recentRemotes().filter((r) => !sameRemote(r, entry))]);
|
||
}
|
||
|
||
export function forgetRemote(target: RemoteTarget, path: string) {
|
||
saveRemotes(recentRemotes().filter((r) => !sameRemote(r, { target, path })));
|
||
}
|
||
|
||
// Any way of leaving the remote project (Close Project, opening a local one) ends remote routing.
|
||
useProject.subscribe((s, prev) => {
|
||
const session = useRemote.getState().session;
|
||
if (!session || s.opening) return;
|
||
// Only a window whose project was the session's root can leave it. Auxiliary windows
|
||
// (comparison, deployment) hold no project of their own — they ride their parent's
|
||
// session, and a settings-only update (the comparison window keeps just the settings)
|
||
// must not drop the routing: its next commands would run here instead of on the host,
|
||
// where no project is open (`open_project` / `light_edit_open` / `close_project` end
|
||
// the session themselves, see `route`).
|
||
if (prev.project?.root !== session.root || s.project?.root === session.root) return;
|
||
disableRemote();
|
||
});
|