@downcity/workspace-ssh 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +183 -0
- package/bin/RemoteDirectoryProgram.d.ts +7 -0
- package/bin/RemoteDirectoryProgram.d.ts.map +1 -0
- package/bin/RemoteDirectoryProgram.js +66 -0
- package/bin/RemoteDirectoryProgram.js.map +1 -0
- package/bin/RemoteProgram.d.ts +7 -0
- package/bin/RemoteProgram.d.ts.map +1 -0
- package/bin/RemoteProgram.js +507 -0
- package/bin/RemoteProgram.js.map +1 -0
- package/bin/RemoteShellProgram.d.ts +3 -0
- package/bin/RemoteShellProgram.d.ts.map +1 -0
- package/bin/RemoteShellProgram.js +67 -0
- package/bin/RemoteShellProgram.js.map +1 -0
- package/bin/SshBrowseConfig.d.ts +11 -0
- package/bin/SshBrowseConfig.d.ts.map +1 -0
- package/bin/SshBrowseConfig.js +191 -0
- package/bin/SshBrowseConfig.js.map +1 -0
- package/bin/SshConnection.d.ts +32 -0
- package/bin/SshConnection.d.ts.map +1 -0
- package/bin/SshConnection.js +139 -0
- package/bin/SshConnection.js.map +1 -0
- package/bin/SshDirectoryBrowser.d.ts +17 -0
- package/bin/SshDirectoryBrowser.d.ts.map +1 -0
- package/bin/SshDirectoryBrowser.js +81 -0
- package/bin/SshDirectoryBrowser.js.map +1 -0
- package/bin/SshDirectorySetup.d.ts +10 -0
- package/bin/SshDirectorySetup.d.ts.map +1 -0
- package/bin/SshDirectorySetup.js +49 -0
- package/bin/SshDirectorySetup.js.map +1 -0
- package/bin/SshFileSystem.d.ts +39 -0
- package/bin/SshFileSystem.d.ts.map +1 -0
- package/bin/SshFileSystem.js +171 -0
- package/bin/SshFileSystem.js.map +1 -0
- package/bin/SshInitialization.d.ts +11 -0
- package/bin/SshInitialization.d.ts.map +1 -0
- package/bin/SshInitialization.js +92 -0
- package/bin/SshInitialization.js.map +1 -0
- package/bin/SshMedia.d.ts +7 -0
- package/bin/SshMedia.d.ts.map +1 -0
- package/bin/SshMedia.js +49 -0
- package/bin/SshMedia.js.map +1 -0
- package/bin/SshShell.d.ts +29 -0
- package/bin/SshShell.d.ts.map +1 -0
- package/bin/SshShell.js +233 -0
- package/bin/SshShell.js.map +1 -0
- package/bin/SshWorkspace.d.ts +31 -0
- package/bin/SshWorkspace.d.ts.map +1 -0
- package/bin/SshWorkspace.js +121 -0
- package/bin/SshWorkspace.js.map +1 -0
- package/bin/index.d.ts +8 -0
- package/bin/index.d.ts.map +1 -0
- package/bin/index.js +6 -0
- package/bin/index.js.map +1 -0
- package/bin/types/Ssh.d.ts +133 -0
- package/bin/types/Ssh.d.ts.map +1 -0
- package/bin/types/Ssh.js +2 -0
- package/bin/types/Ssh.js.map +1 -0
- package/bin/types/SshDirectory.d.ts +44 -0
- package/bin/types/SshDirectory.d.ts.map +1 -0
- package/bin/types/SshDirectory.js +2 -0
- package/bin/types/SshDirectory.js.map +1 -0
- package/package.json +35 -3
- package/README.md +0 -3
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/** SSH rooted 文件能力;所有文件字节和路径均留在远端处理,RPC 无自动重试。 */
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { expected_file_version, observe_file_version } from "@downcity/type/workspace";
|
|
4
|
+
/** 只保留活动事务的队列;同进程跨 Workspace 排序,完成后删除。 */
|
|
5
|
+
const file_action_queues = new Map();
|
|
6
|
+
import { ssh_runtime_command } from "./SshInitialization.js";
|
|
7
|
+
import { remote_program } from "./RemoteProgram.js";
|
|
8
|
+
/** 工具事务队列独立于 SDK 的 with_file_lock,避免嵌套使用同名锁造成自等待。 */
|
|
9
|
+
async function with_file_action_queue(key, action) {
|
|
10
|
+
const previous = file_action_queues.get(key) ?? Promise.resolve();
|
|
11
|
+
let release = () => { };
|
|
12
|
+
const pending = new Promise(resolve => { release = resolve; });
|
|
13
|
+
const queued = previous.then(() => pending);
|
|
14
|
+
file_action_queues.set(key, queued);
|
|
15
|
+
await previous;
|
|
16
|
+
try {
|
|
17
|
+
return await action();
|
|
18
|
+
}
|
|
19
|
+
finally {
|
|
20
|
+
release();
|
|
21
|
+
if (file_action_queues.get(key) === queued)
|
|
22
|
+
file_action_queues.delete(key);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/** 具有远端错误码的文件失败。 */
|
|
26
|
+
export class SshFileError extends Error {
|
|
27
|
+
code;
|
|
28
|
+
constructor(code, message) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.code = code;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** 规范化远程 POSIX 绝对根目录,不调用宿主 realpath/resolve。 */
|
|
34
|
+
export function normalize_remote_root(value) {
|
|
35
|
+
if (!value.startsWith("/") || value.includes("\0"))
|
|
36
|
+
throw new Error("Remote directory must be an absolute POSIX path");
|
|
37
|
+
return path.posix.normalize(value).replace(/\/$/u, "") || "/";
|
|
38
|
+
}
|
|
39
|
+
/** 每次 RPC 使用远端标准输入 JSON,无远程缓存或工作副本。 */
|
|
40
|
+
export class SshFileSystem {
|
|
41
|
+
connection;
|
|
42
|
+
canonical_root;
|
|
43
|
+
get root_path() { return this.canonical_root; }
|
|
44
|
+
/** 连接验证后固定远端规范根;后续请求拒绝该根被替换成 symlink。 */
|
|
45
|
+
use_canonical_root(root_path) { this.canonical_root = normalize_remote_root(root_path); }
|
|
46
|
+
locks = new Map();
|
|
47
|
+
constructor(connection, root_path) {
|
|
48
|
+
this.connection = connection;
|
|
49
|
+
this.canonical_root = normalize_remote_root(root_path);
|
|
50
|
+
}
|
|
51
|
+
resolve_path(...segments) {
|
|
52
|
+
let result = this.root_path;
|
|
53
|
+
for (let segment of segments) {
|
|
54
|
+
if (segment.startsWith("ssh://")) {
|
|
55
|
+
const location = new URL(segment);
|
|
56
|
+
if (decodeURIComponent(location.hostname) !== this.connection.host)
|
|
57
|
+
throw new Error("SSH path targets another host");
|
|
58
|
+
segment = location.pathname.split("/").map(decodeURIComponent).join("/");
|
|
59
|
+
}
|
|
60
|
+
result = path.posix.resolve(result, segment);
|
|
61
|
+
}
|
|
62
|
+
if (result !== this.root_path && !result.startsWith(this.root_path === "/" ? "/" : `${this.root_path}/`))
|
|
63
|
+
throw new Error("Path escapes SSH Workspace");
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
/** 远端需要预装 Python;缺失时保留 SSH 返回的明确错误。 */
|
|
67
|
+
async rpc(operation, args = {}, options = {}) {
|
|
68
|
+
const command = ssh_runtime_command(`command -v python3 >/dev/null 2>&1 || { printf '%s\\n' 'SSH Workspace needs Python 3.8+ on the remote host; run SSH Workspace dependency initialization' >&2; exit 127; }; exec python3 -c "$1"`, remote_program);
|
|
69
|
+
const raw = await this.connection.request(command, { ...options, input: JSON.stringify({ root: this.root_path, operation, arguments: args }) });
|
|
70
|
+
let response;
|
|
71
|
+
try {
|
|
72
|
+
response = JSON.parse(raw);
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
throw new Error(options.mutation ? "SSH mutation response was invalid; result may be unknown. Inspect remote state before retrying. It was not replayed." : "SSH returned an invalid file response; ensure remote startup scripts do not print to stdout for non-interactive commands");
|
|
76
|
+
}
|
|
77
|
+
if (!response.ok)
|
|
78
|
+
throw new SshFileError(response.code || "internal_error", response.message || "Remote operation failed");
|
|
79
|
+
return response.result;
|
|
80
|
+
}
|
|
81
|
+
async stat(file_path) { return await this.rpc("stat", { path: this.resolve_path(file_path) }); }
|
|
82
|
+
async path_exists(file_path) {
|
|
83
|
+
try {
|
|
84
|
+
await this.stat(file_path);
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
if (error instanceof SshFileError && error.code === "file_not_found")
|
|
89
|
+
return false;
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
async read_file(file_path) { return Buffer.from(await this.rpc("read", { path: this.resolve_path(file_path) }), "base64"); }
|
|
94
|
+
/** 远端根约束、有界普通文件读取;取消传播到实际 SSH 请求,不回退宿主文件。 */
|
|
95
|
+
async read_file_bounded(file_path, max_bytes, abort_signal) {
|
|
96
|
+
if (!Number.isSafeInteger(max_bytes) || max_bytes < 1 || max_bytes > 8 * 1024 * 1024)
|
|
97
|
+
throw new Error("Invalid resource byte budget");
|
|
98
|
+
return Buffer.from(await this.rpc("read_bounded", { path: this.resolve_path(file_path), max_bytes }, { signal: abort_signal }), "base64");
|
|
99
|
+
}
|
|
100
|
+
async file_size(file_path) { return (await this.stat(file_path)).size; }
|
|
101
|
+
async ensure_directory(directory_path) { await this.rpc("mkdir", { path: this.resolve_path(directory_path) }, { mutation: true }); }
|
|
102
|
+
async remove_path(target_path) { await this.rpc("remove", { path: this.resolve_path(target_path) }, { mutation: true }); }
|
|
103
|
+
async move_path(source_path, target_path) { await this.rpc("move", { path: this.resolve_path(source_path), target: this.resolve_path(target_path) }, { mutation: true }); }
|
|
104
|
+
async read_directory(directory_path) { return await this.rpc("list", { path: this.resolve_path(directory_path) }); }
|
|
105
|
+
async write_file_atomically(file_path, content) { await this.rpc("write", { path: this.resolve_path(file_path), content: Buffer.from(content).toString("base64") }, { mutation: true }); }
|
|
106
|
+
async append_file(file_path, content) { await this.rpc("append", { path: this.resolve_path(file_path), content: Buffer.from(content).toString("base64") }, { mutation: true }); }
|
|
107
|
+
async with_file_lock(lock_path, action) {
|
|
108
|
+
const key = this.resolve_path(lock_path);
|
|
109
|
+
const previous = this.locks.get(key) || Promise.resolve();
|
|
110
|
+
let release = () => { };
|
|
111
|
+
const pending = new Promise((resolve) => { release = resolve; });
|
|
112
|
+
const queued = previous.then(() => pending);
|
|
113
|
+
this.locks.set(key, queued);
|
|
114
|
+
await previous;
|
|
115
|
+
try {
|
|
116
|
+
return await action();
|
|
117
|
+
}
|
|
118
|
+
finally {
|
|
119
|
+
release();
|
|
120
|
+
if (this.locks.get(key) === queued)
|
|
121
|
+
this.locks.delete(key);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
async run_file_action(request, observer, abort_signal, observed_versions) {
|
|
125
|
+
try {
|
|
126
|
+
const canonical_path = await this.rpc("canonical_path", { path: this.resolve_path(request.input.file_path) }, { signal: abort_signal });
|
|
127
|
+
const key = `ssh:${this.connection.host}:${canonical_path}`;
|
|
128
|
+
return await with_file_action_queue(key, async () => {
|
|
129
|
+
try {
|
|
130
|
+
abort_signal?.throwIfAborted();
|
|
131
|
+
const expected_version = request.action === "read" ? undefined : expected_file_version(observed_versions, key, request.input.expected_version);
|
|
132
|
+
if (request.action !== "read" && request.input.expected_version && !expected_version)
|
|
133
|
+
throw new SshFileError("file_changed", "Read this file again and review its content before retrying; do not overwrite to bypass a conflict.");
|
|
134
|
+
const result = await this.rpc("file_action", { ...request, input: { ...request.input, file_path: canonical_path, ...(request.action !== "read" ? { expected_version } : {}) } }, { signal: abort_signal, mutation: request.action !== "read" });
|
|
135
|
+
if (result.success && result.file_path !== canonical_path)
|
|
136
|
+
throw new SshFileError("file_changed", "File path changed during access; read it again before retrying.");
|
|
137
|
+
if (result.success && result.file_version)
|
|
138
|
+
observe_file_version(observed_versions, key, result.file_version);
|
|
139
|
+
if (result.success && request.action !== "read" && "sha256" in result) {
|
|
140
|
+
try {
|
|
141
|
+
observer?.on_file_mutation({ file_path: result.file_path, before: { exists: request.action === "edit" || ("overwritten" in result && result.overwritten), ...("previous_sha256" in result ? { sha256: result.previous_sha256 } : {}) }, after: { exists: true, sha256: result.sha256, ...(request.action === "write" ? { content: request.input.content } : {}) } });
|
|
142
|
+
}
|
|
143
|
+
catch { /* 已提交,不以观察失败回滚真实结果。 */ }
|
|
144
|
+
}
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
if (error instanceof SshFileError && error.code === "file_changed")
|
|
149
|
+
observed_versions?.delete(key);
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
if (!(error instanceof SshFileError))
|
|
156
|
+
throw error;
|
|
157
|
+
return { success: false, error_code: error.code, message: error.message };
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
async run_search_action(request) {
|
|
161
|
+
try {
|
|
162
|
+
return await this.rpc("search_action", { action: request.action, input: { ...request.input, path: this.resolve_path(request.input.path || ".") } }, { signal: request.abort_signal });
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
if (!(error instanceof SshFileError))
|
|
166
|
+
throw error;
|
|
167
|
+
return { success: false, error_code: error.code === "file_not_found" ? "rg_unavailable" : "search_failed", message: error.message };
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
//# sourceMappingURL=SshFileSystem.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SshFileSystem.js","sourceRoot":"","sources":["../src/SshFileSystem.ts"],"names":[],"mappings":"AAAA,kDAAkD;AAClD,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAEvF,0CAA0C;AAC1C,MAAM,kBAAkB,GAAG,IAAI,GAAG,EAAyB,CAAC;AAG5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAC7D,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEpD,qDAAqD;AACrD,KAAK,UAAU,sBAAsB,CAAI,GAAW,EAAE,MAAwB;IAC1E,MAAM,QAAQ,GAAG,kBAAkB,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;IAClE,IAAI,OAAO,GAAG,GAAG,EAAE,GAAE,CAAC,CAAC;IACvB,MAAM,OAAO,GAAG,IAAI,OAAO,CAAO,OAAO,CAAC,EAAE,GAAG,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;IACrE,MAAM,MAAM,GAAG,QAAQ,CAAC,IAAI,C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,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC;QAC1L,CAAC;QACD,OAAO,KAAK,EAAE,CAAC;YACX,IAAI,CAAC,CAAC,KAAK,YAAY,YAAY,CAAC;gBAChC,MAAM,KAAK,CAAC;YAChB,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,KAAK,CAAC,IAAI,KAAK,gBAAgB,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,eAAe,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC;QACxI,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSH Workspace 的 POSIX Shell 入口和内置依赖安装脚本。
|
|
3
|
+
* 安装不读取项目脚本;只在显式初始化时调用已有的远端包管理器。
|
|
4
|
+
*/
|
|
5
|
+
import { SshConnection } from "./SshConnection.js";
|
|
6
|
+
import type { SshRequestOptions } from "./types/Ssh.js";
|
|
7
|
+
/** 给所有 SSH Workspace 能力使用相同的非交互 PATH,包含标准 Homebrew 前缀。 */
|
|
8
|
+
export declare function ssh_runtime_command(script: string, ...args: string[]): string;
|
|
9
|
+
/** 检查主机和目录不依赖 Python;仅通过验证后的严格 SSH 连接运行有副作用的命令。 */
|
|
10
|
+
export declare function initialize_ssh_dependencies(connection: SshConnection, root_path: string, canonical_root: boolean, options: SshRequestOptions): Promise<string>;
|
|
11
|
+
//# sourceMappingURL=SshInitialization.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SshInitialization.d.ts","sourceRoot":"","sources":["../src/SshInitialization.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,aAAa,EAAe,MAAM,oBAAoB,CAAC;AAChE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAExD,0DAA0D;AAC1D,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAG7E;AAgED,mDAAmD;AACnD,wBAAsB,2BAA2B,CAC7C,UAAU,EAAE,aAAa,EACzB,SAAS,EAAE,MAAM,EACjB,cAAc,EAAE,OAAO,EACvB,OAAO,EAAE,iBAAiB,GAC3B,OAAO,CAAC,MAAM,CAAC,CAmBjB"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSH Workspace 的 POSIX Shell 入口和内置依赖安装脚本。
|
|
3
|
+
* 安装不读取项目脚本;只在显式初始化时调用已有的远端包管理器。
|
|
4
|
+
*/
|
|
5
|
+
import { quote_posix } from "./SshConnection.js";
|
|
6
|
+
/** 给所有 SSH Workspace 能力使用相同的非交互 PATH,包含标准 Homebrew 前缀。 */
|
|
7
|
+
export function ssh_runtime_command(script, ...args) {
|
|
8
|
+
const runtime_path = 'for prefix in /usr/local /opt/homebrew; do if [ -x "$prefix/bin/brew" ]; then PATH="$prefix/bin:$PATH"; fi; done; export PATH';
|
|
9
|
+
return `exec /bin/sh -c ${quote_posix(`${runtime_path}\n${script}`)} downcity${args.map((arg) => ` ${quote_posix(arg)}`).join("")}`;
|
|
10
|
+
}
|
|
11
|
+
const check_target = String.raw `
|
|
12
|
+
fail() { printf '%s\n' "$*" >&2; exit 1; }
|
|
13
|
+
case "$1" in /*) ;; *) fail 'SSH Workspace requires an absolute remote directory' ;; esac
|
|
14
|
+
cd -P "$1" && [ -r . ] && [ -x . ] || fail 'Remote Workspace directory is not readable and accessible'
|
|
15
|
+
if [ "$2" = canonical ] && [ "$PWD" != "$1" ]; then
|
|
16
|
+
fail 'Registered SSH directory now resolves to a different path; verify the directory before initializing'
|
|
17
|
+
fi
|
|
18
|
+
canonical_path=$PWD
|
|
19
|
+
# 包管理器不从项目目录查找程序或配置。
|
|
20
|
+
cd / || fail 'Cannot enter the remote filesystem root'
|
|
21
|
+
platform=$(uname -s) || fail 'Cannot detect the remote operating system'
|
|
22
|
+
case "$platform" in Linux|Darwin) ;; *) fail 'Only Linux and macOS SSH hosts are supported' ;; esac
|
|
23
|
+
`;
|
|
24
|
+
const install_dependencies = String.raw `
|
|
25
|
+
python_ready() { command -v python3 >/dev/null 2>&1 && python3 -I -S -c 'import sys; sys.exit(sys.version_info < (3, 8))' >/dev/null 2>&1; }
|
|
26
|
+
rg_ready() { command -v rg >/dev/null 2>&1; }
|
|
27
|
+
ps_ready() { command -v ps >/dev/null 2>&1; }
|
|
28
|
+
if python_ready && rg_ready && ps_ready; then exit 0; fi
|
|
29
|
+
|
|
30
|
+
set --
|
|
31
|
+
if [ "$platform" = Darwin ]; then
|
|
32
|
+
ps_ready || fail 'POSIX ps is missing; restore the macOS system ps command before retrying'
|
|
33
|
+
command -v brew >/dev/null 2>&1 || fail 'Homebrew is required to initialize missing SSH Workspace dependencies on macOS; install Homebrew or provide Python 3.8+ and ripgrep manually'
|
|
34
|
+
python_ready || set -- "$@" python
|
|
35
|
+
rg_ready || set -- "$@" ripgrep
|
|
36
|
+
manager=brew
|
|
37
|
+
else
|
|
38
|
+
python_ready || set -- "$@" python3
|
|
39
|
+
rg_ready || set -- "$@" ripgrep
|
|
40
|
+
ps_ready || set -- "$@" procps
|
|
41
|
+
if command -v apt-get >/dev/null 2>&1; then
|
|
42
|
+
manager=apt-get
|
|
43
|
+
elif command -v dnf >/dev/null 2>&1; then
|
|
44
|
+
manager=dnf
|
|
45
|
+
elif command -v apk >/dev/null 2>&1; then
|
|
46
|
+
manager=apk
|
|
47
|
+
else
|
|
48
|
+
fail 'No supported package manager (apt-get, dnf, apk) found; provide Python 3.8+, ripgrep and POSIX ps manually'
|
|
49
|
+
fi
|
|
50
|
+
fi
|
|
51
|
+
|
|
52
|
+
# 无交互执行:既不读取 sudo 密码,也不在缺少权限时等待输入。
|
|
53
|
+
if [ "$platform" = Linux ] && [ "$(id -u)" != 0 ]; then
|
|
54
|
+
command -v sudo >/dev/null 2>&1 || fail 'Installing SSH Workspace dependencies requires root or non-interactive sudo; install the missing packages as an administrator'
|
|
55
|
+
run_manager() { sudo -n -- "$manager" "$@"; }
|
|
56
|
+
else
|
|
57
|
+
run_manager() { "$manager" "$@"; }
|
|
58
|
+
fi
|
|
59
|
+
|
|
60
|
+
case "$manager" in
|
|
61
|
+
brew) run_manager install "$@" ;;
|
|
62
|
+
apt-get) run_manager update && run_manager -o Dpkg::Use-Pty=0 install -y "$@" ;;
|
|
63
|
+
dnf) run_manager install -y "$@" ;;
|
|
64
|
+
apk) run_manager add "$@" ;;
|
|
65
|
+
esac 1>&2 || fail "Installing SSH Workspace dependencies with $manager failed ($*). See the package manager output above."
|
|
66
|
+
|
|
67
|
+
python_ready || fail 'Python 3.8+ is still unavailable on the non-interactive SSH PATH after installation'
|
|
68
|
+
rg_ready || fail 'ripgrep (rg) is still unavailable on the non-interactive SSH PATH after installation'
|
|
69
|
+
ps_ready || fail 'POSIX ps is still unavailable on the non-interactive SSH PATH after installation'
|
|
70
|
+
`;
|
|
71
|
+
/** 检查主机和目录不依赖 Python;仅通过验证后的严格 SSH 连接运行有副作用的命令。 */
|
|
72
|
+
export async function initialize_ssh_dependencies(connection, root_path, canonical_root, options) {
|
|
73
|
+
const root_mode = canonical_root ? "canonical" : "resolve";
|
|
74
|
+
const probe = `${check_target}\nprintf '%s\\0' "$canonical_path"`;
|
|
75
|
+
const response = await connection.request(ssh_runtime_command(probe, root_path, root_mode), {
|
|
76
|
+
discover_host: options.discover_host,
|
|
77
|
+
confirmed_prompt: options.confirmed_prompt,
|
|
78
|
+
signal: options.signal,
|
|
79
|
+
max_bytes: 4096,
|
|
80
|
+
});
|
|
81
|
+
if (!response.startsWith("/") || response.indexOf("\0") !== response.length - 1)
|
|
82
|
+
throw new Error("SSH returned an invalid directory response; ensure startup scripts do not print to stdout");
|
|
83
|
+
const verified_path = response.slice(0, -1);
|
|
84
|
+
await connection.request(ssh_runtime_command(`${check_target}\n${install_dependencies}`, verified_path, "canonical"), {
|
|
85
|
+
mutation: true,
|
|
86
|
+
signal: options.signal,
|
|
87
|
+
timeout_ms: 5 * 60_000,
|
|
88
|
+
max_bytes: 4096,
|
|
89
|
+
});
|
|
90
|
+
return verified_path;
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=SshInitialization.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SshInitialization.js","sourceRoot":"","sources":["../src/SshInitialization.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAiB,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAGhE,0DAA0D;AAC1D,MAAM,UAAU,mBAAmB,CAAC,MAAc,EAAE,GAAG,IAAc;IACjE,MAAM,YAAY,GAAG,+HAA+H,CAAC;IACrJ,OAAO,mBAAmB,WAAW,CAAC,GAAG,YAAY,KAAK,MAAM,EAAE,CAAC,YAAY,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;AACxI,CAAC;AAED,MAAM,YAAY,GAAG,MAAM,CAAC,GAAG,CAAA;;;;;;;;;;;;CAY9B,CAAC;AAEF,MAAM,oBAAoB,GAAG,MAAM,CAAC,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA8CtC,CAAC;AAEF,mDAAmD;AACnD,MAAM,CAAC,KAAK,UAAU,2BAA2B,CAC7C,UAAyB,EACzB,SAAiB,EACjB,cAAuB,EACvB,OAA0B;IAE1B,MAAM,SAAS,GAAG,cAAc,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,CAAC;IAC3D,MAAM,KAAK,GAAG,GAAG,YAAY,oCAAoC,CAAC;IAClE,MAAM,QAAQ,GAAG,MAAM,UAAU,CAAC,OAAO,CAAC,mBAAmB,CAAC,KAAK,EAAE,SAAS,EAAE,SAAS,CAAC,EAAE;QACxF,aAAa,EAAE,OAAO,CAAC,aAAa;QACpC,gBAAgB,EAAE,OAAO,CAAC,gBAAgB;QAC1C,MAAM,EAAE,OAAO,CAAC,MAAM;QACtB,SAAS,EAAE,IAAI;KAClB,CAAC,CAAC;IACH,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,QAAQ,CAAC,MAAM,GAAG,CAAC;QAC3E,MAAM,IAAI,KAAK,CAAC,2FAA2F,CAAC,CAAC;IACjH,MAAM,aAAa,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAC5C,MAAM,UAAU,CAAC,OAAO,CAAC,mBAAmB,CAAC,GAAG,YAAY,KAAK,oBAAoB,EAAE,EAAE,aAAa,EAAE,WAAW,CAAC,EAAE;QAClH,QAAQ,EAAE,IAAI;QACd,MAAM,EAAE,OAAO,CAAC,MAAM;QACtB,UAAU,EAAE,CAAC,GAAG,MAAM;QACtB,SAAS,EAAE,IAAI;KAClB,CAAC,CAAC;IACH,OAAO,aAAa,CAAC;AACzB,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ToolCallContext } from "@downcity/type";
|
|
2
|
+
import type { ReadFileToolSuccess } from "@downcity/type/workspace";
|
|
3
|
+
import type { SshMediaResult } from "./types/Ssh.js";
|
|
4
|
+
import type { SshFileSystem } from "./SshFileSystem.js";
|
|
5
|
+
/** 返回已绑定字节快照的消息;未知格式和超限媒体保留引用。 */
|
|
6
|
+
export declare function read_ssh_media(files: SshFileSystem, output: ReadFileToolSuccess, context: ToolCallContext): Promise<SshMediaResult>;
|
|
7
|
+
//# sourceMappingURL=SshMedia.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SshMedia.d.ts","sourceRoot":"","sources":["../src/SshMedia.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAEpE,OAAO,KAAK,EAAE,cAAc,EAAmB,MAAM,gBAAgB,CAAC;AACtE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAexD,kCAAkC;AAClC,wBAAsB,cAAc,CAAC,KAAK,EAAE,aAAa,EAAE,MAAM,EAAE,mBAAmB,EAAE,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,cAAc,CAAC,CAyBzI"}
|
package/bin/SshMedia.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 显式远程媒体读取:有界取得远端字节,再交给当前 Session 持久化为本地附件。
|
|
3
|
+
* 不把远端文件名作为本机路径,不自动下载大文件;与 Workspace read 共用媒体预算。
|
|
4
|
+
*/
|
|
5
|
+
import crypto from "node:crypto";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { max_explicit_media_bytes, max_request_media_bytes, read_image_dimensions, estimate_image_tokens } from "@downcity/type/workspace";
|
|
8
|
+
/** 只从真实签名识别受支持的媒体。 */
|
|
9
|
+
function media_type(bytes) {
|
|
10
|
+
if (bytes.subarray(0, 5).toString("ascii") === "%PDF-")
|
|
11
|
+
return "application/pdf";
|
|
12
|
+
if (bytes.subarray(0, 8).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10])))
|
|
13
|
+
return "image/png";
|
|
14
|
+
if (bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff)
|
|
15
|
+
return "image/jpeg";
|
|
16
|
+
if (bytes.toString("ascii", 0, 4) === "RIFF" && bytes.toString("ascii", 8, 12) === "WEBP")
|
|
17
|
+
return "image/webp";
|
|
18
|
+
if (["GIF87a", "GIF89a"].includes(bytes.toString("ascii", 0, 6)))
|
|
19
|
+
return "image/gif";
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
/** 返回已绑定字节快照的消息;未知格式和超限媒体保留引用。 */
|
|
23
|
+
export async function read_ssh_media(files, output, context) {
|
|
24
|
+
const messages = [];
|
|
25
|
+
if (output.total_bytes > max_explicit_media_bytes)
|
|
26
|
+
return { output, messages };
|
|
27
|
+
context.abort_signal?.throwIfAborted();
|
|
28
|
+
const data = Buffer.from(await files.rpc("media", { path: output.file_path, expected_version: output.file_version }, { signal: context.abort_signal }), "base64");
|
|
29
|
+
if (data.length > max_explicit_media_bytes)
|
|
30
|
+
throw new Error("Remote media changed and exceeds 2 MiB");
|
|
31
|
+
const type = media_type(data);
|
|
32
|
+
if (!type)
|
|
33
|
+
return { output, messages };
|
|
34
|
+
const dimensions = type.startsWith("image/") ? read_image_dimensions(data) : undefined;
|
|
35
|
+
const tokens = dimensions ? estimate_image_tokens(dimensions) : Math.ceil(data.length / 3) * 4;
|
|
36
|
+
const existing_bytes = (context.messages || []).reduce((sum, message) => sum + message.content.reduce((total, item) => {
|
|
37
|
+
for (const part of item.type === "tool_result" ? item.content : [item]) {
|
|
38
|
+
if (part.type === "file" && part.source.type === "base64")
|
|
39
|
+
total += Buffer.byteLength(part.source.data, "base64");
|
|
40
|
+
}
|
|
41
|
+
return total;
|
|
42
|
+
}, 0), 0);
|
|
43
|
+
if ((context.reserve_media && !context.reserve_media(data.length, existing_bytes, tokens)) || data.length + existing_bytes > max_request_media_bytes)
|
|
44
|
+
throw new Error("Remote media exceeds the current request budget");
|
|
45
|
+
// 版本绑定的是已经读取的内容快照;Session 持久化后会转换为该本地附件的真实版本。
|
|
46
|
+
messages.push({ role: "user", parts: [{ type: "file", url: `data:${type};base64,${data.toString("base64")}`, media_type: type, filename: path.posix.basename(output.file_path), media_version: `sha256:${crypto.createHash("sha256").update(data).digest("hex")}` }] });
|
|
47
|
+
return { output: { ...output, type: type.startsWith("image/") ? "image" : "binary", mime_type: type }, messages };
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=SshMedia.js.map
|
|
@@ -0,0 +1 @@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AC,QAAQ,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,EAAE,SAAS,EAAE,IAAI,EAAE,EAAE,QAAQ,EAAE,CAAC;AACtH,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type AgentTool } from "@downcity/type";
|
|
2
|
+
import type { WorkspaceShell, WorkspaceShellSandboxCommandInput, WorkspaceShellSandboxCommandResult } from "@downcity/type/shell";
|
|
3
|
+
import type { SshConnection } from "./SshConnection.js";
|
|
4
|
+
import { SshFileSystem } from "./SshFileSystem.js";
|
|
5
|
+
/** 远程 Shell,不把宿主 env 或本地凭据传给远端进程。 */
|
|
6
|
+
export declare class SshShell implements WorkspaceShell {
|
|
7
|
+
private readonly connection;
|
|
8
|
+
private readonly files;
|
|
9
|
+
readonly tools: Record<string, AgentTool>;
|
|
10
|
+
private env;
|
|
11
|
+
private readonly sessions;
|
|
12
|
+
constructor(connection: SshConnection, files: SshFileSystem);
|
|
13
|
+
bind(): void;
|
|
14
|
+
set_env(env: Readonly<Record<string, string>>): void;
|
|
15
|
+
describe_sandbox(): null;
|
|
16
|
+
reset_sandbox(): Promise<void>;
|
|
17
|
+
has_active_sessions(): boolean;
|
|
18
|
+
/** 只投影活动后台会话,不包含一次性命令,不读取输出或创建远程连接。 */
|
|
19
|
+
get_active_sessions(): import("@downcity/type/shell").WorkspaceActiveShell[];
|
|
20
|
+
private owner_id;
|
|
21
|
+
private start;
|
|
22
|
+
/** 只等待下一块输出或退出;计时器与监听在任何完成分支释放。 */
|
|
23
|
+
private wait_output;
|
|
24
|
+
private snapshot;
|
|
25
|
+
/** 适配既有 Shell SDK 入口;结果明确标记 ssh 后端,始终使用远程用户权限。 */
|
|
26
|
+
run_sandbox_command(input: WorkspaceShellSandboxCommandInput): Promise<WorkspaceShellSandboxCommandResult>;
|
|
27
|
+
dispose(): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=SshShell.d.ts.map
|
|
@@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