aicomputer 0.1.22 → 0.2.0

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.
@@ -1,184 +0,0 @@
1
- // src/lib/mount-config.ts
2
- import {
3
- existsSync,
4
- mkdirSync,
5
- readFileSync,
6
- renameSync,
7
- rmSync,
8
- writeFileSync
9
- } from "fs";
10
- import { homedir } from "os";
11
- import { join } from "path";
12
- var MOUNT_SERVICE_LABEL = "ai.agentcomputer.mount";
13
- function getDefaultMountRoot() {
14
- return join(homedir(), "agentcomputer");
15
- }
16
- function getMountStateDir() {
17
- return join(homedir(), ".computer", "mount");
18
- }
19
- function getMountPaths(rootPath = getDefaultMountRoot()) {
20
- const stateDir = getMountStateDir();
21
- return {
22
- stateDir,
23
- rootPath,
24
- configPath: join(stateDir, "config.json"),
25
- statusPath: join(stateDir, "status.json"),
26
- socketPath: join(stateDir, "control.sock"),
27
- controllerLockPath: join(stateDir, "controller.lock.json"),
28
- handlesDir: join(stateDir, "handles"),
29
- sshToolsDir: join(stateDir, "ssh-tools"),
30
- staleDir: join(stateDir, "stale")
31
- };
32
- }
33
- function getMountHandlePaths(handle, rootPath = getDefaultMountRoot()) {
34
- const paths = getMountPaths(rootPath);
35
- const stateDir = join(paths.handlesDir, handle);
36
- return {
37
- stateDir,
38
- metaPath: join(stateDir, "meta.json"),
39
- sshToolsDir: join(stateDir, "ssh-tools")
40
- };
41
- }
42
- function defaultMountServiceConfig() {
43
- return {
44
- alias: "agentcomputer.ai",
45
- host: "ssh.agentcomputer.ai",
46
- port: 443,
47
- rootPath: getDefaultMountRoot(),
48
- pollIntervalMs: 5e3,
49
- connectTimeoutSeconds: 5
50
- };
51
- }
52
- function ensureMountDirectories(paths) {
53
- for (const directory of [
54
- paths.stateDir,
55
- paths.handlesDir,
56
- paths.sshToolsDir,
57
- paths.staleDir
58
- ]) {
59
- if (!existsSync(directory)) {
60
- mkdirSync(directory, { recursive: true, mode: 448 });
61
- }
62
- }
63
- }
64
- function ensureHandleDirectories(handle, rootPath) {
65
- const handlePaths = getMountHandlePaths(handle, rootPath);
66
- for (const directory of [handlePaths.stateDir, handlePaths.sshToolsDir]) {
67
- if (!existsSync(directory)) {
68
- mkdirSync(directory, { recursive: true, mode: 448 });
69
- }
70
- }
71
- return handlePaths;
72
- }
73
- function readMountConfig() {
74
- const defaults = defaultMountServiceConfig();
75
- const paths = getMountPaths(defaults.rootPath);
76
- if (!existsSync(paths.configPath)) {
77
- return null;
78
- }
79
- try {
80
- const raw = JSON.parse(readFileSync(paths.configPath, "utf8"));
81
- return {
82
- alias: typeof raw.alias === "string" && raw.alias.trim() ? raw.alias.trim() : defaults.alias,
83
- host: typeof raw.host === "string" && raw.host.trim() ? raw.host.trim() : defaults.host,
84
- port: typeof raw.port === "number" ? raw.port : defaults.port,
85
- rootPath: typeof raw.rootPath === "string" && raw.rootPath.trim() ? raw.rootPath.trim() : defaults.rootPath,
86
- pollIntervalMs: typeof raw.pollIntervalMs === "number" && raw.pollIntervalMs > 0 ? raw.pollIntervalMs : defaults.pollIntervalMs,
87
- connectTimeoutSeconds: typeof raw.connectTimeoutSeconds === "number" && raw.connectTimeoutSeconds > 0 ? raw.connectTimeoutSeconds : defaults.connectTimeoutSeconds
88
- };
89
- } catch {
90
- return null;
91
- }
92
- }
93
- function writeMountConfig(config) {
94
- const paths = getMountPaths(config.rootPath);
95
- ensureMountDirectories(paths);
96
- writeJSONFile(paths.configPath, config);
97
- }
98
- function readMountStatusSnapshot(rootPath = getDefaultMountRoot()) {
99
- const paths = getMountPaths(rootPath);
100
- if (!existsSync(paths.statusPath)) {
101
- return null;
102
- }
103
- try {
104
- return JSON.parse(readFileSync(paths.statusPath, "utf8"));
105
- } catch {
106
- return null;
107
- }
108
- }
109
- function writeMountStatusSnapshot(snapshot, rootPath = getDefaultMountRoot()) {
110
- const paths = getMountPaths(rootPath);
111
- ensureMountDirectories(paths);
112
- writeJSONFile(paths.statusPath, snapshot);
113
- }
114
- function readMountControllerLock(rootPath = getDefaultMountRoot()) {
115
- const paths = getMountPaths(rootPath);
116
- if (!existsSync(paths.controllerLockPath)) {
117
- return null;
118
- }
119
- try {
120
- return JSON.parse(readFileSync(paths.controllerLockPath, "utf8"));
121
- } catch {
122
- return null;
123
- }
124
- }
125
- function writeMountControllerLock(lock, rootPath = getDefaultMountRoot()) {
126
- const paths = getMountPaths(rootPath);
127
- ensureMountDirectories(paths);
128
- writeJSONFile(paths.controllerLockPath, lock);
129
- }
130
- function removeMountControllerLock(rootPath = getDefaultMountRoot()) {
131
- const paths = getMountPaths(rootPath);
132
- try {
133
- rmSync(paths.controllerLockPath, { force: true });
134
- } catch {
135
- }
136
- }
137
- function readMountHandleMeta(handle, rootPath = getDefaultMountRoot()) {
138
- const paths = getMountHandlePaths(handle, rootPath);
139
- if (!existsSync(paths.metaPath)) {
140
- return null;
141
- }
142
- try {
143
- return JSON.parse(readFileSync(paths.metaPath, "utf8"));
144
- } catch {
145
- return null;
146
- }
147
- }
148
- function writeMountHandleMeta(handle, meta, rootPath = getDefaultMountRoot()) {
149
- const paths = ensureHandleDirectories(handle, rootPath);
150
- writeJSONFile(paths.metaPath, meta);
151
- }
152
- function removeMountHandleState(handle, rootPath = getDefaultMountRoot()) {
153
- const paths = getMountHandlePaths(handle, rootPath);
154
- try {
155
- rmSync(paths.stateDir, { recursive: true, force: true });
156
- } catch {
157
- }
158
- }
159
- function writeJSONFile(path, value) {
160
- const tempFile = `${path}.${process.pid}.tmp`;
161
- writeFileSync(tempFile, JSON.stringify(value, null, 2), { mode: 384 });
162
- renameSync(tempFile, path);
163
- }
164
-
165
- export {
166
- MOUNT_SERVICE_LABEL,
167
- getDefaultMountRoot,
168
- getMountStateDir,
169
- getMountPaths,
170
- getMountHandlePaths,
171
- defaultMountServiceConfig,
172
- ensureMountDirectories,
173
- ensureHandleDirectories,
174
- readMountConfig,
175
- writeMountConfig,
176
- readMountStatusSnapshot,
177
- writeMountStatusSnapshot,
178
- readMountControllerLock,
179
- writeMountControllerLock,
180
- removeMountControllerLock,
181
- readMountHandleMeta,
182
- writeMountHandleMeta,
183
- removeMountHandleState
184
- };
@@ -1,242 +0,0 @@
1
- import {
2
- AUTOSSH_SSH_PATH_ENV,
3
- ensureAutosshCommandPath,
4
- resolveAutosshSSHCommandPath
5
- } from "./chunk-3ZF7JRBW.js";
6
- import {
7
- api,
8
- getConnectionInfo,
9
- resolveComputer
10
- } from "./chunk-E7QD4MHI.js";
11
-
12
- // src/lib/ssh-access.ts
13
- import { spawn } from "child_process";
14
- import { basename as basename2 } from "path";
15
-
16
- // src/lib/ssh-keys.ts
17
- import { basename } from "path";
18
- import { homedir } from "os";
19
- import { readFile, mkdir } from "fs/promises";
20
- import { execFileSync } from "child_process";
21
- import { existsSync } from "fs";
22
- var DEFAULT_PUBLIC_KEY_PATHS = [
23
- `${homedir()}/.ssh/id_ed25519.pub`,
24
- `${homedir()}/.ssh/id_ecdsa.pub`,
25
- `${homedir()}/.ssh/id_rsa.pub`
26
- ];
27
- async function ensureDefaultSSHKeyRegistered() {
28
- for (const path of DEFAULT_PUBLIC_KEY_PATHS) {
29
- try {
30
- const publicKey2 = (await readFile(path, "utf8")).trim();
31
- if (!publicKey2) {
32
- continue;
33
- }
34
- const key2 = await api("/v1/ssh-keys", {
35
- method: "POST",
36
- body: JSON.stringify({
37
- name: basename(path),
38
- public_key: publicKey2
39
- })
40
- });
41
- return {
42
- key: key2,
43
- publicKeyPath: path,
44
- privateKeyPath: path.replace(/\.pub$/, "")
45
- };
46
- } catch (error) {
47
- if (error?.code === "ENOENT") {
48
- continue;
49
- }
50
- throw error;
51
- }
52
- }
53
- const generated = await generateSSHKey();
54
- const publicKey = (await readFile(generated.publicKeyPath, "utf8")).trim();
55
- const key = await api("/v1/ssh-keys", {
56
- method: "POST",
57
- body: JSON.stringify({
58
- name: basename(generated.publicKeyPath),
59
- public_key: publicKey
60
- })
61
- });
62
- return {
63
- key,
64
- publicKeyPath: generated.publicKeyPath,
65
- privateKeyPath: generated.privateKeyPath
66
- };
67
- }
68
- async function generateSSHKey() {
69
- const sshDir = `${homedir()}/.ssh`;
70
- if (!existsSync(sshDir)) {
71
- await mkdir(sshDir, { mode: 448 });
72
- }
73
- const privateKeyPath = `${sshDir}/id_ed25519`;
74
- const publicKeyPath = `${privateKeyPath}.pub`;
75
- console.log("No SSH key found \u2014 generating one at", publicKeyPath);
76
- execFileSync("ssh-keygen", ["-t", "ed25519", "-f", privateKeyPath, "-N", ""], {
77
- stdio: "inherit"
78
- });
79
- return { publicKeyPath, privateKeyPath };
80
- }
81
-
82
- // src/lib/ssh-access.ts
83
- var AUTOSSH_BINARY = "autossh";
84
- var AUTOSSH_MONITOR_PORT = "0";
85
- var TMUX_SESSION_NAME = "agentcomputer";
86
- var TMUX_INCOMPATIBLE_SSH_ARGS = /* @__PURE__ */ new Set(["-N", "-T", "-f"]);
87
- var SSH_SERVER_ALIVE_INTERVAL_SECONDS = 30;
88
- var SSH_SERVER_ALIVE_COUNT_MAX = 3;
89
- async function prepareSSHConnection(computer, options = {}) {
90
- const registered = await ensureDefaultSSHKeyRegistered();
91
- const info = await getConnectionInfo(computer.id);
92
- if (!info.connection.ssh_available) {
93
- throw new Error("SSH is not available for this computer");
94
- }
95
- const launchPlan = buildSSHLaunchPlan({
96
- user: info.connection.ssh_user,
97
- host: info.connection.ssh_host,
98
- port: info.connection.ssh_port,
99
- identityFilePath: registered.privateKeyPath,
100
- commandPath: await ensureAutosshCommandPath(),
101
- sshCommandPath: resolveAutosshSSHCommandPath(),
102
- extraArgs: options.extraArgs,
103
- tmux: options.tmux
104
- });
105
- return {
106
- computer,
107
- ...launchPlan
108
- };
109
- }
110
- async function prepareSSHConnectionByIdentifier(identifier, options = {}) {
111
- const computer = await resolveComputer(identifier);
112
- return prepareSSHConnection(computer, options);
113
- }
114
- async function openSSHConnection(connection) {
115
- await new Promise((resolve, reject) => {
116
- const child = spawn(connection.commandPath, connection.args, {
117
- env: {
118
- ...process.env,
119
- ...connection.env
120
- },
121
- stdio: "inherit"
122
- });
123
- child.on("error", reject);
124
- child.on("exit", (code) => {
125
- if (code === 0) {
126
- resolve();
127
- return;
128
- }
129
- reject(
130
- new Error(
131
- `${basename2(connection.commandPath)} exited with code ${code ?? 1}`
132
- )
133
- );
134
- });
135
- });
136
- }
137
- function buildSSHLaunchPlan(input) {
138
- const user = input.user.trim();
139
- const host = input.host.trim();
140
- const port = input.port;
141
- const identityFilePath = input.identityFilePath.trim();
142
- const extraArgs = [...input.extraArgs ?? []];
143
- const tmux = input.tmux === true;
144
- if (!user || !host) {
145
- throw new Error("ssh is unavailable");
146
- }
147
- if (!identityFilePath) {
148
- throw new Error("ssh identity file is required");
149
- }
150
- if (tmux) {
151
- validateTmuxSSHArgs(extraArgs);
152
- }
153
- const commandPath = input.commandPath?.trim() || AUTOSSH_BINARY;
154
- const env = {};
155
- if (input.sshCommandPath?.trim()) {
156
- env[AUTOSSH_SSH_PATH_ENV] = input.sshCommandPath.trim();
157
- }
158
- return {
159
- commandPath,
160
- command: formatSSHCommand(user, host, port, extraArgs, { tmux }),
161
- args: [
162
- "-M",
163
- AUTOSSH_MONITOR_PORT,
164
- ...getSSHResilienceOptionArgs(),
165
- "-i",
166
- identityFilePath,
167
- "-p",
168
- String(port),
169
- ...tmux ? ["-t"] : [],
170
- ...extraArgs,
171
- `${user}@${host}`,
172
- ...tmux ? getTmuxRemoteCommandArgs() : []
173
- ],
174
- env
175
- };
176
- }
177
- function getSSHResilienceOptionArgs() {
178
- return [
179
- "-o",
180
- `ServerAliveInterval=${SSH_SERVER_ALIVE_INTERVAL_SECONDS}`,
181
- "-o",
182
- `ServerAliveCountMax=${SSH_SERVER_ALIVE_COUNT_MAX}`,
183
- "-o",
184
- "ExitOnForwardFailure=yes"
185
- ];
186
- }
187
- function getSSHResilienceConfigLines() {
188
- return [
189
- `ServerAliveInterval ${SSH_SERVER_ALIVE_INTERVAL_SECONDS}`,
190
- `ServerAliveCountMax ${SSH_SERVER_ALIVE_COUNT_MAX}`,
191
- "ExitOnForwardFailure yes"
192
- ];
193
- }
194
- function formatSSHCommand(user, host, port, extraArgs = [], options = {}) {
195
- if (!user.trim() || !host.trim()) {
196
- return "ssh unavailable";
197
- }
198
- const parts = [AUTOSSH_BINARY, "-M", AUTOSSH_MONITOR_PORT];
199
- if (port > 0 && port !== 22) {
200
- parts.push("-p", String(port));
201
- }
202
- if (options.tmux) {
203
- parts.push("-t");
204
- }
205
- parts.push(...extraArgs, `${user}@${host}`);
206
- if (options.tmux) {
207
- parts.push(...getTmuxRemoteCommandArgs());
208
- }
209
- return parts.map(formatShellDisplayArg).join(" ");
210
- }
211
- function getTmuxRemoteCommandArgs() {
212
- return ["tmux", "new-session", "-A", "-s", TMUX_SESSION_NAME];
213
- }
214
- function validateTmuxSSHArgs(extraArgs) {
215
- const incompatibleArg = extraArgs.find(
216
- (arg) => TMUX_INCOMPATIBLE_SSH_ARGS.has(arg)
217
- );
218
- if (incompatibleArg) {
219
- throw new Error(
220
- `--tmux cannot be combined with ${incompatibleArg} because tmux attach mode requires an interactive shell.`
221
- );
222
- }
223
- }
224
- function formatShellDisplayArg(value) {
225
- if (/^[A-Za-z0-9_@%+=:,./-]+$/.test(value)) {
226
- return value;
227
- }
228
- return JSON.stringify(value);
229
- }
230
-
231
- export {
232
- ensureDefaultSSHKeyRegistered,
233
- SSH_SERVER_ALIVE_INTERVAL_SECONDS,
234
- SSH_SERVER_ALIVE_COUNT_MAX,
235
- prepareSSHConnection,
236
- prepareSSHConnectionByIdentifier,
237
- openSSHConnection,
238
- buildSSHLaunchPlan,
239
- getSSHResilienceOptionArgs,
240
- getSSHResilienceConfigLines,
241
- formatSSHCommand
242
- };