Files
NetMesh/domain/sftpLocatePathInTerminal.ts
zhaolei 3c72efcb7f
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
[Init] Initial commit - NetMesh terminal manager
2026-09-13 18:24:01 +08:00

80 lines
2.9 KiB
TypeScript

import { resolveInteractiveTerminalCdIntent } from "./sessionRestore";
export type LocateSftpPathInTerminalContext = {
path?: string | null;
sessionId?: string | null;
sessionStatus?: string | null;
sessionHostId?: string | null;
sftpHostId?: string | null;
sftpIsLocal?: boolean;
protocol?: string | null;
shellType?: string | null;
isNetworkDevice?: boolean;
moshEnabled?: boolean;
etEnabled?: boolean;
sessionHostname?: string | null;
sessionUsername?: string | null;
sessionPort?: number | null;
sftpHostname?: string | null;
sftpUsername?: string | null;
sftpPort?: number | null;
};
/**
* Prefer the SFTP-reusable SSH session id when present; otherwise use the
* focused terminal (mosh/et/local) so locate is not stuck behind connection reuse.
*/
export function resolveLocateSftpPathSessionId(options: {
activeSessionId?: string | null;
focusedSessionId?: string | null;
}): string | null {
return options.activeSessionId ?? options.focusedSessionId ?? null;
}
function remoteEndpointsMatch(options: LocateSftpPathInTerminalContext): boolean {
if (!options.sessionHostname || !options.sftpHostname) return true;
return options.sessionHostname === options.sftpHostname
&& (options.sessionPort ?? 22) === (options.sftpPort ?? 22)
&& (options.sessionUsername || "root") === (options.sftpUsername || "root");
}
/** Whether the SFTP current path can be sent as `cd` to the linked terminal. */
export function canLocateSftpPathInTerminal(
options: LocateSftpPathInTerminalContext,
): boolean {
if (!options.sessionId || options.sessionStatus !== "connected") return false;
if (options.isNetworkDevice) return false;
if (!resolveInteractiveTerminalCdIntent(options.path)) return false;
const protocol = options.protocol ?? "ssh";
if (protocol === "telnet" || protocol === "serial") return false;
if (protocol === "local" && (options.shellType === "powershell" || options.shellType === "cmd")) {
return false;
}
if (options.sftpIsLocal) {
return protocol === "local";
}
if (!options.sftpHostId || !options.sessionHostId) return false;
if (options.sftpHostId !== options.sessionHostId) return false;
if (!remoteEndpointsMatch(options)) return false;
// Interactive locate allows mosh/et (unlike silent restore). Accept both the
// transport protocol strings and ssh+flag forms used by session factories.
return protocol === "ssh"
|| protocol === "mosh"
|| protocol === "et"
|| protocol === "local";
}
/** Session write payload for locating the SFTP path in the linked terminal. */
export function resolveLocateSftpPathInTerminalAction(
options: LocateSftpPathInTerminalContext,
): { sessionId: string; data: string } | null {
if (!canLocateSftpPathInTerminal(options) || !options.sessionId) return null;
const intent = resolveInteractiveTerminalCdIntent(options.path);
if (!intent) return null;
return { sessionId: options.sessionId, data: `${intent.command}\r` };
}